diff options
Diffstat (limited to 'src/report.c')
-rw-r--r-- | src/report.c | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/src/report.c b/src/report.c index 71a735f..ab4cada 100644 --- a/src/report.c +++ b/src/report.c @@ -30,31 +30,41 @@ #include <arpa/inet.h> #include <tagr.h> #include <report.h> #include <fprintftime.h> #include <gdbm.h> +unsigned lock_retry_count_option = 5; +unsigned lock_retry_timeout_option = 1; + static char *dbname; static GDBM_FILE dbf; static void tagr_db_report (char *str) { logmsg (L_CRIT, "%s: %s", dbname, str); } int open_db (int flag) { - dbname = xmalloc (strlen (basedir) + 1 + sizeof (TAGR_DBNAME)); - strcpy (dbname, basedir); - strcat (dbname, "/"); - strcat (dbname, TAGR_DBNAME); - dbf = gdbm_open (dbname, 0, - flag == TAGR_DB_WR ? GDBM_WRCREAT : GDBM_READER, - TAGR_DBMODE, tagr_db_report); + unsigned i; + + dbname = mkfilename (basedir, TAGR_DBNAME, NULL); + + for (i = 0; i < lock_retry_count_option; i++) + { + dbf = gdbm_open (dbname, 0, + flag == TAGR_DB_WR ? GDBM_WRCREAT : GDBM_READER, + TAGR_DBMODE, tagr_db_report); + if (dbf || errno != EAGAIN) + break; + sleep (lock_retry_timeout_option); + } + if (dbf == NULL) { logmsg (L_ERR, _("cannot open database %s: %s"), dbname, gdbm_strerror (gdbm_errno)); return 1; } @@ -248,13 +258,13 @@ report (Stat *stat, time_t timestamp) } else logmsg (L_WARNING, _("%s not found in config"), stat->name); } int -update_monitor (datum key, time_t timestamp, int force) +update_monitor (datum key, time_t timestamp, int flags) { char id[MAX_NAME_LENGTH+1]; struct traffic_record *tr = NULL; struct monitor *mon; if (key.dsize > MAX_NAME_LENGTH) @@ -270,35 +280,34 @@ update_monitor (datum key, time_t timestamp, int force) logmsg (L_ERR, _("%s: no such monitor"), id); return 1; } _read_db (key, &tr); if (tr) { - update_output (mon, tr, tr->last.time /*FIXME: must be now? */, - force); + update_output (mon, tr, timestamp, flags); free (tr); } return 0; } void -rebuild (int force) +rebuild (int flags) { datum key; datum content; time_t now = time (NULL); - + verbose (1, _("rebuild initiated")); if (open_db (TAGR_DB_RD) == 0) { key = gdbm_firstkey (dbf); while (key.dptr) { - datum nextkey = gdbm_nextkey ( dbf, key ); - update_monitor (key, now, force); + datum nextkey = gdbm_nextkey (dbf, key); + update_monitor (key, now, flags); free (key.dptr); key = nextkey; } close_db (); } verbose (1, _("rebuild finished")); |