abduco

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

commit 91b7338f9dbdb746b834395f1efca86066b75076
parent af531f98ae5c0503cb81af213f7df9c6982265dd
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Sat, 21 Feb 2015 18:03:56 +0100

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).

Diffstat:
server.c | 7++-----
1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/server.c b/server.c @@ -38,13 +38,10 @@ 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 = S_IRUSR|S_IWUSR; - if (fchmod(fd, mode) == -1) - goto error1; + mode_t mode = umask(S_IXUSR|S_IXGRP|S_IRWXO); if (bind(fd, (struct sockaddr*)&sockaddr, socklen) == -1) goto error1; - if (fchmod(fd, mode) == -1 || chmod(sockaddr.sun_path, mode) == -1) - goto error2; + umask(mode); if (listen(fd, 5) == -1) goto error2; return fd;