]> git.sur5r.net Git - openldap/commitdiff
Add slap_timestamp(), use mutex in slap_get_csn()
authorHoward Chu <hyc@openldap.org>
Tue, 7 Jun 2005 04:12:14 +0000 (04:12 +0000)
committerHoward Chu <hyc@openldap.org>
Tue, 7 Jun 2005 04:12:14 +0000 (04:12 +0000)
servers/slapd/ctxcsn.c
servers/slapd/modify.c
servers/slapd/overlays/lastmod.c
servers/slapd/overlays/ppolicy.c
servers/slapd/proto-slap.h
servers/slapd/schema.c
servers/slapd/slapadd.c

index defcac2e89dec319756c4d99f707dc9e1b4b078c..408ee66838d796f916e667aff634b37718e6147f 100644 (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 )
index fe7d7aae7864b7674d5828515f65eccef34e5208..17ac17c4a344cab02b66af639e91ec33d9c23964 100644 (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 );
index a645f7d8b49eb425bf22cdc6d7e69a6e21d5b120..8a9996708f00a4cb10bd4e5394dc5887b12d8c7c 100644 (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 );
 
index 87c643aaff6afe1347b6d326a28ce6ad78e96ede..9941f865dca0888bed07f5e5c839b38d04eb98f5 100644 (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;
index f5ed80742f02c2f5032ed69e8e966994895c66e4..cb453fe715604580d854d98bf9e525e7ac73fe42 100644 (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,
index bd1601ce0da03b76c69c08a3afba93f3b84c9248..c2f0970ff9b1615a0e4381909ee6a11f99b1979e 100644 (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 */
index ac66a3f00864a38ed6b9f74d2b03b16b33869053..fec476894d3d82e4c489f940de4a7dc3b629d17b 100644 (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 );