X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Fliblutil%2Flockf.c;h=b808e6abdb7d58c04cd99cc204a73109108e198c;hb=c3e28a5488a8011ef0352f48fca85c48679205ba;hp=7307720dd68744af161365b245c2762fb73e092b;hpb=11add79cb131e2ace75337c84c9170035a159aa5;p=openldap diff --git a/libraries/liblutil/lockf.c b/libraries/liblutil/lockf.c index 7307720dd6..b808e6abdb 100644 --- a/libraries/liblutil/lockf.c +++ b/libraries/liblutil/lockf.c @@ -1,39 +1,118 @@ -/* - * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA +/* $OpenLDAP$ */ +/* This work is part of OpenLDAP Software . + * + * Copyright 1998-2007 The OpenLDAP Foundation. * All rights reserved. * - * Redistribution and use in source and binary forms are permitted only - * as authorized by the OpenLDAP Public License. A copy of this - * license is available at http://www.OpenLDAP.org/license.html or - * in file LICENSE in the top-level directory of the distribution. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted only as authorized by the OpenLDAP + * Public License. + * + * A copy of this license is available in the file LICENSE in the + * top-level directory of the distribution or, alternatively, at + * . + */ + +/* + * File Locking Routines + * + * Implementations (in order of preference) + * - lockf + * - fcntl + * - flock + * + * Other implementations will be added as needed. + * + * NOTE: lutil_lockf() MUST block until an exclusive lock is acquired. */ -/* File locking methods */ #include "portable.h" #include #include -#if defined(NEED_FCNTL_LOCKING) +#undef LOCK_API + +#if HAVE_LOCKF && defined(F_LOCK) +# define USE_LOCKF 1 +# define LOCK_API "lockf" +#endif + +#if !defined(LOCK_API) && HAVE_FCNTL +# ifdef HAVE_FCNTL_H +# include +# endif +# ifdef F_WRLCK +# define USE_FCNTL 1 +# define LOCK_API "fcntl" +# endif +#endif + +#if !defined(LOCK_API) && HAVE_FLOCK +# if HAVE_SYS_FILE_H +# include +# endif +# define USE_FLOCK 1 +# define LOCK_API "flock" +#endif + +#if !defined(USE_LOCKF) && !defined(USE_FCNTL) && !defined(USE_FLOCK) +int lutil_lockf ( int fd ) { + fd = fd; + return 0; +} -int lutil_lockf ( FILE *fp ) { +int lutil_unlockf ( int fd ) { + fd = fd; + return 0; +} +#endif + +#ifdef USE_LOCKF +int lutil_lockf ( int fd ) { + /* use F_LOCK instead of F_TLOCK, ie: block */ + return lockf( fd, F_LOCK, 0 ); +} + +int lutil_unlockf ( int fd ) { + return lockf( fd, F_ULOCK, 0 ); +} +#endif + +#ifdef USE_FCNTL +int lutil_lockf ( int fd ) { struct flock file_lock; - memset( &file_lock, 0, sizeof( file_lock ) ); + + memset( &file_lock, '\0', sizeof( file_lock ) ); file_lock.l_type = F_WRLCK; file_lock.l_whence = SEEK_SET; file_lock.l_start = 0; file_lock.l_len = 0; - return( fcntl( fileno(fp), F_SETLKW, &file_lock ) ); + + /* use F_SETLKW instead of F_SETLK, ie: block */ + return( fcntl( fd, F_SETLKW, &file_lock ) ); } -int lutil_unlockf ( FILE *fp ) { +int lutil_unlockf ( int fd ) { struct flock file_lock; - memset( &file_lock, 0, sizeof( file_lock ) ); + + memset( &file_lock, '\0', sizeof( file_lock ) ); file_lock.l_type = F_UNLCK; file_lock.l_whence = SEEK_SET; file_lock.l_start = 0; file_lock.l_len = 0; - return ( fcntl( fileno(fp), F_SETLK, &file_lock ) ); + + return( fcntl ( fd, F_SETLKW, &file_lock ) ); } +#endif -#endif /* !HAVE_FILE_LOCKING */ +#ifdef USE_FLOCK +int lutil_lockf ( int fd ) { + /* use LOCK_EX instead of LOCK_EX|LOCK_NB, ie: block */ + return flock( fd, LOCK_EX ); +} + +int lutil_unlockf ( int fd ) { + return flock( fd, LOCK_UN ); +} +#endif