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;
58 char buf[1024], ztmbuf[20];
60 #ifdef HACK_LOCAL_TIME
66 * Note: ltmbuf, ltm are used only if HACK_LOCAL_TIME is defined
71 mi = ( struct monitorinfo * )be->be_private;
73 if ( monitor_cache_get( mi,
74 &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
76 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
77 "monitor_subsys_time_init: "
78 "unable to get entry '%s'\n",
79 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
81 Debug( LDAP_DEBUG_ANY,
82 "monitor_subsys_time_init: "
83 "unable to get entry '%s'\n%s%s",
84 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
95 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
96 ztm = gmtime( &starttime );
97 strftime( ztmbuf, sizeof(ztmbuf), "%Y%m%d%H%M%SZ", ztm );
98 #ifdef HACK_LOCAL_TIME
99 ltm = localtime( &starttime );
100 local_time( ltm, -timezone, ltmbuf, sizeof( ltmbuf ) );
101 #endif /* HACK_LOCAL_TIME */
102 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
103 snprintf( buf, sizeof( buf ),
105 SLAPD_MONITOR_OBJECTCLASSES
108 #ifdef HACK_LOCAL_TIME
109 "description;lang-x-local: %s"
110 #endif /* HACK_LOCAL_TIME */
111 , monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
113 #ifdef HACK_LOCAL_TIME
115 #endif /* HACK_LOCAL_TIME */
118 e = str2entry( buf );
121 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
122 "monitor_subsys_time_init: "
123 "unable to create entry 'cn=Start,%s'\n",
124 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
126 Debug( LDAP_DEBUG_ANY,
127 "monitor_subsys_time_init: "
128 "unable to create entry 'cn=Start,%s'\n%s%s",
129 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
135 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
136 e->e_private = ( void * )mp;
138 mp->mp_children = NULL;
139 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
140 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
141 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
143 if ( monitor_cache_add( mi, e ) ) {
145 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
146 "monitor_subsys_time_init: "
147 "unable to add entry 'cn=Start,%s'\n",
148 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
150 Debug( LDAP_DEBUG_ANY,
151 "monitor_subsys_time_init: "
152 "unable to add entry 'cn=Start,%s'\n%s%s",
153 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
164 snprintf( buf, sizeof( buf ),
165 "dn: cn=Current,%s\n"
166 SLAPD_MONITOR_OBJECTCLASSES
169 #ifdef HACK_LOCAL_TIME
170 "description;lang-x-local: %s"
171 #endif /* HACK_LOCAL_TIME */
172 , monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
174 #ifdef HACK_LOCAL_TIME
176 #endif /* HACK_LOCAL_TIME */
179 e = str2entry( buf );
182 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
183 "monitor_subsys_time_init: "
184 "unable to create entry 'cn=Current,%s'\n",
185 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
187 Debug( LDAP_DEBUG_ANY,
188 "monitor_subsys_time_init: "
189 "unable to create entry 'cn=Current,%s'\n%s%s",
190 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
196 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
197 e->e_private = ( void * )mp;
199 mp->mp_children = NULL;
200 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
201 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
202 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
204 if ( monitor_cache_add( mi, e ) ) {
206 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
207 "monitor_subsys_time_init: "
208 "unable to add entry 'cn=Current,%s'\n",
209 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
211 Debug( LDAP_DEBUG_ANY,
212 "monitor_subsys_time_init: "
213 "unable to add entry 'cn=Current,%s'\n%s%s",
214 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
222 mp = ( struct monitorentrypriv * )e_time->e_private;
223 mp->mp_children = e_tmp;
225 monitor_cache_release( mi, e_time );
231 monitor_subsys_time_update(
232 struct monitorinfo *mi,
238 #ifdef HACK_LOCAL_TIME
241 #endif /* HACK_LOCAL_TIME */
244 #ifdef HACK_LOCAL_TIME
245 static AttributeDescription *ad_local = NULL;
246 const char *text = NULL;
247 #endif /* HACK_LOCAL_TIME */
250 static int init_start = 0;
253 * Note: ltmbuf, ltm, ad_local, text are used only if HACK_LOCAL_TIME
260 if ( init_start == 0 && strncmp( e->e_nname.bv_val, "cn=START",
261 sizeof("cn=START")-1 ) == 0 ) {
262 currenttime = starttime;
265 } else if ( strncmp( e->e_nname.bv_val, "cn=CURRENT",
266 sizeof("cn=CURRENT")-1 ) == 0 ) {
267 currenttime = slap_get_time();
273 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
274 ztm = gmtime( ¤ttime );
275 strftime( ztmbuf, sizeof( ztmbuf ), "%Y%m%d%H%M%SZ", ztm );
276 #ifdef HACK_LOCAL_TIME
277 ltm = localtime( ¤ttime );
278 local_time( ltm, -timezone, ltmbuf, sizeof( ltmbuf ) );
279 #endif /* HACK_LOCAL_TIME */
280 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
282 a = attr_find( e->e_attrs, monitor_ad_desc );
287 len = strlen( ztmbuf );
288 assert( len == a->a_vals[0].bv_len );
289 AC_MEMCPY( a->a_vals[0].bv_val, ztmbuf, len );
291 #ifdef HACK_LOCAL_TIME
292 if ( ad_local == NULL ) {
293 if ( slap_str2ad( "description;lang-x-local",
294 &ad_local, &text ) != LDAP_SUCCESS ) {
298 a = attr_find( e->e_attrs, ad_local );
303 len = strlen( ltmbuf );
304 assert( len == a->a_vals[0].bv_len );
305 AC_MEMCPY( a->a_vals[0].bv_val, ltmbuf, len );
306 #endif /* HACK_LOCAL_TIME */
311 #ifdef HACK_LOCAL_TIME
313 * assumes gmtime_mutex is locked
316 local_time( const struct tm *ltm, long delta, char *buf, size_t len )
323 strftime( buf, len, "%Y%m%d%H%M%S", ltm );
335 snprintf( p, len - 15, "%02ld%02ld", delta / 3600, delta % 3600 );
339 #endif /* HACK_LOCAL_TIME */