blob: 38031ac16b7c37d6ec1cce46c3c750ae4a7c8785 [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 * 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>
Garrett Coopere8530df2010-12-21 11:37:57 -080052#include "test.h"
plars865695b2001-08-27 22:15:12 +000053
Wanlong Gao23e89832012-11-20 10:32:33 +080054static void setup(void);
55static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000056
57char *TCID = "waitpid01";
58int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000059
plars74948ad2002-11-14 16:16:14 +000060int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000061{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020062 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020063 const char *msg;
plars865695b2001-08-27 22:15:12 +000064
65 int pid, npid, sig, nsig;
66 int exno, nexno, status;
67
Wanlong Gao23e89832012-11-20 10:32:33 +080068 msg = parse_opts(argc, argv, NULL, NULL);
69 if (msg != NULL)
plars865695b2001-08-27 22:15:12 +000070 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -080071
plars865695b2001-08-27 22:15:12 +000072 setup();
73
74 /* check for looping state if -i option is given */
75 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080076 /* reset tst_count in case we are looping */
77 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000078
79 exno = 1;
80 sig = 14;
81
Wanlong Gao23e89832012-11-20 10:32:33 +080082 pid = FORK_OR_VFORK();
83 if (pid < 0) {
plars865695b2001-08-27 22:15:12 +000084 tst_brkm(TFAIL, cleanup, "Fork Failed");
85 } else if (pid == 0) {
86 alarm(2);
87 pause();
88 exit(exno);
89 } else {
90 errno = 0;
91 while (((npid = waitpid(pid, &status, 0)) != -1) ||
92 (errno == EINTR)) {
Wanlong Gao23e89832012-11-20 10:32:33 +080093 if (errno == EINTR)
plars865695b2001-08-27 22:15:12 +000094 continue;
plars865695b2001-08-27 22:15:12 +000095
96 if (npid != pid) {
97 tst_resm(TFAIL, "waitpid error: "
98 "unexpected pid returned");
99 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000100 tst_resm(TPASS,
101 "recieved expected pid");
plars865695b2001-08-27 22:15:12 +0000102 }
103
104 nsig = WTERMSIG(status);
105
106 /*
107 * nsig is the signal number returned by
108 * waitpid
109 */
110 if (nsig != sig) {
111 tst_resm(TFAIL, "waitpid error: "
112 "unexpected signal "
113 "returned");
114 } else {
115 tst_resm(TPASS, "recieved expected "
subrata_modak56207ce2009-03-23 13:35:39 +0000116 "signal");
plars865695b2001-08-27 22:15:12 +0000117 }
118
119 /*
120 * nexno is the exit number returned by
121 * waitpid
122 */
123 nexno = WEXITSTATUS(status);
124 if (nexno != 0) {
125 tst_resm(TFAIL, "signal error: "
126 "unexpected exit number "
127 "returned");
128 }
129 }
130 }
131 }
Wanlong Gao23e89832012-11-20 10:32:33 +0800132
plars865695b2001-08-27 22:15:12 +0000133 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800134 tst_exit();
plars865695b2001-08-27 22:15:12 +0000135}
136
Wanlong Gao23e89832012-11-20 10:32:33 +0800137static void setup(void)
plars865695b2001-08-27 22:15:12 +0000138{
plars865695b2001-08-27 22:15:12 +0000139 TEST_PAUSE;
140}
141
Wanlong Gao23e89832012-11-20 10:32:33 +0800142static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000143{
plars865695b2001-08-27 22:15:12 +0000144 tst_sig(FORK, DEF_HANDLER, cleanup);
Wanlong Gao23e89832012-11-20 10:32:33 +0800145}