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 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 13 | int kill_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 14 | { |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 15 | char *arg; |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 16 | pid_t pid; |
| 17 | int signo = SIGTERM, errors = 0, quiet = 0; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 18 | const int killall = (ENABLE_KILLALL && applet_name[4] == 'a' |
| 19 | && (!ENABLE_KILLALL5 || applet_name[7] != '5')); |
| 20 | const int killall5 = (ENABLE_KILLALL5 && applet_name[4] == 'a' |
| 21 | && (!ENABLE_KILLALL || applet_name[7] == '5')); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 23 | /* Parse any options */ |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 24 | argc--; |
| 25 | arg = *++argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 27 | if (argc < 1 || arg[0] != '-') { |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 28 | goto do_it_now; |
| 29 | } |
| 30 | |
| 31 | /* The -l option, which prints out signal names. */ |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 32 | if (arg[1] == 'l' && arg[2] == '\0') { |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 33 | const char *name; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 34 | if (argc == 1) { |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 35 | /* Print the whole signal list */ |
| 36 | int col = 0; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 37 | for (signo = 1; signo < 32; signo++) { |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 38 | name = get_signame(signo); |
| 39 | if (isdigit(name[0])) continue; |
| 40 | if (col > 66) { |
| 41 | puts(""); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 42 | col = 0; |
| 43 | } |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 44 | col += printf("%2d) %-6s", signo, name); |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 45 | } |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 46 | puts(""); |
| 47 | } else { /* -l <sig list> */ |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 48 | while ((arg = *++argv)) { |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 49 | if (isdigit(arg[0])) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 50 | signo = xatoi_u(arg); |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 51 | name = get_signame(signo); |
| 52 | } else { |
| 53 | signo = get_signum(arg); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 54 | if (signo < 0) |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 55 | bb_error_msg_and_die("unknown signal '%s'", arg); |
| 56 | name = get_signame(signo); |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 57 | } |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 58 | printf("%2d) %s\n", signo, name); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 61 | /* If they specified -l, we are all done */ |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 62 | return EXIT_SUCCESS; |
| 63 | } |
| 64 | |
| 65 | /* The -q quiet option */ |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 66 | if (killall && arg[1] == 'q' && arg[2] == '\0') { |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 67 | quiet = 1; |
| 68 | arg = *++argv; |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 69 | argc--; |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 70 | if (argc < 1) bb_show_usage(); |
| 71 | if (arg[0] != '-') goto do_it_now; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 72 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 74 | /* -SIG */ |
| 75 | signo = get_signum(&arg[1]); |
Denis Vlasenko | fa07680 | 2006-11-05 00:38:51 +0000 | [diff] [blame] | 76 | if (signo < 0) |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 77 | bb_error_msg_and_die("bad signal name '%s'", &arg[1]); |
| 78 | arg = *++argv; |
| 79 | argc--; |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 80 | |
Eric Andersen | 80cd3cf | 2002-07-23 23:45:11 +0000 | [diff] [blame] | 81 | do_it_now: |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 83 | if (killall5) { |
| 84 | pid_t sid; |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame^] | 85 | procps_status_t* p = NULL; |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 86 | |
Denis Vlasenko | dd539f7 | 2006-11-01 20:20:37 +0000 | [diff] [blame] | 87 | // Cannot happen anyway? We don't TERM ourself, we STOP |
| 88 | // /* kill(-1, sig) on Linux (at least 2.1.x) |
| 89 | // * might send signal to the calling process too */ |
| 90 | // signal(SIGTERM, SIG_IGN); |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 91 | /* Now stop all processes */ |
| 92 | kill(-1, SIGSTOP); |
| 93 | /* Find out our own session id */ |
| 94 | pid = getpid(); |
| 95 | sid = getsid(pid); |
| 96 | /* Now kill all processes except our session */ |
Denis Vlasenko | 459e4d6 | 2006-11-05 00:43:51 +0000 | [diff] [blame^] | 97 | while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) { |
| 98 | if (p->sid != sid && p->pid != pid && p->pid != 1) |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 99 | kill(p->pid, signo); |
| 100 | } |
| 101 | /* And let them continue */ |
| 102 | kill(-1, SIGCONT); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /* Pid or name required for kill/killall */ |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 107 | if (argc < 1) |
"Vladimir N. Oleynik" | 1e98a07 | 2006-01-25 13:21:08 +0000 | [diff] [blame] | 108 | bb_show_usage(); |
| 109 | |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 110 | if (killall) { |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 111 | /* Looks like they want to do a killall. Do that */ |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 112 | pid = getpid(); |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 113 | while (arg) { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 114 | pid_t* pidList; |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 115 | |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 116 | pidList = find_pid_by_name(arg); |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 117 | if (*pidList == 0) { |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 118 | errors++; |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 119 | if (!quiet) |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 120 | bb_error_msg("%s: no process killed", arg); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 121 | } else { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 122 | pid_t *pl; |
Glenn L McGrath | bb2e9d4 | 2002-11-25 22:12:28 +0000 | [diff] [blame] | 123 | |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 124 | for (pl = pidList; *pl; pl++) { |
| 125 | if (*pl == pid) |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 126 | continue; |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 127 | if (kill(*pl, signo) == 0) |
| 128 | continue; |
| 129 | errors++; |
| 130 | if (!quiet) |
| 131 | bb_perror_msg("cannot kill pid %u", (unsigned)*pl); |
Eric Andersen | 7d72e79 | 2003-07-26 07:41:56 +0000 | [diff] [blame] | 132 | } |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 133 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 134 | free(pidList); |
Denis Vlasenko | a77947f | 2006-09-27 14:19:16 +0000 | [diff] [blame] | 135 | arg = *++argv; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 136 | } |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 137 | return errors; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 138 | } |
Rob Landley | c9c1a41 | 2006-07-12 19:17:55 +0000 | [diff] [blame] | 139 | |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 140 | /* Looks like they want to do a kill. Do that */ |
| 141 | while (arg) { |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 142 | /* Huh? |
| 143 | if (!isdigit(arg[0]) && arg[0] != '-') |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 144 | bb_error_msg_and_die("bad pid '%s'", arg); |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 145 | */ |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 146 | pid = xatou(arg); |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 147 | /* FIXME: better overflow check? */ |
Denis Vlasenko | 35fb512 | 2006-11-01 09:16:49 +0000 | [diff] [blame] | 148 | if (kill(pid, signo) != 0) { |
| 149 | bb_perror_msg("cannot kill pid %u", (unsigned)pid); |
Denis Vlasenko | 0bb628f | 2006-09-27 14:25:33 +0000 | [diff] [blame] | 150 | errors++; |
| 151 | } |
| 152 | arg = *++argv; |
| 153 | } |
Eric Andersen | c38678d | 2002-09-16 06:22:25 +0000 | [diff] [blame] | 154 | return errors; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 155 | } |