1 /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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;
51 * We have to do this stupid dance because POSIX says that closing *any* file
52 * descriptor for a file on which a process holds a lock drops *all* locks on
53 * that file. That means that we can't afford to open a lockfile more than
55 static struct ovs_mutex lock_table_mutex = OVS_MUTEX_INITIALIZER;
56 static struct hmap lock_table__ = HMAP_INITIALIZER(&lock_table__);
57 static struct hmap *const lock_table OVS_GUARDED_BY(lock_table_mutex)
60 static void lockfile_unhash(struct lockfile *);
61 static int lockfile_try_lock(const char *name, pid_t *pidp,
62 struct lockfile **lockfilep);
64 /* Returns the name of the lockfile that would be created for locking a file
65 * named 'filename_'. The caller is responsible for freeing the returned name,
66 * with free(), when it is no longer needed. */
68 lockfile_name(const char *filename_)
74 /* If 'filename_' is a symlink, base the name of the lockfile on the
75 * symlink's target rather than the name of the symlink. That way, if a
76 * file is symlinked, but there is no symlink for its lockfile, then there
77 * is only a single lockfile for both the source and the target of the
78 * symlink, not one for each. */
79 filename = follow_symlinks(filename_);
80 slash = strrchr(filename, '/');
82 ? xasprintf("%.*s/.%s.~lock~",
83 (int) (slash - filename), filename, slash + 1)
84 : xasprintf(".%s.~lock~", filename));
90 /* Locks the configuration file against modification by other processes and
91 * re-reads it from disk.
93 * Returns 0 on success, otherwise a positive errno value. On success,
94 * '*lockfilep' is set to point to a new "struct lockfile *" that may be
95 * unlocked with lockfile_unlock(). On failure, '*lockfilep' is set to
96 * NULL. Will not block if the lock cannot be immediately acquired. */
98 lockfile_lock(const char *file, struct lockfile **lockfilep)
100 /* Only exclusive ("write") locks are supported. This is not a problem
101 * because the Open vSwitch code that currently uses lock files does so in
102 * stylized ways such that any number of readers may access a file while it
103 * is being written. */
108 COVERAGE_INC(lockfile_lock);
110 lock_name = lockfile_name(file);
112 ovs_mutex_lock(&lock_table_mutex);
113 error = lockfile_try_lock(lock_name, &pid, lockfilep);
114 ovs_mutex_unlock(&lock_table_mutex);
117 COVERAGE_INC(lockfile_error);
118 if (error == EACCES) {
122 VLOG_WARN("%s: cannot lock file because it is already locked by "
123 "pid %ld", lock_name, (long int) pid);
125 VLOG_WARN("%s: failed to lock file: %s",
126 lock_name, ovs_strerror(error));
134 /* Unlocks 'lockfile', which must have been created by a call to
135 * lockfile_lock(), and frees 'lockfile'. */
137 lockfile_unlock(struct lockfile *lockfile)
140 ovs_mutex_lock(&lock_table_mutex);
141 lockfile_unhash(lockfile);
142 ovs_mutex_unlock(&lock_table_mutex);
144 COVERAGE_INC(lockfile_unlock);
145 free(lockfile->name);
150 /* Marks all the currently locked lockfiles as no longer locked. It makes
151 * sense to call this function after fork(), because a child created by fork()
152 * does not hold its parents' locks. */
154 lockfile_postfork(void)
156 struct lockfile *lockfile;
158 ovs_mutex_lock(&lock_table_mutex);
159 HMAP_FOR_EACH (lockfile, hmap_node, lock_table) {
160 if (lockfile->fd >= 0) {
161 VLOG_WARN("%s: child does not inherit lock", lockfile->name);
162 lockfile_unhash(lockfile);
165 ovs_mutex_unlock(&lock_table_mutex);
169 lockfile_hash(dev_t device, ino_t inode)
171 return hash_bytes(&device, sizeof device,
172 hash_bytes(&inode, sizeof inode, 0));
175 static struct lockfile *
176 lockfile_find(dev_t device, ino_t inode) OVS_REQUIRES(&lock_table_mutex)
178 struct lockfile *lockfile;
180 HMAP_FOR_EACH_WITH_HASH (lockfile, hmap_node,
181 lockfile_hash(device, inode), lock_table) {
182 if (lockfile->device == device && lockfile->inode == inode) {
190 lockfile_unhash(struct lockfile *lockfile) OVS_REQUIRES(&lock_table_mutex)
192 if (lockfile->fd >= 0) {
195 hmap_remove(lock_table, &lockfile->hmap_node);
199 static struct lockfile *
200 lockfile_register(const char *name, dev_t device, ino_t inode, int fd)
201 OVS_REQUIRES(&lock_table_mutex)
203 struct lockfile *lockfile;
205 lockfile = lockfile_find(device, inode);
207 VLOG_ERR("%s: lock file disappeared and reappeared!", name);
208 lockfile_unhash(lockfile);
211 lockfile = xmalloc(sizeof *lockfile);
212 lockfile->name = xstrdup(name);
213 lockfile->device = device;
214 lockfile->inode = inode;
216 hmap_insert(lock_table, &lockfile->hmap_node,
217 lockfile_hash(device, inode));
222 lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep)
223 OVS_REQUIRES(&lock_table_mutex)
233 /* Check whether we've already got a lock on that file. */
234 if (!stat(name, &s)) {
235 if (lockfile_find(s.st_dev, s.st_ino)) {
238 } else if (errno != ENOENT) {
239 VLOG_WARN("%s: failed to stat lock file: %s",
240 name, ovs_strerror(errno));
244 /* Open the lock file. */
245 fd = open(name, O_RDWR | O_CREAT, 0600);
247 VLOG_WARN("%s: failed to open lock file: %s",
248 name, ovs_strerror(errno));
252 /* Get the inode and device number for the lock table. */
254 VLOG_ERR("%s: failed to fstat lock file: %s",
255 name, ovs_strerror(errno));
260 /* Try to lock the file. */
261 memset(&l, 0, sizeof l);
263 l.l_whence = SEEK_SET;
267 error = fcntl(fd, F_SETLK, &l) == -1 ? errno : 0;
270 *lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd);
272 if (!fcntl(fd, F_GETLK, &l) && l.l_type != F_UNLCK) {