home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ldapsdk.zip / libraries / liblutil / lockf.c < prev    next >
C/C++ Source or Header  |  2000-06-14  |  2KB  |  115 lines

  1. /* $OpenLDAP: pkg/ldap/libraries/liblutil/lockf.c,v 1.6.8.2 2000/06/13 17:57:25 kurt Exp $ */
  2. /*
  3.  * Copyright 1998-2000 The OpenLDAP Foundation, Redwood City, California, USA
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms are permitted only
  7.  * as authorized by the OpenLDAP Public License.  A copy of this
  8.  * license is available at http://www.OpenLDAP.org/license.html or
  9.  * in file LICENSE in the top-level directory of the distribution.
  10.  */
  11.  
  12. /*
  13.  * File Locking Routines
  14.  *
  15.  * Implementations (in order of preference)
  16.  *    - lockf
  17.  *    - fcntl
  18.  *  - flock
  19.  *
  20.  * Other implementations will be added as needed.
  21.  *
  22.  * NOTE: lutil_lockf() MUST block until an exclusive lock is acquired.
  23.  */
  24.  
  25. #include "portable.h"
  26.  
  27. #include <stdio.h>
  28. #include <ac/unistd.h>
  29.  
  30. #undef LOCK_API
  31.  
  32. #if HAVE_LOCKF && defined(F_LOCK)
  33. #    define USE_LOCKF 1
  34. #    define LOCK_API    "lockf"
  35. #endif
  36.  
  37. #if !defined(LOCK_API) && HAVE_FCNTL
  38. #    ifdef HAVE_FCNTL_H
  39. #        include <fcntl.h>
  40. #    endif
  41. #    ifdef F_WRLCK
  42. #        define USE_FCNTL 1
  43. #        define LOCK_API    "fcntl"
  44. #    endif
  45. #endif
  46.  
  47. #if !defined(LOCK_API) && HAVE_FLOCK
  48. #    if HAVE_SYS_FILE_H
  49. #        include <sys/file.h>
  50. #    endif
  51. #    define USE_FLOCK 1
  52. #    define LOCK_API    "flock"
  53. #endif
  54.  
  55. #if !defined(USE_LOCKF) && !defined(USE_FCNTL) && !defined(USE_FLOCK)
  56. int lutil_lockf ( int fd ) {
  57.     fd = fd;
  58.     return 0;
  59. }
  60.  
  61. int lutil_unlockf ( int fd ) {
  62.     fd = fd;
  63.     return 0;
  64. }
  65. #endif
  66.  
  67. #ifdef USE_LOCKF
  68. int lutil_lockf ( int fd ) {
  69.     /* use F_LOCK instead of F_TLOCK, ie: block */
  70.     return lockf( fd, F_LOCK, 0 );
  71. }
  72.  
  73. int lutil_unlockf ( int fd ) {
  74.     return lockf( fd, F_ULOCK, 0 );
  75. }
  76. #endif
  77.  
  78. #ifdef USE_FCNTL
  79. int lutil_lockf ( int fd ) {
  80.     struct flock file_lock;
  81.  
  82.     memset( &file_lock, '\0', sizeof( file_lock ) );
  83.     file_lock.l_type = F_WRLCK;
  84.     file_lock.l_whence = SEEK_SET;
  85.     file_lock.l_start = 0;
  86.     file_lock.l_len = 0;
  87.  
  88.     /* use F_SETLKW instead of F_SETLK, ie: block */
  89.     return( fcntl( fd, F_SETLKW, &file_lock ) );
  90. }
  91.  
  92. int lutil_unlockf ( int fd ) {
  93.     struct flock file_lock;
  94.  
  95.     memset( &file_lock, '\0', sizeof( file_lock ) );
  96.     file_lock.l_type = F_UNLCK;
  97.     file_lock.l_whence = SEEK_SET;
  98.     file_lock.l_start = 0;
  99.     file_lock.l_len = 0;
  100.  
  101.     return( fcntl ( fd, F_SETLKW, &file_lock ) );
  102. }
  103. #endif
  104.  
  105. #ifdef USE_FLOCK
  106. int lutil_lockf ( int fd ) {
  107.     /* use LOCK_EX instead of LOCK_EX|LOCK_NB, ie: block */
  108.     return flock( fd, LOCK_EX );
  109. }
  110.  
  111. int lutil_unlockf ( int fd ) {
  112.     return flock( fd, LOCK_UN );
  113. }
  114. #endif
  115.