blob: e418046e7098ad101e4afcbd24e3cf80fa84a974 [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 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenc4996011999-10-20 22:08:37 +00009 */
10
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000012
Denis Vlasenkof20de5b2007-04-29 23:42:54 +000013/* Note: kill_main is directly called from shell in order to implement
14 * kill built-in. Shell substitutes job ids with process groups first.
15 *
16 * This brings some complications:
17 *
18 * + we can't use xfunc here
19 * + we can't use applet_name
20 * + we can't use bb_show_usage
21 * (Above doesn't apply for killall[5] cases)
22 *
23 * kill %n gets translated into kill ' -<process group>' by shell (note space!)
24 * This is needed to avoid collision with kill -9 ... syntax
25 */
26
Rob Landleydfba7412006-03-06 20:47:33 +000027int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000028{
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000029 char *arg;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +000030 pid_t pid;
31 int signo = SIGTERM, errors = 0, quiet = 0;
Denis Vlasenko02f47e92007-05-06 22:48:55 +000032#if !ENABLE_KILLALL && !ENABLE_KILLALL5
33#define killall 0
34#define killall5 0
35#else
36/* How to determine who we are? find 3rd char from the end:
37 * kill, killall, killall5
Denis Vlasenko0cacc802007-05-06 22:51:52 +000038 * ^i ^a ^l - it's unique
39 * (checking from the start is complicated by /bin/kill... case) */
Denis Vlasenko02f47e92007-05-06 22:48:55 +000040 const char char3 = argv[0][strlen(argv[0]) - 3];
41#define killall (ENABLE_KILLALL && char3 == 'a')
42#define killall5 (ENABLE_KILLALL5 && char3 == 'l')
43#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000044
Erik Andersene49d5ec2000-02-08 19:58:47 +000045 /* Parse any options */
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000046 argc--;
47 arg = *++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +000048
Denis Vlasenkofa076802006-11-05 00:38:51 +000049 if (argc < 1 || arg[0] != '-') {
Eric Andersen7d72e792003-07-26 07:41:56 +000050 goto do_it_now;
51 }
52
Denis Vlasenkof20de5b2007-04-29 23:42:54 +000053 /* The -l option, which prints out signal names.
54 * Intended usage in shell:
55 * echo "Died of SIG`kill -l $?`"
56 * We try to mimic what kill from coreutils-6.8 does */
Denis Vlasenkofa076802006-11-05 00:38:51 +000057 if (arg[1] == 'l' && arg[2] == '\0') {
Denis Vlasenkofa076802006-11-05 00:38:51 +000058 if (argc == 1) {
Eric Andersen7d72e792003-07-26 07:41:56 +000059 /* Print the whole signal list */
Denis Vlasenkoa4f4de92007-09-30 16:32:01 +000060 print_signames();
61 return 0;
Denis Vlasenko72e1c892007-09-29 22:26:01 +000062 }
63 /* -l <sig list> */
64 while ((arg = *++argv)) {
65 if (isdigit(arg[0])) {
66 signo = bb_strtou(arg, NULL, 10);
67 if (errno) {
68 bb_error_msg("unknown signal '%s'", arg);
69 return EXIT_FAILURE;
Rob Landleyc9c1a412006-07-12 19:17:55 +000070 }
Denis Vlasenko72e1c892007-09-29 22:26:01 +000071 /* Exitcodes >= 0x80 are to be treated
72 * as "killed by signal (exitcode & 0x7f)" */
73 puts(get_signame(signo & 0x7f));
74 /* TODO: 'bad' signal# - coreutils says:
75 * kill: 127: invalid signal
76 * we just print "127" instead */
77 } else {
78 signo = get_signum(arg);
79 if (signo < 0) {
80 bb_error_msg("unknown signal '%s'", arg);
81 return EXIT_FAILURE;
82 }
83 printf("%d\n", signo);
Eric Andersen7d72e792003-07-26 07:41:56 +000084 }
85 }
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000086 /* If they specified -l, we are all done */
Eric Andersen7d72e792003-07-26 07:41:56 +000087 return EXIT_SUCCESS;
88 }
89
90 /* The -q quiet option */
Denis Vlasenkofa076802006-11-05 00:38:51 +000091 if (killall && arg[1] == 'q' && arg[2] == '\0') {
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000092 quiet = 1;
93 arg = *++argv;
Eric Andersen7d72e792003-07-26 07:41:56 +000094 argc--;
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +000095 if (argc < 1)
96 bb_show_usage();
97 if (arg[0] != '-')
98 goto do_it_now;
Eric Andersencc8ed391999-10-05 16:24:54 +000099 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000100
Denis Vlasenkob9b344a2008-10-31 00:30:48 +0000101 arg++; /* skip '-' */
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000102
103 /* -o PID? (if present, it always is at the end of command line) */
104 if (killall5 && arg[0] == 'o')
105 goto do_it_now;
106
Denis Vlasenkob9b344a2008-10-31 00:30:48 +0000107 if (argc > 1 && arg[0] == 's' && arg[1] == '\0') { /* -s SIG? */
108 argc--;
109 arg = *++argv;
110 } /* else it must be -SIG */
111 signo = get_signum(arg);
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000112 if (signo < 0) { /* || signo > MAX_SIGNUM ? */
Denis Vlasenkob9b344a2008-10-31 00:30:48 +0000113 bb_error_msg("bad signal name '%s'", arg);
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000114 return EXIT_FAILURE;
115 }
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000116 arg = *++argv;
117 argc--;
Eric Andersen7d72e792003-07-26 07:41:56 +0000118
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000119 do_it_now:
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000120 pid = getpid();
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000121
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000122 if (killall5) {
123 pid_t sid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000124 procps_status_t* p = NULL;
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000125 int ret = 0;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000126
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000127 /* Find out our session id */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000128 sid = getsid(pid);
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000129 /* Stop all processes */
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000130 kill(-1, SIGSTOP);
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000131 /* Signal all processes except those in our session */
Denis Vlasenko150f4022007-01-13 21:06:21 +0000132 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) {
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000133 int i;
134
135 if (p->sid == (unsigned)sid
136 || p->pid == (unsigned)pid
137 || p->pid == 1)
138 continue;
139
140 /* All remaining args must be -o PID options.
141 * Check p->pid against them. */
142 for (i = 0; i < argc; i++) {
143 pid_t omit;
144
145 arg = argv[i];
146 if (arg[0] != '-' || arg[1] != 'o') {
147 bb_error_msg("bad option '%s'", arg);
148 ret = 1;
149 goto resume;
150 }
151 arg += 2;
152 if (!arg[0] && argv[++i])
153 arg = argv[i];
154 omit = bb_strtoi(arg, NULL, 10);
155 if (errno) {
156 bb_error_msg("bad pid '%s'", arg);
157 ret = 1;
158 goto resume;
159 }
160 if (p->pid == omit)
161 goto dont_kill;
162 }
163 kill(p->pid, signo);
164 dont_kill: ;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000165 }
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000166 resume:
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000167 /* And let them continue */
168 kill(-1, SIGCONT);
Denis Vlasenkoa0ab9432009-02-07 22:30:39 +0000169 return ret;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000170 }
171
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000172 /* Pid or name is required for kill/killall */
173 if (argc < 1) {
Denis Vlasenko1fe4e9e2007-11-15 00:57:40 +0000174 bb_error_msg("you need to specify whom to kill");
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000175 return EXIT_FAILURE;
176 }
"Vladimir N. Oleynik"1e98a072006-01-25 13:21:08 +0000177
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000178 if (killall) {
Erik Andersen246cc6d2000-03-07 07:41:42 +0000179 /* Looks like they want to do a killall. Do that */
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000180 while (arg) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000181 pid_t* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000182
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000183 pidList = find_pid_by_name(arg);
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000184 if (*pidList == 0) {
Eric Andersenc38678d2002-09-16 06:22:25 +0000185 errors++;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000186 if (!quiet)
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000187 bb_error_msg("%s: no process killed", arg);
Eric Andersen7d72e792003-07-26 07:41:56 +0000188 } else {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000189 pid_t *pl;
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +0000190
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000191 for (pl = pidList; *pl; pl++) {
192 if (*pl == pid)
Eric Andersen7d72e792003-07-26 07:41:56 +0000193 continue;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000194 if (kill(*pl, signo) == 0)
195 continue;
196 errors++;
197 if (!quiet)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100198 bb_perror_msg("can't kill pid %u", (unsigned)*pl);
Eric Andersen7d72e792003-07-26 07:41:56 +0000199 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000200 }
Eric Andersen44608e92002-10-22 12:21:15 +0000201 free(pidList);
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000202 arg = *++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000203 }
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000204 return errors;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000205 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000206
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000207 /* Looks like they want to do a kill. Do that */
208 while (arg) {
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000209 /* Support shell 'space' trick */
210 if (arg[0] == ' ')
211 arg++;
212 pid = bb_strtoi(arg, NULL, 10);
213 if (errno) {
214 bb_error_msg("bad pid '%s'", arg);
215 errors++;
216 } else if (kill(pid, signo) != 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100217 bb_perror_msg("can't kill pid %d", (int)pid);
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000218 errors++;
219 }
220 arg = *++argv;
221 }
Eric Andersenc38678d2002-09-16 06:22:25 +0000222 return errors;
Eric Andersencc8ed391999-10-05 16:24:54 +0000223}