comparison toys/lsb/killall.c @ 653:2986aa63a021

Move commands into "posix", "lsb", and "other" menus/directories.
author Rob Landley <rob@landley.net>
date Sat, 25 Aug 2012 14:25:22 -0500
parents toys/killall.c@c9aaceccd6bd
children 6df4ccc0acbe
comparison
equal deleted inserted replaced
652:2d7c56913fda 653:2986aa63a021
1 /* vi: set sw=4 ts=4:
2 *
3 * killall.c - Send signal (default: TERM) to all processes with given names.
4 *
5 * Copyright 2012 Andreas Heck <aheck@gmx.de>
6 *
7 * Not in SUSv4.
8
9 USE_KILLALL(NEWTOY(killall, "<1?lq", TOYFLAG_USR|TOYFLAG_BIN))
10
11 config KILLALL
12 bool "killall"
13 default y
14 help
15 usage: killall [-l] [-q] [-SIG] PROCESS_NAME...
16
17 Send a signal (default: TERM) to all processes with the given names.
18
19 -l print list of all available signals
20 -q don't print any warnings or error messages
21 */
22
23 #include "toys.h"
24
25 #define FLAG_q 1
26 #define FLAG_l 2
27
28 DEFINE_GLOBALS(
29 int signum;
30 )
31 #define TT this.killall
32
33 static void kill_process(pid_t pid)
34 {
35 int ret;
36
37 toys.exitval = 0;
38 ret = kill(pid, TT.signum);
39
40 if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill");
41 }
42
43 void killall_main(void)
44 {
45 char **names;
46
47 if (toys.optflags & FLAG_l) {
48 sig_to_num(NULL);
49 return;
50 }
51
52 TT.signum = SIGTERM;
53 toys.exitval++;
54
55 if (!*toys.optargs) {
56 toys.exithelp = 1;
57 error_exit("Process name missing!");
58 }
59
60 names = toys.optargs;
61
62 if (**names == '-') {
63 if (0 > (TT.signum = sig_to_num((*names)+1))) {
64 if (toys.optflags & FLAG_q) exit(1);
65 error_exit("Invalid signal");
66 }
67 names++;
68
69 if (!*names) {
70 toys.exithelp++;
71 error_exit("Process name missing!");
72 }
73 }
74
75 for_each_pid_with_name_in(names, kill_process);
76
77 if (toys.exitval && !(toys.optflags & FLAG_q))
78 error_exit("No such process");
79 }