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-2008 Novell, Inc. (www.novell.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
37 #include <sys/types.h>
50 #include <glib/gi18n-lib.h>
51 #include <glib/gstdio.h>
57 #include "camel-lock.h"
59 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
64 * @error: return location for a #GError, or %NULL
66 * Create an exclusive lock using .lock semantics.
67 * All locks are equivalent to write locks (exclusive).
69 * Returns: -1 on error, sets @ex appropriately.
72 camel_lock_dot (const gchar *path,
76 gchar *locktmp, *lock;
81 /* TODO: Is there a reliable way to refresh the lock, if we're still busy with it?
82 * Does it matter? We will normally also use fcntl too ... */
84 /* use alloca, save cleaning up afterwards */
85 lock = alloca (strlen (path) + strlen (".lock") + 1);
86 sprintf (lock, "%s.lock", path);
87 locktmp = alloca (strlen (path) + strlen ("XXXXXX") + 1);
89 while (retry < CAMEL_LOCK_DOT_RETRY) {
91 d (printf ("trying to lock '%s', attempt %d\n", lock, retry));
94 sleep (CAMEL_LOCK_DOT_DELAY);
96 sprintf (locktmp, "%sXXXXXX", path);
97 fdtmp = g_mkstemp (locktmp);
101 g_io_error_from_errno (errno),
102 _("Could not create lock file for %s: %s"),
103 path, g_strerror (errno));
108 /* apparently return code from link can be unreliable for nfs (see link(2)), so we ignore it */
109 link (locktmp, lock);
111 /* but we check stat instead (again, see link(2)) */
112 if (g_stat (locktmp, &st) == -1) {
113 d (printf ("Our lock file %s vanished!?\n", locktmp));
115 /* well that was unexpected, try cleanup/retry */
119 d (printf ("tmp lock created, link count is %d\n", st.st_nlink));
123 /* if we had 2 links, we have created the .lock, return ok, otherwise we need to keep trying */
124 if (st.st_nlink == 2)
128 /* check for stale lock, kill it */
129 if (g_stat (lock, &st) == 0) {
130 time_t now = time (NULL);
131 (printf ("There is an existing lock %ld seconds old\n", now - st.st_ctime));
132 if (st.st_ctime < now - CAMEL_LOCK_DOT_STALE) {
133 d (printf ("Removing it now\n"));
141 d (printf ("failed to get lock after %d retries\n", retry));
144 error, G_IO_ERROR, G_IO_ERROR_FAILED,
145 _("Timed out trying to get lock file on %s. "
146 "Try again later."), path);
157 * Attempt to unlock a .lock lock.
160 camel_unlock_dot (const gchar *path)
165 lock = alloca (strlen (path) + strlen (".lock") + 1);
166 sprintf (lock, "%s.lock", path);
167 d (printf ("unlocking %s\n", lock));
168 (void) unlink (lock);
176 * @error: return location for a #GError, or %NULL
178 * Create a lock using fcntl(2).
180 * @type is CAMEL_LOCK_WRITE or CAMEL_LOCK_READ,
181 * to create exclusive or shared read locks
183 * Returns: -1 on error.
186 camel_lock_fcntl (gint fd,
193 d (printf ("fcntl locking %d\n", fd));
195 memset (&lock, 0, sizeof (lock));
196 lock.l_type = type == CAMEL_LOCK_READ ? F_RDLCK : F_WRLCK;
197 if (fcntl (fd, F_SETLK, &lock) == -1) {
198 /* If we get a 'locking not vailable' type error,
199 * we assume the filesystem doesn't support fcntl () locking */
200 /* this is somewhat system-dependent */
201 if (errno != EINVAL && errno != ENOLCK) {
204 g_io_error_from_errno (errno),
205 _("Failed to get lock using fcntl(2): %s"),
209 static gint failed = 0;
212 fprintf (stderr, "fcntl(2) locking appears not to work on this filesystem");
221 * camel_unlock_fcntl:
224 * Unlock an fcntl lock.
227 camel_unlock_fcntl (gint fd)
232 d (printf ("fcntl unlocking %d\n", fd));
234 memset (&lock, 0, sizeof (lock));
235 lock.l_type = F_UNLCK;
236 fcntl (fd, F_SETLK, &lock);
244 * @error: return location for a #GError, or %NULL
246 * Create a lock using flock(2).
248 * @type is CAMEL_LOCK_WRITE or CAMEL_LOCK_READ,
249 * to create exclusive or shared read locks
251 * Returns: -1 on error.
254 camel_lock_flock (gint fd,
261 d (printf ("flock locking %d\n", fd));
263 if (type == CAMEL_LOCK_READ)
264 op = LOCK_SH | LOCK_NB;
266 op = LOCK_EX | LOCK_NB;
268 if (flock (fd, op) == -1) {
271 g_io_error_from_errno (errno),
272 _("Failed to get lock using flock(2): %s"),
281 * camel_unlock_flock:
284 * Unlock an flock lock.
287 camel_unlock_flock (gint fd)
290 d (printf ("flock unlocking %d\n", fd));
292 (void) flock (fd, LOCK_UN);
298 * @path: Path to the file to lock (used for .locking only).
299 * @fd: Open file descriptor of the right type to lock.
300 * @type: Type of lock, CAMEL_LOCK_READ or CAMEL_LOCK_WRITE.
301 * @error: return location for a #GError, or %NULL
303 * Attempt to lock a folder, multiple attempts will be made using all
304 * locking strategies available.
306 * Returns: -1 on error, @ex will describe the locking system that failed.
309 camel_lock_folder (const gchar *path,
316 while (retry < CAMEL_LOCK_RETRY) {
318 g_usleep (CAMEL_LOCK_DELAY * 1000000);
320 if (camel_lock_fcntl (fd, type, error) == 0) {
321 if (camel_lock_flock (fd, type, error) == 0) {
322 if (camel_lock_dot (path, error) == 0)
324 camel_unlock_flock (fd);
326 camel_unlock_fcntl (fd);
335 * camel_unlock_folder:
336 * @path: Filename of folder.
337 * @fd: Open descrptor on which locks were placed.
339 * Free a lock on a folder.
342 camel_unlock_folder (const gchar *path,
345 camel_unlock_dot (path);
346 camel_unlock_flock (fd);
347 camel_unlock_fcntl (fd);
355 GError *error = NULL;
359 if (camel_lock_dot ("mylock", &error) == 0) {
360 if (camel_lock_dot ("mylock", &error) == 0) {
361 printf ("Got lock twice?\n");
363 printf ("failed to get lock 2: %s\n", error->message);
365 camel_unlock_dot ("mylock");
367 printf ("failed to get lock 1: %s\n", error->message);
371 g_clear_error (&error);
374 fd1 = open ("mylock", O_RDWR);
376 printf ("Could not open lock file (mylock): %s", g_strerror (errno));
379 fd2 = open ("mylock", O_RDWR);
381 printf ("Could not open lock file (mylock): %s", g_strerror (errno));
386 if (camel_lock_fcntl (fd1, CAMEL_LOCK_WRITE, &error) == 0) {
387 printf ("got fcntl write lock once\n");
389 if (camel_lock_fcntl (fd2, CAMEL_LOCK_WRITE, &error) == 0) {
390 printf ("got fcntl write lock twice!\n");
392 printf ("failed to get write lock: %s\n", error->message);
396 g_clear_error (&error);
398 if (camel_lock_fcntl (fd2, CAMEL_LOCK_READ, &error) == 0) {
399 printf ("got fcntl read lock as well?\n");
400 camel_unlock_fcntl (fd2);
402 printf ("failed to get read lock: %s\n", error->message);
406 g_clear_error (&error);
407 camel_unlock_fcntl (fd1);
409 printf ("failed to get write lock at all: %s\n", error->message);
412 if (camel_lock_fcntl (fd1, CAMEL_LOCK_READ, &error) == 0) {
413 printf ("got fcntl read lock once\n");
415 if (camel_lock_fcntl (fd2, CAMEL_LOCK_WRITE, &error) == 0) {
416 printf ("got fcntl write lock too?!\n");
418 printf ("failed to get write lock: %s\n", error->message);
422 g_clear_error (&error);
424 if (camel_lock_fcntl (fd2, CAMEL_LOCK_READ, &error) == 0) {
425 printf ("got fcntl read lock twice\n");
426 camel_unlock_fcntl (fd2);
428 printf ("failed to get read lock: %s\n", error->message);
432 g_clear_error (&error);
433 camel_unlock_fcntl (fd1);