lockfile: Remove lockfile_lock timeout argument
[sliver-openvswitch.git] / lib / lockfile.c
1  /* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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, struct lockfile **lockfilep);
59
60 /* Returns the name of the lockfile that would be created for locking a file
61  * named 'filename_'.  The caller is responsible for freeing the returned name,
62  * with free(), when it is no longer needed. */
63 char *
64 lockfile_name(const char *filename_)
65 {
66     char *filename;
67     const char *slash;
68     char *lockname;
69
70     /* If 'filename_' is a symlink, base the name of the lockfile on the
71      * symlink's target rather than the name of the symlink.  That way, if a
72      * file is symlinked, but there is no symlink for its lockfile, then there
73      * is only a single lockfile for both the source and the target of the
74      * symlink, not one for each. */
75     filename = follow_symlinks(filename_);
76     slash = strrchr(filename, '/');
77     lockname = (slash
78                 ? xasprintf("%.*s/.%s.~lock~",
79                             (int) (slash - filename), filename, slash + 1)
80                 : xasprintf(".%s.~lock~", filename));
81     free(filename);
82
83     return lockname;
84 }
85
86 /* Locks the configuration file against modification by other processes and
87  * re-reads it from disk.
88  *
89  * Returns 0 on success, otherwise a positive errno value.  On success,
90  * '*lockfilep' is set to point to a new "struct lockfile *" that may be
91  * unlocked with lockfile_unlock().  On failure, '*lockfilep' is set to
92  * NULL.  Will not block if the lock cannot be immediately acquired. */
93 int
94 lockfile_lock(const char *file, struct lockfile **lockfilep)
95 {
96     /* Only exclusive ("write") locks are supported.  This is not a problem
97      * because the Open vSwitch code that currently uses lock files does so in
98      * stylized ways such that any number of readers may access a file while it
99      * is being written. */
100     char *lock_name;
101     int error;
102
103     COVERAGE_INC(lockfile_lock);
104
105     lock_name = lockfile_name(file);
106
107     error = lockfile_try_lock(lock_name, lockfilep);
108
109     if (error) {
110         COVERAGE_INC(lockfile_error);
111         if (error == EACCES) {
112             error = EAGAIN;
113         }
114         VLOG_WARN("%s: failed to lock file: %s",
115                   lock_name, strerror(error));
116     }
117
118     free(lock_name);
119     return error;
120 }
121
122 /* Unlocks 'lockfile', which must have been created by a call to
123  * lockfile_lock(), and frees 'lockfile'. */
124 void
125 lockfile_unlock(struct lockfile *lockfile)
126 {
127     if (lockfile) {
128         COVERAGE_INC(lockfile_unlock);
129         lockfile_unhash(lockfile);
130         free(lockfile->name);
131         free(lockfile);
132     }
133 }
134
135 /* Marks all the currently locked lockfiles as no longer locked.  It makes
136  * sense to call this function after fork(), because a child created by fork()
137  * does not hold its parents' locks. */
138 void
139 lockfile_postfork(void)
140 {
141     struct lockfile *lockfile;
142
143     HMAP_FOR_EACH (lockfile, hmap_node, &lock_table) {
144         if (lockfile->fd >= 0) {
145             VLOG_WARN("%s: child does not inherit lock", lockfile->name);
146             lockfile_unhash(lockfile);
147         }
148     }
149 }
150 \f
151 static uint32_t
152 lockfile_hash(dev_t device, ino_t inode)
153 {
154     return hash_bytes(&device, sizeof device,
155                       hash_bytes(&inode, sizeof inode, 0));
156 }
157
158 static struct lockfile *
159 lockfile_find(dev_t device, ino_t inode)
160 {
161     struct lockfile *lockfile;
162
163     HMAP_FOR_EACH_WITH_HASH (lockfile, hmap_node,
164                              lockfile_hash(device, inode), &lock_table) {
165         if (lockfile->device == device && lockfile->inode == inode) {
166             return lockfile;
167         }
168     }
169     return NULL;
170 }
171
172 static void
173 lockfile_unhash(struct lockfile *lockfile)
174 {
175     if (lockfile->fd >= 0) {
176         close(lockfile->fd);
177         lockfile->fd = -1;
178         hmap_remove(&lock_table, &lockfile->hmap_node);
179     }
180 }
181
182 static struct lockfile *
183 lockfile_register(const char *name, dev_t device, ino_t inode, int fd)
184 {
185     struct lockfile *lockfile;
186
187     lockfile = lockfile_find(device, inode);
188     if (lockfile) {
189         VLOG_ERR("%s: lock file disappeared and reappeared!", name);
190         lockfile_unhash(lockfile);
191     }
192
193     lockfile = xmalloc(sizeof *lockfile);
194     lockfile->name = xstrdup(name);
195     lockfile->device = device;
196     lockfile->inode = inode;
197     lockfile->fd = fd;
198     hmap_insert(&lock_table, &lockfile->hmap_node,
199                 lockfile_hash(device, inode));
200     return lockfile;
201 }
202
203 static int
204 lockfile_try_lock(const char *name, struct lockfile **lockfilep)
205 {
206     struct flock l;
207     struct stat s;
208     int error;
209     int fd;
210
211     *lockfilep = NULL;
212
213     /* Check whether we've already got a lock on that file. */
214     if (!stat(name, &s)) {
215         if (lockfile_find(s.st_dev, s.st_ino)) {
216             return EDEADLK;
217         }
218     } else if (errno != ENOENT) {
219         VLOG_WARN("%s: failed to stat lock file: %s",
220                   name, strerror(errno));
221         return errno;
222     }
223
224     /* Open the lock file. */
225     fd = open(name, O_RDWR | O_CREAT, 0600);
226     if (fd < 0) {
227         VLOG_WARN("%s: failed to open lock file: %s",
228                   name, strerror(errno));
229         return errno;
230     }
231
232     /* Get the inode and device number for the lock table. */
233     if (fstat(fd, &s)) {
234         VLOG_ERR("%s: failed to fstat lock file: %s", name, strerror(errno));
235         close(fd);
236         return errno;
237     }
238
239     /* Try to lock the file. */
240     memset(&l, 0, sizeof l);
241     l.l_type = F_WRLCK;
242     l.l_whence = SEEK_SET;
243     l.l_start = 0;
244     l.l_len = 0;
245
246     time_disable_restart();
247     error = fcntl(fd, F_SETLK, &l) == -1 ? errno : 0;
248     time_enable_restart();
249
250     if (!error) {
251         *lockfilep = lockfile_register(name, s.st_dev, s.st_ino, fd);
252     } else {
253         close(fd);
254     }
255     return error;
256 }
257