From 34471d85fca14a5ec359d2d06a8d7018cb23beb2 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Wed, 5 Oct 2022 10:23:44 +0200 Subject: systemd: add support for socket activation via systemd (#401) dlt-daemon now support activation via systemd socket. This allows dlt-daemon to be started as soon as an application is requesting access to the dlt socket. This commits implements opening the socket only when UNIX_SOCKET is used for IPC Signed-off-by: Alexander Mohr Signed-off-by: Alexander Mohr --- src/daemon/CMakeLists.txt | 3 +++ src/daemon/dlt_daemon_unix_socket.c | 50 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) (limited to 'src') diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txt index e4e96bd..f7aaa26 100644 --- a/src/daemon/CMakeLists.txt +++ b/src/daemon/CMakeLists.txt @@ -61,6 +61,9 @@ endif(WITH_UDP_CONNECTION) add_executable(dlt-daemon ${dlt_daemon_SRCS} ${systemd_SRCS}) target_link_libraries(dlt-daemon ${RT_LIBRARY} ${SOCKET_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) +if (WITH_SYSTEMD_SOCKET_ACTIVATION) + target_link_libraries(dlt-daemon systemd) +endif() install(TARGETS dlt-daemon RUNTIME DESTINATION bin diff --git a/src/daemon/dlt_daemon_unix_socket.c b/src/daemon/dlt_daemon_unix_socket.c index ea889b0..4b9f472 100644 --- a/src/daemon/dlt_daemon_unix_socket.c +++ b/src/daemon/dlt_daemon_unix_socket.c @@ -38,6 +38,9 @@ #include #include #include +#if DLT_SYSTEM_SOCKET_ACTIVATION_ENABLE +#include +#endif #include "dlt-daemon.h" #include "dlt_common.h" @@ -91,6 +94,48 @@ int dlt_daemon_unix_socket_open(int *sock, char *sock_path, int type, int mask) return -1; } +#ifdef DLT_SYSTEM_SOCKET_ACTIVATION_ENABLE + + char **names = NULL; + const int num_fds = sd_listen_fds_with_names(0, &names); + bool sd_socket_open = false; + int i; + + if (num_fds <= 0) { + dlt_vlog(LOG_WARNING, "unix socket: no sockets configured via systemd, error: %s\n", strerror(errno)); + } else { + for (i = 0; i < num_fds; ++i) { + if (strcmp(sock_path, names[i]) != 0) { + continue; + } + + if (sd_is_socket_unix(i + SD_LISTEN_FDS_START, type, 1, names[i], strlen(names[i])) < 0) { + dlt_vlog(LOG_WARNING, + "unix socket: socket with matching name is not of correct type or not in listen mode, error: %s\n", + strerror(errno)); + continue; + } + + *sock = i + SD_LISTEN_FDS_START; + sd_socket_open = true; + dlt_vlog(LOG_INFO, "unix socket: sock_path %s found systemd socket %s\n", sock_path, names[i]); + break; + } + + /* + * The caller [of sd_listen_fds_with_names] needs to free the array + * itself and each of its elements with libc's free() call after use. + * */ + for (i = 0; i < num_fds; ++i) { + free(names[i]); + } + free(names); + } + + if (!sd_socket_open) { + dlt_vlog(LOG_INFO, "unix socket: sock_path %s no systemd socket found\n", sock_path); +#endif + if ((*sock = socket(AF_UNIX, type, 0)) == -1) { dlt_log(LOG_WARNING, "unix socket: socket() error"); return -1; @@ -118,6 +163,11 @@ int dlt_daemon_unix_socket_open(int *sock, char *sock_path, int type, int mask) /* restore permissions */ umask(old_mask); +#ifdef DLT_SYSTEM_SOCKET_ACTIVATION_ENABLE + } // end of: if (!sd_socket_open) { +#endif + + return 0; } -- cgit v1.2.1