blob: f64327984be3224a879c29c88e10853da07be3a5 [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
nstrazfa31d552002-05-14 16:50:06 +000022 * kill08.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
25 * Test case to check the basic functionality of kill() when kill an
26 * entire process group.
27 *
28 * ALGORITHM
29 * call setup
30 * loop if the -i option was given
31 * fork 5 childeren
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
nstrazfa31d552002-05-14 16:50:06 +000044 * kill08 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000045 * 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 = "kill08";
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++) {
mridge161b01e2006-02-01 22:48:00 +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 */
123 TEST(kill(0, TEST_SIG));
124 pause();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800125 exit(exno);
plars865695b2001-08-27 22:15:12 +0000126 } else {
127 waitpid(pid1, &status, 0);
128 if (TEST_RETURN != 0) {
129 tst_brkm(TFAIL, cleanup, "%s failed - errno = "
subrata_modak56207ce2009-03-23 13:35:39 +0000130 "%d : %s", TCID, TEST_ERRNO,
131 strerror(TEST_ERRNO));
plars865695b2001-08-27 22:15:12 +0000132 }
133 }
134
Cyril Hrubise38b9612014-06-02 17:20:57 +0200135 /*
136 * Check to see if the process was terminated with the
137 * expected signal.
138 */
139 nsig = WTERMSIG(status);
140 if (nsig == TEST_SIG) {
141 tst_resm(TPASS, "received expected signal %d",
142 nsig);
plars865695b2001-08-27 22:15:12 +0000143 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200144 tst_resm(TFAIL,
145 "expected signal %d received %d",
146 TEST_SIG, nsig);
plars865695b2001-08-27 22:15:12 +0000147 }
148 }
plars865695b2001-08-27 22:15:12 +0000149
Cyril Hrubise38b9612014-06-02 17:20:57 +0200150 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800151 tst_exit();
plars865695b2001-08-27 22:15:12 +0000152}
153
robbiewd34d5812005-07-11 22:28:09 +0000154/*
155 * do_child()
156 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400157void do_child(void)
robbiewd34d5812005-07-11 22:28:09 +0000158{
159 int exno = 1;
160
161 pause();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800162 exit(exno);
robbiewd34d5812005-07-11 22:28:09 +0000163}
plars865695b2001-08-27 22:15:12 +0000164
165/*
166 * setup() - performs all ONE TIME setup for this test
167 */
subrata_modak56207ce2009-03-23 13:35:39 +0000168void setup(void)
plars865695b2001-08-27 22:15:12 +0000169{
Garrett Cooper2c282152010-12-16 00:55:50 -0800170
plars865695b2001-08-27 22:15:12 +0000171 TEST_PAUSE;
172}
173
174/*
175 * cleanup() - performs all the ONE TIME cleanup for this test at completion
176 * or premature exit.
177 */
subrata_modak56207ce2009-03-23 13:35:39 +0000178void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000179{
plars865695b2001-08-27 22:15:12 +0000180
Chris Dearmanec6edca2012-10-17 19:54:01 -0700181}