2011-06-10 01:29:10 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 20:02:41 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2014-11-11 15:35:27 +00:00
|
|
|
#include <ctype.h>
|
2015-03-16 11:37:46 +00:00
|
|
|
#include <limits.h>
|
2015-02-14 20:15:01 +00:00
|
|
|
#include <signal.h>
|
2011-06-10 01:56:13 +00:00
|
|
|
#include <stdio.h>
|
2014-11-11 14:40:47 +00:00
|
|
|
#include <string.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-06-10 01:29:10 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char *name;
|
2015-03-16 11:37:46 +00:00
|
|
|
const int sig;
|
2011-06-10 01:29:10 +00:00
|
|
|
} sigs[] = {
|
2014-11-11 17:42:43 +00:00
|
|
|
{ "0", 0 },
|
2011-06-10 01:29:10 +00:00
|
|
|
#define SIG(n) { #n, SIG##n }
|
|
|
|
SIG(ABRT), SIG(ALRM), SIG(BUS), SIG(CHLD), SIG(CONT), SIG(FPE), SIG(HUP),
|
|
|
|
SIG(ILL), SIG(INT), SIG(KILL), SIG(PIPE), SIG(QUIT), SIG(SEGV), SIG(STOP),
|
|
|
|
SIG(TERM), SIG(TSTP), SIG(TTIN), SIG(TTOU), SIG(USR1), SIG(USR2), SIG(URG),
|
|
|
|
#undef SIG
|
|
|
|
};
|
|
|
|
|
2015-03-07 14:39:39 +00:00
|
|
|
const char *
|
2015-03-16 11:37:46 +00:00
|
|
|
sig2name(const int sig)
|
2015-03-07 14:39:39 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < LEN(sigs); i++)
|
|
|
|
if (sigs[i].sig == sig)
|
|
|
|
return sigs[i].name;
|
|
|
|
eprintf("%d: bad signal number\n", sig);
|
2015-03-16 11:37:46 +00:00
|
|
|
|
|
|
|
return NULL; /* not reached */
|
2015-03-07 14:39:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 11:37:46 +00:00
|
|
|
const int
|
2015-03-07 14:39:39 +00:00
|
|
|
name2sig(const char *name)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < LEN(sigs); i++)
|
2015-03-16 11:37:46 +00:00
|
|
|
if (!strcasecmp(sigs[i].name, name))
|
2015-03-07 14:39:39 +00:00
|
|
|
return sigs[i].sig;
|
|
|
|
eprintf("%s: bad signal name\n", name);
|
2015-03-16 11:37:46 +00:00
|
|
|
|
|
|
|
return -1; /* not reached */
|
2015-03-07 14:39:39 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-16 11:37:46 +00:00
|
|
|
eprintf("usage: %s [-s signame | -num | -signame] pid ...\n"
|
|
|
|
" %s -l [num]\n", argv0, argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
2012-05-14 20:28:41 +00:00
|
|
|
|
2011-06-10 01:29:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
pid_t pid;
|
2011-06-21 04:05:37 +00:00
|
|
|
size_t i;
|
2015-03-16 11:37:46 +00:00
|
|
|
int ret = 0, sig = SIGTERM;
|
2011-06-10 01:29:10 +00:00
|
|
|
|
2015-03-16 11:37:46 +00:00
|
|
|
argv0 = argv[0], argc--, argv++;
|
|
|
|
if (!argc)
|
2014-11-11 14:40:47 +00:00
|
|
|
usage();
|
|
|
|
|
2015-03-16 11:37:46 +00:00
|
|
|
if ((*argv)[0] == '-') {
|
|
|
|
switch ((*argv)[1]) {
|
|
|
|
case 'l':
|
|
|
|
if ((*argv)[2])
|
|
|
|
goto longopt;
|
|
|
|
argc--, argv++;
|
|
|
|
if (!argc) {
|
|
|
|
for (i = 0; i < LEN(sigs); i++)
|
|
|
|
puts(sigs[i].name);
|
|
|
|
return 0;
|
|
|
|
} else if (argc == 1) {
|
|
|
|
sig = estrtonum(*argv, 0, INT_MAX);
|
|
|
|
if (sig > 128)
|
|
|
|
sig = WTERMSIG(sig);
|
|
|
|
puts(sig2name(sig));
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if ((*argv)[2])
|
|
|
|
goto longopt;
|
|
|
|
argc--, argv++;
|
|
|
|
if (!argc)
|
|
|
|
usage();
|
|
|
|
sig = name2sig(*argv);
|
2014-11-11 17:28:27 +00:00
|
|
|
argc--, argv++;
|
2015-03-16 11:37:46 +00:00
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
if ((*argv)[2])
|
|
|
|
goto longopt;
|
|
|
|
argc--, argv++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
longopt:
|
|
|
|
/* XSI-extensions -argnum and -argname*/
|
|
|
|
if (isdigit((*argv)[1])) {
|
|
|
|
sig = estrtonum((*argv) + 1, 0, INT_MAX);
|
|
|
|
sig2name(sig);
|
|
|
|
} else {
|
|
|
|
sig = name2sig((*argv) + 1);
|
|
|
|
}
|
2014-11-11 17:11:58 +00:00
|
|
|
argc--, argv++;
|
2014-11-11 15:35:27 +00:00
|
|
|
}
|
2014-11-11 14:40:47 +00:00
|
|
|
}
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2015-03-16 11:37:46 +00:00
|
|
|
if (argc && !strcmp(*argv, "--"))
|
2014-11-11 17:28:27 +00:00
|
|
|
argc--, argv++;
|
|
|
|
|
2015-03-16 11:37:46 +00:00
|
|
|
if (!argc)
|
2012-05-15 12:32:56 +00:00
|
|
|
usage();
|
2011-06-10 01:56:13 +00:00
|
|
|
|
2015-03-16 11:37:46 +00:00
|
|
|
for (; *argv; argc--, argv++) {
|
|
|
|
pid = estrtonum(*argv, INT_MIN, INT_MAX);
|
|
|
|
if (kill(pid, sig) < 0) {
|
|
|
|
weprintf("kill %d:", pid);
|
2014-11-11 14:40:47 +00:00
|
|
|
ret = 1;
|
|
|
|
}
|
2011-06-10 01:29:10 +00:00
|
|
|
}
|
2012-05-14 20:28:41 +00:00
|
|
|
|
2015-03-16 11:37:46 +00:00
|
|
|
return ret;
|
2014-11-11 14:40:47 +00:00
|
|
|
}
|