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>
43 #include "proto-slap.h"
44 #include "back-monitor.h"
46 #ifdef HACK_LOCAL_TIME
48 local_time( const struct tm *ztm, long delta, char *buf, size_t len );
49 #endif /* HACK_LOCAL_TIME */
52 monitor_subsys_time_init(
56 struct monitorinfo *mi;
58 Entry *e, *e_tmp, *e_time;
59 struct monitorentrypriv *mp;
62 char tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
65 * Note: ltmbuf, ltm are used only if HACK_LOCAL_TIME is defined
70 mi = ( struct monitorinfo * )be->be_private;
72 if ( monitor_cache_get( mi,
73 &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
75 LDAP_LOG( OPERATION, CRIT,
76 "monitor_subsys_time_init: "
77 "unable to get entry '%s'\n",
78 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
80 Debug( LDAP_DEBUG_ANY,
81 "monitor_subsys_time_init: "
82 "unable to get entry '%s'\n%s%s",
83 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
94 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
95 #ifdef HACK_LOCAL_TIME
96 tms = localtime( &starttime );
97 local_time( tms, -timezone, tmbuf, sizeof( tmbuf ) );
98 #else /* !HACK_LOCAL_TIME */
99 tms = gmtime( &starttime );
100 lutil_gentime( tmbuf, sizeof(tmbuf), tms );
101 #endif /* !HACK_LOCAL_TIME */
102 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
103 snprintf( buf, sizeof( buf ),
105 SLAPD_MONITOR_OBJECTCLASSES
107 "createTimestamp: %s",
108 monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
111 e = str2entry( buf );
114 LDAP_LOG( OPERATION, CRIT,
115 "monitor_subsys_time_init: "
116 "unable to create entry 'cn=Start,%s'\n",
117 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
119 Debug( LDAP_DEBUG_ANY,
120 "monitor_subsys_time_init: "
121 "unable to create entry 'cn=Start,%s'\n%s%s",
122 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
128 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
129 e->e_private = ( void * )mp;
131 mp->mp_children = NULL;
132 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
133 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
134 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
136 if ( monitor_cache_add( mi, e ) ) {
138 LDAP_LOG( OPERATION, CRIT,
139 "monitor_subsys_time_init: "
140 "unable to add entry 'cn=Start,%s'\n",
141 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
143 Debug( LDAP_DEBUG_ANY,
144 "monitor_subsys_time_init: "
145 "unable to add entry 'cn=Start,%s'\n%s%s",
146 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
157 snprintf( buf, sizeof( buf ),
158 "dn: cn=Current,%s\n"
159 SLAPD_MONITOR_OBJECTCLASSES
161 "createTimestamp: %s\n"
162 "modifyTimestamp: %s",
163 monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
166 e = str2entry( buf );
169 LDAP_LOG( OPERATION, CRIT,
170 "monitor_subsys_time_init: "
171 "unable to create entry 'cn=Current,%s'\n",
172 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
174 Debug( LDAP_DEBUG_ANY,
175 "monitor_subsys_time_init: "
176 "unable to create entry 'cn=Current,%s'\n%s%s",
177 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
183 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
184 e->e_private = ( void * )mp;
186 mp->mp_children = NULL;
187 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
188 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
189 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
191 if ( monitor_cache_add( mi, e ) ) {
193 LDAP_LOG( OPERATION, CRIT,
194 "monitor_subsys_time_init: "
195 "unable to add entry 'cn=Current,%s'\n",
196 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
198 Debug( LDAP_DEBUG_ANY,
199 "monitor_subsys_time_init: "
200 "unable to add entry 'cn=Current,%s'\n%s%s",
201 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
209 mp = ( struct monitorentrypriv * )e_time->e_private;
210 mp->mp_children = e_tmp;
212 monitor_cache_release( mi, e_time );
218 monitor_subsys_time_update(
219 struct monitorinfo *mi,
223 char stmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ],
224 ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
225 struct tm *stm, *ctm;
229 static int init_start = 0, init_current = 0;
231 #define ENTRY_START 1
232 #define ENTRY_CURRENT 2
233 int entry = ENTRY_TIME;
238 if ( strncmp( e->e_nname.bv_val, "cn=start",
239 sizeof("cn=start")-1 ) == 0 ) {
241 if ( init_start == 1 ) {
245 } else if ( strncmp( e->e_nname.bv_val, "cn=current",
246 sizeof("cn=current")-1 ) == 0 ) {
247 entry = ENTRY_CURRENT;
250 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
251 if ( init_start == 0 ) {
252 #ifdef HACK_LOCAL_TIME
253 stm = localtime( &starttime );
254 local_time( stm, -timezone, stmbuf, sizeof( stmbuf ) );
255 #else /* !HACK_LOCAL_TIME */
256 stm = gmtime( &starttime );
257 lutil_gentime( stmbuf, sizeof( stmbuf ), stm );
258 #endif /* !HACK_LOCAL_TIME */
261 if ( entry == ENTRY_CURRENT ) {
262 time_t currentTime = slap_get_time();
263 #ifdef HACK_LOCAL_TIME
264 ctm = localtime( ¤tTime );
265 local_time( ctm, -timezone, ctmbuf, sizeof( ctmbuf ) );
266 #else /* !HACK_LOCAL_TIME */
267 ctm = gmtime( ¤tTime );
268 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
269 #endif /* !HACK_LOCAL_TIME */
271 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
273 if ( ( entry == ENTRY_START && init_start == 0 )
274 || ( entry == ENTRY_CURRENT && init_current == 0 ) ) {
275 a = attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp );
280 len = strlen( stmbuf );
281 assert( len == a->a_vals[0].bv_len );
282 AC_MEMCPY( a->a_vals[0].bv_val, stmbuf, len );
284 if ( entry == ENTRY_START ) {
286 } else if ( entry == ENTRY_CURRENT ) {
291 if ( entry == ENTRY_CURRENT ) {
292 a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
297 len = strlen( ctmbuf );
298 assert( len == a->a_vals[0].bv_len );
299 AC_MEMCPY( a->a_vals[0].bv_val, ctmbuf, len );
305 #ifdef HACK_LOCAL_TIME
307 * assumes gmtime_mutex is locked
310 local_time( const struct tm *ltm, long delta, char *buf, size_t len )
317 strftime( buf, len, "%Y%m%d%H%M%S", ltm );
329 snprintf( p, len - 15, "%02ld%02ld", delta / 3600, delta % 3600 );
333 #endif /* HACK_LOCAL_TIME */