forked from zmanda/amanda
/
amflock.c
301 lines (256 loc) · 6.93 KB
/
amflock.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Amanda, The Advanced Maryland Automatic Network Disk Archiver
* Copyright (c) 1991-1998 University of Maryland at College Park
* All Rights Reserved.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of U.M. not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. U.M. makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Authors: the Amanda Development Team. Its members are listed in a
* file named AUTHORS, in the root directory of this distribution.
*/
/*
* $Id: amflock.c 7161 2007-07-03 16:27:26Z dustin $
*
* file locking routines, put here to hide the system dependant stuff
* from the rest of the code
*/
#include "amanda.h"
/*
* New Implementation
*/
static GStaticMutex lock_lock = G_STATIC_MUTEX_INIT;
static GHashTable *locally_locked_files = NULL;
file_lock *
file_lock_new(
const char *filename)
{
file_lock *lock = g_new0(file_lock, 1);
lock->filename = g_strdup(filename);
lock->fd = -1;
return lock;
}
void
file_lock_free(
file_lock *lock)
{
g_static_mutex_lock(&lock_lock);
if (locally_locked_files) {
g_hash_table_remove(locally_locked_files,
lock->filename);
}
if (lock->data)
g_free(lock->data);
if (lock->filename)
g_free(lock->filename);
if (lock->fd != -1)
close(lock->fd);
g_static_mutex_unlock(&lock_lock);
}
int
file_lock_lock(
file_lock *lock)
{
int rv = -2;
int fd = -1;
int saved_errno;
struct flock lock_buf;
struct stat stat_buf;
g_assert(!lock->locked);
/* protect from overlapping lock operations within a process */
g_static_mutex_lock(&lock_lock);
if (!locally_locked_files) {
locally_locked_files = g_hash_table_new(g_str_hash, g_str_equal);
}
/* if this filename is in the hash table, then some other thread in this
* process has locked it */
if (g_hash_table_lookup(locally_locked_files, lock->filename)) {
rv = 1;
goto done;
}
/* The locks are advisory, so an error here never means the lock is already
* taken. */
lock->fd = fd = open(lock->filename, O_CREAT|O_RDWR, 0666);
if (fd < 0) {
rv = -1;
goto done;
}
/* now try locking it */
lock_buf.l_type = F_WRLCK;
lock_buf.l_start = 0;
lock_buf.l_whence = SEEK_SET;
lock_buf.l_len = 0; /* to EOF */
if (fcntl(fd, F_SETLK, &lock_buf) < 0) {
if (errno == EACCES || errno == EAGAIN)
rv = 1;
else
rv = -1;
goto done;
}
/* and read the file in its entirety */
if (fstat(fd, &stat_buf) < 0) {
rv = -1;
goto done;
}
if (!(stat_buf.st_mode & S_IFREG)) {
rv = -1;
errno = EINVAL;
goto done;
}
if (stat_buf.st_size) {
lock->data = g_malloc(stat_buf.st_size);
lock->len = stat_buf.st_size;
if (full_read(fd, lock->data, lock->len) < lock->len) {
rv = -1;
goto done;
}
}
fd = -1; /* we'll keep the file now */
lock->locked = TRUE;
/* the lock is acquired; record this in the hash table */
g_hash_table_insert(locally_locked_files, lock->filename, lock->filename);
rv = 0;
done:
saved_errno = errno;
g_static_mutex_unlock(&lock_lock);
if (fd >= 0) /* close and unlock if an error occurred */
close(fd);
errno = saved_errno;
return rv;
}
int
file_lock_write(
file_lock *lock,
const char *data,
size_t len)
{
int fd = lock->fd;
g_assert(lock->locked);
/* seek to position 0, rewrite, and truncate */
if (lseek(fd, 0, SEEK_SET) < 0)
return -1;
/* from here on out, any errors have corrupted the datafile.. */
if (full_write(fd, data, len) < len)
return -1;
if (lock->len > len) {
if (ftruncate(fd, len) < 0)
return -1;
}
if (lock->data)
g_free(lock->data);
lock->data = g_strdup(data);
lock->len = len;
return 0;
}
int
file_lock_unlock(
file_lock *lock)
{
g_assert(lock->locked);
g_static_mutex_lock(&lock_lock);
/* relase the filesystem-level lock */
close(lock->fd);
/* and the hash table entry */
g_hash_table_remove(locally_locked_files, lock->filename);
g_static_mutex_unlock(&lock_lock);
if (lock->data)
g_free(lock->data);
lock->data = NULL;
lock->len = 0;
lock->fd = -1;
lock->locked = FALSE;
return 0;
}
/*
* Old Implementation
*/
/*
**
** Notes:
** - These are "best effort" routines.
** - "configure" has four variables that are used to determine which type of
** locking to use:
** USE_POSIX_FCNTL - use fcntl(). The full job.
** USE_FLOCK - use flock(). Does just as well.
** USE_LOCKF - use lockf(). Only handles advisory, exclusive,
** blocking file locks as used by Amanda.
** USE_LNLOCK - Home brew exclusive, blocking file lock.
** <none> - No locking available. User beware!
*/
/* Interface to the implementations in common-src/amflock-*.c */
#ifdef WANT_AMFLOCK_POSIX
extern amflock_impl_t amflock_posix_impl;
#endif
#ifdef WANT_AMFLOCK_FLOCK
extern amflock_impl_t amflock_flock_impl;
#endif
#ifdef WANT_AMFLOCK_LOCKF
extern amflock_impl_t amflock_lockf_impl;
#endif
#ifdef WANT_AMFLOCK_LNLOCK
extern amflock_impl_t amflock_lnlock_impl;
#endif
amflock_impl_t *amflock_impls[] = {
#ifdef WANT_AMFLOCK_POSIX
&amflock_posix_impl,
#endif
#ifdef WANT_AMFLOCK_FLOCK
&amflock_flock_impl,
#endif
#ifdef WANT_AMFLOCK_LOCKF
&amflock_lockf_impl,
#endif
#ifdef WANT_AMFLOCK_LNLOCK
&amflock_lnlock_impl,
#endif
NULL
};
/* Interface functions */
/* FIXME: for now, these just use the first non-NULL implementation
*/
/* Get a file lock (for read/write files).
*/
int
amflock(
int fd,
char * resource)
{
if (!amflock_impls[0]) return 0; /* no locking */
return amflock_impls[0]->amflock_impl(fd, resource);
}
/*
* Get a file lock (for read-only files).
*/
int
amroflock(
int fd,
char * resource)
{
if (!amflock_impls[0]) return 0; /* no locking */
return amflock_impls[0]->amroflock_impl(fd, resource);
}
/*
* Release a file lock.
*/
int
amfunlock(
int fd,
char * resource)
{
if (!amflock_impls[0]) return 0; /* no locking */
return amflock_impls[0]->amfunlock_impl(fd, resource);
}