]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/time.c
Notice and acknowledgements
[openldap] / servers / slapd / back-monitor / time.c
1 /* time.c - deal with time subsystem */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2003 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Pierangelo Masarati for inclusion
18  * in OpenLDAP Software.
19  */
20 /* This is an altered version */
21 /*
22  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
23  * 
24  * This work has beed deveolped for the OpenLDAP Foundation 
25  * in the hope that it may be useful to the Open Source community, 
26  * but WITHOUT ANY WARRANTY.
27  * 
28  * Permission is granted to anyone to use this software for any purpose
29  * on any computer system, and to alter it and redistribute it, subject
30  * to the following restrictions:
31  * 
32  * 1. The author and SysNet s.n.c. are not responsible for the consequences
33  *    of use of this software, no matter how awful, even if they arise from
34  *    flaws in it.
35  * 
36  * 2. The origin of this software must not be misrepresented, either by
37  *    explicit claim or by omission.  Since few users ever read sources,
38  *    credits should appear in the documentation.
39  * 
40  * 3. Altered versions must be plainly marked as such, and must not be
41  *    misrepresented as being the original software.  Since few users
42  *    ever read sources, credits should appear in the documentation.
43  *    SysNet s.n.c. cannot be responsible for the consequences of the
44  *    alterations.
45  * 
46  * 4. This notice may not be removed or altered.
47  */
48
49 #include "portable.h"
50
51 #include <stdio.h>
52 #include <ac/string.h>
53 #include <ac/time.h>
54
55
56 #include "slap.h"
57 #include <lutil.h>
58 #include "proto-slap.h"
59 #include "back-monitor.h"
60
61 int
62 monitor_subsys_time_init(
63         BackendDB               *be
64 )
65 {
66         struct monitorinfo      *mi;
67         
68         Entry                   *e, *e_tmp, *e_time;
69         struct monitorentrypriv *mp;
70         char                    buf[ BACKMONITOR_BUFSIZE ];
71
72         assert( be != NULL );
73
74         mi = ( struct monitorinfo * )be->be_private;
75
76         if ( monitor_cache_get( mi,
77                         &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
78 #ifdef NEW_LOGGING
79                 LDAP_LOG( OPERATION, CRIT,
80                         "monitor_subsys_time_init: "
81                         "unable to get entry '%s'\n",
82                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
83 #else
84                 Debug( LDAP_DEBUG_ANY,
85                         "monitor_subsys_time_init: "
86                         "unable to get entry '%s'\n%s%s",
87                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 
88                         "", "" );
89 #endif
90                 return( -1 );
91         }
92
93         e_tmp = NULL;
94
95         snprintf( buf, sizeof( buf ),
96                         "dn: cn=Start,%s\n"
97                         "objectClass: %s\n"
98                         "structuralObjectClass: %s\n"
99                         "cn: Start\n"
100                         "%s: %s\n"
101                         "createTimestamp: %s\n"
102                         "modifyTimestamp: %s\n", 
103                         monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
104                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
105                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
106                         mi->mi_ad_monitorTimestamp->ad_cname.bv_val,
107                         mi->mi_startTime.bv_val,
108                         mi->mi_startTime.bv_val,
109                         mi->mi_startTime.bv_val );
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                         "objectClass: %s\n"
160                         "structuralObjectClass: %s\n"
161                         "cn: Current\n"
162                         "%s: %s\n"
163                         "createTimestamp: %s\n"
164                         "modifyTimestamp: %s\n",
165                         monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
166                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
167                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
168                         mi->mi_ad_monitorTimestamp->ad_cname.bv_val,
169                         mi->mi_startTime.bv_val,
170                         mi->mi_startTime.bv_val,
171                         mi->mi_startTime.bv_val );
172
173         e = str2entry( buf );
174         if ( e == NULL ) {
175 #ifdef NEW_LOGGING
176                 LDAP_LOG( OPERATION, CRIT,
177                         "monitor_subsys_time_init: "
178                         "unable to create entry 'cn=Current,%s'\n",
179                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
180 #else
181                 Debug( LDAP_DEBUG_ANY,
182                         "monitor_subsys_time_init: "
183                         "unable to create entry 'cn=Current,%s'\n%s%s",
184                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
185                         "", "" );
186 #endif
187                 return( -1 );
188         }
189
190         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
191         e->e_private = ( void * )mp;
192         mp->mp_next = e_tmp;
193         mp->mp_children = NULL;
194         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
195         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
196                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
197
198         if ( monitor_cache_add( mi, e ) ) {
199 #ifdef NEW_LOGGING
200                 LDAP_LOG( OPERATION, CRIT,
201                         "monitor_subsys_time_init: "
202                         "unable to add entry 'cn=Current,%s'\n",
203                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
204 #else
205                 Debug( LDAP_DEBUG_ANY,
206                         "monitor_subsys_time_init: "
207                         "unable to add entry 'cn=Current,%s'\n%s%s",
208                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
209                         "", "" );
210 #endif
211                 return( -1 );
212         }
213         
214         e_tmp = e;
215
216         mp = ( struct monitorentrypriv * )e_time->e_private;
217         mp->mp_children = e_tmp;
218
219         monitor_cache_release( mi, e_time );
220
221         return( 0 );
222 }
223
224 int
225 monitor_subsys_time_update(
226         Operation               *op,
227         Entry                   *e
228 )
229 {
230         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
231
232         assert( mi );
233         assert( e );
234         
235         if ( strncmp( e->e_nname.bv_val, "cn=current",
236                                 sizeof("cn=current") - 1 ) == 0 ) {
237                 struct tm       *tm;
238                 char            tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
239                 Attribute       *a;
240                 ber_len_t       len;
241                 time_t          currtime;
242
243                 currtime = slap_get_time();
244
245                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
246 #ifdef HACK_LOCAL_TIME
247                 tm = localtime( &currtime );
248                 lutil_localtime( tmbuf, sizeof( tmbuf ), tm, -timezone );
249 #else /* !HACK_LOCAL_TIME */
250                 tm = gmtime( &currtime );
251                 lutil_gentime( tmbuf, sizeof( tmbuf ), tm );
252 #endif /* !HACK_LOCAL_TIME */
253                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
254
255                 len = strlen( tmbuf );
256
257                 a = attr_find( e->e_attrs, mi->mi_ad_monitorTimestamp );
258                 if ( a == NULL ) {
259                         return( -1 );
260                 }
261
262                 assert( len == a->a_vals[0].bv_len );
263                 AC_MEMCPY( a->a_vals[0].bv_val, tmbuf, len );
264         }
265
266         return( 0 );
267 }
268