[PATCH 0/3]: Support systemd notifications
The patchset coming after this implements support for Type=notify units, and support for the STATUS notifications in systemd. This means that syslog-ng becomes able to signal systemd upon startup when it finished starting up, and thanks to the STATUS notifications, an administrator will be able to check syslog-ng's status with systemctl. If a reload fails for example, that will be shown there. -- |8]
From: Gergely Nagy <algernon@madhouse-project.org> When compiling with systemd support, notify systemd about our status and readiness via sd_notify(). Also set the service type to Type=notify in the systemd unit file. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org> --- contrib/systemd/syslog-ng.service | 1 + lib/Makefile.am | 3 ++- lib/mainloop.c | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/contrib/systemd/syslog-ng.service b/contrib/systemd/syslog-ng.service index ee8d28b..46e61bf 100644 --- a/contrib/systemd/syslog-ng.service +++ b/contrib/systemd/syslog-ng.service @@ -3,6 +3,7 @@ Description=System Logger Daemon Documentation=man:syslog-ng(8) [Service] +Type=notify Sockets=syslog.socket ExecStart=/usr/sbin/syslog-ng -F ExecReload=/bin/kill -HUP $MAINPID diff --git a/lib/Makefile.am b/lib/Makefile.am index f572499..a735df2 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -10,7 +10,8 @@ export top_srcdir include logproto/logproto.am lib_LTLIBRARIES = libsyslog-ng.la -libsyslog_ng_la_LIBADD = @CORE_DEPS_LIBS@ +libsyslog_ng_la_CPPFLAGS = $(libsystemd_daemon_CFLAGS) +libsyslog_ng_la_LIBADD = @CORE_DEPS_LIBS@ $(libsystemd_daemon_LIBS) libsyslog_ng_la_LDFLAGS = -no-undefined -release @VERSION@ # this is intentionally formatted so conflicts are less likely to arise. one name in every line. diff --git a/lib/mainloop.c b/lib/mainloop.c index 6dd1979..68b8bf5 100644 --- a/lib/mainloop.c +++ b/lib/mainloop.c @@ -33,6 +33,7 @@ #include "dnscache.h" #include "tls-support.h" #include "scratch-buffers.h" +#include "config.h" #include <sys/types.h> #include <sys/wait.h> @@ -45,6 +46,12 @@ #include <iv_work.h> #include <iv_event.h> +#if ENABLE_SYSTEMD +#include <systemd/sd-daemon.h> +#else +#define sd_notify(...) +#endif + /** * Processing model * ================ @@ -506,10 +513,12 @@ main_loop_reload_config_apply(void) main_loop_new_config->persist = NULL; cfg_free(main_loop_old_config); current_configuration = main_loop_new_config; + sd_notify(0, "STATUS="); } else { msg_error("Error initializing new configuration, reverting to old config", NULL); + sd_notify(0, "STATUS=Error initializing new configuration, using the old config"); cfg_persist_config_move(main_loop_new_config, main_loop_old_config); if (!cfg_init(main_loop_old_config)) { @@ -548,6 +557,8 @@ main_loop_reload_config_apply(void) void main_loop_reload_config_initiate(void) { + sd_notify(0, "STATUS=Reloading configuration"); + if (main_loop_new_config) { /* This block is entered only if this function is reentered before @@ -559,6 +570,7 @@ main_loop_reload_config_initiate(void) cfg_free(main_loop_new_config); main_loop_new_config = NULL; } + main_loop_old_config = current_configuration; app_pre_config_loaded(); main_loop_new_config = cfg_new(0); @@ -570,6 +582,7 @@ main_loop_reload_config_initiate(void) msg_error("Error parsing configuration", evt_tag_str(EVT_TAG_FILENAME, cfgfilename), NULL); + sd_notify(0, "STATUS=Error parsing new configuration, using the old config"); return; } main_loop_io_worker_sync_call(main_loop_reload_config_apply); @@ -700,6 +713,7 @@ main_loop_run(void) msg_notice("syslog-ng starting up", evt_tag_str("version", VERSION), NULL); + sd_notify(0, "STATUS=starting up"); IV_TIMER_INIT(&stats_timer); stats_timer.handler = stats_timer_elapsed; @@ -734,7 +748,10 @@ main_loop_run(void) stats_timer_kickoff(current_configuration); /* main loop */ + sd_notify(0, "STATUS="); + sd_notify(0, "READY=1"); iv_main(); + sd_notify(0, "STATUS=Shutting down"); control_destroy(); -- 1.7.10.4
From: Balazs Scheidler <bazsi@balabit.hu> --- lib/mainloop.c | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/lib/mainloop.c b/lib/mainloop.c index 68b8bf5..91b37e4 100644 --- a/lib/mainloop.c +++ b/lib/mainloop.c @@ -33,7 +33,6 @@ #include "dnscache.h" #include "tls-support.h" #include "scratch-buffers.h" -#include "config.h" #include <sys/types.h> #include <sys/wait.h> @@ -48,8 +47,6 @@ #if ENABLE_SYSTEMD #include <systemd/sd-daemon.h> -#else -#define sd_notify(...) #endif /** @@ -471,6 +468,35 @@ main_loop_io_worker_sync_call(void (*func)(void)) } /************************************************************************************ + * + ************************************************************************************/ + +#if ENABLE_SYSTEMD +static void +main_loop_publish_status(const gchar *status) +{ + gchar *status_buffer; + time_t now = time(NULL); + + status_buffer = g_strdup_printf("STATUS=%s (%s)", status, ctime(now, buf)); + sd_notify(0, status_buffer); + g_free(status_buffer); +} + +static void +main_loop_indicate_readiness(void) +{ + sd_notify(0, "READY=1"); +} + +#else + +#define main_loop_publish_status(x) +#define main_loop_indicate_readiness() + +#endif + +/************************************************************************************ * config load/reload ************************************************************************************/ @@ -513,12 +539,12 @@ main_loop_reload_config_apply(void) main_loop_new_config->persist = NULL; cfg_free(main_loop_old_config); current_configuration = main_loop_new_config; - sd_notify(0, "STATUS="); + main_loop_publish_status("Running"); } else { msg_error("Error initializing new configuration, reverting to old config", NULL); - sd_notify(0, "STATUS=Error initializing new configuration, using the old config"); + main_loop_publish_status("Error initializing new configuration, using the old config"); cfg_persist_config_move(main_loop_new_config, main_loop_old_config); if (!cfg_init(main_loop_old_config)) { @@ -557,7 +583,7 @@ main_loop_reload_config_apply(void) void main_loop_reload_config_initiate(void) { - sd_notify(0, "STATUS=Reloading configuration"); + main_loop_publish_status("Reloading configuration"); if (main_loop_new_config) { @@ -582,7 +608,7 @@ main_loop_reload_config_initiate(void) msg_error("Error parsing configuration", evt_tag_str(EVT_TAG_FILENAME, cfgfilename), NULL); - sd_notify(0, "STATUS=Error parsing new configuration, using the old config"); + main_loop_publish_status("Error parsing new configuration, using the old config"); return; } main_loop_io_worker_sync_call(main_loop_reload_config_apply); @@ -679,6 +705,8 @@ setup_signals(void) int main_loop_init(void) { + main_loop_publish_status("Starting up..."); + app_startup(); setup_signals(); main_loop_io_workers.thread_start = main_loop_io_worker_thread_start; @@ -713,7 +741,6 @@ main_loop_run(void) msg_notice("syslog-ng starting up", evt_tag_str("version", VERSION), NULL); - sd_notify(0, "STATUS=starting up"); IV_TIMER_INIT(&stats_timer); stats_timer.handler = stats_timer_elapsed; @@ -748,10 +775,10 @@ main_loop_run(void) stats_timer_kickoff(current_configuration); /* main loop */ - sd_notify(0, "STATUS="); - sd_notify(0, "READY=1"); + main_loop_indicate_readiness(); + main_loop_publish_status("Running..."); iv_main(); - sd_notify(0, "STATUS=Shutting down"); + main_loop_publish_status("Shutting down..."); control_destroy(); -- 1.7.10.4
This introduces a new function, main_loop_clear_status(), that will clear the status when running with systemd support enabled. It also changes the code that under normal conditions, no status is shown. Signed-off-by: Gergely Nagy <algernon@balabit.hu> --- lib/mainloop.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/mainloop.c b/lib/mainloop.c index 91b37e4..6f59756 100644 --- a/lib/mainloop.c +++ b/lib/mainloop.c @@ -484,6 +484,12 @@ main_loop_publish_status(const gchar *status) } static void +main_loop_clear_status(void) +{ + sd_notify(0, "STATUS="); +} + +static void main_loop_indicate_readiness(void) { sd_notify(0, "READY=1"); @@ -492,6 +498,7 @@ main_loop_indicate_readiness(void) #else #define main_loop_publish_status(x) +#define main_loop_clear_status() #define main_loop_indicate_readiness() #endif @@ -539,7 +546,7 @@ main_loop_reload_config_apply(void) main_loop_new_config->persist = NULL; cfg_free(main_loop_old_config); current_configuration = main_loop_new_config; - main_loop_publish_status("Running"); + main_loop_clear_status(); } else { @@ -776,7 +783,7 @@ main_loop_run(void) /* main loop */ main_loop_indicate_readiness(); - main_loop_publish_status("Running..."); + main_loop_clear_status(); iv_main(); main_loop_publish_status("Shutting down..."); -- 1.7.10.4
participants (1)
-
Gergely Nagy