]> git.sur5r.net Git - openldap/blobdiff - servers/slurpd/rq.c
ITS#3917 revert prev commit
[openldap] / servers / slurpd / rq.c
index 260872b7d90cdc9483a53feb8c05a5bc615c8b23..4649118000a0075b299be3757eb82c8537cbf223 100644 (file)
@@ -1,5 +1,18 @@
-/*
- * Copyright (c) 1996 Regents of the University of Michigan.
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2005 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 file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* Portions Copyright (c) 1996 Regents of the University of Michigan.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
@@ -9,6 +22,10 @@
  * software without specific prior written permission. This software
  * is provided ``as is'' without express or implied warranty.
  */
+/* ACKNOWLEDGEMENTS:
+ * This work was originally developed by the University of Michigan
+ * (as part of U-MICH LDAP).
+ */
 
 /*
  * rq.c - routines used to manage the queue of replication entries.
 #include "portable.h"
 
 #include <stdio.h>
+#include <sys/stat.h>
 
-#include "slurp.h"
-#include "globals.h"
+#include <ac/stdlib.h>
+#include <ac/string.h>
+#include <ac/unistd.h>         /* get ftruncate() */
 
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
 
-/* externs */
-extern void Re_dump LDAP_P(( Re *re ));
-
-#ifdef DECL_SYS_ERRLIST
-extern char *sys_errlist[];
-#endif /* DECL_SYS_ERRLIST */
+#include "slurp.h"
+#include "globals.h"
 
 /*
  * Lock the replication queue.
@@ -54,7 +75,7 @@ Rq_lock(
     Rq *rq
 )
 {
-    return( pthread_mutex_lock( &rq->rq_mutex ));
+    return( ldap_pvt_thread_mutex_lock( &rq->rq_mutex ));
 }
 
 
@@ -66,7 +87,7 @@ Rq_unlock(
     Rq *rq
 )
 {
-    return( pthread_mutex_unlock( &rq->rq_mutex ));
+    return( ldap_pvt_thread_mutex_unlock( &rq->rq_mutex ));
 }
 
 
@@ -177,7 +198,7 @@ Rq_add(
 
     /* set the sequence number */
     re->re_seq = 0;
-    if ( !wasempty && !strcmp(rq->rq_tail->re_timestamp, re->re_timestamp )) {
+    if ( !wasempty && ( rq->rq_tail->re_timestamp == re->re_timestamp )) {
        /*
         * Our new re has the same timestamp as the tail's timestamp.
         * Increment the seq number in the tail and use it as our seq number.
@@ -189,7 +210,7 @@ Rq_add(
     /* Increment count of items in queue */
     rq->rq_nre++;
     /* wake up any threads waiting for more work */
-    pthread_cond_broadcast( &rq->rq_more );
+    ldap_pvt_thread_cond_broadcast( &rq->rq_more );
 
     /* ... and unlock the queue */
     rq->rq_unlock( rq );
@@ -232,17 +253,28 @@ Rq_dump(
 {
     Re         *re;
     FILE       *fp;
+    int                tmpfd;
 
     if ( rq == NULL ) {
        Debug( LDAP_DEBUG_ANY, "Rq_dump: rq is NULL!\n", 0, 0, 0 );
        return;
     }
 
-    if (( fp = fopen( SLURPD_DUMPFILE, "w" )) == NULL ) {
+    if (unlink(SLURPD_DUMPFILE) == -1 && errno != ENOENT) {
+       Debug( LDAP_DEBUG_ANY, "Rq_dump: \"%s\" exists, and cannot unlink\n",
+               SLURPD_DUMPFILE, 0, 0 );
+       return;
+    }
+    if (( tmpfd = open(SLURPD_DUMPFILE, O_CREAT|O_RDWR|O_EXCL, 0600)) == -1) {
        Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot open \"%s\" for write\n",
                SLURPD_DUMPFILE, 0, 0 );
        return;
     }
+    if (( fp = fdopen( tmpfd, "w" )) == NULL ) {
+       Debug( LDAP_DEBUG_ANY, "Rq_dump: cannot fdopen \"%s\" for write\n",
+               SLURPD_DUMPFILE, 0, 0 );
+       return;
+    }
 
     rq->rq_lock( rq );
     for ( re = rq->rq_gethead( rq ); re != NULL; re = rq->rq_getnext( re )) {
@@ -318,8 +350,6 @@ Rq_needtrim(
 )
 {
     int                rc = 0;
-    Re         *re;
-    int                nzrc = 0;       /* nzrc is count of entries with refcnt == 0 */
     time_t     now;
 
     if ( rq == NULL ) {
@@ -363,7 +393,7 @@ Rq_getcount(
        for ( re = rq->rq_gethead( rq ); re != NULL;
                re = rq->rq_getnext( re )) {
            if ( type == RQ_COUNT_NZRC ) {
-               if ( re->re_getrefcnt( re ) > 1 ) {
+               if ( re->re_getrefcnt( re ) > 0 ) {
                    count++;
                }
            }
@@ -402,8 +432,8 @@ Rq_init(
     (*rq)->rq_getcount = Rq_getcount;
 
     /* Initialize private data */
-    pthread_mutex_init( &((*rq)->rq_mutex), pthread_mutexattr_default );
-    pthread_cond_init( &((*rq)->rq_more), pthread_condattr_default );
+    ldap_pvt_thread_mutex_init( &((*rq)->rq_mutex) );
+    ldap_pvt_thread_cond_init( &((*rq)->rq_more) );
     (*rq)->rq_head = NULL;
     (*rq)->rq_tail = NULL;
     (*rq)->rq_nre = 0;
@@ -412,4 +442,3 @@ Rq_init(
 
     return 0;
 }
-