Generalize the clone function slightly.
* Allow clone where both the child function and stack are null. It's
obviously wrong to ask to call a function without a stack, but it's not
necessarily wrong to supply no stack if you're also not supplying a
function.
* Reimplement fork in terms of the clone function, rather than using the
clone system call directly.
This is intended as a step towards enabling use of pid namespaces.
Change-Id: I03c89bd1dc540d8b4ed1c8fdf6644290744b9e91
diff --git a/libc/bionic/fork.cpp b/libc/bionic/fork.cpp
index 6cfc736..ffe94f4 100644
--- a/libc/bionic/fork.cpp
+++ b/libc/bionic/fork.cpp
@@ -31,8 +31,6 @@
#include "pthread_internal.h"
-#define FORK_FLAGS (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD)
-
int fork() {
__bionic_atfork_run_prepare();
@@ -41,11 +39,13 @@
// Remember the parent pid and invalidate the cached value while we fork.
pid_t parent_pid = self->invalidate_cached_pid();
-#if defined(__x86_64__) // sys_clone's last two arguments are flipped on x86-64.
- int result = syscall(__NR_clone, FORK_FLAGS, NULL, NULL, &(self->tid), NULL);
-#else
- int result = syscall(__NR_clone, FORK_FLAGS, NULL, NULL, NULL, &(self->tid));
-#endif
+ int result = clone(nullptr,
+ nullptr,
+ (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID | SIGCHLD),
+ nullptr,
+ nullptr,
+ nullptr,
+ &(self->tid));
if (result == 0) {
self->set_cached_pid(gettid());
__bionic_atfork_run_child();