blob: e892755977ca02a3eaa67544ee6507a87b58a48d [file] [log] [blame]
Rob Landley7aa651a2012-11-13 17:14:08 -06001/* killall.c - Send signal (default: TERM) to all processes with given names.
Rob Landleyff9ee8f2012-02-18 15:12:41 -06002 *
3 * Copyright 2012 Andreas Heck <aheck@gmx.de>
4 *
Rob Landleyf91b7c82012-08-25 18:08:51 -05005 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/killall.html
Rob Landleyff9ee8f2012-02-18 15:12:41 -06006
Rob Landleyf070ec02013-12-22 19:39:12 -06007USE_KILLALL(NEWTOY(killall, "?s:lqvi", TOYFLAG_USR|TOYFLAG_BIN))
Rob Landleyff9ee8f2012-02-18 15:12:41 -06008
9config KILLALL
Rob Landley7aa651a2012-11-13 17:14:08 -060010 bool "killall"
11 default y
12 help
Rob Landleyf070ec02013-12-22 19:39:12 -060013 usage: killall [-l] [-iqv] [-SIGNAL|-s SIGNAL] PROCESS_NAME...
Rob Landleyff9ee8f2012-02-18 15:12:41 -060014
Rob Landley7aa651a2012-11-13 17:14:08 -060015 Send a signal (default: TERM) to all processes with the given names.
Rob Landleyff9ee8f2012-02-18 15:12:41 -060016
Elie De Brauwerca4035b2012-12-16 13:43:36 +010017 -i ask for confirmation before killing
Rob Landley27cec9a2012-12-23 15:07:28 -060018 -l print list of all available signals
Rob Landley7aa651a2012-11-13 17:14:08 -060019 -q don't print any warnings or error messages
Rob Landleyf070ec02013-12-22 19:39:12 -060020 -s send SIGNAL instead of SIGTERM
Rob Landley27cec9a2012-12-23 15:07:28 -060021 -v report if the signal was successfully sent
Rob Landleyff9ee8f2012-02-18 15:12:41 -060022*/
23
Rob Landleyc0e56ed2012-10-08 00:02:30 -050024#define FOR_killall
Rob Landleyff9ee8f2012-02-18 15:12:41 -060025#include "toys.h"
26
Rob Landleyc0e56ed2012-10-08 00:02:30 -050027GLOBALS(
Rob Landleyf070ec02013-12-22 19:39:12 -060028 char *sig;
29
Rob Landley7aa651a2012-11-13 17:14:08 -060030 int signum;
Elie De Brauwer41b47482012-12-16 16:51:30 +010031 pid_t cur_pid;
Rob Landley06387552013-12-19 22:20:08 -060032 char **names;
33 short *err;
Rob Landleyff9ee8f2012-02-18 15:12:41 -060034)
Rob Landleyff9ee8f2012-02-18 15:12:41 -060035
Elie De Brauwerca4035b2012-12-16 13:43:36 +010036static int kill_process(pid_t pid, char *name)
Rob Landleyf42e11b2012-02-18 18:09:14 -060037{
Rob Landley06387552013-12-19 22:20:08 -060038 int offset = 0;
Rob Landleyff9ee8f2012-02-18 15:12:41 -060039
Rob Landley7d64dae2013-09-03 18:43:32 -050040 if (pid == TT.cur_pid) return 0;
Elie De Brauwer41b47482012-12-16 16:51:30 +010041
Rob Landley27cec9a2012-12-23 15:07:28 -060042 if (toys.optflags & FLAG_i) {
Rob Landleyb1353fb2015-09-07 17:12:57 -050043 fprintf(stderr, "Signal %s(%d)", name, (int)pid);
44 if (!yesno(0)) return 0;
Elie De Brauwerca4035b2012-12-16 13:43:36 +010045 }
46
Rob Landley06387552013-12-19 22:20:08 -060047 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 Brauwerca4035b2012-12-16 13:43:36 +010058 printf("Killed %s(%d) with signal %d\n", name, pid, TT.signum);
Rob Landleyff9ee8f2012-02-18 15:12:41 -060059
Rob Landley7d64dae2013-09-03 18:43:32 -050060 return 0;
Rob Landleyff9ee8f2012-02-18 15:12:41 -060061}
62
63void killall_main(void)
64{
Rob Landley06387552013-12-19 22:20:08 -060065 int i;
Rob Landley27cec9a2012-12-23 15:07:28 -060066
Rob Landley06387552013-12-19 22:20:08 -060067 TT.names = toys.optargs;
Rob Landley27cec9a2012-12-23 15:07:28 -060068 TT.signum = SIGTERM;
Rob Landleyff9ee8f2012-02-18 15:12:41 -060069
Rob Landley7aa651a2012-11-13 17:14:08 -060070 if (toys.optflags & FLAG_l) {
71 sig_to_num(NULL);
72 return;
73 }
Rob Landleyff9ee8f2012-02-18 15:12:41 -060074
?ukasz Szpakowski08ac30d2014-09-22 08:32:21 -050075 if (TT.sig || (*TT.names && **TT.names == '-')) {
Rob Landleyf070ec02013-12-22 19:39:12 -060076 if (0 > (TT.signum = sig_to_num(TT.sig ? TT.sig : (*TT.names)+1))) {
Rob Landley7aa651a2012-11-13 17:14:08 -060077 if (toys.optflags & FLAG_q) exit(1);
78 error_exit("Invalid signal");
79 }
Rob Landleyf070ec02013-12-22 19:39:12 -060080 if (!TT.sig) {
81 TT.names++;
82 toys.optc--;
83 }
Rob Landley27cec9a2012-12-23 15:07:28 -060084 }
Rob Landleyff9ee8f2012-02-18 15:12:41 -060085
Rob Landleyf070ec02013-12-22 19:39:12 -060086 if (!(toys.optflags & FLAG_l) && !toys.optc) {
Rob Landley27cec9a2012-12-23 15:07:28 -060087 toys.exithelp++;
Rob Landley06387552013-12-19 22:20:08 -060088 error_exit("no name");
Rob Landley7aa651a2012-11-13 17:14:08 -060089 }
Rob Landleyff9ee8f2012-02-18 15:12:41 -060090
Elie De Brauwer41b47482012-12-16 16:51:30 +010091 TT.cur_pid = getpid();
92
Rob Landley06387552013-12-19 22:20:08 -060093 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 Landleyff9ee8f2012-02-18 15:12:41 -0600104}