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];
61 mi = ( struct monitorinfo * )be->be_private;
63 if ( monitor_cache_get( mi,
64 &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
66 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
67 "monitor_subsys_time_init: "
68 "unable to get entry '%s'\n",
69 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
71 Debug( LDAP_DEBUG_ANY,
72 "monitor_subsys_time_init: "
73 "unable to get entry '%s'\n%s%s",
74 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
85 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
86 ztm = gmtime( &starttime );
87 strftime( ztmbuf, sizeof(ztmbuf), "%Y%m%d%H%M%SZ", ztm );
88 ltm = localtime( &starttime );
89 local_time( ltm, -timezone, ltmbuf, sizeof( ltmbuf ) );
90 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
91 snprintf( buf, sizeof( buf ),
93 SLAPD_MONITOR_OBJECTCLASSES
96 "description;lang-x-local: %s",
97 monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
100 e = str2entry( buf );
103 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
104 "monitor_subsys_time_init: "
105 "unable to create entry 'cn=Start,%s'\n",
106 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
108 Debug( LDAP_DEBUG_ANY,
109 "monitor_subsys_time_init: "
110 "unable to create entry 'cn=Start,%s'\n%s%s",
111 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
117 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
118 e->e_private = ( void * )mp;
120 mp->mp_children = NULL;
121 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
122 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
123 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
125 if ( monitor_cache_add( mi, e ) ) {
127 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
128 "monitor_subsys_time_init: "
129 "unable to add entry 'cn=Start,%s'\n",
130 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
132 Debug( LDAP_DEBUG_ANY,
133 "monitor_subsys_time_init: "
134 "unable to add entry 'cn=Start,%s'\n%s%s",
135 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
146 snprintf( buf, sizeof( buf ),
147 "dn: cn=Current,%s\n"
148 SLAPD_MONITOR_OBJECTCLASSES
151 "description;lang-x-local: %s",
152 monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
155 e = str2entry( buf );
158 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
159 "monitor_subsys_time_init: "
160 "unable to create entry 'cn=Current,%s'\n",
161 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
163 Debug( LDAP_DEBUG_ANY,
164 "monitor_subsys_time_init: "
165 "unable to create entry 'cn=Current,%s'\n%s%s",
166 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
172 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
173 e->e_private = ( void * )mp;
175 mp->mp_children = NULL;
176 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
177 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
178 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
180 if ( monitor_cache_add( mi, e ) ) {
182 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
183 "monitor_subsys_time_init: "
184 "unable to add 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 add entry 'cn=Current,%s'\n%s%s",
190 monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
198 mp = ( struct monitorentrypriv * )e_time->e_private;
199 mp->mp_children = e_tmp;
201 monitor_cache_release( mi, e_time );
207 monitor_subsys_time_update(
208 struct monitorinfo *mi,
212 char ztmbuf[20], ltmbuf[20];
213 struct tm *ztm, *ltm;
216 static AttributeDescription *ad_local = NULL;
217 const char *text = NULL;
220 static int init_start = 0;
225 if ( init_start == 0 && strncmp( e->e_nname.bv_val, "cn=START",
226 sizeof("cn=START")-1 ) == 0 ) {
227 currenttime = starttime;
230 } else if ( strncmp( e->e_nname.bv_val, "cn=CURRENT",
231 sizeof("cn=CURRENT")-1 ) == 0 ) {
232 currenttime = slap_get_time();
238 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
239 ztm = gmtime( ¤ttime );
240 strftime( ztmbuf, sizeof( ztmbuf ), "%Y%m%d%H%M%SZ", ztm );
241 ltm = localtime( ¤ttime );
242 local_time( ltm, -timezone, ltmbuf, sizeof( ltmbuf ) );
243 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
245 a = attr_find( e->e_attrs, monitor_ad_desc );
250 len = strlen( ztmbuf );
251 assert( len == a->a_vals[0].bv_len );
252 AC_MEMCPY( a->a_vals[0].bv_val, ztmbuf, len );
254 if ( ad_local == NULL ) {
255 if ( slap_str2ad( "description;lang-x-local",
256 &ad_local, &text ) != LDAP_SUCCESS ) {
260 a = attr_find( e->e_attrs, ad_local );
265 len = strlen( ltmbuf );
266 assert( len == a->a_vals[0].bv_len );
267 AC_MEMCPY( a->a_vals[0].bv_val, ltmbuf, len );
273 * assumes gmtime_mutex is locked
276 local_time( const struct tm *ltm, long delta, char *buf, size_t len )
283 strftime( buf, len, "%Y%m%d%H%M%S", ltm );
295 snprintf( p, len - 15, "%02ld%02ld", delta / 3600, delta % 3600 );