From patchwork Fri Sep 22 14:42:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Storm, Christian" X-Patchwork-Id: 817553 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:400c:c0c::238; helo=mail-wr0-x238.google.com; envelope-from=swupdate+bncbdd6bwv65qpbbcgesthakgqe2uzdjpa@googlegroups.com; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b="SfpiD0bs"; dkim-atps=neutral Received: from mail-wr0-x238.google.com (mail-wr0-x238.google.com [IPv6:2a00:1450:400c:c0c::238]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3xzGVg0wGDz9sPm for ; Sat, 23 Sep 2017 00:45:30 +1000 (AEST) Received: by mail-wr0-x238.google.com with SMTP id k10sf149990wrk.23 for ; Fri, 22 Sep 2017 07:45:30 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1506091528; cv=pass; d=google.com; s=arc-20160816; b=x45TYoqyrG0sR351vseB+RUpaSXEDr9zVdlFPcmUPCFimBPEFCzJKFGz+ebSQeRSgV K1Vc/4bDvxj8l5y5REtJPkB+Vkspog8V7smRom5wL+zmH3YQf6jZnQUu0vFWrjJXlx35 2Ij7JKzSip3LCbWrcZrlBtigwJyn5WQHEw9TIQkMSt5495s74OtzGtGsSn9JZ2jfmbdQ Qn8A25JEshpOshYDkQkgkAF5xM02QA3ctfx+3/ZB6cIvz2w8dIggCLnipRGndD6ryiOM Kakz9Y7bAaqmmWYlIFhQNJhuh6WFYf5GqlZEYDLn5kE5pCcVzJZc9JS8PqYz9PPaRz8e LjBA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:arc-authentication-results :arc-message-signature:mime-version:sender:dkim-signature :arc-authentication-results; bh=1XmijuXLh3N2HgoBFH9ITTX9eHI3uFnQIB4NUgqt+KA=; b=k6eNyXwY/hKHjQVfeOKvEVjNbUd93A/SdfZAfaD0jJAtmD66H9PDY9E2bKMY8KchZ5 Yqt40jzFWrLtiohdoSSmpfkj2d3r0mOGewfpKSfID5xjvq5ZK9PgFJlQd90jHqqRaFSo o/VZg1/+x0U07lQh6daK/to/30cpYg2FJMbS8VD9s985nen4wQ4HFkWVqCUTm9kN+o9A kTS6GNerrcUAhI8gSMgxL7kgUhbZRe1L7KLDx1WkyNMV0u+1Um1TSu8FyTLOz2y3SFjQ Z1kXNwQJhP69fphW6obnN70tehqBY9rRk0GK4F7P7JccTx0zdLrJycHvXuNgvFieiasV 5EzA== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 192.35.17.28 is neither permitted nor denied by domain of christian.storm@siemens.com) smtp.mailfrom=christian.storm@siemens.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=1XmijuXLh3N2HgoBFH9ITTX9eHI3uFnQIB4NUgqt+KA=; b=SfpiD0bsHb1QObD5sdb6p5xXeBzZW9eSEDtCb8LxhsLSJ/KS0Mgf9mfA6EthV842HU aWgPW3j6wbJWsebFJDzHWpOnONwHI5kKc8XKmVjvfeo8UhwNOv45gGqnWkgiOIw/39nc wAXzFRmdIztQ59oLrYMC4Z9bf5ZfghpzTPKA3gI0HFd8x4VFygMAOXng3sA0vZW4uUBu 0+AoAYKxXzF6Dlx0ZgXC7+mywQ+hP2rvQ2mEYIU4OaKSZ1EgUniDAPTUxw797XihOkaU uvv7bGNOMBrncaUmQykxvh0P4ezHmHkVzIxrslqVxRxqoPYiNcVebb7XpSHSGRjghDnw CcrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=sender:x-gm-message-state:mime-version:from:to:cc:subject:date :message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=1XmijuXLh3N2HgoBFH9ITTX9eHI3uFnQIB4NUgqt+KA=; b=NNgCFoBUDHLsJL1VJwFPyZOwFgPxBcmH+w0onl5oJk5YkreA/QCOtOTy6xDsAOGMwm 4F9JIIGUUmVj8Dq7RC5Mf6E5S9t4shotsgo+u8LFyyVXTHGtygCWjofRuVx4mp8ruxrC ZwKKVLb+zUeutIVYQ42wK6ZwJ1UD0uz8/jBYNMtpONGgAWy6i197k3VXdqgKif/T/1y9 mD1c1zdY3SZKrjXIQ/e94f1V5m/JQKqaspmZMh3UOBWR9drmEueqNQO1uBC7udok2+bC +YT17+KOehE3ZuVmHBBv7aiCWLdg7MVjYbN4mq0UDnTnA5SqQ1M12YQpZEdn7vAk55Gb P/Pg== Sender: swupdate@googlegroups.com X-Gm-Message-State: AHPjjUjhzYg3Zkzchyfj7gYgxXKYkpINMDySGKjqGMMsLbgYE1/iP/MI RH7yzGP1flHEuE4SCGZDPtY= X-Google-Smtp-Source: AOwi7QBmsk0dYwkfvB/ljAg/T63A14tLTj3DeaJq+t5MK1Y7/7XU/J3QSiqdSG8hJl/7aFUaI9c/nQ== X-Received: by 10.28.27.83 with SMTP id b80mr11334wmb.25.1506091528341; Fri, 22 Sep 2017 07:45:28 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: swupdate@googlegroups.com Received: by 10.223.161.208 with SMTP id v16ls1398495wrv.8.gmail; Fri, 22 Sep 2017 07:45:27 -0700 (PDT) X-Received: by 10.28.184.81 with SMTP id i78mr232700wmf.2.1506091527856; Fri, 22 Sep 2017 07:45:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1506091527; cv=none; d=google.com; s=arc-20160816; b=WEUnSi9l1CXjc+LQN9sXIIk90Z+zqzS4VqTZSSvHUMpCUr0VfLBWKzE8v2FEO2ktP7 HG6ceJNiR+O0H4TwtGjg75BVInmRDP3jsGv5jk8569wIbVqZ5ZA4d4a2ylTis6tcQlfj hd3pX5c1ZIn7FpdmVQaAJW0L6M1s8bzb+dsJDLOXfQXJ7G4545fcPnH3RjWY4qpNg4kA cTG+4hOO9ATdVqWugjWM/F9rBL/EDHHfA9r1zsa0GvKlZ0tF27FHqhddTOJvgahQXRDP imPU/zppB2ruhHnatqnrvApbstkhVx9TdAuPZX7dgbTd6t0kz8nbNC6P2RFsHFgSNty7 Fz7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=ghIUdNNhKGbpqXatPg2xLjrPKsrfGh2UC8D5IudB1dA=; b=qz8nPiRvwY4WshrHH0UteMirc3fIqZp0Dso+AIXTiC11VQjhzxiHgMJFbhV22MF1CT hgsxSPGColX4JeLPkHDu1Qlmi5ibUZ9T5RAz0y2G0LSSybmLpfSrBEaSpJy+OZ1ygfGo hPIXXoLkdpDTYCgLDiPB68gZrto0pBM/v1xluPGr7TJmRHsoHo/kkeuTpt5zWmje6A/C EPbolZvtQNQG2+Wl6vWpy1G4eAnHVhSMe7tUZ8CfCPnRCVz73OgQdRMCsFHiwSkE09Zl uW2pHbRImdtwagwIeJcCICCszJ0SPNwt31MbPHSUD7tEZVLTg3lOjnUuec+zlhLaNIw7 slfA== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 192.35.17.28 is neither permitted nor denied by domain of christian.storm@siemens.com) smtp.mailfrom=christian.storm@siemens.com Received: from goliath.siemens.de (goliath.siemens.de. [192.35.17.28]) by gmr-mx.google.com with ESMTPS id 200si171666wmj.0.2017.09.22.07.45.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 22 Sep 2017 07:45:27 -0700 (PDT) Received-SPF: neutral (google.com: 192.35.17.28 is neither permitted nor denied by domain of christian.storm@siemens.com) client-ip=192.35.17.28; Received: from mail1.siemens.de (mail1.siemens.de [139.23.33.14]) by goliath.siemens.de (8.15.2/8.15.2) with ESMTPS id v8MEjR2p004509 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 22 Sep 2017 16:45:27 +0200 Received: from MD1KR9XC.ww002.siemens.net ([139.25.69.251]) by mail1.siemens.de (8.15.2/8.15.2) with ESMTP id v8MEjRlG008491; Fri, 22 Sep 2017 16:45:27 +0200 From: Christian Storm To: swupdate@googlegroups.com Cc: Christian Storm Subject: [swupdate] [PATCH v2] systemd support Date: Fri, 22 Sep 2017 16:42:41 +0200 Message-Id: <20170922144241.23316-1-christian.storm@siemens.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170922143946.vandvh4yvqhu7ttj@MD1KR9XC.ww002.siemens.net> References: <20170922143946.vandvh4yvqhu7ttj@MD1KR9XC.ww002.siemens.net> X-Original-Sender: christian.storm@siemens.com X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 192.35.17.28 is neither permitted nor denied by domain of christian.storm@siemens.com) smtp.mailfrom=christian.storm@siemens.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Enable support for systemd's start-up completion notification and, optionally, socket-based activation. Signed-off-by: Christian Storm Reviewed-by: Stefano Babic --- Kconfig | 7 ++++++ Makefile.flags | 4 ++++ core/swupdate.c | 10 ++++++++ corelib/network_thread.c | 54 +++++++++++++++++++++++++++++++++--------- corelib/progress_thread.c | 13 ++++++++++ doc/source/swupdate.rst | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 137 insertions(+), 11 deletions(-) diff --git a/Kconfig b/Kconfig index f90d218..c6dde7f 100644 --- a/Kconfig +++ b/Kconfig @@ -61,6 +61,13 @@ menu "Swupdate Settings" menu "General Configuration" +config SYSTEMD + bool "enable systemd support" + default n + help + Enable support for systemd's start-up completion + notification and socket-based activation features. + config SCRIPTS bool "enable pre and postinstall scripts" default y diff --git a/Makefile.flags b/Makefile.flags index 391fc7f..3dbf379 100644 --- a/Makefile.flags +++ b/Makefile.flags @@ -165,6 +165,10 @@ ifeq ($(CONFIG_UBOOT),y) LDLIBS += z ubootenv endif +ifeq ($(CONFIG_SYSTEMD),y) +LDLIBS += systemd +endif + # suricatta ifneq ($(CONFIG_SURICATTA),) ifneq ($(CONFIG_SURICATTA_SSL),) diff --git a/core/swupdate.c b/core/swupdate.c index e35289e..ef94bd9 100644 --- a/core/swupdate.c +++ b/core/swupdate.c @@ -59,6 +59,10 @@ #include "pctl.h" #include "bootloader.h" +#ifdef CONFIG_SYSTEMD +#include +#endif + #define MODULE_NAME "swupdate" static pthread_t network_daemon; @@ -864,6 +868,12 @@ int main(int argc, char **argv) } } +#ifdef CONFIG_SYSTEMD + if (sd_booted()) { + sd_notify(0, "READY=1"); + } +#endif + /* * Install a handler for SIGTERM that cancels * the network_daemon thread to allow atexit() diff --git a/corelib/network_thread.c b/corelib/network_thread.c index 5ed516e..747ee1b 100644 --- a/corelib/network_thread.c +++ b/corelib/network_thread.c @@ -44,6 +44,10 @@ #include "pctl.h" #include "generated/autoconf.h" +#ifdef CONFIG_SYSTEMD +#include +#endif + #define LISTENQ 1024 #define NUM_CACHED_MESSAGES 100 @@ -109,20 +113,39 @@ static void network_notifier(RECOVERY_STATUS status, int error, const char *msg) int listener_create(const char *path, int type) { struct sockaddr_un servaddr; - int listenfd; - - listenfd = socket(AF_LOCAL, type, 0); - unlink(path); - bzero(&servaddr, sizeof(servaddr)); - servaddr.sun_family = AF_LOCAL; - strcpy(servaddr.sun_path, path); + int listenfd = -1; - if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { - close(listenfd); - return -1; +#ifdef CONFIG_SYSTEMD + if (sd_booted()) { + for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + sd_listen_fds(0); fd++) { + if (sd_is_socket_unix(fd, SOCK_STREAM, 1, path, 0)) { + listenfd = fd; + break; + } + } + if (listenfd == -1) { + TRACE("got no socket at %s from systemd", path); + } else { + TRACE("got socket fd=%d at %s from systemd", listenfd, path); + } } +#endif + + if (listenfd == -1) { + TRACE("creating socket at %s", path); + listenfd = socket(AF_LOCAL, type, 0); + unlink(path); + bzero(&servaddr, sizeof(servaddr)); + servaddr.sun_family = AF_LOCAL; + strcpy(servaddr.sun_path, path); - chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { + close(listenfd); + return -1; + } + + chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + } if (type == SOCK_STREAM) if (listen(listenfd, LISTENQ) < 0) { @@ -173,6 +196,15 @@ static void empty_pipe(int fd) static void unlink_socket(void) { +#ifdef CONFIG_SYSTEMD + if (sd_booted() && sd_listen_fds(0) > 0) { + /* + * There were socket fds handed-over by systemd, + * so don't delete the socket file. + */ + return; + } +#endif unlink((char*)CONFIG_SOCKET_CTRL_PATH); } diff --git a/corelib/progress_thread.c b/corelib/progress_thread.c index e0a11fe..d215166 100644 --- a/corelib/progress_thread.c +++ b/corelib/progress_thread.c @@ -43,6 +43,10 @@ #include #include "generated/autoconf.h" +#ifdef CONFIG_SYSTEMD +#include +#endif + struct progress_conn { SIMPLEQ_ENTRY(progress_conn) next; int sockfd; @@ -185,6 +189,15 @@ void swupdate_progress_done(const char *info) static void unlink_socket(void) { +#ifdef CONFIG_SYSTEMD + if (sd_booted() && sd_listen_fds(0) > 0) { + /* + * There were socket fds handed-over by systemd, + * so don't delete the socket file. + */ + return; + } +#endif unlink((char*)CONFIG_SOCKET_PROGRESS_PATH); } diff --git a/doc/source/swupdate.rst b/doc/source/swupdate.rst index 4df0b6b..5302e04 100644 --- a/doc/source/swupdate.rst +++ b/doc/source/swupdate.rst @@ -504,6 +504,66 @@ Command line parameters | | | downloading | +-------------+----------+--------------------------------------------+ + +systemd Integration +------------------- + +SWUpdate has optional systemd_ support via the compile-time +configuration switch ``CONFIG_SYSTEMD``. If enabled, SWUpdate +signals systemd about start-up completion and can make optional +use of systemd's socket-based activation feature. + +A sample systemd service unit file ``/etc/systemd/system/swupdate.service`` +may look like the following starting SWUpdate in suricatta daemon mode: + +:: + + [Unit] + Description=SWUpdate daemon + Documentation=https://github.com/sbabic/swupdate + Documentation=https://sbabic.github.io/swupdate + + [Service] + Type=notify + ExecStart=/usr/bin/swupdate -u '-t default -u http://localhost -i 25' + + [Install] + WantedBy=multi-user.target + +Started via ``systemctl start swupdate.service``, SWUpdate +(re)creates its sockets on startup. For using socket-based +activation, an accompanying systemd socket unit file +``/etc/systemd/system/swupdate.socket`` is required: + +:: + + [Unit] + Description=SWUpdate socket listener + Documentation=https://github.com/sbabic/swupdate + Documentation=https://sbabic.github.io/swupdate + + [Socket] + ListenStream=/tmp/sockinstctrl + ListenStream=/tmp/swupdateprog + + [Install] + WantedBy=sockets.target + +On ``swupdate.socket`` being started, systemd creates the socket +files and hands them over to SWUpdate when it starts. So, for +example, when talking to ``/tmp/swupdateprog``, systemd starts +``swupdate.service`` and hands-over the socket files. The socket +files are also handed over on a "regular" start of SWUpdate via +``systemctl start swupdate.service``. + +Note that the socket paths in the two ``ListenStream=`` directives +have to match the socket paths ``CONFIG_SOCKET_CTRL_PATH`` and +``CONFIG_SOCKET_PROGRESS_PATH`` in SWUpdate's configuration. +Here, the default socket path configuration is depicted. + +.. _systemd: https://www.freedesktop.org/wiki/Software/systemd/ + + Changes in boot-loader code ===========================