annotate toys/killall.c @ 479:1a7110479d49

Cleanups to killall.
author Rob Landley <rob@landley.net>
date Sat, 18 Feb 2012 18:54:30 -0600
parents f0b07ce5f125
children c9aaceccd6bd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
3 * killall.c - Send signal (default: TERM) to all processes with given names.
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2012 Andreas Heck <aheck@gmx.de>
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * Not in SUSv4.
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
9 USE_KILLALL(NEWTOY(killall, "<1?lq", TOYFLAG_USR|TOYFLAG_BIN))
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config KILLALL
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "killall"
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 default y
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 usage: killall [-l] [-q] [-SIG] PROCESS_NAME...
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
16
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 Send a signal (default: TERM) to all processes with the given names.
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
18
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 -l print list of all available signals
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 -q don't print any warnings or error messages
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 */
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
22
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 #include "toys.h"
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 #define FLAG_q 1
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 #define FLAG_l 2
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
27
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 DEFINE_GLOBALS(
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
29 int signum;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 )
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 #define TT this.killall
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
32
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 struct signame {
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 int num;
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
35 char *name;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 };
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
37
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
38 // Signals required by POSIX 2008:
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
39 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
40
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
41 #define SIGNIFY(x) {SIG##x, #x}
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
42
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 static struct signame signames[] = {
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
44 SIGNIFY(ABRT), SIGNIFY(ALRM), SIGNIFY(BUS), SIGNIFY(CHLD), SIGNIFY(CONT),
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
45 SIGNIFY(FPE), SIGNIFY(HUP), SIGNIFY(ILL), SIGNIFY(INT), SIGNIFY(KILL),
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
46 SIGNIFY(PIPE), SIGNIFY(QUIT), SIGNIFY(SEGV), SIGNIFY(STOP), SIGNIFY(TERM),
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
47 SIGNIFY(TSTP), SIGNIFY(TTIN), SIGNIFY(TTOU), SIGNIFY(USR1), SIGNIFY(USR2),
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
48 SIGNIFY(SYS), SIGNIFY(TRAP), SIGNIFY(URG), SIGNIFY(VTALRM), SIGNIFY(XCPU),
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
49 SIGNIFY(XFSZ)
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 };
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
51
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
52 // SIGNIFY(STKFLT), SIGNIFY(WINCH), SIGNIFY(IO), SIGNIFY(PWR)
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
53
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
54 // Convert name to signal number. If name == NULL print names.
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
55 static int sig_to_num(char *pidstr)
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
56 {
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 int i;
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
58
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
59 if (pidstr) {
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
60 if (isdigit(*pidstr)) return atol(pidstr);
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
61 if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 }
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
63 for (i = 0; i < sizeof(signames)/sizeof(struct signame); i++)
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
64 if (!pidstr) xputs(signames[i].name);
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
65 else if (!strcasecmp(pidstr, signames[i].name))
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
66 return signames[i].num;
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
67
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
68 return -1;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 }
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
70
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
71 static void kill_process(pid_t pid)
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
72 {
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 int ret;
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
74
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
75 toys.exitval = 0;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 ret = kill(pid, TT.signum);
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
77
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
78 if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill");
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 }
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
80
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 void killall_main(void)
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 {
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 char **names;
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
84
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 if (toys.optflags & FLAG_l) {
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
86 sig_to_num(NULL);
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
87 return;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 }
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
89
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
90 TT.signum = SIGTERM;
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
91 toys.exitval++;
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
92
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 if (!*toys.optargs) {
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 toys.exithelp = 1;
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 error_exit("Process name missing!");
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 }
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
97
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 names = toys.optargs;
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
99
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
100 if (**names == '-') {
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
101 if (0 > (TT.signum = sig_to_num((*names)+1))) {
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
102 if (toys.optflags & FLAG_q) exit(1);
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
103 error_exit("Invalid signal");
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 }
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
105 names++;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
106
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
107 if (!*names) {
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
108 toys.exithelp++;
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
109 error_exit("Process name missing!");
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
110 }
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 }
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
112
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 for_each_pid_with_name_in(names, kill_process);
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
114
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
115 if (toys.exitval && !(toys.optflags & FLAG_q))
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
116 error_exit("No such process");
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 }