From 16e25b2890927108ec15297aabb1d86a49792741 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Fri, 3 Oct 2014 09:42:27 +0200 Subject: p11-kit: Use pthread_atfork() in a safe manner Instead of trying to perform actions in pthread_atfork() which are not async-signal-safe, just increment a counter so we can later tell if the process has forked. Note this does not make it safe to mix threads and forking without immediately execing. This is a far broader problem that p11-kit, however we now do the right thing when fork+exec is used from a thread. https://bugs.freedesktop.org/show_bug.cgi?id=84567 --- common/library.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'common/library.c') diff --git a/common/library.c b/common/library.c index b7d6923..502ea98 100644 --- a/common/library.c +++ b/common/library.c @@ -63,6 +63,8 @@ p11_mutex_t p11_library_mutex; pthread_once_t p11_library_once = PTHREAD_ONCE_INIT; #endif +unsigned int p11_forkid = 1; + static char * thread_local_message (void) { @@ -103,6 +105,13 @@ _p11_library_get_thread_local (void) return local; } +static void +count_forks (void) +{ + /* Thread safe, executed in child, one thread exists */ + p11_forkid++; +} + void p11_library_init_impl (void) { @@ -111,6 +120,8 @@ p11_library_init_impl (void) p11_mutex_init (&p11_library_mutex); pthread_key_create (&thread_local, free); p11_message_storage = thread_local_message; + + pthread_atfork (NULL, NULL, count_forks); } void -- cgit v1.1