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