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"
54 #if !defined(USE_LOCKF) && !defined(USE_FCNTL) && !defined(USE_FLOCK)
55 int lutil_lockf ( int fd ) {
60 int lutil_unlockf ( int fd ) {
67 int lutil_lockf ( int fd ) {
68 /* use F_LOCK instead of F_TLOCK, ie: block */
69 return lockf( fd, F_LOCK, 0 );
72 int lutil_unlockf ( int fd ) {
73 return lockf( fd, F_ULOCK, 0 );
78 int lutil_lockf ( int fd ) {
79 struct flock file_lock;
81 memset( &file_lock, 0, sizeof( file_lock ) );
82 file_lock.l_type = F_WRLCK;
83 file_lock.l_whence = SEEK_SET;
84 file_lock.l_start = 0;
87 /* use F_SETLKW instead of F_SETLK, ie: block */
88 return( fcntl( fd, F_SETLKW, &file_lock ) );
91 int lutil_unlockf ( int fd ) {
92 struct flock file_lock;
94 memset( &file_lock, 0, sizeof( file_lock ) );
95 file_lock.l_type = F_UNLCK;
96 file_lock.l_whence = SEEK_SET;
97 file_lock.l_start = 0;
100 return( fcntl ( fd, F_SETLKW, &file_lock ) );
105 int lutil_lockf ( int fd ) {
106 /* use LOCK_EX instead of LOCK_EX|LOCK_NB, ie: block */
107 return flock( fd, LOCK_EX );
110 int lutil_unlockf ( int fd ) {
111 return flock( fd, LOCK_UN );