blob: 7994d0fa5d90e6a455358dba26dcd1cdfed86184 [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"
nstraz882ee2d2003-07-16 17:40:32 +000033#include "clone_platform.h"
nstraze3219812003-07-14 17:06:37 +000034
35static void cleanup(void);
36static void setup(void);
37static int child_fn();
38
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080039static void *child_stack;
nstraze3219812003-07-14 17:06:37 +000040
nstraze3219812003-07-14 17:06:37 +000041static struct test_case_t {
subrata_modak56207ce2009-03-23 13:35:39 +000042 int (*child_fn) ();
nstraz882ee2d2003-07-16 17:40:32 +000043 void **child_stack;
nstraze3219812003-07-14 17:06:37 +000044 int exp_errno;
45 char err_desc[10];
46} test_cases[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080047 {
48child_fn, NULL, EINVAL, "EINVAL"},};
nstraze3219812003-07-14 17:06:37 +000049
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080050char *TCID = "clone04";
nstraze3219812003-07-14 17:06:37 +000051int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
52
subrata_modak56207ce2009-03-23 13:35:39 +000053int main(int ac, char **av)
nstraze3219812003-07-14 17:06:37 +000054{
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080055 int lc, ind;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020056 const char *msg;
nstraze3219812003-07-14 17:06:37 +000057 void *test_stack;
58
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080059 msg = parse_opts(ac, av, NULL, NULL);
60 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080061 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
nstraze3219812003-07-14 17:06:37 +000062
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080063 setup();
nstraze3219812003-07-14 17:06:37 +000064
65 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080066 tst_count = 0;
nstraze3219812003-07-14 17:06:37 +000067
68 for (ind = 0; ind < TST_TOTAL; ind++) {
nstraz882ee2d2003-07-16 17:40:32 +000069 if (test_cases[ind].child_stack == NULL) {
Cyril Hrubiscf0d6262014-09-23 14:03:31 +020070 test_stack = NULL;
nstraz882ee2d2003-07-16 17:40:32 +000071 } else if (*test_cases[ind].child_stack == NULL) {
72 tst_resm(TWARN, "Can not allocate stack for"
subrata_modak56207ce2009-03-23 13:35:39 +000073 "child, skipping test case");
nstraz882ee2d2003-07-16 17:40:32 +000074 continue;
75 } else {
nstraze3219812003-07-14 17:06:37 +000076 test_stack = child_stack;
nstraze3219812003-07-14 17:06:37 +000077 }
subrata_modak56207ce2009-03-23 13:35:39 +000078
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080079 TEST(ltp_clone(0, test_cases[ind].child_fn, NULL,
80 CHILD_STACK_SIZE, test_stack));
subrata_modakbdbaec52009-02-26 12:14:51 +000081
nstraze3219812003-07-14 17:06:37 +000082 if ((TEST_RETURN == -1) &&
83 (TEST_ERRNO == test_cases[ind].exp_errno)) {
84 tst_resm(TPASS, "expected failure; Got %s",
subrata_modak56207ce2009-03-23 13:35:39 +000085 test_cases[ind].err_desc);
nstraze3219812003-07-14 17:06:37 +000086 } else {
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080087 tst_resm(TFAIL | TTERRNO,
88 "Call failed to produce expected error; "
vapier0874e6f2009-08-28 12:07:53 +000089 "expected errno %d and result -1; got result %ld",
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +080090 test_cases[ind].exp_errno,
91 TEST_RETURN);
nstraze3219812003-07-14 17:06:37 +000092 }
nstraze3219812003-07-14 17:06:37 +000093 }
94 }
95
nstraze3219812003-07-14 17:06:37 +000096 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -080097 tst_exit();
nstraze3219812003-07-14 17:06:37 +000098}
99
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800100static void setup(void)
nstraze3219812003-07-14 17:06:37 +0000101{
nstraze3219812003-07-14 17:06:37 +0000102 tst_sig(NOFORK, DEF_HANDLER, cleanup);
nstraze3219812003-07-14 17:06:37 +0000103 TEST_PAUSE;
104
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800105 child_stack = malloc(CHILD_STACK_SIZE);
nstraze3219812003-07-14 17:06:37 +0000106}
107
Wanlong Gao3dd0c6a2012-12-04 15:16:50 +0800108static void cleanup(void)
nstraze3219812003-07-14 17:06:37 +0000109{
nstraz882ee2d2003-07-16 17:40:32 +0000110 free(child_stack);
nstraze3219812003-07-14 17:06:37 +0000111}
112
Mike Frysingerc57fba52014-04-09 18:56:30 -0400113static int child_fn(void)
nstraze3219812003-07-14 17:06:37 +0000114{
robbiewb8bd30c2003-07-31 20:41:11 +0000115 exit(1);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700116}