nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 3 | * Copyright (c) 2012 Wanlong Gao <gaowanlong@cn.fujitsu.com> |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 4 | * |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 16 | * |
| 17 | */ |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 18 | /* |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 19 | * Verify that, |
| 20 | * clone(2) returns -1 and sets errno to EINVAL if |
| 21 | * child stack is set to a zero value(NULL) |
| 22 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 23 | |
| 24 | #if defined UCLINUX && !__THROW |
| 25 | /* workaround for libc bug */ |
| 26 | #define __THROW |
| 27 | #endif |
| 28 | |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 29 | #include <sched.h> |
| 30 | #include <errno.h> |
| 31 | #include <sys/wait.h> |
| 32 | #include "test.h" |
| 33 | #include "usctest.h" |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 34 | #include "clone_platform.h" |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 35 | |
| 36 | static void cleanup(void); |
| 37 | static void setup(void); |
| 38 | static int child_fn(); |
| 39 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 40 | static void *child_stack; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 41 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 42 | static int exp_enos[] = { EINVAL, 0 }; |
| 43 | |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 44 | static struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 45 | int (*child_fn) (); |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 46 | void **child_stack; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 47 | int exp_errno; |
| 48 | char err_desc[10]; |
| 49 | } test_cases[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 50 | { |
| 51 | child_fn, NULL, EINVAL, "EINVAL"},}; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 52 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 53 | char *TCID = "clone04"; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 54 | int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]); |
| 55 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 56 | int main(int ac, char **av) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 57 | { |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 58 | int lc, ind; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 59 | char *msg; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 60 | void *test_stack; |
| 61 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 62 | msg = parse_opts(ac, av, NULL, NULL); |
| 63 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 64 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 65 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 66 | setup(); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 67 | |
| 68 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 69 | tst_count = 0; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 70 | |
| 71 | for (ind = 0; ind < TST_TOTAL; ind++) { |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 72 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | "child, skipping test case"); |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 77 | continue; |
| 78 | } else { |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 79 | test_stack = child_stack; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 80 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 82 | TEST(ltp_clone(0, test_cases[ind].child_fn, NULL, |
| 83 | CHILD_STACK_SIZE, test_stack)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 84 | |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 85 | if ((TEST_RETURN == -1) && |
| 86 | (TEST_ERRNO == test_cases[ind].exp_errno)) { |
| 87 | tst_resm(TPASS, "expected failure; Got %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | test_cases[ind].err_desc); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 89 | } else { |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 90 | tst_resm(TFAIL | TTERRNO, |
| 91 | "Call failed to produce expected error; " |
vapier | 0874e6f | 2009-08-28 12:07:53 +0000 | [diff] [blame] | 92 | "expected errno %d and result -1; got result %ld", |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 93 | test_cases[ind].exp_errno, |
| 94 | TEST_RETURN); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 95 | } |
| 96 | TEST_ERROR_LOG(TEST_ERRNO); |
| 97 | } |
| 98 | } |
| 99 | |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 100 | cleanup(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 101 | tst_exit(); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 104 | static void setup(void) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 105 | { |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 106 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 107 | /* Set up the expected error numbers for -e option */ |
| 108 | TEST_EXP_ENOS(exp_enos); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 109 | TEST_PAUSE; |
| 110 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 111 | child_stack = malloc(CHILD_STACK_SIZE); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 114 | static void cleanup(void) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 115 | { |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 116 | TEST_CLEANUP; |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 117 | free(child_stack); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 120 | static int child_fn() |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 121 | { |
robbiew | b8bd30c | 2003-07-31 20:41:11 +0000 | [diff] [blame] | 122 | exit(1); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 123 | } |