Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 1 | /* killall.c - Send signal (default: TERM) to all processes with given names. |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 2 | * |
| 3 | * Copyright 2012 Andreas Heck <aheck@gmx.de> |
| 4 | * |
Rob Landley | f91b7c8 | 2012-08-25 18:08:51 -0500 | [diff] [blame] | 5 | * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/killall.html |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 6 | |
Rob Landley | f070ec0 | 2013-12-22 19:39:12 -0600 | [diff] [blame] | 7 | USE_KILLALL(NEWTOY(killall, "?s:lqvi", TOYFLAG_USR|TOYFLAG_BIN)) |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 8 | |
| 9 | config KILLALL |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 10 | bool "killall" |
| 11 | default y |
| 12 | help |
Rob Landley | f070ec0 | 2013-12-22 19:39:12 -0600 | [diff] [blame] | 13 | usage: killall [-l] [-iqv] [-SIGNAL|-s SIGNAL] PROCESS_NAME... |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 14 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 15 | Send a signal (default: TERM) to all processes with the given names. |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 16 | |
Elie De Brauwer | ca4035b | 2012-12-16 13:43:36 +0100 | [diff] [blame] | 17 | -i ask for confirmation before killing |
Rob Landley | 27cec9a | 2012-12-23 15:07:28 -0600 | [diff] [blame] | 18 | -l print list of all available signals |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 19 | -q don't print any warnings or error messages |
Rob Landley | f070ec0 | 2013-12-22 19:39:12 -0600 | [diff] [blame] | 20 | -s send SIGNAL instead of SIGTERM |
Rob Landley | 27cec9a | 2012-12-23 15:07:28 -0600 | [diff] [blame] | 21 | -v report if the signal was successfully sent |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 22 | */ |
| 23 | |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 24 | #define FOR_killall |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 25 | #include "toys.h" |
| 26 | |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 27 | GLOBALS( |
Rob Landley | f070ec0 | 2013-12-22 19:39:12 -0600 | [diff] [blame] | 28 | char *sig; |
| 29 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 30 | int signum; |
Elie De Brauwer | 41b4748 | 2012-12-16 16:51:30 +0100 | [diff] [blame] | 31 | pid_t cur_pid; |
Rob Landley | 0638755 | 2013-12-19 22:20:08 -0600 | [diff] [blame] | 32 | char **names; |
| 33 | short *err; |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 34 | ) |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 35 | |
Elie De Brauwer | ca4035b | 2012-12-16 13:43:36 +0100 | [diff] [blame] | 36 | static int kill_process(pid_t pid, char *name) |
Rob Landley | f42e11b | 2012-02-18 18:09:14 -0600 | [diff] [blame] | 37 | { |
Rob Landley | 0638755 | 2013-12-19 22:20:08 -0600 | [diff] [blame] | 38 | int offset = 0; |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 39 | |
Rob Landley | 7d64dae | 2013-09-03 18:43:32 -0500 | [diff] [blame] | 40 | if (pid == TT.cur_pid) return 0; |
Elie De Brauwer | 41b4748 | 2012-12-16 16:51:30 +0100 | [diff] [blame] | 41 | |
Rob Landley | 27cec9a | 2012-12-23 15:07:28 -0600 | [diff] [blame] | 42 | if (toys.optflags & FLAG_i) { |
Rob Landley | b1353fb | 2015-09-07 17:12:57 -0500 | [diff] [blame^] | 43 | fprintf(stderr, "Signal %s(%d)", name, (int)pid); |
| 44 | if (!yesno(0)) return 0; |
Elie De Brauwer | ca4035b | 2012-12-16 13:43:36 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Rob Landley | 0638755 | 2013-12-19 22:20:08 -0600 | [diff] [blame] | 47 | errno = 0; |
| 48 | kill(pid, TT.signum); |
| 49 | for (;;) { |
| 50 | if (TT.names[offset] == name) { |
| 51 | TT.err[offset] = errno; |
| 52 | break; |
| 53 | } else offset++; |
| 54 | } |
| 55 | if (errno) { |
| 56 | if (!(toys.optflags & FLAG_q)) perror_msg("pid %d", (int)pid); |
| 57 | } else if (toys.optflags & FLAG_v) |
Elie De Brauwer | ca4035b | 2012-12-16 13:43:36 +0100 | [diff] [blame] | 58 | printf("Killed %s(%d) with signal %d\n", name, pid, TT.signum); |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 59 | |
Rob Landley | 7d64dae | 2013-09-03 18:43:32 -0500 | [diff] [blame] | 60 | return 0; |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void killall_main(void) |
| 64 | { |
Rob Landley | 0638755 | 2013-12-19 22:20:08 -0600 | [diff] [blame] | 65 | int i; |
Rob Landley | 27cec9a | 2012-12-23 15:07:28 -0600 | [diff] [blame] | 66 | |
Rob Landley | 0638755 | 2013-12-19 22:20:08 -0600 | [diff] [blame] | 67 | TT.names = toys.optargs; |
Rob Landley | 27cec9a | 2012-12-23 15:07:28 -0600 | [diff] [blame] | 68 | TT.signum = SIGTERM; |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 69 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 70 | if (toys.optflags & FLAG_l) { |
| 71 | sig_to_num(NULL); |
| 72 | return; |
| 73 | } |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 74 | |
?ukasz Szpakowski | 08ac30d | 2014-09-22 08:32:21 -0500 | [diff] [blame] | 75 | if (TT.sig || (*TT.names && **TT.names == '-')) { |
Rob Landley | f070ec0 | 2013-12-22 19:39:12 -0600 | [diff] [blame] | 76 | if (0 > (TT.signum = sig_to_num(TT.sig ? TT.sig : (*TT.names)+1))) { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 77 | if (toys.optflags & FLAG_q) exit(1); |
| 78 | error_exit("Invalid signal"); |
| 79 | } |
Rob Landley | f070ec0 | 2013-12-22 19:39:12 -0600 | [diff] [blame] | 80 | if (!TT.sig) { |
| 81 | TT.names++; |
| 82 | toys.optc--; |
| 83 | } |
Rob Landley | 27cec9a | 2012-12-23 15:07:28 -0600 | [diff] [blame] | 84 | } |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 85 | |
Rob Landley | f070ec0 | 2013-12-22 19:39:12 -0600 | [diff] [blame] | 86 | if (!(toys.optflags & FLAG_l) && !toys.optc) { |
Rob Landley | 27cec9a | 2012-12-23 15:07:28 -0600 | [diff] [blame] | 87 | toys.exithelp++; |
Rob Landley | 0638755 | 2013-12-19 22:20:08 -0600 | [diff] [blame] | 88 | error_exit("no name"); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 89 | } |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 90 | |
Elie De Brauwer | 41b4748 | 2012-12-16 16:51:30 +0100 | [diff] [blame] | 91 | TT.cur_pid = getpid(); |
| 92 | |
Rob Landley | 0638755 | 2013-12-19 22:20:08 -0600 | [diff] [blame] | 93 | TT.err = xmalloc(2*toys.optc); |
| 94 | for (i=0; i<toys.optc; i++) TT.err[i] = ESRCH; |
| 95 | names_to_pid(TT.names, kill_process); |
| 96 | for (i=0; i<toys.optc; i++) { |
| 97 | if (TT.err[i]) { |
| 98 | toys.exitval = 1; |
| 99 | errno = TT.err[i]; |
| 100 | perror_msg("%s", TT.names[i]); |
| 101 | } |
| 102 | } |
| 103 | if (CFG_TOYBOX_FREE) free(TT.err); |
Rob Landley | ff9ee8f | 2012-02-18 15:12:41 -0600 | [diff] [blame] | 104 | } |