blob: 1e5e1d9f82169d54f6ed0a7c221f99efd6ecffc2 [file] [log] [blame]
Rob Landley7aa651a2012-11-13 17:14:08 -06001/* kill.c - a program to send signals to processes
Daniel Waltereb7204b2012-02-21 19:45:19 -06002 *
3 * Copyright 2012 Daniel Walter <d.walter@0x90.at>
4 *
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/kill.html
6
Rob Landleyb081ce92012-02-26 16:11:25 -06007USE_KILL(NEWTOY(kill, "?s: l", TOYFLAG_BIN))
Daniel Waltereb7204b2012-02-21 19:45:19 -06008
9config KILL
Rob Landley7aa651a2012-11-13 17:14:08 -060010 bool "kill"
11 default y
12 help
13 usage: kill [-l [SIGNAL] | -s SIGNAL | -SIGNAL] pid...
Daniel Waltereb7204b2012-02-21 19:45:19 -060014
Rob Landley7aa651a2012-11-13 17:14:08 -060015 Send a signal to a process
Daniel Waltereb7204b2012-02-21 19:45:19 -060016*/
17
Rob Landleyc0e56ed2012-10-08 00:02:30 -050018#define FOR_kill
Daniel Waltereb7204b2012-02-21 19:45:19 -060019#include "toys.h"
20
Rob Landleyc0e56ed2012-10-08 00:02:30 -050021GLOBALS(
Rob Landley7aa651a2012-11-13 17:14:08 -060022 char *signame;
Rob Landley2dd50ad2012-02-26 13:48:00 -060023)
Daniel Waltereb7204b2012-02-21 19:45:19 -060024
Daniel Waltereb7204b2012-02-21 19:45:19 -060025void kill_main(void)
26{
Rob Landley7aa651a2012-11-13 17:14:08 -060027 int signum;
28 char *tmp, **args = toys.optargs;
29 pid_t pid;
Rob Landley2dd50ad2012-02-26 13:48:00 -060030
Rob Landley7aa651a2012-11-13 17:14:08 -060031 // list signal(s)
32 if (toys.optflags & FLAG_l) {
33 if (*args) {
34 int signum = sig_to_num(*args);
35 char *s = NULL;
Rob Landley2dd50ad2012-02-26 13:48:00 -060036
Rob Landley7aa651a2012-11-13 17:14:08 -060037 if (signum>=0) s = num_to_sig(signum&127);
38 puts(s ? s : "UNKNOWN");
39 } else sig_to_num(NULL);
40 return;
41 }
Rob Landley2dd50ad2012-02-26 13:48:00 -060042
Rob Landley7aa651a2012-11-13 17:14:08 -060043 // signal must come before pids, so "kill -9 -1" isn't confusing.
Rob Landley2dd50ad2012-02-26 13:48:00 -060044
Rob Landley7aa651a2012-11-13 17:14:08 -060045 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 Landley2dd50ad2012-02-26 13:48:00 -060051
Rob Landley7aa651a2012-11-13 17:14:08 -060052 if (!arg || -1 == (signum = sig_to_num(arg)))
53 error_exit("Unknown signal '%s'", arg);
54 } else signum = SIGTERM;
Rob Landley2dd50ad2012-02-26 13:48:00 -060055
Rob Landley7aa651a2012-11-13 17:14:08 -060056 if (!*args) {
57 toys.exithelp++;
58 error_exit("missing argument");
59 }
Rob Landley2dd50ad2012-02-26 13:48:00 -060060
Rob Landley7aa651a2012-11-13 17:14:08 -060061 while (*args) {
62 char *arg = *(args++);
Rob Landley2dd50ad2012-02-26 13:48:00 -060063
Rob Landley7aa651a2012-11-13 17:14:08 -060064 pid = strtol(arg, &tmp, 10);
Rob Landley662a2672013-01-02 02:00:35 -060065 if (*tmp || kill(pid, signum) < 0) error_msg("unknown pid '%s'", arg);
Rob Landley7aa651a2012-11-13 17:14:08 -060066 }
Daniel Waltereb7204b2012-02-21 19:45:19 -060067}