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>
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#define LEN(x) (sizeof (x) / sizeof *(x))
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2011-06-09 21:56:13 -04:00
|
|
|
bool lflag = false;
|
2011-06-09 21:29:10 -04:00
|
|
|
char c, *end;
|
|
|
|
int i, sig = SIGTERM;
|
|
|
|
pid_t pid;
|
|
|
|
|
2011-06-09 21:56:13 -04:00
|
|
|
while((c = getopt(argc, argv, "ls:")) != -1)
|
2011-06-09 21:29:10 -04:00
|
|
|
switch(c) {
|
2011-06-09 21:56:13 -04:00
|
|
|
case 'l':
|
|
|
|
lflag = true;
|
|
|
|
break;
|
2011-06-09 21:29:10 -04:00
|
|
|
case 's':
|
|
|
|
for(i = 0; i < LEN(sigs); i++)
|
|
|
|
if(!strcasecmp(optarg, sigs[i].name)) {
|
|
|
|
sig = sigs[i].sig;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(i == LEN(sigs))
|
|
|
|
eprintf("%s: unknown signal\n", optarg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2011-06-09 21:56:13 -04:00
|
|
|
if(lflag) {
|
|
|
|
if(optind == argc-1) {
|
|
|
|
sig = strtol(argv[optind], &end, 0);
|
|
|
|
if(*end != '\0')
|
|
|
|
eprintf("%s: not a number\n", argv[optind]);
|
|
|
|
}
|
|
|
|
else if(optind == argc)
|
|
|
|
sig = 0;
|
|
|
|
else
|
|
|
|
eprintf("usage: %s [-s signal] [pid...]\n"
|
|
|
|
" %s -l [signum]\n", argv[0], argv[0]);
|
|
|
|
|
|
|
|
for(i = 0; i < LEN(sigs); i++)
|
|
|
|
if(sigs[i].sig == sig || sig == 0)
|
|
|
|
putword(sigs[i].name);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
else for(; optind < argc; optind++) {
|
2011-06-09 21:29:10 -04:00
|
|
|
pid = strtol(argv[optind], &end, 0);
|
|
|
|
if(*end != '\0')
|
|
|
|
eprintf("%s: not a number\n", argv[optind]);
|
|
|
|
if(kill(pid, sig) == -1)
|
|
|
|
eprintf("kill %d:", pid);
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|