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_USED ) {
202 /* give persistent clients a chance to cleanup */
203 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
204 ldap_pvt_thread_pool_submit( &connection_pool,
205 connections[i].c_clientfunc, connections[i].c_clientarg );
209 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
211 /* c_mutex is locked */
212 connection_closing( &connections[i], "slapd shutdown" );
213 connection_close( &connections[i] );
215 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
222 * Timeout idle connections.
224 int connections_timeout_idle(time_t now)
230 for( c = connection_first( &connindex );
232 c = connection_next( c, &connindex ) )
234 /* Don't timeout a slow-running request or a persistent
235 * outbound connection */
236 if( c->c_n_ops_executing || c->c_conn_state == SLAP_C_CLIENT ) {
240 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
242 connection_closing( c, "idletimeout" );
243 connection_close( c );
247 connection_done( c );
252 static Connection* connection_get( ber_socket_t s )
256 Debug( LDAP_DEBUG_ARGS,
257 "connection_get(%ld)\n",
260 assert( connections != NULL );
262 if(s == AC_SOCKET_INVALID) return NULL;
265 assert( s < dtblsize );
273 ldap_pvt_thread_mutex_lock( &connections_mutex );
274 for(i=0; i<dtblsize; i++) {
275 if( connections[i].c_struct_state == SLAP_C_PENDING )
278 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
279 assert( connections[i].c_conn_state == SLAP_C_INVALID );
280 assert( connections[i].c_sb == 0 );
284 ber_sockbuf_ctrl( connections[i].c_sb,
285 LBER_SB_OPT_GET_FD, &sd );
287 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
288 assert( connections[i].c_conn_state == SLAP_C_INVALID );
289 assert( sd == AC_SOCKET_INVALID );
293 /* state can actually change from used -> unused by resched,
294 * so don't assert details here.
302 ldap_pvt_thread_mutex_unlock( &connections_mutex );
309 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
311 ldap_pvt_thread_mutex_lock( &c->c_mutex );
313 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
314 if( c->c_struct_state != SLAP_C_USED ) {
315 /* connection must have been closed due to resched */
317 assert( c->c_conn_state == SLAP_C_INVALID );
318 assert( sd == AC_SOCKET_INVALID );
320 Debug( LDAP_DEBUG_TRACE,
321 "connection_get(%d): connection not used\n",
324 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
328 Debug( LDAP_DEBUG_TRACE,
329 "connection_get(%d): got connid=%lu\n",
334 assert( c->c_struct_state == SLAP_C_USED );
335 assert( c->c_conn_state != SLAP_C_INVALID );
336 assert( sd != AC_SOCKET_INVALID );
338 #ifndef SLAPD_MONITOR
339 if ( global_idletimeout > 0 )
340 #endif /* ! SLAPD_MONITOR */
342 c->c_activitytime = slap_get_time();
349 static void connection_return( Connection *c )
351 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
354 long connection_init(
358 const char* peername,
361 struct berval *authid )
367 assert( connections != NULL );
369 assert( listener != NULL );
370 assert( dnsname != NULL );
371 assert( peername != NULL );
374 assert( flags != CONN_IS_TLS );
377 if( s == AC_SOCKET_INVALID ) {
378 Debug( LDAP_DEBUG_ANY,
379 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
385 assert( s < dtblsize );
387 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
390 assert( c->c_struct_state == SLAP_C_UNUSED );
397 ldap_pvt_thread_mutex_lock( &connections_mutex );
398 for( i=0; i < dtblsize; i++) {
401 if ( connections[i].c_struct_state == SLAP_C_PENDING )
404 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
405 assert( connections[i].c_sb == 0 );
407 c->c_struct_state = SLAP_C_PENDING;
412 sd = AC_SOCKET_INVALID;
413 if (connections[i].c_sb != NULL) {
414 ber_sockbuf_ctrl( connections[i].c_sb,
415 LBER_SB_OPT_GET_FD, &sd );
418 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
419 assert( sd == AC_SOCKET_INVALID );
421 c->c_struct_state = SLAP_C_PENDING;
425 if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
427 assert( connections[i].c_struct_state == SLAP_C_USED );
428 assert( connections[i].c_conn_state != SLAP_C_INVALID );
429 assert( sd != AC_SOCKET_INVALID );
431 ldap_pvt_thread_mutex_unlock( &connections_mutex );
434 Debug( LDAP_DEBUG_ANY,
435 "connection_init(%d): connection table full "
436 "(%d/%d)\n", s, i, dtblsize);
443 c->c_send_ldap_result = slap_send_ldap_result;
444 c->c_send_search_entry = slap_send_search_entry;
445 c->c_send_search_reference = slap_send_search_reference;
446 c->c_send_ldap_extended = slap_send_ldap_extended;
447 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
449 BER_BVZERO( &c->c_authmech );
450 BER_BVZERO( &c->c_dn );
451 BER_BVZERO( &c->c_ndn );
453 c->c_listener = NULL;
454 BER_BVZERO( &c->c_peer_domain );
455 BER_BVZERO( &c->c_peer_name );
457 LDAP_STAILQ_INIT(&c->c_ops);
458 LDAP_STAILQ_INIT(&c->c_pending_ops);
461 c->c_txn = CONN_TXN_INACTIVE;
462 c->c_txn_backend = NULL;
463 LDAP_STAILQ_INIT(&c->c_txn_ops);
466 BER_BVZERO( &c->c_sasl_bind_mech );
468 c->c_sasl_authctx = NULL;
469 c->c_sasl_sockctx = NULL;
470 c->c_sasl_extra = NULL;
471 c->c_sasl_bindop = NULL;
473 c->c_sb = ber_sockbuf_alloc( );
476 ber_len_t max = sockbuf_max_incoming;
477 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
480 c->c_currentber = NULL;
482 /* should check status of thread calls */
483 ldap_pvt_thread_mutex_init( &c->c_mutex );
484 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
485 ldap_pvt_thread_cond_init( &c->c_write_cv );
488 if ( slapi_plugins_used ) {
489 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
494 ldap_pvt_thread_mutex_lock( &c->c_mutex );
496 assert( BER_BVISNULL( &c->c_authmech ) );
497 assert( BER_BVISNULL( &c->c_dn ) );
498 assert( BER_BVISNULL( &c->c_ndn ) );
499 assert( c->c_listener == NULL );
500 assert( BER_BVISNULL( &c->c_peer_domain ) );
501 assert( BER_BVISNULL( &c->c_peer_name ) );
502 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
503 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
505 assert( c->c_txn == CONN_TXN_INACTIVE );
506 assert( c->c_txn_backend == NULL );
507 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
509 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
510 assert( c->c_sasl_done == 0 );
511 assert( c->c_sasl_authctx == NULL );
512 assert( c->c_sasl_sockctx == NULL );
513 assert( c->c_sasl_extra == NULL );
514 assert( c->c_sasl_bindop == NULL );
515 assert( c->c_currentber == NULL );
516 assert( c->c_writewaiter == 0);
518 c->c_listener = listener;
520 if ( flags == CONN_IS_CLIENT ) {
521 c->c_conn_state = SLAP_C_CLIENT;
522 c->c_struct_state = SLAP_C_USED;
523 c->c_close_reason = "?"; /* should never be needed */
524 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
525 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
530 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
531 ber_str2bv( peername, 0, 1, &c->c_peer_name );
533 c->c_n_ops_received = 0;
534 c->c_n_ops_executing = 0;
535 c->c_n_ops_pending = 0;
536 c->c_n_ops_completed = 0;
542 /* set to zero until bind, implies LDAP_VERSION3 */
545 #ifndef SLAPD_MONITOR
546 if ( global_idletimeout > 0 )
547 #endif /* ! SLAPD_MONITOR */
549 c->c_activitytime = c->c_starttime = slap_get_time();
552 #ifdef LDAP_CONNECTIONLESS
554 if( flags == CONN_IS_UDP ) {
557 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
558 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
560 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
561 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
562 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
563 LBER_SBIOD_LEVEL_PROVIDER, NULL );
568 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
569 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
571 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
572 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
576 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
577 INT_MAX, (void*)"ldap_" );
580 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
581 c /* non-NULL */ ) < 0 )
583 Debug( LDAP_DEBUG_ANY,
584 "connection_init(%d, %s): set nonblocking failed\n",
585 s, c->c_peer_name.bv_val, 0 );
588 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
589 id = c->c_connid = conn_nextid++;
590 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
592 c->c_conn_state = SLAP_C_INACTIVE;
593 c->c_struct_state = SLAP_C_USED;
594 c->c_close_reason = "?"; /* should never be needed */
596 c->c_ssf = c->c_transport_ssf = ssf;
600 if ( flags == CONN_IS_TLS ) {
602 c->c_needs_tls_accept = 1;
605 c->c_needs_tls_accept = 0;
609 slap_sasl_open( c, 0 );
610 slap_sasl_external( c, ssf, authid );
612 slapd_add_internal( s, 1 );
613 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
615 backend_connection_init(c);
620 void connection2anonymous( Connection *c )
622 assert( connections != NULL );
626 ber_len_t max = sockbuf_max_incoming;
627 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
630 if ( !BER_BVISNULL( &c->c_authmech ) ) {
631 ch_free(c->c_authmech.bv_val);
633 BER_BVZERO( &c->c_authmech );
635 if ( !BER_BVISNULL( &c->c_dn ) ) {
636 ch_free(c->c_dn.bv_val);
638 BER_BVZERO( &c->c_dn );
640 if ( !BER_BVISNULL( &c->c_ndn ) ) {
641 ch_free(c->c_ndn.bv_val);
643 BER_BVZERO( &c->c_ndn );
645 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
646 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
648 BER_BVZERO( &c->c_sasl_authz_dn );
650 c->c_authz_backend = NULL;
654 connection_destroy( Connection *c )
657 unsigned long connid;
658 const char *close_reason;
661 assert( connections != NULL );
663 assert( c->c_struct_state != SLAP_C_UNUSED );
664 assert( c->c_conn_state != SLAP_C_INVALID );
665 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
666 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
668 assert( c->c_txn == CONN_TXN_INACTIVE );
669 assert( c->c_txn_backend == NULL );
670 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
672 assert( c->c_writewaiter == 0);
674 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
675 connid = c->c_connid;
676 close_reason = c->c_close_reason;
678 ldap_pvt_thread_mutex_lock( &connections_mutex );
679 c->c_struct_state = SLAP_C_PENDING;
680 ldap_pvt_thread_mutex_unlock( &connections_mutex );
682 backend_connection_destroy(c);
687 c->c_activitytime = c->c_starttime = 0;
689 connection2anonymous( c );
690 c->c_listener = NULL;
692 if(c->c_peer_domain.bv_val != NULL) {
693 free(c->c_peer_domain.bv_val);
695 BER_BVZERO( &c->c_peer_domain );
696 if(c->c_peer_name.bv_val != NULL) {
697 free(c->c_peer_name.bv_val);
699 BER_BVZERO( &c->c_peer_name );
701 c->c_sasl_bind_in_progress = 0;
702 if(c->c_sasl_bind_mech.bv_val != NULL) {
703 free(c->c_sasl_bind_mech.bv_val);
705 BER_BVZERO( &c->c_sasl_bind_mech );
707 slap_sasl_close( c );
709 if ( c->c_currentber != NULL ) {
710 ber_free( c->c_currentber, 1 );
711 c->c_currentber = NULL;
716 /* call destructors, then constructors; avoids unnecessary allocation */
717 if ( slapi_plugins_used ) {
718 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
722 c->c_conn_state = SLAP_C_INVALID;
723 c->c_struct_state = SLAP_C_UNUSED;
724 c->c_close_reason = "?"; /* should never be needed */
727 c->c_sb = ber_sockbuf_alloc( );
729 ber_len_t max = sockbuf_max_incoming;
730 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
733 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
735 /* c must be fully reset by this point; when we call slapd_remove
736 * it may get immediately reused by a new connection.
738 if ( sd != AC_SOCKET_INVALID ) {
739 slapd_remove( sd, sb, 1, 0, 0 );
741 if ( close_reason == NULL ) {
745 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
746 connid, (long) sd, close_reason, 0, 0 );
750 int connection_state_closing( Connection *c )
752 /* c_mutex must be locked by caller */
756 assert( c->c_struct_state == SLAP_C_USED );
758 state = c->c_conn_state;
760 assert( state != SLAP_C_INVALID );
762 return state == SLAP_C_CLOSING;
765 static void connection_abandon( Connection *c )
767 /* c_mutex must be locked by caller */
769 Operation *o, *next, op = {0};
775 op.o_connid = c->c_connid;
776 op.o_tag = LDAP_REQ_ABANDON;
778 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
779 next = LDAP_STAILQ_NEXT( o, o_next );
780 op.orn_msgid = o->o_msgid;
782 op.o_bd = frontendDB;
783 frontendDB->be_abandon( &op, &rs );
787 /* remove operations in pending transaction */
788 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
789 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
790 LDAP_STAILQ_NEXT(o, o_next) = NULL;
794 /* clear transaction */
795 c->c_txn_backend = NULL;
796 c->c_txn = CONN_TXN_INACTIVE;
799 /* remove pending operations */
800 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
801 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
802 LDAP_STAILQ_NEXT(o, o_next) = NULL;
807 void connection_closing( Connection *c, const char *why )
809 assert( connections != NULL );
811 assert( c->c_struct_state == SLAP_C_USED );
812 assert( c->c_conn_state != SLAP_C_INVALID );
814 /* c_mutex must be locked by caller */
816 if( c->c_conn_state != SLAP_C_CLOSING ) {
819 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
820 Debug( LDAP_DEBUG_TRACE,
821 "connection_closing: readying conn=%lu sd=%d for close\n",
822 c->c_connid, sd, 0 );
823 /* update state to closing */
824 c->c_conn_state = SLAP_C_CLOSING;
825 c->c_close_reason = why;
827 /* don't listen on this port anymore */
828 slapd_clr_read( sd, 0 );
830 /* abandon active operations */
831 connection_abandon( c );
833 /* wake write blocked operations */
834 if ( c->c_writewaiter ) {
835 ldap_pvt_thread_cond_signal( &c->c_write_cv );
836 /* ITS#4667 this may allow another thread to drop into
837 * connection_resched / connection_close before we
838 * finish, but that's OK.
840 slapd_clr_write( sd, 1 );
841 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
842 ldap_pvt_thread_mutex_lock( &c->c_write_mutex );
843 ldap_pvt_thread_mutex_lock( &c->c_mutex );
844 ldap_pvt_thread_mutex_unlock( &c->c_write_mutex );
846 slapd_clr_write( sd, 1 );
849 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
850 /* Client closed connection after doing Unbind. */
851 c->c_close_reason = NULL;
856 connection_close( Connection *c )
858 ber_socket_t sd = AC_SOCKET_INVALID;
860 assert( connections != NULL );
863 /* ITS#4667 we may have gotten here twice */
864 if ( c->c_conn_state == SLAP_C_INVALID )
867 assert( c->c_struct_state == SLAP_C_USED );
868 assert( c->c_conn_state == SLAP_C_CLOSING );
870 /* NOTE: c_mutex should be locked by caller */
872 /* NOTE: don't get the file descriptor if not needed */
874 if ( slap_debug & LDAP_DEBUG_TRACE ) {
875 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
877 #endif /* LDAP_DEBUG */
879 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
880 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
882 Debug( LDAP_DEBUG_TRACE,
883 "connection_close: deferring conn=%lu sd=%d\n",
884 c->c_connid, sd, 0 );
888 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
889 c->c_connid, sd, 0 );
891 connection_destroy( c );
894 unsigned long connections_nextid(void)
897 assert( connections != NULL );
899 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
903 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
908 Connection* connection_first( ber_socket_t *index )
910 assert( connections != NULL );
911 assert( index != NULL );
913 ldap_pvt_thread_mutex_lock( &connections_mutex );
914 for( *index = 0; *index < dtblsize; (*index)++) {
915 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
919 ldap_pvt_thread_mutex_unlock( &connections_mutex );
921 return connection_next(NULL, index);
924 Connection* connection_next( Connection *c, ber_socket_t *index )
926 assert( connections != NULL );
927 assert( index != NULL );
928 assert( *index <= dtblsize );
930 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
934 ldap_pvt_thread_mutex_lock( &connections_mutex );
935 for(; *index < dtblsize; (*index)++) {
937 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
938 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
946 if( connections[*index].c_struct_state == SLAP_C_USED ) {
947 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
948 c = &connections[(*index)++];
949 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
951 ldap_pvt_thread_mutex_unlock( &connections_mutex );
952 ldap_pvt_thread_mutex_lock( &c->c_mutex );
953 ldap_pvt_thread_mutex_lock( &connections_mutex );
954 if ( c->c_struct_state != SLAP_C_USED ) {
955 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
963 c_struct = connections[*index].c_struct_state;
964 if ( c_struct == SLAP_C_PENDING )
966 assert( c_struct == SLAP_C_UNUSED );
967 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
970 ldap_pvt_thread_mutex_unlock( &connections_mutex );
974 void connection_done( Connection *c )
976 assert( connections != NULL );
978 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
982 * connection_activity - handle the request operation op on connection
983 * conn. This routine figures out what kind of operation it is and
984 * calls the appropriate stub to handle it.
988 /* FIXME: returns 0 in case of failure */
989 #define INCR_OP_INITIATED(index) \
991 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
992 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
993 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
995 #define INCR_OP_COMPLETED(index) \
997 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
998 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
999 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
1000 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1002 #else /* !SLAPD_MONITOR */
1003 #define INCR_OP_INITIATED(index) do { } while (0)
1004 #define INCR_OP_COMPLETED(index) \
1006 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
1007 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
1008 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
1010 #endif /* !SLAPD_MONITOR */
1013 * NOTE: keep in sync with enum in slapd.h
1015 static BI_op_func *opfun[] = {
1030 connection_operation( void *ctx, void *arg_v )
1032 int rc = LDAP_OTHER;
1033 Operation *op = arg_v;
1034 SlapReply rs = {REP_RESULT};
1035 ber_tag_t tag = op->o_tag;
1036 slap_op_t opidx = SLAP_OP_LAST;
1037 Connection *conn = op->o_conn;
1038 void *memctx = NULL;
1039 void *memctx_null = NULL;
1042 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
1043 /* FIXME: returns 0 in case of failure */
1044 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
1045 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1047 op->o_threadctx = ctx;
1049 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1050 #endif /* LDAP_DEVEL */
1054 case LDAP_REQ_UNBIND:
1056 case LDAP_REQ_DELETE:
1057 case LDAP_REQ_MODDN:
1058 case LDAP_REQ_MODIFY:
1059 case LDAP_REQ_COMPARE:
1060 case LDAP_REQ_SEARCH:
1061 case LDAP_REQ_ABANDON:
1062 case LDAP_REQ_EXTENDED:
1065 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1066 "conn %lu unknown LDAP request 0x%lx\n",
1067 conn->c_connid, tag, 0 );
1068 op->o_tag = LBER_ERROR;
1069 rs.sr_err = LDAP_PROTOCOL_ERROR;
1070 rs.sr_text = "unknown LDAP request";
1071 send_ldap_disconnect( op, &rs );
1072 rc = SLAPD_DISCONNECT;
1073 goto operations_error;
1076 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1077 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1078 "error: SASL bind in progress (tag=%ld).\n",
1080 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1081 "SASL bind in progress" );
1082 rc = LDAP_OPERATIONS_ERROR;
1083 goto operations_error;
1087 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1088 ( tag == LDAP_REQ_ADD ) ||
1089 ( tag == LDAP_REQ_DELETE ) ||
1090 ( tag == LDAP_REQ_MODIFY ) ||
1091 ( tag == LDAP_REQ_MODRDN )))
1093 /* Disable SLAB allocator for all update operations
1094 issued inside of a transaction */
1095 op->o_tmpmemctx = NULL;
1096 op->o_tmpmfuncs = &ch_mfuncs;
1100 /* We can use Thread-Local storage for most mallocs. We can
1101 * also use TL for ber parsing, but not on Add or Modify.
1104 memsiz = ber_len( op->o_ber ) * 64;
1105 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1107 memsiz = SLAP_SLAB_SIZE;
1109 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
1110 op->o_tmpmemctx = memctx;
1111 op->o_tmpmfuncs = &slap_sl_mfuncs;
1112 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1113 /* Note - the ber and its buffer are already allocated from
1114 * regular memory; this only affects subsequent mallocs that
1115 * ber_scanf may invoke.
1117 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1121 opidx = slap_req2op( tag );
1122 assert( opidx != SLAP_OP_LAST );
1123 INCR_OP_INITIATED( opidx );
1124 rc = (*(opfun[opidx]))( op, &rs );
1127 if ( rc == SLAPD_DISCONNECT ) {
1130 } else if ( opidx != SLAP_OP_LAST ) {
1131 /* increment completed operations count
1132 * only if operation was initiated
1133 * and rc != SLAPD_DISCONNECT */
1134 INCR_OP_COMPLETED( opidx );
1137 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1138 if ( rc == SLAPD_ABANDON ) {
1139 op->o_cancel = SLAP_CANCEL_ACK;
1141 op->o_cancel = LDAP_TOO_LATE;
1145 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1146 op->o_cancel != SLAP_CANCEL_DONE )
1148 ldap_pvt_thread_yield();
1151 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1153 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1155 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1156 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1158 conn->c_n_ops_executing--;
1159 conn->c_n_ops_completed++;
1163 case LDAP_REQ_UNBIND:
1164 /* c_mutex is locked */
1165 connection_closing( conn,
1166 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1170 connection_resched( conn );
1171 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1175 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1177 int connection_client_setup(
1179 ldap_pvt_thread_start_t *func,
1185 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1186 CONN_IS_CLIENT, 0, NULL );
1187 if ( rc < 0 ) return -1;
1189 c = connection_get( s );
1190 c->c_clientfunc = func;
1191 c->c_clientarg = arg;
1193 slapd_add_internal( s, 0 );
1194 slapd_set_read( s, 1 );
1195 connection_return( c );
1199 void connection_client_enable(
1202 slapd_set_read( s, 1 );
1205 void connection_client_stop(
1211 /* get (locked) connection */
1212 c = connection_get( s );
1214 assert( c->c_conn_state == SLAP_C_CLIENT );
1216 c->c_listener = NULL;
1217 c->c_conn_state = SLAP_C_INVALID;
1218 c->c_struct_state = SLAP_C_UNUSED;
1219 c->c_close_reason = "?"; /* should never be needed */
1221 c->c_sb = ber_sockbuf_alloc( );
1223 ber_len_t max = sockbuf_max_incoming;
1224 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1226 slapd_remove( s, sb, 0, 1, 0 );
1228 connection_return( c );
1231 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1233 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1235 static void* connection_read_thread( void* ctx, void* argv )
1238 conn_readinfo cri = { NULL, NULL, NULL, 0 };
1239 ber_socket_t s = (long)argv;
1242 * read incoming LDAP requests. If there is more than one,
1243 * the first one is returned with new_op
1245 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1246 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1247 return (void*)(long)rc;
1250 /* execute a single queued request in the same thread */
1251 if( cri.op && !cri.nullop ) {
1252 rc = (long)connection_operation( ctx, cri.op );
1253 } else if ( cri.func ) {
1254 rc = (long)cri.func( ctx, cri.arg );
1257 return (void*)(long)rc;
1260 int connection_read_activate( ber_socket_t s )
1265 * suspend reading on this file descriptor until a connection processing
1266 * thread reads data on it. Otherwise the listener thread will repeatedly
1267 * submit the same event on it to the pool.
1269 rc = slapd_clr_read( s, 0 );
1273 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1274 connection_read_thread, (void *)(long)s );
1277 Debug( LDAP_DEBUG_ANY,
1278 "connection_read_activate(%d): submit failed (%d)\n",
1286 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1288 connection_read( ber_socket_t s, conn_readinfo *cri )
1290 int connection_read(ber_socket_t s)
1296 assert( connections != NULL );
1298 /* get (locked) connection */
1299 c = connection_get( s );
1302 Debug( LDAP_DEBUG_ANY,
1303 "connection_read(%ld): no connection!\n",
1311 if( c->c_conn_state == SLAP_C_CLOSING ) {
1312 Debug( LDAP_DEBUG_TRACE,
1313 "connection_read(%d): closing, ignoring input for id=%lu\n",
1314 s, c->c_connid, 0 );
1315 connection_return( c );
1319 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1320 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1321 cri->func = c->c_clientfunc;
1322 cri->arg = c->c_clientarg;
1323 /* read should already be cleared */
1325 slapd_clr_read( s, 0 );
1326 ldap_pvt_thread_pool_submit( &connection_pool,
1327 c->c_clientfunc, c->c_clientarg );
1329 connection_return( c );
1333 Debug( LDAP_DEBUG_TRACE,
1334 "connection_read(%d): checking for input on id=%lu\n",
1335 s, c->c_connid, 0 );
1338 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1339 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1341 Debug( LDAP_DEBUG_TRACE,
1342 "connection_read(%d): TLS accept failure "
1343 "error=%d id=%lu, closing\n",
1344 s, rc, c->c_connid );
1346 c->c_needs_tls_accept = 0;
1347 /* c_mutex is locked */
1348 connection_closing( c, "TLS negotiation failure" );
1349 connection_close( c );
1350 connection_return( c );
1353 } else if ( rc == 0 ) {
1355 struct berval authid = BER_BVNULL;
1357 c->c_needs_tls_accept = 0;
1359 /* we need to let SASL know */
1360 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1362 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1363 if( c->c_tls_ssf > c->c_ssf ) {
1364 c->c_ssf = c->c_tls_ssf;
1367 rc = dnX509peerNormalize( ssl, &authid );
1368 if ( rc != LDAP_SUCCESS ) {
1369 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1370 "unable to get TLS client DN, error=%d id=%lu\n",
1371 s, rc, c->c_connid );
1373 Statslog( LDAP_DEBUG_STATS,
1374 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1375 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1376 slap_sasl_external( c, c->c_tls_ssf, &authid );
1377 if ( authid.bv_val ) free( authid.bv_val );
1380 /* if success and data is ready, fall thru to data input loop */
1381 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1383 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1384 slapd_set_read( s, 1 );
1387 connection_return( c );
1393 #ifdef HAVE_CYRUS_SASL
1394 if ( c->c_sasl_layers ) {
1395 /* If previous layer is not removed yet, give up for now */
1396 if ( !c->c_sasl_sockctx ) {
1397 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1398 slapd_set_read( s, 1 );
1401 connection_return( c );
1405 c->c_sasl_layers = 0;
1407 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1408 if( rc != LDAP_SUCCESS ) {
1409 Debug( LDAP_DEBUG_TRACE,
1410 "connection_read(%d): SASL install error "
1411 "error=%d id=%lu, closing\n",
1412 s, rc, c->c_connid );
1414 /* c_mutex is locked */
1415 connection_closing( c, "SASL layer install failure" );
1416 connection_close( c );
1417 connection_return( c );
1423 #define CONNECTION_INPUT_LOOP 1
1424 /* #define DATA_READY_LOOP 1 */
1427 /* How do we do this without getting into a busy loop ? */
1428 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1429 rc = connection_input( c, cri );
1431 rc = connection_input( c );
1434 #ifdef DATA_READY_LOOP
1435 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1436 #elif CONNECTION_INPUT_LOOP
1443 Debug( LDAP_DEBUG_CONNS,
1444 "connection_read(%d): input error=%d id=%lu, closing.\n",
1445 s, rc, c->c_connid );
1447 /* c_mutex is locked */
1448 connection_closing( c, conn_lost_str );
1449 connection_close( c );
1450 connection_return( c );
1454 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1455 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1456 slapd_set_write( s, 0 );
1459 slapd_set_read( s, 1 );
1461 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1462 slapd_set_read( s, 1 );
1465 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1466 slapd_set_write( s, 1 );
1470 connection_return( c );
1476 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1477 connection_input( Connection *conn , conn_readinfo *cri )
1479 connection_input( Connection *conn )
1488 #ifdef LDAP_CONNECTIONLESS
1494 if ( conn->c_currentber == NULL &&
1495 ( conn->c_currentber = ber_alloc()) == NULL )
1497 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1503 #ifdef LDAP_CONNECTIONLESS
1504 if ( conn->c_is_udp ) {
1505 char peername[sizeof("IP=255.255.255.255:65336")];
1507 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1508 if (len != sizeof(struct sockaddr)) return 1;
1510 sprintf( peername, "IP=%s:%d",
1511 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1512 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1513 Statslog( LDAP_DEBUG_STATS,
1514 "conn=%lu UDP request from %s (%s) accepted.\n",
1515 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1519 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1520 if ( tag != LDAP_TAG_MESSAGE ) {
1521 int err = sock_errno();
1524 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1526 Debug( LDAP_DEBUG_TRACE,
1527 "ber_get_next on fd %d failed errno=%d (%s)\n",
1528 sd, err, sock_errstr(err) );
1529 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1530 /* log, close and send error */
1531 ber_free( conn->c_currentber, 1 );
1532 conn->c_currentber = NULL;
1539 ber = conn->c_currentber;
1540 conn->c_currentber = NULL;
1542 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1543 /* log, close and send error */
1544 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1549 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1550 /* log, close and send error */
1551 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1557 #ifdef LDAP_CONNECTIONLESS
1558 if( conn->c_is_udp ) {
1559 if( tag == LBER_OCTETSTRING ) {
1560 ber_get_stringa( ber, &cdn );
1561 tag = ber_peek_tag(ber, &len);
1563 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1564 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1571 if(tag == LDAP_REQ_BIND) {
1572 /* immediately abandon all existing operations upon BIND */
1573 connection_abandon( conn );
1576 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1579 /* clear state if the connection is being reused from inactive */
1580 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1581 memset( &conn->c_pagedresults_state, 0,
1582 sizeof( conn->c_pagedresults_state ) );
1585 op->o_res_ber = NULL;
1587 #ifdef LDAP_CONNECTIONLESS
1588 if (conn->c_is_udp) {
1590 ber_str2bv( cdn, 0, 1, &op->o_dn );
1591 op->o_protocol = LDAP_VERSION2;
1593 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1594 if (op->o_res_ber == NULL) return 1;
1596 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1597 sizeof(struct sockaddr), 0 );
1599 if (rc != sizeof(struct sockaddr)) {
1600 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1604 if (op->o_protocol == LDAP_VERSION2) {
1605 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1607 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1612 #endif /* LDAP_CONNECTIONLESS */
1616 /* Don't process requests when the conn is in the middle of a
1617 * Bind, or if it's closing. Also, don't let any single conn
1618 * use up all the available threads, and don't execute if we're
1619 * currently blocked on output. And don't execute if there are
1620 * already pending ops, let them go first. Abandon operations
1621 * get exceptions to some, but not all, cases.
1625 /* Abandon and Unbind are exempt from these checks */
1626 if (conn->c_conn_state == SLAP_C_CLOSING) {
1629 } else if (conn->c_writewaiter) {
1630 defer = "awaiting write";
1632 } else if (conn->c_n_ops_pending) {
1633 defer = "pending operations";
1637 case LDAP_REQ_ABANDON:
1638 /* Unbind is exempt from these checks */
1639 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1640 defer = "too many executing";
1642 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1647 case LDAP_REQ_UNBIND:
1652 int max = conn->c_dn.bv_len
1653 ? slap_conn_max_pending_auth
1654 : slap_conn_max_pending;
1656 Debug( LDAP_DEBUG_ANY,
1657 "connection_input: conn=%lu deferring operation: %s\n",
1658 conn->c_connid, defer, 0 );
1659 conn->c_n_ops_pending++;
1660 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1661 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1664 conn->c_n_ops_executing++;
1666 #ifdef SLAP_LIGHTWEIGHT_DISPATCHER
1668 * The first op will be processed in the same thread context,
1669 * as long as there is only one op total.
1670 * Subsequent ops will be submitted to the pool by
1671 * calling connection_op_activate()
1673 if ( cri->op == NULL ) {
1674 /* the first incoming request */
1675 connection_op_queue( op );
1678 if ( !cri->nullop ) {
1680 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1681 connection_operation, (void *) cri->op );
1683 connection_op_activate( op );
1686 connection_op_activate( op );
1691 if ( conn->c_struct_state != SLAP_C_USED ) {
1692 /* connection must have got closed underneath us */
1697 assert( conn->c_struct_state == SLAP_C_USED );
1702 connection_resched( Connection *conn )
1706 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1708 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1710 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1711 "attempting closing conn=%lu sd=%d\n",
1712 conn->c_connid, sd, 0 );
1713 connection_close( conn );
1717 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1718 /* other states need different handling */
1722 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1723 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1725 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1726 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1728 /* pending operations should not be marked for abandonment */
1729 assert(!op->o_abandon);
1731 conn->c_n_ops_pending--;
1732 conn->c_n_ops_executing++;
1734 connection_op_activate( op );
1736 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1742 connection_init_log_prefix( Operation *op )
1744 if ( op->o_connid == (unsigned long)(-1) ) {
1745 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1746 "conn=-1 op=%lu", op->o_opid );
1749 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1750 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1754 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1756 op->o_conn->c_sasl_bindop = NULL;
1758 ch_free( op->o_callback );
1759 op->o_callback = NULL;
1761 return SLAP_CB_CONTINUE;
1764 static int connection_bind_cb( Operation *op, SlapReply *rs )
1766 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1767 if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1768 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1769 op->o_conn->c_sasl_bind_in_progress =
1770 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1772 /* Moved here from bind.c due to ITS#4158 */
1773 op->o_conn->c_sasl_bindop = NULL;
1774 if ( op->orb_method == LDAP_AUTH_SASL ) {
1775 if( rs->sr_err == LDAP_SUCCESS ) {
1776 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1777 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1778 /* edn is always normalized already */
1779 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1781 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1782 BER_BVZERO( &op->orb_edn );
1783 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1784 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1786 op->o_conn->c_sasl_ssf = op->orb_ssf;
1787 if( op->orb_ssf > op->o_conn->c_ssf ) {
1788 op->o_conn->c_ssf = op->orb_ssf;
1791 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1792 ber_len_t max = sockbuf_max_incoming_auth;
1793 ber_sockbuf_ctrl( op->o_conn->c_sb,
1794 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1797 /* log authorization identity */
1798 Statslog( LDAP_DEBUG_STATS,
1799 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
1801 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1802 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
1804 Debug( LDAP_DEBUG_TRACE,
1805 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
1806 op->o_conn->c_authmech.bv_val,
1807 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1810 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1811 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1812 free( op->o_conn->c_sasl_bind_mech.bv_val );
1813 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1817 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1819 ch_free( op->o_callback );
1820 op->o_callback = NULL;
1822 return SLAP_CB_CONTINUE;
1825 static void connection_op_queue( Operation *op )
1827 ber_tag_t tag = op->o_tag;
1829 if (tag == LDAP_REQ_BIND) {
1830 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1831 sc->sc_response = connection_bind_cb;
1832 sc->sc_cleanup = connection_bind_cleanup_cb;
1833 sc->sc_next = op->o_callback;
1834 op->o_callback = sc;
1835 op->o_conn->c_conn_state = SLAP_C_BINDING;
1838 if (!op->o_dn.bv_len) {
1839 op->o_authz = op->o_conn->c_authz;
1840 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1841 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1842 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1844 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1845 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1849 op->o_authtype = op->o_conn->c_authtype;
1850 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1852 if (!op->o_protocol) {
1853 op->o_protocol = op->o_conn->c_protocol
1854 ? op->o_conn->c_protocol : LDAP_VERSION3;
1857 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1858 op->o_protocol > LDAP_VERSION2)
1860 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1863 op->o_connid = op->o_conn->c_connid;
1864 connection_init_log_prefix( op );
1866 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1869 static int connection_op_activate( Operation *op )
1873 connection_op_queue( op );
1875 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1876 connection_operation, (void *) op );
1879 Debug( LDAP_DEBUG_ANY,
1880 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1881 rc, op->o_connid, 0 );
1882 /* should move op to pending list */
1888 int connection_write(ber_socket_t s)
1893 assert( connections != NULL );
1895 slapd_clr_write( s, 0 );
1897 c = connection_get( s );
1899 Debug( LDAP_DEBUG_ANY,
1900 "connection_write(%ld): no connection!\n",
1907 Debug( LDAP_DEBUG_TRACE,
1908 "connection_write(%d): waking output for id=%lu\n",
1909 s, c->c_connid, 0 );
1910 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1912 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1913 slapd_set_read( s, 1 );
1915 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1916 slapd_set_write( s, 1 );
1919 /* If there are ops pending because of a writewaiter,
1922 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1923 if ( !c->c_writewaiter ) break;
1924 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1926 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1927 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1929 /* pending operations should not be marked for abandonment */
1930 assert(!op->o_abandon);
1932 c->c_n_ops_pending--;
1933 c->c_n_ops_executing++;
1935 connection_op_activate( op );
1940 connection_return( c );
1945 connection_fake_init(
1950 conn->c_connid = -1;
1951 conn->c_send_ldap_result = slap_send_ldap_result;
1952 conn->c_send_search_entry = slap_send_search_entry;
1953 conn->c_send_search_reference = slap_send_search_reference;
1954 conn->c_listener = (Listener *)&dummy_list;
1955 conn->c_peer_domain = slap_empty_bv;
1956 conn->c_peer_name = slap_empty_bv;
1958 memset(op, 0, OPERATION_BUFFER_SIZE);
1959 op->o_hdr = (Opheader *)(op+1);
1960 op->o_controls = (void **)(op->o_hdr+1);
1961 /* set memory context */
1962 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
1963 op->o_tmpmfuncs = &slap_sl_mfuncs;
1964 op->o_threadctx = ctx;
1966 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1967 #endif /* LDAP_DEVEL */
1970 op->o_connid = op->o_conn->c_connid;
1971 connection_init_log_prefix( op );
1974 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
1975 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
1976 #endif /* LDAP_SLAPI */
1978 slap_op_time( &op->o_time, &op->o_tincr );
1982 connection_assign_nextid( Connection *conn )
1984 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1985 conn->c_connid = conn_nextid++;
1986 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );