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