]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add support for reporting status via sd_notify()
authorOndřej Surý <ondrej@sury.org>
Wed, 20 Oct 2021 16:14:49 +0000 (18:14 +0200)
committerPetr Špaček <pspacek@isc.org>
Thu, 15 Sep 2022 08:12:15 +0000 (10:12 +0200)
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>
bin/named/Makefile.am
bin/named/server.c
configure.ac

index 57a023b9faf2b4b830be714b794b252a787dc7b3..38045f00aa3c83f089e5aec085c6aab469fa4738 100644 (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
index 0c366242ea209a050b75820c4a4133432eabb7a0..168007233acc3245fdacd4f17cba9d409d2d0275 100644 (file)
 #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>
        } 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);
 }
 
index a39afef720ab84c0fd6ebbb2194cc76c2cf7d7be..786f228c4295dade9e62ed68bb1d37a648ea9a28 100644 (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.
 #