skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit b992db748b9ffe8d88d609950f5da28b5a1db135
parent 34896cda4e7156a5e232dfa4cde92d1ea1e93da2
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Sun,  8 Nov 2020 09:32:32 +0000

 Add ipc_bind_reuse_lock, rewrite ipc_bind_reuse to use it

Diffstat:
Mpackage/deps.mak | 3++-
Msrc/include/skalibs/webipc.h | 1+
Msrc/libstddjb/ipc_bind_reuse.c | 9++-------
Asrc/libstddjb/ipc_bind_reuse_lock.c | 28++++++++++++++++++++++++++++
4 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/package/deps.mak b/package/deps.mak @@ -400,7 +400,8 @@ src/libstddjb/ip6_scan.o src/libstddjb/ip6_scan.lo: src/libstddjb/ip6_scan.c src src/libstddjb/ip6_scanlist.o src/libstddjb/ip6_scanlist.lo: src/libstddjb/ip6_scanlist.c src/include/skalibs/fmtscan.h 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/sysdeps.h src/include/skalibs/webipc.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/webipc.h -src/libstddjb/ipc_bind_reuse.o src/libstddjb/ipc_bind_reuse.lo: src/libstddjb/ipc_bind_reuse.c src/include/skalibs/nonposix.h src/include/skalibs/webipc.h +src/libstddjb/ipc_bind_reuse.o src/libstddjb/ipc_bind_reuse.lo: src/libstddjb/ipc_bind_reuse.c src/include/skalibs/webipc.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/nonposix.h src/include/skalibs/webipc.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/webipc.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/webipc.h src/libstddjb/ipc_dgram.o src/libstddjb/ipc_dgram.lo: src/libstddjb/ipc_dgram.c src/include/skalibs/djbunix.h src/include/skalibs/nonposix.h src/include/skalibs/webipc.h diff --git a/src/include/skalibs/webipc.h b/src/include/skalibs/webipc.h @@ -40,6 +40,7 @@ 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 *) ; 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.c b/src/libstddjb/ipc_bind_reuse.c @@ -1,14 +1,9 @@ /* ISC license. */ -#include <skalibs/nonposix.h> -#include <sys/socket.h> -#include <unistd.h> #include <skalibs/webipc.h> int ipc_bind_reuse (int s, char const *p) { - unsigned int opt = 1 ; - setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) ; - unlink(p) ; - return ipc_bind(s, p) ; + int fdlock ; + return ipc_bind_reuse_lock(s, p, &fdlock) ; } diff --git a/src/libstddjb/ipc_bind_reuse_lock.c b/src/libstddjb/ipc_bind_reuse_lock.c @@ -0,0 +1,28 @@ +/* ISC license. */ + +#include <skalibs/nonposix.h> + +#include <string.h> +#include <unistd.h> +#include <sys/socket.h> + +#include <skalibs/djbunix.h> +#include <skalibs/webipc.h> + +int ipc_bind_reuse_lock (int s, char const *p, int *fdlock) +{ + unsigned int opt = 1 ; + size_t len = strlen(p) ; + int fd ; + char lockname[len + 6] ; + memcpy(lockname, p, len) ; + memcpy(lockname + len, ".lock", 6) ; + fd = openc_create(lockname) ; + if (fd < 0) return -1 ; + if (lock_exnb(fd) < 0) return -1 ; + setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) ; + unlink(p) ; + if (ipc_bind(s, p) < 0) return -1 ; + *fdlock = fd ; + return 0 ; +}