]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/conn.c
Remove lint
[openldap] / servers / slapd / back-monitor / conn.c
1 /* conn.c - deal with connection subsystem */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37 #include "lutil.h"
38
39 #include "slap.h"
40 #include "back-monitor.h"
41
42 #define CONN_CN_PREFIX  "Connection"
43
44 int
45 monitor_subsys_conn_init(
46         BackendDB               *be
47 )
48 {
49         struct monitorinfo      *mi;
50         
51         Entry                   *e, *e_tmp, *e_conn;
52         struct monitorentrypriv *mp;
53         char                    buf[1024];
54         struct berval           bv[2];
55
56         assert( be != NULL );
57
58         mi = ( struct monitorinfo * )be->be_private;
59
60         if ( monitor_cache_get( mi,
61                         &monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn, &e_conn ) ) {
62 #ifdef NEW_LOGGING
63                 LDAP_LOG( OPERATION, CRIT,
64                         "monitor_subsys_conn_init: "
65                         "unable to get entry '%s'\n",
66                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
67 #else
68                 Debug( LDAP_DEBUG_ANY,
69                         "monitor_subsys_conn_init: "
70                         "unable to get entry '%s'\n%s%s",
71                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 
72                         "", "" );
73 #endif
74                 return( -1 );
75         }
76
77         e_tmp = NULL;
78
79         /*
80          * Total conns
81          */
82         snprintf( buf, sizeof( buf ),
83                 "dn: cn=Total,%s\n"
84                 SLAPD_MONITOR_OBJECTCLASSES
85                 "cn: Total\n",
86                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val );
87         
88         e = str2entry( buf );
89         if ( e == NULL ) {
90 #ifdef NEW_LOGGING
91                 LDAP_LOG( OPERATION, CRIT,
92                         "monitor_subsys_conn_init: "
93                         "unable to create entry 'cn=Total,%s'\n",
94                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
95 #else
96                 Debug( LDAP_DEBUG_ANY,
97                         "monitor_subsys_conn_init: "
98                         "unable to create entry 'cn=Total,%s'\n%s%s",
99                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
100                         "", "" );
101 #endif
102                 return( -1 );
103         }
104         
105         bv[1].bv_val = NULL;
106         bv[0].bv_val = "0";
107         bv[0].bv_len = 1;
108         attr_merge( e, monitor_ad_desc, bv );
109         
110         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
111         e->e_private = ( void * )mp;
112         mp->mp_next = e_tmp;
113         mp->mp_children = NULL;
114         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
115         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
116                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
117         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
118
119         if ( monitor_cache_add( mi, e ) ) {
120 #ifdef NEW_LOGGING
121                 LDAP_LOG( OPERATION, CRIT,
122                         "monitor_subsys_conn_init: "
123                         "unable to add entry 'cn=Total,%s'\n",
124                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
125 #else
126                 Debug( LDAP_DEBUG_ANY,
127                         "monitor_subsys_conn_init: "
128                         "unable to add entry 'cn=Total,%s'\n%s%s",
129                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
130                         "", "" );
131 #endif
132                 return( -1 );
133         }
134         
135         e_tmp = e;
136
137         /*
138          * Current conns
139          */
140         snprintf( buf, sizeof( buf ),
141                 "dn: cn=Current,%s\n"
142                 SLAPD_MONITOR_OBJECTCLASSES
143                 "cn: Current\n",
144                 monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val );
145         
146         e = str2entry( buf );
147         if ( e == NULL ) {
148 #ifdef NEW_LOGGING
149                 LDAP_LOG( OPERATION, CRIT,
150                         "monitor_subsys_conn_init: "
151                         "unable to create entry 'cn=Current,%s'\n",
152                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
153 #else
154                 Debug( LDAP_DEBUG_ANY,
155                         "monitor_subsys_conn_init: "
156                         "unable to create entry 'cn=Current,%s'\n%s%s",
157                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
158                         "", "" );
159 #endif
160                 return( -1 );
161         }
162         
163         bv[1].bv_val = NULL;
164         bv[0].bv_val = "0";
165         bv[0].bv_len = 1;
166         attr_merge( e, monitor_ad_desc, bv );
167         
168         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
169         e->e_private = ( void * )mp;
170         mp->mp_next = e_tmp;
171         mp->mp_children = NULL;
172         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_CONN];
173         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_CONN].mss_flags \
174                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
175         mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;
176
177         if ( monitor_cache_add( mi, e ) ) {
178 #ifdef NEW_LOGGING
179                 LDAP_LOG( OPERATION, CRIT,
180                         "monitor_subsys_conn_init: "
181                         "unable to add entry 'cn=Current,%s'\n",
182                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val, 0, 0 );
183 #else
184                 Debug( LDAP_DEBUG_ANY,
185                         "monitor_subsys_conn_init: "
186                         "unable to add entry 'cn=Current,%s'\n%s%s",
187                         monitor_subsys[SLAPD_MONITOR_CONN].mss_ndn.bv_val,
188                         "", "" );
189 #endif
190                 return( -1 );
191         }
192         
193         e_tmp = e;
194
195         mp = ( struct monitorentrypriv * )e_conn->e_private;
196         mp->mp_children = e_tmp;
197
198         monitor_cache_release( mi, e_conn );
199
200         return( 0 );
201 }
202
203 int
204 monitor_subsys_conn_update(
205         struct monitorinfo      *mi,
206         Entry                   *e
207 )
208 {
209         long            n = -1;
210
211         assert( mi );
212         assert( e );
213         
214         if ( strncasecmp( e->e_ndn, "cn=total", 
215                                 sizeof("cn=total")-1 ) == 0 ) {
216                 n = connections_nextid();
217
218         } else if ( strncasecmp( e->e_ndn, "cn=current", 
219                                 sizeof("cn=current")-1 ) == 0 ) {
220                 Connection      *c;
221                 int             connindex;
222
223                 for ( n = 0, c = connection_first( &connindex );
224                                 c != NULL;
225                                 n++, c = connection_next( c, &connindex ) ) {
226                         /* No Op */ ;
227                 }
228                 connection_done(c);
229         }
230
231         if ( n != -1 ) {
232                 Attribute       *a;
233                 char            buf[16];
234
235                 a = attr_find( e->e_attrs, monitor_ad_desc );
236                 if ( a == NULL ) {
237                         return( -1 );
238                 }
239
240                 snprintf( buf, sizeof( buf ), "%ld", n );
241                 free( a->a_vals[ 0 ].bv_val );
242                 ber_str2bv( buf, 0, 1, a->a_vals );
243         }
244
245         return( 0 );
246 }
247
248 static int
249 conn_create(
250         Connection              *c,
251         Entry                   **ep
252 )
253 {
254         struct monitorentrypriv *mp;
255         struct tm               *ltm;
256         char                    buf[1024];
257         char                    buf2[22];
258         char                    buf3[22];
259
260         struct berval           bv[2];
261
262         Entry                   *e;
263
264         assert( c != NULL );
265         assert( ep != NULL );
266
267         snprintf( buf, sizeof( buf ),
268                 "dn: cn=" CONN_CN_PREFIX " %ld,%s\n"
269                 SLAPD_MONITOR_OBJECTCLASSES
270                 "cn: " CONN_CN_PREFIX " %ld\n",
271                 c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val,
272                 c->c_connid );
273         e = str2entry( buf );
274
275         if ( e == NULL) {
276 #ifdef NEW_LOGGING
277                 LDAP_LOG( OPERATION, CRIT,
278                         "monitor_subsys_conn_create: "
279                         "unable to create entry "
280                         "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
281                         c->c_connid, monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
282 #else
283                 Debug( LDAP_DEBUG_ANY,
284                         "monitor_subsys_conn_create: "
285                         "unable to create entry "
286                         "'cn=" CONN_CN_PREFIX " %ld,%s' entry\n",
287                         c->c_connid, 
288                         monitor_subsys[SLAPD_MONITOR_CONN].mss_dn.bv_val, 0 );
289 #endif
290                 return( -1 );
291         }
292
293         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
294         
295         ltm = gmtime( &c->c_starttime );
296         lutil_gentime( buf2, sizeof(buf2), ltm );
297                         
298         ltm = gmtime( &c->c_activitytime );
299         lutil_gentime( buf3, sizeof(buf2), ltm );
300                         
301         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
302
303         sprintf( buf,
304                 "%ld : %ld "
305                 ": %ld/%ld/%ld/%ld "
306                 ": %ld/%ld/%ld "
307                 ": %s%s%s%s%s%s "
308                 ": %s : %s : %s "
309                 ": %s : %s : %s : %s",
310                 c->c_connid,
311                 (long) c->c_protocol,
312                 c->c_n_ops_received, c->c_n_ops_executing,
313                 c->c_n_ops_pending, c->c_n_ops_completed,
314                 
315                 /* add low-level counters here */
316                 c->c_n_get, c->c_n_read, c->c_n_write,
317                 
318                 c->c_currentber ? "r" : "",
319                 c->c_writewaiter ? "w" : "",
320                 LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
321                 LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
322                 connection_state2str( c->c_conn_state ),
323                 c->c_sasl_bind_in_progress ? "S" : "",
324                 
325                 c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
326                 
327                 c->c_listener_url.bv_val,
328                 c->c_peer_domain.bv_val,
329                 c->c_peer_name.bv_val,
330                 c->c_sock_name.bv_val,
331                 
332                 buf2,
333                 buf3
334                 );
335
336         bv[1].bv_val = NULL;
337         bv[0].bv_val = buf;
338         bv[0].bv_len = strlen( buf );
339         attr_merge( e, monitor_ad_desc, bv );
340
341         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
342         e->e_private = ( void * )mp;
343         mp->mp_info = &monitor_subsys[ SLAPD_MONITOR_CONN ];
344         mp->mp_children = NULL;
345         mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;
346
347         *ep = e;
348
349         return( 0 );
350 }
351
352 int 
353 monitor_subsys_conn_create( 
354         struct monitorinfo      *mi,
355         struct berval           *ndn,
356         Entry                   *e_parent,
357         Entry                   **ep
358 )
359 {
360         Connection              *c;
361         int                     connindex;
362         struct monitorentrypriv *mp;
363
364         assert( mi != NULL );
365         assert( e_parent != NULL );
366         assert( ep != NULL );
367
368         *ep = NULL;
369
370         if ( ndn == NULL ) {
371                 Entry *e, *e_tmp = NULL;
372
373                 /* create all the children of e_parent */
374                 for ( c = connection_first( &connindex );
375                                 c != NULL;
376                                 c = connection_next( c, &connindex )) {
377                         if ( conn_create( c, &e ) || e == NULL ) {
378                                 connection_done(c);
379                                 for ( ; e_tmp != NULL; ) {
380                                         mp = ( struct monitorentrypriv * )e_tmp->e_private;
381                                         e = mp->mp_next;
382
383                                         ch_free( mp );
384                                         e_tmp->e_private = NULL;
385                                         entry_free( e_tmp );
386
387                                         e_tmp = e;
388                                 }
389                                 return( -1 );
390                         }
391                         mp = ( struct monitorentrypriv * )e->e_private;
392                         mp->mp_next = e_tmp;
393                         e_tmp = e;
394                 }
395                 connection_done(c);
396
397                 *ep = e;
398
399         } else {
400                 LDAPRDN         *values = NULL;
401                 const char      *text = NULL;
402                 unsigned long   connid;
403                
404                 /* create exactly the required entry */
405
406                 if ( ldap_bv2rdn( ndn, &values, (char **)&text,
407                         LDAP_DN_FORMAT_LDAP ) )
408                 {
409                         return( -1 );
410                 }
411                 
412                 assert( values );
413                 assert( values[ 0 ][ 0 ] );
414
415                 connid = atol( values[ 0 ][ 0 ]->la_value.bv_val
416                                 + sizeof( CONN_CN_PREFIX ) );
417
418                 ldap_rdnfree( values );
419
420                 for ( c = connection_first( &connindex );
421                                 c != NULL;
422                                 c = connection_next( c, &connindex )) {
423                         if ( c->c_connid == connid ) {
424                                 if ( conn_create( c, ep ) || *ep == NULL ) {
425                                         connection_done(c);
426                                         return( -1 );
427                                 }
428
429                                 break;
430                         }
431                 }
432                 
433                 connection_done(c);
434         
435         }
436
437         return( 0 );
438 }
439