]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/time.c
6beb389c6d0bc09a64b89deec28b83a15f0f4f2b
[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-2007 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 static int
35 monitor_subsys_time_update(
36         Operation               *op,
37         SlapReply               *rs,
38         Entry                   *e );
39
40 int
41 monitor_subsys_time_init(
42         BackendDB               *be,
43         monitor_subsys_t        *ms )
44 {
45         monitor_info_t  *mi;
46         
47         Entry           *e, **ep, *e_time;
48         monitor_entry_t *mp;
49         char            buf[ BACKMONITOR_BUFSIZE ];
50
51         assert( be != NULL );
52
53         ms->mss_update = monitor_subsys_time_update;
54
55         mi = ( monitor_info_t * )be->be_private;
56
57         if ( monitor_cache_get( mi,
58                         &ms->mss_ndn, &e_time ) ) {
59                 Debug( LDAP_DEBUG_ANY,
60                         "monitor_subsys_time_init: "
61                         "unable to get entry \"%s\"\n",
62                         ms->mss_ndn.bv_val, 0, 0 );
63                 return( -1 );
64         }
65
66         mp = ( monitor_entry_t * )e_time->e_private;
67         mp->mp_children = NULL;
68         ep = &mp->mp_children;
69
70         snprintf( buf, sizeof( buf ),
71                         "dn: cn=Start,%s\n"
72                         "objectClass: %s\n"
73                         "structuralObjectClass: %s\n"
74                         "cn: Start\n"
75                         "%s: %s\n"
76                         "creatorsName: %s\n"
77                         "modifiersName: %s\n"
78                         "createTimestamp: %s\n"
79                         "modifyTimestamp: %s\n", 
80                         ms->mss_dn.bv_val,
81                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
82                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
83                         mi->mi_ad_monitorTimestamp->ad_cname.bv_val,
84                         mi->mi_startTime.bv_val,
85                         mi->mi_creatorsName.bv_val,
86                         mi->mi_creatorsName.bv_val,
87                         mi->mi_startTime.bv_val,
88                         mi->mi_startTime.bv_val );
89
90         e = str2entry( buf );
91         if ( e == NULL ) {
92                 Debug( LDAP_DEBUG_ANY,
93                         "monitor_subsys_time_init: "
94                         "unable to create entry \"cn=Start,%s\"\n",
95                         ms->mss_ndn.bv_val, 0, 0 );
96                 return( -1 );
97         }
98         
99         mp = monitor_entrypriv_create();
100         if ( mp == NULL ) {
101                 return -1;
102         }
103         e->e_private = ( void * )mp;
104         mp->mp_info = ms;
105         mp->mp_flags = ms->mss_flags \
106                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
107
108         if ( monitor_cache_add( mi, e ) ) {
109                 Debug( LDAP_DEBUG_ANY,
110                         "monitor_subsys_time_init: "
111                         "unable to add entry \"cn=Start,%s\"\n",
112                         ms->mss_ndn.bv_val, 0, 0 );
113                 return( -1 );
114         }
115         
116         *ep = e;
117         ep = &mp->mp_next;
118
119         /*
120          * Current
121          */
122         snprintf( buf, sizeof( buf ),
123                         "dn: cn=Current,%s\n"
124                         "objectClass: %s\n"
125                         "structuralObjectClass: %s\n"
126                         "cn: Current\n"
127                         "%s: %s\n"
128                         "creatorsName: %s\n"
129                         "modifiersName: %s\n"
130                         "createTimestamp: %s\n"
131                         "modifyTimestamp: %s\n",
132                         ms->mss_dn.bv_val,
133                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
134                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
135                         mi->mi_ad_monitorTimestamp->ad_cname.bv_val,
136                         mi->mi_startTime.bv_val,
137                         mi->mi_creatorsName.bv_val,
138                         mi->mi_creatorsName.bv_val,
139                         mi->mi_startTime.bv_val,
140                         mi->mi_startTime.bv_val );
141
142         e = str2entry( buf );
143         if ( e == NULL ) {
144                 Debug( LDAP_DEBUG_ANY,
145                         "monitor_subsys_time_init: "
146                         "unable to create entry \"cn=Current,%s\"\n",
147                         ms->mss_ndn.bv_val, 0, 0 );
148                 return( -1 );
149         }
150
151         mp = monitor_entrypriv_create();
152         if ( mp == NULL ) {
153                 return -1;
154         }
155         e->e_private = ( void * )mp;
156         mp->mp_info = ms;
157         mp->mp_flags = ms->mss_flags \
158                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
159
160         if ( monitor_cache_add( mi, e ) ) {
161                 Debug( LDAP_DEBUG_ANY,
162                         "monitor_subsys_time_init: "
163                         "unable to add entry \"cn=Current,%s\"\n",
164                         ms->mss_ndn.bv_val, 0, 0 );
165                 return( -1 );
166         }
167         
168         *ep = e;
169         ep = &mp->mp_next;
170
171         monitor_cache_release( mi, e_time );
172
173         return( 0 );
174 }
175
176 static int
177 monitor_subsys_time_update(
178         Operation               *op,
179         SlapReply               *rs,
180         Entry                   *e )
181 {
182         monitor_info_t          *mi = ( monitor_info_t * )op->o_bd->be_private;
183         static struct berval    bv_current = BER_BVC( "cn=current" );
184         struct berval           rdn;
185
186         assert( mi != NULL );
187         assert( e != NULL );
188
189         dnRdn( &e->e_nname, &rdn );
190         
191         if ( dn_match( &rdn, &bv_current ) ) {
192                 struct tm       *tm;
193 #ifdef HAVE_GMTIME_R
194                 struct tm       tm_buf;
195 #endif
196                 char            tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
197                 Attribute       *a;
198                 ber_len_t       len;
199                 time_t          currtime;
200
201                 currtime = slap_get_time();
202
203 #ifndef HAVE_GMTIME_R
204                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
205 #endif
206 #ifdef HACK_LOCAL_TIME
207 # ifdef HAVE_LOCALTIME_R
208                 tm = localtime_r( &currtime, &tm_buf );
209 # else
210                 tm = localtime( &currtime );
211 # endif /* HAVE_LOCALTIME_R */
212                 lutil_localtime( tmbuf, sizeof( tmbuf ), tm, -timezone );
213 #else /* !HACK_LOCAL_TIME */
214 # ifdef HAVE_GMTIME_R
215                 tm = gmtime_r( &currtime, &tm_buf );
216 # else
217                 tm = gmtime( &currtime );
218 # endif /* HAVE_GMTIME_R */
219                 lutil_gentime( tmbuf, sizeof( tmbuf ), tm );
220 #endif /* !HACK_LOCAL_TIME */
221 #ifndef HAVE_GMTIME_R
222                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
223 #endif
224
225                 len = strlen( tmbuf );
226
227                 a = attr_find( e->e_attrs, mi->mi_ad_monitorTimestamp );
228                 if ( a == NULL ) {
229                         return rs->sr_err = LDAP_OTHER;
230                 }
231
232                 assert( len == a->a_vals[ 0 ].bv_len );
233                 AC_MEMCPY( a->a_vals[ 0 ].bv_val, tmbuf, len );
234
235                 /* FIXME: touch modifyTimestamp? */
236         }
237
238         return SLAP_CB_CONTINUE;
239 }
240