]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/alock.c
fix previous commit (ITS#5819)
[openldap] / servers / slapd / alock.c
index 461f4b556c1eeb0b32b6e7f95620c3ba610e3cb7..1a50a399f159cc2176cdf1e433ca1c93528bacd8 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2005-2006 The OpenLDAP Foundation.
+ * Copyright 2005-2008 The OpenLDAP Foundation.
  * Portions Copyright 2004-2005 Symas Corporation.
  * All rights reserved.
  *
@@ -24,6 +24,7 @@
 #if SLAPD_BDB || SLAPD_HDB
 
 #include "alock.h"
+#include "lutil.h"
 
 #include <ac/stdlib.h>
 #include <ac/string.h>
@@ -32,7 +33,9 @@
 #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
@@ -175,7 +178,7 @@ alock_read_iattr ( unsigned char * bufptr )
        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--;
        }
@@ -229,7 +232,7 @@ alock_read_slot ( alock_info_t * info,
        }
        
        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);
@@ -237,6 +240,9 @@ alock_read_slot ( alock_info_t * info,
 
        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';
 
@@ -262,7 +268,8 @@ alock_write_slot ( alock_info_t * info,
        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, 
@@ -332,6 +339,7 @@ alock_open ( alock_info_t * info,
        char * filename;
        int res, max_slot;
        int dirty_count, live_count, nosave;
+       char *ptr;
 
        assert (info != NULL);
        assert (appname != NULL);
@@ -342,12 +350,19 @@ alock_open ( alock_info_t * info,
        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) {
@@ -513,7 +528,7 @@ alock_scan ( alock_info_t * info )
 }
 
 int
-alock_close ( alock_info_t * info )
+alock_close ( alock_info_t * info, int nosave )
 {
        alock_slot_t slot_data;
        int res;
@@ -537,7 +552,9 @@ alock_close ( alock_info_t * info )
                        free (slot_data.al_appname);
                return ALOCK_UNSTABLE;
        }
-       slot_data.al_lock = ALOCK_UNLOCKED | (slot_data.al_lock & ALOCK_NOSAVE);
+       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);