Changeset 135 for trunk


Ignore:
Timestamp:
Oct 27, 2007, 12:51:09 AM (17 years ago)
Author:
rainer
Message:

Reentrant sh_entropy() function.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/Changelog

    r134 r135  
    55        * protect readdir(), getpwent(), gethostname() with mutexes
    66          (readdir_r considered harmful)
    7         * make checksum/hash function reentrant
     7        * make checksum/hash, entropy function reentrant
    88        * use thread-specific conversion buffer for globber()
    99
  • trunk/src/sh_entropy.c

    r133 r135  
    117117int sh_entropy(int getbytes, char * nbuf)
    118118{
    119     static int fd = -1;
     119    int fd = -1;
    120120    int n;
    121121    byte buffer[256+2];
     
    148148        int addr_len;
    149149
    150       #ifdef EGD_SOCKET_NAME
     150#ifdef EGD_SOCKET_NAME
    151151        bname = EGD_SOCKET_NAME;
    152       #endif
     152#endif
    153153        if ( !bname || !*bname )
    154154            bname = _("=entropy");
     
    191191                             _("sh_entropy") );
    192192            SH_FREE(name);
     193            close(fd);
    193194            SL_RETURN( -1, _("sh_entropy") );
    194195          }
     
    206207            sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
    207208                             _("cannot write to EGD"),
    208                              _("sh_entropy") );
     209                             _("sh_entropy") );
     210            close(fd);
    209211            SL_RETURN( -1, _("sh_entropy") );
    210212          }
     
    247249            sh_error_handle ((-1), FIL__, __LINE__, myerror, MSG_E_SUBGEN,
    248250                             _("cannot write to EGD"),
    249                              _("sh_entropy") );
     251                             _("sh_entropy") );
     252            close(fd);
    250253            SL_RETURN( -1, _("sh_entropy") );
    251254          }
     
    267270    }
    268271    memset(buffer, 0, sizeof(buffer) );
    269 
     272    close(fd);
    270273    SL_RETURN( 0, _("sh_entropy") ); /* success */
    271274}
     
    275278
    276279#if defined (HAVE_URANDOM)
     280
     281#include "sh_pthread.h"
    277282
    278283int read_mbytes(int timeout_val, char * path, char * nbuf, int nbytes)
     
    373378      /* -- Give out nbytes bytes from the new pool. --
    374379       */
     380      SH_MUTEX_LOCK_UNSAFE(mutex_skey);
    375381      for (i = 0; i < KEY_BYT; ++i)
    376382        {
     
    379385            nbuf[i] = keybuf[i];
    380386        }
     387      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
    381388      memset (keybuf, '\0', KEY_BYT);
    382389     
     
    408415
    409416#include "sh_static.h"
     417#include "sh_pthread.h"
    410418
    411419static
     
    432440
    433441static
    434 sourcetable_t source[] = {
     442sourcetable_t source_template[] = {
    435443  { N_("w"),
    436444    N_("w"),
     
    712720  int    imax, selcount;
    713721  char errbuf[SH_ERRBUF_SIZE];
     722
     723  sourcetable_t  *source = NULL;
    714724 
    715725  SL_ENTER(_("sh_entropy"));
     
    723733  /* --- If there is entropy in the pool, return it. ---
    724734   */
     735  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
    725736  if (skey->poolc >= nbytes)
    726737    {
     
    731742          --skey->poolc;
    732743        }
     744      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey); /* alternative path */
    733745      SL_RETURN(0, _("sh_entropy"));
    734746    }
     747  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
    735748
    736749
     
    746759                      _("sl_get_cap_sub"));
    747760    }
     761
     762  while (source_template[i].command != NULL) {
     763    ++i;
     764  }
     765  source = SH_ALLOC(i * sizeof(sourcetable_t));
     766  for (j = 0; j < i;++j)
     767    memcpy(&source[j], &source_template[j], sizeof(sourcetable_t));
     768  i = 0;
    748769
    749770  while (source[i].command != NULL) {
     
    877898    }
    878899  buffer[bufcount] = '\0';
     900 
     901  SH_FREE(source);
    879902
    880903  if (0 != (caperr = sl_drop_cap_sub()))
     
    906929      /* store in system pool
    907930       */
     931      SH_MUTEX_LOCK_UNSAFE(mutex_skey);
    908932      for (i = 0; i < KEY_BYT; ++i)
    909933        skey->poolv[i] = keybuf[i];
    910934      skey->poolc = KEY_BYT;
     935      SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
    911936      memset (buffer, '\0', BUF_ENT+2);
    912937      memset (keybuf, '\0', KEY_BYT);
     
    921946  /* give out nbytes Bytes from the entropy pool
    922947   */
     948  SH_MUTEX_LOCK_UNSAFE(mutex_skey);
    923949  for (i = 0; i < nbytes; ++i)
    924950    {
     
    926952      --skey->poolc;
    927953    }
     954  SH_MUTEX_UNLOCK_UNSAFE(mutex_skey);
    928955
    929956  SL_RETURN(0, _("sh_entropy"));
Note: See TracChangeset for help on using the changeset viewer.