From 852228dea5186bd08edb4e9e21d53b9f0142324d Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 1 Aug 2010 15:55:49 +0200 Subject: [PATCH] Implement file locking primitives Windows support is not yet done. --- Makefile.in | 2 +- ccache.h | 6 ++ lockfile.c | 139 +++++++++++++++++++++++++++++++++++++++++++ test/test_lockfile.c | 66 ++++++++++++++++++++ 4 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 lockfile.c create mode 100644 test/test_lockfile.c diff --git a/Makefile.in b/Makefile.in index 5452b6d9d..891c4c306 100644 --- a/Makefile.in +++ b/Makefile.in @@ -21,7 +21,7 @@ libs = @LIBS@ -lm -lz base_sources = \ ccache.c mdfour.c hash.c execute.c util.c args.c stats.c version.c \ cleanup.c snprintf.c unify.c manifest.c hashtable.c hashtable_itr.c \ - murmurhashneutral2.c hashutil.c getopt_long.c exitfn.c + murmurhashneutral2.c hashutil.c getopt_long.c exitfn.c lockfile.c base_objs = $(base_sources:.c=.o) ccache_sources = main.c $(base_sources) diff --git a/ccache.h b/ccache.h index 3a650a740..f4413fccc 100644 --- a/ccache.h +++ b/ccache.h @@ -205,6 +205,12 @@ void args_remove_first(struct args *args); char *args_to_string(struct args *args); int args_equal(struct args *args1, struct args *args2); +/* ------------------------------------------------------------------------- */ +/* lockfile.c */ + +int lockfile_acquire(const char *path, unsigned staleness_limit); +void lockfile_release(const char *path); + /* ------------------------------------------------------------------------- */ /* ccache.c */ diff --git a/lockfile.c b/lockfile.c new file mode 100644 index 000000000..f852b813d --- /dev/null +++ b/lockfile.c @@ -0,0 +1,139 @@ +#include "ccache.h" +#include +#include +#include +#include + +/* + * This function acquires a lockfile for the given path. Returns true if the + * lock was acquired, otherwise false. If the lock has been considered stale + * for the number of microseconds specified by staleness_limit, the function + * will (if possible) break the lock and then try to acquire it again. The + * staleness limit should be reasonably larger than the longest time the lock + * can be expected to be held, and the updates of the locked path should + * probably be made with an atomic rename(2) to avoid corruption in the rare + * case that the lock is broken by another process. + */ +int +lockfile_acquire(const char *path, unsigned staleness_limit) +{ + char *lockfile = format("%s.lock", path); + char *my_content = NULL, *content = NULL, *initial_content = NULL; + const char *hostname = get_hostname(); + int result = 0; + int ret; + unsigned to_sleep = 1000, slept = 0; /* Microseconds. */ + + while (1) { + free(my_content); + my_content = format("%s:%d:%d", hostname, (int)getpid(), (int)time(NULL)); + + ret = symlink(my_content, lockfile); + if (ret == 0) { + /* We got the lock. */ + result = 1; + goto out; + } + cc_log("lockfile_acquire: symlink %s: %s", lockfile, strerror(errno)); + if (errno != EEXIST) { + /* Directory doesn't exist or isn't writable? */ + goto out; + } + free(content); + content = x_readlink(lockfile); + if (!content) { + if (errno == ENOENT) { + /* + * The symlink was removed after the symlink() call above, so retry + * acquiring it. + */ + continue; + } else { + cc_log("lockfile_acquire: readlink %s: %s", lockfile, strerror(errno)); + goto out; + } + } + + if (str_eq(content, my_content)) { + /* Lost NFS reply? */ + cc_log("lockfile_acquire: symlink %s failed but we got the lock anyway", + lockfile); + result = 1; + goto out; + } + /* + * A possible improvement here would be to check if the process holding the + * lock is still alive and break the lock early if it isn't. + */ + cc_log("lockfile_acquire: lock info for %s: %s", lockfile, content); + if (!initial_content) { + initial_content = x_strdup(content); + } + if (slept > staleness_limit) { + if (str_eq(content, initial_content)) { + /* The lock seems to be stale -- break it. */ + cc_log("lockfile_acquire: breaking %s", lockfile); + if (lockfile_acquire(lockfile, staleness_limit)) { + lockfile_release(path); + lockfile_release(lockfile); + to_sleep = 1000; + slept = 0; + continue; + } + } + cc_log("lockfile_acquire: gave up acquiring %s", lockfile); + goto out; + } + cc_log("lockfile_acquire: failed to acquire %s; sleeping %u microseconds", + lockfile, to_sleep); + usleep(to_sleep); + slept += to_sleep; + to_sleep *= 2; + } + +out: + if (result == 1) { + cc_log("Acquired lock %s", lockfile); + } else { + cc_log("Failed to acquire lock %s", lockfile); + } + free(lockfile); + free(my_content); + free(initial_content); + free(content); + return result; +} + +/* + * Release the lockfile for the given path. Assumes that we are the legitimate + * owner. + */ +void +lockfile_release(const char *path) +{ + char *lockfile = format("%s.lock", path); + cc_log("Releasing lock %s", lockfile); + unlink(lockfile); + free(lockfile); +} + +#ifdef TEST_LOCKFILE +int +main(int argc, char **argv) +{ + extern char *cache_logfile; + cache_logfile = "/dev/stdout"; + if (argc == 4) { + unsigned staleness_limit = atoi(argv[1]); + if (str_eq(argv[2], "acquire")) { + return lockfile_acquire(argv[3], staleness_limit) == 0; + } else if (str_eq(argv[2], "release")) { + lockfile_release(argv[3]); + return 0; + } + } + fprintf(stderr, + "Usage: testlockfile \n"); + return 1; +} +#endif diff --git a/test/test_lockfile.c b/test/test_lockfile.c new file mode 100644 index 000000000..2c50856ef --- /dev/null +++ b/test/test_lockfile.c @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2010 Joel Rosdahl + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + * This file contains tests for functions in lockfile.c. + */ + +#include "ccache.h" +#include "test/framework.h" +#include "test/util.h" + +TEST_SUITE(lockfile) + +TEST(acquire_should_create_symlink) +{ + lockfile_acquire("test", 1000); + + CHECK(is_symlink("test.lock")); +} + +TEST(release_should_delete_file) +{ + create_file("test.lock", ""); + lockfile_release("test"); + + CHECK(!path_exists("test.lock")); +} + +TEST(lock_breaking) +{ + char *p; + + CHECK_INT_EQ(0, symlink("foo", "test.lock")); + CHECK_INT_EQ(0, symlink("foo", "test.lock.lock")); + CHECK(lockfile_acquire("test", 1000)); + + p = x_readlink("test.lock"); + CHECK(p); + CHECK(!str_eq(p, "foo")); + CHECK(!path_exists("test.lock.lock")); + + free(p); +} + +TEST(failed_lock_breaking) +{ + create_file("test.lock", ""); + CHECK(!lockfile_acquire("test", 1000)); +} + +TEST_SUITE_END -- 2.47.3