Mercurial > hg > toybox
annotate toys/lsb/killall.c @ 1008:a55854bde872
su: cleanery
* make help message more like others
* s/TT\.(.)Argu/TT.\1/g
* move environ to toys.h
* simplify failure messages
* clear password before quit
* not check what execve returns
* -lc
author | Strake <strake888@gmail.com> |
---|---|
date | Sat, 17 Aug 2013 02:54:58 -0500 |
parents | 979d038c1688 |
children | 242c5de2bb22 |
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; |
475
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 ) |
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
30 |
762
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
31 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
|
32 { |
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
|
33 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
|
34 |
766
5d435b48da8d
LSB does not allow killall to kill itself
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
762
diff
changeset
|
35 if (pid == TT.cur_pid) return 1; |
5d435b48da8d
LSB does not allow killall to kill itself
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
762
diff
changeset
|
36 |
767 | 37 if (toys.optflags & FLAG_i) { |
38 sprintf(toybuf, "Signal %s(%d) ?", name, pid); | |
762
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
39 if (yesno(toybuf, 0) == 0) return 1; |
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
40 } |
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
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 toys.exitval = 0; |
762
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
43 |
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
|
44 ret = kill(pid, TT.signum); |
762
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
45 if (toys.optflags & FLAG_v) |
f169d9708518
Extend killall with support for -v and -i
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
744
diff
changeset
|
46 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
|
47 |
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
|
48 if (ret == -1 && !(toys.optflags & FLAG_q)) perror("kill"); |
744
43e6ec52aa29
Adding -s (single shot) and -o (omit pids) options to pidof
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
696
diff
changeset
|
49 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
|
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 |
1fb149e75ebf
Add killall by Andreas Heck, and factor out common pid code to lib.h.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 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
|
53 { |
767 | 54 char **names = toys.optargs; |
55 | |
56 TT.signum = SIGTERM; | |
57 toys.exitval++; | |
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 |
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
|
59 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
|
60 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
|
61 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
|
62 } |
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 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
|
66 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
|
67 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
|
68 } |
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 names++; |
767 | 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 |
767 | 72 if (!*names) { |
73 toys.exithelp++; | |
74 error_exit("Process name missing!"); | |
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
|
75 } |
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 |
766
5d435b48da8d
LSB does not allow killall to kill itself
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
762
diff
changeset
|
77 TT.cur_pid = getpid(); |
5d435b48da8d
LSB does not allow killall to kill itself
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
762
diff
changeset
|
78 |
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
|
79 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
|
80 |
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
|
81 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
|
82 } |