X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Flockfile.c;h=779b74eb390fdc0b8690885bc2acd65c34f1bdcd;hb=8f6736d32683434b24632496c58ce196f2418d09;hp=14e553d291c2ba844f92caa65b1f5b2fc68a33a5;hpb=10a89ef04df5669c5cdd02f786150a7ab8454e01;p=sliver-openvswitch.git diff --git a/lib/lockfile.c b/lib/lockfile.c index 14e553d29..779b74eb3 100644 --- a/lib/lockfile.c +++ b/lib/lockfile.c @@ -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" @@ -34,7 +35,6 @@ VLOG_DEFINE_THIS_MODULE(lockfile); COVERAGE_DEFINE(lockfile_lock); -COVERAGE_DEFINE(lockfile_timeout); COVERAGE_DEFINE(lockfile_error); COVERAGE_DEFINE(lockfile_unlock); @@ -44,6 +44,7 @@ struct lockfile { dev_t device; ino_t inode; int fd; + HANDLE lock_handle; }; /* Lock table. @@ -52,11 +53,20 @@ struct lockfile { * 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. */ -static struct hmap lock_table = HMAP_INITIALIZER(&lock_table); +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__; static void lockfile_unhash(struct lockfile *); -static int lockfile_try_lock(const char *name, pid_t *pidp, - struct lockfile **lockfilep); +#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 /* 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, @@ -106,7 +116,13 @@ lockfile_lock(const char *file, struct lockfile **lockfilep) lock_name = lockfile_name(file); - error = lockfile_try_lock(lock_name, &pid, lockfilep); + ovs_mutex_lock(&lock_table_mutex); +#ifdef _WIN32 + error = lockfile_try_lock_windows(lock_name, &pid, lockfilep); +#else + error = lockfile_try_lock_posix(lock_name, &pid, lockfilep); +#endif + ovs_mutex_unlock(&lock_table_mutex); if (error) { COVERAGE_INC(lockfile_error); @@ -132,8 +148,15 @@ void lockfile_unlock(struct lockfile *lockfile) { if (lockfile) { - COVERAGE_INC(lockfile_unlock); + ovs_mutex_lock(&lock_table_mutex); +#ifdef _WIN32 + lockfile_unlock_windows(lockfile); +#else lockfile_unhash(lockfile); +#endif + ovs_mutex_unlock(&lock_table_mutex); + + COVERAGE_INC(lockfile_unlock); free(lockfile->name); free(lockfile); } @@ -147,12 +170,14 @@ lockfile_postfork(void) { struct lockfile *lockfile; - HMAP_FOR_EACH (lockfile, hmap_node, &lock_table) { + ovs_mutex_lock(&lock_table_mutex); + HMAP_FOR_EACH (lockfile, hmap_node, lock_table) { if (lockfile->fd >= 0) { VLOG_WARN("%s: child does not inherit lock", lockfile->name); lockfile_unhash(lockfile); } } + ovs_mutex_unlock(&lock_table_mutex); } static uint32_t @@ -163,12 +188,12 @@ lockfile_hash(dev_t device, ino_t inode) } static struct lockfile * -lockfile_find(dev_t device, ino_t inode) +lockfile_find(dev_t device, ino_t inode) OVS_REQUIRES(&lock_table_mutex) { struct lockfile *lockfile; HMAP_FOR_EACH_WITH_HASH (lockfile, hmap_node, - lockfile_hash(device, inode), &lock_table) { + lockfile_hash(device, inode), lock_table) { if (lockfile->device == device && lockfile->inode == inode) { return lockfile; } @@ -177,17 +202,18 @@ lockfile_find(dev_t device, ino_t inode) } static void -lockfile_unhash(struct lockfile *lockfile) +lockfile_unhash(struct lockfile *lockfile) OVS_REQUIRES(&lock_table_mutex) { if (lockfile->fd >= 0) { close(lockfile->fd); lockfile->fd = -1; - hmap_remove(&lock_table, &lockfile->hmap_node); + hmap_remove(lock_table, &lockfile->hmap_node); } } static struct lockfile * lockfile_register(const char *name, dev_t device, ino_t inode, int fd) + OVS_REQUIRES(&lock_table_mutex) { struct lockfile *lockfile; @@ -202,13 +228,82 @@ lockfile_register(const char *name, dev_t device, ino_t inode, int fd) lockfile->device = device; lockfile->inode = inode; lockfile->fd = fd; - hmap_insert(&lock_table, &lockfile->hmap_node, + hmap_insert(lock_table, &lockfile->hmap_node, lockfile_hash(device, inode)); return lockfile; } +#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(const char *name, pid_t *pidp, struct lockfile **lockfilep) +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) { + VLOG_WARN("Failed to lock file : %s", ovs_lasterror_to_string()); + 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 +static int +lockfile_try_lock_posix(const char *name, pid_t *pidp, + struct lockfile **lockfilep) + OVS_REQUIRES(&lock_table_mutex) { struct flock l; struct stat s; @@ -264,4 +359,4 @@ lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep) } return error; } - +#endif