From patchwork Tue May 24 00:01:48 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Schiffer X-Patchwork-Id: 625445 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rDG1J1D52z9sdQ for ; Tue, 24 May 2016 10:07:00 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1b4zne-00041N-La; Tue, 24 May 2016 00:02:18 +0000 Received: from chaos.universe-factory.net ([37.72.148.22]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1b4znb-0003tk-48 for lede-dev@lists.infradead.org; Tue, 24 May 2016 00:02:16 +0000 Received: from avalon.localdomain (unknown [IPv6:fd1b:c28a:2fd6::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by chaos.universe-factory.net (Postfix) with ESMTPSA id 68E43183CC2; Tue, 24 May 2016 02:01:52 +0200 (CEST) From: Matthias Schiffer To: openwrt-devel@lists.openwrt.org, lede-dev@lists.infradead.org, nbd@nbd.name Date: Tue, 24 May 2016 02:01:48 +0200 Message-Id: <95e2dc0c4042f956767757c8204fc616cf9435b8.1464048108.git.mschiffer@universe-factory.net> X-Mailer: git-send-email 2.8.2 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160523_170215_516330_116E092C X-CRM114-Status: GOOD ( 12.74 ) X-Spam-Score: -3.3 (---) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-3.3 points) pts rule name description ---- ---------------------- -------------------------------------------------- -1.4 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Subject: [LEDE-DEV] [PATCH ubus] ubusd: fix systemd socket activation support X-BeenThere: lede-dev@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "Lede-dev" Errors-To: lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org 62cdfc3 added systemd units including a ubus.socket unit, but didn't actually add socket activation support to ubusd. This would cause the first connection that activated ubusd to hang (as ubusd ignored it), and stopping ubusd would break it completely (as ubusd removed the socket file). The ENABLE_SYSTEMD default is changed to OFF as the socket activation uses libsystemd, so setting ENABLE_SYSTEMD to ON will now require libsystemd. Signed-off-by: Matthias Schiffer --- CMakeLists.txt | 15 ++++++++++----- ubusd.c | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e21a046..faab342 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ ADD_DEFINITIONS(-Os -Wall -Werror --std=gnu99 -g3 -Wmissing-declarations) OPTION(BUILD_LUA "build Lua plugin" ON) OPTION(BUILD_EXAMPLES "build examples" ON) -OPTION(ENABLE_SYSTEMD "systemd support" ON) +OPTION(ENABLE_SYSTEMD "systemd support" OFF) SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "") SET(UBUS_UNIX_SOCKET "/var/run/ubus.sock") @@ -60,8 +60,13 @@ SET(UBUSD_BINARY "${CMAKE_INSTALL_PREFIX}/sbin/ubusd") # do this after the installs so we have the proper paths IF(ENABLE_SYSTEMD) INCLUDE(FindPkgConfig) - PKG_CHECK_MODULES(SYSTEMD systemd) - IF(SYSTEMD_FOUND) - ADD_SUBDIRECTORY(systemd) - ENDIF() + PKG_CHECK_MODULES(SYSTEMD libsystemd REQUIRED) + + SET_PROPERTY(TARGET ubusd APPEND PROPERTY COMPILE_FLAGS "${SYSTEMD_CFLAGS}") + SET_PROPERTY(TARGET ubusd APPEND PROPERTY LINK_FLAGS "${SYSTEMD_LDFLAGS}") + SET_PROPERTY(TARGET ubusd APPEND PROPERTY INCLUDE_DIRECTORIES ${SYSTEMD_INCLUDE_DIRS}) + TARGET_LINK_LIBRARIES(ubusd ${SYSTEMD_LIBRARIES}) + ADD_DEFINITIONS( -DENABLE_SYSTEMD) + + ADD_SUBDIRECTORY(systemd) ENDIF() diff --git a/ubusd.c b/ubusd.c index 7279a70..5b1d52c 100644 --- a/ubusd.c +++ b/ubusd.c @@ -22,6 +22,9 @@ #include #include #include +#ifdef ENABLE_SYSTEMD +#include +#endif #include #include @@ -380,8 +383,12 @@ static void sighup_handler(int sig) int main(int argc, char **argv) { const char *ubus_socket = UBUS_UNIX_SOCKET; + bool remove_socket = true; int ret = 0; int ch; +#ifdef ENABLE_SYSTEMD + int n_fds; +#endif signal(SIGPIPE, SIG_IGN); signal(SIGHUP, sighup_handler); @@ -402,19 +409,37 @@ int main(int argc, char **argv) } } - unlink(ubus_socket); - umask(0111); - server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL); - if (server_fd.fd < 0) { - perror("usock"); - ret = -1; +#ifdef ENABLE_SYSTEMD + n_fds = sd_listen_fds(1); + if (n_fds > 1) { + fprintf(stderr, "Too many file descriptors received.\n"); + ret = -1; goto out; + } else if (n_fds == 1) { + server_fd.fd = SD_LISTEN_FDS_START + 0; + fcntl(server_fd.fd, F_SETFD, fcntl(server_fd.fd, F_GETFD) | FD_CLOEXEC); + fcntl(server_fd.fd, F_SETFL, fcntl(server_fd.fd, F_GETFL) | O_NONBLOCK); + + remove_socket = false; + } else +#endif + { + unlink(ubus_socket); + umask(0111); + server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL); + if (server_fd.fd < 0) { + perror("usock"); + ret = -1; + goto out; + } } uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER); ubusd_acl_load(); uloop_run(); - unlink(ubus_socket); + + if (remove_socket) + unlink(ubus_socket); out: uloop_done();