aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.c46
-rw-r--r--src/report.c26
-rw-r--r--src/stat.c6
-rw-r--r--src/tagr.h2
4 files changed, 47 insertions, 33 deletions
diff --git a/src/main.c b/src/main.c
index 9b98ec5..6530ca0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -433,7 +433,7 @@ change_user ()
if (user == NULL)
return;
- if (getuid () == 0)
+ if (getuid ())
{
logmsg (L_NOTICE, _("not a superuser: ignoring the `user' statement"));
return;
@@ -494,15 +494,17 @@ decode_buffer ()
}
}
- open_db (TAGR_DB_WR);
- for (i = 0; i < reply->n_addr; i++, sp++)
+ if (open_db (TAGR_DB_WR) == 0)
{
- sp->in = ntohl (sp->in);
- sp->out = ntohl (sp->out);
- verbose (1, _("Monitor %s: %lu %lu"), sp->name, sp->in, sp->out);
- report (sp, reply->timestamp);
+ for (i = 0; i < reply->n_addr; i++, sp++)
+ {
+ sp->in = ntohl (sp->in);
+ sp->out = ntohl (sp->out);
+ verbose (1, _("Monitor %s: %lu %lu"), sp->name, sp->in, sp->out);
+ report (sp, reply->timestamp);
+ }
+ close_db ();
}
- close_db ();
if (child)
exit (0);
@@ -531,9 +533,9 @@ read_input (const char *name)
name, strerror (errno));
}
- verbose (2, _("Reading `%s'"), name);
-
- open_db (TAGR_DB_WR);
+ if (open_db (TAGR_DB_WR))
+ exit (EX_UNAVAILABLE);
+
while (getline (&buf, &bufsize, fp) > 0)
{
char *p;
@@ -771,6 +773,8 @@ main (int argc, char **argv)
if (html_template_option)
html_template = html_template_option;
+ if (user_option)
+ user = user_option;
argc -= index;
argv += index;
@@ -790,11 +794,6 @@ main (int argc, char **argv)
exit (0);
}
- if (log_to_stderr == -1)
- log_to_stderr = foreground && isatty (0);
- grecs_log_to_stderr = log_to_stderr;
- init_syslog (program_invocation_short_name);
-
if (test_template_option)
{
CHECK_USAGE (import_option, "--import", "--test-template");
@@ -804,6 +803,18 @@ main (int argc, char **argv)
exit (check_template ());
}
+ if (user)
+ change_user ();
+
+ if (log_to_stderr == -1)
+ log_to_stderr = (import_option
+ || read_option
+ || rebuild_option
+ || list_option
+ || foreground) && isatty (0);
+ grecs_log_to_stderr = log_to_stderr;
+ init_syslog (program_invocation_short_name);
+
if (import_option)
{
CHECK_USAGE (read_option, "--read", "--import");
@@ -838,9 +849,6 @@ main (int argc, char **argv)
exit (EX_CONFIG);
}
- if (user_option)
- change_user ();
-
sockfd = socket (listen_sockaddr.sa->sa_family,
SOCK_DGRAM, 0);
if (sockfd < 0)
diff --git a/src/report.c b/src/report.c
index ed3a81b..398881b 100644
--- a/src/report.c
+++ b/src/report.c
@@ -41,7 +41,7 @@ tagr_db_report (char *str)
logmsg (L_CRIT, "%s: %s", dbname, str);
}
-void
+int
open_db (int flag)
{
dbname = xmalloc (strlen (basedir) + 1 + sizeof (TAGR_DBNAME));
@@ -55,8 +55,9 @@ open_db (int flag)
{
logmsg (L_ERR, _("cannot open database %s: %s"),
dbname, gdbm_strerror (gdbm_errno));
- exit (1);
+ return 1;
}
+ return 0;
}
void
@@ -202,7 +203,8 @@ list_db ()
datum content;
struct monitor *mon;
- open_db (TAGR_DB_RD);
+ if (open_db (TAGR_DB_RD))
+ exit (EX_UNAVAILABLE);
key = gdbm_firstkey (dbf);
while (key.dptr)
{
@@ -278,15 +280,17 @@ rebuild (int force)
time_t now = time (NULL);
verbose (1, _("rebuild initiated"));
- open_db (TAGR_DB_WR);
- key = gdbm_firstkey (dbf);
- while (key.dptr)
+ if (open_db (TAGR_DB_RD) == 0)
{
- datum nextkey = gdbm_nextkey ( dbf, key );
- update_monitor (key, now, force);
- free (key.dptr);
- key = nextkey;
+ key = gdbm_firstkey (dbf);
+ while (key.dptr)
+ {
+ datum nextkey = gdbm_nextkey ( dbf, key );
+ update_monitor (key, now, force);
+ free (key.dptr);
+ key = nextkey;
+ }
+ close_db ();
}
- close_db ();
verbose (1, _("rebuild finished"));
}
diff --git a/src/stat.c b/src/stat.c
index 85b76d9..813c5cb 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -426,7 +426,8 @@ import (const char *dirname)
dirname, strerror (errno));
else if (S_ISREG (st.st_mode))
{
- open_db (TAGR_DB_WR);
+ if (open_db (TAGR_DB_WR))
+ exit (EX_UNAVAILABLE);
if (import_log (dirname) == 0)
count++;
close_db ();
@@ -445,7 +446,8 @@ import (const char *dirname)
switch (rc)
{
case 0:
- open_db (TAGR_DB_WR);
+ if (open_db (TAGR_DB_WR))
+ exit (EX_UNAVAILABLE);
for (i = 0; i < gl.gl_pathc; i++)
if (import_log (gl.gl_pathv[i]) == 0)
count++;
diff --git a/src/tagr.h b/src/tagr.h
index fafd56b..a66837e 100644
--- a/src/tagr.h
+++ b/src/tagr.h
@@ -216,7 +216,7 @@ struct traffic_record
/* report.c */
#define TAGR_DB_RD 0
#define TAGR_DB_WR 1
-void open_db (int);
+int open_db (int);
void close_db ();
void read_db (struct monitor *mon, struct traffic_record **tr);
void write_db (struct monitor *mon, struct traffic_record *tr);

Return to:

Send suggestions and report system problems to the System administrator.