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> |
Cyril Hrubis | b6a5657 | 2012-12-11 20:58:12 +0100 | [diff] [blame] | 4 | * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz> |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of version 2 of the GNU General Public License as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it would be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 15 | * with this program; if not, write the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 17 | * |
| 18 | */ |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 19 | |
Cyril Hrubis | c91af2f | 2012-11-28 18:02:01 +0100 | [diff] [blame] | 20 | /* |
| 21 | * Call clone() with CLONE_VFORK flag set. verify that |
| 22 | * execution of parent is suspended until child finishes |
| 23 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 24 | |
Cyril Hrubis | 019ed6c | 2011-08-26 12:59:32 +0200 | [diff] [blame] | 25 | #define _GNU_SOURCE |
| 26 | |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | #include <sched.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include "test.h" |
nstraz | 882ee2d | 2003-07-16 17:40:32 +0000 | [diff] [blame] | 31 | #include "clone_platform.h" |
robbiew | 666adf1 | 2003-10-01 16:10:49 +0000 | [diff] [blame] | 32 | |
Cyril Hrubis | c91af2f | 2012-11-28 18:02:01 +0100 | [diff] [blame] | 33 | char *TCID = "clone05"; |
| 34 | int TST_TOTAL = 1; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 35 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 36 | static void setup(void); |
| 37 | static void cleanup(void); |
Cyril Hrubis | c91af2f | 2012-11-28 18:02:01 +0100 | [diff] [blame] | 38 | static int child_fn(void *); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 39 | |
Cyril Hrubis | c91af2f | 2012-11-28 18:02:01 +0100 | [diff] [blame] | 40 | static int child_exited = 0; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 41 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 42 | int main(int ac, char **av) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 43 | { |
| 44 | |
Jan Stancek | 443d16e | 2012-12-03 13:19:57 +0100 | [diff] [blame] | 45 | int lc, status; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 46 | const char *msg; |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 47 | void *child_stack; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 48 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 49 | msg = parse_opts(ac, av, NULL, NULL); |
Cyril Hrubis | b6a5657 | 2012-12-11 20:58:12 +0100 | [diff] [blame] | 50 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 51 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 52 | |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 53 | setup(); |
| 54 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 55 | child_stack = malloc(CHILD_STACK_SIZE); |
| 56 | if (child_stack == NULL) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 57 | tst_brkm(TBROK, cleanup, "Cannot allocate stack for child"); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 58 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 60 | tst_count = 0; |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 61 | |
Wanlong Gao | 79b7729 | 2012-12-12 10:38:05 +0800 | [diff] [blame] | 62 | TEST(ltp_clone(CLONE_VM | CLONE_VFORK | SIGCHLD, child_fn, NULL, |
Cyril Hrubis | c91af2f | 2012-11-28 18:02:01 +0100 | [diff] [blame] | 63 | CHILD_STACK_SIZE, child_stack)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 64 | |
Cyril Hrubis | b6a5657 | 2012-12-11 20:58:12 +0100 | [diff] [blame] | 65 | if ((TEST_RETURN != -1) && (child_exited)) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 66 | tst_resm(TPASS, "Test Passed"); |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 67 | else |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 68 | tst_resm(TFAIL, "Test Failed"); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 69 | |
Jan Stancek | 443d16e | 2012-12-03 13:19:57 +0100 | [diff] [blame] | 70 | if ((wait(&status)) == -1) |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 71 | tst_brkm(TBROK | TERRNO, cleanup, |
| 72 | "wait failed, status: %d", status); |
Jan Stancek | 443d16e | 2012-12-03 13:19:57 +0100 | [diff] [blame] | 73 | |
Cyril Hrubis | c91af2f | 2012-11-28 18:02:01 +0100 | [diff] [blame] | 74 | child_exited = 0; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 75 | } |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 76 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | free(child_stack); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 78 | |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 79 | cleanup(); |
Garrett Cooper | a2e4248 | 2010-12-17 01:58:55 -0800 | [diff] [blame] | 80 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 81 | } |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 82 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 83 | static void setup(void) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 84 | { |
Jan Stancek | 443d16e | 2012-12-03 13:19:57 +0100 | [diff] [blame] | 85 | tst_sig(FORK, DEF_HANDLER, cleanup); |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 86 | |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 87 | TEST_PAUSE; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 88 | } |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 89 | |
Wanlong Gao | 3dd0c6a | 2012-12-04 15:16:50 +0800 | [diff] [blame] | 90 | static void cleanup(void) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 91 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 92 | } |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 93 | |
Cyril Hrubis | c91af2f | 2012-11-28 18:02:01 +0100 | [diff] [blame] | 94 | static int child_fn(void *unused __attribute__((unused))) |
nstraz | e321981 | 2003-07-14 17:06:37 +0000 | [diff] [blame] | 95 | { |
Cyril Hrubis | c91af2f | 2012-11-28 18:02:01 +0100 | [diff] [blame] | 96 | int i; |
| 97 | |
| 98 | /* give the kernel scheduler chance to run the parent */ |
| 99 | for (i = 0; i < 100; i++) { |
| 100 | sched_yield(); |
| 101 | usleep(1000); |
| 102 | } |
| 103 | |
| 104 | child_exited = 1; |
robbiew | b8bd30c | 2003-07-31 20:41:11 +0000 | [diff] [blame] | 105 | exit(1); |
Cyril Hrubis | 019ed6c | 2011-08-26 12:59:32 +0200 | [diff] [blame] | 106 | } |