client: Allow absolute paths in WAYLAND_DISPLAY

In order to support system compositor instances, it is necessary to
allow clients' wl_display_connect() to find the compositor's listening
socket somewhere outside of XDG_RUNTIME_DIR. For a full account, see
the discussion beginning here:

https://lists.freedesktop.org/archives/wayland-devel/2017-November/035664.html

This change adjusts the client-side connection logic so that, if
WAYLAND_DISPLAY is formatted as an absolute pathname, the socket
connection attempt is made to just $WAYLAND_DISPLAY rather than
usual user-private location $XDG_RUNTIME_DIR/$WAYLAND_DISPLAY.

This change is based on Davide Bettio's submission of the same concept
at:

https://lists.freedesktop.org/archives/wayland-devel/2015-August/023838.html.

v4 changes:

* Improved internal comments and some boundary-condition
  error checks in test case.
* Refer to compositor as "Wayland server" rather than "Wayland
  display" in wl_display_connect() doxygen comments.
* Remove redundant descriptions of parameter-interpretation
  mechanics from wl_display_connect() manpage. Reworked things
  to make it clear that 'name' and $WAYLAND_DISLAY are each
  capable of encoding absolute server socket paths.
* Remove callout to reference implementation behavior in protocol
  documented. In its place there is now a simple statement that
  implementations can optionally support absolute socket paths.

v3 changes:

* Added test case.
* Clarified documentation to note that 'name' parameter to wl_display_connect()
  can also be an absolute path.

v2 changes:

* Added backward incompatibility note to wl_display_connect() manpage.
* Rephased wl_display_connect() manpage changes to precisely match actual
  changed behavior.
* Added mention of new absolute path behavior in wl_display_connect()
  doxygen comments.
* Mentioned new absolute path interpretation of WAYLAND_DISPLAY in
  protocol documentation.

Signed-off-by: Matt Hoosier <matt.hoosier@gmail.com>
Acked-by: Jonas Ådahl <jadahl@gmail.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
This commit is contained in:
Matt Hoosier 2017-11-27 08:54:54 -06:00 committed by Pekka Paalanen
parent de24f4dd76
commit 1b6521e695
4 changed files with 177 additions and 16 deletions

View file

@ -26,10 +26,14 @@
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <unistd.h>
#include "wayland-client.h"
#include "wayland-os.h"
#include "wayland-server.h"
#include "test-runner.h"
@ -173,3 +177,108 @@ TEST(add_socket_auto)
wl_display_destroy(d);
}
struct client_create_listener {
struct wl_listener listener;
struct wl_display *display;
};
struct client_destroy_listener {
struct wl_listener listener;
struct wl_display *display;
};
static void
client_destroy_notify(struct wl_listener *l, void *data)
{
struct client_destroy_listener *listener =
wl_container_of(l, listener, listener);
wl_display_terminate(listener->display);
free(listener);
}
static void
client_create_notify(struct wl_listener *l, void *data)
{
struct wl_client *client = data;
struct client_create_listener *listener =
wl_container_of(l, listener, listener);
struct client_destroy_listener *destroy_listener = (struct client_destroy_listener *)malloc(sizeof *destroy_listener);
assert(destroy_listener != NULL);
destroy_listener->display = listener->display;
destroy_listener->listener.notify = client_destroy_notify;
wl_client_add_destroy_listener(client, &destroy_listener->listener);
}
TEST(absolute_socket_path)
{
struct wl_display *display;
struct client_create_listener client_create_listener;
struct sockaddr_un addr;
int fd;
socklen_t size;
const char *xdg_runtime_dir;
size_t len;
int ret;
pid_t pid;
/* It's a little weird that this test about absolute socket paths
* uses XDG_RUNTIME_DIR, but that's the only location guaranteed
* by test-runner to be both writable and unique. This isn't
* really a problem; we'll just take care that the leaf-level
* filename used for the socket isn't anything that would
* accidentally be generated by a default usage of wl_display_connect(). */
xdg_runtime_dir = require_xdg_runtime_dir();
memset(&addr, 0, sizeof addr);
len = snprintf(addr.sun_path, sizeof addr.sun_path,
"%s/%s", xdg_runtime_dir, "wayland-absolute-0");
assert(len < sizeof addr.sun_path
&& "Bug in test. Path too long");
/* The path must not exist prior to binding. */
assert(access(addr.sun_path, F_OK) == -1);
size = offsetof (struct sockaddr_un, sun_path) + strlen(addr.sun_path);
addr.sun_family = AF_LOCAL;
fd = wl_os_socket_cloexec(PF_LOCAL, SOCK_STREAM, 0);
assert(fd >= 0 );
ret = bind(fd, (struct sockaddr *) &addr, size);
assert(ret >= 0);
ret = listen(fd, 128);
assert(ret >= 0);
/* Start server display. Be careful (by avoiding wl_display_add_socket_auto()
* to offer only the absolutely qualified socket made above. */
display = wl_display_create();
assert(display != NULL);
client_create_listener.listener.notify = client_create_notify;
client_create_listener.display = display;
wl_display_add_client_created_listener(display, &client_create_listener.listener);
ret = wl_display_add_socket_fd(display, fd);
assert(ret == 0);
/* Execute client that connects to the absolutely qualified server socket path. */
pid = fork();
assert(pid != -1);
if (pid == 0) {
ret = setenv("WAYLAND_DISPLAY", addr.sun_path, 1);
assert(ret == 0);
struct wl_display *client_display = wl_display_connect(NULL);
assert(client_display != NULL);
ret = wl_display_roundtrip(client_display);
assert(ret != -1);
wl_display_disconnect(client_display);
exit(0);
assert(false);
}
wl_display_run(display);
ret = waitpid(pid, NULL, 0);
assert(ret == pid);
wl_display_destroy(display);
ret = unlink(addr.sun_path);
assert(ret == 0);
}