blob: cd189bcd61a235648c1c5ce76b83a9405070e6a5 [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 */
10
Pere Orga5bc8c002011-04-11 03:29:49 +020011//usage:#define kill_trivial_usage
12//usage: "[-l] [-SIG] PID..."
13//usage:#define kill_full_usage "\n\n"
14//usage: "Send a signal (default: TERM) to given PIDs\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020015//usage: "\n -l List all signal names and numbers"
16/* //usage: "\n -s SIG Yet another way of specifying SIG" */
17//usage:
18//usage:#define kill_example_usage
19//usage: "$ ps | grep apache\n"
20//usage: "252 root root S [apache]\n"
21//usage: "263 www-data www-data S [apache]\n"
22//usage: "264 www-data www-data S [apache]\n"
23//usage: "265 www-data www-data S [apache]\n"
24//usage: "266 www-data www-data S [apache]\n"
25//usage: "267 www-data www-data S [apache]\n"
26//usage: "$ kill 252\n"
27//usage:
28//usage:#define killall_trivial_usage
29//usage: "[-l] [-q] [-SIG] PROCESS_NAME..."
30//usage:#define killall_full_usage "\n\n"
31//usage: "Send a signal (default: TERM) to given processes\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020032//usage: "\n -l List all signal names and numbers"
33/* //usage: "\n -s SIG Yet another way of specifying SIG" */
34//usage: "\n -q Don't complain if no processes were killed"
35//usage:
36//usage:#define killall_example_usage
37//usage: "$ killall apache\n"
38//usage:
39//usage:#define killall5_trivial_usage
40//usage: "[-l] [-SIG] [-o PID]..."
41//usage:#define killall5_full_usage "\n\n"
42//usage: "Send a signal (default: TERM) to all processes outside current session\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020043//usage: "\n -l List all signal names and numbers"
44//usage: "\n -o PID Don't signal this PID"
45/* //usage: "\n -s SIG Yet another way of specifying SIG" */
46
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000047#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000048
Denis Vlasenkof20de5b2007-04-29 23:42:54 +000049/* Note: kill_main is directly called from shell in order to implement
50 * kill built-in. Shell substitutes job ids with process groups first.
51 *
52 * This brings some complications:
53 *
54 * + we can't use xfunc here
55 * + we can't use applet_name
56 * + we can't use bb_show_usage
57 * (Above doesn't apply for killall[5] cases)
58 *
59 * kill %n gets translated into kill ' -<process group>' by shell (note space!)
60 * This is needed to avoid collision with kill -9 ... syntax
61 */
62
Rob Landleydfba7412006-03-06 20:47:33 +000063int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000064{
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000065 char *arg;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +000066 pid_t pid;
67 int signo = SIGTERM, errors = 0, quiet = 0;
Denis Vlasenko02f47e92007-05-06 22:48:55 +000068#if !ENABLE_KILLALL && !ENABLE_KILLALL5
69#define killall 0
70#define killall5 0
71#else
72/* How to determine who we are? find 3rd char from the end:
73 * kill, killall, killall5
Denis Vlasenko0cacc802007-05-06 22:51:52 +000074 * ^i ^a ^l - it's unique
75 * (checking from the start is complicated by /bin/kill... case) */
Denis Vlasenko02f47e92007-05-06 22:48:55 +000076 const char char3 = argv[0][strlen(argv[0]) - 3];
77#define killall (ENABLE_KILLALL && char3 == 'a')
78#define killall5 (ENABLE_KILLALL5 && char3 == 'l')
79#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000080
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 /* Parse any options */
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000082 argc--;
83 arg = *++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +000084
Denis Vlasenkofa076802006-11-05 00:38:51 +000085 if (argc < 1 || arg[0] != '-') {
Eric Andersen7d72e792003-07-26 07:41:56 +000086 goto do_it_now;
87 }
88
Denis Vlasenkof20de5b2007-04-29 23:42:54 +000089 /* The -l option, which prints out signal names.
90 * Intended usage in shell:
91 * echo "Died of SIG`kill -l $?`"
92 * We try to mimic what kill from coreutils-6.8 does */
Denis Vlasenkofa076802006-11-05 00:38:51 +000093 if (arg[1] == 'l' && arg[2] == '\0') {
Denis Vlasenkofa076802006-11-05 00:38:51 +000094 if (argc == 1) {
Eric Andersen7d72e792003-07-26 07:41:56 +000095 /* Print the whole signal list */
Denis Vlasenkoa4f4de92007-09-30 16:32:01 +000096 print_signames();
97 return 0;
Denis Vlasenko72e1c892007-09-29 22:26:01 +000098 }
99 /* -l <sig list> */
100 while ((arg = *++argv)) {
101 if (isdigit(arg[0])) {
102 signo = bb_strtou(arg, NULL, 10);
103 if (errno) {
104 bb_error_msg("unknown signal '%s'", arg);
105 return EXIT_FAILURE;
Rob Landleyc9c1a412006-07-12 19:17:55 +0000106 }
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000107 /* Exitcodes >= 0x80 are to be treated
108 * as "killed by signal (exitcode & 0x7f)" */
109 puts(get_signame(signo & 0x7f));
110 /* TODO: 'bad' signal# - coreutils says:
111 * kill: 127: invalid signal
112 * we just print "127" instead */
113 } else {
114 signo = get_signum(arg);
115 if (signo < 0) {
116 bb_error_msg("unknown signal '%s'", arg);
117 return EXIT_FAILURE;
118 }
119 printf("%d\n", signo);
Eric Andersen7d72e792003-07-26 07:41:56 +0000120 }
121 }
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000122 /* If they specified -l, we are all done */
Eric Andersen7d72e792003-07-26 07:41:56 +0000123 return EXIT_SUCCESS;
124 }
125
126 /* The -q quiet option */
Denis Vlasenkofa076802006-11-05 00:38:51 +0000127 if (killall && arg[1] == 'q' && arg[2] == '\0') {
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000128 quiet = 1;
129 arg = *++argv;
Eric Andersen7d72e792003-07-26 07:41:56 +0000130 argc--;
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000131 if (argc < 1)
132 bb_show_usage();
133 if (arg[0] != '-')
134 goto do_it_now;
Eric Andersencc8ed391999-10-05 16:24:54 +0000135 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000136
Denis Vlasenkob9b344a2008-10-31 00:30:48 +0000137 arg++; /* skip '-' */
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000138
139 /* -o PID? (if present, it always is at the end of command line) */
140 if (killall5 && arg[0] == 'o')
141 goto do_it_now;
142
Denis Vlasenkob9b344a2008-10-31 00:30:48 +0000143 if (argc > 1 && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
144 argc--;
145 arg = *++argv;
146 } /* else it must be -SIG */
147 signo = get_signum(arg);
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000148 if (signo < 0) { /* || signo > MAX_SIGNUM ? */
Denis Vlasenkob9b344a2008-10-31 00:30:48 +0000149 bb_error_msg("bad signal name '%s'", arg);
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000150 return EXIT_FAILURE;
151 }
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000152 arg = *++argv;
153 argc--;
Eric Andersen7d72e792003-07-26 07:41:56 +0000154
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000155 do_it_now:
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000156 pid = getpid();
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000157
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000158 if (killall5) {
159 pid_t sid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000160 procps_status_t* p = NULL;
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000161 int ret = 0;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000162
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000163 /* Find out our session id */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000164 sid = getsid(pid);
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000165 /* Stop all processes */
Denys Vlasenko14850302012-04-03 08:16:05 +0200166 if (signo != SIGSTOP && signo != SIGCONT)
167 kill(-1, SIGSTOP);
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000168 /* Signal all processes except those in our session */
Alexey Fomenko6a932122011-12-22 11:38:57 +0100169 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID)) != NULL) {
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000170 int i;
171
172 if (p->sid == (unsigned)sid
173 || p->pid == (unsigned)pid
Alexey Fomenko6a932122011-12-22 11:38:57 +0100174 || p->pid == 1
175 ) {
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000176 continue;
Alexey Fomenko6a932122011-12-22 11:38:57 +0100177 }
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000178
179 /* All remaining args must be -o PID options.
180 * Check p->pid against them. */
181 for (i = 0; i < argc; i++) {
182 pid_t omit;
183
184 arg = argv[i];
185 if (arg[0] != '-' || arg[1] != 'o') {
186 bb_error_msg("bad option '%s'", arg);
187 ret = 1;
188 goto resume;
189 }
190 arg += 2;
191 if (!arg[0] && argv[++i])
192 arg = argv[i];
193 omit = bb_strtoi(arg, NULL, 10);
194 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200195 bb_error_msg("invalid number '%s'", arg);
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000196 ret = 1;
197 goto resume;
198 }
199 if (p->pid == omit)
200 goto dont_kill;
201 }
202 kill(p->pid, signo);
203 dont_kill: ;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000204 }
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000205 resume:
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000206 /* And let them continue */
Denys Vlasenko14850302012-04-03 08:16:05 +0200207 if (signo != SIGSTOP && signo != SIGCONT)
208 kill(-1, SIGCONT);
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000209 return ret;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000210 }
211
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000212 /* Pid or name is required for kill/killall */
213 if (argc < 1) {
Denis Vlasenko1fe4e9e2007-11-15 00:57:40 +0000214 bb_error_msg("you need to specify whom to kill");
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000215 return EXIT_FAILURE;
216 }
"Vladimir N. Oleynik"1e98a072006-01-25 13:21:08 +0000217
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000218 if (killall) {
Erik Andersen246cc6d2000-03-07 07:41:42 +0000219 /* Looks like they want to do a killall. Do that */
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000220 while (arg) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000221 pid_t* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000222
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000223 pidList = find_pid_by_name(arg);
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000224 if (*pidList == 0) {
Eric Andersenc38678d2002-09-16 06:22:25 +0000225 errors++;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000226 if (!quiet)
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000227 bb_error_msg("%s: no process killed", arg);
Eric Andersen7d72e792003-07-26 07:41:56 +0000228 } else {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000229 pid_t *pl;
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +0000230
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000231 for (pl = pidList; *pl; pl++) {
232 if (*pl == pid)
Eric Andersen7d72e792003-07-26 07:41:56 +0000233 continue;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000234 if (kill(*pl, signo) == 0)
235 continue;
236 errors++;
237 if (!quiet)
Denys Vlasenko651a2692010-03-23 16:25:17 +0100238 bb_perror_msg("can't kill pid %d", (int)*pl);
Eric Andersen7d72e792003-07-26 07:41:56 +0000239 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000240 }
Eric Andersen44608e92002-10-22 12:21:15 +0000241 free(pidList);
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000242 arg = *++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000243 }
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000244 return errors;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000245 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000246
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000247 /* Looks like they want to do a kill. Do that */
248 while (arg) {
Denys Vlasenkob12553f2011-02-21 03:22:20 +0100249#if ENABLE_ASH || ENABLE_HUSH
250 /*
251 * We need to support shell's "hack formats" of
252 * " -PRGP_ID" (yes, with a leading space)
253 * and " PID1 PID2 PID3" (with degenerate case "")
254 */
255 while (*arg != '\0') {
256 char *end;
257 if (*arg == ' ')
258 arg++;
259 pid = bb_strtoi(arg, &end, 10);
260 if (errno && (errno != EINVAL || *end != ' ')) {
261 bb_error_msg("invalid number '%s'", arg);
262 errors++;
Alexey Fomenko6a932122011-12-22 11:38:57 +0100263 break;
264 }
265 if (kill(pid, signo) != 0) {
Denys Vlasenkob12553f2011-02-21 03:22:20 +0100266 bb_perror_msg("can't kill pid %d", (int)pid);
267 errors++;
268 }
269 arg = end; /* can only point to ' ' or '\0' now */
270 }
271#else
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000272 pid = bb_strtoi(arg, NULL, 10);
273 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200274 bb_error_msg("invalid number '%s'", arg);
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000275 errors++;
276 } else if (kill(pid, signo) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100277 bb_perror_msg("can't kill pid %d", (int)pid);
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000278 errors++;
279 }
Denys Vlasenkob12553f2011-02-21 03:22:20 +0100280#endif
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000281 arg = *++argv;
282 }
Eric Andersenc38678d2002-09-16 06:22:25 +0000283 return errors;
Eric Andersencc8ed391999-10-05 16:24:54 +0000284}