summaryrefslogtreecommitdiffstats
path: root/libraries/liblutil/lockf.c
blob: fd9eca3283b7c64a87f5668cc9f6bcf1a35633f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 1998-2024 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * 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>.
 */

/*
 * 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.
 */

#include "portable.h"

#include <stdio.h>
#include <ac/unistd.h>

#undef LOCK_API

#if defined(HAVE_LOCKF) && defined(F_LOCK)
#	define USE_LOCKF 1
#	define LOCK_API	"lockf"
#endif

#if !defined(LOCK_API) && defined(HAVE_FCNTL)
#	ifdef HAVE_FCNTL_H
#		include <fcntl.h>
#	endif
#	ifdef F_WRLCK
#		define USE_FCNTL 1
#		define LOCK_API	"fcntl"
#	endif
#endif

#if !defined(LOCK_API) && defined(HAVE_FLOCK)
#	ifdef HAVE_SYS_FILE_H
#		include <sys/file.h>
#	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_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 ) );
	file_lock.l_type = F_WRLCK;
	file_lock.l_whence = SEEK_SET;
	file_lock.l_start = 0;
	file_lock.l_len = 0;

	/* use F_SETLKW instead of F_SETLK, ie: block */
	return( fcntl( fd, F_SETLKW, &file_lock ) );
}

int lutil_unlockf ( int fd ) {
	struct flock 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 ( fd, F_SETLKW, &file_lock ) );
}
#endif

#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