| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- /** BEGIN COPYRIGHT BLOCK
- * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
- * Copyright (C) 2005 Red Hat, Inc.
- * All rights reserved.
- *
- * License: GPL (version 3 or any later version).
- * See LICENSE for details.
- * END COPYRIGHT BLOCK **/
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include "retrocl.h"
- static changeNumber retrocl_internal_cn = 0;
- static changeNumber retrocl_first_cn = 0;
- static int check_last_changenumber = 0;
- /*
- * Function: a2changeNumber
- *
- * Returns: changeNumber (long)
- *
- * Arguments: string
- *
- * Description: parses the string to a changenumber. changenumbers are
- * positive integers.
- *
- */
- static changeNumber a2changeNumber (const char *p)
- {
- changeNumber c;
- c = strntoul((char *)p,strlen(p),10);
- return c;
- }
- /*
- * Function: handle_cnum_entry
- * Arguments: op - pointer to Operation struct for this operation
- * e - pointer to returned entry.
- * Returns: nothing
- * Description: Search result handler for retrocl_getchangenum(). Sets the
- * op->o_handler_data to point to a structure which contains
- * the changenumber retrieved and an error code.
- */
- static int
- handle_cnum_entry( Slapi_Entry *e, void *callback_data )
- {
- cnumRet *cr = (cnumRet *)callback_data;
- Slapi_Value *sval=NULL;
- const struct berval *value;
- cr->cr_cnum = 0UL;
- cr->cr_time = NULL;
- if ( NULL != e ) {
- Slapi_Attr *chattr = NULL;
- sval = NULL;
- value = NULL;
- if ( slapi_entry_attr_find( e, attr_changenumber, &chattr ) == 0 ) {
- slapi_attr_first_value( chattr,&sval );
- if ( NULL != sval ) {
- value = slapi_value_get_berval ( sval );
- if( NULL != value && NULL != value->bv_val &&
- '\0' != value->bv_val[0]) {
- cr->cr_cnum = a2changeNumber( value->bv_val );
- }
- }
- }
- chattr = NULL;
- sval = NULL;
- value = NULL;
- chattr = NULL;
- sval = NULL;
- value = NULL;
- if ( slapi_entry_attr_find( e, attr_changetime, &chattr ) == 0 ) {
- slapi_attr_first_value( chattr,&sval );
- if ( NULL != sval) {
- value = slapi_value_get_berval ( sval );
- if (NULL != value && NULL != value->bv_val &&
- '\0' != value->bv_val[0]) {
- cr->cr_time = slapi_ch_strdup( value->bv_val );
- }
- }
- }
- }
- return 0;
- }
- /*
- * Function: handle_cnum_result
- * Arguments: err - error code returned from search
- * callback_data - private data for callback
- * Returns: nothing
- * Description: result handler for retrocl_getchangenum(). Sets the cr_lderr
- * field of the cnumRet struct to the error returned
- * from the backend.
- */
- static void
- handle_cnum_result( int err, void *callback_data )
- {
- cnumRet *cr = (cnumRet *)callback_data;
- cr->cr_lderr = err;
- }
- /*
- * Function: retrocl_get_changenumbers
- *
- * Returns: 0/-1
- *
- * Arguments: none
- *
- * Description: reads the first and last entry in the changelog to obtain
- * the starting and ending change numbers.
- *
- */
- int retrocl_get_changenumbers(void)
- {
- cnumRet cr;
- if (retrocl_be_changelog == NULL) return -1;
-
- cr.cr_cnum = 0;
- cr.cr_time = 0;
- slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_FIRST,
- (char *)attr_changenumber, /* cast away const */
- NULL,NULL,0,&cr,NULL,handle_cnum_result,
- handle_cnum_entry, NULL);
- slapi_rwlock_wrlock(retrocl_cn_lock);
- retrocl_first_cn = cr.cr_cnum;
- slapi_ch_free(( void **) &cr.cr_time );
- slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_LAST,
- (char *)attr_changenumber, /* cast away const */
- NULL,NULL,0,&cr,NULL,handle_cnum_result,
- handle_cnum_entry, NULL);
- retrocl_internal_cn = cr.cr_cnum;
-
- slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Got changenumbers %lu and %lu\n",
- retrocl_first_cn,
- retrocl_internal_cn);
- slapi_rwlock_unlock(retrocl_cn_lock);
- slapi_ch_free(( void **) &cr.cr_time );
- return 0;
- }
- /*
- * Function: retrocl_getchangetime
- * Arguments: type - one of SLAPI_SEQ_FIRST, SLAPI_SEQ_LAST
- * Returns: The time of the requested change record. If the return value is
- * NO_TIME, the changelog could not be read.
- * If err is non-NULL, the memory it points to is set the the
- * error code returned from the backend. If "type" is not valid,
- * *err is set to -1.
- * Description: Get the first or last changenumber stored in the changelog,
- * depending on the value of argument "type".
- */
- time_t retrocl_getchangetime( int type, int *err )
- {
- cnumRet cr;
- time_t ret;
- if ( type != SLAPI_SEQ_FIRST && type != SLAPI_SEQ_LAST ) {
- if ( err != NULL ) {
- *err = -1;
- }
- return NO_TIME;
- }
- memset( &cr, '\0', sizeof( cnumRet ));
- slapi_seq_callback( RETROCL_CHANGELOG_DN, type,
- (char *)attr_changenumber, /* cast away const */
- NULL,
- NULL, 0, &cr, NULL,
- handle_cnum_result, handle_cnum_entry, NULL );
-
- if ( err != NULL ) {
- *err = cr.cr_lderr;
- }
-
- if ( NULL == cr.cr_time ) {
- ret = NO_TIME;
- } else {
- ret = parse_localTime( cr.cr_time );
- }
- slapi_ch_free(( void **) &cr.cr_time );
- return ret;
- }
- /*
- * Function: retrocl_forget_changenumbers
- *
- * Returns: none
- *
- * Arguments: none
- *
- * Description: used only when the server is shutting down
- *
- */
- void retrocl_forget_changenumbers(void)
- {
- slapi_rwlock_wrlock(retrocl_cn_lock);
- retrocl_first_cn = 0;
- retrocl_internal_cn = 0;
- slapi_rwlock_unlock(retrocl_cn_lock);
- }
- /*
- * Function: retrocl_get_first_changenumber
- *
- * Returns: changeNumber
- *
- * Arguments: none
- *
- * Description: used in root DSE
- *
- */
- changeNumber retrocl_get_first_changenumber(void)
- {
- changeNumber cn;
- slapi_rwlock_rdlock(retrocl_cn_lock);
- cn = retrocl_first_cn;
- slapi_rwlock_unlock(retrocl_cn_lock);
- return cn;
- }
- /*
- * Function: retrocl_set_first_changenumber
- *
- * Returns: none
- *
- * Arguments: changenumber
- *
- * Description: used in changelog trimming
- *
- */
- void retrocl_set_first_changenumber(changeNumber cn)
- {
- slapi_rwlock_wrlock(retrocl_cn_lock);
- retrocl_first_cn = cn;
- slapi_rwlock_unlock(retrocl_cn_lock);
- }
- /*
- * Function: retrocl_get_last_changenumber
- *
- * Returns:
- *
- * Arguments:
- *
- * Description: used in root DSE
- *
- */
- changeNumber retrocl_get_last_changenumber(void)
- {
- changeNumber cn;
- slapi_rwlock_rdlock(retrocl_cn_lock);
- cn = retrocl_internal_cn;
- slapi_rwlock_unlock(retrocl_cn_lock);
- return cn;
- }
- /*
- * Function: retrocl_commit_changenumber
- *
- * Returns: none
- *
- * Arguments: none, lock must be held
- *
- * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
- *
- */
- void retrocl_commit_changenumber(void)
- {
- slapi_rwlock_wrlock(retrocl_cn_lock);
- if ( retrocl_first_cn == 0) {
- retrocl_first_cn = retrocl_internal_cn;
- }
- slapi_rwlock_unlock(retrocl_cn_lock);
- }
- /*
- * Function: retrocl_release_changenumber
- *
- * Returns: none
- *
- * Arguments: none, lock must be held
- *
- * Description: NOTE! MUST BE PRECEEDED BY retrocl_assign_changenumber
- *
- */
- void retrocl_release_changenumber(void)
- {
- slapi_rwlock_wrlock(retrocl_cn_lock);
- retrocl_internal_cn--;
- slapi_rwlock_unlock(retrocl_cn_lock);
- }
- /*
- * Function: retrocl_update_lastchangenumber
- *
- * Returns: 0/-1
- *
- * Arguments: none. The caller should have taken write lock for the change numbers
- *
- * Description: reads the last entry in the changelog to obtain
- * the last change number.
- *
- */
- int retrocl_update_lastchangenumber(void)
- {
- cnumRet cr;
- if (retrocl_be_changelog == NULL) return -1;
- slapi_rwlock_unlock(retrocl_cn_lock);
- cr.cr_cnum = 0;
- cr.cr_time = 0;
- slapi_seq_callback(RETROCL_CHANGELOG_DN,SLAPI_SEQ_LAST,
- (char *)attr_changenumber, /* cast away const */
- NULL,NULL,0,&cr,NULL,handle_cnum_result,
- handle_cnum_entry, NULL);
- slapi_rwlock_wrlock(retrocl_cn_lock);
- retrocl_internal_cn = cr.cr_cnum;
- slapi_log_error(SLAPI_LOG_PLUGIN,"retrocl","Refetched last changenumber = %lu \n",
- retrocl_internal_cn);
- slapi_ch_free(( void **) &cr.cr_time );
- return 0;
- }
- /*
- * Function: retrocl_assign_changenumber
- *
- * Returns: change number, 0 on error
- *
- * Arguments: none. Lock must be held.
- *
- * Description: NOTE! MUST BE FOLLOWED BY retrocl_commit_changenumber or
- * retrocl_release_changenumber
- *
- */
- changeNumber retrocl_assign_changenumber(void)
- {
- changeNumber cn;
-
- /* Before we assign the changenumber; we should check for the
- * validity of the internal assignment of retrocl_internal_cn
- * we had from the startup */
- slapi_rwlock_wrlock(retrocl_cn_lock);
- if((check_last_changenumber) ||
- ((retrocl_internal_cn <= retrocl_first_cn) &&
- (retrocl_internal_cn > 1 ))){
- /* the numbers have become out of sync - retrocl_get_changenumbers
- * gets called only once during startup and it may have had a problem
- * getting the last changenumber.
- * If there was any problem then update the lastchangenumber from the changelog db.
- * This function is being called by only the thread that is actually writing
- * to the changelog.
- *
- * after the first change was applied both _cn numbers are 1, that's ok
- */
- retrocl_update_lastchangenumber();
- check_last_changenumber = 0;
- }
- retrocl_internal_cn++;
- cn = retrocl_internal_cn;
- slapi_rwlock_unlock(retrocl_cn_lock);
- return cn;
- }
- void retrocl_set_check_changenumber(void)
- {
- slapi_rwlock_wrlock(retrocl_cn_lock);
- check_last_changenumber = 1;
- slapi_rwlock_unlock(retrocl_cn_lock);
- }
|