1 /* time.c - deal with time subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2011 The OpenLDAP Foundation.
6 * Portions Copyright 2001-2003 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
25 #include <ac/string.h>
31 #include "proto-slap.h"
32 #include "back-monitor.h"
35 monitor_subsys_time_update(
41 monitor_subsys_time_init(
43 monitor_subsys_t *ms )
47 Entry *e, **ep, *e_time;
49 struct berval bv, value;
53 ms->mss_update = monitor_subsys_time_update;
55 mi = ( monitor_info_t * )be->be_private;
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 );
66 mp = ( monitor_entry_t * )e_time->e_private;
67 mp->mp_children = NULL;
68 ep = &mp->mp_children;
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 );
74 Debug( LDAP_DEBUG_ANY,
75 "monitor_subsys_time_init: "
76 "unable to create entry \"%s,%s\"\n",
77 bv.bv_val, ms->mss_ndn.bv_val, 0 );
80 attr_merge_normalize_one( e, mi->mi_ad_monitorTimestamp,
81 &mi->mi_startTime, NULL );
83 mp = monitor_entrypriv_create();
87 e->e_private = ( void * )mp;
89 mp->mp_flags = ms->mss_flags \
90 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
92 if ( monitor_cache_add( mi, e ) ) {
93 Debug( LDAP_DEBUG_ANY,
94 "monitor_subsys_time_init: "
95 "unable to add entry \"%s,%s\"\n",
96 bv.bv_val, ms->mss_ndn.bv_val, 0 );
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 );
110 Debug( LDAP_DEBUG_ANY,
111 "monitor_subsys_time_init: "
112 "unable to create entry \"%s,%s\"\n",
113 bv.bv_val, ms->mss_ndn.bv_val, 0 );
116 attr_merge_normalize_one( e, mi->mi_ad_monitorTimestamp,
117 &mi->mi_startTime, NULL );
119 mp = monitor_entrypriv_create();
123 e->e_private = ( void * )mp;
125 mp->mp_flags = ms->mss_flags \
126 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
128 if ( monitor_cache_add( mi, e ) ) {
129 Debug( LDAP_DEBUG_ANY,
130 "monitor_subsys_time_init: "
131 "unable to add entry \"%s,%s\"\n",
132 bv.bv_val, ms->mss_ndn.bv_val, 0 );
142 BER_BVSTR( &bv, "cn=Uptime" );
143 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
144 mi->mi_oc_monitoredObject, mi, NULL, NULL );
146 Debug( LDAP_DEBUG_ANY,
147 "monitor_subsys_time_init: "
148 "unable to create entry \"%s,%s\"\n",
149 bv.bv_val, ms->mss_ndn.bv_val, 0 );
152 BER_BVSTR( &value, "0" );
153 attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
156 mp = monitor_entrypriv_create();
160 e->e_private = ( void * )mp;
162 mp->mp_flags = ms->mss_flags \
163 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
165 if ( monitor_cache_add( mi, e ) ) {
166 Debug( LDAP_DEBUG_ANY,
167 "monitor_subsys_time_init: "
168 "unable to add entry \"%s,%s\"\n",
169 bv.bv_val, ms->mss_ndn.bv_val, 0 );
176 monitor_cache_release( mi, e_time );
182 monitor_subsys_time_update(
187 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
188 static struct berval bv_current = BER_BVC( "cn=current" ),
189 bv_uptime = BER_BVC( "cn=uptime" );
192 assert( mi != NULL );
195 dnRdn( &e->e_nname, &rdn );
197 if ( dn_match( &rdn, &bv_current ) ) {
199 char tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
204 currtime = slap_get_time();
206 ldap_pvt_gmtime( &currtime, &tm );
207 lutil_gentime( tmbuf, sizeof( tmbuf ), &tm );
209 len = strlen( tmbuf );
211 a = attr_find( e->e_attrs, mi->mi_ad_monitorTimestamp );
213 return rs->sr_err = LDAP_OTHER;
216 assert( len == a->a_vals[ 0 ].bv_len );
217 AC_MEMCPY( a->a_vals[ 0 ].bv_val, tmbuf, len );
219 /* FIXME: touch modifyTimestamp? */
221 } else if ( dn_match( &rdn, &bv_uptime ) ) {
224 char buf[ BACKMONITOR_BUFSIZE ];
227 a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo );
229 return rs->sr_err = LDAP_OTHER;
232 diff = difftime( slap_get_time(), starttime );
233 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu",
234 (unsigned long) diff );
237 ber_bvreplace( &a->a_vals[ 0 ], &bv );
238 if ( a->a_nvals != a->a_vals ) {
239 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
242 /* FIXME: touch modifyTimestamp? */
245 return SLAP_CB_CONTINUE;