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>
|
2014-11-11 14:40:47 +00:00
|
|
|
#include <errno.h>
|
2015-02-14 20:15:01 +00:00
|
|
|
#include <signal.h>
|
2011-06-10 01:56:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-11-11 14:40:47 +00:00
|
|
|
#include <string.h>
|
2015-02-20 16:00:39 +00:00
|
|
|
#include <strings.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-06-10 01:29:10 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
struct {
|
|
|
|
const char *name;
|
|
|
|
int sig;
|
|
|
|
} 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
|
|
|
|
};
|
|
|
|
|
2014-11-11 14:40:47 +00:00
|
|
|
const char *sig2name(int);
|
2014-11-11 16:27:17 +00:00
|
|
|
int name2sig(const char *);
|
2014-11-11 14:40:47 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-11-11 15:50:21 +00:00
|
|
|
weprintf("usage: %s [-s signame | -signum | -signame] pid ...\n", argv0);
|
2014-11-11 16:08:13 +00:00
|
|
|
weprintf(" %s -l [exit_status]\n", argv0);
|
2014-11-11 14:40:47 +00:00
|
|
|
exit(1);
|
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[])
|
|
|
|
{
|
2014-11-11 14:40:47 +00:00
|
|
|
char *end;
|
|
|
|
int ret = 0;
|
2011-06-21 04:05:37 +00:00
|
|
|
int sig = SIGTERM;
|
2011-06-10 01:29:10 +00:00
|
|
|
pid_t pid;
|
2011-06-21 04:05:37 +00:00
|
|
|
size_t i;
|
2011-06-10 01:29:10 +00:00
|
|
|
|
2014-11-11 14:40:47 +00:00
|
|
|
argv0 = argv[0];
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
|
2014-11-11 17:11:58 +00:00
|
|
|
argc--, argv++;
|
2014-11-11 17:28:27 +00:00
|
|
|
if (strcmp(argv[0], "-l") == 0) {
|
2014-11-11 17:11:58 +00:00
|
|
|
argc--, argv++;
|
2014-11-11 14:40:47 +00:00
|
|
|
if (argc == 0) {
|
|
|
|
for (i = 0; i < LEN(sigs); i++)
|
|
|
|
puts(sigs[i].name);
|
|
|
|
exit(0);
|
2014-11-11 16:09:07 +00:00
|
|
|
} else if (argc > 1)
|
|
|
|
usage();
|
2014-11-11 16:08:13 +00:00
|
|
|
errno = 0;
|
2014-11-11 16:27:17 +00:00
|
|
|
sig = strtol(argv[0], &end, 10);
|
|
|
|
if (*end != '\0' || errno != 0)
|
|
|
|
eprintf("%s: bad signal number\n", argv[0]);
|
2014-11-11 16:32:28 +00:00
|
|
|
if (sig > 128)
|
|
|
|
sig = WTERMSIG(sig);
|
2014-11-11 16:27:17 +00:00
|
|
|
puts(sig2name(sig));
|
2014-11-11 14:40:47 +00:00
|
|
|
exit(0);
|
2014-11-11 17:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[0], "-s") == 0) {
|
|
|
|
argc--, argv++;
|
|
|
|
if (argc == 0)
|
|
|
|
usage();
|
2014-11-11 17:40:09 +00:00
|
|
|
sig = name2sig(argv[0]);
|
2014-11-11 17:28:27 +00:00
|
|
|
argc--, argv++;
|
|
|
|
} else if (argv[0][0] == '-') {
|
|
|
|
if (isdigit(argv[0][1])) {
|
|
|
|
/* handle XSI extension -signum */
|
|
|
|
errno = 0;
|
|
|
|
sig = strtol(&argv[0][1], &end, 10);
|
|
|
|
if (*end != '\0' || errno != 0)
|
|
|
|
eprintf("%s: bad signal number\n", &argv[0][1]);
|
|
|
|
sig2name(sig);
|
|
|
|
argc--, argv++;
|
|
|
|
} else if (argv[0][1] != '-') {
|
|
|
|
/* handle XSI extension -signame */
|
2014-11-11 17:40:09 +00:00
|
|
|
sig = name2sig(&argv[0][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
|
|
|
|
2014-11-11 17:28:27 +00:00
|
|
|
if (argc > 0 && strcmp(argv[0], "--") == 0)
|
|
|
|
argc--, argv++;
|
|
|
|
|
2014-11-11 14:40:47 +00:00
|
|
|
if (argc == 0)
|
2012-05-15 12:32:56 +00:00
|
|
|
usage();
|
2011-06-10 01:56:13 +00:00
|
|
|
|
2014-11-11 14:40:47 +00:00
|
|
|
for (; argc; argc--, argv++) {
|
|
|
|
errno = 0;
|
2014-11-11 16:27:17 +00:00
|
|
|
pid = strtol(argv[0], &end, 10);
|
2014-11-11 14:40:47 +00:00
|
|
|
if (*end == '\0' && errno == 0) {
|
|
|
|
if (kill(pid, sig) < 0) {
|
|
|
|
weprintf("kill %d:", pid);
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
weprintf("%s: bad pid\n", argv[0]);
|
|
|
|
ret = 1;
|
|
|
|
}
|
2011-06-10 01:29:10 +00:00
|
|
|
}
|
2012-05-14 20:28:41 +00:00
|
|
|
|
2014-11-11 14:40:47 +00:00
|
|
|
exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
sig2name(int sig)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < LEN(sigs); i++)
|
|
|
|
if (sigs[i].sig == sig)
|
|
|
|
return sigs[i].name;
|
2014-11-11 16:27:17 +00:00
|
|
|
eprintf("%d: bad signal number\n", sig);
|
|
|
|
/* unreachable */
|
2014-11-11 14:40:47 +00:00
|
|
|
return NULL;
|
2012-05-14 20:28:41 +00:00
|
|
|
}
|
2014-11-11 16:27:17 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
name2sig(const char *name)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < LEN(sigs); i++)
|
|
|
|
if (strcasecmp(sigs[i].name, name) == 0)
|
|
|
|
return sigs[i].sig;
|
|
|
|
eprintf("%s: bad signal name\n", name);
|
|
|
|
/* unreachable */
|
|
|
|
return -1;
|
|
|
|
}
|