Mercurial > hg > toybox
annotate toys/kill.c @ 498:c9aaceccd6bd
Factor out common code between killall/kill and move it to lib/lib.c, plus cleanups on kill.c.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 26 Feb 2012 13:48:00 -0600 |
parents | 96a5e66a7dae |
children | bc4615e2e339 |
rev | line source |
---|---|
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
|
1 /* vi: set sw=4 ts=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
|
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 * kill.c - a program to send signals to processes |
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 * 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
|
6 * |
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
|
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/kill.html |
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
|
8 |
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
|
9 USE_KILL(NEWTOY(kill, "?s:l", TOYFLAG_BIN)) |
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
|
10 |
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
|
11 config KILL |
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
|
12 bool "kill" |
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 default y |
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
|
14 help |
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
|
15 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
|
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 Send a signal to a process |
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
|
18 |
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
|
19 */ |
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
|
20 |
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
|
21 #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
|
22 |
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
|
23 DEFINE_GLOBALS( |
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
|
24 char *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
|
25 ) |
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
|
26 |
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
|
27 #define TT this.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
|
28 |
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
|
29 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
|
30 { |
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
|
31 int signum; |
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
|
32 char *tmp, **args = toys.optargs; |
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
|
33 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
|
34 |
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
|
35 // list signal(s) |
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
|
36 if (toys.optflags & 1) { |
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
|
37 if (*args) { |
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
|
38 int signum = sig_to_num(*args); |
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
|
39 char *s = NULL; |
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
|
40 |
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
|
41 if (signum>=0) s = num_to_sig(signum&127); |
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
|
42 puts(s ? s : "UNKNOWN"); |
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
|
43 } else sig_to_num(NULL); |
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
|
44 return; |
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
|
45 } |
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
|
46 |
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
|
47 // signal must come before pids, so "kill -9 -1" isn't confusing. |
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
|
48 printf("*args=%s\n", *args); |
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 |
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
|
50 if (!TT.signame && *args && **args=='-') TT.signame=*(args++)+1; |
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
|
51 printf("TT.signame=%s\n", TT.signame); |
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
|
52 if (TT.signame) { |
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
|
53 char *arg; |
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
|
54 int i = strtol(TT.signame, &arg, 10); |
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
|
55 if (!*arg) arg = num_to_sig(i); |
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
|
56 else arg = TT.signame; |
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
|
57 |
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
|
58 if (!arg || -1 == (signum = sig_to_num(arg))) |
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
|
59 error_exit("Unknown signal '%s'", arg); |
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
|
60 } else signum = SIGTERM; |
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
|
61 |
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 if (!*args) { |
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
|
63 toys.exithelp++; |
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
|
64 error_exit("missing argument"); |
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
|
65 } |
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
|
66 |
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
|
67 while (*args) { |
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 char *arg = *(args++); |
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
|
69 |
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
|
70 pid = strtol(arg, &tmp, 10); |
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
|
71 if (*tmp || kill(pid, signum) < 0) { |
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
|
72 error_msg("unknown pid '%s'", arg); |
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
|
73 toys.exitval = EXIT_FAILURE; |
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
|
74 } |
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
|
75 } |
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
|
76 } |