Back to index

openldap  2.4.31
lockf.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 /*
00017  * File Locking Routines
00018  *
00019  * Implementations (in order of preference)
00020  *     - lockf
00021  *     - fcntl
00022  *  - flock
00023  *
00024  * Other implementations will be added as needed.
00025  *
00026  * NOTE: lutil_lockf() MUST block until an exclusive lock is acquired.
00027  */
00028 
00029 #include "portable.h"
00030 
00031 #include <stdio.h>
00032 #include <ac/unistd.h>
00033 
00034 #undef LOCK_API
00035 
00036 #if defined(HAVE_LOCKF) && defined(F_LOCK)
00037 #      define USE_LOCKF 1
00038 #      define LOCK_API      "lockf"
00039 #endif
00040 
00041 #if !defined(LOCK_API) && defined(HAVE_FCNTL)
00042 #      ifdef HAVE_FCNTL_H
00043 #             include <fcntl.h>
00044 #      endif
00045 #      ifdef F_WRLCK
00046 #             define USE_FCNTL 1
00047 #             define LOCK_API      "fcntl"
00048 #      endif
00049 #endif
00050 
00051 #if !defined(LOCK_API) && defined(HAVE_FLOCK)
00052 #      ifdef HAVE_SYS_FILE_H
00053 #             include <sys/file.h>
00054 #      endif
00055 #      define USE_FLOCK 1
00056 #      define LOCK_API      "flock"
00057 #endif
00058 
00059 #if !defined(USE_LOCKF) && !defined(USE_FCNTL) && !defined(USE_FLOCK)
00060 int lutil_lockf ( int fd ) {
00061     fd = fd;
00062     return 0;
00063 }
00064 
00065 int lutil_unlockf ( int fd ) {
00066     fd = fd;
00067     return 0;
00068 }
00069 #endif
00070 
00071 #ifdef USE_LOCKF
00072 int lutil_lockf ( int fd ) {
00073        /* use F_LOCK instead of F_TLOCK, ie: block */
00074        return lockf( fd, F_LOCK, 0 );
00075 }
00076 
00077 int lutil_unlockf ( int fd ) {
00078        return lockf( fd, F_ULOCK, 0 );
00079 }
00080 #endif
00081 
00082 #ifdef USE_FCNTL
00083 int lutil_lockf ( int fd ) {
00084        struct flock file_lock;
00085 
00086        memset( &file_lock, '\0', sizeof( file_lock ) );
00087        file_lock.l_type = F_WRLCK;
00088        file_lock.l_whence = SEEK_SET;
00089        file_lock.l_start = 0;
00090        file_lock.l_len = 0;
00091 
00092        /* use F_SETLKW instead of F_SETLK, ie: block */
00093        return( fcntl( fd, F_SETLKW, &file_lock ) );
00094 }
00095 
00096 int lutil_unlockf ( int fd ) {
00097        struct flock file_lock;
00098 
00099        memset( &file_lock, '\0', sizeof( file_lock ) );
00100        file_lock.l_type = F_UNLCK;
00101        file_lock.l_whence = SEEK_SET;
00102        file_lock.l_start = 0;
00103        file_lock.l_len = 0;
00104 
00105        return( fcntl ( fd, F_SETLKW, &file_lock ) );
00106 }
00107 #endif
00108 
00109 #ifdef USE_FLOCK
00110 int lutil_lockf ( int fd ) {
00111        /* use LOCK_EX instead of LOCK_EX|LOCK_NB, ie: block */
00112        return flock( fd, LOCK_EX );
00113 }
00114 
00115 int lutil_unlockf ( int fd ) {
00116        return flock( fd, LOCK_UN );
00117 }
00118 #endif