]> git.proxmox.com Git - mirror_ovs.git/blame - lib/lockfile.c
ofp-actions: Fix userspace support for mpls_ttl.
[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"
ee89ea7b 29#include "openvswitch/hmap.h"
5947bbe6 30#include "ovs-thread.h"
ac718c9d
BP
31#include "timeval.h"
32#include "util.h"
e6211adc 33#include "openvswitch/vlog.h"
ac718c9d 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)
ae2d6e3f 64 OVS_REQUIRES(lock_table_mutex);
f21fa45f 65static void lockfile_do_unlock(struct lockfile * lockfile)
ae2d6e3f 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, '/');
d314fc84
AS
85
86#ifdef _WIN32
87 char *backslash = strrchr(filename, '\\');
88 if (backslash && (!slash || backslash > slash)) {
89 slash = backslash;
90 }
91#endif
92
b1fdc5fb
BP
93 lockname = (slash
94 ? xasprintf("%.*s/.%s.~lock~",
95 (int) (slash - filename), filename, slash + 1)
96 : xasprintf(".%s.~lock~", filename));
97 free(filename);
98
99 return lockname;
ac718c9d
BP
100}
101
102/* Locks the configuration file against modification by other processes and
103 * re-reads it from disk.
104 *
ac718c9d
BP
105 * Returns 0 on success, otherwise a positive errno value. On success,
106 * '*lockfilep' is set to point to a new "struct lockfile *" that may be
107 * unlocked with lockfile_unlock(). On failure, '*lockfilep' is set to
4770e795 108 * NULL. Will not block if the lock cannot be immediately acquired. */
ac718c9d 109int
4770e795 110lockfile_lock(const char *file, struct lockfile **lockfilep)
ac718c9d
BP
111{
112 /* Only exclusive ("write") locks are supported. This is not a problem
113 * because the Open vSwitch code that currently uses lock files does so in
114 * stylized ways such that any number of readers may access a file while it
115 * is being written. */
ac718c9d 116 char *lock_name;
45f1a0d9 117 pid_t pid;
ac718c9d
BP
118 int error;
119
120 COVERAGE_INC(lockfile_lock);
121
122 lock_name = lockfile_name(file);
4770e795 123
97be1538 124 ovs_mutex_lock(&lock_table_mutex);
f21fa45f 125 error = lockfile_try_lock(lock_name, &pid, lockfilep);
97be1538 126 ovs_mutex_unlock(&lock_table_mutex);
4770e795
LA
127
128 if (error) {
ac718c9d
BP
129 COVERAGE_INC(lockfile_error);
130 if (error == EACCES) {
131 error = EAGAIN;
132 }
607bfcb5
BP
133 if (pid == getpid()) {
134 VLOG_WARN("%s: cannot lock file because this process has already "
135 "locked it", lock_name);
136 } else if (pid) {
45f1a0d9
BP
137 VLOG_WARN("%s: cannot lock file because it is already locked by "
138 "pid %ld", lock_name, (long int) pid);
139 } else {
140 VLOG_WARN("%s: failed to lock file: %s",
10a89ef0 141 lock_name, ovs_strerror(error));
45f1a0d9 142 }
ac718c9d
BP
143 }
144
145 free(lock_name);
146 return error;
147}
148
149/* Unlocks 'lockfile', which must have been created by a call to
150 * lockfile_lock(), and frees 'lockfile'. */
151void
152lockfile_unlock(struct lockfile *lockfile)
153{
154 if (lockfile) {
97be1538 155 ovs_mutex_lock(&lock_table_mutex);
f21fa45f 156 lockfile_do_unlock(lockfile);
97be1538 157 ovs_mutex_unlock(&lock_table_mutex);
5947bbe6
BP
158
159 COVERAGE_INC(lockfile_unlock);
ac718c9d
BP
160 free(lockfile->name);
161 free(lockfile);
162 }
163}
164
165/* Marks all the currently locked lockfiles as no longer locked. It makes
166 * sense to call this function after fork(), because a child created by fork()
167 * does not hold its parents' locks. */
168void
169lockfile_postfork(void)
170{
171 struct lockfile *lockfile;
172
97be1538
EJ
173 ovs_mutex_lock(&lock_table_mutex);
174 HMAP_FOR_EACH (lockfile, hmap_node, lock_table) {
ac718c9d
BP
175 if (lockfile->fd >= 0) {
176 VLOG_WARN("%s: child does not inherit lock", lockfile->name);
177 lockfile_unhash(lockfile);
178 }
179 }
97be1538 180 ovs_mutex_unlock(&lock_table_mutex);
ac718c9d
BP
181}
182\f
183static uint32_t
184lockfile_hash(dev_t device, ino_t inode)
185{
186 return hash_bytes(&device, sizeof device,
187 hash_bytes(&inode, sizeof inode, 0));
188}
189
190static struct lockfile *
ae2d6e3f 191lockfile_find(dev_t device, ino_t inode) OVS_REQUIRES(lock_table_mutex)
ac718c9d
BP
192{
193 struct lockfile *lockfile;
194
4e8e4213 195 HMAP_FOR_EACH_WITH_HASH (lockfile, hmap_node,
97be1538 196 lockfile_hash(device, inode), lock_table) {
ac718c9d
BP
197 if (lockfile->device == device && lockfile->inode == inode) {
198 return lockfile;
199 }
200 }
201 return NULL;
202}
203
204static void
ae2d6e3f 205lockfile_unhash(struct lockfile *lockfile) OVS_REQUIRES(lock_table_mutex)
ac718c9d
BP
206{
207 if (lockfile->fd >= 0) {
208 close(lockfile->fd);
209 lockfile->fd = -1;
97be1538 210 hmap_remove(lock_table, &lockfile->hmap_node);
ac718c9d
BP
211 }
212}
213
214static struct lockfile *
215lockfile_register(const char *name, dev_t device, ino_t inode, int fd)
ae2d6e3f 216 OVS_REQUIRES(lock_table_mutex)
ac718c9d
BP
217{
218 struct lockfile *lockfile;
219
220 lockfile = lockfile_find(device, inode);
221 if (lockfile) {
222 VLOG_ERR("%s: lock file disappeared and reappeared!", name);
223 lockfile_unhash(lockfile);
224 }
225
226 lockfile = xmalloc(sizeof *lockfile);
227 lockfile->name = xstrdup(name);
228 lockfile->device = device;
229 lockfile->inode = inode;
230 lockfile->fd = fd;
97be1538 231 hmap_insert(lock_table, &lockfile->hmap_node,
ac718c9d
BP
232 lockfile_hash(device, inode));
233 return lockfile;
234}
235
f4c340e1
GS
236#ifdef _WIN32
237static void
f21fa45f 238lockfile_do_unlock(struct lockfile *lockfile)
ae2d6e3f 239 OVS_REQUIRES(lock_table_mutex)
f4c340e1
GS
240{
241 if (lockfile->fd >= 0) {
242 OVERLAPPED overl;
243 overl.hEvent = 0;
244 overl.Offset = 0;
245 overl.OffsetHigh = 0;
246 UnlockFileEx(lockfile->lock_handle, 0, 1, 0, &overl);
247
248 close(lockfile->fd);
249 lockfile->fd = -1;
250 }
251}
252
253static int
f21fa45f 254lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep)
ae2d6e3f 255 OVS_REQUIRES(lock_table_mutex)
f4c340e1
GS
256{
257 HANDLE lock_handle;
258 BOOL retval;
259 OVERLAPPED overl;
260 struct lockfile *lockfile;
261 int fd;
262
263 *pidp = 0;
264
265 fd = open(name, O_RDWR | O_CREAT, 0600);
266 if (fd < 0) {
267 VLOG_WARN("%s: failed to open lock file: %s",
268 name, ovs_strerror(errno));
269 return errno;
270 }
271
272 lock_handle = (HANDLE)_get_osfhandle(fd);
273 if (lock_handle < 0) {
274 VLOG_WARN("%s: failed to get the file handle: %s",
275 name, ovs_strerror(errno));
276 return errno;
277 }
278
279 /* Lock the file 'name' for the region that includes just the first
280 * byte. */
281 overl.hEvent = 0;
282 overl.Offset = 0;
283 overl.OffsetHigh = 0;
284 retval = LockFileEx(lock_handle, LOCKFILE_EXCLUSIVE_LOCK
285 | LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &overl);
286 if (!retval) {
7acb0553
GS
287 VLOG_DBG("Failed to lock file : %s", ovs_lasterror_to_string());
288 *pidp = getpid();
289 return EDEADLK;
f4c340e1
GS
290 }
291
292 lockfile = xmalloc(sizeof *lockfile);
293 lockfile->name = xstrdup(name);
294 lockfile->fd = fd;
295 lockfile->lock_handle = lock_handle;
296
297 *lockfilep = lockfile;
298 return 0;
299}
f21fa45f
BP
300#else /* !_WIN32 */
301static void
302lockfile_do_unlock(struct lockfile *lockfile)
303{
304 lockfile_unhash(lockfile);
305}
f4c340e1 306
ac718c9d 307static int
f21fa45f 308lockfile_try_lock(const char *name, pid_t *pidp, struct lockfile **lockfilep)
ae2d6e3f 309 OVS_REQUIRES(lock_table_mutex)
ac718c9d
BP
310{
311 struct flock l;
312 struct stat s;
313 int error;
314 int fd;
315
316 *lockfilep = NULL;
45f1a0d9 317 *pidp = 0;
ac718c9d 318
77c513a4
BP
319 /* Check whether we've already got a lock on that file. */
320 if (!stat(name, &s)) {
321 if (lockfile_find(s.st_dev, s.st_ino)) {
607bfcb5 322 *pidp = getpid();
77c513a4 323 return EDEADLK;
ac718c9d 324 }
77c513a4
BP
325 } else if (errno != ENOENT) {
326 VLOG_WARN("%s: failed to stat lock file: %s",
10a89ef0 327 name, ovs_strerror(errno));
77c513a4
BP
328 return errno;
329 }
ac718c9d 330
77c513a4
BP
331 /* Open the lock file. */
332 fd = open(name, O_RDWR | O_CREAT, 0600);
333 if (fd < 0) {
334 VLOG_WARN("%s: failed to open lock file: %s",
10a89ef0 335 name, ovs_strerror(errno));
77c513a4 336 return errno;
ac718c9d
BP
337 }
338
339 /* Get the inode and device number for the lock table. */
340 if (fstat(fd, &s)) {
10a89ef0
BP
341 VLOG_ERR("%s: failed to fstat lock file: %s",
342 name, ovs_strerror(errno));
ac718c9d
BP
343 close(fd);
344 return errno;
345 }
346
347 /* Try to lock the file. */
348 memset(&l, 0, sizeof l);
349 l.l_type = F_WRLCK;
350 l.l_whence = SEEK_SET;
351 l.l_start = 0;
352 l.l_len = 0;
353
4770e795 354 error = fcntl(fd, F_SETLK, &l) == -1 ? errno : 0;
ac718c9d
BP
355
356 if (!error) {
357 *lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd);
358 } else {
45f1a0d9
BP
359 if (!fcntl(fd, F_GETLK, &l) && l.l_type != F_UNLCK) {
360 *pidp = l.l_pid;
361 }
ac718c9d
BP
362 close(fd);
363 }
364 return error;
365}
f4c340e1 366#endif