aboutsummaryrefslogtreecommitdiffstats
path: root/libc/bionic/clone.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2014-06-23 17:49:45 -0700
committerElliott Hughes <enh@google.com>2014-06-23 17:49:45 -0700
commitfa9e16efaf0e885f6044e725eb759ef6de10f7ef (patch)
treeb2788038c17cde5a1d53c13a51d6a16868bd229f /libc/bionic/clone.cpp
parent0d7415fb225573c37ff9e880957cd2044dd658ce (diff)
downloadandroid_bionic-fa9e16efaf0e885f6044e725eb759ef6de10f7ef.tar.gz
android_bionic-fa9e16efaf0e885f6044e725eb759ef6de10f7ef.tar.bz2
android_bionic-fa9e16efaf0e885f6044e725eb759ef6de10f7ef.zip
Fix getpid caching across a clone.
If you make clone, fork, or vfork system calls directly, you're still on your own, but we now do the right thing for the clone wrapper. With this implementation, children lose the getpid caching, but we've no reason to think that that covers any significant use cases. Bug: 15387103 Change-Id: Icfab6b63c708fea830960742ec92aeba8ce7680d
Diffstat (limited to 'libc/bionic/clone.cpp')
-rw-r--r--libc/bionic/clone.cpp17
1 files changed, 16 insertions, 1 deletions
diff --git a/libc/bionic/clone.cpp b/libc/bionic/clone.cpp
index 001e245d7..0a0fdd526 100644
--- a/libc/bionic/clone.cpp
+++ b/libc/bionic/clone.cpp
@@ -31,6 +31,8 @@
#include <stdlib.h>
#include <stdarg.h>
+#include "pthread_internal.h"
+
extern "C" pid_t __bionic_clone(uint32_t flags, void* child_stack, int* parent_tid, void* tls, int* child_tid, int (*fn)(void*), void* arg);
extern "C" __noreturn void __exit(int status);
@@ -64,5 +66,18 @@ int clone(int (*fn)(void*), void* child_stack, int flags, void* arg, ...) {
child_stack_addr &= ~0xf;
child_stack = reinterpret_cast<void*>(child_stack_addr);
- return __bionic_clone(flags, child_stack, parent_tid, new_tls, child_tid, fn, arg);
+ // Remember the parent pid and invalidate the cached value while we clone.
+ pthread_internal_t* self = __get_thread();
+ pid_t parent_pid = self->invalidate_cached_pid();
+
+ // Actually do the clone.
+ int clone_result = __bionic_clone(flags, child_stack, parent_tid, new_tls, child_tid, fn, arg);
+
+ // We're the parent, so put our known pid back in place.
+ // We leave the child without a cached pid, but:
+ // 1. pthread_create gives its children their own pthread_internal_t with the correct pid.
+ // 2. fork makes a clone system call directly.
+ // If any other cases become important, we could use a double trampoline like __pthread_start.
+ self->set_cached_pid(parent_pid);
+ return clone_result;
}