[PATCH 1/4] lib: add function to seed random()

Sami Kerola
2013-01-09T19:37:05Z

include/randutils.h | 1 +
lib/randutils.c | 18 ++++++++++++++++++
2 files changed, 19 insertions(+)

diff --git a/include/randutils.h b/include/randutils.h
index dec5e35..9d4d41c 100644
--- a/include/randutils.h
+++ b/include/randutils.h
@@ -8,5 +8,6 @@

extern int random_get_fd(void);
extern void random_get_bytes(void *buf, size_t nbytes);
+extern void seed_random(void);

#endif
diff --git a/lib/randutils.c b/lib/randutils.c
index 85cb1a9..44d996e 100644
--- a/lib/randutils.c
+++ b/lib/randutils.c
@@ -104,6 +104,24 @@ void random_get_bytes(void *buf, size_t nbytes)
return;
}

+/* Seed random(3). */
+void seed_random(void)
+{
+ struct timeval tv;
+ unsigned int seed;
+ FILE *fd;
+
+ if ((fd = fopen("/dev/urandom", "r"))) {
+ fread(&seed, sizeof seed, 1, fd);
+ fclose(fd);
+ } else {
+ gettimeofday(&tv, NULL);
+ /* NOTE: intentional use of uninitialized variable */
+ seed ^= (getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec;
+ }
+ srandom(seed);
+}
+
#ifdef TEST_PROGRAM
int main(int argc __attribute__ ((__unused__)),
char *argv[] __attribute__ ((__unused__)))
--
1.8.1