]> git.sur5r.net Git - openldap/blob - libraries/liblutil/lockf.c
Rebuild configure due to MDBM addition.
[openldap] / libraries / liblutil / lockf.c
1 /*
2  * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted only
6  * as authorized by the OpenLDAP Public License.  A copy of this
7  * license is available at http://www.OpenLDAP.org/license.html or
8  * in file LICENSE in the top-level directory of the distribution.
9  */
10
11 /*
12  * File Locking Routines
13  *
14  * Implementations (in order of preference)
15  *      - lockf
16  *      - fcntl
17  *  - flock
18  *
19  * Other implementations will be added as needed.
20  *
21  * NOTE: lutil_lockf() MUST block until an exclusive lock is acquired.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27 #include <ac/unistd.h>
28
29 #undef LOCK_API
30
31 #if HAVE_LOCKF && defined(F_LOCK)
32 #       define USE_LOCKF 1
33 #       define LOCK_API "lockf"
34 #endif
35
36 #if !defined(LOCK_API) && HAVE_FCNTL
37 #       ifdef HAVE_FCNTL_H
38 #               include <fcntl.h>
39 #       endif
40 #       ifdef F_WRLCK
41 #               define USE_FCNTL 1
42 #               define LOCK_API "fcntl"
43 #       endif
44 #endif
45
46 #if !defined(LOCK_API) && HAVE_FLOCK
47 #       if HAVE_SYS_FILE_H
48 #               include <sys/file.h>
49 #       endif
50 #       define USE_FLOCK 1
51 #       define LOCK_API "flock"
52 #endif
53
54 #ifdef USE_LOCKF
55 int lutil_lockf ( int fd ) {
56         /* use F_LOCK instead of F_TLOCK, ie: block */
57         return lockf( fd, F_LOCK, 0 );
58 }
59
60 int lutil_unlockf ( int fd ) {
61         return lockf( fd, F_ULOCK, 0 );
62 }
63 #endif
64
65 #ifdef USE_FCNTL
66 int lutil_lockf ( int fd ) {
67         struct flock file_lock;
68
69         memset( &file_lock, 0, sizeof( file_lock ) );
70         file_lock.l_type = F_WRLCK;
71         file_lock.l_whence = SEEK_SET;
72         file_lock.l_start = 0;
73         file_lock.l_len = 0;
74
75         /* use F_SETLKW instead of F_SETLK, ie: block */
76         return( fcntl( fd, F_SETLKW, &file_lock ) );
77 }
78
79 int lutil_unlockf ( int fd ) {
80         struct flock file_lock;
81
82         memset( &file_lock, 0, sizeof( file_lock ) );
83         file_lock.l_type = F_UNLCK;
84         file_lock.l_whence = SEEK_SET;
85         file_lock.l_start = 0;
86         file_lock.l_len = 0;
87
88         return( fcntl ( fd, F_SETLKW, &file_lock ) );
89 }
90 #endif
91
92 #ifdef USE_FLOCK
93 int lutil_lockf ( int fd ) {
94         /* use LOCK_EX instead of LOCK_EX|LOCK_NB, ie: block */
95         return flock( fd, LOCK_EX );
96 }
97
98 int lutil_unlockf ( int fd ) {
99         return flock( fd, LOCK_UN );
100 }
101 #endif