blob: 09beefb2d378771d28e8c88277e05ab0f56f062d [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
Denis Vlasenko02f47e92007-05-06 22:48:55 +00003 * Mini kill/killall[5] implementation for busybox
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenc4996011999-10-20 22:08:37 +00009 */
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010010//config:config KILL
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "kill (2.6 kb)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010012//config: default y
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//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 Vlasenkof8f81ed2016-11-23 06:23:44 +010017//config:
18//config:config KILLALL
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020019//config: bool "killall (5.6 kb)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010020//config: default y
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010021//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020022//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 Vlasenkof8f81ed2016-11-23 06:23:44 +010025//config:
26//config:config KILLALL5
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020027//config: bool "killall5 (5.3 kb)"
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010028//config: default y
Denys Vlasenko5467d262016-11-23 06:43:46 +010029//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020030//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 Vlasenkof8f81ed2016-11-23 06:23:44 +010034
35//applet:IF_KILL(APPLET(kill, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenko205d48e2017-01-29 14:57:33 +010036// APPLET_ODDNAME:name main location suid_type help
37//applet:IF_KILLALL( APPLET_ODDNAME(killall, kill, BB_DIR_USR_BIN, BB_SUID_DROP, killall))
Denys Vlasenkof8f81ed2016-11-23 06:23:44 +010038//applet:IF_KILLALL5(APPLET_ODDNAME(killall5, kill, BB_DIR_USR_SBIN, BB_SUID_DROP, killall5))
39
40//kbuild:lib-$(CONFIG_KILL) += kill.o
41//kbuild:lib-$(CONFIG_KILLALL) += kill.o
42//kbuild:lib-$(CONFIG_KILLALL5) += kill.o
Eric Andersenc4996011999-10-20 22:08:37 +000043
Pere Orga5bc8c002011-04-11 03:29:49 +020044//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 Orga5bc8c002011-04-11 03:29:49 +020048//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
62//usage: "[-l] [-q] [-SIG] PROCESS_NAME..."
63//usage:#define killall_full_usage "\n\n"
64//usage: "Send a signal (default: TERM) to given processes\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020065//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 Orga5bc8c002011-04-11 03:29:49 +020076//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 Vlasenkob6adbf12007-05-26 19:00:18 +000080#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000081
Denis Vlasenkof20de5b2007-04-29 23:42:54 +000082/* 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
90 * (Above doesn't apply for killall[5] cases)
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 Sung61a91af2017-01-09 18:46:58 +010096//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 Vlasenko4b26f822013-12-16 17:45:44 +0100107int kill_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000108{
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000109 char *arg;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000110 pid_t pid;
111 int signo = SIGTERM, errors = 0, quiet = 0;
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100112
113#if KILL_APPLET_CNT == 1
114# define is_killall ENABLE_KILLALL
115# define is_killall5 ENABLE_KILLALL5
Denis Vlasenko02f47e92007-05-06 22:48:55 +0000116#else
117/* How to determine who we are? find 3rd char from the end:
118 * kill, killall, killall5
Denis Vlasenko0cacc802007-05-06 22:51:52 +0000119 * ^i ^a ^l - it's unique
120 * (checking from the start is complicated by /bin/kill... case) */
Denis Vlasenko02f47e92007-05-06 22:48:55 +0000121 const char char3 = argv[0][strlen(argv[0]) - 3];
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100122# define is_killall (ENABLE_KILLALL && char3 == 'a')
123# define is_killall5 (ENABLE_KILLALL5 && char3 == 'l')
Denis Vlasenko02f47e92007-05-06 22:48:55 +0000124#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000125
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 /* Parse any options */
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000127 arg = *++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100129 if (!arg || arg[0] != '-') {
Eric Andersen7d72e792003-07-26 07:41:56 +0000130 goto do_it_now;
131 }
132
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000133 /* The -l option, which prints out signal names.
134 * Intended usage in shell:
135 * echo "Died of SIG`kill -l $?`"
136 * We try to mimic what kill from coreutils-6.8 does */
Denis Vlasenkofa076802006-11-05 00:38:51 +0000137 if (arg[1] == 'l' && arg[2] == '\0') {
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100138 arg = *++argv;
139 if (!arg) {
Eric Andersen7d72e792003-07-26 07:41:56 +0000140 /* Print the whole signal list */
Denis Vlasenkoa4f4de92007-09-30 16:32:01 +0000141 print_signames();
142 return 0;
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000143 }
144 /* -l <sig list> */
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100145 do {
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000146 if (isdigit(arg[0])) {
147 signo = bb_strtou(arg, NULL, 10);
148 if (errno) {
149 bb_error_msg("unknown signal '%s'", arg);
150 return EXIT_FAILURE;
Rob Landleyc9c1a412006-07-12 19:17:55 +0000151 }
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000152 /* Exitcodes >= 0x80 are to be treated
153 * as "killed by signal (exitcode & 0x7f)" */
154 puts(get_signame(signo & 0x7f));
155 /* TODO: 'bad' signal# - coreutils says:
156 * kill: 127: invalid signal
157 * we just print "127" instead */
158 } else {
159 signo = get_signum(arg);
160 if (signo < 0) {
161 bb_error_msg("unknown signal '%s'", arg);
162 return EXIT_FAILURE;
163 }
164 printf("%d\n", signo);
Eric Andersen7d72e792003-07-26 07:41:56 +0000165 }
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100166 arg = *++argv;
167 } while (arg);
Eric Andersen7d72e792003-07-26 07:41:56 +0000168 return EXIT_SUCCESS;
169 }
170
171 /* The -q quiet option */
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100172 if (is_killall && arg[1] == 'q' && arg[2] == '\0') {
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000173 quiet = 1;
174 arg = *++argv;
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100175 if (!arg)
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000176 bb_show_usage();
177 if (arg[0] != '-')
178 goto do_it_now;
Eric Andersencc8ed391999-10-05 16:24:54 +0000179 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000180
Denis Vlasenkob9b344a2008-10-31 00:30:48 +0000181 arg++; /* skip '-' */
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000182
183 /* -o PID? (if present, it always is at the end of command line) */
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100184 if (is_killall5 && arg[0] == 'o')
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000185 goto do_it_now;
186
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100187 if (argv[1] && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
Denis Vlasenkob9b344a2008-10-31 00:30:48 +0000188 arg = *++argv;
189 } /* else it must be -SIG */
190 signo = get_signum(arg);
Denys Vlasenko86981e32017-07-25 20:06:17 +0200191 if (signo < 0) {
Denis Vlasenkob9b344a2008-10-31 00:30:48 +0000192 bb_error_msg("bad signal name '%s'", arg);
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000193 return EXIT_FAILURE;
194 }
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000195 arg = *++argv;
Eric Andersen7d72e792003-07-26 07:41:56 +0000196
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000197 do_it_now:
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000198 pid = getpid();
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000199
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100200 if (is_killall5) {
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000201 pid_t sid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000202 procps_status_t* p = NULL;
Uros Vampl3ac1e0d2013-12-16 17:44:58 +0100203 /* compat: exitcode 2 is "no one was signaled" */
Denys Vlasenkod9eb40c2017-04-12 15:48:19 +0200204 errors = 2;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000205
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000206 /* Find out our session id */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000207 sid = getsid(pid);
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000208 /* Stop all processes */
Denys Vlasenko14850302012-04-03 08:16:05 +0200209 if (signo != SIGSTOP && signo != SIGCONT)
210 kill(-1, SIGSTOP);
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000211 /* Signal all processes except those in our session */
Alexey Fomenko6a932122011-12-22 11:38:57 +0100212 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID)) != NULL) {
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100213 char **args;
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000214
215 if (p->sid == (unsigned)sid
Uros Vampl3ac1e0d2013-12-16 17:44:58 +0100216 || p->sid == 0 /* compat: kernel thread, don't signal it */
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000217 || p->pid == (unsigned)pid
Alexey Fomenko6a932122011-12-22 11:38:57 +0100218 || p->pid == 1
219 ) {
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000220 continue;
Alexey Fomenko6a932122011-12-22 11:38:57 +0100221 }
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000222
223 /* All remaining args must be -o PID options.
224 * Check p->pid against them. */
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100225 args = argv;
226 while (*args) {
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000227 pid_t omit;
228
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100229 arg = *args++;
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000230 if (arg[0] != '-' || arg[1] != 'o') {
231 bb_error_msg("bad option '%s'", arg);
Denys Vlasenkod9eb40c2017-04-12 15:48:19 +0200232 errors = 1;
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000233 goto resume;
234 }
235 arg += 2;
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100236 if (!arg[0] && *args)
237 arg = *args++;
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000238 omit = bb_strtoi(arg, NULL, 10);
239 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200240 bb_error_msg("invalid number '%s'", arg);
Denys Vlasenkod9eb40c2017-04-12 15:48:19 +0200241 errors = 1;
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000242 goto resume;
243 }
244 if (p->pid == omit)
245 goto dont_kill;
246 }
247 kill(p->pid, signo);
Denys Vlasenkod9eb40c2017-04-12 15:48:19 +0200248 errors = 0;
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000249 dont_kill: ;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000250 }
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000251 resume:
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000252 /* And let them continue */
Denys Vlasenko14850302012-04-03 08:16:05 +0200253 if (signo != SIGSTOP && signo != SIGCONT)
254 kill(-1, SIGCONT);
Denys Vlasenkod9eb40c2017-04-12 15:48:19 +0200255 return errors;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000256 }
257
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100258#if ENABLE_KILL || ENABLE_KILLALL
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000259 /* Pid or name is required for kill/killall */
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100260 if (!arg) {
Denis Vlasenko1fe4e9e2007-11-15 00:57:40 +0000261 bb_error_msg("you need to specify whom to kill");
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000262 return EXIT_FAILURE;
263 }
"Vladimir N. Oleynik"1e98a072006-01-25 13:21:08 +0000264
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100265 if (!ENABLE_KILL || is_killall) {
Erik Andersen246cc6d2000-03-07 07:41:42 +0000266 /* Looks like they want to do a killall. Do that */
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100267 do {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000268 pid_t* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000269
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000270 pidList = find_pid_by_name(arg);
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000271 if (*pidList == 0) {
Eric Andersenc38678d2002-09-16 06:22:25 +0000272 errors++;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000273 if (!quiet)
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000274 bb_error_msg("%s: no process killed", arg);
Eric Andersen7d72e792003-07-26 07:41:56 +0000275 } else {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000276 pid_t *pl;
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +0000277
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000278 for (pl = pidList; *pl; pl++) {
279 if (*pl == pid)
Eric Andersen7d72e792003-07-26 07:41:56 +0000280 continue;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000281 if (kill(*pl, signo) == 0)
282 continue;
283 errors++;
284 if (!quiet)
Denys Vlasenko651a2692010-03-23 16:25:17 +0100285 bb_perror_msg("can't kill pid %d", (int)*pl);
Eric Andersen7d72e792003-07-26 07:41:56 +0000286 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000287 }
Eric Andersen44608e92002-10-22 12:21:15 +0000288 free(pidList);
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000289 arg = *++argv;
Denys Vlasenko4b26f822013-12-16 17:45:44 +0100290 } while (arg);
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000291 return errors;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000292 }
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100293#endif
Rob Landleyc9c1a412006-07-12 19:17:55 +0000294
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100295#if ENABLE_KILL
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000296 /* Looks like they want to do a kill. Do that */
297 while (arg) {
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100298# if SH_KILL
Denys Vlasenkob12553f2011-02-21 03:22:20 +0100299 /*
300 * We need to support shell's "hack formats" of
301 * " -PRGP_ID" (yes, with a leading space)
302 * and " PID1 PID2 PID3" (with degenerate case "")
303 */
304 while (*arg != '\0') {
305 char *end;
306 if (*arg == ' ')
307 arg++;
308 pid = bb_strtoi(arg, &end, 10);
309 if (errno && (errno != EINVAL || *end != ' ')) {
310 bb_error_msg("invalid number '%s'", arg);
311 errors++;
Alexey Fomenko6a932122011-12-22 11:38:57 +0100312 break;
313 }
314 if (kill(pid, signo) != 0) {
Denys Vlasenkob12553f2011-02-21 03:22:20 +0100315 bb_perror_msg("can't kill pid %d", (int)pid);
316 errors++;
317 }
318 arg = end; /* can only point to ' ' or '\0' now */
319 }
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100320# else /* ENABLE_KILL but !SH_KILL */
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000321 pid = bb_strtoi(arg, NULL, 10);
322 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200323 bb_error_msg("invalid number '%s'", arg);
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000324 errors++;
325 } else if (kill(pid, signo) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100326 bb_perror_msg("can't kill pid %d", (int)pid);
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000327 errors++;
328 }
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100329# endif
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000330 arg = *++argv;
331 }
Eric Andersenc38678d2002-09-16 06:22:25 +0000332 return errors;
Kang-Che Sung61a91af2017-01-09 18:46:58 +0100333#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000334}