Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Systemd service type 'notify' #155

Merged
merged 1 commit into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ addons:
packages:
- tree
- libcap-dev
- libsystemd-dev
coverity_scan:
project:
name: "troglobit/smcroute"
Expand Down
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