conn.c 12.7 KB
Newer Older
1
/* conn.c - deal with connection subsystem */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
4
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Copyright 2001-2009 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
 * Portions Copyright 2001-2003 Pierangelo Masarati.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Pierangelo Masarati for inclusion
 * in OpenLDAP Software.
 */
21
22
23
24

#include "portable.h"

#include <stdio.h>
25
#include <ac/string.h>
26
27

#include "slap.h"
Pierangelo Masarati's avatar
Pierangelo Masarati committed
28
#include "lutil.h"
29
30
#include "back-monitor.h"

31
32
33
34
35
36
37
38
39
40
41
42
43
44
static int
monitor_subsys_conn_update(
	Operation		*op,
	SlapReply		*rs,
	Entry                   *e );

static int 
monitor_subsys_conn_create( 
	Operation		*op,
	SlapReply		*rs,
	struct berval		*ndn,
	Entry 			*e_parent,
	Entry			**ep );

45
46
int
monitor_subsys_conn_init(
47
	BackendDB		*be,
48
	monitor_subsys_t	*ms )
49
{
50
51
52
	monitor_info_t	*mi;
	Entry		*e, **ep, *e_conn;
	monitor_entry_t	*mp;
53
	char		buf[ BACKMONITOR_BUFSIZE ];
54
	struct berval	bv;
55
56
57

	assert( be != NULL );

58
59
60
	ms->mss_update = monitor_subsys_conn_update;
	ms->mss_create = monitor_subsys_conn_create;

61
	mi = ( monitor_info_t * )be->be_private;
62

63
	if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
64
65
		Debug( LDAP_DEBUG_ANY,
			"monitor_subsys_conn_init: "
66
			"unable to get entry \"%s\"\n",
67
			ms->mss_ndn.bv_val, 0, 0 );
68
69
70
		return( -1 );
	}

71
	mp = ( monitor_entry_t * )e_conn->e_private;
72
73
	mp->mp_children = NULL;
	ep = &mp->mp_children;
74

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
	/*
	 * Max file descriptors
	 */
	BER_BVSTR( &bv, "cn=Max File Descriptors" );
	e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
		mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
	
	if ( e == NULL ) {
		Debug( LDAP_DEBUG_ANY,
			"monitor_subsys_conn_init: "
			"unable to create entry \"%s,%s\"\n",
			bv.bv_val, ms->mss_ndn.bv_val, 0 );
		return( -1 );
	}

	if ( dtblsize ) {
		bv.bv_val = buf;
		bv.bv_len = snprintf( buf, sizeof( buf ), "%d", dtblsize );

	} else {
		BER_BVSTR( &bv, "0" );
	}
97
	attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
	
	mp = monitor_entrypriv_create();
	if ( mp == NULL ) {
		return -1;
	}
	e->e_private = ( void * )mp;
	mp->mp_info = ms;
	mp->mp_flags = ms->mss_flags \
		| MONITOR_F_SUB | MONITOR_F_PERSISTENT;
	mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;

	if ( monitor_cache_add( mi, e ) ) {
		Debug( LDAP_DEBUG_ANY,
			"monitor_subsys_conn_init: "
			"unable to add entry \"cn=Total,%s\"\n",
			ms->mss_ndn.bv_val, 0, 0 );
		return( -1 );
	}

	*ep = e;
	ep = &mp->mp_next;
	
120
121
122
	/*
	 * Total conns
	 */
123
124
125
	BER_BVSTR( &bv, "cn=Total" );
	e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
		mi->mi_oc_monitorCounterObject, mi, NULL, NULL );
126
127
128
129
	
	if ( e == NULL ) {
		Debug( LDAP_DEBUG_ANY,
			"monitor_subsys_conn_init: "
130
			"unable to create entry \"cn=Total,%s\"\n",
131
			ms->mss_ndn.bv_val, 0, 0 );
132
133
134
		return( -1 );
	}
	
135
	BER_BVSTR( &bv, "-1" );
136
	attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
137
	
138
139
140
141
	mp = monitor_entrypriv_create();
	if ( mp == NULL ) {
		return -1;
	}
142
	e->e_private = ( void * )mp;
143
144
	mp->mp_info = ms;
	mp->mp_flags = ms->mss_flags \
145
146
147
148
149
150
		| MONITOR_F_SUB | MONITOR_F_PERSISTENT;
	mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;

	if ( monitor_cache_add( mi, e ) ) {
		Debug( LDAP_DEBUG_ANY,
			"monitor_subsys_conn_init: "
151
			"unable to add entry \"cn=Total,%s\"\n",
152
			ms->mss_ndn.bv_val, 0, 0 );
153
154
155
		return( -1 );
	}

156
157
158
	*ep = e;
	ep = &mp->mp_next;
	
159
160
161
	/*
	 * Current conns
	 */
162
163
164
165
	BER_BVSTR( &bv, "cn=Current" );
	e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
		mi->mi_oc_monitorCounterObject, mi, NULL, NULL );

166
167
168
	if ( e == NULL ) {
		Debug( LDAP_DEBUG_ANY,
			"monitor_subsys_conn_init: "
169
			"unable to create entry \"cn=Current,%s\"\n",
170
			ms->mss_ndn.bv_val, 0, 0 );
171
172
173
		return( -1 );
	}
	
174
	BER_BVSTR( &bv, "0" );
175
	attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
176
	
177
178
179
180
	mp = monitor_entrypriv_create();
	if ( mp == NULL ) {
		return -1;
	}
181
	e->e_private = ( void * )mp;
182
183
	mp->mp_info = ms;
	mp->mp_flags = ms->mss_flags \
184
185
186
187
188
189
		| MONITOR_F_SUB | MONITOR_F_PERSISTENT;
	mp->mp_flags &= ~MONITOR_F_VOLATILE_CH;

	if ( monitor_cache_add( mi, e ) ) {
		Debug( LDAP_DEBUG_ANY,
			"monitor_subsys_conn_init: "
190
			"unable to add entry \"cn=Current,%s\"\n",
191
			ms->mss_ndn.bv_val, 0, 0 );
192
193
194
		return( -1 );
	}
	
195
196
	*ep = e;
	ep = &mp->mp_next;
197

198
	monitor_cache_release( mi, e_conn );
199
200
201
202

	return( 0 );
}

203
static int
204
monitor_subsys_conn_update(
205
	Operation		*op,
206
	SlapReply		*rs,
207
	Entry                   *e )
208
{
209
	monitor_info_t	*mi = ( monitor_info_t * )op->o_bd->be_private;
210
211
212
213
214

	long 			n = -1;
	static struct berval	total_bv = BER_BVC( "cn=total" ),
				current_bv = BER_BVC( "cn=current" );
	struct berval		rdn;
215

216
217
	assert( mi != NULL );
	assert( e != NULL );
218
219

	dnRdn( &e->e_nname, &rdn );
220
	
221
	if ( dn_match( &rdn, &total_bv ) ) {
222
223
		n = connections_nextid();

224
	} else if ( dn_match( &rdn, &current_bv ) ) {
225
226
227
228
229
		Connection	*c;
		int		connindex;

		for ( n = 0, c = connection_first( &connindex );
				c != NULL;
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
230
231
				n++, c = connection_next( c, &connindex ) )
		{
232
			/* No Op */ ;
233
		}
234
		connection_done( c );
235
	}
236
237
238

	if ( n != -1 ) {
		Attribute	*a;
239
		char		buf[LDAP_PVT_INTTYPE_CHARS(long)];
240
		ber_len_t	len;
241

242
		a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
243
244
		if ( a == NULL ) {
			return( -1 );
245
		}
246
247

		snprintf( buf, sizeof( buf ), "%ld", n );
248
249
250
251
252
253
		len = strlen( buf );
		if ( len > a->a_vals[ 0 ].bv_len ) {
			a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
		}
		a->a_vals[ 0 ].bv_len = len;
		AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
254
255

		/* FIXME: touch modifyTimestamp? */
256
257
	}

258
	return SLAP_CB_CONTINUE;
259
260
261
262
}

static int
conn_create(
263
	monitor_info_t		*mi,
264
	Connection		*c,
265
	Entry			**ep,
266
	monitor_subsys_t	*ms )
267
{
268
	monitor_entry_t *mp;
269
	struct tm	tm;
270
271
272
	char		buf[ BACKMONITOR_BUFSIZE ];
	char		buf2[ LDAP_LUTIL_GENTIME_BUFSIZE ];
	char		buf3[ LDAP_LUTIL_GENTIME_BUFSIZE ];
273

274
	struct berval bv, ctmbv, mtmbv;
275
276
	struct berval bv_unknown= BER_BVC("unknown");

277
	Entry		*e;
278
279
280
281

	assert( c != NULL );
	assert( ep != NULL );

282
283
284
285
286
287
288
289
	ldap_pvt_gmtime( &c->c_starttime, &tm );

	ctmbv.bv_len = lutil_gentime( buf2, sizeof( buf2 ), &tm );
	ctmbv.bv_val = buf2;

	ldap_pvt_gmtime( &c->c_activitytime, &tm );
	mtmbv.bv_len = lutil_gentime( buf3, sizeof( buf3 ), &tm );
	mtmbv.bv_val = buf3;
290
291
292
293
294
295

	bv.bv_len = snprintf( buf, sizeof( buf ),
		"cn=Connection %ld", c->c_connid );
	bv.bv_val = buf;
	e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv, 
		mi->mi_oc_monitorConnection, mi, &ctmbv, &mtmbv );
296

297
298
299
300
301
302
303
304
305
	if ( e == NULL) {
		Debug( LDAP_DEBUG_ANY,
			"monitor_subsys_conn_create: "
			"unable to create entry "
			"\"cn=Connection %ld,%s\"\n",
			c->c_connid, 
			ms->mss_dn.bv_val, 0 );
		return( -1 );
	}
306
307

#ifdef MONITOR_LEGACY_CONN
308
309
310
	/* NOTE: this will disappear, as the exploded data
	 * has been moved to dedicated attributes */
	bv.bv_len = snprintf( buf, sizeof( buf ),
311
312
313
314
315
316
317
318
319
320
321
			"%ld "
			": %ld "
			": %ld/%ld/%ld/%ld "
			": %ld/%ld/%ld "
			": %s%s%s%s%s%s "
			": %s "
			": %s "
			": %s "
			": %s "
			": %s "
			": %s "
322
			": %s",
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
			c->c_connid,
			(long) c->c_protocol,
			c->c_n_ops_received, c->c_n_ops_executing,
				c->c_n_ops_pending, c->c_n_ops_completed,
			
			/* add low-level counters here */
			c->c_n_get, c->c_n_read, c->c_n_write,
			
			c->c_currentber ? "r" : "",
			c->c_writewaiter ? "w" : "",
			LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
			LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
			connection_state2str( c->c_conn_state ),
			c->c_sasl_bind_in_progress ? "S" : "",
			
			c->c_dn.bv_len ? c->c_dn.bv_val : SLAPD_ANONYMOUS,
			
			c->c_listener_url.bv_val,
341
342
343
344
			BER_BVISNULL( &c->c_peer_domain )
				? "" : c->c_peer_domain.bv_val,
			BER_BVISNULL( &c->c_peer_name )
				? "" : c->c_peer_name.bv_val,
345
346
347
			c->c_sock_name.bv_val,
			
			buf2,
348
			buf3 );
349
	attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
350
#endif /* MONITOR_LEGACY_CONN */
351

352
353
354
	bv.bv_len = snprintf( buf, sizeof( buf ), "%lu", c->c_connid );
	attr_merge_one( e, mi->mi_ad_monitorConnectionNumber, &bv, NULL );

355
	bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", (long) c->c_protocol );
356
	attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionProtocol, &bv, NULL );
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379

	bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_received );
	attr_merge_one( e, mi->mi_ad_monitorConnectionOpsReceived, &bv, NULL );

	bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_executing );
	attr_merge_one( e, mi->mi_ad_monitorConnectionOpsExecuting, &bv, NULL );

	bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_pending );
	attr_merge_one( e, mi->mi_ad_monitorConnectionOpsPending, &bv, NULL );

	bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_ops_completed );
	attr_merge_one( e, mi->mi_ad_monitorConnectionOpsCompleted, &bv, NULL );

	bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_get );
	attr_merge_one( e, mi->mi_ad_monitorConnectionGet, &bv, NULL );

	bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_read );
	attr_merge_one( e, mi->mi_ad_monitorConnectionRead, &bv, NULL );

	bv.bv_len = snprintf( buf, sizeof( buf ), "%ld", c->c_n_write );
	attr_merge_one( e, mi->mi_ad_monitorConnectionWrite, &bv, NULL );

	bv.bv_len = snprintf( buf, sizeof( buf ), "%s%s%s%s%s%s",
380
381
382
383
384
			c->c_currentber ? "r" : "",
			c->c_writewaiter ? "w" : "",
			LDAP_STAILQ_EMPTY( &c->c_ops ) ? "" : "x",
			LDAP_STAILQ_EMPTY( &c->c_pending_ops ) ? "" : "p",
			connection_state2str( c->c_conn_state ),
385
			c->c_sasl_bind_in_progress ? "S" : "" );
386
	attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionMask, &bv, NULL );
387

388
389
	attr_merge_one( e, mi->mi_ad_monitorConnectionAuthzDN,
		&c->c_dn, &c->c_ndn );
390

391
	/* NOTE: client connections leave the c_peer_* fields NULL */
392
	assert( !BER_BVISNULL( &c->c_listener_url ) );
393
	attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionListener,
394
		&c->c_listener_url, NULL );
395

396
	attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionPeerDomain,
397
398
		BER_BVISNULL( &c->c_peer_domain ) ? &bv_unknown : &c->c_peer_domain,
		NULL );
399

400
	attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionPeerAddress,
401
402
403
		BER_BVISNULL( &c->c_peer_name ) ? &bv_unknown : &c->c_peer_name,
		NULL );

404
	assert( !BER_BVISNULL( &c->c_sock_name ) );
405
	attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionLocalAddress,
406
407
		&c->c_sock_name, NULL );

408
	attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionStartTime, &ctmbv, NULL );
409

410
	attr_merge_normalize_one( e, mi->mi_ad_monitorConnectionActivityTime, &mtmbv, NULL );
411

412
413
	mp = monitor_entrypriv_create();
	if ( mp == NULL ) {
414
		return LDAP_OTHER;
415
	}
416
	e->e_private = ( void * )mp;
417
	mp->mp_info = ms;
418
419
420
421
	mp->mp_flags = MONITOR_F_SUB | MONITOR_F_VOLATILE;

	*ep = e;

422
	return SLAP_CB_CONTINUE;
423
424
}

425
static int 
426
monitor_subsys_conn_create( 
427
	Operation		*op,
428
	SlapReply		*rs,
429
	struct berval		*ndn,
430
	Entry 			*e_parent,
431
	Entry			**ep )
432
{
433
	monitor_info_t	*mi = ( monitor_info_t * )op->o_bd->be_private;
434

435
	int			rc = SLAP_CB_CONTINUE;
436
	monitor_subsys_t	*ms;
437
438
439
440
441

	assert( mi != NULL );
	assert( e_parent != NULL );
	assert( ep != NULL );

442
	ms = (( monitor_entry_t *)e_parent->e_private)->mp_info;
443

444
445
446
	*ep = NULL;

	if ( ndn == NULL ) {
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
447
448
449
450
		Connection	*c;
		int		connindex;
		Entry		*e = NULL,
				*e_tmp = NULL;
451
452
453
454

		/* create all the children of e_parent */
		for ( c = connection_first( &connindex );
				c != NULL;
455
				c = connection_next( c, &connindex ) )
456
		{
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
457
458
			monitor_entry_t 	*mp;

459
460
461
			if ( conn_create( mi, c, &e, ms ) != SLAP_CB_CONTINUE
					|| e == NULL )
			{
462
				for ( ; e_tmp != NULL; ) {
463
					mp = ( monitor_entry_t * )e_tmp->e_private;
464
465
466
467
468
469
470
471
					e = mp->mp_next;

					ch_free( mp );
					e_tmp->e_private = NULL;
					entry_free( e_tmp );

					e_tmp = e;
				}
472
				rc = rs->sr_err = LDAP_OTHER;
473
				break;
474
			}
475
			mp = ( monitor_entry_t * )e->e_private;
476
477
478
			mp->mp_next = e_tmp;
			e_tmp = e;
		}
479
		connection_done( c );
480
		*ep = e;
481

482
	} else {
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
483
484
		Connection		*c;
		int			connindex;
485
486
487
		unsigned long 		connid;
		char			*next = NULL;
		static struct berval	nconn_bv = BER_BVC( "cn=connection " );
488

489
		rc = LDAP_NO_SUCH_OBJECT;
490
491
492
493
494
495
496
	       
		/* create exactly the required entry;
		 * the normalized DN must start with "cn=connection ",
		 * followed by the connection id, followed by
		 * the RDN separator "," */
		if ( ndn->bv_len <= nconn_bv.bv_len
				|| strncmp( ndn->bv_val, nconn_bv.bv_val, nconn_bv.bv_len ) != 0 )
497
		{
498
			return -1;
499
		}
500
		
501
502
		connid = strtol( &ndn->bv_val[ nconn_bv.bv_len ], &next, 10 );
		if ( next[ 0 ] != ',' ) {
503
			return ( rs->sr_err = LDAP_OTHER );
504
		}
505
506
507

		for ( c = connection_first( &connindex );
				c != NULL;
508
509
				c = connection_next( c, &connindex ) )
		{
510
			if ( c->c_connid == connid ) {
511
512
513
514
515
516
				rc = conn_create( mi, c, ep, ms );
				if ( rc != SLAP_CB_CONTINUE ) {
					rs->sr_err = rc;

				} else if ( *ep == NULL ) {
					rc = rs->sr_err = LDAP_OTHER;
517
				}
518
519

				break;
520
521
522
			}
		}
		
523
		connection_done( c );
524
525
	}

526
	return rc;
527
528
}