aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstddjb/ipc_bind_reuse_lock.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2020-11-29 21:02:32 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2020-11-29 21:02:32 +0000
commit90b819c6d832046840018ff08b9bc5d0e3b69c37 (patch)
treeefea05788cc982395ee114474d84096e7fc70862 /src/libstddjb/ipc_bind_reuse_lock.c
parente6c5c984461dc4cec0ef2d68524d6bd457e23853 (diff)
downloadskalibs-90b819c6d832046840018ff08b9bc5d0e3b69c37.tar.gz
Revamp lock primitives; prepare for 2.10.0.0 instead of 2.9.4.0
flock() doesn't have a way to test for a lock without taking it. lockf() doesn't have shared locks. The only way to have both is fcntl(). So I rewrote all the locking stuff around fcntl(), and used the opportunity to change the interface. The point of changing the interface is to stop having to bother with the old one, so to hell with compatibility, let's just do a major bump.
Diffstat (limited to 'src/libstddjb/ipc_bind_reuse_lock.c')
-rw-r--r--src/libstddjb/ipc_bind_reuse_lock.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/libstddjb/ipc_bind_reuse_lock.c b/src/libstddjb/ipc_bind_reuse_lock.c
index aede744..b1bccfd 100644
--- a/src/libstddjb/ipc_bind_reuse_lock.c
+++ b/src/libstddjb/ipc_bind_reuse_lock.c
@@ -4,6 +4,7 @@
#include <string.h>
#include <unistd.h>
+#include <errno.h>
#include <sys/socket.h>
#include <skalibs/djbunix.h>
@@ -14,12 +15,15 @@ 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 = openc_create(lockname) ;
if (fd < 0) return -1 ;
- if (lock_exnb(fd) < 0) return -1 ;
+ r = fd_lock(fd, 1, 1) ;
+ if (r < 0) return -1 ;
+ if (!r) return (errno = EBUSY, -1) ;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) ;
unlink(p) ;
if (ipc_bind(s, p) < 0) return -1 ;