Debian Codegraph @

servers/slapd/back-monitor/time.c

raw
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-2024 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, *e_time;
48 monitor_entry_t *mp;
49 struct berval bv, value;
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 );
63 return( -1 );
64 }
65
66 BER_BVSTR( &bv, "cn=Start" );
67 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
68 mi->mi_oc_monitoredObject, NULL, NULL );
69 if ( e == NULL ) {
70 Debug( LDAP_DEBUG_ANY,
71 "monitor_subsys_time_init: "
72 "unable to create entry \"%s,%s\"\n",
73 bv.bv_val, ms->mss_ndn.bv_val );
74 return( -1 );
75 }
76 attr_merge_normalize_one( e, mi->mi_ad_monitorTimestamp,
77 &mi->mi_startTime, NULL );
78
79 mp = monitor_entrypriv_create();
80 if ( mp == NULL ) {
81 return -1;
82 }
83 e->e_private = ( void * )mp;
84 mp->mp_info = ms;
85 mp->mp_flags = ms->mss_flags \
86 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
87
88 if ( monitor_cache_add( mi, e, e_time ) ) {
89 Debug( LDAP_DEBUG_ANY,
90 "monitor_subsys_time_init: "
91 "unable to add entry \"%s,%s\"\n",
92 bv.bv_val, ms->mss_ndn.bv_val );
93 return( -1 );
94 }
95
96 /*
97 * Current
98 */
99 BER_BVSTR( &bv, "cn=Current" );
100 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
101 mi->mi_oc_monitoredObject, NULL, NULL );
102 if ( e == NULL ) {
103 Debug( LDAP_DEBUG_ANY,
104 "monitor_subsys_time_init: "
105 "unable to create entry \"%s,%s\"\n",
106 bv.bv_val, ms->mss_ndn.bv_val );
107 return( -1 );
108 }
109 attr_merge_normalize_one( e, mi->mi_ad_monitorTimestamp,
110 &mi->mi_startTime, NULL );
111
112 mp = monitor_entrypriv_create();
113 if ( mp == NULL ) {
114 return -1;
115 }
116 e->e_private = ( void * )mp;
117 mp->mp_info = ms;
118 mp->mp_flags = ms->mss_flags \
119 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
120
121 if ( monitor_cache_add( mi, e, e_time ) ) {
122 Debug( LDAP_DEBUG_ANY,
123 "monitor_subsys_time_init: "
124 "unable to add entry \"%s,%s\"\n",
125 bv.bv_val, ms->mss_ndn.bv_val );
126 return( -1 );
127 }
128
129 /*
130 * Uptime
131 */
132 BER_BVSTR( &bv, "cn=Uptime" );
133 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
134 mi->mi_oc_monitoredObject, NULL, NULL );
135 if ( e == NULL ) {
136 Debug( LDAP_DEBUG_ANY,
137 "monitor_subsys_time_init: "
138 "unable to create entry \"%s,%s\"\n",
139 bv.bv_val, ms->mss_ndn.bv_val );
140 return( -1 );
141 }
142 BER_BVSTR( &value, "0" );
143 attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo,
144 &value, NULL );
145
146 mp = monitor_entrypriv_create();
147 if ( mp == NULL ) {
148 return -1;
149 }
150 e->e_private = ( void * )mp;
151 mp->mp_info = ms;
152 mp->mp_flags = ms->mss_flags \
153 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
154
155 if ( monitor_cache_add( mi, e, e_time ) ) {
156 Debug( LDAP_DEBUG_ANY,
157 "monitor_subsys_time_init: "
158 "unable to add entry \"%s,%s\"\n",
159 bv.bv_val, ms->mss_ndn.bv_val );
160 return( -1 );
161 }
162
163 monitor_cache_release( mi, e_time );
164
165 return( 0 );
166 }
167
168 static int
169 monitor_subsys_time_update(
170 Operation *op,
171 SlapReply *rs,
172 Entry *e )
173 {
174 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
175 static struct berval bv_current = BER_BVC( "cn=current" ),
176 bv_uptime = BER_BVC( "cn=uptime" );
177 struct berval rdn;
178
179 assert( mi != NULL );
180 assert( e != NULL );
181
182 dnRdn( &e->e_nname, &rdn );
183
184 if ( dn_match( &rdn, &bv_current ) ) {
185 struct tm tm;
186 char tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
187 Attribute *a;
188 ber_len_t len;
189 time_t currtime;
190
191 currtime = slap_get_time();
192
193 ldap_pvt_gmtime( &currtime, &tm );
194 lutil_gentime( tmbuf, sizeof( tmbuf ), &tm );
195
196 len = strlen( tmbuf );
197
198 a = attr_find( e->e_attrs, mi->mi_ad_monitorTimestamp );
199 if ( a == NULL ) {
200 return rs->sr_err = LDAP_OTHER;
201 }
202
203 assert( len == a->a_vals[ 0 ].bv_len );
204 AC_MEMCPY( a->a_vals[ 0 ].bv_val, tmbuf, len );
205
206 /* FIXME: touch modifyTimestamp? */
207
208 } else if ( dn_match( &rdn, &bv_uptime ) ) {
209 Attribute *a;
210 double diff;
211 char buf[ BACKMONITOR_BUFSIZE ];
212 struct berval bv;
213
214 a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo );
215 if ( a == NULL ) {
216 return rs->sr_err = LDAP_OTHER;
217 }
218
219 diff = difftime( slap_get_time(), starttime );
220 bv.bv_len = snprintf( buf, sizeof( buf ), "%lu",
221 (unsigned long) diff );
222 bv.bv_val = buf;
223
224 ber_bvreplace( &a->a_vals[ 0 ], &bv );
225 if ( a->a_nvals != a->a_vals ) {
226 ber_bvreplace( &a->a_nvals[ 0 ], &bv );
227 }
228
229 /* FIXME: touch modifyTimestamp? */
230 }
231
232 return SLAP_CB_CONTINUE;
233 }
234