public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/4] lib: add function to seed random()
@ 2013-01-09 19:36 Sami Kerola
  2013-01-09 19:36 ` [PATCH 2/4] ipcmk: remove unnecessary create_key() function Sami Kerola
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Sami Kerola @ 2013-01-09 19:36 UTC (permalink / raw)
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-01-24 15:35 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-09 19:36 [PATCH 1/4] lib: add function to seed random() Sami Kerola
2013-01-09 19:36 ` [PATCH 2/4] ipcmk: remove unnecessary create_key() function Sami Kerola
2013-01-10 10:19   ` Karel Zak
2013-01-24 15:32     ` Karel Zak
2013-01-09 19:36 ` [PATCH 3/4] ipcmk: allow user to create more ipc resources at a time Sami Kerola
2013-01-10  9:25   ` Karel Zak
2013-01-09 19:36 ` [PATCH 4/4] include: add missing values to sysfs_cxt initializer definition Sami Kerola
2013-01-24 15:35   ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox