2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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;
49 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
50 static unsigned long conn_nextid = 0;
52 /* structure state (protected by connections_mutex) */
53 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
54 #define SLAP_C_UNUSED 0x01
55 #define SLAP_C_USED 0x02
57 /* connection state (protected by c_mutex ) */
58 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
59 #define SLAP_C_INACTIVE 0x01 /* zero threads */
60 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
61 #define SLAP_C_BINDING 0x03 /* binding */
62 #define SLAP_C_CLOSING 0x04 /* closing */
63 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
66 connection_state2str( int state )
69 case SLAP_C_INVALID: return "!";
70 case SLAP_C_INACTIVE: return "|";
71 case SLAP_C_ACTIVE: return "";
72 case SLAP_C_BINDING: return "B";
73 case SLAP_C_CLOSING: return "C";
74 case SLAP_C_CLIENT: return "L";
80 static Connection* connection_get( ber_socket_t s );
82 static int connection_input( Connection *c );
83 static void connection_close( Connection *c );
85 static int connection_op_activate( Operation *op );
86 static int connection_resched( Connection *conn );
87 static void connection_abandon( Connection *conn );
88 static void connection_destroy( Connection *c );
90 static ldap_pvt_thread_start_t connection_operation;
93 * Initialize connection management infrastructure.
95 int connections_init(void)
99 assert( connections == NULL );
101 if( connections != NULL) {
103 LDAP_LOG( CONNECTION, INFO,
104 "connections_init: already initialized.\n", 0, 0, 0 );
106 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
112 /* should check return of every call */
113 ldap_pvt_thread_mutex_init( &connections_mutex );
114 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
116 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
118 if( connections == NULL ) {
120 LDAP_LOG( CONNECTION, ERR,
121 "connections_init: allocation (%d * %ld) of connection "
122 "array failed\n", dtblsize, (long) sizeof(Connection), 0 );
124 Debug( LDAP_DEBUG_ANY,
125 "connections_init: allocation (%d*%ld) of connection array failed\n",
126 dtblsize, (long) sizeof(Connection), 0 );
131 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
132 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
134 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
137 * per entry initialization of the Connection array initialization
138 * will be done by connection_init()
145 * Destroy connection management infrastructure.
147 int connections_destroy(void)
151 /* should check return of every call */
153 if( connections == NULL) {
155 LDAP_LOG( CONNECTION, INFO,
156 "connections_destroy: nothing to destroy.\n", 0, 0, 0 );
158 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
164 for ( i = 0; i < dtblsize; i++ ) {
165 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
166 ber_sockbuf_free( connections[i].c_sb );
167 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
168 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
169 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
171 if ( slapi_plugins_used ) {
172 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &connections[i] );
181 ldap_pvt_thread_mutex_destroy( &connections_mutex );
182 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
187 * shutdown all connections
189 int connections_shutdown(void)
193 ldap_pvt_thread_mutex_lock( &connections_mutex );
195 for ( i = 0; i < dtblsize; i++ ) {
196 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 );
206 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
208 /* connections_mutex and c_mutex are locked */
209 connection_closing( &connections[i] );
210 connection_close( &connections[i] );
212 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
215 ldap_pvt_thread_mutex_unlock( &connections_mutex );
221 * Timeout idle connections.
223 int connections_timeout_idle(time_t now)
229 for( c = connection_first( &connindex );
231 c = connection_next( c, &connindex ) )
233 /* Don't timeout a slow-running request or a persistent
234 * outbound connection */
235 if( c->c_n_ops_executing ||
236 c->c_conn_state == SLAP_C_CLIENT ) continue;
238 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
240 connection_closing( c );
241 connection_close( c );
245 connection_done( c );
250 static Connection* connection_get( ber_socket_t s )
252 /* connections_mutex should be locked by caller */
257 LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
259 Debug( LDAP_DEBUG_ARGS,
260 "connection_get(%ld)\n",
264 assert( connections != NULL );
266 if(s == AC_SOCKET_INVALID) {
273 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
280 for(i=0; i<dtblsize; i++) {
281 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
282 assert( connections[i].c_conn_state == SLAP_C_INVALID );
283 assert( connections[i].c_sb == 0 );
287 ber_sockbuf_ctrl( connections[i].c_sb,
288 LBER_SB_OPT_GET_FD, &sd );
290 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
291 assert( connections[i].c_conn_state == SLAP_C_INVALID );
292 assert( sd == AC_SOCKET_INVALID );
296 /* state can actually change from used -> unused by resched,
297 * so don't assert details here.
311 ldap_pvt_thread_mutex_lock( &c->c_mutex );
313 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
314 if( c->c_struct_state != SLAP_C_USED ) {
315 /* connection must have been closed due to resched */
317 assert( c->c_conn_state == SLAP_C_INVALID );
318 assert( sd == AC_SOCKET_INVALID );
321 LDAP_LOG( CONNECTION, ARGS,
322 "connection_get: connection %d not used\n", s, 0, 0 );
324 Debug( LDAP_DEBUG_TRACE,
325 "connection_get(%d): connection not used\n",
329 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
334 LDAP_LOG( CONNECTION, RESULTS,
335 "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
337 Debug( LDAP_DEBUG_TRACE,
338 "connection_get(%d): got connid=%lu\n",
344 assert( c->c_struct_state == SLAP_C_USED );
345 assert( c->c_conn_state != SLAP_C_INVALID );
346 assert( sd != AC_SOCKET_INVALID );
349 c->c_activitytime = slap_get_time();
351 if( global_idletimeout > 0 ) {
352 c->c_activitytime = slap_get_time();
360 static void connection_return( Connection *c )
362 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
365 long connection_init(
369 const char* peername,
372 struct berval *authid )
377 assert( connections != NULL );
379 assert( listener != NULL );
380 assert( dnsname != NULL );
381 assert( peername != NULL );
384 assert( flags != CONN_IS_TLS );
387 if( s == AC_SOCKET_INVALID ) {
389 LDAP_LOG( CONNECTION, INFO,
390 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
392 Debug( LDAP_DEBUG_ANY,
393 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
400 assert( s < dtblsize );
403 ldap_pvt_thread_mutex_lock( &connections_mutex );
413 for( i=0; i < dtblsize; i++) {
416 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
417 assert( connections[i].c_sb == 0 );
422 sd = AC_SOCKET_INVALID;
423 if (connections[i].c_sb != NULL) {
424 ber_sockbuf_ctrl( connections[i].c_sb,
425 LBER_SB_OPT_GET_FD, &sd );
428 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
429 assert( sd == AC_SOCKET_INVALID );
434 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
438 assert( connections[i].c_struct_state == SLAP_C_USED );
439 assert( connections[i].c_conn_state != SLAP_C_INVALID );
440 assert( sd != AC_SOCKET_INVALID );
445 LDAP_LOG( CONNECTION, INFO,
446 "connection_init(%d): connection table full "
447 "(%d/%d)\n", s, i, dtblsize );
449 Debug( LDAP_DEBUG_ANY,
450 "connection_init(%d): connection table full "
451 "(%d/%d)\n", s, i, dtblsize);
453 ldap_pvt_thread_mutex_unlock( &connections_mutex );
461 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
462 c->c_send_ldap_result = slap_send_ldap_result;
463 c->c_send_search_entry = slap_send_search_entry;
464 c->c_send_search_reference = slap_send_search_reference;
465 c->c_send_ldap_extended = slap_send_ldap_extended;
466 #ifdef LDAP_RES_INTERMEDIATE
467 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
470 c->c_authmech.bv_val = NULL;
471 c->c_authmech.bv_len = 0;
472 c->c_dn.bv_val = NULL;
474 c->c_ndn.bv_val = NULL;
477 c->c_listener = NULL;
478 c->c_peer_domain.bv_val = NULL;
479 c->c_peer_domain.bv_len = 0;
480 c->c_peer_name.bv_val = NULL;
481 c->c_peer_name.bv_len = 0;
483 LDAP_STAILQ_INIT(&c->c_ops);
484 LDAP_STAILQ_INIT(&c->c_pending_ops);
486 c->c_sasl_bind_mech.bv_val = NULL;
487 c->c_sasl_bind_mech.bv_len = 0;
489 c->c_sasl_authctx = NULL;
490 c->c_sasl_sockctx = NULL;
491 c->c_sasl_extra = NULL;
492 c->c_sasl_bindop = NULL;
494 c->c_sb = ber_sockbuf_alloc( );
497 ber_len_t max = sockbuf_max_incoming;
498 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
501 c->c_currentber = NULL;
503 /* should check status of thread calls */
504 ldap_pvt_thread_mutex_init( &c->c_mutex );
505 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
506 ldap_pvt_thread_cond_init( &c->c_write_cv );
509 if ( slapi_plugins_used ) {
510 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
514 c->c_struct_state = SLAP_C_UNUSED;
517 ldap_pvt_thread_mutex_lock( &c->c_mutex );
519 assert( c->c_struct_state == SLAP_C_UNUSED );
520 assert( c->c_authmech.bv_val == NULL );
521 assert( c->c_dn.bv_val == NULL );
522 assert( c->c_ndn.bv_val == NULL );
523 assert( c->c_listener == NULL );
524 assert( c->c_peer_domain.bv_val == NULL );
525 assert( c->c_peer_name.bv_val == NULL );
526 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
527 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
528 assert( c->c_sasl_bind_mech.bv_val == NULL );
529 assert( c->c_sasl_done == 0 );
530 assert( c->c_sasl_authctx == NULL );
531 assert( c->c_sasl_sockctx == NULL );
532 assert( c->c_sasl_extra == NULL );
533 assert( c->c_sasl_bindop == NULL );
534 assert( c->c_currentber == NULL );
535 assert( c->c_writewaiter == 0);
537 c->c_listener = listener;
539 if ( flags == CONN_IS_CLIENT ) {
540 c->c_conn_state = SLAP_C_CLIENT;
541 c->c_struct_state = SLAP_C_USED;
542 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
543 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
544 ldap_pvt_thread_mutex_unlock( &connections_mutex );
549 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
550 ber_str2bv( peername, 0, 1, &c->c_peer_name );
552 c->c_n_ops_received = 0;
553 c->c_n_ops_executing = 0;
554 c->c_n_ops_pending = 0;
555 c->c_n_ops_completed = 0;
561 /* set to zero until bind, implies LDAP_VERSION3 */
565 c->c_activitytime = c->c_starttime = slap_get_time();
567 if( global_idletimeout > 0 ) {
568 c->c_activitytime = c->c_starttime = slap_get_time();
572 #ifdef LDAP_CONNECTIONLESS
574 if( flags == CONN_IS_UDP ) {
577 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
578 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
580 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
581 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
582 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
583 LBER_SBIOD_LEVEL_PROVIDER, NULL );
588 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
589 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
591 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
592 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
596 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
597 INT_MAX, (void*)"ldap_" );
600 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
601 c /* non-NULL */ ) < 0 )
604 LDAP_LOG( CONNECTION, INFO,
605 "connection_init: conn %lu set nonblocking failed\n",
608 Debug( LDAP_DEBUG_ANY,
609 "connection_init(%d, %s): set nonblocking failed\n",
610 s, c->c_peer_name.bv_val, 0 );
614 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
615 id = c->c_connid = conn_nextid++;
616 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
618 c->c_conn_state = SLAP_C_INACTIVE;
619 c->c_struct_state = SLAP_C_USED;
621 c->c_ssf = c->c_transport_ssf = ssf;
625 if ( flags == CONN_IS_TLS ) {
627 c->c_needs_tls_accept = 1;
630 c->c_needs_tls_accept = 0;
634 slap_sasl_open( c, 0 );
635 slap_sasl_external( c, ssf, authid );
637 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
638 ldap_pvt_thread_mutex_unlock( &connections_mutex );
640 backend_connection_init(c);
645 void connection2anonymous( Connection *c )
647 assert( connections != NULL );
651 ber_len_t max = sockbuf_max_incoming;
652 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
655 if(c->c_authmech.bv_val != NULL ) {
656 free(c->c_authmech.bv_val);
657 c->c_authmech.bv_val = NULL;
659 c->c_authmech.bv_len = 0;
661 if(c->c_dn.bv_val != NULL) {
662 free(c->c_dn.bv_val);
663 c->c_dn.bv_val = NULL;
666 if(c->c_ndn.bv_val != NULL) {
667 free(c->c_ndn.bv_val);
668 c->c_ndn.bv_val = NULL;
672 c->c_authz_backend = NULL;
676 connection_destroy( Connection *c )
678 /* note: connections_mutex should be locked by caller */
680 unsigned long connid;
682 assert( connections != NULL );
684 assert( c->c_struct_state != SLAP_C_UNUSED );
685 assert( c->c_conn_state != SLAP_C_INVALID );
686 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
687 assert( c->c_writewaiter == 0);
689 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
690 connid = c->c_connid;
692 backend_connection_destroy(c);
697 c->c_activitytime = c->c_starttime = 0;
699 connection2anonymous( c );
700 c->c_listener = NULL;
702 if(c->c_peer_domain.bv_val != NULL) {
703 free(c->c_peer_domain.bv_val);
704 c->c_peer_domain.bv_val = NULL;
706 c->c_peer_domain.bv_len = 0;
707 if(c->c_peer_name.bv_val != NULL) {
708 free(c->c_peer_name.bv_val);
709 c->c_peer_name.bv_val = NULL;
711 c->c_peer_name.bv_len = 0;
713 c->c_sasl_bind_in_progress = 0;
714 if(c->c_sasl_bind_mech.bv_val != NULL) {
715 free(c->c_sasl_bind_mech.bv_val);
716 c->c_sasl_bind_mech.bv_val = NULL;
718 c->c_sasl_bind_mech.bv_len = 0;
720 slap_sasl_close( c );
722 if ( c->c_currentber != NULL ) {
723 ber_free( c->c_currentber, 1 );
724 c->c_currentber = NULL;
727 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
728 if ( sd != AC_SOCKET_INVALID ) {
729 slapd_remove( sd, 1, 0 );
731 Statslog( LDAP_DEBUG_STATS,
732 "conn=%lu fd=%ld closed\n",
733 connid, (long) sd, 0, 0, 0 );
736 ber_sockbuf_free( c->c_sb );
738 c->c_sb = ber_sockbuf_alloc( );
741 ber_len_t max = sockbuf_max_incoming;
742 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
745 c->c_conn_state = SLAP_C_INVALID;
746 c->c_struct_state = SLAP_C_UNUSED;
749 /* call destructors, then constructors; avoids unnecessary allocation */
750 if ( slapi_plugins_used ) {
751 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
756 int connection_state_closing( Connection *c )
758 /* c_mutex must be locked by caller */
762 assert( c->c_struct_state == SLAP_C_USED );
764 state = c->c_conn_state;
766 assert( state != SLAP_C_INVALID );
768 return state == SLAP_C_CLOSING;
771 static void connection_abandon( Connection *c )
773 /* c_mutex must be locked by caller */
775 Operation *o, *next, op = {0};
779 op.o_connid = c->c_connid;
780 op.o_tag = LDAP_REQ_ABANDON;
781 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
782 next = LDAP_STAILQ_NEXT( o, o_next );
783 op.orn_msgid = o->o_msgid;
785 fe_op_abandon( &op, &rs );
788 /* remove pending operations */
789 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
790 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
791 LDAP_STAILQ_NEXT(o, o_next) = NULL;
796 void connection_closing( Connection *c )
798 assert( connections != NULL );
800 assert( c->c_struct_state == SLAP_C_USED );
801 assert( c->c_conn_state != SLAP_C_INVALID );
803 /* c_mutex must be locked by caller */
805 if( c->c_conn_state != SLAP_C_CLOSING ) {
808 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
810 LDAP_LOG( CONNECTION, DETAIL1,
811 "connection_closing: conn %lu readying socket %d for close.\n",
812 c->c_connid, sd, 0 );
814 Debug( LDAP_DEBUG_TRACE,
815 "connection_closing: readying conn=%lu sd=%d for close\n",
816 c->c_connid, sd, 0 );
818 /* update state to closing */
819 c->c_conn_state = SLAP_C_CLOSING;
821 /* don't listen on this port anymore */
822 slapd_clr_read( sd, 1 );
824 /* abandon active operations */
825 connection_abandon( c );
827 /* wake write blocked operations */
828 slapd_clr_write( sd, 1 );
829 ldap_pvt_thread_cond_signal( &c->c_write_cv );
833 static void connection_close( Connection *c )
837 assert( connections != NULL );
839 assert( c->c_struct_state == SLAP_C_USED );
840 assert( c->c_conn_state == SLAP_C_CLOSING );
842 /* note: connections_mutex and c_mutex should be locked by caller */
844 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
845 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
847 LDAP_LOG( CONNECTION, DETAIL1,
848 "connection_close: conn %lu deferring sd %d\n",
849 c->c_connid, sd, 0 );
851 Debug( LDAP_DEBUG_TRACE,
852 "connection_close: deferring conn=%lu sd=%d\n",
853 c->c_connid, sd, 0 );
859 LDAP_LOG( CONNECTION, RESULTS,
860 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
862 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
863 c->c_connid, sd, 0 );
865 connection_destroy( c );
868 unsigned long connections_nextid(void)
871 assert( connections != NULL );
873 ldap_pvt_thread_mutex_lock( &connections_mutex );
877 ldap_pvt_thread_mutex_unlock( &connections_mutex );
882 Connection* connection_first( ber_socket_t *index )
884 assert( connections != NULL );
885 assert( index != NULL );
887 ldap_pvt_thread_mutex_lock( &connections_mutex );
891 return connection_next(NULL, index);
894 Connection* connection_next( Connection *c, ber_socket_t *index )
896 assert( connections != NULL );
897 assert( index != NULL );
898 assert( *index <= dtblsize );
901 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
906 for(; *index < dtblsize; (*index)++) {
907 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
908 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
916 if( connections[*index].c_struct_state == SLAP_C_USED ) {
917 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
918 c = &connections[(*index)++];
922 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
923 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
927 ldap_pvt_thread_mutex_lock( &c->c_mutex );
933 void connection_done( Connection *c )
935 assert( connections != NULL );
938 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
941 ldap_pvt_thread_mutex_unlock( &connections_mutex );
945 * connection_activity - handle the request operation op on connection
946 * conn. This routine figures out what kind of operation it is and
947 * calls the appropriate stub to handle it.
951 #define INCR_OP(var,index) \
953 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
955 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
957 #else /* !SLAPD_MONITOR */
958 #define INCR_OP(var,index)
959 #endif /* !SLAPD_MONITOR */
962 connection_operation( void *ctx, void *arg_v )
965 Operation *op = arg_v;
966 SlapReply rs = {REP_RESULT};
967 ber_tag_t tag = op->o_tag;
969 ber_tag_t oldtag = tag;
970 #endif /* SLAPD_MONITOR */
971 Connection *conn = op->o_conn;
973 void *memctx_null = NULL;
976 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
978 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
980 op->o_threadctx = ctx;
984 case LDAP_REQ_UNBIND:
986 case LDAP_REQ_DELETE:
987 case LDAP_REQ_MODRDN:
988 case LDAP_REQ_MODIFY:
989 case LDAP_REQ_COMPARE:
990 case LDAP_REQ_SEARCH:
991 case LDAP_REQ_ABANDON:
992 case LDAP_REQ_EXTENDED:
996 LDAP_LOG( CONNECTION, INFO, "connection_operation: "
997 "conn %lu unknown LDAP request 0x%lx\n",
998 conn->c_connid, tag, 0 );
1000 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1001 "conn %lu unknown LDAP request 0x%lx\n",
1002 conn->c_connid, tag, 0 );
1004 op->o_tag = LBER_ERROR;
1005 rs.sr_err = LDAP_PROTOCOL_ERROR;
1006 rs.sr_text = "unknown LDAP request";
1007 send_ldap_disconnect( op, &rs );
1008 rc = SLAPD_DISCONNECT;
1009 goto operations_error;
1012 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1014 LDAP_LOG( CONNECTION, ERR,
1015 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
1016 conn->c_connid, (long)tag, 0 );
1018 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1019 "error: SASL bind in progress (tag=%ld).\n",
1022 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1023 "SASL bind in progress" );
1024 rc = LDAP_OPERATIONS_ERROR;
1025 goto operations_error;
1028 /* We can use Thread-Local storage for most mallocs. We can
1029 * also use TL for ber parsing, but not on Add or Modify.
1032 memsiz = ber_len( op->o_ber ) * 64;
1033 if ( SLMALLOC_SLAB_SIZE > memsiz ) memsiz = SLMALLOC_SLAB_SIZE;
1035 memsiz = SLMALLOC_SLAB_SIZE;
1037 memctx = sl_mem_create( memsiz, ctx );
1038 op->o_tmpmemctx = memctx;
1039 op->o_tmpmfuncs = &sl_mfuncs;
1040 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1041 /* Note - the ber and its buffer are already allocated from
1042 * regular memory; this only affects subsequent mallocs that
1043 * ber_scanf may invoke.
1045 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1050 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
1051 rc = do_bind( op, &rs );
1054 case LDAP_REQ_UNBIND:
1055 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
1056 rc = do_unbind( op, &rs );
1060 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
1061 rc = do_add( op, &rs );
1064 case LDAP_REQ_DELETE:
1065 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
1066 rc = do_delete( op, &rs );
1069 case LDAP_REQ_MODRDN:
1070 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
1071 rc = do_modrdn( op, &rs );
1074 case LDAP_REQ_MODIFY:
1075 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
1076 rc = do_modify( op, &rs );
1079 case LDAP_REQ_COMPARE:
1080 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
1081 rc = do_compare( op, &rs );
1084 case LDAP_REQ_SEARCH:
1085 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
1086 rc = do_search( op, &rs );
1089 case LDAP_REQ_ABANDON:
1090 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
1091 rc = do_abandon( op, &rs );
1094 case LDAP_REQ_EXTENDED:
1095 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
1096 rc = do_extended( op, &rs );
1105 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1107 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1109 num_ops_completed++;
1110 #ifdef SLAPD_MONITOR
1113 num_ops_completed_[SLAP_OP_BIND]++;
1115 case LDAP_REQ_UNBIND:
1116 num_ops_completed_[SLAP_OP_UNBIND]++;
1119 num_ops_completed_[SLAP_OP_ADD]++;
1121 case LDAP_REQ_DELETE:
1122 num_ops_completed_[SLAP_OP_DELETE]++;
1124 case LDAP_REQ_MODRDN:
1125 num_ops_completed_[SLAP_OP_MODRDN]++;
1127 case LDAP_REQ_MODIFY:
1128 num_ops_completed_[SLAP_OP_MODIFY]++;
1130 case LDAP_REQ_COMPARE:
1131 num_ops_completed_[SLAP_OP_COMPARE]++;
1133 case LDAP_REQ_SEARCH:
1134 num_ops_completed_[SLAP_OP_SEARCH]++;
1136 case LDAP_REQ_ABANDON:
1137 num_ops_completed_[SLAP_OP_ABANDON]++;
1139 case LDAP_REQ_EXTENDED:
1140 num_ops_completed_[SLAP_OP_EXTENDED]++;
1143 /* this is reachable */
1146 #endif /* SLAPD_MONITOR */
1147 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1149 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1150 op->o_cancel = LDAP_TOO_LATE;
1152 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1153 op->o_cancel != SLAP_CANCEL_DONE )
1155 ldap_pvt_thread_yield();
1158 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1160 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1162 if ( op->o_cancel != SLAP_CANCEL_ACK &&
1163 ( op->o_sync_mode & SLAP_SYNC_PERSIST ) )
1165 sl_mem_detach( ctx, memctx );
1167 } else if ( op->o_sync_slog_size != -1 ) {
1168 sl_mem_detach( ctx, memctx );
1169 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1170 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1171 conn->c_n_ops_executing--;
1172 conn->c_n_ops_completed++;
1175 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1176 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1178 conn->c_n_ops_executing--;
1179 conn->c_n_ops_completed++;
1184 case LDAP_REQ_UNBIND:
1185 /* c_mutex is locked */
1186 connection_closing( conn );
1190 conn->c_sasl_bind_in_progress =
1191 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1193 if( conn->c_conn_state == SLAP_C_BINDING) {
1194 conn->c_conn_state = SLAP_C_ACTIVE;
1198 connection_resched( conn );
1199 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1203 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1205 int connection_client_setup(
1207 ldap_pvt_thread_start_t *func,
1213 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1214 CONN_IS_CLIENT, 0, NULL );
1215 if ( rc < 0 ) return -1;
1217 c = connection_get( s );
1218 c->c_clientfunc = func;
1219 c->c_clientarg = arg;
1220 connection_return( c );
1221 slapd_add_internal( s, 0 );
1222 slapd_set_read( s, 1 );
1226 void connection_client_enable(
1229 slapd_set_read( s, 1 );
1232 void connection_client_stop(
1237 /* get (locked) connection */
1238 c = connection_get( s );
1240 assert( c->c_conn_state == SLAP_C_CLIENT );
1242 c->c_listener = NULL;
1243 c->c_conn_state = SLAP_C_INVALID;
1244 c->c_struct_state = SLAP_C_UNUSED;
1245 connection_return( c );
1246 slapd_remove( s, 0, 1 );
1249 int connection_read(ber_socket_t s)
1254 assert( connections != NULL );
1256 ldap_pvt_thread_mutex_lock( &connections_mutex );
1258 /* get (locked) connection */
1259 c = connection_get( s );
1263 LDAP_LOG( CONNECTION, INFO,
1264 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1266 Debug( LDAP_DEBUG_ANY,
1267 "connection_read(%ld): no connection!\n",
1270 slapd_remove(s, 1, 0);
1272 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1278 if( c->c_conn_state == SLAP_C_CLOSING ) {
1280 LDAP_LOG( CONNECTION, INFO,
1281 "connection_read: conn %lu connection closing, ignoring input\n",
1282 c->c_connid, 0, 0 );
1284 Debug( LDAP_DEBUG_TRACE,
1285 "connection_read(%d): closing, ignoring input for id=%lu\n",
1286 s, c->c_connid, 0 );
1288 connection_return( c );
1289 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1293 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1294 slapd_clr_read( s, 0 );
1295 ldap_pvt_thread_pool_submit( &connection_pool,
1296 c->c_clientfunc, c->c_clientarg );
1297 connection_return( c );
1298 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1303 LDAP_LOG( CONNECTION, DETAIL1,
1304 "connection_read: conn %lu checking for input.\n",
1305 c->c_connid, 0, 0 );
1307 Debug( LDAP_DEBUG_TRACE,
1308 "connection_read(%d): checking for input on id=%lu\n",
1309 s, c->c_connid, 0 );
1313 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1314 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1316 #if 0 /* required by next #if 0 */
1322 LDAP_LOG( CONNECTION, ERR,
1323 "connection_read: conn %lu TLS accept error, error %d\n",
1324 c->c_connid, rc, 0 );
1326 Debug( LDAP_DEBUG_TRACE,
1327 "connection_read(%d): TLS accept error "
1328 "error=%d id=%lu, closing\n",
1329 s, rc, c->c_connid );
1331 c->c_needs_tls_accept = 0;
1332 /* connections_mutex and c_mutex are locked */
1333 connection_closing( c );
1336 /* Drain input before close, to allow SSL error codes
1337 * to propagate to client. */
1343 rc = select(s+1, &rfd, NULL, NULL, &tv);
1345 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1349 connection_close( c );
1351 } else if ( rc == 0 ) {
1353 struct berval authid = BER_BVNULL;
1355 c->c_needs_tls_accept = 0;
1357 /* we need to let SASL know */
1358 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1360 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1361 if( c->c_tls_ssf > c->c_ssf ) {
1362 c->c_ssf = c->c_tls_ssf;
1365 rc = dnX509peerNormalize( ssl, &authid );
1366 if ( rc != LDAP_SUCCESS ) {
1368 LDAP_LOG( CONNECTION, INFO, "connection_read: "
1369 "conn %lu unable to get TLS client DN, error %d\n",
1370 c->c_connid, rc, 0 );
1372 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1373 "unable to get TLS client DN, error=%d id=%lu\n",
1374 s, rc, c->c_connid );
1377 slap_sasl_external( c, c->c_tls_ssf, &authid );
1378 if ( authid.bv_val ) free( authid.bv_val );
1381 /* if success and data is ready, fall thru to data input loop */
1383 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1385 connection_return( c );
1386 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1392 #ifdef HAVE_CYRUS_SASL
1393 if ( c->c_sasl_layers ) {
1394 /* If previous layer is not removed yet, give up for now */
1395 if ( !c->c_sasl_sockctx ) {
1396 connection_return( c );
1397 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1401 c->c_sasl_layers = 0;
1403 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1405 if( rc != LDAP_SUCCESS ) {
1407 LDAP_LOG( CONNECTION, ERR,
1408 "connection_read: conn %lu SASL install error %d, closing\n",
1409 c->c_connid, rc, 0 );
1411 Debug( LDAP_DEBUG_TRACE,
1412 "connection_read(%d): SASL install error "
1413 "error=%d id=%lu, closing\n",
1414 s, rc, c->c_connid );
1416 /* connections_mutex and c_mutex are locked */
1417 connection_closing( c );
1418 connection_close( c );
1419 connection_return( c );
1420 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1426 #define CONNECTION_INPUT_LOOP 1
1427 /* #define DATA_READY_LOOP 1 */
1430 /* How do we do this without getting into a busy loop ? */
1431 rc = connection_input( c );
1433 #ifdef DATA_READY_LOOP
1434 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1435 #elif CONNECTION_INPUT_LOOP
1443 LDAP_LOG( CONNECTION, ERR,
1444 "connection_read: conn %lu input error %d, closing.\n",
1445 c->c_connid, rc, 0 );
1447 Debug( LDAP_DEBUG_TRACE,
1448 "connection_read(%d): input error=%d id=%lu, closing.\n",
1449 s, rc, c->c_connid );
1451 /* connections_mutex and c_mutex are locked */
1452 connection_closing( c );
1453 connection_close( c );
1454 connection_return( c );
1455 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1459 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1460 slapd_set_read( s, 1 );
1463 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1464 slapd_set_write( s, 1 );
1467 connection_return( c );
1468 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1482 #ifdef LDAP_CONNECTIONLESS
1488 if ( conn->c_currentber == NULL &&
1489 ( conn->c_currentber = ber_alloc()) == NULL )
1492 LDAP_LOG( CONNECTION, ERR,
1493 "connection_input: conn %lu ber_alloc failed.\n",
1494 conn->c_connid, 0, 0 );
1496 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1503 #ifdef LDAP_CONNECTIONLESS
1504 if ( conn->c_is_udp ) {
1505 char peername[sizeof("IP=255.255.255.255:65336")];
1506 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1507 sizeof(struct sockaddr));
1508 if (len != sizeof(struct sockaddr))
1510 sprintf( peername, "IP=%s:%d",
1511 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1512 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1513 Statslog( LDAP_DEBUG_STATS,
1514 "conn=%lu UDP request from %s (%s) accepted.\n",
1515 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1518 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1519 if ( tag != LDAP_TAG_MESSAGE ) {
1523 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1526 LDAP_LOG( CONNECTION, ERR,
1527 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1528 conn->c_connid, err, sock_errstr(err) );
1530 Debug( LDAP_DEBUG_TRACE,
1531 "ber_get_next on fd %d failed errno=%d (%s)\n",
1532 sd, err, sock_errstr(err) );
1534 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1535 /* log, close and send error */
1536 ber_free( conn->c_currentber, 1 );
1537 conn->c_currentber = NULL;
1544 ber = conn->c_currentber;
1545 conn->c_currentber = NULL;
1547 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1548 /* log, close and send error */
1550 LDAP_LOG( CONNECTION, ERR,
1551 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1552 conn->c_connid, tag, 0 );
1554 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n",
1561 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1562 /* log, close and send error */
1564 LDAP_LOG( CONNECTION, ERR,
1565 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1566 conn->c_connid, tag, 0 );
1568 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n",
1576 #ifdef LDAP_CONNECTIONLESS
1577 if( conn->c_is_udp ) {
1578 if( tag == LBER_OCTETSTRING ) {
1579 ber_get_stringa( ber, &cdn );
1580 tag = ber_peek_tag(ber, &len);
1582 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1584 LDAP_LOG( CONNECTION, ERR,
1585 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1586 conn->c_connid, tag, 0 );
1588 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1595 if(tag == LDAP_REQ_BIND) {
1596 /* immediately abandon all exiting operations upon BIND */
1597 connection_abandon( conn );
1600 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1603 op->o_assertion = NULL;
1604 op->o_preread_attrs = NULL;
1605 op->o_postread_attrs = NULL;
1606 op->o_vrFilter = NULL;
1607 /* clear state if the connection is being reused from inactive */
1608 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1609 memset( &conn->c_pagedresults_state, 0, sizeof( conn->c_pagedresults_state ) );
1611 op->o_pagedresults_state = conn->c_pagedresults_state;
1613 op->o_res_ber = NULL;
1615 #ifdef LDAP_CONNECTIONLESS
1616 if (conn->c_is_udp) {
1618 ber_str2bv( cdn, 0, 1, &op->o_dn );
1619 op->o_protocol = LDAP_VERSION2;
1621 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1622 if (op->o_res_ber == NULL) return 1;
1624 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1625 sizeof(struct sockaddr), 0 );
1627 if (rc != sizeof(struct sockaddr)) {
1629 LDAP_LOG( CONNECTION, INFO,
1630 "connection_input: conn %lu ber_write failed\n",
1631 conn->c_connid, 0, 0 );
1633 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1638 if (op->o_protocol == LDAP_VERSION2) {
1639 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1642 LDAP_LOG( CONNECTION, INFO,
1643 "connection_input: conn %lu put outer sequence failed\n",
1644 conn->c_connid, 0, 0 );
1646 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1652 #endif /* LDAP_CONNECTIONLESS */
1656 /* Don't process requests when the conn is in the middle of a
1657 * Bind, or if it's closing. Also, don't let any single conn
1658 * use up all the available threads, and don't execute if we're
1659 * currently blocked on output. And don't execute if there are
1660 * already pending ops, let them go first. Abandon operations
1661 * get exceptions to some, but not all, cases.
1663 if (tag != LDAP_REQ_ABANDON && conn->c_conn_state == SLAP_C_CLOSING) {
1665 } else if (tag != LDAP_REQ_ABANDON && conn->c_writewaiter) {
1666 defer = "awaiting write";
1667 } else if (conn->c_n_ops_executing >= connection_pool_max/2) {
1668 defer = "too many executing";
1669 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1671 } else if (tag != LDAP_REQ_ABANDON && conn->c_n_ops_pending) {
1672 defer = "pending operations";
1676 int max = conn->c_dn.bv_len
1677 ? slap_conn_max_pending_auth
1678 : slap_conn_max_pending;
1681 LDAP_LOG( CONNECTION, INFO,
1682 "connection_input: conn %lu deferring operation: %s\n",
1683 conn->c_connid, defer, 0 );
1685 Debug( LDAP_DEBUG_ANY,
1686 "connection_input: conn=%lu deferring operation: %s\n",
1687 conn->c_connid, defer, 0 );
1689 conn->c_n_ops_pending++;
1690 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1691 if ( conn->c_n_ops_pending > max ) {
1697 conn->c_n_ops_executing++;
1698 connection_op_activate( op );
1702 if ( conn->c_struct_state != SLAP_C_USED ) {
1703 /* connection must have got closed underneath us */
1707 assert( conn->c_struct_state == SLAP_C_USED );
1713 connection_resched( Connection *conn )
1717 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1720 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1722 /* us trylock to avoid possible deadlock */
1723 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1727 LDAP_LOG( CONNECTION, DETAIL1,
1728 "connection_resched: conn %lu reaquiring locks.\n",
1729 conn->c_connid, 0, 0 );
1731 Debug( LDAP_DEBUG_TRACE,
1732 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1733 conn->c_connid, sd, 0 );
1736 * reaquire locks in the right order...
1737 * this may allow another thread to close this connection,
1738 * so recheck state below.
1740 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1741 ldap_pvt_thread_mutex_lock( &connections_mutex );
1742 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1745 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1747 LDAP_LOG( CONNECTION, INFO,
1748 "connection_resched: conn %lu closed by other thread.\n",
1749 conn->c_connid, 0, 0 );
1751 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1752 "closed by other thread conn=%lu sd=%d\n",
1753 conn->c_connid, sd, 0 );
1757 LDAP_LOG( CONNECTION, DETAIL1,
1758 "connection_resched: conn %lu attempting closing.\n",
1759 conn->c_connid, 0, 0 );
1761 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1762 "attempting closing conn=%lu sd=%d\n",
1763 conn->c_connid, sd, 0 );
1765 connection_close( conn );
1768 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1772 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1773 /* other states need different handling */
1777 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1778 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1781 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1782 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1783 /* pending operations should not be marked for abandonment */
1784 assert(!op->o_abandon);
1786 conn->c_n_ops_pending--;
1787 conn->c_n_ops_executing++;
1789 connection_op_activate( op );
1791 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1798 static int connection_op_activate( Operation *op )
1801 ber_tag_t tag = op->o_tag;
1803 if(tag == LDAP_REQ_BIND) {
1804 op->o_conn->c_conn_state = SLAP_C_BINDING;
1807 if (!op->o_dn.bv_len) {
1808 op->o_authz = op->o_conn->c_authz;
1809 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1810 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1812 op->o_authtype = op->o_conn->c_authtype;
1813 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1815 if (!op->o_protocol) {
1816 op->o_protocol = op->o_conn->c_protocol
1817 ? op->o_conn->c_protocol : LDAP_VERSION3;
1819 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1820 && op->o_protocol > LDAP_VERSION2)
1822 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1825 op->o_connid = op->o_conn->c_connid;
1827 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1829 status = ldap_pvt_thread_pool_submit( &connection_pool,
1830 connection_operation, (void *) op );
1832 if ( status != 0 ) {
1834 LDAP_LOG( CONNECTION, ERR,
1835 "connection_op_activate: conn %lu thread pool submit failed.\n",
1836 op->o_connid, 0, 0 );
1838 Debug( LDAP_DEBUG_ANY,
1839 "ldap_pvt_thread_pool_submit: failed (%d) for conn=%lu\n",
1840 status, op->o_connid, 0 );
1842 /* should move op to pending list */
1848 int connection_write(ber_socket_t s)
1852 assert( connections != NULL );
1854 ldap_pvt_thread_mutex_lock( &connections_mutex );
1856 c = connection_get( s );
1858 slapd_clr_write( s, 0);
1862 LDAP_LOG( CONNECTION, ERR,
1863 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1865 Debug( LDAP_DEBUG_ANY,
1866 "connection_write(%ld): no connection!\n",
1869 slapd_remove(s, 1, 0);
1870 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1877 LDAP_LOG( CONNECTION, DETAIL1,
1878 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1880 Debug( LDAP_DEBUG_TRACE,
1881 "connection_write(%d): waking output for id=%lu\n",
1882 s, c->c_connid, 0 );
1884 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1886 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1887 slapd_set_read( s, 1 );
1889 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1890 slapd_set_write( s, 1 );
1892 connection_return( c );
1893 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1898 connection_fake_init(
1903 conn->c_connid = -1;
1904 conn->c_send_ldap_result = slap_send_ldap_result;
1905 conn->c_send_search_entry = slap_send_search_entry;
1906 conn->c_send_search_reference = slap_send_search_reference;
1907 conn->c_listener = (Listener *)&dummy_list;
1908 conn->c_peer_domain = slap_empty_bv;
1909 conn->c_peer_name = slap_empty_bv;
1911 /* set memory context */
1912 op->o_tmpmemctx = sl_mem_create( SLMALLOC_SLAB_SIZE, ctx );
1913 op->o_tmpmfuncs = &sl_mfuncs;
1914 op->o_threadctx = ctx;
1917 op->o_connid = op->o_conn->c_connid;
1919 op->o_time = slap_get_time();
1923 connection_assign_nextid( Connection *conn )
1925 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1926 conn->c_connid = conn_nextid++;
1927 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );