blob: 933086cb9e7992bcfb17681e7470ae9a48ff685c [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
Wanlong Gaobfca91c2012-11-20 10:32:33 +080022 * waitpid10.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
Wanlong Gaobfca91c2012-11-20 10:32:33 +080025 * Tests to see if pid's returned from fork and waitpid are same
plars865695b2001-08-27 22:15:12 +000026 *
27 * ALGORITHM
Wanlong Gaobfca91c2012-11-20 10:32:33 +080028 * Set up to catch SIGINTs, SIGALRMs, and the real time timer.
29 * Until the timer interrupts, do the following. Fork 8 kids.
30 * 2 will immediately exit, 2 will sleep, 2 will be compute bound,
31 * and 2 will fork another child, both which will do mkdirs on
32 * the same directory 50 times. When the timer expires, kill all
33 * kids and remove the directory.
plars865695b2001-08-27 22:15:12 +000034 *
35 * USAGE: <for command-line>
36 * waitpid10 [-c n] [-i n] [-I x] [-P x] [-t]
37 * where, -c n : Run n copies concurrently.
38 * -i n : Execute test n times.
39 * -I x : Execute test for x seconds.
40 * -P x : Pause for x seconds between iterations.
41 * -t : Turn on syscall timing.
42 *
43 * NOTE
Wanlong Gaobfca91c2012-11-20 10:32:33 +080044 * This test was designed to see if the intermittant occurrance
45 * of a waitpid returning a pid different from that returned by the
46 * fork can be reproduced.
plars865695b2001-08-27 22:15:12 +000047 *
48 * History
49 * 07/2001 John George
50 * -Ported
robbiew4644c7e2002-04-26 14:33:32 +000051 * 04/2002 wjhuie sigset cleanups
plars865695b2001-08-27 22:15:12 +000052 *
53 * Restrictions
Wanlong Gaobfca91c2012-11-20 10:32:33 +080054 * None
plars865695b2001-08-27 22:15:12 +000055 */
56
plars74948ad2002-11-14 16:16:14 +000057#include <sys/types.h>
58#include <sys/stat.h>
59#include <sys/wait.h>
60
plars865695b2001-08-27 22:15:12 +000061#include <stdio.h>
62#include <signal.h>
63#include <errno.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080064#include "test.h"
plars865695b2001-08-27 22:15:12 +000065
66#define MAXKIDS 8
67
68char *TCID = "waitpid10";
69int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000070
Wanlong Gaobfca91c2012-11-20 10:32:33 +080071static int alrmintr;
robbiew3d5c7352003-01-13 17:49:51 +000072volatile int intintr;
plars865695b2001-08-27 22:15:12 +000073
Wanlong Gaobfca91c2012-11-20 10:32:33 +080074static void setup(void);
75static void cleanup(void);
76static void inthandlr();
77static void alrmhandlr();
78static void wait_for_parent(void);
79static void do_exit(void);
80static void do_compute(void);
81static void do_fork(void);
82static void do_sleep(void);
83static void do_mkdir(void);
plars865695b2001-08-27 22:15:12 +000084
Wanlong Gaobfca91c2012-11-20 10:32:33 +080085static int fail;
plars865695b2001-08-27 22:15:12 +000086
robbiewd34d5812005-07-11 22:28:09 +000087#ifdef UCLINUX
88static char *argv0;
89#endif
90
plars74948ad2002-11-14 16:16:14 +000091int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000092{
93 int kid_count, ret_val, status, nkids;
plars74948ad2002-11-14 16:16:14 +000094 int i, j, k, found;
plars865695b2001-08-27 22:15:12 +000095 int fork_kid_pid[MAXKIDS], wait_kid_pid[MAXKIDS];
subrata_modak56207ce2009-03-23 13:35:39 +000096 int runtime; /* time(sec) to run this process */
subrata_modakbdbaec52009-02-26 12:14:51 +000097
Cyril Hrubis89af32a2012-10-24 16:39:11 +020098 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020099 const char *msg;
plars865695b2001-08-27 22:15:12 +0000100
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800101 msg = parse_opts(ac, av, NULL, NULL);
102 if (msg != NULL)
plars865695b2001-08-27 22:15:12 +0000103 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800104
robbiewd34d5812005-07-11 22:28:09 +0000105#ifdef UCLINUX
106 argv0 = av[0];
107
108 maybe_run_child(&do_exit, "n", 1);
109 maybe_run_child(&do_compute, "n", 2);
110 maybe_run_child(&do_fork, "n", 3);
111 maybe_run_child(&do_sleep, "n", 4);
112 maybe_run_child(&do_mkdir, "n", 5);
113#endif
subrata_modakbdbaec52009-02-26 12:14:51 +0000114
plars865695b2001-08-27 22:15:12 +0000115 /*
116 * process the arg -- If there is one arg, it is the
117 * number of seconds to run. If there is no arg the program
118 * defaults to 60 sec runtime.
119 */
120 if (ac == 2) {
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800121 if (sscanf(av[1], "%d", &runtime) != 1)
plars865695b2001-08-27 22:15:12 +0000122 tst_resm(TFAIL, "%s is an invalid argument", av[1]);
plars865695b2001-08-27 22:15:12 +0000123 } else {
124 runtime = 60;
125 }
126
127 setup();
128
plars865695b2001-08-27 22:15:12 +0000129 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800130 /* reset tst_count in case we are looping */
131 tst_count = 0;
subrata_modak6c9301a2008-02-25 09:31:51 +0000132 fail = 0;
plars865695b2001-08-27 22:15:12 +0000133
robbiew4644c7e2002-04-26 14:33:32 +0000134 if (signal(SIGALRM, alrmhandlr) == SIG_ERR) {
135 tst_resm(TFAIL, "signal SIGALRM failed. errno = %d",
plars865695b2001-08-27 22:15:12 +0000136 errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800137
plars865695b2001-08-27 22:15:12 +0000138 }
139 alrmintr = 0;
140
141 /*
142 * Set up to catch SIGINT. The kids will wait till a SIGINT
143 * has been received before they proceed.
144 */
robbiew4644c7e2002-04-26 14:33:32 +0000145 if (signal(SIGINT, inthandlr) == SIG_ERR) {
146 tst_resm(TFAIL, "signal SIGINT failed. errno = %d",
plars865695b2001-08-27 22:15:12 +0000147 errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800148
plars865695b2001-08-27 22:15:12 +0000149 }
150 intintr = 0;
151
152 /* Turn on the real time interval timer. */
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800153 if ((alarm(runtime)) < 0)
plars865695b2001-08-27 22:15:12 +0000154 tst_resm(TFAIL, "alarm failed. errno = %d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800155
plars865695b2001-08-27 22:15:12 +0000156 /* Run the test over and over until the timer expires */
157 for (;;) {
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800158 if (alrmintr)
plars865695b2001-08-27 22:15:12 +0000159 break;
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800160
plars865695b2001-08-27 22:15:12 +0000161 /*
162 * Fork 8 kids. There will be 4 sets of 2 processes
163 * doing the same thing. Save all kid pid's in an
164 * array for future use. The kids will first wait for
165 * the parent to send SIGINT. Then will proceed to
166 * their assigned tasks.
167 */
168 kid_count = 0;
robbiew9400d952005-02-22 22:18:49 +0000169 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000170 * Clearing the intinitr flag here for all the children.
171 * So that we may not miss any signals !
172 */
robbiew9400d952005-02-22 22:18:49 +0000173 intintr = 0;
robbiewd34d5812005-07-11 22:28:09 +0000174 ret_val = FORK_OR_VFORK();
subrata_modak56207ce2009-03-23 13:35:39 +0000175 if (ret_val == 0) { /* child 0 */
robbiewd34d5812005-07-11 22:28:09 +0000176#ifdef UCLINUX
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800177 if (self_exec(argv0, "n", 1) < 0)
robbiewd34d5812005-07-11 22:28:09 +0000178 tst_resm(TFAIL, "self_exec 0 failed");
robbiewd34d5812005-07-11 22:28:09 +0000179#else
plars865695b2001-08-27 22:15:12 +0000180 do_exit();
robbiewd34d5812005-07-11 22:28:09 +0000181#endif
plars865695b2001-08-27 22:15:12 +0000182 }
183 if (ret_val < 0) {
184 tst_resm(TFAIL, "Fork kid 0 failed. errno = "
185 "%d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800186
plars865695b2001-08-27 22:15:12 +0000187 }
188
189 /* parent */
190 fork_kid_pid[kid_count++] = ret_val;
191
robbiewd34d5812005-07-11 22:28:09 +0000192 ret_val = FORK_OR_VFORK();
subrata_modak56207ce2009-03-23 13:35:39 +0000193 if (ret_val == 0) { /* child 1 */
robbiewd34d5812005-07-11 22:28:09 +0000194#ifdef UCLINUX
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800195 if (self_exec(argv0, "n", 1) < 0)
robbiewd34d5812005-07-11 22:28:09 +0000196 tst_resm(TFAIL, "self_exec 1 failed");
robbiewd34d5812005-07-11 22:28:09 +0000197#else
plars865695b2001-08-27 22:15:12 +0000198 do_exit();
robbiewd34d5812005-07-11 22:28:09 +0000199#endif
plars865695b2001-08-27 22:15:12 +0000200 }
201 if (ret_val < 0) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000202 tst_resm(TFAIL, "Fork kid 1 failed. errno = "
plars865695b2001-08-27 22:15:12 +0000203 "%d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800204
plars865695b2001-08-27 22:15:12 +0000205 }
206
207 /* parent */
208 fork_kid_pid[kid_count++] = ret_val;
209
robbiewd34d5812005-07-11 22:28:09 +0000210 ret_val = FORK_OR_VFORK();
subrata_modak56207ce2009-03-23 13:35:39 +0000211 if (ret_val == 0) { /* child 2 */
robbiewd34d5812005-07-11 22:28:09 +0000212#ifdef UCLINUX
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800213 if (self_exec(argv0, "n", 2) < 0)
robbiewd34d5812005-07-11 22:28:09 +0000214 tst_resm(TFAIL, "self_exec 2 failed");
robbiewd34d5812005-07-11 22:28:09 +0000215#else
plars865695b2001-08-27 22:15:12 +0000216 do_compute();
robbiewd34d5812005-07-11 22:28:09 +0000217#endif
plars865695b2001-08-27 22:15:12 +0000218 }
219 if (ret_val < 0) {
220 tst_resm(TFAIL, "Fork kid 2 failed. errno = "
221 "%d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800222
plars865695b2001-08-27 22:15:12 +0000223 }
224
225 /* parent */
226 fork_kid_pid[kid_count++] = ret_val;
227
robbiewd34d5812005-07-11 22:28:09 +0000228 ret_val = FORK_OR_VFORK();
subrata_modak56207ce2009-03-23 13:35:39 +0000229 if (ret_val == 0) { /* child 3 */
robbiewd34d5812005-07-11 22:28:09 +0000230#ifdef UCLINUX
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800231 if (self_exec(argv0, "n", 2) < 0)
robbiewd34d5812005-07-11 22:28:09 +0000232 tst_resm(TFAIL, "self_exec 3 failed");
robbiewd34d5812005-07-11 22:28:09 +0000233#else
plars865695b2001-08-27 22:15:12 +0000234 do_compute();
robbiewd34d5812005-07-11 22:28:09 +0000235#endif
plars865695b2001-08-27 22:15:12 +0000236 }
237 if (ret_val < 0) {
238 tst_resm(TFAIL, "Fork kid 3 failed. errno = "
239 "%d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800240
plars865695b2001-08-27 22:15:12 +0000241 }
242
243 /* parent */
244 fork_kid_pid[kid_count++] = ret_val;
245
robbiewd34d5812005-07-11 22:28:09 +0000246 ret_val = FORK_OR_VFORK();
subrata_modak56207ce2009-03-23 13:35:39 +0000247 if (ret_val == 0) { /* child 4 */
robbiewd34d5812005-07-11 22:28:09 +0000248#ifdef UCLINUX
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800249 if (self_exec(argv0, "n", 3) < 0)
robbiewd34d5812005-07-11 22:28:09 +0000250 tst_resm(TFAIL, "self_exec 4 failed");
robbiewd34d5812005-07-11 22:28:09 +0000251#else
plars865695b2001-08-27 22:15:12 +0000252 do_fork();
robbiewd34d5812005-07-11 22:28:09 +0000253#endif
plars865695b2001-08-27 22:15:12 +0000254 }
255 if (ret_val < 0) {
256 tst_resm(TFAIL, "Fork kid 4 failed. errno = "
257 "%d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800258
plars865695b2001-08-27 22:15:12 +0000259 }
260
261 /* parent */
262 fork_kid_pid[kid_count++] = ret_val;
263
robbiewd34d5812005-07-11 22:28:09 +0000264 ret_val = FORK_OR_VFORK();
subrata_modak56207ce2009-03-23 13:35:39 +0000265 if (ret_val == 0) { /* child 5 */
robbiewd34d5812005-07-11 22:28:09 +0000266#ifdef UCLINUX
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800267 if (self_exec(argv0, "n", 3) < 0)
robbiewd34d5812005-07-11 22:28:09 +0000268 tst_resm(TFAIL, "self_exec 5 failed");
robbiewd34d5812005-07-11 22:28:09 +0000269#else
plars865695b2001-08-27 22:15:12 +0000270 do_fork();
robbiewd34d5812005-07-11 22:28:09 +0000271#endif
plars865695b2001-08-27 22:15:12 +0000272 }
273 if (ret_val < 0) {
274 tst_resm(TFAIL, "Fork kid 5 failed. errno = "
275 "%d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800276
plars865695b2001-08-27 22:15:12 +0000277 }
278
279 /* parent */
280 fork_kid_pid[kid_count++] = ret_val;
281
robbiewd34d5812005-07-11 22:28:09 +0000282 ret_val = FORK_OR_VFORK();
subrata_modak56207ce2009-03-23 13:35:39 +0000283 if (ret_val == 0) { /* child 6 */
robbiewd34d5812005-07-11 22:28:09 +0000284#ifdef UCLINUX
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800285 if (self_exec(argv0, "n", 4) < 0)
robbiewd34d5812005-07-11 22:28:09 +0000286 tst_resm(TFAIL, "self_exec 6 failed");
robbiewd34d5812005-07-11 22:28:09 +0000287#else
plars865695b2001-08-27 22:15:12 +0000288 do_sleep();
robbiewd34d5812005-07-11 22:28:09 +0000289#endif
plars865695b2001-08-27 22:15:12 +0000290 }
291 if (ret_val < 0) {
292 tst_resm(TFAIL, "Fork kid 6 failed. errno = "
293 "%d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800294
plars865695b2001-08-27 22:15:12 +0000295 }
296
297 /* parent */
298 fork_kid_pid[kid_count++] = ret_val;
299
robbiewd34d5812005-07-11 22:28:09 +0000300 ret_val = FORK_OR_VFORK();
subrata_modak56207ce2009-03-23 13:35:39 +0000301 if (ret_val == 0) { /* child 7 */
robbiewd34d5812005-07-11 22:28:09 +0000302#ifdef UCLINUX
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800303 if (self_exec(argv0, "n", 4) < 0)
robbiewd34d5812005-07-11 22:28:09 +0000304 tst_resm(TFAIL, "self_exec 7 failed");
robbiewd34d5812005-07-11 22:28:09 +0000305#else
plars865695b2001-08-27 22:15:12 +0000306 do_sleep();
robbiewd34d5812005-07-11 22:28:09 +0000307#endif
plars865695b2001-08-27 22:15:12 +0000308 }
309 if (ret_val < 0) {
310 tst_resm(TFAIL, "Fork kid 7 failed. errno = "
311 "%d", errno);
Garrett Cooper2c282152010-12-16 00:55:50 -0800312
plars865695b2001-08-27 22:15:12 +0000313 }
314
315 /* parent */
316 fork_kid_pid[kid_count++] = ret_val;
317
318 nkids = kid_count;
319
320 /*
321 * Now send all the kids a SIGINT to tell them to
322 * proceed. We sleep for a while first to allow the
323 * children to initialize their "intintr" variables
324 * and get set up.
325 */
326 sleep(15);
327
328 for (i = 0; i < nkids; i++) {
329 if (kill(fork_kid_pid[i], SIGINT) < 0) {
330 tst_resm(TFAIL, "Kill of child %d "
331 "failed, errno = %d", i,
332 errno);
plars865695b2001-08-27 22:15:12 +0000333 }
334 }
335
subrata_modak4bb656a2009-02-26 12:02:09 +0000336 /* Wait till all kids have terminated. */
plars865695b2001-08-27 22:15:12 +0000337 kid_count = 0;
338 errno = 0;
339 for (i = 0; i < nkids; i++) {
340 while (((ret_val = waitpid(fork_kid_pid[i],
341 &status, 0)) != -1)
342 || (errno == EINTR)) {
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800343 if (ret_val == -1)
plars865695b2001-08-27 22:15:12 +0000344 continue;
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800345
plars865695b2001-08-27 22:15:12 +0000346 wait_kid_pid[kid_count++] = ret_val;
347 }
348 }
349
350 /*
351 * Check that for every entry in the fork_kid_pid
352 * array, there is a matching pid in the
353 * wait_kid_pid array.
354 */
355 for (i = 0; i < MAXKIDS; i++) {
356 found = 0;
357 for (j = 0; j < MAXKIDS; j++) {
subrata_modak56207ce2009-03-23 13:35:39 +0000358 if (fork_kid_pid[i] == wait_kid_pid[j]) {
plars865695b2001-08-27 22:15:12 +0000359 found = 1;
360 break;
361 }
362 }
363 if (!found) {
364 tst_resm(TFAIL, "Did not find a "
365 "wait_kid_pid for the "
366 "fork_kid_pid of %d",
367 fork_kid_pid[i]);
368 for (k = 0; k < nkids; k++) {
369 tst_resm(TFAIL,
370 "fork_kid_pid[%d] = "
371 "%d", k,
372 fork_kid_pid[k]);
373 }
374 for (k = 0; k < kid_count; k++) {
375 tst_resm(TFAIL,
376 "wait_kid_pid[%d] = "
377 "%d", k,
378 wait_kid_pid[k]);
379 }
380 fail = 1;
381 }
382 }
383 }
384
385 /* Kill kids and remove file from do_mkdir */
386 rmdir("waitpid14.ttt.ttt");
387
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800388 if (fail)
plars865695b2001-08-27 22:15:12 +0000389 tst_resm(TFAIL, "Test FAILED");
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800390 else
plars865695b2001-08-27 22:15:12 +0000391 tst_resm(TPASS, "Test PASSED");
plars865695b2001-08-27 22:15:12 +0000392 }
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800393
plars865695b2001-08-27 22:15:12 +0000394 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800395 tst_exit();
plars865695b2001-08-27 22:15:12 +0000396}
397
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800398static void setup(void)
plars865695b2001-08-27 22:15:12 +0000399{
plars865695b2001-08-27 22:15:12 +0000400 tst_sig(FORK, DEF_HANDLER, cleanup);
401
plars865695b2001-08-27 22:15:12 +0000402 TEST_PAUSE;
403}
404
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800405static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000406{
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800407}
plars865695b2001-08-27 22:15:12 +0000408
Mike Frysingerc57fba52014-04-09 18:56:30 -0400409static void alrmhandlr(void)
plars865695b2001-08-27 22:15:12 +0000410{
411 alrmintr++;
412}
413
Mike Frysingerc57fba52014-04-09 18:56:30 -0400414static void inthandlr(void)
plars865695b2001-08-27 22:15:12 +0000415{
416 intintr++;
417}
418
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800419static void wait_for_parent(void)
plars865695b2001-08-27 22:15:12 +0000420{
421 int testvar;
422
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800423 while (!intintr)
plars865695b2001-08-27 22:15:12 +0000424 testvar = 0;
plars865695b2001-08-27 22:15:12 +0000425}
426
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800427static void do_exit(void)
plars865695b2001-08-27 22:15:12 +0000428{
429 wait_for_parent();
430 exit(3);
431}
432
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800433static void do_compute(void)
plars865695b2001-08-27 22:15:12 +0000434{
435 int i;
436
437 wait_for_parent();
438
subrata_modak56207ce2009-03-23 13:35:39 +0000439 for (i = 0; i < 100000; i++) ;
440 for (i = 0; i < 100000; i++) ;
441 for (i = 0; i < 100000; i++) ;
442 for (i = 0; i < 100000; i++) ;
443 for (i = 0; i < 100000; i++) ;
444 for (i = 0; i < 100000; i++) ;
445 for (i = 0; i < 100000; i++) ;
446 for (i = 0; i < 100000; i++) ;
447 for (i = 0; i < 100000; i++) ;
448 for (i = 0; i < 100000; i++) ;
robbiewd34d5812005-07-11 22:28:09 +0000449
450 exit(4);
plars865695b2001-08-27 22:15:12 +0000451}
452
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800453static void do_fork(void)
plars865695b2001-08-27 22:15:12 +0000454{
plars74948ad2002-11-14 16:16:14 +0000455 int fork_pid, wait_pid;
plars865695b2001-08-27 22:15:12 +0000456 int status, i;
457
458 wait_for_parent();
459
460 /*
461 * Fork a kid. Keep track of the kid's pid, have the kid do_mkdir,
462 * and wait for it. Compare the fork_pid with the wait_pid to be
463 * sure they are the same.
464 */
465 for (i = 0; i < 50; i++) {
robbiewd34d5812005-07-11 22:28:09 +0000466 fork_pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000467 if (fork_pid < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100468 tst_brkm(TFAIL, NULL, "Fork failed");
plars865695b2001-08-27 22:15:12 +0000469 }
470 if (fork_pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000471#ifdef UCLINUX
472 if (self_exec(argv0, "n", 5) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100473 tst_brkm(TFAIL, NULL,
474 "do_fork self_exec failed");
robbiewd34d5812005-07-11 22:28:09 +0000475 }
476#else
plars865695b2001-08-27 22:15:12 +0000477 do_mkdir();
robbiewd34d5812005-07-11 22:28:09 +0000478#endif
plars865695b2001-08-27 22:15:12 +0000479 }
480
481 errno = 0;
482 while (((wait_pid = waitpid(fork_pid, &status, 0)) != -1) ||
subrata_modak56207ce2009-03-23 13:35:39 +0000483 (errno == EINTR)) {
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800484 if (wait_pid == -1)
plars865695b2001-08-27 22:15:12 +0000485 continue;
plars865695b2001-08-27 22:15:12 +0000486
487 if (fork_pid != wait_pid) {
488 tst_resm(TFAIL, "Didnt get a pid returned "
489 "from waitpid that matches the one "
490 "returned by fork");
491 tst_resm(TFAIL, "fork pid = %d, wait pid = "
492 "%d", fork_pid, wait_pid);
493 fail = 1;
494 }
495 }
496 }
robbiewd34d5812005-07-11 22:28:09 +0000497
498 exit(4);
plars865695b2001-08-27 22:15:12 +0000499}
500
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800501static void do_sleep(void)
plars865695b2001-08-27 22:15:12 +0000502{
503 wait_for_parent();
504 sleep(1);
505 sleep(1);
robbiewd34d5812005-07-11 22:28:09 +0000506
507 exit(4);
plars865695b2001-08-27 22:15:12 +0000508}
509
Wanlong Gaobfca91c2012-11-20 10:32:33 +0800510static void do_mkdir(void)
plars865695b2001-08-27 22:15:12 +0000511{
512 int ret_val;
513
514 /*
515 * Please note that this will succeed once, and then fail. That's
516 * part of the test.
517 */
518 ret_val = mkdir("waitpid14.ttt.ttt", 0777);
robbiewd34d5812005-07-11 22:28:09 +0000519
520 exit(4);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700521}