]> git.sur5r.net Git - openldap/blob - libraries/liblutil/lockf.c
fix flag on formatMessage to not allocate buffer
[openldap] / libraries / liblutil / lockf.c
1 /*
2  * Copyright 1998,1999 The OpenLDAP Foundation, Redwood City, California, USA
3  * All rights reserved.
4  *
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.
9  */
10
11 /*
12  * File Locking Routines
13  *
14  * Implementations (in order of preference)
15  *      - lockf
16  *      - fcntl
17  *  - flock
18  *
19  * Other implementations will be added as needed.
20  *
21  * NOTE: lutil_lockf() MUST block until an exclusive lock is acquired.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27 #include <ac/unistd.h>
28
29 #undef LOCK_API
30
31 #if HAVE_LOCKF && defined(F_LOCK)
32 #       define USE_LOCKF 1
33 #       define LOCK_API "lockf"
34 #endif
35
36 #if !defined(LOCK_API) && HAVE_FCNTL
37 #       ifdef HAVE_FCNTL_H
38 #               include <fcntl.h>
39 #       endif
40 #       ifdef F_WRLCK
41 #               define USE_FCNTL 1
42 #               define LOCK_API "fcntl"
43 #       endif
44 #endif
45
46 #if !defined(LOCK_API) && HAVE_FLOCK
47 #       if HAVE_SYS_FILE_H
48 #               include <sys/file.h>
49 #       endif
50 #       define USE_FLOCK 1
51 #       define LOCK_API "flock"
52 #endif
53
54 #if !defined(USE_LOCKF) && !defined(USE_FCNTL) && !defined(USE_FLOCK)
55 int lutil_lockf ( int fd ) {
56     fd = fd;
57     return 0;
58 }
59
60 int lutil_unlockf ( int fd ) {
61     fd = fd;
62     return 0;
63 }
64 #endif
65
66 #ifdef USE_LOCKF
67 int lutil_lockf ( int fd ) {
68         /* use F_LOCK instead of F_TLOCK, ie: block */
69         return lockf( fd, F_LOCK, 0 );
70 }
71
72 int lutil_unlockf ( int fd ) {
73         return lockf( fd, F_ULOCK, 0 );
74 }
75 #endif
76
77 #ifdef USE_FCNTL
78 int lutil_lockf ( int fd ) {
79         struct flock file_lock;
80
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;
85         file_lock.l_len = 0;
86
87         /* use F_SETLKW instead of F_SETLK, ie: block */
88         return( fcntl( fd, F_SETLKW, &file_lock ) );
89 }
90
91 int lutil_unlockf ( int fd ) {
92         struct flock file_lock;
93
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;
98         file_lock.l_len = 0;
99
100         return( fcntl ( fd, F_SETLKW, &file_lock ) );
101 }
102 #endif
103
104 #ifdef USE_FLOCK
105 int lutil_lockf ( int fd ) {
106         /* use LOCK_EX instead of LOCK_EX|LOCK_NB, ie: block */
107         return flock( fd, LOCK_EX );
108 }
109
110 int lutil_unlockf ( int fd ) {
111         return flock( fd, LOCK_UN );
112 }
113 #endif