]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/time.c
0da78a8318ca7d8122b9cba0de277a5a42474e27
[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 int
45 monitor_subsys_time_init(
46         BackendDB               *be
47 )
48 {
49         struct monitorinfo      *mi;
50         
51         Entry                   *e, *e_tmp, *e_time;
52         struct monitorentrypriv *mp;
53         char                    buf[1024], tmbuf[20];
54         struct tm               *ltm;
55
56         assert( be != NULL );
57
58         mi = ( struct monitorinfo * )be->be_private;
59
60         if ( monitor_cache_get( mi,
61                         &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
62 #ifdef NEW_LOGGING
63                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
64                         "monitor_subsys_time_init: "
65                         "unable to get entry '%s'\n",
66                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
67 #else
68                 Debug( LDAP_DEBUG_ANY,
69                         "monitor_subsys_time_init: "
70                         "unable to get entry '%s'\n%s%s",
71                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 
72                         "", "" );
73 #endif
74                 return( -1 );
75         }
76
77         e_tmp = NULL;
78
79         /*
80          * Start
81          */
82         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
83         ltm = gmtime( &starttime );
84         strftime( tmbuf, sizeof(tmbuf), "%Y%m%d%H%M%SZ", ltm );
85         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
86         snprintf( buf, sizeof( buf ),
87                         "dn: cn=Start,%s\n"
88                         SLAPD_MONITOR_OBJECTCLASSES
89                         "cn: Start\n"
90                         "description: %s",
91                         monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
92                         tmbuf );
93
94         e = str2entry( buf );
95         if ( e == NULL ) {
96 #ifdef NEW_LOGGING
97                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
98                         "monitor_subsys_time_init: "
99                         "unable to create entry 'cn=Start,%s'\n",
100                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
101 #else
102                 Debug( LDAP_DEBUG_ANY,
103                         "monitor_subsys_time_init: "
104                         "unable to create entry 'cn=Start,%s'\n%s%s",
105                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
106                         "", "" );
107 #endif
108                 return( -1 );
109         }
110         
111         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
112         e->e_private = ( void * )mp;
113         mp->mp_next = e_tmp;
114         mp->mp_children = NULL;
115         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
116         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
117                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
118
119         if ( monitor_cache_add( mi, e ) ) {
120 #ifdef NEW_LOGGING
121                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
122                         "monitor_subsys_time_init: "
123                         "unable to add entry 'cn=Start,%s'\n",
124                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
125 #else
126                 Debug( LDAP_DEBUG_ANY,
127                         "monitor_subsys_time_init: "
128                         "unable to add entry 'cn=Start,%s'\n%s%s",
129                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
130                         "", "" );
131 #endif
132                 return( -1 );
133         }
134         
135         e_tmp = e;
136
137         /*
138          * Current
139          */
140         snprintf( buf, sizeof( buf ),
141                         "dn: cn=Current,%s\n"
142                         SLAPD_MONITOR_OBJECTCLASSES
143                         "cn: Current\n"
144                         "description: %s",
145                         monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
146                         tmbuf );
147
148         e = str2entry( buf );
149         if ( e == NULL ) {
150 #ifdef NEW_LOGGING
151                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
152                         "monitor_subsys_time_init: "
153                         "unable to create entry 'cn=Current,%s'\n",
154                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
155 #else
156                 Debug( LDAP_DEBUG_ANY,
157                         "monitor_subsys_time_init: "
158                         "unable to create entry 'cn=Current,%s'\n%s%s",
159                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
160                         "", "" );
161 #endif
162                 return( -1 );
163         }
164         
165         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
166         e->e_private = ( void * )mp;
167         mp->mp_next = e_tmp;
168         mp->mp_children = NULL;
169         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
170         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
171                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
172
173         if ( monitor_cache_add( mi, e ) ) {
174 #ifdef NEW_LOGGING
175                 LDAP_LOG(( "operation", LDAP_LEVEL_CRIT,
176                         "monitor_subsys_time_init: "
177                         "unable to add entry 'cn=Current,%s'\n",
178                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val ));
179 #else
180                 Debug( LDAP_DEBUG_ANY,
181                         "monitor_subsys_time_init: "
182                         "unable to add entry 'cn=Current,%s'\n%s%s",
183                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
184                         "", "" );
185 #endif
186                 return( -1 );
187         }
188         
189         e_tmp = e;
190
191         mp = ( struct monitorentrypriv * )e_time->e_private;
192         mp->mp_children = e_tmp;
193
194         monitor_cache_release( mi, e_time );
195
196         return( 0 );
197 }
198
199 int
200 monitor_subsys_time_update(
201         struct monitorinfo      *mi,
202         Entry                   *e
203 )
204 {
205         char            tmbuf[20];
206         struct tm       *ltm;
207         time_t          currenttime;
208         struct berval   bv[ 2 ];
209         Attribute       *a;
210         ber_len_t       len;
211
212         static int      init_start = 0;
213
214         assert( mi );
215         assert( e );
216         
217         if ( init_start == 0 && strncmp( e->e_nname.bv_val, "cn=START",
218                                 sizeof("cn=START")-1 ) == 0 ) {
219                 currenttime = starttime;
220                 init_start = 1;
221
222         } else if ( strncmp( e->e_nname.bv_val, "cn=CURRENT",
223                                 sizeof("cn=CURRENT")-1 ) == 0 ) {
224                 currenttime = slap_get_time();
225
226         } else {
227                 return( 0 );
228         }
229
230         a = attr_find( e->e_attrs, monitor_ad_desc );
231         if ( a == NULL ) {
232                 return( -1 );
233         }
234
235
236         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
237         ltm = gmtime( &currenttime );
238         strftime( tmbuf, sizeof(tmbuf), "%Y%m%d%H%M%SZ", ltm );
239         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
240
241         len = strlen( tmbuf );
242         assert( len == a->a_vals[0].bv_len );
243         AC_MEMCPY( a->a_vals[0].bv_val, tmbuf, len );
244
245         return( 0 );
246 }
247