abduco

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 7962cb99f3e1032384b11ef6f73e6d24dce7435d
parent 2dd042f9742f7f18b61044a91effa331b56d30e0
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Wed, 25 Feb 2015 23:27:44 +0100

Cleanup socket creation

Diffstat:
server.c | 26+++++++++++++++-----------
1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/server.c b/server.c @@ -38,18 +38,22 @@ static int server_create_socket(const char *name) { if (fd == -1) return -1; socklen_t socklen = offsetof(struct sockaddr_un, sun_path) + strlen(sockaddr.sun_path) + 1; - mode_t mode = umask(S_IXUSR|S_IXGRP|S_IRWXO); - if (bind(fd, (struct sockaddr*)&sockaddr, socklen) == -1) - goto error1; - umask(mode); - if (listen(fd, 5) == -1) - goto error2; + mode_t mask = umask(S_IXUSR|S_IRWXG|S_IRWXO); + int r = bind(fd, (struct sockaddr*)&sockaddr, socklen); + umask(mask); + + if (r == -1) { + close(fd); + return -1; + } + + if (listen(fd, 5) == -1) { + unlink(sockaddr.sun_path); + close(fd); + return -1; + } + return fd; -error2: - unlink(sockaddr.sun_path); -error1: - close(fd); - return -1; } static int server_set_socket_non_blocking(int sock) {