2011-06-09 21:29:10 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <signal.h>
|
2011-06-09 21:56:13 -04:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-06-09 21:29:10 -04:00
|
|
|
#include <strings.h>
|
|
|
|
#include <unistd.h>
|
2011-06-20 23:56:16 -04:00
|
|
|
#include <sys/wait.h>
|
2011-06-09 21:29:10 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char *name;
|
|
|
|
int sig;
|
|
|
|
} sigs[] = {
|
|
|
|
#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
|
|
|
|
};
|
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-s signal] [pid...]\n"
|
|
|
|
" %s -l [signum]\n", argv0, argv0);
|
|
|
|
}
|
2012-05-14 16:28:41 -04:00
|
|
|
|
2011-06-09 21:29:10 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2011-06-09 21:56:13 -04:00
|
|
|
bool lflag = false;
|
2013-06-14 14:20:47 -04:00
|
|
|
char *end, *v;
|
2011-06-21 00:05:37 -04:00
|
|
|
int sig = SIGTERM;
|
2011-06-09 21:29:10 -04:00
|
|
|
pid_t pid;
|
2011-06-21 00:05:37 -04:00
|
|
|
size_t i;
|
2011-06-09 21:29:10 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'l':
|
|
|
|
lflag = true;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
v = EARGF(usage());
|
|
|
|
sig = strtol(v, &end, 0);
|
|
|
|
if(*end == '\0')
|
2011-06-09 21:56:13 -04:00
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
for(i = 0; i < LEN(sigs); i++) {
|
|
|
|
if(!strcasecmp(v, sigs[i].name)) {
|
|
|
|
sig = sigs[i].sig;
|
2011-06-09 22:12:45 -04:00
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
2011-06-09 21:29:10 -04:00
|
|
|
}
|
2013-06-14 14:20:47 -04:00
|
|
|
if(i == LEN(sigs))
|
|
|
|
eprintf("%s: unknown signal\n", v);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-03-07 13:35:43 -05:00
|
|
|
if(argc < 1)
|
2012-05-15 08:32:56 -04:00
|
|
|
usage();
|
2011-06-09 21:56:13 -04:00
|
|
|
|
2012-05-14 16:28:41 -04:00
|
|
|
if(lflag) {
|
2013-06-14 14:20:47 -04:00
|
|
|
sig = (argc > 0) ? 0 : estrtol(argv[0], 0);
|
2011-06-20 23:56:16 -04:00
|
|
|
if(sig > 128)
|
|
|
|
sig = WTERMSIG(sig);
|
2011-06-09 21:56:13 -04:00
|
|
|
for(i = 0; i < LEN(sigs); i++)
|
|
|
|
if(sigs[i].sig == sig || sig == 0)
|
|
|
|
putword(sigs[i].name);
|
|
|
|
putchar('\n');
|
2013-06-14 14:20:47 -04:00
|
|
|
} else for(; argc > 0; argc--, argv++) {
|
|
|
|
pid = estrtol(argv[0], 0);
|
2011-06-09 21:29:10 -04:00
|
|
|
if(kill(pid, sig) == -1)
|
|
|
|
eprintf("kill %d:", pid);
|
|
|
|
}
|
2012-05-14 16:28:41 -04:00
|
|
|
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2012-05-14 16:28:41 -04:00
|
|
|
}
|