git » libfiu » commit 31d1f8d

preload/posix: Seed the PRNG after initialization

author Alberto Bertogli
2009-06-15 03:15:06 UTC
committer Alberto Bertogli
2009-06-16 15:42:54 UTC
parent c3cdb253b5d7a5a1c71fe3a6ce821270d2acbe42

preload/posix: Seed the PRNG after initialization

Because we use random() to choose from possible errnos, we want it to be
properly seeded.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

preload/posix/codegen.c +17 -0

diff --git a/preload/posix/codegen.c b/preload/posix/codegen.c
index 8a9c349..a267d1c 100644
--- a/preload/posix/codegen.c
+++ b/preload/posix/codegen.c
@@ -1,6 +1,7 @@
 
 #include <stdio.h>
 #include <dlfcn.h>
+#include <sys/time.h>
 #include "codegen.h"
 
 /* Dynamically load libc */
@@ -22,3 +23,19 @@ static void __attribute__((constructor(200))) _fiu_init(void)
 	printd("done\n");
 }
 
+/* this runs after all function-specific constructors */
+static void __attribute__((constructor(250))) _fiu_init_final(void)
+{
+	struct timeval tv;
+
+	rec_inc();
+
+	fiu_init(0);
+
+	/* since we use random() in the wrappers, we need to seed it */
+	gettimeofday(&tv, NULL);
+	srandom(tv.tv_usec);
+
+	rec_dec();
+}
+