static struct hmap lock_table = HMAP_INITIALIZER(&lock_table);
static void lockfile_unhash(struct lockfile *);
-static int lockfile_try_lock(const char *name, bool block,
- struct lockfile **lockfilep);
+static int lockfile_try_lock(const char *name, struct lockfile **lockfilep);
/* Returns the name of the lockfile that would be created for locking a file
* named 'filename_'. The caller is responsible for freeing the returned name,
/* Locks the configuration file against modification by other processes and
* re-reads it from disk.
*
- * The 'timeout' specifies the maximum number of milliseconds to wait for the
- * config file to become free. Use 0 to avoid waiting or INT_MAX to wait
- * forever.
- *
* Returns 0 on success, otherwise a positive errno value. On success,
* '*lockfilep' is set to point to a new "struct lockfile *" that may be
* unlocked with lockfile_unlock(). On failure, '*lockfilep' is set to
- * NULL. */
+ * NULL. Will not block if the lock cannot be immediately acquired. */
int
-lockfile_lock(const char *file, int timeout, struct lockfile **lockfilep)
+lockfile_lock(const char *file, struct lockfile **lockfilep)
{
/* Only exclusive ("write") locks are supported. This is not a problem
* because the Open vSwitch code that currently uses lock files does so in
* stylized ways such that any number of readers may access a file while it
* is being written. */
- long long int warn_elapsed = 1000;
- long long int start, elapsed;
char *lock_name;
int error;
COVERAGE_INC(lockfile_lock);
lock_name = lockfile_name(file);
- time_refresh();
- start = time_msec();
-
- do {
- error = lockfile_try_lock(lock_name, timeout > 0, lockfilep);
- time_refresh();
- elapsed = time_msec() - start;
- if (elapsed > warn_elapsed) {
- warn_elapsed *= 2;
- VLOG_WARN("%s: waiting for lock file, %lld ms elapsed",
- lock_name, elapsed);
- }
- } while (error == EINTR && (timeout == INT_MAX || elapsed < timeout));
-
- if (error == EINTR) {
- COVERAGE_INC(lockfile_timeout);
- VLOG_WARN("%s: giving up on lock file after %lld ms",
- lock_name, elapsed);
- error = ETIMEDOUT;
- } else if (error) {
+
+ error = lockfile_try_lock(lock_name, lockfilep);
+
+ if (error) {
COVERAGE_INC(lockfile_error);
if (error == EACCES) {
error = EAGAIN;
}
- VLOG_WARN("%s: failed to lock file "
- "(after %lld ms, with %d-ms timeout): %s",
- lock_name, elapsed, timeout, strerror(error));
+ VLOG_WARN("%s: failed to lock file: %s",
+ lock_name, strerror(error));
}
free(lock_name);
}
static int
-lockfile_try_lock(const char *name, bool block, struct lockfile **lockfilep)
+lockfile_try_lock(const char *name, struct lockfile **lockfilep)
{
struct flock l;
struct stat s;
l.l_len = 0;
time_disable_restart();
- error = fcntl(fd, block ? F_SETLKW : F_SETLK, &l) == -1 ? errno : 0;
+ error = fcntl(fd, F_SETLK, &l) == -1 ? errno : 0;
time_enable_restart();
if (!error) {
{
struct lockfile *lockfile;
- CHECK(lockfile_lock("file", 0, &lockfile), 0);
+ CHECK(lockfile_lock("file", &lockfile), 0);
lockfile_unlock(lockfile);
}
{
struct lockfile *lockfile;
- CHECK(lockfile_lock("file", 0, &lockfile), 0);
+ CHECK(lockfile_lock("file", &lockfile), 0);
lockfile_unlock(lockfile);
- CHECK(lockfile_lock("file", 0, &lockfile), 0);
+ CHECK(lockfile_lock("file", &lockfile), 0);
lockfile_unlock(lockfile);
}
{
struct lockfile *lockfile;
- CHECK(lockfile_lock("file", 0, &lockfile), 0);
- CHECK(lockfile_lock("file", 0, &lockfile), EDEADLK);
+ CHECK(lockfile_lock("file", &lockfile), 0);
+ CHECK(lockfile_lock("file", &lockfile), EDEADLK);
lockfile_unlock(lockfile);
}
{
struct lockfile *lockfile;
- CHECK(lockfile_lock("file", 0, &lockfile), 0);
- CHECK(lockfile_lock("file", 0, &lockfile), EDEADLK);
- CHECK(lockfile_lock("file", 0, &lockfile), EDEADLK);
+ CHECK(lockfile_lock("file", &lockfile), 0);
+ CHECK(lockfile_lock("file", &lockfile), EDEADLK);
+ CHECK(lockfile_lock("file", &lockfile), EDEADLK);
lockfile_unlock(lockfile);
}
* this function that does the wait() call. */
static struct lockfile *lockfile;
- CHECK(lockfile_lock("file", 0, &lockfile), 0);
+ CHECK(lockfile_lock("file", &lockfile), 0);
if (do_fork() == CHILD) {
lockfile_unlock(lockfile);
- CHECK(lockfile_lock("file", 0, &lockfile), EAGAIN);
+ CHECK(lockfile_lock("file", &lockfile), EAGAIN);
exit(11);
}
}
{
struct lockfile *lockfile, *dummy;
- CHECK(lockfile_lock("file", 0, &lockfile), 0);
- CHECK(lockfile_lock("file", 0, &dummy), EDEADLK);
+ CHECK(lockfile_lock("file", &lockfile), 0);
+ CHECK(lockfile_lock("file", &dummy), EDEADLK);
if (do_fork() == CHILD) {
- CHECK(lockfile_lock("file", 0, &dummy), EAGAIN);
+ CHECK(lockfile_lock("file", &dummy), EAGAIN);
exit(11);
}
}
{
struct lockfile *lockfile;
- CHECK(lockfile_lock("file", 0, &lockfile), 0);
+ CHECK(lockfile_lock("file", &lockfile), 0);
lockfile_unlock(lockfile);
if (do_fork() == CHILD) {
- CHECK(lockfile_lock("file", 0, &lockfile), 0);
+ CHECK(lockfile_lock("file", &lockfile), 0);
exit(11);
}
}
-static void
-run_lock_timeout_gets_the_lock(void)
-{
- struct lockfile *lockfile;
-
- CHECK(lockfile_lock("file", 0, &lockfile), 0);
-
- if (do_fork() == CHILD) {
- lockfile_unlock(lockfile);
- CHECK(lockfile_lock("file", TIME_UPDATE_INTERVAL * 3, &lockfile), 0);
- exit(11);
- } else {
- long long int now = time_msec();
- while (time_msec() < now + TIME_UPDATE_INTERVAL) {
- pause();
- }
- lockfile_unlock(lockfile);
- }
-}
-
-static void
-run_lock_timeout_runs_out(void)
-{
- struct lockfile *lockfile;
-
- CHECK(lockfile_lock("file", 0, &lockfile), 0);
-
- if (do_fork() == CHILD) {
- lockfile_unlock(lockfile);
- CHECK(lockfile_lock("file", TIME_UPDATE_INTERVAL, &lockfile),
- ETIMEDOUT);
- exit(11);
- } else {
- long long int now = time_msec();
- while (time_msec() < now + TIME_UPDATE_INTERVAL * 3) {
- pause();
- }
- lockfile_unlock(lockfile);
- }
-}
-
static void
run_lock_multiple(void)
{
struct lockfile *a, *b, *c, *dummy;
- CHECK(lockfile_lock("a", 0, &a), 0);
- CHECK(lockfile_lock("b", 0, &b), 0);
- CHECK(lockfile_lock("c", 0, &c), 0);
+ CHECK(lockfile_lock("a", &a), 0);
+ CHECK(lockfile_lock("b", &b), 0);
+ CHECK(lockfile_lock("c", &c), 0);
lockfile_unlock(a);
- CHECK(lockfile_lock("a", 0, &a), 0);
- CHECK(lockfile_lock("a", 0, &dummy), EDEADLK);
+ CHECK(lockfile_lock("a", &a), 0);
+ CHECK(lockfile_lock("a", &dummy), EDEADLK);
lockfile_unlock(a);
lockfile_unlock(b);
- CHECK(lockfile_lock("a", 0, &a), 0);
+ CHECK(lockfile_lock("a", &a), 0);
lockfile_unlock(c);
lockfile_unlock(a);
CHECK(stat(".b.~lock~", &s), -1);
CHECK(errno, ENOENT);
- CHECK(lockfile_lock("a", 0, &a), 0);
- CHECK(lockfile_lock("a", 0, &dummy), EDEADLK);
- CHECK(lockfile_lock("b", 0, &dummy), EDEADLK);
+ CHECK(lockfile_lock("a", &a), 0);
+ CHECK(lockfile_lock("a", &dummy), EDEADLK);
+ CHECK(lockfile_lock("b", &dummy), EDEADLK);
lockfile_unlock(a);
- CHECK(lockfile_lock("b", 0, &b), 0);
- CHECK(lockfile_lock("b", 0, &dummy), EDEADLK);
- CHECK(lockfile_lock("a", 0, &dummy), EDEADLK);
+ CHECK(lockfile_lock("b", &b), 0);
+ CHECK(lockfile_lock("b", &dummy), EDEADLK);
+ CHECK(lockfile_lock("a", &dummy), EDEADLK);
lockfile_unlock(b);
CHECK(lstat(".a.~lock~", &s), 0);
CHECK(S_ISLNK(s.st_mode) != 0, 1);
/* Lock 'a'. */
- CHECK(lockfile_lock("a", 0, &a), 0);
+ CHECK(lockfile_lock("a", &a), 0);
CHECK(lstat("dir/.b.~lock~", &s), 0);
CHECK(S_ISREG(s.st_mode) != 0, 1);
CHECK(lstat(".a.~lock~", &s), -1);
CHECK(errno, ENOENT);
- CHECK(lockfile_lock("dir/b", 0, &dummy), EDEADLK);
+ CHECK(lockfile_lock("dir/b", &dummy), EDEADLK);
lockfile_unlock(a);
}
TEST(lock_blocks_other_process),
TEST(lock_twice_blocks_other_process),
TEST(lock_and_unlock_allows_other_process),
- TEST(lock_timeout_gets_the_lock),
- TEST(lock_timeout_runs_out),
TEST(lock_multiple),
TEST(lock_symlink),
TEST(lock_symlink_to_dir),