From: Mark Andrews Date: Thu, 26 Oct 2023 03:50:43 +0000 (+1100) Subject: Only remove the lock file if we managed to lock it X-Git-Tag: v9.19.18~23^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b9c789b8b34a45a1572590a9ef4c8aa1306b4c63;p=thirdparty%2Fbind9.git Only remove the lock file if we managed to lock it The lock file was being removed when we hadn't successfully locked it which defeated the purpose of the lockfile. Adjust cleanup_lockfile such that it only unlinks the lockfile if we have successfully locked the lockfile and it is still active (lockfile != NULL). --- diff --git a/bin/named/os.c b/bin/named/os.c index 858ccbff3f9..ee8a094e5bf 100644 --- a/bin/named/os.c +++ b/bin/named/os.c @@ -706,17 +706,19 @@ cleanup_pidfile(void) { } static void -cleanup_lockfile(void) { +cleanup_lockfile(bool unlink_lockfile) { if (singletonfd != -1) { close(singletonfd); singletonfd = -1; } if (lockfile != NULL) { - int n = unlink(lockfile); - if (n == -1 && errno != ENOENT) { - named_main_earlywarning("unlink '%s': failed", - lockfile); + if (unlink_lockfile) { + int n = unlink(lockfile); + if (n == -1 && errno != ENOENT) { + named_main_earlywarning("unlink '%s': failed", + lockfile); + } } free(lockfile); lockfile = NULL; @@ -932,7 +934,7 @@ named_os_issingleton(const char *filename) { if (ret == -1) { named_main_earlywarning("couldn't create '%s'", filename); - cleanup_lockfile(); + cleanup_lockfile(false); return (false); } } @@ -944,7 +946,7 @@ named_os_issingleton(const char *filename) { singletonfd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (singletonfd == -1) { - cleanup_lockfile(); + cleanup_lockfile(false); return (false); } @@ -956,8 +958,7 @@ named_os_issingleton(const char *filename) { /* Non-blocking (does not wait for lock) */ if (fcntl(singletonfd, F_SETLK, &lock) == -1) { - close(singletonfd); - singletonfd = -1; + cleanup_lockfile(false); return (false); } @@ -968,7 +969,7 @@ void named_os_shutdown(void) { closelog(); cleanup_pidfile(); - cleanup_lockfile(); + cleanup_lockfile(true); } void