From 9eb16aa2cdcdf14642ef43e5498a8f626a72d99c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 17 Oct 2017 18:24:03 +0200 Subject: Rewrite and relicense MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- orphan-reaper.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 orphan-reaper.c (limited to 'orphan-reaper.c') diff --git a/orphan-reaper.c b/orphan-reaper.c new file mode 100644 index 0000000..07304ba --- /dev/null +++ b/orphan-reaper.c @@ -0,0 +1,78 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include +#include + +#include "arg.h" + +#ifdef TEST +# define LIBEXECDIR "." +# define REAPD "reapd.test" +#else +# ifndef REAPD +# define REAPD "reapd" +# endif +#endif + +char *argv0; + +static +void usage(void) +{ + fprintf(stderr, "usage: %s [-f] command ...\n", argv0); + exit(1); +} + +int +main(int argc, char *argv[]) +{ + int fatal = 0; + + ARGBEGIN { + case 'f': + fatal = 1; + break; + default: + usage(); + } ARGEND; + + if (!argc) + usage(); + + if (access(LIBEXECDIR"/"REAPD, X_OK) < 0) { + fprintf(stderr, "%s: access %s: %s\n", argv0, LIBEXECDIR"/"REAPD, strerror(errno)); + return 2; + } + + if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0) { + fprintf(stderr, "%s: prctl PR_SET_CHILD_SUBREAPER: %s\n", argv0, strerror(errno)); + if (fatal) + return 2; + goto exec; + } + + switch (fork()) { + case -1: + fprintf(stderr, "%s: fork: %s\n", argv0, strerror(errno)); + if (fatal) + break; + /* fall through */ + + case 0: + (void) prctl(PR_SET_CHILD_SUBREAPER, 0); + exec: + execvp(*argv, argv); + fprintf(stderr, "%s: execvp %s: %s\n", argv0, *argv, strerror(errno)); + break; + + default: + execl(LIBEXECDIR"/"REAPD, LIBEXECDIR"/"REAPD, NULL); + fprintf(stderr, "%s: execl %s: %s\n", argv0, *argv, strerror(errno)); + break; + } + + return 2; +} -- cgit v1.2.3-70-g09d2