From 2111575e3c20fdf6f737d24e40721cb5a24ab9ae Mon Sep 17 00:00:00 2001 From: Trumeet Date: Wed, 16 Feb 2022 14:43:35 -0800 Subject: Add ReapperRun --- ReapperRun/main.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 ReapperRun/main.c (limited to 'ReapperRun/main.c') diff --git a/ReapperRun/main.c b/ReapperRun/main.c new file mode 100644 index 0000000..ec209d8 --- /dev/null +++ b/ReapperRun/main.c @@ -0,0 +1,52 @@ +/* + * Starts any programs with PR_SET_CHILD_SUBREAPER on Linux. + * Author: Yuuta Liang + */ + +#define _POSIX_C_SOURCE 200809L +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef TEST +int main(int argc, char **argv) { + int r; + assert(argc); + if (argc == 1) + errx(64, "Usage: %s /path/to/binary [arg1] [arg2] ...", + argv[0]); + /* path is not freed on intend. */ + char *path = realpath(argv[1], NULL); + if (!path) + err(errno, "realpath(%s)", argv[1]); + if ((r = prctl(PR_SET_CHILD_SUBREAPER, 1) < 0)) + err(errno, "prctl(PR_SET_CHILD_SUBREAPER)"); + if ((r = execve(path, &argv[1], environ))) + err(errno, "exec(%s)", path); +} +#else /* ifndef TEST */ +#include +int main(void) { + printf("Parent PID: %d\n", getpid()); + if (!fork()) { + printf("Fork PID: %d\n", getpid()); + unsigned int counter = 0; + goto f; +f: + counter ++; + if (!fork()) { + printf("Child PID: %d\n", getpid()); + pause(); + } + if (counter < 20) goto f; + exit(0); + } + pause(); +} +#endif /* ifndef TEST */ -- cgit v1.2.3