aboutsummaryrefslogtreecommitdiff
path: root/ReapperRun/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'ReapperRun/main.c')
-rw-r--r--ReapperRun/main.c52
1 files changed, 52 insertions, 0 deletions
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 <sys/prctl.h>
+#include <err.h>
+#include <errno.h>
+#include <assert.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <limits.h>
+
+#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 <stdio.h>
+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 */