summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2017-07-10 21:07:12 +0300
committerIgor Sysoev <igor@sysoev.ru>2017-07-10 21:07:12 +0300
commit3bccb7f358e27f932c951a55bc17aa113bfae5f0 (patch)
tree78fc9f0fc0b20c7df284208db901eacc7dd6b6a8
parentdc874cd22ef7ec72fc6e5927b1695d4a12536625 (diff)
downloadunit-3bccb7f358e27f932c951a55bc17aa113bfae5f0.tar.gz
unit-3bccb7f358e27f932c951a55bc17aa113bfae5f0.tar.bz2
Introducing thread-safe nxt_random().
-rw-r--r--src/nxt_lib.c3
-rw-r--r--src/nxt_main.h1
-rw-r--r--src/nxt_port_memory.c2
-rw-r--r--src/nxt_process.c5
-rw-r--r--src/nxt_random.c7
-rw-r--r--src/nxt_random.h20
-rw-r--r--src/nxt_router.c2
-rw-r--r--src/nxt_thread.c2
-rw-r--r--src/nxt_thread.h2
9 files changed, 12 insertions, 32 deletions
diff --git a/src/nxt_lib.c b/src/nxt_lib.c
index d113bcbb..d5daa6e8 100644
--- a/src/nxt_lib.c
+++ b/src/nxt_lib.c
@@ -11,7 +11,6 @@ nxt_uint_t nxt_ncpu = 1;
nxt_uint_t nxt_pagesize;
nxt_task_t nxt_main_task;
nxt_atomic_t nxt_task_ident;
-nxt_random_t nxt_random_data;
nxt_thread_declare_data(nxt_thread_t, nxt_thread_context);
@@ -100,7 +99,7 @@ nxt_lib_start(const char *app, char **argv, char ***envp)
nxt_thread_spin_init(nxt_ncpu, 0);
- nxt_random_init(&nxt_random_data);
+ nxt_random_init(&thread->random);
nxt_pagesize = getpagesize();
diff --git a/src/nxt_main.h b/src/nxt_main.h
index 0a7c1469..05cca62b 100644
--- a/src/nxt_main.h
+++ b/src/nxt_main.h
@@ -175,7 +175,6 @@ NXT_EXPORT extern nxt_uint_t nxt_ncpu;
NXT_EXPORT extern nxt_uint_t nxt_pagesize;
NXT_EXPORT extern nxt_task_t nxt_main_task;
NXT_EXPORT extern nxt_atomic_t nxt_task_ident;
-NXT_EXPORT extern nxt_random_t nxt_random_data;
#endif /* _NXT_LIB_H_INCLUDED_ */
diff --git a/src/nxt_port_memory.c b/src/nxt_port_memory.c
index 6aaff5cf..38e7db70 100644
--- a/src/nxt_port_memory.c
+++ b/src/nxt_port_memory.c
@@ -217,7 +217,7 @@ nxt_port_new_port_mmap(nxt_task_t *task, nxt_process_t *process,
}
p = nxt_sprintf(name, name + sizeof(name), "/nginext.%PI.%uxD",
- nxt_pid, nxt_random(&nxt_random_data));
+ nxt_pid, nxt_random(&task->thread->random));
*p = '\0';
#if (NXT_HAVE_MEMFD_CREATE)
diff --git a/src/nxt_process.c b/src/nxt_process.c
index 97b51c4e..7d5af92d 100644
--- a/src/nxt_process.c
+++ b/src/nxt_process.c
@@ -82,7 +82,9 @@ nxt_process_start(nxt_task_t *task, nxt_process_init_t *process)
nxt_process_title(task, "nginext: %s", process->name);
- nxt_random_init(&nxt_random_data);
+ thread = task->thread;
+
+ nxt_random_init(&thread->random);
if (process->user_cred != NULL && getuid() == 0) {
/* Super-user. */
@@ -93,7 +95,6 @@ nxt_process_start(nxt_task_t *task, nxt_process_init_t *process)
}
}
- thread = task->thread;
rt = thread->runtime;
rt->types |= (1U << process->type);
diff --git a/src/nxt_random.c b/src/nxt_random.c
index 9af40079..ea73ae18 100644
--- a/src/nxt_random.c
+++ b/src/nxt_random.c
@@ -8,8 +8,6 @@
#include <nxt_main.h>
-#if !(NXT_HAVE_ARC4RANDOM)
-
/*
* The pseudorandom generator based on OpenBSD arc4random. Although it is
* usually stated that arc4random uses RC4 pseudorandom generation algorithm
@@ -56,7 +54,7 @@ nxt_random_stir(nxt_random_t *r)
ssize_t n;
struct timeval tv;
union {
- uint32_t value[3];
+ uint32_t value[4];
u_char bytes[NXT_RANDOM_KEY_SIZE];
} key;
@@ -87,6 +85,7 @@ nxt_random_stir(nxt_random_t *r)
key.value[0] ^= tv.tv_usec;
key.value[1] ^= tv.tv_sec;
key.value[2] ^= nxt_pid;
+ key.value[3] ^= nxt_thread_tid(NULL);
}
nxt_random_add(r, key.bytes, NXT_RANDOM_KEY_SIZE);
@@ -202,5 +201,3 @@ nxt_random_unit_test(nxt_thread_t *thr)
}
#endif
-
-#endif
diff --git a/src/nxt_random.h b/src/nxt_random.h
index bfc4e3b3..f9c07e54 100644
--- a/src/nxt_random.h
+++ b/src/nxt_random.h
@@ -8,24 +8,6 @@
#define _NXT_RANDOM_H_INCLUDED_
-#if (NXT_HAVE_ARC4RANDOM)
-
-/*
- * arc4random() has been introduced in OpenBSD 2.1 and then was ported
- * to FreeBSD 2.2.6, NetBSD 2.0, MacOSX and SmartOS.
- *
- * arc4random() automatically initializes itself in the first call and
- * then reinitializes itself in the first call in every forked processes.
- */
-
-typedef void *nxt_random_t;
-
-
-#define nxt_random_init(r)
-#define nxt_random(r) arc4random()
-
-#else
-
typedef struct {
uint8_t i;
uint8_t j;
@@ -41,7 +23,5 @@ uint32_t nxt_random(nxt_random_t *r);
nxt_int_t nxt_random_unit_test(nxt_thread_t *thr);
#endif
-#endif
-
#endif /* _NXT_RANDOM_H_INCLUDED_ */
diff --git a/src/nxt_router.c b/src/nxt_router.c
index be47e453..8c5236be 100644
--- a/src/nxt_router.c
+++ b/src/nxt_router.c
@@ -1655,7 +1655,7 @@ nxt_router_process_http_request(nxt_task_t *task, nxt_conn_t *c,
engine = task->thread->engine;
do {
- req_id = nxt_random(&nxt_random_data);
+ req_id = nxt_random(&task->thread->random);
} while (nxt_event_engine_request_find(engine, req_id) != NULL);
rc = nxt_conn_request_add(c, req_id);
diff --git a/src/nxt_thread.c b/src/nxt_thread.c
index 851ebb4f..0619ad1b 100644
--- a/src/nxt_thread.c
+++ b/src/nxt_thread.c
@@ -160,6 +160,8 @@ nxt_thread_init(void)
nxt_thread_time_update(thr);
}
+ nxt_random_init(&thr->random);
+
return thr;
}
diff --git a/src/nxt_thread.h b/src/nxt_thread.h
index ad1c58e8..33314b71 100644
--- a/src/nxt_thread.h
+++ b/src/nxt_thread.h
@@ -188,6 +188,8 @@ struct nxt_thread_s {
* engine->fibers, its placement here eliminates 2 memory accesses.
*/
nxt_fiber_t *fiber;
+
+ nxt_random_t random;
};