summaryrefslogtreecommitdiff
path: root/src/libs6
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2017-07-12 18:22:55 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2017-07-12 18:22:55 +0000
commitf7c2e436ed0144d09cff0e3ac90f510a47f0aeac (patch)
tree2306f029b468623ed316ac3df40a4df8d13c5c5e /src/libs6
parentf80dbef73f98ae1a801078e5f69a071651e6b52d (diff)
downloads6-f7c2e436ed0144d09cff0e3ac90f510a47f0aeac.tar.xz
Add s6_svc_lock_take() and s6_svc_lock_release()
Allows fixing a race condition in s6-rc-init. s6-supervise also creates event/ before supervise/ so that locking the servicedir ensures event/ can be used.
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 ;
+}