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 */
207 if( c->c_n_ops_executing ) continue;
209 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
211 connection_closing( c );
212 connection_close( c );
216 connection_done( c );
221 static Connection* connection_get( ber_socket_t s )
223 /* connections_mutex should be locked by caller */
228 LDAP_LOG( CONNECTION, ENTRY, "connection_get: socket %ld\n", (long)s, 0, 0 );
230 Debug( LDAP_DEBUG_ARGS,
231 "connection_get(%ld)\n",
235 assert( connections != NULL );
237 if(s == AC_SOCKET_INVALID) {
244 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
251 for(i=0; i<dtblsize; i++) {
252 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
253 assert( connections[i].c_conn_state == SLAP_C_INVALID );
254 assert( connections[i].c_sb == 0 );
258 ber_sockbuf_ctrl( connections[i].c_sb,
259 LBER_SB_OPT_GET_FD, &sd );
261 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
262 assert( connections[i].c_conn_state == SLAP_C_INVALID );
263 assert( sd == AC_SOCKET_INVALID );
267 /* state can actually change from used -> unused by resched,
268 * so don't assert details here.
282 ldap_pvt_thread_mutex_lock( &c->c_mutex );
284 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
285 if( c->c_struct_state != SLAP_C_USED ) {
286 /* connection must have been closed due to resched */
288 assert( c->c_conn_state == SLAP_C_INVALID );
289 assert( sd == AC_SOCKET_INVALID );
292 LDAP_LOG( CONNECTION, ARGS,
293 "connection_get: connection %d not used\n", s, 0, 0 );
295 Debug( LDAP_DEBUG_TRACE,
296 "connection_get(%d): connection not used\n",
300 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
303 if( c->c_conn_state == SLAP_C_CLIENT ) sd = 0;
306 LDAP_LOG( CONNECTION, RESULTS,
307 "connection_get: get for %d got connid %lu\n", s, c->c_connid, 0 );
309 Debug( LDAP_DEBUG_TRACE,
310 "connection_get(%d): got connid=%lu\n",
316 assert( c->c_struct_state == SLAP_C_USED );
317 assert( c->c_conn_state != SLAP_C_INVALID );
318 assert( sd != AC_SOCKET_INVALID );
321 c->c_activitytime = slap_get_time();
323 if( global_idletimeout > 0 ) {
324 c->c_activitytime = slap_get_time();
332 static void connection_return( Connection *c )
334 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
337 long connection_init(
341 const char* peername,
349 assert( connections != NULL );
351 assert( listener != NULL );
352 assert( dnsname != NULL );
353 assert( peername != NULL );
356 assert( flags != CONN_IS_TLS );
359 if( s == AC_SOCKET_INVALID ) {
361 LDAP_LOG( CONNECTION, INFO,
362 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
364 Debug( LDAP_DEBUG_ANY,
365 "connection_init(%ld): invalid.\n",
373 assert( s < dtblsize );
376 ldap_pvt_thread_mutex_lock( &connections_mutex );
387 for( i=0; i < dtblsize; i++) {
390 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
391 assert( connections[i].c_sb == 0 );
396 sd = AC_SOCKET_INVALID;
397 if (connections[i].c_sb != NULL)
398 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
400 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
401 assert( sd == AC_SOCKET_INVALID );
406 assert( connections[i].c_struct_state == SLAP_C_USED );
407 assert( connections[i].c_conn_state != SLAP_C_INVALID );
408 assert( sd != AC_SOCKET_INVALID );
413 LDAP_LOG( CONNECTION, INFO,
414 "connection_init: skt %d connection table full "
415 "(%d/%d)\n", s, i, dtblsize );
417 Debug( LDAP_DEBUG_ANY,
418 "connection_init(%d): connection table full "
419 "(%d/%d)\n", s, i, dtblsize);
421 ldap_pvt_thread_mutex_unlock( &connections_mutex );
429 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
430 c->c_send_ldap_result = slap_send_ldap_result;
431 c->c_send_search_entry = slap_send_search_entry;
432 c->c_send_search_reference = slap_send_search_reference;
433 c->c_send_ldap_extended = slap_send_ldap_extended;
434 #ifdef LDAP_RES_INTERMEDIATE
435 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
438 c->c_authmech.bv_val = NULL;
439 c->c_authmech.bv_len = 0;
440 c->c_dn.bv_val = NULL;
442 c->c_ndn.bv_val = NULL;
445 c->c_listener = NULL;
446 c->c_peer_domain.bv_val = NULL;
447 c->c_peer_domain.bv_len = 0;
448 c->c_peer_name.bv_val = NULL;
449 c->c_peer_name.bv_len = 0;
451 LDAP_STAILQ_INIT(&c->c_ops);
452 LDAP_STAILQ_INIT(&c->c_pending_ops);
454 c->c_sasl_bind_mech.bv_val = NULL;
455 c->c_sasl_bind_mech.bv_len = 0;
457 c->c_sasl_authctx = NULL;
458 c->c_sasl_sockctx = NULL;
459 c->c_sasl_extra = NULL;
460 c->c_sasl_bindop = NULL;
462 c->c_sb = ber_sockbuf_alloc( );
465 ber_len_t max = sockbuf_max_incoming;
466 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
469 c->c_currentber = NULL;
471 /* should check status of thread calls */
472 ldap_pvt_thread_mutex_init( &c->c_mutex );
473 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
474 ldap_pvt_thread_cond_init( &c->c_write_cv );
477 if ( slapi_plugins_used ) {
478 slapi_x_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
482 c->c_struct_state = SLAP_C_UNUSED;
485 ldap_pvt_thread_mutex_lock( &c->c_mutex );
487 assert( c->c_struct_state == SLAP_C_UNUSED );
488 assert( c->c_authmech.bv_val == NULL );
489 assert( c->c_dn.bv_val == NULL );
490 assert( c->c_ndn.bv_val == NULL );
491 assert( c->c_listener == NULL );
492 assert( c->c_peer_domain.bv_val == NULL );
493 assert( c->c_peer_name.bv_val == NULL );
494 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
495 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
496 assert( c->c_sasl_bind_mech.bv_val == NULL );
497 assert( c->c_sasl_done == 0 );
498 assert( c->c_sasl_authctx == NULL );
499 assert( c->c_sasl_sockctx == NULL );
500 assert( c->c_sasl_extra == NULL );
501 assert( c->c_sasl_bindop == NULL );
502 assert( c->c_currentber == NULL );
504 c->c_listener = listener;
506 if ( flags == CONN_IS_CLIENT ) {
507 c->c_conn_state = SLAP_C_CLIENT;
508 c->c_struct_state = SLAP_C_USED;
509 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
510 ldap_pvt_thread_mutex_unlock( &connections_mutex );
515 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
516 ber_str2bv( peername, 0, 1, &c->c_peer_name );
518 c->c_n_ops_received = 0;
519 c->c_n_ops_executing = 0;
520 c->c_n_ops_pending = 0;
521 c->c_n_ops_completed = 0;
527 /* set to zero until bind, implies LDAP_VERSION3 */
531 c->c_activitytime = c->c_starttime = slap_get_time();
533 if( global_idletimeout > 0 ) {
534 c->c_activitytime = c->c_starttime = slap_get_time();
538 #ifdef LDAP_CONNECTIONLESS
540 if( flags == CONN_IS_UDP ) {
543 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
544 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
546 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
547 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
548 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
549 LBER_SBIOD_LEVEL_PROVIDER, NULL );
554 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
555 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
557 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
558 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
562 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
563 INT_MAX, (void*)"ldap_" );
566 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
567 c /* non-NULL */ ) < 0 )
570 LDAP_LOG( CONNECTION, INFO,
571 "connection_init: conn %lu set nonblocking failed\n",
574 Debug( LDAP_DEBUG_ANY,
575 "connection_init(%d, %s): set nonblocking failed\n",
576 s, c->c_peer_name.bv_val, 0 );
580 id = c->c_connid = conn_nextid++;
582 c->c_conn_state = SLAP_C_INACTIVE;
583 c->c_struct_state = SLAP_C_USED;
585 c->c_ssf = c->c_transport_ssf = ssf;
589 if ( flags == CONN_IS_TLS ) {
591 c->c_needs_tls_accept = 1;
594 c->c_needs_tls_accept = 0;
598 slap_sasl_open( c, 0 );
599 slap_sasl_external( c, ssf, authid );
601 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
602 ldap_pvt_thread_mutex_unlock( &connections_mutex );
604 backend_connection_init(c);
609 void connection2anonymous( Connection *c )
611 assert( connections != NULL );
615 ber_len_t max = sockbuf_max_incoming;
616 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
619 if(c->c_authmech.bv_val != NULL ) {
620 free(c->c_authmech.bv_val);
621 c->c_authmech.bv_val = NULL;
623 c->c_authmech.bv_len = 0;
625 if(c->c_dn.bv_val != NULL) {
626 free(c->c_dn.bv_val);
627 c->c_dn.bv_val = NULL;
630 if(c->c_ndn.bv_val != NULL) {
631 free(c->c_ndn.bv_val);
632 c->c_ndn.bv_val = NULL;
636 c->c_authz_backend = NULL;
640 connection_destroy( Connection *c )
642 /* note: connections_mutex should be locked by caller */
644 unsigned long connid;
646 assert( connections != NULL );
648 assert( c->c_struct_state != SLAP_C_UNUSED );
649 assert( c->c_conn_state != SLAP_C_INVALID );
650 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
652 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
653 connid = c->c_connid;
655 backend_connection_destroy(c);
660 c->c_activitytime = c->c_starttime = 0;
662 connection2anonymous( c );
663 c->c_listener = NULL;
665 if(c->c_peer_domain.bv_val != NULL) {
666 free(c->c_peer_domain.bv_val);
667 c->c_peer_domain.bv_val = NULL;
669 c->c_peer_domain.bv_len = 0;
670 if(c->c_peer_name.bv_val != NULL) {
671 free(c->c_peer_name.bv_val);
672 c->c_peer_name.bv_val = NULL;
674 c->c_peer_name.bv_len = 0;
676 c->c_sasl_bind_in_progress = 0;
677 if(c->c_sasl_bind_mech.bv_val != NULL) {
678 free(c->c_sasl_bind_mech.bv_val);
679 c->c_sasl_bind_mech.bv_val = NULL;
681 c->c_sasl_bind_mech.bv_len = 0;
683 slap_sasl_close( c );
685 if ( c->c_currentber != NULL ) {
686 ber_free( c->c_currentber, 1 );
687 c->c_currentber = NULL;
690 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
691 if ( sd != AC_SOCKET_INVALID ) {
692 slapd_remove( sd, 0 );
694 Statslog( LDAP_DEBUG_STATS,
695 "conn=%lu fd=%ld closed\n",
696 connid, (long) sd, 0, 0, 0 );
699 ber_sockbuf_free( c->c_sb );
701 c->c_sb = ber_sockbuf_alloc( );
704 ber_len_t max = sockbuf_max_incoming;
705 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
708 c->c_conn_state = SLAP_C_INVALID;
709 c->c_struct_state = SLAP_C_UNUSED;
712 /* call destructors, then constructors; avoids unnecessary allocation */
713 if ( slapi_plugins_used ) {
714 slapi_x_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
719 int connection_state_closing( Connection *c )
721 /* c_mutex must be locked by caller */
725 assert( c->c_struct_state == SLAP_C_USED );
727 state = c->c_conn_state;
729 assert( state != SLAP_C_INVALID );
731 return state == SLAP_C_CLOSING;
734 static void connection_abandon( Connection *c )
736 /* c_mutex must be locked by caller */
740 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
744 /* remove pending operations */
745 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
746 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
747 LDAP_STAILQ_NEXT(o, o_next) = NULL;
752 void connection_closing( Connection *c )
754 assert( connections != NULL );
756 assert( c->c_struct_state == SLAP_C_USED );
757 assert( c->c_conn_state != SLAP_C_INVALID );
759 /* c_mutex must be locked by caller */
761 if( c->c_conn_state != SLAP_C_CLOSING ) {
764 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
766 LDAP_LOG( CONNECTION, DETAIL1,
767 "connection_closing: conn %lu readying socket %d for close.\n",
768 c->c_connid, sd, 0 );
770 Debug( LDAP_DEBUG_TRACE,
771 "connection_closing: readying conn=%lu sd=%d for close\n",
772 c->c_connid, sd, 0 );
774 /* update state to closing */
775 c->c_conn_state = SLAP_C_CLOSING;
777 /* don't listen on this port anymore */
778 slapd_clr_read( sd, 1 );
780 /* abandon active operations */
781 connection_abandon( c );
783 /* wake write blocked operations */
784 slapd_clr_write( sd, 1 );
785 ldap_pvt_thread_cond_signal( &c->c_write_cv );
789 static void connection_close( Connection *c )
793 assert( connections != NULL );
795 assert( c->c_struct_state == SLAP_C_USED );
796 assert( c->c_conn_state == SLAP_C_CLOSING );
798 /* note: connections_mutex and c_mutex should be locked by caller */
800 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
801 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
803 LDAP_LOG( CONNECTION, DETAIL1,
804 "connection_close: conn %lu deferring sd %d\n",
805 c->c_connid, sd, 0 );
807 Debug( LDAP_DEBUG_TRACE,
808 "connection_close: deferring conn=%lu sd=%d\n",
809 c->c_connid, sd, 0 );
815 LDAP_LOG( CONNECTION, RESULTS,
816 "connection_close: conn %lu sd %d\n", c->c_connid, sd, 0 );
818 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
819 c->c_connid, sd, 0 );
821 connection_destroy( c );
824 unsigned long connections_nextid(void)
827 assert( connections != NULL );
829 ldap_pvt_thread_mutex_lock( &connections_mutex );
833 ldap_pvt_thread_mutex_unlock( &connections_mutex );
838 Connection* connection_first( ber_socket_t *index )
840 assert( connections != NULL );
841 assert( index != NULL );
843 ldap_pvt_thread_mutex_lock( &connections_mutex );
847 return connection_next(NULL, index);
850 Connection* connection_next( Connection *c, ber_socket_t *index )
852 assert( connections != NULL );
853 assert( index != NULL );
854 assert( *index <= dtblsize );
857 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
862 for(; *index < dtblsize; (*index)++) {
863 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
864 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
872 if( connections[*index].c_struct_state == SLAP_C_USED ) {
873 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
874 c = &connections[(*index)++];
878 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
879 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
883 ldap_pvt_thread_mutex_lock( &c->c_mutex );
889 void connection_done( Connection *c )
891 assert( connections != NULL );
894 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
897 ldap_pvt_thread_mutex_unlock( &connections_mutex );
901 * connection_activity - handle the request operation op on connection
902 * conn. This routine figures out what kind of operation it is and
903 * calls the appropriate stub to handle it.
907 #define INCR_OP(var,index) \
909 ldap_pvt_thread_mutex_lock( &num_ops_mutex ); \
911 ldap_pvt_thread_mutex_unlock( &num_ops_mutex ); \
913 #else /* !SLAPD_MONITOR */
914 #define INCR_OP(var,index)
915 #endif /* !SLAPD_MONITOR */
918 connection_operation( void *ctx, void *arg_v )
920 int rc = SLAPD_DISCONNECT;
921 Operation *op = arg_v;
922 SlapReply rs = {REP_RESULT};
923 ber_tag_t tag = op->o_tag;
925 ber_tag_t oldtag = tag;
926 #endif /* SLAPD_MONITOR */
927 Connection *conn = op->o_conn;
931 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
933 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
935 op->o_threadctx = ctx;
937 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
939 LDAP_LOG( CONNECTION, ERR,
940 "connection_operation: conn %lu SASL bind in progress (tag=%ld).\n",
941 conn->c_connid, (long)tag, 0 );
943 Debug( LDAP_DEBUG_ANY, "connection_operation: "
944 "error: SASL bind in progress (tag=%ld).\n",
947 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
948 "SASL bind in progress" );
949 goto operations_error;
952 /* We can use Thread-Local storage for most mallocs. We can
953 * also use TL for ber parsing, but not on Add or Modify.
955 #define SLAB_SIZE 1048576
957 memsiz = ber_len( op->o_ber ) * 64;
958 if ( SLAB_SIZE > memsiz ) memsiz = SLAB_SIZE;
962 memctx = sl_mem_create( memsiz, ctx );
963 op->o_tmpmemctx = memctx;
964 op->o_tmpmfuncs = &sl_mfuncs;
965 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
966 /* Note - the ber and its buffer are already allocated from
967 * regular memory; this only affects subsequent mallocs that
968 * ber_scanf may invoke.
970 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
975 INCR_OP(num_ops_initiated_, SLAP_OP_BIND);
976 rc = do_bind( op, &rs );
979 case LDAP_REQ_UNBIND:
980 INCR_OP(num_ops_initiated_, SLAP_OP_UNBIND);
981 rc = do_unbind( op, &rs );
985 INCR_OP(num_ops_initiated_, SLAP_OP_ADD);
986 rc = do_add( op, &rs );
989 case LDAP_REQ_DELETE:
990 INCR_OP(num_ops_initiated_, SLAP_OP_DELETE);
991 rc = do_delete( op, &rs );
994 case LDAP_REQ_MODRDN:
995 INCR_OP(num_ops_initiated_, SLAP_OP_MODRDN);
996 rc = do_modrdn( op, &rs );
999 case LDAP_REQ_MODIFY:
1000 INCR_OP(num_ops_initiated_, SLAP_OP_MODIFY);
1001 rc = do_modify( op, &rs );
1004 case LDAP_REQ_COMPARE:
1005 INCR_OP(num_ops_initiated_, SLAP_OP_COMPARE);
1006 rc = do_compare( op, &rs );
1009 case LDAP_REQ_SEARCH:
1010 INCR_OP(num_ops_initiated_, SLAP_OP_SEARCH);
1011 rc = do_search( op, &rs );
1014 case LDAP_REQ_ABANDON:
1015 INCR_OP(num_ops_initiated_, SLAP_OP_ABANDON);
1016 rc = do_abandon( op, &rs );
1019 case LDAP_REQ_EXTENDED:
1020 INCR_OP(num_ops_initiated_, SLAP_OP_EXTENDED);
1021 rc = do_extended( op, &rs );
1026 LDAP_LOG( CONNECTION, INFO,
1027 "connection_operation: conn %lu unknown LDAP request 0x%lx\n",
1028 conn->c_connid, tag, 0 );
1030 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
1033 op->o_tag = LBER_ERROR;
1034 rs.sr_err = LDAP_PROTOCOL_ERROR;
1035 rs.sr_text = "unknown LDAP request";
1036 send_ldap_disconnect( op, &rs );
1041 #ifdef SLAPD_MONITOR
1043 #endif /* SLAPD_MONITOR */
1044 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1047 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
1048 num_ops_completed++;
1049 #ifdef SLAPD_MONITOR
1052 num_ops_completed_[SLAP_OP_BIND]++;
1054 case LDAP_REQ_UNBIND:
1055 num_ops_completed_[SLAP_OP_UNBIND]++;
1058 num_ops_completed_[SLAP_OP_ADD]++;
1060 case LDAP_REQ_DELETE:
1061 num_ops_completed_[SLAP_OP_DELETE]++;
1063 case LDAP_REQ_MODRDN:
1064 num_ops_completed_[SLAP_OP_MODRDN]++;
1066 case LDAP_REQ_MODIFY:
1067 num_ops_completed_[SLAP_OP_MODIFY]++;
1069 case LDAP_REQ_COMPARE:
1070 num_ops_completed_[SLAP_OP_COMPARE]++;
1072 case LDAP_REQ_SEARCH:
1073 num_ops_completed_[SLAP_OP_SEARCH]++;
1075 case LDAP_REQ_ABANDON:
1076 num_ops_completed_[SLAP_OP_ABANDON]++;
1078 case LDAP_REQ_EXTENDED:
1079 num_ops_completed_[SLAP_OP_EXTENDED]++;
1082 #endif /* SLAPD_MONITOR */
1083 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
1085 #ifdef LDAP_EXOP_X_CANCEL
1086 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1087 op->o_cancel = LDAP_TOO_LATE;
1090 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1091 op->o_cancel != SLAP_CANCEL_DONE )
1093 ldap_pvt_thread_yield();
1097 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1099 if ( op->o_cancel != SLAP_CANCEL_ACK && ( op->o_sync_mode & SLAP_SYNC_PERSIST ) ) {
1100 sl_mem_detach( ctx, memctx );
1104 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1105 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1107 conn->c_n_ops_executing--;
1108 conn->c_n_ops_completed++;
1110 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1117 case LDAP_REQ_UNBIND:
1118 /* c_mutex is locked */
1119 connection_closing( conn );
1123 conn->c_sasl_bind_in_progress =
1124 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1126 if( conn->c_conn_state == SLAP_C_BINDING) {
1127 conn->c_conn_state = SLAP_C_ACTIVE;
1131 connection_resched( conn );
1133 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1138 int connection_client_setup(
1141 ldap_pvt_thread_start_t *func,
1146 if ( connection_init( s, l, "", "", CONN_IS_CLIENT, 0, "" ) < 0 ) {
1150 c = connection_get( s );
1151 c->c_clientfunc = func;
1152 c->c_clientarg = arg;
1153 connection_return( c );
1154 slapd_add_internal( s );
1155 slapd_set_read( s, 1 );
1159 void connection_client_enable(
1163 slapd_set_read( s, 1 );
1166 void connection_client_stop(
1172 /* get (locked) connection */
1173 c = connection_get( s );
1175 assert( c->c_conn_state == SLAP_C_CLIENT );
1177 c->c_listener = NULL;
1178 c->c_conn_state = SLAP_C_INVALID;
1179 c->c_struct_state = SLAP_C_UNUSED;
1180 connection_return( c );
1183 int connection_read(ber_socket_t s)
1188 assert( connections != NULL );
1190 ldap_pvt_thread_mutex_lock( &connections_mutex );
1192 /* get (locked) connection */
1193 c = connection_get( s );
1197 LDAP_LOG( CONNECTION, INFO,
1198 "connection_read: sock %ld no connection\n", (long)s, 0, 0 );
1200 Debug( LDAP_DEBUG_ANY,
1201 "connection_read(%ld): no connection!\n",
1206 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1212 if( c->c_conn_state == SLAP_C_CLOSING ) {
1214 LDAP_LOG( CONNECTION, INFO,
1215 "connection_read: conn %lu connection closing, ignoring input\n",
1216 c->c_connid, 0, 0 );
1218 Debug( LDAP_DEBUG_TRACE,
1219 "connection_read(%d): closing, ignoring input for id=%lu\n",
1220 s, c->c_connid, 0 );
1222 connection_return( c );
1223 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1227 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1228 slapd_clr_read( s, 0 );
1229 ldap_pvt_thread_pool_submit( &connection_pool,
1230 c->c_clientfunc, c->c_clientarg );
1231 connection_return( c );
1232 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1237 LDAP_LOG( CONNECTION, DETAIL1,
1238 "connection_read: conn %lu checking for input.\n",
1239 c->c_connid, 0, 0 );
1241 Debug( LDAP_DEBUG_TRACE,
1242 "connection_read(%d): checking for input on id=%lu\n",
1243 s, c->c_connid, 0 );
1247 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1248 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
1250 #if 0 /* required by next #if 0 */
1256 LDAP_LOG( CONNECTION, ERR,
1257 "connection_read: conn %lu TLS accept error, error %d\n",
1258 c->c_connid, rc, 0 );
1260 Debug( LDAP_DEBUG_TRACE,
1261 "connection_read(%d): TLS accept error "
1262 "error=%d id=%lu, closing\n",
1263 s, rc, c->c_connid );
1265 c->c_needs_tls_accept = 0;
1266 /* connections_mutex and c_mutex are locked */
1267 connection_closing( c );
1270 /* Drain input before close, to allow SSL error codes
1271 * to propagate to client. */
1277 rc = select(s+1, &rfd, NULL, NULL, &tv);
1279 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1283 connection_close( c );
1285 } else if ( rc == 0 ) {
1287 struct berval authid = { 0, NULL };
1289 c->c_needs_tls_accept = 0;
1291 /* we need to let SASL know */
1292 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1294 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1295 if( c->c_tls_ssf > c->c_ssf ) {
1296 c->c_ssf = c->c_tls_ssf;
1299 rc = dnX509peerNormalize( ssl, &authid );
1300 if ( rc != LDAP_SUCCESS ) {
1302 LDAP_LOG( CONNECTION, INFO,
1303 "connection_read: conn %lu unable to get TLS client DN, "
1304 "error %d\n", c->c_connid, rc, 0 );
1306 Debug( LDAP_DEBUG_TRACE,
1307 "connection_read(%d): unable to get TLS client DN "
1308 "error=%d id=%lu\n",
1309 s, rc, c->c_connid );
1312 slap_sasl_external( c, c->c_tls_ssf, authid.bv_val );
1313 if ( authid.bv_val ) free( authid.bv_val );
1316 /* if success and data is ready, fall thru to data input loop */
1318 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1320 connection_return( c );
1321 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1327 #ifdef HAVE_CYRUS_SASL
1328 if ( c->c_sasl_layers ) {
1329 /* If previous layer is not removed yet, give up for now */
1330 if ( !c->c_sasl_sockctx ) {
1331 connection_return( c );
1332 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1336 c->c_sasl_layers = 0;
1338 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1340 if( rc != LDAP_SUCCESS ) {
1342 LDAP_LOG( CONNECTION, ERR,
1343 "connection_read: conn %lu SASL install error %d, closing\n",
1344 c->c_connid, rc, 0 );
1346 Debug( LDAP_DEBUG_TRACE,
1347 "connection_read(%d): SASL install error "
1348 "error=%d id=%lu, closing\n",
1349 s, rc, c->c_connid );
1351 /* connections_mutex and c_mutex are locked */
1352 connection_closing( c );
1353 connection_close( c );
1354 connection_return( c );
1355 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1361 #define CONNECTION_INPUT_LOOP 1
1362 /* #define DATA_READY_LOOP 1 */
1366 /* How do we do this without getting into a busy loop ? */
1367 rc = connection_input( c );
1369 #ifdef DATA_READY_LOOP
1370 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) );
1371 #elif CONNECTION_INPUT_LOOP
1379 LDAP_LOG( CONNECTION, ERR,
1380 "connection_read: conn %lu input error %d, closing.\n",
1381 c->c_connid, rc, 0 );
1383 Debug( LDAP_DEBUG_TRACE,
1384 "connection_read(%d): input error=%d id=%lu, closing.\n",
1385 s, rc, c->c_connid );
1387 /* connections_mutex and c_mutex are locked */
1388 connection_closing( c );
1389 connection_close( c );
1390 connection_return( c );
1391 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1395 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1396 slapd_set_read( s, 1 );
1399 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1400 slapd_set_write( s, 1 );
1403 connection_return( c );
1404 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1419 #ifdef LDAP_CONNECTIONLESS
1424 if ( conn->c_currentber == NULL &&
1425 ( conn->c_currentber = ber_alloc()) == NULL )
1428 LDAP_LOG( CONNECTION, ERR,
1429 "connection_input: conn %lu ber_alloc failed.\n",
1430 conn->c_connid, 0, 0 );
1432 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1439 #ifdef LDAP_CONNECTIONLESS
1440 if ( conn->c_is_udp ) {
1441 char peername[sizeof("IP=255.255.255.255:65336")];
1442 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1443 sizeof(struct sockaddr));
1444 if (len != sizeof(struct sockaddr))
1446 sprintf( peername, "IP=%s:%d",
1447 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1448 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1449 Statslog( LDAP_DEBUG_STATS,
1450 "conn=%lu UDP request from %s (%s) accepted.\n",
1451 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1454 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1455 if ( tag != LDAP_TAG_MESSAGE ) {
1459 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1462 LDAP_LOG( CONNECTION, ERR,
1463 "connection_input: conn %lu ber_get_next failed, errno %d (%s).\n",
1464 conn->c_connid, err, sock_errstr(err) );
1466 Debug( LDAP_DEBUG_TRACE,
1467 "ber_get_next on fd %d failed errno=%d (%s)\n",
1468 sd, err, sock_errstr(err) );
1470 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1471 /* log, close and send error */
1472 ber_free( conn->c_currentber, 1 );
1473 conn->c_currentber = NULL;
1480 ber = conn->c_currentber;
1481 conn->c_currentber = NULL;
1483 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1484 /* log, close and send error */
1486 LDAP_LOG( CONNECTION, ERR,
1487 "connection_input: conn %lu ber_get_int returns 0x%lx.\n",
1488 conn->c_connid, tag, 0 );
1490 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1497 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1498 /* log, close and send error */
1500 LDAP_LOG( CONNECTION, ERR,
1501 "connection_input: conn %lu ber_peek_tag returns 0x%lx.\n",
1502 conn->c_connid, tag, 0 );
1504 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1512 #ifdef LDAP_CONNECTIONLESS
1513 if( conn->c_is_udp ) {
1514 if( tag == LBER_OCTETSTRING ) {
1515 ber_get_stringa( ber, &cdn );
1516 tag = ber_peek_tag(ber, &len);
1518 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1520 LDAP_LOG( CONNECTION, ERR,
1521 "connection_input: conn %lu invalid req for UDP 0x%lx.\n",
1522 conn->c_connid, tag, 0 );
1524 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0,
1532 if(tag == LDAP_REQ_BIND) {
1533 /* immediately abandon all exiting operations upon BIND */
1534 connection_abandon( conn );
1537 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1540 op->o_assertion = NULL;
1541 op->o_preread_attrs = NULL;
1542 op->o_postread_attrs = NULL;
1543 op->o_vrFilter = NULL;
1545 #ifdef LDAP_CONTROL_PAGEDRESULTS
1546 op->o_pagedresults_state = conn->c_pagedresults_state;
1549 op->o_res_ber = NULL;
1551 #ifdef LDAP_CONNECTIONLESS
1552 if (conn->c_is_udp) {
1554 ber_str2bv( cdn, 0, 1, &op->o_dn );
1555 op->o_protocol = LDAP_VERSION2;
1557 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1558 if (op->o_res_ber == NULL) return 1;
1560 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1561 sizeof(struct sockaddr), 0 );
1563 if (rc != sizeof(struct sockaddr)) {
1565 LDAP_LOG( CONNECTION, INFO,
1566 "connection_input: conn %lu ber_write failed\n",
1567 conn->c_connid, 0, 0 );
1569 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1574 if (op->o_protocol == LDAP_VERSION2) {
1575 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1578 LDAP_LOG( CONNECTION, INFO,
1579 "connection_input: conn %lu put outer sequence failed\n",
1580 conn->c_connid, 0, 0 );
1582 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1588 #endif /* LDAP_CONNECTIONLESS */
1592 /* Don't process requests when the conn is in the middle of a
1593 * Bind, or if it's closing. Also, don't let any single conn
1594 * use up all the available threads, and don't execute if we're
1595 * currently blocked on output. And don't execute if there are
1596 * already pending ops, let them go first.
1598 * But always allow Abandon through; it won't cost much.
1600 if ( tag != LDAP_REQ_ABANDON && (conn->c_conn_state == SLAP_C_BINDING
1601 || conn->c_conn_state == SLAP_C_CLOSING
1602 || conn->c_n_ops_executing >= connection_pool_max/2
1603 || conn->c_n_ops_pending
1604 || conn->c_writewaiter))
1606 int max = conn->c_dn.bv_len ? slap_conn_max_pending_auth
1607 : slap_conn_max_pending;
1609 LDAP_LOG( CONNECTION, INFO,
1610 "connection_input: conn %lu deferring operation\n",
1611 conn->c_connid, 0, 0 );
1613 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1615 conn->c_n_ops_pending++;
1616 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1617 if ( conn->c_n_ops_pending > max ) {
1623 conn->c_n_ops_executing++;
1624 connection_op_activate( op );
1628 if ( conn->c_struct_state != SLAP_C_USED ) {
1629 /* connection must have got closed underneath us */
1633 assert( conn->c_struct_state == SLAP_C_USED );
1639 connection_resched( Connection *conn )
1643 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1646 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1648 /* us trylock to avoid possible deadlock */
1649 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1653 LDAP_LOG( CONNECTION, DETAIL1,
1654 "connection_resched: conn %lu reaquiring locks.\n",
1655 conn->c_connid, 0, 0 );
1657 Debug( LDAP_DEBUG_TRACE,
1658 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1659 conn->c_connid, sd, 0 );
1662 * reaquire locks in the right order...
1663 * this may allow another thread to close this connection,
1664 * so recheck state below.
1666 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1667 ldap_pvt_thread_mutex_lock( &connections_mutex );
1668 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1671 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1673 LDAP_LOG( CONNECTION, INFO,
1674 "connection_resched: conn %lu closed by other thread.\n",
1675 conn->c_connid, 0, 0 );
1677 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1678 "closed by other thread conn=%lu sd=%d\n",
1679 conn->c_connid, sd, 0 );
1683 LDAP_LOG( CONNECTION, DETAIL1,
1684 "connection_resched: conn %lu attempting closing.\n",
1685 conn->c_connid, 0, 0 );
1687 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1688 "attempting closing conn=%lu sd=%d\n",
1689 conn->c_connid, sd, 0 );
1691 connection_close( conn );
1694 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1698 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1699 /* other states need different handling */
1703 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1704 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1707 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1708 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1709 /* pending operations should not be marked for abandonment */
1710 assert(!op->o_abandon);
1712 conn->c_n_ops_pending--;
1713 conn->c_n_ops_executing++;
1715 connection_op_activate( op );
1717 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1724 static int connection_op_activate( Operation *op )
1727 ber_tag_t tag = op->o_tag;
1729 if(tag == LDAP_REQ_BIND) {
1730 op->o_conn->c_conn_state = SLAP_C_BINDING;
1733 if (!op->o_dn.bv_len) {
1734 op->o_authz = op->o_conn->c_authz;
1735 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1736 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1738 op->o_authtype = op->o_conn->c_authtype;
1739 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1741 if (!op->o_protocol) {
1742 op->o_protocol = op->o_conn->c_protocol
1743 ? op->o_conn->c_protocol : LDAP_VERSION3;
1745 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1746 && op->o_protocol > LDAP_VERSION2) {
1747 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1750 op->o_connid = op->o_conn->c_connid;
1752 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1754 status = ldap_pvt_thread_pool_submit( &connection_pool,
1755 connection_operation, (void *) op );
1757 if ( status != 0 ) {
1759 LDAP_LOG( CONNECTION, ERR,
1760 "connection_op_activate: conn %lu thread pool submit failed.\n",
1761 op->o_connid, 0, 0 );
1763 Debug( LDAP_DEBUG_ANY,
1764 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1766 /* should move op to pending list */
1772 int connection_write(ber_socket_t s)
1776 assert( connections != NULL );
1778 ldap_pvt_thread_mutex_lock( &connections_mutex );
1780 c = connection_get( s );
1782 slapd_clr_write( s, 0);
1786 LDAP_LOG( CONNECTION, ERR,
1787 "connection_write: sock %ld no connection!\n", (long)s, 0, 0);
1789 Debug( LDAP_DEBUG_ANY,
1790 "connection_write(%ld): no connection!\n",
1794 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1801 LDAP_LOG( CONNECTION, DETAIL1,
1802 "connection_write conn %lu waking output.\n", c->c_connid, 0, 0 );
1804 Debug( LDAP_DEBUG_TRACE,
1805 "connection_write(%d): waking output for id=%lu\n",
1806 s, c->c_connid, 0 );
1808 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1810 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1811 slapd_set_read( s, 1 );
1812 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1813 slapd_set_write( s, 1 );
1814 connection_return( c );
1815 ldap_pvt_thread_mutex_unlock( &connections_mutex );