forked from github/abduco
Set socket creation permission in a portable way
fchmod(2) on a socket file descriptor is unspecified behaviour which happens to work on Linux. Use umask(2) instead which should affect the permissions used by bind(2).
This commit is contained in:
parent
af531f98ae
commit
91b7338f9d
7
server.c
7
server.c
|
|
@ -38,13 +38,10 @@ static int server_create_socket(const char *name) {
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
return -1;
|
return -1;
|
||||||
socklen_t socklen = offsetof(struct sockaddr_un, sun_path) + strlen(sockaddr.sun_path) + 1;
|
socklen_t socklen = offsetof(struct sockaddr_un, sun_path) + strlen(sockaddr.sun_path) + 1;
|
||||||
mode_t mode = S_IRUSR|S_IWUSR;
|
mode_t mode = umask(S_IXUSR|S_IXGRP|S_IRWXO);
|
||||||
if (fchmod(fd, mode) == -1)
|
|
||||||
goto error1;
|
|
||||||
if (bind(fd, (struct sockaddr*)&sockaddr, socklen) == -1)
|
if (bind(fd, (struct sockaddr*)&sockaddr, socklen) == -1)
|
||||||
goto error1;
|
goto error1;
|
||||||
if (fchmod(fd, mode) == -1 || chmod(sockaddr.sun_path, mode) == -1)
|
umask(mode);
|
||||||
goto error2;
|
|
||||||
if (listen(fd, 5) == -1)
|
if (listen(fd, 5) == -1)
|
||||||
goto error2;
|
goto error2;
|
||||||
return fd;
|
return fd;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue