]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/cache: do not unlink cache lock file
authorPetr Špaček <petr.spacek@nic.cz>
Fri, 9 Oct 2020 09:30:56 +0000 (11:30 +0200)
committerPetr Špaček <petr.spacek@nic.cz>
Fri, 9 Oct 2020 10:43:17 +0000 (12:43 +0200)
This change serves two purposes:
- removes corner case where lock is deleted and created by another
  instance
- prevents incredibly rare problem where filesystem inodes run out

Beware that lock file is still not created on start-up.
It gets created only when emergency cache clear is executed and stays there.

We might consider creating the file at start-up as well
but that's topic for another MR.

lib/cache/cdb_lmdb.c

index f7a4557d3a09c1f8922447b1b7c11dcfa7992b6c..aef9303125ec33d2d2c0de72fc18c3ce6ffc3560 100644 (file)
@@ -517,17 +517,14 @@ static int lockfile_get(const char *path)
 }
 
 /** Release and remove lockfile created by lockfile_get().  Return kr_error(). */
-static int lockfile_release(const char *path, int fd)
+static int lockfile_release(int fd)
 {
-       assert(path && fd > 0); // fd == 0 is surely a mistake, in our case at least
-       int err = 0;
-       // To avoid a race, we unlink it first.
-       if (unlink(path))
-               err = kr_error(errno);
-       // And we try to close it even on error.
-       if (close(fd) && !err)
-               err = kr_error(errno);
-       return err;
+       assert(fd > 0); // fd == 0 is surely a mistake, in our case at least
+       if (close(fd)) {
+               return kr_error(errno);
+       } else {
+               return kr_ok();
+       }
 }
 
 static int cdb_clear(kr_cdb_pt db, struct kr_cdb_stats *stats)
@@ -595,7 +592,7 @@ static int cdb_clear(kr_cdb_pt db, struct kr_cdb_stats *stats)
        }
 
        /* Environment updated, release lockfile. */
-       int lrerr = lockfile_release(lockfile, lockfile_fd);
+       int lrerr = lockfile_release(lockfile_fd);
        if (lrerr) {
                kr_log_error("[cache] failed to release ./.cachelock: %s\n",
                                kr_strerror(lrerr));