]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/time.c
cleanup
[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 <lutil.h>
41
42 #include "slap.h"
43 #include "proto-slap.h"
44 #include "back-monitor.h"
45
46 #ifdef HACK_LOCAL_TIME
47 static int
48 local_time( const struct tm *ztm, long delta, char *buf, size_t len );
49 #endif /* HACK_LOCAL_TIME */
50
51 int
52 monitor_subsys_time_init(
53         BackendDB               *be
54 )
55 {
56         struct monitorinfo      *mi;
57         
58         Entry                   *e, *e_tmp, *e_time;
59         struct monitorentrypriv *mp;
60         char                    buf[1024];
61         struct tm               *tms;
62         char                    tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
63
64         /*
65          * Note: ltmbuf, ltm are used only if HACK_LOCAL_TIME is defined
66          */
67
68         assert( be != NULL );
69
70         mi = ( struct monitorinfo * )be->be_private;
71
72         if ( monitor_cache_get( mi,
73                         &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
74 #ifdef NEW_LOGGING
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 );
79 #else
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, 
84                         "", "" );
85 #endif
86                 return( -1 );
87         }
88
89         e_tmp = NULL;
90
91         /*
92          * Start
93          */
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 ),
104                         "dn: cn=Start,%s\n"
105                         SLAPD_MONITOR_OBJECTCLASSES
106                         "cn: Start\n"
107                         "createTimestamp: %s", 
108                         monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
109                         tmbuf );
110
111         e = str2entry( buf );
112         if ( e == NULL ) {
113 #ifdef NEW_LOGGING
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 );
118 #else
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,
123                         "", "" );
124 #endif
125                 return( -1 );
126         }
127         
128         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
129         e->e_private = ( void * )mp;
130         mp->mp_next = e_tmp;
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;
135
136         if ( monitor_cache_add( mi, e ) ) {
137 #ifdef NEW_LOGGING
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 );
142 #else
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,
147                         "", "" );
148 #endif
149                 return( -1 );
150         }
151         
152         e_tmp = e;
153
154         /*
155          * Current
156          */
157         snprintf( buf, sizeof( buf ),
158                         "dn: cn=Current,%s\n"
159                         SLAPD_MONITOR_OBJECTCLASSES
160                         "cn: Current\n"
161                         "createTimestamp: %s\n"
162                         "modifyTimestamp: %s",
163                         monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
164                         tmbuf, tmbuf );
165
166         e = str2entry( buf );
167         if ( e == NULL ) {
168 #ifdef NEW_LOGGING
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 );
173 #else
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,
178                         "", "" );
179 #endif
180                 return( -1 );
181         }
182         
183         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
184         e->e_private = ( void * )mp;
185         mp->mp_next = e_tmp;
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;
190
191         if ( monitor_cache_add( mi, e ) ) {
192 #ifdef NEW_LOGGING
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 );
197 #else
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,
202                         "", "" );
203 #endif
204                 return( -1 );
205         }
206         
207         e_tmp = e;
208
209         mp = ( struct monitorentrypriv * )e_time->e_private;
210         mp->mp_children = e_tmp;
211
212         monitor_cache_release( mi, e_time );
213
214         return( 0 );
215 }
216
217 int
218 monitor_subsys_time_update(
219         struct monitorinfo      *mi,
220         Entry                   *e
221 )
222 {
223         char            stmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ],
224                         ctmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
225         struct tm       *stm, *ctm;
226         Attribute       *a;
227         ber_len_t       len;
228
229         static int      init_start = 0, init_current = 0;
230 #define ENTRY_TIME      0
231 #define ENTRY_START     1
232 #define ENTRY_CURRENT   2
233         int             entry = ENTRY_TIME;
234
235         assert( mi );
236         assert( e );
237         
238         if ( strncmp( e->e_nname.bv_val, "cn=start", 
239                                 sizeof("cn=start")-1 ) == 0 ) {
240                 entry = ENTRY_START;
241                 if ( init_start == 1 ) {
242                         return( 0 );
243                 }
244
245         } else if ( strncmp( e->e_nname.bv_val, "cn=current",
246                                 sizeof("cn=current")-1 ) == 0 ) {
247                 entry = ENTRY_CURRENT;
248         }
249         
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 */
259         }
260
261         if ( entry == ENTRY_CURRENT ) {
262                 time_t currentTime = slap_get_time();
263 #ifdef HACK_LOCAL_TIME
264                 ctm = localtime( &currentTime );
265                 local_time( ctm, -timezone, ctmbuf, sizeof( ctmbuf ) );
266 #else /* !HACK_LOCAL_TIME */
267                 ctm = gmtime( &currentTime );
268                 lutil_gentime( ctmbuf, sizeof( ctmbuf ), ctm );
269 #endif /* !HACK_LOCAL_TIME */
270         }
271         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
272
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 );
276                 if ( a == NULL ) {
277                         return( -1 );
278                 }
279
280                 len = strlen( stmbuf );
281                 assert( len == a->a_vals[0].bv_len );
282                 AC_MEMCPY( a->a_vals[0].bv_val, stmbuf, len );
283
284                 if ( entry == ENTRY_START ) {
285                         init_start = 1;
286                 } else if ( entry == ENTRY_CURRENT ) {
287                         init_current = 1;
288                 }
289         }
290
291         if ( entry == ENTRY_CURRENT ) {
292                 a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
293                 if ( a == NULL ) {
294                         return( -1 );
295                 }
296
297                 len = strlen( ctmbuf );
298                 assert( len == a->a_vals[0].bv_len );
299                 AC_MEMCPY( a->a_vals[0].bv_val, ctmbuf, len );
300         }
301
302         return( 0 );
303 }
304
305 #ifdef HACK_LOCAL_TIME
306 /*
307  * assumes gmtime_mutex is locked
308  */
309 static int
310 local_time( const struct tm *ltm, long delta, char *buf, size_t len )
311 {
312         char *p;
313
314         if ( len < 20 ) {
315                 return -1;
316         }
317         strftime( buf, len, "%Y%m%d%H%M%S", ltm );
318
319         p = buf + 14;
320
321         if ( delta < 0 ) {
322                 p[ 0 ] = '-';
323                 delta = -delta;
324         } else {
325                 p[ 0 ] = '+';
326         }
327         p++;
328
329         snprintf( p, len - 15, "%02ld%02ld", delta / 3600, delta % 3600 );
330         
331         return 0;
332 }
333 #endif /* HACK_LOCAL_TIME */
334