Skip to content

Commit

Permalink
sandbox: use XWayland in wayland session
Browse files Browse the repository at this point in the history
XWayland in an xserver for client running under wayland.

Signed-off-by: Petr Lautrbach <[email protected]>
  • Loading branch information
bachradsusi committed Jan 29, 2024
1 parent 73cdf35 commit 067ae5c
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 6 deletions.
8 changes: 7 additions & 1 deletion sandbox/sandboxX.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ export TITLE="Sandbox $context -- `grep ^#TITLE: ~/.sandboxrc | /usr/bin/cut -b8
[ -z $2 ] && export DPI="96" || export DPI="$2"
trap "exit 0" HUP

(/usr/bin/Xephyr -resizeable -title "$TITLE" -terminate -reset -screen $SCREENSIZE -dpi $DPI -nolisten tcp -displayfd 5 5>&1 2>/dev/null) | while read D; do
if [ -z "$WAYLAND_DISPLAY" ]; then
DISPLAY_COMMAND='/usr/bin/Xephyr -resizeable -title "$TITLE" -terminate -screen $SCREENSIZE -dpi $DPI -nolisten tcp -displayfd 5 5>&1 2>/dev/null'
else
DISPLAY_COMMAND='/usr/bin/Xwayland -terminate -dpi $DPI -retro -geometry $SCREENSIZE -decorate -displayfd 5 5>&1 2>/dev/null'
fi

eval $DISPLAY_COMMAND | while read D; do
export DISPLAY=:$D
cat > ~/seremote << __EOF
#!/bin/sh
Expand Down
112 changes: 107 additions & 5 deletions sandbox/seunshare.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ static int seunshare_mount(const char *src, const char *dst, struct stat *src_st
is_tmp = 1;
}

if (strncmp("/run/user", dst, 9) == 0) {
flags = flags | MS_REC;
}

/* mount directory */
if (mount(src, dst, NULL, MS_BIND | flags, NULL) < 0) {
fprintf(stderr, _("Failed to mount %s on %s: %s\n"), src, dst, strerror(errno));
Expand All @@ -289,6 +293,31 @@ static int seunshare_mount(const char *src, const char *dst, struct stat *src_st

}

/**
* Mount directory and check that we mounted the right directory.
*/
static int seunshare_mount_file(const char *src, const char *dst)
{
int flags = 0;

if (verbose)
printf(_("Mounting %s on %s\n"), src, dst);

if (access(dst, F_OK) == -1) {
FILE *fptr;
fptr = fopen(dst, "w");
fclose(fptr);
}
/* mount file */
if (mount(src, dst, NULL, MS_BIND | flags, NULL) < 0) {
fprintf(stderr, _("Failed to mount %s on %s: %s\n"), src, dst, strerror(errno));
return -1;
}

return 0;

}

/*
If path is empty or ends with "/." or "/.. return -1 else return 0;
*/
Expand Down Expand Up @@ -765,10 +794,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"));
Expand Down Expand Up @@ -805,6 +841,47 @@ int main(int argc, char **argv) {
}
}

if ((XDG_SESSION_TYPE = getenv("XDG_SESSION_TYPE")) != NULL) {
if ((XDG_SESSION_TYPE = strdup(XDG_SESSION_TYPE)) == NULL) {
perror(_("Out of memory"));
goto childerr;
}
}

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 (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;
}

if (asprintf(&pipewire_path_s, "%s/%s", runuserdir_s, "pipewire-0") == -1) {
perror(_("Out of memory"));
goto childerr;
}
if (asprintf(&pipewire_path, "%s/pipewire-0", RUNTIME_DIR) == -1) {
perror(_("Out of memory"));
goto childerr;
}
seunshare_mount_file(pipewire_path, pipewire_path_s);
}

/* mount homedir, runuserdir and tmpdir, in this order */
if (runuserdir_s && seunshare_mount(runuserdir_s, RUNTIME_DIR,
&st_runuserdir_s) != 0) goto childerr;
Expand All @@ -816,10 +893,22 @@ int main(int argc, char **argv) {
if (drop_privs(uid) != 0) goto childerr;

/* construct a new environment */
if ((display = getenv("DISPLAY")) != NULL) {
if ((display = strdup(display)) == NULL) {
perror(_("Out of memory"));
goto childerr;

if (XDG_SESSION_TYPE && strcmp(XDG_SESSION_TYPE, "wayland") == 0) {
if (wayland_display == NULL && (wayland_display = getenv("WAYLAND_DISPLAY")) != NULL) {
if ((wayland_display = strdup(wayland_display)) == NULL) {
perror(_("Out of memory"));
goto childerr;
}
}

}
else {
if ((display = getenv("DISPLAY")) != NULL) {
if ((display = strdup(display)) == NULL) {
perror(_("Out of memory"));
goto childerr;
}
}
}

Expand All @@ -835,8 +924,16 @@ int main(int argc, char **argv) {
perror(_("Failed to clear environment"));
goto childerr;
}
if (display)
if (display) {
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);

if (LANG)
rc |= setenv("LANG", LANG, 1);
if (RUNTIME_DIR)
Expand Down Expand Up @@ -874,7 +971,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);
Expand Down

0 comments on commit 067ae5c

Please sign in to comment.