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