]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-bus: Make sure we can connect to user machines as well
authorDaan De Meyer <daan@amutable.com>
Mon, 16 Feb 2026 10:27:21 +0000 (11:27 +0100)
committerDaan De Meyer <daan@amutable.com>
Tue, 17 Feb 2026 16:52:20 +0000 (17:52 +0100)
Don't unconditionally look into /run/systemd/machines. If we're a
connected to a session bus, look at the machines for the current user
instead.

src/basic/process-util.c
src/basic/process-util.h
src/libsystemd/sd-bus/bus-container.c
src/libsystemd/sd-bus/bus-container.h
src/libsystemd/sd-id128/id128-util.c

index 9d77e96b1a7eb59cbb20d8b1a3088d0b9bb91813..3f2c2d3b23815847011df32374ba358d5d0867d3 100644 (file)
 #include "cgroup-util.h"
 #include "dirent-util.h"
 #include "dlfcn-util.h"
-#include "env-file.h"
 #include "errno-util.h"
 #include "escape.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "fs-util.h"
-#include "hostname-util.h"
 #include "io-util.h"
 #include "iovec-util.h"
 #include "locale-util.h"
@@ -53,6 +51,7 @@
 #include "stdio-util.h"
 #include "string-table.h"
 #include "string-util.h"
+#include "strv.h"
 #include "time-util.h"
 #include "user-util.h"
 
@@ -349,47 +348,6 @@ int pidref_get_cmdline_strv(const PidRef *pid, ProcessCmdlineFlags flags, char *
         return 0;
 }
 
-int container_get_leader(const char *machine, pid_t *pid) {
-        _cleanup_free_ char *s = NULL, *class = NULL;
-        const char *p;
-        pid_t leader;
-        int r;
-
-        assert(machine);
-        assert(pid);
-
-        if (streq(machine, ".host")) {
-                *pid = 1;
-                return 0;
-        }
-
-        if (!hostname_is_valid(machine, 0))
-                return -EINVAL;
-
-        p = strjoina("/run/systemd/machines/", machine);
-        r = parse_env_file(NULL, p,
-                           "LEADER", &s,
-                           "CLASS", &class);
-        if (r == -ENOENT)
-                return -EHOSTDOWN;
-        if (r < 0)
-                return r;
-        if (!s)
-                return -EIO;
-
-        if (!streq_ptr(class, "container"))
-                return -EIO;
-
-        r = parse_pid(s, &leader);
-        if (r < 0)
-                return r;
-        if (leader <= 1)
-                return -EIO;
-
-        *pid = leader;
-        return 0;
-}
-
 int pid_is_kernel_thread(pid_t pid) {
         int r;
 
index b3f612005307d75f2242b7cab3f4d2221c12eef2..9d16055d5e28f0ad5f07c05ae894798bd2774627 100644 (file)
@@ -54,8 +54,6 @@ int pid_get_start_time(pid_t pid, usec_t *ret);
 int pidref_get_start_time(const PidRef *pid, usec_t *ret);
 int get_process_umask(pid_t pid, mode_t *ret);
 
-int container_get_leader(const char *machine, pid_t *pid);
-
 static inline bool SIGINFO_CODE_IS_DEAD(int code) {
         return IN_SET(code, CLD_EXITED, CLD_KILLED, CLD_DUMPED);
 }
index 3bbcbb81c6fcd525cd5e67c36a5e0987ae738e15..66cb0ab0c7a1c1d26644ec1135d0a65fe21fbc6d 100644 (file)
@@ -5,14 +5,68 @@
 #include "bus-container.h"
 #include "bus-internal.h"
 #include "bus-socket.h"
+#include "env-file.h"
 #include "fd-util.h"
 #include "format-util.h"
+#include "hostname-util.h"
 #include "log.h"
 #include "namespace-util.h"
+#include "parse-util.h"
+#include "path-lookup.h"
+#include "path-util.h"
 #include "pidref.h"
 #include "process-util.h"
 #include "string-util.h"
 
+int container_get_leader(RuntimeScope scope, const char *machine, pid_t *ret) {
+        _cleanup_free_ char *p = NULL, *s = NULL, *class = NULL;
+        pid_t leader;
+        int r;
+
+        assert(machine);
+        assert(ret);
+
+        if (streq(machine, ".host")) {
+                if (scope == RUNTIME_SCOPE_USER)
+                        return -EHOSTDOWN;
+
+                *ret = 1;
+                return 0;
+        }
+
+        if (!hostname_is_valid(machine, 0))
+                return -EINVAL;
+
+        r = runtime_directory_generic(scope, "systemd/machines", &p);
+        if (r < 0)
+                return r;
+
+        if (!path_extend(&p, machine))
+                return -ENOMEM;
+
+        r = parse_env_file(NULL, p,
+                           "LEADER", &s,
+                           "CLASS", &class);
+        if (r == -ENOENT)
+                return -EHOSTDOWN;
+        if (r < 0)
+                return r;
+        if (!s)
+                return -ESRCH;
+
+        if (!streq_ptr(class, "container"))
+                return -EMEDIUMTYPE;
+
+        r = parse_pid(s, &leader);
+        if (r < 0)
+                return r;
+        if (leader <= 1)
+                return -EBADMSG;
+
+        *ret = leader;
+        return 0;
+}
+
 int bus_container_connect_socket(sd_bus *b) {
         _cleanup_close_ int pidnsfd = -EBADF, mntnsfd = -EBADF, usernsfd = -EBADF, rootfd = -EBADF;
         _cleanup_(pidref_done) PidRef child = PIDREF_NULL;
@@ -29,7 +83,9 @@ int bus_container_connect_socket(sd_bus *b) {
                 log_debug("sd-bus: connecting bus%s%s to machine %s...",
                           b->description ? " " : "", strempty(b->description), b->machine);
 
-                r = container_get_leader(b->machine, &b->nspid);
+                r = container_get_leader(RUNTIME_SCOPE_USER, b->machine, &b->nspid);
+                if (IN_SET(r, -EHOSTDOWN, -ENXIO))
+                        r = container_get_leader(RUNTIME_SCOPE_SYSTEM, b->machine, &b->nspid);
                 if (r < 0)
                         return r;
         } else
index 2e933bd9a703b426644b55b33d6d71bdbcf7b56e..f6ede2ecd65a7c4d7124f05bed528159e62f38ab 100644 (file)
@@ -3,4 +3,6 @@
 
 #include "sd-forward.h"
 
+int container_get_leader(RuntimeScope scope, const char *machine, pid_t *ret);
+
 int bus_container_connect_socket(sd_bus *b);
index 0cae92b7289e3e61bbcd07c5b239138acda96298..9d406a45d1316741c512037d1c45106745ba56f9 100644 (file)
@@ -5,6 +5,7 @@
 #include <sys/socket.h>
 #include <unistd.h>
 
+#include "bus-container.h"
 #include "fd-util.h"
 #include "fs-util.h"
 #include "hash-funcs.h"
@@ -13,6 +14,7 @@
 #include "namespace-util.h"
 #include "pidref.h"
 #include "process-util.h"
+#include "runtime-scope.h"
 #include "sha256.h"
 #include "siphash24.h"
 #include "string-util.h"
@@ -287,7 +289,7 @@ int id128_get_boot_for_machine(const char *machine, sd_id128_t *ret) {
         if (isempty(machine))
                 return sd_id128_get_boot(ret);
 
-        r = container_get_leader(machine, &pid);
+        r = container_get_leader(RUNTIME_SCOPE_SYSTEM, machine, &pid);
         if (r < 0)
                 return r;