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