]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon: systemd socket activation
authorMarek Vavrusa <marek@vavrusa.com>
Tue, 29 Mar 2016 01:35:41 +0000 (18:35 -0700)
committerMarek Vavrusa <marek@vavrusa.com>
Tue, 29 Mar 2016 01:39:11 +0000 (18:39 -0700)
addition to previous generic socket activation

fixes #11

Makefile
daemon/README.rst
daemon/daemon.mk
daemon/main.c
doc/build.rst

index 63b307ab51ba67b69076c46c4754b44d4751b6b4..5263fced1309515512cea96b19ac02b191b284c4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -26,6 +26,7 @@ $(eval $(call find_lib,libmemcached,1.0))
 $(eval $(call find_lib,hiredis))
 $(eval $(call find_lib,socket_wrapper))
 $(eval $(call find_lib,libdnssec))
+$(eval $(call find_lib,libsystemd))
 $(eval $(call find_gopkg,geoip,github.com/abh/geoip))
 # Find Go compiler version
 # @note Go 1.5 support amd64 only, disabled for other architectures
@@ -43,7 +44,7 @@ ifneq (,$(findstring luajit, $(lua_LIBS)))
 endif
 endif
 
-BUILD_CFLAGS += $(libknot_CFLAGS) $(libuv_CFLAGS) $(cmocka_CFLAGS) $(lua_CFLAGS) $(libdnssec_CFLAGS)
+BUILD_CFLAGS += $(libknot_CFLAGS) $(libuv_CFLAGS) $(cmocka_CFLAGS) $(lua_CFLAGS) $(libdnssec_CFLAGS) $(libsystemd_CFLAGS)
 BUILD_CFLAGS += $(addprefix -I,$(wildcard contrib/ccan/*) contrib/murmurhash3)
 
 # Overview
@@ -78,6 +79,7 @@ info:
        $(info [$(HAS_libmemcached)] libmemcached (modules/memcached))
        $(info [$(HAS_hiredis)] hiredis (modules/redis))
        $(info [$(HAS_cmocka)] cmocka (tests/unit))
+       $(info [$(HAS_libsystemd)] systemd (daemon))
        $(info )
 
 # Installation directories
index 546e8a117fcd0a105fd6dfb7a3f18d8575601a55..acef29203d09ba463600f26357a5b7d55f7c07a0 100644 (file)
@@ -140,6 +140,8 @@ The watchdog process must notify kresd about active file descriptors, and kresd
    [system] interactive mode
    >
 
+The daemon also supports [systemd socket activation](http://0pointer.de/blog/projects/socket-activation.html), it is automatically detected and requires no configuration on users's side.
+
 Configuration
 =============
 
index 2693ab7ec0f7ed192c51936f547cca47dc2c8a94..4a21e179be8f2058cb8cd60687a32235d3330aae 100644 (file)
@@ -24,7 +24,14 @@ bindings-install: $(kresd_DIST) $(DESTDIR)$(MODULEDIR)
 
 kresd_CFLAGS := -fPIE
 kresd_DEPEND := $(libkres) $(contrib)
-kresd_LIBS := $(libkres_TARGET) $(contrib_TARGET) $(libknot_LIBS) $(libzscanner_LIBS) $(libdnssec_LIBS) $(libuv_LIBS) $(lua_LIBS)
+kresd_LIBS := $(libkres_TARGET) $(contrib_TARGET) $(libknot_LIBS) \
+              $(libzscanner_LIBS) $(libdnssec_LIBS) $(libuv_LIBS) $(lua_LIBS)
+
+# Enable systemd
+ifeq ($(HAS_libsystemd), yes)
+kresd_CFLAGS += -DHAS_SYSTEMD
+kresd_LIBS += $(libsystemd_LIBS)
+endif
 
 # Make binary
 ifeq ($(HAS_lua)|$(HAS_libuv), yes|yes)
index 36e1606389c664272f465a76c5d93fd11a21da73..a9a1429740af2205c99c189b045a5cb2fe5bd511 100644 (file)
@@ -23,6 +23,9 @@
 #include <contrib/ucw/mempool.h>
 #include <contrib/ccan/asprintf/asprintf.h>
 #include <libknot/error.h>
+#ifdef HAS_SYSTEMD
+#include <systemd/sd-daemon.h>
+#endif
 
 #include "lib/defines.h"
 #include "lib/resolve.h"
@@ -214,6 +217,10 @@ static int run_worker(uv_loop_t *loop, struct engine *engine)
                        uv_listen((uv_stream_t *) &pipe, 16, tty_accept);
                }
        }
+       /* Notify supervisor. */
+#ifdef HAS_SYSTEMD
+       sd_notify(0, "READY=1");
+#endif
        /* Run event loop */
        uv_run(loop, UV_RUN_DEFAULT);
        if (sock_file) {
@@ -319,6 +326,15 @@ int main(int argc, char **argv)
                }
        }
 
+#ifdef HAS_SYSTEMD
+       /* Accept passed sockets from systemd supervisor. */
+       int sd_nsocks = sd_listen_fds(0);
+       for (int i = 0; i < sd_nsocks; ++i) {
+               int fd = SD_LISTEN_FDS_START + i;
+               array_push(fd_set, fd);
+       }
+#endif
+
        /* Switch to rundir. */
        if (optind < argc) {
                const char *rundir = argv[optind];
index 0374be556f45cf8e0df87de48dbf4c87ef915cd5..faea9d0acb93f810e9e447a35d42fd1d9585d223 100644 (file)
@@ -48,6 +48,7 @@ There are also *optional* packages that enable specific functionality in Knot DN
    "Doxygen_", "``documentation``", "Generating API documentation."
    "Sphinx_", "``documentation``", "Building this HTML/PDF documentation."
    "breathe_", "``documentation``", "Exposing Doxygen API doc to Sphinx."
+   "libsystemd_", "``daemon``", "Systemd socket activation support."
 
 .. [#] Requires C99, ``__attribute__((cleanup))`` and ``-MMD -MP`` for dependency file generation. GCC, Clang and ICC are supported.
 .. [#] You can use variables ``<dependency>_CFLAGS`` and ``<dependency>_LIBS`` to configure dependencies manually (i.e. ``libknot_CFLAGS`` and ``libknot_LIBS``).
@@ -265,4 +266,6 @@ You can hack on the container by changing the container entrypoint to shell like
 .. _deckard: https://gitlab.labs.nic.cz/knot/deckard
 .. _deckard_doc: https://gitlab.labs.nic.cz/knot/resolver/blob/master/tests/README.rst
 
+.. _libsystemd: https://www.freedesktop.org/wiki/Software/systemd/
+
 .. _DESTDIR: https://www.gnu.org/prep/standards/html_node/DESTDIR.html