]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Replace rand.c with i_rand function
authorAki Tuomi <aki.tuomi@dovecot.fi>
Wed, 23 Aug 2017 09:38:07 +0000 (12:38 +0300)
committerTimo Sirainen <tss@dovecot.fi>
Thu, 7 Sep 2017 08:40:31 +0000 (11:40 +0300)
Replacement for rand

src/lib/Makefile.am
src/lib/lib.c
src/lib/lib.h
src/lib/rand.c
src/lib/rand.h [deleted file]

index 8791f1fc62b99282b7a4cbe454f96e91ea1ba876..a018b7c5ebe25dc17476e222eea4faedd615ac25 100644 (file)
@@ -269,7 +269,6 @@ headers = \
        printf-format-fix.h \
        process-title.h \
        priorityq.h \
-       rand.h \
        randgen.h \
        read-full.h \
        restrict-access.h \
index 7a987ea0985b404ed520e87162267fc95c52edd9..4f998ba07df81b873882993ba3dc3cfe88eb059b 100644 (file)
@@ -9,6 +9,7 @@
 #include "ipwd.h"
 #include "process-title.h"
 #include "var-expand-private.h"
+#include "randgen.h"
 
 #include <fcntl.h>
 #include <unistd.h>
@@ -163,14 +164,8 @@ static void lib_open_non_stdio_dev_null(void)
 
 void lib_init(void)
 {
-       struct timeval tv;
        i_assert(!lib_initialized);
-
-       /* standard way to get rand() return different values. */
-       if (gettimeofday(&tv, NULL) < 0)
-               i_fatal("gettimeofday(): %m");
-       srand((unsigned int) (tv.tv_sec ^ tv.tv_usec ^ getpid()));
-
+       random_init();
        data_stack_init();
        hostpid_init();
        lib_open_non_stdio_dev_null();
@@ -197,4 +192,5 @@ void lib_deinit(void)
        env_deinit();
        failures_deinit();
        process_title_deinit();
+       random_deinit();
 }
index 528165829f361b58ba9f40fb5df4ed42e873dab6..e795101e4dbe8e2f2ab046c5e6ff4e53586a7dec 100644 (file)
@@ -32,7 +32,6 @@
 #include "mempool.h"
 #include "imem.h"
 #include "byteorder.h"
-#include "rand.h"
 
 typedef struct buffer buffer_t;
 typedef struct buffer string_t;
@@ -90,4 +89,6 @@ void lib_init(void);
 bool lib_is_initialized(void);
 void lib_deinit(void);
 
+uint32_t i_rand(void);
+
 #endif
index ec4930594ae9738aa88b1b63aba62237cf5e8e54..781a38f6bc47cbb13df8d1bd3dbcbbf51f82e618 100644 (file)
@@ -1,14 +1,22 @@
 /* Copyright (c) 2014-2017 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
+#include "randgen.h"
 
 #ifdef HAVE_ARC4RANDOM
 #ifdef HAVE_LIBBSD
 #include <bsd/stdlib.h>
 #endif
 
-/* this returns [0,RAND_MAX), to keep it compatible with rand() */
-int arc4random_rand(void) {
-       return (int)(arc4random() % ((unsigned)RAND_MAX + 1));
+uint32_t i_rand(void)
+{
+       return arc4random();
+}
+#else
+uint32_t i_rand(void)
+{
+       uint32_t value;
+       random_fill(&value, sizeof(value));
+       return value;
 }
 #endif
diff --git a/src/lib/rand.h b/src/lib/rand.h
deleted file mode 100644 (file)
index cf3d8c2..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-#ifndef RAND_H
-#define RAND_H
-
-#ifdef HAVE_ARC4RANDOM
-
-int arc4random_rand(void);
-#define rand arc4random_rand
-
-#endif
-
-#endif