X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Frrd_create.c;h=9ee68961b4ad6e3755fb1bf56f7d9ab85554bc1d;hb=eaf3af3f1af054366dbdf3eb522f5ef270759604;hp=a8f6a2967fb1abaf8740e1559008b7a191f3b739;hpb=3d99d8360586a320cb4e349c0a3c5c2f13b4169c;p=rrdtool.git diff --git a/src/rrd_create.c b/src/rrd_create.c index a8f6a29..9ee6896 100644 --- a/src/rrd_create.c +++ b/src/rrd_create.c @@ -1,9 +1,11 @@ /***************************************************************************** - * RRDtool 1.2.99907080300 Copyright by Tobi Oetiker, 1997-2007 + * RRDtool 1.3.2 Copyright by Tobi Oetiker, 1997-2008 ***************************************************************************** * rrd_create.c creates new rrds *****************************************************************************/ +#include +#include #include #include "rrd_tool.h" @@ -22,6 +24,11 @@ void parseGENERIC_DS( const char *def, rrd_t *rrd, int ds_idx); +long int rra_random_row( + rra_def_t *); + +static void rrd_free2( + rrd_t *rrd); /* our onwn copy, immmune to mmap */ int rrd_create( int argc, @@ -36,7 +43,7 @@ int rrd_create( int opt; time_t last_up = time(NULL) - 10; unsigned long pdp_step = 300; - struct rrd_time_value last_up_tv; + rrd_time_value_t last_up_tv; char *parsetime_error = NULL; long long_tmp; int rc; @@ -52,7 +59,7 @@ int rrd_create( switch (opt) { case 'b': - if ((parsetime_error = parsetime(optarg, &last_up_tv))) { + if ((parsetime_error = rrd_parsetime(optarg, &last_up_tv))) { rrd_set_error("start time: %s", parsetime_error); return (-1); } @@ -121,15 +128,14 @@ int rrd_create_r( /* static header */ if ((rrd.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) { rrd_set_error("allocating rrd.stat_head"); - free(rrd.stat_head); + rrd_free2(&rrd); return (-1); } /* live header */ if ((rrd.live_head = calloc(1, sizeof(live_head_t))) == NULL) { rrd_set_error("allocating rrd.live_head"); - free(rrd.stat_head); - free(rrd.live_head); + rrd_free2(&rrd); return (-1); } @@ -162,8 +168,7 @@ int rrd_create_r( old_size + sizeof(ds_def_t))) == NULL) { rrd_set_error("allocating rrd.ds_def"); - free(rrd.stat_head); - free(rrd.live_head); + rrd_free2(&rrd); return (-1); } memset(&rrd.ds_def[rrd.stat_head->ds_cnt], 0, sizeof(ds_def_t)); @@ -195,8 +200,7 @@ int rrd_create_r( rrd_set_error("invalid DS format"); } if (rrd_test_error()) { - free(rrd.stat_head); - free(rrd.live_head); + rrd_free2(&rrd); return -1; } @@ -219,8 +223,7 @@ int rrd_create_r( } if (rrd_test_error()) { - free(rrd.stat_head); - free(rrd.live_head); + rrd_free2(&rrd); return -1; } rrd.stat_head->ds_cnt++; @@ -228,13 +231,13 @@ int rrd_create_r( char *argvcopy; char *tokptr; size_t old_size = sizeof(rra_def_t) * (rrd.stat_head->rra_cnt); + int row_cnt; if ((rrd.rra_def = rrd_realloc(rrd.rra_def, old_size + sizeof(rra_def_t))) == NULL) { rrd_set_error("allocating rrd.rra_def"); - free(rrd.stat_head); - free(rrd.live_head); + rrd_free2(&rrd); return (-1); } memset(&rrd.rra_def[rrd.stat_head->rra_cnt], 0, @@ -308,8 +311,10 @@ int rrd_create_r( case CF_SEASONAL: case CF_DEVPREDICT: case CF_FAILURES: - rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = - atoi(token); + row_cnt = atoi(token); + if (row_cnt <= 0) + rrd_set_error("Invalid row count: %i", row_cnt); + rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = row_cnt; break; default: rrd.rra_def[rrd.stat_head->rra_cnt]. @@ -366,6 +371,8 @@ int rrd_create_r( default: rrd.rra_def[rrd.stat_head->rra_cnt].pdp_cnt = atoi(token); + if (atoi(token) < 1) + rrd_set_error("Invalid step: must be >= 1"); break; } break; @@ -412,8 +419,10 @@ int rrd_create_r( ("Unexpected extra argument for consolidation function DEVPREDICT"); break; default: - rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = - atoi(token); + row_cnt = atoi(token); + if (row_cnt <= 0) + rrd_set_error("Invalid row count: %i", row_cnt); + rrd.rra_def[rrd.stat_head->rra_cnt].row_cnt = row_cnt; break; } break; @@ -485,8 +494,7 @@ int rrd_create_r( if (rrd_test_error()) { /* all errors are unrecoverable */ free(argvcopy); - free(rrd.stat_head); - free(rrd.live_head); + rrd_free2(&rrd); return (-1); } token = strtok_r(NULL, ":", &tokptr); @@ -513,16 +521,14 @@ int rrd_create_r( if (create_hw_contingent_rras(&rrd, period, hashed_name) == -1) { rrd_set_error("creating contingent RRA"); - free(rrd.stat_head); - free(rrd.live_head); + rrd_free2(&rrd); return -1; } } rrd.stat_head->rra_cnt++; } else { rrd_set_error("can't parse argument '%s'", argv[i]); - free(rrd.stat_head); - free(rrd.live_head); + rrd_free2(&rrd); return -1; } } @@ -530,15 +536,13 @@ int rrd_create_r( if (rrd.stat_head->rra_cnt < 1) { rrd_set_error("you must define at least one Round Robin Archive"); - free(rrd.stat_head); - free(rrd.live_head); + rrd_free2(&rrd); return (-1); } if (rrd.stat_head->ds_cnt < 1) { rrd_set_error("you must define at least one Data Source"); - free(rrd.stat_head); - free(rrd.live_head); + rrd_free2(&rrd); return (-1); } return rrd_create_fn(filename, &rrd); @@ -607,6 +611,7 @@ int create_hw_contingent_rras( if ((rrd->rra_def = rrd_realloc(rrd->rra_def, old_size + 4 * sizeof(rra_def_t))) == NULL) { + rrd_free2(rrd); rrd_set_error("allocating rrd.rra_def"); return (-1); } @@ -671,11 +676,15 @@ int rrd_create_fn( int unkn_cnt; rrd_file_t *rrd_file_dn; rrd_t rrd_dn; + unsigned flags = O_WRONLY | O_CREAT | O_TRUNC; + +#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) + flags |= O_BINARY; +#endif - if ((rrd_file = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { + if ((rrd_file = open(file_name, flags, 0666)) < 0) { rrd_set_error("creating '%s': %s", file_name, rrd_strerror(errno)); - free(rrd->stat_head); - free(rrd->live_head); + rrd_free2(rrd); return (-1); } @@ -690,13 +699,12 @@ int rrd_create_fn( if ((rrd->pdp_prep = calloc(1, sizeof(pdp_prep_t))) == NULL) { rrd_set_error("allocating pdp_prep"); - free(rrd->stat_head); - free(rrd->live_head); + rrd_free2(rrd); close(rrd_file); return (-1); } - strcpy(rrd->pdp_prep->last_ds, "UNKN"); + strcpy(rrd->pdp_prep->last_ds, "U"); rrd->pdp_prep->scratch[PDP_val].u_val = 0.0; rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt = @@ -707,8 +715,7 @@ int rrd_create_fn( if ((rrd->cdp_prep = calloc(1, sizeof(cdp_prep_t))) == NULL) { rrd_set_error("allocating cdp_prep"); - free(rrd->stat_head); - free(rrd->live_head); + rrd_free2(rrd); close(rrd_file); return (-1); } @@ -755,8 +762,7 @@ int rrd_create_fn( if ((rrd->rra_ptr = calloc(1, sizeof(rra_ptr_t))) == NULL) { rrd_set_error("allocating rra_ptr"); - free(rrd->stat_head); - free(rrd->live_head); + rrd_free2(rrd); close(rrd_file); return (-1); } @@ -766,15 +772,14 @@ int rrd_create_fn( * would occur for cur_row = 1 because rrd_update increments * the pointer a priori. */ for (i = 0; i < rrd->stat_head->rra_cnt; i++) { - rrd->rra_ptr->cur_row = rrd->rra_def[i].row_cnt - 1; + rrd->rra_ptr->cur_row = rra_random_row(&rrd->rra_def[i]); write(rrd_file, rrd->rra_ptr, sizeof(rra_ptr_t)); } /* write the empty data area */ if ((unknown = (rrd_value_t *) malloc(512 * sizeof(rrd_value_t))) == NULL) { rrd_set_error("allocating unknown"); - free(rrd->stat_head); - free(rrd->live_head); + rrd_free2(rrd); close(rrd_file); return (-1); } @@ -792,8 +797,7 @@ int rrd_create_fn( } free(unknown); fdatasync(rrd_file); - free(rrd->stat_head); - free(rrd->live_head); + rrd_free2(rrd); if (close(rrd_file) == -1) { rrd_set_error("creating rrd: %s", rrd_strerror(errno)); return -1; @@ -805,3 +809,30 @@ int rrd_create_fn( rrd_close(rrd_file_dn); return (0); } + + +static void rrd_free2( + rrd_t *rrd) +{ + free(rrd->live_head); + free(rrd->stat_head); + free(rrd->ds_def); + free(rrd->rra_def); + free(rrd->rra_ptr); + free(rrd->pdp_prep); + free(rrd->cdp_prep); + free(rrd->rrd_value); +} + +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; +}