From: Howard Chu Date: Tue, 7 Jun 2005 04:12:14 +0000 (+0000) Subject: Add slap_timestamp(), use mutex in slap_get_csn() X-Git-Tag: OPENLDAP_AC_BP~568 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=21b8be393a3e5c3661fdc543be094ba04883010b;p=openldap Add slap_timestamp(), use mutex in slap_get_csn() --- diff --git a/servers/slapd/ctxcsn.c b/servers/slapd/ctxcsn.c index defcac2e89..408ee66838 100644 --- a/servers/slapd/ctxcsn.c +++ b/servers/slapd/ctxcsn.c @@ -183,7 +183,13 @@ slap_get_csn( { if ( csn == NULL ) return LDAP_OTHER; +#ifndef HAVE_GMTIME_R + ldap_pvt_thread_mutex_lock( &gmtime_mutex ); +#endif csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 ); +#ifndef HAVE_GMTIME_R + ldap_pvt_thread_mutex_unlock( &gmtime_mutex ); +#endif csn->bv_val = csnbuf; if ( manage_ctxcsn ) diff --git a/servers/slapd/modify.c b/servers/slapd/modify.c index fe7d7aae78..17ac17c4a3 100644 --- a/servers/slapd/modify.c +++ b/servers/slapd/modify.c @@ -832,6 +832,28 @@ int slap_mods_check( return LDAP_SUCCESS; } +/* Enter with bv->bv_len = sizeof buffer, returns with + * actual length of string + */ +void slap_timestamp( time_t *tm, struct berval *bv ) +{ + struct tm *ltm; +#ifdef HAVE_GMTIME_R + struct tm ltm_buf; + + ltm = gmtime_r( tm, <m_buf ); +#else + ldap_pvt_thread_mutex_lock( &gmtime_mutex ); + ltm = gmtime( &tm ); +#endif + + bv->bv_len = lutil_gentime( bv->bv_val, bv->bv_len, ltm ); + +#ifndef HAVE_GMTIME_R + ldap_pvt_thread_mutex_unlock( &gmtime_mutex ); +#endif +} + int slap_mods_opattrs( Operation *op, Modifications *mods, @@ -853,28 +875,14 @@ int slap_mods_opattrs( assert( *modtail == NULL ); if ( SLAP_LASTMOD( op->o_bd )) { - struct tm *ltm; -#ifdef HAVE_GMTIME_R - struct tm ltm_buf; -#endif time_t now = slap_get_time(); -#ifdef HAVE_GMTIME_R - ltm = gmtime_r( &now, <m_buf ); -#else - ldap_pvt_thread_mutex_lock( &gmtime_mutex ); - ltm = gmtime( &now ); -#endif /* HAVE_GMTIME_R */ - lutil_gentime( timebuf, sizeof(timebuf), ltm ); - slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, manage_ctxcsn ); -#ifndef HAVE_GMTIME_R - ldap_pvt_thread_mutex_unlock( &gmtime_mutex ); -#endif - timestamp.bv_val = timebuf; - timestamp.bv_len = strlen(timebuf); + timestamp.bv_len = sizeof(timebuf); + + slap_timestamp( &now, ×tamp ); if( op->o_dn.bv_len == 0 ) { BER_BVSTR( &name, SLAPD_ANONYMOUS ); diff --git a/servers/slapd/overlays/lastmod.c b/servers/slapd/overlays/lastmod.c index a645f7d8b4..8a9996708f 100644 --- a/servers/slapd/overlays/lastmod.c +++ b/servers/slapd/overlays/lastmod.c @@ -378,11 +378,8 @@ best_guess( Operation *op, } if ( bv_modifyTimestamp ) { - struct tm *tm; -#ifdef HAVE_GMTIME_R - struct tm tm_buf; -#endif char tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ]; + struct berval timestamp; time_t currtime; /* best guess */ @@ -392,18 +389,11 @@ best_guess( Operation *op, /* maybe we better use the time the operation was initiated */ currtime = op->o_time; -#ifndef HAVE_GMTIME_R - ldap_pvt_thread_mutex_lock( &gmtime_mutex ); - tm = gmtime( &currtime ); -#else /* HAVE_GMTIME_R */ - tm = gmtime_r( &currtime, &tm_buf ); -#endif /* HAVE_GMTIME_R */ - lutil_gentime( tmbuf, sizeof( tmbuf ), tm ); -#ifndef HAVE_GMTIME_R - ldap_pvt_thread_mutex_unlock( &gmtime_mutex ); -#endif + timestamp.bv_val = tmbuf; + timestamp.bv_len = sizeof(tmbuf); + slap_timestamp( &currtime, ×tamp ); - ber_str2bv( tmbuf, 0, 1, bv_modifyTimestamp ); + ber_dupbv( bv_modifyTimestamp, ×tamp ); ber_dupbv( bv_nmodifyTimestamp, bv_modifyTimestamp ); } @@ -904,14 +894,11 @@ lastmod_db_open( slap_overinst *on = (slap_overinst *) be->bd_info; lastmod_info_t *lmi = (lastmod_info_t *)on->on_bi.bi_private; char buf[ 8192 ]; - struct tm *tms; -#ifdef HAVE_GMTIME_R - struct tm tm_buf; -#endif static char tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ]; char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ]; struct berval entryCSN; + struct berval timestamp; if ( !SLAP_LASTMOD( be ) ) { fprintf( stderr, "set \"lastmod on\" to make this overlay effective\n" ); @@ -921,16 +908,9 @@ lastmod_db_open( /* * Start */ -#ifndef HAVE_GMTIME_R - ldap_pvt_thread_mutex_lock( &gmtime_mutex ); - tms = gmtime( &starttime ); -#else /* HAVE_GMTIME_R */ - tms = gmtime_r( &starttime, &tm_buf ); -#endif /* HAVE_GMTIME_R */ - lutil_gentime( tmbuf, sizeof(tmbuf), tms ); -#ifndef HAVE_GMTIME_R - ldap_pvt_thread_mutex_unlock( &gmtime_mutex ); -#endif + timestamp.bv_val = tmbuf; + timestamp.bv_len = sizeof(tmbuf); + slap_timestamp( &starttime, ×tamp ); slap_get_csn( NULL, csnbuf, sizeof(csnbuf), &entryCSN, 0 ); diff --git a/servers/slapd/overlays/ppolicy.c b/servers/slapd/overlays/ppolicy.c index 87c643aaff..9941f865dc 100644 --- a/servers/slapd/overlays/ppolicy.c +++ b/servers/slapd/overlays/ppolicy.c @@ -683,10 +683,10 @@ ppolicy_bind_resp( Operation *op, SlapReply *rs ) int pwExpired = 0; int ngut = -1, warn = -1, age, rc, i; Attribute *a; - struct tm *tm; time_t now, then, pwtime = (time_t)-1; const char *txt; char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ]; + struct berval timestamp; BackendInfo *bi = op->o_bd->bd_info; Entry *e; @@ -704,10 +704,9 @@ ppolicy_bind_resp( Operation *op, SlapReply *rs ) } now = slap_get_time(); /* stored for later consideration */ - ldap_pvt_thread_mutex_lock( &gmtime_mutex ); - tm = gmtime(&now); - lutil_gentime( nowstr, sizeof(nowstr), tm ); - ldap_pvt_thread_mutex_unlock( &gmtime_mutex ); + timestamp.bv_val = nowstr; + timestamp.bv_len = sizeof(nowstr); + slap_timestamp( &now, ×tamp ); if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) { int i = 0, fc = 0; @@ -719,7 +718,7 @@ ppolicy_bind_resp( Operation *op, SlapReply *rs ) m->sml_desc = ad_pwdFailureTime; m->sml_values = ch_calloc( sizeof(struct berval), 2 ); - ber_str2bv( nowstr, 0, 1, &m->sml_values[0] ); + ber_dupbv( &m->sml_values[0], ×tamp ); m->sml_next = mod; mod = m; @@ -765,7 +764,7 @@ ppolicy_bind_resp( Operation *op, SlapReply *rs ) m->sml_type = ad_pwdAccountLockedTime->ad_cname; m->sml_desc = ad_pwdAccountLockedTime; m->sml_values = ch_calloc( sizeof(struct berval), 2 ); - ber_str2bv( nowstr, 0, 1, &m->sml_values[0] ); + ber_dupbv( &m->sml_values[0], ×tamp ); m->sml_next = mod; mod = m; } @@ -867,7 +866,7 @@ grace: m->sml_type = ad_pwdGraceUseTime->ad_cname; m->sml_desc = ad_pwdGraceUseTime; m->sml_values = ch_calloc( sizeof(struct berval), 2 ); - ber_str2bv( nowstr, 0, 1, &m->sml_values[0] ); + ber_dupbv( &m->sml_values[0], ×tamp ); m->sml_next = mod; mod = m; @@ -1129,16 +1128,11 @@ ppolicy_add( if (( pp.pwdMaxAge || pp.pwdMinAge ) && !be_shadow_update( op )) { struct berval timestamp; char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ]; - struct tm *ltm; time_t now = slap_get_time(); - ldap_pvt_thread_mutex_lock( &gmtime_mutex ); - ltm = gmtime( &now ); - lutil_gentime( timebuf, sizeof(timebuf), ltm ); - ldap_pvt_thread_mutex_unlock( &gmtime_mutex ); - timestamp.bv_val = timebuf; - timestamp.bv_len = strlen(timebuf); + timestamp.bv_len = sizeof(timebuf); + slap_timestamp( &now, ×tamp ); attr_merge_one( op->ora_e, ad_pwdChangedTime, ×tamp, NULL ); } @@ -1443,7 +1437,6 @@ do_modify: if ((pwmod) && (!be_shadow_update( op ))) { struct berval timestamp; char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ]; - struct tm *ltm; time_t now = slap_get_time(); Attribute *ga; @@ -1452,13 +1445,10 @@ do_modify: * up to date. */ - ldap_pvt_thread_mutex_lock( &gmtime_mutex ); - ltm = gmtime( &now ); - lutil_gentime( timebuf, sizeof(timebuf), ltm ); - ldap_pvt_thread_mutex_unlock( &gmtime_mutex ); - timestamp.bv_val = timebuf; - timestamp.bv_len = strlen(timebuf); + timestamp.bv_len = sizeof(timebuf); + slap_timestamp( &now, ×tamp ); + mods = (Modifications *) ch_malloc( sizeof( Modifications ) ); mods->sml_type.bv_val = NULL; mods->sml_desc = ad_pwdChangedTime; diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h index f5ed80742f..cb453fe715 100644 --- a/servers/slapd/proto-slap.h +++ b/servers/slapd/proto-slap.h @@ -897,6 +897,10 @@ LDAP_SLAPD_F( int ) slap_mods_check( const char **text, char *textbuf, size_t textlen, void *ctx ); +LDAP_SLAPD_F( void ) slap_timestamp( + time_t *tm, + struct berval *bv ); + LDAP_SLAPD_F( int ) slap_mods_opattrs( Operation *op, Modifications *mods, diff --git a/servers/slapd/schema.c b/servers/slapd/schema.c index bd1601ce0d..c2f0970ff9 100644 --- a/servers/slapd/schema.c +++ b/servers/slapd/schema.c @@ -117,10 +117,6 @@ schema_info( Entry **entry, const char **text ) } { - struct tm *ltm; -#ifdef HAVE_GMTIME_R - struct tm ltm_buf; -#endif char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ]; /* @@ -134,19 +130,10 @@ schema_info( Entry **entry, const char **text ) * AND modified at server startup time ... */ -#ifdef HAVE_GMTIME_R - ltm = gmtime_r( &starttime, <m_buf ); -#else - ldap_pvt_thread_mutex_lock( &gmtime_mutex ); - ltm = gmtime( &starttime ); -#endif /* HAVE_GMTIME_R */ - lutil_gentime( timebuf, sizeof(timebuf), ltm ); -#ifndef HAVE_GMTIME_R - ldap_pvt_thread_mutex_unlock( &gmtime_mutex ); -#endif - vals[0].bv_val = timebuf; - vals[0].bv_len = strlen( timebuf ); + vals[0].bv_len = sizeof( timebuf ); + + slap_timestamp( &starttime, vals ); if( attr_merge_one( e, ad_createTimestamp, vals, NULL ) ) { /* Out of memory, do something about it */ diff --git a/servers/slapd/slapadd.c b/servers/slapd/slapadd.c index ac66a3f008..fec476894d 100644 --- a/servers/slapd/slapadd.c +++ b/servers/slapd/slapadd.c @@ -196,7 +196,6 @@ slapadd( int argc, char **argv ) } if ( SLAP_LASTMOD(be) ) { - struct tm *ltm; time_t now = slap_get_time(); char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ]; struct berval vals[ 2 ]; @@ -213,14 +212,13 @@ slapadd( int argc, char **argv ) nvals[1].bv_len = 0; nvals[1].bv_val = NULL; - ltm = gmtime(&now); - lutil_gentime( timebuf, sizeof(timebuf), ltm ); - csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 ); csn.bv_val = csnbuf; timestamp.bv_val = timebuf; - timestamp.bv_len = strlen(timebuf); + timestamp.bv_len = sizeof(timebuf); + + slap_timestamp( &now, ×tamp ); if ( BER_BVISEMPTY( &be->be_rootndn ) ) { BER_BVSTR( &name, SLAPD_ANONYMOUS );