summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-10-30 15:35:37 +0100
committerLennart Poettering <lennart@poettering.net>2014-10-30 15:35:37 +0100
commitef309a681f4c761503e4cd4cc6884d7d6ef70436 (patch)
treef8f465ddb86dd8c9c65f11b4596aba875f14acc3 /src/shared
parent97768fc5746ea97cb2d3ac6854b4fc7ce1c14f24 (diff)
util: unify how we see srand()
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/util.c53
-rw-r--r--src/shared/util.h1
2 files changed, 31 insertions, 23 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
index 1d67abe58f..0f44eb5afe 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -2521,8 +2521,36 @@ int dev_urandom(void *p, size_t n) {
return 0;
}
-void random_bytes(void *p, size_t n) {
+void initialize_srand(void) {
static bool srand_called = false;
+ unsigned x;
+#ifdef HAVE_SYS_AUXV_H
+ void *auxv;
+#endif
+
+ if (srand_called)
+ return;
+
+ x = 0;
+
+#ifdef HAVE_SYS_AUXV_H
+ /* The kernel provides us with a bit of entropy in auxv, so
+ * let's try to make use of that to seed the pseudo-random
+ * generator. It's better than nothing... */
+
+ auxv = (void*) getauxval(AT_RANDOM);
+ if (auxv)
+ x ^= *(unsigned*) auxv;
+#endif
+
+ x ^= (unsigned) now(CLOCK_REALTIME);
+ x ^= (unsigned) gettid();
+
+ srand(x);
+ srand_called = true;
+}
+
+void random_bytes(void *p, size_t n) {
uint8_t *q;
int r;
@@ -2533,28 +2561,7 @@ void random_bytes(void *p, size_t n) {
/* If some idiot made /dev/urandom unavailable to us, he'll
* get a PRNG instead. */
- if (!srand_called) {
- unsigned x = 0;
-
-#ifdef HAVE_SYS_AUXV_H
- /* The kernel provides us with a bit of entropy in
- * auxv, so let's try to make use of that to seed the
- * pseudo-random generator. It's better than
- * nothing... */
-
- void *auxv;
-
- auxv = (void*) getauxval(AT_RANDOM);
- if (auxv)
- x ^= *(unsigned*) auxv;
-#endif
-
- x ^= (unsigned) now(CLOCK_REALTIME);
- x ^= (unsigned) gettid();
-
- srand(x);
- srand_called = true;
- }
+ initialize_srand();
for (q = p; q < (uint8_t*) p + n; q ++)
*q = rand();
diff --git a/src/shared/util.h b/src/shared/util.h
index 35584467c1..aad9eb7fcb 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -321,6 +321,7 @@ int make_console_stdio(void);
int dev_urandom(void *p, size_t n);
void random_bytes(void *p, size_t n);
+void initialize_srand(void);
static inline uint64_t random_u64(void) {
uint64_t u;