1 /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
30 #include "ovs-thread.h"
35 VLOG_DEFINE_THIS_MODULE(lockfile);
37 COVERAGE_DEFINE(lockfile_lock);
38 COVERAGE_DEFINE(lockfile_error);
39 COVERAGE_DEFINE(lockfile_unlock);
42 struct hmap_node hmap_node;
52 * We have to do this stupid dance because POSIX says that closing *any* file
53 * descriptor for a file on which a process holds a lock drops *all* locks on
54 * that file. That means that we can't afford to open a lockfile more than
56 static struct ovs_mutex lock_table_mutex = OVS_MUTEX_INITIALIZER;
57 static struct hmap lock_table__ = HMAP_INITIALIZER(&lock_table__);
58 static struct hmap *const lock_table OVS_GUARDED_BY(lock_table_mutex)
61 static void lockfile_unhash(struct lockfile *);
62 static int lockfile_try_lock(const char *name, pid_t *pidp,
63 struct lockfile **lockfilep)
64 OVS_REQUIRES(&lock_table_mutex);
65 static void lockfile_do_unlock(struct lockfile * lockfile)
66 OVS_REQUIRES(&lock_table_mutex);
68 /* Returns the name of the lockfile that would be created for locking a file
69 * named 'filename_'. The caller is responsible for freeing the returned name,
70 * with free(), when it is no longer needed. */
72 lockfile_name(const char *filename_)
78 /* If 'filename_' is a symlink, base the name of the lockfile on the
79 * symlink's target rather than the name of the symlink. That way, if a
80 * file is symlinked, but there is no symlink for its lockfile, then there
81 * is only a single lockfile for both the source and the target of the
82 * symlink, not one for each. */
83 filename = follow_symlinks(filename_);
84 slash = strrchr(filename, '/');
86 ? xasprintf("%.*s/.%s.~lock~",
87 (int) (slash - filename), filename, slash + 1)
88 : xasprintf(".%s.~lock~", filename));
94 /* Locks the configuration file against modification by other processes and
95 * re-reads it from disk.
97 * Returns 0 on success, otherwise a positive errno value. On success,
98 * '*lockfilep' is set to point to a new "struct lockfile *" that may be
99 * unlocked with lockfile_unlock(). On failure, '*lockfilep' is set to
100 * NULL. Will not block if the lock cannot be immediately acquired. */
102 lockfile_lock(const char *file, struct lockfile **lockfilep)
104 /* Only exclusive ("write") locks are supported. This is not a problem
105 * because the Open vSwitch code that currently uses lock files does so in
106 * stylized ways such that any number of readers may access a file while it
107 * is being written. */
112 COVERAGE_INC(lockfile_lock);
114 lock_name = lockfile_name(file);
116 ovs_mutex_lock(&lock_table_mutex);
117 error = lockfile_try_lock(lock_name, &pid, lockfilep);
118 ovs_mutex_unlock(&lock_table_mutex);
121 COVERAGE_INC(lockfile_error);
122 if (error == EACCES) {
126 VLOG_WARN("%s: cannot lock file because it is already locked by "
127 "pid %ld", lock_name, (long int) pid);
129 VLOG_WARN("%s: failed to lock file: %s",
130 lock_name, ovs_strerror(error));
138 /* Unlocks 'lockfile', which must have been created by a call to
139 * lockfile_lock(), and frees 'lockfile'. */
141 lockfile_unlock(struct lockfile *lockfile)
144 ovs_mutex_lock(&lock_table_mutex);
145 lockfile_do_unlock(lockfile);
146 ovs_mutex_unlock(&lock_table_mutex);
148 COVERAGE_INC(lockfile_unlock);
149 free(lockfile->name);
154 /* Marks all the currently locked lockfiles as no longer locked. It makes
155 * sense to call this function after fork(), because a child created by fork()
156 * does not hold its parents' locks. */
158 lockfile_postfork(void)
160 struct lockfile *lockfile;
162 ovs_mutex_lock(&lock_table_mutex);
163 HMAP_FOR_EACH (lockfile, hmap_node, lock_table) {
164 if (lockfile->fd >= 0) {
165 VLOG_WARN("%s: child does not inherit lock", lockfile->name);
166 lockfile_unhash(lockfile);
169 ovs_mutex_unlock(&lock_table_mutex);
173 lockfile_hash(dev_t device, ino_t inode)
175 return hash_bytes(&device, sizeof device,
176 hash_bytes(&inode, sizeof inode, 0));
179 static struct lockfile *
180 lockfile_find(dev_t device, ino_t inode) OVS_REQUIRES(&lock_table_mutex)
182 struct lockfile *lockfile;
184 HMAP_FOR_EACH_WITH_HASH (lockfile, hmap_node,
185 lockfile_hash(device, inode), lock_table) {
186 if (lockfile->device == device && lockfile->inode == inode) {
194 lockfile_unhash(struct lockfile *lockfile) OVS_REQUIRES(&lock_table_mutex)
196 if (lockfile->fd >= 0) {
199 hmap_remove(lock_table, &lockfile->hmap_node);
203 static struct lockfile *
204 lockfile_register(const char *name, dev_t device, ino_t inode, int fd)
205 OVS_REQUIRES(&lock_table_mutex)
207 struct lockfile *lockfile;
209 lockfile = lockfile_find(device, inode);
211 VLOG_ERR("%s: lock file disappeared and reappeared!", name);
212 lockfile_unhash(lockfile);
215 lockfile = xmalloc(sizeof *lockfile);
216 lockfile->name = xstrdup(name);
217 lockfile->device = device;
218 lockfile->inode = inode;
220 hmap_insert(lock_table, &lockfile->hmap_node,
221 lockfile_hash(device, inode));
227 lockfile_do_unlock(struct lockfile *lockfile)
228 OVS_REQUIRES(&lock_table_mutex)
230 if (lockfile->fd >= 0) {
234 overl.OffsetHigh = 0;
235 UnlockFileEx(lockfile->lock_handle, 0, 1, 0, &overl);
243 lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep)
244 OVS_REQUIRES(&lock_table_mutex)
249 struct lockfile *lockfile;
254 fd = open(name, O_RDWR | O_CREAT, 0600);
256 VLOG_WARN("%s: failed to open lock file: %s",
257 name, ovs_strerror(errno));
261 lock_handle = (HANDLE)_get_osfhandle(fd);
262 if (lock_handle < 0) {
263 VLOG_WARN("%s: failed to get the file handle: %s",
264 name, ovs_strerror(errno));
268 /* Lock the file 'name' for the region that includes just the first
272 overl.OffsetHigh = 0;
273 retval = LockFileEx(lock_handle, LOCKFILE_EXCLUSIVE_LOCK
274 | LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &overl);
276 VLOG_WARN("Failed to lock file : %s", ovs_lasterror_to_string());
280 lockfile = xmalloc(sizeof *lockfile);
281 lockfile->name = xstrdup(name);
283 lockfile->lock_handle = lock_handle;
285 *lockfilep = lockfile;
290 lockfile_do_unlock(struct lockfile *lockfile)
292 lockfile_unhash(lockfile);
296 lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep)
297 OVS_REQUIRES(&lock_table_mutex)
307 /* Check whether we've already got a lock on that file. */
308 if (!stat(name, &s)) {
309 if (lockfile_find(s.st_dev, s.st_ino)) {
312 } else if (errno != ENOENT) {
313 VLOG_WARN("%s: failed to stat lock file: %s",
314 name, ovs_strerror(errno));
318 /* Open the lock file. */
319 fd = open(name, O_RDWR | O_CREAT, 0600);
321 VLOG_WARN("%s: failed to open lock file: %s",
322 name, ovs_strerror(errno));
326 /* Get the inode and device number for the lock table. */
328 VLOG_ERR("%s: failed to fstat lock file: %s",
329 name, ovs_strerror(errno));
334 /* Try to lock the file. */
335 memset(&l, 0, sizeof l);
337 l.l_whence = SEEK_SET;
341 error = fcntl(fd, F_SETLK, &l) == -1 ? errno : 0;
344 *lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd);
346 if (!fcntl(fd, F_GETLK, &l) && l.l_type != F_UNLCK) {