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