1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * wait401 - check that a call to wait4() correctly waits for a child
23 * process to exit
24 */
25
26 #include "test.h"
27
28 #include <errno.h>
29 #define _USE_BSD
30 #include <sys/types.h>
31 #include <sys/resource.h>
32 #include <sys/wait.h>
33
34 char *TCID = "wait401";
35 int TST_TOTAL = 1;
36
37 static void cleanup(void);
38 static void setup(void);
39
main(int ac,char ** av)40 int main(int ac, char **av)
41 {
42 int lc;
43 pid_t pid;
44 int status = 1;
45 struct rusage rusage;
46
47 tst_parse_opts(ac, av, NULL, NULL);
48
49 setup();
50
51 for (lc = 0; TEST_LOOPING(lc); lc++) {
52 tst_count = 0;
53
54 pid = FORK_OR_VFORK();
55
56 switch (pid) {
57 case -1:
58 tst_brkm(TBROK, cleanup, "fork() failed");
59 break;
60 case 0:
61 sleep(1);
62 exit(0);
63 break;
64 default:
65 TEST(wait4(pid, &status, 0, &rusage));
66 break;
67 }
68
69 if (TEST_RETURN == -1) {
70 tst_brkm(TFAIL, cleanup, "%s call failed - errno = %d "
71 ": %s", TCID, TEST_ERRNO,
72 strerror(TEST_ERRNO));
73 }
74
75 if (WIFEXITED(status) == 0) {
76 tst_brkm(TFAIL, cleanup,
77 "%s call succeeded but "
78 "WIFEXITED() did not return expected value "
79 "- %d", TCID, WIFEXITED(status));
80 } else if (TEST_RETURN != pid) {
81 tst_resm(TFAIL, "%s did not return the "
82 "expected value (%d), actual: %ld",
83 TCID, pid, TEST_RETURN);
84 } else {
85
86 tst_resm(TPASS,
87 "Received child pid as expected.");
88 }
89
90 tst_resm(TPASS, "%s call succeeded", TCID);
91 }
92
93 cleanup();
94 tst_exit();
95 }
96
setup(void)97 static void setup(void)
98 {
99 tst_sig(FORK, DEF_HANDLER, cleanup);
100
101 TEST_PAUSE;
102 }
103
cleanup(void)104 static void cleanup(void)
105 {
106 }
107