blob: 889c3fa5ea5f98f5bd4d256df4b6a2cd5b2a5d27 [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 * kill06.c
23 *
24 * DESCRIPTION
25 * Test case to check the basic functionality of kill() when killing an
26 * entire process group with a negative pid.
27 *
28 * ALGORITHM
29 * call setup
30 * loop if the -i option was given
31 * fork 5 children
32 * execute the kill system call
33 * check the return value
34 * if return value is -1
35 * issue a FAIL message, break remaining tests and cleanup
36 * if we are doing functional testing
37 * if the processes were terminated with the expected signal.
38 * issue a PASS message
39 * otherwise
40 * issue a FAIL message
41 * call cleanup
42 *
43 * USAGE
44 * kill06 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
45 * where, -c n : Run n copies concurrently.
46 * -f : Turn off functionality Testing.
47 * -i n : Execute test n times.
48 * -I x : Execute test for x seconds.
49 * -P x : Pause for x seconds between iterations.
50 * -t : Turn on syscall timing.
51 *
52 * HISTORY
53 * 07/2001 Ported by Wayne Boyer
54 *
55 * RESTRICTIONS
56 * This test should be run as a non-root user.
57 */
58
59#include "test.h"
plars865695b2001-08-27 22:15:12 +000060
61#include <signal.h>
62#include <errno.h>
63#include <sys/wait.h>
64
65void cleanup(void);
66void setup(void);
robbiewd34d5812005-07-11 22:28:09 +000067void do_child(void);
plars865695b2001-08-27 22:15:12 +000068
subrata_modak56207ce2009-03-23 13:35:39 +000069char *TCID = "kill06";
plars865695b2001-08-27 22:15:12 +000070int TST_TOTAL = 1;
71
plars865695b2001-08-27 22:15:12 +000072#define TEST_SIG SIGKILL
73
robbiewb8360ee2003-03-26 22:04:58 +000074int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000075{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020076 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020077 const char *msg;
plars865695b2001-08-27 22:15:12 +000078 pid_t pid1, pid2;
79 int exno, status, nsig, i;
80
Garrett Cooper45e285d2010-11-22 12:19:25 -080081 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080082 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000083 }
robbiewd34d5812005-07-11 22:28:09 +000084#ifdef UCLINUX
85 maybe_run_child(&do_child, "");
86#endif
87
subrata_modak56207ce2009-03-23 13:35:39 +000088 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000089
90 /* The following loop checks looping state if -i option given */
91 for (lc = 0; TEST_LOOPING(lc); lc++) {
92
Caspar Zhangd59a6592013-03-07 14:59:12 +080093 /* reset tst_count in case we are looping */
94 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000095 status = 1;
96 exno = 1;
97
98 /* Fork a process and set the process group so that */
99 /* it is different from this one. Fork 5 more children. */
100
robbiewd34d5812005-07-11 22:28:09 +0000101 pid1 = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000102 if (pid1 < 0) {
103 tst_brkm(TBROK, cleanup, "Fork of first child failed");
104 } else if (pid1 == 0) {
105 setpgrp();
106 for (i = 0; i < 5; i++) {
robbiewd34d5812005-07-11 22:28:09 +0000107 pid2 = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000108 if (pid2 < 0) {
109 tst_brkm(TBROK, cleanup, "Fork failed");
110 } else if (pid2 == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000111#ifdef UCLINUX
112 if (self_exec(av[0], "") < 0) {
113 tst_brkm(TBROK, cleanup,
114 "self_exec of "
115 "child failed");
116 }
117#else
118 do_child();
119#endif
plars865695b2001-08-27 22:15:12 +0000120 }
121 }
122 /* Kill all processes in this process group */
subrata_modak1f3a6972008-03-19 09:52:36 +0000123 TEST(kill(-getpgrp(), TEST_SIG));
plars865695b2001-08-27 22:15:12 +0000124 sleep(300);
Garrett Cooper2c282152010-12-16 00:55:50 -0800125
Wanlong Gao354ebb42012-12-07 10:10:04 +0800126 tst_resm(TINFO, "%d never recieved a"
127 " signal", getpid());
plars865695b2001-08-27 22:15:12 +0000128 exit(exno);
129 } else {
130 waitpid(pid1, &status, 0);
131 if (TEST_RETURN != 0) {
132 tst_brkm(TFAIL, cleanup, "%s failed - errno = "
subrata_modak56207ce2009-03-23 13:35:39 +0000133 "%d : %s", TCID, TEST_ERRNO,
134 strerror(TEST_ERRNO));
plars865695b2001-08-27 22:15:12 +0000135 }
136 }
137
Cyril Hrubise38b9612014-06-02 17:20:57 +0200138 /*
139 * Check to see if the process was terminated with the
140 * expected signal.
141 */
142 nsig = WTERMSIG(status);
143 if (!nsig) {
144 tst_resm(TFAIL, "Did not receive any signal");
145 } else if (nsig == TEST_SIG) {
146 tst_resm(TPASS, "received expected signal %d",
147 nsig);
148 } else {
149 tst_resm(TFAIL,
150 "expected signal %d received %d",
151 TEST_SIG, nsig);
plars865695b2001-08-27 22:15:12 +0000152 }
153 }
plars865695b2001-08-27 22:15:12 +0000154
Cyril Hrubise38b9612014-06-02 17:20:57 +0200155 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800156 tst_exit();
plars865695b2001-08-27 22:15:12 +0000157}
158
robbiewd34d5812005-07-11 22:28:09 +0000159/*
160 * do_child()
161 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400162void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000163{
164 int exno = 1;
165
166 sleep(299);
Garrett Cooper2c282152010-12-16 00:55:50 -0800167
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 tst_resm(TINFO, "%d never recieved a" " signal", getpid());
robbiewd34d5812005-07-11 22:28:09 +0000169 exit(exno);
170}
plars865695b2001-08-27 22:15:12 +0000171
172/*
173 * setup() - performs all ONE TIME setup for this test
174 */
subrata_modak56207ce2009-03-23 13:35:39 +0000175void setup(void)
plars865695b2001-08-27 22:15:12 +0000176{
177 /* Setup default signal handling */
178 tst_sig(FORK, DEF_HANDLER, cleanup);
179
plars865695b2001-08-27 22:15:12 +0000180 TEST_PAUSE;
181}
182
183/*
184 * cleanup() - performs all the ONE TIME cleanup for this test at completion
185 * or premature exit.
186 */
subrata_modak56207ce2009-03-23 13:35:39 +0000187void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000188{
plars865695b2001-08-27 22:15:12 +0000189
Chris Dearmanec6edca2012-10-17 19:54:01 -0700190}