]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/time.c
added local time under 'lang' (lang-x-local)
[openldap] / servers / slapd / back-monitor / time.c
1 /* time.c - deal with time subsystem */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
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.
12  * 
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:
16  * 
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
19  *    flaws in it.
20  * 
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.
24  * 
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
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37 #include <ac/string.h>
38 #include <ac/time.h>
39
40 #include "slap.h"
41 #include "proto-slap.h"
42 #include "back-monitor.h"
43
44 static int
45 local_time( const struct tm *ztm, long delta, char *buf, size_t len );
46
47 int
48 monitor_subsys_time_init(
49         BackendDB               *be
50 )
51 {
52         struct monitorinfo      *mi;
53         
54         Entry                   *e, *e_tmp, *e_time;
55         struct monitorentrypriv *mp;
56         char                    buf[1024], ztmbuf[20], ltmbuf[20];
57         struct tm               *ztm, *ltm;
58
59         assert( be != NULL );
60
61         mi = ( struct monitorinfo * )be->be_private;
62
63         if ( monitor_cache_get( mi,
64                         &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
65 #ifdef NEW_LOGGING
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 ));
70 #else
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, 
75                         "", "" );
76 #endif
77                 return( -1 );
78         }
79
80         e_tmp = NULL;
81
82         /*
83          * Start
84          */
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 ),
92                         "dn: cn=Start,%s\n"
93                         SLAPD_MONITOR_OBJECTCLASSES
94                         "cn: Start\n"
95                         "description: %s\n"
96                         "description;lang-x-local: %s",
97                         monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
98                         ztmbuf, ltmbuf );
99
100         e = str2entry( buf );
101         if ( e == NULL ) {
102 #ifdef NEW_LOGGING
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 ));
107 #else
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,
112                         "", "" );
113 #endif
114                 return( -1 );
115         }
116         
117         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
118         e->e_private = ( void * )mp;
119         mp->mp_next = e_tmp;
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;
124
125         if ( monitor_cache_add( mi, e ) ) {
126 #ifdef NEW_LOGGING
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 ));
131 #else
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,
136                         "", "" );
137 #endif
138                 return( -1 );
139         }
140         
141         e_tmp = e;
142
143         /*
144          * Current
145          */
146         snprintf( buf, sizeof( buf ),
147                         "dn: cn=Current,%s\n"
148                         SLAPD_MONITOR_OBJECTCLASSES
149                         "cn: Current\n"
150                         "description: %s\n"
151                         "description;lang-x-local: %s",
152                         monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
153                         ztmbuf, ltmbuf );
154
155         e = str2entry( buf );
156         if ( e == NULL ) {
157 #ifdef NEW_LOGGING
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 ));
162 #else
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,
167                         "", "" );
168 #endif
169                 return( -1 );
170         }
171         
172         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
173         e->e_private = ( void * )mp;
174         mp->mp_next = e_tmp;
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;
179
180         if ( monitor_cache_add( mi, e ) ) {
181 #ifdef NEW_LOGGING
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 ));
186 #else
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,
191                         "", "" );
192 #endif
193                 return( -1 );
194         }
195         
196         e_tmp = e;
197
198         mp = ( struct monitorentrypriv * )e_time->e_private;
199         mp->mp_children = e_tmp;
200
201         monitor_cache_release( mi, e_time );
202
203         return( 0 );
204 }
205
206 int
207 monitor_subsys_time_update(
208         struct monitorinfo      *mi,
209         Entry                   *e
210 )
211 {
212         char            ztmbuf[20], ltmbuf[20];
213         struct tm       *ztm, *ltm;
214         time_t          currenttime;
215         Attribute       *a;
216         static AttributeDescription     *ad_local = NULL;
217         const char      *text = NULL;
218         ber_len_t       len;
219
220         static int      init_start = 0;
221
222         assert( mi );
223         assert( e );
224         
225         if ( init_start == 0 && strncmp( e->e_nname.bv_val, "cn=START",
226                                 sizeof("cn=START")-1 ) == 0 ) {
227                 currenttime = starttime;
228                 init_start = 1;
229
230         } else if ( strncmp( e->e_nname.bv_val, "cn=CURRENT",
231                                 sizeof("cn=CURRENT")-1 ) == 0 ) {
232                 currenttime = slap_get_time();
233
234         } else {
235                 return( 0 );
236         }
237
238         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
239         ztm = gmtime( &currenttime );
240         strftime( ztmbuf, sizeof( ztmbuf ), "%Y%m%d%H%M%SZ", ztm );
241         ltm = localtime( &currenttime );
242         local_time( ltm, -timezone, ltmbuf, sizeof( ltmbuf ) );
243         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
244
245         a = attr_find( e->e_attrs, monitor_ad_desc );
246         if ( a == NULL ) {
247                 return( -1 );
248         }
249
250         len = strlen( ztmbuf );
251         assert( len == a->a_vals[0].bv_len );
252         AC_MEMCPY( a->a_vals[0].bv_val, ztmbuf, len );
253
254         if ( ad_local == NULL ) {
255                 if ( slap_str2ad( "description;lang-x-local", 
256                                         &ad_local, &text ) != LDAP_SUCCESS ) {
257                         return( -1 );
258                 }
259         }
260         a = attr_find( e->e_attrs, ad_local );
261         if ( a == NULL ) {
262                 return( -1 );
263         }
264
265         len = strlen( ltmbuf );
266         assert( len == a->a_vals[0].bv_len );
267         AC_MEMCPY( a->a_vals[0].bv_val, ltmbuf, len );
268
269         return( 0 );
270 }
271
272 /*
273  * assumes gmtime_mutex is locked
274  */
275 static int
276 local_time( const struct tm *ltm, long delta, char *buf, size_t len )
277 {
278         char *p;
279
280         if ( len < 20 ) {
281                 return -1;
282         }
283         strftime( buf, len, "%Y%m%d%H%M%S", ltm );
284
285         p = buf + 14;
286
287         if ( delta < 0 ) {
288                 p[ 0 ] = '-';
289                 delta = -delta;
290         } else {
291                 p[ 0 ] = '+';
292         }
293         p++;
294
295         snprintf( p, len - 15, "%02ld%02ld", delta / 3600, delta % 3600 );
296         
297         return 0;
298 }
299