1 /*
2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3  * Copyright (c) 2012 Wanlong Gao <gaowanlong@cn.fujitsu.com>
4  * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
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
15  * with this program; if not, write the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  */
19 
20 /*
21  * Call clone() with CLONE_VFORK flag set. verify that
22  * execution of parent is suspended until child finishes
23  */
24 
25 #define _GNU_SOURCE
26 
27 #include <errno.h>
28 #include <sched.h>
29 #include <sys/wait.h>
30 #include "test.h"
31 #include "clone_platform.h"
32 
33 char *TCID = "clone05";
34 int TST_TOTAL = 1;
35 
36 static void setup(void);
37 static void cleanup(void);
38 static int child_fn(void *);
39 
40 static int child_exited = 0;
41 
main(int ac,char ** av)42 int main(int ac, char **av)
43 {
44 
45 	int lc, status;
46 	void *child_stack;
47 
48 	tst_parse_opts(ac, av, NULL, NULL);
49 
50 	setup();
51 
52 	child_stack = malloc(CHILD_STACK_SIZE);
53 	if (child_stack == NULL)
54 		tst_brkm(TBROK, cleanup, "Cannot allocate stack for child");
55 
56 	for (lc = 0; TEST_LOOPING(lc); lc++) {
57 		tst_count = 0;
58 
59 		TEST(ltp_clone(CLONE_VM | CLONE_VFORK | SIGCHLD, child_fn, NULL,
60 		               CHILD_STACK_SIZE, child_stack));
61 
62 		if ((TEST_RETURN != -1) && (child_exited))
63 			tst_resm(TPASS, "Test Passed");
64 		else
65 			tst_resm(TFAIL, "Test Failed");
66 
67 		if ((wait(&status)) == -1)
68 			tst_brkm(TBROK | TERRNO, cleanup,
69 				 "wait failed, status: %d", status);
70 
71 		child_exited = 0;
72 	}
73 
74 	free(child_stack);
75 
76 	cleanup();
77 	tst_exit();
78 }
79 
setup(void)80 static void setup(void)
81 {
82 	tst_sig(FORK, DEF_HANDLER, cleanup);
83 
84 	TEST_PAUSE;
85 }
86 
cleanup(void)87 static void cleanup(void)
88 {
89 }
90 
child_fn(void * unused)91 static int child_fn(void *unused __attribute__((unused)))
92 {
93 	int i;
94 
95 	/* give the kernel scheduler chance to run the parent */
96 	for (i = 0; i < 100; i++) {
97 		sched_yield();
98 		usleep(1000);
99 	}
100 
101 	child_exited = 1;
102 	_exit(1);
103 }
104