Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 1 | /* kill.c - a program to send signals to processes |
Daniel Walter | eb7204b | 2012-02-21 19:45:19 -0600 | [diff] [blame] | 2 | * |
| 3 | * Copyright 2012 Daniel Walter <d.walter@0x90.at> |
| 4 | * |
| 5 | * See http://opengroup.org/onlinepubs/9699919799/utilities/kill.html |
| 6 | |
Rob Landley | b081ce9 | 2012-02-26 16:11:25 -0600 | [diff] [blame] | 7 | USE_KILL(NEWTOY(kill, "?s: l", TOYFLAG_BIN)) |
Daniel Walter | eb7204b | 2012-02-21 19:45:19 -0600 | [diff] [blame] | 8 | |
| 9 | config KILL |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 10 | bool "kill" |
| 11 | default y |
| 12 | help |
| 13 | usage: kill [-l [SIGNAL] | -s SIGNAL | -SIGNAL] pid... |
Daniel Walter | eb7204b | 2012-02-21 19:45:19 -0600 | [diff] [blame] | 14 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 15 | Send a signal to a process |
Daniel Walter | eb7204b | 2012-02-21 19:45:19 -0600 | [diff] [blame] | 16 | */ |
| 17 | |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 18 | #define FOR_kill |
Daniel Walter | eb7204b | 2012-02-21 19:45:19 -0600 | [diff] [blame] | 19 | #include "toys.h" |
| 20 | |
Rob Landley | c0e56ed | 2012-10-08 00:02:30 -0500 | [diff] [blame] | 21 | GLOBALS( |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 22 | char *signame; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 23 | ) |
Daniel Walter | eb7204b | 2012-02-21 19:45:19 -0600 | [diff] [blame] | 24 | |
Daniel Walter | eb7204b | 2012-02-21 19:45:19 -0600 | [diff] [blame] | 25 | void kill_main(void) |
| 26 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 27 | int signum; |
| 28 | char *tmp, **args = toys.optargs; |
| 29 | pid_t pid; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 30 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 31 | // list signal(s) |
| 32 | if (toys.optflags & FLAG_l) { |
| 33 | if (*args) { |
| 34 | int signum = sig_to_num(*args); |
| 35 | char *s = NULL; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 36 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 37 | if (signum>=0) s = num_to_sig(signum&127); |
| 38 | puts(s ? s : "UNKNOWN"); |
| 39 | } else sig_to_num(NULL); |
| 40 | return; |
| 41 | } |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 42 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 43 | // signal must come before pids, so "kill -9 -1" isn't confusing. |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 44 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 45 | if (!TT.signame && *args && **args=='-') TT.signame=*(args++)+1; |
| 46 | if (TT.signame) { |
| 47 | char *arg; |
| 48 | int i = strtol(TT.signame, &arg, 10); |
| 49 | if (!*arg) arg = num_to_sig(i); |
| 50 | else arg = TT.signame; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 51 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 52 | if (!arg || -1 == (signum = sig_to_num(arg))) |
| 53 | error_exit("Unknown signal '%s'", arg); |
| 54 | } else signum = SIGTERM; |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 55 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 56 | if (!*args) { |
| 57 | toys.exithelp++; |
| 58 | error_exit("missing argument"); |
| 59 | } |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 60 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 61 | while (*args) { |
| 62 | char *arg = *(args++); |
Rob Landley | 2dd50ad | 2012-02-26 13:48:00 -0600 | [diff] [blame] | 63 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 64 | pid = strtol(arg, &tmp, 10); |
Rob Landley | 662a267 | 2013-01-02 02:00:35 -0600 | [diff] [blame] | 65 | if (*tmp || kill(pid, signum) < 0) error_msg("unknown pid '%s'", arg); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 66 | } |
Daniel Walter | eb7204b | 2012-02-21 19:45:19 -0600 | [diff] [blame] | 67 | } |