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