Don't use SOCK_CLOEXEC

Manually set the CLOEXEC flag instead, since SOCK_CLOEXEC isn't POSIX.
This commit is contained in:
emersion 2019-02-19 16:18:31 +01:00 committed by Drew DeVault
parent 6e55111cd3
commit 468d09b664

View file

@ -175,12 +175,33 @@ static void handle_swaybg_client_destroy(struct wl_listener *listener,
output->swaybg_client = NULL; output->swaybg_client = NULL;
} }
static bool set_cloexec(int fd, bool cloexec) {
int flags = fcntl(fd, F_GETFD);
if (flags == -1) {
sway_log_errno(SWAY_ERROR, "fcntl failed");
return false;
}
if (cloexec) {
flags = flags | FD_CLOEXEC;
} else {
flags = flags & ~FD_CLOEXEC;
}
if (fcntl(fd, F_SETFD, flags) == -1) {
sway_log_errno(SWAY_ERROR, "fcntl failed");
return false;
}
return true;
}
static bool spawn_swaybg(struct sway_output *output, char *const cmd[]) { static bool spawn_swaybg(struct sway_output *output, char *const cmd[]) {
int sockets[2]; int sockets[2];
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sockets) != 0) { if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) != 0) {
sway_log_errno(SWAY_ERROR, "socketpair failed"); sway_log_errno(SWAY_ERROR, "socketpair failed");
return false; return false;
} }
if (!set_cloexec(sockets[0], true) || !set_cloexec(sockets[1], true)) {
return false;
}
output->swaybg_client = wl_client_create(server.wl_display, sockets[0]); output->swaybg_client = wl_client_create(server.wl_display, sockets[0]);
if (output->swaybg_client == NULL) { if (output->swaybg_client == NULL) {
@ -202,14 +223,7 @@ static bool spawn_swaybg(struct sway_output *output, char *const cmd[]) {
sway_log_errno(SWAY_ERROR, "fork failed"); sway_log_errno(SWAY_ERROR, "fork failed");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else if (pid == 0) { } else if (pid == 0) {
// Remove the CLOEXEC flag if (!set_cloexec(sockets[1], false)) {
int flags = fcntl(sockets[1], F_GETFD);
if (flags == -1) {
sway_log_errno(SWAY_ERROR, "fcntl() failed");
exit(EXIT_FAILURE);
}
if (fcntl(sockets[1], F_SETFD, flags & ~FD_CLOEXEC) == -1) {
sway_log_errno(SWAY_ERROR, "fcntl() failed");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -225,6 +239,10 @@ static bool spawn_swaybg(struct sway_output *output, char *const cmd[]) {
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
if (close(sockets[1]) != 0) {
sway_log_errno(SWAY_ERROR, "close failed");
return false;
}
if (waitpid(pid, NULL, 0) < 0) { if (waitpid(pid, NULL, 0) < 0) {
sway_log_errno(SWAY_ERROR, "waitpid failed"); sway_log_errno(SWAY_ERROR, "waitpid failed");
return false; return false;