1 /*
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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * NAME
22 * waitpid01.c
23 *
24 * DESCRIPTION
25 * Check that when a child kills itself by generating an alarm
26 * exception, the waiting parent is correctly notified.
27 *
28 * ALGORITHM
29 * Fork a child that sets an alarm. When the alarm goes off, causing
30 * the death of the child, the parent checks that SIG_ALRM was returned
31 *
32 * USAGE: <for command-line>
33 * waitpid01 [-c n] [-i n] [-I x] [-P x] [-t]
34 * where, -c n : Run n copies concurrently.
35 * -i n : Execute test n times.
36 * -I x : Execute test for x seconds.
37 * -P x : Pause for x seconds between iterations.
38 * -t : Turn on syscall timing.
39 *
40 * History
41 * 07/2001 John George
42 * -Ported
43 *
44 * Restrictions
45 * None
46 */
47
48 #include <sys/signal.h>
49 #include <sys/types.h>
50 #include <sys/wait.h>
51 #include <errno.h>
52 #include "test.h"
53
54 static void setup(void);
55 static void cleanup(void);
56
57 char *TCID = "waitpid01";
58 int TST_TOTAL = 1;
59
main(int argc,char ** argv)60 int main(int argc, char **argv)
61 {
62 int lc;
63
64 int pid, npid, sig, nsig;
65 int exno, nexno, status;
66
67 tst_parse_opts(argc, argv, NULL, NULL);
68
69 setup();
70
71 /* check for looping state if -i option is given */
72 for (lc = 0; TEST_LOOPING(lc); lc++) {
73 /* reset tst_count in case we are looping */
74 tst_count = 0;
75
76 exno = 1;
77 sig = 14;
78
79 pid = FORK_OR_VFORK();
80 if (pid < 0) {
81 tst_brkm(TFAIL, cleanup, "Fork Failed");
82 } else if (pid == 0) {
83 alarm(2);
84 pause();
85 exit(exno);
86 } else {
87 errno = 0;
88 while (((npid = waitpid(pid, &status, 0)) != -1) ||
89 (errno == EINTR)) {
90 if (errno == EINTR)
91 continue;
92
93 if (npid != pid) {
94 tst_resm(TFAIL, "waitpid error: "
95 "unexpected pid returned");
96 } else {
97 tst_resm(TPASS,
98 "recieved expected pid");
99 }
100
101 nsig = WTERMSIG(status);
102
103 /*
104 * nsig is the signal number returned by
105 * waitpid
106 */
107 if (nsig != sig) {
108 tst_resm(TFAIL, "waitpid error: "
109 "unexpected signal "
110 "returned");
111 } else {
112 tst_resm(TPASS, "recieved expected "
113 "signal");
114 }
115
116 /*
117 * nexno is the exit number returned by
118 * waitpid
119 */
120 nexno = WEXITSTATUS(status);
121 if (nexno != 0) {
122 tst_resm(TFAIL, "signal error: "
123 "unexpected exit number "
124 "returned");
125 }
126 }
127 }
128 }
129
130 cleanup();
131 tst_exit();
132 }
133
setup(void)134 static void setup(void)
135 {
136 TEST_PAUSE;
137 }
138
cleanup(void)139 static void cleanup(void)
140 {
141 tst_sig(FORK, DEF_HANDLER, cleanup);
142 }
143