Private bus: don't take over the session's document portal mount
CI / test (push) Canceled after 24m10s
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:
1 parent
50149e6b99
commit
70b24ffe47
5 files changed
+182
-4
No files matched your search
@@ -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]);
|
||||
|
||||
Reference in new issue
Block a user