From b97deaecc5743c40a6ea3301ba4f2f09af912cb8 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 25 Mar 2014 06:14:13 +0100 Subject: Don't depend on libsystemd-daemon for systemd integration, always use systemd integration on Linux --- src/CMakeLists.txt | 10 +++++----- src/fastd.c | 42 ++++++++++++++++++++++++++++++++++++------ src/fastd_config.h.in | 2 +- 3 files changed, 42 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6ea65e8..e7fb940 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,8 @@ set_directory_properties(PROPERTIES COMPILE_DEFINITIONS _GNU_SOURCE) -set(FASTD_CFLAGS "-pthread -std=c99 ${UECC_CFLAGS_OTHER} ${NACL_CFLAGS_OTHER} ${OPENSSL_CRYPTO_CFLAGS_OTHER} ${SYSTEMD_DAEMON_CFLAGS_OTHER} -Wall") +set(FASTD_CFLAGS "-pthread -std=c99 ${UECC_CFLAGS_OTHER} ${NACL_CFLAGS_OTHER} ${OPENSSL_CRYPTO_CFLAGS_OTHER} -Wall") include_directories(${FASTD_SOURCE_DIR} ${FASTD_BINARY_DIR}/src) -link_directories(${UECC_LIBRARY_DIRS} ${NACL_LIBRARY_DIRS} ${OPENSSL_CRYPTO_LIBRARY_DIRS} ${SYSTEMD_DAEMON_LIBRARY_DIRS}) +link_directories(${UECC_LIBRARY_DIRS} ${NACL_LIBRARY_DIRS} ${OPENSSL_CRYPTO_LIBRARY_DIRS}) include(generate_version) @@ -38,9 +38,9 @@ add_executable(fastd ${BISON_fastd_config_parse_OUTPUTS} ) set_property(TARGET fastd PROPERTY COMPILE_FLAGS "${FASTD_CFLAGS}") -set_property(TARGET fastd PROPERTY LINK_FLAGS "-pthread ${UECC_LDFLAGS_OTHER} ${NACL_LDFLAGS_OTHER} ${OPENSSL_CRYPTO_LDFLAGS_OTHER} ${SYSTEMD_DAEMON_LDFLAGS_OTHER}") -set_property(TARGET fastd APPEND PROPERTY INCLUDE_DIRECTORIES ${CAP_INCLUDE_DIR} ${NACL_INCLUDE_DIRS} ${SYSTEMD_DAEMON_INCLUDE_DIRS}) -target_link_libraries(fastd protocols methods ciphers macs ${RT_LIBRARY} ${CAP_LIBRARY} ${UECC_LIBRARIES} ${NACL_LIBRARIES} ${OPENSSL_CRYPTO_LIBRARIES} ${SYSTEMD_DAEMON_LIBRARIES}) +set_property(TARGET fastd PROPERTY LINK_FLAGS "-pthread ${UECC_LDFLAGS_OTHER} ${NACL_LDFLAGS_OTHER} ${OPENSSL_CRYPTO_LDFLAGS_OTHER}") +set_property(TARGET fastd APPEND PROPERTY INCLUDE_DIRECTORIES ${CAP_INCLUDE_DIR} ${NACL_INCLUDE_DIRS}) +target_link_libraries(fastd protocols methods ciphers macs ${RT_LIBRARY} ${CAP_LIBRARY} ${UECC_LIBRARIES} ${NACL_LIBRARIES} ${OPENSSL_CRYPTO_LIBRARIES}) add_dependencies(fastd version) diff --git a/src/fastd.c b/src/fastd.c index a6b8310..bbc5c61 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -51,8 +51,8 @@ #include #endif -#ifdef ENABLE_SYSTEMD -#include +#ifdef USE_SYSTEMD +#include #endif @@ -873,11 +873,42 @@ static int daemonize(fastd_context_t *ctx) { return -1; } +#ifdef USE_SYSTEMD +static void notify_systemd(fastd_context_t *ctx, const char *notify_socket) { + int fd; + struct sockaddr_un sa = {}; + + if ((notify_socket[0] != '@' && notify_socket[0] != '/') || notify_socket[1] == 0) + return; + + fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); + if (fd < 0) + return; + + sa.sun_family = AF_UNIX; + + strncpy(sa.sun_path, notify_socket, sizeof(sa.sun_path)); + if (sa.sun_path[0] == '@') + sa.sun_path[0] = 0; + + if (connect(fd, (struct sockaddr*)&sa, offsetof(struct sockaddr_un, sun_path) + strnlen(notify_socket, sizeof(sa.sun_path))) < 0) { + pr_debug_errno(ctx, "unable to connect to notify socket: connect"); + close(fd); + return; + } + + dprintf(fd, "READY=1\nMAINPID=%lu", (unsigned long) getpid()); + pr_debug(ctx, "sent startup notification to systemd"); + + close(fd); +} +#endif + int main(int argc, char *argv[]) { fastd_context_t ctx = {}; int status_fd = -1; -#ifdef ENABLE_SYSTEMD +#ifdef USE_SYSTEMD char *notify_socket = getenv("NOTIFY_SOCKET"); if (notify_socket) { @@ -959,10 +990,9 @@ int main(int argc, char *argv[]) { write_pid(&ctx, getpid()); -#ifdef ENABLE_SYSTEMD +#ifdef USE_SYSTEMD if (notify_socket) { - setenv("NOTIFY_SOCKET", notify_socket, 1); - sd_notifyf(1, "READY=1\nMAINPID=%lu", (unsigned long) getpid()); + notify_systemd(&ctx, notify_socket); free(notify_socket); } #endif diff --git a/src/fastd_config.h.in b/src/fastd_config.h.in index 6851c25..4402139 100644 --- a/src/fastd_config.h.in +++ b/src/fastd_config.h.in @@ -37,6 +37,7 @@ #cmakedefine USE_PKTINFO #cmakedefine USE_PACKET_MARK #cmakedefine USE_MULTIAF_BIND +#cmakedefine USE_SYSTEMD #cmakedefine WITH_CAPABILITIES @@ -48,7 +49,6 @@ #cmakedefine HAVE_LIBSODIUM #cmakedefine ENABLE_OPENSSL -#cmakedefine ENABLE_SYSTEMD #define MAX_CONFIG_DEPTH @MAX_CONFIG_DEPTH_NUM@ -- cgit v1.2.3