Fix file descriptor leakage #30724

This commit is contained in:
Marc André Tanner 2015-02-16 22:42:01 +01:00
parent 678c509f3d
commit 5c7f933dcc
1 changed files with 4 additions and 2 deletions

View File

@ -39,16 +39,18 @@ static int server_create_socket(const char *name) {
return -1;
socklen_t socklen = offsetof(struct sockaddr_un, sun_path) + strlen(sockaddr.sun_path) + 1;
mode_t mode = S_IRUSR|S_IWUSR;
fchmod(fd, mode);
if (fchmod(fd, mode) == -1)
goto error;
if (bind(fd, (struct sockaddr*)&sockaddr, socklen) == -1)
return -1;
if (fchmod(fd, mode) == -1 && chmod(sockaddr.sun_path, mode) == -1)
if (fchmod(fd, mode) == -1 || chmod(sockaddr.sun_path, mode) == -1)
goto error;
if (listen(fd, 5) == -1)
goto error;
return fd;
error:
unlink(sockaddr.sun_path);
close(fd);
return -1;
}