Mercurial > hg > toybox
annotate toys/lsb/killall.c @ 1151:a9374aa2631a draft
killall: fix return code, improve error reporting, avoid buffer overflow.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 19 Dec 2013 22:20:08 -0600 |
parents | 242c5de2bb22 |
children | f7b777035025 |
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 |
762
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
7 USE_KILLALL(NEWTOY(killall, "<1?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 |
767 | 13 usage: killall [-l] [-iqv] [-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 |
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 |
762
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
20 -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
|
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 |
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
|
23 #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
|
24 #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
|
25 |
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
|
26 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
|
27 int signum; |
766
5d435b48da8d
LSB does not allow killall to kill itself
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
762
diff
changeset
|
28 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
|
29 char **names; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
30 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
|
31 ) |
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 |
762
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
33 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
|
34 { |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
35 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
|
36 |
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
|
37 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
|
38 |
767 | 39 if (toys.optflags & FLAG_i) { |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
40 sprintf(toybuf, "Signal %4000s(%d) ?", name, (int)pid); |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
41 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
|
42 } |
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
43 |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
44 errno = 0; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
45 kill(pid, TT.signum); |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
46 for (;;) { |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
47 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
|
48 TT.err[offset] = errno; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
49 break; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
50 } else offset++; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
51 } |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
52 if (errno) { |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
53 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
|
54 } 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
|
55 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
|
56 |
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
|
57 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
|
58 } |
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
59 |
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
60 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
|
61 { |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
62 int i; |
767 | 63 |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
64 TT.names = toys.optargs; |
767 | 65 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
|
66 |
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
|
67 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
|
68 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
|
69 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
|
70 } |
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 |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
72 if (**TT.names == '-') { |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
73 if (0 > (TT.signum = sig_to_num((*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
|
74 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
|
75 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
|
76 } |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
77 TT.names++; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
78 toys.optc--; |
767 | 79 } |
475
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
80 |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
81 if (!toys.optc) { |
767 | 82 toys.exithelp++; |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
83 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
|
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 |
766
5d435b48da8d
LSB does not allow killall to kill itself
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
762
diff
changeset
|
86 TT.cur_pid = getpid(); |
5d435b48da8d
LSB does not allow killall to kill itself
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
762
diff
changeset
|
87 |
1151
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
88 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
|
89 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
|
90 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
|
91 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
|
92 if (TT.err[i]) { |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
93 toys.exitval = 1; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
94 errno = TT.err[i]; |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
95 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
|
96 } |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
97 } |
a9374aa2631a
killall: fix return code, improve error reporting, avoid buffer overflow.
Rob Landley <rob@landley.net>
parents:
1057
diff
changeset
|
98 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
|
99 } |