pike.git / src / builtin.cmod

version» Context lines:

pike.git/src/builtin.cmod:2195:    {    int f = low_find_lfun(o->prog, LFUN__RANDOM);    if (f < 0)    Pike_error("Calling undefined lfun::%s.\n", lfun_names[LFUN__RANDOM]);    apply_low(o, f, 0);    }   }      #ifdef __NT__   #include <wincrypt.h> + static HCRYPTPROV crypto_handle;   PIKECLASS RandomSystem   {    INHERIT RandomInterface; -  CVAR HCRYPTPROV handle; +     -  INIT -  { -  THIS->handle = NULL; -  } -  +     PIKEFUN string(8bit) random_string(int len)    {    if( len<1 )    RETURN empty_pike_string;    if(!crypto_handle)    { -  if( !CryptAcquireContext(&THIS->handle, 0, 0, PROV_RSA_FULL, +  if( !CryptAcquireContext(&crypto_handle, 0, 0, PROV_RSA_FULL,    CRYPT_VERIFYCONTEXT|CRYPT_SILENT) )    Pike_error("Failed to set up Crypto Service.\n");    }       struct pike_string *res = begin_shared_string(size); -  if( !CryptGenRandom(THIS->handle, size, (BYTE*)res->str) ) +  if( !CryptGenRandom(crypto_handle, size, (BYTE*)res->str) )    {    do_free_unlinked_pike_string (res);    Pike_error("Failed to create random data.\n");    }       RETURN end_shared_string(res);    }   }   #else -  + static int random_fd;   PIKECLASS RandomSystem   {    INHERIT RandomInterface; -  CVAR int fd; +     -  INIT -  { -  THIS->fd = -1; -  } -  -  EXIT -  { -  close(THIS->fd); -  } -  +     PIKEFUN string(8bit) random_string(int len)    {    if( len<1 )    RETURN empty_pike_string;    -  if( THIS->fd==-1 ) +  if( random_fd==-1 )    { -  THIS->fd = open("/dev/urandom", O_RDONLY); -  if( THIS->fd==-1 ) +  random_fd = open("/dev/urandom", O_RDONLY); +  if( random_fd==-1 )    Pike_error("Failed to open /dev/urandom.\n");    }       struct pike_string *ret = begin_shared_string(len);    char* str = ret->str;    while( len )    { -  int sz = read(THIS->fd, str, len); +  int sz = read(random_fd, str, len);    str += sz;    len -= sz;    }    -  pop_n_elems(args); -  push_string(end_shared_string(ret)); +  RETURN end_shared_string(ret);    }   }   #endif      /*! @decl mixed random(object o)    *! If random is called with an object, @[lfun::random] will be    *! called in the object.    *!    *! @seealso    *! @[lfun::_random()]
pike.git/src/builtin.cmod:6066:    }   }      void init_builtin(void)   {    SET_SVAL(gc_pre_cb, PIKE_T_INT, NUMBER_NUMBER, integer, 0);    SET_SVAL(gc_post_cb, PIKE_T_INT, NUMBER_NUMBER, integer, 0);    SET_SVAL(gc_destruct_cb, PIKE_T_INT, NUMBER_NUMBER, integer, 0);    SET_SVAL(gc_done_cb, PIKE_T_INT, NUMBER_NUMBER, integer, 0);    INIT; +  random_fd = -1;   }      void exit_builtin(void)   {    struct svalue zero;    if (val_module) free_object (val_module);    EXIT;    SET_SVAL(zero, PIKE_T_INT, NUMBER_NUMBER, integer, 0);    assign_svalue(&gc_pre_cb, &zero);    assign_svalue(&gc_post_cb, &zero);
pike.git/src/builtin.cmod:6088:   #ifndef DO_PIKE_CLEANUP    /* This is performed by exit_builtin_modules() at a later point    * in this case, so that the pike_list_node's are valid at cleanup    * time, thus avoiding "got invalid pointer" fatals at exit.    */    ba_destroy(&pike_list_node_allocator);   #endif   #ifndef USE_SETENV    if (env_allocs) free_mapping (env_allocs);   #endif +  if (random_fd!=-1) close(random_fd);   }