[Libguestfs] [PATCH libnbd] generator: Pass LISTEN_FDNAMES=nbd with systemd socket activation

Richard W.M. Jones rjones at redhat.com
Sat Jan 28 12:47:30 UTC 2023


systemd allows sockets passed through socket activation to be named
with the protocol they require.  We only ever pass one socket, name
it.  This environment variable is currently ignored by qemu-nbd and
nbdkit, but might be used by qemu-storage-daemon:

https://lists.nongnu.org/archive/html/qemu-devel/2023-01/msg06114.html
---
 generator/states-connect-socket-activation.c | 41 +++++++++++---------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/generator/states-connect-socket-activation.c b/generator/states-connect-socket-activation.c
index 9a83834915..22f06d4fd3 100644
--- a/generator/states-connect-socket-activation.c
+++ b/generator/states-connect-socket-activation.c
@@ -34,16 +34,18 @@
 /* This is baked into the systemd socket activation API. */
 #define FIRST_SOCKET_ACTIVATION_FD 3
 
-/* == strlen ("LISTEN_PID=") | strlen ("LISTEN_FDS=") */
-#define PREFIX_LENGTH 11
-
 extern char **environ;
 
 /* Prepare environment for calling execvp when doing systemd socket
  * activation.  Takes the current environment and copies it.  Removes
- * any existing LISTEN_PID or LISTEN_FDS and replaces them with new
- * variables.  env[0] is "LISTEN_PID=..." which is filled in by
- * CONNECT_SA.START, and env[1] is "LISTEN_FDS=1".
+ * any existing LISTEN_PID, LISTEN_FDS or LISTEN_FDNAMES, and replaces
+ * them with new variables.
+ *
+ * env[0] is "LISTEN_PID=..." which is filled in by CONNECT_SA.START
+ *
+ * env[1] is "LISTEN_FDS=1"
+ *
+ * env[2] is "LISTEN_FDNAMES=nbd"
  */
 static int
 prepare_socket_activation_environment (string_vector *env)
@@ -53,26 +55,29 @@ prepare_socket_activation_environment (string_vector *env)
 
   assert (env->len == 0);
 
-  /* Reserve slots env[0] and env[1]. */
+  /* Reserve slots env[0]..env[2] */
+  if (string_vector_reserve (env, 3) == -1)
+    goto err;
   p = strdup ("LISTEN_PID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
   if (p == NULL)
     goto err;
-  if (string_vector_append (env, p) == -1) {
-    free (p);
-    goto err;
-  }
+  string_vector_append (env, p);
   p = strdup ("LISTEN_FDS=1");
   if (p == NULL)
     goto err;
-  if (string_vector_append (env, p) == -1) {
-    free (p);
+  string_vector_append (env, p);
+  p = strdup ("LISTEN_FDNAMES=nbd");
+  if (p == NULL)
     goto err;
-  }
+  string_vector_append (env, p);
 
-  /* Append the current environment, but remove LISTEN_PID, LISTEN_FDS. */
+  /* Append the current environment, but remove the special
+   * environment variables.
+   */
   for (i = 0; environ[i] != NULL; ++i) {
-    if (strncmp (environ[i], "LISTEN_PID=", PREFIX_LENGTH) != 0 &&
-        strncmp (environ[i], "LISTEN_FDS=", PREFIX_LENGTH) != 0) {
+    if (strncmp (environ[i], "LISTEN_PID=", 11) != 0 &&
+        strncmp (environ[i], "LISTEN_FDS=", 11) != 0 &&
+        strncmp (environ[i], "LISTEN_FDNAMES=", 15) != 0) {
       char *copy = strdup (environ[i]);
       if (copy == NULL)
         goto err;
@@ -194,7 +199,7 @@  CONNECT_SA.START:
     char buf[32];
     const char *v =
       nbd_internal_fork_safe_itoa ((long) getpid (), buf, sizeof buf);
-    strcpy (&env.ptr[0][PREFIX_LENGTH], v);
+    strcpy (&env.ptr[0][strlen ("LISTEN_FDS=")], v);
 
     /* Restore SIGPIPE back to SIG_DFL. */
     signal (SIGPIPE, SIG_DFL);
-- 
2.39.0



More information about the Libguestfs mailing list