Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
Erik Andersen | 5661fe0 | 2000-04-05 01:00:52 +0000 | [diff] [blame] | 3 | * Mini kill/killall implementation for busybox |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 11 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 13 | /* 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 Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 27 | int kill_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 28 | int kill_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | { |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 30 | char *arg; |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 31 | pid_t pid; |
| 32 | int signo = SIGTERM, errors = 0, quiet = 0; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 33 | 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 37 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | /* Parse any options */ |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 39 | argc--; |
| 40 | arg = *++argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 42 | if (argc < 1 || arg[0] != '-') { |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 43 | goto do_it_now; |
| 44 | } |
| 45 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 46 | /* 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 Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 50 | if (arg[1] == 'l' && arg[2] == '\0') { |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 51 | if (argc == 1) { |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 52 | /* Print the whole signal list */ |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 53 | for (signo = 1; signo < 32; signo++) { |
Denis Vlasenko | dcf4de2 | 2007-05-01 20:07:29 +0000 | [diff] [blame^] | 54 | const char *name = get_signame(signo); |
| 55 | if (!isdigit(name[0])) |
| 56 | puts(name); |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 57 | } |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 58 | } else { /* -l <sig list> */ |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 59 | while ((arg = *++argv)) { |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 60 | if (isdigit(arg[0])) { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 61 | 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 Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 72 | } else { |
| 73 | signo = get_signum(arg); |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 74 | if (signo < 0) { |
| 75 | bb_error_msg("unknown signal '%s'", arg); |
| 76 | return EXIT_FAILURE; |
| 77 | } |
| 78 | printf("%d\n", signo); |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 79 | } |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 82 | /* If they specified -l, we are all done */ |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 83 | return EXIT_SUCCESS; |
| 84 | } |
| 85 | |
| 86 | /* The -q quiet option */ |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 87 | if (killall && arg[1] == 'q' && arg[2] == '\0') { |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 88 | quiet = 1; |
| 89 | arg = *++argv; |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 90 | argc--; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 91 | if (argc < 1) bb_show_usage(); |
| 92 | if (arg[0] != '-') goto do_it_now; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 93 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 95 | /* -SIG */ |
| 96 | signo = get_signum(&arg[1]); |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 97 | if (signo < 0) { /* || signo > MAX_SIGNUM ? */ |
| 98 | bb_error_msg("bad signal name '%s'", &arg[1]); |
| 99 | return EXIT_FAILURE; |
| 100 | } |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 101 | arg = *++argv; |
| 102 | argc--; |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 103 | |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 104 | do_it_now: |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 105 | |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 106 | if (killall5) { |
| 107 | pid_t sid; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 108 | procps_status_t* p = NULL; |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 109 | |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 110 | /* 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 Vlasenko | 150f402 | 2007-01-13 21:06:21 +0000 | [diff] [blame] | 116 | while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) { |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame] | 117 | if (p->sid != sid && p->pid != pid && p->pid != 1) |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 118 | kill(p->pid, signo); |
| 119 | } |
| 120 | /* And let them continue */ |
| 121 | kill(-1, SIGCONT); |
| 122 | return 0; |
| 123 | } |
| 124 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 125 | /* 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" | 1e98a07 | 2006-01-25 13:21:08 +0000 | [diff] [blame] | 130 | |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 131 | if (killall) { |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 132 | /* Looks like they want to do a killall. Do that */ |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 133 | pid = getpid(); |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 134 | while (arg) { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 135 | pid_t* pidList; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 137 | pidList = find_pid_by_name(arg); |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 138 | if (*pidList == 0) { |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 139 | errors++; |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 140 | if (!quiet) |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 141 | bb_error_msg("%s: no process killed", arg); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 142 | } else { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 143 | pid_t *pl; |
Glenn L McGrath | bb2e9d4 | 2002-11-25 22:12:28 +0000 | [diff] [blame] | 144 | |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 145 | for (pl = pidList; *pl; pl++) { |
| 146 | if (*pl == pid) |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 147 | continue; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 148 | if (kill(*pl, signo) == 0) |
| 149 | continue; |
| 150 | errors++; |
| 151 | if (!quiet) |
| 152 | bb_perror_msg("cannot kill pid %u", (unsigned)*pl); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 153 | } |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 154 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 155 | free(pidList); |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 156 | arg = *++argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 157 | } |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 158 | return errors; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 159 | } |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 160 | |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 161 | /* Looks like they want to do a kill. Do that */ |
| 162 | while (arg) { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 163 | /* 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 Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 172 | errors++; |
| 173 | } |
| 174 | arg = *++argv; |
| 175 | } |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 176 | return errors; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 177 | } |