summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2021-08-22 02:05:58 +0000
committerLaurent Bercot <ska@appnovation.com>2021-08-22 02:05:58 +0000
commitb8e8f5b0c490063a351a1cb5740419b8ba45a2e6 (patch)
tree9dd3343b76d295e748a94c47d06161cb63c38636 /src
parent8e6215ee13dc507f2c0a4ab8a02e2d974265d83d (diff)
downloadskalibs-b8e8f5b0c490063a351a1cb5740419b8ba45a2e6.tar.xz
Add ipc_bind_reuse_perms
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r--src/include/skalibs/socket.h4
-rw-r--r--src/libstddjb/ipc_bind_reuse_lock_perms.c (renamed from src/libstddjb/ipc_bind_reuse_lock.c)12
-rw-r--r--src/libstddjb/ipc_bind_reuse_perms.c9
3 files changed, 22 insertions, 3 deletions
diff --git a/src/include/skalibs/socket.h b/src/include/skalibs/socket.h
index 1ee9970..85340bb 100644
--- a/src/include/skalibs/socket.h
+++ b/src/include/skalibs/socket.h
@@ -48,7 +48,9 @@ extern int ipc_pair_internal (int *, unsigned int) ;
extern int ipc_bind (int, char const *) ;
extern int ipc_bind_reuse (int, char const *) ;
-extern int ipc_bind_reuse_lock (int, char const *, int *) ;
+#define ipc_bind_reuse_lock(fd, path, fdlock) ipc_bind_reuse_lock_perms(fd, path, (fdlock), 0)
+extern int ipc_bind_reuse_perms (int, char const *, unsigned int) ;
+extern int ipc_bind_reuse_lock_perms (int, char const *, int *, unsigned int) ;
extern int ipc_listen (int, int) ;
#define ipc_accept(s, path, len, trunc) ipc_accept_internal(s, path, len, (trunc), 0)
diff --git a/src/libstddjb/ipc_bind_reuse_lock.c b/src/libstddjb/ipc_bind_reuse_lock_perms.c
index 09e440c..6e4d9ff 100644
--- a/src/libstddjb/ipc_bind_reuse_lock.c
+++ b/src/libstddjb/ipc_bind_reuse_lock_perms.c
@@ -7,16 +7,18 @@
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
+#include <sys/stat.h>
#include <skalibs/djbunix.h>
#include <skalibs/socket.h>
-int ipc_bind_reuse_lock (int s, char const *p, int *fdlock)
+int ipc_bind_reuse_lock_perms (int s, char const *p, int *fdlock, unsigned int perms)
{
unsigned int opt = 1 ;
size_t len = strlen(p) ;
int fd ;
int r ;
+ mode_t m ;
char lockname[len + 6] ;
memcpy(lockname, p, len) ;
memcpy(lockname + len, ".lock", 6) ;
@@ -29,7 +31,13 @@ int ipc_bind_reuse_lock (int s, char const *p, int *fdlock)
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) ;
errno = r ;
unlink_void(p) ;
- if (ipc_bind(s, p) < 0) return -1 ;
+ if (perms) m = umask(~perms & 0777) ;
+ if (ipc_bind(s, p) < 0)
+ {
+ if (perms) umask(m) ;
+ return -1 ;
+ }
+ if (perms) umask(m) ;
*fdlock = fd ;
return 0 ;
}
diff --git a/src/libstddjb/ipc_bind_reuse_perms.c b/src/libstddjb/ipc_bind_reuse_perms.c
new file mode 100644
index 0000000..6e25068
--- /dev/null
+++ b/src/libstddjb/ipc_bind_reuse_perms.c
@@ -0,0 +1,9 @@
+/* ISC license. */
+
+#include <skalibs/socket.h>
+
+int ipc_bind_reuse_perms (int s, char const *p, unsigned int perms)
+{
+ int fdlock ;
+ return ipc_bind_reuse_lock_perms(s, p, &fdlock, perms) ;
+}