1 /* Copyright (c) 2008, 2009, 2010, 2011, 2012 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.
34 VLOG_DEFINE_THIS_MODULE(lockfile);
36 COVERAGE_DEFINE(lockfile_lock);
37 COVERAGE_DEFINE(lockfile_timeout);
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 hmap lock_table = HMAP_INITIALIZER(&lock_table);
57 static void lockfile_unhash(struct lockfile *);
58 static int lockfile_try_lock(const char *name, struct lockfile **lockfilep);
60 /* Returns the name of the lockfile that would be created for locking a file
61 * named 'filename_'. The caller is responsible for freeing the returned name,
62 * with free(), when it is no longer needed. */
64 lockfile_name(const char *filename_)
70 /* If 'filename_' is a symlink, base the name of the lockfile on the
71 * symlink's target rather than the name of the symlink. That way, if a
72 * file is symlinked, but there is no symlink for its lockfile, then there
73 * is only a single lockfile for both the source and the target of the
74 * symlink, not one for each. */
75 filename = follow_symlinks(filename_);
76 slash = strrchr(filename, '/');
78 ? xasprintf("%.*s/.%s.~lock~",
79 (int) (slash - filename), filename, slash + 1)
80 : xasprintf(".%s.~lock~", filename));
86 /* Locks the configuration file against modification by other processes and
87 * re-reads it from disk.
89 * Returns 0 on success, otherwise a positive errno value. On success,
90 * '*lockfilep' is set to point to a new "struct lockfile *" that may be
91 * unlocked with lockfile_unlock(). On failure, '*lockfilep' is set to
92 * NULL. Will not block if the lock cannot be immediately acquired. */
94 lockfile_lock(const char *file, struct lockfile **lockfilep)
96 /* Only exclusive ("write") locks are supported. This is not a problem
97 * because the Open vSwitch code that currently uses lock files does so in
98 * stylized ways such that any number of readers may access a file while it
99 * is being written. */
103 COVERAGE_INC(lockfile_lock);
105 lock_name = lockfile_name(file);
107 error = lockfile_try_lock(lock_name, lockfilep);
110 COVERAGE_INC(lockfile_error);
111 if (error == EACCES) {
114 VLOG_WARN("%s: failed to lock file: %s",
115 lock_name, strerror(error));
122 /* Unlocks 'lockfile', which must have been created by a call to
123 * lockfile_lock(), and frees 'lockfile'. */
125 lockfile_unlock(struct lockfile *lockfile)
128 COVERAGE_INC(lockfile_unlock);
129 lockfile_unhash(lockfile);
130 free(lockfile->name);
135 /* Marks all the currently locked lockfiles as no longer locked. It makes
136 * sense to call this function after fork(), because a child created by fork()
137 * does not hold its parents' locks. */
139 lockfile_postfork(void)
141 struct lockfile *lockfile;
143 HMAP_FOR_EACH (lockfile, hmap_node, &lock_table) {
144 if (lockfile->fd >= 0) {
145 VLOG_WARN("%s: child does not inherit lock", lockfile->name);
146 lockfile_unhash(lockfile);
152 lockfile_hash(dev_t device, ino_t inode)
154 return hash_bytes(&device, sizeof device,
155 hash_bytes(&inode, sizeof inode, 0));
158 static struct lockfile *
159 lockfile_find(dev_t device, ino_t inode)
161 struct lockfile *lockfile;
163 HMAP_FOR_EACH_WITH_HASH (lockfile, hmap_node,
164 lockfile_hash(device, inode), &lock_table) {
165 if (lockfile->device == device && lockfile->inode == inode) {
173 lockfile_unhash(struct lockfile *lockfile)
175 if (lockfile->fd >= 0) {
178 hmap_remove(&lock_table, &lockfile->hmap_node);
182 static struct lockfile *
183 lockfile_register(const char *name, dev_t device, ino_t inode, int fd)
185 struct lockfile *lockfile;
187 lockfile = lockfile_find(device, inode);
189 VLOG_ERR("%s: lock file disappeared and reappeared!", name);
190 lockfile_unhash(lockfile);
193 lockfile = xmalloc(sizeof *lockfile);
194 lockfile->name = xstrdup(name);
195 lockfile->device = device;
196 lockfile->inode = inode;
198 hmap_insert(&lock_table, &lockfile->hmap_node,
199 lockfile_hash(device, inode));
204 lockfile_try_lock(const char *name, struct lockfile **lockfilep)
213 /* Check whether we've already got a lock on that file. */
214 if (!stat(name, &s)) {
215 if (lockfile_find(s.st_dev, s.st_ino)) {
218 } else if (errno != ENOENT) {
219 VLOG_WARN("%s: failed to stat lock file: %s",
220 name, strerror(errno));
224 /* Open the lock file. */
225 fd = open(name, O_RDWR | O_CREAT, 0600);
227 VLOG_WARN("%s: failed to open lock file: %s",
228 name, strerror(errno));
232 /* Get the inode and device number for the lock table. */
234 VLOG_ERR("%s: failed to fstat lock file: %s", name, strerror(errno));
239 /* Try to lock the file. */
240 memset(&l, 0, sizeof l);
242 l.l_whence = SEEK_SET;
246 time_disable_restart();
247 error = fcntl(fd, F_SETLK, &l) == -1 ? errno : 0;
248 time_enable_restart();
251 *lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd);