blob: 37afb46064bd3d737efbe640aaa3561d52bdce6a [file] [log] [blame]
nstraze3219812003-07-14 17:06:37 +00001/*
2 * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved.
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +08003 * Copyright (c) 2012 Wanlong Gao <gaowanlong@cn.fujitsu.com>
nstraze3219812003-07-14 17:06:37 +00004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080015 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
nstraze3219812003-07-14 17:06:37 +000016 *
17 */
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080018 /*
Wanlong Gao354ebb42012-12-07 10:10:04 +080019 * Verify that,
20 * clone(2) returns -1 and sets errno to EINVAL if
21 * child stack is set to a zero value(NULL)
22 */
robbiewd34d5812005-07-11 22:28:09 +000023
24#if defined UCLINUX && !__THROW
25/* workaround for libc bug */
26#define __THROW
27#endif
28
nstraze3219812003-07-14 17:06:37 +000029#include <sched.h>
30#include <errno.h>
31#include <sys/wait.h>
32#include "test.h"
33#include "usctest.h"
nstraz882ee2d2003-07-16 17:40:32 +000034#include "clone_platform.h"
nstraze3219812003-07-14 17:06:37 +000035
36static void cleanup(void);
37static void setup(void);
38static int child_fn();
39
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080040static void *child_stack;
nstraze3219812003-07-14 17:06:37 +000041
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080042static int exp_enos[] = { EINVAL, 0 };
43
nstraze3219812003-07-14 17:06:37 +000044static struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000045 int (*child_fn) ();
nstraz882ee2d2003-07-16 17:40:32 +000046 void **child_stack;
nstraze3219812003-07-14 17:06:37 +000047 int exp_errno;
48 char err_desc[10];
49} test_cases[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080050 {
51child_fn, NULL, EINVAL, "EINVAL"},};
nstraze3219812003-07-14 17:06:37 +000052
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080053char *TCID = "clone04";
nstraze3219812003-07-14 17:06:37 +000054int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
55
subrata_modak56207ce2009-03-23 13:35:39 +000056int main(int ac, char **av)
nstraze3219812003-07-14 17:06:37 +000057{
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080058 int lc, ind;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020059 char *msg;
nstraze3219812003-07-14 17:06:37 +000060 void *test_stack;
61
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080062 msg = parse_opts(ac, av, NULL, NULL);
63 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080064 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
nstraze3219812003-07-14 17:06:37 +000065
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080066 setup();
nstraze3219812003-07-14 17:06:37 +000067
68 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080069 tst_count = 0;
nstraze3219812003-07-14 17:06:37 +000070
71 for (ind = 0; ind < TST_TOTAL; ind++) {
nstraz882ee2d2003-07-16 17:40:32 +000072 if (test_cases[ind].child_stack == NULL) {
73 test_stack = (void *)NULL;
74 } else if (*test_cases[ind].child_stack == NULL) {
75 tst_resm(TWARN, "Can not allocate stack for"
subrata_modak56207ce2009-03-23 13:35:39 +000076 "child, skipping test case");
nstraz882ee2d2003-07-16 17:40:32 +000077 continue;
78 } else {
nstraze3219812003-07-14 17:06:37 +000079 test_stack = child_stack;
nstraze3219812003-07-14 17:06:37 +000080 }
subrata_modak56207ce2009-03-23 13:35:39 +000081
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080082 TEST(ltp_clone(0, test_cases[ind].child_fn, NULL,
83 CHILD_STACK_SIZE, test_stack));
subrata_modakbdbaec52009-02-26 12:14:51 +000084
nstraze3219812003-07-14 17:06:37 +000085 if ((TEST_RETURN == -1) &&
86 (TEST_ERRNO == test_cases[ind].exp_errno)) {
87 tst_resm(TPASS, "expected failure; Got %s",
subrata_modak56207ce2009-03-23 13:35:39 +000088 test_cases[ind].err_desc);
nstraze3219812003-07-14 17:06:37 +000089 } else {
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080090 tst_resm(TFAIL | TTERRNO,
91 "Call failed to produce expected error; "
vapier0874e6f2009-08-28 12:07:53 +000092 "expected errno %d and result -1; got result %ld",
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080093 test_cases[ind].exp_errno,
94 TEST_RETURN);
nstraze3219812003-07-14 17:06:37 +000095 }
96 TEST_ERROR_LOG(TEST_ERRNO);
97 }
98 }
99
nstraze3219812003-07-14 17:06:37 +0000100 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800101 tst_exit();
nstraze3219812003-07-14 17:06:37 +0000102}
103
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800104static void setup(void)
nstraze3219812003-07-14 17:06:37 +0000105{
nstraze3219812003-07-14 17:06:37 +0000106 tst_sig(NOFORK, DEF_HANDLER, cleanup);
nstraze3219812003-07-14 17:06:37 +0000107 /* Set up the expected error numbers for -e option */
108 TEST_EXP_ENOS(exp_enos);
nstraze3219812003-07-14 17:06:37 +0000109 TEST_PAUSE;
110
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800111 child_stack = malloc(CHILD_STACK_SIZE);
nstraze3219812003-07-14 17:06:37 +0000112}
113
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800114static void cleanup(void)
nstraze3219812003-07-14 17:06:37 +0000115{
nstraze3219812003-07-14 17:06:37 +0000116 TEST_CLEANUP;
nstraz882ee2d2003-07-16 17:40:32 +0000117 free(child_stack);
nstraze3219812003-07-14 17:06:37 +0000118}
119
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800120static int child_fn()
nstraze3219812003-07-14 17:06:37 +0000121{
robbiewb8bd30c2003-07-31 20:41:11 +0000122 exit(1);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700123}