killall.c revision 7aa651a6a4496d848f86de9b1e6b3a003256a01f
1/* killall.c - Send signal (default: TERM) to all processes with given names.
2 *
3 * Copyright 2012 Andreas Heck <aheck@gmx.de>
4 *
5 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/killall.html
6
7USE_KILLALL(NEWTOY(killall, "<1?lq", TOYFLAG_USR|TOYFLAG_BIN))
8
9config KILLALL
10  bool "killall"
11  default y
12  help
13    usage: killall [-l] [-q] [-SIG] PROCESS_NAME...
14
15    Send a signal (default: TERM) to all processes with the given names.
16
17    -l	print list of all available signals
18    -q	don't print any warnings or error messages
19*/
20
21#define FOR_killall
22#include "toys.h"
23
24GLOBALS(
25  int signum;
26)
27
28static void kill_process(pid_t pid)
29{
30  int ret;
31
32  toys.exitval = 0;
33  ret = kill(pid, TT.signum);
34
35  if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill");
36}
37
38void killall_main(void)
39{
40  char **names;
41
42  if (toys.optflags & FLAG_l) {
43    sig_to_num(NULL);
44    return;
45  }
46
47  TT.signum = SIGTERM;
48  toys.exitval++;
49
50  if (!*toys.optargs) {
51    toys.exithelp = 1;
52    error_exit("Process name missing!");
53  }
54
55  names = toys.optargs;
56
57  if (**names == '-') {
58    if (0 > (TT.signum = sig_to_num((*names)+1))) {
59      if (toys.optflags & FLAG_q) exit(1);
60      error_exit("Invalid signal");
61    }
62    names++;
63
64    if (!*names) {
65      toys.exithelp++;
66      error_exit("Process name missing!");
67    }
68  }
69
70  for_each_pid_with_name_in(names, kill_process);
71
72  if (toys.exitval && !(toys.optflags & FLAG_q)) error_exit("No such process");
73}
74