From 2f0b984b0dfee0a8ab2cb1d41670f40a07ec5cdb Mon Sep 17 00:00:00 2001 From: oetiker Date: Tue, 14 Apr 2009 06:10:21 +0000 Subject: [PATCH] rrd_random() is a wrapper around random() that ensures the PRNG is seeded exactly ONCE per process. rrd_utils.c is introduced for functions that do not have a better home. --kevin git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk/program@1789 a5681a0c-68f1-0310-ab6d-d61299d08faa --- src/Makefile.am | 1 + src/librrd.sym.in.in | 1 + src/rrd.h | 4 ++++ src/rrd_daemon.c | 2 +- src/rrd_open.c | 24 +----------------------- src/rrd_restore.c | 6 +----- src/rrd_utils.c | 36 ++++++++++++++++++++++++++++++++++++ 7 files changed, 45 insertions(+), 29 deletions(-) create mode 100644 src/rrd_utils.c 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.h b/src/rrd.h index 67c213f..fd506d9 100644 --- a/src/rrd.h +++ b/src/rrd.h @@ -319,6 +319,10 @@ int rrd_proc_start_end( /* int rrd_test_error_r (rrd_context_t *); */ /* char *rrd_get_error_r (rrd_context_t *); */ +/** UTILITY FUNCTIONS */ + + long rrd_random(void); + /* * The following functions are _internal_ functions needed to read the raw RRD * files. Since they are _internal_ they may change with the file format and diff --git a/src/rrd_daemon.c b/src/rrd_daemon.c index dcca55d..7a7ae88 100644 --- a/src/rrd_daemon.c +++ b/src/rrd_daemon.c @@ -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 /* 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_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 + +#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(); +} -- 2.11.0