From patchwork Sat Apr 9 09:13:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 38464 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 3BD2527BBEB; Sat, 9 Apr 2022 10:14:29 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id BFA6D27BBEA for ; Sat, 9 Apr 2022 10:14:28 +0100 (BST) Received: from localhost ([::1]:42472 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nd7Ap-0000Jf-Tm for patchwork@mira.cbaines.net; Sat, 09 Apr 2022 05:14:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53838) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nd7AR-0008Rp-HG for guix-patches@gnu.org; Sat, 09 Apr 2022 05:14:03 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:42825) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nd7AR-0003lc-8K for guix-patches@gnu.org; Sat, 09 Apr 2022 05:14:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nd7AR-0008OE-4G for guix-patches@gnu.org; Sat, 09 Apr 2022 05:14:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#54811] [PATCH 3/3] daemon: Support systemd-style socket activation. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 09 Apr 2022 09:14:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54811 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 54811@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 54811-submit@debbugs.gnu.org id=B54811.164949563732210 (code B ref 54811); Sat, 09 Apr 2022 09:14:03 +0000 Received: (at 54811) by debbugs.gnu.org; 9 Apr 2022 09:13:57 +0000 Received: from localhost ([127.0.0.1]:36719 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nd7AL-0008NR-Eu for submit@debbugs.gnu.org; Sat, 09 Apr 2022 05:13:57 -0400 Received: from eggs.gnu.org ([209.51.188.92]:60506) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nd7AJ-0008Mm-Ms for 54811@debbugs.gnu.org; Sat, 09 Apr 2022 05:13:56 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:46964) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nd7AC-0003kN-Mv; Sat, 09 Apr 2022 05:13:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=aXuSOoUD9PFCYxpT6+JbDOPQcLH7omCCf5f8oHqBu1s=; b=j/BNaR5/P/EASeBJz0or jXIueR6NdAmbjp214A9ODRbBY2GzWZXS8EX5Fr/767LQo3toaYpzq4KWFbftTTky7tnVovcKu1e/T dxJXCFNC60zxSuqMEWr9ckbHzZF+tSmkMuvwD9YOV7jKRgRQLKNKgpW1O/IssUrGCtX8zbCQXNvw9 VTB5xRAvDvdg6aJeRs78QxB1P9VfLvKbAio61gTR3TW/gg9DVuoY9LOMVLAZJAs52ZZTh1bFho4LX N+ADNazKHTlUNb9jwDUfx/lVFMQ1v+OJqnstq9AxiJtHzvZAgXCPAHUzOG6xZ4xTn/EA7ZyDM4KCd g2R3saDgryIMsA==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:58682 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nd7AC-00086k-AM; Sat, 09 Apr 2022 05:13:48 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Sat, 9 Apr 2022 11:13:40 +0200 Message-Id: <20220409091340.7013-3-ludo@gnu.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220409091340.7013-1-ludo@gnu.org> References: <20220409091340.7013-1-ludo@gnu.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * nix/nix-daemon/guix-daemon.cc (SD_LISTEN_FDS_START): New macro. (systemd_activation_sockets): New function. (main): Use it. Remove obsolete 'printMsg' call. * doc/guix.texi (Invoking guix-daemon): Document socket activation. --- doc/guix.texi | 6 ++++- nix/nix-daemon/guix-daemon.cc | 42 ++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 16bd858a3b..81fec35eb7 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -1554,7 +1554,11 @@ is normally run as @code{root} like this: # guix-daemon --build-users-group=guixbuild @end example -@noindent +@cindex socket activation, for @command{guix-daemon} +This daemon can also be started following the systemd ``socket +activation'' protocol (@pxref{Service De- and Constructors, +@code{make-systemd-constructor},, shepherd, The GNU Shepherd Manual}). + For details on how to set it up, @pxref{Setting Up the Daemon}. @cindex chroot diff --git a/nix/nix-daemon/guix-daemon.cc b/nix/nix-daemon/guix-daemon.cc index 36a06a3fae..d7ab9c5e64 100644 --- a/nix/nix-daemon/guix-daemon.cc +++ b/nix/nix-daemon/guix-daemon.cc @@ -1,5 +1,5 @@ /* GNU Guix --- Functional package management for GNU - Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2021 Ludovic Courtès + Copyright (C) 2012-2019, 2021-2022 Ludovic Courtès Copyright (C) 2006, 2010, 2012, 2014 Eelco Dolstra This file is part of GNU Guix. @@ -434,6 +434,31 @@ listening_sockets (const std::list &options) return result; } +/* First file descriptor provided at startup using systemd-style socket + activation. */ +#define SD_LISTEN_FDS_START 3 + +/* Return a list of file descriptors of listening sockets provided following + the systemd "socket activation" protocol. Return the empty list if we are + not being socket-activated. */ +static std::vector +systemd_activation_sockets () +{ + std::vector result; + + if (getEnv ("LISTEN_PID") == std::to_string (getpid ())) + { + unsigned int fdCount; + if (string2Int (getEnv ("LISTEN_FDS"), fdCount)) + { + for (unsigned int i = 0; i < fdCount; i++) + result.push_back (SD_LISTEN_FDS_START + i); + } + } + + return result; +} + int main (int argc, char *argv[]) @@ -494,7 +519,17 @@ main (int argc, char *argv[]) argp_parse (&argp, argc, argv, 0, 0, 0); - auto sockets = listening_sockets (listen_options); + auto sockets = systemd_activation_sockets (); + if (sockets.empty ()) + /* We were not "socket-activated" so open the sockets specified by + LISTEN_OPTIONS. */ + sockets = listening_sockets (listen_options); + else + printMsg (lvlInfo, + format (ngettext ("socket-activated with %1% socket", + "socket-activated with %1% sockets", + sockets.size ())) + % sockets.size ()); /* Effect all the changes made via 'settings.set'. */ settings.update (); @@ -531,9 +566,6 @@ using `--build-users-group' is highly recommended\n")); format ("automatic deduplication set to %1%") % settings.autoOptimiseStore); - printMsg (lvlDebug, - format ("listening on `%1%'") % settings.nixDaemonSocketFile); - run (sockets); } catch (std::exception &e)