Private bus: don't take over the session's document portal mount
CI / test (push) Canceled after 24m10s

A nested compositor's private D-Bus kept the session's XDG_RUNTIME_DIR, so
an xdg-document-portal activated on it mounted over $XDG_RUNTIME_DIR/doc and
removed the mount when the bus exited. The session's portal kept running
without it, and flatpak apps stopped starting.

The bus now has its own config that starts the document portal with a runtime
directory per compositor. scripts/test-private-bus.sh covers it. The KDE
colors test gets its own runtime directory for the same reason.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-25 22:06:51 -08:00
1 parent 50149e6b99
commit 70b24ffe47
5 files changed
+182 -4

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
///|
/// Run by scripts/test-private-bus.sh, which supplies the runtime directory with a "host"
/// document portal mounted in it, and checks afterwards that the mount outlived this process.
test "the private bus's document portal leaves the host's mount alone" {
if @sys.getenv("CHANDRA_PRIVATE_BUS_E2E") != "1" {
return
}
let out = @sys.getenv("XDG_RUNTIME_DIR") + "/activation.txt"
let cmd = "{ echo bus=$DBUS_SESSION_BUS_ADDRESS; " +
"dbus-send --session --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus " +
"org.freedesktop.DBus.StartServiceByName string:org.freedesktop.portal.Documents uint32:0; " +
"sleep 1; grep -o '[^ ]*/private-bus-[^ ]*/doc ' /proc/self/mountinfo; " +
"dbus-send --session --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus " +
"org.freedesktop.DBus.StartServiceByName string:org.freedesktop.portal.Desktop uint32:0; " +
"sleep 3; dbus-send --session --print-reply --dest=org.freedesktop.portal.Desktop " +
"/org/freedesktop/portal/desktop org.freedesktop.DBus.Properties.Get " +
"string:org.freedesktop.portal.Trash string:version; echo done; } >\'" +
out +
"' 2>&1"
assert_true(@sys.spawn(cmd, "wayland-test"))
let mut text = ""
for _i = 0; _i < 300 && !text.contains("done\n"); _i = _i + 1 {
@sys.sleep_ms(100)
// The shim's bytes are ASCII here: dbus-send's reply.
let sb = StringBuilder::new()
for b in @sys.read_file_bytes(out) {
sb.write_char(b.to_int().unsafe_to_char())
}
text = sb.to_string()
}
assert_true(text.contains("done\n"))
// A bus of its own, not the one we were started on...
assert_true(text.contains("bus=unix:"))
assert_false(
text.contains("bus=" + @sys.getenv("DBUS_SESSION_BUS_ADDRESS") + "\n"),
)
// ...where the document portal mounts in a directory of this compositor's own...
assert_true(
text.contains(
"/chandra/private-bus-" + @sys.getpid().to_string() + "/runtime/doc \n",
),
)
// ...and the desktop portal, which exports nothing without it, still works (Trash is one of
// the interfaces it needs no backend for).
assert_true(text.contains("variant uint32"))
}
+86 -2
View File
@@ -427,7 +427,15 @@ int32_t mp_has_exe(moonbit_bytes_t name_bytes) {
* Only when nested inside another graphical session (WAYLAND_DISPLAY or DISPLAY set).
* As a login session (--drm) the session bus is ours, and it is where the PAM-unlocked
* gnome-keyring (org.freedesktop.secrets), portals and notifications live: a private bus
* would hide them, so apps would D-Bus-activate a fresh, locked keyring on it. */
* would hide them, so apps would D-Bus-activate a fresh, locked keyring on it.
*
* An xdg-document-portal activated on the private bus must not see our XDG_RUNTIME_DIR: every
* instance mounts $XDG_RUNTIME_DIR/doc, whatever bus it is on, so a second one takes over the
* host session's mount and removes it when this bus exits. The host's portal keeps running
* without it, and `flatpak run` fails ("Can't find source path .../doc/by-app/APP") until it is
* restarted. So the bus gets its own config, whose first service directory starts that service
* with a runtime directory of this compositor's own. (Not starting it at all is no way out:
* xdg-desktop-portal then exports nothing.) Without that config there is no private bus. */
static char private_bus[256];
static int private_bus_tried = 0;
@@ -436,10 +444,84 @@ static int env_set(const char *name) {
return v && v[0];
}
/* Replace `path` with `text`, atomically. */
static int write_text_file(const char *path, const char *text) {
char tmp[PATH_MAX];
if (snprintf(tmp, sizeof(tmp), "%s.%d", path, (int)getpid()) >= (int)sizeof(tmp)) return -1;
FILE *f = fopen(tmp, "w");
if (!f) return -1;
int ok = fputs(text, f) >= 0;
ok = fclose(f) == 0 && ok;
if (ok && rename(tmp, path) == 0) return 0;
unlink(tmp);
return -1;
}
/* mkdir that accepts a directory already there. */
static int make_dir(const char *path) {
return mkdir(path, 0700) == 0 || errno == EEXIST ? 0 : -1;
}
/* Write the private bus's config and its document portal service file, and put the config's
* path in `conf`. Both stay in place: dbus-daemon rereads its service directories as it runs. */
static int write_private_bus_config(char *conf, size_t cap) {
/* One directory per compositor: two nested ones would take over each other's mounts too. */
char dir[PATH_MAX - 128];
const char *rt = getenv("XDG_RUNTIME_DIR");
if (rt && rt[0] == '/') {
if (snprintf(dir, sizeof(dir), "%s/chandra", rt) >= (int)sizeof(dir)) return -1;
if (make_dir(dir) != 0) return -1;
if (snprintf(dir, sizeof(dir), "%s/chandra/private-bus-%d", rt, (int)getpid()) >=
(int)sizeof(dir))
return -1;
if (make_dir(dir) != 0) return -1;
} else {
strcpy(dir, "/tmp/chandra-bus-XXXXXX");
if (!mkdtemp(dir)) return -1;
}
/* The path goes into XML and into an Exec= command line as it is. */
if (strpbrk(dir, "<>& \t\n\"'\\")) return -1;
char runtime[PATH_MAX];
snprintf(runtime, sizeof(runtime), "%s/runtime", dir);
if (make_dir(runtime) != 0) return -1;
const char *portal = NULL;
if (access("/usr/lib/xdg-document-portal", X_OK) == 0)
portal = "/usr/lib/xdg-document-portal";
else if (access("/usr/libexec/xdg-document-portal", X_OK) == 0)
portal = "/usr/libexec/xdg-document-portal";
/* Not where we know to look: don't start it at all, rather than with the shared directory. */
char exec[2 * PATH_MAX];
if (portal)
snprintf(exec, sizeof(exec), "/usr/bin/env XDG_RUNTIME_DIR=%s %s", runtime, portal);
else
strcpy(exec, "/bin/false");
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s/org.freedesktop.portal.Documents.service", dir);
char service[2 * PATH_MAX + 128];
snprintf(service, sizeof(service),
"[D-BUS Service]\n"
"Name=org.freedesktop.portal.Documents\n"
"Exec=%s\n",
exec);
if (write_text_file(path, service) != 0) return -1;
/* Of two service files for one name, the one in the earlier directory wins. */
char xml[PATH_MAX + 256];
snprintf(xml, sizeof(xml),
"<busconfig>\n"
" <servicedir>%s</servicedir>\n"
" <include>/usr/share/dbus-1/session.conf</include>\n"
"</busconfig>\n",
dir);
if (snprintf(conf, cap, "%s/bus.conf", dir) >= (int)cap) return -1;
return write_text_file(conf, xml);
}
static void start_private_bus(void) {
if (private_bus_tried) return;
private_bus_tried = 1;
if (!env_set("WAYLAND_DISPLAY") && !env_set("DISPLAY")) return;
char conf[PATH_MAX];
if (write_private_bus_config(conf, sizeof(conf)) != 0) return;
int fds[2];
if (pipe(fds) != 0) return;
pid_t pid = fork();
@@ -449,7 +531,9 @@ static void start_private_bus(void) {
close(fds[0]);
char arg[64];
snprintf(arg, sizeof(arg), "--print-address=%d", fds[1]);
execlp("dbus-daemon", "dbus-daemon", "--session", "--nofork", arg, (char *)NULL);
char conf_arg[PATH_MAX + 16];
snprintf(conf_arg, sizeof(conf_arg), "--config-file=%s", conf);
execlp("dbus-daemon", "dbus-daemon", conf_arg, "--nofork", arg, (char *)NULL);
_exit(127);
}
close(fds[1]);
+5 -2
View File
@@ -53,9 +53,12 @@ echo "kdeglobals: ok"
for c in kwrite Xvfb import convert dbus-run-session; do
command -v $c >/dev/null 2>&1 || { echo "skipping the KWrite check ($c not installed)"; exit 0; }
done
export XDG_CONFIG_HOME="$tmp/kw" HOME="$tmp/kw/home" DISPLAY=:77 QT_QPA_PLATFORM=xcb \
QT_QPA_PLATFORMTHEME=kde KC tmp
# Its own runtime dir: a document portal started on this private bus would otherwise take over the
# session's $XDG_RUNTIME_DIR/doc mount, and remove it on exit.
export XDG_CONFIG_HOME="$tmp/kw" HOME="$tmp/kw/home" XDG_RUNTIME_DIR="$tmp/run" DISPLAY=:77 \
QT_QPA_PLATFORM=xcb QT_QPA_PLATFORMTHEME=kde KC tmp
mkdir -p "$HOME"
mkdir -m 700 "$XDG_RUNTIME_DIR"
Xvfb :77 -screen 0 800x600x24 >/dev/null 2>&1 & xvfb=$!
trap 'kill $xvfb 2>/dev/null; rm -rf "$tmp"' EXIT
sleep 1
+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
# Run the private session bus's end-to-end test (compositor/sys): a nested compositor's apps must
# not be able to take over the session's document portal mount. A "host" xdg-document-portal
# mounts doc/ in a throwaway runtime directory, as a login session's does; the test spawns an app
# that asks the compositor's private bus for another one, which must mount elsewhere (and
# xdg-desktop-portal must still work there); the host's mount must still be there after the
# compositor (the test process) and its bus are gone. Needs FUSE, dbus-daemon, xdg-document-portal
# and xdg-desktop-portal; skips without them. Extra arguments go to `moon test`.
set -euo pipefail
cd "$(dirname "$0")/.."
export PATH="$HOME/.moon/bin:$PATH"
XDOC=
for x in /usr/libexec/xdg-document-portal /usr/lib/xdg-document-portal; do
[ -x "$x" ] && XDOC=$x && break
done
skip() { echo "private bus test skipped: $1"; exit 0; }
[ -n "$XDOC" ] || skip "xdg-document-portal not installed"
[ -x "${XDOC%/*}/xdg-desktop-portal" ] || skip "xdg-desktop-portal not installed"
[ -w /dev/fuse ] && command -v fusermount3 >/dev/null || skip "no FUSE"
command -v dbus-daemon >/dev/null && command -v dbus-send >/dev/null || skip "no dbus-daemon"
tmp=$(mktemp -d)
mkdir -m 700 "$tmp/run"
export XDG_RUNTIME_DIR="$tmp/run" XDG_DATA_HOME="$tmp/data"
addr_pid=$(dbus-daemon --session --fork --print-address=1 --print-pid=1)
export DBUS_SESSION_BUS_ADDRESS=$(echo "$addr_pid" | sed -n 1p)
DPID=$(echo "$addr_pid" | sed -n 2p)
cleanup() {
kill ${XPID:-} "$DPID" 2>/dev/null || true
sleep 1
fusermount3 -u -z "$tmp/run/doc" 2>/dev/null || true
rm -rf "$tmp"
}
trap cleanup EXIT
mounted() { grep -q -F " $tmp/run/doc " /proc/self/mountinfo; }
"$XDOC" -v >"$tmp/xdoc.log" 2>&1 &
XPID=$!
for _ in $(seq 50); do mounted && break; sleep 0.1; done
mounted || { cat "$tmp/xdoc.log"; skip "the document portal could not mount here"; }
# WAYLAND_DISPLAY makes the compositor "nested", which is when it starts a private bus.
CHANDRA_PRIVATE_BUS_E2E=1 WAYLAND_DISPLAY=wayland-host \
moon test --target native --warn-list -a compositor/sys "$@"
sleep 2 # for the private bus, and anything it started, to exit
mounted || { echo "FAIL: the host's $tmp/run/doc mount is gone"; cat "$tmp/xdoc.log"; exit 1; }
echo "private bus: ok"
+1
View File
@@ -53,4 +53,5 @@ fi
XDG_CONFIG_HOME="$(mktemp -d)" CHANDRA_TEST_CONFIG=1 \
moon test --target native --warn-list -a compositor/renderer compositor/gfx compositor/wasm \
compositor/network compositor/portal compositor/polkit compositor/x11 compositor/host
scripts/test-private-bus.sh
moon build --target native --warn-list -a compositor/bin