]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ssh-generator: split out common helper function
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 18 Nov 2025 10:19:41 +0000 (11:19 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 19 Nov 2025 10:37:48 +0000 (11:37 +0100)
src/ssh-generator/meson.build
src/ssh-generator/ssh-generator.c
src/ssh-generator/ssh-issue.c
src/ssh-generator/ssh-util.c [new file with mode: 0644]
src/ssh-generator/ssh-util.h [new file with mode: 0644]

index f281a251849198318fed43aa556c5e08fc06ffde..a7162e9b869a87b0a8c8735dae4a4e296d1c62c8 100644 (file)
@@ -3,15 +3,26 @@
 executables += [
         generator_template + {
                 'name' : 'systemd-ssh-generator',
-                'sources' : files('ssh-generator.c'),
+                'sources' : files(
+                        'ssh-generator.c',
+                        'ssh-util.c',
+                ),
+                'extract' : files(
+                        'ssh-util.c',
+                ),
         },
         libexec_template + {
                 'name' : 'systemd-ssh-proxy',
-                'sources' : files('ssh-proxy.c'),
+                'sources' : files(
+                        'ssh-proxy.c',
+                ),
         },
         libexec_template + {
                 'name' : 'systemd-ssh-issue',
-                'sources' : files('ssh-issue.c'),
+                'sources' : files(
+                        'ssh-issue.c',
+                ),
+                'objects' : ['systemd-ssh-generator'],
         },
 ]
 
index 8124999585f84869e4e4073cf7d22872f8c9d3fc..8454d31141d904545eccdb0e49633296ac414589 100644 (file)
@@ -17,6 +17,7 @@
 #include "socket-netlink.h"
 #include "socket-util.h"
 #include "special.h"
+#include "ssh-util.h"
 #include "string-util.h"
 #include "strv.h"
 #include "virt.h"
@@ -212,17 +213,9 @@ static int add_vsock_socket(
                 return 0;
         }
 
-        _cleanup_close_ int vsock_fd = socket(AF_VSOCK, SOCK_STREAM|SOCK_CLOEXEC, 0);
-        if (vsock_fd < 0) {
-                if (ERRNO_IS_NOT_SUPPORTED(errno)) {
-                        log_debug("Not creating AF_VSOCK ssh listener, since AF_VSOCK is not available.");
-                        return 0;
-                }
-
-                return log_error_errno(errno, "Unable to test if AF_VSOCK is available: %m");
-        }
-
-        vsock_fd = safe_close(vsock_fd);
+        r = vsock_open_or_warn(/* ret= */ NULL);
+        if (r <= 0)
+                return r;
 
         /* Determine the local CID so that we can log it to help users to connect to this VM */
         unsigned local_cid;
index 80c1679c4725e1b59389756f21260b880ffd6484..71cd596cf85bb133a62e9aa22a7f975d3239f4f0 100644 (file)
@@ -16,6 +16,7 @@
 #include "parse-argument.h"
 #include "pretty-print.h"
 #include "socket-util.h"
+#include "ssh-util.h"
 #include "string-util.h"
 #include "tmpfile-util.h"
 #include "virt.h"
@@ -135,18 +136,9 @@ static int acquire_cid(unsigned *ret_cid) {
                 return 0;
         }
 
-        _cleanup_close_ int vsock_fd = socket(AF_VSOCK, SOCK_STREAM|SOCK_CLOEXEC, 0);
-        if (vsock_fd < 0) {
-                if (ERRNO_IS_NOT_SUPPORTED(errno)) {
-                        log_debug("Not creating issue file, since AF_VSOCK is not available.");
-                        *ret_cid = 0;
-                        return 0;
-                }
-
-                return log_error_errno(errno, "Unable to test if AF_VSOCK is available: %m");
-        }
-
-        vsock_fd = safe_close(vsock_fd);
+        r = vsock_open_or_warn(/* ret= */ NULL);
+        if (r <= 0)
+                return r;
 
         unsigned local_cid;
         r = vsock_get_local_cid(&local_cid);
diff --git a/src/ssh-generator/ssh-util.c b/src/ssh-generator/ssh-util.c
new file mode 100644 (file)
index 0000000..5723a2b
--- /dev/null
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include "errno-util.h"
+#include "log.h"
+#include "ssh-util.h"
+
+int vsock_open_or_warn(int *ret) {
+        int fd = RET_NERRNO(socket(AF_VSOCK, SOCK_STREAM|SOCK_CLOEXEC, 0));
+        if (ERRNO_IS_NEG_NOT_SUPPORTED(fd))
+                log_debug_errno(fd, "AF_VSOCK is not available, ignoring: %m");
+        else if (fd < 0)
+                return log_error_errno(fd, "Unable to test if AF_VSOCK is available: %m");
+
+        if (ret)
+                *ret = fd;
+        else
+                close(fd);
+
+        return fd >= 0;
+}
diff --git a/src/ssh-generator/ssh-util.h b/src/ssh-generator/ssh-util.h
new file mode 100644 (file)
index 0000000..60984a5
--- /dev/null
@@ -0,0 +1,3 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+int vsock_open_or_warn(int *ret);