2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: amflock.c 7161 2007-07-03 16:27:26Z dustin $
29 * file locking routines, put here to hide the system dependant stuff
30 * from the rest of the code
35 ** - These are "best effort" routines.
36 ** - "configure" has four variables that are used to determine which type of
38 ** USE_POSIX_FCNTL - use fcntl(). The full job.
39 ** USE_FLOCK - use flock(). Does just as well.
40 ** USE_LOCKF - use lockf(). Only handles advisory, exclusive,
41 ** blocking file locks as used by Amanda.
42 ** USE_LNLOCK - Home brew exclusive, blocking file lock.
43 ** <none> - No locking available. User beware!
46 /* FIXME: This code has several limitations to be fixed:
47 * - It should be possible to select a locking mode (or detect the
48 * best mode for a particular filesystem) at runtime.
49 * - There should be a locking mode that works with NFS filesystems.
50 * - Semantics should be clear when different parts of a single
51 * process (possibly in the same/different threads) both try to lock
52 * the same file (but with different file descriptors).
53 * - It should be possible to promote a read-only lock to an
55 * - Arbitrary strings should be useable as resource names. */
58 /* Interface to the implementations in common-src/amflock-*.c */
60 #ifdef WANT_AMFLOCK_POSIX
61 extern amflock_impl_t amflock_posix_impl;
63 #ifdef WANT_AMFLOCK_FLOCK
64 extern amflock_impl_t amflock_flock_impl;
66 #ifdef WANT_AMFLOCK_LOCKF
67 extern amflock_impl_t amflock_lockf_impl;
69 #ifdef WANT_AMFLOCK_LNLOCK
70 extern amflock_impl_t amflock_lnlock_impl;
73 amflock_impl_t *amflock_impls[] = {
74 #ifdef WANT_AMFLOCK_POSIX
77 #ifdef WANT_AMFLOCK_FLOCK
80 #ifdef WANT_AMFLOCK_LOCKF
83 #ifdef WANT_AMFLOCK_LNLOCK
89 /* Interface functions */
90 /* FIXME: for now, these just use the first non-NULL implementation
93 /* Get a file lock (for read/write files).
100 if (!amflock_impls[0]) return 0; /* no locking */
101 return amflock_impls[0]->amflock_impl(fd, resource);
105 * Get a file lock (for read-only files).
112 if (!amflock_impls[0]) return 0; /* no locking */
113 return amflock_impls[0]->amroflock_impl(fd, resource);
117 * Release a file lock.
124 if (!amflock_impls[0]) return 0; /* no locking */
125 return amflock_impls[0]->amfunlock_impl(fd, resource);