]> git.proxmox.com Git - mirror_ovs.git/blob - lib/lockfile.c
Merge "next" branch into "master".
[mirror_ovs.git] / lib / lockfile.c
1 /* Copyright (c) 2008, 2009, 2010 Nicira Networks
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"
30 #include "timeval.h"
31 #include "util.h"
32
33 #define THIS_MODULE VLM_lockfile
34 #include "vlog.h"
35
36 struct lockfile {
37 struct hmap_node hmap_node;
38 char *name;
39 dev_t device;
40 ino_t inode;
41 int fd;
42 };
43
44 /* Lock table.
45 *
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
49 * once. */
50 static struct hmap lock_table = HMAP_INITIALIZER(&lock_table);
51
52 static void lockfile_unhash(struct lockfile *);
53 static int lockfile_try_lock(const char *name, bool block,
54 struct lockfile **lockfilep);
55
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. */
59 char *
60 lockfile_name(const char *file_name)
61 {
62 const char *slash = strrchr(file_name, '/');
63 return (slash
64 ? xasprintf("%.*s/.%s.~lock~",
65 (int) (slash - file_name), file_name, slash + 1)
66 : xasprintf(".%s.~lock~", file_name));
67 }
68
69 /* Locks the configuration file against modification by other processes and
70 * re-reads it from disk.
71 *
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
74 * forever.
75 *
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
79 * NULL. */
80 int
81 lockfile_lock(const char *file, int timeout, struct lockfile **lockfilep)
82 {
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 warn_elapsed = 1000;
88 long long int start, elapsed;
89 char *lock_name;
90 int error;
91
92 COVERAGE_INC(lockfile_lock);
93
94 lock_name = lockfile_name(file);
95 time_refresh();
96 start = time_msec();
97
98 do {
99 error = lockfile_try_lock(lock_name, timeout > 0, lockfilep);
100 time_refresh();
101 elapsed = time_msec() - start;
102 if (elapsed > warn_elapsed) {
103 warn_elapsed *= 2;
104 VLOG_WARN("%s: waiting for lock file, %lld ms elapsed",
105 lock_name, elapsed);
106 }
107 } while (error == EINTR && (timeout == INT_MAX || elapsed < timeout));
108
109 if (!error) {
110 if (elapsed) {
111 VLOG_WARN("%s: waited %lld ms for lock file",
112 lock_name, elapsed);
113 }
114 } else if (error == EINTR) {
115 COVERAGE_INC(lockfile_timeout);
116 VLOG_WARN("%s: giving up on lock file after %lld ms",
117 lock_name, elapsed);
118 error = ETIMEDOUT;
119 } else {
120 COVERAGE_INC(lockfile_error);
121 if (error == EACCES) {
122 error = EAGAIN;
123 }
124 VLOG_WARN("%s: failed to lock file "
125 "(after %lld ms, with %d-ms timeout): %s",
126 lock_name, elapsed, timeout, strerror(error));
127 }
128
129 free(lock_name);
130 return error;
131 }
132
133 /* Unlocks 'lockfile', which must have been created by a call to
134 * lockfile_lock(), and frees 'lockfile'. */
135 void
136 lockfile_unlock(struct lockfile *lockfile)
137 {
138 if (lockfile) {
139 COVERAGE_INC(lockfile_unlock);
140 lockfile_unhash(lockfile);
141 free(lockfile->name);
142 free(lockfile);
143 }
144 }
145
146 /* Marks all the currently locked lockfiles as no longer locked. It makes
147 * sense to call this function after fork(), because a child created by fork()
148 * does not hold its parents' locks. */
149 void
150 lockfile_postfork(void)
151 {
152 struct lockfile *lockfile;
153
154 HMAP_FOR_EACH (lockfile, struct lockfile, hmap_node, &lock_table) {
155 if (lockfile->fd >= 0) {
156 VLOG_WARN("%s: child does not inherit lock", lockfile->name);
157 lockfile_unhash(lockfile);
158 }
159 }
160 }
161 \f
162 static uint32_t
163 lockfile_hash(dev_t device, ino_t inode)
164 {
165 return hash_bytes(&device, sizeof device,
166 hash_bytes(&inode, sizeof inode, 0));
167 }
168
169 static struct lockfile *
170 lockfile_find(dev_t device, ino_t inode)
171 {
172 struct lockfile *lockfile;
173
174 HMAP_FOR_EACH_WITH_HASH (lockfile, struct lockfile, hmap_node,
175 lockfile_hash(device, inode), &lock_table) {
176 if (lockfile->device == device && lockfile->inode == inode) {
177 return lockfile;
178 }
179 }
180 return NULL;
181 }
182
183 static void
184 lockfile_unhash(struct lockfile *lockfile)
185 {
186 if (lockfile->fd >= 0) {
187 close(lockfile->fd);
188 lockfile->fd = -1;
189 hmap_remove(&lock_table, &lockfile->hmap_node);
190 }
191 }
192
193 static struct lockfile *
194 lockfile_register(const char *name, dev_t device, ino_t inode, int fd)
195 {
196 struct lockfile *lockfile;
197
198 lockfile = lockfile_find(device, inode);
199 if (lockfile) {
200 VLOG_ERR("%s: lock file disappeared and reappeared!", name);
201 lockfile_unhash(lockfile);
202 }
203
204 lockfile = xmalloc(sizeof *lockfile);
205 lockfile->name = xstrdup(name);
206 lockfile->device = device;
207 lockfile->inode = inode;
208 lockfile->fd = fd;
209 hmap_insert(&lock_table, &lockfile->hmap_node,
210 lockfile_hash(device, inode));
211 return lockfile;
212 }
213
214 static int
215 lockfile_try_lock(const char *name, bool block, struct lockfile **lockfilep)
216 {
217 struct flock l;
218 struct stat s;
219 int error;
220 int fd;
221
222 *lockfilep = NULL;
223
224 /* Open the lock file, first creating it if necessary. */
225 for (;;) {
226 /* Check whether we've already got a lock on that file. */
227 if (!stat(name, &s)) {
228 if (lockfile_find(s.st_dev, s.st_ino)) {
229 return EDEADLK;
230 }
231 } else if (errno != ENOENT) {
232 VLOG_WARN("%s: failed to stat lock file: %s",
233 name, strerror(errno));
234 return errno;
235 }
236
237 /* Try to open an existing lock file. */
238 fd = open(name, O_RDWR);
239 if (fd >= 0) {
240 break;
241 } else if (errno != ENOENT) {
242 VLOG_WARN("%s: failed to open lock file: %s",
243 name, strerror(errno));
244 return errno;
245 }
246
247 /* Try to create a new lock file. */
248 VLOG_INFO("%s: lock file does not exist, creating", name);
249 fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
250 if (fd >= 0) {
251 break;
252 } else if (errno != EEXIST) {
253 VLOG_WARN("%s: failed to create lock file: %s",
254 name, strerror(errno));
255 return errno;
256 }
257
258 /* Someone else created the lock file. Try again. */
259 }
260
261 /* Get the inode and device number for the lock table. */
262 if (fstat(fd, &s)) {
263 VLOG_ERR("%s: failed to fstat lock file: %s", name, strerror(errno));
264 close(fd);
265 return errno;
266 }
267
268 /* Try to lock the file. */
269 memset(&l, 0, sizeof l);
270 l.l_type = F_WRLCK;
271 l.l_whence = SEEK_SET;
272 l.l_start = 0;
273 l.l_len = 0;
274
275 time_disable_restart();
276 error = fcntl(fd, block ? F_SETLKW : F_SETLK, &l) == -1 ? errno : 0;
277 time_enable_restart();
278
279 if (!error) {
280 *lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd);
281 } else {
282 close(fd);
283 }
284 return error;
285 }
286