3 * Copyright 1998-2000 The OpenLDAP Foundation, Redwood City, California, USA
6 * Redistribution and use in source and binary forms are permitted only
7 * as authorized by the OpenLDAP Public License. A copy of this
8 * license is available at http://www.OpenLDAP.org/license.html or
9 * in file LICENSE in the top-level directory of the distribution.
13 * File Locking Routines
15 * Implementations (in order of preference)
20 * Other implementations will be added as needed.
22 * NOTE: lutil_lockf() MUST block until an exclusive lock is acquired.
28 #include <ac/unistd.h>
32 #if HAVE_LOCKF && defined(F_LOCK)
34 # define LOCK_API "lockf"
37 #if !defined(LOCK_API) && HAVE_FCNTL
43 # define LOCK_API "fcntl"
47 #if !defined(LOCK_API) && HAVE_FLOCK
49 # include <sys/file.h>
52 # define LOCK_API "flock"
55 #if !defined(USE_LOCKF) && !defined(USE_FCNTL) && !defined(USE_FLOCK)
56 int lutil_lockf ( int fd ) {
61 int lutil_unlockf ( int fd ) {
68 int lutil_lockf ( int fd ) {
69 /* use F_LOCK instead of F_TLOCK, ie: block */
70 return lockf( fd, F_LOCK, 0 );
73 int lutil_unlockf ( int fd ) {
74 return lockf( fd, F_ULOCK, 0 );
79 int lutil_lockf ( int fd ) {
80 struct flock file_lock;
82 memset( &file_lock, '\0', sizeof( file_lock ) );
83 file_lock.l_type = F_WRLCK;
84 file_lock.l_whence = SEEK_SET;
85 file_lock.l_start = 0;
88 /* use F_SETLKW instead of F_SETLK, ie: block */
89 return( fcntl( fd, F_SETLKW, &file_lock ) );
92 int lutil_unlockf ( int fd ) {
93 struct flock file_lock;
95 memset( &file_lock, '\0', sizeof( file_lock ) );
96 file_lock.l_type = F_UNLCK;
97 file_lock.l_whence = SEEK_SET;
98 file_lock.l_start = 0;
101 return( fcntl ( fd, F_SETLKW, &file_lock ) );
106 int lutil_lockf ( int fd ) {
107 /* use LOCK_EX instead of LOCK_EX|LOCK_NB, ie: block */
108 return flock( fd, LOCK_EX );
111 int lutil_unlockf ( int fd ) {
112 return flock( fd, LOCK_UN );