1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2021-01-12 03:25:53 +08:00
|
|
|
* Copyright 1998-2021 The OpenLDAP Foundation.
|
1999-03-14 03:25:15 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2003-11-26 07:17:08 +08:00
|
|
|
* 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
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
1999-03-14 03:25:15 +08:00
|
|
|
*/
|
1999-03-29 05:39:02 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* File Locking Routines
|
|
|
|
*
|
|
|
|
* Implementations (in order of preference)
|
|
|
|
* - lockf
|
|
|
|
* - fcntl
|
|
|
|
* - flock
|
|
|
|
*
|
|
|
|
* Other implementations will be added as needed.
|
1999-03-29 06:43:43 +08:00
|
|
|
*
|
|
|
|
* NOTE: lutil_lockf() MUST block until an exclusive lock is acquired.
|
1999-03-29 05:39:02 +08:00
|
|
|
*/
|
1999-03-14 03:25:15 +08:00
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ac/unistd.h>
|
|
|
|
|
1999-03-29 05:39:02 +08:00
|
|
|
#undef LOCK_API
|
|
|
|
|
2007-05-10 02:17:10 +08:00
|
|
|
#if defined(HAVE_LOCKF) && defined(F_LOCK)
|
1999-03-29 05:39:02 +08:00
|
|
|
# define USE_LOCKF 1
|
|
|
|
# define LOCK_API "lockf"
|
|
|
|
#endif
|
|
|
|
|
2007-05-10 02:17:10 +08:00
|
|
|
#if !defined(LOCK_API) && defined(HAVE_FCNTL)
|
1999-03-29 05:39:02 +08:00
|
|
|
# ifdef HAVE_FCNTL_H
|
|
|
|
# include <fcntl.h>
|
|
|
|
# endif
|
|
|
|
# ifdef F_WRLCK
|
|
|
|
# define USE_FCNTL 1
|
|
|
|
# define LOCK_API "fcntl"
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2007-05-10 02:17:10 +08:00
|
|
|
#if !defined(LOCK_API) && defined(HAVE_FLOCK)
|
|
|
|
# ifdef HAVE_SYS_FILE_H
|
1999-03-29 05:39:02 +08:00
|
|
|
# include <sys/file.h>
|
|
|
|
# endif
|
|
|
|
# define USE_FLOCK 1
|
|
|
|
# define LOCK_API "flock"
|
|
|
|
#endif
|
1999-03-14 03:25:15 +08:00
|
|
|
|
1999-04-01 23:51:11 +08:00
|
|
|
#if !defined(USE_LOCKF) && !defined(USE_FCNTL) && !defined(USE_FLOCK)
|
|
|
|
int lutil_lockf ( int fd ) {
|
|
|
|
fd = fd;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int lutil_unlockf ( int fd ) {
|
|
|
|
fd = fd;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-03-29 05:39:02 +08:00
|
|
|
#ifdef USE_LOCKF
|
|
|
|
int lutil_lockf ( int fd ) {
|
1999-03-29 06:43:43 +08:00
|
|
|
/* use F_LOCK instead of F_TLOCK, ie: block */
|
1999-03-29 05:39:02 +08:00
|
|
|
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 ) {
|
1999-03-14 03:25:15 +08:00
|
|
|
struct flock file_lock;
|
1999-03-29 06:43:43 +08:00
|
|
|
|
2000-06-11 06:39:30 +08:00
|
|
|
memset( &file_lock, '\0', sizeof( file_lock ) );
|
1999-03-14 03:25:15 +08:00
|
|
|
file_lock.l_type = F_WRLCK;
|
|
|
|
file_lock.l_whence = SEEK_SET;
|
|
|
|
file_lock.l_start = 0;
|
|
|
|
file_lock.l_len = 0;
|
1999-03-29 06:43:43 +08:00
|
|
|
|
|
|
|
/* use F_SETLKW instead of F_SETLK, ie: block */
|
1999-03-29 05:39:02 +08:00
|
|
|
return( fcntl( fd, F_SETLKW, &file_lock ) );
|
1999-03-14 03:25:15 +08:00
|
|
|
}
|
|
|
|
|
1999-03-29 05:39:02 +08:00
|
|
|
int lutil_unlockf ( int fd ) {
|
1999-03-14 03:25:15 +08:00
|
|
|
struct flock file_lock;
|
1999-03-29 06:43:43 +08:00
|
|
|
|
2000-06-11 06:39:30 +08:00
|
|
|
memset( &file_lock, '\0', sizeof( file_lock ) );
|
1999-03-14 03:25:15 +08:00
|
|
|
file_lock.l_type = F_UNLCK;
|
|
|
|
file_lock.l_whence = SEEK_SET;
|
|
|
|
file_lock.l_start = 0;
|
|
|
|
file_lock.l_len = 0;
|
1999-03-29 06:43:43 +08:00
|
|
|
|
|
|
|
return( fcntl ( fd, F_SETLKW, &file_lock ) );
|
1999-03-14 03:25:15 +08:00
|
|
|
}
|
1999-03-29 05:39:02 +08:00
|
|
|
#endif
|
1999-03-14 03:25:15 +08:00
|
|
|
|
1999-03-29 05:39:02 +08:00
|
|
|
#ifdef USE_FLOCK
|
|
|
|
int lutil_lockf ( int fd ) {
|
1999-03-29 06:43:43 +08:00
|
|
|
/* use LOCK_EX instead of LOCK_EX|LOCK_NB, ie: block */
|
1999-03-29 05:39:02 +08:00
|
|
|
return flock( fd, LOCK_EX );
|
|
|
|
}
|
|
|
|
|
|
|
|
int lutil_unlockf ( int fd ) {
|
|
|
|
return flock( fd, LOCK_UN );
|
|
|
|
}
|
|
|
|
#endif
|