diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2022-11-10 13:16:19 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2022-11-10 13:16:19 +0000 |
commit | 8c7fa08f6ef2f7ed9afeaf5ffca70a3c8b48177f (patch) | |
tree | 511c4b3c8d71864c196f119f1e7fd7dbc82b2958 | |
parent | 2046e5ba1c5c6c9d62aaa686b9cdf655135af743 (diff) | |
download | s6-rc-8c7fa08f6ef2f7ed9afeaf5ffca70a3c8b48177f.tar.xz |
Open the shared locks O_RDONLY, duh.
Instead of writing workarounds for EROFS and chattr +i and still
failing to properly address other cases. Noob.
Signed-off-by: Laurent Bercot <ska@appnovation.com>
-rw-r--r-- | src/libs6rc/s6rc_lock.c | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/src/libs6rc/s6rc_lock.c b/src/libs6rc/s6rc_lock.c index 7762ea7..95e5d70 100644 --- a/src/libs6rc/s6rc_lock.c +++ b/src/libs6rc/s6rc_lock.c @@ -8,6 +8,11 @@ #include <s6-rc/s6rc-utils.h> +static inline int modefor (int what) +{ + return (what > 1 ? O_RDWR | O_CREAT | O_TRUNC : O_RDONLY) | O_NONBLOCK | O_CLOEXEC ; +} + int s6rc_lock (char const *live, int lwhat, int *llfd, char const *compiled, int cwhat, int *ccfd, int blocking) { int lfd = -1, cfd = -1 ; @@ -19,7 +24,7 @@ int s6rc_lock (char const *live, int lwhat, int *llfd, char const *compiled, int char lfn[llen + 6] ; memcpy(lfn, live, llen) ; memcpy(lfn + llen, "/lock", 6) ; - lfd = open(lfn, O_RDWR | O_CREAT | O_TRUNC | O_NONBLOCK | O_CLOEXEC, 0644) ; + lfd = open(lfn, modefor(lwhat), 0644) ; if (lfd < 0) return 0 ; r = fd_lock(lfd, lwhat > 1, !blocking) ; if (!r) errno = EBUSY ; @@ -28,24 +33,20 @@ int s6rc_lock (char const *live, int lwhat, int *llfd, char const *compiled, int if (cwhat) { + int r ; size_t clen = strlen(compiled) ; char cfn[clen + 6] ; memcpy(cfn, compiled, clen) ; memcpy(cfn + clen, "/lock", 6) ; - cfd = open(cfn, O_RDWR | O_CREAT | O_TRUNC | O_NONBLOCK | O_CLOEXEC, 0644) ; - if (cfd < 0) - if (cwhat > 1 || (errno != EROFS && errno != EPERM)) goto lerr ; - else cfd = -errno ; - else - { - int r = fd_lock(cfd, cwhat > 1, !blocking) ; - if (!r) errno = EBUSY ; - if (r < 1) goto cerr ; - } + cfd = open(cfn, modefor(cwhat), 0644) ; + if (cfd < 0) goto lerr ; + r = fd_lock(cfd, cwhat > 1, !blocking) ; + if (!r) errno = EBUSY ; + if (r < 1) goto cerr ; + *ccfd = cfd ; } if (lwhat) *llfd = lfd ; - if (cwhat) *ccfd = cfd ; return 1 ; cerr: |