blob: 84948663878c7c6f435632a2746076b765631b47 [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 * waitpid02.c
23 *
24 * DESCRIPTION
plarsa775b982002-10-31 18:55:02 +000025 * Check that when a child gets killed by an integer zero
plars865695b2001-08-27 22:15:12 +000026 * divide exception, the waiting parent is correctly notified.
27 *
28 * ALGORITHM
subrata_modak4bb656a2009-02-26 12:02:09 +000029 * Fork a child and send a SIGFPE to it. The parent waits for the
plarsa775b982002-10-31 18:55:02 +000030 * death of the child and checks that SIGFPE was returned.
plars865695b2001-08-27 22:15:12 +000031 *
32 * USAGE: <for command-line>
33 * waitpid02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
34 * where, -c n : Run n copies concurrently.
35 * -e : Turn on errno logging.
36 * -i n : Execute test n times.
37 * -I x : Execute test for x seconds.
38 * -P x : Pause for x seconds between iterations.
39 * -t : Turn on syscall timing.
40 *
41 * History
42 * 07/2001 John George
43 * -Ported
plarsa775b982002-10-31 18:55:02 +000044 * 10/2002 Paul Larson
subrata_modak4bb656a2009-02-26 12:02:09 +000045 * Div by zero doesn't cause SIGFPE on some archs, fixed
plarsa775b982002-10-31 18:55:02 +000046 * to send the signal with kill
plars865695b2001-08-27 22:15:12 +000047 *
48 * Restrictions
49 * None
50 */
51
52#include <sys/file.h>
Jan Stancekd0bebd62012-11-14 10:36:33 +010053#include <sys/resource.h>
plars865695b2001-08-27 22:15:12 +000054#include <sys/signal.h>
55#include <sys/types.h>
56#include <sys/wait.h>
57#include <errno.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080058#include "test.h"
plars865695b2001-08-27 22:15:12 +000059
Wanlong Gao9554de52012-11-20 10:32:33 +080060static void do_child(void);
61static void setup(void);
62static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000063
64char *TCID = "waitpid02";
65int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000066
plars74948ad2002-11-14 16:16:14 +000067int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000068{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020069 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020070 const char *msg;
plars865695b2001-08-27 22:15:12 +000071
72 int pid, npid, sig, nsig;
73 int exno, nexno, status;
74
Wanlong Gao9554de52012-11-20 10:32:33 +080075 msg = parse_opts(argc, argv, NULL, NULL);
76 if (msg != NULL)
plars865695b2001-08-27 22:15:12 +000077 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd34d5812005-07-11 22:28:09 +000078#ifdef UCLINUX
79 maybe_run_child(&do_child, "");
80#endif
81
plars865695b2001-08-27 22:15:12 +000082 setup();
83
84 /* check for looping state if -i option is given */
85 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080086 /* reset tst_count in case we are looping */
87 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000088
89 exno = 1;
plarsa775b982002-10-31 18:55:02 +000090 sig = SIGFPE;
plars865695b2001-08-27 22:15:12 +000091
robbiewd34d5812005-07-11 22:28:09 +000092 pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +000093
94 if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +000095#ifdef UCLINUX
96 self_exec(argv[0], "");
97 /* No fork() error check is done so don't check here */
98#else
99 do_child();
100#endif
plars865695b2001-08-27 22:15:12 +0000101 } else {
plarsa775b982002-10-31 18:55:02 +0000102 kill(pid, sig);
plars865695b2001-08-27 22:15:12 +0000103 errno = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000104 while (((npid = waitpid(pid, &status, 0)) != -1) ||
105 (errno == EINTR)) {
Wanlong Gao9554de52012-11-20 10:32:33 +0800106 if (errno == EINTR)
plars865695b2001-08-27 22:15:12 +0000107 continue;
plars865695b2001-08-27 22:15:12 +0000108
109 if (npid != pid) {
110 tst_resm(TFAIL, "waitpid error: "
111 "unexpected pid returned");
112 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000113 tst_resm(TPASS,
114 "recieved expected pid");
plars865695b2001-08-27 22:15:12 +0000115 }
116
117 nsig = WTERMSIG(status);
118
119 /*
120 * nsig is the signal number returned by
121 * waitpid
122 */
123 if (nsig != sig) {
124 tst_resm(TFAIL, "waitpid error: "
125 "unexpected signal returned");
126 } else {
127 tst_resm(TPASS, "recieved expected "
subrata_modak56207ce2009-03-23 13:35:39 +0000128 "signal");
plars865695b2001-08-27 22:15:12 +0000129 }
130
131 /*
132 * nexno is the exit number returned by
133 * waitpid
134 */
135 nexno = WEXITSTATUS(status);
136 if (nexno != 0) {
137 tst_resm(TFAIL, "signal error: "
138 "unexpected exit number "
139 "returned");
140 } else {
141 tst_resm(TPASS, "recieved expected "
subrata_modak56207ce2009-03-23 13:35:39 +0000142 "exit value");
plars865695b2001-08-27 22:15:12 +0000143 }
144 }
145 }
plars865695b2001-08-27 22:15:12 +0000146 }
Wanlong Gao9554de52012-11-20 10:32:33 +0800147
plars865695b2001-08-27 22:15:12 +0000148 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800149 tst_exit();
plars865695b2001-08-27 22:15:12 +0000150}
151
Wanlong Gao9554de52012-11-20 10:32:33 +0800152static void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000153{
154 int exno = 1;
155
subrata_modak56207ce2009-03-23 13:35:39 +0000156 while (1)
robbiewd34d5812005-07-11 22:28:09 +0000157 usleep(10);
subrata_modakbdbaec52009-02-26 12:14:51 +0000158
robbiewd34d5812005-07-11 22:28:09 +0000159 exit(exno);
160}
161
Wanlong Gao9554de52012-11-20 10:32:33 +0800162static void setup(void)
plars865695b2001-08-27 22:15:12 +0000163{
Jan Stancekd0bebd62012-11-14 10:36:33 +0100164 /* SIGFPE is expected signal, so avoid creating any corefile.
165 * '1' is a special value, that will also avoid dumping via pipe. */
166 struct rlimit r;
167 r.rlim_cur = 1;
168 r.rlim_max = 1;
169 setrlimit(RLIMIT_CORE, &r);
170
plars865695b2001-08-27 22:15:12 +0000171 TEST_PAUSE;
172}
173
Wanlong Gao9554de52012-11-20 10:32:33 +0800174static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000175{
Wanlong Gao9554de52012-11-20 10:32:33 +0800176}