2 * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA
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.
12 * File Locking Routines
14 * Implementations (in order of preference)
19 * Other implementations will be added as needed.
21 * NOTE: lutil_lockf() MUST block until an exclusive lock is acquired.
27 #include <ac/unistd.h>
31 #if HAVE_LOCKF && defined(F_LOCK)
33 # define LOCK_API "lockf"
36 #if !defined(LOCK_API) && HAVE_FCNTL
42 # define LOCK_API "fcntl"
46 #if !defined(LOCK_API) && HAVE_FLOCK
48 # include <sys/file.h>
51 # define LOCK_API "flock"
55 int lutil_lockf ( int fd ) {
56 /* use F_LOCK instead of F_TLOCK, ie: block */
57 return lockf( fd, F_LOCK, 0 );
60 int lutil_unlockf ( int fd ) {
61 return lockf( fd, F_ULOCK, 0 );
66 int lutil_lockf ( int fd ) {
67 struct flock file_lock;
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;
75 /* use F_SETLKW instead of F_SETLK, ie: block */
76 return( fcntl( fd, F_SETLKW, &file_lock ) );
79 int lutil_unlockf ( int fd ) {
80 struct flock file_lock;
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;
88 return( fcntl ( fd, F_SETLKW, &file_lock ) );
93 int lutil_lockf ( int fd ) {
94 /* use LOCK_EX instead of LOCK_EX|LOCK_NB, ie: block */
95 return flock( fd, LOCK_EX );
98 int lutil_unlockf ( int fd ) {
99 return flock( fd, LOCK_UN );