From a685ed3f238626e9574308b12927e336bd01c8db Mon Sep 17 00:00:00 2001 From: Petr Lautrbach Date: Thu, 25 Jan 2024 11:18:32 +0100 Subject: [PATCH] Use RUNTIME_DIR and work with X11 --- sandbox/seunshare.c | 68 ++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/sandbox/seunshare.c b/sandbox/seunshare.c index c6791257..1917d218 100644 --- a/sandbox/seunshare.c +++ b/sandbox/seunshare.c @@ -652,7 +652,6 @@ int main(int argc, char **argv) { char *tmpdir_r = NULL; /* tmpdir created by seunshare */ char *runuserdir_s = NULL; /* /var/run/user/UID spec'd by user in argv[] */ char *runuserdir_r = NULL; /* /var/run/user/UID created by seunshare */ - char *wayland_path = NULL; /* /run/user/UID/wayland-0 */ struct stat st_curhomedir; struct stat st_homedir; @@ -793,11 +792,17 @@ int main(int argc, char **argv) { if (child == 0) { char *display = NULL; + char *wayland_display = NULL; char *LANG = NULL; char *RUNTIME_DIR = NULL; char *XDG_SESSION_TYPE = NULL; int rc = -1; char *resolved_path = NULL; + char *wayland_path_s = NULL; /* /tmp/.../wayland-0 */ + char *wayland_path = NULL; /* /run/user/UID/wayland-0 */ + char *pipewire_path_s = NULL; /* /tmp/.../wayland-0 */ + char *pipewire_path = NULL; /* /run/user/UID/wayland-0 */ + if (unshare(CLONE_NEWNS) < 0) { perror(_("Failed to unshare")); @@ -841,18 +846,32 @@ int main(int argc, char **argv) { } } - printf("XDG_SESSION_TYPE=%s, runuserdir_s=%s\n", XDG_SESSION_TYPE, runuserdir_s); + if (runuserdir_s) { + if (XDG_SESSION_TYPE && strcmp(XDG_SESSION_TYPE, "wayland") == 0) { + if ((wayland_display = getenv("WAYLAND_DISPLAY")) != NULL) { + if ((wayland_display = strdup(wayland_display)) == NULL) { + perror(_("Out of memory")); + goto childerr; + } + } + if (asprintf(&wayland_path_s, "%s/%s", runuserdir_s, wayland_display) == -1) { + perror(_("Out of memory")); + goto childerr; + } - if (runuserdir_s && XDG_SESSION_TYPE && strcmp(XDG_SESSION_TYPE, "wayland") == 0) { - asprintf(&wayland_path, "%s/%s", runuserdir_s, "wayland-0"); - printf("wayland_path=%s\n", wayland_path); - system("ls /run/user/1000"); - seunshare_mount_file("/run/user/1000/wayland-0", wayland_path); - asprintf(&wayland_path, "%s/%s", runuserdir_s, "pipewire-0"); - printf("wayland_path=%s\n", wayland_path); - system("ls /run/user/1000"); - seunshare_mount_file("/run/user/1000/pipewire-0", wayland_path); + if (asprintf(&wayland_path, "%s/%s", RUNTIME_DIR, wayland_display) == -1) { + perror(_("Out of memory")); + goto childerr; + } + + if (seunshare_mount_file(wayland_path, wayland_path_s) == -1) + goto childerr; + } + + asprintf(&pipewire_path_s, "%s/%s", runuserdir_s, "pipewire-0"); + asprintf(&pipewire_path, "%s/pipewire-0", RUNTIME_DIR); + seunshare_mount_file(pipewire_path, pipewire_path_s); } /* mount homedir, runuserdir and tmpdir, in this order */ @@ -869,8 +888,8 @@ int main(int argc, char **argv) { /* construct a new environment */ if (XDG_SESSION_TYPE && strcmp(XDG_SESSION_TYPE, "wayland") == 0) { - if ((display = getenv("WAYLAND_DISPLAY")) != NULL) { - if ((display = strdup(display)) == NULL) { + if (wayland_display == NULL && (wayland_display = getenv("WAYLAND_DISPLAY")) != NULL) { + if ((wayland_display = strdup(wayland_display)) == NULL) { perror(_("Out of memory")); goto childerr; } @@ -886,15 +905,6 @@ int main(int argc, char **argv) { } } -#if 0 - if ((display = getenv("WAYLAND_DISPLAY")) != NULL) { - if ((display = strdup(display)) == NULL) { - perror(_("Out of memory")); - goto childerr; - } - } -#endif - /* construct a new environment */ if ((LANG = getenv("LANG")) != NULL) { if ((LANG = strdup(LANG)) == NULL) { @@ -908,11 +918,12 @@ int main(int argc, char **argv) { goto childerr; } if (display) { - if (XDG_SESSION_TYPE && strcmp(XDG_SESSION_TYPE, "wayland") == 0) - rc |= setenv("WAYLAND_DISPLAY", display, 1); - else - rc |= setenv("DISPLAY", display, 1); + rc |= setenv("DISPLAY", display, 1); } + if (wayland_display) { + rc |= setenv("WAYLAND_DISPLAY", wayland_display, 1); + } + if (XDG_SESSION_TYPE) rc |= setenv("XDG_SESSION_TYPE", XDG_SESSION_TYPE, 1); @@ -953,7 +964,12 @@ int main(int argc, char **argv) { fprintf(stderr, _("Failed to execute command %s: %s\n"), argv[optind], strerror(errno)); childerr: free(resolved_path); + free(wayland_path); + free(wayland_path_s); + free(pipewire_path); + free(pipewire_path_s); free(display); + free(wayland_display); free(LANG); free(RUNTIME_DIR); exit(-1);