plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 22 | * kill08.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 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 |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 44 | * kill08 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
| 61 | #include <signal.h> |
| 62 | #include <errno.h> |
| 63 | #include <sys/wait.h> |
| 64 | |
| 65 | void cleanup(void); |
| 66 | void setup(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 67 | void do_child(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | char *TCID = "kill08"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | int TST_TOTAL = 1; |
| 71 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | #define TEST_SIG SIGKILL |
| 73 | |
robbiew | b8360ee | 2003-03-26 22:04:58 +0000 | [diff] [blame] | 74 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 76 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 77 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | pid_t pid1, pid2; |
| 79 | int exno, status, nsig, i; |
| 80 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 81 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 82 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 84 | #ifdef UCLINUX |
| 85 | maybe_run_child(&do_child, ""); |
| 86 | #endif |
| 87 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
| 90 | /* The following loop checks looping state if -i option given */ |
| 91 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 92 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 93 | /* reset tst_count in case we are looping */ |
| 94 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | 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 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 101 | pid1 = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | 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++) { |
mridge | 161b01e | 2006-02-01 22:48:00 +0000 | [diff] [blame] | 107 | pid2 = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | if (pid2 < 0) { |
| 109 | tst_brkm(TBROK, cleanup, "Fork failed"); |
| 110 | } else if (pid2 == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 111 | #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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | /* Kill all processes in this process group */ |
| 123 | TEST(kill(0, TEST_SIG)); |
| 124 | pause(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | exit(exno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | } else { |
| 127 | waitpid(pid1, &status, 0); |
| 128 | if (TEST_RETURN != 0) { |
| 129 | tst_brkm(TFAIL, cleanup, "%s failed - errno = " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | "%d : %s", TCID, TEST_ERRNO, |
| 131 | strerror(TEST_ERRNO)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 135 | /* |
| 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 144 | tst_resm(TFAIL, |
| 145 | "expected signal %d received %d", |
| 146 | TEST_SIG, nsig); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 150 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 151 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | } |
| 153 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 154 | /* |
| 155 | * do_child() |
| 156 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 157 | void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 158 | { |
| 159 | int exno = 1; |
| 160 | |
| 161 | pause(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 162 | exit(exno); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 163 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * setup() - performs all ONE TIME setup for this test |
| 167 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 168 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 170 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | TEST_PAUSE; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 176 | * or premature exit. |
| 177 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 178 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 181 | } |