BUG#: 5604
authorkumpf <kumpf>
Wed, 4 Oct 2006 19:40:04 +0000 (19:40 +0000)
committerkumpf <kumpf>
Wed, 4 Oct 2006 19:40:04 +0000 (19:40 +0000)
TITLE: Tracer Mutex is not fork safe
DESCRIPTION: Control the status of the trace file mutex across fork() calls.

src/Pegasus/Common/IPCUnix.cpp
src/Pegasus/Common/Mutex.h
src/Pegasus/Common/TraceFileHandlerUnix.cpp

index 041c1d6173383f1680136541abd78414fd847200..dcc7423cd8af6ea47438792fcfec1ed39743b89a 100644 (file)
@@ -209,6 +209,14 @@ Mutex::~Mutex()
    }
 }
 
+#if defined(PEGASUS_OS_LINUX)
+void Mutex::reinitialize()
+{
+   pthread_mutex_init(&_mutex.mut, &_mutex.mutatt);
+   _mutex.owner = 0;
+}
+#endif
+
 
 #ifdef PEGASUS_READWRITE_NATIVE
 //-----------------------------------------------------------------
index e4f4cd4c63dabdf37112e795887b3a96d9eadbe2..98e481c3738cf333459ecdead19d743732a6db59 100644 (file)
@@ -89,6 +89,15 @@ public:
 
     inline PEGASUS_THREAD_TYPE get_owner() { return(_mutex.owner); }
 
+#if defined(PEGASUS_OS_LINUX)
+    /**
+        This method must only be called after a fork() to reset the mutex
+        lock status in the new process.  Any other use of this method is
+        unsafe.
+    */
+    void reinitialize();
+#endif
+
 private:
     inline void _set_owner(PEGASUS_THREAD_TYPE owner) { _mutex.owner = owner; }
     PEGASUS_MUTEX_HANDLE _mutex;
index e592f64c516da36b8a3bc2223a123055616e1c92..9a056a5b5f47807732ba802db8695ee643105882 100644 (file)
@@ -50,6 +50,59 @@ PEGASUS_NAMESPACE_BEGIN
 
 static Mutex writeMutex;
 
+// Ensure that the static writeMutex is not locked across a fork().
+
+#if defined(PEGASUS_OS_LINUX)
+
+class ForkSafeWriteMutex
+{
+public:
+    ForkSafeWriteMutex()
+    {
+        pthread_atfork(
+            0,
+            0,
+            _reinitializeMutex);
+    }
+
+private:
+    static void _reinitializeMutex()
+    {
+        writeMutex.reinitialize();
+    }
+};
+
+static ForkSafeWriteMutex __forkSafeWriteMutex;
+
+#elif !defined(PEGASUS_OS_ZOS) && !defined(PEGASUS_OS_VMS)
+
+class ForkSafeWriteMutex
+{
+public:
+    ForkSafeWriteMutex()
+    {
+        pthread_atfork(
+            _lockMutex,
+            _unlockMutex,
+            _unlockMutex);
+    }
+
+private:
+    static void _lockMutex()
+    {
+        writeMutex.lock(pegasus_thread_self());
+    }
+
+    static void _unlockMutex()
+    {
+        writeMutex.unlock();
+    }
+};
+
+static ForkSafeWriteMutex __forkSafeWriteMutex;
+
+#endif
+
 ///////////////////////////////////////////////////////////////////////////////
 //  Writes message to file. Locks the file before writing to it
 //  Implementation of this function is platform specific