From 73638ee8e3c7b0ee0d6603d1ead2e3829d9f7686 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Sat, 3 Apr 2021 10:18:24 +0000 Subject: bugfix: setlock -r --- src/daemontools-extras/s6-setlock.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/daemontools-extras/s6-setlock.c b/src/daemontools-extras/s6-setlock.c index cd938b7..6ba2ec8 100644 --- a/src/daemontools-extras/s6-setlock.c +++ b/src/daemontools-extras/s6-setlock.c @@ -42,9 +42,25 @@ int main (int argc, char const *const *argv) if (nb < 2) { - int r ; - int fd = open_create(argv[0]) ; - if (fd < 0) strerr_diefu2sys(111, "open_create ", argv[0]) ; + int r, fd ; + if (ex) + { + fd = open_create(argv[0]) ; + if (fd < 0) strerr_diefu3sys(111, "open ", argv[0], " for writing") ; + } + else + { + fd = open_read(argv[0]) ; + if (fd < 0) + { + if (errno != ENOENT) strerr_diefu3sys(111, "open ", argv[0], " for reading") ; + fd = open_create(argv[0]) ; + if (fd < 0) strerr_diefu2sys(111, "create ", argv[0]) ; + close(fd) ; + fd = open_read(argv[0]) ; + if (fd < 0) strerr_diefu3sys(111, "open ", argv[0], " for reading") ; + } + } r = fd_lock(fd, ex, nb) ; if (!r) errno = EBUSY ; if (r < 1) strerr_diefu2sys(1, "lock ", argv[0]) ; -- cgit v1.2.3