From a568a539bbbe387cef14185488a75e8d3825c68b Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 17 Oct 2017 20:11:30 +0200 Subject: Simplify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 6 +-- Makefile | 50 ++++++++---------- README | 5 +- config.mk | 6 +++ doc/pdeath.1 | 34 ------------ pdeath.1 | 27 ++++++++++ pdeath.c | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pdeath.c | 170 ----------------------------------------------------------- 8 files changed, 224 insertions(+), 240 deletions(-) create mode 100644 config.mk delete mode 100644 doc/pdeath.1 create mode 100644 pdeath.1 create mode 100644 pdeath.c delete mode 100644 src/pdeath.c diff --git a/.gitignore b/.gitignore index 22bfb15..5358cd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,6 @@ -\#*\# -.\#* *~ -/obj/ -/bin/ +*\#* *.out *.o *.su +/pdeath diff --git a/Makefile b/Makefile index 8db7678..0f5e821 100644 --- a/Makefile +++ b/Makefile @@ -1,38 +1,32 @@ -PREFIX = /usr -BINDIR = $(PREFIX)/bin -DATADIR = $(PREFIX)/share -LICENSEDIR = $(DATADIR)/licences -MANDIR = $(DATADIR)/man -MAN1DIR = $(MANDIR)/man1 +.POSIX: -PKGNAME = pdeath -COMMAND = pdeath +CONFIGFILE = config.mk +include $(CONFIGFILE) -OPTIMISE = -O2 -WARN = -Wall -Wextra -pedantic +all: pdeath +.o: + $(CC) -o $@ $^ $(LDFLAGS) -all: cmd - -cmd: bin/pdeath - -bin/pdeath: src/pdeath.c - @mkdir -p bin - $(CC) -std=c99 $(OPTIMISE) $(WARN) -o $@ $^ +.c.o: + $(CC) -o $@ $< $(CPPFLAGS) $(CFLAGS) install: bin/pdeath - mkdir -p -- "$(DESTDIR)$(BINDIR)" - cp -- bin/pdeath "$(DESTDIR)$(BINDIR)/$(COMMAND)" - mkdir -p -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)" - cp -- LICENSE "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)/LICENSE" - mkdir -p -- "$(DESTDIR)$(MAN1DIR)" - cp doc/pdeath.1 -- "$(DESTDIR)$(MAN1DIR)/$(COMMAND).1" + mkdir -p -- "$(DESTDIR)$(PREFIX)/bin" + mkdir -p -- "$(DESTDIR)$(PREFIX)/share/licenses/pdeath" + mkdir -p -- "$(DESTDIR)$(MANPREFIX)/man1" + cp -- pdeath "$(DESTDIR)$(PREFIX)/bin/" + cp -- LICENSE "$(DESTDIR)$(PREFIX)/share/licenses/pdeath/" + cp -- pdeath.1 "$(DESTDIR)$(MANPREFIX)/man1/" uninstall: - -rm -- "$(DESTDIR)$(BINDIR)/$(COMMAND)" - -rm -- "$(DESTDIR)$(MAN1DIR)/$(COMMAND).1" - -rm -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)/LICENSE" - -rmdir -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)" + -rm -f -- "$(DESTDIR)$(PREFIX)/bin/pdeath" + -rm -f -- "$(DESTDIR)$(MANPREFIX)/man1/pdeath.1" + -rm -rf -- "$(DESTDIR)$(PREFIX)/share/licenses/pdeath" clean: - -rm -rf bin + -rm -f -- *.o pdeath + +SUFFIXES: .o .c.o + +.PHONY: all check install uninstall clean diff --git a/README b/README index f09127a..5c184d2 100644 --- a/README +++ b/README @@ -2,9 +2,7 @@ NAME pdeath - Run a command that is killed when its parent exits SYNOPSIS - pdeath signal[(+|-)off] command [argument]... - - pdeath -L + pdeath -L (signal[(+|-)off] command [argument] ...) DESCRIPTION pdeath runs the select command with the parent death signal set to @@ -16,4 +14,3 @@ OPTIONS LIMITATIONS pdeath cannot be used on binaries with set-user-ID or set-group-ID set. - diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..9e148ca --- /dev/null +++ b/config.mk @@ -0,0 +1,6 @@ +PREFIX = /usr/local +MANPREFIX = $(PREFIX)/share/man + +CFLAGS = -std=c99 -Wall -Wextra -O2 +CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 +LDFLAGS = -s diff --git a/doc/pdeath.1 b/doc/pdeath.1 deleted file mode 100644 index b852635..0000000 --- a/doc/pdeath.1 +++ /dev/null @@ -1,34 +0,0 @@ -.TH PDEATH 1 PDEATH -.SH NAME -pdeath \- Run a command that is killed when its parent exits -.SH SYNOPSIS -.B pdeath -\fIsignal\fP[(\fB+\fP|\fB-\fP)\fIoff\fP] -.I command -.RI [ argument ]... -.P -.B pdeath -.B -L -.SH DESCRIPTION -.B pdeath -runs the select command with the parent death signal set to -.I signal -plus or minus -.IR off . -This means that the process receives this signal when its -parent process dies. -.SH OPTIONS -.TP -.B \-L -List all signals. -.SH LIMITATIONS -.B pdeath -cannot be used on binaries with set-user-ID or set-group-ID set. -.SH AUTHORS -Principal author, Mattias Andrée. See the LICENSE file for the full -list of authors. -.SH LICENSE -ISC License. -.SH BUGS -Please report bugs to https://github.com/maandree/pdeath/issues or to -maandree@kth.se diff --git a/pdeath.1 b/pdeath.1 new file mode 100644 index 0000000..d61bfdc --- /dev/null +++ b/pdeath.1 @@ -0,0 +1,27 @@ +.TH PDEATH 1 PDEATH +.SH NAME +pdeath \- Run a command that is killed when its parent exits +.SH SYNOPSIS +.B pdeath +(-L | +.RB \fIsignal\fP[( + | - )\fIoff\fP] +.I command +.RI [ argument ]\ ...) +.SH DESCRIPTION +.B pdeath +runs the select command with the parent death signal set to +.I signal +plus or minus +.IR off . +This means that the process receives this signal when its +parent process dies. +.SH OPTIONS +.TP +.B \-L +List all signals. +.SH LIMITATIONS +.B pdeath +cannot be used on binaries with set-user-ID or set-group-ID set. +.SH AUTHORS +Mattias Andrée +.RI < maandree@kth.se > diff --git a/pdeath.c b/pdeath.c new file mode 100644 index 0000000..2cdbee2 --- /dev/null +++ b/pdeath.c @@ -0,0 +1,166 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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 (-L | (signal)[(+|-)off] command [argument] ...)\n", argv0); + exit(127); +} + +static void +invalid_signal(void) +{ + fprintf(stderr, "%s: invalid signal\n", argv0); + exit(127); +} + +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 127; + } + 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 127; + } + + execvp(*argv, argv); + fprintf(stderr, "%s: %s: %s\n", argv0, strerror(errno), *argv); + return 127; +} 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 -#include -#include -#include -#include -#include -#include -#include -#include - -#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; -} -- cgit v1.2.3-70-g09d2