blob: faa62b6a252db2eeb307a04aa1cd0d7e9f86c678 [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
22 * waitpid06.c
23 *
24 * DESCRIPTION
25 * Tests to see if pid's returned from fork and waitpid are same.
26 *
27 * ALGORITHM
28 * Check proper functioning of waitpid with pid = -1 and arg = 0
29 *
30 * USAGE: <for command-line>
31 * waitpid06 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
32 * where, -c n : Run n copies concurrently.
33 * -e : Turn on errno logging.
34 * -i n : Execute test n times.
35 * -I x : Execute test for x seconds.
36 * -P x : Pause for x seconds between iterations.
37 * -t : Turn on syscall timing.
38 *
39 * History
40 * 07/2001 John George
41 * -Ported
robbiew4644c7e2002-04-26 14:33:32 +000042 * 04/2002 wjhuie sigset cleanups
plars865695b2001-08-27 22:15:12 +000043 *
44 * Restrictions
45 * None
46 */
47
48#include <sys/types.h>
49#include <signal.h>
50#include <errno.h>
51#include <sys/wait.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080052#include "test.h"
plars865695b2001-08-27 22:15:12 +000053
Wanlong Gaodd621352012-11-20 10:32:33 +080054static void setup_sigint(void);
55static void do_child_1(void);
56static void setup(void);
57static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000058
59char *TCID = "waitpid06";
60int TST_TOTAL = 1;
robbiew3d5c7352003-01-13 17:49:51 +000061volatile int intintr;
Wanlong Gaodd621352012-11-20 10:32:33 +080062static void inthandlr();
63static void do_exit(void);
64static int flag;
plars865695b2001-08-27 22:15:12 +000065
66#define FAILED 1
67#define MAXKIDS 8
68
robbiewd34d5812005-07-11 22:28:09 +000069#ifdef UCLINUX
70static char *argv0;
Wanlong Gaodd621352012-11-20 10:32:33 +080071static void do_child_2_uclinux(void);
robbiewd34d5812005-07-11 22:28:09 +000072#endif
73
plarsac85d1c2002-06-10 14:52:37 +000074int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000075{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020076 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020077 const char *msg;
plars865695b2001-08-27 22:15:12 +000078 int fail = 0;
plars865695b2001-08-27 22:15:12 +000079 int pid;
robbiewd34d5812005-07-11 22:28:09 +000080 int status;
plars865695b2001-08-27 22:15:12 +000081
Wanlong Gaodd621352012-11-20 10:32:33 +080082 msg = parse_opts(argc, argv, NULL, NULL);
83 if (msg != NULL)
plars865695b2001-08-27 22:15:12 +000084 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080085
robbiewd34d5812005-07-11 22:28:09 +000086#ifdef UCLINUX
87 argv0 = argv[0];
88
89 maybe_run_child(&do_child_1, "n", 1);
90 maybe_run_child(&do_child_2_uclinux, "n", 2);
91#endif
92
plars865695b2001-08-27 22:15:12 +000093 setup();
94
plars865695b2001-08-27 22:15:12 +000095 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080096 /* reset tst_count in case we are looping */
97 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000098
Wanlong Gaodd621352012-11-20 10:32:33 +080099 pid = FORK_OR_VFORK();
100 if (pid < 0) {
plars865695b2001-08-27 22:15:12 +0000101 tst_resm(TINFO, "Fork Failed, may be OK under stress");
102 exit(pid);
103 } else if (pid == 0) {
104 /*
105 * Child:
106 * Set up to catch SIGINT. The kids will wait till a
107 * SIGINT has been received before they proceed.
108 */
robbiewd34d5812005-07-11 22:28:09 +0000109#ifdef UCLINUX
110 if (self_exec(argv[0], "n", 1) < 0) {
111 tst_resm(TINFO, "self_exec failed");
112 exit(pid);
plars865695b2001-08-27 22:15:12 +0000113 }
robbiewd34d5812005-07-11 22:28:09 +0000114#else
115 do_child_1();
116#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000117 } else { /* parent */
plars865695b2001-08-27 22:15:12 +0000118 fail = 0;
119 waitpid(pid, &status, 0);
120 if (WEXITSTATUS(status) != 0) {
121 tst_resm(TFAIL, "child returned bad status");
122 fail = 1;
123 }
Wanlong Gaodd621352012-11-20 10:32:33 +0800124 if (fail)
plars865695b2001-08-27 22:15:12 +0000125 tst_resm(TFAIL, "%s FAILED", TCID);
Wanlong Gaodd621352012-11-20 10:32:33 +0800126 else
plars865695b2001-08-27 22:15:12 +0000127 tst_resm(TPASS, "%s PASSED", TCID);
plars865695b2001-08-27 22:15:12 +0000128 }
129 }
Wanlong Gaodd621352012-11-20 10:32:33 +0800130
plars865695b2001-08-27 22:15:12 +0000131 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800132 tst_exit();
plars865695b2001-08-27 22:15:12 +0000133}
134
135/*
robbiewd34d5812005-07-11 22:28:09 +0000136 * setup_sigint()
137 * Sets up a SIGINT handler
138 */
Wanlong Gaodd621352012-11-20 10:32:33 +0800139static void setup_sigint(void)
robbiewd34d5812005-07-11 22:28:09 +0000140{
subrata_modak56207ce2009-03-23 13:35:39 +0000141 if ((sig_t) signal(SIGINT, inthandlr) == SIG_ERR) {
142 tst_resm(TFAIL, "signal SIGINT failed. " "errno = %d", errno);
robbiewd34d5812005-07-11 22:28:09 +0000143 exit(-1);
144 }
145}
146
Wanlong Gaodd621352012-11-20 10:32:33 +0800147static void do_child_1(void)
robbiewd34d5812005-07-11 22:28:09 +0000148{
149 int kid_count, fork_kid_pid[MAXKIDS];
150 int ret_val;
151 int i, j, k, found;
152 int group1, group2;
153 int wait_kid_pid[MAXKIDS], status;
154
155 setup_sigint();
156
157 group1 = getpgrp();
158 for (kid_count = 0; kid_count < MAXKIDS; kid_count++) {
Wanlong Gaodd621352012-11-20 10:32:33 +0800159 if (kid_count == (MAXKIDS / 2))
robbiewd34d5812005-07-11 22:28:09 +0000160 group2 = setpgrp();
Wanlong Gaodd621352012-11-20 10:32:33 +0800161
robbiewd34d5812005-07-11 22:28:09 +0000162 intintr = 0;
163 ret_val = FORK_OR_VFORK();
164 if (ret_val == 0) { /* child */
165#ifdef UCLINUX
166 if (self_exec(argv0, "n", 2) < 0) {
167 tst_resm(TFAIL, "Fork kid %d failed. "
subrata_modak56207ce2009-03-23 13:35:39 +0000168 "errno = %d", kid_count, errno);
robbiewd34d5812005-07-11 22:28:09 +0000169 exit(ret_val);
170 }
171#else
172 do_exit();
173#endif
Wanlong Gaodd621352012-11-20 10:32:33 +0800174 } else if (ret_val < 0) {
robbiewd34d5812005-07-11 22:28:09 +0000175 tst_resm(TFAIL, "Fork kid %d failed. "
subrata_modak56207ce2009-03-23 13:35:39 +0000176 "errno = %d", kid_count, errno);
robbiewd34d5812005-07-11 22:28:09 +0000177 exit(ret_val);
178 }
179
180 /* parent */
181 fork_kid_pid[kid_count] = ret_val;
182 }
183
184#ifdef UCLINUX
185 /* Give the kids a chance to setup SIGINT again, since this is
186 * cleared by exec().
187 */
188 sleep(3);
189#endif
190
191 /* Now send all the kids a SIGINT to tell them to
192 * proceed
193 */
194 for (i = 0; i < MAXKIDS; i++) {
195 if (kill(fork_kid_pid[i], SIGINT) < 0) {
196 tst_resm(TFAIL, "Kill of child %d "
197 "failed, errno = %d", i, errno);
198 exit(-1);
199 }
200 }
201
202 /*
203 * Wait till all kids have terminated. Stash away their
204 * pid's in an array.
205 */
206 kid_count = 0;
207 errno = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000208 while (((ret_val = waitpid(-1, &status, 0)) != -1) || (errno == EINTR)) {
Wanlong Gaodd621352012-11-20 10:32:33 +0800209 if (ret_val == -1)
robbiewd34d5812005-07-11 22:28:09 +0000210 continue;
robbiewd34d5812005-07-11 22:28:09 +0000211
212 if (!WIFEXITED(status)) {
213 tst_resm(TFAIL, "Child %d did not exit "
214 "normally", ret_val);
215 flag = FAILED;
216 printf("status: %d\n", status);
217 } else {
218 if (WEXITSTATUS(status) != 3) {
219 tst_resm(TFAIL, "Child %d"
220 "exited with wrong "
221 "status", ret_val);
222 tst_resm(TFAIL, "Expected 3 "
subrata_modak56207ce2009-03-23 13:35:39 +0000223 "got %d ", WEXITSTATUS(status));
robbiewd34d5812005-07-11 22:28:09 +0000224 flag = FAILED;
225 }
226 }
227 wait_kid_pid[kid_count++] = ret_val;
228 }
229
230 /*
231 * Check that for every entry in the fork_kid_pid array,
232 * there is a matching pid in the wait_kid_pid array. If
233 * not, it's an error.
234 */
235 for (i = 0; i < kid_count; i++) {
236 found = 0;
237 for (j = 0; j < MAXKIDS; j++) {
subrata_modak56207ce2009-03-23 13:35:39 +0000238 if (fork_kid_pid[j] == wait_kid_pid[i]) {
robbiewd34d5812005-07-11 22:28:09 +0000239 found = 1;
240 break;
241 }
242 }
243
244 if (!found) {
245 tst_resm(TFAIL, "Did not find a "
246 "wait_kid_pid for the "
subrata_modak56207ce2009-03-23 13:35:39 +0000247 "fork_kid_pid of %d", wait_kid_pid[i]);
robbiewd34d5812005-07-11 22:28:09 +0000248 for (k = 0; k < MAXKIDS; k++) {
249 tst_resm(TFAIL,
250 "fork_kid_pid[%d] = "
subrata_modak56207ce2009-03-23 13:35:39 +0000251 "%d", k, fork_kid_pid[k]);
robbiewd34d5812005-07-11 22:28:09 +0000252 }
253 for (k = 0; k < kid_count; k++) {
254 tst_resm(TFAIL,
255 "wait_kid_pid[%d] = "
subrata_modak56207ce2009-03-23 13:35:39 +0000256 "%d", k, wait_kid_pid[k]);
robbiewd34d5812005-07-11 22:28:09 +0000257 }
258 flag = FAILED;
259 }
260 }
261
Wanlong Gaodd621352012-11-20 10:32:33 +0800262 if (flag)
robbiewd34d5812005-07-11 22:28:09 +0000263 exit(1);
Wanlong Gaodd621352012-11-20 10:32:33 +0800264 else
robbiewd34d5812005-07-11 22:28:09 +0000265 exit(0);
robbiewd34d5812005-07-11 22:28:09 +0000266}
267
268#ifdef UCLINUX
269/*
270 * do_child_2_uclinux()
271 * sets up sigint handler again, then calls the normal child 2 function
272 */
Wanlong Gaodd621352012-11-20 10:32:33 +0800273static void do_child_2_uclinux(void)
robbiewd34d5812005-07-11 22:28:09 +0000274{
275 setup_sigint();
276 do_exit();
277}
278#endif
279
Wanlong Gaodd621352012-11-20 10:32:33 +0800280static void setup(void)
plars865695b2001-08-27 22:15:12 +0000281{
plars865695b2001-08-27 22:15:12 +0000282 TEST_PAUSE;
283}
284
Wanlong Gaodd621352012-11-20 10:32:33 +0800285static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000286{
Wanlong Gaodd621352012-11-20 10:32:33 +0800287}
plars865695b2001-08-27 22:15:12 +0000288
Mike Frysingerc57fba52014-04-09 18:56:30 -0400289static void inthandlr(void)
plars865695b2001-08-27 22:15:12 +0000290{
291 intintr++;
292}
293
Wanlong Gaodd621352012-11-20 10:32:33 +0800294static void wait_for_parent(void)
plars865695b2001-08-27 22:15:12 +0000295{
296 int testvar;
297
Wanlong Gaodd621352012-11-20 10:32:33 +0800298 while (!intintr)
plars865695b2001-08-27 22:15:12 +0000299 testvar = 0;
plars865695b2001-08-27 22:15:12 +0000300}
301
Wanlong Gaodd621352012-11-20 10:32:33 +0800302static void do_exit(void)
plars865695b2001-08-27 22:15:12 +0000303{
304 wait_for_parent();
305 exit(3);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700306}