Mercurial > hg > toybox
comparison toys/killall.c @ 477:f0b07ce5f125
Cleanups to pidof (including some global infrastructure shared with killall).
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 18 Feb 2012 18:09:14 -0600 |
parents | 1fb149e75ebf |
children | 1a7110479d49 |
comparison
equal
deleted
inserted
replaced
476:d10b58563cff | 477:f0b07ce5f125 |
---|---|
1 /* vi: set sw=4 ts=4: | 1 /* vi: set sw=4 ts=4: |
2 * | 2 * |
3 * killall.c - Send a signal (default: TERM) to all processes with the given names. | 3 * killall.c - Send signal (default: TERM) to all processes with given names. |
4 * | 4 * |
5 * Copyright 2012 Andreas Heck <aheck@gmx.de> | 5 * Copyright 2012 Andreas Heck <aheck@gmx.de> |
6 * | 6 * |
7 * Not in SUSv4. | 7 * Not in SUSv4. |
8 * See http://opengroup.org/onlinepubs/9699919799/utilities/ | 8 |
9 | 9 USE_KILLALL(NEWTOY(killall, "<1?lq", TOYFLAG_USR|TOYFLAG_BIN)) |
10 USE_KILLALL(NEWTOY(killall, "?lq", TOYFLAG_USR|TOYFLAG_BIN)) | |
11 | 10 |
12 config KILLALL | 11 config KILLALL |
13 bool "killall" | 12 bool "killall" |
14 default y | 13 default y |
15 help | 14 help |
25 | 24 |
26 #define FLAG_q 1 | 25 #define FLAG_q 1 |
27 #define FLAG_l 2 | 26 #define FLAG_l 2 |
28 | 27 |
29 DEFINE_GLOBALS( | 28 DEFINE_GLOBALS( |
30 int matched; | 29 int matched; |
31 int signum; | 30 int signum; |
32 ) | 31 ) |
33 #define TT this.killall | 32 #define TT this.killall |
34 | 33 |
35 struct signame { | 34 struct signame { |
36 int num; | 35 int num; |
147 {SIGUNUSED, "UNUSED"}, | 146 {SIGUNUSED, "UNUSED"}, |
148 #endif | 147 #endif |
149 {0, NULL} | 148 {0, NULL} |
150 }; | 149 }; |
151 | 150 |
152 static int sig_to_num(const char *pidstr) { | 151 static int sig_to_num(const char *pidstr) |
152 { | |
153 int i, num; | 153 int i, num; |
154 | 154 |
155 if (isdigit(pidstr[0])) { | 155 if (isdigit(pidstr[0])) { |
156 num = atoi(pidstr); | 156 num = atoi(pidstr); |
157 | 157 |
165 } | 165 } |
166 | 166 |
167 return -1; | 167 return -1; |
168 } | 168 } |
169 | 169 |
170 static void print_signals() { | 170 static void print_signals() |
171 { | |
171 int i; | 172 int i; |
172 | 173 |
173 for (i = 0; signames[i].num; i++) { | 174 for (i = 0; signames[i].num; i++) { |
174 puts(signames[i].name); | 175 puts(signames[i].name); |
175 } | 176 } |
176 } | 177 } |
177 | 178 |
178 static void kill_process(const char *pidstr) { | 179 static void kill_process(pid_t pid) |
180 { | |
179 int ret; | 181 int ret; |
180 pid_t pid = atoi(pidstr); | |
181 | 182 |
182 TT.matched = 1; | 183 TT.matched = 1; |
183 ret = kill(pid, TT.signum); | 184 ret = kill(pid, TT.signum); |
184 | 185 |
185 if (ret == -1) { | 186 if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill"); |
186 if (toys.optflags & FLAG_q) perror("kill"); | |
187 } | |
188 } | 187 } |
189 | 188 |
190 void killall_main(void) | 189 void killall_main(void) |
191 { | 190 { |
192 char **names; | 191 char **names; |
193 | 192 |
194 TT.matched = 0; | |
195 TT.signum = SIGTERM; | 193 TT.signum = SIGTERM; |
196 | 194 |
197 if (toys.optflags & FLAG_l) { | 195 if (toys.optflags & FLAG_l) { |
198 print_signals(); | 196 print_signals(); |
199 exit(0); | 197 return; |
200 } | 198 } |
201 | 199 |
202 if (!*toys.optargs) { | 200 if (!*toys.optargs) { |
203 toys.exithelp = 1; | 201 toys.exithelp = 1; |
204 error_exit("Process name missing!"); | 202 error_exit("Process name missing!"); |
205 } | 203 } |
206 | 204 |
207 names = toys.optargs; | 205 names = toys.optargs; |
208 | 206 |
209 if ((*toys.optargs)[0] == '-') { | 207 if (**names == '-') { |
210 TT.signum = sig_to_num(&(*toys.optargs)[1]); | 208 TT.signum = sig_to_num((*names)+1); |
211 if (TT.signum <= 0) { | 209 if (TT.signum <= 0) { |
212 if (toys.optflags & FLAG_q) fprintf(stderr, "Invalid signal\n"); | 210 if (toys.optflags & FLAG_q) error_exit("Invalid signal"); |
213 exit(1); | 211 exit(1); |
214 } | 212 } |
215 names = ++toys.optargs; | 213 names++; |
216 } | 214 } |
217 | 215 |
218 if (!*names) { | 216 if (!*names) { |
219 toys.exithelp = 1; | 217 toys.exithelp = 1; |
220 error_exit("Process name missing!"); | 218 error_exit("Process name missing!"); |