diff options
author | Mattias Andrée <maandree@kth.se> | 2017-10-17 20:11:30 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2017-10-17 20:11:30 +0200 |
commit | a568a539bbbe387cef14185488a75e8d3825c68b (patch) | |
tree | 06ecb3bd0ec4e328ed6ad63a49de0f23c9b3f7fc /src/pdeath.c | |
parent | Makefile: fix uninstall rule: uninstall license (diff) | |
download | pdeath-a568a539bbbe387cef14185488a75e8d3825c68b.tar.gz pdeath-a568a539bbbe387cef14185488a75e8d3825c68b.tar.bz2 pdeath-a568a539bbbe387cef14185488a75e8d3825c68b.tar.xz |
Simplify
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/pdeath.c')
-rw-r--r-- | src/pdeath.c | 170 |
1 files changed, 0 insertions, 170 deletions
diff --git a/src/pdeath.c b/src/pdeath.c deleted file mode 100644 index 2d564a8..0000000 --- a/src/pdeath.c +++ /dev/null @@ -1,170 +0,0 @@ -/* See LICENSE file for copyright and license details. */ - -#include <sys/prctl.h> -#include <errno.h> -#include <limits.h> -#include <signal.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <strings.h> -#include <unistd.h> - -#define FAILURE 127 - -struct sig { - int signo; - const char *name; -} sigs[] = { - {SIGHUP, "HUP"}, - {SIGINT, "INT"}, - {SIGQUIT, "QUIT"}, - {SIGILL, "ILL"}, - {SIGTRAP, "TRAP"}, - {SIGABRT, "ABRT"}, - {SIGIOT, "IOT"}, - {SIGBUS, "BUS"}, - {SIGFPE, "FPE"}, - {SIGKILL, "KILL"}, - {SIGUSR1, "USR1"}, - {SIGSEGV, "SEGV"}, - {SIGUSR2, "USR2"}, - {SIGPIPE, "PIPE"}, - {SIGALRM, "ALRM"}, - {SIGTERM, "TERM"}, - {SIGSTKFLT, "TKFLT"}, - {SIGCLD, "CLD"}, - {SIGCHLD, "CHLD"}, - {SIGCONT, "CONT"}, - {SIGSTOP, "STOP"}, - {SIGTSTP, "TSTP"}, - {SIGTTIN, "TTIN"}, - {SIGTTOU, "TTOU"}, - {SIGURG, "URG"}, - {SIGXCPU, "XCPU"}, - {SIGXFSZ, "XFSZ"}, - {SIGVTALRM, "VTALRM"}, - {SIGPROF, "PROF"}, - {SIGWINCH, "WINCH"}, - {SIGPOLL, "POLL"}, - {SIGIO, "IO"}, - {SIGPWR, "PWR"}, - {SIGSYS, "SYS"}, - {SIGUNUSED, "UNUSED"}, - {0, NULL} -}; - -const char *argv0; - -static void -usage(void) -{ - fprintf(stderr, "usage: %s (signal)[(+|-)off] command [arguments]...\n", argv0); - fprintf(stderr, "usage: %s -L\n", argv0); - exit(FAILURE); -} - -static void -invalid_signal(void) -{ - fprintf(stderr, "%s: invalid signal\n", argv0); - exit(FAILURE); -} - -static void -print_signals(void) -{ - struct sig *sig = sigs; - for (; sig->name; sig++) - printf("%s\n", sig->name); - printf("RTMIN\nRTMAX\n"); -} - -static int -strict_strtoi(const char *str, int min, int max) -{ - char *end; - long int ret; - errno = 0; - ret = strtol(str, &end, 10); - if (errno || *end) - usage(); - if (ret < min || ret > max) - invalid_signal(); - return (int)ret; -} - -static int -get_signal(const char *str) -{ - struct sig *sig = sigs; - int have_prefix = 0; - char *end; - long int ret; - if (!strncasecmp(str, "SIG", 3)) { - str += 3; - have_prefix = 1; - } - if (!strcasecmp(str, "RTMIN")) - return SIGRTMIN; - if (!strcasecmp(str, "RTMAX")) - return SIGRTMAX; - for (; sig->name; sig++) { - if (!strcasecmp(str, sig->name)) - return sig->signo; - } - if (!have_prefix) { - errno = 0; - ret = strtol(str, &end, 10); - if (!errno && !*end && 0 <= ret && ret < _NSIG) - return (int)ret; - - } - invalid_signal(); - return -1; -} - -int -main(int argc, char *argv[]) -{ - char *off; - char sign = '\0'; - int signo; - - argv0 = *argv ? (argc--, *argv++) : "pdeath"; - - if (*argv && **argv == '-') { - if (argc == 1 && !strcmp(*argv, "-L")) { - print_signals(); - if (fflush(stdout) || fclose(stdout)) { - perror(argv0); - return FAILURE; - } - return 0; - } - usage(); - } - - if (argc < 2) - usage(); - - if ((off = strpbrk(*argv, "+-"))) { - sign = *off; - *off++ = '\0'; - } - signo = get_signal(*argv); - if (sign == '-') - signo -= strict_strtoi(off, 0, signo); - else if (sign == '+') - signo += strict_strtoi(off, 0, _NSIG - 1 - signo); - argv++, argc--; - - if (prctl(PR_SET_PDEATHSIG, signo) == -1) { - perror(argv0); - return FAILURE; - } - - execvp(*argv, argv); - fprintf(stderr, "%s: %s: %s\n", argv0, strerror(errno), *argv); - return FAILURE; -} |