Code

Fix for #167 - rrdcreate is arguably missing a check for 'step>=1' for RRAs with...
[rrdtool.git] / src / rrd_create.c
index 6f4c8043da656128e2adc2be9971c54edf6a8227..186b3b40fa6e92818e759b0e63ecc998363327c6 100644 (file)
@@ -1,9 +1,12 @@
 /*****************************************************************************
- * RRDtool 1.2.23  Copyright by Tobi Oetiker, 1997-2007
+ * 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"
@@ -13,6 +16,7 @@
 unsigned long FnvHash(const char *str);
 int create_hw_contingent_rras(rrd_t *rrd, unsigned short period, unsigned long hashed_name);
 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) 
@@ -308,6 +312,8 @@ rrd_create_r(const 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;
@@ -553,17 +559,10 @@ rrd_create_fn(const char *file_name, rrd_t *rrd)
     FILE             *rrd_file;
     rrd_value_t      *unknown;
     int        unkn_cnt;
-
-    long rrd_head_size;
-
+    
     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);
     }
     
@@ -657,11 +656,10 @@ rrd_create_fn(const 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);
     }
-    rrd_head_size = ftell(rrd_file);
-
+    
     /* write the empty data area */
     if ((unknown = (rrd_value_t *)malloc(512 * sizeof(rrd_value_t))) == NULL) {
        rrd_set_error("allocating unknown");
@@ -690,24 +688,21 @@ rrd_create_fn(const char *file_name, rrd_t *rrd)
        return(-1);
     }
     
-#ifdef HAVE_POSIX_FADVISE
-    /* this file is not going to be read again any time
-       soon, so we drop everything except the header portion from
-       the buffer cache. for this to work, we have to fdsync the file
-       first though. This will not be all that fast, but 'good' data
-       like other rrdfiles headers will stay in cache. Now this only works if creating
-       a single rrd file is not too large, but I assume this should not be the case
-       in general. Otherwhise we would have to sync and release while writing all
-       the unknown data. */
-    fdatasync(fileno(rrd_file));
-    if (0 != posix_fadvise(fileno(rrd_file), rrd_head_size, 0, POSIX_FADV_DONTNEED)) {
-        rrd_set_error("setting POSIX_FADV_DONTNEED on '%s': %s",file_name, rrd_strerror(errno));
-        fclose(rrd_file);
-        return(-1);
-    }    
-#endif
-
     fclose(rrd_file);    
     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;
+}