blob: 744165030f9953ec6e9d88d9c102be468cd0b5ee [file] [log] [blame]
Cyril Hrubis7adbcab2014-08-12 18:29:40 +02001/* IBM Corporation
2 * 01/02/2003 Port to LTP avenkat@us.ibm.com
3 * 06/30/2001 Port to Linux nsharoff@us.ibm.com
robbiewe237cd32002-12-31 21:21:45 +00004 *
5 * Copyright (c) International Business Machines Corp., 2002
Cyril Hrubis7adbcab2014-08-12 18:29:40 +02006 * Copyright (c) Cyril Hrubis <chrubis@suse.cz> 2014
robbiewe237cd32002-12-31 21:21:45 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
16 * the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewe237cd32002-12-31 21:21:45 +000021 */
22
Cyril Hrubis7adbcab2014-08-12 18:29:40 +020023/*
24
25 Test check that when a child is killed by its parent, it returns the correct
26 values to the waiting parent--default behaviour assumed by child.
27
28 */
29
robbiewe237cd32002-12-31 21:21:45 +000030#define _GNU_SOURCE 1
31
32#include <stdio.h>
Cyril Hrubis7adbcab2014-08-12 18:29:40 +020033#include <errno.h>
robbiewe237cd32002-12-31 21:21:45 +000034#include <sys/types.h>
35#include <signal.h>
36#include <stdlib.h>
37#include <unistd.h>
38#include <sys/wait.h>
Cyril Hrubis491fab82014-08-12 17:34:27 +020039#include <sys/resource.h>
robbiewe237cd32002-12-31 21:21:45 +000040
robbiewe237cd32002-12-31 21:21:45 +000041#include "test.h"
Cyril Hrubis491fab82014-08-12 17:34:27 +020042#include "safe_macros.h"
43
robbiewe237cd32002-12-31 21:21:45 +000044#define FAILED 0
45#define PASSED 1
46
robbiewe237cd32002-12-31 21:21:45 +000047char *TCID = "kill11";
48
robbiewe237cd32002-12-31 21:21:45 +000049int local_flag = PASSED;
50int block_number;
51FILE *temp;
52int TST_TOTAL = 1;
robbiewd34d5812005-07-11 22:28:09 +000053static int sig;
robbiewe237cd32002-12-31 21:21:45 +000054
Cyril Hrubis7adbcab2014-08-12 18:29:40 +020055void setup(void);
56void do_child(void);
robbiewe237cd32002-12-31 21:21:45 +000057
Cyril Hrubis7adbcab2014-08-12 18:29:40 +020058/*
59 * These signals terminate process by default, some create core file.
60 */
61struct tcase {
62 int sig;
63 int dumps_core;
64} tcases[] = {
65 {SIGHUP, 0},
66 {SIGINT, 0},
67 {SIGQUIT, 1},
68 {SIGILL, 1},
69 {SIGTRAP, 1},
70 {SIGABRT, 1},
71 {SIGIOT, 1},
72 {SIGBUS, 1},
73 {SIGFPE, 1},
74 {SIGKILL, 0},
75 {SIGUSR1, 0},
76 {SIGSEGV, 1},
77 {SIGUSR2, 0},
78 {SIGPIPE, 0},
79 {SIGALRM, 0},
80 {SIGTERM, 0},
81 {SIGXCPU, 1},
82 {SIGXFSZ, 1},
83 {SIGVTALRM, 0},
84 {SIGPROF, 0},
85 {SIGIO, 0},
86 {SIGPWR, 0},
87 {SIGSYS, 1},
88};
robbiewe237cd32002-12-31 21:21:45 +000089
Cyril Hrubis7adbcab2014-08-12 18:29:40 +020090static void verify_kill(struct tcase *t)
robbiewe237cd32002-12-31 21:21:45 +000091{
robbiewe237cd32002-12-31 21:21:45 +000092 int core;
93 int pid, npid;
Cyril Hrubis7adbcab2014-08-12 18:29:40 +020094 int nsig, nexno, status;
robbiewd34d5812005-07-11 22:28:09 +000095
Cyril Hrubis7adbcab2014-08-12 18:29:40 +020096 if (t->sig != SIGKILL) {
97#ifndef BCS
98 if (t->sig != SIGSTOP)
99#endif
100 if (sigset(t->sig, SIG_DFL) == SIG_ERR) {
101 tst_brkm(TBROK | TERRNO, tst_rmdir,
102 "sigset(%d) failed", sig);
103 }
104 }
105
106 pid = FORK_OR_VFORK();
107 if (pid < 0)
108 tst_brkm(TBROK | TERRNO, tst_rmdir, "fork() failed");
109
110 if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000111#ifdef UCLINUX
Cyril Hrubis7adbcab2014-08-12 18:29:40 +0200112 if (self_exec(argv[0], "dd", t->sig) < 0)
113 exit(1);
114#else
115 do_child();
116#endif
117 }
118
119 kill(pid, t->sig);
120 npid = wait(&status);
121
122 if (npid != pid) {
123 tst_resm(TFAIL, "wait() returned %d, expected %d", npid, pid);
124 return;
125 }
126
127 nsig = WTERMSIG(status);
128#ifdef WCOREDUMP
129 core = WCOREDUMP(status);
130#endif
131 nexno = WIFEXITED(status);
132
133 if (t->dumps_core) {
134 if (!core) {
135 tst_resm(TFAIL, "core dump bit not set for %s", tst_strsig(t->sig));
136 return;
137 }
138 } else {
139 if (core) {
140 tst_resm(TFAIL, "core dump bit set for %s", tst_strsig(t->sig));
141 return;
142 }
143 }
144
145 if (nsig != t->sig) {
146 tst_resm(TFAIL, "wait: unexpected signal %d returned, expected %d", nsig, t->sig);
147 return;
148 }
149
150 if (nexno != 0) {
151 tst_resm(TFAIL,
152 "signal: unexpected exit number %d returned, expected 0\n",
153 nexno);
154 return;
155 }
156
157 tst_resm(TPASS, "signal %-16s%s", tst_strsig(t->sig),
158 t->dumps_core ? " dumped core" : "");
159}
160
161int main(int argc, char **argv)
162{
163 int lc;
164 unsigned int i;
165 const char *msg;
166
Garrett Cooper43088e12010-12-13 23:30:59 -0800167 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
robbiewd34d5812005-07-11 22:28:09 +0000168 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd34d5812005-07-11 22:28:09 +0000169
Cyril Hrubis7adbcab2014-08-12 18:29:40 +0200170#ifdef UCLINUX
171 maybe_run_child(&do_child, "dd", &sig);
robbiewd34d5812005-07-11 22:28:09 +0000172#endif
173
robbiewe237cd32002-12-31 21:21:45 +0000174 setup();
robbiewe237cd32002-12-31 21:21:45 +0000175
Cyril Hrubis7adbcab2014-08-12 18:29:40 +0200176 for (lc = 0; TEST_LOOPING(lc); lc++) {
177 for (i = 0; i < ARRAY_SIZE(tcases); i++)
178 verify_kill(tcases + i);
robbiewe237cd32002-12-31 21:21:45 +0000179 }
robbiewe237cd32002-12-31 21:21:45 +0000180
Cyril Hrubis7adbcab2014-08-12 18:29:40 +0200181 tst_rmdir();
subrata_modak56207ce2009-03-23 13:35:39 +0000182 tst_exit();
robbiewe237cd32002-12-31 21:21:45 +0000183}
184
Mike Frysingerc57fba52014-04-09 18:56:30 -0400185void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000186{
Cyril Hrubis7adbcab2014-08-12 18:29:40 +0200187 int i;
subrata_modakbdbaec52009-02-26 12:14:51 +0000188
subrata_modak56207ce2009-03-23 13:35:39 +0000189 for (i = 0; i < 180; i++)
robbiewd34d5812005-07-11 22:28:09 +0000190 sleep(1);
Cyril Hrubis7adbcab2014-08-12 18:29:40 +0200191
192 fprintf(stderr, "Child missed siggnal");
193 fflush(stderr);
194 exit(1);
robbiewd34d5812005-07-11 22:28:09 +0000195}
robbiewe237cd32002-12-31 21:21:45 +0000196
Cyril Hrubis491fab82014-08-12 17:34:27 +0200197/* 1024 GNU blocks */
198#define MIN_RLIMIT_CORE (1024 * 1024)
199
Mike Frysingerc57fba52014-04-09 18:56:30 -0400200void setup(void)
robbiewe237cd32002-12-31 21:21:45 +0000201{
Cyril Hrubis491fab82014-08-12 17:34:27 +0200202 struct rlimit rlim;
203
204 SAFE_GETRLIMIT(NULL, RLIMIT_CORE, &rlim);
205
206 if (rlim.rlim_cur < MIN_RLIMIT_CORE) {
207 tst_resm(TINFO, "Adjusting RLIMIT_CORE to %i", MIN_RLIMIT_CORE);
208 rlim.rlim_cur = MIN_RLIMIT_CORE;
209 SAFE_SETRLIMIT(NULL, RLIMIT_CORE, &rlim);
210 }
211
subrata_modak56207ce2009-03-23 13:35:39 +0000212 temp = stderr;
213 tst_tmpdir();
robbiewe237cd32002-12-31 21:21:45 +0000214}