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