Index: trunk/src/dnmalloc.c
===================================================================
--- trunk/src/dnmalloc.c	(revision 173)
+++ trunk/src/dnmalloc.c	(revision 174)
@@ -1508,12 +1508,22 @@
     pthread_mutex_unlock(&mALLOC_MUTEx); 
 }
-void dnmalloc_fork_child(void) { 
+void dnmalloc_fork_child(void) {
+  int rc;
 #ifdef __GLIBC__
   if (dnmalloc_use_mutex)
-    pthread_mutex_init(&mALLOC_MUTEx, NULL); 
+    {
+      pthread_mutex_unlock (&mALLOC_MUTEx);
+      pthread_mutex_destroy(&mALLOC_MUTEx);
+      rc = pthread_mutex_init(&mALLOC_MUTEx, NULL);
+    } 
 #else
   if (dnmalloc_use_mutex)
-    pthread_mutex_unlock(&mALLOC_MUTEx); 
-#endif
+    rc = pthread_mutex_unlock(&mALLOC_MUTEx); 
+#endif
+  if (rc != 0)
+    {
+      fputs("fork_child failed", stderr);
+      _exit(EXIT_FAILURE);
+    }
 }
 static int dnmalloc_mutex_lock(pthread_mutex_t *mutex)
Index: trunk/src/samhain.c
===================================================================
--- trunk/src/samhain.c	(revision 173)
+++ trunk/src/samhain.c	(revision 174)
@@ -1234,5 +1234,5 @@
   /* --- Close all but first three file descriptors. ---
    */
-  sh_unix_closeall(3, -1); /* at program start */
+  sh_unix_closeall(3, -1, SL_FALSE); /* at program start */
 
 
Index: trunk/src/sh_entropy.c
===================================================================
--- trunk/src/sh_entropy.c	(revision 173)
+++ trunk/src/sh_entropy.c	(revision 174)
@@ -581,5 +581,5 @@
       /* don't leak file descriptors
        */
-      sh_unix_closeall (3, -1); /* in child process */
+      sh_unix_closeall (3, -1, SL_TRUE); /* in child process */
 
       /* zero priv info
Index: trunk/src/sh_extern.c
===================================================================
--- trunk/src/sh_extern.c	(revision 173)
+++ trunk/src/sh_extern.c	(revision 174)
@@ -294,5 +294,5 @@
 	   */
 #if !defined(PDGBFILE)
-	  sh_unix_closeall (3, task->com_fd); /* in child process */
+	  sh_unix_closeall (3, task->com_fd, SL_TRUE); /* in child process */
 #endif
 
Index: trunk/src/sh_gpg.c
===================================================================
--- trunk/src/sh_gpg.c	(revision 173)
+++ trunk/src/sh_gpg.c	(revision 174)
@@ -487,5 +487,5 @@
       /* don't leak file descriptors
        */
-      sh_unix_closeall (3, -1); /* in child process */
+      sh_unix_closeall (3, -1, SL_TRUE); /* in child process */
 
       if (NULL == freopen(_("/dev/null"), "r+", stderr))
Index: trunk/src/sh_unix.c
===================================================================
--- trunk/src/sh_unix.c	(revision 173)
+++ trunk/src/sh_unix.c	(revision 174)
@@ -1246,5 +1246,5 @@
 #endif
 
-void sh_unix_closeall (int fd, int except)
+void sh_unix_closeall (int fd, int except, int inchild)
 {
   int fdx = fd;
@@ -1281,5 +1281,8 @@
     }
 
-  sl_dropall (fdx, except);
+  if (!inchild)
+    sl_dropall (fdx, except);
+  else
+    sl_dropall_dirty (fdx, except);
 
   SL_RET0(_("sh_unix_closeall"));
Index: trunk/src/slib.c
===================================================================
--- trunk/src/slib.c	(revision 173)
+++ trunk/src/slib.c	(revision 174)
@@ -2093,4 +2093,17 @@
 	    (void) free(ofiles[fd]->path);
 	  (void) free(ofiles[fd]);
+	  ofiles[fd] = NULL;
+	}
+      ++fd;
+    }
+  return 0;
+}
+
+int sl_dropall_dirty(int fd, int except)
+{
+  while (fd < MAXFD)
+    {
+      if (ofiles[fd] != NULL && fd != except)
+	{
 	  ofiles[fd] = NULL;
 	}
