Merge branch 'wip-libc'
authorRalph Ronnquist <ralph.ronnquist@gmail.com>
Wed, 30 Mar 2022 11:12:59 +0000 (22:12 +1100)
committerRalph Ronnquist <ralph.ronnquist@gmail.com>
Wed, 30 Mar 2022 11:12:59 +0000 (22:12 +1100)
src/Makefile [new file with mode: 0644]
src/reaper.c [new file with mode: 0644]
src/reaper_nsl.c [new file with mode: 0644]

diff --git a/src/Makefile b/src/Makefile
new file mode 100644 (file)
index 0000000..7567e7b
--- /dev/null
@@ -0,0 +1,13 @@
+all: reaper
+
+# Pick compiler by preference
+CCPREFS = /usr/bin/musl-gcc /usr/bin/gcc
+CC = $(word 1,$(shell ls $(CCPREFS) 2>/dev/null))
+
+CFLAGS = -Wall -static -O3 -flto
+
+reaper: reaper.c
+       $(CC) $(CFLAGS) -o $@ $^
+
+clean:
+       rm -f reaper
diff --git a/src/reaper.c b/src/reaper.c
new file mode 100644 (file)
index 0000000..9da2124
--- /dev/null
@@ -0,0 +1,21 @@
+/**
+ * This program waits for child process and "reaps" them, i.e. read
+ * off their status so that they can terminate. The program exits when
+ * it runs out of children.
+ */
+#include <signal.h>
+#include <string.h>
+#include <sys/wait.h>
+
+int main(void) {
+    sigset_t set;
+    siginfo_t status;
+
+    sigfillset(&set);
+    sigprocmask(SIG_BLOCK,&set,NULL);
+
+    do {
+       memset( &status, 0, sizeof( status ) );
+    } while ( waitid( P_ALL, 0, &status, WEXITED ) == 0 );
+    return 0;
+}
diff --git a/src/reaper_nsl.c b/src/reaper_nsl.c
new file mode 100644 (file)
index 0000000..b788562
--- /dev/null
@@ -0,0 +1,36 @@
+//#include <errno.h>
+//#include <signal.h>
+//#include <string.h>
+//#include <unistd.h>
+//#include <sys/wait.h>
+//#include <sys/types.h>
+
+//int main(void) {
+//     sigset_t set;
+//     siginfo_t status;
+//
+//     if (getpid()!=1)
+//             return 1;
+//     sigfillset(&set);
+//     sigprocmask(SIG_BLOCK,&set,NULL);
+//     memset(&status,0,sizeof status);
+//     while (-ECHILD!=waitid(P_ALL,0,&status,WEXITED));
+//     return 1;
+//}
+
+#define _GNU_SOURCE
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/syscall.h>
+#include <linux/unistd.h>
+
+static inline void nsl_exit1(void) {
+       asm("mov $60,%eax");
+       asm("mov $1,%rdi");
+       asm("syscall");
+}
+
+__attribute__((naked)) void _start(void) {
+       nsl_exit1();
+}