From: Marek Vavrusa Date: Tue, 29 Mar 2016 01:35:41 +0000 (-0700) Subject: daemon: systemd socket activation X-Git-Tag: v1.0.0~54^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4b7d12112f13e09c16bdb7b556f85321eacfa1b8;p=thirdparty%2Fknot-resolver.git daemon: systemd socket activation addition to previous generic socket activation fixes #11 --- diff --git a/Makefile b/Makefile index 63b307ab5..5263fced1 100644 --- 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 diff --git a/daemon/README.rst b/daemon/README.rst index 546e8a117..acef29203 100644 --- a/daemon/README.rst +++ b/daemon/README.rst @@ -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 ============= diff --git a/daemon/daemon.mk b/daemon/daemon.mk index 2693ab7ec..4a21e179b 100644 --- a/daemon/daemon.mk +++ b/daemon/daemon.mk @@ -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) diff --git a/daemon/main.c b/daemon/main.c index 36e160638..a9a142974 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -23,6 +23,9 @@ #include #include #include +#ifdef HAS_SYSTEMD +#include +#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]; diff --git a/doc/build.rst b/doc/build.rst index 0374be556..faea9d0ac 100644 --- a/doc/build.rst +++ b/doc/build.rst @@ -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 ``_CFLAGS`` and ``_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