summaryrefslogtreecommitdiff
path: root/src/libs6/s6lockd_openandlock.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2021-05-04 12:16:18 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2021-05-04 12:16:18 +0000
commit95157aa7d44750e248665744518f453ec65e2636 (patch)
tree658736d14430f4188c7e0420e3f3a23de0936c1a /src/libs6/s6lockd_openandlock.c
parent80882cf01005ea49672c496eff9f622d5ee5751d (diff)
downloads6-95157aa7d44750e248665744518f453ec65e2636.tar.xz
Also allow shared locks in s6lockd-helper
Diffstat (limited to 'src/libs6/s6lockd_openandlock.c')
-rw-r--r--src/libs6/s6lockd_openandlock.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/libs6/s6lockd_openandlock.c b/src/libs6/s6lockd_openandlock.c
new file mode 100644
index 0000000..42c3ca6
--- /dev/null
+++ b/src/libs6/s6lockd_openandlock.c
@@ -0,0 +1,35 @@
+/* ISC license. */
+
+#include <errno.h>
+
+#include <skalibs/strerr2.h>
+#include <skalibs/djbunix.h>
+
+#include "s6lockd.h"
+
+int s6lockd_openandlock (char const *file, int ex, int nb)
+{
+ int fd, r ;
+ if (ex)
+ {
+ fd = open_create(file) ;
+ if (fd < 0) strerr_diefu3sys(111, "open ", file, " for writing") ;
+ }
+ else
+ {
+ fd = open_read(file) ;
+ if (fd < 0)
+ {
+ if (errno != ENOENT) strerr_diefu3sys(111, "open ", file, " for reading") ;
+ fd = open_create(file) ;
+ if (fd < 0) strerr_diefu2sys(111, "create ", file) ;
+ fd_close(fd) ;
+ fd = open_read(file) ;
+ if (fd < 0) strerr_diefu3sys(111, "open ", file, " for reading") ;
+ }
+ }
+ r = fd_lock(fd, ex, nb) ;
+ if (!r) errno = EBUSY ;
+ if (r < 1) strerr_diefu2sys(1, "lock ", file) ;
+ return fd ;
+}