Back to index

openldap  2.4.31
time.c
Go to the documentation of this file.
00001 /* time.c - deal with time subsystem */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2001-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2001-2003 Pierangelo Masarati.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by Pierangelo Masarati for inclusion
00019  * in OpenLDAP Software.
00020  */
00021 
00022 #include "portable.h"
00023 
00024 #include <stdio.h>
00025 #include <ac/string.h>
00026 #include <ac/time.h>
00027 
00028 
00029 #include "slap.h"
00030 #include <lutil.h>
00031 #include "proto-slap.h"
00032 #include "back-monitor.h"
00033 
00034 static int
00035 monitor_subsys_time_update(
00036        Operation            *op,
00037        SlapReply            *rs,
00038        Entry                   *e );
00039 
00040 int
00041 monitor_subsys_time_init(
00042        BackendDB            *be,
00043        monitor_subsys_t     *ms )
00044 {
00045        monitor_info_t       *mi;
00046        
00047        Entry         *e, **ep, *e_time;
00048        monitor_entry_t      *mp;
00049        struct berval bv, value;
00050 
00051        assert( be != NULL );
00052 
00053        ms->mss_update = monitor_subsys_time_update;
00054 
00055        mi = ( monitor_info_t * )be->be_private;
00056 
00057        if ( monitor_cache_get( mi,
00058                      &ms->mss_ndn, &e_time ) ) {
00059               Debug( LDAP_DEBUG_ANY,
00060                      "monitor_subsys_time_init: "
00061                      "unable to get entry \"%s\"\n",
00062                      ms->mss_ndn.bv_val, 0, 0 );
00063               return( -1 );
00064        }
00065 
00066        mp = ( monitor_entry_t * )e_time->e_private;
00067        mp->mp_children = NULL;
00068        ep = &mp->mp_children;
00069 
00070        BER_BVSTR( &bv, "cn=Start" );
00071        e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
00072               mi->mi_oc_monitoredObject, mi, NULL, NULL );
00073        if ( e == NULL ) {
00074               Debug( LDAP_DEBUG_ANY,
00075                      "monitor_subsys_time_init: "
00076                      "unable to create entry \"%s,%s\"\n",
00077                      bv.bv_val, ms->mss_ndn.bv_val, 0 );
00078               return( -1 );
00079        }
00080        attr_merge_normalize_one( e, mi->mi_ad_monitorTimestamp,
00081               &mi->mi_startTime, NULL );
00082        
00083        mp = monitor_entrypriv_create();
00084        if ( mp == NULL ) {
00085               return -1;
00086        }
00087        e->e_private = ( void * )mp;
00088        mp->mp_info = ms;
00089        mp->mp_flags = ms->mss_flags \
00090               | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
00091 
00092        if ( monitor_cache_add( mi, e ) ) {
00093               Debug( LDAP_DEBUG_ANY,
00094                      "monitor_subsys_time_init: "
00095                      "unable to add entry \"%s,%s\"\n",
00096                      bv.bv_val, ms->mss_ndn.bv_val, 0 );
00097               return( -1 );
00098        }
00099        
00100        *ep = e;
00101        ep = &mp->mp_next;
00102 
00103        /*
00104         * Current
00105         */
00106        BER_BVSTR( &bv, "cn=Current" );
00107        e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
00108               mi->mi_oc_monitoredObject, mi, NULL, NULL );
00109        if ( e == NULL ) {
00110               Debug( LDAP_DEBUG_ANY,
00111                      "monitor_subsys_time_init: "
00112                      "unable to create entry \"%s,%s\"\n",
00113                      bv.bv_val, ms->mss_ndn.bv_val, 0 );
00114               return( -1 );
00115        }
00116        attr_merge_normalize_one( e, mi->mi_ad_monitorTimestamp,
00117               &mi->mi_startTime, NULL );
00118 
00119        mp = monitor_entrypriv_create();
00120        if ( mp == NULL ) {
00121               return -1;
00122        }
00123        e->e_private = ( void * )mp;
00124        mp->mp_info = ms;
00125        mp->mp_flags = ms->mss_flags \
00126               | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
00127 
00128        if ( monitor_cache_add( mi, e ) ) {
00129               Debug( LDAP_DEBUG_ANY,
00130                      "monitor_subsys_time_init: "
00131                      "unable to add entry \"%s,%s\"\n",
00132                      bv.bv_val, ms->mss_ndn.bv_val, 0 );
00133               return( -1 );
00134        }
00135        
00136        *ep = e;
00137        ep = &mp->mp_next;
00138 
00139        /*
00140         * Uptime
00141         */
00142        BER_BVSTR( &bv, "cn=Uptime" );
00143        e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
00144               mi->mi_oc_monitoredObject, mi, NULL, NULL );
00145        if ( e == NULL ) {
00146               Debug( LDAP_DEBUG_ANY,
00147                      "monitor_subsys_time_init: "
00148                      "unable to create entry \"%s,%s\"\n",
00149                      bv.bv_val, ms->mss_ndn.bv_val, 0 );
00150               return( -1 );
00151        }
00152        BER_BVSTR( &value, "0" );
00153        attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
00154               &value, NULL );
00155 
00156        mp = monitor_entrypriv_create();
00157        if ( mp == NULL ) {
00158               return -1;
00159        }
00160        e->e_private = ( void * )mp;
00161        mp->mp_info = ms;
00162        mp->mp_flags = ms->mss_flags \
00163               | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
00164 
00165        if ( monitor_cache_add( mi, e ) ) {
00166               Debug( LDAP_DEBUG_ANY,
00167                      "monitor_subsys_time_init: "
00168                      "unable to add entry \"%s,%s\"\n",
00169                      bv.bv_val, ms->mss_ndn.bv_val, 0 );
00170               return( -1 );
00171        }
00172        
00173        *ep = e;
00174        ep = &mp->mp_next;
00175 
00176        monitor_cache_release( mi, e_time );
00177 
00178        return( 0 );
00179 }
00180 
00181 static int
00182 monitor_subsys_time_update(
00183        Operation            *op,
00184        SlapReply            *rs,
00185        Entry                   *e )
00186 {
00187        monitor_info_t              *mi = ( monitor_info_t * )op->o_bd->be_private;
00188        static struct berval bv_current = BER_BVC( "cn=current" ),
00189                             bv_uptime = BER_BVC( "cn=uptime" );
00190        struct berval        rdn;
00191 
00192        assert( mi != NULL );
00193        assert( e != NULL );
00194 
00195        dnRdn( &e->e_nname, &rdn );
00196        
00197        if ( dn_match( &rdn, &bv_current ) ) {
00198               struct tm     tm;
00199               char          tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
00200               Attribute     *a;
00201               ber_len_t     len;
00202               time_t        currtime;
00203 
00204               currtime = slap_get_time();
00205 
00206               ldap_pvt_gmtime( &currtime, &tm );
00207               lutil_gentime( tmbuf, sizeof( tmbuf ), &tm );
00208 
00209               len = strlen( tmbuf );
00210 
00211               a = attr_find( e->e_attrs, mi->mi_ad_monitorTimestamp );
00212               if ( a == NULL ) {
00213                      return rs->sr_err = LDAP_OTHER;
00214               }
00215 
00216               assert( len == a->a_vals[ 0 ].bv_len );
00217               AC_MEMCPY( a->a_vals[ 0 ].bv_val, tmbuf, len );
00218 
00219               /* FIXME: touch modifyTimestamp? */
00220 
00221        } else if ( dn_match( &rdn, &bv_uptime ) ) {
00222               Attribute     *a;
00223               double        diff;
00224               char          buf[ BACKMONITOR_BUFSIZE ];
00225               struct berval bv;
00226 
00227               a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo );
00228               if ( a == NULL ) {
00229                      return rs->sr_err = LDAP_OTHER;
00230               }
00231 
00232               diff = difftime( slap_get_time(), starttime );
00233               bv.bv_len = snprintf( buf, sizeof( buf ), "%lu",
00234                      (unsigned long) diff );
00235               bv.bv_val = buf;
00236 
00237               ber_bvreplace( &a->a_vals[ 0 ], &bv );
00238               if ( a->a_nvals != a->a_vals ) {
00239                      ber_bvreplace( &a->a_nvals[ 0 ], &bv );
00240               }
00241 
00242               /* FIXME: touch modifyTimestamp? */
00243        }
00244 
00245        return SLAP_CB_CONTINUE;
00246 }
00247