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