Mercurial > hg > toybox
annotate toys/lsb/killall.c @ 1539:3e85af1f7e22 draft
First batch of sed tests.
Only good for TEST_HOST=1 at the moment because the test infrastructure itself
depends on sed, so if an unfinished sed is in the $PATH it goes boing. But
hey, corner cases!
I have... more.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 29 Oct 2014 18:44:33 -0500 |
parents | 4bfbd8b96f66 |
children |
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 |
1154
f7b777035025
Add -s option, allow zero optargs for -l. (Suggested by Ashwini Sharma.)
Rob Landley <rob@landley.net>
parents:
1151
diff
changeset
|
7 USE_KILLALL(NEWTOY(killall, "?s:lqvi", 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 |
1154
f7b777035025
Add -s option, allow zero optargs for -l. (Suggested by Ashwini Sharma.)
Rob Landley <rob@landley.net>
parents:
1151
diff
changeset
|
13 usage: killall [-l] [-iqv] [-SIGNAL|-s SIGNAL] 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 |
767 | 17 -i ask for confirmation before killing |
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
|
18 -l print list of all available signals |
767 | 19 -q don't print any warnings or error messages |
1154
f7b777035025
Add -s option, allow zero optargs for -l. (Suggested by Ashwini Sharma.)
Rob Landley <rob@landley.net>
parents:
1151
diff
changeset
|
20 -s send SIGNAL instead of SIGTERM |
762
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
21 -v report if the signal was successfully sent |
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 */ |
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 #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
|
25 #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
|
26 |
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
|
27 GLOBALS( |
1154
f7b777035025
Add -s option, allow zero optargs for -l. (Suggested by Ashwini Sharma.)
Rob Landley <rob@landley.net>
parents:
1151
diff
changeset
|
28 char *sig; |
f7b777035025
Add -s option, allow zero optargs for -l. (Suggested by Ashwini Sharma.)
Rob Landley <rob@landley.net>
parents:
1151
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 signum; |
766
5d435b48da8d
LSB does not allow killall to kill itself
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
762
diff
changeset
|
31 pid_t cur_pid; |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
32 char **names; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
33 short *err; |
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 ) |
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 |
762
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
36 static int kill_process(pid_t pid, char *name) |
477
f0b07ce5f125
Cleanups to pidof (including some global infrastructure shared with killall).
Rob Landley <rob@landley.net>
parents:
475
diff
changeset
|
37 { |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
38 int offset = 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
|
39 |
1057
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
767
diff
changeset
|
40 if (pid == TT.cur_pid) return 0; |
766
5d435b48da8d
LSB does not allow killall to kill itself
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
762
diff
changeset
|
41 |
767 | 42 if (toys.optflags & FLAG_i) { |
1518
4bfbd8b96f66
Various bugfixes (mostly resource leaks) from Ashwini Sharma's static analysis, plus occasional tweak by me while reviewing them.
Rob Landley <rob@landley.net>
parents:
1498
diff
changeset
|
43 snprintf(toybuf, sizeof(toybuf), "Signal %s(%d) ?", name, (int)pid); |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
44 if (!yesno(toybuf, 0)) return 0; |
762
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
45 } |
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
46 |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
47 errno = 0; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
48 kill(pid, TT.signum); |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
49 for (;;) { |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
50 if (TT.names[offset] == name) { |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
51 TT.err[offset] = errno; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
52 break; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
53 } else offset++; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
54 } |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
55 if (errno) { |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
56 if (!(toys.optflags & FLAG_q)) perror_msg("pid %d", (int)pid); |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
57 } else if (toys.optflags & FLAG_v) |
762
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
58 printf("Killed %s(%d) with signal %d\n", name, 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
|
59 |
1057
242c5de2bb22
Replace for_each_pid_with_name_in_array_perform_callback_function_upon_translated_value() with name_to_pid(), comparing absolute paths or just basename() consistently as spotted by Lukasz Skalski, and adjust callers.
Rob Landley <rob@landley.net>
parents:
767
diff
changeset
|
60 return 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
|
61 } |
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
62 |
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
63 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
|
64 { |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
65 int i; |
767 | 66 |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
67 TT.names = toys.optargs; |
767 | 68 TT.signum = SIGTERM; |
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 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
|
71 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
|
72 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
|
73 } |
475
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
74 |
1498
d965dfbee154
When killall was invoked without arguments, there appeared segmentation fault
?ukasz Szpakowski <luckboy@vp.pl>
parents:
1154
diff
changeset
|
75 if (TT.sig || (*TT.names && **TT.names == '-')) { |
1154
f7b777035025
Add -s option, allow zero optargs for -l. (Suggested by Ashwini Sharma.)
Rob Landley <rob@landley.net>
parents:
1151
diff
changeset
|
76 if (0 > (TT.signum = sig_to_num(TT.sig ? TT.sig : (*TT.names)+1))) { |
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
|
77 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
|
78 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
|
79 } |
1154
f7b777035025
Add -s option, allow zero optargs for -l. (Suggested by Ashwini Sharma.)
Rob Landley <rob@landley.net>
parents:
1151
diff
changeset
|
80 if (!TT.sig) { |
f7b777035025
Add -s option, allow zero optargs for -l. (Suggested by Ashwini Sharma.)
Rob Landley <rob@landley.net>
parents:
1151
diff
changeset
|
81 TT.names++; |
f7b777035025
Add -s option, allow zero optargs for -l. (Suggested by Ashwini Sharma.)
Rob Landley <rob@landley.net>
parents:
1151
diff
changeset
|
82 toys.optc--; |
f7b777035025
Add -s option, allow zero optargs for -l. (Suggested by Ashwini Sharma.)
Rob Landley <rob@landley.net>
parents:
1151
diff
changeset
|
83 } |
767 | 84 } |
475
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 |
1154
f7b777035025
Add -s option, allow zero optargs for -l. (Suggested by Ashwini Sharma.)
Rob Landley <rob@landley.net>
parents:
1151
diff
changeset
|
86 if (!(toys.optflags & FLAG_l) && !toys.optc) { |
767 | 87 toys.exithelp++; |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
88 error_exit("no name"); |
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
|
89 } |
475
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
90 |
766
5d435b48da8d
LSB does not allow killall to kill itself
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
762
diff
changeset
|
91 TT.cur_pid = getpid(); |
5d435b48da8d
LSB does not allow killall to kill itself
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
762
diff
changeset
|
92 |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
93 TT.err = xmalloc(2*toys.optc); |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
94 for (i=0; i<toys.optc; i++) TT.err[i] = ESRCH; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
95 names_to_pid(TT.names, kill_process); |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
96 for (i=0; i<toys.optc; i++) { |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
97 if (TT.err[i]) { |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
98 toys.exitval = 1; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
99 errno = TT.err[i]; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
100 perror_msg("%s", TT.names[i]); |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
101 } |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
102 } |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
103 if (CFG_TOYBOX_FREE) free(TT.err); |
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 } |