Mercurial > hg > toybox
annotate toys/kill.c @ 499:bc4615e2e339
Teach lib/args.c that " " this option must take a _separate_ argument, so "kill -stop" and "kill -s top" aren't the same thing. Make kill.c use it, and remove leftover debug printfs.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 26 Feb 2012 16:11:25 -0600 |
parents | c9aaceccd6bd |
children |
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 |
499
bc4615e2e339
Teach lib/args.c that " " this option must take a _separate_ argument, so "kill -stop" and "kill -s top" aren't the same thing. Make kill.c use it, and remove leftover debug printfs.
Rob Landley <rob@landley.net>
parents:
498
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 |
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 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 |
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 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
|
57 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
|
58 } 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
|
59 |
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 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
|
61 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
|
62 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
|
63 } |
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 |
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 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
|
66 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
|
67 |
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 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
|
69 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
|
70 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
|
71 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
|
72 } |
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
|
73 } |
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 } |