[rrd-developers] [PATCH] use rrd_random() for PRNG

kevin brintnall kbrint at rufus.net
Sun Apr 12 10:57:44 CEST 2009


The utility function ensures that the PRNG is seeded exactly ONCE per
process.

Created "rrd_utils.c" for utility functions that do not have a more
obvious home.

---
diff --git a/src/Makefile.am b/src/Makefile.am
index 35ed960..129e3ad 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -28,6 +28,7 @@ UPD_C_FILES =		\
 	rrd_client.c	\
 	rrd_nan_inf.c	\
 	rrd_rpncalc.c	\
+	rrd_utils.c	\
 	rrd_update.c
 
 RRD_C_FILES =		\
diff --git a/src/librrd.sym.in.in b/src/librrd.sym.in.in
index b4e8484..0a7a699 100644
--- a/src/librrd.sym.in.in
+++ b/src/librrd.sym.in.in
@@ -32,6 +32,7 @@ rrd_new_context
 rrd_open
 rrd_parsetime
 rrd_proc_start_end
+rrd_random
 rrd_read
 rrd_resize
 rrd_restore
diff --git a/src/rrd_daemon.c b/src/rrd_daemon.c
index dcca55d..b0168e1 100644
--- a/src/rrd_daemon.c
+++ b/src/rrd_daemon.c
@@ -71,7 +71,7 @@
 #endif
 #endif
 
-#include "rrd.h"
+#include "rrd_tool.h"
 #include "rrd_client.h"
 
 #include <stdlib.h>
@@ -601,7 +601,7 @@ static void wipe_ci_values(cache_item_t *ci, time_t when)
 
   ci->last_flush_time = when;
   if (config_write_jitter > 0)
-    ci->last_flush_time += (random() % config_write_jitter);
+    ci->last_flush_time += (rrd_random() % config_write_jitter);
 }
 
 /* remove_from_queue
diff --git a/src/rrd_open.c b/src/rrd_open.c
index 1654514..f74c6d8 100644
--- a/src/rrd_open.c
+++ b/src/rrd_open.c
@@ -16,10 +16,6 @@
 #define MEMBLK 8192
 
 #ifdef WIN32
-#	define random() rand()
-#	define srandom(x) srand(x)
-#	define getpid() 0
-
 #define	_LK_UNLCK	0	/* Unlock */
 #define	_LK_LOCK	1	/* Lock */
 #define	_LK_NBLCK	2	/* Non-blocking lock */
@@ -78,10 +74,6 @@
 #endif
 #endif
 
-long int  rra_random_row(
-    rra_def_t *);
-
-
 /* Open a database file, return its header and an open filehandle,
  * positioned to the first cdp in the first rra.
  * In the error path of rrd_open, only rrd_free(&rrd) has to be called
@@ -763,19 +755,5 @@ unsigned long rrd_select_initial_row(
     rra_def_t *rra
     )
 {
-    return rra_random_row(rra);
-}
-
-static int rand_init = 0;
-
-long int rra_random_row(
-    rra_def_t *rra)
-{
-    if (!rand_init) {
-        srandom((unsigned int) time(NULL) + (unsigned int) getpid());
-        rand_init++;
-    }
-
-    return random() % rra->row_cnt;
+    return rrd_random() % rra->row_cnt;
 }
-
diff --git a/src/rrd_restore.c b/src/rrd_restore.c
index e9363c6..9af8ab0 100644
--- a/src/rrd_restore.c
+++ b/src/rrd_restore.c
@@ -36,9 +36,6 @@
 #ifndef WIN32
 #	include <unistd.h>     /* for off_t */
 #else
-#	define random() rand()
-#	define srandom(x) srand(x)
-#	define getpid() 0
 	typedef size_t ssize_t;
 	typedef long off_t;
 #endif 
@@ -713,7 +710,7 @@ static int parse_tag_rra(
     }
 
     /* Set the RRA pointer to a random location */
-    cur_rra_ptr->cur_row = random() % cur_rra_def->row_cnt;
+    cur_rra_ptr->cur_row = rrd_random() % cur_rra_def->row_cnt;
 
     return (status);
 }                       /* int parse_tag_rra */
@@ -1061,7 +1058,6 @@ int rrd_restore(
 {
     rrd_t    *rrd;
 
-    srandom((unsigned int) time(NULL) + (unsigned int) getpid());
     /* init rrd clean */
     optind = 0;
     opterr = 0;         /* initialize getopt */
diff --git a/src/rrd_tool.h b/src/rrd_tool.h
index f58cd78..ddcd2a9 100644
--- a/src/rrd_tool.h
+++ b/src/rrd_tool.h
@@ -119,6 +119,8 @@ int rrd_fetch_fn_libdbi(char *filename, enum cf_en cf_idx,
     char *a,
     char *b);
 
+    long rrd_random(void);
+
 #endif /* _RRD_TOOL_H */
 
 #ifdef  __cplusplus
diff --git a/src/rrd_utils.c b/src/rrd_utils.c
new file mode 100644
index 0000000..39d2aca
--- /dev/null
+++ b/src/rrd_utils.c
@@ -0,0 +1,36 @@
+/**
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ **/
+
+#include "rrd_tool.h"
+
+#include <stdlib.h>
+
+#ifdef WIN32
+#	define random() rand()
+#	define srandom(x) srand(x)
+#	define getpid() 0
+#endif /* WIN32 */
+
+/* make sure that the random number generator seeded EXACTLY ONCE */
+long rrd_random(void)
+{
+    static int rand_init = 0;
+    if (!rand_init) {
+        srandom((unsigned int) time(NULL) + (unsigned int) getpid());
+        rand_init++;
+    }
+
+    return random();
+}



More information about the rrd-developers mailing list