c10599683681a8068ff14bfab5a4f1bcd5fa4c3e
[debian/amanda] / common-src / amflock.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: amflock.c 7161 2007-07-03 16:27:26Z dustin $
28  *
29  * file locking routines, put here to hide the system dependant stuff
30  * from the rest of the code
31  */
32 /*
33 **
34 ** Notes:
35 ** - These are "best effort" routines.
36 ** - "configure" has four variables that are used to determine which type of
37 **   locking to use:
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!
44 */
45
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 
54  *   exclusive lock.
55  * - Arbitrary strings should be useable as resource names. */
56
57 #include "amanda.h"
58 /* Interface to the implementations in common-src/amflock-*.c */
59
60 #ifdef WANT_AMFLOCK_POSIX
61 extern amflock_impl_t amflock_posix_impl;
62 #endif
63 #ifdef WANT_AMFLOCK_FLOCK
64 extern amflock_impl_t amflock_flock_impl;
65 #endif
66 #ifdef WANT_AMFLOCK_LOCKF
67 extern amflock_impl_t amflock_lockf_impl;
68 #endif
69 #ifdef WANT_AMFLOCK_LNLOCK
70 extern amflock_impl_t amflock_lnlock_impl;
71 #endif
72
73 amflock_impl_t *amflock_impls[] = {
74 #ifdef WANT_AMFLOCK_POSIX
75     &amflock_posix_impl,
76 #endif
77 #ifdef WANT_AMFLOCK_FLOCK
78     &amflock_flock_impl,
79 #endif
80 #ifdef WANT_AMFLOCK_LOCKF
81     &amflock_lockf_impl,
82 #endif
83 #ifdef WANT_AMFLOCK_LNLOCK
84     &amflock_lnlock_impl,
85 #endif
86     NULL
87 };
88
89 /* Interface functions */
90 /* FIXME: for now, these just use the first non-NULL implementation
91  */
92
93 /* Get a file lock (for read/write files).
94 */
95 int
96 amflock(
97     int         fd,
98     char *      resource)
99 {
100     if (!amflock_impls[0]) return 0; /* no locking */
101     return amflock_impls[0]->amflock_impl(fd, resource);
102 }
103
104 /*
105  * Get a file lock (for read-only files).
106  */
107 int
108 amroflock(
109     int         fd,
110     char *      resource)
111 {
112     if (!amflock_impls[0]) return 0; /* no locking */
113     return amflock_impls[0]->amroflock_impl(fd, resource);
114 }
115
116 /*
117  * Release a file lock.
118  */
119 int
120 amfunlock(
121     int         fd,
122     char *      resource)
123 {
124     if (!amflock_impls[0]) return 0; /* no locking */
125     return amflock_impls[0]->amfunlock_impl(fd, resource);
126 }