]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/time.c
fix typo in rww; general 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-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 = 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         Entry                   *e
173 )
174 {
175         struct monitorinfo      *mi =
176                 (struct monitorinfo *)op->o_bd->be_private;
177
178         static struct berval    bv_current = BER_BVC( "cn=current" );
179         struct berval           rdn;
180
181         assert( mi );
182         assert( e );
183
184         dnRdn( &e->e_nname, &rdn );
185         
186         if ( dn_match( &rdn, &bv_current ) ) {
187                 struct tm       *tm;
188 #ifdef HAVE_GMTIME_R
189                 struct tm       tm_buf;
190 #endif
191                 char            tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
192                 Attribute       *a;
193                 ber_len_t       len;
194                 time_t          currtime;
195
196                 currtime = slap_get_time();
197
198 #ifndef HAVE_GMTIME_R
199                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
200 #endif
201 #ifdef HACK_LOCAL_TIME
202 # ifdef HAVE_LOCALTIME_R
203                 tm = localtime_r( &currtime, &tm_buf );
204 # else
205                 tm = localtime( &currtime );
206 # endif /* HAVE_LOCALTIME_R */
207                 lutil_localtime( tmbuf, sizeof( tmbuf ), tm, -timezone );
208 #else /* !HACK_LOCAL_TIME */
209 # ifdef HAVE_GMTIME_R
210                 tm = gmtime_r( &currtime, &tm_buf );
211 # else
212                 tm = gmtime( &currtime );
213 # endif /* HAVE_GMTIME_R */
214                 lutil_gentime( tmbuf, sizeof( tmbuf ), tm );
215 #endif /* !HACK_LOCAL_TIME */
216 #ifndef HAVE_GMTIME_R
217                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
218 #endif
219
220                 len = strlen( tmbuf );
221
222                 a = attr_find( e->e_attrs, mi->mi_ad_monitorTimestamp );
223                 if ( a == NULL ) {
224                         return( -1 );
225                 }
226
227                 assert( len == a->a_vals[ 0 ].bv_len );
228                 AC_MEMCPY( a->a_vals[ 0 ].bv_val, tmbuf, len );
229         }
230
231         return( 0 );
232 }
233