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 2/3] publish: Support systemd-style socket activation.
Date: Sat,  9 Apr 2022 11:13:39 +0200	[thread overview]
Message-ID: <20220409091340.7013-2-ludo@gnu.org> (raw)
In-Reply-To: <20220409091340.7013-1-ludo@gnu.org>

* guix/scripts/publish.scm (systemd-socket): New procedure.
(guix-publish): Add 'style' variable.  Adjust startup message depending
on whether STYLE is 'systemd.
* doc/guix.texi (Invoking guix publish): Mention socket activation.
---
 doc/guix.texi            |  5 +++++
 guix/scripts/publish.scm | 34 +++++++++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 59db00e581..16bd858a3b 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -14041,6 +14041,11 @@ spawn an HTTP server on port 8080:
 guix publish
 @end example
 
+@cindex socket activation, for @command{guix publish}
+@command{guix publish} can also be started following the systemd
+``socket activation'' protocol (@pxref{Service De- and Constructors,
+@code{make-systemd-constructor},, shepherd, The GNU Shepherd Manual}).
+
 Once a publishing server has been authorized, the daemon may download
 substitutes from it.  @xref{Getting Substitutes from Other Servers}.
 
diff --git a/guix/scripts/publish.scm b/guix/scripts/publish.scm
index d6eb65d912..a4b9565f91 100644
--- a/guix/scripts/publish.scm
+++ b/guix/scripts/publish.scm
@@ -1235,6 +1235,23 @@ (define (open-server-socket address)
     (bind sock address)
     sock))
 
+(define (systemd-socket)
+  "If this program is being spawned through systemd-style \"socket
+activation\", whereby the listening socket is passed as file descriptor 3,
+return the corresponding socket."
+  (and (equal? (and=> (getenv "LISTEN_PID") string->number)
+               (getpid))
+       (match (getenv "LISTEN_FDS")
+         ((= string->number 1)
+          (let ((sock (fdopen 3 "r+0")))
+            (configure-socket sock)
+            sock))
+         ((= string->number (? integer? n))
+          (leave (G_ "~a: unexpected number of startup file descriptors")
+                 n))
+         (_
+          #f))))
+
 (define (gather-user-privileges user)
   "Switch to the identity of USER, a user name."
   (catch 'misc-error
@@ -1280,7 +1297,12 @@ (define-command (guix-publish . args)
                       (make-socket-address (sockaddr:fam addr)
                                            (sockaddr:addr addr)
                                            port)))
-           (socket  (open-server-socket address))
+           (socket style (match (systemd-socket)
+                           (#f
+                            (values (open-server-socket address)
+                                    'normal))
+                           (socket
+                            (values socket 'systemd))))
            (nar-path  (assoc-ref opts 'nar-path))
            (repl-port (assoc-ref opts 'repl))
            (cache     (assoc-ref opts 'cache))
@@ -1305,10 +1327,12 @@ (define-command (guix-publish . args)
                      (cache-bypass-threshold
                       (or (assoc-ref opts 'cache-bypass-threshold)
                           (cache-bypass-threshold))))
-        (info (G_ "publishing ~a on ~a, port ~d~%")
-              %store-directory
-              (inet-ntop (sockaddr:fam address) (sockaddr:addr address))
-              (sockaddr:port address))
+        (if (eq? style 'systemd)
+            (info (G_ "publishing (started via socket activation)~%"))
+            (info (G_ "publishing ~a on ~a, port ~d~%")
+                  %store-directory
+                  (inet-ntop (sockaddr:fam address) (sockaddr:addr address))
+                  (sockaddr:port address)))
 
         (for-each (lambda (compression)
                     (info (G_ "using '~a' compression method, level ~a~%")
-- 
2.35.1





  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   ` Ludovic Courtès [this message]
2022-04-09  9:20     ` [bug#54811] [PATCH 2/3] publish: Support systemd-style socket activation 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   ` [bug#54811] [PATCH 3/3] daemon: Support systemd-style socket activation Ludovic Courtès
2022-04-09  9:29     ` 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-2-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).