sd-bus: Make sure we can connect to user machines as well

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.
This commit is contained in:
Daan De Meyer
2026-02-17 17:52:20 +01:00
parent 8d11835397
commit a12c5bf4f5
5 changed files with 63 additions and 47 deletions
+1 -43
View File
@@ -25,13 +25,11 @@
#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;
-2
View 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);
}
+57 -1
View 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
+2
View 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);
+3 -1
View 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;