From cfe7e28416323c1384b95a4eeb377c738444f063 Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 20 Feb 2019 11:21:42 +0100 Subject: xwayland: remove remaining SOCK_CLOEXEC --- xwayland/sockets.c | 36 +++++++++++++++++++++++++++--------- xwayland/sockets.h | 1 + xwayland/xwayland.c | 18 ------------------ 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/xwayland/sockets.c b/xwayland/sockets.c index 112a8bb0..f90e3ca4 100644 --- a/xwayland/sockets.c +++ b/xwayland/sockets.c @@ -1,8 +1,4 @@ #define _POSIX_C_SOURCE 200809L -#ifdef __FreeBSD__ -// for SOCK_CLOEXEC -#define __BSD_VISIBLE 1 -#endif #include #include #include @@ -25,31 +21,53 @@ static const char *socket_fmt = "/tmp/.X11-unix/X%d"; static const char *socket_fmt2 = "/tmp/.X11-unix/X%d_"; #endif +bool set_cloexec(int fd, bool cloexec) { + int flags = fcntl(fd, F_GETFD); + if (flags == -1) { + wlr_log_errno(WLR_ERROR, "fcntl failed"); + return false; + } + if (cloexec) { + flags = flags | FD_CLOEXEC; + } else { + flags = flags & ~FD_CLOEXEC; + } + if (fcntl(fd, F_SETFD, flags) == -1) { + wlr_log_errno(WLR_ERROR, "fcntl failed"); + return false; + } + 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(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0); + fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd < 0) { - wlr_log_errno(WLR_DEBUG, "Failed to create socket %c%s", + 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_DEBUG, "Failed to bind socket %c%s", + 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_DEBUG, "Failed to listen to socket %c%s", + 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; @@ -67,7 +85,7 @@ cleanup: } static bool open_sockets(int socks[2], int display) { - struct sockaddr_un addr = { .sun_family = AF_LOCAL }; + struct sockaddr_un addr = { .sun_family = AF_UNIX }; size_t path_size; mkdir(socket_dir, 0777); diff --git a/xwayland/sockets.h b/xwayland/sockets.h index 73eb36e0..cf42b970 100644 --- a/xwayland/sockets.h +++ b/xwayland/sockets.h @@ -1,6 +1,7 @@ #ifndef XWAYLAND_SOCKETS_H #define XWAYLAND_SOCKETS_H +bool set_cloexec(int fd, bool cloexec); void unlink_display_sockets(int display); int open_display_sockets(int socks[2]); diff --git a/xwayland/xwayland.c b/xwayland/xwayland.c index be2e34d9..c01acb33 100644 --- a/xwayland/xwayland.c +++ b/xwayland/xwayland.c @@ -32,24 +32,6 @@ static void safe_close(int fd) { } } -static bool set_cloexec(int fd, bool cloexec) { - int flags = fcntl(fd, F_GETFD); - if (flags == -1) { - wlr_log_errno(WLR_ERROR, "fcntl failed"); - return false; - } - if (cloexec) { - flags = flags | FD_CLOEXEC; - } else { - flags = flags & ~FD_CLOEXEC; - } - if (fcntl(fd, F_SETFD, flags) == -1) { - wlr_log_errno(WLR_ERROR, "fcntl failed"); - return false; - } - return true; -} - static int fill_arg(char ***argv, const char *fmt, ...) { int len; char **cur_arg = *argv; -- cgit v1.2.3