]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/time.c
Referrals should not (except in special cases) be
[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 2001-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26 #include <ac/time.h>
27
28
29 #include "slap.h"
30 #include <lutil.h>
31 #include "proto-slap.h"
32 #include "back-monitor.h"
33
34 int
35 monitor_subsys_time_init(
36         BackendDB               *be
37 )
38 {
39         struct monitorinfo      *mi;
40         
41         Entry                   *e, *e_tmp, *e_time;
42         struct monitorentrypriv *mp;
43         char                    buf[ BACKMONITOR_BUFSIZE ];
44
45         assert( be != NULL );
46
47         mi = ( struct monitorinfo * )be->be_private;
48
49         if ( monitor_cache_get( mi,
50                         &monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn, &e_time ) ) {
51 #ifdef NEW_LOGGING
52                 LDAP_LOG( OPERATION, CRIT,
53                         "monitor_subsys_time_init: "
54                         "unable to get entry '%s'\n",
55                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
56 #else
57                 Debug( LDAP_DEBUG_ANY,
58                         "monitor_subsys_time_init: "
59                         "unable to get entry '%s'\n%s%s",
60                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 
61                         "", "" );
62 #endif
63                 return( -1 );
64         }
65
66         e_tmp = NULL;
67
68         snprintf( buf, sizeof( buf ),
69                         "dn: cn=Start,%s\n"
70                         "objectClass: %s\n"
71                         "structuralObjectClass: %s\n"
72                         "cn: Start\n"
73                         "%s: %s\n"
74                         "creatorsName: %s\n"
75                         "modifiersName: %s\n"
76                         "createTimestamp: %s\n"
77                         "modifyTimestamp: %s\n", 
78                         monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
79                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
80                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
81                         mi->mi_ad_monitorTimestamp->ad_cname.bv_val,
82                         mi->mi_startTime.bv_val,
83                         mi->mi_creatorsName.bv_val,
84                         mi->mi_creatorsName.bv_val,
85                         mi->mi_startTime.bv_val,
86                         mi->mi_startTime.bv_val );
87
88         e = str2entry( buf );
89         if ( e == NULL ) {
90 #ifdef NEW_LOGGING
91                 LDAP_LOG( OPERATION, CRIT,
92                         "monitor_subsys_time_init: "
93                         "unable to create entry 'cn=Start,%s'\n",
94                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
95 #else
96                 Debug( LDAP_DEBUG_ANY,
97                         "monitor_subsys_time_init: "
98                         "unable to create entry 'cn=Start,%s'\n%s%s",
99                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
100                         "", "" );
101 #endif
102                 return( -1 );
103         }
104         
105         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
106         e->e_private = ( void * )mp;
107         mp->mp_next = e_tmp;
108         mp->mp_children = NULL;
109         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
110         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
111                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
112
113         if ( monitor_cache_add( mi, e ) ) {
114 #ifdef NEW_LOGGING
115                 LDAP_LOG( OPERATION, CRIT,
116                         "monitor_subsys_time_init: "
117                         "unable to add entry 'cn=Start,%s'\n",
118                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
119 #else
120                 Debug( LDAP_DEBUG_ANY,
121                         "monitor_subsys_time_init: "
122                         "unable to add entry 'cn=Start,%s'\n%s%s",
123                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
124                         "", "" );
125 #endif
126                 return( -1 );
127         }
128         
129         e_tmp = e;
130
131         /*
132          * Current
133          */
134         snprintf( buf, sizeof( buf ),
135                         "dn: cn=Current,%s\n"
136                         "objectClass: %s\n"
137                         "structuralObjectClass: %s\n"
138                         "cn: Current\n"
139                         "%s: %s\n"
140                         "creatorsName: %s\n"
141                         "modifiersName: %s\n"
142                         "createTimestamp: %s\n"
143                         "modifyTimestamp: %s\n",
144                         monitor_subsys[SLAPD_MONITOR_TIME].mss_dn.bv_val,
145                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
146                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
147                         mi->mi_ad_monitorTimestamp->ad_cname.bv_val,
148                         mi->mi_startTime.bv_val,
149                         mi->mi_creatorsName.bv_val,
150                         mi->mi_creatorsName.bv_val,
151                         mi->mi_startTime.bv_val,
152                         mi->mi_startTime.bv_val );
153
154         e = str2entry( buf );
155         if ( e == NULL ) {
156 #ifdef NEW_LOGGING
157                 LDAP_LOG( OPERATION, CRIT,
158                         "monitor_subsys_time_init: "
159                         "unable to create entry 'cn=Current,%s'\n",
160                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
161 #else
162                 Debug( LDAP_DEBUG_ANY,
163                         "monitor_subsys_time_init: "
164                         "unable to create entry 'cn=Current,%s'\n%s%s",
165                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
166                         "", "" );
167 #endif
168                 return( -1 );
169         }
170
171         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
172         e->e_private = ( void * )mp;
173         mp->mp_next = e_tmp;
174         mp->mp_children = NULL;
175         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_TIME];
176         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_TIME].mss_flags \
177                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
178
179         if ( monitor_cache_add( mi, e ) ) {
180 #ifdef NEW_LOGGING
181                 LDAP_LOG( OPERATION, CRIT,
182                         "monitor_subsys_time_init: "
183                         "unable to add entry 'cn=Current,%s'\n",
184                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val, 0, 0 );
185 #else
186                 Debug( LDAP_DEBUG_ANY,
187                         "monitor_subsys_time_init: "
188                         "unable to add entry 'cn=Current,%s'\n%s%s",
189                         monitor_subsys[SLAPD_MONITOR_TIME].mss_ndn.bv_val,
190                         "", "" );
191 #endif
192                 return( -1 );
193         }
194         
195         e_tmp = e;
196
197         mp = ( struct monitorentrypriv * )e_time->e_private;
198         mp->mp_children = e_tmp;
199
200         monitor_cache_release( mi, e_time );
201
202         return( 0 );
203 }
204
205 int
206 monitor_subsys_time_update(
207         Operation               *op,
208         Entry                   *e
209 )
210 {
211         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
212
213         assert( mi );
214         assert( e );
215         
216         if ( strncmp( e->e_nname.bv_val, "cn=current",
217                                 sizeof("cn=current") - 1 ) == 0 ) {
218                 struct tm       *tm;
219 #ifdef HAVE_GMTIME_R
220                 struct tm       tm_buf;
221 #endif
222                 char            tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
223                 Attribute       *a;
224                 ber_len_t       len;
225                 time_t          currtime;
226
227                 currtime = slap_get_time();
228
229 #ifndef HAVE_GMTIME_R
230                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
231 #endif
232 #ifdef HACK_LOCAL_TIME
233 # ifdef HAVE_LOCALTIME_R
234                 tm = localtime_r( &currtime, &tm_buf );
235 # else
236                 tm = localtime( &currtime );
237 # endif /* HAVE_LOCALTIME_R */
238                 lutil_localtime( tmbuf, sizeof( tmbuf ), tm, -timezone );
239 #else /* !HACK_LOCAL_TIME */
240 # ifdef HAVE_GMTIME_R
241                 tm = gmtime_r( &currtime, &tm_buf );
242 # else
243                 tm = gmtime( &currtime );
244 # endif /* HAVE_GMTIME_R */
245                 lutil_gentime( tmbuf, sizeof( tmbuf ), tm );
246 #endif /* !HACK_LOCAL_TIME */
247 #ifndef HAVE_GMTIME_R
248                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
249 #endif
250
251                 len = strlen( tmbuf );
252
253                 a = attr_find( e->e_attrs, mi->mi_ad_monitorTimestamp );
254                 if ( a == NULL ) {
255                         return( -1 );
256                 }
257
258                 assert( len == a->a_vals[0].bv_len );
259                 AC_MEMCPY( a->a_vals[0].bv_val, tmbuf, len );
260         }
261
262         return( 0 );
263 }
264