unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: 54811@debbugs.gnu.org
Cc: "Ludovic Courtès" <ludo@gnu.org>
Subject: [bug#54811] [PATCH 3/3] daemon: Support systemd-style socket activation.
Date: Sat,  9 Apr 2022 11:13:40 +0200	[thread overview]
Message-ID: <20220409091340.7013-3-ludo@gnu.org> (raw)
In-Reply-To: <20220409091340.7013-1-ludo@gnu.org>

* 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 <ludo@gnu.org>
+   Copyright (C) 2012-2019, 2021-2022 Ludovic Courtès <ludo@gnu.org>
    Copyright (C) 2006, 2010, 2012, 2014 Eelco Dolstra <e.dolstra@tudelft.nl>
 
    This file is part of GNU Guix.
@@ -434,6 +434,31 @@ listening_sockets (const std::list<std::string> &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<int>
+systemd_activation_sockets ()
+{
+  std::vector<int> 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;
+}
+
 \f
 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)
-- 
2.35.1





  parent reply	other threads:[~2022-04-09  9:14 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-09  9:12 [bug#54811] [PATCH 0/3] Support socket activation in 'guix publish' and 'guix-daemon' Ludovic Courtès
2022-04-09  9:13 ` [bug#54811] [PATCH 1/3] publish: Use SRFI-71 instead of SRFI-11 Ludovic Courtès
2022-04-09  9:13   ` [bug#54811] [PATCH 2/3] publish: Support systemd-style socket activation Ludovic Courtès
2022-04-09  9:20     ` Maxime Devos
2022-04-14 12:51       ` bug#54811: [PATCH 0/3] Support socket activation in 'guix publish' and 'guix-daemon' Ludovic Courtès
2022-04-09  9:13   ` Ludovic Courtès [this message]
2022-04-09  9:29     ` [bug#54811] [PATCH 3/3] daemon: Support systemd-style socket activation Maxime Devos
2022-04-09 20:28       ` [bug#54811] [PATCH 0/3] Support socket activation in 'guix publish' and 'guix-daemon' Ludovic Courtès
2022-04-09  9:19 ` Maxime Devos
2022-04-09 20:30   ` Ludovic Courtès
2022-04-09 20:49     ` Maxime Devos
2022-04-10 17:34       ` Ludovic Courtès
2022-04-10 17:43         ` Maxime Devos
2022-04-11  9:48           ` Ludovic Courtès
2022-04-11 10:06             ` Maxime Devos
2022-04-11 20:33               ` Ludovic Courtès
2022-04-12  8:42                 ` Maxime Devos
2022-04-12 10:15                   ` Ludovic Courtès
2022-04-12  8:48                 ` Maxime Devos
2022-04-11 10:08             ` Maxime Devos
2022-04-11 20:34               ` Ludovic Courtès
2022-04-11 10:10             ` Maxime Devos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220409091340.7013-3-ludo@gnu.org \
    --to=ludo@gnu.org \
    --cc=54811@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).