X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Flockfile.c;h=d157bc616c4b5d684c070c909b6f040c2add29af;hb=cfc50ae514f805dcd9c14589f21158185424daf6;hp=50a4e0c0715981794296d829620a55d409cb7e85;hpb=5947bbe6094a68814f8da8045d170ca03c88fe26;p=sliver-openvswitch.git diff --git a/lib/lockfile.c b/lib/lockfile.c index 50a4e0c07..d157bc616 100644 --- a/lib/lockfile.c +++ b/lib/lockfile.c @@ -1,4 +1,4 @@ - /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. + /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,7 +35,6 @@ VLOG_DEFINE_THIS_MODULE(lockfile); COVERAGE_DEFINE(lockfile_lock); -COVERAGE_DEFINE(lockfile_timeout); COVERAGE_DEFINE(lockfile_error); COVERAGE_DEFINE(lockfile_unlock); @@ -45,6 +44,7 @@ struct lockfile { dev_t device; ino_t inode; int fd; + HANDLE lock_handle; }; /* Lock table. @@ -53,14 +53,17 @@ 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); - -/* Protects 'lock_table'. */ -static pthread_mutex_t lock_table_mutex = PTHREAD_MUTEX_INITIALIZER; +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); + struct lockfile **lockfilep) + OVS_REQUIRES(&lock_table_mutex); +static void lockfile_do_unlock(struct lockfile * lockfile) + OVS_REQUIRES(&lock_table_mutex); /* 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, @@ -110,16 +113,19 @@ lockfile_lock(const char *file, struct lockfile **lockfilep) lock_name = lockfile_name(file); - xpthread_mutex_lock(&lock_table_mutex); + ovs_mutex_lock(&lock_table_mutex); error = lockfile_try_lock(lock_name, &pid, lockfilep); - xpthread_mutex_unlock(&lock_table_mutex); + ovs_mutex_unlock(&lock_table_mutex); if (error) { COVERAGE_INC(lockfile_error); if (error == EACCES) { error = EAGAIN; } - if (pid) { + if (pid == getpid()) { + VLOG_WARN("%s: cannot lock file because this process has already " + "locked it", lock_name); + } else if (pid) { VLOG_WARN("%s: cannot lock file because it is already locked by " "pid %ld", lock_name, (long int) pid); } else { @@ -138,9 +144,9 @@ void lockfile_unlock(struct lockfile *lockfile) { if (lockfile) { - xpthread_mutex_lock(&lock_table_mutex); - lockfile_unhash(lockfile); - xpthread_mutex_unlock(&lock_table_mutex); + ovs_mutex_lock(&lock_table_mutex); + lockfile_do_unlock(lockfile); + ovs_mutex_unlock(&lock_table_mutex); COVERAGE_INC(lockfile_unlock); free(lockfile->name); @@ -156,12 +162,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 @@ -172,12 +180,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; } @@ -186,17 +194,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; @@ -211,13 +220,84 @@ 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_do_unlock(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) + 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; +} +#else /* !_WIN32 */ +static void +lockfile_do_unlock(struct lockfile *lockfile) +{ + lockfile_unhash(lockfile); +} + static int lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep) + OVS_REQUIRES(&lock_table_mutex) { struct flock l; struct stat s; @@ -230,6 +310,7 @@ lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep) /* Check whether we've already got a lock on that file. */ if (!stat(name, &s)) { if (lockfile_find(s.st_dev, s.st_ino)) { + *pidp = getpid(); return EDEADLK; } } else if (errno != ENOENT) { @@ -273,4 +354,4 @@ lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep) } return error; } - +#endif