2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
88 typedef struct conn_readinfo {
90 ldap_pvt_thread_start_t *func;
95 static int connection_input( Connection *c, conn_readinfo *cri );
97 static int connection_input( Connection *c );
99 static void connection_close( Connection *c );
101 static int connection_op_activate( Operation *op );
102 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
103 static void connection_op_queue( Operation *op );
105 static int connection_resched( Connection *conn );
106 static void connection_abandon( Connection *conn );
107 static void connection_destroy( Connection *c );
109 static ldap_pvt_thread_start_t connection_operation;
112 * Initialize connection management infrastructure.
114 int connections_init(void)
118 assert( connections == NULL );
120 if( connections != NULL) {
121 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
126 /* should check return of every call */
127 ldap_pvt_thread_mutex_init( &connections_mutex );
128 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
130 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
132 if( connections == NULL ) {
133 Debug( LDAP_DEBUG_ANY, "connections_init: "
134 "allocation (%d*%ld) of connection array failed\n",
135 dtblsize, (long) sizeof(Connection), 0 );
139 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
140 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
142 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
145 * per entry initialization of the Connection array initialization
146 * will be done by connection_init()
153 * Destroy connection management infrastructure.
156 int connections_destroy(void)
160 /* should check return of every call */
162 if( connections == NULL) {
163 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
168 for ( i = 0; i < dtblsize; i++ ) {
169 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
170 ber_sockbuf_free( connections[i].c_sb );
171 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
172 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
173 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
175 if ( slapi_plugins_used ) {
176 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
186 ldap_pvt_thread_mutex_destroy( &connections_mutex );
187 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
192 * shutdown all connections
194 int connections_shutdown(void)
198 for ( i = 0; i < dtblsize; i++ ) {
199 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
200 if( connections[i].c_struct_state == SLAP_C_USED ) {
202 /* give persistent clients a chance to cleanup */
203 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
204 ldap_pvt_thread_pool_submit( &connection_pool,
205 connections[i].c_clientfunc, connections[i].c_clientarg );
207 /* c_mutex is locked */
208 connection_closing( &connections[i], "slapd shutdown" );
209 connection_close( &connections[i] );
212 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
219 * Timeout idle connections.
221 int connections_timeout_idle(time_t now)
227 for( c = connection_first( &connindex );
229 c = connection_next( c, &connindex ) )
231 /* Don't timeout a slow-running request or a persistent
232 * outbound connection */
233 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
237 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
239 connection_closing( c, "idletimeout" );
240 connection_close( c );
244 connection_done( c );
249 static Connection* connection_get( ber_socket_t s )
253 Debug( LDAP_DEBUG_ARGS,
254 "connection_get(%ld)\n",
257 assert( connections != NULL );
259 if(s == AC_SOCKET_INVALID) return NULL;
262 assert( s < dtblsize );
270 ldap_pvt_thread_mutex_lock( &connections_mutex );
271 for(i=0; i<dtblsize; i++) {
272 if( connections[i].c_struct_state == SLAP_C_PENDING )
275 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
276 assert( connections[i].c_conn_state == SLAP_C_INVALID );
277 assert( connections[i].c_sb == 0 );
281 ber_sockbuf_ctrl( connections[i].c_sb,
282 LBER_SB_OPT_GET_FD, &sd );
284 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
285 assert( connections[i].c_conn_state == SLAP_C_INVALID );
286 assert( sd == AC_SOCKET_INVALID );
290 /* state can actually change from used -> unused by resched,
291 * so don't assert details here.
299 ldap_pvt_thread_mutex_unlock( &connections_mutex );
306 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
308 ldap_pvt_thread_mutex_lock( &c->c_mutex );
310 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
311 if( c->c_struct_state != SLAP_C_USED ) {
312 /* connection must have been closed due to resched */
314 assert( c->c_conn_state == SLAP_C_INVALID );
315 assert( sd == AC_SOCKET_INVALID );
317 Debug( LDAP_DEBUG_TRACE,
318 "connection_get(%d): connection not used\n",
321 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
325 Debug( LDAP_DEBUG_TRACE,
326 "connection_get(%d): got connid=%lu\n",
331 assert( c->c_struct_state == SLAP_C_USED );
332 assert( c->c_conn_state != SLAP_C_INVALID );
333 assert( sd != AC_SOCKET_INVALID );
335 #ifndef SLAPD_MONITOR
336 if ( global_idletimeout > 0 )
337 #endif /* ! SLAPD_MONITOR */
339 c->c_activitytime = slap_get_time();
346 static void connection_return( Connection *c )
348 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
351 long connection_init(
355 const char* peername,
358 struct berval *authid )
364 assert( connections != NULL );
366 assert( listener != NULL );
367 assert( dnsname != NULL );
368 assert( peername != NULL );
371 assert( flags != CONN_IS_TLS );
374 if( s == AC_SOCKET_INVALID ) {
375 Debug( LDAP_DEBUG_ANY,
376 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
382 assert( s < dtblsize );
384 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
387 assert( c->c_struct_state == SLAP_C_UNUSED );
394 ldap_pvt_thread_mutex_lock( &connections_mutex );
395 for( i=0; i < dtblsize; i++) {
398 if ( connections[i].c_struct_state == SLAP_C_PENDING )
401 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
402 assert( connections[i].c_sb == 0 );
404 c->c_struct_state = SLAP_C_PENDING;
409 sd = AC_SOCKET_INVALID;
410 if (connections[i].c_sb != NULL) {
411 ber_sockbuf_ctrl( connections[i].c_sb,
412 LBER_SB_OPT_GET_FD, &sd );
415 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
416 assert( sd == AC_SOCKET_INVALID );
418 c->c_struct_state = SLAP_C_PENDING;
422 if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
424 assert( connections[i].c_struct_state == SLAP_C_USED );
425 assert( connections[i].c_conn_state != SLAP_C_INVALID );
426 assert( sd != AC_SOCKET_INVALID );
428 ldap_pvt_thread_mutex_unlock( &connections_mutex );
431 Debug( LDAP_DEBUG_ANY,
432 "connection_init(%d): connection table full "
433 "(%d/%d)\n", s, i, dtblsize);
440 c->c_send_ldap_result = slap_send_ldap_result;
441 c->c_send_search_entry = slap_send_search_entry;
442 c->c_send_search_reference = slap_send_search_reference;
443 c->c_send_ldap_extended = slap_send_ldap_extended;
444 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
446 BER_BVZERO( &c->c_authmech );
447 BER_BVZERO( &c->c_dn );
448 BER_BVZERO( &c->c_ndn );
450 c->c_listener = NULL;
451 BER_BVZERO( &c->c_peer_domain );
452 BER_BVZERO( &c->c_peer_name );
454 LDAP_STAILQ_INIT(&c->c_ops);
455 LDAP_STAILQ_INIT(&c->c_pending_ops);
458 c->c_txn = CONN_TXN_INACTIVE;
459 c->c_txn_backend = NULL;
460 LDAP_STAILQ_INIT(&c->c_txn_ops);
463 BER_BVZERO( &c->c_sasl_bind_mech );
465 c->c_sasl_authctx = NULL;
466 c->c_sasl_sockctx = NULL;
467 c->c_sasl_extra = NULL;
468 c->c_sasl_bindop = NULL;
470 c->c_sb = ber_sockbuf_alloc( );
473 ber_len_t max = sockbuf_max_incoming;
474 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
477 c->c_currentber = NULL;
479 /* should check status of thread calls */
480 ldap_pvt_thread_mutex_init( &c->c_mutex );
481 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
482 ldap_pvt_thread_cond_init( &c->c_write_cv );
485 if ( slapi_plugins_used ) {
486 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
491 ldap_pvt_thread_mutex_lock( &c->c_mutex );
493 assert( BER_BVISNULL( &c->c_authmech ) );
494 assert( BER_BVISNULL( &c->c_dn ) );
495 assert( BER_BVISNULL( &c->c_ndn ) );
496 assert( c->c_listener == NULL );
497 assert( BER_BVISNULL( &c->c_peer_domain ) );
498 assert( BER_BVISNULL( &c->c_peer_name ) );
499 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
500 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
502 assert( c->c_txn == CONN_TXN_INACTIVE );
503 assert( c->c_txn_backend == NULL );
504 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
506 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
507 assert( c->c_sasl_done == 0 );
508 assert( c->c_sasl_authctx == NULL );
509 assert( c->c_sasl_sockctx == NULL );
510 assert( c->c_sasl_extra == NULL );
511 assert( c->c_sasl_bindop == NULL );
512 assert( c->c_currentber == NULL );
513 assert( c->c_writewaiter == 0);
515 c->c_listener = listener;
517 if ( flags == CONN_IS_CLIENT ) {
518 c->c_conn_state = SLAP_C_CLIENT;
519 c->c_struct_state = SLAP_C_USED;
520 c->c_close_reason = "?"; /* should never be needed */
521 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
522 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
527 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
528 ber_str2bv( peername, 0, 1, &c->c_peer_name );
530 c->c_n_ops_received = 0;
531 c->c_n_ops_executing = 0;
532 c->c_n_ops_pending = 0;
533 c->c_n_ops_completed = 0;
539 /* set to zero until bind, implies LDAP_VERSION3 */
542 #ifndef SLAPD_MONITOR
543 if ( global_idletimeout > 0 )
544 #endif /* ! SLAPD_MONITOR */
546 c->c_activitytime = c->c_starttime = slap_get_time();
549 #ifdef LDAP_CONNECTIONLESS
551 if( flags == CONN_IS_UDP ) {
554 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
555 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
557 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
558 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
559 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
560 LBER_SBIOD_LEVEL_PROVIDER, NULL );
565 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
566 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
568 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
569 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
573 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
574 INT_MAX, (void*)"ldap_" );
577 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
578 c /* non-NULL */ ) < 0 )
580 Debug( LDAP_DEBUG_ANY,
581 "connection_init(%d, %s): set nonblocking failed\n",
582 s, c->c_peer_name.bv_val, 0 );
585 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
586 id = c->c_connid = conn_nextid++;
587 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
589 c->c_conn_state = SLAP_C_INACTIVE;
590 c->c_struct_state = SLAP_C_USED;
591 c->c_close_reason = "?"; /* should never be needed */
593 c->c_ssf = c->c_transport_ssf = ssf;
597 if ( flags == CONN_IS_TLS ) {
599 c->c_needs_tls_accept = 1;
602 c->c_needs_tls_accept = 0;
606 slap_sasl_open( c, 0 );
607 slap_sasl_external( c, ssf, authid );
609 slapd_add_internal( s, 1 );
610 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
612 backend_connection_init(c);
617 void connection2anonymous( Connection *c )
619 assert( connections != NULL );
623 ber_len_t max = sockbuf_max_incoming;
624 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
627 if ( !BER_BVISNULL( &c->c_authmech ) ) {
628 ch_free(c->c_authmech.bv_val);
630 BER_BVZERO( &c->c_authmech );
632 if ( !BER_BVISNULL( &c->c_dn ) ) {
633 ch_free(c->c_dn.bv_val);
635 BER_BVZERO( &c->c_dn );
637 if ( !BER_BVISNULL( &c->c_ndn ) ) {
638 ch_free(c->c_ndn.bv_val);
640 BER_BVZERO( &c->c_ndn );
642 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
643 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
645 BER_BVZERO( &c->c_sasl_authz_dn );
647 c->c_authz_backend = NULL;
651 connection_destroy( Connection *c )
654 unsigned long connid;
655 const char *close_reason;
658 assert( connections != NULL );
660 assert( c->c_struct_state != SLAP_C_UNUSED );
661 assert( c->c_conn_state != SLAP_C_INVALID );
662 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
663 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
665 assert( c->c_txn == CONN_TXN_INACTIVE );
666 assert( c->c_txn_backend == NULL );
667 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
669 assert( c->c_writewaiter == 0);
671 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
672 connid = c->c_connid;
673 close_reason = c->c_close_reason;
675 ldap_pvt_thread_mutex_lock( &connections_mutex );
676 c->c_struct_state = SLAP_C_PENDING;
677 ldap_pvt_thread_mutex_unlock( &connections_mutex );
679 backend_connection_destroy(c);
684 c->c_activitytime = c->c_starttime = 0;
686 connection2anonymous( c );
687 c->c_listener = NULL;
689 if(c->c_peer_domain.bv_val != NULL) {
690 free(c->c_peer_domain.bv_val);
692 BER_BVZERO( &c->c_peer_domain );
693 if(c->c_peer_name.bv_val != NULL) {
694 free(c->c_peer_name.bv_val);
696 BER_BVZERO( &c->c_peer_name );
698 c->c_sasl_bind_in_progress = 0;
699 if(c->c_sasl_bind_mech.bv_val != NULL) {
700 free(c->c_sasl_bind_mech.bv_val);
702 BER_BVZERO( &c->c_sasl_bind_mech );
704 slap_sasl_close( c );
706 if ( c->c_currentber != NULL ) {
707 ber_free( c->c_currentber, 1 );
708 c->c_currentber = NULL;
713 /* call destructors, then constructors; avoids unnecessary allocation */
714 if ( slapi_plugins_used ) {
715 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
719 c->c_conn_state = SLAP_C_INVALID;
720 c->c_struct_state = SLAP_C_UNUSED;
721 c->c_close_reason = "?"; /* should never be needed */
724 c->c_sb = ber_sockbuf_alloc( );
726 ber_len_t max = sockbuf_max_incoming;
727 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
730 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
732 /* c must be fully reset by this point; when we call slapd_remove
733 * it may get immediately reused by a new connection.
735 if ( sd != AC_SOCKET_INVALID ) {
736 slapd_remove( sd, sb, 1, 0, 0 );
738 if ( close_reason == NULL ) {
742 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
743 connid, (long) sd, close_reason, 0, 0 );
747 int connection_state_closing( Connection *c )
749 /* c_mutex must be locked by caller */
753 assert( c->c_struct_state == SLAP_C_USED );
755 state = c->c_conn_state;
757 assert( state != SLAP_C_INVALID );
759 return state == SLAP_C_CLOSING;
762 static void connection_abandon( Connection *c )
764 /* c_mutex must be locked by caller */
766 Operation *o, *next, op = {0};
772 op.o_connid = c->c_connid;
773 op.o_tag = LDAP_REQ_ABANDON;
775 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
776 next = LDAP_STAILQ_NEXT( o, o_next );
777 op.orn_msgid = o->o_msgid;
779 op.o_bd = frontendDB;
780 frontendDB->be_abandon( &op, &rs );
784 /* remove operations in pending transaction */
785 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
786 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
787 LDAP_STAILQ_NEXT(o, o_next) = NULL;
791 /* clear transaction */
792 c->c_txn_backend = NULL;
793 c->c_txn = CONN_TXN_INACTIVE;
796 /* remove pending operations */
797 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
798 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
799 LDAP_STAILQ_NEXT(o, o_next) = NULL;
804 void connection_closing( Connection *c, const char *why )
806 assert( connections != NULL );
808 assert( c->c_struct_state == SLAP_C_USED );
809 assert( c->c_conn_state != SLAP_C_INVALID );
811 /* c_mutex must be locked by caller */
813 if( c->c_conn_state != SLAP_C_CLOSING ) {
816 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
817 Debug( LDAP_DEBUG_TRACE,
818 "connection_closing: readying conn=%lu sd=%d for close\n",
819 c->c_connid, sd, 0 );
820 /* update state to closing */
821 c->c_conn_state = SLAP_C_CLOSING;
822 c->c_close_reason = why;
824 /* don't listen on this port anymore */
825 slapd_clr_read( sd, 0 );
827 /* abandon active operations */
828 connection_abandon( c );
830 /* wake write blocked operations */
831 if ( c->c_writewaiter ) {
832 ldap_pvt_thread_cond_signal( &c->c_write_cv );
833 /* ITS#4667 this may allow another thread to drop into
834 * connection_resched / connection_close before we
835 * finish, but that's OK.
837 slapd_clr_write( sd, 1 );
838 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
839 ldap_pvt_thread_mutex_lock( &c->c_write_mutex );
840 ldap_pvt_thread_mutex_lock( &c->c_mutex );
841 ldap_pvt_thread_mutex_unlock( &c->c_write_mutex );
843 slapd_clr_write( sd, 1 );
846 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
847 /* Client closed connection after doing Unbind. */
848 c->c_close_reason = NULL;
853 connection_close( Connection *c )
855 ber_socket_t sd = AC_SOCKET_INVALID;
857 assert( connections != NULL );
860 /* ITS#4667 we may have gotten here twice */
861 if ( c->c_conn_state == SLAP_C_INVALID )
864 assert( c->c_struct_state == SLAP_C_USED );
865 assert( c->c_conn_state == SLAP_C_CLOSING );
867 /* NOTE: c_mutex should be locked by caller */
869 /* NOTE: don't get the file descriptor if not needed */
870 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
871 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
874 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
875 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
877 Debug( LDAP_DEBUG_TRACE,
878 "connection_close: deferring conn=%lu sd=%d\n",
879 c->c_connid, sd, 0 );
883 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
884 c->c_connid, sd, 0 );
886 connection_destroy( c );
889 unsigned long connections_nextid(void)
892 assert( connections != NULL );
894 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
898 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
903 Connection* connection_first( ber_socket_t *index )
905 assert( connections != NULL );
906 assert( index != NULL );
908 ldap_pvt_thread_mutex_lock( &connections_mutex );
909 for( *index = 0; *index < dtblsize; (*index)++) {
910 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
914 ldap_pvt_thread_mutex_unlock( &connections_mutex );
916 return connection_next(NULL, index);
919 Connection* connection_next( Connection *c, ber_socket_t *index )
921 assert( connections != NULL );
922 assert( index != NULL );
923 assert( *index <= dtblsize );
925 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
929 ldap_pvt_thread_mutex_lock( &connections_mutex );
930 for(; *index < dtblsize; (*index)++) {
932 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
933 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
941 if( connections[*index].c_struct_state == SLAP_C_USED ) {
942 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
943 c = &connections[(*index)++];
944 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
946 ldap_pvt_thread_mutex_unlock( &connections_mutex );
947 ldap_pvt_thread_mutex_lock( &c->c_mutex );
948 ldap_pvt_thread_mutex_lock( &connections_mutex );
949 if ( c->c_struct_state != SLAP_C_USED ) {
950 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
958 c_struct = connections[*index].c_struct_state;
959 if ( c_struct == SLAP_C_PENDING )
961 assert( c_struct == SLAP_C_UNUSED );
962 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
965 ldap_pvt_thread_mutex_unlock( &connections_mutex );
969 void connection_done( Connection *c )
971 assert( connections != NULL );
973 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
977 * connection_activity - handle the request operation op on connection
978 * conn. This routine figures out what kind of operation it is and
979 * calls the appropriate stub to handle it.
983 /* FIXME: returns 0 in case of failure */
984 #define INCR_OP_INITIATED(index) \
986 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
987 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
988 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
990 #define INCR_OP_COMPLETED(index) \
992 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
993 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
994 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
995 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
997 #else /* !SLAPD_MONITOR */
998 #define INCR_OP_INITIATED(index) do { } while (0)
999 #define INCR_OP_COMPLETED(index) \
1001 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1002 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1003 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1005 #endif /* !SLAPD_MONITOR */
1008 * NOTE: keep in sync with enum in slapd.h
1010 static BI_op_func *opfun[] = {
1025 connection_operation( void *ctx, void *arg_v )
1027 int rc = LDAP_OTHER;
1028 Operation *op = arg_v;
1029 SlapReply rs = {REP_RESULT};
1030 ber_tag_t tag = op->o_tag;
1031 slap_op_t opidx = SLAP_OP_LAST;
1032 Connection *conn = op->o_conn;
1033 void *memctx = NULL;
1034 void *memctx_null = NULL;
1037 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1038 /* FIXME: returns 0 in case of failure */
1039 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1040 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1042 op->o_threadctx = ctx;
1043 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1047 case LDAP_REQ_UNBIND:
1049 case LDAP_REQ_DELETE:
1050 case LDAP_REQ_MODDN:
1051 case LDAP_REQ_MODIFY:
1052 case LDAP_REQ_COMPARE:
1053 case LDAP_REQ_SEARCH:
1054 case LDAP_REQ_ABANDON:
1055 case LDAP_REQ_EXTENDED:
1058 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1059 "conn %lu unknown LDAP request 0x%lx\n",
1060 conn->c_connid, tag, 0 );
1061 op->o_tag = LBER_ERROR;
1062 rs.sr_err = LDAP_PROTOCOL_ERROR;
1063 rs.sr_text = "unknown LDAP request";
1064 send_ldap_disconnect( op, &rs );
1065 rc = SLAPD_DISCONNECT;
1066 goto operations_error;
1069 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1070 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1071 "error: SASL bind in progress (tag=%ld).\n",
1073 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1074 "SASL bind in progress" );
1075 rc = LDAP_OPERATIONS_ERROR;
1076 goto operations_error;
1080 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1081 ( tag == LDAP_REQ_ADD ) ||
1082 ( tag == LDAP_REQ_DELETE ) ||
1083 ( tag == LDAP_REQ_MODIFY ) ||
1084 ( tag == LDAP_REQ_MODRDN )))
1086 /* Disable SLAB allocator for all update operations
1087 issued inside of a transaction */
1088 op->o_tmpmemctx = NULL;
1089 op->o_tmpmfuncs = &ch_mfuncs;
1093 /* We can use Thread-Local storage for most mallocs. We can
1094 * also use TL for ber parsing, but not on Add or Modify.
1097 memsiz = ber_len( op->o_ber ) * 64;
1098 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1100 memsiz = SLAP_SLAB_SIZE;
1102 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1103 op->o_tmpmemctx = memctx;
1104 op->o_tmpmfuncs = &slap_sl_mfuncs;
1105 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1106 /* Note - the ber and its buffer are already allocated from
1107 * regular memory; this only affects subsequent mallocs that
1108 * ber_scanf may invoke.
1110 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1114 opidx = slap_req2op( tag );
1115 assert( opidx != SLAP_OP_LAST );
1116 INCR_OP_INITIATED( opidx );
1117 rc = (*(opfun[opidx]))( op, &rs );
1120 if ( rc == SLAPD_DISCONNECT ) {
1123 } else if ( opidx != SLAP_OP_LAST ) {
1124 /* increment completed operations count
1125 * only if operation was initiated
1126 * and rc != SLAPD_DISCONNECT */
1127 INCR_OP_COMPLETED( opidx );
1130 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1131 if ( rc == SLAPD_ABANDON ) {
1132 op->o_cancel = SLAP_CANCEL_ACK;
1134 op->o_cancel = LDAP_TOO_LATE;
1138 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1139 op->o_cancel != SLAP_CANCEL_DONE )
1141 ldap_pvt_thread_yield();
1144 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1146 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1148 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1149 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1151 conn->c_n_ops_executing--;
1152 conn->c_n_ops_completed++;
1156 case LDAP_REQ_UNBIND:
1157 /* c_mutex is locked */
1158 connection_closing( conn,
1159 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1163 connection_resched( conn );
1164 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1168 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1170 int connection_client_setup(
1172 ldap_pvt_thread_start_t *func,
1178 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1179 CONN_IS_CLIENT, 0, NULL );
1180 if ( rc < 0 ) return -1;
1182 c = connection_get( s );
1183 c->c_clientfunc = func;
1184 c->c_clientarg = arg;
1186 slapd_add_internal( s, 0 );
1187 slapd_set_read( s, 1 );
1188 connection_return( c );
1192 void connection_client_enable(
1195 slapd_set_read( s, 1 );
1198 void connection_client_stop(
1204 /* get (locked) connection */
1205 c = connection_get( s );
1207 assert( c->c_conn_state == SLAP_C_CLIENT );
1209 c->c_listener = NULL;
1210 c->c_conn_state = SLAP_C_INVALID;
1211 c->c_struct_state = SLAP_C_UNUSED;
1212 c->c_close_reason = "?"; /* should never be needed */
1214 c->c_sb = ber_sockbuf_alloc( );
1216 ber_len_t max = sockbuf_max_incoming;
1217 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1219 slapd_remove( s, sb, 0, 1, 0 );
1221 connection_return( c );
1224 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1226 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1228 static void* connection_read_thread( void* ctx, void* argv )
1231 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1232 ber_socket_t s = (long)argv;
1235 * read incoming LDAP requests. If there is more than one,
1236 * the first one is returned with new_op
1238 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1239 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1240 return (void*)(long)rc;
1243 /* execute a single queued request in the same thread */
1244 if( cri.op && !cri.nullop ) {
1245 rc = (long)connection_operation( ctx, cri.op );
1246 } else if ( cri.func ) {
1247 rc = (long)cri.func( ctx, cri.arg );
1250 return (void*)(long)rc;
1253 int connection_read_activate( ber_socket_t s )
1258 * suspend reading on this file descriptor until a connection processing
1259 * thread reads data on it. Otherwise the listener thread will repeatedly
1260 * submit the same event on it to the pool.
1262 rc = slapd_clr_read( s, 0 );
1266 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1267 connection_read_thread, (void *)(long)s );
1270 Debug( LDAP_DEBUG_ANY,
1271 "connection_read_activate(%d): submit failed (%d)\n",
1279 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1281 connection_read( ber_socket_t s, conn_readinfo *cri )
1283 int connection_read(ber_socket_t s)
1289 assert( connections != NULL );
1291 /* get (locked) connection */
1292 c = connection_get( s );
1295 Debug( LDAP_DEBUG_ANY,
1296 "connection_read(%ld): no connection!\n",
1304 if( c->c_conn_state == SLAP_C_CLOSING ) {
1305 Debug( LDAP_DEBUG_TRACE,
1306 "connection_read(%d): closing, ignoring input for id=%lu\n",
1307 s, c->c_connid, 0 );
1308 connection_return( c );
1312 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1313 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1314 cri->func = c->c_clientfunc;
1315 cri->arg = c->c_clientarg;
1316 /* read should already be cleared */
1318 slapd_clr_read( s, 0 );
1319 ldap_pvt_thread_pool_submit( &connection_pool,
1320 c->c_clientfunc, c->c_clientarg );
1322 connection_return( c );
1326 Debug( LDAP_DEBUG_TRACE,
1327 "connection_read(%d): checking for input on id=%lu\n",
1328 s, c->c_connid, 0 );
1331 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1332 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1334 Debug( LDAP_DEBUG_TRACE,
1335 "connection_read(%d): TLS accept failure "
1336 "error=%d id=%lu, closing\n",
1337 s, rc, c->c_connid );
1339 c->c_needs_tls_accept = 0;
1340 /* c_mutex is locked */
1341 connection_closing( c, "TLS negotiation failure" );
1342 connection_close( c );
1343 connection_return( c );
1346 } else if ( rc == 0 ) {
1348 struct berval authid = BER_BVNULL;
1350 c->c_needs_tls_accept = 0;
1352 /* we need to let SASL know */
1353 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1355 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1356 if( c->c_tls_ssf > c->c_ssf ) {
1357 c->c_ssf = c->c_tls_ssf;
1360 rc = dnX509peerNormalize( ssl, &authid );
1361 if ( rc != LDAP_SUCCESS ) {
1362 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1363 "unable to get TLS client DN, error=%d id=%lu\n",
1364 s, rc, c->c_connid );
1366 Statslog( LDAP_DEBUG_STATS,
1367 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1368 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1369 slap_sasl_external( c, c->c_tls_ssf, &authid );
1370 if ( authid.bv_val ) free( authid.bv_val );
1373 /* if success and data is ready, fall thru to data input loop */
1374 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1376 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1377 slapd_set_read( s, 1 );
1380 connection_return( c );
1386 #ifdef HAVE_CYRUS_SASL
1387 if ( c->c_sasl_layers ) {
1388 /* If previous layer is not removed yet, give up for now */
1389 if ( !c->c_sasl_sockctx ) {
1390 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1391 slapd_set_read( s, 1 );
1394 connection_return( c );
1398 c->c_sasl_layers = 0;
1400 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1401 if( rc != LDAP_SUCCESS ) {
1402 Debug( LDAP_DEBUG_TRACE,
1403 "connection_read(%d): SASL install error "
1404 "error=%d id=%lu, closing\n",
1405 s, rc, c->c_connid );
1407 /* c_mutex is locked */
1408 connection_closing( c, "SASL layer install failure" );
1409 connection_close( c );
1410 connection_return( c );
1416 #define CONNECTION_INPUT_LOOP 1
1417 /* #define DATA_READY_LOOP 1 */
1420 /* How do we do this without getting into a busy loop ? */
1421 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1422 rc = connection_input( c, cri );
1424 rc = connection_input( c );
1427 #ifdef DATA_READY_LOOP
1428 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1429 #elif CONNECTION_INPUT_LOOP
1436 Debug( LDAP_DEBUG_CONNS,
1437 "connection_read(%d): input error=%d id=%lu, closing.\n",
1438 s, rc, c->c_connid );
1440 /* c_mutex is locked */
1441 connection_closing( c, conn_lost_str );
1442 connection_close( c );
1443 connection_return( c );
1447 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1448 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1449 slapd_set_write( s, 0 );
1452 slapd_set_read( s, 1 );
1454 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1455 slapd_set_read( s, 1 );
1458 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1459 slapd_set_write( s, 1 );
1463 connection_return( c );
1469 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1470 connection_input( Connection *conn , conn_readinfo *cri )
1472 connection_input( Connection *conn )
1481 #ifdef LDAP_CONNECTIONLESS
1487 if ( conn->c_currentber == NULL &&
1488 ( conn->c_currentber = ber_alloc()) == NULL )
1490 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1496 #ifdef LDAP_CONNECTIONLESS
1497 if ( conn->c_is_udp ) {
1498 char peername[sizeof("IP=255.255.255.255:65336")];
1500 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1501 if (len != sizeof(struct sockaddr)) return 1;
1503 sprintf( peername, "IP=%s:%d",
1504 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1505 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1506 Statslog( LDAP_DEBUG_STATS,
1507 "conn=%lu UDP request from %s (%s) accepted.\n",
1508 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1512 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1513 if ( tag != LDAP_TAG_MESSAGE ) {
1514 int err = sock_errno();
1517 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1519 Debug( LDAP_DEBUG_TRACE,
1520 "ber_get_next on fd %d failed errno=%d (%s)\n",
1521 sd, err, sock_errstr(err) );
1522 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1523 /* log, close and send error */
1524 ber_free( conn->c_currentber, 1 );
1525 conn->c_currentber = NULL;
1532 ber = conn->c_currentber;
1533 conn->c_currentber = NULL;
1535 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1536 /* log, close and send error */
1537 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1542 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1543 /* log, close and send error */
1544 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1550 #ifdef LDAP_CONNECTIONLESS
1551 if( conn->c_is_udp ) {
1552 if( tag == LBER_OCTETSTRING ) {
1553 ber_get_stringa( ber, &cdn );
1554 tag = ber_peek_tag(ber, &len);
1556 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1557 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1564 if(tag == LDAP_REQ_BIND) {
1565 /* immediately abandon all existing operations upon BIND */
1566 connection_abandon( conn );
1569 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1572 /* clear state if the connection is being reused from inactive */
1573 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1574 memset( &conn->c_pagedresults_state, 0,
1575 sizeof( conn->c_pagedresults_state ) );
1578 op->o_res_ber = NULL;
1580 #ifdef LDAP_CONNECTIONLESS
1581 if (conn->c_is_udp) {
1583 ber_str2bv( cdn, 0, 1, &op->o_dn );
1584 op->o_protocol = LDAP_VERSION2;
1586 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1587 if (op->o_res_ber == NULL) return 1;
1589 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1590 sizeof(struct sockaddr), 0 );
1592 if (rc != sizeof(struct sockaddr)) {
1593 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1597 if (op->o_protocol == LDAP_VERSION2) {
1598 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1600 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1605 #endif /* LDAP_CONNECTIONLESS */
1609 /* Don't process requests when the conn is in the middle of a
1610 * Bind, or if it's closing. Also, don't let any single conn
1611 * use up all the available threads, and don't execute if we're
1612 * currently blocked on output. And don't execute if there are
1613 * already pending ops, let them go first. Abandon operations
1614 * get exceptions to some, but not all, cases.
1618 /* Abandon and Unbind are exempt from these checks */
1619 if (conn->c_conn_state == SLAP_C_CLOSING) {
1622 } else if (conn->c_writewaiter) {
1623 defer = "awaiting write";
1625 } else if (conn->c_n_ops_pending) {
1626 defer = "pending operations";
1630 case LDAP_REQ_ABANDON:
1631 /* Unbind is exempt from these checks */
1632 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1633 defer = "too many executing";
1635 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1640 case LDAP_REQ_UNBIND:
1645 int max = conn->c_dn.bv_len
1646 ? slap_conn_max_pending_auth
1647 : slap_conn_max_pending;
1649 Debug( LDAP_DEBUG_ANY,
1650 "connection_input: conn=%lu deferring operation: %s\n",
1651 conn->c_connid, defer, 0 );
1652 conn->c_n_ops_pending++;
1653 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1654 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1657 conn->c_n_ops_executing++;
1659 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1661 * The first op will be processed in the same thread context,
1662 * as long as there is only one op total.
1663 * Subsequent ops will be submitted to the pool by
1664 * calling connection_op_activate()
1666 if ( cri->op == NULL ) {
1667 /* the first incoming request */
1668 connection_op_queue( op );
1671 if ( !cri->nullop ) {
1673 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1674 connection_operation, (void *) cri->op );
1676 connection_op_activate( op );
1679 connection_op_activate( op );
1684 if ( conn->c_struct_state != SLAP_C_USED ) {
1685 /* connection must have got closed underneath us */
1690 assert( conn->c_struct_state == SLAP_C_USED );
1695 connection_resched( Connection *conn )
1699 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1701 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1703 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1704 "attempting closing conn=%lu sd=%d\n",
1705 conn->c_connid, sd, 0 );
1706 connection_close( conn );
1710 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1711 /* other states need different handling */
1715 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1716 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1718 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1719 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1721 /* pending operations should not be marked for abandonment */
1722 assert(!op->o_abandon);
1724 conn->c_n_ops_pending--;
1725 conn->c_n_ops_executing++;
1727 connection_op_activate( op );
1729 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1735 connection_init_log_prefix( Operation *op )
1737 if ( op->o_connid == (unsigned long)(-1) ) {
1738 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1739 "conn=-1 op=%lu", op->o_opid );
1742 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1743 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1747 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1749 op->o_conn->c_sasl_bindop = NULL;
1751 ch_free( op->o_callback );
1752 op->o_callback = NULL;
1754 return SLAP_CB_CONTINUE;
1757 static int connection_bind_cb( Operation *op, SlapReply *rs )
1759 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1760 if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1761 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1762 op->o_conn->c_sasl_bind_in_progress =
1763 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1765 /* Moved here from bind.c due to ITS#4158 */
1766 op->o_conn->c_sasl_bindop = NULL;
1767 if ( op->orb_method == LDAP_AUTH_SASL ) {
1768 if( rs->sr_err == LDAP_SUCCESS ) {
1769 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1770 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1771 /* edn is always normalized already */
1772 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1774 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1775 BER_BVZERO( &op->orb_edn );
1776 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1777 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1779 op->o_conn->c_sasl_ssf = op->orb_ssf;
1780 if( op->orb_ssf > op->o_conn->c_ssf ) {
1781 op->o_conn->c_ssf = op->orb_ssf;
1784 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1785 ber_len_t max = sockbuf_max_incoming_auth;
1786 ber_sockbuf_ctrl( op->o_conn->c_sb,
1787 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1790 /* log authorization identity */
1791 Statslog( LDAP_DEBUG_STATS,
1792 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
1794 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1795 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
1797 Debug( LDAP_DEBUG_TRACE,
1798 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
1799 op->o_conn->c_authmech.bv_val,
1800 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1803 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1804 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1805 free( op->o_conn->c_sasl_bind_mech.bv_val );
1806 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1810 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1812 ch_free( op->o_callback );
1813 op->o_callback = NULL;
1815 return SLAP_CB_CONTINUE;
1818 static void connection_op_queue( Operation *op )
1820 ber_tag_t tag = op->o_tag;
1822 if (tag == LDAP_REQ_BIND) {
1823 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1824 sc->sc_response = connection_bind_cb;
1825 sc->sc_cleanup = connection_bind_cleanup_cb;
1826 sc->sc_next = op->o_callback;
1827 op->o_callback = sc;
1828 op->o_conn->c_conn_state = SLAP_C_BINDING;
1831 if (!op->o_dn.bv_len) {
1832 op->o_authz = op->o_conn->c_authz;
1833 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1834 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1835 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1837 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1838 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1842 op->o_authtype = op->o_conn->c_authtype;
1843 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1845 if (!op->o_protocol) {
1846 op->o_protocol = op->o_conn->c_protocol
1847 ? op->o_conn->c_protocol : LDAP_VERSION3;
1850 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1851 op->o_protocol > LDAP_VERSION2)
1853 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1856 op->o_connid = op->o_conn->c_connid;
1857 connection_init_log_prefix( op );
1859 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1862 static int connection_op_activate( Operation *op )
1866 connection_op_queue( op );
1868 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1869 connection_operation, (void *) op );
1872 Debug( LDAP_DEBUG_ANY,
1873 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1874 rc, op->o_connid, 0 );
1875 /* should move op to pending list */
1881 int connection_write(ber_socket_t s)
1886 assert( connections != NULL );
1888 slapd_clr_write( s, 0 );
1890 c = connection_get( s );
1892 Debug( LDAP_DEBUG_ANY,
1893 "connection_write(%ld): no connection!\n",
1900 Debug( LDAP_DEBUG_TRACE,
1901 "connection_write(%d): waking output for id=%lu\n",
1902 s, c->c_connid, 0 );
1903 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1905 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1906 slapd_set_read( s, 1 );
1908 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1909 slapd_set_write( s, 1 );
1912 /* If there are ops pending because of a writewaiter,
1915 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1916 if ( !c->c_writewaiter ) break;
1917 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1919 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1920 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1922 /* pending operations should not be marked for abandonment */
1923 assert(!op->o_abandon);
1925 c->c_n_ops_pending--;
1926 c->c_n_ops_executing++;
1928 connection_op_activate( op );
1933 connection_return( c );
1938 connection_fake_init(
1943 conn->c_connid = -1;
1944 conn->c_send_ldap_result = slap_send_ldap_result;
1945 conn->c_send_search_entry = slap_send_search_entry;
1946 conn->c_send_search_reference = slap_send_search_reference;
1947 conn->c_listener = (Listener *)&dummy_list;
1948 conn->c_peer_domain = slap_empty_bv;
1949 conn->c_peer_name = slap_empty_bv;
1951 memset(op, 0, OPERATION_BUFFER_SIZE);
1952 op->o_hdr = (Opheader *)(op+1);
1953 op->o_controls = (void **)(op->o_hdr+1);
1954 /* set memory context */
1955 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
1956 op->o_tmpmfuncs = &slap_sl_mfuncs;
1957 op->o_threadctx = ctx;
1958 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1961 op->o_connid = op->o_conn->c_connid;
1962 connection_init_log_prefix( op );
1965 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
1966 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
1967 #endif /* LDAP_SLAPI */
1969 slap_op_time( &op->o_time, &op->o_tincr );
1973 connection_assign_nextid( Connection *conn )
1975 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1976 conn->c_connid = conn_nextid++;
1977 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );