]> 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 03:55:31 +0000 (14:55 +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).

bin/named/os.c

index 858ccbff3f995b3f34d52f6134264ff34b27a7fe..ee8a094e5bfac73836b15e5723f4d14be5e0d167 100644 (file)
@@ -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