Code

Fix for #167 - rrdcreate is arguably missing a check for 'step>=1' for RRAs with...
[rrdtool-all.git] / program / src / rrd_create.c
index 027132c7c78a74e58d353dd47878d1966b9bb1e1..186b3b40fa6e92818e759b0e63ecc998363327c6 100644 (file)
@@ -1,18 +1,22 @@
 /*****************************************************************************
- * RRDtool 1.2.15  Copyright by Tobi Oetiker, 1997-2006
+ * RRDtool 1.2.27  Copyright by Tobi Oetiker, 1997-2008
  *****************************************************************************
  * rrd_create.c  creates new rrds
  *****************************************************************************/
 
+#include <stdlib.h>
+#include <time.h>
+
 #include "rrd_tool.h"
 #include "rrd_rpncalc.h"
 #include "rrd_hw.h"
 
 #include "rrd_is_thread_safe.h"
 
-unsigned long FnvHash(char *str);
+unsigned long FnvHash(const char *str);
 int create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name);
-void parseGENERIC_DS(char *def,rrd_t *rrd, int ds_idx);
+void parseGENERIC_DS(const char *def,rrd_t *rrd, int ds_idx);
+long int rra_random_row(rra_def_t *);
 
 int
 rrd_create(int argc, char **argv) 
@@ -84,16 +88,16 @@ rrd_create(int argc, char **argv)
     }
     rc = rrd_create_r(argv[optind],
                      pdp_step, last_up,
-                     argc - optind - 1, argv + optind + 1);
+                     argc - optind - 1, (const char **)(argv + optind + 1));
     
     return rc;
 }
 
 /* #define DEBUG */
 int
-rrd_create_r(char *filename,
+rrd_create_r(const char *filename,
             unsigned long pdp_step, time_t last_up,
-            int argc, char **argv) 
+            int argc, const char **argv) 
 {
     rrd_t             rrd;
     long              i;
@@ -308,6 +312,8 @@ rrd_create_r(char *filename,
                         break;
                     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;
@@ -439,7 +445,7 @@ rrd_create_r(char *filename,
     return rrd_create_fn(filename, &rrd);
 }
 
-void parseGENERIC_DS(char *def,rrd_t *rrd, int ds_idx)
+void parseGENERIC_DS(const char *def,rrd_t *rrd, int ds_idx)
 {
     char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE];     
     /*
@@ -547,7 +553,7 @@ create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashe
 /* create and empty rrd file according to the specs given */
 
 int
-rrd_create_fn(char *file_name, rrd_t *rrd)
+rrd_create_fn(const char *file_name, rrd_t *rrd)
 {
     unsigned long    i,ii;
     FILE             *rrd_file;
@@ -556,12 +562,7 @@ rrd_create_fn(char *file_name, rrd_t *rrd)
     
     if ((rrd_file = fopen(file_name,"wb")) == NULL ) {
        rrd_set_error("creating '%s': %s",file_name, rrd_strerror(errno));
-       free(rrd->stat_head);
-        rrd->stat_head = NULL; 
-       free(rrd->ds_def);
-        rrd->ds_def = NULL; 
-       free(rrd->rra_def);
-        rrd->rra_def = NULL;
+        rrd_free(rrd);
        return(-1);
     }
     
@@ -655,7 +656,7 @@ rrd_create_fn(char *file_name, rrd_t *rrd)
      * 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]);
         fwrite( rrd->rra_ptr, sizeof(rra_ptr_t),1,rrd_file);
     }
     
@@ -691,3 +692,17 @@ rrd_create_fn(char *file_name, rrd_t *rrd)
     rrd_free(rrd);
     return (0);
 }
+
+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;
+}