}
}
if (optind == argc) {
- rrd_set_error("what is the name of the rrd file you want to create?");
+ rrd_set_error("need name of an rrd file to create");
return -1;
}
rc = rrd_create_r(argv[optind],
}
fprintf(out_file, "</rrd>\n");
rrd_free(&rrd);
- close(rrd_file->fd);
if (out_file != stdout) {
fclose(out_file);
}
- return (0);
+ return rrd_close(rrd_file);
}
cf = argv[optind + 1];
if (rrd_fetch_r(argv[optind], cf, start, end, step, ds_cnt, ds_namv, data)
- == -1)
+ != 0)
return (-1);
return (0);
}
rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
if (rrd_file == NULL)
- return (-1);
+ goto err_free;
/* when was the really last update of this file ? */
if (((*ds_namv) =
(char **) malloc(rrd.stat_head->ds_cnt * sizeof(char *))) == NULL) {
rrd_set_error("malloc fetch ds_namv array");
- rrd_free(&rrd);
- close(rrd_file->fd);
- return (-1);
+ goto err_close;
}
for (i = 0; (unsigned long) i < rrd.stat_head->ds_cnt; i++) {
if ((((*ds_namv)[i]) = malloc(sizeof(char) * DS_NAM_SIZE)) == NULL) {
rrd_set_error("malloc fetch ds_namv entry");
- rrd_free(&rrd);
- free(*ds_namv);
- close(rrd_file->fd);
- return (-1);
+ goto err_free_ds_namv;
}
strncpy((*ds_namv)[i], rrd.ds_def[i].ds_nam, DS_NAM_SIZE - 1);
(*ds_namv)[i][DS_NAM_SIZE - 1] = '\0';
best_full_rra = i;
#ifdef DEBUG
fprintf(stderr, "best full match so far\n");
-#endif
} else {
-#ifdef DEBUG
fprintf(stderr, "full match, not best\n");
#endif
}
else {
rrd_set_error
("the RRD does not contain an RRA matching the chosen CF");
- rrd_free(&rrd);
- close(rrd_file->fd);
- return (-1);
+ goto err_free_all_ds_namv;
}
/* set the wish parameters to their real values */
*ds_cnt = rrd.stat_head->ds_cnt;
if (((*data) = malloc(*ds_cnt * rows * sizeof(rrd_value_t))) == NULL) {
rrd_set_error("malloc fetch data area");
- for (i = 0; (unsigned long) i < *ds_cnt; i++)
- free((*ds_namv)[i]);
- free(*ds_namv);
- rrd_free(&rrd);
- close(rrd_file->fd);
- return (-1);
+ goto err_free_all_ds_namv;
}
data_ptr = (*data);
* sizeof(rrd_value_t))),
SEEK_SET) != 0) {
rrd_set_error("seek error in RRA");
- for (i = 0; (unsigned) i < *ds_cnt; i++)
- free((*ds_namv)[i]);
- free(*ds_namv);
- rrd_free(&rrd);
- free(*data);
- *data = NULL;
- close(rrd_file->fd);
- return (-1);
-
+ goto err_free_data;
}
#ifdef DEBUG
fprintf(stderr, "First Seek: rra_base %lu rra_pointer %lu\n",
* sizeof(rrd_value_t)),
SEEK_SET) != 0) {
rrd_set_error("wrap seek in RRA did fail");
- for (ii = 0; (unsigned) ii < *ds_cnt; ii++)
- free((*ds_namv)[ii]);
- free(*ds_namv);
- rrd_free(&rrd);
- free(*data);
- *data = NULL;
- close(rrd_file->fd);
- return (-1);
+ goto err_free_data;
}
#ifdef DEBUG
fprintf(stderr, "wrap seek ...\n");
if (rrd_read(rrd_file, data_ptr, sizeof(rrd_value_t) * (*ds_cnt))
!= (ssize_t) (sizeof(rrd_value_t) * (*ds_cnt))) {
rrd_set_error("fetching cdp from rra");
- for (ii = 0; (unsigned) ii < *ds_cnt; ii++)
- free((*ds_namv)[ii]);
- free(*ds_namv);
- rrd_free(&rrd);
- free(*data);
- *data = NULL;
- close(rrd_file->fd);
- return (-1);
+ goto err_free_data;
}
#ifdef HAVE_POSIX_FADVISE
/* don't pollute the buffer cache with data read from the file. We do this while reading to
POSIX_FADV_DONTNEED)) {
rrd_set_error("setting POSIX_FADV_DONTNEED on '%s': %s",
filename, rrd_strerror(errno));
- close(rrd_file->fd);
- return (-1);
+ goto err_close;/*XXX: should use err_free_all_ds_namv */
}
#endif
#endif
}
- rrd_free(&rrd);
#ifdef HAVE_POSIX_FADVISE
/* and just to be sure we drop everything except the header at the end */
if (0 !=
POSIX_FADV_DONTNEED)) {
rrd_set_error("setting POSIX_FADV_DONTNEED on '%s': %s", filename,
rrd_strerror(errno));
- close(rrd_file->fd);
- return (-1);
+ goto err_free; /*XXX: should use err_free_all_ds_namv */
}
#endif
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return (0);
+err_free_data:
+ free(*data);
+ *data = NULL;
+err_free_all_ds_namv:
+ for (i = 0; (unsigned long)i < rrd.stat_head->ds_cnt; ++i)
+ free((*ds_namv)[i]);
+err_free_ds_namv:
+ free(*ds_namv);
+err_close:
+ rrd_close(rrd_file);
+err_free:
+ rrd_free(&rrd);
+ return (-1);
}
const int rraindex)
{
off_t rra_start, timer;
- time_t then;
+ time_t then = -1;
rrd_t rrd;
rrd_file_t *rrd_file;
rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
if (rrd_file == NULL) {
- rrd_set_error("could not open RRD");
- return (-1);
+ goto err_free;
}
if ((rraindex < 0) || (rraindex >= (int) rrd.stat_head->rra_cnt)) {
rrd_set_error("invalid rraindex number");
- rrd_free(&rrd);
- close(rrd_file->fd);
- return (-1);
+ goto err_close;
}
rra_start = rrd_file->header_len;
rrd.live_head->last_up %
(rrd.rra_def[rraindex].pdp_cnt * rrd.stat_head->pdp_step)) +
(timer * rrd.rra_def[rraindex].pdp_cnt * rrd.stat_head->pdp_step);
-
- rrd_free(&rrd);
- close(rrd_file->fd);
+err_close:
rrd_close(rrd_file);
+err_free:
+ rrd_free(&rrd);
return (then);
}
{
unsigned int i, ii = 0;
rrd_t rrd;
- info_t *data, *cd;
+ info_t *data = NULL, *cd;
infoval info;
rrd_file_t *rrd_file;
enum cf_en current_cf;
enum dst_en current_ds;
rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
- if (rrd_file == NULL) {
- return (NULL);
- }
- close(rrd_file->fd);
+ if (rrd_file == NULL)
+ goto err_free;
info.u_str = filename;
cd = info_push(NULL, sprintf_alloc("filename"), RD_I_STR, info);
}
}
}
- rrd_free(&rrd);
+
rrd_close(rrd_file);
+err_free:
+ rrd_free(&rrd);
return (data);
-
}
time_t rrd_last_r(
const char *filename)
{
- time_t lastup;
+ time_t lastup = -1;
rrd_file_t *rrd_file;
rrd_t rrd;
rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
- if (rrd_file == NULL)
- return (-1);
-
- lastup = rrd.live_head->last_up;
+ if (rrd_file != NULL) {
+ lastup = rrd.live_head->last_up;
+ rrd_close(rrd_file);
+ }
rrd_free(&rrd);
- close(rrd_file->fd);
- rrd_close(rrd_file);
return (lastup);
}
if (argc < 2) {
rrd_set_error("please specify an rrd");
- return -1;
+ goto err_out;
}
filename = argv[1];
rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
if (rrd_file == NULL)
- return (-1);
+ goto err_free;
*last_update = rrd.live_head->last_up;
*ds_cnt = rrd.stat_head->ds_cnt;
if (((*ds_namv) =
(char **) malloc(rrd.stat_head->ds_cnt * sizeof(char *))) == NULL) {
rrd_set_error("malloc fetch ds_namv array");
- rrd_free(&rrd);
- return (-1);
+ goto err_close;
}
if (((*last_ds) =
(char **) malloc(rrd.stat_head->ds_cnt * sizeof(char *))) == NULL) {
rrd_set_error("malloc fetch last_ds array");
- rrd_free(&rrd);
- free(*ds_namv);
- return (-1);
+ goto err_free_ds_namv;
}
for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
}
rrd_free(&rrd);
- close(rrd_file->fd);
rrd_close(rrd_file);
return (0);
+
+err_free_ds_namv:
+ free(*ds_namv);
+err_close:
+ rrd_close(rrd_file);
+err_free:
+ rrd_free(&rrd);
+err_out:
+ return (-1);
}
/* DEBUG 2 prints information obtained via mincore(2) */
// #define DEBUG 2
-/* do not calculate exact madvise hints but assume 2 pages for headers and
+/* do not calculate exact madvise hints but assume 1 page for headers and
* set DONTNEED for the rest, which is assumed to be data */
-//#define TWO_PAGES 1
+//#define ONE_PAGE 1
/* Avoid calling madvise on areas that were already hinted. May be benefical if
* your syscalls are very slow */
//#define CHECK_MADVISE_OVERLAPS 1
madvise((_start), (_off), (_hint))
#endif
-/* open a database file, return its header and an open filehandle */
-/* positioned to the first cdp in the first rra */
+/* 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
+ * before returning an error. Do not call rrd_close upon failure of rrd_open.
+ */
rrd_file_t *rrd_open(
const char *const file_name,
#endif
off_t offset = 0;
struct stat statb;
- rrd_file_t *rrd_file = malloc(sizeof(rrd_file_t));
+ rrd_file_t *rrd_file = NULL;
+ rrd_init(rrd);
+ rrd_file = malloc(sizeof(rrd_file_t));
if (rrd_file == NULL) {
rrd_set_error("allocating rrd_file descriptor for '%s'", file_name);
return NULL;
}
memset(rrd_file, 0, sizeof(rrd_file_t));
- rrd_init(rrd);
+
#ifdef DEBUG
if ((rdwr & (RRD_READONLY | RRD_READWRITE)) ==
(RRD_READONLY | RRD_READWRITE)) {
goto out_done;
}
/* We do not need to read anything in for the moment */
-#ifndef TWO_PAGES
+#ifndef ONE_PAGE
_madvise(data, rrd_file->file_len, MADV_DONTNEED);
// _madvise(data, rrd_file->file_len, MADV_RANDOM);
#else
/* alternatively: keep 2 pages worth of data, likely headers,
* don't need the rest. */
- _madvise(data, _page_size * 2, MADV_WILLNEED | MADV_SEQUENTIAL);
- _madvise(data + _page_size * 2, (rrd_file->file_len >= _page_size * 2)
- ? rrd_file->file_len - _page_size * 2 : 0, MADV_DONTNEED);
+ _madvise(data, _page_size, MADV_WILLNEED | MADV_SEQUENTIAL);
+ _madvise(data + _page_size, (rrd_file->file_len >= _page_size)
+ ? rrd_file->file_len - _page_size : 0, MADV_DONTNEED);
#endif
#endif
-#if defined USE_MADVISE && !defined TWO_PAGES
+#if defined USE_MADVISE && !defined ONE_PAGE
/* the stat_head will be needed soonish, so hint accordingly */
// too finegrained to calc the individual sizes, just keep 2 pages worth of hdr
_madvise(data + PAGE_ALIGN_DOWN(offset), PAGE_ALIGN(sizeof(stat_head_t)),
rrd->stat_head->version);
goto out_nullify_head;
}
-#if defined USE_MADVISE && !defined TWO_PAGES
+#if defined USE_MADVISE && !defined ONE_PAGE
/* the ds_def will be needed soonish, so hint accordingly */
_madvise(data + PAGE_ALIGN_DOWN(offset),
PAGE_ALIGN(sizeof(ds_def_t) * rrd->stat_head->ds_cnt),
__rrd_read(rrd->ds_def, ds_def_t,
rrd->stat_head->ds_cnt);
-#if defined USE_MADVISE && !defined TWO_PAGES
+#if defined USE_MADVISE && !defined ONE_PAGE
/* the rra_def will be needed soonish, so hint accordingly */
_madvise(data + PAGE_ALIGN_DOWN(offset),
PAGE_ALIGN(sizeof(rra_def_t) * rrd->stat_head->rra_cnt),
#endif
rrd->live_head->last_up_usec = 0;
} else {
-#if defined USE_MADVISE && !defined TWO_PAGES
+#if defined USE_MADVISE && !defined ONE_PAGE
/* the live_head will be needed soonish, so hint accordingly */
_madvise(data + PAGE_ALIGN_DOWN(offset),
PAGE_ALIGN(sizeof(live_head_t)), MADV_WILLNEED);
{
int ret;
-#if defined HAVE_MMAP
+#if defined HAVE_MMAP || defined DEBUG
ssize_t _page_size = sysconf(_SC_PAGESIZE);
#endif
#if defined DEBUG && DEBUG > 1
off_t off;
unsigned char *vec;
- off =
- rrd_file->file_len +
- ((rrd_file->file_len + sysconf(_SC_PAGESIZE) -
- 1) / sysconf(_SC_PAGESIZE));
+ off = rrd_file->file_len +
+ ((rrd_file->file_len + _page_size - 1) / _page_size);
vec = malloc(off);
if (vec != NULL) {
memset(vec, 0, off);
#endif /* DEBUG */
#ifdef USE_MADVISE
-#ifdef TWO_PAGES
-//XXX: ?
- /* Keep 2 pages worth of headers around, round up to next page boundary. */
+#ifdef ONE_PAGE
+ /* Keep headers around, round up to next page boundary. */
ret =
PAGE_ALIGN(rrd_file->header_len % _page_size + rrd_file->header_len);
if (rrd_file->file_len > ret)
ret = munmap(rrd_file->file_start, rrd_file->file_len);
if (ret != 0)
rrd_set_error("munmap rrd_file: %s", rrd_strerror(errno));
-#else
- ret = 0;
#endif
-// ret = close(rrd_file->fd);
-// if (ret != 0)
-// rrd_set_error("closing file: %s", rrd_strerror(errno));
+ ret = close(rrd_file->fd);
+ if (ret != 0)
+ rrd_set_error("closing file: %s", rrd_strerror(errno));
free(rrd_file);
rrd_file = NULL;
return ret;
/* free RRD header data. */
+#ifdef HAVE_MMAP
+inline void rrd_free(
+ rrd_t UNUSED(*rrd)) {}
+#else
void rrd_free(
- rrd_t UNUSED(*rrd))
+ rrd_t *rrd)
{
-#ifndef HAVE_MMAP
if (atoi(rrd->stat_head->version) < 3)
free(rrd->live_head);
free(rrd->stat_head);
free(rrd->pdp_prep);
free(rrd->cdp_prep);
free(rrd->rrd_value);
-#endif
}
+#endif
/* routine used by external libraries to free memory allocated by
rrd_file = rrd_open(infilename, &rrdold, RRD_READWRITE | RRD_COPY);
if (rrd_file == NULL) {
- rrd_set_error("could not open RRD");
+ rrd_free(&rrdold);
return (-1);
}
if (LockRRD(rrd_file->fd) != 0) {
rrd_set_error("could not lock original RRD");
rrd_free(&rrdold);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return (-1);
}
if (target_rra >= rrdold.stat_head->rra_cnt) {
rrd_set_error("no such RRA in this RRD");
rrd_free(&rrdold);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return (-1);
}
if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
rrd_set_error("This RRA is not that big");
rrd_free(&rrdold);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return (-1);
}
if (rrd_out_file == NULL) {
rrd_set_error("Can't create '%s': %s", outfilename,
rrd_strerror(errno));
+ rrd_free(&rrdnew);
return (-1);
}
if (LockRRD(rrd_out_file->fd) != 0) {
rrd_set_error("could not lock new RRD");
rrd_free(&rrdold);
- close(rrd_file->fd);
- close(rrd_out_file->fd);
+ rrd_close(rrd_file);
+ rrd_close(rrd_out_file);
return (-1);
}
/*XXX: do one write for those parts of header that are unchanged */
sizeof(rra_ptr_t) * rrdnew.stat_head->rra_cnt);
rrd_free(&rrdold);
- close(rrd_file->fd);
rrd_close(rrd_file);
rrd_free(&rrdnew);
- close(rrd_out_file->fd);
rrd_close(rrd_out_file);
return (0);
rrd_file = rrd_open(argv[1], &rrd, RRD_READWRITE);
if (rrd_file == NULL) {
+ rrd_free(&rrd);
return -1;
}
&heartbeat)) != 2) {
rrd_set_error("invalid arguments for heartbeat");
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
if ((ds = ds_match(&rrd, ds_nam)) == -1) {
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
rrd.ds_def[ds].par[DS_mrhb_cnt].u_cnt = heartbeat;
sscanf(optarg, DS_NAM_FMT ":%lf", ds_nam, &min)) < 1) {
rrd_set_error("invalid arguments for minimum ds value");
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
if ((ds = ds_match(&rrd, ds_nam)) == -1) {
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
sscanf(optarg, DS_NAM_FMT ":%lf", ds_nam, &max)) < 1) {
rrd_set_error("invalid arguments for maximum ds value");
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
if ((ds = ds_match(&rrd, ds_nam)) == -1) {
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
if (matches == 1)
sscanf(optarg, DS_NAM_FMT ":" DST_FMT, ds_nam, dst)) != 2) {
rrd_set_error("invalid arguments for data source type");
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
if ((ds = ds_match(&rrd, ds_nam)) == -1) {
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
if ((int) dst_conv(dst) == -1) {
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
strncpy(rrd.ds_def[ds].dst, dst, DST_SIZE - 1);
ds_new)) != 2) {
rrd_set_error("invalid arguments for data source type");
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
if ((ds = ds_match(&rrd, ds_nam)) == -1) {
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
strncpy(rrd.ds_def[ds].ds_nam, ds_new, DS_NAM_SIZE - 1);
if (sscanf(optarg, DS_NAM_FMT, ds_nam) != 1) {
rrd_set_error("invalid argument for aberrant-reset");
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
if ((ds = ds_match(&rrd, ds_nam)) == -1) {
/* ds_match handles it own errors */
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
reset_aberrant_coefficients(&rrd, rrd_file, (unsigned long) ds);
if (rrd_test_error()) {
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
break;
else
rrd_set_error("unknown option '%s'", argv[optind - 1]);
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return -1;
}
}
free(buffer);
}
}
- close(rrd_file->fd);
+ rrd_close(rrd_file);
rrd_free(&rrd);
return 0;
}
rrd_file = rrd_open(filename, &rrd, RRD_READWRITE);
if (rrd_file == NULL) {
- goto err_out;
+ goto err_free;
}
/* initialize time */
free(pdp_temp);
free(tmpl_idx);
rrd_free(&rrd);
- close(rrd_file->fd);
+ rrd_close(rrd_file);
return (-1);
}
#ifdef USE_MADVISE
}
rrd_free(&rrd);
- close(rrd_file->fd);
rrd_close(rrd_file);
free(pdp_new);
err_free_updvals:
free(updvals);
err_close:
- rrd_free(&rrd);
- close(rrd_file->fd);
rrd_close(rrd_file);
+ err_free:
+ rrd_free(&rrd);
err_out:
return (-1);
}