From 543c1405653c48d23e19d03d7c62e9534fc9a110 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 23 Mar 2017 14:34:16 +0000 Subject: Don't wait on locks by default (fail instead); add a -b option to get the waiting behaviour Also fix a few types in s6-rc-compile (unsigned int -> uint32_t for avltree indices) --- src/libs6rc/s6rc_lock.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/libs6rc') diff --git a/src/libs6rc/s6rc_lock.c b/src/libs6rc/s6rc_lock.c index dab3035..2a623d0 100644 --- a/src/libs6rc/s6rc_lock.c +++ b/src/libs6rc/s6rc_lock.c @@ -5,7 +5,17 @@ #include #include -int s6rc_lock (char const *live, int lwhat, int *llfd, char const *compiled, int cwhat, int *ccfd) +static inline int lockex (int fd, int blocking) +{ + return blocking ? lock_ex(fd) : lock_exnb(fd) ; +} + +static inline int locksh (int fd, int blocking) +{ + return blocking ? lock_sh(fd) : lock_shnb(fd) ; +} + +int s6rc_lock (char const *live, int lwhat, int *llfd, char const *compiled, int cwhat, int *ccfd, int blocking) { int e = 0 ; int lfd = -1, cfd = -1 ; @@ -18,7 +28,7 @@ int s6rc_lock (char const *live, int lwhat, int *llfd, char const *compiled, int memcpy(lfn + llen, "/lock", 6) ; lfd = open_create(lfn) ; if (lfd < 0) return 0 ; - if ((lwhat > 1 ? lock_ex(lfd) : lock_sh(lfd)) < 0) { e = errno ; goto lerr ; } + if ((lwhat > 1 ? lockex(lfd, blocking) : locksh(lfd, blocking)) < 0) { e = errno ; goto lerr ; } } if (cwhat) @@ -31,7 +41,7 @@ int s6rc_lock (char const *live, int lwhat, int *llfd, char const *compiled, int 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 ; } + else if ((cwhat > 1 ? lockex(cfd, blocking) : locksh(cfd, blocking)) < 0) { e = errno ; goto cerr ; } } if (lwhat) *llfd = lfd ; -- cgit v1.2.3