1
/* vi: set sw=4 ts=4: */
3
* Mini kill/killall[5] implementation for busybox
2
* Mini kill implementation for busybox
5
4
* Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
8
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
11
//config: bool "kill (3.4 kb)"
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.
18
//config:config KILLALL
19
//config: bool "killall (5.9 kb)"
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
26
//config:config KILLALL5
27
//config: bool "killall5 (5.6 kb)"
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.
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))
40
//kbuild:lib-$(CONFIG_KILL) += kill.o
41
//kbuild:lib-$(CONFIG_KILLALL) += kill.o
42
//kbuild:lib-$(CONFIG_KILLALL5) += kill.o
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"
48
//usage: "\n -l List all signal names and numbers"
49
/* //usage: "\n -s SIG Yet another way of specifying SIG" */
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"
61
//usage:#define killall_trivial_usage
62
//usage: "[-lq] [-SIG] PROCESS_NAME..."
63
//usage:#define killall_full_usage "\n\n"
64
//usage: "Send a signal (default: TERM) to given processes\n"
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"
69
//usage:#define killall_example_usage
70
//usage: "$ killall apache\n"
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"
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" */
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.
85
* This brings some complications:
87
* + we can't use xfunc here
88
* + we can't use bb_show_usage
89
* + applet_name can be the name of the shell
90
* (doesn't apply for killall[5], still should be careful b/c NOFORK)
92
* kill %n gets translated into kill ' -<process group>' by shell (note space!)
93
* This is needed to avoid collision with kill -9 ... syntax
96
//kbuild:lib-$(CONFIG_ASH_JOB_CONTROL) += kill.o
97
//kbuild:lib-$(CONFIG_HUSH_KILL) += kill.o
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 */
103
# define ENABLE_KILL 1
105
#define KILL_APPLET_CNT (ENABLE_KILL + ENABLE_KILLALL + ENABLE_KILLALL5)
107
int kill_main(int argc UNUSED_PARAM, char **argv)
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30
const char kill_usage[] = "kill [-signal] process-id [process-id ...]\n";
37
const struct signal_name signames[] = {
47
#if defined(__sparc__) || defined(__alpha__)
54
#if defined(__sparc__) || defined(__alpha__)
60
#if defined(__sparc__) || defined(__alpha__)
68
#if defined(__sparc__) || defined(__alpha__)
82
{"VTALRM", SIGVTALRM},
93
{"STKFLT", SIGSTKFLT},
103
{"VTALRM", SIGVTALRM},
109
{"UNUSED", SIGUNUSED},
114
extern int kill_main (int argc, char **argv)
111
int signo = SIGTERM, errors = 0;
112
#if ENABLE_KILL || ENABLE_KILLALL
116
#if KILL_APPLET_CNT == 1
117
# define is_killall ENABLE_KILLALL
118
# define is_killall5 ENABLE_KILLALL5
120
/* How to determine who we are? find 3rd char from the end:
121
* kill, killall, killall5
122
* ^i ^a ^l - it's unique
123
* (checking from the start is complicated by /bin/kill... case) */
124
const char char3 = argv[0][strlen(argv[0]) - 3];
125
# define is_killall (ENABLE_KILLALL && char3 == 'a')
126
# define is_killall5 (ENABLE_KILLALL5 && char3 == 'l')
129
/* Parse any options */
132
if (!arg || arg[0] != '-') {
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 */
140
if (arg[1] == 'l' && arg[2] == '\0') {
143
/* Print the whole signal list */
149
if (isdigit(arg[0])) {
150
signo = bb_strtou(arg, NULL, 10);
152
bb_error_msg("unknown signal '%s'", arg);
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 */
162
signo = get_signum(arg);
164
bb_error_msg("unknown signal '%s'", arg);
167
printf("%d\n", signo);
174
/* The -q quiet option */
175
if (is_killall && arg[1] == 'q' && arg[2] == '\0') {
176
#if ENABLE_KILL || ENABLE_KILLALL
186
arg++; /* skip '-' */
188
/* -o PID? (if present, it always is at the end of command line) */
189
if (is_killall5 && arg[0] == 'o')
192
/* "--" separates options from args. Testcase: "kill -- -123" */
193
if (!is_killall5 && arg[0] == '-' && arg[1] == '\0')
196
if (argv[1] && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
198
} /* else it must be -SIG */
199
signo = get_signum(arg);
201
bb_error_msg("bad signal name '%s'", arg);
212
procps_status_t* p = NULL;
213
/* compat: exitcode 2 is "no one was signaled" */
216
/* Find out our session id */
218
/* Stop all processes */
219
if (signo != SIGSTOP && signo != SIGCONT)
221
/* Signal all processes except those in our session */
222
while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID)) != NULL) {
225
if (p->sid == (unsigned)sid
226
|| p->sid == 0 /* compat: kernel thread, don't signal it */
227
|| p->pid == (unsigned)pid
233
/* All remaining args must be -o PID options.
234
* Check p->pid against them. */
240
if (arg[0] != '-' || arg[1] != 'o') {
241
bb_error_msg("bad option '%s'", arg);
246
if (!arg[0] && *args)
248
omit = bb_strtoi(arg, NULL, 10);
250
bb_error_msg("invalid number '%s'", arg);
262
/* And let them continue */
263
if (signo != SIGSTOP && signo != SIGCONT)
268
#if ENABLE_KILL || ENABLE_KILLALL
269
/* Pid or name is required for kill/killall */
271
bb_simple_error_msg("you need to specify whom to kill");
275
if (!ENABLE_KILL || is_killall) {
276
/* Looks like they want to do a killall. Do that */
280
pidList = find_pid_by_name(arg);
284
bb_error_msg("%s: no process killed", arg);
288
for (pl = pidList; *pl; pl++) {
291
if (kill(*pl, signo) == 0)
295
bb_perror_msg("can't kill pid %d", (int)*pl);
306
/* Looks like they want to do a kill. Do that */
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 "")
314
while (*arg != '\0') {
318
pid = bb_strtoi(arg, &end, 10);
319
if (errno && (errno != EINVAL || *end != ' ')) {
320
bb_error_msg("invalid number '%s'", arg);
324
if (kill(pid, signo) != 0) {
325
bb_perror_msg("can't kill pid %d", (int)pid);
328
arg = end; /* can only point to ' ' or '\0' now */
330
# else /* ENABLE_KILL but !SH_KILL */
331
pid = bb_strtoi(arg, NULL, 10);
333
bb_error_msg("invalid number '%s'", arg);
335
} else if (kill(pid, signo) != 0) {
336
bb_perror_msg("can't kill pid %d", (int)pid);
121
if ( **(argv+1) == '-' ) {
122
if (isdigit( *(*(++argv)+1) )) {
124
if (sig < 0 || sig >= NSIG)
128
const struct signal_name *s = signames;
129
while (s->name != 0) {
130
if (strcasecmp (s->name, *argv+1) == 0) {
143
if (! isdigit( **(++argv))) {
144
fprintf(stderr, "bad PID: %s\n", *argv);
148
if (kill (pid, sig) != 0) {
155
fprintf(stderr, "bad signal name: %s\n", *argv);