2013-06-24 10:54:49 -07:00
|
|
|
|
/* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
|
2009-10-14 16:52:04 -07:00
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "lockfile.h"
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include "coverage.h"
|
|
|
|
|
#include "hash.h"
|
|
|
|
|
#include "hmap.h"
|
2013-04-15 15:05:19 -07:00
|
|
|
|
#include "ovs-thread.h"
|
2009-10-14 16:52:04 -07:00
|
|
|
|
#include "timeval.h"
|
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "vlog.h"
|
|
|
|
|
|
2010-10-19 14:47:01 -07:00
|
|
|
|
VLOG_DEFINE_THIS_MODULE(lockfile);
|
2010-07-16 11:02:49 -07:00
|
|
|
|
|
coverage: Make the coverage counters catalog program-specific.
Until now, the collection of coverage counters supported by a given OVS
program was not specific to that program. That means that, for example,
even though ovs-dpctl does not have anything to do with mac_learning, it
still has a coverage counter for it. This is confusing, at best.
This commit fixes the problem on some systems, in particular on ones that
use GCC and the GNU linker. It uses the feature of the GNU linker
described in its manual as:
If an orphaned section's name is representable as a C identifier then
the linker will automatically see PROVIDE two symbols: __start_SECNAME
and __end_SECNAME, where SECNAME is the name of the section. These
indicate the start address and end address of the orphaned section
respectively.
Systems that don't support these features retain the earlier behavior.
This commit also fixes the annoyance that files that include coverage
counters must be listed on COVERAGE_FILES in lib/automake.mk.
This commit also fixes the annoyance that modifying any source file that
includes a coverage counter caused all programs that link against
libopenvswitch.a to relink, even programs that the source file was not
linked into. For example, modifying ofproto/ofproto.c (which includes
coverage counters) caused tests/test-aes128 to relink, even though
test-aes128 does not link again ofproto.o.
2010-11-01 14:14:27 -07:00
|
|
|
|
COVERAGE_DEFINE(lockfile_lock);
|
|
|
|
|
COVERAGE_DEFINE(lockfile_error);
|
|
|
|
|
COVERAGE_DEFINE(lockfile_unlock);
|
|
|
|
|
|
2009-10-14 16:52:04 -07:00
|
|
|
|
struct lockfile {
|
|
|
|
|
struct hmap_node hmap_node;
|
|
|
|
|
char *name;
|
|
|
|
|
dev_t device;
|
|
|
|
|
ino_t inode;
|
|
|
|
|
int fd;
|
2014-02-13 10:50:00 -08:00
|
|
|
|
HANDLE lock_handle;
|
2009-10-14 16:52:04 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Lock table.
|
|
|
|
|
*
|
|
|
|
|
* We have to do this stupid dance because POSIX says that closing *any* file
|
|
|
|
|
* descriptor for a file on which a process holds a lock drops *all* locks on
|
|
|
|
|
* that file. That means that we can't afford to open a lockfile more than
|
|
|
|
|
* once. */
|
2013-07-30 15:31:48 -07:00
|
|
|
|
static struct ovs_mutex lock_table_mutex = OVS_MUTEX_INITIALIZER;
|
|
|
|
|
static struct hmap lock_table__ = HMAP_INITIALIZER(&lock_table__);
|
|
|
|
|
static struct hmap *const lock_table OVS_GUARDED_BY(lock_table_mutex)
|
|
|
|
|
= &lock_table__;
|
2013-04-15 15:05:19 -07:00
|
|
|
|
|
2009-10-14 16:52:04 -07:00
|
|
|
|
static void lockfile_unhash(struct lockfile *);
|
2014-02-13 10:50:00 -08:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
static int lockfile_try_lock_windows(const char *name, pid_t *pidp,
|
|
|
|
|
struct lockfile **lockfilep);
|
|
|
|
|
static void lockfile_unlock_windows(struct lockfile * lockfile);
|
|
|
|
|
#else
|
|
|
|
|
static int lockfile_try_lock_posix(const char *name, pid_t *pidp,
|
|
|
|
|
struct lockfile **lockfilep);
|
|
|
|
|
#endif
|
2009-10-14 16:52:04 -07:00
|
|
|
|
|
|
|
|
|
/* Returns the name of the lockfile that would be created for locking a file
|
2012-07-30 14:41:13 -07:00
|
|
|
|
* named 'filename_'. The caller is responsible for freeing the returned name,
|
|
|
|
|
* with free(), when it is no longer needed. */
|
2009-10-14 16:52:04 -07:00
|
|
|
|
char *
|
2012-07-30 14:41:13 -07:00
|
|
|
|
lockfile_name(const char *filename_)
|
2009-10-14 16:52:04 -07:00
|
|
|
|
{
|
2012-07-30 14:41:13 -07:00
|
|
|
|
char *filename;
|
|
|
|
|
const char *slash;
|
|
|
|
|
char *lockname;
|
|
|
|
|
|
|
|
|
|
/* If 'filename_' is a symlink, base the name of the lockfile on the
|
|
|
|
|
* symlink's target rather than the name of the symlink. That way, if a
|
|
|
|
|
* file is symlinked, but there is no symlink for its lockfile, then there
|
|
|
|
|
* is only a single lockfile for both the source and the target of the
|
|
|
|
|
* symlink, not one for each. */
|
|
|
|
|
filename = follow_symlinks(filename_);
|
|
|
|
|
slash = strrchr(filename, '/');
|
|
|
|
|
lockname = (slash
|
|
|
|
|
? xasprintf("%.*s/.%s.~lock~",
|
|
|
|
|
(int) (slash - filename), filename, slash + 1)
|
|
|
|
|
: xasprintf(".%s.~lock~", filename));
|
|
|
|
|
free(filename);
|
|
|
|
|
|
|
|
|
|
return lockname;
|
2009-10-14 16:52:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Locks the configuration file against modification by other processes and
|
|
|
|
|
* re-reads it from disk.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
2012-08-08 17:40:43 -07:00
|
|
|
|
* NULL. Will not block if the lock cannot be immediately acquired. */
|
2009-10-14 16:52:04 -07:00
|
|
|
|
int
|
2012-08-08 17:40:43 -07:00
|
|
|
|
lockfile_lock(const char *file, struct lockfile **lockfilep)
|
2009-10-14 16:52:04 -07:00
|
|
|
|
{
|
|
|
|
|
/* 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. */
|
|
|
|
|
char *lock_name;
|
2012-08-31 10:11:15 -07:00
|
|
|
|
pid_t pid;
|
2009-10-14 16:52:04 -07:00
|
|
|
|
int error;
|
|
|
|
|
|
|
|
|
|
COVERAGE_INC(lockfile_lock);
|
|
|
|
|
|
|
|
|
|
lock_name = lockfile_name(file);
|
2012-08-08 17:40:43 -07:00
|
|
|
|
|
2013-07-30 15:31:48 -07:00
|
|
|
|
ovs_mutex_lock(&lock_table_mutex);
|
2014-02-13 10:50:00 -08:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
error = lockfile_try_lock_windows(lock_name, &pid, lockfilep);
|
|
|
|
|
#else
|
|
|
|
|
error = lockfile_try_lock_posix(lock_name, &pid, lockfilep);
|
|
|
|
|
#endif
|
2013-07-30 15:31:48 -07:00
|
|
|
|
ovs_mutex_unlock(&lock_table_mutex);
|
2012-08-08 17:40:43 -07:00
|
|
|
|
|
|
|
|
|
if (error) {
|
2009-10-14 16:52:04 -07:00
|
|
|
|
COVERAGE_INC(lockfile_error);
|
|
|
|
|
if (error == EACCES) {
|
|
|
|
|
error = EAGAIN;
|
|
|
|
|
}
|
2012-08-31 10:11:15 -07:00
|
|
|
|
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",
|
2013-06-24 10:54:49 -07:00
|
|
|
|
lock_name, ovs_strerror(error));
|
2012-08-31 10:11:15 -07:00
|
|
|
|
}
|
2009-10-14 16:52:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(lock_name);
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Unlocks 'lockfile', which must have been created by a call to
|
|
|
|
|
* lockfile_lock(), and frees 'lockfile'. */
|
|
|
|
|
void
|
|
|
|
|
lockfile_unlock(struct lockfile *lockfile)
|
|
|
|
|
{
|
|
|
|
|
if (lockfile) {
|
2013-07-30 15:31:48 -07:00
|
|
|
|
ovs_mutex_lock(&lock_table_mutex);
|
2014-02-13 10:50:00 -08:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
lockfile_unlock_windows(lockfile);
|
|
|
|
|
#else
|
2009-10-14 16:52:04 -07:00
|
|
|
|
lockfile_unhash(lockfile);
|
2014-02-13 10:50:00 -08:00
|
|
|
|
#endif
|
2013-07-30 15:31:48 -07:00
|
|
|
|
ovs_mutex_unlock(&lock_table_mutex);
|
2013-04-15 15:05:19 -07:00
|
|
|
|
|
|
|
|
|
COVERAGE_INC(lockfile_unlock);
|
2009-10-14 16:52:04 -07:00
|
|
|
|
free(lockfile->name);
|
|
|
|
|
free(lockfile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Marks all the currently locked lockfiles as no longer locked. It makes
|
|
|
|
|
* sense to call this function after fork(), because a child created by fork()
|
|
|
|
|
* does not hold its parents' locks. */
|
|
|
|
|
void
|
|
|
|
|
lockfile_postfork(void)
|
|
|
|
|
{
|
|
|
|
|
struct lockfile *lockfile;
|
|
|
|
|
|
2013-07-30 15:31:48 -07:00
|
|
|
|
ovs_mutex_lock(&lock_table_mutex);
|
|
|
|
|
HMAP_FOR_EACH (lockfile, hmap_node, lock_table) {
|
2009-10-14 16:52:04 -07:00
|
|
|
|
if (lockfile->fd >= 0) {
|
|
|
|
|
VLOG_WARN("%s: child does not inherit lock", lockfile->name);
|
|
|
|
|
lockfile_unhash(lockfile);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-30 15:31:48 -07:00
|
|
|
|
ovs_mutex_unlock(&lock_table_mutex);
|
2009-10-14 16:52:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
|
lockfile_hash(dev_t device, ino_t inode)
|
|
|
|
|
{
|
|
|
|
|
return hash_bytes(&device, sizeof device,
|
|
|
|
|
hash_bytes(&inode, sizeof inode, 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct lockfile *
|
2013-07-30 15:31:48 -07:00
|
|
|
|
lockfile_find(dev_t device, ino_t inode) OVS_REQUIRES(&lock_table_mutex)
|
2009-10-14 16:52:04 -07:00
|
|
|
|
{
|
|
|
|
|
struct lockfile *lockfile;
|
|
|
|
|
|
2010-09-17 10:33:10 -07:00
|
|
|
|
HMAP_FOR_EACH_WITH_HASH (lockfile, hmap_node,
|
2013-07-30 15:31:48 -07:00
|
|
|
|
lockfile_hash(device, inode), lock_table) {
|
2009-10-14 16:52:04 -07:00
|
|
|
|
if (lockfile->device == device && lockfile->inode == inode) {
|
|
|
|
|
return lockfile;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2013-07-30 15:31:48 -07:00
|
|
|
|
lockfile_unhash(struct lockfile *lockfile) OVS_REQUIRES(&lock_table_mutex)
|
2009-10-14 16:52:04 -07:00
|
|
|
|
{
|
|
|
|
|
if (lockfile->fd >= 0) {
|
|
|
|
|
close(lockfile->fd);
|
|
|
|
|
lockfile->fd = -1;
|
2013-07-30 15:31:48 -07:00
|
|
|
|
hmap_remove(lock_table, &lockfile->hmap_node);
|
2009-10-14 16:52:04 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct lockfile *
|
|
|
|
|
lockfile_register(const char *name, dev_t device, ino_t inode, int fd)
|
2013-07-30 15:31:48 -07:00
|
|
|
|
OVS_REQUIRES(&lock_table_mutex)
|
2009-10-14 16:52:04 -07:00
|
|
|
|
{
|
|
|
|
|
struct lockfile *lockfile;
|
|
|
|
|
|
|
|
|
|
lockfile = lockfile_find(device, inode);
|
|
|
|
|
if (lockfile) {
|
|
|
|
|
VLOG_ERR("%s: lock file disappeared and reappeared!", name);
|
|
|
|
|
lockfile_unhash(lockfile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lockfile = xmalloc(sizeof *lockfile);
|
|
|
|
|
lockfile->name = xstrdup(name);
|
|
|
|
|
lockfile->device = device;
|
|
|
|
|
lockfile->inode = inode;
|
|
|
|
|
lockfile->fd = fd;
|
2013-07-30 15:31:48 -07:00
|
|
|
|
hmap_insert(lock_table, &lockfile->hmap_node,
|
2009-10-14 16:52:04 -07:00
|
|
|
|
lockfile_hash(device, inode));
|
|
|
|
|
return lockfile;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 10:50:00 -08:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
static void
|
|
|
|
|
lockfile_unlock_windows(struct lockfile *lockfile)
|
|
|
|
|
OVS_REQUIRES(&lock_table_mutex)
|
|
|
|
|
{
|
|
|
|
|
if (lockfile->fd >= 0) {
|
|
|
|
|
OVERLAPPED overl;
|
|
|
|
|
overl.hEvent = 0;
|
|
|
|
|
overl.Offset = 0;
|
|
|
|
|
overl.OffsetHigh = 0;
|
|
|
|
|
UnlockFileEx(lockfile->lock_handle, 0, 1, 0, &overl);
|
|
|
|
|
|
|
|
|
|
close(lockfile->fd);
|
|
|
|
|
lockfile->fd = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
lockfile_try_lock_windows(const char *name, pid_t *pidp,
|
|
|
|
|
struct lockfile **lockfilep)
|
|
|
|
|
OVS_REQUIRES(&lock_table_mutex)
|
|
|
|
|
{
|
|
|
|
|
HANDLE lock_handle;
|
|
|
|
|
BOOL retval;
|
|
|
|
|
OVERLAPPED overl;
|
|
|
|
|
struct lockfile *lockfile;
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
*pidp = 0;
|
|
|
|
|
|
|
|
|
|
fd = open(name, O_RDWR | O_CREAT, 0600);
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
VLOG_WARN("%s: failed to open lock file: %s",
|
|
|
|
|
name, ovs_strerror(errno));
|
|
|
|
|
return errno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lock_handle = (HANDLE)_get_osfhandle(fd);
|
|
|
|
|
if (lock_handle < 0) {
|
|
|
|
|
VLOG_WARN("%s: failed to get the file handle: %s",
|
|
|
|
|
name, ovs_strerror(errno));
|
|
|
|
|
return errno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Lock the file 'name' for the region that includes just the first
|
|
|
|
|
* byte. */
|
|
|
|
|
overl.hEvent = 0;
|
|
|
|
|
overl.Offset = 0;
|
|
|
|
|
overl.OffsetHigh = 0;
|
|
|
|
|
retval = LockFileEx(lock_handle, LOCKFILE_EXCLUSIVE_LOCK
|
|
|
|
|
| LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &overl);
|
|
|
|
|
if (!retval) {
|
2014-02-14 08:12:32 -08:00
|
|
|
|
VLOG_WARN("Failed to lock file : %s", ovs_lasterror_to_string());
|
2014-02-13 10:50:00 -08:00
|
|
|
|
return EEXIST;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lockfile = xmalloc(sizeof *lockfile);
|
|
|
|
|
lockfile->name = xstrdup(name);
|
|
|
|
|
lockfile->fd = fd;
|
|
|
|
|
lockfile->lock_handle = lock_handle;
|
|
|
|
|
|
|
|
|
|
*lockfilep = lockfile;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2009-10-14 16:52:04 -07:00
|
|
|
|
static int
|
2014-02-13 10:50:00 -08:00
|
|
|
|
lockfile_try_lock_posix(const char *name, pid_t *pidp,
|
|
|
|
|
struct lockfile **lockfilep)
|
2013-07-30 15:31:48 -07:00
|
|
|
|
OVS_REQUIRES(&lock_table_mutex)
|
2009-10-14 16:52:04 -07:00
|
|
|
|
{
|
|
|
|
|
struct flock l;
|
|
|
|
|
struct stat s;
|
|
|
|
|
int error;
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
*lockfilep = NULL;
|
2012-08-31 10:11:15 -07:00
|
|
|
|
*pidp = 0;
|
2009-10-14 16:52:04 -07:00
|
|
|
|
|
2012-07-26 14:36:24 -07:00
|
|
|
|
/* Check whether we've already got a lock on that file. */
|
|
|
|
|
if (!stat(name, &s)) {
|
|
|
|
|
if (lockfile_find(s.st_dev, s.st_ino)) {
|
|
|
|
|
return EDEADLK;
|
2009-10-14 16:52:04 -07:00
|
|
|
|
}
|
2012-07-26 14:36:24 -07:00
|
|
|
|
} else if (errno != ENOENT) {
|
|
|
|
|
VLOG_WARN("%s: failed to stat lock file: %s",
|
2013-06-24 10:54:49 -07:00
|
|
|
|
name, ovs_strerror(errno));
|
2012-07-26 14:36:24 -07:00
|
|
|
|
return errno;
|
|
|
|
|
}
|
2009-10-14 16:52:04 -07:00
|
|
|
|
|
2012-07-26 14:36:24 -07:00
|
|
|
|
/* Open the lock file. */
|
|
|
|
|
fd = open(name, O_RDWR | O_CREAT, 0600);
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
VLOG_WARN("%s: failed to open lock file: %s",
|
2013-06-24 10:54:49 -07:00
|
|
|
|
name, ovs_strerror(errno));
|
2012-07-26 14:36:24 -07:00
|
|
|
|
return errno;
|
2009-10-14 16:52:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get the inode and device number for the lock table. */
|
|
|
|
|
if (fstat(fd, &s)) {
|
2013-06-24 10:54:49 -07:00
|
|
|
|
VLOG_ERR("%s: failed to fstat lock file: %s",
|
|
|
|
|
name, ovs_strerror(errno));
|
2009-10-14 16:52:04 -07:00
|
|
|
|
close(fd);
|
|
|
|
|
return errno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Try to lock the file. */
|
|
|
|
|
memset(&l, 0, sizeof l);
|
|
|
|
|
l.l_type = F_WRLCK;
|
|
|
|
|
l.l_whence = SEEK_SET;
|
|
|
|
|
l.l_start = 0;
|
|
|
|
|
l.l_len = 0;
|
|
|
|
|
|
2012-08-08 17:40:43 -07:00
|
|
|
|
error = fcntl(fd, F_SETLK, &l) == -1 ? errno : 0;
|
2009-10-14 16:52:04 -07:00
|
|
|
|
|
|
|
|
|
if (!error) {
|
|
|
|
|
*lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd);
|
|
|
|
|
} else {
|
2012-08-31 10:11:15 -07:00
|
|
|
|
if (!fcntl(fd, F_GETLK, &l) && l.l_type != F_UNLCK) {
|
|
|
|
|
*pidp = l.l_pid;
|
|
|
|
|
}
|
2009-10-14 16:52:04 -07:00
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
return error;
|
|
|
|
|
}
|
2014-02-13 10:50:00 -08:00
|
|
|
|
#endif
|