From: Teemu Toivola Date: Tue, 25 Jun 2019 19:37:18 +0000 (+0300) Subject: fix interfaces having the possibility of staying marked as disabled after coming... X-Git-Tag: v2.3~11 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c4fb5bde9f2b5d101917d68cf96debcfbfd8e5de;p=vnstat fix interfaces having the possibility of staying marked as disabled after coming back online while still getting correctly monitored --- diff --git a/CHANGES b/CHANGES index 571dfc8..78952c0 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,9 @@ if the used systemd version supported ProtectSystem=strict but didn't support StateDirectory (issue seen at least with systemd 232 in Debian 9) - Debian and Red Hat init.d example files had wrong path for the pid file + - Interfaces could end up staying marked as 'disabled' in the database even + after becoming back active and monitored, only the shown status was wrong + without resulting in any data loss - New - Automatic interface selection when the Interface configuration setting is left empty (new default) diff --git a/src/daemon.c b/src/daemon.c index 92c371f..b9f1f12 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -636,12 +636,6 @@ void flushcachetodisk(DSTATE *s) } } - /* update interface timestamp in database */ - if (!db_setupdated(iterator->interface, iterator->updated)) { - handledatabaseerror(s); - break; - } - if (!iterator->active && !logcount) { /* throw away if interface hasn't seen any data and is disabled */ if (!iterator->currx && !iterator->curtx) { @@ -657,12 +651,18 @@ void flushcachetodisk(DSTATE *s) break; } } + } - /* update interface activity status in database if changed */ - if (!db_setactive(iterator->interface, iterator->active)) { - handledatabaseerror(s); - break; - } + /* update interface timestamp in database */ + if (!db_setupdated(iterator->interface, iterator->updated)) { + handledatabaseerror(s); + break; + } + + /* update interface activity status in database */ + if (!db_setactive(iterator->interface, iterator->active)) { + handledatabaseerror(s); + break; } iterator = iterator->next; diff --git a/src/dbsql.c b/src/dbsql.c index ede57e0..97957d5 100644 --- a/src/dbsql.c +++ b/src/dbsql.c @@ -807,17 +807,6 @@ int db_addtraffic_dated(const char *iface, const uint64_t rx, const uint64_t tx, } } - /* change updated only if more recent than previous when timestamp provided */ - if (timestamp > 0) { - sqlite3_snprintf(1024, sql, "update interface set active=1, updated=datetime(%s, 'localtime') where id=%" PRId64 " and updated < datetime(%s, 'localtime')", nowdate, (int64_t)ifaceid, nowdate); - } else { - sqlite3_snprintf(1024, sql, "update interface set active=1, updated=datetime(%s, 'localtime') where id=%" PRId64 "", nowdate, (int64_t)ifaceid); - } - if (!db_exec(sql)) { - /* no transaction rollback needed here as failure of the first step results in no transaction being active */ - return 0; - } - /* total */ if (rx > 0 || tx > 0) { sqlite3_snprintf(1024, sql, "update interface set rxtotal=rxtotal+%" PRIu64 ", txtotal=txtotal+%" PRIu64 " where id=%" PRId64 "", rx, tx, (int64_t)ifaceid); diff --git a/tests/dbsql_tests.c b/tests/dbsql_tests.c index fac72ba..1211409 100644 --- a/tests/dbsql_tests.c +++ b/tests/dbsql_tests.c @@ -178,7 +178,7 @@ START_TEST(db_addtraffic_can_add_traffic_and_interfaces) } END_TEST -START_TEST(db_addtraffic_dated_does_not_turn_back_time) +START_TEST(db_addtraffic_dated_does_not_touch_updated_time) { int ret; interfaceinfo info; @@ -212,7 +212,7 @@ START_TEST(db_addtraffic_dated_does_not_turn_back_time) ck_assert_int_eq(ret, 1); ck_assert_int_eq(info.rxtotal, 3); ck_assert_int_eq(info.txtotal, 3); - ck_assert_int_eq(info.updated, 2100000000); + ck_assert_int_lt(info.updated, 2100000000); ret = db_addtraffic_dated("eth0", 1, 1, 1000); ck_assert_int_eq(ret, 1); @@ -220,7 +220,7 @@ START_TEST(db_addtraffic_dated_does_not_turn_back_time) ck_assert_int_eq(ret, 1); ck_assert_int_eq(info.rxtotal, 4); ck_assert_int_eq(info.txtotal, 4); - ck_assert_int_eq(info.updated, 2100000000); + ck_assert_int_lt(info.updated, 2100000000); ret = db_addtraffic("eth0", 1, 1); ck_assert_int_eq(ret, 1); @@ -2198,7 +2198,7 @@ void add_dbsql_tests(Suite *s) tcase_add_test(tc_dbsql, db_setinfo_can_not_update_nonexisting_name); tcase_add_test(tc_dbsql, db_addtraffic_with_no_traffic_does_nothing); tcase_add_test(tc_dbsql, db_addtraffic_can_add_traffic_and_interfaces); - tcase_add_test(tc_dbsql, db_addtraffic_dated_does_not_turn_back_time); + tcase_add_test(tc_dbsql, db_addtraffic_dated_does_not_touch_updated_time); tcase_add_test(tc_dbsql, db_getinterfacecount_counts_interfaces); tcase_add_test(tc_dbsql, db_getinterfacecountbyname_counts_interfaces); tcase_add_test(tc_dbsql, db_setactive_fails_with_no_open_db);