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