2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 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 );
314 /* Avoid race condition after releasing
318 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
322 if( c->c_struct_state != SLAP_C_USED ) {
323 /* connection must have been closed due to resched */
325 assert( c->c_conn_state == SLAP_C_INVALID );
326 assert( sd == AC_SOCKET_INVALID );
328 Debug( LDAP_DEBUG_TRACE,
329 "connection_get(%d): connection not used\n",
332 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
336 Debug( LDAP_DEBUG_TRACE,
337 "connection_get(%d): got connid=%lu\n",
342 assert( c->c_struct_state == SLAP_C_USED );
343 assert( c->c_conn_state != SLAP_C_INVALID );
344 assert( sd != AC_SOCKET_INVALID );
346 #ifndef SLAPD_MONITOR
347 if ( global_idletimeout > 0 )
348 #endif /* ! SLAPD_MONITOR */
350 c->c_activitytime = slap_get_time();
357 static void connection_return( Connection *c )
359 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
362 Connection * connection_init(
366 const char* peername,
369 struct berval *authid
370 LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
376 assert( connections != NULL );
378 assert( listener != NULL );
379 assert( dnsname != NULL );
380 assert( peername != NULL );
383 assert( !( flags & CONN_IS_TLS ));
386 if( s == AC_SOCKET_INVALID ) {
387 Debug( LDAP_DEBUG_ANY,
388 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
394 assert( s < dtblsize );
396 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
399 assert( c->c_struct_state == SLAP_C_UNUSED );
406 ldap_pvt_thread_mutex_lock( &connections_mutex );
407 for( i=0; i < dtblsize; i++) {
410 if ( connections[i].c_struct_state == SLAP_C_PENDING )
413 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
414 assert( connections[i].c_sb == 0 );
416 c->c_struct_state = SLAP_C_PENDING;
421 sd = AC_SOCKET_INVALID;
422 if (connections[i].c_sb != NULL) {
423 ber_sockbuf_ctrl( connections[i].c_sb,
424 LBER_SB_OPT_GET_FD, &sd );
427 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
428 assert( sd == AC_SOCKET_INVALID );
430 c->c_struct_state = SLAP_C_PENDING;
434 if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
436 assert( connections[i].c_struct_state == SLAP_C_USED );
437 assert( connections[i].c_conn_state != SLAP_C_INVALID );
438 assert( sd != AC_SOCKET_INVALID );
440 ldap_pvt_thread_mutex_unlock( &connections_mutex );
443 Debug( LDAP_DEBUG_ANY,
444 "connection_init(%d): connection table full "
445 "(%d/%d)\n", s, i, dtblsize);
452 c->c_send_ldap_result = slap_send_ldap_result;
453 c->c_send_search_entry = slap_send_search_entry;
454 c->c_send_search_reference = slap_send_search_reference;
455 c->c_send_ldap_extended = slap_send_ldap_extended;
456 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
458 BER_BVZERO( &c->c_authmech );
459 BER_BVZERO( &c->c_dn );
460 BER_BVZERO( &c->c_ndn );
462 c->c_listener = NULL;
463 BER_BVZERO( &c->c_peer_domain );
464 BER_BVZERO( &c->c_peer_name );
466 LDAP_STAILQ_INIT(&c->c_ops);
467 LDAP_STAILQ_INIT(&c->c_pending_ops);
470 c->c_txn = CONN_TXN_INACTIVE;
471 c->c_txn_backend = NULL;
472 LDAP_STAILQ_INIT(&c->c_txn_ops);
475 BER_BVZERO( &c->c_sasl_bind_mech );
477 c->c_sasl_authctx = NULL;
478 c->c_sasl_sockctx = NULL;
479 c->c_sasl_extra = NULL;
480 c->c_sasl_bindop = NULL;
482 c->c_sb = ber_sockbuf_alloc( );
485 ber_len_t max = sockbuf_max_incoming;
486 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
489 c->c_currentber = NULL;
491 /* should check status of thread calls */
492 ldap_pvt_thread_mutex_init( &c->c_mutex );
493 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
494 ldap_pvt_thread_cond_init( &c->c_write_cv );
497 if ( slapi_plugins_used ) {
498 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
503 ldap_pvt_thread_mutex_lock( &c->c_mutex );
505 assert( BER_BVISNULL( &c->c_authmech ) );
506 assert( BER_BVISNULL( &c->c_dn ) );
507 assert( BER_BVISNULL( &c->c_ndn ) );
508 assert( c->c_listener == NULL );
509 assert( BER_BVISNULL( &c->c_peer_domain ) );
510 assert( BER_BVISNULL( &c->c_peer_name ) );
511 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
512 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
514 assert( c->c_txn == CONN_TXN_INACTIVE );
515 assert( c->c_txn_backend == NULL );
516 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
518 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
519 assert( c->c_sasl_done == 0 );
520 assert( c->c_sasl_authctx == NULL );
521 assert( c->c_sasl_sockctx == NULL );
522 assert( c->c_sasl_extra == NULL );
523 assert( c->c_sasl_bindop == NULL );
524 assert( c->c_currentber == NULL );
525 assert( c->c_writewaiter == 0);
527 c->c_listener = listener;
529 if ( flags & CONN_IS_CLIENT ) {
531 c->c_conn_state = SLAP_C_CLIENT;
532 c->c_struct_state = SLAP_C_USED;
533 c->c_close_reason = "?"; /* should never be needed */
534 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
535 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
540 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
541 ber_str2bv( peername, 0, 1, &c->c_peer_name );
543 c->c_n_ops_received = 0;
544 c->c_n_ops_executing = 0;
545 c->c_n_ops_pending = 0;
546 c->c_n_ops_completed = 0;
552 /* set to zero until bind, implies LDAP_VERSION3 */
555 #ifndef SLAPD_MONITOR
556 if ( global_idletimeout > 0 )
557 #endif /* ! SLAPD_MONITOR */
559 c->c_activitytime = c->c_starttime = slap_get_time();
562 #ifdef LDAP_CONNECTIONLESS
564 if( flags & CONN_IS_UDP ) {
567 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
568 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
570 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
571 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
572 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
573 LBER_SBIOD_LEVEL_PROVIDER, NULL );
575 #endif /* LDAP_CONNECTIONLESS */
577 if ( flags & CONN_IS_IPC ) {
579 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
580 LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
582 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
583 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
584 #ifdef LDAP_PF_LOCAL_SENDMSG
585 if ( !BER_BVISEMPTY( peerbv ))
586 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
589 #endif /* LDAP_PF_LOCAL */
592 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
593 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
595 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
596 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
600 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
601 INT_MAX, (void*)"ldap_" );
604 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
605 c /* non-NULL */ ) < 0 )
607 Debug( LDAP_DEBUG_ANY,
608 "connection_init(%d, %s): set nonblocking failed\n",
609 s, c->c_peer_name.bv_val, 0 );
612 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
613 id = c->c_connid = conn_nextid++;
614 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
616 c->c_conn_state = SLAP_C_INACTIVE;
617 c->c_struct_state = SLAP_C_USED;
618 c->c_close_reason = "?"; /* should never be needed */
620 c->c_ssf = c->c_transport_ssf = ssf;
624 if ( flags & CONN_IS_TLS ) {
626 c->c_needs_tls_accept = 1;
629 c->c_needs_tls_accept = 0;
633 slap_sasl_open( c, 0 );
634 slap_sasl_external( c, ssf, authid );
636 slapd_add_internal( s, 1 );
637 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
639 backend_connection_init(c);
644 void connection2anonymous( Connection *c )
646 assert( connections != NULL );
650 ber_len_t max = sockbuf_max_incoming;
651 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
654 if ( !BER_BVISNULL( &c->c_authmech ) ) {
655 ch_free(c->c_authmech.bv_val);
657 BER_BVZERO( &c->c_authmech );
659 if ( !BER_BVISNULL( &c->c_dn ) ) {
660 ch_free(c->c_dn.bv_val);
662 BER_BVZERO( &c->c_dn );
664 if ( !BER_BVISNULL( &c->c_ndn ) ) {
665 ch_free(c->c_ndn.bv_val);
667 BER_BVZERO( &c->c_ndn );
669 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
670 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
672 BER_BVZERO( &c->c_sasl_authz_dn );
674 c->c_authz_backend = NULL;
678 connection_destroy( Connection *c )
681 unsigned long connid;
682 const char *close_reason;
685 assert( connections != NULL );
687 assert( c->c_struct_state != SLAP_C_UNUSED );
688 assert( c->c_conn_state != SLAP_C_INVALID );
689 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
690 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
692 assert( c->c_txn == CONN_TXN_INACTIVE );
693 assert( c->c_txn_backend == NULL );
694 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
696 assert( c->c_writewaiter == 0);
698 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
699 connid = c->c_connid;
700 close_reason = c->c_close_reason;
702 ldap_pvt_thread_mutex_lock( &connections_mutex );
703 c->c_struct_state = SLAP_C_PENDING;
704 ldap_pvt_thread_mutex_unlock( &connections_mutex );
706 backend_connection_destroy(c);
711 c->c_activitytime = c->c_starttime = 0;
713 connection2anonymous( c );
714 c->c_listener = NULL;
716 if(c->c_peer_domain.bv_val != NULL) {
717 free(c->c_peer_domain.bv_val);
719 BER_BVZERO( &c->c_peer_domain );
720 if(c->c_peer_name.bv_val != NULL) {
721 free(c->c_peer_name.bv_val);
723 BER_BVZERO( &c->c_peer_name );
725 c->c_sasl_bind_in_progress = 0;
726 if(c->c_sasl_bind_mech.bv_val != NULL) {
727 free(c->c_sasl_bind_mech.bv_val);
729 BER_BVZERO( &c->c_sasl_bind_mech );
731 slap_sasl_close( c );
733 if ( c->c_currentber != NULL ) {
734 ber_free( c->c_currentber, 1 );
735 c->c_currentber = NULL;
740 /* call destructors, then constructors; avoids unnecessary allocation */
741 if ( slapi_plugins_used ) {
742 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
746 c->c_conn_state = SLAP_C_INVALID;
747 c->c_struct_state = SLAP_C_UNUSED;
748 c->c_close_reason = "?"; /* should never be needed */
751 c->c_sb = ber_sockbuf_alloc( );
753 ber_len_t max = sockbuf_max_incoming;
754 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
757 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
759 /* c must be fully reset by this point; when we call slapd_remove
760 * it may get immediately reused by a new connection.
762 if ( sd != AC_SOCKET_INVALID ) {
763 slapd_remove( sd, sb, 1, 0, 0 );
765 if ( close_reason == NULL ) {
766 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
767 connid, (long) sd, 0, 0, 0 );
769 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
770 connid, (long) sd, close_reason, 0, 0 );
775 int connection_state_closing( Connection *c )
777 /* c_mutex must be locked by caller */
781 assert( c->c_struct_state == SLAP_C_USED );
783 state = c->c_conn_state;
785 assert( state != SLAP_C_INVALID );
787 return state == SLAP_C_CLOSING;
790 static void connection_abandon( Connection *c )
792 /* c_mutex must be locked by caller */
794 Operation *o, *next, op = {0};
800 op.o_connid = c->c_connid;
801 op.o_tag = LDAP_REQ_ABANDON;
803 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
804 next = LDAP_STAILQ_NEXT( o, o_next );
805 op.orn_msgid = o->o_msgid;
807 op.o_bd = frontendDB;
808 frontendDB->be_abandon( &op, &rs );
812 /* remove operations in pending transaction */
813 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
814 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
815 LDAP_STAILQ_NEXT(o, o_next) = NULL;
819 /* clear transaction */
820 c->c_txn_backend = NULL;
821 c->c_txn = CONN_TXN_INACTIVE;
824 /* remove pending operations */
825 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
826 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
827 LDAP_STAILQ_NEXT(o, o_next) = NULL;
832 void connection_closing( Connection *c, const char *why )
834 assert( connections != NULL );
836 assert( c->c_struct_state == SLAP_C_USED );
837 assert( c->c_conn_state != SLAP_C_INVALID );
839 /* c_mutex must be locked by caller */
841 if( c->c_conn_state != SLAP_C_CLOSING ) {
844 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
845 Debug( LDAP_DEBUG_TRACE,
846 "connection_closing: readying conn=%lu sd=%d for close\n",
847 c->c_connid, sd, 0 );
848 /* update state to closing */
849 c->c_conn_state = SLAP_C_CLOSING;
850 c->c_close_reason = why;
852 /* don't listen on this port anymore */
853 slapd_clr_read( sd, 0 );
855 /* abandon active operations */
856 connection_abandon( c );
858 /* wake write blocked operations */
859 if ( c->c_writewaiter ) {
860 ldap_pvt_thread_cond_signal( &c->c_write_cv );
861 /* ITS#4667 this may allow another thread to drop into
862 * connection_resched / connection_close before we
863 * finish, but that's OK.
865 slapd_clr_write( sd, 1 );
866 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
867 ldap_pvt_thread_mutex_lock( &c->c_write_mutex );
868 ldap_pvt_thread_mutex_lock( &c->c_mutex );
869 ldap_pvt_thread_mutex_unlock( &c->c_write_mutex );
871 slapd_clr_write( sd, 1 );
874 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
875 /* Client closed connection after doing Unbind. */
876 c->c_close_reason = NULL;
881 connection_close( Connection *c )
883 ber_socket_t sd = AC_SOCKET_INVALID;
885 assert( connections != NULL );
888 /* ITS#4667 we may have gotten here twice */
889 if ( c->c_conn_state == SLAP_C_INVALID )
892 assert( c->c_struct_state == SLAP_C_USED );
893 assert( c->c_conn_state == SLAP_C_CLOSING );
895 /* NOTE: c_mutex should be locked by caller */
897 /* NOTE: don't get the file descriptor if not needed */
898 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
899 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
902 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
903 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
905 Debug( LDAP_DEBUG_TRACE,
906 "connection_close: deferring conn=%lu sd=%d\n",
907 c->c_connid, sd, 0 );
911 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
912 c->c_connid, sd, 0 );
914 connection_destroy( c );
917 unsigned long connections_nextid(void)
920 assert( connections != NULL );
922 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
926 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
931 Connection* connection_first( ber_socket_t *index )
933 assert( connections != NULL );
934 assert( index != NULL );
936 ldap_pvt_thread_mutex_lock( &connections_mutex );
937 for( *index = 0; *index < dtblsize; (*index)++) {
938 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
942 ldap_pvt_thread_mutex_unlock( &connections_mutex );
944 return connection_next(NULL, index);
947 Connection* connection_next( Connection *c, ber_socket_t *index )
949 assert( connections != NULL );
950 assert( index != NULL );
951 assert( *index <= dtblsize );
953 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
957 ldap_pvt_thread_mutex_lock( &connections_mutex );
958 for(; *index < dtblsize; (*index)++) {
960 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
961 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
969 if( connections[*index].c_struct_state == SLAP_C_USED ) {
970 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
971 c = &connections[(*index)++];
972 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
974 ldap_pvt_thread_mutex_unlock( &connections_mutex );
975 ldap_pvt_thread_mutex_lock( &c->c_mutex );
976 ldap_pvt_thread_mutex_lock( &connections_mutex );
977 if ( c->c_struct_state != SLAP_C_USED ) {
978 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
986 c_struct = connections[*index].c_struct_state;
987 if ( c_struct == SLAP_C_PENDING )
989 assert( c_struct == SLAP_C_UNUSED );
990 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
993 ldap_pvt_thread_mutex_unlock( &connections_mutex );
997 void connection_done( Connection *c )
999 assert( connections != NULL );
1001 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
1005 * connection_activity - handle the request operation op on connection
1006 * conn. This routine figures out what kind of operation it is and
1007 * calls the appropriate stub to handle it.
1010 #ifdef SLAPD_MONITOR
1011 /* FIXME: returns 0 in case of failure */
1012 #define INCR_OP_INITIATED(index) \
1014 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1015 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
1016 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1018 #define INCR_OP_COMPLETED(index) \
1020 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1021 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1022 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1023 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1025 #else /* !SLAPD_MONITOR */
1026 #define INCR_OP_INITIATED(index) do { } while (0)
1027 #define INCR_OP_COMPLETED(index) \
1029 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1030 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1031 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1033 #endif /* !SLAPD_MONITOR */
1036 * NOTE: keep in sync with enum in slapd.h
1038 static BI_op_func *opfun[] = {
1053 connection_operation( void *ctx, void *arg_v )
1055 int rc = LDAP_OTHER;
1056 Operation *op = arg_v;
1057 SlapReply rs = {REP_RESULT};
1058 ber_tag_t tag = op->o_tag;
1059 slap_op_t opidx = SLAP_OP_LAST;
1060 Connection *conn = op->o_conn;
1061 void *memctx = NULL;
1062 void *memctx_null = NULL;
1065 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1066 /* FIXME: returns 0 in case of failure */
1067 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1068 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1070 op->o_threadctx = ctx;
1071 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1075 case LDAP_REQ_UNBIND:
1077 case LDAP_REQ_DELETE:
1078 case LDAP_REQ_MODDN:
1079 case LDAP_REQ_MODIFY:
1080 case LDAP_REQ_COMPARE:
1081 case LDAP_REQ_SEARCH:
1082 case LDAP_REQ_ABANDON:
1083 case LDAP_REQ_EXTENDED:
1086 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1087 "conn %lu unknown LDAP request 0x%lx\n",
1088 conn->c_connid, tag, 0 );
1089 op->o_tag = LBER_ERROR;
1090 rs.sr_err = LDAP_PROTOCOL_ERROR;
1091 rs.sr_text = "unknown LDAP request";
1092 send_ldap_disconnect( op, &rs );
1093 rc = SLAPD_DISCONNECT;
1094 goto operations_error;
1097 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1098 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1099 "error: SASL bind in progress (tag=%ld).\n",
1101 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1102 "SASL bind in progress" );
1103 rc = LDAP_OPERATIONS_ERROR;
1104 goto operations_error;
1108 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1109 ( tag == LDAP_REQ_ADD ) ||
1110 ( tag == LDAP_REQ_DELETE ) ||
1111 ( tag == LDAP_REQ_MODIFY ) ||
1112 ( tag == LDAP_REQ_MODRDN )))
1114 /* Disable SLAB allocator for all update operations
1115 issued inside of a transaction */
1116 op->o_tmpmemctx = NULL;
1117 op->o_tmpmfuncs = &ch_mfuncs;
1121 /* We can use Thread-Local storage for most mallocs. We can
1122 * also use TL for ber parsing, but not on Add or Modify.
1125 memsiz = ber_len( op->o_ber ) * 64;
1126 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1128 memsiz = SLAP_SLAB_SIZE;
1130 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1131 op->o_tmpmemctx = memctx;
1132 op->o_tmpmfuncs = &slap_sl_mfuncs;
1133 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1134 /* Note - the ber and its buffer are already allocated from
1135 * regular memory; this only affects subsequent mallocs that
1136 * ber_scanf may invoke.
1138 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1142 opidx = slap_req2op( tag );
1143 assert( opidx != SLAP_OP_LAST );
1144 INCR_OP_INITIATED( opidx );
1145 rc = (*(opfun[opidx]))( op, &rs );
1148 if ( rc == SLAPD_DISCONNECT ) {
1151 } else if ( opidx != SLAP_OP_LAST ) {
1152 /* increment completed operations count
1153 * only if operation was initiated
1154 * and rc != SLAPD_DISCONNECT */
1155 INCR_OP_COMPLETED( opidx );
1158 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1159 if ( rc == SLAPD_ABANDON ) {
1160 op->o_cancel = SLAP_CANCEL_ACK;
1162 op->o_cancel = LDAP_TOO_LATE;
1166 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1167 op->o_cancel != SLAP_CANCEL_DONE )
1169 ldap_pvt_thread_yield();
1172 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1174 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1176 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1177 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1179 conn->c_n_ops_executing--;
1180 conn->c_n_ops_completed++;
1184 case LDAP_REQ_UNBIND:
1185 /* c_mutex is locked */
1186 connection_closing( conn,
1187 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1191 connection_resched( conn );
1192 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1196 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1198 int connection_client_setup(
1200 ldap_pvt_thread_start_t *func,
1205 c = connection_init( s, (Listener *)&dummy_list, "", "",
1206 CONN_IS_CLIENT, 0, NULL
1207 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
1208 if ( !c ) return -1;
1210 c->c_clientfunc = func;
1211 c->c_clientarg = arg;
1213 slapd_add_internal( s, 0 );
1214 slapd_set_read( s, 1 );
1218 void connection_client_enable(
1221 slapd_set_read( s, 1 );
1224 void connection_client_stop(
1230 /* get (locked) connection */
1231 c = connection_get( s );
1233 assert( c->c_conn_state == SLAP_C_CLIENT );
1235 c->c_listener = NULL;
1236 c->c_conn_state = SLAP_C_INVALID;
1237 c->c_struct_state = SLAP_C_UNUSED;
1238 c->c_close_reason = "?"; /* should never be needed */
1240 c->c_sb = ber_sockbuf_alloc( );
1242 ber_len_t max = sockbuf_max_incoming;
1243 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1245 slapd_remove( s, sb, 0, 1, 0 );
1247 connection_return( c );
1250 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1252 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1254 static void* connection_read_thread( void* ctx, void* argv )
1257 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1258 ber_socket_t s = (long)argv;
1261 * read incoming LDAP requests. If there is more than one,
1262 * the first one is returned with new_op
1264 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1265 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1266 return (void*)(long)rc;
1269 /* execute a single queued request in the same thread */
1270 if( cri.op && !cri.nullop ) {
1271 rc = (long)connection_operation( ctx, cri.op );
1272 } else if ( cri.func ) {
1273 rc = (long)cri.func( ctx, cri.arg );
1276 return (void*)(long)rc;
1279 int connection_read_activate( ber_socket_t s )
1284 * suspend reading on this file descriptor until a connection processing
1285 * thread reads data on it. Otherwise the listener thread will repeatedly
1286 * submit the same event on it to the pool.
1288 rc = slapd_clr_read( s, 0 );
1292 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1293 connection_read_thread, (void *)(long)s );
1296 Debug( LDAP_DEBUG_ANY,
1297 "connection_read_activate(%d): submit failed (%d)\n",
1305 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1307 connection_read( ber_socket_t s, conn_readinfo *cri )
1309 int connection_read(ber_socket_t s)
1315 assert( connections != NULL );
1317 /* get (locked) connection */
1318 c = connection_get( s );
1321 Debug( LDAP_DEBUG_ANY,
1322 "connection_read(%ld): no connection!\n",
1330 if( c->c_conn_state == SLAP_C_CLOSING ) {
1331 Debug( LDAP_DEBUG_TRACE,
1332 "connection_read(%d): closing, ignoring input for id=%lu\n",
1333 s, c->c_connid, 0 );
1334 connection_return( c );
1338 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1339 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1340 cri->func = c->c_clientfunc;
1341 cri->arg = c->c_clientarg;
1342 /* read should already be cleared */
1344 slapd_clr_read( s, 0 );
1345 ldap_pvt_thread_pool_submit( &connection_pool,
1346 c->c_clientfunc, c->c_clientarg );
1348 connection_return( c );
1352 Debug( LDAP_DEBUG_TRACE,
1353 "connection_read(%d): checking for input on id=%lu\n",
1354 s, c->c_connid, 0 );
1357 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1358 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1360 Debug( LDAP_DEBUG_TRACE,
1361 "connection_read(%d): TLS accept failure "
1362 "error=%d id=%lu, closing\n",
1363 s, rc, c->c_connid );
1365 c->c_needs_tls_accept = 0;
1366 /* c_mutex is locked */
1367 connection_closing( c, "TLS negotiation failure" );
1368 connection_close( c );
1369 connection_return( c );
1372 } else if ( rc == 0 ) {
1374 struct berval authid = BER_BVNULL;
1376 c->c_needs_tls_accept = 0;
1378 /* we need to let SASL know */
1379 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1381 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1382 if( c->c_tls_ssf > c->c_ssf ) {
1383 c->c_ssf = c->c_tls_ssf;
1386 rc = dnX509peerNormalize( ssl, &authid );
1387 if ( rc != LDAP_SUCCESS ) {
1388 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1389 "unable to get TLS client DN, error=%d id=%lu\n",
1390 s, rc, c->c_connid );
1392 Statslog( LDAP_DEBUG_STATS,
1393 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1394 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1395 slap_sasl_external( c, c->c_tls_ssf, &authid );
1396 if ( authid.bv_val ) free( authid.bv_val );
1399 /* if success and data is ready, fall thru to data input loop */
1400 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1402 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1403 slapd_set_read( s, 1 );
1406 connection_return( c );
1412 #ifdef HAVE_CYRUS_SASL
1413 if ( c->c_sasl_layers ) {
1414 /* If previous layer is not removed yet, give up for now */
1415 if ( !c->c_sasl_sockctx ) {
1416 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1417 slapd_set_read( s, 1 );
1420 connection_return( c );
1424 c->c_sasl_layers = 0;
1426 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1427 if( rc != LDAP_SUCCESS ) {
1428 Debug( LDAP_DEBUG_TRACE,
1429 "connection_read(%d): SASL install error "
1430 "error=%d id=%lu, closing\n",
1431 s, rc, c->c_connid );
1433 /* c_mutex is locked */
1434 connection_closing( c, "SASL layer install failure" );
1435 connection_close( c );
1436 connection_return( c );
1442 #define CONNECTION_INPUT_LOOP 1
1443 /* #define DATA_READY_LOOP 1 */
1446 /* How do we do this without getting into a busy loop ? */
1447 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1448 rc = connection_input( c, cri );
1450 rc = connection_input( c );
1453 #ifdef DATA_READY_LOOP
1454 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1455 #elif defined CONNECTION_INPUT_LOOP
1462 Debug( LDAP_DEBUG_CONNS,
1463 "connection_read(%d): input error=%d id=%lu, closing.\n",
1464 s, rc, c->c_connid );
1466 /* c_mutex is locked */
1467 connection_closing( c, conn_lost_str );
1468 connection_close( c );
1469 connection_return( c );
1473 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1474 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1475 slapd_set_write( s, 0 );
1478 slapd_set_read( s, 1 );
1480 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1481 slapd_set_read( s, 1 );
1484 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1485 slapd_set_write( s, 1 );
1489 connection_return( c );
1495 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1496 connection_input( Connection *conn , conn_readinfo *cri )
1498 connection_input( Connection *conn )
1507 #ifdef LDAP_CONNECTIONLESS
1513 if ( conn->c_currentber == NULL &&
1514 ( conn->c_currentber = ber_alloc()) == NULL )
1516 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1522 #ifdef LDAP_CONNECTIONLESS
1523 if ( conn->c_is_udp ) {
1524 char peername[sizeof("IP=255.255.255.255:65336")];
1526 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1527 if (len != sizeof(struct sockaddr)) return 1;
1529 sprintf( peername, "IP=%s:%d",
1530 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1531 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1532 Statslog( LDAP_DEBUG_STATS,
1533 "conn=%lu UDP request from %s (%s) accepted.\n",
1534 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1538 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1539 if ( tag != LDAP_TAG_MESSAGE ) {
1540 int err = sock_errno();
1543 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1545 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1546 /* log, close and send error */
1547 Debug( LDAP_DEBUG_TRACE,
1548 "ber_get_next on fd %d failed errno=%d (%s)\n",
1549 sd, err, sock_errstr(err) );
1550 ber_free( conn->c_currentber, 1 );
1551 conn->c_currentber = NULL;
1558 ber = conn->c_currentber;
1559 conn->c_currentber = NULL;
1561 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1562 /* log, close and send error */
1563 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1568 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1569 /* log, close and send error */
1570 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1576 #ifdef LDAP_CONNECTIONLESS
1577 if( conn->c_is_udp ) {
1578 if( tag == LBER_OCTETSTRING ) {
1579 ber_get_stringa( ber, &cdn );
1580 tag = ber_peek_tag(ber, &len);
1582 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1583 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1590 if(tag == LDAP_REQ_BIND) {
1591 /* immediately abandon all existing operations upon BIND */
1592 connection_abandon( conn );
1595 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1598 /* clear state if the connection is being reused from inactive */
1599 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1600 memset( &conn->c_pagedresults_state, 0,
1601 sizeof( conn->c_pagedresults_state ) );
1604 op->o_res_ber = NULL;
1606 #ifdef LDAP_CONNECTIONLESS
1607 if (conn->c_is_udp) {
1609 ber_str2bv( cdn, 0, 1, &op->o_dn );
1610 op->o_protocol = LDAP_VERSION2;
1612 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1613 if (op->o_res_ber == NULL) return 1;
1615 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1616 sizeof(struct sockaddr), 0 );
1618 if (rc != sizeof(struct sockaddr)) {
1619 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1623 if (op->o_protocol == LDAP_VERSION2) {
1624 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1626 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1631 #endif /* LDAP_CONNECTIONLESS */
1635 /* Don't process requests when the conn is in the middle of a
1636 * Bind, or if it's closing. Also, don't let any single conn
1637 * use up all the available threads, and don't execute if we're
1638 * currently blocked on output. And don't execute if there are
1639 * already pending ops, let them go first. Abandon operations
1640 * get exceptions to some, but not all, cases.
1644 /* Abandon and Unbind are exempt from these checks */
1645 if (conn->c_conn_state == SLAP_C_CLOSING) {
1648 } else if (conn->c_writewaiter) {
1649 defer = "awaiting write";
1651 } else if (conn->c_n_ops_pending) {
1652 defer = "pending operations";
1656 case LDAP_REQ_ABANDON:
1657 /* Unbind is exempt from these checks */
1658 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1659 defer = "too many executing";
1661 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1666 case LDAP_REQ_UNBIND:
1671 int max = conn->c_dn.bv_len
1672 ? slap_conn_max_pending_auth
1673 : slap_conn_max_pending;
1675 Debug( LDAP_DEBUG_ANY,
1676 "connection_input: conn=%lu deferring operation: %s\n",
1677 conn->c_connid, defer, 0 );
1678 conn->c_n_ops_pending++;
1679 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1680 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1683 conn->c_n_ops_executing++;
1685 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1687 * The first op will be processed in the same thread context,
1688 * as long as there is only one op total.
1689 * Subsequent ops will be submitted to the pool by
1690 * calling connection_op_activate()
1692 if ( cri->op == NULL ) {
1693 /* the first incoming request */
1694 connection_op_queue( op );
1697 if ( !cri->nullop ) {
1699 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1700 connection_operation, (void *) cri->op );
1702 connection_op_activate( op );
1705 connection_op_activate( op );
1710 if ( conn->c_struct_state != SLAP_C_USED ) {
1711 /* connection must have got closed underneath us */
1716 assert( conn->c_struct_state == SLAP_C_USED );
1721 connection_resched( Connection *conn )
1725 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1727 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1729 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1730 "attempting closing conn=%lu sd=%d\n",
1731 conn->c_connid, sd, 0 );
1732 connection_close( conn );
1736 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1737 /* other states need different handling */
1741 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1742 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1744 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1745 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1747 /* pending operations should not be marked for abandonment */
1748 assert(!op->o_abandon);
1750 conn->c_n_ops_pending--;
1751 conn->c_n_ops_executing++;
1753 connection_op_activate( op );
1755 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1761 connection_init_log_prefix( Operation *op )
1763 if ( op->o_connid == (unsigned long)(-1) ) {
1764 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1765 "conn=-1 op=%lu", op->o_opid );
1768 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1769 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1773 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1775 op->o_conn->c_sasl_bindop = NULL;
1777 ch_free( op->o_callback );
1778 op->o_callback = NULL;
1780 return SLAP_CB_CONTINUE;
1783 static int connection_bind_cb( Operation *op, SlapReply *rs )
1785 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1786 if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1787 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1788 op->o_conn->c_sasl_bind_in_progress =
1789 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1791 /* Moved here from bind.c due to ITS#4158 */
1792 op->o_conn->c_sasl_bindop = NULL;
1793 if ( op->orb_method == LDAP_AUTH_SASL ) {
1794 if( rs->sr_err == LDAP_SUCCESS ) {
1795 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1796 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1797 /* edn is always normalized already */
1798 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1800 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1801 BER_BVZERO( &op->orb_edn );
1802 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1803 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1805 op->o_conn->c_sasl_ssf = op->orb_ssf;
1806 if( op->orb_ssf > op->o_conn->c_ssf ) {
1807 op->o_conn->c_ssf = op->orb_ssf;
1810 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1811 ber_len_t max = sockbuf_max_incoming_auth;
1812 ber_sockbuf_ctrl( op->o_conn->c_sb,
1813 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1816 /* log authorization identity */
1817 Statslog( LDAP_DEBUG_STATS,
1818 "%s BIND dn=\"%s\" mech=%s sasl_ssf=%d ssf=%d\n",
1820 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1821 op->o_conn->c_authmech.bv_val,
1822 op->orb_ssf, op->o_conn->c_ssf );
1824 Debug( LDAP_DEBUG_TRACE,
1825 "do_bind: SASL/%s bind: dn=\"%s\" sasl_ssf=%d\n",
1826 op->o_conn->c_authmech.bv_val,
1827 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1830 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1831 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1832 free( op->o_conn->c_sasl_bind_mech.bv_val );
1833 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1837 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1839 ch_free( op->o_callback );
1840 op->o_callback = NULL;
1842 return SLAP_CB_CONTINUE;
1845 static void connection_op_queue( Operation *op )
1847 ber_tag_t tag = op->o_tag;
1849 if (tag == LDAP_REQ_BIND) {
1850 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1851 sc->sc_response = connection_bind_cb;
1852 sc->sc_cleanup = connection_bind_cleanup_cb;
1853 sc->sc_next = op->o_callback;
1854 op->o_callback = sc;
1855 op->o_conn->c_conn_state = SLAP_C_BINDING;
1858 if (!op->o_dn.bv_len) {
1859 op->o_authz = op->o_conn->c_authz;
1860 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1861 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1862 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1864 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1865 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1869 op->o_authtype = op->o_conn->c_authtype;
1870 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1872 if (!op->o_protocol) {
1873 op->o_protocol = op->o_conn->c_protocol
1874 ? op->o_conn->c_protocol : LDAP_VERSION3;
1877 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1878 op->o_protocol > LDAP_VERSION2)
1880 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1883 op->o_connid = op->o_conn->c_connid;
1884 connection_init_log_prefix( op );
1886 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1889 static int connection_op_activate( Operation *op )
1893 connection_op_queue( op );
1895 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1896 connection_operation, (void *) op );
1899 Debug( LDAP_DEBUG_ANY,
1900 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1901 rc, op->o_connid, 0 );
1902 /* should move op to pending list */
1908 int connection_write(ber_socket_t s)
1913 assert( connections != NULL );
1915 slapd_clr_write( s, 0 );
1917 c = connection_get( s );
1919 Debug( LDAP_DEBUG_ANY,
1920 "connection_write(%ld): no connection!\n",
1927 Debug( LDAP_DEBUG_TRACE,
1928 "connection_write(%d): waking output for id=%lu\n",
1929 s, c->c_connid, 0 );
1930 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1932 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1933 slapd_set_read( s, 1 );
1935 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1936 slapd_set_write( s, 1 );
1939 /* If there are ops pending because of a writewaiter,
1942 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1943 if ( !c->c_writewaiter ) break;
1944 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1946 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1947 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1949 /* pending operations should not be marked for abandonment */
1950 assert(!op->o_abandon);
1952 c->c_n_ops_pending--;
1953 c->c_n_ops_executing++;
1955 connection_op_activate( op );
1960 connection_return( c );
1965 typedef struct conn_fake_extblock {
1968 } conn_fake_extblock;
1971 connection_fake_destroy(
1975 Connection conn = {0};
1977 Opheader ohdr = {0};
1979 conn_fake_extblock *eb = data;
1982 op.o_hdr->oh_extensions = eb->eb_op;
1983 conn.c_extensions = eb->eb_conn;
1988 ber_memfree_x( eb, NULL );
1989 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
1990 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
1995 connection_fake_init(
1997 OperationBuffer *opbuf,
2000 connection_fake_init2( conn, opbuf, ctx, 1 );
2004 connection_fake_init2(
2006 OperationBuffer *opbuf,
2010 Operation *op = (Operation *) opbuf;
2012 conn->c_connid = -1;
2013 conn->c_send_ldap_result = slap_send_ldap_result;
2014 conn->c_send_search_entry = slap_send_search_entry;
2015 conn->c_send_search_reference = slap_send_search_reference;
2016 conn->c_listener = (Listener *)&dummy_list;
2017 conn->c_peer_domain = slap_empty_bv;
2018 conn->c_peer_name = slap_empty_bv;
2020 memset( opbuf, 0, sizeof( *opbuf ));
2021 op->o_hdr = &opbuf->ob_hdr;
2022 op->o_controls = opbuf->ob_controls;
2024 /* set memory context */
2025 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
2027 op->o_tmpmfuncs = &slap_sl_mfuncs;
2028 op->o_threadctx = ctx;
2029 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
2032 op->o_connid = op->o_conn->c_connid;
2033 connection_init_log_prefix( op );
2036 if ( slapi_plugins_used ) {
2037 conn_fake_extblock *eb;
2040 /* Use thread keys to make sure these eventually get cleaned up */
2041 if ( ldap_pvt_thread_pool_getkey( ctx, connection_fake_init, &ebx,
2043 eb = ch_malloc( sizeof( *eb ));
2044 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
2045 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
2046 eb->eb_conn = conn->c_extensions;
2047 eb->eb_op = op->o_hdr->oh_extensions;
2048 ldap_pvt_thread_pool_setkey( ctx, connection_fake_init, eb,
2049 connection_fake_destroy );
2052 conn->c_extensions = eb->eb_conn;
2053 op->o_hdr->oh_extensions = eb->eb_op;
2056 #endif /* LDAP_SLAPI */
2058 slap_op_time( &op->o_time, &op->o_tincr );
2062 connection_assign_nextid( Connection *conn )
2064 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
2065 conn->c_connid = conn_nextid++;
2066 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );