Imported Upstream version 2.6.1
[debian/amanda] / config / amanda / flock.m4
1 # SYNOPSIS
2 #
3 #   AMANDA_SETUP_FILE_LOCKING
4 #
5 # OVERVIEW
6 #
7 #   Set up file locking support.  Four locking mechanisms are available:
8 #     POSIX_FCNTL - use fcntl().  The full job.
9 #     FLOCK       - use flock().  Does just as well.
10 #     LOCKF       - use lockf().  Only handles advisory, exclusive,
11 #                       blocking file locks as used by Amanda.
12 #     LNLOCK      - Home brew exclusive, blocking file lock.
13 #
14 #   For the chosen method, WANT_AMFLOCK_mech is defined and set up as an
15 #   AM_CONDITIONAL.  Also, LOCKING, which contains the mechanism, is substituted.
16 #
17 AC_DEFUN([AMANDA_SETUP_FILE_LOCKING],
18 [
19     AC_CHECK_HEADERS(
20         fcntl.h \
21         sys/fcntl.h \
22         sys/types.h \
23         sys/file.h \
24         unistd.h \
25     )
26
27     # find a working file-locking mechanism.
28     # Note: these all use AC_TRY_LINK to make sure that we can compile
29     # and link each variant.  They do not try to test the variants --
30     # that is left to runtime.
31     LOCKING="no"
32
33     # check POSIX locking
34     AC_CACHE_CHECK(
35        [whether POSIX locking (with fcntl(2)) is available],
36        amanda_cv_posix_filelocking,
37        [
38             AC_TRY_LINK([
39 #if HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
42 #if HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45 #if HAVE_FCNTL_H
46 # include <fcntl.h>
47 #endif
48             ], [
49             struct flock lock;
50
51             lock.l_type = F_RDLCK;
52             lock.l_start = 0;
53             lock.l_whence = SEEK_CUR;
54             lock.l_len = 0;
55             return fcntl(1, F_SETLK, &lock);
56             ], [
57         amanda_cv_posix_filelocking="yes"
58             ],[
59         amanda_cv_posix_filelocking="no"
60             ])
61         ])
62     if test "x$amanda_cv_posix_filelocking" = xyes; then
63         AC_DEFINE(WANT_AMFLOCK_POSIX,1,[Define to use POSIX (fcntl()) for file locking])
64         WANT_AMFLOCK_POSIX="yes"
65         LOCKING="POSIX_FCNTL"
66     fi
67     AM_CONDITIONAL(WANT_AMFLOCK_POSIX, test x"$WANT_AMFLOCK_POSIX" = x"yes")
68
69     # check flock-based (BSD) locking
70     AC_CACHE_CHECK(
71        [whether flock locking is available],
72        amanda_cv_flock_filelocking,
73        [
74             AC_TRY_LINK([
75 #if HAVE_SYS_FILE_H
76 # include <sys/file.h>
77 #endif
78             ], [
79             return flock(1, LOCK_SH);
80             ], [
81         amanda_cv_flock_filelocking="yes"
82             ],[
83         amanda_cv_flock_filelocking="no"
84             ])
85         ])
86     if test "x$amanda_cv_flock_filelocking" = xyes; then
87         AC_DEFINE(WANT_AMFLOCK_FLOCK,1,[Define to use flock(2) for file locking])
88         WANT_AMFLOCK_FLOCK="yes"
89         LOCKING="FLOCK"
90     fi
91     AM_CONDITIONAL(WANT_AMFLOCK_FLOCK, test x"$WANT_AMFLOCK_FLOCK" = x"yes")
92
93     # check lockf-based (SVR2, SVR3, SVR4) locking
94     AC_CACHE_CHECK(
95        [whether lockf(3) locking is available],
96        amanda_cv_lockf_filelocking,
97        [
98             AC_TRY_LINK([
99 #if HAVE_UNISTD_H
100 # include <unistd.h>
101 #endif
102             ], [
103             return lockf(1, F_LOCK, 0);
104             ], [
105         amanda_cv_lockf_filelocking="yes"
106             ],[
107         amanda_cv_lockf_filelocking="no"
108             ])
109         ])
110     if test "x$amanda_cv_lockf_filelocking" = xyes; then
111         AC_DEFINE(WANT_AMFLOCK_LOCKF,1,[Define to use lockf(3) for file locking.])
112         WANT_AMFLOCK_LOCKF="yes"
113         LOCKING="LOCKF"
114     fi
115     AM_CONDITIONAL(WANT_AMFLOCK_LOCKF, test x"$WANT_AMFLOCK_LOCKF" = x"yes")
116
117     # check our homebrew hardlink-based locking (requires hardlinks)
118     AC_CACHE_CHECK(
119        [whether link(2) is available for locking],
120        amanda_cv_lnlock_filelocking,
121        [
122             AC_TRY_LINK([
123 #if HAVE_UNISTD_H
124 # include <unistd.h>
125 #endif
126             ], [
127             return link("/tmp/foo", "/tmp/bar");
128             ], [
129         amanda_cv_lnlock_filelocking="yes"
130             ],[
131         amanda_cv_lnlock_filelocking="no"
132             ])
133         ])
134     if test "x$amanda_cv_lnlock_filelocking" = xyes; then
135         AC_DEFINE(WANT_AMFLOCK_LNLOCK,1,[Define to use link(2) to emulate file locking.])
136         WANT_AMFLOCK_LNLOCK="yes"
137         LOCKING="LNLOCK"
138     fi
139     AM_CONDITIONAL(WANT_AMFLOCK_LNLOCK, test x"$WANT_AMFLOCK_LNLOCK" = x"yes")
140
141     if test x"$LOCKING" = "no"; then
142         # this shouldn't happen, and is *bad* if it does
143         AC_MSG_ERROR([*** No working file locking capability found!])
144     fi
145
146     AC_SUBST(LOCKING)
147 ])