blob: b3257492d5e5131961e3efc7fa64bd2bea5d5bc3 [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/*
Erik Andersen5661fe02000-04-05 01:00:52 +00003 * Mini kill/killall 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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000011#include "busybox.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
Denis Vlasenko06af2162007-02-03 17:28:39 +000027int kill_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000028int kill_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000029{
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000030 char *arg;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +000031 pid_t pid;
32 int signo = SIGTERM, errors = 0, quiet = 0;
Denis Vlasenkof20de5b2007-04-29 23:42:54 +000033 const int killall = (ENABLE_KILLALL && argv[0][4] == 'a'
34 && (!ENABLE_KILLALL5 || argv[0][7] != '5'));
35 const int killall5 = (ENABLE_KILLALL5 && argv[0][4] == 'a'
36 && (!ENABLE_KILLALL || argv[0][7] == '5'));
Eric Andersencc8ed391999-10-05 16:24:54 +000037
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 /* Parse any options */
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000039 argc--;
40 arg = *++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +000041
Denis Vlasenkofa076802006-11-05 00:38:51 +000042 if (argc < 1 || arg[0] != '-') {
Eric Andersen7d72e792003-07-26 07:41:56 +000043 goto do_it_now;
44 }
45
Denis Vlasenkof20de5b2007-04-29 23:42:54 +000046 /* The -l option, which prints out signal names.
47 * Intended usage in shell:
48 * echo "Died of SIG`kill -l $?`"
49 * We try to mimic what kill from coreutils-6.8 does */
Denis Vlasenkofa076802006-11-05 00:38:51 +000050 if (arg[1] == 'l' && arg[2] == '\0') {
Denis Vlasenkofa076802006-11-05 00:38:51 +000051 if (argc == 1) {
Eric Andersen7d72e792003-07-26 07:41:56 +000052 /* Print the whole signal list */
Denis Vlasenkofa076802006-11-05 00:38:51 +000053 for (signo = 1; signo < 32; signo++) {
Denis Vlasenkodcf4de22007-05-01 20:07:29 +000054 const char *name = get_signame(signo);
55 if (!isdigit(name[0]))
56 puts(name);
Eric Andersen80cd3cf2002-07-23 23:45:11 +000057 }
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000058 } else { /* -l <sig list> */
Denis Vlasenkofa076802006-11-05 00:38:51 +000059 while ((arg = *++argv)) {
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000060 if (isdigit(arg[0])) {
Denis Vlasenkof20de5b2007-04-29 23:42:54 +000061 signo = bb_strtou(arg, NULL, 10);
62 if (errno) {
63 bb_error_msg("unknown signal '%s'", arg);
64 return EXIT_FAILURE;
65 }
66 /* Exitcodes >= 0x80 are to be treated
67 * as "killed by signal (exitcode & 0x7f)" */
68 puts(get_signame(signo & 0x7f));
69 /* TODO: 'bad' signal# - coreutils says:
70 * kill: 127: invalid signal
71 * we just print "127" instead */
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000072 } else {
73 signo = get_signum(arg);
Denis Vlasenkof20de5b2007-04-29 23:42:54 +000074 if (signo < 0) {
75 bb_error_msg("unknown signal '%s'", arg);
76 return EXIT_FAILURE;
77 }
78 printf("%d\n", signo);
Rob Landleyc9c1a412006-07-12 19:17:55 +000079 }
Eric Andersen7d72e792003-07-26 07:41:56 +000080 }
81 }
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000082 /* If they specified -l, we are all done */
Eric Andersen7d72e792003-07-26 07:41:56 +000083 return EXIT_SUCCESS;
84 }
85
86 /* The -q quiet option */
Denis Vlasenkofa076802006-11-05 00:38:51 +000087 if (killall && arg[1] == 'q' && arg[2] == '\0') {
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000088 quiet = 1;
89 arg = *++argv;
Eric Andersen7d72e792003-07-26 07:41:56 +000090 argc--;
Denis Vlasenkofa076802006-11-05 00:38:51 +000091 if (argc < 1) bb_show_usage();
92 if (arg[0] != '-') goto do_it_now;
Eric Andersencc8ed391999-10-05 16:24:54 +000093 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000094
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000095 /* -SIG */
96 signo = get_signum(&arg[1]);
Denis Vlasenkof20de5b2007-04-29 23:42:54 +000097 if (signo < 0) { /* || signo > MAX_SIGNUM ? */
98 bb_error_msg("bad signal name '%s'", &arg[1]);
99 return EXIT_FAILURE;
100 }
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000101 arg = *++argv;
102 argc--;
Eric Andersen7d72e792003-07-26 07:41:56 +0000103
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000104do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000105
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000106 if (killall5) {
107 pid_t sid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000108 procps_status_t* p = NULL;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000109
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000110 /* Now stop all processes */
111 kill(-1, SIGSTOP);
112 /* Find out our own session id */
113 pid = getpid();
114 sid = getsid(pid);
115 /* Now kill all processes except our session */
Denis Vlasenko150f4022007-01-13 21:06:21 +0000116 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000117 if (p->sid != sid && p->pid != pid && p->pid != 1)
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000118 kill(p->pid, signo);
119 }
120 /* And let them continue */
121 kill(-1, SIGCONT);
122 return 0;
123 }
124
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000125 /* Pid or name is required for kill/killall */
126 if (argc < 1) {
127 puts("You need to specify whom to kill");
128 return EXIT_FAILURE;
129 }
"Vladimir N. Oleynik"1e98a072006-01-25 13:21:08 +0000130
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000131 if (killall) {
Erik Andersen246cc6d2000-03-07 07:41:42 +0000132 /* Looks like they want to do a killall. Do that */
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000133 pid = getpid();
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000134 while (arg) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000135 pid_t* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000136
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000137 pidList = find_pid_by_name(arg);
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000138 if (*pidList == 0) {
Eric Andersenc38678d2002-09-16 06:22:25 +0000139 errors++;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000140 if (!quiet)
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000141 bb_error_msg("%s: no process killed", arg);
Eric Andersen7d72e792003-07-26 07:41:56 +0000142 } else {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000143 pid_t *pl;
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +0000144
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000145 for (pl = pidList; *pl; pl++) {
146 if (*pl == pid)
Eric Andersen7d72e792003-07-26 07:41:56 +0000147 continue;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000148 if (kill(*pl, signo) == 0)
149 continue;
150 errors++;
151 if (!quiet)
152 bb_perror_msg("cannot kill pid %u", (unsigned)*pl);
Eric Andersen7d72e792003-07-26 07:41:56 +0000153 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000154 }
Eric Andersen44608e92002-10-22 12:21:15 +0000155 free(pidList);
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000156 arg = *++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 }
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000158 return errors;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000159 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000160
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000161 /* Looks like they want to do a kill. Do that */
162 while (arg) {
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000163 /* Support shell 'space' trick */
164 if (arg[0] == ' ')
165 arg++;
166 pid = bb_strtoi(arg, NULL, 10);
167 if (errno) {
168 bb_error_msg("bad pid '%s'", arg);
169 errors++;
170 } else if (kill(pid, signo) != 0) {
171 bb_perror_msg("cannot kill pid %d", (int)pid);
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000172 errors++;
173 }
174 arg = *++argv;
175 }
Eric Andersenc38678d2002-09-16 06:22:25 +0000176 return errors;
Eric Andersencc8ed391999-10-05 16:24:54 +0000177}