X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_create.c;h=52a099b3cc73b55fc0bd36228f04e8631cebee82;hp=027c6abd1bc10c5fd40d015686e0bea3b6d461fa;hb=56d67cdd0c5b2c27c9242a3d5810c7184917f663;hpb=f4fd83170248b7945de8105a0ecbe13e11dbbcfe diff --git a/src/rrd_create.c b/src/rrd_create.c index 027c6ab..52a099b 100644 --- a/src/rrd_create.c +++ b/src/rrd_create.c @@ -1,5 +1,5 @@ /***************************************************************************** - * RRDtool 1.2.99907080300 Copyright by Tobi Oetiker, 1997-2007 + * RRDtool 1.4.0 Copyright by Tobi Oetiker, 1997-2009 ***************************************************************************** * rrd_create.c creates new rrds *****************************************************************************/ @@ -14,6 +14,10 @@ #include "rrd_is_thread_safe.h" +#ifdef WIN32 +# include +#endif + unsigned long FnvHash( const char *str); int create_hw_contingent_rras( @@ -24,8 +28,9 @@ 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, @@ -40,7 +45,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; @@ -56,7 +61,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); } @@ -123,17 +128,16 @@ int rrd_create_r( /* init rrd clean */ rrd_init(&rrd); /* static header */ - if ((rrd.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) { + if ((rrd.stat_head = (stat_head_t*)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) { + if ((rrd.live_head = (live_head_t*)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,12 +166,11 @@ int rrd_create_r( if (strncmp(argv[i], "DS:", 3) == 0) { size_t old_size = sizeof(ds_def_t) * (rrd.stat_head->ds_cnt); - if ((rrd.ds_def = rrd_realloc(rrd.ds_def, + if ((rrd.ds_def = (ds_def_t*)rrd_realloc(rrd.ds_def, 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)); @@ -199,8 +202,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; } @@ -223,22 +225,21 @@ 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++; } else if (strncmp(argv[i], "RRA:", 4) == 0) { char *argvcopy; - char *tokptr; + 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, + if ((rrd.rra_def = (rra_def_t*)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, @@ -312,8 +313,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]. @@ -370,6 +373,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; @@ -416,8 +421,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; @@ -489,8 +496,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); @@ -517,16 +523,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; } } @@ -534,15 +538,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); @@ -608,9 +610,10 @@ int create_hw_contingent_rras( (rrd->stat_head->rra_cnt)++; /* allocate the memory for the 4 contingent RRAs */ old_size = sizeof(rra_def_t) * (rrd->stat_head->rra_cnt); - if ((rrd->rra_def = rrd_realloc(rrd->rra_def, + if ((rrd->rra_def = (rra_def_t*)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); } @@ -670,50 +673,51 @@ int rrd_create_fn( rrd_t *rrd) { unsigned long i, ii; - int rrd_file; rrd_value_t *unknown; int unkn_cnt; rrd_file_t *rrd_file_dn; rrd_t rrd_dn; + unsigned rrd_flags = RRD_READWRITE | RRD_CREAT; - if ((rrd_file = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { + unkn_cnt = 0; + for (i = 0; i < rrd->stat_head->rra_cnt; i++) + unkn_cnt += rrd->stat_head->ds_cnt * rrd->rra_def[i].row_cnt; + + if ((rrd_file_dn = rrd_open(file_name, rrd, rrd_flags)) == NULL) { rrd_set_error("creating '%s': %s", file_name, rrd_strerror(errno)); - free(rrd->stat_head); - free(rrd->live_head); + rrd_free2(rrd); return (-1); } - write(rrd_file, rrd->stat_head, sizeof(stat_head_t)); + rrd_write(rrd_file_dn, rrd->stat_head, sizeof(stat_head_t)); - write(rrd_file, rrd->ds_def, sizeof(ds_def_t) * rrd->stat_head->ds_cnt); + rrd_write(rrd_file_dn, rrd->ds_def, sizeof(ds_def_t) * rrd->stat_head->ds_cnt); - write(rrd_file, rrd->rra_def, + rrd_write(rrd_file_dn, rrd->rra_def, sizeof(rra_def_t) * rrd->stat_head->rra_cnt); - write(rrd_file, rrd->live_head, sizeof(live_head_t)); + rrd_write(rrd_file_dn, rrd->live_head, sizeof(live_head_t)); - if ((rrd->pdp_prep = calloc(1, sizeof(pdp_prep_t))) == NULL) { + if ((rrd->pdp_prep = (pdp_prep_t*)calloc(1, sizeof(pdp_prep_t))) == NULL) { rrd_set_error("allocating pdp_prep"); - free(rrd->stat_head); - free(rrd->live_head); - close(rrd_file); + rrd_free2(rrd); + rrd_close(rrd_file_dn); 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 = rrd->live_head->last_up % rrd->stat_head->pdp_step; for (i = 0; i < rrd->stat_head->ds_cnt; i++) - write(rrd_file, rrd->pdp_prep, sizeof(pdp_prep_t)); + rrd_write(rrd_file_dn, rrd->pdp_prep, sizeof(pdp_prep_t)); - if ((rrd->cdp_prep = calloc(1, sizeof(cdp_prep_t))) == NULL) { + if ((rrd->cdp_prep = (cdp_prep_t*)calloc(1, sizeof(cdp_prep_t))) == NULL) { rrd_set_error("allocating cdp_prep"); - free(rrd->stat_head); - free(rrd->live_head); - close(rrd_file); + rrd_free2(rrd); + rrd_close(rrd_file_dn); return (-1); } @@ -750,18 +754,17 @@ int rrd_create_fn( } for (ii = 0; ii < rrd->stat_head->ds_cnt; ii++) { - write(rrd_file, rrd->cdp_prep, sizeof(cdp_prep_t)); + rrd_write(rrd_file_dn, rrd->cdp_prep, sizeof(cdp_prep_t)); } } /* now, we must make sure that the rest of the rrd struct is properly initialized */ - if ((rrd->rra_ptr = calloc(1, sizeof(rra_ptr_t))) == NULL) { + if ((rrd->rra_ptr = (rra_ptr_t*)calloc(1, sizeof(rra_ptr_t))) == NULL) { rrd_set_error("allocating rra_ptr"); - free(rrd->stat_head); - free(rrd->live_head); - close(rrd_file); + rrd_free2(rrd); + rrd_close(rrd_file_dn); return (-1); } @@ -770,56 +773,57 @@ 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 = rra_random_row(&rrd->rra_def[i]); - write(rrd_file, rrd->rra_ptr, sizeof(rra_ptr_t)); + rrd->rra_ptr->cur_row = rrd_select_initial_row(rrd_file_dn, i, &rrd->rra_def[i]); + rrd_write(rrd_file_dn, 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); - close(rrd_file); + rrd_free2(rrd); + rrd_close(rrd_file_dn); return (-1); } for (i = 0; i < 512; ++i) unknown[i] = DNAN; - unkn_cnt = 0; - for (i = 0; i < rrd->stat_head->rra_cnt; i++) - unkn_cnt += rrd->stat_head->ds_cnt * rrd->rra_def[i].row_cnt; - while (unkn_cnt > 0) { - write(rrd_file, unknown, sizeof(rrd_value_t) * min(unkn_cnt, 512)); + if(rrd_write(rrd_file_dn, unknown, sizeof(rrd_value_t) * min(unkn_cnt, 512)) < 0) + { + rrd_set_error("creating rrd: %s", rrd_strerror(errno)); + return -1; + } unkn_cnt -= 512; } free(unknown); - fdatasync(rrd_file); - free(rrd->stat_head); - free(rrd->live_head); - if (close(rrd_file) == -1) { + rrd_free2(rrd); + if (rrd_close(rrd_file_dn) == -1) { rrd_set_error("creating rrd: %s", rrd_strerror(errno)); return -1; } /* flush all we don't need out of the cache */ - rrd_file_dn = rrd_open(file_name, &rrd_dn, RRD_READONLY); - rrd_dontneed(rrd_file_dn, &rrd_dn); - rrd_free(&rrd_dn); - rrd_close(rrd_file_dn); + rrd_init(&rrd_dn); + if((rrd_file_dn = rrd_open(file_name, &rrd_dn, RRD_READONLY)) != NULL) + { + rrd_dontneed(rrd_file_dn, &rrd_dn); + /* rrd_free(&rrd_dn); */ + rrd_close(rrd_file_dn); + } return (0); } -static int rand_init = 0; -long int -rra_random_row(rra_def_t *rra) +static void rrd_free2( + rrd_t *rrd) { - if (!rand_init) - { - srandom((unsigned int)time(NULL) + (unsigned int)getpid()); - rand_init++; - } - - return random() % rra->row_cnt; + 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); } +