2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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.
33 #include <ac/socket.h>
35 #include <ac/string.h>
37 #include <ac/unistd.h>
43 #include "slapi/slapi.h"
46 /* protected by connections_mutex */
47 static ldap_pvt_thread_mutex_t connections_mutex;
48 static Connection *connections = NULL;
50 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
51 static unsigned long conn_nextid = 0;
53 static const char conn_lost_str[] = "connection lost";
55 /* structure state (protected by connections_mutex) */
56 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
57 #define SLAP_C_UNUSED 0x01
58 #define SLAP_C_USED 0x02
59 #define SLAP_C_PENDING 0x03
61 /* connection state (protected by c_mutex ) */
62 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
63 #define SLAP_C_INACTIVE 0x01 /* zero threads */
64 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
65 #define SLAP_C_BINDING 0x03 /* binding */
66 #define SLAP_C_CLOSING 0x04 /* closing */
67 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
70 connection_state2str( int state )
73 case SLAP_C_INVALID: return "!";
74 case SLAP_C_INACTIVE: return "|";
75 case SLAP_C_ACTIVE: return "";
76 case SLAP_C_BINDING: return "B";
77 case SLAP_C_CLOSING: return "C";
78 case SLAP_C_CLIENT: return "L";
84 static Connection* connection_get( ber_socket_t s );
86 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
88 typedef struct conn_readinfo {
90 ldap_pvt_thread_start_t *func;
95 static int connection_input( Connection *c, conn_readinfo *cri );
97 static int connection_input( Connection *c );
99 static void connection_close( Connection *c );
101 static int connection_op_activate( Operation *op );
102 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
103 static void connection_op_queue( Operation *op );
105 static int connection_resched( Connection *conn );
106 static void connection_abandon( Connection *conn );
107 static void connection_destroy( Connection *c );
109 static ldap_pvt_thread_start_t connection_operation;
112 * Initialize connection management infrastructure.
114 int connections_init(void)
118 assert( connections == NULL );
120 if( connections != NULL) {
121 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
126 /* should check return of every call */
127 ldap_pvt_thread_mutex_init( &connections_mutex );
128 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
130 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
132 if( connections == NULL ) {
133 Debug( LDAP_DEBUG_ANY, "connections_init: "
134 "allocation (%d*%ld) of connection array failed\n",
135 dtblsize, (long) sizeof(Connection), 0 );
139 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
140 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
142 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
145 * per entry initialization of the Connection array initialization
146 * will be done by connection_init()
153 * Destroy connection management infrastructure.
156 int connections_destroy(void)
160 /* should check return of every call */
162 if( connections == NULL) {
163 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
168 for ( i = 0; i < dtblsize; i++ ) {
169 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
170 ber_sockbuf_free( connections[i].c_sb );
171 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
172 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
173 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
175 if ( slapi_plugins_used ) {
176 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
186 ldap_pvt_thread_mutex_destroy( &connections_mutex );
187 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
192 * shutdown all connections
194 int connections_shutdown(void)
198 for ( i = 0; i < dtblsize; i++ ) {
199 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
200 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
201 if( connections[i].c_struct_state == SLAP_C_USED ) {
203 /* give persistent clients a chance to cleanup */
204 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
205 ldap_pvt_thread_pool_submit( &connection_pool,
206 connections[i].c_clientfunc, connections[i].c_clientarg );
208 /* c_mutex is locked */
209 connection_closing( &connections[i], "slapd shutdown" );
210 connection_close( &connections[i] );
213 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
221 * Timeout idle connections.
223 int connections_timeout_idle(time_t now)
229 for( c = connection_first( &connindex );
231 c = connection_next( c, &connindex ) )
233 /* Don't timeout a slow-running request or a persistent
234 * outbound connection */
235 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
239 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
241 connection_closing( c, "idletimeout" );
242 connection_close( c );
246 connection_done( c );
251 static Connection* connection_get( ber_socket_t s )
255 Debug( LDAP_DEBUG_ARGS,
256 "connection_get(%ld)\n",
259 assert( connections != NULL );
261 if(s == AC_SOCKET_INVALID) return NULL;
264 assert( s < dtblsize );
272 ldap_pvt_thread_mutex_lock( &connections_mutex );
273 for(i=0; i<dtblsize; i++) {
274 if( connections[i].c_struct_state == SLAP_C_PENDING )
277 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
278 assert( connections[i].c_conn_state == SLAP_C_INVALID );
279 assert( connections[i].c_sb == 0 );
283 ber_sockbuf_ctrl( connections[i].c_sb,
284 LBER_SB_OPT_GET_FD, &sd );
286 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
287 assert( connections[i].c_conn_state == SLAP_C_INVALID );
288 assert( sd == AC_SOCKET_INVALID );
292 /* state can actually change from used -> unused by resched,
293 * so don't assert details here.
301 ldap_pvt_thread_mutex_unlock( &connections_mutex );
308 ldap_pvt_thread_mutex_lock( &c->c_mutex );
310 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
312 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
313 if( c->c_struct_state != SLAP_C_USED ) {
314 /* connection must have been closed due to resched */
316 assert( c->c_conn_state == SLAP_C_INVALID );
317 assert( sd == AC_SOCKET_INVALID );
319 Debug( LDAP_DEBUG_TRACE,
320 "connection_get(%d): connection not used\n",
323 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
327 Debug( LDAP_DEBUG_TRACE,
328 "connection_get(%d): got connid=%lu\n",
333 assert( c->c_struct_state == SLAP_C_USED );
334 assert( c->c_conn_state != SLAP_C_INVALID );
335 assert( sd != AC_SOCKET_INVALID );
337 #ifndef SLAPD_MONITOR
338 if ( global_idletimeout > 0 )
339 #endif /* ! SLAPD_MONITOR */
341 c->c_activitytime = slap_get_time();
348 static void connection_return( Connection *c )
350 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
353 long connection_init(
357 const char* peername,
360 struct berval *authid )
366 assert( connections != NULL );
368 assert( listener != NULL );
369 assert( dnsname != NULL );
370 assert( peername != NULL );
373 assert( flags != CONN_IS_TLS );
376 if( s == AC_SOCKET_INVALID ) {
377 Debug( LDAP_DEBUG_ANY,
378 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
384 assert( s < dtblsize );
386 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
389 assert( c->c_struct_state == SLAP_C_UNUSED );
396 ldap_pvt_thread_mutex_lock( &connections_mutex );
397 for( i=0; i < dtblsize; i++) {
400 if ( connections[i].c_struct_state == SLAP_C_PENDING )
403 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
404 assert( connections[i].c_sb == 0 );
406 c->c_struct_state = SLAP_C_PENDING;
411 sd = AC_SOCKET_INVALID;
412 if (connections[i].c_sb != NULL) {
413 ber_sockbuf_ctrl( connections[i].c_sb,
414 LBER_SB_OPT_GET_FD, &sd );
417 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
418 assert( sd == AC_SOCKET_INVALID );
420 c->c_struct_state = SLAP_C_PENDING;
424 if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
426 assert( connections[i].c_struct_state == SLAP_C_USED );
427 assert( connections[i].c_conn_state != SLAP_C_INVALID );
428 assert( sd != AC_SOCKET_INVALID );
430 ldap_pvt_thread_mutex_unlock( &connections_mutex );
433 Debug( LDAP_DEBUG_ANY,
434 "connection_init(%d): connection table full "
435 "(%d/%d)\n", s, i, dtblsize);
442 c->c_send_ldap_result = slap_send_ldap_result;
443 c->c_send_search_entry = slap_send_search_entry;
444 c->c_send_search_reference = slap_send_search_reference;
445 c->c_send_ldap_extended = slap_send_ldap_extended;
446 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
448 BER_BVZERO( &c->c_authmech );
449 BER_BVZERO( &c->c_dn );
450 BER_BVZERO( &c->c_ndn );
452 c->c_listener = NULL;
453 BER_BVZERO( &c->c_peer_domain );
454 BER_BVZERO( &c->c_peer_name );
456 LDAP_STAILQ_INIT(&c->c_ops);
457 LDAP_STAILQ_INIT(&c->c_pending_ops);
460 c->c_txn = CONN_TXN_INACTIVE;
461 c->c_txn_backend = NULL;
462 LDAP_STAILQ_INIT(&c->c_txn_ops);
465 BER_BVZERO( &c->c_sasl_bind_mech );
467 c->c_sasl_authctx = NULL;
468 c->c_sasl_sockctx = NULL;
469 c->c_sasl_extra = NULL;
470 c->c_sasl_bindop = NULL;
472 c->c_sb = ber_sockbuf_alloc( );
475 ber_len_t max = sockbuf_max_incoming;
476 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
479 c->c_currentber = NULL;
481 /* should check status of thread calls */
482 ldap_pvt_thread_mutex_init( &c->c_mutex );
483 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
484 ldap_pvt_thread_cond_init( &c->c_write_cv );
487 if ( slapi_plugins_used ) {
488 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
493 ldap_pvt_thread_mutex_lock( &c->c_mutex );
495 assert( BER_BVISNULL( &c->c_authmech ) );
496 assert( BER_BVISNULL( &c->c_dn ) );
497 assert( BER_BVISNULL( &c->c_ndn ) );
498 assert( c->c_listener == NULL );
499 assert( BER_BVISNULL( &c->c_peer_domain ) );
500 assert( BER_BVISNULL( &c->c_peer_name ) );
501 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
502 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
504 assert( c->c_txn == CONN_TXN_INACTIVE );
505 assert( c->c_txn_backend == NULL );
506 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
508 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
509 assert( c->c_sasl_done == 0 );
510 assert( c->c_sasl_authctx == NULL );
511 assert( c->c_sasl_sockctx == NULL );
512 assert( c->c_sasl_extra == NULL );
513 assert( c->c_sasl_bindop == NULL );
514 assert( c->c_currentber == NULL );
515 assert( c->c_writewaiter == 0);
517 c->c_listener = listener;
519 if ( flags == CONN_IS_CLIENT ) {
520 c->c_conn_state = SLAP_C_CLIENT;
521 c->c_struct_state = SLAP_C_USED;
522 c->c_close_reason = "?"; /* should never be needed */
523 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
524 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
529 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
530 ber_str2bv( peername, 0, 1, &c->c_peer_name );
532 c->c_n_ops_received = 0;
533 c->c_n_ops_executing = 0;
534 c->c_n_ops_pending = 0;
535 c->c_n_ops_completed = 0;
541 /* set to zero until bind, implies LDAP_VERSION3 */
544 #ifndef SLAPD_MONITOR
545 if ( global_idletimeout > 0 )
546 #endif /* ! SLAPD_MONITOR */
548 c->c_activitytime = c->c_starttime = slap_get_time();
551 #ifdef LDAP_CONNECTIONLESS
553 if( flags == CONN_IS_UDP ) {
556 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
557 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
559 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
560 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
561 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
562 LBER_SBIOD_LEVEL_PROVIDER, NULL );
567 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
568 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
570 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
571 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
575 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
576 INT_MAX, (void*)"ldap_" );
579 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
580 c /* non-NULL */ ) < 0 )
582 Debug( LDAP_DEBUG_ANY,
583 "connection_init(%d, %s): set nonblocking failed\n",
584 s, c->c_peer_name.bv_val, 0 );
587 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
588 id = c->c_connid = conn_nextid++;
589 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
591 c->c_conn_state = SLAP_C_INACTIVE;
592 c->c_struct_state = SLAP_C_USED;
593 c->c_close_reason = "?"; /* should never be needed */
595 c->c_ssf = c->c_transport_ssf = ssf;
599 if ( flags == CONN_IS_TLS ) {
601 c->c_needs_tls_accept = 1;
604 c->c_needs_tls_accept = 0;
608 slap_sasl_open( c, 0 );
609 slap_sasl_external( c, ssf, authid );
611 slapd_add_internal( s, 1 );
612 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
614 backend_connection_init(c);
619 void connection2anonymous( Connection *c )
621 assert( connections != NULL );
625 ber_len_t max = sockbuf_max_incoming;
626 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
629 if ( !BER_BVISNULL( &c->c_authmech ) ) {
630 ch_free(c->c_authmech.bv_val);
632 BER_BVZERO( &c->c_authmech );
634 if ( !BER_BVISNULL( &c->c_dn ) ) {
635 ch_free(c->c_dn.bv_val);
637 BER_BVZERO( &c->c_dn );
639 if ( !BER_BVISNULL( &c->c_ndn ) ) {
640 ch_free(c->c_ndn.bv_val);
642 BER_BVZERO( &c->c_ndn );
644 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
645 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
647 BER_BVZERO( &c->c_sasl_authz_dn );
649 c->c_authz_backend = NULL;
653 connection_destroy( Connection *c )
656 unsigned long connid;
657 const char *close_reason;
660 assert( connections != NULL );
662 assert( c->c_struct_state != SLAP_C_UNUSED );
663 assert( c->c_conn_state != SLAP_C_INVALID );
664 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
665 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
667 assert( c->c_txn == CONN_TXN_INACTIVE );
668 assert( c->c_txn_backend == NULL );
669 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
671 assert( c->c_writewaiter == 0);
673 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
674 connid = c->c_connid;
675 close_reason = c->c_close_reason;
677 ldap_pvt_thread_mutex_lock( &connections_mutex );
678 c->c_struct_state = SLAP_C_PENDING;
679 ldap_pvt_thread_mutex_unlock( &connections_mutex );
681 backend_connection_destroy(c);
686 c->c_activitytime = c->c_starttime = 0;
688 connection2anonymous( c );
689 c->c_listener = NULL;
691 if(c->c_peer_domain.bv_val != NULL) {
692 free(c->c_peer_domain.bv_val);
694 BER_BVZERO( &c->c_peer_domain );
695 if(c->c_peer_name.bv_val != NULL) {
696 free(c->c_peer_name.bv_val);
698 BER_BVZERO( &c->c_peer_name );
700 c->c_sasl_bind_in_progress = 0;
701 if(c->c_sasl_bind_mech.bv_val != NULL) {
702 free(c->c_sasl_bind_mech.bv_val);
704 BER_BVZERO( &c->c_sasl_bind_mech );
706 slap_sasl_close( c );
708 if ( c->c_currentber != NULL ) {
709 ber_free( c->c_currentber, 1 );
710 c->c_currentber = NULL;
715 /* call destructors, then constructors; avoids unnecessary allocation */
716 if ( slapi_plugins_used ) {
717 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
721 c->c_conn_state = SLAP_C_INVALID;
722 c->c_struct_state = SLAP_C_UNUSED;
723 c->c_close_reason = "?"; /* should never be needed */
726 c->c_sb = ber_sockbuf_alloc( );
728 ber_len_t max = sockbuf_max_incoming;
729 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
732 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
734 /* c must be fully reset by this point; when we call slapd_remove
735 * it may get immediately reused by a new connection.
737 if ( sd != AC_SOCKET_INVALID ) {
738 slapd_remove( sd, sb, 1, 0, 0 );
740 if ( close_reason == NULL ) {
744 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
745 connid, (long) sd, close_reason, 0, 0 );
749 int connection_state_closing( Connection *c )
751 /* c_mutex must be locked by caller */
755 assert( c->c_struct_state == SLAP_C_USED );
757 state = c->c_conn_state;
759 assert( state != SLAP_C_INVALID );
761 return state == SLAP_C_CLOSING;
764 static void connection_abandon( Connection *c )
766 /* c_mutex must be locked by caller */
768 Operation *o, *next, op = {0};
774 op.o_connid = c->c_connid;
775 op.o_tag = LDAP_REQ_ABANDON;
777 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
778 next = LDAP_STAILQ_NEXT( o, o_next );
779 op.orn_msgid = o->o_msgid;
781 op.o_bd = frontendDB;
782 frontendDB->be_abandon( &op, &rs );
786 /* remove operations in pending transaction */
787 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
788 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
789 LDAP_STAILQ_NEXT(o, o_next) = NULL;
793 /* clear transaction */
794 c->c_txn_backend = NULL;
795 c->c_txn = CONN_TXN_INACTIVE;
798 /* remove pending operations */
799 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
800 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
801 LDAP_STAILQ_NEXT(o, o_next) = NULL;
806 void connection_closing( Connection *c, const char *why )
808 assert( connections != NULL );
810 assert( c->c_struct_state == SLAP_C_USED );
811 assert( c->c_conn_state != SLAP_C_INVALID );
813 /* c_mutex must be locked by caller */
815 if( c->c_conn_state != SLAP_C_CLOSING ) {
818 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
819 Debug( LDAP_DEBUG_TRACE,
820 "connection_closing: readying conn=%lu sd=%d for close\n",
821 c->c_connid, sd, 0 );
822 /* update state to closing */
823 c->c_conn_state = SLAP_C_CLOSING;
824 c->c_close_reason = why;
826 /* don't listen on this port anymore */
827 slapd_clr_read( sd, 0 );
829 /* abandon active operations */
830 connection_abandon( c );
832 /* wake write blocked operations */
833 if ( c->c_writewaiter ) {
834 ldap_pvt_thread_cond_signal( &c->c_write_cv );
835 /* ITS#4667 this may allow another thread to drop into
836 * connection_resched / connection_close before we
837 * finish, but that's OK.
839 slapd_clr_write( sd, 1 );
840 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
841 ldap_pvt_thread_mutex_lock( &c->c_write_mutex );
842 ldap_pvt_thread_mutex_lock( &c->c_mutex );
843 ldap_pvt_thread_mutex_unlock( &c->c_write_mutex );
845 slapd_clr_write( sd, 1 );
848 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
849 /* Client closed connection after doing Unbind. */
850 c->c_close_reason = NULL;
855 connection_close( Connection *c )
857 ber_socket_t sd = AC_SOCKET_INVALID;
859 assert( connections != NULL );
862 /* ITS#4667 we may have gotten here twice */
863 if ( c->c_conn_state == SLAP_C_INVALID )
866 assert( c->c_struct_state == SLAP_C_USED );
867 assert( c->c_conn_state == SLAP_C_CLOSING );
869 /* NOTE: c_mutex should be locked by caller */
871 /* NOTE: don't get the file descriptor if not needed */
872 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
873 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
876 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
877 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
879 Debug( LDAP_DEBUG_TRACE,
880 "connection_close: deferring conn=%lu sd=%d\n",
881 c->c_connid, sd, 0 );
885 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
886 c->c_connid, sd, 0 );
888 connection_destroy( c );
891 unsigned long connections_nextid(void)
894 assert( connections != NULL );
896 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
900 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
905 Connection* connection_first( ber_socket_t *index )
907 assert( connections != NULL );
908 assert( index != NULL );
910 ldap_pvt_thread_mutex_lock( &connections_mutex );
911 for( *index = 0; *index < dtblsize; (*index)++) {
912 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
916 ldap_pvt_thread_mutex_unlock( &connections_mutex );
918 return connection_next(NULL, index);
921 Connection* connection_next( Connection *c, ber_socket_t *index )
923 assert( connections != NULL );
924 assert( index != NULL );
925 assert( *index <= dtblsize );
927 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
931 ldap_pvt_thread_mutex_lock( &connections_mutex );
932 for(; *index < dtblsize; (*index)++) {
934 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
935 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
943 if( connections[*index].c_struct_state == SLAP_C_USED ) {
944 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
945 c = &connections[(*index)++];
946 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
948 ldap_pvt_thread_mutex_unlock( &connections_mutex );
949 ldap_pvt_thread_mutex_lock( &c->c_mutex );
950 ldap_pvt_thread_mutex_lock( &connections_mutex );
951 if ( c->c_struct_state != SLAP_C_USED ) {
952 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
960 c_struct = connections[*index].c_struct_state;
961 if ( c_struct == SLAP_C_PENDING )
963 assert( c_struct == SLAP_C_UNUSED );
964 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
967 ldap_pvt_thread_mutex_unlock( &connections_mutex );
971 void connection_done( Connection *c )
973 assert( connections != NULL );
975 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
979 * connection_activity - handle the request operation op on connection
980 * conn. This routine figures out what kind of operation it is and
981 * calls the appropriate stub to handle it.
985 /* FIXME: returns 0 in case of failure */
986 #define INCR_OP_INITIATED(index) \
988 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
989 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
990 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
992 #define INCR_OP_COMPLETED(index) \
994 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
995 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
996 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
997 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
999 #else /* !SLAPD_MONITOR */
1000 #define INCR_OP_INITIATED(index) do { } while (0)
1001 #define INCR_OP_COMPLETED(index) \
1003 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1004 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1005 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1007 #endif /* !SLAPD_MONITOR */
1010 * NOTE: keep in sync with enum in slapd.h
1012 static BI_op_func *opfun[] = {
1027 connection_operation( void *ctx, void *arg_v )
1029 int rc = LDAP_OTHER;
1030 Operation *op = arg_v;
1031 SlapReply rs = {REP_RESULT};
1032 ber_tag_t tag = op->o_tag;
1033 slap_op_t opidx = SLAP_OP_LAST;
1034 Connection *conn = op->o_conn;
1035 void *memctx = NULL;
1036 void *memctx_null = NULL;
1039 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1040 /* FIXME: returns 0 in case of failure */
1041 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1042 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1044 op->o_threadctx = ctx;
1045 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1049 case LDAP_REQ_UNBIND:
1051 case LDAP_REQ_DELETE:
1052 case LDAP_REQ_MODDN:
1053 case LDAP_REQ_MODIFY:
1054 case LDAP_REQ_COMPARE:
1055 case LDAP_REQ_SEARCH:
1056 case LDAP_REQ_ABANDON:
1057 case LDAP_REQ_EXTENDED:
1060 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1061 "conn %lu unknown LDAP request 0x%lx\n",
1062 conn->c_connid, tag, 0 );
1063 op->o_tag = LBER_ERROR;
1064 rs.sr_err = LDAP_PROTOCOL_ERROR;
1065 rs.sr_text = "unknown LDAP request";
1066 send_ldap_disconnect( op, &rs );
1067 rc = SLAPD_DISCONNECT;
1068 goto operations_error;
1071 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1072 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1073 "error: SASL bind in progress (tag=%ld).\n",
1075 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1076 "SASL bind in progress" );
1077 rc = LDAP_OPERATIONS_ERROR;
1078 goto operations_error;
1082 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1083 ( tag == LDAP_REQ_ADD ) ||
1084 ( tag == LDAP_REQ_DELETE ) ||
1085 ( tag == LDAP_REQ_MODIFY ) ||
1086 ( tag == LDAP_REQ_MODRDN )))
1088 /* Disable SLAB allocator for all update operations
1089 issued inside of a transaction */
1090 op->o_tmpmemctx = NULL;
1091 op->o_tmpmfuncs = &ch_mfuncs;
1095 /* We can use Thread-Local storage for most mallocs. We can
1096 * also use TL for ber parsing, but not on Add or Modify.
1099 memsiz = ber_len( op->o_ber ) * 64;
1100 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1102 memsiz = SLAP_SLAB_SIZE;
1104 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1105 op->o_tmpmemctx = memctx;
1106 op->o_tmpmfuncs = &slap_sl_mfuncs;
1107 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1108 /* Note - the ber and its buffer are already allocated from
1109 * regular memory; this only affects subsequent mallocs that
1110 * ber_scanf may invoke.
1112 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1116 opidx = slap_req2op( tag );
1117 assert( opidx != SLAP_OP_LAST );
1118 INCR_OP_INITIATED( opidx );
1119 rc = (*(opfun[opidx]))( op, &rs );
1122 if ( rc == SLAPD_DISCONNECT ) {
1125 } else if ( opidx != SLAP_OP_LAST ) {
1126 /* increment completed operations count
1127 * only if operation was initiated
1128 * and rc != SLAPD_DISCONNECT */
1129 INCR_OP_COMPLETED( opidx );
1132 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1133 if ( rc == SLAPD_ABANDON ) {
1134 op->o_cancel = SLAP_CANCEL_ACK;
1136 op->o_cancel = LDAP_TOO_LATE;
1140 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1141 op->o_cancel != SLAP_CANCEL_DONE )
1143 ldap_pvt_thread_yield();
1146 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1148 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1150 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1151 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1153 conn->c_n_ops_executing--;
1154 conn->c_n_ops_completed++;
1158 case LDAP_REQ_UNBIND:
1159 /* c_mutex is locked */
1160 connection_closing( conn,
1161 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1165 connection_resched( conn );
1166 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1170 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1172 int connection_client_setup(
1174 ldap_pvt_thread_start_t *func,
1180 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1181 CONN_IS_CLIENT, 0, NULL );
1182 if ( rc < 0 ) return -1;
1184 c = connection_get( s );
1185 c->c_clientfunc = func;
1186 c->c_clientarg = arg;
1188 slapd_add_internal( s, 0 );
1189 slapd_set_read( s, 1 );
1190 connection_return( c );
1194 void connection_client_enable(
1197 slapd_set_read( s, 1 );
1200 void connection_client_stop(
1206 /* get (locked) connection */
1207 c = connection_get( s );
1209 assert( c->c_conn_state == SLAP_C_CLIENT );
1211 c->c_listener = NULL;
1212 c->c_conn_state = SLAP_C_INVALID;
1213 c->c_struct_state = SLAP_C_UNUSED;
1214 c->c_close_reason = "?"; /* should never be needed */
1216 c->c_sb = ber_sockbuf_alloc( );
1218 ber_len_t max = sockbuf_max_incoming;
1219 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1221 slapd_remove( s, sb, 0, 1, 0 );
1223 connection_return( c );
1226 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1228 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1230 static void* connection_read_thread( void* ctx, void* argv )
1233 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1234 ber_socket_t s = (long)argv;
1237 * read incoming LDAP requests. If there is more than one,
1238 * the first one is returned with new_op
1240 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1241 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1242 return (void*)(long)rc;
1245 /* execute a single queued request in the same thread */
1246 if( cri.op && !cri.nullop ) {
1247 rc = (long)connection_operation( ctx, cri.op );
1248 } else if ( cri.func ) {
1249 rc = (long)cri.func( ctx, cri.arg );
1252 return (void*)(long)rc;
1255 int connection_read_activate( ber_socket_t s )
1260 * suspend reading on this file descriptor until a connection processing
1261 * thread reads data on it. Otherwise the listener thread will repeatedly
1262 * submit the same event on it to the pool.
1264 rc = slapd_clr_read( s, 0 );
1268 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1269 connection_read_thread, (void *)(long)s );
1272 Debug( LDAP_DEBUG_ANY,
1273 "connection_read_activate(%d): submit failed (%d)\n",
1281 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1283 connection_read( ber_socket_t s, conn_readinfo *cri )
1285 int connection_read(ber_socket_t s)
1291 assert( connections != NULL );
1293 /* get (locked) connection */
1294 c = connection_get( s );
1297 Debug( LDAP_DEBUG_ANY,
1298 "connection_read(%ld): no connection!\n",
1306 if( c->c_conn_state == SLAP_C_CLOSING ) {
1307 Debug( LDAP_DEBUG_TRACE,
1308 "connection_read(%d): closing, ignoring input for id=%lu\n",
1309 s, c->c_connid, 0 );
1310 connection_return( c );
1314 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1315 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1316 cri->func = c->c_clientfunc;
1317 cri->arg = c->c_clientarg;
1318 /* read should already be cleared */
1320 slapd_clr_read( s, 0 );
1321 ldap_pvt_thread_pool_submit( &connection_pool,
1322 c->c_clientfunc, c->c_clientarg );
1324 connection_return( c );
1328 Debug( LDAP_DEBUG_TRACE,
1329 "connection_read(%d): checking for input on id=%lu\n",
1330 s, c->c_connid, 0 );
1333 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1334 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1336 Debug( LDAP_DEBUG_TRACE,
1337 "connection_read(%d): TLS accept failure "
1338 "error=%d id=%lu, closing\n",
1339 s, rc, c->c_connid );
1341 c->c_needs_tls_accept = 0;
1342 /* c_mutex is locked */
1343 connection_closing( c, "TLS negotiation failure" );
1344 connection_close( c );
1345 connection_return( c );
1348 } else if ( rc == 0 ) {
1350 struct berval authid = BER_BVNULL;
1352 c->c_needs_tls_accept = 0;
1354 /* we need to let SASL know */
1355 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1357 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1358 if( c->c_tls_ssf > c->c_ssf ) {
1359 c->c_ssf = c->c_tls_ssf;
1362 rc = dnX509peerNormalize( ssl, &authid );
1363 if ( rc != LDAP_SUCCESS ) {
1364 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1365 "unable to get TLS client DN, error=%d id=%lu\n",
1366 s, rc, c->c_connid );
1368 Statslog( LDAP_DEBUG_STATS,
1369 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1370 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1371 slap_sasl_external( c, c->c_tls_ssf, &authid );
1372 if ( authid.bv_val ) free( authid.bv_val );
1375 /* if success and data is ready, fall thru to data input loop */
1376 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1378 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1379 slapd_set_read( s, 1 );
1382 connection_return( c );
1388 #ifdef HAVE_CYRUS_SASL
1389 if ( c->c_sasl_layers ) {
1390 /* If previous layer is not removed yet, give up for now */
1391 if ( !c->c_sasl_sockctx ) {
1392 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1393 slapd_set_read( s, 1 );
1396 connection_return( c );
1400 c->c_sasl_layers = 0;
1402 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1403 if( rc != LDAP_SUCCESS ) {
1404 Debug( LDAP_DEBUG_TRACE,
1405 "connection_read(%d): SASL install error "
1406 "error=%d id=%lu, closing\n",
1407 s, rc, c->c_connid );
1409 /* c_mutex is locked */
1410 connection_closing( c, "SASL layer install failure" );
1411 connection_close( c );
1412 connection_return( c );
1418 #define CONNECTION_INPUT_LOOP 1
1419 /* #define DATA_READY_LOOP 1 */
1422 /* How do we do this without getting into a busy loop ? */
1423 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1424 rc = connection_input( c, cri );
1426 rc = connection_input( c );
1429 #ifdef DATA_READY_LOOP
1430 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1431 #elif CONNECTION_INPUT_LOOP
1438 Debug( LDAP_DEBUG_CONNS,
1439 "connection_read(%d): input error=%d id=%lu, closing.\n",
1440 s, rc, c->c_connid );
1442 /* c_mutex is locked */
1443 connection_closing( c, conn_lost_str );
1444 connection_close( c );
1445 connection_return( c );
1449 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1450 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1451 slapd_set_write( s, 0 );
1454 slapd_set_read( s, 1 );
1456 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1457 slapd_set_read( s, 1 );
1460 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1461 slapd_set_write( s, 1 );
1465 connection_return( c );
1471 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1472 connection_input( Connection *conn , conn_readinfo *cri )
1474 connection_input( Connection *conn )
1483 #ifdef LDAP_CONNECTIONLESS
1489 if ( conn->c_currentber == NULL &&
1490 ( conn->c_currentber = ber_alloc()) == NULL )
1492 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1498 #ifdef LDAP_CONNECTIONLESS
1499 if ( conn->c_is_udp ) {
1500 char peername[sizeof("IP=255.255.255.255:65336")];
1502 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1503 if (len != sizeof(struct sockaddr)) return 1;
1505 sprintf( peername, "IP=%s:%d",
1506 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1507 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1508 Statslog( LDAP_DEBUG_STATS,
1509 "conn=%lu UDP request from %s (%s) accepted.\n",
1510 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1514 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1515 if ( tag != LDAP_TAG_MESSAGE ) {
1516 int err = sock_errno();
1519 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1521 Debug( LDAP_DEBUG_TRACE,
1522 "ber_get_next on fd %d failed errno=%d (%s)\n",
1523 sd, err, sock_errstr(err) );
1524 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1525 /* log, close and send error */
1526 ber_free( conn->c_currentber, 1 );
1527 conn->c_currentber = NULL;
1534 ber = conn->c_currentber;
1535 conn->c_currentber = NULL;
1537 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1538 /* log, close and send error */
1539 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1544 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1545 /* log, close and send error */
1546 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1552 #ifdef LDAP_CONNECTIONLESS
1553 if( conn->c_is_udp ) {
1554 if( tag == LBER_OCTETSTRING ) {
1555 ber_get_stringa( ber, &cdn );
1556 tag = ber_peek_tag(ber, &len);
1558 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1559 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1566 if(tag == LDAP_REQ_BIND) {
1567 /* immediately abandon all existing operations upon BIND */
1568 connection_abandon( conn );
1571 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1574 /* clear state if the connection is being reused from inactive */
1575 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1576 memset( &conn->c_pagedresults_state, 0,
1577 sizeof( conn->c_pagedresults_state ) );
1580 op->o_res_ber = NULL;
1582 #ifdef LDAP_CONNECTIONLESS
1583 if (conn->c_is_udp) {
1585 ber_str2bv( cdn, 0, 1, &op->o_dn );
1586 op->o_protocol = LDAP_VERSION2;
1588 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1589 if (op->o_res_ber == NULL) return 1;
1591 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1592 sizeof(struct sockaddr), 0 );
1594 if (rc != sizeof(struct sockaddr)) {
1595 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1599 if (op->o_protocol == LDAP_VERSION2) {
1600 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1602 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1607 #endif /* LDAP_CONNECTIONLESS */
1611 /* Don't process requests when the conn is in the middle of a
1612 * Bind, or if it's closing. Also, don't let any single conn
1613 * use up all the available threads, and don't execute if we're
1614 * currently blocked on output. And don't execute if there are
1615 * already pending ops, let them go first. Abandon operations
1616 * get exceptions to some, but not all, cases.
1620 /* Abandon and Unbind are exempt from these checks */
1621 if (conn->c_conn_state == SLAP_C_CLOSING) {
1624 } else if (conn->c_writewaiter) {
1625 defer = "awaiting write";
1627 } else if (conn->c_n_ops_pending) {
1628 defer = "pending operations";
1632 case LDAP_REQ_ABANDON:
1633 /* Unbind is exempt from these checks */
1634 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1635 defer = "too many executing";
1637 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1642 case LDAP_REQ_UNBIND:
1647 int max = conn->c_dn.bv_len
1648 ? slap_conn_max_pending_auth
1649 : slap_conn_max_pending;
1651 Debug( LDAP_DEBUG_ANY,
1652 "connection_input: conn=%lu deferring operation: %s\n",
1653 conn->c_connid, defer, 0 );
1654 conn->c_n_ops_pending++;
1655 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1656 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1659 conn->c_n_ops_executing++;
1661 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1663 * The first op will be processed in the same thread context,
1664 * as long as there is only one op total.
1665 * Subsequent ops will be submitted to the pool by
1666 * calling connection_op_activate()
1668 if ( cri->op == NULL ) {
1669 /* the first incoming request */
1670 connection_op_queue( op );
1673 if ( !cri->nullop ) {
1675 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1676 connection_operation, (void *) cri->op );
1678 connection_op_activate( op );
1681 connection_op_activate( op );
1686 if ( conn->c_struct_state != SLAP_C_USED ) {
1687 /* connection must have got closed underneath us */
1692 assert( conn->c_struct_state == SLAP_C_USED );
1697 connection_resched( Connection *conn )
1701 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1703 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1705 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1706 "attempting closing conn=%lu sd=%d\n",
1707 conn->c_connid, sd, 0 );
1708 connection_close( conn );
1712 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1713 /* other states need different handling */
1717 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1718 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1720 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1721 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1723 /* pending operations should not be marked for abandonment */
1724 assert(!op->o_abandon);
1726 conn->c_n_ops_pending--;
1727 conn->c_n_ops_executing++;
1729 connection_op_activate( op );
1731 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1737 connection_init_log_prefix( Operation *op )
1739 if ( op->o_connid == (unsigned long)(-1) ) {
1740 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1741 "conn=-1 op=%lu", op->o_opid );
1744 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1745 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1749 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1751 op->o_conn->c_sasl_bindop = NULL;
1753 ch_free( op->o_callback );
1754 op->o_callback = NULL;
1756 return SLAP_CB_CONTINUE;
1759 static int connection_bind_cb( Operation *op, SlapReply *rs )
1761 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1762 if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1763 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1764 op->o_conn->c_sasl_bind_in_progress =
1765 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1767 /* Moved here from bind.c due to ITS#4158 */
1768 op->o_conn->c_sasl_bindop = NULL;
1769 if ( op->orb_method == LDAP_AUTH_SASL ) {
1770 if( rs->sr_err == LDAP_SUCCESS ) {
1771 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1772 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1773 /* edn is always normalized already */
1774 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1776 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1777 BER_BVZERO( &op->orb_edn );
1778 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1779 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1781 op->o_conn->c_sasl_ssf = op->orb_ssf;
1782 if( op->orb_ssf > op->o_conn->c_ssf ) {
1783 op->o_conn->c_ssf = op->orb_ssf;
1786 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1787 ber_len_t max = sockbuf_max_incoming_auth;
1788 ber_sockbuf_ctrl( op->o_conn->c_sb,
1789 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1792 /* log authorization identity */
1793 Statslog( LDAP_DEBUG_STATS,
1794 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
1796 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1797 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
1799 Debug( LDAP_DEBUG_TRACE,
1800 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
1801 op->o_conn->c_authmech.bv_val,
1802 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1805 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1806 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1807 free( op->o_conn->c_sasl_bind_mech.bv_val );
1808 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1812 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1814 ch_free( op->o_callback );
1815 op->o_callback = NULL;
1817 return SLAP_CB_CONTINUE;
1820 static void connection_op_queue( Operation *op )
1822 ber_tag_t tag = op->o_tag;
1824 if (tag == LDAP_REQ_BIND) {
1825 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1826 sc->sc_response = connection_bind_cb;
1827 sc->sc_cleanup = connection_bind_cleanup_cb;
1828 sc->sc_next = op->o_callback;
1829 op->o_callback = sc;
1830 op->o_conn->c_conn_state = SLAP_C_BINDING;
1833 if (!op->o_dn.bv_len) {
1834 op->o_authz = op->o_conn->c_authz;
1835 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1836 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1837 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1839 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1840 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1844 op->o_authtype = op->o_conn->c_authtype;
1845 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1847 if (!op->o_protocol) {
1848 op->o_protocol = op->o_conn->c_protocol
1849 ? op->o_conn->c_protocol : LDAP_VERSION3;
1852 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1853 op->o_protocol > LDAP_VERSION2)
1855 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1858 op->o_connid = op->o_conn->c_connid;
1859 connection_init_log_prefix( op );
1861 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1864 static int connection_op_activate( Operation *op )
1868 connection_op_queue( op );
1870 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1871 connection_operation, (void *) op );
1874 Debug( LDAP_DEBUG_ANY,
1875 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1876 rc, op->o_connid, 0 );
1877 /* should move op to pending list */
1883 int connection_write(ber_socket_t s)
1888 assert( connections != NULL );
1890 slapd_clr_write( s, 0 );
1892 c = connection_get( s );
1894 Debug( LDAP_DEBUG_ANY,
1895 "connection_write(%ld): no connection!\n",
1902 Debug( LDAP_DEBUG_TRACE,
1903 "connection_write(%d): waking output for id=%lu\n",
1904 s, c->c_connid, 0 );
1905 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1907 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1908 slapd_set_read( s, 1 );
1910 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1911 slapd_set_write( s, 1 );
1914 /* If there are ops pending because of a writewaiter,
1917 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1918 if ( !c->c_writewaiter ) break;
1919 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1921 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1922 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1924 /* pending operations should not be marked for abandonment */
1925 assert(!op->o_abandon);
1927 c->c_n_ops_pending--;
1928 c->c_n_ops_executing++;
1930 connection_op_activate( op );
1935 connection_return( c );
1940 connection_fake_init(
1945 conn->c_connid = -1;
1946 conn->c_send_ldap_result = slap_send_ldap_result;
1947 conn->c_send_search_entry = slap_send_search_entry;
1948 conn->c_send_search_reference = slap_send_search_reference;
1949 conn->c_listener = (Listener *)&dummy_list;
1950 conn->c_peer_domain = slap_empty_bv;
1951 conn->c_peer_name = slap_empty_bv;
1953 memset(op, 0, OPERATION_BUFFER_SIZE);
1954 op->o_hdr = (Opheader *)(op+1);
1955 op->o_controls = (void **)(op->o_hdr+1);
1956 /* set memory context */
1957 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
1958 op->o_tmpmfuncs = &slap_sl_mfuncs;
1959 op->o_threadctx = ctx;
1960 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1963 op->o_connid = op->o_conn->c_connid;
1964 connection_init_log_prefix( op );
1967 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
1968 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
1969 #endif /* LDAP_SLAPI */
1971 slap_op_time( &op->o_time, &op->o_tincr );
1975 connection_assign_nextid( Connection *conn )
1977 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1978 conn->c_connid = conn_nextid++;
1979 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );