aboutsummaryrefslogtreecommitdiff
path: root/toys/lsb/killall.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-08-25 14:25:22 -0500
committerRob Landley <rob@landley.net>2012-08-25 14:25:22 -0500
commit3a9241add947cb6d24b5de7a8927517426a78795 (patch)
treed122ab6570439cd6b17c7d73ed8d4e085e0b8a95 /toys/lsb/killall.c
parent689f095bc976417bf50810fe59a3b3ac32b21105 (diff)
downloadtoybox-3a9241add947cb6d24b5de7a8927517426a78795.tar.gz
Move commands into "posix", "lsb", and "other" menus/directories.
Diffstat (limited to 'toys/lsb/killall.c')
-rw-r--r--toys/lsb/killall.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/toys/lsb/killall.c b/toys/lsb/killall.c
new file mode 100644
index 00000000..b4e06944
--- /dev/null
+++ b/toys/lsb/killall.c
@@ -0,0 +1,79 @@
+/* vi: set sw=4 ts=4:
+ *
+ * killall.c - Send signal (default: TERM) to all processes with given names.
+ *
+ * Copyright 2012 Andreas Heck <aheck@gmx.de>
+ *
+ * Not in SUSv4.
+
+USE_KILLALL(NEWTOY(killall, "<1?lq", TOYFLAG_USR|TOYFLAG_BIN))
+
+config KILLALL
+ bool "killall"
+ default y
+ help
+ usage: killall [-l] [-q] [-SIG] PROCESS_NAME...
+
+ Send a signal (default: TERM) to all processes with the given names.
+
+ -l print list of all available signals
+ -q don't print any warnings or error messages
+*/
+
+#include "toys.h"
+
+#define FLAG_q 1
+#define FLAG_l 2
+
+DEFINE_GLOBALS(
+ int signum;
+)
+#define TT this.killall
+
+static void kill_process(pid_t pid)
+{
+ int ret;
+
+ toys.exitval = 0;
+ ret = kill(pid, TT.signum);
+
+ if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill");
+}
+
+void killall_main(void)
+{
+ char **names;
+
+ if (toys.optflags & FLAG_l) {
+ sig_to_num(NULL);
+ return;
+ }
+
+ TT.signum = SIGTERM;
+ toys.exitval++;
+
+ if (!*toys.optargs) {
+ toys.exithelp = 1;
+ error_exit("Process name missing!");
+ }
+
+ names = toys.optargs;
+
+ if (**names == '-') {
+ if (0 > (TT.signum = sig_to_num((*names)+1))) {
+ if (toys.optflags & FLAG_q) exit(1);
+ error_exit("Invalid signal");
+ }
+ names++;
+
+ if (!*names) {
+ toys.exithelp++;
+ error_exit("Process name missing!");
+ }
+ }
+
+ for_each_pid_with_name_in(names, kill_process);
+
+ if (toys.exitval && !(toys.optflags & FLAG_q))
+ error_exit("No such process");
+}