Skip to content

Commit

Permalink
daemon: systemd socket activation
Browse files Browse the repository at this point in the history
addition to previous generic socket activation

fixes #11
  • Loading branch information
vavrusa committed Mar 29, 2016
1 parent d944e31 commit 4b7d121
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Makefile
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions daemon/README.rst
Expand Up @@ -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
=============

Expand Down
9 changes: 8 additions & 1 deletion daemon/daemon.mk
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions daemon/main.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
Expand Down
3 changes: 3 additions & 0 deletions doc/build.rst
Expand Up @@ -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``).
Expand Down Expand Up @@ -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

0 comments on commit 4b7d121

Please sign in to comment.