2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 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 typedef struct conn_readinfo {
88 ldap_pvt_thread_start_t *func;
94 static int connection_input( Connection *c, conn_readinfo *cri );
95 static void connection_close( Connection *c );
97 static int connection_op_activate( Operation *op );
98 static void connection_op_queue( Operation *op );
99 static int connection_resched( Connection *conn );
100 static void connection_abandon( Connection *conn );
101 static void connection_destroy( Connection *c );
103 static ldap_pvt_thread_start_t connection_operation;
106 * Initialize connection management infrastructure.
108 int connections_init(void)
112 assert( connections == NULL );
114 if( connections != NULL) {
115 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
120 /* should check return of every call */
121 ldap_pvt_thread_mutex_init( &connections_mutex );
122 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
124 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
126 if( connections == NULL ) {
127 Debug( LDAP_DEBUG_ANY, "connections_init: "
128 "allocation (%d*%ld) of connection array failed\n",
129 dtblsize, (long) sizeof(Connection), 0 );
133 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
134 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
136 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
139 * per entry initialization of the Connection array initialization
140 * will be done by connection_init()
147 * Destroy connection management infrastructure.
150 int connections_destroy(void)
154 /* should check return of every call */
156 if( connections == NULL) {
157 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
162 for ( i = 0; i < dtblsize; i++ ) {
163 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
164 ber_sockbuf_free( connections[i].c_sb );
165 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
166 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
167 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
169 if ( slapi_plugins_used ) {
170 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION,
180 ldap_pvt_thread_mutex_destroy( &connections_mutex );
181 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
186 * shutdown all connections
188 int connections_shutdown(void)
192 for ( i = 0; i < dtblsize; i++ ) {
193 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
194 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
195 if( connections[i].c_struct_state == SLAP_C_USED ) {
197 /* give persistent clients a chance to cleanup */
198 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
199 ldap_pvt_thread_pool_submit( &connection_pool,
200 connections[i].c_clientfunc, connections[i].c_clientarg );
202 /* c_mutex is locked */
203 connection_closing( &connections[i], "slapd shutdown" );
204 connection_close( &connections[i] );
207 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
215 * Timeout idle connections.
217 int connections_timeout_idle(time_t now)
223 for( c = connection_first( &connindex );
225 c = connection_next( c, &connindex ) )
227 /* Don't timeout a slow-running request or a persistent
228 * outbound connection. But if it has a writewaiter, see
229 * if the waiter has been there too long.
231 if(( c->c_n_ops_executing && !c->c_writewaiter)
232 || c->c_conn_state == SLAP_C_CLIENT ) {
236 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
238 connection_closing( c, "idletimeout" );
239 connection_close( c );
243 connection_done( c );
248 static Connection* connection_get( ber_socket_t s )
252 Debug( LDAP_DEBUG_ARGS,
253 "connection_get(%ld)\n",
256 assert( connections != NULL );
258 if(s == AC_SOCKET_INVALID) return NULL;
260 assert( s < dtblsize );
264 ldap_pvt_thread_mutex_lock( &c->c_mutex );
266 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
268 if( c->c_struct_state != SLAP_C_USED ) {
269 /* connection must have been closed due to resched */
271 assert( c->c_conn_state == SLAP_C_INVALID );
272 assert( c->c_sd == AC_SOCKET_INVALID );
274 Debug( LDAP_DEBUG_CONNS,
275 "connection_get(%d): connection not used\n",
278 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
282 Debug( LDAP_DEBUG_TRACE,
283 "connection_get(%d): got connid=%lu\n",
288 assert( c->c_struct_state == SLAP_C_USED );
289 assert( c->c_conn_state != SLAP_C_INVALID );
290 assert( c->c_sd != AC_SOCKET_INVALID );
292 #ifndef SLAPD_MONITOR
293 if ( global_idletimeout > 0 )
294 #endif /* ! SLAPD_MONITOR */
296 c->c_activitytime = slap_get_time();
303 static void connection_return( Connection *c )
305 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
308 Connection * connection_init(
312 const char* peername,
315 struct berval *authid
316 LDAP_PF_LOCAL_SENDMSG_ARG(struct berval *peerbv))
321 ber_socket_t sfd = SLAP_FD2SOCK(s);
323 assert( connections != NULL );
325 assert( listener != NULL );
326 assert( dnsname != NULL );
327 assert( peername != NULL );
330 assert( !( flags & CONN_IS_TLS ));
333 if( s == AC_SOCKET_INVALID ) {
334 Debug( LDAP_DEBUG_ANY,
335 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
340 assert( s < dtblsize );
342 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
345 assert( c->c_struct_state == SLAP_C_UNUSED );
349 c->c_send_ldap_result = slap_send_ldap_result;
350 c->c_send_search_entry = slap_send_search_entry;
351 c->c_send_search_reference = slap_send_search_reference;
352 c->c_send_ldap_extended = slap_send_ldap_extended;
353 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
355 BER_BVZERO( &c->c_authmech );
356 BER_BVZERO( &c->c_dn );
357 BER_BVZERO( &c->c_ndn );
359 c->c_listener = NULL;
360 BER_BVZERO( &c->c_peer_domain );
361 BER_BVZERO( &c->c_peer_name );
363 LDAP_STAILQ_INIT(&c->c_ops);
364 LDAP_STAILQ_INIT(&c->c_pending_ops);
367 c->c_txn = CONN_TXN_INACTIVE;
368 c->c_txn_backend = NULL;
369 LDAP_STAILQ_INIT(&c->c_txn_ops);
372 BER_BVZERO( &c->c_sasl_bind_mech );
374 c->c_sasl_authctx = NULL;
375 c->c_sasl_sockctx = NULL;
376 c->c_sasl_extra = NULL;
377 c->c_sasl_bindop = NULL;
379 c->c_sb = ber_sockbuf_alloc( );
382 ber_len_t max = sockbuf_max_incoming;
383 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
386 c->c_currentber = NULL;
388 /* should check status of thread calls */
389 ldap_pvt_thread_mutex_init( &c->c_mutex );
390 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
391 ldap_pvt_thread_cond_init( &c->c_write_cv );
394 if ( slapi_plugins_used ) {
395 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
400 ldap_pvt_thread_mutex_lock( &c->c_mutex );
402 assert( BER_BVISNULL( &c->c_authmech ) );
403 assert( BER_BVISNULL( &c->c_dn ) );
404 assert( BER_BVISNULL( &c->c_ndn ) );
405 assert( c->c_listener == NULL );
406 assert( BER_BVISNULL( &c->c_peer_domain ) );
407 assert( BER_BVISNULL( &c->c_peer_name ) );
408 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
409 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
411 assert( c->c_txn == CONN_TXN_INACTIVE );
412 assert( c->c_txn_backend == NULL );
413 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
415 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
416 assert( c->c_sasl_done == 0 );
417 assert( c->c_sasl_authctx == NULL );
418 assert( c->c_sasl_sockctx == NULL );
419 assert( c->c_sasl_extra == NULL );
420 assert( c->c_sasl_bindop == NULL );
421 assert( c->c_currentber == NULL );
422 assert( c->c_writewaiter == 0);
424 c->c_listener = listener;
427 if ( flags & CONN_IS_CLIENT ) {
429 ldap_pvt_thread_mutex_lock( &connections_mutex );
430 c->c_conn_state = SLAP_C_CLIENT;
431 c->c_struct_state = SLAP_C_USED;
432 ldap_pvt_thread_mutex_unlock( &connections_mutex );
433 c->c_close_reason = "?"; /* should never be needed */
434 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
435 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
440 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
441 ber_str2bv( peername, 0, 1, &c->c_peer_name );
443 c->c_n_ops_received = 0;
444 c->c_n_ops_executing = 0;
445 c->c_n_ops_pending = 0;
446 c->c_n_ops_completed = 0;
452 /* set to zero until bind, implies LDAP_VERSION3 */
455 #ifndef SLAPD_MONITOR
456 if ( global_idletimeout > 0 )
457 #endif /* ! SLAPD_MONITOR */
459 c->c_activitytime = c->c_starttime = slap_get_time();
462 #ifdef LDAP_CONNECTIONLESS
464 if( flags & CONN_IS_UDP ) {
467 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
468 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
470 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
471 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
472 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
473 LBER_SBIOD_LEVEL_PROVIDER, NULL );
475 #endif /* LDAP_CONNECTIONLESS */
477 if ( flags & CONN_IS_IPC ) {
479 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
480 LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
482 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
483 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
484 #ifdef LDAP_PF_LOCAL_SENDMSG
485 if ( !BER_BVISEMPTY( peerbv ))
486 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
489 #endif /* LDAP_PF_LOCAL */
492 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
493 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
495 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
496 LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
500 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
501 INT_MAX, (void*)"ldap_" );
504 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
505 c /* non-NULL */ ) < 0 )
507 Debug( LDAP_DEBUG_ANY,
508 "connection_init(%d, %s): set nonblocking failed\n",
509 s, c->c_peer_name.bv_val, 0 );
512 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
513 id = c->c_connid = conn_nextid++;
514 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
516 ldap_pvt_thread_mutex_lock( &connections_mutex );
517 c->c_conn_state = SLAP_C_INACTIVE;
518 c->c_struct_state = SLAP_C_USED;
519 ldap_pvt_thread_mutex_unlock( &connections_mutex );
520 c->c_close_reason = "?"; /* should never be needed */
522 c->c_ssf = c->c_transport_ssf = ssf;
526 if ( flags & CONN_IS_TLS ) {
528 c->c_needs_tls_accept = 1;
531 c->c_needs_tls_accept = 0;
535 slap_sasl_open( c, 0 );
536 slap_sasl_external( c, ssf, authid );
538 slapd_add_internal( s, 1 );
539 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
541 backend_connection_init(c);
546 void connection2anonymous( Connection *c )
548 assert( connections != NULL );
552 ber_len_t max = sockbuf_max_incoming;
553 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
556 if ( !BER_BVISNULL( &c->c_authmech ) ) {
557 ch_free(c->c_authmech.bv_val);
559 BER_BVZERO( &c->c_authmech );
561 if ( !BER_BVISNULL( &c->c_dn ) ) {
562 ch_free(c->c_dn.bv_val);
564 BER_BVZERO( &c->c_dn );
566 if ( !BER_BVISNULL( &c->c_ndn ) ) {
567 ch_free(c->c_ndn.bv_val);
569 BER_BVZERO( &c->c_ndn );
571 if ( !BER_BVISNULL( &c->c_sasl_authz_dn ) ) {
572 ber_memfree_x( c->c_sasl_authz_dn.bv_val, NULL );
574 BER_BVZERO( &c->c_sasl_authz_dn );
576 c->c_authz_backend = NULL;
580 connection_destroy( Connection *c )
582 unsigned long connid;
583 const char *close_reason;
587 assert( connections != NULL );
589 assert( c->c_struct_state != SLAP_C_UNUSED );
590 assert( c->c_conn_state != SLAP_C_INVALID );
591 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
592 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
594 assert( c->c_txn == CONN_TXN_INACTIVE );
595 assert( c->c_txn_backend == NULL );
596 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
598 assert( c->c_writewaiter == 0);
600 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
601 connid = c->c_connid;
602 close_reason = c->c_close_reason;
604 ldap_pvt_thread_mutex_lock( &connections_mutex );
605 c->c_struct_state = SLAP_C_PENDING;
606 ldap_pvt_thread_mutex_unlock( &connections_mutex );
608 backend_connection_destroy(c);
613 c->c_activitytime = c->c_starttime = 0;
615 connection2anonymous( c );
616 c->c_listener = NULL;
618 if(c->c_peer_domain.bv_val != NULL) {
619 free(c->c_peer_domain.bv_val);
621 BER_BVZERO( &c->c_peer_domain );
622 if(c->c_peer_name.bv_val != NULL) {
623 free(c->c_peer_name.bv_val);
625 BER_BVZERO( &c->c_peer_name );
627 c->c_sasl_bind_in_progress = 0;
628 if(c->c_sasl_bind_mech.bv_val != NULL) {
629 free(c->c_sasl_bind_mech.bv_val);
631 BER_BVZERO( &c->c_sasl_bind_mech );
633 slap_sasl_close( c );
635 if ( c->c_currentber != NULL ) {
636 ber_free( c->c_currentber, 1 );
637 c->c_currentber = NULL;
642 /* call destructors, then constructors; avoids unnecessary allocation */
643 if ( slapi_plugins_used ) {
644 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
649 c->c_sd = AC_SOCKET_INVALID;
650 c->c_conn_state = SLAP_C_INVALID;
651 c->c_struct_state = SLAP_C_UNUSED;
652 c->c_close_reason = "?"; /* should never be needed */
655 c->c_sb = ber_sockbuf_alloc( );
657 ber_len_t max = sockbuf_max_incoming;
658 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
661 /* c must be fully reset by this point; when we call slapd_remove
662 * it may get immediately reused by a new connection.
664 if ( sd != AC_SOCKET_INVALID ) {
665 slapd_remove( sd, sb, 1, 0, 0 );
667 if ( close_reason == NULL ) {
668 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed\n",
669 connid, (long) sd, 0, 0, 0 );
671 Statslog( LDAP_DEBUG_STATS, "conn=%lu fd=%ld closed (%s)\n",
672 connid, (long) sd, close_reason, 0, 0 );
677 int connection_state_closing( Connection *c )
679 /* c_mutex must be locked by caller */
683 assert( c->c_struct_state == SLAP_C_USED );
685 state = c->c_conn_state;
687 assert( state != SLAP_C_INVALID );
689 return state == SLAP_C_CLOSING;
692 static void connection_abandon( Connection *c )
694 /* c_mutex must be locked by caller */
696 Operation *o, *next, op = {0};
702 op.o_connid = c->c_connid;
703 op.o_tag = LDAP_REQ_ABANDON;
705 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
706 next = LDAP_STAILQ_NEXT( o, o_next );
707 op.orn_msgid = o->o_msgid;
709 op.o_bd = frontendDB;
710 frontendDB->be_abandon( &op, &rs );
714 /* remove operations in pending transaction */
715 while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) {
716 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
717 LDAP_STAILQ_NEXT(o, o_next) = NULL;
718 slap_op_free( o, NULL );
721 /* clear transaction */
722 c->c_txn_backend = NULL;
723 c->c_txn = CONN_TXN_INACTIVE;
726 /* remove pending operations */
727 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
728 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
729 LDAP_STAILQ_NEXT(o, o_next) = NULL;
730 slap_op_free( o, NULL );
734 void connection_closing( Connection *c, const char *why )
736 assert( connections != NULL );
738 assert( c->c_struct_state == SLAP_C_USED );
739 assert( c->c_conn_state != SLAP_C_INVALID );
741 /* c_mutex must be locked by caller */
743 if( c->c_conn_state != SLAP_C_CLOSING ) {
744 Debug( LDAP_DEBUG_CONNS,
745 "connection_closing: readying conn=%lu sd=%d for close\n",
746 c->c_connid, c->c_sd, 0 );
747 /* update state to closing */
748 c->c_conn_state = SLAP_C_CLOSING;
749 c->c_close_reason = why;
751 /* don't listen on this port anymore */
752 slapd_clr_read( c->c_sd, 0 );
754 /* abandon active operations */
755 connection_abandon( c );
757 /* wake write blocked operations */
758 if ( c->c_writewaiter ) {
759 ldap_pvt_thread_cond_signal( &c->c_write_cv );
760 /* ITS#4667 this may allow another thread to drop into
761 * connection_resched / connection_close before we
762 * finish, but that's OK.
764 slapd_clr_write( c->c_sd, 1 );
765 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
766 ldap_pvt_thread_mutex_lock( &c->c_write_mutex );
767 ldap_pvt_thread_mutex_lock( &c->c_mutex );
768 ldap_pvt_thread_mutex_unlock( &c->c_write_mutex );
770 slapd_clr_write( c->c_sd, 1 );
773 } else if( why == NULL && c->c_close_reason == conn_lost_str ) {
774 /* Client closed connection after doing Unbind. */
775 c->c_close_reason = NULL;
780 connection_close( Connection *c )
782 assert( connections != NULL );
785 /* ITS#4667 we may have gotten here twice */
786 if ( c->c_conn_state == SLAP_C_INVALID )
789 assert( c->c_struct_state == SLAP_C_USED );
790 assert( c->c_conn_state == SLAP_C_CLOSING );
792 /* NOTE: c_mutex should be locked by caller */
794 if ( !LDAP_STAILQ_EMPTY(&c->c_ops) ||
795 !LDAP_STAILQ_EMPTY(&c->c_pending_ops) )
797 Debug( LDAP_DEBUG_CONNS,
798 "connection_close: deferring conn=%lu sd=%d\n",
799 c->c_connid, c->c_sd, 0 );
803 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
804 c->c_connid, c->c_sd, 0 );
806 connection_destroy( c );
809 unsigned long connections_nextid(void)
812 assert( connections != NULL );
814 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
818 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
823 Connection* connection_first( ber_socket_t *index )
825 assert( connections != NULL );
826 assert( index != NULL );
828 ldap_pvt_thread_mutex_lock( &connections_mutex );
829 for( *index = 0; *index < dtblsize; (*index)++) {
830 if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) {
834 ldap_pvt_thread_mutex_unlock( &connections_mutex );
836 return connection_next(NULL, index);
839 Connection* connection_next( Connection *c, ber_socket_t *index )
841 assert( connections != NULL );
842 assert( index != NULL );
843 assert( *index <= dtblsize );
845 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
849 ldap_pvt_thread_mutex_lock( &connections_mutex );
850 for(; *index < dtblsize; (*index)++) {
852 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
853 /* FIXME: accessing c_conn_state without locking c_mutex */
854 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
858 if( connections[*index].c_struct_state == SLAP_C_USED ) {
859 c = &connections[(*index)++];
860 if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) {
862 ldap_pvt_thread_mutex_unlock( &connections_mutex );
863 ldap_pvt_thread_mutex_lock( &c->c_mutex );
864 ldap_pvt_thread_mutex_lock( &connections_mutex );
865 if ( c->c_struct_state != SLAP_C_USED ) {
866 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
871 assert( c->c_conn_state != SLAP_C_INVALID );
875 c_struct = connections[*index].c_struct_state;
876 if ( c_struct == SLAP_C_PENDING )
878 assert( c_struct == SLAP_C_UNUSED );
879 /* FIXME: accessing c_conn_state without locking c_mutex */
880 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
883 ldap_pvt_thread_mutex_unlock( &connections_mutex );
887 void connection_done( Connection *c )
889 assert( connections != NULL );
891 if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex );
895 * connection_activity - handle the request operation op on connection
896 * conn. This routine figures out what kind of operation it is and
897 * calls the appropriate stub to handle it.
901 /* FIXME: returns 0 in case of failure */
902 #define INCR_OP_INITIATED(index) \
904 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
905 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated_[(index)], 1); \
906 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
908 #define INCR_OP_COMPLETED(index) \
910 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
911 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
912 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed_[(index)], 1); \
913 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
915 #else /* !SLAPD_MONITOR */
916 #define INCR_OP_INITIATED(index) do { } while (0)
917 #define INCR_OP_COMPLETED(index) \
919 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex ); \
920 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_completed, 1); \
921 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex ); \
923 #endif /* !SLAPD_MONITOR */
926 * NOTE: keep in sync with enum in slapd.h
928 static BI_op_func *opfun[] = {
942 /* Counters are per-thread, not per-connection.
945 conn_counter_destroy( void *key, void *data )
947 slap_counters_t **prev, *sc;
949 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
950 for ( prev = &slap_counters.sc_next, sc = slap_counters.sc_next; sc;
951 prev = &sc->sc_next, sc = sc->sc_next ) {
956 /* Copy data to main counter */
957 ldap_pvt_mp_add( slap_counters.sc_bytes, sc->sc_bytes );
958 ldap_pvt_mp_add( slap_counters.sc_pdu, sc->sc_pdu );
959 ldap_pvt_mp_add( slap_counters.sc_entries, sc->sc_entries );
960 ldap_pvt_mp_add( slap_counters.sc_refs, sc->sc_refs );
961 ldap_pvt_mp_add( slap_counters.sc_ops_initiated, sc->sc_ops_initiated );
962 ldap_pvt_mp_add( slap_counters.sc_ops_completed, sc->sc_ops_completed );
964 for ( i = 0; i < SLAP_OP_LAST; i++ ) {
965 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_initiated_[ i ] );
966 ldap_pvt_mp_add( slap_counters.sc_ops_initiated_[ i ], sc->sc_ops_completed_[ i ] );
968 #endif /* SLAPD_MONITOR */
969 slap_counters_destroy( sc );
970 ber_memfree_x( data, NULL );
974 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
978 conn_counter_init( Operation *op, void *ctx )
983 if ( ldap_pvt_thread_pool_getkey(
984 ctx, (void *)conn_counter_init, &vsc, NULL ) || !vsc ) {
985 vsc = ch_malloc( sizeof( slap_counters_t ));
987 slap_counters_init( sc );
988 ldap_pvt_thread_pool_setkey( ctx, (void*)conn_counter_init, vsc,
989 conn_counter_destroy, NULL, NULL );
991 ldap_pvt_thread_mutex_lock( &slap_counters.sc_mutex );
992 sc->sc_next = slap_counters.sc_next;
993 slap_counters.sc_next = sc;
994 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_mutex );
996 op->o_counters = vsc;
1000 connection_operation( void *ctx, void *arg_v )
1002 int rc = LDAP_OTHER;
1003 Operation *op = arg_v;
1004 SlapReply rs = {REP_RESULT};
1005 ber_tag_t tag = op->o_tag;
1006 slap_op_t opidx = SLAP_OP_LAST;
1007 Connection *conn = op->o_conn;
1008 void *memctx = NULL;
1009 void *memctx_null = NULL;
1012 conn_counter_init( op, ctx );
1013 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1014 /* FIXME: returns 0 in case of failure */
1015 ldap_pvt_mp_add_ulong(op->o_counters->sc_ops_initiated, 1);
1016 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1018 op->o_threadctx = ctx;
1019 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1023 case LDAP_REQ_UNBIND:
1025 case LDAP_REQ_DELETE:
1026 case LDAP_REQ_MODDN:
1027 case LDAP_REQ_MODIFY:
1028 case LDAP_REQ_COMPARE:
1029 case LDAP_REQ_SEARCH:
1030 case LDAP_REQ_ABANDON:
1031 case LDAP_REQ_EXTENDED:
1034 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1035 "conn %lu unknown LDAP request 0x%lx\n",
1036 conn->c_connid, tag, 0 );
1037 op->o_tag = LBER_ERROR;
1038 rs.sr_err = LDAP_PROTOCOL_ERROR;
1039 rs.sr_text = "unknown LDAP request";
1040 send_ldap_disconnect( op, &rs );
1041 rc = SLAPD_DISCONNECT;
1042 goto operations_error;
1045 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
1046 Debug( LDAP_DEBUG_ANY, "connection_operation: "
1047 "error: SASL bind in progress (tag=%ld).\n",
1049 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
1050 "SASL bind in progress" );
1051 rc = LDAP_OPERATIONS_ERROR;
1052 goto operations_error;
1056 if (( conn->c_txn == CONN_TXN_SPECIFY ) && (
1057 ( tag == LDAP_REQ_ADD ) ||
1058 ( tag == LDAP_REQ_DELETE ) ||
1059 ( tag == LDAP_REQ_MODIFY ) ||
1060 ( tag == LDAP_REQ_MODRDN )))
1062 /* Disable SLAB allocator for all update operations
1063 issued inside of a transaction */
1064 op->o_tmpmemctx = NULL;
1065 op->o_tmpmfuncs = &ch_mfuncs;
1069 /* We can use Thread-Local storage for most mallocs. We can
1070 * also use TL for ber parsing, but not on Add or Modify.
1073 memsiz = ber_len( op->o_ber ) * 64;
1074 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
1076 memsiz = SLAP_SLAB_SIZE;
1078 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx, 1 );
1079 op->o_tmpmemctx = memctx;
1080 op->o_tmpmfuncs = &slap_sl_mfuncs;
1081 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
1082 /* Note - the ber and its buffer are already allocated from
1083 * regular memory; this only affects subsequent mallocs that
1084 * ber_scanf may invoke.
1086 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1090 opidx = slap_req2op( tag );
1091 assert( opidx != SLAP_OP_LAST );
1092 INCR_OP_INITIATED( opidx );
1093 rc = (*(opfun[opidx]))( op, &rs );
1096 if ( rc == SLAPD_DISCONNECT ) {
1099 } else if ( opidx != SLAP_OP_LAST ) {
1100 /* increment completed operations count
1101 * only if operation was initiated
1102 * and rc != SLAPD_DISCONNECT */
1103 INCR_OP_COMPLETED( opidx );
1106 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1107 if ( rc == SLAPD_ABANDON ) {
1108 op->o_cancel = SLAP_CANCEL_ACK;
1110 op->o_cancel = LDAP_TOO_LATE;
1114 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1115 op->o_cancel != SLAP_CANCEL_DONE )
1117 ldap_pvt_thread_yield();
1120 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1122 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1124 LDAP_STAILQ_REMOVE( &conn->c_ops, op, Operation, o_next);
1125 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1126 conn->c_n_ops_executing--;
1127 conn->c_n_ops_completed++;
1131 case LDAP_REQ_UNBIND:
1132 /* c_mutex is locked */
1133 connection_closing( conn,
1134 tag == LDAP_REQ_UNBIND ? NULL : "operations error" );
1138 connection_resched( conn );
1139 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1140 slap_op_free( op, ctx );
1144 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1146 Connection *connection_client_setup(
1148 ldap_pvt_thread_start_t *func,
1152 ber_socket_t sfd = SLAP_SOCKNEW( s );
1154 c = connection_init( sfd, (Listener *)&dummy_list, "", "",
1155 CONN_IS_CLIENT, 0, NULL
1156 LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
1158 c->c_clientfunc = func;
1159 c->c_clientarg = arg;
1161 slapd_add_internal( sfd, 0 );
1166 void connection_client_enable(
1169 slapd_set_read( c->c_sd, 1 );
1172 void connection_client_stop(
1176 ber_socket_t s = c->c_sd;
1178 /* get (locked) connection */
1179 c = connection_get( s );
1181 assert( c->c_conn_state == SLAP_C_CLIENT );
1183 c->c_listener = NULL;
1184 c->c_conn_state = SLAP_C_INVALID;
1185 c->c_struct_state = SLAP_C_UNUSED;
1186 c->c_sd = AC_SOCKET_INVALID;
1187 c->c_close_reason = "?"; /* should never be needed */
1189 c->c_sb = ber_sockbuf_alloc( );
1191 ber_len_t max = sockbuf_max_incoming;
1192 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
1194 slapd_remove( s, sb, 0, 1, 0 );
1196 connection_return( c );
1199 static int connection_read( ber_socket_t s, conn_readinfo *cri );
1201 static void* connection_read_thread( void* ctx, void* argv )
1204 conn_readinfo cri = { NULL, NULL, NULL, NULL, 0 };
1205 ber_socket_t s = (long)argv;
1208 * read incoming LDAP requests. If there is more than one,
1209 * the first one is returned with new_op
1212 if( ( rc = connection_read( s, &cri ) ) < 0 ) {
1213 Debug( LDAP_DEBUG_CONNS, "connection_read(%d) error\n", s, 0, 0 );
1214 return (void*)(long)rc;
1217 /* execute a single queued request in the same thread */
1218 if( cri.op && !cri.nullop ) {
1219 rc = (long)connection_operation( ctx, cri.op );
1220 } else if ( cri.func ) {
1221 rc = (long)cri.func( ctx, cri.arg );
1224 return (void*)(long)rc;
1227 int connection_read_activate( ber_socket_t s )
1232 * suspend reading on this file descriptor until a connection processing
1233 * thread reads data on it. Otherwise the listener thread will repeatedly
1234 * submit the same event on it to the pool.
1236 rc = slapd_clr_read( s, 0 );
1240 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1241 connection_read_thread, (void *)(long)s );
1244 Debug( LDAP_DEBUG_ANY,
1245 "connection_read_activate(%d): submit failed (%d)\n",
1253 connection_read( ber_socket_t s, conn_readinfo *cri )
1258 assert( connections != NULL );
1260 /* get (locked) connection */
1261 c = connection_get( s );
1264 Debug( LDAP_DEBUG_ANY,
1265 "connection_read(%ld): no connection!\n",
1273 if( c->c_conn_state == SLAP_C_CLOSING ) {
1274 Debug( LDAP_DEBUG_CONNS,
1275 "connection_read(%d): closing, ignoring input for id=%lu\n",
1276 s, c->c_connid, 0 );
1277 connection_return( c );
1281 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1282 cri->func = c->c_clientfunc;
1283 cri->arg = c->c_clientarg;
1284 /* read should already be cleared */
1285 connection_return( c );
1289 Debug( LDAP_DEBUG_TRACE,
1290 "connection_read(%d): checking for input on id=%lu\n",
1291 s, c->c_connid, 0 );
1294 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1295 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1297 Debug( LDAP_DEBUG_TRACE,
1298 "connection_read(%d): TLS accept failure "
1299 "error=%d id=%lu, closing\n",
1300 s, rc, c->c_connid );
1302 c->c_needs_tls_accept = 0;
1303 /* c_mutex is locked */
1304 connection_closing( c, "TLS negotiation failure" );
1305 connection_close( c );
1306 connection_return( c );
1309 } else if ( rc == 0 ) {
1311 struct berval authid = BER_BVNULL;
1313 c->c_needs_tls_accept = 0;
1315 /* we need to let SASL know */
1316 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1318 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1319 if( c->c_tls_ssf > c->c_ssf ) {
1320 c->c_ssf = c->c_tls_ssf;
1323 rc = dnX509peerNormalize( ssl, &authid );
1324 if ( rc != LDAP_SUCCESS ) {
1325 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1326 "unable to get TLS client DN, error=%d id=%lu\n",
1327 s, rc, c->c_connid );
1329 Statslog( LDAP_DEBUG_STATS,
1330 "conn=%lu fd=%d TLS established tls_ssf=%u ssf=%u\n",
1331 c->c_connid, (int) s, c->c_tls_ssf, c->c_ssf, 0 );
1332 slap_sasl_external( c, c->c_tls_ssf, &authid );
1333 if ( authid.bv_val ) free( authid.bv_val );
1336 /* if success and data is ready, fall thru to data input loop */
1337 if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1339 slapd_set_read( s, 1 );
1340 connection_return( c );
1346 #ifdef HAVE_CYRUS_SASL
1347 if ( c->c_sasl_layers ) {
1348 /* If previous layer is not removed yet, give up for now */
1349 if ( !c->c_sasl_sockctx ) {
1350 slapd_set_read( s, 1 );
1351 connection_return( c );
1355 c->c_sasl_layers = 0;
1357 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1358 if( rc != LDAP_SUCCESS ) {
1359 Debug( LDAP_DEBUG_TRACE,
1360 "connection_read(%d): SASL install error "
1361 "error=%d id=%lu, closing\n",
1362 s, rc, c->c_connid );
1364 /* c_mutex is locked */
1365 connection_closing( c, "SASL layer install failure" );
1366 connection_close( c );
1367 connection_return( c );
1373 #define CONNECTION_INPUT_LOOP 1
1374 /* #define DATA_READY_LOOP 1 */
1377 /* How do we do this without getting into a busy loop ? */
1378 rc = connection_input( c, cri );
1380 #ifdef DATA_READY_LOOP
1381 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1382 #elif defined CONNECTION_INPUT_LOOP
1389 Debug( LDAP_DEBUG_CONNS,
1390 "connection_read(%d): input error=%d id=%lu, closing.\n",
1391 s, rc, c->c_connid );
1393 /* c_mutex is locked */
1394 connection_closing( c, conn_lost_str );
1395 connection_close( c );
1396 connection_return( c );
1400 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1401 slapd_set_write( s, 0 );
1404 slapd_set_read( s, 1 );
1405 connection_return( c );
1411 connection_input( Connection *conn , conn_readinfo *cri )
1419 #ifdef LDAP_CONNECTIONLESS
1426 if ( conn->c_currentber == NULL &&
1427 ( conn->c_currentber = ber_alloc()) == NULL )
1429 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1435 #ifdef LDAP_CONNECTIONLESS
1436 if ( conn->c_is_udp ) {
1437 char peername[sizeof("IP=255.255.255.255:65336")];
1439 len = ber_int_sb_read(conn->c_sb, &peeraddr, sizeof(struct sockaddr));
1440 if (len != sizeof(struct sockaddr)) return 1;
1442 sprintf( peername, "IP=%s:%d",
1443 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1444 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1445 Statslog( LDAP_DEBUG_STATS,
1446 "conn=%lu UDP request from %s (%s) accepted.\n",
1447 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1451 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1452 if ( tag != LDAP_TAG_MESSAGE ) {
1453 int err = sock_errno();
1455 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1456 /* log, close and send error */
1457 Debug( LDAP_DEBUG_TRACE,
1458 "ber_get_next on fd %d failed errno=%d (%s)\n",
1459 conn->c_sd, err, sock_errstr(err) );
1460 ber_free( conn->c_currentber, 1 );
1461 conn->c_currentber = NULL;
1468 ber = conn->c_currentber;
1469 conn->c_currentber = NULL;
1471 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1472 /* log, close and send error */
1473 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0, 0 );
1478 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1479 /* log, close and send error */
1480 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0, 0 );
1486 #ifdef LDAP_CONNECTIONLESS
1487 if( conn->c_is_udp ) {
1488 if( tag == LBER_OCTETSTRING ) {
1489 ber_get_stringa( ber, &cdn );
1490 tag = ber_peek_tag(ber, &len);
1492 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1493 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1500 if(tag == LDAP_REQ_BIND) {
1501 /* immediately abandon all existing operations upon BIND */
1502 connection_abandon( conn );
1506 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++, ctx );
1509 /* clear state if the connection is being reused from inactive */
1510 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1511 memset( &conn->c_pagedresults_state, 0,
1512 sizeof( conn->c_pagedresults_state ) );
1515 op->o_res_ber = NULL;
1517 #ifdef LDAP_CONNECTIONLESS
1518 if (conn->c_is_udp) {
1520 ber_str2bv( cdn, 0, 1, &op->o_dn );
1521 op->o_protocol = LDAP_VERSION2;
1523 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1524 if (op->o_res_ber == NULL) return 1;
1526 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1527 sizeof(struct sockaddr), 0 );
1529 if (rc != sizeof(struct sockaddr)) {
1530 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1534 if (op->o_protocol == LDAP_VERSION2) {
1535 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1537 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1542 #endif /* LDAP_CONNECTIONLESS */
1546 /* Don't process requests when the conn is in the middle of a
1547 * Bind, or if it's closing. Also, don't let any single conn
1548 * use up all the available threads, and don't execute if we're
1549 * currently blocked on output. And don't execute if there are
1550 * already pending ops, let them go first. Abandon operations
1551 * get exceptions to some, but not all, cases.
1555 /* Abandon and Unbind are exempt from these checks */
1556 if (conn->c_conn_state == SLAP_C_CLOSING) {
1559 } else if (conn->c_writewaiter) {
1560 defer = "awaiting write";
1562 } else if (conn->c_n_ops_pending) {
1563 defer = "pending operations";
1567 case LDAP_REQ_ABANDON:
1568 /* Unbind is exempt from these checks */
1569 if (conn->c_n_ops_executing >= connection_pool_max/2) {
1570 defer = "too many executing";
1572 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1577 case LDAP_REQ_UNBIND:
1582 int max = conn->c_dn.bv_len
1583 ? slap_conn_max_pending_auth
1584 : slap_conn_max_pending;
1586 Debug( LDAP_DEBUG_ANY,
1587 "connection_input: conn=%lu deferring operation: %s\n",
1588 conn->c_connid, defer, 0 );
1589 conn->c_n_ops_pending++;
1590 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1591 rc = ( conn->c_n_ops_pending > max ) ? -1 : 0;
1594 conn->c_n_ops_executing++;
1597 * The first op will be processed in the same thread context,
1598 * as long as there is only one op total.
1599 * Subsequent ops will be submitted to the pool by
1600 * calling connection_op_activate()
1602 if ( cri->op == NULL ) {
1603 /* the first incoming request */
1604 connection_op_queue( op );
1607 if ( !cri->nullop ) {
1609 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1610 connection_operation, (void *) cri->op );
1612 connection_op_activate( op );
1617 if ( conn->c_struct_state != SLAP_C_USED ) {
1618 /* connection must have got closed underneath us */
1623 assert( conn->c_struct_state == SLAP_C_USED );
1628 connection_resched( Connection *conn )
1632 if( conn->c_writewaiter )
1635 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1636 Debug( LDAP_DEBUG_CONNS, "connection_resched: "
1637 "attempting closing conn=%lu sd=%d\n",
1638 conn->c_connid, conn->c_sd, 0 );
1639 connection_close( conn );
1643 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1644 /* other states need different handling */
1648 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1649 if ( conn->c_n_ops_executing > connection_pool_max/2 ) break;
1651 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1652 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1654 /* pending operations should not be marked for abandonment */
1655 assert(!op->o_abandon);
1657 conn->c_n_ops_pending--;
1658 conn->c_n_ops_executing++;
1660 connection_op_activate( op );
1662 if ( conn->c_conn_state == SLAP_C_BINDING ) break;
1668 connection_init_log_prefix( Operation *op )
1670 if ( op->o_connid == (unsigned long)(-1) ) {
1671 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1672 "conn=-1 op=%lu", op->o_opid );
1675 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1676 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1680 static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs )
1682 op->o_conn->c_sasl_bindop = NULL;
1684 ch_free( op->o_callback );
1685 op->o_callback = NULL;
1687 return SLAP_CB_CONTINUE;
1690 static int connection_bind_cb( Operation *op, SlapReply *rs )
1692 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1693 if ( op->o_conn->c_conn_state == SLAP_C_BINDING )
1694 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1695 op->o_conn->c_sasl_bind_in_progress =
1696 ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS );
1698 /* Moved here from bind.c due to ITS#4158 */
1699 op->o_conn->c_sasl_bindop = NULL;
1700 if ( op->orb_method == LDAP_AUTH_SASL ) {
1701 if( rs->sr_err == LDAP_SUCCESS ) {
1702 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
1703 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
1704 /* edn is always normalized already */
1705 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
1707 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
1708 BER_BVZERO( &op->orb_edn );
1709 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
1710 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1712 op->o_conn->c_sasl_ssf = op->orb_ssf;
1713 if( op->orb_ssf > op->o_conn->c_ssf ) {
1714 op->o_conn->c_ssf = op->orb_ssf;
1717 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
1718 ber_len_t max = sockbuf_max_incoming_auth;
1719 ber_sockbuf_ctrl( op->o_conn->c_sb,
1720 LBER_SB_OPT_SET_MAX_INCOMING, &max );
1723 /* log authorization identity */
1724 Statslog( LDAP_DEBUG_STATS,
1725 "%s BIND dn=\"%s\" mech=%s sasl_ssf=%d ssf=%d\n",
1727 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1728 op->o_conn->c_authmech.bv_val,
1729 op->orb_ssf, op->o_conn->c_ssf );
1731 Debug( LDAP_DEBUG_TRACE,
1732 "do_bind: SASL/%s bind: dn=\"%s\" sasl_ssf=%d\n",
1733 op->o_conn->c_authmech.bv_val,
1734 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
1737 } else if ( rs->sr_err != LDAP_SASL_BIND_IN_PROGRESS ) {
1738 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
1739 free( op->o_conn->c_sasl_bind_mech.bv_val );
1740 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
1744 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1746 ch_free( op->o_callback );
1747 op->o_callback = NULL;
1749 return SLAP_CB_CONTINUE;
1752 static void connection_op_queue( Operation *op )
1754 ber_tag_t tag = op->o_tag;
1756 if (tag == LDAP_REQ_BIND) {
1757 slap_callback *sc = ch_calloc( 1, sizeof( slap_callback ));
1758 sc->sc_response = connection_bind_cb;
1759 sc->sc_cleanup = connection_bind_cleanup_cb;
1760 sc->sc_next = op->o_callback;
1761 op->o_callback = sc;
1762 op->o_conn->c_conn_state = SLAP_C_BINDING;
1765 if (!op->o_dn.bv_len) {
1766 op->o_authz = op->o_conn->c_authz;
1767 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1768 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1769 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1771 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1772 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1776 op->o_authtype = op->o_conn->c_authtype;
1777 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1779 if (!op->o_protocol) {
1780 op->o_protocol = op->o_conn->c_protocol
1781 ? op->o_conn->c_protocol : LDAP_VERSION3;
1784 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE &&
1785 op->o_protocol > LDAP_VERSION2)
1787 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1790 op->o_connid = op->o_conn->c_connid;
1791 connection_init_log_prefix( op );
1793 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1796 static int connection_op_activate( Operation *op )
1800 connection_op_queue( op );
1802 rc = ldap_pvt_thread_pool_submit( &connection_pool,
1803 connection_operation, (void *) op );
1806 Debug( LDAP_DEBUG_ANY,
1807 "connection_op_activate: submit failed (%d) for conn=%lu\n",
1808 rc, op->o_connid, 0 );
1809 /* should move op to pending list */
1815 int connection_write(ber_socket_t s)
1820 assert( connections != NULL );
1822 slapd_clr_write( s, 0 );
1824 c = connection_get( s );
1826 Debug( LDAP_DEBUG_ANY,
1827 "connection_write(%ld): no connection!\n",
1834 Debug( LDAP_DEBUG_TRACE,
1835 "connection_write(%d): waking output for id=%lu\n",
1836 s, c->c_connid, 0 );
1837 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1839 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1840 slapd_set_read( s, 1 );
1842 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1843 slapd_set_write( s, 1 );
1846 /* If there are ops pending because of a writewaiter,
1849 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1850 if ( !c->c_writewaiter ) break;
1851 if ( c->c_n_ops_executing > connection_pool_max/2 ) break;
1853 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1854 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1856 /* pending operations should not be marked for abandonment */
1857 assert(!op->o_abandon);
1859 c->c_n_ops_pending--;
1860 c->c_n_ops_executing++;
1862 connection_op_activate( op );
1867 connection_return( c );
1872 typedef struct conn_fake_extblock {
1875 } conn_fake_extblock;
1878 connection_fake_destroy(
1882 Connection conn = {0};
1884 Opheader ohdr = {0};
1886 conn_fake_extblock *eb = data;
1889 op.o_hdr->oh_extensions = eb->eb_op;
1890 conn.c_extensions = eb->eb_conn;
1895 ber_memfree_x( eb, NULL );
1896 slapi_int_free_object_extensions( SLAPI_X_EXT_OPERATION, &op );
1897 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &conn );
1902 connection_fake_init(
1904 OperationBuffer *opbuf,
1907 connection_fake_init2( conn, opbuf, ctx, 1 );
1911 operation_fake_init(
1917 /* set memory context */
1918 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx,
1920 op->o_tmpmfuncs = &slap_sl_mfuncs;
1921 op->o_threadctx = ctx;
1922 op->o_tid = ldap_pvt_thread_pool_tid( ctx );
1924 op->o_counters = &slap_counters;
1926 op->o_connid = op->o_conn->c_connid;
1927 connection_init_log_prefix( op );
1932 connection_fake_init2(
1934 OperationBuffer *opbuf,
1938 Operation *op = (Operation *) opbuf;
1940 conn->c_connid = -1;
1941 conn->c_conn_idx = -1;
1942 conn->c_send_ldap_result = slap_send_ldap_result;
1943 conn->c_send_search_entry = slap_send_search_entry;
1944 conn->c_send_search_reference = slap_send_search_reference;
1945 conn->c_listener = (Listener *)&dummy_list;
1946 conn->c_peer_domain = slap_empty_bv;
1947 conn->c_peer_name = slap_empty_bv;
1949 memset( opbuf, 0, sizeof( *opbuf ));
1950 op->o_hdr = &opbuf->ob_hdr;
1951 op->o_controls = opbuf->ob_controls;
1953 operation_fake_init( conn, op, ctx, newmem );
1956 if ( slapi_plugins_used ) {
1957 conn_fake_extblock *eb;
1960 /* Use thread keys to make sure these eventually get cleaned up */
1961 if ( ldap_pvt_thread_pool_getkey( ctx, (void *)connection_fake_init,
1963 eb = ch_malloc( sizeof( *eb ));
1964 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, conn );
1965 slapi_int_create_object_extensions( SLAPI_X_EXT_OPERATION, op );
1966 eb->eb_conn = conn->c_extensions;
1967 eb->eb_op = op->o_hdr->oh_extensions;
1968 ldap_pvt_thread_pool_setkey( ctx, (void *)connection_fake_init,
1969 eb, connection_fake_destroy, NULL, NULL );
1972 conn->c_extensions = eb->eb_conn;
1973 op->o_hdr->oh_extensions = eb->eb_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 );