xwayland: remove remaining SOCK_CLOEXEC

This commit is contained in:
emersion 2019-02-20 11:21:42 +01:00
parent 50011e7170
commit cfe7e28416
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 28 additions and 27 deletions

View File

@ -1,8 +1,4 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#ifdef __FreeBSD__
// for SOCK_CLOEXEC
#define __BSD_VISIBLE 1
#endif
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
@ -25,31 +21,53 @@ static const char *socket_fmt = "/tmp/.X11-unix/X%d";
static const char *socket_fmt2 = "/tmp/.X11-unix/X%d_"; static const char *socket_fmt2 = "/tmp/.X11-unix/X%d_";
#endif #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) { static int open_socket(struct sockaddr_un *addr, size_t path_size) {
int fd, rc; int fd, rc;
socklen_t size = offsetof(struct sockaddr_un, sun_path) + path_size + 1; 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) { 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[0] ? addr->sun_path[0] : '@',
addr->sun_path + 1); addr->sun_path + 1);
return -1; return -1;
} }
if (!set_cloexec(fd, true)) {
close(fd);
return -1;
}
if (addr->sun_path[0]) { if (addr->sun_path[0]) {
unlink(addr->sun_path); unlink(addr->sun_path);
} }
if (bind(fd, (struct sockaddr*)addr, size) < 0) { if (bind(fd, (struct sockaddr*)addr, size) < 0) {
rc = errno; 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[0] ? addr->sun_path[0] : '@',
addr->sun_path + 1); addr->sun_path + 1);
goto cleanup; goto cleanup;
} }
if (listen(fd, 1) < 0) { if (listen(fd, 1) < 0) {
rc = errno; 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[0] ? addr->sun_path[0] : '@',
addr->sun_path + 1); addr->sun_path + 1);
goto cleanup; goto cleanup;
@ -67,7 +85,7 @@ cleanup:
} }
static bool open_sockets(int socks[2], int display) { 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; size_t path_size;
mkdir(socket_dir, 0777); mkdir(socket_dir, 0777);

View File

@ -1,6 +1,7 @@
#ifndef XWAYLAND_SOCKETS_H #ifndef XWAYLAND_SOCKETS_H
#define XWAYLAND_SOCKETS_H #define XWAYLAND_SOCKETS_H
bool set_cloexec(int fd, bool cloexec);
void unlink_display_sockets(int display); void unlink_display_sockets(int display);
int open_display_sockets(int socks[2]); int open_display_sockets(int socks[2]);

View File

@ -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, ...) { static int fill_arg(char ***argv, const char *fmt, ...) {
int len; int len;
char **cur_arg = *argv; char **cur_arg = *argv;