1 /* Copyright (c) 2008, 2009 Nicira Networks
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.
33 #define THIS_MODULE VLM_lockfile
37 struct hmap_node hmap_node;
46 * We have to do this stupid dance because POSIX says that closing *any* file
47 * descriptor for a file on which a process holds a lock drops *all* locks on
48 * that file. That means that we can't afford to open a lockfile more than
50 static struct hmap lock_table = HMAP_INITIALIZER(&lock_table);
52 static void lockfile_unhash(struct lockfile *);
53 static int lockfile_try_lock(const char *name, bool block,
54 struct lockfile **lockfilep);
56 /* Returns the name of the lockfile that would be created for locking a file
57 * named 'file_name'. The caller is responsible for freeing the returned
58 * name, with free(), when it is no longer needed. */
60 lockfile_name(const char *file_name)
62 const char *slash = strrchr(file_name, '/');
64 ? xasprintf("%.*s/.%s.~lock~",
65 (int) (slash - file_name), file_name, slash + 1)
66 : xasprintf(".%s.~lock~", file_name));
69 /* Locks the configuration file against modification by other processes and
70 * re-reads it from disk.
72 * The 'timeout' specifies the maximum number of milliseconds to wait for the
73 * config file to become free. Use 0 to avoid waiting or INT_MAX to wait
76 * Returns 0 on success, otherwise a positive errno value. On success,
77 * '*lockfilep' is set to point to a new "struct lockfile *" that may be
78 * unlocked with lockfile_unlock(). On failure, '*lockfilep' is set to
81 lockfile_lock(const char *file, int timeout, struct lockfile **lockfilep)
83 /* Only exclusive ("write") locks are supported. This is not a problem
84 * because the Open vSwitch code that currently uses lock files does so in
85 * stylized ways such that any number of readers may access a file while it
86 * is being written. */
87 long long int start, elapsed;
91 COVERAGE_INC(lockfile_lock);
93 lock_name = lockfile_name(file);
98 error = lockfile_try_lock(lock_name, timeout > 0, lockfilep);
100 elapsed = time_msec() - start;
101 } while (error == EINTR && (timeout == INT_MAX || elapsed < timeout));
105 VLOG_WARN("%s: waited %lld ms for lock file",
108 } else if (error == EINTR) {
109 COVERAGE_INC(lockfile_timeout);
110 VLOG_WARN("%s: giving up on lock file after %lld ms",
114 COVERAGE_INC(lockfile_error);
115 if (error == EACCES) {
118 VLOG_WARN("%s: failed to lock file "
119 "(after %lld ms, with %d-ms timeout): %s",
120 lock_name, elapsed, timeout, strerror(error));
127 /* Unlocks 'lockfile', which must have been created by a call to
128 * lockfile_lock(), and frees 'lockfile'. */
130 lockfile_unlock(struct lockfile *lockfile)
133 COVERAGE_INC(lockfile_unlock);
134 lockfile_unhash(lockfile);
135 free(lockfile->name);
140 /* Marks all the currently locked lockfiles as no longer locked. It makes
141 * sense to call this function after fork(), because a child created by fork()
142 * does not hold its parents' locks. */
144 lockfile_postfork(void)
146 struct lockfile *lockfile;
148 HMAP_FOR_EACH (lockfile, struct lockfile, hmap_node, &lock_table) {
149 if (lockfile->fd >= 0) {
150 VLOG_WARN("%s: child does not inherit lock", lockfile->name);
151 lockfile_unhash(lockfile);
157 lockfile_hash(dev_t device, ino_t inode)
159 return hash_bytes(&device, sizeof device,
160 hash_bytes(&inode, sizeof inode, 0));
163 static struct lockfile *
164 lockfile_find(dev_t device, ino_t inode)
166 struct lockfile *lockfile;
168 HMAP_FOR_EACH_WITH_HASH (lockfile, struct lockfile, hmap_node,
169 lockfile_hash(device, inode), &lock_table) {
170 if (lockfile->device == device && lockfile->inode == inode) {
178 lockfile_unhash(struct lockfile *lockfile)
180 if (lockfile->fd >= 0) {
183 hmap_remove(&lock_table, &lockfile->hmap_node);
187 static struct lockfile *
188 lockfile_register(const char *name, dev_t device, ino_t inode, int fd)
190 struct lockfile *lockfile;
192 lockfile = lockfile_find(device, inode);
194 VLOG_ERR("%s: lock file disappeared and reappeared!", name);
195 lockfile_unhash(lockfile);
198 lockfile = xmalloc(sizeof *lockfile);
199 lockfile->name = xstrdup(name);
200 lockfile->device = device;
201 lockfile->inode = inode;
203 hmap_insert(&lock_table, &lockfile->hmap_node,
204 lockfile_hash(device, inode));
209 lockfile_try_lock(const char *name, bool block, struct lockfile **lockfilep)
218 /* Open the lock file, first creating it if necessary. */
220 /* Check whether we've already got a lock on that file. */
221 if (!stat(name, &s)) {
222 if (lockfile_find(s.st_dev, s.st_ino)) {
225 } else if (errno != ENOENT) {
226 VLOG_WARN("%s: failed to stat lock file: %s",
227 name, strerror(errno));
231 /* Try to open an existing lock file. */
232 fd = open(name, O_RDWR);
235 } else if (errno != ENOENT) {
236 VLOG_WARN("%s: failed to open lock file: %s",
237 name, strerror(errno));
241 /* Try to create a new lock file. */
242 VLOG_INFO("%s: lock file does not exist, creating", name);
243 fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
246 } else if (errno != EEXIST) {
247 VLOG_WARN("%s: failed to create lock file: %s",
248 name, strerror(errno));
252 /* Someone else created the lock file. Try again. */
255 /* Get the inode and device number for the lock table. */
257 VLOG_ERR("%s: failed to fstat lock file: %s", name, strerror(errno));
262 /* Try to lock the file. */
263 memset(&l, 0, sizeof l);
265 l.l_whence = SEEK_SET;
269 time_disable_restart();
270 error = fcntl(fd, block ? F_SETLKW : F_SETLK, &l) == -1 ? errno : 0;
271 time_enable_restart();
274 *lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd);