sbase/kill.c

139 lines
2.7 KiB
C
Raw Normal View History

2011-06-09 21:29:10 -04:00
/* See LICENSE file for copyright and license details. */
#include <sys/wait.h>
#include <ctype.h>
#include <errno.h>
2011-06-09 21:29:10 -04:00
#include <signal.h>
2011-06-09 21:56:13 -04:00
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2011-06-09 21:29:10 -04:00
#include <strings.h>
#include <unistd.h>
#include "util.h"
struct {
const char *name;
int sig;
} sigs[] = {
{ "0", 0 },
2011-06-09 21:29:10 -04: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 11:27:17 -05:00
int name2sig(const char *);
2013-06-14 14:20:47 -04:00
static void
usage(void)
{
2014-11-11 10:50:21 -05:00
weprintf("usage: %s [-s signame | -signum | -signame] pid ...\n", argv0);
2014-11-11 11:08:13 -05:00
weprintf(" %s -l [exit_status]\n", argv0);
exit(1);
2013-06-14 14:20:47 -04:00
}
2012-05-14 16:28:41 -04:00
2011-06-09 21:29:10 -04:00
int
main(int argc, char *argv[])
{
char *end;
int ret = 0;
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
argv0 = argv[0];
if (argc < 2)
usage();
argc--, argv++;
2014-11-11 12:28:27 -05: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 11:08:13 -05:00
errno = 0;
2014-11-11 11:27:17 -05:00
sig = strtol(argv[0], &end, 10);
if (*end != '\0' || errno != 0)
eprintf("%s: bad signal number\n", argv[0]);
2014-11-11 11:32:28 -05:00
if (sig > 128)
sig = WTERMSIG(sig);
2014-11-11 11:27:17 -05:00
puts(sig2name(sig));
exit(0);
2014-11-11 12:28:27 -05:00
}
if (strcmp(argv[0], "-s") == 0) {
argc--, argv++;
if (argc == 0)
usage();
sig = name2sig(argv[0]);
2014-11-11 12:28:27 -05: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 14:20:47 -04:00
2014-11-11 12:28:27 -05:00
if (argc > 0 && strcmp(argv[0], "--") == 0)
argc--, argv++;
if (argc == 0)
2012-05-15 08:32:56 -04:00
usage();
2011-06-09 21:56:13 -04:00
for (; argc; argc--, argv++) {
errno = 0;
2014-11-11 11:27:17 -05: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-09 21:29:10 -04:00
}
2012-05-14 16:28:41 -04: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 11:27:17 -05:00
eprintf("%d: bad signal number\n", sig);
/* unreachable */
return NULL;
2012-05-14 16:28:41 -04:00
}
2014-11-11 11:27:17 -05: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;
}