Skip to content

Commit

Permalink
fix daemon not trying to import legacy databases when --initdb was us…
Browse files Browse the repository at this point in the history
…ed and no current version database initially existed, add option for using --noadd in combination with --initdb if this behaviour is still wanted, produce error if --nodaemon and --initdb are used at the same time
  • Loading branch information
vergoh committed Jan 7, 2024
1 parent bf4532a commit 91a77f1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
- Daemon didn't try to import legacy databases when --noadd was used and no
current version database initially existed resulting in the process
exiting even when something could have been done
- Daemon didn't try to import legacy databases when --initdb was used and
no current version database initially existed, this behaviour can still
be enabled by using --noadd in combination with --initdb
- Using --nodaemon and --initdb at the same time didn't result in an error
being shown
- New
- Add 95th percentile output as --95%, also available via --alert, --json,
--xml and image output, requires 5MinuteHours configuration to be set to
Expand Down
21 changes: 17 additions & 4 deletions man/vnstatd.8
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,35 @@ can only be used when the process is started as root.

.TP
.B "--initdb"
Create a new empty database without interface data and exit. If the database
already exists then access to it is only verified. The daemon will not stay
running when this option is used. This option cannot be used in combination with
Create a new database, import data from found legacy databases if
.B "--noadd"
option isn't used and exit without creating database entries for
available interfaces if no legacy data was imported. If the database already
exists then access to it is only verified. The daemon will not stay running
when this option is used. This option cannot be used in combination with
.BR "-d, --daemon" ,
.B "-n, --nodaemon"
or
.BR "--startempty" .

.TP
.B "--noadd"
Disable the automatic creation of new database entries for all currently available
When used in combination with
.B "-d, --daemon"
or
.BR "-n, --nodaemon" ,
disable the automatic creation of new database entries for all currently available
interfaces when the daemon is started with no existing database or with a database
containing zero interfaces. The daemon will still create an empty database if one doesn't
already exist. Pseudo interfaces lo, lo0 and sit0 are always excluded from getting
added regardless of this option.

.IP
When used in combination with
.BR "--initdb" ,
create only an empty database if one doesn't already exist without importing data
from possible legacy databases and exit.

.TP
.B "-n, --nodaemon"
Stay in foreground attached to the current terminal and start the update
Expand Down
18 changes: 11 additions & 7 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void initdstate(DSTATE *s)
debug = 0; /* debug disabled by default */
disableprinte = 0; /* let printe() output be visible */
stderrprinte = 0; /* use stdout for printe() output */
s->rundaemon = 0; /* daemon disabled by default */
s->rundaemon = -1; /* daemon disabled by default */

s->running = 0;
s->dbsaved = 1;
Expand Down Expand Up @@ -315,9 +315,13 @@ void preparedatabase(DSTATE *s)
exit(EXIT_FAILURE);
}

if (s->initdb && s->noadd) {
return;
}

if (s->dbifcount == 0) {
s->dbifcount += importlegacydbs();
if (s->dbifcount > 0 && !cfg.alwaysadd) {
if ((s->dbifcount > 0 && !cfg.alwaysadd) || s->initdb) {
return;
}
}
Expand Down Expand Up @@ -841,7 +845,7 @@ void handleintsignals(DSTATE *s)
if (!db_open_rw(1)) {
snprintf(errorstring, 1024, "Opening database after SIGHUP failed (%s), exiting.", strerror(errno));
printe(PT_Error);
if (s->rundaemon && !debug) {
if (s->rundaemon == 1 && !debug) {
close(pidfile);
unlink(cfg.pidfile);
}
Expand All @@ -850,7 +854,7 @@ void handleintsignals(DSTATE *s)
if (!db_removedisabledresolutionentries()) {
snprintf(errorstring, 1024, "Disabled resolution entry cleanup after SIGHUP failed (%s), exiting.", strerror(errno));
printe(PT_Error);
if (s->rundaemon && !debug) {
if (s->rundaemon == 1 && !debug) {
close(pidfile);
unlink(cfg.pidfile);
}
Expand All @@ -860,7 +864,7 @@ void handleintsignals(DSTATE *s)
if (!db_vacuum()) {
snprintf(errorstring, 1024, "Database vacuum after SIGHUP failed (%s), exiting.", strerror(errno));
printe(PT_Error);
if (s->rundaemon && !debug) {
if (s->rundaemon == 1 && !debug) {
close(pidfile);
unlink(cfg.pidfile);
}
Expand Down Expand Up @@ -905,7 +909,7 @@ void preparedirs(const DSTATE *s)
updatedirowner(cfg.dbdir, s->user, s->group);
}

if (!cfg.createdirs || !s->rundaemon) {
if (!cfg.createdirs || s->rundaemon != 1) {
return;
}

Expand Down Expand Up @@ -1060,7 +1064,7 @@ __attribute__((noreturn)) void errorexitdaemon(DSTATE *s, const int fataldberror
datacache_clear(&s->dcache);
ibwflush();

if (s->rundaemon && !debug) {
if (s->rundaemon == 1 && !debug) {
close(pidfile);
unlink(cfg.pidfile);
}
Expand Down
4 changes: 4 additions & 0 deletions src/dbsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ int db_open(const int createifnotfound, const int readonly)
if (stat(dbfilename, &filestat) != 0) {
if (errno == ENOENT && createifnotfound && !readonly) {
createdb = 1;
if (debug)
printf("\"%s\" doesn't exist, creating new database\n", dbfilename);
} else {
if (debug)
printf("Error (debug): Handling database \"%s\" failed: %s\n", dbfilename, strerror(errno));
Expand All @@ -49,6 +51,8 @@ int db_open(const int createifnotfound, const int readonly)
if (filestat.st_size == 0) {
if (createifnotfound) {
createdb = 1;
if (debug)
printf("\"%s\" exists but is 0 sized, creating new database\n", dbfilename);
} else {
printf("Error: Database \"%s\" contains 0 bytes and isn't a valid database, exiting.\n", dbfilename);
exit(EXIT_FAILURE);
Expand Down
27 changes: 17 additions & 10 deletions src/vnstatd.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}

detectboot(&s);
preparedatabase(&s);

if (s.initdb) {
db_close();
if (debug) {
Expand All @@ -92,9 +95,6 @@ int main(int argc, char *argv[])
exit(EXIT_SUCCESS);
}

detectboot(&s);
preparedatabase(&s);

if (cfg.fiveminutehours == 0 && cfg.hourlydays == 0 && cfg.dailydays == 0 && cfg.monthlymonths == 0 && cfg.yearlyyears == 0 && cfg.topdayentries == 0) {
printf("Error: All data resolutions have been disabled in data retention configuration:");
printf(" 5MinuteHours %d\n", cfg.fiveminutehours);
Expand Down Expand Up @@ -132,7 +132,7 @@ int main(int argc, char *argv[])
setsignaltraps();

/* start as daemon if requested, debug can't be enabled at the same time */
if (s.rundaemon && !debug) {
if (s.rundaemon == 1 && !debug) {
if (!db_close()) {
printf("Error: Failed to close database \"%s/%s\" before starting daemon: %s\n", cfg.dbdir, DATABASEFILE, strerror(errno));
printf("Exiting...\n");
Expand Down Expand Up @@ -239,7 +239,7 @@ int main(int argc, char *argv[])
datacache_clear(&s.dcache);
ibwflush();

if (s.rundaemon && !debug) {
if (s.rundaemon == 1 && !debug) {
close(pidfile);
unlink(cfg.pidfile);
}
Expand All @@ -263,10 +263,11 @@ void showhelp(void)
printf(" -g, --group <group> set daemon process group\n");
printf(" -t, --timestamp timestamp prints when running in foreground\n");
printf(" --config <config file> select used config file\n");
printf(" --initdb create empty database and exit\n");
printf(" --initdb create database if it doesn't exist and exit\n");
printf(" --alwaysadd [mode] automatically start monitoring all new interfaces\n");
printf(" --noadd disable discovery of interfaces when database\n");
printf(" contains none\n");
printf(" contains none, disable legacy data import when used\n");
printf(" in combination with --initdb\n");
printf(" --noremove disable removal of never seen interfaces\n");
printf(" --startempty start even when database is empty\n\n");

Expand All @@ -292,6 +293,7 @@ void parseargs(DSTATE *s, int argc, char **argv)
s->rundaemon = 1;
s->showhelp = 0;
} else if ((strcmp(argv[currentarg], "-n") == 0) || (strcmp(argv[currentarg], "--nodaemon") == 0)) {
s->rundaemon = 0;
s->showhelp = 0;
} else if ((strcmp(argv[currentarg], "-s") == 0) || (strcmp(argv[currentarg], "--sync") == 0)) {
s->sync = 1;
Expand Down Expand Up @@ -374,16 +376,21 @@ void parseargs(DSTATE *s, int argc, char **argv)
cfg.alwaysadd = 0;
}

if (s->rundaemon && debug) {
if (s->rundaemon == 1 && debug) {
printf("Error: --daemon and --debug can't both be used at the same time.\n");
exit(EXIT_FAILURE);
}

if (s->rundaemon && s->initdb) {
if (s->rundaemon == 1 && s->initdb) {
printf("Error: --daemon and --initdb can't both be used at the same time.\n");
exit(EXIT_FAILURE);
}

if (s->rundaemon == 0 && s->initdb) {
printf("Error: --nodaemon and --initdb can't both be used at the same time.\n");
exit(EXIT_FAILURE);
}

if (s->startempty && s->initdb) {
printf("Error: --startempty and --initdb can't both be used at the same time.\n");
exit(EXIT_FAILURE);
Expand All @@ -395,7 +402,7 @@ void parseargs(DSTATE *s, int argc, char **argv)
exit(EXIT_SUCCESS);
}

if (!s->rundaemon && pidfiledefined) {
if (s->rundaemon != 1 && pidfiledefined) {
printf("Error: --pidfile can only be used together with --daemon\n");
exit(EXIT_FAILURE);
}
Expand Down

0 comments on commit 91a77f1

Please sign in to comment.