Skip to content

Commit

Permalink
Add support for Systemd service type 'notify'
Browse files Browse the repository at this point in the history
By calling sd_notify() the smcroute daemon (smcrouted) can tell the systemd
service manager when it is fully up and running. This allows systemd to manage
services synchronously (i.e. use service type 'notify') so that dependent tasks
can rely on smcrouted being fully up and running.

This patch is already in use in Debian package smcroute/2.4.4-2.

Related Debian bug: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=924361
  • Loading branch information
bombadil committed Mar 1, 2021
1 parent 9633811 commit b058327
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
11 changes: 11 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ AS_IF([test "x$with_systemd" != "xno"],
[AC_SUBST([systemddir], [$with_systemd])])
AM_CONDITIONAL([HAVE_SYSTEMD], [test "x$with_systemd" != "xno"])

have_libsystemd=no
AS_IF([test "x$with_systemd" != "xno"],
[PKG_CHECK_MODULES(LIBSYSTEMD, libsystemd,
[AC_DEFINE(HAVE_LIBSYSTEMD, 1, [Enable libsystemd integration])
have_libsystemd=yes])]
)
AS_IF([test "x$have_libsystemd" = "xyes"],
[AC_SUBST([systemd_service_type], [notify])],
[AC_SUBST([systemd_service_type], [simple])])
AM_CONDITIONAL([HAVE_LIBSYSTEMD], [test "x$have_libsystemd" = "xyes"])

# Check if we need -lpthread (building statically) and -lrt (older GLIBC)
# Unset cached values when retrying with -lpthread and reset LIBS for each API
need_librt=no
Expand Down
3 changes: 2 additions & 1 deletion smcroute.service.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ After=network-online.target
Requires=network-online.target

[Service]
Type=simple
Type=@systemd_service_type@
ExecStart=@SBINDIR@/smcrouted -n -s
NotifyAccess=main

# Hardening settings
NoNewPrivileges=true
Expand Down
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ if USE_DOTCONF
smcrouted_SOURCES += conf.c conf.h
endif

if HAVE_LIBSYSTEMD
smcrouted_CFLAGS += $(LIBSYSTEMD_CFLAGS)
smcrouted_LDADD += $(LIBSYSTEMD_LIBS)
endif
24 changes: 24 additions & 0 deletions src/smcrouted.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/time.h> /* gettimeofday() */
#include <sys/un.h>

#if HAVE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif

#include "cap.h"
#include "ipc.h"
#include "log.h"
Expand Down Expand Up @@ -135,15 +140,34 @@ static void signal_init(void)

static int server_loop(void)
{
#if HAVE_LIBSYSTEMD
bool need_sd_notify_ready = true;
#endif

script_init(script);
mrdisc_init(interval);

while (running) {
if (reloading) {
#if HAVE_LIBSYSTEMD
sd_notify(0, "RELOADING=1\n"
"STATUS=Reloading configuration...\n");
need_sd_notify_ready = true;
#endif

reload();
reloading = 0;
}


#if HAVE_LIBSYSTEMD
if (need_sd_notify_ready) {
sd_notify(0, "READY=1\n"
"STATUS=Configuration loaded.\n");
need_sd_notify_ready = false;
}
#endif

socket_poll(NULL);
}

Expand Down

0 comments on commit b058327

Please sign in to comment.