]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Only remove the lock file if we managed to lock it
authorMark Andrews <marka@isc.org>
Thu, 26 Oct 2023 03:50:43 +0000 (14:50 +1100)
committerMark Andrews <marka@isc.org>
Thu, 26 Oct 2023 07:05:07 +0000 (18:05 +1100)
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).

(cherry picked from commit b9c789b8b34a45a1572590a9ef4c8aa1306b4c63)

bin/named/os.c

index 7af47294ba5ae6b0b6e56557a0ea9be1afa494c2..0222abb7c2c68075da94982880f327b17ce01bc7 100644 (file)
@@ -552,17 +552,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;
@@ -830,7 +832,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);
                }
        }
@@ -842,7 +844,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);
        }
 
@@ -854,8 +856,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);
        }
 
@@ -866,7 +867,7 @@ void
 named_os_shutdown(void) {
        closelog();
        cleanup_pidfile();
-       cleanup_lockfile();
+       cleanup_lockfile(true);
 }
 
 void