xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/clone/clone02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2012 Wanlong Gao <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
6*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
7*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
10*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
14*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
15*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker /*
19*49cdfc7eSAndroid Build Coastguard Worker  *	 TEST1
20*49cdfc7eSAndroid Build Coastguard Worker  *	 -----
21*49cdfc7eSAndroid Build Coastguard Worker  *		Call clone() with all resources shared.
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  *		CHILD:
24*49cdfc7eSAndroid Build Coastguard Worker  *			modify the shared resources
25*49cdfc7eSAndroid Build Coastguard Worker  *			return 1 on success
26*49cdfc7eSAndroid Build Coastguard Worker  *		PARENT:
27*49cdfc7eSAndroid Build Coastguard Worker  *			wait for child to finish
28*49cdfc7eSAndroid Build Coastguard Worker  *			verify that the shared resourses are modified
29*49cdfc7eSAndroid Build Coastguard Worker  *			return 1 on success
30*49cdfc7eSAndroid Build Coastguard Worker  *		If parent & child returns successfully
31*49cdfc7eSAndroid Build Coastguard Worker  *			test passed
32*49cdfc7eSAndroid Build Coastguard Worker  *		else
33*49cdfc7eSAndroid Build Coastguard Worker  *			test failed
34*49cdfc7eSAndroid Build Coastguard Worker  *
35*49cdfc7eSAndroid Build Coastguard Worker  *	 TEST2
36*49cdfc7eSAndroid Build Coastguard Worker  *	 -----
37*49cdfc7eSAndroid Build Coastguard Worker  *		Call clone() with no resources shared.
38*49cdfc7eSAndroid Build Coastguard Worker  *
39*49cdfc7eSAndroid Build Coastguard Worker  *		CHILD:
40*49cdfc7eSAndroid Build Coastguard Worker  *			modify the resources in child's address space
41*49cdfc7eSAndroid Build Coastguard Worker  *			return 1 on success
42*49cdfc7eSAndroid Build Coastguard Worker  *		PARENT:
43*49cdfc7eSAndroid Build Coastguard Worker  *			wait for child to finish
44*49cdfc7eSAndroid Build Coastguard Worker  *			verify that the parent's resourses are not modified
45*49cdfc7eSAndroid Build Coastguard Worker  *			return 1 on success
46*49cdfc7eSAndroid Build Coastguard Worker  *		If parent & child returns successfully
47*49cdfc7eSAndroid Build Coastguard Worker  *			test passed
48*49cdfc7eSAndroid Build Coastguard Worker  *		else
49*49cdfc7eSAndroid Build Coastguard Worker  *			test failed
50*49cdfc7eSAndroid Build Coastguard Worker  */
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
55*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
56*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
57*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
58*49cdfc7eSAndroid Build Coastguard Worker #include <sys/syscall.h>
59*49cdfc7eSAndroid Build Coastguard Worker #include <sched.h>
60*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
61*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker #define FLAG_ALL (CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|SIGCHLD)
64*49cdfc7eSAndroid Build Coastguard Worker #define FLAG_NONE SIGCHLD
65*49cdfc7eSAndroid Build Coastguard Worker #define PARENT_VALUE 1
66*49cdfc7eSAndroid Build Coastguard Worker #define CHILD_VALUE 2
67*49cdfc7eSAndroid Build Coastguard Worker #define TRUE 1
68*49cdfc7eSAndroid Build Coastguard Worker #define FALSE 0
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker #include "clone_platform.h"
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
73*49cdfc7eSAndroid Build Coastguard Worker static int test_setup(void);
74*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
75*49cdfc7eSAndroid Build Coastguard Worker static void test_cleanup(void);
76*49cdfc7eSAndroid Build Coastguard Worker static int child_fn();
77*49cdfc7eSAndroid Build Coastguard Worker static int parent_test1(void);
78*49cdfc7eSAndroid Build Coastguard Worker static int parent_test2(void);
79*49cdfc7eSAndroid Build Coastguard Worker static int test_VM(void);
80*49cdfc7eSAndroid Build Coastguard Worker static int test_FS(void);
81*49cdfc7eSAndroid Build Coastguard Worker static int test_FILES(void);
82*49cdfc7eSAndroid Build Coastguard Worker static int test_SIG(void);
83*49cdfc7eSAndroid Build Coastguard Worker static int modified_VM(void);
84*49cdfc7eSAndroid Build Coastguard Worker static int modified_FS(void);
85*49cdfc7eSAndroid Build Coastguard Worker static int modified_FILES(void);
86*49cdfc7eSAndroid Build Coastguard Worker static int modified_SIG(void);
87*49cdfc7eSAndroid Build Coastguard Worker static void sig_child_defined_handler(int);
88*49cdfc7eSAndroid Build Coastguard Worker static void sig_default_handler();
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker static int fd_parent;
91*49cdfc7eSAndroid Build Coastguard Worker static char file_name[25];
92*49cdfc7eSAndroid Build Coastguard Worker static int parent_variable;
93*49cdfc7eSAndroid Build Coastguard Worker static char cwd_parent[FILENAME_MAX];
94*49cdfc7eSAndroid Build Coastguard Worker static int parent_got_signal, child_pid;
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "clone02";
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {
99*49cdfc7eSAndroid Build Coastguard Worker 	int flags;
100*49cdfc7eSAndroid Build Coastguard Worker 	int (*parent_fn) ();
101*49cdfc7eSAndroid Build Coastguard Worker } test_cases[] = {
102*49cdfc7eSAndroid Build Coastguard Worker 	{
103*49cdfc7eSAndroid Build Coastguard Worker 	FLAG_ALL, parent_test1}, {
104*49cdfc7eSAndroid Build Coastguard Worker 	FLAG_NONE, parent_test2}
105*49cdfc7eSAndroid Build Coastguard Worker };
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
108*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)109*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
110*49cdfc7eSAndroid Build Coastguard Worker {
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
113*49cdfc7eSAndroid Build Coastguard Worker 	void *child_stack;
114*49cdfc7eSAndroid Build Coastguard Worker 	int status, i;
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 	setup();
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	child_stack = malloc(CHILD_STACK_SIZE);
121*49cdfc7eSAndroid Build Coastguard Worker 	if (child_stack == NULL)
122*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "Cannot allocate stack for child");
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
125*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < TST_TOTAL; ++i) {
128*49cdfc7eSAndroid Build Coastguard Worker 			if (test_setup() != 0) {
129*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TWARN, "test_setup() failed, skipping this test case");
130*49cdfc7eSAndroid Build Coastguard Worker 				continue;
131*49cdfc7eSAndroid Build Coastguard Worker 			}
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker 			/* Test the system call */
134*49cdfc7eSAndroid Build Coastguard Worker 			TEST(ltp_clone(test_cases[i].flags, child_fn, NULL,
135*49cdfc7eSAndroid Build Coastguard Worker 				       CHILD_STACK_SIZE, child_stack));
136*49cdfc7eSAndroid Build Coastguard Worker 
137*49cdfc7eSAndroid Build Coastguard Worker 			/* check return code */
138*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_RETURN == -1) {
139*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL | TTERRNO, "clone() failed");
140*49cdfc7eSAndroid Build Coastguard Worker 				/* Cleanup & continue with next test case */
141*49cdfc7eSAndroid Build Coastguard Worker 				test_cleanup();
142*49cdfc7eSAndroid Build Coastguard Worker 				continue;
143*49cdfc7eSAndroid Build Coastguard Worker 			}
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker 			/* Wait for child to finish */
146*49cdfc7eSAndroid Build Coastguard Worker 			if ((wait(&status)) == -1) {
147*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TWARN | TERRNO,
148*49cdfc7eSAndroid Build Coastguard Worker 					 "wait failed; skipping testcase");
149*49cdfc7eSAndroid Build Coastguard Worker 				/* Cleanup & continue with next test case */
150*49cdfc7eSAndroid Build Coastguard Worker 				test_cleanup();
151*49cdfc7eSAndroid Build Coastguard Worker 				continue;
152*49cdfc7eSAndroid Build Coastguard Worker 			}
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 			if (WTERMSIG(status))
155*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TWARN, "child exitied with signal %d",
156*49cdfc7eSAndroid Build Coastguard Worker 					 WTERMSIG(status));
157*49cdfc7eSAndroid Build Coastguard Worker 
158*49cdfc7eSAndroid Build Coastguard Worker 			/*
159*49cdfc7eSAndroid Build Coastguard Worker 			 * Check the return value from child function and
160*49cdfc7eSAndroid Build Coastguard Worker 			 * parent function. If both functions returned
161*49cdfc7eSAndroid Build Coastguard Worker 			 * successfully, test passed, else failed
162*49cdfc7eSAndroid Build Coastguard Worker 			 */
163*49cdfc7eSAndroid Build Coastguard Worker 			if (WIFEXITED(status) && WEXITSTATUS(status) == 0 &&
164*49cdfc7eSAndroid Build Coastguard Worker 			    test_cases[i].parent_fn())
165*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS, "Test Passed");
166*49cdfc7eSAndroid Build Coastguard Worker 			else
167*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "Test Failed");
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker 			/* Do test specific cleanup */
170*49cdfc7eSAndroid Build Coastguard Worker 			test_cleanup();
171*49cdfc7eSAndroid Build Coastguard Worker 		}
172*49cdfc7eSAndroid Build Coastguard Worker 	}
173*49cdfc7eSAndroid Build Coastguard Worker 
174*49cdfc7eSAndroid Build Coastguard Worker 	free(child_stack);
175*49cdfc7eSAndroid Build Coastguard Worker 
176*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
177*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
178*49cdfc7eSAndroid Build Coastguard Worker }
179*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)180*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
181*49cdfc7eSAndroid Build Coastguard Worker {
182*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
183*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
184*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
185*49cdfc7eSAndroid Build Coastguard Worker 
186*49cdfc7eSAndroid Build Coastguard Worker 	/* Get unique file name for each child process */
187*49cdfc7eSAndroid Build Coastguard Worker 	if ((sprintf(file_name, "parent_file_%ld", syscall(__NR_gettid))) <= 0)
188*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "sprintf() failed");
189*49cdfc7eSAndroid Build Coastguard Worker }
190*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)191*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
192*49cdfc7eSAndroid Build Coastguard Worker {
193*49cdfc7eSAndroid Build Coastguard Worker 	if (unlink(file_name) == -1)
194*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "unlink(%s) failed", file_name);
195*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
196*49cdfc7eSAndroid Build Coastguard Worker }
197*49cdfc7eSAndroid Build Coastguard Worker 
test_setup(void)198*49cdfc7eSAndroid Build Coastguard Worker static int test_setup(void)
199*49cdfc7eSAndroid Build Coastguard Worker {
200*49cdfc7eSAndroid Build Coastguard Worker 
201*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction def_act;
202*49cdfc7eSAndroid Build Coastguard Worker 
203*49cdfc7eSAndroid Build Coastguard Worker 	/* Save current working directory of parent */
204*49cdfc7eSAndroid Build Coastguard Worker 	if (getcwd(cwd_parent, sizeof(cwd_parent)) == NULL) {
205*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "getcwd() failed in test_setup()");
206*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
207*49cdfc7eSAndroid Build Coastguard Worker 	}
208*49cdfc7eSAndroid Build Coastguard Worker 
209*49cdfc7eSAndroid Build Coastguard Worker 	/*
210*49cdfc7eSAndroid Build Coastguard Worker 	 * Set value for parent_variable in parent, which will be
211*49cdfc7eSAndroid Build Coastguard Worker 	 * changed by child in test_VM(), for testing CLONE_VM flag
212*49cdfc7eSAndroid Build Coastguard Worker 	 */
213*49cdfc7eSAndroid Build Coastguard Worker 	parent_variable = PARENT_VALUE;
214*49cdfc7eSAndroid Build Coastguard Worker 
215*49cdfc7eSAndroid Build Coastguard Worker 	/*
216*49cdfc7eSAndroid Build Coastguard Worker 	 * Open file from parent, which will be closed by
217*49cdfc7eSAndroid Build Coastguard Worker 	 * child in test_FILES(), used for testing CLONE_FILES flag
218*49cdfc7eSAndroid Build Coastguard Worker 	 */
219*49cdfc7eSAndroid Build Coastguard Worker 	fd_parent = open(file_name, O_CREAT | O_RDWR, 0777);
220*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_parent == -1) {
221*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "open() failed in test_setup()");
222*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
223*49cdfc7eSAndroid Build Coastguard Worker 	}
224*49cdfc7eSAndroid Build Coastguard Worker 
225*49cdfc7eSAndroid Build Coastguard Worker 	/*
226*49cdfc7eSAndroid Build Coastguard Worker 	 * set parent_got_signal to FALSE, used for testing
227*49cdfc7eSAndroid Build Coastguard Worker 	 * CLONE_SIGHAND flag
228*49cdfc7eSAndroid Build Coastguard Worker 	 */
229*49cdfc7eSAndroid Build Coastguard Worker 	parent_got_signal = FALSE;
230*49cdfc7eSAndroid Build Coastguard Worker 
231*49cdfc7eSAndroid Build Coastguard Worker 	/* Setup signal handler for SIGUSR2 */
232*49cdfc7eSAndroid Build Coastguard Worker 	def_act.sa_handler = sig_default_handler;
233*49cdfc7eSAndroid Build Coastguard Worker 	def_act.sa_flags = SA_RESTART;
234*49cdfc7eSAndroid Build Coastguard Worker 	sigemptyset(&def_act.sa_mask);
235*49cdfc7eSAndroid Build Coastguard Worker 
236*49cdfc7eSAndroid Build Coastguard Worker 	if (sigaction(SIGUSR2, &def_act, NULL) == -1) {
237*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "sigaction() failed in test_setup()");
238*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
239*49cdfc7eSAndroid Build Coastguard Worker 	}
240*49cdfc7eSAndroid Build Coastguard Worker 
241*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
242*49cdfc7eSAndroid Build Coastguard Worker }
243*49cdfc7eSAndroid Build Coastguard Worker 
test_cleanup(void)244*49cdfc7eSAndroid Build Coastguard Worker static void test_cleanup(void)
245*49cdfc7eSAndroid Build Coastguard Worker {
246*49cdfc7eSAndroid Build Coastguard Worker 
247*49cdfc7eSAndroid Build Coastguard Worker 	/* Restore parent's working directory */
248*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHDIR(cleanup, cwd_parent);
249*49cdfc7eSAndroid Build Coastguard Worker 
250*49cdfc7eSAndroid Build Coastguard Worker }
251*49cdfc7eSAndroid Build Coastguard Worker 
child_fn(void)252*49cdfc7eSAndroid Build Coastguard Worker static int child_fn(void)
253*49cdfc7eSAndroid Build Coastguard Worker {
254*49cdfc7eSAndroid Build Coastguard Worker 
255*49cdfc7eSAndroid Build Coastguard Worker 	/* save child pid */
256*49cdfc7eSAndroid Build Coastguard Worker 	child_pid = syscall(__NR_gettid);
257*49cdfc7eSAndroid Build Coastguard Worker 
258*49cdfc7eSAndroid Build Coastguard Worker 	if (test_VM() == 0 && test_FILES() == 0 && test_FS() == 0 &&
259*49cdfc7eSAndroid Build Coastguard Worker 	    test_SIG() == 0)
260*49cdfc7eSAndroid Build Coastguard Worker 		_exit(0);
261*49cdfc7eSAndroid Build Coastguard Worker 	_exit(1);
262*49cdfc7eSAndroid Build Coastguard Worker }
263*49cdfc7eSAndroid Build Coastguard Worker 
parent_test1(void)264*49cdfc7eSAndroid Build Coastguard Worker static int parent_test1(void)
265*49cdfc7eSAndroid Build Coastguard Worker {
266*49cdfc7eSAndroid Build Coastguard Worker 
267*49cdfc7eSAndroid Build Coastguard Worker 	/*
268*49cdfc7eSAndroid Build Coastguard Worker 	 * For first test case (with all flags set), all resources are
269*49cdfc7eSAndroid Build Coastguard Worker 	 * shared between parent and child. So whatever changes made by
270*49cdfc7eSAndroid Build Coastguard Worker 	 * child should get reflected in parent also. modified_*()
271*49cdfc7eSAndroid Build Coastguard Worker 	 * functions check this. All of them should return 1 for
272*49cdfc7eSAndroid Build Coastguard Worker 	 * parent_test1() to return 1
273*49cdfc7eSAndroid Build Coastguard Worker 	 */
274*49cdfc7eSAndroid Build Coastguard Worker 
275*49cdfc7eSAndroid Build Coastguard Worker 	if (modified_VM() && modified_FILES() && modified_FS() &&
276*49cdfc7eSAndroid Build Coastguard Worker 	    modified_SIG())
277*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
278*49cdfc7eSAndroid Build Coastguard Worker 	return -1;
279*49cdfc7eSAndroid Build Coastguard Worker }
280*49cdfc7eSAndroid Build Coastguard Worker 
parent_test2(void)281*49cdfc7eSAndroid Build Coastguard Worker static int parent_test2(void)
282*49cdfc7eSAndroid Build Coastguard Worker {
283*49cdfc7eSAndroid Build Coastguard Worker 
284*49cdfc7eSAndroid Build Coastguard Worker 	/*
285*49cdfc7eSAndroid Build Coastguard Worker 	 * For second test case (with no resouce shared), all of the
286*49cdfc7eSAndroid Build Coastguard Worker 	 * modified_*() functions should return 0 for parent_test2()
287*49cdfc7eSAndroid Build Coastguard Worker 	 * to return 1
288*49cdfc7eSAndroid Build Coastguard Worker 	 */
289*49cdfc7eSAndroid Build Coastguard Worker 	if (modified_VM() || modified_FILES() || modified_FS() ||
290*49cdfc7eSAndroid Build Coastguard Worker 	    modified_SIG())
291*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
292*49cdfc7eSAndroid Build Coastguard Worker 
293*49cdfc7eSAndroid Build Coastguard Worker 	return -1;
294*49cdfc7eSAndroid Build Coastguard Worker }
295*49cdfc7eSAndroid Build Coastguard Worker 
296*49cdfc7eSAndroid Build Coastguard Worker /*
297*49cdfc7eSAndroid Build Coastguard Worker  * test_VM() - function to change parent_variable from child's
298*49cdfc7eSAndroid Build Coastguard Worker  *	       address space. If CLONE_VM flag is set, child shares
299*49cdfc7eSAndroid Build Coastguard Worker  *	       the memory space with parent so this will be visible
300*49cdfc7eSAndroid Build Coastguard Worker  *	       to parent also.
301*49cdfc7eSAndroid Build Coastguard Worker  */
302*49cdfc7eSAndroid Build Coastguard Worker 
test_VM(void)303*49cdfc7eSAndroid Build Coastguard Worker static int test_VM(void)
304*49cdfc7eSAndroid Build Coastguard Worker {
305*49cdfc7eSAndroid Build Coastguard Worker 	parent_variable = CHILD_VALUE;
306*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
307*49cdfc7eSAndroid Build Coastguard Worker }
308*49cdfc7eSAndroid Build Coastguard Worker 
309*49cdfc7eSAndroid Build Coastguard Worker /*
310*49cdfc7eSAndroid Build Coastguard Worker  * test_FILES() - This function closes a file descriptor opened by
311*49cdfc7eSAndroid Build Coastguard Worker  *		  parent. If CLONE_FILES flag is set, the parent and
312*49cdfc7eSAndroid Build Coastguard Worker  *		  the child process share the same file descriptor
313*49cdfc7eSAndroid Build Coastguard Worker  *		  table. so this affects the parent also
314*49cdfc7eSAndroid Build Coastguard Worker  */
test_FILES(void)315*49cdfc7eSAndroid Build Coastguard Worker static int test_FILES(void)
316*49cdfc7eSAndroid Build Coastguard Worker {
317*49cdfc7eSAndroid Build Coastguard Worker 	if (close(fd_parent) == -1) {
318*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "close failed in test_FILES");
319*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
320*49cdfc7eSAndroid Build Coastguard Worker 	}
321*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
322*49cdfc7eSAndroid Build Coastguard Worker }
323*49cdfc7eSAndroid Build Coastguard Worker 
324*49cdfc7eSAndroid Build Coastguard Worker /*
325*49cdfc7eSAndroid Build Coastguard Worker  * test_FS() -  This function changes the current working directory
326*49cdfc7eSAndroid Build Coastguard Worker  *		of the child process. If CLONE_FS flag is set, this
327*49cdfc7eSAndroid Build Coastguard Worker  *		will be visible to parent also.
328*49cdfc7eSAndroid Build Coastguard Worker  */
test_FS(void)329*49cdfc7eSAndroid Build Coastguard Worker static int test_FS(void)
330*49cdfc7eSAndroid Build Coastguard Worker {
331*49cdfc7eSAndroid Build Coastguard Worker 	char *test_tmpdir;
332*49cdfc7eSAndroid Build Coastguard Worker 	int rval;
333*49cdfc7eSAndroid Build Coastguard Worker 
334*49cdfc7eSAndroid Build Coastguard Worker 	test_tmpdir = tst_get_tmpdir();
335*49cdfc7eSAndroid Build Coastguard Worker 	if (test_tmpdir == NULL) {
336*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "tst_get_tmpdir failed");
337*49cdfc7eSAndroid Build Coastguard Worker 		rval = -1;
338*49cdfc7eSAndroid Build Coastguard Worker 	} else if (chdir(test_tmpdir) == -1) {
339*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "chdir failed in test_FS");
340*49cdfc7eSAndroid Build Coastguard Worker 		rval = -1;
341*49cdfc7eSAndroid Build Coastguard Worker 	} else {
342*49cdfc7eSAndroid Build Coastguard Worker 		rval = 0;
343*49cdfc7eSAndroid Build Coastguard Worker 	}
344*49cdfc7eSAndroid Build Coastguard Worker 
345*49cdfc7eSAndroid Build Coastguard Worker 	free(test_tmpdir);
346*49cdfc7eSAndroid Build Coastguard Worker 	return rval;
347*49cdfc7eSAndroid Build Coastguard Worker }
348*49cdfc7eSAndroid Build Coastguard Worker 
349*49cdfc7eSAndroid Build Coastguard Worker /*
350*49cdfc7eSAndroid Build Coastguard Worker  * test_SIG() - This function changes the signal handler for SIGUSR2
351*49cdfc7eSAndroid Build Coastguard Worker  *		signal for child. If CLONE_SIGHAND flag is set, this
352*49cdfc7eSAndroid Build Coastguard Worker  *		affects parent also.
353*49cdfc7eSAndroid Build Coastguard Worker  */
test_SIG(void)354*49cdfc7eSAndroid Build Coastguard Worker static int test_SIG(void)
355*49cdfc7eSAndroid Build Coastguard Worker {
356*49cdfc7eSAndroid Build Coastguard Worker 
357*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction new_act;
358*49cdfc7eSAndroid Build Coastguard Worker 
359*49cdfc7eSAndroid Build Coastguard Worker 	new_act.sa_handler = sig_child_defined_handler;
360*49cdfc7eSAndroid Build Coastguard Worker 	new_act.sa_flags = SA_RESTART;
361*49cdfc7eSAndroid Build Coastguard Worker 	sigemptyset(&new_act.sa_mask);
362*49cdfc7eSAndroid Build Coastguard Worker 
363*49cdfc7eSAndroid Build Coastguard Worker 	/* Set signal handler to sig_child_defined_handler */
364*49cdfc7eSAndroid Build Coastguard Worker 	if (sigaction(SIGUSR2, &new_act, NULL) == -1) {
365*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "signal failed in test_SIG");
366*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
367*49cdfc7eSAndroid Build Coastguard Worker 	}
368*49cdfc7eSAndroid Build Coastguard Worker 
369*49cdfc7eSAndroid Build Coastguard Worker 	/* Send SIGUSR2 signal to parent */
370*49cdfc7eSAndroid Build Coastguard Worker 	if (kill(getppid(), SIGUSR2) == -1) {
371*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "kill failed in test_SIG");
372*49cdfc7eSAndroid Build Coastguard Worker 		return -1;
373*49cdfc7eSAndroid Build Coastguard Worker 	}
374*49cdfc7eSAndroid Build Coastguard Worker 
375*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
376*49cdfc7eSAndroid Build Coastguard Worker }
377*49cdfc7eSAndroid Build Coastguard Worker 
378*49cdfc7eSAndroid Build Coastguard Worker /*
379*49cdfc7eSAndroid Build Coastguard Worker  * modified_VM() - This function is called by parent process to check
380*49cdfc7eSAndroid Build Coastguard Worker  *		   whether child's modification to parent_variable
381*49cdfc7eSAndroid Build Coastguard Worker  *		   is visible to parent
382*49cdfc7eSAndroid Build Coastguard Worker  */
383*49cdfc7eSAndroid Build Coastguard Worker 
modified_VM(void)384*49cdfc7eSAndroid Build Coastguard Worker static int modified_VM(void)
385*49cdfc7eSAndroid Build Coastguard Worker {
386*49cdfc7eSAndroid Build Coastguard Worker 
387*49cdfc7eSAndroid Build Coastguard Worker 	if (parent_variable == CHILD_VALUE)
388*49cdfc7eSAndroid Build Coastguard Worker 		/* child has modified parent_variable */
389*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
390*49cdfc7eSAndroid Build Coastguard Worker 
391*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
392*49cdfc7eSAndroid Build Coastguard Worker }
393*49cdfc7eSAndroid Build Coastguard Worker 
394*49cdfc7eSAndroid Build Coastguard Worker /*
395*49cdfc7eSAndroid Build Coastguard Worker  * modified_FILES() - This function checks for file descriptor table
396*49cdfc7eSAndroid Build Coastguard Worker  *		      modifications done by child
397*49cdfc7eSAndroid Build Coastguard Worker  */
modified_FILES(void)398*49cdfc7eSAndroid Build Coastguard Worker static int modified_FILES(void)
399*49cdfc7eSAndroid Build Coastguard Worker {
400*49cdfc7eSAndroid Build Coastguard Worker 	char buff[20];
401*49cdfc7eSAndroid Build Coastguard Worker 
402*49cdfc7eSAndroid Build Coastguard Worker 	if (((read(fd_parent, buff, sizeof(buff))) == -1) && (errno == EBADF))
403*49cdfc7eSAndroid Build Coastguard Worker 		/* Child has closed this file descriptor */
404*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
405*49cdfc7eSAndroid Build Coastguard Worker 
406*49cdfc7eSAndroid Build Coastguard Worker 	/* close fd_parent */
407*49cdfc7eSAndroid Build Coastguard Worker 	if ((close(fd_parent)) == -1)
408*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "close() failed in modified_FILES()");
409*49cdfc7eSAndroid Build Coastguard Worker 
410*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
411*49cdfc7eSAndroid Build Coastguard Worker }
412*49cdfc7eSAndroid Build Coastguard Worker 
413*49cdfc7eSAndroid Build Coastguard Worker /*
414*49cdfc7eSAndroid Build Coastguard Worker  * modified_FS() - This function checks parent's current working directory
415*49cdfc7eSAndroid Build Coastguard Worker  *		   to see whether its modified by child or not.
416*49cdfc7eSAndroid Build Coastguard Worker  */
modified_FS(void)417*49cdfc7eSAndroid Build Coastguard Worker static int modified_FS(void)
418*49cdfc7eSAndroid Build Coastguard Worker {
419*49cdfc7eSAndroid Build Coastguard Worker 	char cwd[FILENAME_MAX];
420*49cdfc7eSAndroid Build Coastguard Worker 
421*49cdfc7eSAndroid Build Coastguard Worker 	if ((getcwd(cwd, sizeof(cwd))) == NULL)
422*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN | TERRNO, "getcwd() failed");
423*49cdfc7eSAndroid Build Coastguard Worker 
424*49cdfc7eSAndroid Build Coastguard Worker 	if (!(strcmp(cwd, cwd_parent)))
425*49cdfc7eSAndroid Build Coastguard Worker 		/* cwd hasn't changed */
426*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
427*49cdfc7eSAndroid Build Coastguard Worker 
428*49cdfc7eSAndroid Build Coastguard Worker 	return 1;
429*49cdfc7eSAndroid Build Coastguard Worker }
430*49cdfc7eSAndroid Build Coastguard Worker 
431*49cdfc7eSAndroid Build Coastguard Worker /*
432*49cdfc7eSAndroid Build Coastguard Worker  * modified_SIG() - This function checks whether child has changed
433*49cdfc7eSAndroid Build Coastguard Worker  *		    parent's signal handler for signal, SIGUSR2
434*49cdfc7eSAndroid Build Coastguard Worker  */
modified_SIG(void)435*49cdfc7eSAndroid Build Coastguard Worker static int modified_SIG(void)
436*49cdfc7eSAndroid Build Coastguard Worker {
437*49cdfc7eSAndroid Build Coastguard Worker 
438*49cdfc7eSAndroid Build Coastguard Worker 	if (parent_got_signal)
439*49cdfc7eSAndroid Build Coastguard Worker 		/*
440*49cdfc7eSAndroid Build Coastguard Worker 		 * parent came through sig_child_defined_handler()
441*49cdfc7eSAndroid Build Coastguard Worker 		 * this means child has changed parent's handler
442*49cdfc7eSAndroid Build Coastguard Worker 		 */
443*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
444*49cdfc7eSAndroid Build Coastguard Worker 
445*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
446*49cdfc7eSAndroid Build Coastguard Worker }
447*49cdfc7eSAndroid Build Coastguard Worker 
448*49cdfc7eSAndroid Build Coastguard Worker /*
449*49cdfc7eSAndroid Build Coastguard Worker  * sig_child_defined_handler()  - Signal handler installed by child
450*49cdfc7eSAndroid Build Coastguard Worker  */
sig_child_defined_handler(int pid)451*49cdfc7eSAndroid Build Coastguard Worker static void sig_child_defined_handler(int pid)
452*49cdfc7eSAndroid Build Coastguard Worker {
453*49cdfc7eSAndroid Build Coastguard Worker 	if ((syscall(__NR_gettid)) == child_pid)
454*49cdfc7eSAndroid Build Coastguard Worker 		/* Child got signal, give warning */
455*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TWARN, "Child got SIGUSR2 signal");
456*49cdfc7eSAndroid Build Coastguard Worker 	else
457*49cdfc7eSAndroid Build Coastguard Worker 		parent_got_signal = TRUE;
458*49cdfc7eSAndroid Build Coastguard Worker }
459*49cdfc7eSAndroid Build Coastguard Worker 
460*49cdfc7eSAndroid Build Coastguard Worker /* sig_default_handler() - Default handler for parent */
sig_default_handler(void)461*49cdfc7eSAndroid Build Coastguard Worker static void sig_default_handler(void)
462*49cdfc7eSAndroid Build Coastguard Worker {
463*49cdfc7eSAndroid Build Coastguard Worker }
464