From 91b7338f9dbdb746b834395f1efca86066b75076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sat, 21 Feb 2015 18:03:56 +0100 Subject: [PATCH] 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). --- server.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/server.c b/server.c index a1a4738..26a7eb6 100644 --- 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;