3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/string.h>
16 #include <ac/unistd.h>
26 /* protected by connections_mutex */
27 static ldap_pvt_thread_mutex_t connections_mutex;
28 static Connection *connections = NULL;
29 static unsigned long conn_nextid = 0;
31 /* structure state (protected by connections_mutex) */
32 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
33 #define SLAP_C_UNUSED 0x01
34 #define SLAP_C_USED 0x02
36 /* connection state (protected by c_mutex ) */
37 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
38 #define SLAP_C_INACTIVE 0x01 /* zero threads */
39 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
40 #define SLAP_C_BINDING 0x03 /* binding */
41 #define SLAP_C_CLOSING 0x04 /* closing */
42 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
45 connection_state2str( int state )
48 case SLAP_C_INVALID: return "!";
49 case SLAP_C_INACTIVE: return "|";
50 case SLAP_C_ACTIVE: return "";
51 case SLAP_C_BINDING: return "B";
52 case SLAP_C_CLOSING: return "C";
53 case SLAP_C_CLIENT: return "L";
59 static Connection* connection_get( ber_socket_t s );
61 static int connection_input( Connection *c );
62 static void connection_close( Connection *c );
64 static int connection_op_activate( Operation *op );
65 static int connection_resched( Connection *conn );
66 static void connection_abandon( Connection *conn );
67 static void connection_destroy( Connection *c );
69 static ldap_pvt_thread_start_t connection_operation;
72 * Initialize connection management infrastructure.
74 int connections_init(void)
76 assert( connections == NULL );
78 if( connections != NULL) {
80 LDAP_LOG( CONNECTION, INFO,
81 "connections_init: already initialized.\n", 0, 0, 0 );
83 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
89 /* should check return of every call */
90 ldap_pvt_thread_mutex_init( &connections_mutex );
92 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
94 if( connections == NULL ) {
96 LDAP_LOG( CONNECTION, ERR,
97 "connections_init: allocation (%d * %ld) of connection "
98 "array failed\n", dtblsize, (long) sizeof(Connection), 0 );
100 Debug( LDAP_DEBUG_ANY,
101 "connections_init: allocation (%d*%ld) of connection array failed\n",
102 dtblsize, (long) sizeof(Connection), 0 );
107 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
108 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
111 * per entry initialization of the Connection array initialization
112 * will be done by connection_init()
119 * Destroy connection management infrastructure.
121 int connections_destroy(void)
125 /* should check return of every call */
127 if( connections == NULL) {
129 LDAP_LOG( CONNECTION, INFO,
130 "connections_destroy: nothing to destroy.\n", 0, 0, 0 );
132 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
138 for ( i = 0; i < dtblsize; i++ ) {
139 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
140 ber_sockbuf_free( connections[i].c_sb );
141 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
142 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
143 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
145 if ( slapi_plugins_used ) {
146 slapi_x_free_object_extensions( SLAPI_X_EXT_CONNECTION, &connections[i] );
155 ldap_pvt_thread_mutex_destroy( &connections_mutex );
160 * shutdown all connections
162 int connections_shutdown(void)
166 ldap_pvt_thread_mutex_lock( &connections_mutex );
168 for ( i = 0; i < dtblsize; i++ ) {
169 if( connections[i].c_struct_state != SLAP_C_USED ) {
172 /* give persistent clients a chance to cleanup */
173 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
174 ldap_pvt_thread_pool_submit( &connection_pool,
175 connections[i].c_clientfunc, connections[i].c_clientarg );
179 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
181 /* connections_mutex and c_mutex are locked */
182 connection_closing( &connections[i] );
183 connection_close( &connections[i] );
185 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
188 ldap_pvt_thread_mutex_unlock( &connections_mutex );
194 * Timeout idle connections.
196 int connections_timeout_idle(time_t now)
202 for( c = connection_first( &connindex );
204 c = connection_next( c, &connindex ) )
206 /* Don't timeout a slow-running request or a persistent
207 * outbound connection */
208 if( c->c_n_ops_executing ||
209 c->c_conn_state == SLAP_C_CLIENT ) continue;
211 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
213 connection_closing( c );
214 connection_close( c );
218 connection_done( c );
223 static Connection* connection_get( ber_socket_t s )
225 /* connections_mutex should be locked by caller */
230 LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
232 Debug( LDAP_DEBUG_ARGS,
233 "connection_get(%ld)\n",
237 assert( connections != NULL );
239 if(s == AC_SOCKET_INVALID) {
246 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
253 for(i=0; i<dtblsize; i++) {
254 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
255 assert( connections[i].c_conn_state == SLAP_C_INVALID );
256 assert( connections[i].c_sb == 0 );
260 ber_sockbuf_ctrl( connections[i].c_sb,
261 LBER_SB_OPT_GET_FD, &sd );
263 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
264 assert( connections[i].c_conn_state == SLAP_C_INVALID );
265 assert( sd == AC_SOCKET_INVALID );
269 /* state can actually change from used -> unused by resched,
270 * so don't assert details here.
284 ldap_pvt_thread_mutex_lock( &c->c_mutex );
286 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
287 if( c->c_struct_state != SLAP_C_USED ) {
288 /* connection must have been closed due to resched */
290 assert( c->c_conn_state == SLAP_C_INVALID );
291 assert( sd == AC_SOCKET_INVALID );
294 LDAP_LOG( CONNECTION, ARGS,
295 "connection_get: connection %d not used\n", s, 0, 0 );
297 Debug( LDAP_DEBUG_TRACE,
298 "connection_get(%d): connection not used\n",
302 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
305 if( c->c_conn_state == SLAP_C_CLIENT ) sd = 0;
308 LDAP_LOG( CONNECTION, RESULTS,
309 "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
311 Debug( LDAP_DEBUG_TRACE,
312 "connection_get(%d): got connid=%lu\n",
318 assert( c->c_struct_state == SLAP_C_USED );
319 assert( c->c_conn_state != SLAP_C_INVALID );
320 assert( sd != AC_SOCKET_INVALID );
323 c->c_activitytime = slap_get_time();
325 if( global_idletimeout > 0 ) {
326 c->c_activitytime = slap_get_time();
334 static void connection_return( Connection *c )
336 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
339 long connection_init(
343 const char* peername,
351 assert( connections != NULL );
353 assert( listener != NULL );
354 assert( dnsname != NULL );
355 assert( peername != NULL );
358 assert( flags != CONN_IS_TLS );
361 if( s == AC_SOCKET_INVALID ) {
363 LDAP_LOG( CONNECTION, INFO,
364 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
366 Debug( LDAP_DEBUG_ANY,
367 "connection_init(%ld): invalid.\n",
375 assert( s < dtblsize );
378 ldap_pvt_thread_mutex_lock( &connections_mutex );
389 for( i=0; i < dtblsize; i++) {
392 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
393 assert( connections[i].c_sb == 0 );
398 sd = AC_SOCKET_INVALID;
399 if (connections[i].c_sb != NULL)
400 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
402 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
403 assert( sd == AC_SOCKET_INVALID );
408 assert( connections[i].c_struct_state == SLAP_C_USED );
409 assert( connections[i].c_conn_state != SLAP_C_INVALID );
410 assert( sd != AC_SOCKET_INVALID );
415 LDAP_LOG( CONNECTION, INFO,
416 "connection_init: skt %d connection table full "
417 "(%d/%d)\n", s, i, dtblsize );
419 Debug( LDAP_DEBUG_ANY,
420 "connection_init(%d): connection table full "
421 "(%d/%d)\n", s, i, dtblsize);
423 ldap_pvt_thread_mutex_unlock( &connections_mutex );
431 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
432 c->c_send_ldap_result = slap_send_ldap_result;
433 c->c_send_search_entry = slap_send_search_entry;
434 c->c_send_search_reference = slap_send_search_reference;
435 c->c_send_ldap_extended = slap_send_ldap_extended;
436 #ifdef LDAP_RES_INTERMEDIATE
437 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
440 c->c_authmech.bv_val = NULL;
441 c->c_authmech.bv_len = 0;
442 c->c_dn.bv_val = NULL;
444 c->c_ndn.bv_val = NULL;
447 c->c_listener = NULL;
448 c->c_peer_domain.bv_val = NULL;
449 c->c_peer_domain.bv_len = 0;
450 c->c_peer_name.bv_val = NULL;
451 c->c_peer_name.bv_len = 0;
453 LDAP_STAILQ_INIT(&c->c_ops);
454 LDAP_STAILQ_INIT(&c->c_pending_ops);
456 c->c_sasl_bind_mech.bv_val = NULL;
457 c->c_sasl_bind_mech.bv_len = 0;
459 c->c_sasl_authctx = NULL;
460 c->c_sasl_sockctx = NULL;
461 c->c_sasl_extra = NULL;
462 c->c_sasl_bindop = NULL;
464 c->c_sb = ber_sockbuf_alloc( );
467 ber_len_t max = sockbuf_max_incoming;
468 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
471 c->c_currentber = NULL;
473 /* should check status of thread calls */
474 ldap_pvt_thread_mutex_init( &c->c_mutex );
475 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
476 ldap_pvt_thread_cond_init( &c->c_write_cv );
479 if ( slapi_plugins_used ) {
480 slapi_x_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
484 c->c_struct_state = SLAP_C_UNUSED;
487 ldap_pvt_thread_mutex_lock( &c->c_mutex );
489 assert( c->c_struct_state == SLAP_C_UNUSED );
490 assert( c->c_authmech.bv_val == NULL );
491 assert( c->c_dn.bv_val == NULL );
492 assert( c->c_ndn.bv_val == NULL );
493 assert( c->c_listener == NULL );
494 assert( c->c_peer_domain.bv_val == NULL );
495 assert( c->c_peer_name.bv_val == NULL );
496 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
497 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
498 assert( c->c_sasl_bind_mech.bv_val == NULL );
499 assert( c->c_sasl_done == 0 );
500 assert( c->c_sasl_authctx == NULL );
501 assert( c->c_sasl_sockctx == NULL );
502 assert( c->c_sasl_extra == NULL );
503 assert( c->c_sasl_bindop == NULL );
504 assert( c->c_currentber == NULL );
506 c->c_listener = listener;
508 if ( flags == CONN_IS_CLIENT ) {
509 c->c_conn_state = SLAP_C_CLIENT;
510 c->c_struct_state = SLAP_C_USED;
511 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
512 ldap_pvt_thread_mutex_unlock( &connections_mutex );
517 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
518 ber_str2bv( peername, 0, 1, &c->c_peer_name );
520 c->c_n_ops_received = 0;
521 c->c_n_ops_executing = 0;
522 c->c_n_ops_pending = 0;
523 c->c_n_ops_completed = 0;
529 /* set to zero until bind, implies LDAP_VERSION3 */
533 c->c_activitytime = c->c_starttime = slap_get_time();
535 if( global_idletimeout > 0 ) {
536 c->c_activitytime = c->c_starttime = slap_get_time();
540 #ifdef LDAP_CONNECTIONLESS
542 if( flags == CONN_IS_UDP ) {
545 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
546 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
548 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
549 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
550 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
551 LBER_SBIOD_LEVEL_PROVIDER, NULL );
556 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
557 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
559 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
560 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
564 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
565 INT_MAX, (void*)"ldap_" );
568 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
569 c /* non-NULL */ ) < 0 )
572 LDAP_LOG( CONNECTION, INFO,
573 "connection_init: conn %lu set nonblocking failed\n",
576 Debug( LDAP_DEBUG_ANY,
577 "connection_init(%d, %s): set nonblocking failed\n",
578 s, c->c_peer_name.bv_val, 0 );
582 id = c->c_connid = conn_nextid++;
584 c->c_conn_state = SLAP_C_INACTIVE;
585 c->c_struct_state = SLAP_C_USED;
587 c->c_ssf = c->c_transport_ssf = ssf;
591 if ( flags == CONN_IS_TLS ) {
593 c->c_needs_tls_accept = 1;
596 c->c_needs_tls_accept = 0;
600 slap_sasl_open( c, 0 );
601 slap_sasl_external( c, ssf, authid );
603 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
604 ldap_pvt_thread_mutex_unlock( &connections_mutex );
606 backend_connection_init(c);
611 void connection2anonymous( Connection *c )
613 assert( connections != NULL );
617 ber_len_t max = sockbuf_max_incoming;
618 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
621 if(c->c_authmech.bv_val != NULL ) {
622 free(c->c_authmech.bv_val);
623 c->c_authmech.bv_val = NULL;
625 c->c_authmech.bv_len = 0;
627 if(c->c_dn.bv_val != NULL) {
628 free(c->c_dn.bv_val);
629 c->c_dn.bv_val = NULL;
632 if(c->c_ndn.bv_val != NULL) {
633 free(c->c_ndn.bv_val);
634 c->c_ndn.bv_val = NULL;
638 c->c_authz_backend = NULL;
642 connection_destroy( Connection *c )
644 /* note: connections_mutex should be locked by caller */
646 unsigned long connid;
648 assert( connections != NULL );
650 assert( c->c_struct_state != SLAP_C_UNUSED );
651 assert( c->c_conn_state != SLAP_C_INVALID );
652 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
654 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
655 connid = c->c_connid;
657 backend_connection_destroy(c);
662 c->c_activitytime = c->c_starttime = 0;
664 connection2anonymous( c );
665 c->c_listener = NULL;
667 if(c->c_peer_domain.bv_val != NULL) {
668 free(c->c_peer_domain.bv_val);
669 c->c_peer_domain.bv_val = NULL;
671 c->c_peer_domain.bv_len = 0;
672 if(c->c_peer_name.bv_val != NULL) {
673 free(c->c_peer_name.bv_val);
674 c->c_peer_name.bv_val = NULL;
676 c->c_peer_name.bv_len = 0;
678 c->c_sasl_bind_in_progress = 0;
679 if(c->c_sasl_bind_mech.bv_val != NULL) {
680 free(c->c_sasl_bind_mech.bv_val);
681 c->c_sasl_bind_mech.bv_val = NULL;
683 c->c_sasl_bind_mech.bv_len = 0;
685 slap_sasl_close( c );
687 if ( c->c_currentber != NULL ) {
688 ber_free( c->c_currentber, 1 );
689 c->c_currentber = NULL;
692 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
693 if ( sd != AC_SOCKET_INVALID ) {
694 slapd_remove( sd, 1, 0 );
696 Statslog( LDAP_DEBUG_STATS,
697 "conn=%lu fd=%ld closed\n",
698 connid, (long) sd, 0, 0, 0 );
701 ber_sockbuf_free( c->c_sb );
703 c->c_sb = ber_sockbuf_alloc( );
706 ber_len_t max = sockbuf_max_incoming;
707 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
710 c->c_conn_state = SLAP_C_INVALID;
711 c->c_struct_state = SLAP_C_UNUSED;
714 /* call destructors, then constructors; avoids unnecessary allocation */
715 if ( slapi_plugins_used ) {
716 slapi_x_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
721 int connection_state_closing( Connection *c )
723 /* c_mutex must be locked by caller */
727 assert( c->c_struct_state == SLAP_C_USED );
729 state = c->c_conn_state;
731 assert( state != SLAP_C_INVALID );
733 return state == SLAP_C_CLOSING;
736 static void connection_abandon( Connection *c )
738 /* c_mutex must be locked by caller */
742 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
746 /* remove pending operations */
747 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
748 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
749 LDAP_STAILQ_NEXT(o, o_next) = NULL;
754 void connection_closing( Connection *c )
756 assert( connections != NULL );
758 assert( c->c_struct_state == SLAP_C_USED );
759 assert( c->c_conn_state != SLAP_C_INVALID );
761 /* c_mutex must be locked by caller */
763 if( c->c_conn_state != SLAP_C_CLOSING ) {
766 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
768 LDAP_LOG( CONNECTION, DETAIL1,
769 "connection_closing: conn %lu readying socket %d for close.\n",
770 c->c_connid, sd, 0 );
772 Debug( LDAP_DEBUG_TRACE,
773 "connection_closing: readying conn=%lu sd=%d for close\n",
774 c->c_connid, sd, 0 );
776 /* update state to closing */
777 c->c_conn_state = SLAP_C_CLOSING;
779 /* don't listen on this port anymore */
780 slapd_clr_read( sd, 1 );
782 /* abandon active operations */
783 connection_abandon( c );
785 /* wake write blocked operations */
786 slapd_clr_write( sd, 1 );
787 ldap_pvt_thread_cond_signal( &c->c_write_cv );
791 static void connection_close( Connection *c )
795 assert( connections != NULL );
797 assert( c->c_struct_state == SLAP_C_USED );
798 assert( c->c_conn_state == SLAP_C_CLOSING );
800 /* note: connections_mutex and c_mutex should be locked by caller */
802 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
803 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
805 LDAP_LOG( CONNECTION, DETAIL1,
806 "connection_close: conn %lu deferring sd %d\n",
807 c->c_connid, sd, 0 );
809 Debug( LDAP_DEBUG_TRACE,
810 "connection_close: deferring conn=%lu sd=%d\n",
811 c->c_connid, sd, 0 );
817 LDAP_LOG( CONNECTION, RESULTS,
818 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
820 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
821 c->c_connid, sd, 0 );
823 connection_destroy( c );
826 unsigned long connections_nextid(void)
829 assert( connections != NULL );
831 ldap_pvt_thread_mutex_lock( &connections_mutex );
835 ldap_pvt_thread_mutex_unlock( &connections_mutex );
840 Connection* connection_first( ber_socket_t *index )
842 assert( connections != NULL );
843 assert( index != NULL );
845 ldap_pvt_thread_mutex_lock( &connections_mutex );
849 return connection_next(NULL, index);
852 Connection* connection_next( Connection *c, ber_socket_t *index )
854 assert( connections != NULL );
855 assert( index != NULL );
856 assert( *index <= dtblsize );
859 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
864 for(; *index < dtblsize; (*index)++) {
865 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
866 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
874 if( connections[*index].c_struct_state == SLAP_C_USED ) {
875 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
876 c = &connections[(*index)++];
880 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
881 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
885 ldap_pvt_thread_mutex_lock( &c->c_mutex );
891 void connection_done( Connection *c )
893 assert( connections != NULL );
896 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
899 ldap_pvt_thread_mutex_unlock( &connections_mutex );
903 * connection_activity - handle the request operation op on connection
904 * conn. This routine figures out what kind of operation it is and
905 * calls the appropriate stub to handle it.
909 #define INCR_OP(var,index) \
911 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
913 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
915 #else /* !SLAPD_MONITOR */
916 #define INCR_OP(var,index)
917 #endif /* !SLAPD_MONITOR */
920 connection_operation( void *ctx, void *arg_v )
922 int rc = SLAPD_DISCONNECT;
923 Operation *op = arg_v;
924 SlapReply rs = {REP_RESULT};
925 ber_tag_t tag = op->o_tag;
927 ber_tag_t oldtag = tag;
928 #endif /* SLAPD_MONITOR */
929 Connection *conn = op->o_conn;
933 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
935 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
937 op->o_threadctx = ctx;
939 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
941 LDAP_LOG( CONNECTION, ERR,
942 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
943 conn->c_connid, (long)tag, 0 );
945 Debug( LDAP_DEBUG_ANY, "connection_operation: "
946 "error: SASL bind in progress (tag=%ld).\n",
949 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
950 "SASL bind in progress" );
951 goto operations_error;
954 /* We can use Thread-Local storage for most mallocs. We can
955 * also use TL for ber parsing, but not on Add or Modify.
957 #define SLAB_SIZE 1048576
959 memsiz = ber_len( op->o_ber ) * 64;
960 if ( SLAB_SIZE > memsiz ) memsiz = SLAB_SIZE;
964 memctx = sl_mem_create( memsiz, ctx );
965 op->o_tmpmemctx = memctx;
966 op->o_tmpmfuncs = &sl_mfuncs;
967 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
968 /* Note - the ber and its buffer are already allocated from
969 * regular memory; this only affects subsequent mallocs that
970 * ber_scanf may invoke.
972 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
977 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
978 rc = do_bind( op, &rs );
981 case LDAP_REQ_UNBIND:
982 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
983 rc = do_unbind( op, &rs );
987 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
988 rc = do_add( op, &rs );
991 case LDAP_REQ_DELETE:
992 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
993 rc = do_delete( op, &rs );
996 case LDAP_REQ_MODRDN:
997 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
998 rc = do_modrdn( op, &rs );
1001 case LDAP_REQ_MODIFY:
1002 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
1003 rc = do_modify( op, &rs );
1006 case LDAP_REQ_COMPARE:
1007 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
1008 rc = do_compare( op, &rs );
1011 case LDAP_REQ_SEARCH:
1012 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
1013 rc = do_search( op, &rs );
1016 case LDAP_REQ_ABANDON:
1017 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
1018 rc = do_abandon( op, &rs );
1021 case LDAP_REQ_EXTENDED:
1022 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
1023 rc = do_extended( op, &rs );
1028 LDAP_LOG( CONNECTION, INFO,
1029 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
1030 conn->c_connid, tag, 0 );
1032 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
1035 op->o_tag = LBER_ERROR;
1036 rs.sr_err = LDAP_PROTOCOL_ERROR;
1037 rs.sr_text = "unknown LDAP request";
1038 send_ldap_disconnect( op, &rs );
1043 #ifdef SLAPD_MONITOR
1045 #endif /* SLAPD_MONITOR */
1046 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1049 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1050 num_ops_completed++;
1051 #ifdef SLAPD_MONITOR
1054 num_ops_completed_[SLAP_OP_BIND]++;
1056 case LDAP_REQ_UNBIND:
1057 num_ops_completed_[SLAP_OP_UNBIND]++;
1060 num_ops_completed_[SLAP_OP_ADD]++;
1062 case LDAP_REQ_DELETE:
1063 num_ops_completed_[SLAP_OP_DELETE]++;
1065 case LDAP_REQ_MODRDN:
1066 num_ops_completed_[SLAP_OP_MODRDN]++;
1068 case LDAP_REQ_MODIFY:
1069 num_ops_completed_[SLAP_OP_MODIFY]++;
1071 case LDAP_REQ_COMPARE:
1072 num_ops_completed_[SLAP_OP_COMPARE]++;
1074 case LDAP_REQ_SEARCH:
1075 num_ops_completed_[SLAP_OP_SEARCH]++;
1077 case LDAP_REQ_ABANDON:
1078 num_ops_completed_[SLAP_OP_ABANDON]++;
1080 case LDAP_REQ_EXTENDED:
1081 num_ops_completed_[SLAP_OP_EXTENDED]++;
1084 #endif /* SLAPD_MONITOR */
1085 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1087 #ifdef LDAP_EXOP_X_CANCEL
1088 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1089 op->o_cancel = LDAP_TOO_LATE;
1092 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1093 op->o_cancel != SLAP_CANCEL_DONE )
1095 ldap_pvt_thread_yield();
1099 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1101 if ( op->o_cancel != SLAP_CANCEL_ACK && ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) {
1102 sl_mem_detach( ctx, memctx );
1106 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1107 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1109 conn->c_n_ops_executing--;
1110 conn->c_n_ops_completed++;
1112 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1119 case LDAP_REQ_UNBIND:
1120 /* c_mutex is locked */
1121 connection_closing( conn );
1125 conn->c_sasl_bind_in_progress =
1126 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1128 if( conn->c_conn_state == SLAP_C_BINDING) {
1129 conn->c_conn_state = SLAP_C_ACTIVE;
1133 connection_resched( conn );
1135 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1140 int connection_client_setup(
1143 ldap_pvt_thread_start_t *func,
1148 if ( connection_init( s, l, "", "", CONN_IS_CLIENT, 0, "" ) < 0 ) {
1152 c = connection_get( s );
1153 c->c_clientfunc = func;
1154 c->c_clientarg = arg;
1155 connection_return( c );
1156 slapd_add_internal( s, 0 );
1157 slapd_set_read( s, 1 );
1161 void connection_client_enable(
1165 slapd_set_read( s, 1 );
1168 void connection_client_stop(
1174 /* get (locked) connection */
1175 c = connection_get( s );
1177 assert( c->c_conn_state == SLAP_C_CLIENT );
1179 c->c_listener = NULL;
1180 c->c_conn_state = SLAP_C_INVALID;
1181 c->c_struct_state = SLAP_C_UNUSED;
1182 connection_return( c );
1183 slapd_remove( s, 0, 1 );
1186 int connection_read(ber_socket_t s)
1191 assert( connections != NULL );
1193 ldap_pvt_thread_mutex_lock( &connections_mutex );
1195 /* get (locked) connection */
1196 c = connection_get( s );
1200 LDAP_LOG( CONNECTION, INFO,
1201 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1203 Debug( LDAP_DEBUG_ANY,
1204 "connection_read(%ld): no connection!\n",
1207 slapd_remove(s, 1, 0);
1209 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1215 if( c->c_conn_state == SLAP_C_CLOSING ) {
1217 LDAP_LOG( CONNECTION, INFO,
1218 "connection_read: conn %lu connection closing, ignoring input\n",
1219 c->c_connid, 0, 0 );
1221 Debug( LDAP_DEBUG_TRACE,
1222 "connection_read(%d): closing, ignoring input for id=%lu\n",
1223 s, c->c_connid, 0 );
1225 connection_return( c );
1226 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1230 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1231 slapd_clr_read( s, 0 );
1232 ldap_pvt_thread_pool_submit( &connection_pool,
1233 c->c_clientfunc, c->c_clientarg );
1234 connection_return( c );
1235 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1240 LDAP_LOG( CONNECTION, DETAIL1,
1241 "connection_read: conn %lu checking for input.\n",
1242 c->c_connid, 0, 0 );
1244 Debug( LDAP_DEBUG_TRACE,
1245 "connection_read(%d): checking for input on id=%lu\n",
1246 s, c->c_connid, 0 );
1250 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1251 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1253 #if 0 /* required by next #if 0 */
1259 LDAP_LOG( CONNECTION, ERR,
1260 "connection_read: conn %lu TLS accept error, error %d\n",
1261 c->c_connid, rc, 0 );
1263 Debug( LDAP_DEBUG_TRACE,
1264 "connection_read(%d): TLS accept error "
1265 "error=%d id=%lu, closing\n",
1266 s, rc, c->c_connid );
1268 c->c_needs_tls_accept = 0;
1269 /* connections_mutex and c_mutex are locked */
1270 connection_closing( c );
1273 /* Drain input before close, to allow SSL error codes
1274 * to propagate to client. */
1280 rc = select(s+1, &rfd, NULL, NULL, &tv);
1282 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1286 connection_close( c );
1288 } else if ( rc == 0 ) {
1290 struct berval authid = { 0, NULL };
1292 c->c_needs_tls_accept = 0;
1294 /* we need to let SASL know */
1295 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1297 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1298 if( c->c_tls_ssf > c->c_ssf ) {
1299 c->c_ssf = c->c_tls_ssf;
1302 rc = dnX509peerNormalize( ssl, &authid );
1303 if ( rc != LDAP_SUCCESS ) {
1305 LDAP_LOG( CONNECTION, INFO,
1306 "connection_read: conn %lu unable to get TLS client DN, "
1307 "error %d\n", c->c_connid, rc, 0 );
1309 Debug( LDAP_DEBUG_TRACE,
1310 "connection_read(%d): unable to get TLS client DN "
1311 "error=%d id=%lu\n",
1312 s, rc, c->c_connid );
1315 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1316 if ( authid.bv_val ) free( authid.bv_val );
1319 /* if success and data is ready, fall thru to data input loop */
1321 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1323 connection_return( c );
1324 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1330 #ifdef HAVE_CYRUS_SASL
1331 if ( c->c_sasl_layers ) {
1332 /* If previous layer is not removed yet, give up for now */
1333 if ( !c->c_sasl_sockctx ) {
1334 connection_return( c );
1335 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1339 c->c_sasl_layers = 0;
1341 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1343 if( rc != LDAP_SUCCESS ) {
1345 LDAP_LOG( CONNECTION, ERR,
1346 "connection_read: conn %lu SASL install error %d, closing\n",
1347 c->c_connid, rc, 0 );
1349 Debug( LDAP_DEBUG_TRACE,
1350 "connection_read(%d): SASL install error "
1351 "error=%d id=%lu, closing\n",
1352 s, rc, c->c_connid );
1354 /* connections_mutex and c_mutex are locked */
1355 connection_closing( c );
1356 connection_close( c );
1357 connection_return( c );
1358 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1364 #define CONNECTION_INPUT_LOOP 1
1365 /* #define DATA_READY_LOOP 1 */
1369 /* How do we do this without getting into a busy loop ? */
1370 rc = connection_input( c );
1372 #ifdef DATA_READY_LOOP
1373 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) );
1374 #elif CONNECTION_INPUT_LOOP
1382 LDAP_LOG( CONNECTION, ERR,
1383 "connection_read: conn %lu input error %d, closing.\n",
1384 c->c_connid, rc, 0 );
1386 Debug( LDAP_DEBUG_TRACE,
1387 "connection_read(%d): input error=%d id=%lu, closing.\n",
1388 s, rc, c->c_connid );
1390 /* connections_mutex and c_mutex are locked */
1391 connection_closing( c );
1392 connection_close( c );
1393 connection_return( c );
1394 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1398 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1399 slapd_set_read( s, 1 );
1402 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1403 slapd_set_write( s, 1 );
1406 connection_return( c );
1407 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1422 #ifdef LDAP_CONNECTIONLESS
1427 if ( conn->c_currentber == NULL &&
1428 ( conn->c_currentber = ber_alloc()) == NULL )
1431 LDAP_LOG( CONNECTION, ERR,
1432 "connection_input: conn %lu ber_alloc failed.\n",
1433 conn->c_connid, 0, 0 );
1435 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1442 #ifdef LDAP_CONNECTIONLESS
1443 if ( conn->c_is_udp ) {
1444 char peername[sizeof("IP=255.255.255.255:65336")];
1445 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1446 sizeof(struct sockaddr));
1447 if (len != sizeof(struct sockaddr))
1449 sprintf( peername, "IP=%s:%d",
1450 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1451 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1452 Statslog( LDAP_DEBUG_STATS,
1453 "conn=%lu UDP request from %s (%s) accepted.\n",
1454 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1457 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1458 if ( tag != LDAP_TAG_MESSAGE ) {
1462 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1465 LDAP_LOG( CONNECTION, ERR,
1466 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1467 conn->c_connid, err, sock_errstr(err) );
1469 Debug( LDAP_DEBUG_TRACE,
1470 "ber_get_next on fd %d failed errno=%d (%s)\n",
1471 sd, err, sock_errstr(err) );
1473 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1474 /* log, close and send error */
1475 ber_free( conn->c_currentber, 1 );
1476 conn->c_currentber = NULL;
1483 ber = conn->c_currentber;
1484 conn->c_currentber = NULL;
1486 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1487 /* log, close and send error */
1489 LDAP_LOG( CONNECTION, ERR,
1490 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1491 conn->c_connid, tag, 0 );
1493 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1500 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1501 /* log, close and send error */
1503 LDAP_LOG( CONNECTION, ERR,
1504 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1505 conn->c_connid, tag, 0 );
1507 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1515 #ifdef LDAP_CONNECTIONLESS
1516 if( conn->c_is_udp ) {
1517 if( tag == LBER_OCTETSTRING ) {
1518 ber_get_stringa( ber, &cdn );
1519 tag = ber_peek_tag(ber, &len);
1521 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1523 LDAP_LOG( CONNECTION, ERR,
1524 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1525 conn->c_connid, tag, 0 );
1527 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1535 if(tag == LDAP_REQ_BIND) {
1536 /* immediately abandon all exiting operations upon BIND */
1537 connection_abandon( conn );
1540 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1543 op->o_assertion = NULL;
1544 op->o_preread_attrs = NULL;
1545 op->o_postread_attrs = NULL;
1546 op->o_vrFilter = NULL;
1548 #ifdef LDAP_CONTROL_PAGEDRESULTS
1549 op->o_pagedresults_state = conn->c_pagedresults_state;
1552 op->o_res_ber = NULL;
1554 #ifdef LDAP_CONNECTIONLESS
1555 if (conn->c_is_udp) {
1557 ber_str2bv( cdn, 0, 1, &op->o_dn );
1558 op->o_protocol = LDAP_VERSION2;
1560 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1561 if (op->o_res_ber == NULL) return 1;
1563 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1564 sizeof(struct sockaddr), 0 );
1566 if (rc != sizeof(struct sockaddr)) {
1568 LDAP_LOG( CONNECTION, INFO,
1569 "connection_input: conn %lu ber_write failed\n",
1570 conn->c_connid, 0, 0 );
1572 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1577 if (op->o_protocol == LDAP_VERSION2) {
1578 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1581 LDAP_LOG( CONNECTION, INFO,
1582 "connection_input: conn %lu put outer sequence failed\n",
1583 conn->c_connid, 0, 0 );
1585 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1591 #endif /* LDAP_CONNECTIONLESS */
1595 /* Don't process requests when the conn is in the middle of a
1596 * Bind, or if it's closing. Also, don't let any single conn
1597 * use up all the available threads, and don't execute if we're
1598 * currently blocked on output. And don't execute if there are
1599 * already pending ops, let them go first.
1601 * But always allow Abandon through; it won't cost much.
1603 if ( tag != LDAP_REQ_ABANDON && (conn->c_conn_state == SLAP_C_BINDING
1604 || conn->c_conn_state == SLAP_C_CLOSING
1605 || conn->c_n_ops_executing >= connection_pool_max/2
1606 || conn->c_n_ops_pending
1607 || conn->c_writewaiter))
1609 int max = conn->c_dn.bv_len
1610 ? slap_conn_max_pending_auth
1611 : slap_conn_max_pending;
1614 LDAP_LOG( CONNECTION, INFO,
1615 "connection_input: conn %lu deferring operation\n",
1616 conn->c_connid, 0, 0 );
1618 Debug( LDAP_DEBUG_ANY,
1619 "connection_input: conn=%lu deferring operation\n",
1620 conn->c_connid, 0, 0 );
1622 conn->c_n_ops_pending++;
1623 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1624 if ( conn->c_n_ops_pending > max ) {
1630 conn->c_n_ops_executing++;
1631 connection_op_activate( op );
1635 if ( conn->c_struct_state != SLAP_C_USED ) {
1636 /* connection must have got closed underneath us */
1640 assert( conn->c_struct_state == SLAP_C_USED );
1646 connection_resched( Connection *conn )
1650 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1653 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1655 /* us trylock to avoid possible deadlock */
1656 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1660 LDAP_LOG( CONNECTION, DETAIL1,
1661 "connection_resched: conn %lu reaquiring locks.\n",
1662 conn->c_connid, 0, 0 );
1664 Debug( LDAP_DEBUG_TRACE,
1665 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1666 conn->c_connid, sd, 0 );
1669 * reaquire locks in the right order...
1670 * this may allow another thread to close this connection,
1671 * so recheck state below.
1673 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1674 ldap_pvt_thread_mutex_lock( &connections_mutex );
1675 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1678 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1680 LDAP_LOG( CONNECTION, INFO,
1681 "connection_resched: conn %lu closed by other thread.\n",
1682 conn->c_connid, 0, 0 );
1684 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1685 "closed by other thread conn=%lu sd=%d\n",
1686 conn->c_connid, sd, 0 );
1690 LDAP_LOG( CONNECTION, DETAIL1,
1691 "connection_resched: conn %lu attempting closing.\n",
1692 conn->c_connid, 0, 0 );
1694 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1695 "attempting closing conn=%lu sd=%d\n",
1696 conn->c_connid, sd, 0 );
1698 connection_close( conn );
1701 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1705 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1706 /* other states need different handling */
1710 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1711 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1714 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1715 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1716 /* pending operations should not be marked for abandonment */
1717 assert(!op->o_abandon);
1719 conn->c_n_ops_pending--;
1720 conn->c_n_ops_executing++;
1722 connection_op_activate( op );
1724 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1731 static int connection_op_activate( Operation *op )
1734 ber_tag_t tag = op->o_tag;
1736 if(tag == LDAP_REQ_BIND) {
1737 op->o_conn->c_conn_state = SLAP_C_BINDING;
1740 if (!op->o_dn.bv_len) {
1741 op->o_authz = op->o_conn->c_authz;
1742 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1743 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1745 op->o_authtype = op->o_conn->c_authtype;
1746 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1748 if (!op->o_protocol) {
1749 op->o_protocol = op->o_conn->c_protocol
1750 ? op->o_conn->c_protocol : LDAP_VERSION3;
1752 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1753 && op->o_protocol > LDAP_VERSION2)
1755 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1758 op->o_connid = op->o_conn->c_connid;
1760 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1762 status = ldap_pvt_thread_pool_submit( &connection_pool,
1763 connection_operation, (void *) op );
1765 if ( status != 0 ) {
1767 LDAP_LOG( CONNECTION, ERR,
1768 "connection_op_activate: conn %lu thread pool submit failed.\n",
1769 op->o_connid, 0, 0 );
1771 Debug( LDAP_DEBUG_ANY,
1772 "ldap_pvt_thread_pool_submit: failed (%d) for conn=%lu\n",
1773 status, op->o_connid, 0 );
1775 /* should move op to pending list */
1781 int connection_write(ber_socket_t s)
1785 assert( connections != NULL );
1787 ldap_pvt_thread_mutex_lock( &connections_mutex );
1789 c = connection_get( s );
1791 slapd_clr_write( s, 0);
1795 LDAP_LOG( CONNECTION, ERR,
1796 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1798 Debug( LDAP_DEBUG_ANY,
1799 "connection_write(%ld): no connection!\n",
1802 slapd_remove(s, 1, 0);
1803 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1810 LDAP_LOG( CONNECTION, DETAIL1,
1811 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1813 Debug( LDAP_DEBUG_TRACE,
1814 "connection_write(%d): waking output for id=%lu\n",
1815 s, c->c_connid, 0 );
1817 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1819 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1820 slapd_set_read( s, 1 );
1822 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1823 slapd_set_write( s, 1 );
1825 connection_return( c );
1826 ldap_pvt_thread_mutex_unlock( &connections_mutex );