Add slap_timestamp(), use mutex in slap_get_csn()

This commit is contained in:
Howard Chu 2005-06-07 04:12:14 +00:00
parent 3bae4b4acb
commit 21b8be393a
7 changed files with 62 additions and 89 deletions

View file

@ -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 )

View file

@ -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, &ltm_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, &ltm_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, &timestamp );
if( op->o_dn.bv_len == 0 ) {
BER_BVSTR( &name, SLAPD_ANONYMOUS );

View file

@ -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, &timestamp );
ber_str2bv( tmbuf, 0, 1, bv_modifyTimestamp );
ber_dupbv( bv_modifyTimestamp, &timestamp );
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, &timestamp );
slap_get_csn( NULL, csnbuf, sizeof(csnbuf), &entryCSN, 0 );

View file

@ -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, &timestamp );
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], &timestamp );
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], &timestamp );
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], &timestamp );
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, &timestamp );
attr_merge_one( op->ora_e, ad_pwdChangedTime, &timestamp, 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, &timestamp );
mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
mods->sml_type.bv_val = NULL;
mods->sml_desc = ad_pwdChangedTime;

View file

@ -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,

View file

@ -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, &ltm_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 */

View file

@ -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, &timestamp );
if ( BER_BVISEMPTY( &be->be_rootndn ) ) {
BER_BVSTR( &name, SLAPD_ANONYMOUS );