/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * Copyright 2005 The OpenLDAP Foundation.
+ * Copyright 2005-2009 The OpenLDAP Foundation.
* Portions Copyright 2004-2005 Symas Corporation.
* All rights reserved.
*
#include "portable.h"
-#if SLAPD_BDB || SLAPD_HDB || SLAPD_LDBM
+#if SLAPD_BDB || SLAPD_HDB
#include "alock.h"
+#include "lutil.h"
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/assert.h>
#include <sys/types.h>
#include <sys/stat.h>
+#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
+#endif
#include <fcntl.h>
#ifdef _WIN32
assert (bufptr != NULL);
bufptr += sizeof (unsigned long int);
- for (count=0; count <= sizeof (unsigned long int); ++count) {
+ for (count=0; count <= (int) sizeof (unsigned long int); ++count) {
val <<= 8;
val += (unsigned long int) *bufptr--;
}
}
if (alock_read_iattr (slotbuf) != ALOCK_MAGIC) {
- return 1;
+ return -1;
}
slot_data->al_lock = alock_read_iattr (slotbuf+8);
slot_data->al_stamp = alock_read_iattr (slotbuf+16);
if (slot_data->al_appname) free (slot_data->al_appname);
slot_data->al_appname = calloc (1, ALOCK_MAX_APPNAME);
+ if (slot_data->al_appname == NULL) {
+ return -1;
+ }
strncpy (slot_data->al_appname, (char *)slotbuf+32, ALOCK_MAX_APPNAME-1);
(slot_data->al_appname) [ALOCK_MAX_APPNAME-1] = '\0';
alock_write_iattr (slotbuf+16, slot_data->al_stamp);
alock_write_iattr (slotbuf+24, slot_data->al_pid);
- strncpy ((char *)slotbuf+32, slot_data->al_appname, ALOCK_MAX_APPNAME-1);
+ if (slot_data->al_appname)
+ strncpy ((char *)slotbuf+32, slot_data->al_appname, ALOCK_MAX_APPNAME-1);
slotbuf[ALOCK_SLOT_SIZE-1] = '\0';
res = lseek (info->al_fd,
static int
alock_query_slot ( alock_info_t * info )
{
- int res;
+ int res, nosave;
alock_slot_t slot_data;
assert (info != NULL);
if (slot_data.al_appname != NULL) free (slot_data.al_appname);
slot_data.al_appname = NULL;
- if (slot_data.al_lock == ALOCK_UNLOCKED) return ALOCK_UNLOCKED;
+ nosave = slot_data.al_lock & ALOCK_NOSAVE;
+
+ if ((slot_data.al_lock & ALOCK_SMASK) == ALOCK_UNLOCKED)
+ return slot_data.al_lock;
res = alock_test_lock (info->al_fd, info->al_slot);
if (res < 0) return -1;
if (res > 0) {
- if (slot_data.al_lock == ALOCK_UNIQUE) {
- return ALOCK_UNIQUE;
+ if ((slot_data.al_lock & ALOCK_SMASK) == ALOCK_UNIQUE) {
+ return slot_data.al_lock;
} else {
- return ALOCK_LOCKED;
+ return ALOCK_LOCKED | nosave;
}
}
- return ALOCK_DIRTY;
+ return ALOCK_DIRTY | nosave;
}
int
alock_slot_t slot_data;
char * filename;
int res, max_slot;
- int dirty_count, live_count;
+ int dirty_count, live_count, nosave;
+ char *ptr;
assert (info != NULL);
assert (appname != NULL);
assert (envdir != NULL);
- assert (locktype >= 1 && locktype <= 2);
+ assert ((locktype & ALOCK_SMASK) >= 1 && (locktype & ALOCK_SMASK) <= 2);
slot_data.al_lock = locktype;
slot_data.al_stamp = time(NULL);
slot_data.al_pid = getpid();
slot_data.al_appname = calloc (1, ALOCK_MAX_APPNAME);
+ if (slot_data.al_appname == NULL) {
+ return ALOCK_UNSTABLE;
+ }
strncpy (slot_data.al_appname, appname, ALOCK_MAX_APPNAME-1);
slot_data.al_appname [ALOCK_MAX_APPNAME-1] = '\0';
filename = calloc (1, strlen (envdir) + strlen ("/alock") + 1);
- strcpy (filename, envdir);
- strcat (filename, "/alock");
+ if (filename == NULL ) {
+ free (slot_data.al_appname);
+ return ALOCK_UNSTABLE;
+ }
+ ptr = lutil_strcopy(filename, envdir);
+ lutil_strcopy(ptr, "/alock");
info->al_fd = open (filename, O_CREAT|O_RDWR, 0666);
free (filename);
if (info->al_fd < 0) {
max_slot = (statbuf.st_size + ALOCK_SLOT_SIZE - 1) / ALOCK_SLOT_SIZE;
dirty_count = 0;
live_count = 0;
+ nosave = 0;
scan_info.al_fd = info->al_fd;
for (scan_info.al_slot = 1;
scan_info.al_slot < max_slot;
if (scan_info.al_slot != info->al_slot) {
res = alock_query_slot (&scan_info);
+ if (res & ALOCK_NOSAVE) {
+ nosave = ALOCK_NOSAVE;
+ res ^= ALOCK_NOSAVE;
+ }
if (res == ALOCK_UNLOCKED
&& info->al_slot == 0) {
info->al_slot = scan_info.al_slot;
return ALOCK_UNSTABLE;
}
- if (dirty_count) return ALOCK_RECOVER;
- return ALOCK_CLEAN;
+ if (dirty_count) return ALOCK_RECOVER | nosave;
+ return ALOCK_CLEAN | nosave;
}
int
struct stat statbuf;
alock_info_t scan_info;
int res, max_slot;
- int dirty_count, live_count;
+ int dirty_count, live_count, nosave;
assert (info != NULL);
max_slot = (statbuf.st_size + ALOCK_SLOT_SIZE - 1) / ALOCK_SLOT_SIZE;
dirty_count = 0;
live_count = 0;
+ nosave = 0;
for (scan_info.al_slot = 1;
scan_info.al_slot < max_slot;
++ scan_info.al_slot) {
if (scan_info.al_slot != info->al_slot) {
res = alock_query_slot (&scan_info);
+ if (res & ALOCK_NOSAVE) {
+ nosave = ALOCK_NOSAVE;
+ res ^= ALOCK_NOSAVE;
+ }
+
if (res == ALOCK_LOCKED) {
++live_count;
close (info->al_fd);
return ALOCK_UNSTABLE;
} else {
- return ALOCK_RECOVER;
+ return ALOCK_RECOVER | nosave;
}
}
- return ALOCK_CLEAN;
+ return ALOCK_CLEAN | nosave;
}
int
-alock_close ( alock_info_t * info )
+alock_close ( alock_info_t * info, int nosave )
{
alock_slot_t slot_data;
int res;
+ if ( !info->al_slot )
+ return ALOCK_CLEAN;
+
(void) memset ((void *) &slot_data, 0, sizeof(alock_slot_t));
res = alock_grab_lock (info->al_fd, 0);
return ALOCK_UNSTABLE;
}
slot_data.al_lock = ALOCK_UNLOCKED;
+ if ( nosave )
+ slot_data.al_lock |= ALOCK_NOSAVE;
res = alock_write_slot (info, &slot_data);
if (res == -1) {
close (info->al_fd);
scan_info.al_slot < max_slot;
++ scan_info.al_slot) {
if (scan_info.al_slot != info->al_slot) {
- res = alock_query_slot (&scan_info);
+ res = alock_query_slot (&scan_info) & ~ALOCK_NOSAVE;
if (res == ALOCK_LOCKED
|| res == ALOCK_UNIQUE) {