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