2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2009 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
33 #include <ac/socket.h>
35 #include <ac/string.h>
37 #include <ac/unistd.h>
43 #include "slapi/slapi.h"
46 /* protected by connections_mutex */
47 static ldap_pvt_thread_mutex_t connections_mutex;
48 static Connection *connections = NULL;
50 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
51 static unsigned long conn_nextid = 0;
53 static const char conn_lost_str[] = "connection lost";
55 /* structure state (protected by connections_mutex) */
56 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
57 #define SLAP_C_UNUSED 0x01
58 #define SLAP_C_USED 0x02
59 #define SLAP_C_PENDING 0x03
61 /* connection state (protected by c_mutex ) */
62 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
63 #define SLAP_C_INACTIVE 0x01 /* zero threads */
64 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
65 #define SLAP_C_BINDING 0x03 /* binding */
66 #define SLAP_C_CLOSING 0x04 /* closing */
67 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
70 connection_state2str( int state )
73 case SLAP_C_INVALID: return "!";
74 case SLAP_C_INACTIVE: return "|";
75 case SLAP_C_ACTIVE: return "";
76 case SLAP_C_BINDING: return "B";
77 case SLAP_C_CLOSING: return "C";
78 case SLAP_C_CLIENT: return "L";
84 static Connection* connection_get( ber_socket_t s );
86 typedef struct conn_readinfo {
88 ldap_pvt_thread_start_t *func;
94 static int connection_input( Connection *c, conn_readinfo *cri );
95 static void connection_close( Connection *c );
97 static int connection_op_activate( Operation *op );
98 static void connection_op_queue( Operation *op );
99 static int connection_resched( Connection *conn );
100 static void connection_abandon( Connection *conn );
101 static void connection_destroy( Connection *c );
103 static ldap_pvt_thread_start_t connection_operation;
106 * Initialize connection management infrastructure.
108 int connections_init(void)
112 assert( connections == NULL );
114 if( connections != NULL) {
115 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
120 /* should check return of every call */
121 ldap_pvt_thread_mutex_init( &connections_mutex );
122 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
124 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
126 if( connections == NULL ) {
127 Debug( LDAP_DEBUG_ANY, "connections_init: "
128 "allocation (%d*%ld) of connection array failed\n",
129 dtblsize, (long) sizeof(Connection), 0 );
133 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
134 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
136 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
139 * per entry initialization of the Connection array initialization
140 * will be done by connection_init()
147 * Destroy connection management infrastructure.
150 int connections_destroy(void)
154 /* should check return of every call */
156 if( connections == NULL) {
157 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
162 for ( i = 0; i < dtblsize; i++ ) {
163 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
164 ber_sockbuf_free( connections[i].c_sb );
165 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
166 ldap_pvt_thread_mutex_destroy( &connections[i].c_write1_mutex );
167 ldap_pvt_thread_mutex_destroy( &connections[i].c_write2_mutex );
168 ldap_pvt_thread_cond_destroy( &connections[i].c_write1_cv );
169 ldap_pvt_thread_cond_destroy( &connections[i].c_write2_cv );
171 if ( slapi_plugins_used ) {
172 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
182 ldap_pvt_thread_mutex_destroy( &connections_mutex );
183 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
188 * shutdown all connections
190 int connections_shutdown(void)
194 for ( i = 0; i < dtblsize; i++ ) {
195 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
196 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
197 if( connections[i].c_struct_state == SLAP_C_USED ) {
199 /* give persistent clients a chance to cleanup */
200 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
201 ldap_pvt_thread_pool_submit( &connection_pool,
202 connections[i].c_clientfunc, connections[i].c_clientarg );
204 /* c_mutex is locked */
205 connection_closing( &connections[i], "slapd shutdown" );
206 connection_close( &connections[i] );
209 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
217 * Timeout idle connections.
219 int connections_timeout_idle(time_t now)
225 for( c = connection_first( &connindex );
227 c = connection_next( c, &connindex ) )
229 /* Don't timeout a slow-running request or a persistent
230 * outbound connection. But if it has a writewaiter, see
231 * if the waiter has been there too long.
233 if(( c->c_n_ops_executing && !c->c_writewaiter)
234 || c->c_conn_state == SLAP_C_CLIENT ) {
238 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
240 connection_closing( c, "idletimeout" );
241 connection_close( c );
245 connection_done( c );
250 static Connection* connection_get( ber_socket_t s )
254 Debug( LDAP_DEBUG_ARGS,
255 "connection_get(%ld)\n",
258 assert( connections != NULL );
260 if(s == AC_SOCKET_INVALID) return NULL;
262 assert( s < dtblsize );
266 ldap_pvt_thread_mutex_lock( &c->c_mutex );
268 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
270 if( c->c_struct_state != SLAP_C_USED ) {
271 /* connection must have been closed due to resched */
273 assert( c->c_conn_state == SLAP_C_INVALID );
274 assert( c->c_sd == AC_SOCKET_INVALID );
276 Debug( LDAP_DEBUG_CONNS,
277 "connection_get(%d): connection not used\n",
280 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
284 Debug( LDAP_DEBUG_TRACE,
285 "connection_get(%d): got connid=%lu\n",
290 assert( c->c_struct_state == SLAP_C_USED );
291 assert( c->c_conn_state != SLAP_C_INVALID );
292 assert( c->c_sd != AC_SOCKET_INVALID );
294 #ifndef SLAPD_MONITOR
295 if ( global_idletimeout > 0 )
296 #endif /* ! SLAPD_MONITOR */
298 c->c_activitytime = slap_get_time();
305 static void connection_return( Connection *c )
307 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
310 Connection * connection_init(
314 const char* peername,
317 struct berval *authid
318 LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
323 ber_socket_t sfd = SLAP_FD2SOCK(s);
325 assert( connections != NULL );
327 assert( listener != NULL );
328 assert( dnsname != NULL );
329 assert( peername != NULL );
332 assert( !( flags & CONN_IS_TLS ));
335 if( s == AC_SOCKET_INVALID ) {
336 Debug( LDAP_DEBUG_ANY,
337 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
342 assert( s < dtblsize );
344 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
347 assert( c->c_struct_state == SLAP_C_UNUSED );
351 c->c_send_ldap_result = slap_send_ldap_result;
352 c->c_send_search_entry = slap_send_search_entry;
353 c->c_send_search_reference = slap_send_search_reference;
354 c->c_send_ldap_extended = slap_send_ldap_extended;
355 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
357 BER_BVZERO( &c->c_authmech );
358 BER_BVZERO( &c->c_dn );
359 BER_BVZERO( &c->c_ndn );
361 c->c_listener = NULL;
362 BER_BVZERO( &c->c_peer_domain );
363 BER_BVZERO( &c->c_peer_name );
365 LDAP_STAILQ_INIT(&c->c_ops);
366 LDAP_STAILQ_INIT(&c->c_pending_ops);
369 c->c_txn = CONN_TXN_INACTIVE;
370 c->c_txn_backend = NULL;
371 LDAP_STAILQ_INIT(&c->c_txn_ops);
374 BER_BVZERO( &c->c_sasl_bind_mech );
376 c->c_sasl_authctx = NULL;
377 c->c_sasl_sockctx = NULL;
378 c->c_sasl_extra = NULL;
379 c->c_sasl_bindop = NULL;
381 c->c_sb = ber_sockbuf_alloc( );
384 ber_len_t max = sockbuf_max_incoming;
385 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
388 c->c_currentber = NULL;
390 /* should check status of thread calls */
391 ldap_pvt_thread_mutex_init( &c->c_mutex );
392 ldap_pvt_thread_mutex_init( &c->c_write1_mutex );
393 ldap_pvt_thread_mutex_init( &c->c_write2_mutex );
394 ldap_pvt_thread_cond_init( &c->c_write1_cv );
395 ldap_pvt_thread_cond_init( &c->c_write2_cv );
398 if ( slapi_plugins_used ) {
399 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
404 ldap_pvt_thread_mutex_lock( &c->c_mutex );
406 assert( BER_BVISNULL( &c->c_authmech ) );
407 assert( BER_BVISNULL( &c->c_dn ) );
408 assert( BER_BVISNULL( &c->c_ndn ) );
409 assert( c->c_listener == NULL );
410 assert( BER_BVISNULL( &c->c_peer_domain ) );
411 assert( BER_BVISNULL( &c->c_peer_name ) );
412 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
413 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
415 assert( c->c_txn == CONN_TXN_INACTIVE );
416 assert( c->c_txn_backend == NULL );
417 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
419 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
420 assert( c->c_sasl_done == 0 );
421 assert( c->c_sasl_authctx == NULL );
422 assert( c->c_sasl_sockctx == NULL );
423 assert( c->c_sasl_extra == NULL );
424 assert( c->c_sasl_bindop == NULL );
425 assert( c->c_currentber == NULL );
426 assert( c->c_writewaiter == 0);
427 assert( c->c_writers == 0);
429 c->c_listener = listener;
432 if ( flags & CONN_IS_CLIENT ) {
434 ldap_pvt_thread_mutex_lock( &connections_mutex );
435 c->c_conn_state = SLAP_C_CLIENT;
436 c->c_struct_state = SLAP_C_USED;
437 ldap_pvt_thread_mutex_unlock( &connections_mutex );
438 c->c_close_reason = "?"; /* should never be needed */
439 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
440 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
445 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
446 ber_str2bv( peername, 0, 1, &c->c_peer_name );
448 c->c_n_ops_received = 0;
449 c->c_n_ops_executing = 0;
450 c->c_n_ops_pending = 0;
451 c->c_n_ops_completed = 0;
457 /* set to zero until bind, implies LDAP_VERSION3 */
460 #ifndef SLAPD_MONITOR
461 if ( global_idletimeout > 0 )
462 #endif /* ! SLAPD_MONITOR */
464 c->c_activitytime = c->c_starttime = slap_get_time();
467 #ifdef LDAP_CONNECTIONLESS
469 if( flags & CONN_IS_UDP ) {
472 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
473 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
475 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
476 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
477 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
478 LBER_SBIOD_LEVEL_PROVIDER, NULL );
480 #endif /* LDAP_CONNECTIONLESS */
482 if ( flags & CONN_IS_IPC ) {
484 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
485 LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
487 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
488 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
489 #ifdef LDAP_PF_LOCAL_SENDMSG
490 if ( !BER_BVISEMPTY( peerbv ))
491 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
494 #endif /* LDAP_PF_LOCAL */
497 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
498 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
500 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
501 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
505 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
506 INT_MAX, (void*)"ldap_" );
509 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
510 c /* non-NULL */ ) < 0 )
512 Debug( LDAP_DEBUG_ANY,
513 "connection_init(%d, %s): set nonblocking failed\n",
514 s, c->c_peer_name.bv_val, 0 );
517 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
518 id = c->c_connid = conn_nextid++;
519 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
521 ldap_pvt_thread_mutex_lock( &connections_mutex );
522 c->c_conn_state = SLAP_C_INACTIVE;
523 c->c_struct_state = SLAP_C_USED;
524 ldap_pvt_thread_mutex_unlock( &connections_mutex );
525 c->c_close_reason = "?"; /* should never be needed */
527 c->c_ssf = c->c_transport_ssf = ssf;
531 if ( flags & CONN_IS_TLS ) {
533 c->c_needs_tls_accept = 1;
536 c->c_needs_tls_accept = 0;
540 slap_sasl_open( c, 0 );
541 slap_sasl_external( c, ssf, authid );
543 slapd_add_internal( s, 1 );
544 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
546 backend_connection_init(c);
551 void connection2anonymous( Connection *c )
553 assert( connections != NULL );
557 ber_len_t max = sockbuf_max_incoming;
558 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
561 if ( !BER_BVISNULL( &c->c_authmech ) ) {
562 ch_free(c->c_authmech.bv_val);
564 BER_BVZERO( &c->c_authmech );
566 if ( !BER_BVISNULL( &c->c_dn ) ) {
567 ch_free(c->c_dn.bv_val);
569 BER_BVZERO( &c->c_dn );
571 if ( !BER_BVISNULL( &c->c_ndn ) ) {
572 ch_free(c->c_ndn.bv_val);
574 BER_BVZERO( &c->c_ndn );
576 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
577 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
579 BER_BVZERO( &c->c_sasl_authz_dn );
581 c->c_authz_backend = NULL;
585 connection_destroy( Connection *c )
587 unsigned long connid;
588 const char *close_reason;
592 assert( connections != NULL );
594 assert( c->c_struct_state != SLAP_C_UNUSED );
595 assert( c->c_conn_state != SLAP_C_INVALID );
596 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
597 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
599 assert( c->c_txn == CONN_TXN_INACTIVE );
600 assert( c->c_txn_backend == NULL );
601 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
603 assert( c->c_writewaiter == 0);
604 assert( c->c_writers == 0);
606 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
607 connid = c->c_connid;
608 close_reason = c->c_close_reason;
610 ldap_pvt_thread_mutex_lock( &connections_mutex );
611 c->c_struct_state = SLAP_C_PENDING;
612 ldap_pvt_thread_mutex_unlock( &connections_mutex );
614 backend_connection_destroy(c);
619 c->c_activitytime = c->c_starttime = 0;
621 connection2anonymous( c );
622 c->c_listener = NULL;
624 if(c->c_peer_domain.bv_val != NULL) {
625 free(c->c_peer_domain.bv_val);
627 BER_BVZERO( &c->c_peer_domain );
628 if(c->c_peer_name.bv_val != NULL) {
629 free(c->c_peer_name.bv_val);
631 BER_BVZERO( &c->c_peer_name );
633 c->c_sasl_bind_in_progress = 0;
634 if(c->c_sasl_bind_mech.bv_val != NULL) {
635 free(c->c_sasl_bind_mech.bv_val);
637 BER_BVZERO( &c->c_sasl_bind_mech );
639 slap_sasl_close( c );
641 if ( c->c_currentber != NULL ) {
642 ber_free( c->c_currentber, 1 );
643 c->c_currentber = NULL;
648 /* call destructors, then constructors; avoids unnecessary allocation */
649 if ( slapi_plugins_used ) {
650 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
655 c->c_sd = AC_SOCKET_INVALID;
656 c->c_conn_state = SLAP_C_INVALID;
657 c->c_struct_state = SLAP_C_UNUSED;
658 c->c_close_reason = "?"; /* should never be needed */
661 c->c_sb = ber_sockbuf_alloc( );
663 ber_len_t max = sockbuf_max_incoming;
664 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
667 /* c must be fully reset by this point; when we call slapd_remove
668 * it may get immediately reused by a new connection.
670 if ( sd != AC_SOCKET_INVALID ) {
671 slapd_remove( sd, sb, 1, 0, 0 );
673 if ( close_reason == NULL ) {
674 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
675 connid, (long) sd, 0, 0, 0 );
677 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
678 connid, (long) sd, close_reason, 0, 0 );
683 int connection_state_closing( Connection *c )
685 /* c_mutex must be locked by caller */
689 assert( c->c_struct_state == SLAP_C_USED );
691 state = c->c_conn_state;
693 assert( state != SLAP_C_INVALID );
695 return state == SLAP_C_CLOSING;
698 static void connection_abandon( Connection *c )
700 /* c_mutex must be locked by caller */
702 Operation *o, *next, op = {0};
708 op.o_connid = c->c_connid;
709 op.o_tag = LDAP_REQ_ABANDON;
711 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
712 next = LDAP_STAILQ_NEXT( o, o_next );
713 op.orn_msgid = o->o_msgid;
715 op.o_bd = frontendDB;
716 frontendDB->be_abandon( &op, &rs );
720 /* remove operations in pending transaction */
721 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
722 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
723 LDAP_STAILQ_NEXT(o, o_next) = NULL;
724 slap_op_free( o, NULL );
727 /* clear transaction */
728 c->c_txn_backend = NULL;
729 c->c_txn = CONN_TXN_INACTIVE;
732 /* remove pending operations */
733 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
734 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
735 LDAP_STAILQ_NEXT(o, o_next) = NULL;
736 slap_op_free( o, NULL );
740 void connection_closing( Connection *c, const char *why )
742 assert( connections != NULL );
744 assert( c->c_struct_state == SLAP_C_USED );
745 assert( c->c_conn_state != SLAP_C_INVALID );
747 /* c_mutex must be locked by caller */
749 if( c->c_conn_state != SLAP_C_CLOSING ) {
750 Debug( LDAP_DEBUG_CONNS,
751 "connection_closing: readying conn=%lu sd=%d for close\n",
752 c->c_connid, c->c_sd, 0 );
753 /* update state to closing */
754 c->c_conn_state = SLAP_C_CLOSING;
755 c->c_close_reason = why;
757 /* don't listen on this port anymore */
758 slapd_clr_read( c->c_sd, 0 );
760 /* abandon active operations */
761 connection_abandon( c );
763 /* wake write blocked operations */
764 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
765 if ( c->c_writers > 0 ) {
766 c->c_writers = -c->c_writers;
767 ldap_pvt_thread_cond_broadcast( &c->c_write1_cv );
768 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
769 if ( c->c_writewaiter ) {
770 ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
771 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
772 slapd_clr_write( c->c_sd, 1 );
773 ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
775 ldap_pvt_thread_mutex_lock( &c->c_write1_mutex );
776 while ( c->c_writers ) {
777 ldap_pvt_thread_cond_wait( &c->c_write1_cv, &c->c_write1_mutex );
779 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
781 ldap_pvt_thread_mutex_unlock( &c->c_write1_mutex );
782 slapd_clr_write( c->c_sd, 1 );
785 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
786 /* Client closed connection after doing Unbind. */
787 c->c_close_reason = NULL;
792 connection_close( Connection *c )
794 assert( connections != NULL );
796 assert( c->c_struct_state == SLAP_C_USED );
797 assert( c->c_conn_state == SLAP_C_CLOSING );
799 /* NOTE: c_mutex should be locked by caller */
801 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
802 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
804 Debug( LDAP_DEBUG_CONNS,
805 "connection_close: deferring conn=%lu sd=%d\n",
806 c->c_connid, c->c_sd, 0 );
810 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
811 c->c_connid, c->c_sd, 0 );
813 connection_destroy( c );
816 unsigned long connections_nextid(void)
819 assert( connections != NULL );
821 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
825 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
830 Connection* connection_first( ber_socket_t *index )
832 assert( connections != NULL );
833 assert( index != NULL );
835 ldap_pvt_thread_mutex_lock( &connections_mutex );
836 for( *index = 0; *index < dtblsize; (*index)++) {
837 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
841 ldap_pvt_thread_mutex_unlock( &connections_mutex );
843 return connection_next(NULL, index);
846 Connection* connection_next( Connection *c, ber_socket_t *index )
848 assert( connections != NULL );
849 assert( index != NULL );
850 assert( *index <= dtblsize );
852 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
856 ldap_pvt_thread_mutex_lock( &connections_mutex );
857 for(; *index < dtblsize; (*index)++) {
859 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
860 /* FIXME: accessing c_conn_state without locking c_mutex */
861 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
865 if( connections[*index].c_struct_state == SLAP_C_USED ) {
866 c = &connections[(*index)++];
867 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
869 ldap_pvt_thread_mutex_unlock( &connections_mutex );
870 ldap_pvt_thread_mutex_lock( &c->c_mutex );
871 ldap_pvt_thread_mutex_lock( &connections_mutex );
872 if ( c->c_struct_state != SLAP_C_USED ) {
873 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
878 assert( c->c_conn_state != SLAP_C_INVALID );
882 c_struct = connections[*index].c_struct_state;
883 if ( c_struct == SLAP_C_PENDING )
885 assert( c_struct == SLAP_C_UNUSED );
886 /* FIXME: accessing c_conn_state without locking c_mutex */
887 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
890 ldap_pvt_thread_mutex_unlock( &connections_mutex );
894 void connection_done( Connection *c )
896 assert( connections != NULL );
898 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
902 * connection_activity - handle the request operation op on connection
903 * conn. This routine figures out what kind of operation it is and
904 * calls the appropriate stub to handle it.
908 /* FIXME: returns 0 in case of failure */
909 #define INCR_OP_INITIATED(index) \
911 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
912 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated_[(index)], 1); \
913 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
915 #define INCR_OP_COMPLETED(index) \
917 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
918 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
919 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed_[(index)], 1); \
920 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
922 #else /* !SLAPD_MONITOR */
923 #define INCR_OP_INITIATED(index) do { } while (0)
924 #define INCR_OP_COMPLETED(index) \
926 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
927 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
928 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
930 #endif /* !SLAPD_MONITOR */
933 * NOTE: keep in sync with enum in slapd.h
935 static BI_op_func *opfun[] = {
949 /* Counters are per-thread, not per-connection.
952 conn_counter_destroy( void *key, void *data )
954 slap_counters_t **prev, *sc;
956 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
957 for ( prev = &slap_counters.sc_next, sc = slap_counters.sc_next; sc;
958 prev = &sc->sc_next, sc = sc->sc_next ) {
963 /* Copy data to main counter */
964 ldap_pvt_mp_add( slap_counters.sc_bytes, sc->sc_bytes );
965 ldap_pvt_mp_add( slap_counters.sc_pdu, sc->sc_pdu );
966 ldap_pvt_mp_add( slap_counters.sc_entries, sc->sc_entries );
967 ldap_pvt_mp_add( slap_counters.sc_refs, sc->sc_refs );
968 ldap_pvt_mp_add( slap_counters.sc_ops_initiated, sc->sc_ops_initiated );
969 ldap_pvt_mp_add( slap_counters.sc_ops_completed, sc->sc_ops_completed );
971 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
972 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_initiated_[ i ] );
973 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_completed_[ i ] );
975 #endif /* SLAPD_MONITOR */
976 slap_counters_destroy( sc );
977 ber_memfree_x( data, NULL );
981 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
985 conn_counter_init( Operation *op, void *ctx )
990 if ( ldap_pvt_thread_pool_getkey(
991 ctx, (void *)conn_counter_init, &vsc, NULL ) || !vsc ) {
992 vsc = ch_malloc( sizeof( slap_counters_t ));
994 slap_counters_init( sc );
995 ldap_pvt_thread_pool_setkey( ctx, (void*)conn_counter_init, vsc,
996 conn_counter_destroy, NULL, NULL );
998 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
999 sc->sc_next = slap_counters.sc_next;
1000 slap_counters.sc_next = sc;
1001 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
1003 op->o_counters = vsc;
1007 connection_operation( void *ctx, void *arg_v )
1009 int rc = LDAP_OTHER;
1010 Operation *op = arg_v;
1011 SlapReply rs = {REP_RESULT};
1012 ber_tag_t tag = op->o_tag;
1013 slap_op_t opidx = SLAP_OP_LAST;
1014 Connection *conn = op->o_conn;
1015 void *memctx = NULL;
1016 void *memctx_null = NULL;
1019 conn_counter_init( op, ctx );
1020 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1021 /* FIXME: returns 0 in case of failure */
1022 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated, 1);
1023 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1025 op->o_threadctx = ctx;
1026 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1030 case LDAP_REQ_UNBIND:
1032 case LDAP_REQ_DELETE:
1033 case LDAP_REQ_MODDN:
1034 case LDAP_REQ_MODIFY:
1035 case LDAP_REQ_COMPARE:
1036 case LDAP_REQ_SEARCH:
1037 case LDAP_REQ_ABANDON:
1038 case LDAP_REQ_EXTENDED:
1041 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1042 "conn %lu unknown LDAP request 0x%lx\n",
1043 conn->c_connid, tag, 0 );
1044 op->o_tag = LBER_ERROR;
1045 rs.sr_err = LDAP_PROTOCOL_ERROR;
1046 rs.sr_text = "unknown LDAP request";
1047 send_ldap_disconnect( op, &rs );
1048 rc = SLAPD_DISCONNECT;
1049 goto operations_error;
1052 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1053 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1054 "error: SASL bind in progress (tag=%ld).\n",
1056 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1057 "SASL bind in progress" );
1058 rc = LDAP_OPERATIONS_ERROR;
1059 goto operations_error;
1063 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1064 ( tag == LDAP_REQ_ADD ) ||
1065 ( tag == LDAP_REQ_DELETE ) ||
1066 ( tag == LDAP_REQ_MODIFY ) ||
1067 ( tag == LDAP_REQ_MODRDN )))
1069 /* Disable SLAB allocator for all update operations
1070 issued inside of a transaction */
1071 op->o_tmpmemctx = NULL;
1072 op->o_tmpmfuncs = &ch_mfuncs;
1076 /* We can use Thread-Local storage for most mallocs. We can
1077 * also use TL for ber parsing, but not on Add or Modify.
1080 memsiz = ber_len( op->o_ber ) * 64;
1081 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1083 memsiz = SLAP_SLAB_SIZE;
1085 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1086 op->o_tmpmemctx = memctx;
1087 op->o_tmpmfuncs = &slap_sl_mfuncs;
1088 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1089 /* Note - the ber and its buffer are already allocated from
1090 * regular memory; this only affects subsequent mallocs that
1091 * ber_scanf may invoke.
1093 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1097 opidx = slap_req2op( tag );
1098 assert( opidx != SLAP_OP_LAST );
1099 INCR_OP_INITIATED( opidx );
1100 rc = (*(opfun[opidx]))( op, &rs );
1103 if ( rc == SLAPD_DISCONNECT ) {
1106 } else if ( opidx != SLAP_OP_LAST ) {
1107 /* increment completed operations count
1108 * only if operation was initiated
1109 * and rc != SLAPD_DISCONNECT */
1110 INCR_OP_COMPLETED( opidx );
1113 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1114 if ( rc == SLAPD_ABANDON ) {
1115 op->o_cancel = SLAP_CANCEL_ACK;
1117 op->o_cancel = LDAP_TOO_LATE;
1121 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1122 op->o_cancel != SLAP_CANCEL_DONE )
1124 ldap_pvt_thread_yield();
1127 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1129 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1131 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1132 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1133 conn->c_n_ops_executing--;
1134 conn->c_n_ops_completed++;
1138 case LDAP_REQ_UNBIND:
1139 /* c_mutex is locked */
1140 connection_closing( conn,
1141 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1145 connection_resched( conn );
1146 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1147 slap_op_free( op, ctx );
1151 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1153 Connection *connection_client_setup(
1155 ldap_pvt_thread_start_t *func,
1159 ber_socket_t sfd = SLAP_SOCKNEW( s );
1161 c = connection_init( sfd, (Listener *)&dummy_list, "", "",
1162 CONN_IS_CLIENT, 0, NULL
1163 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
1165 c->c_clientfunc = func;
1166 c->c_clientarg = arg;
1168 slapd_add_internal( sfd, 0 );
1173 void connection_client_enable(
1176 slapd_set_read( c->c_sd, 1 );
1179 void connection_client_stop(
1183 ber_socket_t s = c->c_sd;
1185 /* get (locked) connection */
1186 c = connection_get( s );
1188 assert( c->c_conn_state == SLAP_C_CLIENT );
1190 c->c_listener = NULL;
1191 c->c_conn_state = SLAP_C_INVALID;
1192 c->c_struct_state = SLAP_C_UNUSED;
1193 c->c_sd = AC_SOCKET_INVALID;
1194 c->c_close_reason = "?"; /* should never be needed */
1196 c->c_sb = ber_sockbuf_alloc( );
1198 ber_len_t max = sockbuf_max_incoming;
1199 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1201 slapd_remove( s, sb, 0, 1, 0 );
1203 connection_return( c );
1206 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1208 static void* connection_read_thread( void* ctx, void* argv )
1211 conn_readinfo cri = { NULL, NULL, NULL, NULL, 0 };
1212 ber_socket_t s = (long)argv;
1215 * read incoming LDAP requests. If there is more than one,
1216 * the first one is returned with new_op
1219 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1220 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1221 return (void*)(long)rc;
1224 /* execute a single queued request in the same thread */
1225 if( cri.op && !cri.nullop ) {
1226 rc = (long)connection_operation( ctx, cri.op );
1227 } else if ( cri.func ) {
1228 rc = (long)cri.func( ctx, cri.arg );
1231 return (void*)(long)rc;
1234 int connection_read_activate( ber_socket_t s )
1239 * suspend reading on this file descriptor until a connection processing
1240 * thread reads data on it. Otherwise the listener thread will repeatedly
1241 * submit the same event on it to the pool.
1243 rc = slapd_clr_read( s, 0 );
1247 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1248 connection_read_thread, (void *)(long)s );
1251 Debug( LDAP_DEBUG_ANY,
1252 "connection_read_activate(%d): submit failed (%d)\n",
1260 connection_hangup( ber_socket_t s )
1264 c = connection_get( s );
1266 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1267 connection_return( c );
1268 connection_read_activate( s );
1270 connection_closing( c, "connection lost" );
1271 connection_close( c );
1272 connection_return( c );
1278 connection_read( ber_socket_t s, conn_readinfo *cri )
1283 assert( connections != NULL );
1285 /* get (locked) connection */
1286 c = connection_get( s );
1289 Debug( LDAP_DEBUG_ANY,
1290 "connection_read(%ld): no connection!\n",
1298 if( c->c_conn_state == SLAP_C_CLOSING ) {
1299 Debug( LDAP_DEBUG_CONNS,
1300 "connection_read(%d): closing, ignoring input for id=%lu\n",
1301 s, c->c_connid, 0 );
1302 connection_return( c );
1306 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1307 cri->func = c->c_clientfunc;
1308 cri->arg = c->c_clientarg;
1309 /* read should already be cleared */
1310 connection_return( c );
1314 Debug( LDAP_DEBUG_TRACE,
1315 "connection_read(%d): checking for input on id=%lu\n",
1316 s, c->c_connid, 0 );
1319 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1320 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1322 Debug( LDAP_DEBUG_TRACE,
1323 "connection_read(%d): TLS accept failure "
1324 "error=%d id=%lu, closing\n",
1325 s, rc, c->c_connid );
1327 c->c_needs_tls_accept = 0;
1328 /* c_mutex is locked */
1329 connection_closing( c, "TLS negotiation failure" );
1330 connection_close( c );
1331 connection_return( c );
1334 } else if ( rc == 0 ) {
1336 struct berval authid = BER_BVNULL;
1338 c->c_needs_tls_accept = 0;
1340 /* we need to let SASL know */
1341 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1343 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1344 if( c->c_tls_ssf > c->c_ssf ) {
1345 c->c_ssf = c->c_tls_ssf;
1348 rc = dnX509peerNormalize( ssl, &authid );
1349 if ( rc != LDAP_SUCCESS ) {
1350 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1351 "unable to get TLS client DN, error=%d id=%lu\n",
1352 s, rc, c->c_connid );
1354 Statslog( LDAP_DEBUG_STATS,
1355 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1356 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1357 slap_sasl_external( c, c->c_tls_ssf, &authid );
1358 if ( authid.bv_val ) free( authid.bv_val );
1361 /* if success and data is ready, fall thru to data input loop */
1362 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1364 slapd_set_read( s, 1 );
1365 connection_return( c );
1371 #ifdef HAVE_CYRUS_SASL
1372 if ( c->c_sasl_layers ) {
1373 /* If previous layer is not removed yet, give up for now */
1374 if ( !c->c_sasl_sockctx ) {
1375 slapd_set_read( s, 1 );
1376 connection_return( c );
1380 c->c_sasl_layers = 0;
1382 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1383 if( rc != LDAP_SUCCESS ) {
1384 Debug( LDAP_DEBUG_TRACE,
1385 "connection_read(%d): SASL install error "
1386 "error=%d id=%lu, closing\n",
1387 s, rc, c->c_connid );
1389 /* c_mutex is locked */
1390 connection_closing( c, "SASL layer install failure" );
1391 connection_close( c );
1392 connection_return( c );
1398 #define CONNECTION_INPUT_LOOP 1
1399 /* #define DATA_READY_LOOP 1 */
1402 /* How do we do this without getting into a busy loop ? */
1403 rc = connection_input( c, cri );
1405 #ifdef DATA_READY_LOOP
1406 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1407 #elif defined CONNECTION_INPUT_LOOP
1414 Debug( LDAP_DEBUG_CONNS,
1415 "connection_read(%d): input error=%d id=%lu, closing.\n",
1416 s, rc, c->c_connid );
1418 /* c_mutex is locked */
1419 connection_closing( c, conn_lost_str );
1420 connection_close( c );
1421 connection_return( c );
1425 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1426 slapd_set_write( s, 0 );
1429 slapd_set_read( s, 1 );
1430 connection_return( c );
1436 connection_input( Connection *conn , conn_readinfo *cri )
1444 #ifdef LDAP_CONNECTIONLESS
1451 if ( conn->c_currentber == NULL &&
1452 ( conn->c_currentber = ber_alloc()) == NULL )
1454 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1460 #ifdef LDAP_CONNECTIONLESS
1461 if ( conn->c_is_udp ) {
1462 char peername[sizeof("IP=255.255.255.255:65336")];
1464 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1465 if (len != sizeof(struct sockaddr)) return 1;
1467 sprintf( peername, "IP=%s:%d",
1468 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1469 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1470 Statslog( LDAP_DEBUG_STATS,
1471 "conn=%lu UDP request from %s (%s) accepted.\n",
1472 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1476 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1477 if ( tag != LDAP_TAG_MESSAGE ) {
1478 int err = sock_errno();
1480 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1481 /* log, close and send error */
1482 Debug( LDAP_DEBUG_TRACE,
1483 "ber_get_next on fd %d failed errno=%d (%s)\n",
1484 conn->c_sd, err, sock_errstr(err) );
1485 ber_free( conn->c_currentber, 1 );
1486 conn->c_currentber = NULL;
1493 ber = conn->c_currentber;
1494 conn->c_currentber = NULL;
1496 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1497 /* log, close and send error */
1498 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1503 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1504 /* log, close and send error */
1505 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1511 #ifdef LDAP_CONNECTIONLESS
1512 if( conn->c_is_udp ) {
1513 if( tag == LBER_OCTETSTRING ) {
1514 ber_get_stringa( ber, &cdn );
1515 tag = ber_peek_tag(ber, &len);
1517 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1518 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1525 if(tag == LDAP_REQ_BIND) {
1526 /* immediately abandon all existing operations upon BIND */
1527 connection_abandon( conn );
1531 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++, ctx );
1534 /* clear state if the connection is being reused from inactive */
1535 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1536 memset( &conn->c_pagedresults_state, 0,
1537 sizeof( conn->c_pagedresults_state ) );
1540 op->o_res_ber = NULL;
1542 #ifdef LDAP_CONNECTIONLESS
1543 if (conn->c_is_udp) {
1545 ber_str2bv( cdn, 0, 1, &op->o_dn );
1546 op->o_protocol = LDAP_VERSION2;
1548 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1549 if (op->o_res_ber == NULL) return 1;
1551 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1552 sizeof(struct sockaddr), 0 );
1554 if (rc != sizeof(struct sockaddr)) {
1555 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1559 if (op->o_protocol == LDAP_VERSION2) {
1560 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1562 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1567 #endif /* LDAP_CONNECTIONLESS */
1571 /* Don't process requests when the conn is in the middle of a
1572 * Bind, or if it's closing. Also, don't let any single conn
1573 * use up all the available threads, and don't execute if we're
1574 * currently blocked on output. And don't execute if there are
1575 * already pending ops, let them go first. Abandon operations
1576 * get exceptions to some, but not all, cases.
1580 /* Abandon and Unbind are exempt from these checks */
1581 if (conn->c_conn_state == SLAP_C_CLOSING) {
1584 } else if (conn->c_writewaiter) {
1585 defer = "awaiting write";
1587 } else if (conn->c_n_ops_pending) {
1588 defer = "pending operations";
1592 case LDAP_REQ_ABANDON:
1593 /* Unbind is exempt from these checks */
1594 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1595 defer = "too many executing";
1597 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1602 case LDAP_REQ_UNBIND:
1607 int max = conn->c_dn.bv_len
1608 ? slap_conn_max_pending_auth
1609 : slap_conn_max_pending;
1611 Debug( LDAP_DEBUG_ANY,
1612 "connection_input: conn=%lu deferring operation: %s\n",
1613 conn->c_connid, defer, 0 );
1614 conn->c_n_ops_pending++;
1615 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1616 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1619 conn->c_n_ops_executing++;
1622 * The first op will be processed in the same thread context,
1623 * as long as there is only one op total.
1624 * Subsequent ops will be submitted to the pool by
1625 * calling connection_op_activate()
1627 if ( cri->op == NULL ) {
1628 /* the first incoming request */
1629 connection_op_queue( op );
1632 if ( !cri->nullop ) {
1634 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1635 connection_operation, (void *) cri->op );
1637 connection_op_activate( op );
1642 if ( conn->c_struct_state != SLAP_C_USED ) {
1643 /* connection must have got closed underneath us */
1648 assert( conn->c_struct_state == SLAP_C_USED );
1653 connection_resched( Connection *conn )
1657 if( conn->c_writewaiter )
1660 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1661 Debug( LDAP_DEBUG_CONNS, "connection_resched: "
1662 "attempting closing conn=%lu sd=%d\n",
1663 conn->c_connid, conn->c_sd, 0 );
1664 connection_close( conn );
1668 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1669 /* other states need different handling */
1673 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1674 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1676 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1677 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1679 /* pending operations should not be marked for abandonment */
1680 assert(!op->o_abandon);
1682 conn->c_n_ops_pending--;
1683 conn->c_n_ops_executing++;
1685 connection_op_activate( op );
1687 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1693 connection_init_log_prefix( Operation *op )
1695 if ( op->o_connid == (unsigned long)(-1) ) {
1696 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1697 "conn=-1 op=%lu", op->o_opid );
1700 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1701 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1705 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1707 op->o_conn->c_sasl_bindop = NULL;
1709 ch_free( op->o_callback );
1710 op->o_callback = NULL;
1712 return SLAP_CB_CONTINUE;
1715 static int connection_bind_cb( Operation *op, SlapReply *rs )
1717 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1718 if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1719 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1720 op->o_conn->c_sasl_bind_in_progress =
1721 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1723 /* Moved here from bind.c due to ITS#4158 */
1724 op->o_conn->c_sasl_bindop = NULL;
1725 if ( op->orb_method == LDAP_AUTH_SASL ) {
1726 if( rs->sr_err == LDAP_SUCCESS ) {
1727 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1728 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1729 /* edn is always normalized already */
1730 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1732 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1733 BER_BVZERO( &op->orb_edn );
1734 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1735 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1737 op->o_conn->c_sasl_ssf = op->orb_ssf;
1738 if( op->orb_ssf > op->o_conn->c_ssf ) {
1739 op->o_conn->c_ssf = op->orb_ssf;
1742 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1743 ber_len_t max = sockbuf_max_incoming_auth;
1744 ber_sockbuf_ctrl( op->o_conn->c_sb,
1745 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1748 /* log authorization identity */
1749 Statslog( LDAP_DEBUG_STATS,
1750 "%s BIND dn=\"%s\" mech=%s sasl_ssf=%d ssf=%d\n",
1752 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1753 op->o_conn->c_authmech.bv_val,
1754 op->orb_ssf, op->o_conn->c_ssf );
1756 Debug( LDAP_DEBUG_TRACE,
1757 "do_bind: SASL/%s bind: dn=\"%s\" sasl_ssf=%d\n",
1758 op->o_conn->c_authmech.bv_val,
1759 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1762 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1763 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1764 free( op->o_conn->c_sasl_bind_mech.bv_val );
1765 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1769 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1771 ch_free( op->o_callback );
1772 op->o_callback = NULL;
1774 return SLAP_CB_CONTINUE;
1777 static void connection_op_queue( Operation *op )
1779 ber_tag_t tag = op->o_tag;
1781 if (tag == LDAP_REQ_BIND) {
1782 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1783 sc->sc_response = connection_bind_cb;
1784 sc->sc_cleanup = connection_bind_cleanup_cb;
1785 sc->sc_next = op->o_callback;
1786 op->o_callback = sc;
1787 op->o_conn->c_conn_state = SLAP_C_BINDING;
1790 if (!op->o_dn.bv_len) {
1791 op->o_authz = op->o_conn->c_authz;
1792 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1793 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1794 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1796 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1797 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1801 op->o_authtype = op->o_conn->c_authtype;
1802 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1804 if (!op->o_protocol) {
1805 op->o_protocol = op->o_conn->c_protocol
1806 ? op->o_conn->c_protocol : LDAP_VERSION3;
1809 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1810 op->o_protocol > LDAP_VERSION2)
1812 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1815 op->o_connid = op->o_conn->c_connid;
1816 connection_init_log_prefix( op );
1818 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1821 static int connection_op_activate( Operation *op )
1825 connection_op_queue( op );
1827 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1828 connection_operation, (void *) op );
1831 Debug( LDAP_DEBUG_ANY,
1832 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1833 rc, op->o_connid, 0 );
1834 /* should move op to pending list */
1840 int connection_write(ber_socket_t s)
1845 assert( connections != NULL );
1847 slapd_clr_write( s, 0 );
1849 c = connection_get( s );
1851 Debug( LDAP_DEBUG_ANY,
1852 "connection_write(%ld): no connection!\n",
1859 Debug( LDAP_DEBUG_TRACE,
1860 "connection_write(%d): waking output for id=%lu\n",
1861 s, c->c_connid, 0 );
1862 ldap_pvt_thread_mutex_lock( &c->c_write2_mutex );
1863 ldap_pvt_thread_cond_signal( &c->c_write2_cv );
1864 ldap_pvt_thread_mutex_unlock( &c->c_write2_mutex );
1866 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1867 slapd_set_read( s, 1 );
1869 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1870 slapd_set_write( s, 1 );
1873 /* If there are ops pending because of a writewaiter,
1876 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1877 if ( !c->c_writewaiter ) break;
1878 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1880 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1881 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1883 /* pending operations should not be marked for abandonment */
1884 assert(!op->o_abandon);
1886 c->c_n_ops_pending--;
1887 c->c_n_ops_executing++;
1889 connection_op_activate( op );
1894 connection_return( c );
1899 typedef struct conn_fake_extblock {
1902 } conn_fake_extblock;
1905 connection_fake_destroy(
1909 Connection conn = {0};
1911 Opheader ohdr = {0};
1913 conn_fake_extblock *eb = data;
1916 op.o_hdr->oh_extensions = eb->eb_op;
1917 conn.c_extensions = eb->eb_conn;
1922 ber_memfree_x( eb, NULL );
1923 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
1924 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
1929 connection_fake_init(
1931 OperationBuffer *opbuf,
1934 connection_fake_init2( conn, opbuf, ctx, 1 );
1938 operation_fake_init(
1944 /* set memory context */
1945 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
1947 op->o_tmpmfuncs = &slap_sl_mfuncs;
1948 op->o_threadctx = ctx;
1949 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1951 op->o_counters = &slap_counters;
1953 op->o_connid = op->o_conn->c_connid;
1954 connection_init_log_prefix( op );
1959 connection_fake_init2(
1961 OperationBuffer *opbuf,
1965 Operation *op = (Operation *) opbuf;
1967 conn->c_connid = -1;
1968 conn->c_conn_idx = -1;
1969 conn->c_send_ldap_result = slap_send_ldap_result;
1970 conn->c_send_search_entry = slap_send_search_entry;
1971 conn->c_send_search_reference = slap_send_search_reference;
1972 conn->c_listener = (Listener *)&dummy_list;
1973 conn->c_peer_domain = slap_empty_bv;
1974 conn->c_peer_name = slap_empty_bv;
1976 memset( opbuf, 0, sizeof( *opbuf ));
1977 op->o_hdr = &opbuf->ob_hdr;
1978 op->o_controls = opbuf->ob_controls;
1980 operation_fake_init( conn, op, ctx, newmem );
1983 if ( slapi_plugins_used ) {
1984 conn_fake_extblock *eb;
1987 /* Use thread keys to make sure these eventually get cleaned up */
1988 if ( ldap_pvt_thread_pool_getkey( ctx, (void *)connection_fake_init,
1990 eb = ch_malloc( sizeof( *eb ));
1991 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
1992 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
1993 eb->eb_conn = conn->c_extensions;
1994 eb->eb_op = op->o_hdr->oh_extensions;
1995 ldap_pvt_thread_pool_setkey( ctx, (void *)connection_fake_init,
1996 eb, connection_fake_destroy, NULL, NULL );
1999 conn->c_extensions = eb->eb_conn;
2000 op->o_hdr->oh_extensions = eb->eb_op;
2003 #endif /* LDAP_SLAPI */
2005 slap_op_time( &op->o_time, &op->o_tincr );
2009 connection_assign_nextid( Connection *conn )
2011 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2012 conn->c_connid = conn_nextid++;
2013 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );