Mercurial > hg > toybox
annotate toys/posix/kill.c @ 1381:cae43f3cfff4 draft
Promote killall5 by merging it into kill.
Slight conflict here: this puts killall5 in the posix directory. But the commands sharing code trumps that.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 06 Jul 2014 19:14:05 -0500 |
parents | 6cc69be43c42 |
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 /* kill.c - a program to send signals to processes |
490
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
2 * |
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
3 * Copyright 2012 Daniel Walter <d.walter@0x90.at> |
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
4 * |
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/kill.html |
1381
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
6 * |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
7 * killall5.c - Send signal to all processes outside current session. |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
8 * |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
9 * Copyright 2014 Ranjan Kumar <ranjankumar.bth@gmail.com> |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
10 * Copyright 2014 Kyungwan Han <asura321@gamil.com> |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
11 * |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
12 * No Standard |
490
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
13 |
1381
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
14 USE_KILL(NEWTOY(kill, "?ls: ", TOYFLAG_BIN)) |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
15 USE_KILLALL5(NEWTOY(killall5, "?o*ls: [!lo][!ls]", TOYFLAG_SBIN)) |
490
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
16 |
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
17 config KILL |
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 bool "kill" |
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
|
19 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
|
20 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
|
21 usage: kill [-l [SIGNAL] | -s SIGNAL | -SIGNAL] pid... |
490
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
22 |
1381
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
23 Send signal to process(es). |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
24 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
25 -l List signal name(s) and number(s) |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
26 -s Send SIGNAL (default SIGTERM) |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
27 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
28 config KILLALL5 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
29 bool "killall5" |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
30 default y |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
31 depends on KILL |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
32 help |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
33 usage: killall5 [-l [SIGNAL]] [-SIGNAL|-s SIGNAL] [-o PID]... |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
34 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
35 Send a signal to all processes outside current session. |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
36 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
37 -l List signal name(s) and number(s) |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
38 -o PID Omit PID |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
39 -s send SIGNAL (default SIGTERM) |
490
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
40 */ |
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
41 |
1381
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
42 // This has to match the filename: |
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:
653
diff
changeset
|
43 #define FOR_kill |
490
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
44 #include "toys.h" |
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
45 |
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:
653
diff
changeset
|
46 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
|
47 char *signame; |
1381
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
48 struct arg_list *olist; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
490
diff
changeset
|
49 ) |
490
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
50 |
1381
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
51 // But kill's flags are a subset of killall5's |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
52 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
53 #define CLEANUP_kill |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
54 #define FOR_killall5 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
55 #include "generated/flags.h" |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
56 |
490
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
57 void kill_main(void) |
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
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 int signum; |
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 char *tmp, **args = toys.optargs; |
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 pid_t pid; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
490
diff
changeset
|
62 |
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
|
63 // list signal(s) |
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 (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
|
65 if (*args) { |
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 int signum = sig_to_num(*args); |
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 char *s = NULL; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
490
diff
changeset
|
68 |
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
|
69 if (signum>=0) s = num_to_sig(signum&127); |
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 puts(s ? s : "UNKNOWN"); |
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 } else 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 } |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
490
diff
changeset
|
74 |
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 // signal must come before pids, so "kill -9 -1" isn't confusing. |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
490
diff
changeset
|
76 |
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 (!TT.signame && *args && **args=='-') TT.signame=*(args++)+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 if (TT.signame) { |
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 char *arg; |
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
|
80 int i = strtol(TT.signame, &arg, 10); |
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 (!*arg) arg = num_to_sig(i); |
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
|
82 else arg = TT.signame; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
490
diff
changeset
|
83 |
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 if (!arg || -1 == (signum = sig_to_num(arg))) |
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
|
85 error_exit("Unknown signal '%s'", arg); |
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
|
86 } else signum = SIGTERM; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
490
diff
changeset
|
87 |
1381
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
88 // is it killall5? |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
89 if (CFG_KILLALL5 && toys.which->name[4]=='a') { |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
90 DIR *dp; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
91 struct dirent *entry; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
92 int pid, sid; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
93 long *olist = 0, ocount = 0; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
94 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
95 // parse omit list |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
96 if (toys.optflags & FLAG_o) { |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
97 struct arg_list *ptr; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
98 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
99 for (ptr = TT.olist; ptr; ptr = ptr->next) ocount++; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
100 olist = xmalloc(ocount*sizeof(long)); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
101 ocount = 0; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
102 for (ptr = TT.olist; ptr; ptr=ptr->next) |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
103 olist[ocount++] = atolx(ptr->arg); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
104 } |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
105 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
106 sid = getsid(pid = getpid()); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
107 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
108 if (!(dp = opendir("/proc"))) perror_exit("/proc"); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
109 while ((entry = readdir(dp))) { |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
110 int count, procpid, procsid; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
111 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
112 if (!(procpid = atoi(entry->d_name))) continue; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
113 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
114 snprintf(toybuf, sizeof(toybuf), "/proc/%d/stat", procpid); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
115 if (!readfile(toybuf, toybuf, sizeof(toybuf))) continue; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
116 if (sscanf(toybuf, "%*d %*s %*c %*d %*d %d", &procsid) != 1) continue; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
117 if (pid == procpid || sid == procsid || procpid == 1) continue; |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
490
diff
changeset
|
118 |
1381
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
119 // Check for kernel threads. |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
120 snprintf(toybuf, sizeof(toybuf), "/proc/%d/cmdline", procpid); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
121 if (!readfile(toybuf, toybuf, sizeof(toybuf)) || !*toybuf) continue; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
122 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
123 // Check with omit list. |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
124 for (count = 0; count < ocount; count++) |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
125 if (procpid == olist[count]) break; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
126 if (count != ocount) continue; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
127 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
128 kill(procpid, signum); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
129 } |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
130 if (CFG_TOYBOX_FREE) { |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
131 closedir(dp); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
132 free(olist); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
133 } |
498
c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
Rob Landley <rob@landley.net>
parents:
490
diff
changeset
|
134 |
1381
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
135 // is it kill? |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
136 } else { |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
137 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
138 // "<1" in optstr wouldn't cover this because "-SIGNAL" |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
139 if (!*args) { |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
140 toys.exithelp++; |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
141 error_exit("missing argument"); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
142 } |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
143 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
144 while (*args) { |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
145 char *arg = *(args++); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
146 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
147 pid = strtol(arg, &tmp, 10); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
148 if (*tmp || kill(pid, signum) < 0) error_msg("unknown pid '%s'", arg); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
149 } |
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
|
150 } |
490
96a5e66a7dae
Add new kill toy. Used to send signals to a process or a process group.
Daniel Walter <d.walter@0x90.at>
parents:
diff
changeset
|
151 } |
1381
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
152 |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
153 void killall5_main(void) |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
154 { |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
155 kill_main(); |
cae43f3cfff4
Promote killall5 by merging it into kill.
Rob Landley <rob@landley.net>
parents:
780
diff
changeset
|
156 } |