aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/pthread_atfork.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-03-07 17:28:00 -0500
committerIan Moffett <ian@osmora.org>2024-03-07 17:28:32 -0500
commitbd5969fc876a10b18613302db7087ef3c40f18e1 (patch)
tree7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/tests/posix/pthread_atfork.c
parenta95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff)
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/posix/pthread_atfork.c')
-rw-r--r--lib/mlibc/tests/posix/pthread_atfork.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/mlibc/tests/posix/pthread_atfork.c b/lib/mlibc/tests/posix/pthread_atfork.c
new file mode 100644
index 0000000..45be136
--- /dev/null
+++ b/lib/mlibc/tests/posix/pthread_atfork.c
@@ -0,0 +1,54 @@
+#include <assert.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/wait.h>
+
+_Atomic int prepare_order = 0;
+_Atomic int parent_order = 0;
+_Atomic int child_order = 0;
+
+static void prepare1() { prepare_order = 1; }
+static void prepare2() { prepare_order = 2; }
+
+static void parent1() { parent_order = 1; }
+static void parent2() { parent_order = 2; }
+
+static void child1() { child_order = 1; }
+static void child2() { child_order = 2; }
+
+int main() {
+ assert(!pthread_atfork(prepare1, parent1, child1));
+ assert(!pthread_atfork(prepare2, parent2, child2));
+
+ pid_t pid = fork();
+ assert(pid >= 0);
+
+ if (!pid) {
+ assert(child_order == 2);
+ exit(0);
+ } else {
+ assert(prepare_order == 1);
+ assert(parent_order == 2);
+
+ while (1) {
+ int status = 0;
+
+ int ret = waitpid(pid, &status, 0);
+
+ if (ret == -1 && errno == EINTR)
+ continue;
+
+ assert(ret > 0);
+
+ if (WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT)
+ return 1;
+
+ return WEXITSTATUS(status);
+ }
+ }
+
+ return 0;
+}