1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * Author: Michael Zucchi <notzed@ximian.com>
5 * Copyright (C) 1999 Ximian (www.ximian.com/).
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
41 #include <sys/types.h>
54 #include <glib/gi18n-lib.h>
55 #include <glib/gstdio.h>
61 #include "camel-lock.h"
63 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
70 * Create an exclusive lock using .lock semantics.
71 * All locks are equivalent to write locks (exclusive).
73 * Return value: -1 on error, sets @ex appropriately.
76 camel_lock_dot(const char *path, CamelException *ex)
84 /* TODO: Is there a reliable way to refresh the lock, if we're still busy with it?
85 Does it matter? We will normally also use fcntl too ... */
87 /* use alloca, save cleaning up afterwards */
88 lock = alloca(strlen(path) + strlen(".lock") + 1);
89 sprintf(lock, "%s.lock", path);
90 locktmp = alloca(strlen(path) + strlen("XXXXXX") + 1);
92 while (retry < CAMEL_LOCK_DOT_RETRY) {
94 d(printf("trying to lock '%s', attempt %d\n", lock, retry));
97 sleep(CAMEL_LOCK_DOT_DELAY);
99 sprintf(locktmp, "%sXXXXXX", path);
100 fdtmp = g_mkstemp(locktmp);
102 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
103 _("Could not create lock file for %s: %s"),
104 path, g_strerror (errno));
109 /* apparently return code from link can be unreliable for nfs (see link(2)), so we ignore it */
112 /* but we check stat instead (again, see link(2)) */
113 if (stat(locktmp, &st) == -1) {
114 d(printf("Our lock file %s vanished!?\n", locktmp));
116 /* well that was unexpected, try cleanup/retry */
120 d(printf("tmp lock created, link count is %d\n", st.st_nlink));
124 /* if we had 2 links, we have created the .lock, return ok, otherwise we need to keep trying */
125 if (st.st_nlink == 2)
129 /* check for stale lock, kill it */
130 if (stat(lock, &st) == 0) {
131 time_t now = time (NULL);
132 (printf("There is an existing lock %ld seconds old\n", now-st.st_ctime));
133 if (st.st_ctime < now - CAMEL_LOCK_DOT_STALE) {
134 d(printf("Removing it now\n"));
142 d(printf("failed to get lock after %d retries\n", retry));
144 camel_exception_setv(ex, CAMEL_EXCEPTION_SYSTEM, _("Timed out trying to get lock file on %s. Try again later."), path);
146 #else /* ! USE_DOT */
155 * Attempt to unlock a .lock lock.
158 camel_unlock_dot(const char *path)
163 lock = alloca(strlen(path) + strlen(".lock") + 1);
164 sprintf(lock, "%s.lock", path);
165 d(printf("unlocking %s\n", lock));
176 * Create a lock using fcntl(2).
178 * @type is CAMEL_LOCK_WRITE or CAMEL_LOCK_READ,
179 * to create exclusive or shared read locks
181 * Return value: -1 on error.
184 camel_lock_fcntl(int fd, CamelLockType type, CamelException *ex)
189 d(printf("fcntl locking %d\n", fd));
191 memset(&lock, 0, sizeof(lock));
192 lock.l_type = type==CAMEL_LOCK_READ?F_RDLCK:F_WRLCK;
193 if (fcntl(fd, F_SETLK, &lock) == -1) {
194 /* If we get a 'locking not vailable' type error,
195 we assume the filesystem doesn't support fcntl() locking */
196 /* this is somewhat system-dependent */
197 if (errno != EINVAL && errno != ENOLCK) {
198 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
199 _("Failed to get lock using fcntl(2): %s"),
203 static int failed = 0;
206 fprintf(stderr, "fcntl(2) locking appears not to work on this filesystem");
215 * camel_unlock_fcntl:
218 * Unlock an fcntl lock.
221 camel_unlock_fcntl(int fd)
226 d(printf("fcntl unlocking %d\n", fd));
228 memset(&lock, 0, sizeof(lock));
229 lock.l_type = F_UNLCK;
230 fcntl(fd, F_SETLK, &lock);
240 * Create a lock using flock(2).
242 * @type is CAMEL_LOCK_WRITE or CAMEL_LOCK_READ,
243 * to create exclusive or shared read locks
245 * Return value: -1 on error.
248 camel_lock_flock(int fd, CamelLockType type, CamelException *ex)
253 d(printf("flock locking %d\n", fd));
255 if (type == CAMEL_LOCK_READ)
256 op = LOCK_SH|LOCK_NB;
258 op = LOCK_EX|LOCK_NB;
260 if (flock(fd, op) == -1) {
261 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
262 _("Failed to get lock using flock(2): %s"),
271 * camel_unlock_flock:
274 * Unlock an flock lock.
277 camel_unlock_flock(int fd)
280 d(printf("flock unlocking %d\n", fd));
282 (void)flock(fd, LOCK_UN);
288 * @path: Path to the file to lock (used for .locking only).
289 * @fd: Open file descriptor of the right type to lock.
290 * @type: Type of lock, CAMEL_LOCK_READ or CAMEL_LOCK_WRITE.
293 * Attempt to lock a folder, multiple attempts will be made using all
294 * locking strategies available.
296 * Return value: -1 on error, @ex will describe the locking system that failed.
299 camel_lock_folder(const char *path, int fd, CamelLockType type, CamelException *ex)
303 while (retry < CAMEL_LOCK_RETRY) {
305 g_usleep(CAMEL_LOCK_DELAY*1000000);
307 if (camel_lock_fcntl(fd, type, ex) == 0) {
308 if (camel_lock_flock(fd, type, ex) == 0) {
309 if (camel_lock_dot(path, ex) == 0)
311 camel_unlock_flock(fd);
313 camel_unlock_fcntl(fd);
322 * camel_unlock_folder:
323 * @path: Filename of folder.
324 * @fd: Open descrptor on which locks were placed.
326 * Free a lock on a folder.
329 camel_unlock_folder(const char *path, int fd)
331 camel_unlock_dot(path);
332 camel_unlock_flock(fd);
333 camel_unlock_fcntl(fd);
337 int main(int argc, char **argv)
342 ex = camel_exception_new();
345 if (camel_lock_dot("mylock", ex) == 0) {
346 if (camel_lock_dot("mylock", ex) == 0) {
347 printf("Got lock twice?\n");
349 printf("failed to get lock 2: %s\n", camel_exception_get_description(ex));
351 camel_unlock_dot("mylock");
353 printf("failed to get lock 1: %s\n", camel_exception_get_description(ex));
356 camel_exception_clear(ex);
359 fd1 = open("mylock", O_RDWR);
361 printf("Could not open lock file (mylock): %s", g_strerror (errno));
364 fd2 = open("mylock", O_RDWR);
366 printf("Could not open lock file (mylock): %s", g_strerror (errno));
371 if (camel_lock_fcntl(fd1, CAMEL_LOCK_WRITE, ex) == 0) {
372 printf("got fcntl write lock once\n");
374 if (camel_lock_fcntl(fd2, CAMEL_LOCK_WRITE, ex) == 0) {
375 printf("got fcntl write lock twice!\n");
377 printf("failed to get write lock: %s\n", camel_exception_get_description(ex));
380 camel_exception_clear(ex);
382 if (camel_lock_fcntl(fd2, CAMEL_LOCK_READ, ex) == 0) {
383 printf("got fcntl read lock as well?\n");
384 camel_unlock_fcntl(fd2);
386 printf("failed to get read lock: %s\n", camel_exception_get_description(ex));
389 camel_exception_clear(ex);
390 camel_unlock_fcntl(fd1);
392 printf("failed to get write lock at all: %s\n", camel_exception_get_description(ex));
395 if (camel_lock_fcntl(fd1, CAMEL_LOCK_READ, ex) == 0) {
396 printf("got fcntl read lock once\n");
398 if (camel_lock_fcntl(fd2, CAMEL_LOCK_WRITE, ex) == 0) {
399 printf("got fcntl write lock too?!\n");
401 printf("failed to get write lock: %s\n", camel_exception_get_description(ex));
404 camel_exception_clear(ex);
406 if (camel_lock_fcntl(fd2, CAMEL_LOCK_READ, ex) == 0) {
407 printf("got fcntl read lock twice\n");
408 camel_unlock_fcntl(fd2);
410 printf("failed to get read lock: %s\n", camel_exception_get_description(ex));
413 camel_exception_clear(ex);
414 camel_unlock_fcntl(fd1);