summaryrefslogtreecommitdiff
path: root/src/libs6rc
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2015-08-29 20:25:40 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2015-08-29 20:25:40 +0000
commitee111aabf49128fe88968f658b0f4b683a3caa00 (patch)
tree4e62e9b5728eb711ff48d8ff017ab48aeb6f9775 /src/libs6rc
parent381b64487199a9edd46e84f7ff134722c3c3bcfc (diff)
downloads6-rc-ee111aabf49128fe88968f658b0f4b683a3caa00.tar.xz
- some work on s6-rc-update
- add s6-rc-bundle and relevant doc
Diffstat (limited to 'src/libs6rc')
-rw-r--r--src/libs6rc/deps-lib/s6rc1
-rw-r--r--src/libs6rc/s6rc_lock.c48
2 files changed, 49 insertions, 0 deletions
diff --git a/src/libs6rc/deps-lib/s6rc b/src/libs6rc/deps-lib/s6rc
index 1ee4dfb..00c3aab 100644
--- a/src/libs6rc/deps-lib/s6rc
+++ b/src/libs6rc/deps-lib/s6rc
@@ -5,6 +5,7 @@ s6rc_db_read.o
s6rc_db_read_sizes.o
s6rc_db_read_uint32.o
s6rc_graph_closure.o
+s6rc_lock.o
s6rc_read_uint.o
s6rc_sanitize_dir.o
s6rc_servicedir_internal.o
diff --git a/src/libs6rc/s6rc_lock.c b/src/libs6rc/s6rc_lock.c
new file mode 100644
index 0000000..69ec75b
--- /dev/null
+++ b/src/libs6rc/s6rc_lock.c
@@ -0,0 +1,48 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <skalibs/bytestr.h>
+#include <skalibs/diuint.h>
+#include <skalibs/djbunix.h>
+#include <s6-rc/s6rc-utils.h>
+
+int s6rc_lock (char const *live, int lwhat, int *llfd, char const *compiled, int cwhat, int *ccfd)
+{
+ int e ;
+ int lfd = -1, cfd = -1 ;
+
+ if (lwhat)
+ {
+ unsigned int llen = str_len(live) ;
+ char lfn[llen + 6] ;
+ byte_copy(lfn, llen, live) ;
+ byte_copy(lfn + llen, 6, "/lock") ;
+ lfd = open_create(lfn) ;
+ if (lfd < 0) return 0 ;
+ if ((lwhat > 1 ? lock_ex(lfd) : lock_sh(lfd)) < 0) { e = errno ; goto lerr ; }
+ }
+
+ if (cwhat)
+ {
+ unsigned int clen = str_len(compiled) ;
+ char cfn[clen + 6] ;
+ byte_copy(cfn, clen, compiled) ;
+ byte_copy(cfn + clen, 6, "/lock") ;
+ cfd = open_create(cfn) ;
+ if (cfd < 0)
+ if (cwhat == 1 || errno != EROFS) { e = errno ; goto lerr ; }
+ else cfd = -errno ;
+ else if ((cwhat > 1 ? lock_ex(cfd) : lock_sh(cfd)) < 0) { e = errno ; goto cerr ; }
+ }
+
+ if (lwhat) *llfd = lfd ;
+ if (cwhat) *ccfd = cfd ;
+ return 1 ;
+
+ cerr:
+ fd_close(cfd) ;
+ lerr:
+ if (lwhat) fd_close(lfd) ;
+ errno = e ;
+ return 0 ;
+}