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