X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Flockfile.c;h=50a4e0c0715981794296d829620a55d409cb7e85;hb=cc36576070df622d0fc7a6e26ce01027e12b5b59;hp=7ac34659b36f0637f9fd83d2c2a84c33d30e5197;hpb=b1fdc5fb270c0ca21de4bbe5002aca4dd79e9911;p=sliver-openvswitch.git diff --git a/lib/lockfile.c b/lib/lockfile.c index 7ac34659b..50a4e0c07 100644 --- a/lib/lockfile.c +++ b/lib/lockfile.c @@ -1,4 +1,4 @@ - /* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. + /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ #include "coverage.h" #include "hash.h" #include "hmap.h" +#include "ovs-thread.h" #include "timeval.h" #include "util.h" #include "vlog.h" @@ -54,8 +55,11 @@ struct lockfile { * once. */ static struct hmap lock_table = HMAP_INITIALIZER(&lock_table); +/* Protects 'lock_table'. */ +static pthread_mutex_t lock_table_mutex = PTHREAD_MUTEX_INITIALIZER; + static void lockfile_unhash(struct lockfile *); -static int lockfile_try_lock(const char *name, bool block, +static int lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep); /* Returns the name of the lockfile that would be created for locking a file @@ -87,56 +91,41 @@ lockfile_name(const char *filename_) /* 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; + pid_t pid; 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) { + + xpthread_mutex_lock(&lock_table_mutex); + error = lockfile_try_lock(lock_name, &pid, lockfilep); + xpthread_mutex_unlock(&lock_table_mutex); + + 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)); + if (pid) { + VLOG_WARN("%s: cannot lock file because it is already locked by " + "pid %ld", lock_name, (long int) pid); + } else { + VLOG_WARN("%s: failed to lock file: %s", + lock_name, ovs_strerror(error)); + } } free(lock_name); @@ -149,8 +138,11 @@ void lockfile_unlock(struct lockfile *lockfile) { if (lockfile) { - COVERAGE_INC(lockfile_unlock); + xpthread_mutex_lock(&lock_table_mutex); lockfile_unhash(lockfile); + xpthread_mutex_unlock(&lock_table_mutex); + + COVERAGE_INC(lockfile_unlock); free(lockfile->name); free(lockfile); } @@ -225,7 +217,7 @@ lockfile_register(const char *name, dev_t device, ino_t inode, int fd) } static int -lockfile_try_lock(const char *name, bool block, struct lockfile **lockfilep) +lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep) { struct flock l; struct stat s; @@ -233,6 +225,7 @@ lockfile_try_lock(const char *name, bool block, struct lockfile **lockfilep) int fd; *lockfilep = NULL; + *pidp = 0; /* Check whether we've already got a lock on that file. */ if (!stat(name, &s)) { @@ -241,7 +234,7 @@ lockfile_try_lock(const char *name, bool block, struct lockfile **lockfilep) } } else if (errno != ENOENT) { VLOG_WARN("%s: failed to stat lock file: %s", - name, strerror(errno)); + name, ovs_strerror(errno)); return errno; } @@ -249,13 +242,14 @@ lockfile_try_lock(const char *name, bool block, struct lockfile **lockfilep) fd = open(name, O_RDWR | O_CREAT, 0600); if (fd < 0) { VLOG_WARN("%s: failed to open lock file: %s", - name, strerror(errno)); + name, ovs_strerror(errno)); return errno; } /* Get the inode and device number for the lock table. */ if (fstat(fd, &s)) { - VLOG_ERR("%s: failed to fstat lock file: %s", name, strerror(errno)); + VLOG_ERR("%s: failed to fstat lock file: %s", + name, ovs_strerror(errno)); close(fd); return errno; } @@ -267,13 +261,14 @@ lockfile_try_lock(const char *name, bool block, struct lockfile **lockfilep) l.l_start = 0; l.l_len = 0; - time_disable_restart(); - error = fcntl(fd, block ? F_SETLKW : F_SETLK, &l) == -1 ? errno : 0; - time_enable_restart(); + error = fcntl(fd, F_SETLK, &l) == -1 ? errno : 0; if (!error) { *lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd); } else { + if (!fcntl(fd, F_GETLK, &l) && l.l_type != F_UNLCK) { + *pidp = l.l_pid; + } close(fd); } return error;