1 /* alock.c - access lock library */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005 The OpenLDAP Foundation.
6 * Portions Copyright 2004-2005 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Matthew Backes at Symas
19 * Corporation for inclusion in OpenLDAP Software.
24 #if SLAPD_BDB || SLAPD_HDB || SLAPD_LDBM
28 #include <ac/stdlib.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
32 #include <ac/assert.h>
33 #include <sys/types.h>
41 #include <sys/locking.h>
46 alock_grab_lock ( int fd, int slot )
50 #if defined( HAVE_LOCKF )
51 res = lseek (fd, (off_t) (ALOCK_SLOT_SIZE * slot), SEEK_SET);
52 if (res == -1) return -1;
53 res = lockf (fd, F_LOCK, (off_t) ALOCK_SLOT_SIZE);
54 #elif defined( HAVE_FCNTL )
55 struct flock lock_info;
56 (void) memset ((void *) &lock_info, 0, sizeof (struct flock));
58 lock_info.l_type = F_WRLCK;
59 lock_info.l_whence = SEEK_SET;
60 lock_info.l_start = (off_t) (ALOCK_SLOT_SIZE * slot);
61 lock_info.l_len = (off_t) ALOCK_SLOT_SIZE;
63 res = fcntl (fd, F_SETLKW, &lock_info);
64 #elif defined( _WIN32 )
65 if( _lseek( fd, (ALOCK_SLOT_SIZE * slot), SEEK_SET ) < 0 )
68 * _lock will try for the lock once per second, returning EDEADLOCK
69 * after ten tries. We just loop until we either get the lock
70 * or some other error is returned.
72 while((res = _locking( fd, _LK_LOCK, ALOCK_SLOT_SIZE )) < 0 ) {
73 if( errno != EDEADLOCK )
77 # error alock needs lockf, fcntl, or _locking
80 assert (errno != EDEADLK);
87 alock_release_lock ( int fd, int slot )
91 #if defined( HAVE_LOCKF )
92 res = lseek (fd, (off_t) (ALOCK_SLOT_SIZE * slot), SEEK_SET);
93 if (res == -1) return -1;
94 res = lockf (fd, F_ULOCK, (off_t) ALOCK_SLOT_SIZE);
95 if (res == -1) return -1;
96 #elif defined ( HAVE_FCNTL )
97 struct flock lock_info;
98 (void) memset ((void *) &lock_info, 0, sizeof (struct flock));
100 lock_info.l_type = F_UNLCK;
101 lock_info.l_whence = SEEK_SET;
102 lock_info.l_start = (off_t) (ALOCK_SLOT_SIZE * slot);
103 lock_info.l_len = (off_t) ALOCK_SLOT_SIZE;
105 res = fcntl (fd, F_SETLKW, &lock_info);
106 if (res == -1) return -1;
107 #elif defined( _WIN32 )
108 res = _lseek (fd, (ALOCK_SLOT_SIZE * slot), SEEK_SET);
109 if (res == -1) return -1;
110 res = _locking( fd, _LK_UNLCK, ALOCK_SLOT_SIZE );
111 if (res == -1) return -1;
113 # error alock needs lockf, fcntl, or _locking
120 alock_test_lock ( int fd, int slot )
124 #if defined( HAVE_LOCKF )
125 res = lseek (fd, (off_t) (ALOCK_SLOT_SIZE * slot), SEEK_SET);
126 if (res == -1) return -1;
128 res = lockf (fd, F_TEST, (off_t) ALOCK_SLOT_SIZE);
130 if (errno == EACCES || errno == EAGAIN) {
136 #elif defined( HAVE_FCNTL )
137 struct flock lock_info;
138 (void) memset ((void *) &lock_info, 0, sizeof (struct flock));
140 lock_info.l_type = F_WRLCK;
141 lock_info.l_whence = SEEK_SET;
142 lock_info.l_start = (off_t) (ALOCK_SLOT_SIZE * slot);
143 lock_info.l_len = (off_t) ALOCK_SLOT_SIZE;
145 res = fcntl (fd, F_GETLK, &lock_info);
146 if (res == -1) return -1;
148 if (lock_info.l_type != F_UNLCK) return ALOCK_LOCKED;
149 #elif defined( _WIN32 )
150 res = _lseek (fd, (ALOCK_SLOT_SIZE * slot), SEEK_SET);
151 if (res == -1) return -1;
152 res = _locking( fd, _LK_NBLCK, ALOCK_SLOT_SIZE );
153 _locking( fd, _LK_UNLCK, ALOCK_SLOT_SIZE );
155 if( errno == EACCES ) {
162 # error alock needs lockf, fcntl, or _locking
168 /* Read a 64bit LE value */
169 static unsigned long int
170 alock_read_iattr ( unsigned char * bufptr )
172 unsigned long int val = 0;
175 assert (bufptr != NULL);
177 bufptr += sizeof (unsigned long int);
178 for (count=0; count <= sizeof (unsigned long int); ++count) {
180 val += (unsigned long int) *bufptr--;
186 /* Write a 64bit LE value */
188 alock_write_iattr ( unsigned char * bufptr,
189 unsigned long int val )
193 assert (bufptr != NULL);
195 for (count=0; count < 8; ++count) {
196 *bufptr++ = (unsigned char) (val & 0xff);
202 alock_read_slot ( alock_info_t * info,
203 alock_slot_t * slot_data )
205 unsigned char slotbuf [ALOCK_SLOT_SIZE];
206 int res, size, size_total, err;
208 assert (info != NULL);
209 assert (slot_data != NULL);
210 assert (info->al_slot > 0);
212 res = lseek (info->al_fd,
213 (off_t) (ALOCK_SLOT_SIZE * info->al_slot),
215 if (res == -1) return -1;
218 while (size_total < ALOCK_SLOT_SIZE) {
219 size = read (info->al_fd,
220 slotbuf + size_total,
221 ALOCK_SLOT_SIZE - size_total);
222 if (size == 0) return -1;
225 if (err != EINTR && err != EAGAIN) return -1;
231 if (alock_read_iattr (slotbuf) != ALOCK_MAGIC) {
234 slot_data->al_lock = alock_read_iattr (slotbuf+8);
235 slot_data->al_stamp = alock_read_iattr (slotbuf+16);
236 slot_data->al_pid = alock_read_iattr (slotbuf+24);
238 if (slot_data->al_appname) free (slot_data->al_appname);
239 slot_data->al_appname = calloc (1, ALOCK_MAX_APPNAME);
240 strncpy (slot_data->al_appname, (char *)slotbuf+32, ALOCK_MAX_APPNAME-1);
241 (slot_data->al_appname) [ALOCK_MAX_APPNAME-1] = '\0';
247 alock_write_slot ( alock_info_t * info,
248 alock_slot_t * slot_data )
250 unsigned char slotbuf [ALOCK_SLOT_SIZE];
251 int res, size, size_total, err;
253 assert (info != NULL);
254 assert (slot_data != NULL);
255 assert (info->al_slot > 0);
257 (void) memset ((void *) slotbuf, 0, ALOCK_SLOT_SIZE);
259 alock_write_iattr (slotbuf, ALOCK_MAGIC);
260 assert (alock_read_iattr (slotbuf) == ALOCK_MAGIC);
261 alock_write_iattr (slotbuf+8, slot_data->al_lock);
262 alock_write_iattr (slotbuf+16, slot_data->al_stamp);
263 alock_write_iattr (slotbuf+24, slot_data->al_pid);
265 strncpy ((char *)slotbuf+32, slot_data->al_appname, ALOCK_MAX_APPNAME-1);
266 slotbuf[ALOCK_SLOT_SIZE-1] = '\0';
268 res = lseek (info->al_fd,
269 (off_t) (ALOCK_SLOT_SIZE * info->al_slot),
271 if (res == -1) return -1;
274 while (size_total < ALOCK_SLOT_SIZE) {
275 size = write (info->al_fd,
276 slotbuf + size_total,
277 ALOCK_SLOT_SIZE - size_total);
278 if (size == 0) return -1;
281 if (err != EINTR && err != EAGAIN) return -1;
291 alock_query_slot ( alock_info_t * info )
294 alock_slot_t slot_data;
296 assert (info != NULL);
297 assert (info->al_slot > 0);
299 (void) memset ((void *) &slot_data, 0, sizeof (alock_slot_t));
300 alock_read_slot (info, &slot_data);
302 if (slot_data.al_appname != NULL) free (slot_data.al_appname);
303 slot_data.al_appname = NULL;
305 if (slot_data.al_lock == ALOCK_UNLOCKED) return ALOCK_UNLOCKED;
307 res = alock_test_lock (info->al_fd, info->al_slot);
308 if (res < 0) return -1;
310 if (slot_data.al_lock == ALOCK_UNIQUE) {
321 alock_open ( alock_info_t * info,
322 const char * appname,
327 alock_info_t scan_info;
328 alock_slot_t slot_data;
331 int dirty_count, live_count;
333 assert (info != NULL);
334 assert (appname != NULL);
335 assert (envdir != NULL);
336 assert (locktype >= 1 && locktype <= 2);
338 slot_data.al_lock = locktype;
339 slot_data.al_stamp = time(NULL);
340 slot_data.al_pid = getpid();
341 slot_data.al_appname = calloc (1, ALOCK_MAX_APPNAME);
342 strncpy (slot_data.al_appname, appname, ALOCK_MAX_APPNAME-1);
343 slot_data.al_appname [ALOCK_MAX_APPNAME-1] = '\0';
345 filename = calloc (1, strlen (envdir) + strlen ("/alock") + 1);
346 strcpy (filename, envdir);
347 strcat (filename, "/alock");
348 info->al_fd = open (filename, O_CREAT|O_RDWR, 0666);
350 if (info->al_fd < 0) {
351 free (slot_data.al_appname);
352 return ALOCK_UNSTABLE;
356 res = alock_grab_lock (info->al_fd, 0);
359 free (slot_data.al_appname);
360 return ALOCK_UNSTABLE;
363 res = fstat (info->al_fd, &statbuf);
366 free (slot_data.al_appname);
367 return ALOCK_UNSTABLE;
370 max_slot = (statbuf.st_size + ALOCK_SLOT_SIZE - 1) / ALOCK_SLOT_SIZE;
373 scan_info.al_fd = info->al_fd;
374 for (scan_info.al_slot = 1;
375 scan_info.al_slot < max_slot;
376 ++ scan_info.al_slot) {
377 if (scan_info.al_slot != info->al_slot) {
378 res = alock_query_slot (&scan_info);
380 if (res == ALOCK_UNLOCKED
381 && info->al_slot == 0) {
382 info->al_slot = scan_info.al_slot;
384 } else if (res == ALOCK_LOCKED) {
387 } else if (res == ALOCK_UNIQUE
388 && locktype == ALOCK_UNIQUE) {
390 free (slot_data.al_appname);
393 } else if (res == ALOCK_DIRTY) {
396 } else if (res == -1) {
398 free (slot_data.al_appname);
399 return ALOCK_UNSTABLE;
405 if (dirty_count && live_count) {
407 free (slot_data.al_appname);
408 return ALOCK_UNSTABLE;
411 if (info->al_slot == 0) info->al_slot = max_slot + 1;
412 res = alock_grab_lock (info->al_fd,
416 free (slot_data.al_appname);
417 return ALOCK_UNSTABLE;
419 res = alock_write_slot (info, &slot_data);
420 free (slot_data.al_appname);
423 return ALOCK_UNSTABLE;
426 res = alock_release_lock (info->al_fd, 0);
429 return ALOCK_UNSTABLE;
432 if (dirty_count) return ALOCK_RECOVER;
437 alock_scan ( alock_info_t * info )
440 alock_info_t scan_info;
442 int dirty_count, live_count;
444 assert (info != NULL);
446 scan_info.al_fd = info->al_fd;
448 res = alock_grab_lock (info->al_fd, 0);
451 return ALOCK_UNSTABLE;
454 res = fstat (info->al_fd, &statbuf);
457 return ALOCK_UNSTABLE;
460 max_slot = (statbuf.st_size + ALOCK_SLOT_SIZE - 1) / ALOCK_SLOT_SIZE;
463 for (scan_info.al_slot = 1;
464 scan_info.al_slot < max_slot;
465 ++ scan_info.al_slot) {
466 if (scan_info.al_slot != info->al_slot) {
467 res = alock_query_slot (&scan_info);
469 if (res == ALOCK_LOCKED) {
472 } else if (res == ALOCK_DIRTY) {
475 } else if (res == -1) {
477 return ALOCK_UNSTABLE;
483 res = alock_release_lock (info->al_fd, 0);
486 return ALOCK_UNSTABLE;
492 return ALOCK_UNSTABLE;
494 return ALOCK_RECOVER;
502 alock_close ( alock_info_t * info )
504 alock_slot_t slot_data;
507 (void) memset ((void *) &slot_data, 0, sizeof(alock_slot_t));
509 res = alock_grab_lock (info->al_fd, 0);
512 return ALOCK_UNSTABLE;
515 /* mark our slot as clean */
516 res = alock_read_slot (info, &slot_data);
519 if (slot_data.al_appname != NULL)
520 free (slot_data.al_appname);
521 return ALOCK_UNSTABLE;
523 slot_data.al_lock = ALOCK_UNLOCKED;
524 res = alock_write_slot (info, &slot_data);
527 if (slot_data.al_appname != NULL)
528 free (slot_data.al_appname);
529 return ALOCK_UNSTABLE;
531 if (slot_data.al_appname != NULL) {
532 free (slot_data.al_appname);
533 slot_data.al_appname = NULL;
536 res = alock_release_lock (info->al_fd, info->al_slot);
539 return ALOCK_UNSTABLE;
541 res = alock_release_lock (info->al_fd, 0);
544 return ALOCK_UNSTABLE;
547 res = close (info->al_fd);
548 if (res == -1) return ALOCK_UNSTABLE;
554 alock_recover ( alock_info_t * info )
557 alock_slot_t slot_data;
558 alock_info_t scan_info;
561 assert (info != NULL);
563 scan_info.al_fd = info->al_fd;
565 (void) memset ((void *) &slot_data, 0, sizeof(alock_slot_t));
567 res = alock_grab_lock (info->al_fd, 0);
570 return ALOCK_UNSTABLE;
573 res = fstat (info->al_fd, &statbuf);
576 return ALOCK_UNSTABLE;
579 max_slot = (statbuf.st_size + ALOCK_SLOT_SIZE - 1) / ALOCK_SLOT_SIZE;
580 for (scan_info.al_slot = 1;
581 scan_info.al_slot < max_slot;
582 ++ scan_info.al_slot) {
583 if (scan_info.al_slot != info->al_slot) {
584 res = alock_query_slot (&scan_info);
586 if (res == ALOCK_LOCKED
587 || res == ALOCK_UNIQUE) {
588 /* recovery attempt on an active db? */
590 return ALOCK_UNSTABLE;
592 } else if (res == ALOCK_DIRTY) {
594 res = alock_read_slot (&scan_info, &slot_data);
597 return ALOCK_UNSTABLE;
599 slot_data.al_lock = ALOCK_UNLOCKED;
600 res = alock_write_slot (&scan_info, &slot_data);
603 if (slot_data.al_appname != NULL)
604 free (slot_data.al_appname);
605 return ALOCK_UNSTABLE;
607 if (slot_data.al_appname != NULL) {
608 free (slot_data.al_appname);
609 slot_data.al_appname = NULL;
612 } else if (res == -1) {
614 return ALOCK_UNSTABLE;
620 res = alock_release_lock (info->al_fd, 0);
623 return ALOCK_UNSTABLE;
629 #endif /* SLAPD_BDB || SLAPD_HDB */