Index: /trunk/docs/Changelog
===================================================================
--- /trunk/docs/Changelog	(revision 134)
+++ /trunk/docs/Changelog	(revision 135)
@@ -5,5 +5,5 @@
 	* protect readdir(), getpwent(), gethostname() with mutexes 
 	  (readdir_r considered harmful)
-	* make checksum/hash function reentrant
+	* make checksum/hash, entropy function reentrant
 	* use thread-specific conversion buffer for globber()
 
Index: /trunk/src/sh_entropy.c
===================================================================
--- /trunk/src/sh_entropy.c	(revision 134)
+++ /trunk/src/sh_entropy.c	(revision 135)
@@ -117,5 +117,5 @@
 int sh_entropy(int getbytes, char * nbuf)
 {
-    static int fd = -1;
+    int fd = -1;
     int n;
     byte buffer[256+2];
@@ -148,7 +148,7 @@
         int addr_len;
 
-      #ifdef EGD_SOCKET_NAME
+#ifdef EGD_SOCKET_NAME
         bname = EGD_SOCKET_NAME;
-      #endif
+#endif
         if ( !bname || !*bname )
             bname = _("=entropy");
@@ -191,4 +191,5 @@
 			     _("sh_entropy") ); 
 	    SH_FREE(name);
+	    close(fd);
 	    SL_RETURN( -1, _("sh_entropy") );
 	  }
@@ -206,5 +207,6 @@
 	    sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
 			     _("cannot write to EGD"),
-			     _("sh_entropy") ); 
+			     _("sh_entropy") );
+	    close(fd);
 	    SL_RETURN( -1, _("sh_entropy") );
 	  }
@@ -247,5 +249,6 @@
 	    sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
 			     _("cannot write to EGD"),
-			     _("sh_entropy") ); 
+			     _("sh_entropy") );
+	    close(fd);
 	    SL_RETURN( -1, _("sh_entropy") );
 	  }
@@ -267,5 +270,5 @@
     }
     memset(buffer, 0, sizeof(buffer) );
-
+    close(fd);
     SL_RETURN( 0, _("sh_entropy") ); /* success */
 }
@@ -275,4 +278,6 @@
 
 #if defined (HAVE_URANDOM)
+
+#include "sh_pthread.h"
 
 int read_mbytes(int timeout_val, char * path, char * nbuf, int nbytes)
@@ -373,4 +378,5 @@
       /* -- Give out nbytes bytes from the new pool. --
        */
+      SH_MUTEX_LOCK_UNSAFE(mutex_skey);
       for (i = 0; i < KEY_BYT; ++i)
 	{
@@ -379,4 +385,5 @@
 	    nbuf[i] = keybuf[i];
 	}
+      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
       memset (keybuf, '\0', KEY_BYT);
       
@@ -408,4 +415,5 @@
 
 #include "sh_static.h"
+#include "sh_pthread.h"
 
 static
@@ -432,5 +440,5 @@
 
 static
-sourcetable_t source[] = {
+sourcetable_t source_template[] = {
   { N_("w"),
     N_("w"),
@@ -712,4 +720,6 @@
   int    imax, selcount;
   char errbuf[SH_ERRBUF_SIZE];
+
+  sourcetable_t  *source = NULL;
   
   SL_ENTER(_("sh_entropy"));
@@ -723,4 +733,5 @@
   /* --- If there is entropy in the pool, return it. ---
    */
+  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
   if (skey->poolc >= nbytes)
     {
@@ -731,6 +742,8 @@
 	  --skey->poolc;
 	}
+      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey); /* alternative path */
       SL_RETURN(0, _("sh_entropy"));
     }
+  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
 
 
@@ -746,4 +759,12 @@
 		      _("sl_get_cap_sub"));
     }
+
+  while (source_template[i].command != NULL) {
+    ++i;
+  }
+  source = SH_ALLOC(i * sizeof(sourcetable_t));
+  for (j = 0; j < i;++j)
+    memcpy(&source[j], &source_template[j], sizeof(sourcetable_t));
+  i = 0;
 
   while (source[i].command != NULL) {
@@ -877,4 +898,6 @@
     }
   buffer[bufcount] = '\0';
+  
+  SH_FREE(source);
 
   if (0 != (caperr = sl_drop_cap_sub()))
@@ -906,7 +929,9 @@
       /* store in system pool
        */
+      SH_MUTEX_LOCK_UNSAFE(mutex_skey);
       for (i = 0; i < KEY_BYT; ++i)
 	skey->poolv[i] = keybuf[i];
       skey->poolc = KEY_BYT;
+      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
       memset (buffer, '\0', BUF_ENT+2);
       memset (keybuf, '\0', KEY_BYT);
@@ -921,4 +946,5 @@
   /* give out nbytes Bytes from the entropy pool
    */
+  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
   for (i = 0; i < nbytes; ++i)
     {
@@ -926,4 +952,5 @@
       --skey->poolc;
     }
+  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
 
   SL_RETURN(0, _("sh_entropy"));
