Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
Denis Vlasenko | 02f47e9 | 2007-05-06 22:48:55 +0000 | [diff] [blame] | 3 | * Mini kill/killall[5] implementation for busybox |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 9 | */ |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 10 | //config:config KILL |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 11 | //config: bool "kill (3.1 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 12 | //config: default y |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: The command kill sends the specified signal to the specified |
| 15 | //config: process or process group. If no signal is specified, the TERM |
| 16 | //config: signal is sent. |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 17 | //config: |
| 18 | //config:config KILLALL |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 19 | //config: bool "killall (5.6 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 20 | //config: default y |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 21 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 22 | //config: killall sends a signal to all processes running any of the |
| 23 | //config: specified commands. If no signal name is specified, SIGTERM is |
| 24 | //config: sent. |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 25 | //config: |
| 26 | //config:config KILLALL5 |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 27 | //config: bool "killall5 (5.3 kb)" |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 28 | //config: default y |
Denys Vlasenko | 5467d26 | 2016-11-23 06:43:46 +0100 | [diff] [blame] | 29 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 30 | //config: The SystemV killall command. killall5 sends a signal |
| 31 | //config: to all processes except kernel threads and the processes |
| 32 | //config: in its own session, so it won't kill the shell that is running |
| 33 | //config: the script it was called from. |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 34 | |
Denys Vlasenko | 39194f0 | 2017-08-03 19:00:01 +0200 | [diff] [blame] | 35 | //applet:IF_KILL( APPLET_NOFORK(kill, kill, BB_DIR_BIN, BB_SUID_DROP, kill)) |
| 36 | // APPLET_NOFORK:name main location suid_type help |
| 37 | //applet:IF_KILLALL( APPLET_NOFORK(killall, kill, BB_DIR_USR_BIN, BB_SUID_DROP, killall)) |
| 38 | //applet:IF_KILLALL5(APPLET_NOFORK(killall5, kill, BB_DIR_USR_SBIN, BB_SUID_DROP, killall5)) |
Denys Vlasenko | f8f81ed | 2016-11-23 06:23:44 +0100 | [diff] [blame] | 39 | |
| 40 | //kbuild:lib-$(CONFIG_KILL) += kill.o |
| 41 | //kbuild:lib-$(CONFIG_KILLALL) += kill.o |
| 42 | //kbuild:lib-$(CONFIG_KILLALL5) += kill.o |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 43 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 44 | //usage:#define kill_trivial_usage |
| 45 | //usage: "[-l] [-SIG] PID..." |
| 46 | //usage:#define kill_full_usage "\n\n" |
| 47 | //usage: "Send a signal (default: TERM) to given PIDs\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 48 | //usage: "\n -l List all signal names and numbers" |
| 49 | /* //usage: "\n -s SIG Yet another way of specifying SIG" */ |
| 50 | //usage: |
| 51 | //usage:#define kill_example_usage |
| 52 | //usage: "$ ps | grep apache\n" |
| 53 | //usage: "252 root root S [apache]\n" |
| 54 | //usage: "263 www-data www-data S [apache]\n" |
| 55 | //usage: "264 www-data www-data S [apache]\n" |
| 56 | //usage: "265 www-data www-data S [apache]\n" |
| 57 | //usage: "266 www-data www-data S [apache]\n" |
| 58 | //usage: "267 www-data www-data S [apache]\n" |
| 59 | //usage: "$ kill 252\n" |
| 60 | //usage: |
| 61 | //usage:#define killall_trivial_usage |
Denys Vlasenko | e2b9215 | 2021-06-14 20:47:20 +0200 | [diff] [blame^] | 62 | //usage: "[-lq] [-SIG] PROCESS_NAME..." |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 63 | //usage:#define killall_full_usage "\n\n" |
| 64 | //usage: "Send a signal (default: TERM) to given processes\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 65 | //usage: "\n -l List all signal names and numbers" |
| 66 | /* //usage: "\n -s SIG Yet another way of specifying SIG" */ |
| 67 | //usage: "\n -q Don't complain if no processes were killed" |
| 68 | //usage: |
| 69 | //usage:#define killall_example_usage |
| 70 | //usage: "$ killall apache\n" |
| 71 | //usage: |
| 72 | //usage:#define killall5_trivial_usage |
| 73 | //usage: "[-l] [-SIG] [-o PID]..." |
| 74 | //usage:#define killall5_full_usage "\n\n" |
| 75 | //usage: "Send a signal (default: TERM) to all processes outside current session\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 76 | //usage: "\n -l List all signal names and numbers" |
| 77 | //usage: "\n -o PID Don't signal this PID" |
| 78 | /* //usage: "\n -s SIG Yet another way of specifying SIG" */ |
| 79 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 80 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 82 | /* Note: kill_main is directly called from shell in order to implement |
| 83 | * kill built-in. Shell substitutes job ids with process groups first. |
| 84 | * |
| 85 | * This brings some complications: |
| 86 | * |
| 87 | * + we can't use xfunc here |
| 88 | * + we can't use applet_name |
| 89 | * + we can't use bb_show_usage |
Denys Vlasenko | 39194f0 | 2017-08-03 19:00:01 +0200 | [diff] [blame] | 90 | * (doesn't apply for killall[5], still should be careful b/c NOFORK) |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 91 | * |
| 92 | * kill %n gets translated into kill ' -<process group>' by shell (note space!) |
| 93 | * This is needed to avoid collision with kill -9 ... syntax |
| 94 | */ |
| 95 | |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 96 | //kbuild:lib-$(CONFIG_ASH_JOB_CONTROL) += kill.o |
| 97 | //kbuild:lib-$(CONFIG_HUSH_KILL) += kill.o |
| 98 | |
| 99 | #define SH_KILL (ENABLE_ASH_JOB_CONTROL || ENABLE_HUSH_KILL) |
| 100 | /* If shells want to have "kill", for ifdefs it's like ENABLE_KILL=1 */ |
| 101 | #if SH_KILL |
| 102 | # undef ENABLE_KILL |
| 103 | # define ENABLE_KILL 1 |
| 104 | #endif |
| 105 | #define KILL_APPLET_CNT (ENABLE_KILL + ENABLE_KILLALL + ENABLE_KILLALL5) |
| 106 | |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 107 | int kill_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 108 | { |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 109 | char *arg; |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 110 | pid_t pid; |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 111 | int signo = SIGTERM, errors = 0; |
| 112 | #if ENABLE_KILL || ENABLE_KILLALL |
| 113 | int quiet = 0; |
| 114 | #endif |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 115 | |
| 116 | #if KILL_APPLET_CNT == 1 |
| 117 | # define is_killall ENABLE_KILLALL |
| 118 | # define is_killall5 ENABLE_KILLALL5 |
Denis Vlasenko | 02f47e9 | 2007-05-06 22:48:55 +0000 | [diff] [blame] | 119 | #else |
| 120 | /* How to determine who we are? find 3rd char from the end: |
| 121 | * kill, killall, killall5 |
Denis Vlasenko | 0cacc80 | 2007-05-06 22:51:52 +0000 | [diff] [blame] | 122 | * ^i ^a ^l - it's unique |
| 123 | * (checking from the start is complicated by /bin/kill... case) */ |
Denis Vlasenko | 02f47e9 | 2007-05-06 22:48:55 +0000 | [diff] [blame] | 124 | const char char3 = argv[0][strlen(argv[0]) - 3]; |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 125 | # define is_killall (ENABLE_KILLALL && char3 == 'a') |
| 126 | # define is_killall5 (ENABLE_KILLALL5 && char3 == 'l') |
Denis Vlasenko | 02f47e9 | 2007-05-06 22:48:55 +0000 | [diff] [blame] | 127 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 128 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 129 | /* Parse any options */ |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 130 | arg = *++argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 131 | |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 132 | if (!arg || arg[0] != '-') { |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 133 | goto do_it_now; |
| 134 | } |
| 135 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 136 | /* The -l option, which prints out signal names. |
| 137 | * Intended usage in shell: |
| 138 | * echo "Died of SIG`kill -l $?`" |
| 139 | * We try to mimic what kill from coreutils-6.8 does */ |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 140 | if (arg[1] == 'l' && arg[2] == '\0') { |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 141 | arg = *++argv; |
| 142 | if (!arg) { |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 143 | /* Print the whole signal list */ |
Denis Vlasenko | a4f4de9 | 2007-09-30 16:32:01 +0000 | [diff] [blame] | 144 | print_signames(); |
| 145 | return 0; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 146 | } |
| 147 | /* -l <sig list> */ |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 148 | do { |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 149 | if (isdigit(arg[0])) { |
| 150 | signo = bb_strtou(arg, NULL, 10); |
| 151 | if (errno) { |
| 152 | bb_error_msg("unknown signal '%s'", arg); |
| 153 | return EXIT_FAILURE; |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 154 | } |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 155 | /* Exitcodes >= 0x80 are to be treated |
| 156 | * as "killed by signal (exitcode & 0x7f)" */ |
| 157 | puts(get_signame(signo & 0x7f)); |
| 158 | /* TODO: 'bad' signal# - coreutils says: |
| 159 | * kill: 127: invalid signal |
| 160 | * we just print "127" instead */ |
| 161 | } else { |
| 162 | signo = get_signum(arg); |
| 163 | if (signo < 0) { |
| 164 | bb_error_msg("unknown signal '%s'", arg); |
| 165 | return EXIT_FAILURE; |
| 166 | } |
| 167 | printf("%d\n", signo); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 168 | } |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 169 | arg = *++argv; |
| 170 | } while (arg); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 171 | return EXIT_SUCCESS; |
| 172 | } |
| 173 | |
| 174 | /* The -q quiet option */ |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 175 | if (is_killall && arg[1] == 'q' && arg[2] == '\0') { |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 176 | #if ENABLE_KILL || ENABLE_KILLALL |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 177 | quiet = 1; |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 178 | #endif |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 179 | arg = *++argv; |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 180 | if (!arg) |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 181 | bb_show_usage(); |
| 182 | if (arg[0] != '-') |
| 183 | goto do_it_now; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 184 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 185 | |
Denis Vlasenko | b9b344a | 2008-10-31 00:30:48 +0000 | [diff] [blame] | 186 | arg++; /* skip '-' */ |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 187 | |
| 188 | /* -o PID? (if present, it always is at the end of command line) */ |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 189 | if (is_killall5 && arg[0] == 'o') |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 190 | goto do_it_now; |
| 191 | |
Ron Yorston | 0b6ec06 | 2017-08-27 08:25:18 +0100 | [diff] [blame] | 192 | /* "--" separates options from args. Testcase: "kill -- -123" */ |
| 193 | if (!is_killall5 && arg[0] == '-' && arg[1] == '\0') |
| 194 | goto do_it_sooner; |
| 195 | |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 196 | if (argv[1] && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */ |
Denis Vlasenko | b9b344a | 2008-10-31 00:30:48 +0000 | [diff] [blame] | 197 | arg = *++argv; |
| 198 | } /* else it must be -SIG */ |
| 199 | signo = get_signum(arg); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 200 | if (signo < 0) { |
Denis Vlasenko | b9b344a | 2008-10-31 00:30:48 +0000 | [diff] [blame] | 201 | bb_error_msg("bad signal name '%s'", arg); |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 202 | return EXIT_FAILURE; |
| 203 | } |
Ron Yorston | 0b6ec06 | 2017-08-27 08:25:18 +0100 | [diff] [blame] | 204 | do_it_sooner: |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 205 | arg = *++argv; |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 206 | |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 207 | do_it_now: |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 208 | pid = getpid(); |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 209 | |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 210 | if (is_killall5) { |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 211 | pid_t sid; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 212 | procps_status_t* p = NULL; |
Uros Vampl | 3ac1e0d | 2013-12-16 17:44:58 +0100 | [diff] [blame] | 213 | /* compat: exitcode 2 is "no one was signaled" */ |
Denys Vlasenko | d9eb40c | 2017-04-12 15:48:19 +0200 | [diff] [blame] | 214 | errors = 2; |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 215 | |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 216 | /* Find out our session id */ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 217 | sid = getsid(pid); |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 218 | /* Stop all processes */ |
Denys Vlasenko | 1485030 | 2012-04-03 08:16:05 +0200 | [diff] [blame] | 219 | if (signo != SIGSTOP && signo != SIGCONT) |
| 220 | kill(-1, SIGSTOP); |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 221 | /* Signal all processes except those in our session */ |
Alexey Fomenko | 6a93212 | 2011-12-22 11:38:57 +0100 | [diff] [blame] | 222 | while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID)) != NULL) { |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 223 | char **args; |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 224 | |
| 225 | if (p->sid == (unsigned)sid |
Uros Vampl | 3ac1e0d | 2013-12-16 17:44:58 +0100 | [diff] [blame] | 226 | || p->sid == 0 /* compat: kernel thread, don't signal it */ |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 227 | || p->pid == (unsigned)pid |
Alexey Fomenko | 6a93212 | 2011-12-22 11:38:57 +0100 | [diff] [blame] | 228 | || p->pid == 1 |
| 229 | ) { |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 230 | continue; |
Alexey Fomenko | 6a93212 | 2011-12-22 11:38:57 +0100 | [diff] [blame] | 231 | } |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 232 | |
| 233 | /* All remaining args must be -o PID options. |
| 234 | * Check p->pid against them. */ |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 235 | args = argv; |
| 236 | while (*args) { |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 237 | pid_t omit; |
| 238 | |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 239 | arg = *args++; |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 240 | if (arg[0] != '-' || arg[1] != 'o') { |
| 241 | bb_error_msg("bad option '%s'", arg); |
Denys Vlasenko | d9eb40c | 2017-04-12 15:48:19 +0200 | [diff] [blame] | 242 | errors = 1; |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 243 | goto resume; |
| 244 | } |
| 245 | arg += 2; |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 246 | if (!arg[0] && *args) |
| 247 | arg = *args++; |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 248 | omit = bb_strtoi(arg, NULL, 10); |
| 249 | if (errno) { |
Denys Vlasenko | b32a543 | 2010-08-29 13:29:02 +0200 | [diff] [blame] | 250 | bb_error_msg("invalid number '%s'", arg); |
Denys Vlasenko | d9eb40c | 2017-04-12 15:48:19 +0200 | [diff] [blame] | 251 | errors = 1; |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 252 | goto resume; |
| 253 | } |
| 254 | if (p->pid == omit) |
| 255 | goto dont_kill; |
| 256 | } |
| 257 | kill(p->pid, signo); |
Denys Vlasenko | d9eb40c | 2017-04-12 15:48:19 +0200 | [diff] [blame] | 258 | errors = 0; |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 259 | dont_kill: ; |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 260 | } |
Denis Vlasenko | a0ab943 | 2009-02-07 22:30:39 +0000 | [diff] [blame] | 261 | resume: |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 262 | /* And let them continue */ |
Denys Vlasenko | 1485030 | 2012-04-03 08:16:05 +0200 | [diff] [blame] | 263 | if (signo != SIGSTOP && signo != SIGCONT) |
| 264 | kill(-1, SIGCONT); |
Denys Vlasenko | d9eb40c | 2017-04-12 15:48:19 +0200 | [diff] [blame] | 265 | return errors; |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 268 | #if ENABLE_KILL || ENABLE_KILLALL |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 269 | /* Pid or name is required for kill/killall */ |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 270 | if (!arg) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 271 | bb_simple_error_msg("you need to specify whom to kill"); |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 272 | return EXIT_FAILURE; |
| 273 | } |
"Vladimir N. Oleynik" | 1e98a07 | 2006-01-25 13:21:08 +0000 | [diff] [blame] | 274 | |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 275 | if (!ENABLE_KILL || is_killall) { |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 276 | /* Looks like they want to do a killall. Do that */ |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 277 | do { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 278 | pid_t* pidList; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 279 | |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 280 | pidList = find_pid_by_name(arg); |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 281 | if (*pidList == 0) { |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 282 | errors++; |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 283 | if (!quiet) |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 284 | bb_error_msg("%s: no process killed", arg); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 285 | } else { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 286 | pid_t *pl; |
Glenn L McGrath | bb2e9d4 | 2002-11-25 22:12:28 +0000 | [diff] [blame] | 287 | |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 288 | for (pl = pidList; *pl; pl++) { |
| 289 | if (*pl == pid) |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 290 | continue; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 291 | if (kill(*pl, signo) == 0) |
| 292 | continue; |
| 293 | errors++; |
| 294 | if (!quiet) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 295 | bb_perror_msg("can't kill pid %d", (int)*pl); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 296 | } |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 297 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 298 | free(pidList); |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 299 | arg = *++argv; |
Denys Vlasenko | 4b26f82 | 2013-12-16 17:45:44 +0100 | [diff] [blame] | 300 | } while (arg); |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 301 | return errors; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 302 | } |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 303 | #endif |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 304 | |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 305 | #if ENABLE_KILL |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 306 | /* Looks like they want to do a kill. Do that */ |
| 307 | while (arg) { |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 308 | # if SH_KILL |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 309 | /* |
| 310 | * We need to support shell's "hack formats" of |
| 311 | * " -PRGP_ID" (yes, with a leading space) |
| 312 | * and " PID1 PID2 PID3" (with degenerate case "") |
| 313 | */ |
| 314 | while (*arg != '\0') { |
| 315 | char *end; |
| 316 | if (*arg == ' ') |
| 317 | arg++; |
| 318 | pid = bb_strtoi(arg, &end, 10); |
| 319 | if (errno && (errno != EINVAL || *end != ' ')) { |
| 320 | bb_error_msg("invalid number '%s'", arg); |
| 321 | errors++; |
Alexey Fomenko | 6a93212 | 2011-12-22 11:38:57 +0100 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | if (kill(pid, signo) != 0) { |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 325 | bb_perror_msg("can't kill pid %d", (int)pid); |
| 326 | errors++; |
| 327 | } |
| 328 | arg = end; /* can only point to ' ' or '\0' now */ |
| 329 | } |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 330 | # else /* ENABLE_KILL but !SH_KILL */ |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 331 | pid = bb_strtoi(arg, NULL, 10); |
| 332 | if (errno) { |
Denys Vlasenko | b32a543 | 2010-08-29 13:29:02 +0200 | [diff] [blame] | 333 | bb_error_msg("invalid number '%s'", arg); |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 334 | errors++; |
| 335 | } else if (kill(pid, signo) != 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 336 | bb_perror_msg("can't kill pid %d", (int)pid); |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 337 | errors++; |
| 338 | } |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 339 | # endif |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 340 | arg = *++argv; |
| 341 | } |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 342 | return errors; |
Kang-Che Sung | 61a91af | 2017-01-09 18:46:58 +0100 | [diff] [blame] | 343 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 344 | } |