]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/time.c
ca5b00de4988432bcdf39ff08ca0144c0b019992
[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         monitorsubsys           *ms
38 )
39 {
40         struct monitorinfo      *mi;
41         
42         Entry                   *e, **ep, *e_time;
43         struct monitorentrypriv *mp;
44         char                    buf[ BACKMONITOR_BUFSIZE ];
45
46         assert( be != NULL );
47
48         mi = ( struct monitorinfo * )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 = ( struct monitorentrypriv * )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 = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
93         e->e_private = ( void * )mp;
94         mp->mp_next = NULL;
95         mp->mp_children = NULL;
96         mp->mp_info = ms;
97         mp->mp_flags = ms->mss_flags \
98                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
99
100         if ( monitor_cache_add( mi, e ) ) {
101                 Debug( LDAP_DEBUG_ANY,
102                         "monitor_subsys_time_init: "
103                         "unable to add entry \"cn=Start,%s\"\n",
104                         ms->mss_ndn.bv_val, 0, 0 );
105                 return( -1 );
106         }
107         
108         *ep = e;
109         ep = &mp->mp_next;
110
111         /*
112          * Current
113          */
114         snprintf( buf, sizeof( buf ),
115                         "dn: cn=Current,%s\n"
116                         "objectClass: %s\n"
117                         "structuralObjectClass: %s\n"
118                         "cn: Current\n"
119                         "%s: %s\n"
120                         "creatorsName: %s\n"
121                         "modifiersName: %s\n"
122                         "createTimestamp: %s\n"
123                         "modifyTimestamp: %s\n",
124                         ms->mss_dn.bv_val,
125                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
126                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
127                         mi->mi_ad_monitorTimestamp->ad_cname.bv_val,
128                         mi->mi_startTime.bv_val,
129                         mi->mi_creatorsName.bv_val,
130                         mi->mi_creatorsName.bv_val,
131                         mi->mi_startTime.bv_val,
132                         mi->mi_startTime.bv_val );
133
134         e = str2entry( buf );
135         if ( e == NULL ) {
136                 Debug( LDAP_DEBUG_ANY,
137                         "monitor_subsys_time_init: "
138                         "unable to create entry \"cn=Current,%s\"\n",
139                         ms->mss_ndn.bv_val, 0, 0 );
140                 return( -1 );
141         }
142
143         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
144         e->e_private = ( void * )mp;
145         mp->mp_next = NULL;
146         mp->mp_children = NULL;
147         mp->mp_info = ms;
148         mp->mp_flags = ms->mss_flags \
149                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
150
151         if ( monitor_cache_add( mi, e ) ) {
152                 Debug( LDAP_DEBUG_ANY,
153                         "monitor_subsys_time_init: "
154                         "unable to add entry \"cn=Current,%s\"\n",
155                         ms->mss_ndn.bv_val, 0, 0 );
156                 return( -1 );
157         }
158         
159         *ep = e;
160         ep = &mp->mp_next;
161
162         monitor_cache_release( mi, e_time );
163
164         return( 0 );
165 }
166
167 int
168 monitor_subsys_time_update(
169         Operation               *op,
170         Entry                   *e
171 )
172 {
173         struct monitorinfo      *mi =
174                 (struct monitorinfo *)op->o_bd->be_private;
175
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( -1 );
223                 }
224
225                 assert( len == a->a_vals[0].bv_len );
226                 AC_MEMCPY( a->a_vals[0].bv_val, tmbuf, len );
227         }
228
229         return( 0 );
230 }
231