server: set CLOEXEC | NONBLOCK directly in socket() call

This commit is contained in:
Daniel Eklöf 2019-11-03 01:48:40 +01:00
parent e1b6aa87fb
commit a326427caf
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

View file

@ -225,22 +225,12 @@ try_connect(const char *sock_path)
{
enum connect_status ret = CONNECT_ERR;
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (fd == -1) {
LOG_ERRNO("failed to create UNIX socket");
goto err;
}
/* Use non-blocking, so that we don't get stuck in connect()
* waiting for a timeout */
int flags;
if ((flags = fcntl(fd, F_GETFL)) < 0 ||
fcntl(fd, F_SETFL, flags | O_NONBLOCK))
{
LOG_ERRNO("failed to set O_NONBLOCK");
goto err;
}
struct sockaddr_un addr = {.sun_family = AF_UNIX};
strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path) - 1);
@ -264,7 +254,7 @@ err:
struct server *
server_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl)
{
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd == -1) {
LOG_ERRNO("failed to create UNIX socket");
return NULL;
@ -290,11 +280,6 @@ server_init(const struct config *conf, struct fdm *fdm, struct wayland *wayl)
unlink(sock_path);
if (fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) < 0) {
LOG_ERRNO("failed to set FD_CLOEXEC on socket");
goto err;
}
struct sockaddr_un addr = {.sun_family = AF_UNIX};
strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path) - 1);