2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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.
31 #include <ac/socket.h>
33 #include <ac/string.h>
35 #include <ac/unistd.h>
42 #include "slapi/slapi.h"
45 /* protected by connections_mutex */
46 static ldap_pvt_thread_mutex_t connections_mutex;
47 static Connection *connections = NULL;
48 static unsigned long conn_nextid = 0;
50 /* structure state (protected by connections_mutex) */
51 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
52 #define SLAP_C_UNUSED 0x01
53 #define SLAP_C_USED 0x02
55 /* connection state (protected by c_mutex ) */
56 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
57 #define SLAP_C_INACTIVE 0x01 /* zero threads */
58 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
59 #define SLAP_C_BINDING 0x03 /* binding */
60 #define SLAP_C_CLOSING 0x04 /* closing */
61 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
64 connection_state2str( int state )
67 case SLAP_C_INVALID: return "!";
68 case SLAP_C_INACTIVE: return "|";
69 case SLAP_C_ACTIVE: return "";
70 case SLAP_C_BINDING: return "B";
71 case SLAP_C_CLOSING: return "C";
72 case SLAP_C_CLIENT: return "L";
78 static Connection* connection_get( ber_socket_t s );
80 static int connection_input( Connection *c );
81 static void connection_close( Connection *c );
83 static int connection_op_activate( Operation *op );
84 static int connection_resched( Connection *conn );
85 static void connection_abandon( Connection *conn );
86 static void connection_destroy( Connection *c );
88 static ldap_pvt_thread_start_t connection_operation;
91 * Initialize connection management infrastructure.
93 int connections_init(void)
97 assert( connections == NULL );
99 if( connections != NULL) {
101 LDAP_LOG( CONNECTION, INFO,
102 "connections_init: already initialized.\n", 0, 0, 0 );
104 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
110 /* should check return of every call */
111 ldap_pvt_thread_mutex_init( &connections_mutex );
113 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
115 if( connections == NULL ) {
117 LDAP_LOG( CONNECTION, ERR,
118 "connections_init: allocation (%d * %ld) of connection "
119 "array failed\n", dtblsize, (long) sizeof(Connection), 0 );
121 Debug( LDAP_DEBUG_ANY,
122 "connections_init: allocation (%d*%ld) of connection array failed\n",
123 dtblsize, (long) sizeof(Connection), 0 );
128 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
129 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
131 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
134 * per entry initialization of the Connection array initialization
135 * will be done by connection_init()
142 * Destroy connection management infrastructure.
144 int connections_destroy(void)
148 /* should check return of every call */
150 if( connections == NULL) {
152 LDAP_LOG( CONNECTION, INFO,
153 "connections_destroy: nothing to destroy.\n", 0, 0, 0 );
155 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
161 for ( i = 0; i < dtblsize; i++ ) {
162 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
163 ber_sockbuf_free( connections[i].c_sb );
164 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
165 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
166 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
168 if ( slapi_plugins_used ) {
169 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &connections[i] );
178 ldap_pvt_thread_mutex_destroy( &connections_mutex );
183 * shutdown all connections
185 int connections_shutdown(void)
189 ldap_pvt_thread_mutex_lock( &connections_mutex );
191 for ( i = 0; i < dtblsize; i++ ) {
192 if( connections[i].c_struct_state != SLAP_C_USED ) {
195 /* give persistent clients a chance to cleanup */
196 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
197 ldap_pvt_thread_pool_submit( &connection_pool,
198 connections[i].c_clientfunc, connections[i].c_clientarg );
202 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
204 /* connections_mutex and c_mutex are locked */
205 connection_closing( &connections[i] );
206 connection_close( &connections[i] );
208 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
211 ldap_pvt_thread_mutex_unlock( &connections_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 */
231 if( c->c_n_ops_executing ||
232 c->c_conn_state == SLAP_C_CLIENT ) continue;
234 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
236 connection_closing( c );
237 connection_close( c );
241 connection_done( c );
246 static Connection* connection_get( ber_socket_t s )
248 /* connections_mutex should be locked by caller */
253 LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
255 Debug( LDAP_DEBUG_ARGS,
256 "connection_get(%ld)\n",
260 assert( connections != NULL );
262 if(s == AC_SOCKET_INVALID) {
269 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
276 for(i=0; i<dtblsize; i++) {
277 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
278 assert( connections[i].c_conn_state == SLAP_C_INVALID );
279 assert( connections[i].c_sb == 0 );
283 ber_sockbuf_ctrl( connections[i].c_sb,
284 LBER_SB_OPT_GET_FD, &sd );
286 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
287 assert( connections[i].c_conn_state == SLAP_C_INVALID );
288 assert( sd == AC_SOCKET_INVALID );
292 /* state can actually change from used -> unused by resched,
293 * so don't assert details here.
307 ldap_pvt_thread_mutex_lock( &c->c_mutex );
309 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
310 if( c->c_struct_state != SLAP_C_USED ) {
311 /* connection must have been closed due to resched */
313 assert( c->c_conn_state == SLAP_C_INVALID );
314 assert( sd == AC_SOCKET_INVALID );
317 LDAP_LOG( CONNECTION, ARGS,
318 "connection_get: connection %d not used\n", s, 0, 0 );
320 Debug( LDAP_DEBUG_TRACE,
321 "connection_get(%d): connection not used\n",
325 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
330 LDAP_LOG( CONNECTION, RESULTS,
331 "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
333 Debug( LDAP_DEBUG_TRACE,
334 "connection_get(%d): got connid=%lu\n",
340 assert( c->c_struct_state == SLAP_C_USED );
341 assert( c->c_conn_state != SLAP_C_INVALID );
342 assert( sd != AC_SOCKET_INVALID );
345 c->c_activitytime = slap_get_time();
347 if( global_idletimeout > 0 ) {
348 c->c_activitytime = slap_get_time();
356 static void connection_return( Connection *c )
358 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
361 long connection_init(
365 const char* peername,
368 struct berval *authid )
373 assert( connections != NULL );
375 assert( listener != NULL );
376 assert( dnsname != NULL );
377 assert( peername != NULL );
380 assert( flags != CONN_IS_TLS );
383 if( s == AC_SOCKET_INVALID ) {
385 LDAP_LOG( CONNECTION, INFO,
386 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
388 Debug( LDAP_DEBUG_ANY,
389 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
396 assert( s < dtblsize );
399 ldap_pvt_thread_mutex_lock( &connections_mutex );
409 for( i=0; i < dtblsize; i++) {
412 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
413 assert( connections[i].c_sb == 0 );
418 sd = AC_SOCKET_INVALID;
419 if (connections[i].c_sb != NULL) {
420 ber_sockbuf_ctrl( connections[i].c_sb,
421 LBER_SB_OPT_GET_FD, &sd );
424 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
425 assert( sd == AC_SOCKET_INVALID );
430 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
434 assert( connections[i].c_struct_state == SLAP_C_USED );
435 assert( connections[i].c_conn_state != SLAP_C_INVALID );
436 assert( sd != AC_SOCKET_INVALID );
441 LDAP_LOG( CONNECTION, INFO,
442 "connection_init(%d): connection table full "
443 "(%d/%d)\n", s, i, dtblsize );
445 Debug( LDAP_DEBUG_ANY,
446 "connection_init(%d): connection table full "
447 "(%d/%d)\n", s, i, dtblsize);
449 ldap_pvt_thread_mutex_unlock( &connections_mutex );
457 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
458 c->c_send_ldap_result = slap_send_ldap_result;
459 c->c_send_search_entry = slap_send_search_entry;
460 c->c_send_search_reference = slap_send_search_reference;
461 c->c_send_ldap_extended = slap_send_ldap_extended;
462 #ifdef LDAP_RES_INTERMEDIATE
463 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
466 c->c_authmech.bv_val = NULL;
467 c->c_authmech.bv_len = 0;
468 c->c_dn.bv_val = NULL;
470 c->c_ndn.bv_val = NULL;
473 c->c_listener = NULL;
474 c->c_peer_domain.bv_val = NULL;
475 c->c_peer_domain.bv_len = 0;
476 c->c_peer_name.bv_val = NULL;
477 c->c_peer_name.bv_len = 0;
479 LDAP_STAILQ_INIT(&c->c_ops);
480 LDAP_STAILQ_INIT(&c->c_pending_ops);
482 c->c_sasl_bind_mech.bv_val = NULL;
483 c->c_sasl_bind_mech.bv_len = 0;
485 c->c_sasl_authctx = NULL;
486 c->c_sasl_sockctx = NULL;
487 c->c_sasl_extra = NULL;
488 c->c_sasl_bindop = NULL;
490 c->c_sb = ber_sockbuf_alloc( );
493 ber_len_t max = sockbuf_max_incoming;
494 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
497 c->c_currentber = NULL;
499 /* should check status of thread calls */
500 ldap_pvt_thread_mutex_init( &c->c_mutex );
501 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
502 ldap_pvt_thread_cond_init( &c->c_write_cv );
505 if ( slapi_plugins_used ) {
506 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
510 c->c_struct_state = SLAP_C_UNUSED;
513 ldap_pvt_thread_mutex_lock( &c->c_mutex );
515 assert( c->c_struct_state == SLAP_C_UNUSED );
516 assert( c->c_authmech.bv_val == NULL );
517 assert( c->c_dn.bv_val == NULL );
518 assert( c->c_ndn.bv_val == NULL );
519 assert( c->c_listener == NULL );
520 assert( c->c_peer_domain.bv_val == NULL );
521 assert( c->c_peer_name.bv_val == NULL );
522 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
523 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
524 assert( c->c_sasl_bind_mech.bv_val == NULL );
525 assert( c->c_sasl_done == 0 );
526 assert( c->c_sasl_authctx == NULL );
527 assert( c->c_sasl_sockctx == NULL );
528 assert( c->c_sasl_extra == NULL );
529 assert( c->c_sasl_bindop == NULL );
530 assert( c->c_currentber == NULL );
531 assert( c->c_writewaiter == 0);
533 c->c_listener = listener;
535 if ( flags == CONN_IS_CLIENT ) {
536 c->c_conn_state = SLAP_C_CLIENT;
537 c->c_struct_state = SLAP_C_USED;
538 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
539 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
540 ldap_pvt_thread_mutex_unlock( &connections_mutex );
545 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
546 ber_str2bv( peername, 0, 1, &c->c_peer_name );
548 c->c_n_ops_received = 0;
549 c->c_n_ops_executing = 0;
550 c->c_n_ops_pending = 0;
551 c->c_n_ops_completed = 0;
557 /* set to zero until bind, implies LDAP_VERSION3 */
561 c->c_activitytime = c->c_starttime = slap_get_time();
563 if( global_idletimeout > 0 ) {
564 c->c_activitytime = c->c_starttime = slap_get_time();
568 #ifdef LDAP_CONNECTIONLESS
570 if( flags == CONN_IS_UDP ) {
573 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
574 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
576 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
577 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
578 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
579 LBER_SBIOD_LEVEL_PROVIDER, NULL );
584 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
585 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
587 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
588 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
592 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
593 INT_MAX, (void*)"ldap_" );
596 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
597 c /* non-NULL */ ) < 0 )
600 LDAP_LOG( CONNECTION, INFO,
601 "connection_init: conn %lu set nonblocking failed\n",
604 Debug( LDAP_DEBUG_ANY,
605 "connection_init(%d, %s): set nonblocking failed\n",
606 s, c->c_peer_name.bv_val, 0 );
610 id = c->c_connid = conn_nextid++;
612 c->c_conn_state = SLAP_C_INACTIVE;
613 c->c_struct_state = SLAP_C_USED;
615 c->c_ssf = c->c_transport_ssf = ssf;
619 if ( flags == CONN_IS_TLS ) {
621 c->c_needs_tls_accept = 1;
624 c->c_needs_tls_accept = 0;
628 slap_sasl_open( c, 0 );
629 slap_sasl_external( c, ssf, authid );
631 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
632 ldap_pvt_thread_mutex_unlock( &connections_mutex );
634 backend_connection_init(c);
639 void connection2anonymous( Connection *c )
641 assert( connections != NULL );
645 ber_len_t max = sockbuf_max_incoming;
646 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
649 if(c->c_authmech.bv_val != NULL ) {
650 free(c->c_authmech.bv_val);
651 c->c_authmech.bv_val = NULL;
653 c->c_authmech.bv_len = 0;
655 if(c->c_dn.bv_val != NULL) {
656 free(c->c_dn.bv_val);
657 c->c_dn.bv_val = NULL;
660 if(c->c_ndn.bv_val != NULL) {
661 free(c->c_ndn.bv_val);
662 c->c_ndn.bv_val = NULL;
666 c->c_authz_backend = NULL;
670 connection_destroy( Connection *c )
672 /* note: connections_mutex should be locked by caller */
674 unsigned long connid;
676 assert( connections != NULL );
678 assert( c->c_struct_state != SLAP_C_UNUSED );
679 assert( c->c_conn_state != SLAP_C_INVALID );
680 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
681 assert( c->c_writewaiter == 0);
683 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
684 connid = c->c_connid;
686 backend_connection_destroy(c);
691 c->c_activitytime = c->c_starttime = 0;
693 connection2anonymous( c );
694 c->c_listener = NULL;
696 if(c->c_peer_domain.bv_val != NULL) {
697 free(c->c_peer_domain.bv_val);
698 c->c_peer_domain.bv_val = NULL;
700 c->c_peer_domain.bv_len = 0;
701 if(c->c_peer_name.bv_val != NULL) {
702 free(c->c_peer_name.bv_val);
703 c->c_peer_name.bv_val = NULL;
705 c->c_peer_name.bv_len = 0;
707 c->c_sasl_bind_in_progress = 0;
708 if(c->c_sasl_bind_mech.bv_val != NULL) {
709 free(c->c_sasl_bind_mech.bv_val);
710 c->c_sasl_bind_mech.bv_val = NULL;
712 c->c_sasl_bind_mech.bv_len = 0;
714 slap_sasl_close( c );
716 if ( c->c_currentber != NULL ) {
717 ber_free( c->c_currentber, 1 );
718 c->c_currentber = NULL;
721 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
722 if ( sd != AC_SOCKET_INVALID ) {
723 slapd_remove( sd, 1, 0 );
725 Statslog( LDAP_DEBUG_STATS,
726 "conn=%lu fd=%ld closed\n",
727 connid, (long) sd, 0, 0, 0 );
730 ber_sockbuf_free( c->c_sb );
732 c->c_sb = ber_sockbuf_alloc( );
735 ber_len_t max = sockbuf_max_incoming;
736 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
739 c->c_conn_state = SLAP_C_INVALID;
740 c->c_struct_state = SLAP_C_UNUSED;
743 /* call destructors, then constructors; avoids unnecessary allocation */
744 if ( slapi_plugins_used ) {
745 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
750 int connection_state_closing( Connection *c )
752 /* c_mutex must be locked by caller */
756 assert( c->c_struct_state == SLAP_C_USED );
758 state = c->c_conn_state;
760 assert( state != SLAP_C_INVALID );
762 return state == SLAP_C_CLOSING;
765 static void connection_abandon( Connection *c )
767 /* c_mutex must be locked by caller */
771 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
775 /* remove pending operations */
776 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
777 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
778 LDAP_STAILQ_NEXT(o, o_next) = NULL;
783 void connection_closing( Connection *c )
785 assert( connections != NULL );
787 assert( c->c_struct_state == SLAP_C_USED );
788 assert( c->c_conn_state != SLAP_C_INVALID );
790 /* c_mutex must be locked by caller */
792 if( c->c_conn_state != SLAP_C_CLOSING ) {
795 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
797 LDAP_LOG( CONNECTION, DETAIL1,
798 "connection_closing: conn %lu readying socket %d for close.\n",
799 c->c_connid, sd, 0 );
801 Debug( LDAP_DEBUG_TRACE,
802 "connection_closing: readying conn=%lu sd=%d for close\n",
803 c->c_connid, sd, 0 );
805 /* update state to closing */
806 c->c_conn_state = SLAP_C_CLOSING;
808 /* don't listen on this port anymore */
809 slapd_clr_read( sd, 1 );
811 /* abandon active operations */
812 connection_abandon( c );
814 /* wake write blocked operations */
815 slapd_clr_write( sd, 1 );
816 ldap_pvt_thread_cond_signal( &c->c_write_cv );
820 static void connection_close( Connection *c )
824 assert( connections != NULL );
826 assert( c->c_struct_state == SLAP_C_USED );
827 assert( c->c_conn_state == SLAP_C_CLOSING );
829 /* note: connections_mutex and c_mutex should be locked by caller */
831 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
832 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
834 LDAP_LOG( CONNECTION, DETAIL1,
835 "connection_close: conn %lu deferring sd %d\n",
836 c->c_connid, sd, 0 );
838 Debug( LDAP_DEBUG_TRACE,
839 "connection_close: deferring conn=%lu sd=%d\n",
840 c->c_connid, sd, 0 );
846 LDAP_LOG( CONNECTION, RESULTS,
847 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
849 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
850 c->c_connid, sd, 0 );
852 connection_destroy( c );
855 unsigned long connections_nextid(void)
858 assert( connections != NULL );
860 ldap_pvt_thread_mutex_lock( &connections_mutex );
864 ldap_pvt_thread_mutex_unlock( &connections_mutex );
869 Connection* connection_first( ber_socket_t *index )
871 assert( connections != NULL );
872 assert( index != NULL );
874 ldap_pvt_thread_mutex_lock( &connections_mutex );
878 return connection_next(NULL, index);
881 Connection* connection_next( Connection *c, ber_socket_t *index )
883 assert( connections != NULL );
884 assert( index != NULL );
885 assert( *index <= dtblsize );
888 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
893 for(; *index < dtblsize; (*index)++) {
894 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
895 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
903 if( connections[*index].c_struct_state == SLAP_C_USED ) {
904 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
905 c = &connections[(*index)++];
909 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
910 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
914 ldap_pvt_thread_mutex_lock( &c->c_mutex );
920 void connection_done( Connection *c )
922 assert( connections != NULL );
925 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
928 ldap_pvt_thread_mutex_unlock( &connections_mutex );
932 * connection_activity - handle the request operation op on connection
933 * conn. This routine figures out what kind of operation it is and
934 * calls the appropriate stub to handle it.
938 #define INCR_OP(var,index) \
940 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
942 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
944 #else /* !SLAPD_MONITOR */
945 #define INCR_OP(var,index)
946 #endif /* !SLAPD_MONITOR */
949 connection_operation( void *ctx, void *arg_v )
951 int rc = SLAPD_DISCONNECT;
952 Operation *op = arg_v;
953 SlapReply rs = {REP_RESULT};
954 ber_tag_t tag = op->o_tag;
956 ber_tag_t oldtag = tag;
957 #endif /* SLAPD_MONITOR */
958 Connection *conn = op->o_conn;
960 void *memctx_null = NULL;
963 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
965 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
967 op->o_threadctx = ctx;
969 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
971 LDAP_LOG( CONNECTION, ERR,
972 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
973 conn->c_connid, (long)tag, 0 );
975 Debug( LDAP_DEBUG_ANY, "connection_operation: "
976 "error: SASL bind in progress (tag=%ld).\n",
979 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
980 "SASL bind in progress" );
981 goto operations_error;
984 /* We can use Thread-Local storage for most mallocs. We can
985 * also use TL for ber parsing, but not on Add or Modify.
988 memsiz = ber_len( op->o_ber ) * 64;
989 if ( SLMALLOC_SLAB_SIZE > memsiz ) memsiz = SLMALLOC_SLAB_SIZE;
991 memsiz = SLMALLOC_SLAB_SIZE;
993 memctx = sl_mem_create( memsiz, ctx );
994 op->o_tmpmemctx = memctx;
995 op->o_tmpmfuncs = &sl_mfuncs;
996 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
997 /* Note - the ber and its buffer are already allocated from
998 * regular memory; this only affects subsequent mallocs that
999 * ber_scanf may invoke.
1001 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1006 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
1007 rc = do_bind( op, &rs );
1010 case LDAP_REQ_UNBIND:
1011 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
1012 rc = do_unbind( op, &rs );
1016 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
1017 rc = do_add( op, &rs );
1020 case LDAP_REQ_DELETE:
1021 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
1022 rc = do_delete( op, &rs );
1025 case LDAP_REQ_MODRDN:
1026 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
1027 rc = do_modrdn( op, &rs );
1030 case LDAP_REQ_MODIFY:
1031 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
1032 rc = do_modify( op, &rs );
1035 case LDAP_REQ_COMPARE:
1036 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
1037 rc = do_compare( op, &rs );
1040 case LDAP_REQ_SEARCH:
1041 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
1042 rc = do_search( op, &rs );
1045 case LDAP_REQ_ABANDON:
1046 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
1047 rc = do_abandon( op, &rs );
1050 case LDAP_REQ_EXTENDED:
1051 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
1052 rc = do_extended( op, &rs );
1057 LDAP_LOG( CONNECTION, INFO,
1058 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
1059 conn->c_connid, tag, 0 );
1061 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
1064 op->o_tag = LBER_ERROR;
1065 rs.sr_err = LDAP_PROTOCOL_ERROR;
1066 rs.sr_text = "unknown LDAP request";
1067 send_ldap_disconnect( op, &rs );
1072 #ifdef SLAPD_MONITOR
1074 #endif /* SLAPD_MONITOR */
1075 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1078 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1079 num_ops_completed++;
1080 #ifdef SLAPD_MONITOR
1083 num_ops_completed_[SLAP_OP_BIND]++;
1085 case LDAP_REQ_UNBIND:
1086 num_ops_completed_[SLAP_OP_UNBIND]++;
1089 num_ops_completed_[SLAP_OP_ADD]++;
1091 case LDAP_REQ_DELETE:
1092 num_ops_completed_[SLAP_OP_DELETE]++;
1094 case LDAP_REQ_MODRDN:
1095 num_ops_completed_[SLAP_OP_MODRDN]++;
1097 case LDAP_REQ_MODIFY:
1098 num_ops_completed_[SLAP_OP_MODIFY]++;
1100 case LDAP_REQ_COMPARE:
1101 num_ops_completed_[SLAP_OP_COMPARE]++;
1103 case LDAP_REQ_SEARCH:
1104 num_ops_completed_[SLAP_OP_SEARCH]++;
1106 case LDAP_REQ_ABANDON:
1107 num_ops_completed_[SLAP_OP_ABANDON]++;
1109 case LDAP_REQ_EXTENDED:
1110 num_ops_completed_[SLAP_OP_EXTENDED]++;
1113 #endif /* SLAPD_MONITOR */
1114 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1116 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1117 op->o_cancel = LDAP_TOO_LATE;
1120 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1121 op->o_cancel != SLAP_CANCEL_DONE )
1123 ldap_pvt_thread_yield();
1126 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1128 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1130 if ( op->o_cancel != SLAP_CANCEL_ACK &&
1131 ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) {
1132 sl_mem_detach( ctx, memctx );
1133 } else if (( op->o_sync_slog_size != -1 )) {
1134 sl_mem_detach( ctx, memctx );
1135 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1136 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1137 conn->c_n_ops_executing--;
1138 conn->c_n_ops_completed++;
1140 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1141 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1143 conn->c_n_ops_executing--;
1144 conn->c_n_ops_completed++;
1149 case LDAP_REQ_UNBIND:
1150 /* c_mutex is locked */
1151 connection_closing( conn );
1155 conn->c_sasl_bind_in_progress =
1156 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1158 if( conn->c_conn_state == SLAP_C_BINDING) {
1159 conn->c_conn_state = SLAP_C_ACTIVE;
1163 connection_resched( conn );
1165 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1170 int connection_client_setup(
1173 ldap_pvt_thread_start_t *func,
1178 if ( connection_init( s, l, "", "", CONN_IS_CLIENT, 0, NULL ) < 0 ) {
1182 c = connection_get( s );
1183 c->c_clientfunc = func;
1184 c->c_clientarg = arg;
1185 connection_return( c );
1186 slapd_add_internal( s, 0 );
1187 slapd_set_read( s, 1 );
1191 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 connection_return( c );
1213 slapd_remove( s, 0, 1 );
1216 int connection_read(ber_socket_t s)
1221 assert( connections != NULL );
1223 ldap_pvt_thread_mutex_lock( &connections_mutex );
1225 /* get (locked) connection */
1226 c = connection_get( s );
1230 LDAP_LOG( CONNECTION, INFO,
1231 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1233 Debug( LDAP_DEBUG_ANY,
1234 "connection_read(%ld): no connection!\n",
1237 slapd_remove(s, 1, 0);
1239 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1245 if( c->c_conn_state == SLAP_C_CLOSING ) {
1247 LDAP_LOG( CONNECTION, INFO,
1248 "connection_read: conn %lu connection closing, ignoring input\n",
1249 c->c_connid, 0, 0 );
1251 Debug( LDAP_DEBUG_TRACE,
1252 "connection_read(%d): closing, ignoring input for id=%lu\n",
1253 s, c->c_connid, 0 );
1255 connection_return( c );
1256 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1260 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1261 slapd_clr_read( s, 0 );
1262 ldap_pvt_thread_pool_submit( &connection_pool,
1263 c->c_clientfunc, c->c_clientarg );
1264 connection_return( c );
1265 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1270 LDAP_LOG( CONNECTION, DETAIL1,
1271 "connection_read: conn %lu checking for input.\n",
1272 c->c_connid, 0, 0 );
1274 Debug( LDAP_DEBUG_TRACE,
1275 "connection_read(%d): checking for input on id=%lu\n",
1276 s, c->c_connid, 0 );
1280 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1281 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1283 #if 0 /* required by next #if 0 */
1289 LDAP_LOG( CONNECTION, ERR,
1290 "connection_read: conn %lu TLS accept error, error %d\n",
1291 c->c_connid, rc, 0 );
1293 Debug( LDAP_DEBUG_TRACE,
1294 "connection_read(%d): TLS accept error "
1295 "error=%d id=%lu, closing\n",
1296 s, rc, c->c_connid );
1298 c->c_needs_tls_accept = 0;
1299 /* connections_mutex and c_mutex are locked */
1300 connection_closing( c );
1303 /* Drain input before close, to allow SSL error codes
1304 * to propagate to client. */
1310 rc = select(s+1, &rfd, NULL, NULL, &tv);
1312 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1316 connection_close( c );
1318 } else if ( rc == 0 ) {
1320 struct berval authid = { 0, NULL };
1322 c->c_needs_tls_accept = 0;
1324 /* we need to let SASL know */
1325 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1327 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1328 if( c->c_tls_ssf > c->c_ssf ) {
1329 c->c_ssf = c->c_tls_ssf;
1332 rc = dnX509peerNormalize( ssl, &authid );
1333 if ( rc != LDAP_SUCCESS ) {
1335 LDAP_LOG( CONNECTION, INFO,
1336 "connection_read: conn %lu unable to get TLS client DN, "
1337 "error %d\n", c->c_connid, rc, 0 );
1339 Debug( LDAP_DEBUG_TRACE,
1340 "connection_read(%d): unable to get TLS client DN "
1341 "error=%d id=%lu\n",
1342 s, rc, c->c_connid );
1345 slap_sasl_external( c, c->c_tls_ssf, &authid );
1346 if ( authid.bv_val ) free( authid.bv_val );
1349 /* if success and data is ready, fall thru to data input loop */
1351 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1353 connection_return( c );
1354 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1360 #ifdef HAVE_CYRUS_SASL
1361 if ( c->c_sasl_layers ) {
1362 /* If previous layer is not removed yet, give up for now */
1363 if ( !c->c_sasl_sockctx ) {
1364 connection_return( c );
1365 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1369 c->c_sasl_layers = 0;
1371 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1373 if( rc != LDAP_SUCCESS ) {
1375 LDAP_LOG( CONNECTION, ERR,
1376 "connection_read: conn %lu SASL install error %d, closing\n",
1377 c->c_connid, rc, 0 );
1379 Debug( LDAP_DEBUG_TRACE,
1380 "connection_read(%d): SASL install error "
1381 "error=%d id=%lu, closing\n",
1382 s, rc, c->c_connid );
1384 /* connections_mutex and c_mutex are locked */
1385 connection_closing( c );
1386 connection_close( c );
1387 connection_return( c );
1388 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1394 #define CONNECTION_INPUT_LOOP 1
1395 /* #define DATA_READY_LOOP 1 */
1398 /* How do we do this without getting into a busy loop ? */
1399 rc = connection_input( c );
1401 #ifdef DATA_READY_LOOP
1402 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1403 #elif CONNECTION_INPUT_LOOP
1411 LDAP_LOG( CONNECTION, ERR,
1412 "connection_read: conn %lu input error %d, closing.\n",
1413 c->c_connid, rc, 0 );
1415 Debug( LDAP_DEBUG_TRACE,
1416 "connection_read(%d): input error=%d id=%lu, closing.\n",
1417 s, rc, c->c_connid );
1419 /* connections_mutex and c_mutex are locked */
1420 connection_closing( c );
1421 connection_close( c );
1422 connection_return( c );
1423 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1427 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1428 slapd_set_read( s, 1 );
1431 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1432 slapd_set_write( s, 1 );
1435 connection_return( c );
1436 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1450 #ifdef LDAP_CONNECTIONLESS
1456 if ( conn->c_currentber == NULL &&
1457 ( conn->c_currentber = ber_alloc()) == NULL )
1460 LDAP_LOG( CONNECTION, ERR,
1461 "connection_input: conn %lu ber_alloc failed.\n",
1462 conn->c_connid, 0, 0 );
1464 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1471 #ifdef LDAP_CONNECTIONLESS
1472 if ( conn->c_is_udp ) {
1473 char peername[sizeof("IP=255.255.255.255:65336")];
1474 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1475 sizeof(struct sockaddr));
1476 if (len != sizeof(struct sockaddr))
1478 sprintf( peername, "IP=%s:%d",
1479 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1480 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1481 Statslog( LDAP_DEBUG_STATS,
1482 "conn=%lu UDP request from %s (%s) accepted.\n",
1483 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1486 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1487 if ( tag != LDAP_TAG_MESSAGE ) {
1491 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1494 LDAP_LOG( CONNECTION, ERR,
1495 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1496 conn->c_connid, err, sock_errstr(err) );
1498 Debug( LDAP_DEBUG_TRACE,
1499 "ber_get_next on fd %d failed errno=%d (%s)\n",
1500 sd, err, sock_errstr(err) );
1502 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1503 /* log, close and send error */
1504 ber_free( conn->c_currentber, 1 );
1505 conn->c_currentber = NULL;
1512 ber = conn->c_currentber;
1513 conn->c_currentber = NULL;
1515 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1516 /* log, close and send error */
1518 LDAP_LOG( CONNECTION, ERR,
1519 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1520 conn->c_connid, tag, 0 );
1522 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n",
1529 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1530 /* log, close and send error */
1532 LDAP_LOG( CONNECTION, ERR,
1533 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1534 conn->c_connid, tag, 0 );
1536 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n",
1544 #ifdef LDAP_CONNECTIONLESS
1545 if( conn->c_is_udp ) {
1546 if( tag == LBER_OCTETSTRING ) {
1547 ber_get_stringa( ber, &cdn );
1548 tag = ber_peek_tag(ber, &len);
1550 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1552 LDAP_LOG( CONNECTION, ERR,
1553 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1554 conn->c_connid, tag, 0 );
1556 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1563 if(tag == LDAP_REQ_BIND) {
1564 /* immediately abandon all exiting operations upon BIND */
1565 connection_abandon( conn );
1568 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1571 op->o_assertion = NULL;
1572 op->o_preread_attrs = NULL;
1573 op->o_postread_attrs = NULL;
1574 op->o_vrFilter = NULL;
1575 op->o_pagedresults_state = conn->c_pagedresults_state;
1577 op->o_res_ber = NULL;
1579 #ifdef LDAP_CONNECTIONLESS
1580 if (conn->c_is_udp) {
1582 ber_str2bv( cdn, 0, 1, &op->o_dn );
1583 op->o_protocol = LDAP_VERSION2;
1585 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1586 if (op->o_res_ber == NULL) return 1;
1588 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1589 sizeof(struct sockaddr), 0 );
1591 if (rc != sizeof(struct sockaddr)) {
1593 LDAP_LOG( CONNECTION, INFO,
1594 "connection_input: conn %lu ber_write failed\n",
1595 conn->c_connid, 0, 0 );
1597 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1602 if (op->o_protocol == LDAP_VERSION2) {
1603 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1606 LDAP_LOG( CONNECTION, INFO,
1607 "connection_input: conn %lu put outer sequence failed\n",
1608 conn->c_connid, 0, 0 );
1610 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1616 #endif /* LDAP_CONNECTIONLESS */
1620 /* Don't process requests when the conn is in the middle of a
1621 * Bind, or if it's closing. Also, don't let any single conn
1622 * use up all the available threads, and don't execute if we're
1623 * currently blocked on output. And don't execute if there are
1624 * already pending ops, let them go first. Abandon operations
1625 * get exceptions to some, but not all, cases.
1627 if (tag != LDAP_REQ_ABANDON && conn->c_conn_state == SLAP_C_CLOSING) {
1629 } else if (tag != LDAP_REQ_ABANDON && conn->c_writewaiter) {
1630 defer = "awaiting write";
1631 } else if (conn->c_n_ops_executing >= connection_pool_max/2) {
1632 defer = "too many executing";
1633 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1635 } else if (tag != LDAP_REQ_ABANDON && conn->c_n_ops_pending) {
1636 defer = "pending operations";
1640 int max = conn->c_dn.bv_len
1641 ? slap_conn_max_pending_auth
1642 : slap_conn_max_pending;
1645 LDAP_LOG( CONNECTION, INFO,
1646 "connection_input: conn %lu deferring operation: %s\n",
1647 conn->c_connid, defer, 0 );
1649 Debug( LDAP_DEBUG_ANY,
1650 "connection_input: conn=%lu deferring operation: %s\n",
1651 conn->c_connid, defer, 0 );
1653 conn->c_n_ops_pending++;
1654 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1655 if ( conn->c_n_ops_pending > max ) {
1661 conn->c_n_ops_executing++;
1662 connection_op_activate( op );
1666 if ( conn->c_struct_state != SLAP_C_USED ) {
1667 /* connection must have got closed underneath us */
1671 assert( conn->c_struct_state == SLAP_C_USED );
1677 connection_resched( Connection *conn )
1681 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1684 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1686 /* us trylock to avoid possible deadlock */
1687 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1691 LDAP_LOG( CONNECTION, DETAIL1,
1692 "connection_resched: conn %lu reaquiring locks.\n",
1693 conn->c_connid, 0, 0 );
1695 Debug( LDAP_DEBUG_TRACE,
1696 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1697 conn->c_connid, sd, 0 );
1700 * reaquire locks in the right order...
1701 * this may allow another thread to close this connection,
1702 * so recheck state below.
1704 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1705 ldap_pvt_thread_mutex_lock( &connections_mutex );
1706 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1709 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1711 LDAP_LOG( CONNECTION, INFO,
1712 "connection_resched: conn %lu closed by other thread.\n",
1713 conn->c_connid, 0, 0 );
1715 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1716 "closed by other thread conn=%lu sd=%d\n",
1717 conn->c_connid, sd, 0 );
1721 LDAP_LOG( CONNECTION, DETAIL1,
1722 "connection_resched: conn %lu attempting closing.\n",
1723 conn->c_connid, 0, 0 );
1725 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1726 "attempting closing conn=%lu sd=%d\n",
1727 conn->c_connid, sd, 0 );
1729 connection_close( conn );
1732 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1736 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1737 /* other states need different handling */
1741 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1742 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1745 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1746 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1747 /* pending operations should not be marked for abandonment */
1748 assert(!op->o_abandon);
1750 conn->c_n_ops_pending--;
1751 conn->c_n_ops_executing++;
1753 connection_op_activate( op );
1755 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1762 static int connection_op_activate( Operation *op )
1765 ber_tag_t tag = op->o_tag;
1767 if(tag == LDAP_REQ_BIND) {
1768 op->o_conn->c_conn_state = SLAP_C_BINDING;
1771 if (!op->o_dn.bv_len) {
1772 op->o_authz = op->o_conn->c_authz;
1773 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1774 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1776 op->o_authtype = op->o_conn->c_authtype;
1777 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1779 if (!op->o_protocol) {
1780 op->o_protocol = op->o_conn->c_protocol
1781 ? op->o_conn->c_protocol : LDAP_VERSION3;
1783 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1784 && op->o_protocol > LDAP_VERSION2)
1786 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1789 op->o_connid = op->o_conn->c_connid;
1791 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1793 status = ldap_pvt_thread_pool_submit( &connection_pool,
1794 connection_operation, (void *) op );
1796 if ( status != 0 ) {
1798 LDAP_LOG( CONNECTION, ERR,
1799 "connection_op_activate: conn %lu thread pool submit failed.\n",
1800 op->o_connid, 0, 0 );
1802 Debug( LDAP_DEBUG_ANY,
1803 "ldap_pvt_thread_pool_submit: failed (%d) for conn=%lu\n",
1804 status, op->o_connid, 0 );
1806 /* should move op to pending list */
1812 int connection_write(ber_socket_t s)
1816 assert( connections != NULL );
1818 ldap_pvt_thread_mutex_lock( &connections_mutex );
1820 c = connection_get( s );
1822 slapd_clr_write( s, 0);
1826 LDAP_LOG( CONNECTION, ERR,
1827 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1829 Debug( LDAP_DEBUG_ANY,
1830 "connection_write(%ld): no connection!\n",
1833 slapd_remove(s, 1, 0);
1834 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1841 LDAP_LOG( CONNECTION, DETAIL1,
1842 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1844 Debug( LDAP_DEBUG_TRACE,
1845 "connection_write(%d): waking output for id=%lu\n",
1846 s, c->c_connid, 0 );
1848 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1850 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1851 slapd_set_read( s, 1 );
1853 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1854 slapd_set_write( s, 1 );
1856 connection_return( c );
1857 ldap_pvt_thread_mutex_unlock( &connections_mutex );