1 /* time.c - deal with time subsystem */
3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
9 * This work has beed deveolped for the OpenLDAP Foundation
10 * in the hope that it may be useful to the Open Source community,
11 * but WITHOUT ANY WARRANTY.
13 * Permission is granted to anyone to use this software for any purpose
14 * on any computer system, and to alter it and redistribute it, subject
15 * to the following restrictions:
17 * 1. The author and SysNet s.n.c. are not responsible for the consequences
18 * of use of this software, no matter how awful, even if they arise from
21 * 2. The origin of this software must not be misrepresented, either by
22 * explicit claim or by omission. Since few users ever read sources,
23 * credits should appear in the documentation.
25 * 3. Altered versions must be plainly marked as such, and must not be
26 * misrepresented as being the original software. Since few users
27 * ever read sources, credits should appear in the documentation.
28 * SysNet s.n.c. cannot be responsible for the consequences of the
31 * 4. This notice may not be removed or altered.
37 #include <ac/string.h>
41 #include "proto-slap.h"
42 #include "back-monitor.h"
44 #ifdef HACK_LOCAL_TIME
46 local_time( const struct tm *ztm, long delta, char *buf, size_t len );
47 #endif /* HACK_LOCAL_TIME */
50 monitor_subsys_time_init(
54 struct monitorinfo *mi;
56 Entry *e, *e_tmp, *e_time;
57 struct monitorentrypriv *mp;
63 * Note: ltmbuf, ltm are used only if HACK_LOCAL_TIME is defined
68 mi = ( struct monitorinfo * )be->be_private;
70 if ( monitor_cache_get( mi,
71 &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
73 LDAP_LOG( OPERATION, CRIT,
74 "monitor_subsys_time_init: "
75 "unable to get entry '%s'\n",
76 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
78 Debug( LDAP_DEBUG_ANY,
79 "monitor_subsys_time_init: "
80 "unable to get entry '%s'\n%s%s",
81 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
92 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
93 #ifdef HACK_LOCAL_TIME
94 tms = localtime( &starttime );
95 local_time( tms, -timezone, tmbuf, sizeof( tmbuf ) );
96 #else /* !HACK_LOCAL_TIME */
97 tms = gmtime( &starttime );
98 lutil_gentime( tmbuf, sizeof(tmbuf), tms );
99 #endif /* !HACK_LOCAL_TIME */
100 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
101 snprintf( buf, sizeof( buf ),
103 SLAPD_MONITOR_OBJECTCLASSES
105 "createTimestamp: %s",
106 monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
109 e = str2entry( buf );
112 LDAP_LOG( OPERATION, CRIT,
113 "monitor_subsys_time_init: "
114 "unable to create entry 'cn=Start,%s'\n",
115 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
117 Debug( LDAP_DEBUG_ANY,
118 "monitor_subsys_time_init: "
119 "unable to create entry 'cn=Start,%s'\n%s%s",
120 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
126 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
127 e->e_private = ( void * )mp;
129 mp->mp_children = NULL;
130 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
131 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
132 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
134 if ( monitor_cache_add( mi, e ) ) {
136 LDAP_LOG( OPERATION, CRIT,
137 "monitor_subsys_time_init: "
138 "unable to add entry 'cn=Start,%s'\n",
139 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
141 Debug( LDAP_DEBUG_ANY,
142 "monitor_subsys_time_init: "
143 "unable to add entry 'cn=Start,%s'\n%s%s",
144 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
155 snprintf( buf, sizeof( buf ),
156 "dn: cn=Current,%s\n"
157 SLAPD_MONITOR_OBJECTCLASSES
159 "createTimestamp: %s\n"
160 "modifyTimestamp: %s",
161 monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
164 e = str2entry( buf );
167 LDAP_LOG( OPERATION, CRIT,
168 "monitor_subsys_time_init: "
169 "unable to create entry 'cn=Current,%s'\n",
170 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
172 Debug( LDAP_DEBUG_ANY,
173 "monitor_subsys_time_init: "
174 "unable to create entry 'cn=Current,%s'\n%s%s",
175 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
181 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
182 e->e_private = ( void * )mp;
184 mp->mp_children = NULL;
185 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
186 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
187 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
189 if ( monitor_cache_add( mi, e ) ) {
191 LDAP_LOG( OPERATION, CRIT,
192 "monitor_subsys_time_init: "
193 "unable to add entry 'cn=Current,%s'\n",
194 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
196 Debug( LDAP_DEBUG_ANY,
197 "monitor_subsys_time_init: "
198 "unable to add entry 'cn=Current,%s'\n%s%s",
199 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
207 mp = ( struct monitorentrypriv * )e_time->e_private;
208 mp->mp_children = e_tmp;
210 monitor_cache_release( mi, e_time );
216 monitor_subsys_time_update(
217 struct monitorinfo *mi,
221 char stmbuf[20], ctmbuf[20];
222 struct tm *stm, *ctm;
226 static int init_start = 0, init_current = 0;
228 #define ENTRY_START 1
229 #define ENTRY_CURRENT 2
230 int entry = ENTRY_TIME;
235 if ( strncmp( e->e_nname.bv_val, "cn=start",
236 sizeof("cn=start")-1 ) == 0 ) {
238 if ( init_start == 1 ) {
242 } else if ( strncmp( e->e_nname.bv_val, "cn=current",
243 sizeof("cn=current")-1 ) == 0 ) {
244 entry = ENTRY_CURRENT;
247 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
248 if ( init_start == 0 ) {
249 #ifdef HACK_LOCAL_TIME
250 stm = localtime( &starttime );
251 local_time( stm, -timezone, stmbuf, sizeof( stmbuf ) );
252 #else /* !HACK_LOCAL_TIME */
253 stm = gmtime( &starttime );
254 lutil_gentime( stmbuf, sizeof( stmbuf ), stm );
255 #endif /* !HACK_LOCAL_TIME */
258 if ( entry == ENTRY_CURRENT ) {
259 time_t currentTime = slap_get_time();
260 #ifdef HACK_LOCAL_TIME
261 ctm = localtime( ¤tTime );
262 local_time( ctm, -timezone, ctmbuf, sizeof( ctmbuf ) );
263 #else /* !HACK_LOCAL_TIME */
264 ctm = gmtime( ¤tTime );
265 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
266 #endif /* !HACK_LOCAL_TIME */
268 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
270 if ( ( entry == ENTRY_START && init_start == 0 )
271 || ( entry == ENTRY_CURRENT && init_current == 0 ) ) {
272 a = attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp );
277 len = strlen( stmbuf );
278 assert( len == a->a_vals[0].bv_len );
279 AC_MEMCPY( a->a_vals[0].bv_val, stmbuf, len );
281 if ( entry == ENTRY_START ) {
283 } else if ( entry == ENTRY_CURRENT ) {
288 if ( entry == ENTRY_CURRENT ) {
289 a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
294 len = strlen( ctmbuf );
295 assert( len == a->a_vals[0].bv_len );
296 AC_MEMCPY( a->a_vals[0].bv_val, ctmbuf, len );
302 #ifdef HACK_LOCAL_TIME
304 * assumes gmtime_mutex is locked
307 local_time( const struct tm *ltm, long delta, char *buf, size_t len )
314 strftime( buf, len, "%Y%m%d%H%M%S", ltm );
326 snprintf( p, len - 15, "%02ld%02ld", delta / 3600, delta % 3600 );
330 #endif /* HACK_LOCAL_TIME */