]> git.proxmox.com Git - mirror_ovs.git/blame - lib/lockfile.c
netlink-socket: Refill comment to fit within 79 columns.
[mirror_ovs.git] / lib / lockfile.c
CommitLineData
f21fa45f 1 /* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
ac718c9d
BP
2 *
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <config.h>
17
18#include "lockfile.h"
19
20#include <errno.h>
21#include <fcntl.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <unistd.h>
26
27#include "coverage.h"
28#include "hash.h"
29#include "hmap.h"
5947bbe6 30#include "ovs-thread.h"
ac718c9d
BP
31#include "timeval.h"
32#include "util.h"
ac718c9d
BP
33#include "vlog.h"
34
d98e6007 35VLOG_DEFINE_THIS_MODULE(lockfile);
5136ce49 36
d76f09ea 37COVERAGE_DEFINE(lockfile_lock);
d76f09ea
BP
38COVERAGE_DEFINE(lockfile_error);
39COVERAGE_DEFINE(lockfile_unlock);
40
ac718c9d
BP
41struct lockfile {
42 struct hmap_node hmap_node;
43 char *name;
44 dev_t device;
45 ino_t inode;
46 int fd;
f4c340e1 47 HANDLE lock_handle;
ac718c9d
BP
48};
49
50/* Lock table.
51 *
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
55 * once. */
97be1538
EJ
56static struct ovs_mutex lock_table_mutex = OVS_MUTEX_INITIALIZER;
57static struct hmap lock_table__ = HMAP_INITIALIZER(&lock_table__);
58static struct hmap *const lock_table OVS_GUARDED_BY(lock_table_mutex)
59 = &lock_table__;
5947bbe6 60
ac718c9d 61static void lockfile_unhash(struct lockfile *);
f21fa45f
BP
62static int lockfile_try_lock(const char *name, pid_t *pidp,
63 struct lockfile **lockfilep)
64 OVS_REQUIRES(&lock_table_mutex);
65static void lockfile_do_unlock(struct lockfile * lockfile)
66 OVS_REQUIRES(&lock_table_mutex);
ac718c9d
BP
67
68/* Returns the name of the lockfile that would be created for locking a file
b1fdc5fb
BP
69 * named 'filename_'. The caller is responsible for freeing the returned name,
70 * with free(), when it is no longer needed. */
ac718c9d 71char *
b1fdc5fb 72lockfile_name(const char *filename_)
ac718c9d 73{
b1fdc5fb
BP
74 char *filename;
75 const char *slash;
76 char *lockname;
77
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, '/');
85 lockname = (slash
86 ? xasprintf("%.*s/.%s.~lock~",
87 (int) (slash - filename), filename, slash + 1)
88 : xasprintf(".%s.~lock~", filename));
89 free(filename);
90
91 return lockname;
ac718c9d
BP
92}
93
94/* Locks the configuration file against modification by other processes and
95 * re-reads it from disk.
96 *
ac718c9d
BP
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
4770e795 100 * NULL. Will not block if the lock cannot be immediately acquired. */
ac718c9d 101int
4770e795 102lockfile_lock(const char *file, struct lockfile **lockfilep)
ac718c9d
BP
103{
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. */
ac718c9d 108 char *lock_name;
45f1a0d9 109 pid_t pid;
ac718c9d
BP
110 int error;
111
112 COVERAGE_INC(lockfile_lock);
113
114 lock_name = lockfile_name(file);
4770e795 115
97be1538 116 ovs_mutex_lock(&lock_table_mutex);
f21fa45f 117 error = lockfile_try_lock(lock_name, &pid, lockfilep);
97be1538 118 ovs_mutex_unlock(&lock_table_mutex);
4770e795
LA
119
120 if (error) {
ac718c9d
BP
121 COVERAGE_INC(lockfile_error);
122 if (error == EACCES) {
123 error = EAGAIN;
124 }
607bfcb5
BP
125 if (pid == getpid()) {
126 VLOG_WARN("%s: cannot lock file because this process has already "
127 "locked it", lock_name);
128 } else if (pid) {
45f1a0d9
BP
129 VLOG_WARN("%s: cannot lock file because it is already locked by "
130 "pid %ld", lock_name, (long int) pid);
131 } else {
132 VLOG_WARN("%s: failed to lock file: %s",
10a89ef0 133 lock_name, ovs_strerror(error));
45f1a0d9 134 }
ac718c9d
BP
135 }
136
137 free(lock_name);
138 return error;
139}
140
141/* Unlocks 'lockfile', which must have been created by a call to
142 * lockfile_lock(), and frees 'lockfile'. */
143void
144lockfile_unlock(struct lockfile *lockfile)
145{
146 if (lockfile) {
97be1538 147 ovs_mutex_lock(&lock_table_mutex);
f21fa45f 148 lockfile_do_unlock(lockfile);
97be1538 149 ovs_mutex_unlock(&lock_table_mutex);
5947bbe6
BP
150
151 COVERAGE_INC(lockfile_unlock);
ac718c9d
BP
152 free(lockfile->name);
153 free(lockfile);
154 }
155}
156
157/* Marks all the currently locked lockfiles as no longer locked. It makes
158 * sense to call this function after fork(), because a child created by fork()
159 * does not hold its parents' locks. */
160void
161lockfile_postfork(void)
162{
163 struct lockfile *lockfile;
164
97be1538
EJ
165 ovs_mutex_lock(&lock_table_mutex);
166 HMAP_FOR_EACH (lockfile, hmap_node, lock_table) {
ac718c9d
BP
167 if (lockfile->fd >= 0) {
168 VLOG_WARN("%s: child does not inherit lock", lockfile->name);
169 lockfile_unhash(lockfile);
170 }
171 }
97be1538 172 ovs_mutex_unlock(&lock_table_mutex);
ac718c9d
BP
173}
174\f
175static uint32_t
176lockfile_hash(dev_t device, ino_t inode)
177{
178 return hash_bytes(&device, sizeof device,
179 hash_bytes(&inode, sizeof inode, 0));
180}
181
182static struct lockfile *
97be1538 183lockfile_find(dev_t device, ino_t inode) OVS_REQUIRES(&lock_table_mutex)
ac718c9d
BP
184{
185 struct lockfile *lockfile;
186
4e8e4213 187 HMAP_FOR_EACH_WITH_HASH (lockfile, hmap_node,
97be1538 188 lockfile_hash(device, inode), lock_table) {
ac718c9d
BP
189 if (lockfile->device == device && lockfile->inode == inode) {
190 return lockfile;
191 }
192 }
193 return NULL;
194}
195
196static void
97be1538 197lockfile_unhash(struct lockfile *lockfile) OVS_REQUIRES(&lock_table_mutex)
ac718c9d
BP
198{
199 if (lockfile->fd >= 0) {
200 close(lockfile->fd);
201 lockfile->fd = -1;
97be1538 202 hmap_remove(lock_table, &lockfile->hmap_node);
ac718c9d
BP
203 }
204}
205
206static struct lockfile *
207lockfile_register(const char *name, dev_t device, ino_t inode, int fd)
97be1538 208 OVS_REQUIRES(&lock_table_mutex)
ac718c9d
BP
209{
210 struct lockfile *lockfile;
211
212 lockfile = lockfile_find(device, inode);
213 if (lockfile) {
214 VLOG_ERR("%s: lock file disappeared and reappeared!", name);
215 lockfile_unhash(lockfile);
216 }
217
218 lockfile = xmalloc(sizeof *lockfile);
219 lockfile->name = xstrdup(name);
220 lockfile->device = device;
221 lockfile->inode = inode;
222 lockfile->fd = fd;
97be1538 223 hmap_insert(lock_table, &lockfile->hmap_node,
ac718c9d
BP
224 lockfile_hash(device, inode));
225 return lockfile;
226}
227
f4c340e1
GS
228#ifdef _WIN32
229static void
f21fa45f 230lockfile_do_unlock(struct lockfile *lockfile)
f4c340e1
GS
231 OVS_REQUIRES(&lock_table_mutex)
232{
233 if (lockfile->fd >= 0) {
234 OVERLAPPED overl;
235 overl.hEvent = 0;
236 overl.Offset = 0;
237 overl.OffsetHigh = 0;
238 UnlockFileEx(lockfile->lock_handle, 0, 1, 0, &overl);
239
240 close(lockfile->fd);
241 lockfile->fd = -1;
242 }
243}
244
245static int
f21fa45f 246lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep)
f4c340e1
GS
247 OVS_REQUIRES(&lock_table_mutex)
248{
249 HANDLE lock_handle;
250 BOOL retval;
251 OVERLAPPED overl;
252 struct lockfile *lockfile;
253 int fd;
254
255 *pidp = 0;
256
257 fd = open(name, O_RDWR | O_CREAT, 0600);
258 if (fd < 0) {
259 VLOG_WARN("%s: failed to open lock file: %s",
260 name, ovs_strerror(errno));
261 return errno;
262 }
263
264 lock_handle = (HANDLE)_get_osfhandle(fd);
265 if (lock_handle < 0) {
266 VLOG_WARN("%s: failed to get the file handle: %s",
267 name, ovs_strerror(errno));
268 return errno;
269 }
270
271 /* Lock the file 'name' for the region that includes just the first
272 * byte. */
273 overl.hEvent = 0;
274 overl.Offset = 0;
275 overl.OffsetHigh = 0;
276 retval = LockFileEx(lock_handle, LOCKFILE_EXCLUSIVE_LOCK
277 | LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &overl);
278 if (!retval) {
7acb0553
GS
279 VLOG_DBG("Failed to lock file : %s", ovs_lasterror_to_string());
280 *pidp = getpid();
281 return EDEADLK;
f4c340e1
GS
282 }
283
284 lockfile = xmalloc(sizeof *lockfile);
285 lockfile->name = xstrdup(name);
286 lockfile->fd = fd;
287 lockfile->lock_handle = lock_handle;
288
289 *lockfilep = lockfile;
290 return 0;
291}
f21fa45f
BP
292#else /* !_WIN32 */
293static void
294lockfile_do_unlock(struct lockfile *lockfile)
295{
296 lockfile_unhash(lockfile);
297}
f4c340e1 298
ac718c9d 299static int
f21fa45f 300lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep)
97be1538 301 OVS_REQUIRES(&lock_table_mutex)
ac718c9d
BP
302{
303 struct flock l;
304 struct stat s;
305 int error;
306 int fd;
307
308 *lockfilep = NULL;
45f1a0d9 309 *pidp = 0;
ac718c9d 310
77c513a4
BP
311 /* Check whether we've already got a lock on that file. */
312 if (!stat(name, &s)) {
313 if (lockfile_find(s.st_dev, s.st_ino)) {
607bfcb5 314 *pidp = getpid();
77c513a4 315 return EDEADLK;
ac718c9d 316 }
77c513a4
BP
317 } else if (errno != ENOENT) {
318 VLOG_WARN("%s: failed to stat lock file: %s",
10a89ef0 319 name, ovs_strerror(errno));
77c513a4
BP
320 return errno;
321 }
ac718c9d 322
77c513a4
BP
323 /* Open the lock file. */
324 fd = open(name, O_RDWR | O_CREAT, 0600);
325 if (fd < 0) {
326 VLOG_WARN("%s: failed to open lock file: %s",
10a89ef0 327 name, ovs_strerror(errno));
77c513a4 328 return errno;
ac718c9d
BP
329 }
330
331 /* Get the inode and device number for the lock table. */
332 if (fstat(fd, &s)) {
10a89ef0
BP
333 VLOG_ERR("%s: failed to fstat lock file: %s",
334 name, ovs_strerror(errno));
ac718c9d
BP
335 close(fd);
336 return errno;
337 }
338
339 /* Try to lock the file. */
340 memset(&l, 0, sizeof l);
341 l.l_type = F_WRLCK;
342 l.l_whence = SEEK_SET;
343 l.l_start = 0;
344 l.l_len = 0;
345
4770e795 346 error = fcntl(fd, F_SETLK, &l) == -1 ? errno : 0;
ac718c9d
BP
347
348 if (!error) {
349 *lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd);
350 } else {
45f1a0d9
BP
351 if (!fcntl(fd, F_GETLK, &l) && l.l_type != F_UNLCK) {
352 *pidp = l.l_pid;
353 }
ac718c9d
BP
354 close(fd);
355 }
356 return error;
357}
f4c340e1 358#endif