2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 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>
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)
95 assert( connections == NULL );
97 if( connections != NULL) {
99 LDAP_LOG( CONNECTION, INFO,
100 "connections_init: already initialized.\n", 0, 0, 0 );
102 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
108 /* should check return of every call */
109 ldap_pvt_thread_mutex_init( &connections_mutex );
111 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
113 if( connections == NULL ) {
115 LDAP_LOG( CONNECTION, ERR,
116 "connections_init: allocation (%d * %ld) of connection "
117 "array failed\n", dtblsize, (long) sizeof(Connection), 0 );
119 Debug( LDAP_DEBUG_ANY,
120 "connections_init: allocation (%d*%ld) of connection array failed\n",
121 dtblsize, (long) sizeof(Connection), 0 );
126 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
127 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
130 * per entry initialization of the Connection array initialization
131 * will be done by connection_init()
138 * Destroy connection management infrastructure.
140 int connections_destroy(void)
144 /* should check return of every call */
146 if( connections == NULL) {
148 LDAP_LOG( CONNECTION, INFO,
149 "connections_destroy: nothing to destroy.\n", 0, 0, 0 );
151 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
157 for ( i = 0; i < dtblsize; i++ ) {
158 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
159 ber_sockbuf_free( connections[i].c_sb );
160 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
161 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
162 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
164 if ( slapi_plugins_used ) {
165 slapi_x_free_object_extensions( SLAPI_X_EXT_CONNECTION, &connections[i] );
174 ldap_pvt_thread_mutex_destroy( &connections_mutex );
179 * shutdown all connections
181 int connections_shutdown(void)
185 ldap_pvt_thread_mutex_lock( &connections_mutex );
187 for ( i = 0; i < dtblsize; i++ ) {
188 if( connections[i].c_struct_state != SLAP_C_USED ) {
191 /* give persistent clients a chance to cleanup */
192 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
193 ldap_pvt_thread_pool_submit( &connection_pool,
194 connections[i].c_clientfunc, connections[i].c_clientarg );
198 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
200 /* connections_mutex and c_mutex are locked */
201 connection_closing( &connections[i] );
202 connection_close( &connections[i] );
204 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
207 ldap_pvt_thread_mutex_unlock( &connections_mutex );
213 * Timeout idle connections.
215 int connections_timeout_idle(time_t now)
221 for( c = connection_first( &connindex );
223 c = connection_next( c, &connindex ) )
225 /* Don't timeout a slow-running request or a persistent
226 * outbound connection */
227 if( c->c_n_ops_executing ||
228 c->c_conn_state == SLAP_C_CLIENT ) continue;
230 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
232 connection_closing( c );
233 connection_close( c );
237 connection_done( c );
242 static Connection* connection_get( ber_socket_t s )
244 /* connections_mutex should be locked by caller */
249 LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
251 Debug( LDAP_DEBUG_ARGS,
252 "connection_get(%ld)\n",
256 assert( connections != NULL );
258 if(s == AC_SOCKET_INVALID) {
265 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
272 for(i=0; i<dtblsize; i++) {
273 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
274 assert( connections[i].c_conn_state == SLAP_C_INVALID );
275 assert( connections[i].c_sb == 0 );
279 ber_sockbuf_ctrl( connections[i].c_sb,
280 LBER_SB_OPT_GET_FD, &sd );
282 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
283 assert( connections[i].c_conn_state == SLAP_C_INVALID );
284 assert( sd == AC_SOCKET_INVALID );
288 /* state can actually change from used -> unused by resched,
289 * so don't assert details here.
303 ldap_pvt_thread_mutex_lock( &c->c_mutex );
305 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
306 if( c->c_struct_state != SLAP_C_USED ) {
307 /* connection must have been closed due to resched */
309 assert( c->c_conn_state == SLAP_C_INVALID );
310 assert( sd == AC_SOCKET_INVALID );
313 LDAP_LOG( CONNECTION, ARGS,
314 "connection_get: connection %d not used\n", s, 0, 0 );
316 Debug( LDAP_DEBUG_TRACE,
317 "connection_get(%d): connection not used\n",
321 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
324 if( c->c_conn_state == SLAP_C_CLIENT ) sd = 0;
327 LDAP_LOG( CONNECTION, RESULTS,
328 "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
330 Debug( LDAP_DEBUG_TRACE,
331 "connection_get(%d): got connid=%lu\n",
337 assert( c->c_struct_state == SLAP_C_USED );
338 assert( c->c_conn_state != SLAP_C_INVALID );
339 assert( sd != AC_SOCKET_INVALID );
342 c->c_activitytime = slap_get_time();
344 if( global_idletimeout > 0 ) {
345 c->c_activitytime = slap_get_time();
353 static void connection_return( Connection *c )
355 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
358 long connection_init(
362 const char* peername,
370 assert( connections != NULL );
372 assert( listener != NULL );
373 assert( dnsname != NULL );
374 assert( peername != NULL );
377 assert( flags != CONN_IS_TLS );
380 if( s == AC_SOCKET_INVALID ) {
382 LDAP_LOG( CONNECTION, INFO,
383 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
385 Debug( LDAP_DEBUG_ANY,
386 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
393 assert( s < dtblsize );
396 ldap_pvt_thread_mutex_lock( &connections_mutex );
406 for( i=0; i < dtblsize; i++) {
409 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
410 assert( connections[i].c_sb == 0 );
415 sd = AC_SOCKET_INVALID;
416 if (connections[i].c_sb != NULL) {
417 ber_sockbuf_ctrl( connections[i].c_sb,
418 LBER_SB_OPT_GET_FD, &sd );
421 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
422 assert( sd == AC_SOCKET_INVALID );
427 assert( connections[i].c_struct_state == SLAP_C_USED );
428 assert( connections[i].c_conn_state != SLAP_C_INVALID );
429 assert( sd != AC_SOCKET_INVALID );
434 LDAP_LOG( CONNECTION, INFO,
435 "connection_init(%d): connection table full "
436 "(%d/%d)\n", s, i, dtblsize );
438 Debug( LDAP_DEBUG_ANY,
439 "connection_init(%d): connection table full "
440 "(%d/%d)\n", s, i, dtblsize);
442 ldap_pvt_thread_mutex_unlock( &connections_mutex );
450 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
451 c->c_send_ldap_result = slap_send_ldap_result;
452 c->c_send_search_entry = slap_send_search_entry;
453 c->c_send_search_reference = slap_send_search_reference;
454 c->c_send_ldap_extended = slap_send_ldap_extended;
455 #ifdef LDAP_RES_INTERMEDIATE
456 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
459 c->c_authmech.bv_val = NULL;
460 c->c_authmech.bv_len = 0;
461 c->c_dn.bv_val = NULL;
463 c->c_ndn.bv_val = NULL;
466 c->c_listener = NULL;
467 c->c_peer_domain.bv_val = NULL;
468 c->c_peer_domain.bv_len = 0;
469 c->c_peer_name.bv_val = NULL;
470 c->c_peer_name.bv_len = 0;
472 LDAP_STAILQ_INIT(&c->c_ops);
473 LDAP_STAILQ_INIT(&c->c_pending_ops);
475 c->c_sasl_bind_mech.bv_val = NULL;
476 c->c_sasl_bind_mech.bv_len = 0;
478 c->c_sasl_authctx = NULL;
479 c->c_sasl_sockctx = NULL;
480 c->c_sasl_extra = NULL;
481 c->c_sasl_bindop = NULL;
483 c->c_sb = ber_sockbuf_alloc( );
486 ber_len_t max = sockbuf_max_incoming;
487 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
490 c->c_currentber = NULL;
492 /* should check status of thread calls */
493 ldap_pvt_thread_mutex_init( &c->c_mutex );
494 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
495 ldap_pvt_thread_cond_init( &c->c_write_cv );
498 if ( slapi_plugins_used ) {
499 slapi_x_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
503 c->c_struct_state = SLAP_C_UNUSED;
506 ldap_pvt_thread_mutex_lock( &c->c_mutex );
508 assert( c->c_struct_state == SLAP_C_UNUSED );
509 assert( c->c_authmech.bv_val == NULL );
510 assert( c->c_dn.bv_val == NULL );
511 assert( c->c_ndn.bv_val == NULL );
512 assert( c->c_listener == NULL );
513 assert( c->c_peer_domain.bv_val == NULL );
514 assert( c->c_peer_name.bv_val == NULL );
515 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
516 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
517 assert( c->c_sasl_bind_mech.bv_val == NULL );
518 assert( c->c_sasl_done == 0 );
519 assert( c->c_sasl_authctx == NULL );
520 assert( c->c_sasl_sockctx == NULL );
521 assert( c->c_sasl_extra == NULL );
522 assert( c->c_sasl_bindop == NULL );
523 assert( c->c_currentber == NULL );
524 assert( c->c_writewaiter == 0);
526 c->c_listener = listener;
528 if ( flags == CONN_IS_CLIENT ) {
529 c->c_conn_state = SLAP_C_CLIENT;
530 c->c_struct_state = SLAP_C_USED;
531 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
532 ldap_pvt_thread_mutex_unlock( &connections_mutex );
537 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
538 ber_str2bv( peername, 0, 1, &c->c_peer_name );
540 c->c_n_ops_received = 0;
541 c->c_n_ops_executing = 0;
542 c->c_n_ops_pending = 0;
543 c->c_n_ops_completed = 0;
549 /* set to zero until bind, implies LDAP_VERSION3 */
553 c->c_activitytime = c->c_starttime = slap_get_time();
555 if( global_idletimeout > 0 ) {
556 c->c_activitytime = c->c_starttime = slap_get_time();
560 #ifdef LDAP_CONNECTIONLESS
562 if( flags == CONN_IS_UDP ) {
565 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
566 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
568 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
569 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
570 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
571 LBER_SBIOD_LEVEL_PROVIDER, NULL );
576 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
577 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
579 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
580 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
584 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
585 INT_MAX, (void*)"ldap_" );
588 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
589 c /* non-NULL */ ) < 0 )
592 LDAP_LOG( CONNECTION, INFO,
593 "connection_init: conn %lu set nonblocking failed\n",
596 Debug( LDAP_DEBUG_ANY,
597 "connection_init(%d, %s): set nonblocking failed\n",
598 s, c->c_peer_name.bv_val, 0 );
602 id = c->c_connid = conn_nextid++;
604 c->c_conn_state = SLAP_C_INACTIVE;
605 c->c_struct_state = SLAP_C_USED;
607 c->c_ssf = c->c_transport_ssf = ssf;
611 if ( flags == CONN_IS_TLS ) {
613 c->c_needs_tls_accept = 1;
616 c->c_needs_tls_accept = 0;
620 slap_sasl_open( c, 0 );
621 slap_sasl_external( c, ssf, authid );
623 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
624 ldap_pvt_thread_mutex_unlock( &connections_mutex );
626 backend_connection_init(c);
631 void connection2anonymous( Connection *c )
633 assert( connections != NULL );
637 ber_len_t max = sockbuf_max_incoming;
638 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
641 if(c->c_authmech.bv_val != NULL ) {
642 free(c->c_authmech.bv_val);
643 c->c_authmech.bv_val = NULL;
645 c->c_authmech.bv_len = 0;
647 if(c->c_dn.bv_val != NULL) {
648 free(c->c_dn.bv_val);
649 c->c_dn.bv_val = NULL;
652 if(c->c_ndn.bv_val != NULL) {
653 free(c->c_ndn.bv_val);
654 c->c_ndn.bv_val = NULL;
658 c->c_authz_backend = NULL;
662 connection_destroy( Connection *c )
664 /* note: connections_mutex should be locked by caller */
666 unsigned long connid;
668 assert( connections != NULL );
670 assert( c->c_struct_state != SLAP_C_UNUSED );
671 assert( c->c_conn_state != SLAP_C_INVALID );
672 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
673 assert( c->c_writewaiter == 0);
675 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
676 connid = c->c_connid;
678 backend_connection_destroy(c);
683 c->c_activitytime = c->c_starttime = 0;
685 connection2anonymous( c );
686 c->c_listener = NULL;
688 if(c->c_peer_domain.bv_val != NULL) {
689 free(c->c_peer_domain.bv_val);
690 c->c_peer_domain.bv_val = NULL;
692 c->c_peer_domain.bv_len = 0;
693 if(c->c_peer_name.bv_val != NULL) {
694 free(c->c_peer_name.bv_val);
695 c->c_peer_name.bv_val = NULL;
697 c->c_peer_name.bv_len = 0;
699 c->c_sasl_bind_in_progress = 0;
700 if(c->c_sasl_bind_mech.bv_val != NULL) {
701 free(c->c_sasl_bind_mech.bv_val);
702 c->c_sasl_bind_mech.bv_val = NULL;
704 c->c_sasl_bind_mech.bv_len = 0;
706 slap_sasl_close( c );
708 if ( c->c_currentber != NULL ) {
709 ber_free( c->c_currentber, 1 );
710 c->c_currentber = NULL;
713 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
714 if ( sd != AC_SOCKET_INVALID ) {
715 slapd_remove( sd, 1, 0 );
717 Statslog( LDAP_DEBUG_STATS,
718 "conn=%lu fd=%ld closed\n",
719 connid, (long) sd, 0, 0, 0 );
722 ber_sockbuf_free( c->c_sb );
724 c->c_sb = ber_sockbuf_alloc( );
727 ber_len_t max = sockbuf_max_incoming;
728 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
731 c->c_conn_state = SLAP_C_INVALID;
732 c->c_struct_state = SLAP_C_UNUSED;
735 /* call destructors, then constructors; avoids unnecessary allocation */
736 if ( slapi_plugins_used ) {
737 slapi_x_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
742 int connection_state_closing( Connection *c )
744 /* c_mutex must be locked by caller */
748 assert( c->c_struct_state == SLAP_C_USED );
750 state = c->c_conn_state;
752 assert( state != SLAP_C_INVALID );
754 return state == SLAP_C_CLOSING;
757 static void connection_abandon( Connection *c )
759 /* c_mutex must be locked by caller */
763 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
767 /* remove pending operations */
768 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
769 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
770 LDAP_STAILQ_NEXT(o, o_next) = NULL;
775 void connection_closing( Connection *c )
777 assert( connections != NULL );
779 assert( c->c_struct_state == SLAP_C_USED );
780 assert( c->c_conn_state != SLAP_C_INVALID );
782 /* c_mutex must be locked by caller */
784 if( c->c_conn_state != SLAP_C_CLOSING ) {
787 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
789 LDAP_LOG( CONNECTION, DETAIL1,
790 "connection_closing: conn %lu readying socket %d for close.\n",
791 c->c_connid, sd, 0 );
793 Debug( LDAP_DEBUG_TRACE,
794 "connection_closing: readying conn=%lu sd=%d for close\n",
795 c->c_connid, sd, 0 );
797 /* update state to closing */
798 c->c_conn_state = SLAP_C_CLOSING;
800 /* don't listen on this port anymore */
801 slapd_clr_read( sd, 1 );
803 /* abandon active operations */
804 connection_abandon( c );
806 /* wake write blocked operations */
807 slapd_clr_write( sd, 1 );
808 ldap_pvt_thread_cond_signal( &c->c_write_cv );
812 static void connection_close( Connection *c )
816 assert( connections != NULL );
818 assert( c->c_struct_state == SLAP_C_USED );
819 assert( c->c_conn_state == SLAP_C_CLOSING );
821 /* note: connections_mutex and c_mutex should be locked by caller */
823 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
824 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
826 LDAP_LOG( CONNECTION, DETAIL1,
827 "connection_close: conn %lu deferring sd %d\n",
828 c->c_connid, sd, 0 );
830 Debug( LDAP_DEBUG_TRACE,
831 "connection_close: deferring conn=%lu sd=%d\n",
832 c->c_connid, sd, 0 );
838 LDAP_LOG( CONNECTION, RESULTS,
839 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
841 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
842 c->c_connid, sd, 0 );
844 connection_destroy( c );
847 unsigned long connections_nextid(void)
850 assert( connections != NULL );
852 ldap_pvt_thread_mutex_lock( &connections_mutex );
856 ldap_pvt_thread_mutex_unlock( &connections_mutex );
861 Connection* connection_first( ber_socket_t *index )
863 assert( connections != NULL );
864 assert( index != NULL );
866 ldap_pvt_thread_mutex_lock( &connections_mutex );
870 return connection_next(NULL, index);
873 Connection* connection_next( Connection *c, ber_socket_t *index )
875 assert( connections != NULL );
876 assert( index != NULL );
877 assert( *index <= dtblsize );
880 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
885 for(; *index < dtblsize; (*index)++) {
886 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
887 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
895 if( connections[*index].c_struct_state == SLAP_C_USED ) {
896 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
897 c = &connections[(*index)++];
901 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
902 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
906 ldap_pvt_thread_mutex_lock( &c->c_mutex );
912 void connection_done( Connection *c )
914 assert( connections != NULL );
917 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
920 ldap_pvt_thread_mutex_unlock( &connections_mutex );
924 * connection_activity - handle the request operation op on connection
925 * conn. This routine figures out what kind of operation it is and
926 * calls the appropriate stub to handle it.
930 #define INCR_OP(var,index) \
932 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
934 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
936 #else /* !SLAPD_MONITOR */
937 #define INCR_OP(var,index)
938 #endif /* !SLAPD_MONITOR */
941 connection_operation( void *ctx, void *arg_v )
943 int rc = SLAPD_DISCONNECT;
944 Operation *op = arg_v;
945 SlapReply rs = {REP_RESULT};
946 ber_tag_t tag = op->o_tag;
948 ber_tag_t oldtag = tag;
949 #endif /* SLAPD_MONITOR */
950 Connection *conn = op->o_conn;
952 void *memctx_null = NULL;
955 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
957 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
959 op->o_threadctx = ctx;
961 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
963 LDAP_LOG( CONNECTION, ERR,
964 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
965 conn->c_connid, (long)tag, 0 );
967 Debug( LDAP_DEBUG_ANY, "connection_operation: "
968 "error: SASL bind in progress (tag=%ld).\n",
971 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
972 "SASL bind in progress" );
973 goto operations_error;
976 /* We can use Thread-Local storage for most mallocs. We can
977 * also use TL for ber parsing, but not on Add or Modify.
979 #define SLAB_SIZE 1048576
981 memsiz = ber_len( op->o_ber ) * 64;
982 if ( SLAB_SIZE > memsiz ) memsiz = SLAB_SIZE;
986 memctx = sl_mem_create( memsiz, ctx );
987 op->o_tmpmemctx = memctx;
988 op->o_tmpmfuncs = &sl_mfuncs;
989 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
990 /* Note - the ber and its buffer are already allocated from
991 * regular memory; this only affects subsequent mallocs that
992 * ber_scanf may invoke.
994 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
999 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
1000 rc = do_bind( op, &rs );
1003 case LDAP_REQ_UNBIND:
1004 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
1005 rc = do_unbind( op, &rs );
1009 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
1010 rc = do_add( op, &rs );
1013 case LDAP_REQ_DELETE:
1014 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
1015 rc = do_delete( op, &rs );
1018 case LDAP_REQ_MODRDN:
1019 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
1020 rc = do_modrdn( op, &rs );
1023 case LDAP_REQ_MODIFY:
1024 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
1025 rc = do_modify( op, &rs );
1028 case LDAP_REQ_COMPARE:
1029 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
1030 rc = do_compare( op, &rs );
1033 case LDAP_REQ_SEARCH:
1034 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
1035 rc = do_search( op, &rs );
1038 case LDAP_REQ_ABANDON:
1039 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
1040 rc = do_abandon( op, &rs );
1043 case LDAP_REQ_EXTENDED:
1044 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
1045 rc = do_extended( op, &rs );
1050 LDAP_LOG( CONNECTION, INFO,
1051 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
1052 conn->c_connid, tag, 0 );
1054 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
1057 op->o_tag = LBER_ERROR;
1058 rs.sr_err = LDAP_PROTOCOL_ERROR;
1059 rs.sr_text = "unknown LDAP request";
1060 send_ldap_disconnect( op, &rs );
1065 #ifdef SLAPD_MONITOR
1067 #endif /* SLAPD_MONITOR */
1068 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1071 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1072 num_ops_completed++;
1073 #ifdef SLAPD_MONITOR
1076 num_ops_completed_[SLAP_OP_BIND]++;
1078 case LDAP_REQ_UNBIND:
1079 num_ops_completed_[SLAP_OP_UNBIND]++;
1082 num_ops_completed_[SLAP_OP_ADD]++;
1084 case LDAP_REQ_DELETE:
1085 num_ops_completed_[SLAP_OP_DELETE]++;
1087 case LDAP_REQ_MODRDN:
1088 num_ops_completed_[SLAP_OP_MODRDN]++;
1090 case LDAP_REQ_MODIFY:
1091 num_ops_completed_[SLAP_OP_MODIFY]++;
1093 case LDAP_REQ_COMPARE:
1094 num_ops_completed_[SLAP_OP_COMPARE]++;
1096 case LDAP_REQ_SEARCH:
1097 num_ops_completed_[SLAP_OP_SEARCH]++;
1099 case LDAP_REQ_ABANDON:
1100 num_ops_completed_[SLAP_OP_ABANDON]++;
1102 case LDAP_REQ_EXTENDED:
1103 num_ops_completed_[SLAP_OP_EXTENDED]++;
1106 #endif /* SLAPD_MONITOR */
1107 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1109 #ifdef LDAP_EXOP_X_CANCEL
1110 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1111 op->o_cancel = LDAP_TOO_LATE;
1114 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1115 op->o_cancel != SLAP_CANCEL_DONE )
1117 ldap_pvt_thread_yield();
1121 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1123 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1125 if ( op->o_cancel != SLAP_CANCEL_ACK &&
1126 ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) {
1127 sl_mem_detach( ctx, memctx );
1128 } else if (( op->o_sync_slog_size != -1 )) {
1129 sl_mem_detach( ctx, memctx );
1130 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1131 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1132 conn->c_n_ops_executing--;
1133 conn->c_n_ops_completed++;
1135 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1136 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1138 conn->c_n_ops_executing--;
1139 conn->c_n_ops_completed++;
1144 case LDAP_REQ_UNBIND:
1145 /* c_mutex is locked */
1146 connection_closing( conn );
1150 conn->c_sasl_bind_in_progress =
1151 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1153 if( conn->c_conn_state == SLAP_C_BINDING) {
1154 conn->c_conn_state = SLAP_C_ACTIVE;
1158 connection_resched( conn );
1160 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1165 int connection_client_setup(
1168 ldap_pvt_thread_start_t *func,
1173 if ( connection_init( s, l, "", "", CONN_IS_CLIENT, 0, "" ) < 0 ) {
1177 c = connection_get( s );
1178 c->c_clientfunc = func;
1179 c->c_clientarg = arg;
1180 connection_return( c );
1181 slapd_add_internal( s, 0 );
1182 slapd_set_read( s, 1 );
1186 void connection_client_enable(
1190 slapd_set_read( s, 1 );
1193 void connection_client_stop(
1199 /* get (locked) connection */
1200 c = connection_get( s );
1202 assert( c->c_conn_state == SLAP_C_CLIENT );
1204 c->c_listener = NULL;
1205 c->c_conn_state = SLAP_C_INVALID;
1206 c->c_struct_state = SLAP_C_UNUSED;
1207 connection_return( c );
1208 slapd_remove( s, 0, 1 );
1211 int connection_read(ber_socket_t s)
1216 assert( connections != NULL );
1218 ldap_pvt_thread_mutex_lock( &connections_mutex );
1220 /* get (locked) connection */
1221 c = connection_get( s );
1225 LDAP_LOG( CONNECTION, INFO,
1226 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1228 Debug( LDAP_DEBUG_ANY,
1229 "connection_read(%ld): no connection!\n",
1232 slapd_remove(s, 1, 0);
1234 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1240 if( c->c_conn_state == SLAP_C_CLOSING ) {
1242 LDAP_LOG( CONNECTION, INFO,
1243 "connection_read: conn %lu connection closing, ignoring input\n",
1244 c->c_connid, 0, 0 );
1246 Debug( LDAP_DEBUG_TRACE,
1247 "connection_read(%d): closing, ignoring input for id=%lu\n",
1248 s, c->c_connid, 0 );
1250 connection_return( c );
1251 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1255 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1256 slapd_clr_read( s, 0 );
1257 ldap_pvt_thread_pool_submit( &connection_pool,
1258 c->c_clientfunc, c->c_clientarg );
1259 connection_return( c );
1260 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1265 LDAP_LOG( CONNECTION, DETAIL1,
1266 "connection_read: conn %lu checking for input.\n",
1267 c->c_connid, 0, 0 );
1269 Debug( LDAP_DEBUG_TRACE,
1270 "connection_read(%d): checking for input on id=%lu\n",
1271 s, c->c_connid, 0 );
1275 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1276 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1278 #if 0 /* required by next #if 0 */
1284 LDAP_LOG( CONNECTION, ERR,
1285 "connection_read: conn %lu TLS accept error, error %d\n",
1286 c->c_connid, rc, 0 );
1288 Debug( LDAP_DEBUG_TRACE,
1289 "connection_read(%d): TLS accept error "
1290 "error=%d id=%lu, closing\n",
1291 s, rc, c->c_connid );
1293 c->c_needs_tls_accept = 0;
1294 /* connections_mutex and c_mutex are locked */
1295 connection_closing( c );
1298 /* Drain input before close, to allow SSL error codes
1299 * to propagate to client. */
1305 rc = select(s+1, &rfd, NULL, NULL, &tv);
1307 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1311 connection_close( c );
1313 } else if ( rc == 0 ) {
1315 struct berval authid = { 0, NULL };
1317 c->c_needs_tls_accept = 0;
1319 /* we need to let SASL know */
1320 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1322 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1323 if( c->c_tls_ssf > c->c_ssf ) {
1324 c->c_ssf = c->c_tls_ssf;
1327 rc = dnX509peerNormalize( ssl, &authid );
1328 if ( rc != LDAP_SUCCESS ) {
1330 LDAP_LOG( CONNECTION, INFO,
1331 "connection_read: conn %lu unable to get TLS client DN, "
1332 "error %d\n", c->c_connid, rc, 0 );
1334 Debug( LDAP_DEBUG_TRACE,
1335 "connection_read(%d): unable to get TLS client DN "
1336 "error=%d id=%lu\n",
1337 s, rc, c->c_connid );
1340 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1341 if ( authid.bv_val ) free( authid.bv_val );
1344 /* if success and data is ready, fall thru to data input loop */
1346 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1348 connection_return( c );
1349 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1355 #ifdef HAVE_CYRUS_SASL
1356 if ( c->c_sasl_layers ) {
1357 /* If previous layer is not removed yet, give up for now */
1358 if ( !c->c_sasl_sockctx ) {
1359 connection_return( c );
1360 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1364 c->c_sasl_layers = 0;
1366 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1368 if( rc != LDAP_SUCCESS ) {
1370 LDAP_LOG( CONNECTION, ERR,
1371 "connection_read: conn %lu SASL install error %d, closing\n",
1372 c->c_connid, rc, 0 );
1374 Debug( LDAP_DEBUG_TRACE,
1375 "connection_read(%d): SASL install error "
1376 "error=%d id=%lu, closing\n",
1377 s, rc, c->c_connid );
1379 /* connections_mutex and c_mutex are locked */
1380 connection_closing( c );
1381 connection_close( c );
1382 connection_return( c );
1383 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1389 #define CONNECTION_INPUT_LOOP 1
1390 /* #define DATA_READY_LOOP 1 */
1394 /* How do we do this without getting into a busy loop ? */
1395 rc = connection_input( c );
1397 #ifdef DATA_READY_LOOP
1398 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) );
1399 #elif CONNECTION_INPUT_LOOP
1407 LDAP_LOG( CONNECTION, ERR,
1408 "connection_read: conn %lu input error %d, closing.\n",
1409 c->c_connid, rc, 0 );
1411 Debug( LDAP_DEBUG_TRACE,
1412 "connection_read(%d): input error=%d id=%lu, closing.\n",
1413 s, rc, c->c_connid );
1415 /* connections_mutex and c_mutex are locked */
1416 connection_closing( c );
1417 connection_close( c );
1418 connection_return( c );
1419 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1423 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1424 slapd_set_read( s, 1 );
1427 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1428 slapd_set_write( s, 1 );
1431 connection_return( c );
1432 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1447 #ifdef LDAP_CONNECTIONLESS
1452 if ( conn->c_currentber == NULL &&
1453 ( conn->c_currentber = ber_alloc()) == NULL )
1456 LDAP_LOG( CONNECTION, ERR,
1457 "connection_input: conn %lu ber_alloc failed.\n",
1458 conn->c_connid, 0, 0 );
1460 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1467 #ifdef LDAP_CONNECTIONLESS
1468 if ( conn->c_is_udp ) {
1469 char peername[sizeof("IP=255.255.255.255:65336")];
1470 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1471 sizeof(struct sockaddr));
1472 if (len != sizeof(struct sockaddr))
1474 sprintf( peername, "IP=%s:%d",
1475 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1476 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1477 Statslog( LDAP_DEBUG_STATS,
1478 "conn=%lu UDP request from %s (%s) accepted.\n",
1479 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1482 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1483 if ( tag != LDAP_TAG_MESSAGE ) {
1487 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1490 LDAP_LOG( CONNECTION, ERR,
1491 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1492 conn->c_connid, err, sock_errstr(err) );
1494 Debug( LDAP_DEBUG_TRACE,
1495 "ber_get_next on fd %d failed errno=%d (%s)\n",
1496 sd, err, sock_errstr(err) );
1498 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1499 /* log, close and send error */
1500 ber_free( conn->c_currentber, 1 );
1501 conn->c_currentber = NULL;
1508 ber = conn->c_currentber;
1509 conn->c_currentber = NULL;
1511 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1512 /* log, close and send error */
1514 LDAP_LOG( CONNECTION, ERR,
1515 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1516 conn->c_connid, tag, 0 );
1518 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n",
1525 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1526 /* log, close and send error */
1528 LDAP_LOG( CONNECTION, ERR,
1529 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1530 conn->c_connid, tag, 0 );
1532 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n",
1540 #ifdef LDAP_CONNECTIONLESS
1541 if( conn->c_is_udp ) {
1542 if( tag == LBER_OCTETSTRING ) {
1543 ber_get_stringa( ber, &cdn );
1544 tag = ber_peek_tag(ber, &len);
1546 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1548 LDAP_LOG( CONNECTION, ERR,
1549 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1550 conn->c_connid, tag, 0 );
1552 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1559 if(tag == LDAP_REQ_BIND) {
1560 /* immediately abandon all exiting operations upon BIND */
1561 connection_abandon( conn );
1564 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1567 op->o_assertion = NULL;
1568 op->o_preread_attrs = NULL;
1569 op->o_postread_attrs = NULL;
1570 op->o_vrFilter = NULL;
1572 #ifdef LDAP_CONTROL_PAGEDRESULTS
1573 op->o_pagedresults_state = conn->c_pagedresults_state;
1576 op->o_res_ber = NULL;
1578 #ifdef LDAP_CONNECTIONLESS
1579 if (conn->c_is_udp) {
1581 ber_str2bv( cdn, 0, 1, &op->o_dn );
1582 op->o_protocol = LDAP_VERSION2;
1584 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1585 if (op->o_res_ber == NULL) return 1;
1587 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1588 sizeof(struct sockaddr), 0 );
1590 if (rc != sizeof(struct sockaddr)) {
1592 LDAP_LOG( CONNECTION, INFO,
1593 "connection_input: conn %lu ber_write failed\n",
1594 conn->c_connid, 0, 0 );
1596 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1601 if (op->o_protocol == LDAP_VERSION2) {
1602 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1605 LDAP_LOG( CONNECTION, INFO,
1606 "connection_input: conn %lu put outer sequence failed\n",
1607 conn->c_connid, 0, 0 );
1609 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1615 #endif /* LDAP_CONNECTIONLESS */
1619 /* Don't process requests when the conn is in the middle of a
1620 * Bind, or if it's closing. Also, don't let any single conn
1621 * use up all the available threads, and don't execute if we're
1622 * currently blocked on output. And don't execute if there are
1623 * already pending ops, let them go first.
1625 * But always allow Abandon through; it won't cost much.
1627 if ( tag != LDAP_REQ_ABANDON && (conn->c_conn_state == SLAP_C_BINDING
1628 || conn->c_conn_state == SLAP_C_CLOSING
1629 || conn->c_n_ops_executing >= connection_pool_max/2
1630 || conn->c_n_ops_pending
1631 || conn->c_writewaiter))
1633 int max = conn->c_dn.bv_len
1634 ? slap_conn_max_pending_auth
1635 : slap_conn_max_pending;
1638 LDAP_LOG( CONNECTION, INFO,
1639 "connection_input: conn %lu deferring operation\n",
1640 conn->c_connid, 0, 0 );
1642 Debug( LDAP_DEBUG_ANY,
1643 "connection_input: conn=%lu deferring operation\n",
1644 conn->c_connid, 0, 0 );
1646 conn->c_n_ops_pending++;
1647 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1648 if ( conn->c_n_ops_pending > max ) {
1654 conn->c_n_ops_executing++;
1655 connection_op_activate( op );
1659 if ( conn->c_struct_state != SLAP_C_USED ) {
1660 /* connection must have got closed underneath us */
1664 assert( conn->c_struct_state == SLAP_C_USED );
1670 connection_resched( Connection *conn )
1674 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1677 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1679 /* us trylock to avoid possible deadlock */
1680 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1684 LDAP_LOG( CONNECTION, DETAIL1,
1685 "connection_resched: conn %lu reaquiring locks.\n",
1686 conn->c_connid, 0, 0 );
1688 Debug( LDAP_DEBUG_TRACE,
1689 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1690 conn->c_connid, sd, 0 );
1693 * reaquire locks in the right order...
1694 * this may allow another thread to close this connection,
1695 * so recheck state below.
1697 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1698 ldap_pvt_thread_mutex_lock( &connections_mutex );
1699 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1702 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1704 LDAP_LOG( CONNECTION, INFO,
1705 "connection_resched: conn %lu closed by other thread.\n",
1706 conn->c_connid, 0, 0 );
1708 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1709 "closed by other thread conn=%lu sd=%d\n",
1710 conn->c_connid, sd, 0 );
1714 LDAP_LOG( CONNECTION, DETAIL1,
1715 "connection_resched: conn %lu attempting closing.\n",
1716 conn->c_connid, 0, 0 );
1718 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1719 "attempting closing conn=%lu sd=%d\n",
1720 conn->c_connid, sd, 0 );
1722 connection_close( conn );
1725 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1729 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1730 /* other states need different handling */
1734 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1735 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1738 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1739 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1740 /* pending operations should not be marked for abandonment */
1741 assert(!op->o_abandon);
1743 conn->c_n_ops_pending--;
1744 conn->c_n_ops_executing++;
1746 connection_op_activate( op );
1748 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1755 static int connection_op_activate( Operation *op )
1758 ber_tag_t tag = op->o_tag;
1760 if(tag == LDAP_REQ_BIND) {
1761 op->o_conn->c_conn_state = SLAP_C_BINDING;
1764 if (!op->o_dn.bv_len) {
1765 op->o_authz = op->o_conn->c_authz;
1766 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1767 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1769 op->o_authtype = op->o_conn->c_authtype;
1770 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1772 if (!op->o_protocol) {
1773 op->o_protocol = op->o_conn->c_protocol
1774 ? op->o_conn->c_protocol : LDAP_VERSION3;
1776 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1777 && op->o_protocol > LDAP_VERSION2)
1779 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1782 op->o_connid = op->o_conn->c_connid;
1784 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1786 status = ldap_pvt_thread_pool_submit( &connection_pool,
1787 connection_operation, (void *) op );
1789 if ( status != 0 ) {
1791 LDAP_LOG( CONNECTION, ERR,
1792 "connection_op_activate: conn %lu thread pool submit failed.\n",
1793 op->o_connid, 0, 0 );
1795 Debug( LDAP_DEBUG_ANY,
1796 "ldap_pvt_thread_pool_submit: failed (%d) for conn=%lu\n",
1797 status, op->o_connid, 0 );
1799 /* should move op to pending list */
1805 int connection_write(ber_socket_t s)
1809 assert( connections != NULL );
1811 ldap_pvt_thread_mutex_lock( &connections_mutex );
1813 c = connection_get( s );
1815 slapd_clr_write( s, 0);
1819 LDAP_LOG( CONNECTION, ERR,
1820 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1822 Debug( LDAP_DEBUG_ANY,
1823 "connection_write(%ld): no connection!\n",
1826 slapd_remove(s, 1, 0);
1827 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1834 LDAP_LOG( CONNECTION, DETAIL1,
1835 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1837 Debug( LDAP_DEBUG_TRACE,
1838 "connection_write(%d): waking output for id=%lu\n",
1839 s, c->c_connid, 0 );
1841 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1843 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1844 slapd_set_read( s, 1 );
1846 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1847 slapd_set_write( s, 1 );
1849 connection_return( c );
1850 ldap_pvt_thread_mutex_unlock( &connections_mutex );