Mercurial > hg > toybox
annotate toys/posix/kill.c @ 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.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 2986aa63a021 |
children | 786841fdb1e0 |
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 |
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
|
21 #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
|
22 #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
|
23 |
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
|
24 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
|
25 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
|
26 ) |
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
|
27 |
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 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
|
29 { |
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
|
30 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
|
31 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
|
32 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
|
33 |
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 // list signal(s) |
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
|
35 if (toys.optflags & FLAG_l) { |
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
|
36 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
|
37 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
|
38 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
|
39 |
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 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
|
41 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
|
42 } 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
|
43 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
|
44 } |
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 // 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
|
47 |
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 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 |
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 || -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
|
56 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
|
57 } 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
|
58 |
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 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
|
60 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
|
61 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
|
62 } |
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 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
|
65 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
|
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 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
|
68 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
|
69 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
|
70 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
|
71 } |
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 } |