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"
45 local_time( const struct tm *ztm, long delta, char *buf, size_t len );
48 monitor_subsys_time_init(
52 struct monitorinfo *mi;
54 Entry *e, *e_tmp, *e_time;
55 struct monitorentrypriv *mp;
56 char buf[1024], ztmbuf[20], ltmbuf[20];
60 * Note: ltmbuf, ltm are used only if HACK_LOCAL_TIME is defined
65 mi = ( struct monitorinfo * )be->be_private;
67 if ( monitor_cache_get( mi,
68 &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
70 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
71 "monitor_subsys_time_init: "
72 "unable to get entry '%s'\n",
73 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
75 Debug( LDAP_DEBUG_ANY,
76 "monitor_subsys_time_init: "
77 "unable to get entry '%s'\n%s%s",
78 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
89 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
90 ztm = gmtime( &starttime );
91 strftime( ztmbuf, sizeof(ztmbuf), "%Y%m%d%H%M%SZ", ztm );
92 #ifdef HACK_LOCAL_TIME
93 ltm = localtime( &starttime );
94 local_time( ltm, -timezone, ltmbuf, sizeof( ltmbuf ) );
95 #endif /* HACK_LOCAL_TIME */
96 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
97 snprintf( buf, sizeof( buf ),
99 SLAPD_MONITOR_OBJECTCLASSES
102 #ifdef HACK_LOCAL_TIME
103 "description;lang-x-local: %s"
104 #endif /* HACK_LOCAL_TIME */
105 , monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
107 #ifdef HACK_LOCAL_TIME
109 #endif /* HACK_LOCAL_TIME */
112 e = str2entry( buf );
115 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
116 "monitor_subsys_time_init: "
117 "unable to create entry 'cn=Start,%s'\n",
118 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
120 Debug( LDAP_DEBUG_ANY,
121 "monitor_subsys_time_init: "
122 "unable to create entry 'cn=Start,%s'\n%s%s",
123 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
129 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
130 e->e_private = ( void * )mp;
132 mp->mp_children = NULL;
133 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
134 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
135 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
137 if ( monitor_cache_add( mi, e ) ) {
139 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
140 "monitor_subsys_time_init: "
141 "unable to add entry 'cn=Start,%s'\n",
142 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
144 Debug( LDAP_DEBUG_ANY,
145 "monitor_subsys_time_init: "
146 "unable to add entry 'cn=Start,%s'\n%s%s",
147 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
158 snprintf( buf, sizeof( buf ),
159 "dn: cn=Current,%s\n"
160 SLAPD_MONITOR_OBJECTCLASSES
163 #ifdef HACK_LOCAL_TIME
164 "description;lang-x-local: %s"
165 #endif /* HACK_LOCAL_TIME */
166 , monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
168 #ifdef HACK_LOCAL_TIME
170 #endif /* HACK_LOCAL_TIME */
173 e = str2entry( buf );
176 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
177 "monitor_subsys_time_init: "
178 "unable to create entry 'cn=Current,%s'\n",
179 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
181 Debug( LDAP_DEBUG_ANY,
182 "monitor_subsys_time_init: "
183 "unable to create entry 'cn=Current,%s'\n%s%s",
184 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
190 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
191 e->e_private = ( void * )mp;
193 mp->mp_children = NULL;
194 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
195 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
196 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
198 if ( monitor_cache_add( mi, e ) ) {
200 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
201 "monitor_subsys_time_init: "
202 "unable to add entry 'cn=Current,%s'\n",
203 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
205 Debug( LDAP_DEBUG_ANY,
206 "monitor_subsys_time_init: "
207 "unable to add entry 'cn=Current,%s'\n%s%s",
208 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
216 mp = ( struct monitorentrypriv * )e_time->e_private;
217 mp->mp_children = e_tmp;
219 monitor_cache_release( mi, e_time );
225 monitor_subsys_time_update(
226 struct monitorinfo *mi,
230 char ztmbuf[20], ltmbuf[20];
231 struct tm *ztm, *ltm;
234 static AttributeDescription *ad_local = NULL;
235 const char *text = NULL;
238 static int init_start = 0;
241 * Note: ltmbuf, ltm, ad_local, text are used only if HACK_LOCAL_TIME
248 if ( init_start == 0 && strncmp( e->e_nname.bv_val, "cn=START",
249 sizeof("cn=START")-1 ) == 0 ) {
250 currenttime = starttime;
253 } else if ( strncmp( e->e_nname.bv_val, "cn=CURRENT",
254 sizeof("cn=CURRENT")-1 ) == 0 ) {
255 currenttime = slap_get_time();
261 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
262 ztm = gmtime( ¤ttime );
263 strftime( ztmbuf, sizeof( ztmbuf ), "%Y%m%d%H%M%SZ", ztm );
264 #ifdef HACK_LOCAL_TIME
265 ltm = localtime( ¤ttime );
266 local_time( ltm, -timezone, ltmbuf, sizeof( ltmbuf ) );
267 #endif /* HACK_LOCAL_TIME */
268 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
270 a = attr_find( e->e_attrs, monitor_ad_desc );
275 len = strlen( ztmbuf );
276 assert( len == a->a_vals[0].bv_len );
277 AC_MEMCPY( a->a_vals[0].bv_val, ztmbuf, len );
279 #ifdef HACK_LOCAL_TIME
280 if ( ad_local == NULL ) {
281 if ( slap_str2ad( "description;lang-x-local",
282 &ad_local, &text ) != LDAP_SUCCESS ) {
286 a = attr_find( e->e_attrs, ad_local );
291 len = strlen( ltmbuf );
292 assert( len == a->a_vals[0].bv_len );
293 AC_MEMCPY( a->a_vals[0].bv_val, ltmbuf, len );
294 #endif /* HACK_LOCAL_TIME */
300 * assumes gmtime_mutex is locked
303 local_time( const struct tm *ltm, long delta, char *buf, size_t len )
310 strftime( buf, len, "%Y%m%d%H%M%S", ltm );
322 snprintf( p, len - 15, "%02ld%02ld", delta / 3600, delta % 3600 );