sbase/kill.c

139 lines
2.6 KiB
C
Raw Normal View History

2011-06-10 01:29:10 +00:00
/* See LICENSE file for copyright and license details. */
#include <ctype.h>
#include <errno.h>
2011-06-10 01:29:10 +00:00
#include <signal.h>
2011-06-10 01:56:13 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2011-06-10 01:29:10 +00:00
#include <strings.h>
#include <sys/wait.h>
2011-06-10 01:29:10 +00:00
#include <unistd.h>
2011-06-10 01:29:10 +00:00
#include "util.h"
struct {
const char *name;
int sig;
} sigs[] = {
{ "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
};
const char *sig2name(int);
2014-11-11 16:27:17 +00:00
int name2sig(const char *);
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);
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[])
{
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
argv0 = argv[0];
if (argc < 2)
usage();
argc--, argv++;
2014-11-11 17:28:27 +00:00
if (strcmp(argv[0], "-l") == 0) {
argc--, argv++;
if (argc == 0) {
for (i = 0; i < LEN(sigs); i++)
puts(sigs[i].name);
exit(0);
} 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));
exit(0);
2014-11-11 17:28:27 +00:00
}
if (strcmp(argv[0], "-s") == 0) {
argc--, argv++;
if (argc == 0)
usage();
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 */
sig = name2sig(&argv[0][1]);
argc--, argv++;
}
}
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++;
if (argc == 0)
2012-05-15 12:32:56 +00:00
usage();
2011-06-10 01:56:13 +00:00
for (; argc; argc--, argv++) {
errno = 0;
2014-11-11 16:27:17 +00:00
pid = strtol(argv[0], &end, 10);
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
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 */
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;
}