Skip to content

Commit

Permalink
Always seed PRNG with PID, seconds, and milliseconds.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdocguard committed Aug 4, 2016
1 parent 31389e4 commit 42f6e1f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/os/unix/ngx_posix_init.c
Expand Up @@ -33,7 +33,8 @@ ngx_os_io_t ngx_os_io = {
ngx_int_t
ngx_os_init(ngx_log_t *log)
{
ngx_uint_t n;
ngx_time_t *tp;
ngx_uint_t n;

#if (NGX_HAVE_OS_SPECIFIC_INIT)
if (ngx_os_specific_init(log) != NGX_OK) {
Expand Down Expand Up @@ -76,7 +77,8 @@ ngx_os_init(ngx_log_t *log)
ngx_inherited_nonblocking = 0;
#endif

srandom(ngx_time());
tp = ngx_timeofday();
srandom(((unsigned) ngx_pid << 16) ^ tp->sec ^ tp->msec);

return NGX_OK;
}
Expand Down
4 changes: 3 additions & 1 deletion src/os/unix/ngx_process_cycle.c
Expand Up @@ -785,6 +785,7 @@ ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker)
{
sigset_t set;
ngx_int_t n;
ngx_time_t *tp;
ngx_uint_t i;
ngx_cpuset_t *cpu_affinity;
struct rlimit rlmt;
Expand Down Expand Up @@ -884,7 +885,8 @@ ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker)
"sigprocmask() failed");
}

srandom(((unsigned) ngx_pid << 16) ^ ngx_time());
tp = ngx_timeofday();
srandom(((unsigned) ngx_pid << 16) ^ tp->sec ^ tp->msec);

/*
* disable deleting previous events for the listening sockets because
Expand Down
4 changes: 3 additions & 1 deletion src/os/win32/ngx_process_cycle.c
Expand Up @@ -762,9 +762,11 @@ static ngx_thread_value_t __stdcall
ngx_worker_thread(void *data)
{
ngx_int_t n;
ngx_time_t *tp;
ngx_cycle_t *cycle;

srand((ngx_pid << 16) ^ (unsigned) ngx_time());
tp = ngx_timeofday();
srand((ngx_pid << 16) ^ (unsigned) tp->sec ^ tp->msec);

cycle = (ngx_cycle_t *) ngx_cycle;

Expand Down
16 changes: 9 additions & 7 deletions src/os/win32/ngx_win32_init.c
Expand Up @@ -59,12 +59,13 @@ static GUID dx_guid = WSAID_DISCONNECTEX;
ngx_int_t
ngx_os_init(ngx_log_t *log)
{
DWORD bytes;
SOCKET s;
WSADATA wsd;
ngx_err_t err;
ngx_uint_t n;
SYSTEM_INFO si;
DWORD bytes;
SOCKET s;
WSADATA wsd;
ngx_err_t err;
ngx_time_t *tp;
ngx_uint_t n;
SYSTEM_INFO si;

/* get Windows version */

Expand Down Expand Up @@ -237,7 +238,8 @@ ngx_os_init(ngx_log_t *log)
ngx_sprintf((u_char *) ngx_unique, "%P%Z", ngx_pid);
}

srand((ngx_pid << 16) ^ (unsigned) ngx_time());
tp = ngx_timeofday();
srand((ngx_pid << 16) ^ (unsigned) tp->sec ^ tp->msec);

return NGX_OK;
}
Expand Down

0 comments on commit 42f6e1f

Please sign in to comment.