annotate toys/lsb/killall.c @ 696:99ca30ad3d2b

Add rebound support to intercept error_exit() and longjmp instead.
author Rob Landley <rob@landley.net>
date Fri, 16 Nov 2012 00:35:46 -0600
parents 786841fdb1e0
children 43e6ec52aa29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
1 /* 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
2 *
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * 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
4 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
5 * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/killall.html
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
6
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
7 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
8
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 config KILLALL
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
10 bool "killall"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
11 default y
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
12 help
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
13 usage: killall [-l] [-q] [-SIG] PROCESS_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
14
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
15 Send a signal (default: TERM) to all processes with the 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
16
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
17 -l print list of all available signals
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
18 -q don't print any warnings or error messages
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 */
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
20
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
21 #define FOR_killall
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 #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
23
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
24 GLOBALS(
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
25 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
26 )
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
27
477
f0b07ce5f125 Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents: 475
diff changeset
28 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
29 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
30 int ret;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
31
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
32 toys.exitval = 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
33 ret = kill(pid, TT.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
34
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
35 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
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
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 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
39 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
40 char **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
41
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
42 if (toys.optflags & FLAG_l) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
43 sig_to_num(NULL);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
44 return;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
45 }
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
46
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
47 TT.signum = SIGTERM;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
48 toys.exitval++;
479
1a7110479d49 Cleanups to killall.
Rob Landley <rob@landley.net>
parents: 477
diff changeset
49
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
50 if (!*toys.optargs) {
696
99ca30ad3d2b Add rebound support to intercept error_exit() and longjmp instead.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
51 toys.exithelp++;
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
52 error_exit("Process name missing!");
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
53 }
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
54
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
55 names = toys.optargs;
475
1fb149e75ebf Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff changeset
56
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
57 if (**names == '-') {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
58 if (0 > (TT.signum = sig_to_num((*names)+1))) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
59 if (toys.optflags & FLAG_q) exit(1);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
60 error_exit("Invalid signal");
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
61 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
62 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
63
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
64 if (!*names) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
65 toys.exithelp++;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
66 error_exit("Process name missing!");
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
67 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
68 }
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
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
70 for_each_pid_with_name_in(names, kill_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
71
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
72 if (toys.exitval && !(toys.optflags & FLAG_q)) 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
73 }