From 8cb3afa7dcb50aeca4a00e6d0b9f8a2830a193f6 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Fri, 14 Oct 2016 17:28:55 +1030 Subject: [PATCH] Replace cf_on_config_change() with "config_change" trigger This is required to support feature-driven linking, in which the exact list of which functions to call in response to a configuration re-load is not known until link time. The "config_change_log" trigger is called before "config_change", so that the logger can respond to the change before other subsystems. --- conf.c | 19 +++++++++++++++++-- conf.h | 4 +++- directory_service.c | 4 ---- fakeradio.c | 2 -- log.c | 4 +++- log.h | 3 ++- log_on_config_change.c | 25 ------------------------- log_stderr.c | 4 ---- server.c | 9 +++++++-- simulator.c | 4 ---- 10 files changed, 32 insertions(+), 46 deletions(-) delete mode 100644 log_on_config_change.c diff --git a/conf.c b/conf.c index d291cff4..ab83fb48 100644 --- a/conf.c +++ b/conf.c @@ -236,12 +236,27 @@ static int reload_and_parse(int permissive, int strict) cf_limbo = 0; // let log messages out logFlush(); if (changed) { - logConfigChanged(); - cf_on_config_change(); + CALL_TRIGGER(config_change_log); + CALL_TRIGGER(config_change); } return ret; } +// Put a dummy no-op trigger callback into the "config_change" and "config_change_log" trigger +// sections, otherwise if no other object provides one, the link will fail with errors like: +// undefined reference to `__start_tr_config_change' +// undefined reference to `__stop_tr_config_change' + +static void __dummy_on_config_change_log(); +DEFINE_TRIGGER(config_change_log, __dummy_on_config_change_log); +static void __dummy_on_config_change_log() {} + +static void __dummy_on_config_change(); +DEFINE_TRIGGER(config_change, __dummy_on_config_change); +static void __dummy_on_config_change() {} + +// The configuration API entry points. + int cf_load() { conffile_meta = config_meta = FILE_META_UNKNOWN; diff --git a/conf.h b/conf.h index d7f4d9eb..4c54f30d 100644 --- a/conf.h +++ b/conf.h @@ -233,6 +233,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "rhizome_types.h" #include "strbuf.h" #include "httpd.h" +#include "trigger.h" #define CONFIG_FILE_MAX_SIZE (32 * 1024) #define INTERFACE_NAME_STRLEN 40 @@ -713,6 +714,7 @@ int cf_reload(void); int cf_reload_strict(void); int cf_reload_permissive(void); -void cf_on_config_change(void); +DECLARE_TRIGGER(config_change_log); +DECLARE_TRIGGER(config_change); #endif //__SERVAL_DNA__CONF_H diff --git a/directory_service.c b/directory_service.c index bbdc350f..bd8600a8 100644 --- a/directory_service.c +++ b/directory_service.c @@ -191,10 +191,6 @@ static void resolve_request(){ } } -void cf_on_config_change() -{ -} - int main(void){ struct pollfd fds[2]; int mdp_sockfd; diff --git a/fakeradio.c b/fakeradio.c index 21107d2b..f462ed6c 100644 --- a/fakeradio.c +++ b/fakeradio.c @@ -60,8 +60,6 @@ struct radio_state { #define STATE_PLUSPLUSPLUS 3 #define STATE_COMMAND 4 -void cf_on_config_change(){} - void log_time(){ struct timeval tv; struct tm tm; diff --git a/log.c b/log.c index bac91b26..24afa69f 100644 --- a/log.c +++ b/log.c @@ -669,7 +669,9 @@ void vlogMessage(int level, struct __sourceloc whence, const char *fmt, va_list } } -void logConfigChanged() +static void logConfigChanged(); +DEFINE_TRIGGER(config_change_log, logConfigChanged); +static void logConfigChanged() { _log_iterator it; _log_iterator_start(&it); diff --git a/log.h b/log.h index d8a114ac..9fcce80c 100644 --- a/log.h +++ b/log.h @@ -1,4 +1,6 @@ /* +Serval DNA logging +Copyright (C) 2016 Flinders University Copyright (C) 2012-2015 Serval Project Inc. This program is free software; you can redistribute it and/or @@ -52,7 +54,6 @@ extern int logLevel_NoLogFileConfigured; void close_log_file(); void redirect_stderr_to_log(); void logFlush(); -void logConfigChanged(); // Logging context string. struct strbuf; diff --git a/log_on_config_change.c b/log_on_config_change.c deleted file mode 100644 index e2bd2404..00000000 --- a/log_on_config_change.c +++ /dev/null @@ -1,25 +0,0 @@ -/* -Serval DNA logging -Copyright 2016 Flinders University - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -*/ - -// This compilation unit provides an on_log_config_change() function for -// clients that do not define their own. - -#include "log.h" - -void cf_on_config_change() {} diff --git a/log_stderr.c b/log_stderr.c index 26f5a85e..cc0f802f 100644 --- a/log_stderr.c +++ b/log_stderr.c @@ -88,7 +88,3 @@ void logFlush() { fflush(stderr); } - -void logConfigChanged() -{ -} diff --git a/server.c b/server.c index 4124e6ae..2b5b4d90 100644 --- a/server.c +++ b/server.c @@ -36,6 +36,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "server.h" #include "serval.h" #include "conf.h" +#include "log.h" #include "str.h" #include "numeric_str.h" #include "strbuf.h" @@ -273,7 +274,7 @@ int server_bind() void server_loop(time_ms_t (*waiting)(time_ms_t, time_ms_t, time_ms_t), void (*wokeup)()) { - cf_on_config_change(); + CALL_TRIGGER(config_change); // This log message is used by tests to wait for the server to start. INFOF("Server initialised, entering main loop"); @@ -512,7 +513,11 @@ void rhizome_clean_db(struct sched_ent *alarm) RESCHEDULE(alarm, now + 30*60*1000, TIME_MS_NEVER_WILL, TIME_MS_NEVER_WILL); } -void cf_on_config_change() +static void server_on_config_change(); + +DEFINE_TRIGGER(config_change, server_on_config_change); + +static void server_on_config_change() { if (!serverMode) return; diff --git a/simulator.c b/simulator.c index 1d56a1ef..d2ddcc8f 100644 --- a/simulator.c +++ b/simulator.c @@ -109,10 +109,6 @@ struct command_state *stdin_state; struct network *networks=NULL; static void unicast_alarm(struct sched_ent *alarm); -void cf_on_config_change() -{ -} - static void recv_packet(int fd, struct network *network, struct peer *destination) { struct socket_address addr;