From b8e8f5b0c490063a351a1cb5740419b8ba45a2e6 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Sun, 22 Aug 2021 02:05:58 +0000 Subject: Add ipc_bind_reuse_perms Signed-off-by: Laurent Bercot --- package/deps.mak | 3 ++- src/include/skalibs/socket.h | 4 ++- src/libstddjb/ipc_bind_reuse_lock.c | 35 ------------------------- src/libstddjb/ipc_bind_reuse_lock_perms.c | 43 +++++++++++++++++++++++++++++++ src/libstddjb/ipc_bind_reuse_perms.c | 9 +++++++ 5 files changed, 57 insertions(+), 37 deletions(-) delete mode 100644 src/libstddjb/ipc_bind_reuse_lock.c create mode 100644 src/libstddjb/ipc_bind_reuse_lock_perms.c create mode 100644 src/libstddjb/ipc_bind_reuse_perms.c diff --git a/package/deps.mak b/package/deps.mak index a661837..936734f 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -392,7 +392,8 @@ src/libstddjb/ip6_scanlist.o src/libstddjb/ip6_scanlist.lo: src/libstddjb/ip6_sc src/libstddjb/ipc_accept.o src/libstddjb/ipc_accept.lo: src/libstddjb/ipc_accept.c src/include/skalibs/bytestr.h src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/socket.h src/include/skalibs/sysdeps.h src/libstddjb/ipc_bind.o src/libstddjb/ipc_bind.lo: src/libstddjb/ipc_bind.c src/include/skalibs/nonposix.h src/include/skalibs/posixishard.h src/include/skalibs/socket.h src/libstddjb/ipc_bind_reuse.o src/libstddjb/ipc_bind_reuse.lo: src/libstddjb/ipc_bind_reuse.c src/include/skalibs/socket.h -src/libstddjb/ipc_bind_reuse_lock.o src/libstddjb/ipc_bind_reuse_lock.lo: src/libstddjb/ipc_bind_reuse_lock.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/socket.h +src/libstddjb/ipc_bind_reuse_lock_perms.o src/libstddjb/ipc_bind_reuse_lock_perms.lo: src/libstddjb/ipc_bind_reuse_lock_perms.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/socket.h +src/libstddjb/ipc_bind_reuse_perms.o src/libstddjb/ipc_bind_reuse_perms.lo: src/libstddjb/ipc_bind_reuse_perms.c src/include/skalibs/socket.h src/libstddjb/ipc_connect.o src/libstddjb/ipc_connect.lo: src/libstddjb/ipc_connect.c src/include/skalibs/nonposix.h src/include/skalibs/posixishard.h src/include/skalibs/socket.h src/libstddjb/ipc_connected.o src/libstddjb/ipc_connected.lo: src/libstddjb/ipc_connected.c src/include/skalibs/allreadwrite.h src/include/skalibs/nonposix.h src/include/skalibs/socket.h src/libstddjb/ipc_dgram.o src/libstddjb/ipc_dgram.lo: src/libstddjb/ipc_dgram.c src/include/skalibs/nonposix.h src/include/skalibs/socket.h 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.c deleted file mode 100644 index 09e440c..0000000 --- a/src/libstddjb/ipc_bind_reuse_lock.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ISC license. */ - -#include - -#include -#include -#include -#include -#include - -#include -#include - -int ipc_bind_reuse_lock (int s, char const *p, int *fdlock) -{ - unsigned int opt = 1 ; - size_t len = strlen(p) ; - int fd ; - int r ; - char lockname[len + 6] ; - memcpy(lockname, p, len) ; - memcpy(lockname + len, ".lock", 6) ; - fd = open3(lockname, O_WRONLY | O_NONBLOCK | O_CREAT | O_CLOEXEC, 0600) ; - if (fd < 0) return -1 ; - r = fd_lock(fd, 1, 1) ; - if (r < 0) return -1 ; - if (!r) return (errno = EBUSY, -1) ; - r = errno ; - setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) ; - errno = r ; - unlink_void(p) ; - if (ipc_bind(s, p) < 0) return -1 ; - *fdlock = fd ; - return 0 ; -} diff --git a/src/libstddjb/ipc_bind_reuse_lock_perms.c b/src/libstddjb/ipc_bind_reuse_lock_perms.c new file mode 100644 index 0000000..6e4d9ff --- /dev/null +++ b/src/libstddjb/ipc_bind_reuse_lock_perms.c @@ -0,0 +1,43 @@ +/* ISC license. */ + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +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) ; + fd = open3(lockname, O_WRONLY | O_NONBLOCK | O_CREAT | O_CLOEXEC, 0600) ; + if (fd < 0) return -1 ; + r = fd_lock(fd, 1, 1) ; + if (r < 0) return -1 ; + if (!r) return (errno = EBUSY, -1) ; + r = errno ; + setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) ; + errno = r ; + unlink_void(p) ; + 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 + +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) ; +} -- cgit v1.2.3