blob: a77d66e7a8077382b2b5b48bea8337634b868ae9 [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
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000027int kill_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
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 Vlasenko02f47e92007-05-06 22:48:55 +000033#if !ENABLE_KILLALL && !ENABLE_KILLALL5
34#define killall 0
35#define killall5 0
36#else
37/* How to determine who we are? find 3rd char from the end:
38 * kill, killall, killall5
Denis Vlasenko0cacc802007-05-06 22:51:52 +000039 * ^i ^a ^l - it's unique
40 * (checking from the start is complicated by /bin/kill... case) */
Denis Vlasenko02f47e92007-05-06 22:48:55 +000041 const char char3 = argv[0][strlen(argv[0]) - 3];
42#define killall (ENABLE_KILLALL && char3 == 'a')
43#define killall5 (ENABLE_KILLALL5 && char3 == 'l')
44#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Erik Andersene49d5ec2000-02-08 19:58:47 +000046 /* Parse any options */
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000047 argc--;
48 arg = *++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +000049
Denis Vlasenkofa076802006-11-05 00:38:51 +000050 if (argc < 1 || arg[0] != '-') {
Eric Andersen7d72e792003-07-26 07:41:56 +000051 goto do_it_now;
52 }
53
Denis Vlasenkof20de5b2007-04-29 23:42:54 +000054 /* The -l option, which prints out signal names.
55 * Intended usage in shell:
56 * echo "Died of SIG`kill -l $?`"
57 * We try to mimic what kill from coreutils-6.8 does */
Denis Vlasenkofa076802006-11-05 00:38:51 +000058 if (arg[1] == 'l' && arg[2] == '\0') {
Denis Vlasenkofa076802006-11-05 00:38:51 +000059 if (argc == 1) {
Eric Andersen7d72e792003-07-26 07:41:56 +000060 /* Print the whole signal list */
Denis Vlasenkoa4f4de92007-09-30 16:32:01 +000061 print_signames();
62 return 0;
Denis Vlasenko72e1c892007-09-29 22:26:01 +000063 }
64 /* -l <sig list> */
65 while ((arg = *++argv)) {
66 if (isdigit(arg[0])) {
67 signo = bb_strtou(arg, NULL, 10);
68 if (errno) {
69 bb_error_msg("unknown signal '%s'", arg);
70 return EXIT_FAILURE;
Rob Landleyc9c1a412006-07-12 19:17:55 +000071 }
Denis Vlasenko72e1c892007-09-29 22:26:01 +000072 /* Exitcodes >= 0x80 are to be treated
73 * as "killed by signal (exitcode & 0x7f)" */
74 puts(get_signame(signo & 0x7f));
75 /* TODO: 'bad' signal# - coreutils says:
76 * kill: 127: invalid signal
77 * we just print "127" instead */
78 } else {
79 signo = get_signum(arg);
80 if (signo < 0) {
81 bb_error_msg("unknown signal '%s'", arg);
82 return EXIT_FAILURE;
83 }
84 printf("%d\n", signo);
Eric Andersen7d72e792003-07-26 07:41:56 +000085 }
86 }
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000087 /* If they specified -l, we are all done */
Eric Andersen7d72e792003-07-26 07:41:56 +000088 return EXIT_SUCCESS;
89 }
90
91 /* The -q quiet option */
Denis Vlasenkofa076802006-11-05 00:38:51 +000092 if (killall && arg[1] == 'q' && arg[2] == '\0') {
Denis Vlasenkoa77947f2006-09-27 14:19:16 +000093 quiet = 1;
94 arg = *++argv;
Eric Andersen7d72e792003-07-26 07:41:56 +000095 argc--;
Denis Vlasenkofa076802006-11-05 00:38:51 +000096 if (argc < 1) bb_show_usage();
97 if (arg[0] != '-') goto do_it_now;
Eric Andersencc8ed391999-10-05 16:24:54 +000098 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000099
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000100 /* -SIG */
101 signo = get_signum(&arg[1]);
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000102 if (signo < 0) { /* || signo > MAX_SIGNUM ? */
103 bb_error_msg("bad signal name '%s'", &arg[1]);
104 return EXIT_FAILURE;
105 }
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000106 arg = *++argv;
107 argc--;
Eric Andersen7d72e792003-07-26 07:41:56 +0000108
Eric Andersen80cd3cf2002-07-23 23:45:11 +0000109do_it_now:
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000110
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000111 if (killall5) {
112 pid_t sid;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000113 procps_status_t* p = NULL;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000114
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000115 /* Now stop all processes */
116 kill(-1, SIGSTOP);
117 /* Find out our own session id */
118 pid = getpid();
119 sid = getsid(pid);
120 /* Now kill all processes except our session */
Denis Vlasenko150f4022007-01-13 21:06:21 +0000121 while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000122 if (p->sid != sid && p->pid != pid && p->pid != 1)
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000123 kill(p->pid, signo);
124 }
125 /* And let them continue */
126 kill(-1, SIGCONT);
127 return 0;
128 }
129
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000130 /* Pid or name is required for kill/killall */
131 if (argc < 1) {
Denis Vlasenko1fe4e9e2007-11-15 00:57:40 +0000132 bb_error_msg("you need to specify whom to kill");
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000133 return EXIT_FAILURE;
134 }
"Vladimir N. Oleynik"1e98a072006-01-25 13:21:08 +0000135
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000136 if (killall) {
Erik Andersen246cc6d2000-03-07 07:41:42 +0000137 /* Looks like they want to do a killall. Do that */
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000138 pid = getpid();
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000139 while (arg) {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000140 pid_t* pidList;
Erik Andersen246cc6d2000-03-07 07:41:42 +0000141
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000142 pidList = find_pid_by_name(arg);
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000143 if (*pidList == 0) {
Eric Andersenc38678d2002-09-16 06:22:25 +0000144 errors++;
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000145 if (!quiet)
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000146 bb_error_msg("%s: no process killed", arg);
Eric Andersen7d72e792003-07-26 07:41:56 +0000147 } else {
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000148 pid_t *pl;
Glenn L McGrathbb2e9d42002-11-25 22:12:28 +0000149
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000150 for (pl = pidList; *pl; pl++) {
151 if (*pl == pid)
Eric Andersen7d72e792003-07-26 07:41:56 +0000152 continue;
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000153 if (kill(*pl, signo) == 0)
154 continue;
155 errors++;
156 if (!quiet)
157 bb_perror_msg("cannot kill pid %u", (unsigned)*pl);
Eric Andersen7d72e792003-07-26 07:41:56 +0000158 }
Erik Andersen246cc6d2000-03-07 07:41:42 +0000159 }
Eric Andersen44608e92002-10-22 12:21:15 +0000160 free(pidList);
Denis Vlasenkoa77947f2006-09-27 14:19:16 +0000161 arg = *++argv;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162 }
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000163 return errors;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000164 }
Rob Landleyc9c1a412006-07-12 19:17:55 +0000165
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000166 /* Looks like they want to do a kill. Do that */
167 while (arg) {
Denis Vlasenkof20de5b2007-04-29 23:42:54 +0000168 /* Support shell 'space' trick */
169 if (arg[0] == ' ')
170 arg++;
171 pid = bb_strtoi(arg, NULL, 10);
172 if (errno) {
173 bb_error_msg("bad pid '%s'", arg);
174 errors++;
175 } else if (kill(pid, signo) != 0) {
176 bb_perror_msg("cannot kill pid %d", (int)pid);
Denis Vlasenko0bb628f2006-09-27 14:25:33 +0000177 errors++;
178 }
179 arg = *++argv;
180 }
Eric Andersenc38678d2002-09-16 06:22:25 +0000181 return errors;
Eric Andersencc8ed391999-10-05 16:24:54 +0000182}