compositor: avoid setting WAYLAND_DISPLAY=wayland-0

This commit alters the way that Weston picks a Wayland display socket
name. Instead of using wl_display_add_socket_auto to look for the first
available name in wayland-0, wayland-1, .... to wayland-32, the code now
checks names wayland-1, wayland-2, .... up to wayland-32.

This change is a workaround for a suboptimal behavior of
libwayland-client. If a client program calls wl_display_connect(NULL) and
the WAYLAND_DISPLAY environment variable is not set, then the program will
by default try to connect to 'wayland-0'. This is a problem when a
computer has a running Wayland compositor but is being accessed in some
other fashion, such as through an X session on a different virtual
terminal, over ssh, etc. Client programs launched through those means may
attempt to connect to an unrelated compositor. Changing libwayland
behavior to remove the default would also work, but a) libraries have
stronger backward compatibility expectations b) that would likely break
more people's setups than just changing Weston would.

Signed-off-by: Manuel Stoeckl <code@mstoeckl.com>
dev
Manuel Stoeckl 4 years ago committed by Pekka Paalanen
parent b10c0e843d
commit ae69381b6e
  1. 23
      compositor/main.c

@ -825,24 +825,29 @@ handle_primary_client_destroyed(struct wl_listener *listener, void *data)
static int static int
weston_create_listening_socket(struct wl_display *display, const char *socket_name) weston_create_listening_socket(struct wl_display *display, const char *socket_name)
{ {
char name_candidate[16];
if (socket_name) { if (socket_name) {
if (wl_display_add_socket(display, socket_name)) { if (wl_display_add_socket(display, socket_name)) {
weston_log("fatal: failed to add socket: %s\n", weston_log("fatal: failed to add socket: %s\n",
strerror(errno)); strerror(errno));
return -1; return -1;
} }
setenv("WAYLAND_DISPLAY", socket_name, 1);
return 0;
} else { } else {
socket_name = wl_display_add_socket_auto(display); for (int i = 1; i <= 32; i++) {
if (!socket_name) { sprintf(name_candidate, "wayland-%d", i);
weston_log("fatal: failed to add socket: %s\n", if (wl_display_add_socket(display, name_candidate) >= 0) {
strerror(errno)); setenv("WAYLAND_DISPLAY", name_candidate, 1);
return -1; return 0;
}
} }
weston_log("fatal: failed to add socket: %s\n",
strerror(errno));
return -1;
} }
setenv("WAYLAND_DISPLAY", socket_name, 1);
return 0;
} }
WL_EXPORT void * WL_EXPORT void *

Loading…
Cancel
Save