summaryrefslogtreecommitdiff
path: root/src/libs6
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs6')
-rw-r--r--src/libs6/deps-lib/s61
-rw-r--r--src/libs6/s6_svc_lock_take.c28
2 files changed, 29 insertions, 0 deletions
diff --git a/src/libs6/deps-lib/s6 b/src/libs6/deps-lib/s6
index ace5a05..5acb672 100644
--- a/src/libs6/deps-lib/s6
+++ b/src/libs6/deps-lib/s6
@@ -26,6 +26,7 @@ s6_accessrules_uidgid_cdb.o
s6_accessrules_uidgid_fs.o
s6_supervise_lock.o
s6_supervise_lock_mode.o
+s6_svc_lock_take.o
s6_svc_ok.o
s6_svc_write.o
s6_svc_writectl.o
diff --git a/src/libs6/s6_svc_lock_take.c b/src/libs6/s6_svc_lock_take.c
new file mode 100644
index 0000000..57830c0
--- /dev/null
+++ b/src/libs6/s6_svc_lock_take.c
@@ -0,0 +1,28 @@
+/* ISC license. */
+
+#include <sys/stat.h>
+#include <string.h>
+#include <errno.h>
+#include <skalibs/djbunix.h>
+#include <s6/s6-supervise.h>
+
+int s6_svc_lock_take (char const *dir)
+{
+ size_t dirlen = strlen(dir) ;
+ int fdlock ;
+ char lock[dirlen + sizeof(S6_SUPERVISE_CTLDIR) + 6] ;
+ memcpy(lock, dir, dirlen) ;
+ memcpy(lock + dirlen, "/" S6_SUPERVISE_CTLDIR, sizeof(S6_SUPERVISE_CTLDIR) + 1) ;
+ if ((mkdir(lock, S_IRWXU) < 0) && (errno != EEXIST)) return -1 ;
+ memcpy(lock + dirlen + sizeof(S6_SUPERVISE_CTLDIR), "/lock", 6) ;
+ fdlock = open_create(lock) ;
+ if (fdlock < 0) return -1 ;
+ if (coe(fdlock) < 0 || lock_ex(fdlock) < 0)
+ {
+ int e = errno ;
+ fd_close(fdlock) ;
+ errno = e ;
+ return -1 ;
+ }
+ return fdlock ;
+}