From 92ade89db676955ed66ea968b7dc9bebbce1f03c Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Tue, 9 Mar 2010 21:59:24 +0000 Subject: [PATCH] tsafe: change fork() override to pthread_atfork() callbacks. git-svn-id: http://svn.fedorahosted.org/svn/corosync/trunk@2677 fd59a12c-fef9-0310-b244-a6a79926bd2f --- exec/tsafe.c | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/exec/tsafe.c b/exec/tsafe.c index aecfb155..e2b4f210 100644 --- a/exec/tsafe.c +++ b/exec/tsafe.c @@ -80,6 +80,9 @@ static int tsafe_disabled = 1; static int tsafe_inited = 0; static char **coro_environ; +static void atfork_prepare (void); +static void atfork_parent (void); +static void atfork_child (void); #if defined(HAVE_PTHREAD_SPIN_LOCK) static pthread_spinlock_t tsafe_enabled_mutex; @@ -105,11 +108,13 @@ void tsafe_init (char **envp) #if defined(HAVE_PTHREAD_SPIN_LOCK) pthread_spin_init (&tsafe_enabled_mutex, 0); #endif + + pthread_atfork (atfork_prepare, atfork_parent,atfork_child); + tsafe_disabled = 1; tsafe_inited = 1; } - static void tsafe_lock (void) { #if defined(HAVE_PTHREAD_SPIN_LOCK) @@ -118,6 +123,7 @@ static void tsafe_lock (void) pthread_mutex_lock (&tsafe_enabled_mutex); #endif } + static void tsafe_unlock (void) { #if defined(HAVE_PTHREAD_SPIN_LOCK) @@ -141,6 +147,23 @@ void tsafe_on (void) tsafe_unlock (); } +static void atfork_prepare (void) +{ + tsafe_lock (); +} + +static void atfork_parent (void) +{ + tsafe_unlock (); +} + +static void atfork_child (void) +{ + if (tsafe_inited && !tsafe_disabled) { + tsafe_disabled = 1; + } + tsafe_unlock (); +} static void* _get_real_func_(const char * func_name) { @@ -182,25 +205,6 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr, return real_pthread_create (thread, attr, start_routine, arg); } -pid_t fork(void) -{ - static pid_t (*real_fork)(void) = NULL; - pid_t ret; - - if (tsafe_inited && !tsafe_disabled) { - tsafe_off (); - } - - if (real_fork == NULL) { - real_fork = _get_real_func_ ("fork"); - } - ret = real_fork (); - if (ret == 0) { - /* if we have forked corosync then anything goes. */ - tsafe_off (); - } - return ret; -} #endif /* COROSYNC_LINUX */ /*