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" |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 33 | #include "clone_platform.h" |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 34 | |
| 35 | static void cleanup(void); |
| 36 | static void setup(void); |
| 37 | static int child_fn(); |
| 38 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 39 | static void *child_stack; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 40 | |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 41 | static struct test_case_t { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 42 | int (*child_fn) (); |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 43 | void **child_stack; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 44 | int exp_errno; |
| 45 | char err_desc[10]; |
| 46 | } test_cases[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 47 | { |
| 48 | child_fn, NULL, EINVAL, "EINVAL"},}; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 49 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 50 | char *TCID = "clone04"; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 51 | int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]); |
| 52 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 53 | int main(int ac, char **av) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 54 | { |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 55 | int lc, ind; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 56 | const char *msg; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 57 | void *test_stack; |
| 58 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 59 | msg = parse_opts(ac, av, NULL, NULL); |
| 60 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 61 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 62 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 63 | setup(); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 64 | |
| 65 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 66 | tst_count = 0; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 67 | |
| 68 | for (ind = 0; ind < TST_TOTAL; ind++) { |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 69 | if (test_cases[ind].child_stack == NULL) { |
Cyril Hrubis | cf0d626 | 2014-09-23 14:03:31 +0200 | [diff] [blame] | 70 | test_stack = NULL; |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 71 | } else if (*test_cases[ind].child_stack == NULL) { |
| 72 | tst_resm(TWARN, "Can not allocate stack for" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | "child, skipping test case"); |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 74 | continue; |
| 75 | } else { |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 76 | test_stack = child_stack; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 77 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 78 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 79 | TEST(ltp_clone(0, test_cases[ind].child_fn, NULL, |
| 80 | CHILD_STACK_SIZE, test_stack)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 81 | |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 82 | if ((TEST_RETURN == -1) && |
| 83 | (TEST_ERRNO == test_cases[ind].exp_errno)) { |
| 84 | tst_resm(TPASS, "expected failure; Got %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | test_cases[ind].err_desc); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 86 | } else { |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 87 | tst_resm(TFAIL | TTERRNO, |
| 88 | "Call failed to produce expected error; " |
vapier | 0874e6f | 2009-08-28 12:07:53 +0000 | [diff] [blame] | 89 | "expected errno %d and result -1; got result %ld", |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 90 | test_cases[ind].exp_errno, |
| 91 | TEST_RETURN); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 92 | } |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 96 | cleanup(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 97 | tst_exit(); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 100 | static void setup(void) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 101 | { |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 102 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 103 | TEST_PAUSE; |
| 104 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 105 | child_stack = malloc(CHILD_STACK_SIZE); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 108 | static void cleanup(void) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 109 | { |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 110 | free(child_stack); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 113 | static int child_fn(void) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 114 | { |
robbiew | b8bd30c | 2003-07-31 20:41:11 +0000 | [diff] [blame] | 115 | exit(1); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 116 | } |