From 90b819c6d832046840018ff08b9bc5d0e3b69c37 Mon Sep 17 00:00:00 2001
From: Laurent Bercot
Date: Sun, 29 Nov 2020 21:02:32 +0000
Subject: 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.
---
NEWS | 7 +++---
doc/index.html | 2 +-
doc/libstddjb/djbunix.html | 45 +++++++++++--------------------------
doc/license.html | 2 +-
doc/upgrade.html | 4 +++-
package/deps.mak | 11 ++++-----
package/info | 2 +-
src/include/skalibs/djbunix.h | 12 +++++-----
src/include/skalibs/exec.h | 2 ++
src/libstddjb/fd_islocked.c | 17 ++++++++++++++
src/libstddjb/fd_lock.c | 25 +++++++++++++++++++++
src/libstddjb/fd_unlock.c | 20 +++++++++++++++++
src/libstddjb/ipc_bind_reuse_lock.c | 6 ++++-
src/libstddjb/lock_ex.c | 34 ----------------------------
src/libstddjb/lock_exnb.c | 35 -----------------------------
src/libstddjb/lock_sh.c | 34 ----------------------------
src/libstddjb/lock_shnb.c | 35 -----------------------------
src/libstddjb/lock_un.c | 34 ----------------------------
src/libstddjb/lock_unx.c | 11 ---------
19 files changed, 101 insertions(+), 237 deletions(-)
create mode 100644 src/libstddjb/fd_islocked.c
create mode 100644 src/libstddjb/fd_lock.c
create mode 100644 src/libstddjb/fd_unlock.c
delete mode 100644 src/libstddjb/lock_ex.c
delete mode 100644 src/libstddjb/lock_exnb.c
delete mode 100644 src/libstddjb/lock_sh.c
delete mode 100644 src/libstddjb/lock_shnb.c
delete mode 100644 src/libstddjb/lock_un.c
delete mode 100644 src/libstddjb/lock_unx.c
diff --git a/NEWS b/NEWS
index deb04ba..d390e6a 100644
--- a/NEWS
+++ b/NEWS
@@ -1,14 +1,15 @@
Changelog for skalibs.
-In 2.9.4.0
-----------
+In 2.10.0.0
+-----------
- New openc_* functions.
- New function: ipc_bind_reuse_lock(), taking a lock before
unconditionally deleting the socket.
- ipc_bind_reuse() rewritten to use ipc_bind_reuse_lock(),
so it does the right thing instead of clobbering sockets.
- - Complete revamping of the pathexec functions, see exec.h
+ - Complete revamping of the pathexec functions, see exec.h.
+ - Revamping of the locking functions.
In 2.9.3.0
diff --git a/doc/index.html b/doc/index.html
index c7cb59c..66095a2 100644
--- a/doc/index.html
+++ b/doc/index.html
@@ -60,7 +60,7 @@ with a standard C development environment
Download
- - The current released version of skalibs is 2.9.4.0.
+ - The current released version of skalibs is 2.10.0.0.
- Alternatively, you can checkout a copy of the
skalibs
git repository:
diff --git a/doc/libstddjb/djbunix.html b/doc/libstddjb/djbunix.html
index e70fe4f..54aaf50 100644
--- a/doc/libstddjb/djbunix.html
+++ b/doc/libstddjb/djbunix.html
@@ -202,45 +202,26 @@ with errno saved, used essentially to isolate application code from
- int lock_ex (int fd)
-Gets an exclusive advisory lock on fd. fd must point to
-a regular file, open for writing. Blocks until the lock can be obtained.
-Returns 0 if it succeeds, or -1 (and sets errno) if it fails.
-
-
-
- int lock_exnb (int fd)
-Gets an exclusive advisory lock on fd. fd must point to
-a regular file, open for writing.
-Returns 0 if it succeeds, or -1 (and sets errno) if it fails. If the lock
-is held and the function would block, it immediately returns with -1 EWOULDBLOCK.
-
-
-
- int lock_sh (int fd)
-Gets a shared advisory lock on fd. fd must point to
-a regular file, open for reading. Blocks until the lock can be obtained.
-Returns 0 if it succeeds, or -1 (and sets errno) if it fails.
-
-
-
- int lock_shnb (int fd)
-Gets a shared advisory lock on fd. fd must point to
-a regular file, open for reading.
-Returns 0 if it succeeds, or -1 (and sets errno) if it fails. If the lock
-is held and the function would block, it immediately returns with -1 EWOULDBLOCK.
+ int fd_lock (int fd, int w, int nb)
+Gets an advisory lock on fd: shared if w is
+zero, exclusive otherwise. fd must point to
+a regular file, open for writing or reading depending on whether
+you want an exclusive lock or not. If nb is zero, the
+function blocks until the lock can be obtained; otherwise it
+returns 0 immediately. On success, the function returns 1 ; it
+returns 0 if it cannot take the lock, or -1 (and sets errno) if
+an error occurs.
- int lock_un (int fd)
+ void fd_unlock (int fd)
Releases a previously held lock on fd.
-Returns 0 if it succeeds, or -1 (and sets errno) if it fails.
- void lock_unx (int fd)
-Like lock_un, but without a return code and without
-modifying errno.
+ int fd_islocked (int fd)
+Returns 1 if a lock is currently held on fd, 0 otherwise.
+Returns -1 (and sets errno) if an error occurs.
diff --git a/doc/license.html b/doc/license.html
index 9aae77e..bfc1d5f 100644
--- a/doc/license.html
+++ b/doc/license.html
@@ -74,7 +74,7 @@ color, or different text font.
I am aware that the previous restrictions sound completely
ridiculous while the official skalibs documentation is incomplete.
-As of 2.9.4.0, I'm not going to enforce those restrictions, but if you're
+As of 2.10.0.0, I'm not going to enforce those restrictions, but if you're
going to provide documentation for skalibs, don't keep it to yourself,
please send it to me instead. :-)
diff --git a/doc/upgrade.html b/doc/upgrade.html
index da2185c..8a87d7d 100644
--- a/doc/upgrade.html
+++ b/doc/upgrade.html
@@ -16,7 +16,7 @@
skarnet.org
- in 2.9.4.0
+ in 2.10.0.0
- New openc_* functions, which are O_CLOEXEC versions of the
@@ -25,6 +25,8 @@
deleting a Unix domain socket. The ipc_bind_reuse() function now
uses it, so it won't unconditionally clobber sockets in the filesystem anymore.
- Complete revamping of the pathexec functions, see exec.h.
+ - Revamping of the locking functions, see
+
-
-#ifdef SKALIBS_HASFLOCK
-
-#include
-#include
-#include
-#include
-
-int lock_ex (int fd)
-{
- int r ;
- do r = flock(fd, LOCK_EX) ;
- while ((r == -1) && (errno == EINTR)) ;
- return r ;
-}
-
-#else
-
-#include
-#include
-#include
-
-int lock_ex (int fd)
-{
- int r ;
- do r = lockf(fd, F_LOCK, 0) ;
- while ((r == -1) && (errno == EINTR)) ;
- return r ;
-}
-
-#endif
diff --git a/src/libstddjb/lock_exnb.c b/src/libstddjb/lock_exnb.c
deleted file mode 100644
index 7d5302f..0000000
--- a/src/libstddjb/lock_exnb.c
+++ /dev/null
@@ -1,35 +0,0 @@
-/* ISC license. */
-
-#include
-
-#ifdef SKALIBS_HASFLOCK
-
-#include
-#include
-#include
-#include
-
-int lock_exnb (int fd)
-{
- int r ;
- do r = flock(fd, LOCK_EX | LOCK_NB) ;
- while ((r == -1) && (errno == EINTR)) ;
- return r ;
-}
-
-#else
-
-#include
-#include
-#include
-
-int lock_exnb (int fd)
-{
- int r ;
- do r = lockf(fd, F_TLOCK, 0) ;
- while ((r == -1) && (errno == EINTR)) ;
- if ((r == -1) && (errno == EACCES)) errno = EAGAIN ;
- return r ;
-}
-
-#endif
diff --git a/src/libstddjb/lock_sh.c b/src/libstddjb/lock_sh.c
deleted file mode 100644
index c942d0d..0000000
--- a/src/libstddjb/lock_sh.c
+++ /dev/null
@@ -1,34 +0,0 @@
-/* ISC license. */
-
-#include
-
-#ifdef SKALIBS_HASFLOCK
-
-#include
-#include
-#include
-#include
-
-int lock_sh (int fd)
-{
- int r ;
- do r = flock(fd, LOCK_SH) ;
- while ((r == -1) && (errno == EINTR)) ;
- return r ;
-}
-
-#else
-
-#include
-#include
-#include
-
-int lock_sh (int fd)
-{
- int r ;
- do r = lockf(fd, F_LOCK, 0) ;
- while ((r == -1) && (errno == EINTR)) ;
- return r ;
-}
-
-#endif
diff --git a/src/libstddjb/lock_shnb.c b/src/libstddjb/lock_shnb.c
deleted file mode 100644
index 40c988d..0000000
--- a/src/libstddjb/lock_shnb.c
+++ /dev/null
@@ -1,35 +0,0 @@
-/* ISC license. */
-
-#include
-
-#ifdef SKALIBS_HASFLOCK
-
-#include
-#include
-#include
-#include
-
-int lock_shnb (int fd)
-{
- int r ;
- do r = flock(fd, LOCK_SH | LOCK_NB) ;
- while ((r == -1) && (errno == EINTR)) ;
- return r ;
-}
-
-#else
-
-#include
-#include
-#include
-
-int lock_shnb (int fd)
-{
- int r ;
- do r = lockf(fd, F_TLOCK, 0) ;
- while ((r == -1) && (errno == EINTR)) ;
- if ((r == -1) && (errno == EACCES)) errno = EAGAIN ;
- return r ;
-}
-
-#endif
diff --git a/src/libstddjb/lock_un.c b/src/libstddjb/lock_un.c
deleted file mode 100644
index a029a7c..0000000
--- a/src/libstddjb/lock_un.c
+++ /dev/null
@@ -1,34 +0,0 @@
-/* ISC license. */
-
-#include
-
-#ifdef SKALIBS_HASFLOCK
-
-#include
-#include
-#include
-#include
-
-int lock_un (int fd)
-{
- int r ;
- do r = flock(fd, LOCK_UN) ;
- while ((r == -1) && (errno == EINTR)) ;
- return r ;
-}
-
-#else
-
-#include
-#include
-#include
-
-int lock_un (int fd)
-{
- int r ;
- do r = lockf(fd, F_ULOCK, 0) ;
- while ((r == -1) && (errno == EINTR)) ;
- return r ;
-}
-
-#endif
diff --git a/src/libstddjb/lock_unx.c b/src/libstddjb/lock_unx.c
deleted file mode 100644
index d2f8483..0000000
--- a/src/libstddjb/lock_unx.c
+++ /dev/null
@@ -1,11 +0,0 @@
-/* ISC license. */
-
-#include
-#include
-
-void lock_unx (int fd)
-{
- int e = errno ;
- lock_un(fd) ;
- errno = e ;
-}
--
cgit v1.2.3