xwayland: drop support for abstract sockets

Mutter has done the same [1] a while back. This simplifies the
code and removes support for this legacy feature.

[1]: https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1424
This commit is contained in:
Simon Ser 2024-04-25 11:55:52 +02:00
parent 41fd552f53
commit 4c7c9095d9
4 changed files with 58 additions and 115 deletions

View file

@ -18,9 +18,6 @@
static const char lock_fmt[] = "/tmp/.X%d-lock";
static const char socket_dir[] = "/tmp/.X11-unix";
static const char socket_fmt[] = "/tmp/.X11-unix/X%d";
#ifndef __linux__
static const char socket_fmt2[] = "/tmp/.X11-unix/X%d_";
#endif
bool set_cloexec(int fd, bool cloexec) {
int flags = fcntl(fd, F_GETFD);
@ -40,51 +37,6 @@ bool set_cloexec(int fd, bool cloexec) {
return true;
}
static int open_socket(struct sockaddr_un *addr, size_t path_size) {
int fd, rc;
socklen_t size = offsetof(struct sockaddr_un, sun_path) + path_size + 1;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
wlr_log_errno(WLR_ERROR, "Failed to create socket %c%s",
addr->sun_path[0] ? addr->sun_path[0] : '@',
addr->sun_path + 1);
return -1;
}
if (!set_cloexec(fd, true)) {
close(fd);
return -1;
}
if (addr->sun_path[0]) {
unlink(addr->sun_path);
}
if (bind(fd, (struct sockaddr*)addr, size) < 0) {
rc = errno;
wlr_log_errno(WLR_ERROR, "Failed to bind socket %c%s",
addr->sun_path[0] ? addr->sun_path[0] : '@',
addr->sun_path + 1);
goto cleanup;
}
if (listen(fd, 1) < 0) {
rc = errno;
wlr_log_errno(WLR_ERROR, "Failed to listen to socket %c%s",
addr->sun_path[0] ? addr->sun_path[0] : '@',
addr->sun_path + 1);
goto cleanup;
}
return fd;
cleanup:
close(fd);
if (addr->sun_path[0]) {
unlink(addr->sun_path);
}
errno = rc;
return -1;
}
static bool check_socket_dir(void) {
struct stat buf;
@ -111,10 +63,7 @@ static bool check_socket_dir(void) {
return true;
}
static bool open_sockets(int socks[2], int display) {
struct sockaddr_un addr = { .sun_family = AF_UNIX };
size_t path_size;
static int open_socket(int display) {
if (mkdir(socket_dir, 0755) == 0) {
wlr_log(WLR_INFO, "Created %s ourselves -- other users will "
"be unable to create X11 UNIX sockets of their own",
@ -126,51 +75,62 @@ static bool open_sockets(int socks[2], int display) {
return false;
}
#ifdef __linux__
addr.sun_path[0] = 0;
path_size = snprintf(addr.sun_path + 1, sizeof(addr.sun_path) - 1, socket_fmt, display);
#else
path_size = snprintf(addr.sun_path, sizeof(addr.sun_path), socket_fmt2, display);
#endif
socks[0] = open_socket(&addr, path_size);
if (socks[0] < 0) {
return false;
struct sockaddr_un addr = { .sun_family = AF_UNIX };
size_t path_size = snprintf(addr.sun_path, sizeof(addr.sun_path), socket_fmt, display);
socklen_t size = offsetof(struct sockaddr_un, sun_path) + path_size + 1;
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
wlr_log_errno(WLR_ERROR, "Failed to create socket %s", addr.sun_path);
return -1;
}
if (!set_cloexec(fd, true)) {
close(fd);
return -1;
}
path_size = snprintf(addr.sun_path, sizeof(addr.sun_path), socket_fmt, display);
socks[1] = open_socket(&addr, path_size);
if (socks[1] < 0) {
close(socks[0]);
socks[0] = -1;
return false;
int rc;
unlink(addr.sun_path);
if (bind(fd, (struct sockaddr *)&addr, size) < 0) {
rc = errno;
wlr_log_errno(WLR_ERROR, "Failed to bind socket %s", addr.sun_path);
goto cleanup;
}
if (listen(fd, 1) < 0) {
rc = errno;
wlr_log_errno(WLR_ERROR, "Failed to listen to socket %s", addr.sun_path);
goto cleanup;
}
return true;
return fd;
cleanup:
close(fd);
unlink(addr.sun_path);
errno = rc;
return -1;
}
void unlink_display_sockets(int display) {
char sun_path[64];
char path[64];
snprintf(sun_path, sizeof(sun_path), socket_fmt, display);
unlink(sun_path);
snprintf(path, sizeof(path), socket_fmt, display);
unlink(path);
#ifndef __linux__
snprintf(sun_path, sizeof(sun_path), socket_fmt2, display);
unlink(sun_path);
#endif
snprintf(sun_path, sizeof(sun_path), lock_fmt, display);
unlink(sun_path);
snprintf(path, sizeof(path), lock_fmt, display);
unlink(path);
}
int open_display_sockets(int socks[2]) {
int open_display_sockets(int *sock) {
int lock_fd, display;
char lock_name[64];
for (display = 0; display <= 32; display++) {
snprintf(lock_name, sizeof(lock_name), lock_fmt, display);
if ((lock_fd = open(lock_name, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0444)) >= 0) {
if (!open_sockets(socks, display)) {
*sock = open_socket(display);
if (*sock < 0) {
unlink(lock_name);
close(lock_fd);
continue;