From: Petr Špaček Date: Fri, 9 Oct 2020 09:30:56 +0000 (+0200) Subject: lib/cache: do not unlink cache lock file X-Git-Tag: v5.2.0~18^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=97e549bc8c3eb53c632c9799ed7962ee220657ae;p=thirdparty%2Fknot-resolver.git lib/cache: do not unlink cache lock file 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. --- diff --git a/lib/cache/cdb_lmdb.c b/lib/cache/cdb_lmdb.c index f7a4557d3..aef930312 100644 --- a/lib/cache/cdb_lmdb.c +++ b/lib/cache/cdb_lmdb.c @@ -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));