Add support for reporting status via sd_notify()

sd_notify() may be called by a service to notify the service manager
about state changes. It can be used to send arbitrary information,
encoded in an environment-block-like string. Most importantly, it can be
used for start-up completion notification.

Add libsystemd check to autoconf script and when the library is detected
add calls to sd_notify() around the server->reload_status changes.

Co-authored-by: Petr Špaček <pspacek@isc.org>
This commit is contained in:
Ondřej Surý 2021-10-20 18:14:49 +02:00 committed by Petr Špaček
parent 0b7f082060
commit 52b62b7890
No known key found for this signature in database
GPG key ID: ABD587CDF06581AE
3 changed files with 68 additions and 4 deletions

View file

@ -14,6 +14,7 @@ AM_CPPFLAGS += \
$(MAXMINDDB_CFLAGS) \
$(DNSTAP_CFLAGS) \
$(LIBUV_CFLAGS) \
$(LIBSYSTEMD_CFLAGS) \
$(ZLIB_CFLAGS)
if HAVE_JSON_C
@ -109,7 +110,7 @@ named_LDADD = \
$(MAXMINDDB_LIBS) \
$(DNSTAP_LIBS) \
$(LIBUV_LIBS) \
$(LIBXML2_LIBS) \
$(LIBSYSTEMD_LIBS) \
$(ZLIB_LIBS)
if HAVE_JSON_C
@ -121,3 +122,8 @@ if HAVE_LIBNGHTTP2
named_LDADD += \
$(LIBNGHTTP2_LIBS)
endif HAVE_LIBNGHTTP2
if HAVE_LIBXML2
named_LDADD += \
$(LIBXML2_LIBS)
endif HAVE_LIBXML2

View file

@ -28,6 +28,10 @@
#include <fstrm.h>
#endif
#ifdef HAVE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif
#include <isc/aes.h>
#include <isc/attributes.h>
#include <isc/base64.h>
@ -215,11 +219,12 @@
} while (0)
#define CHECKFATAL(op, msg) \
do { \
{ \
result = (op); \
if (result != ISC_R_SUCCESS) \
if (result != ISC_R_SUCCESS) { \
fatal(server, msg, result); \
} while (0)
} \
}
/*%
* Maximum ADB size for views that share a cache. Use this limit to suppress
@ -9912,6 +9917,15 @@ view_loaded(void *arg) {
"FIPS mode is %s",
FIPS_mode() ? "enabled" : "disabled");
#endif /* ifdef HAVE_FIPS_MODE */
#if HAVE_LIBSYSTEMD
sd_notifyf(0,
"READY=1\n"
"STATUS=running\n"
"MAINPID=%" PRId64 "\n",
(int64_t)getpid());
#endif /* HAVE_LIBSYSTEMD */
atomic_store(&server->reload_status, NAMED_RELOAD_DONE);
isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
@ -10085,6 +10099,10 @@ shutdown_server(isc_task_t *task, isc_event_t *event) {
isc_event_free(&event);
#if HAVE_LIBSYSTEMD
sd_notify(0, "STOPPING=1\n");
#endif /* HAVE_LIBSYSTEMD */
/*
* We need to shutdown the interface before going
* exclusive (which would pause the netmgr).
@ -10526,6 +10544,10 @@ reload(named_server_t *server) {
isc_result_t result;
atomic_store(&server->reload_status, NAMED_RELOAD_IN_PROGRESS);
#if HAVE_LIBSYSTEMD
sd_notify(0, "RELOADING=1\n"
"STATUS=reload command received\n");
#endif /* HAVE_LIBSYSTEMD */
CHECK(loadconfig(server));
@ -10542,6 +10564,12 @@ reload(named_server_t *server) {
atomic_store(&server->reload_status, NAMED_RELOAD_FAILED);
}
cleanup:
#if HAVE_LIBSYSTEMD
sd_notifyf(0,
"READY=1\n"
"STATUS=reload command finished: %s\n",
isc_result_totext(result));
#endif /* HAVE_LIBSYSTEMD */
return (result);
}
@ -10903,6 +10931,10 @@ isc_result_t
named_server_reconfigcommand(named_server_t *server) {
isc_result_t result;
atomic_store(&server->reload_status, NAMED_RELOAD_IN_PROGRESS);
#if HAVE_LIBSYSTEMD
sd_notify(0, "RELOADING=1\n"
"STATUS=reconfig command received\n");
#endif /* HAVE_LIBSYSTEMD */
CHECK(loadconfig(server));
@ -10919,6 +10951,12 @@ named_server_reconfigcommand(named_server_t *server) {
atomic_store(&server->reload_status, NAMED_RELOAD_FAILED);
}
cleanup:
#if HAVE_LIBSYSTEMD
sd_notifyf(0,
"READY=1\n"
"STATUS=reconfig command finished: %s\n",
isc_result_totext(result));
#endif /* HAVE_LIBSYSTEMD */
return (result);
}

View file

@ -924,6 +924,26 @@ AS_CASE([$with_zlib],
AC_SUBST([ZLIB_CFLAGS])
AC_SUBST([ZLIB_LIBS])
#
# was --with-libsystemd specified?
#
# [pairwise: --with-libsystemd=auto, --with-libsystemd=yes, --without-libsystemd]
AC_ARG_WITH([libsystemd],
[AS_HELP_STRING([--with-libsystemd],
[build with libsystemd integration [default=auto]])],
[], [with_libsystemd=auto])
AS_CASE([$with_libsystemd],
[no],[],
[auto],[PKG_CHECK_MODULES([LIBSYSTEMD], [libsystemd],
[AC_DEFINE([HAVE_LIBSYSTEMD], [1], [Use libsystemd library])],
[:])],
[yes],[PKG_CHECK_MODULES([LIBSYSTEMD], [libsystemd],
[AC_DEFINE([HAVE_LIBSYSTEMD], [1], [Use libsystemd library])])],
[AC_MSG_ERROR([Specifying libsystemd installation path is not supported, adjust PKG_CONFIG_PATH instead])])
AC_SUBST([LIBSYSTEMD_CFLAGS])
AC_SUBST([LIBSYSTEMD_LIBS])
#
# Check if the system supports glibc-compatible backtrace() function.
#