2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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 /* structure state (protected by connections_mutex) */
54 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
55 #define SLAP_C_UNUSED 0x01
56 #define SLAP_C_USED 0x02
58 /* connection state (protected by c_mutex ) */
59 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
60 #define SLAP_C_INACTIVE 0x01 /* zero threads */
61 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
62 #define SLAP_C_BINDING 0x03 /* binding */
63 #define SLAP_C_CLOSING 0x04 /* closing */
64 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
67 connection_state2str( int state )
70 case SLAP_C_INVALID: return "!";
71 case SLAP_C_INACTIVE: return "|";
72 case SLAP_C_ACTIVE: return "";
73 case SLAP_C_BINDING: return "B";
74 case SLAP_C_CLOSING: return "C";
75 case SLAP_C_CLIENT: return "L";
81 static Connection* connection_get( ber_socket_t s );
83 static int connection_input( Connection *c );
84 static void connection_close( Connection *c );
86 static int connection_op_activate( Operation *op );
87 static int connection_resched( Connection *conn );
88 static void connection_abandon( Connection *conn );
89 static void connection_destroy( Connection *c );
91 static ldap_pvt_thread_start_t connection_operation;
94 * Initialize connection management infrastructure.
96 int connections_init(void)
100 assert( connections == NULL );
102 if( connections != NULL) {
103 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
108 /* should check return of every call */
109 ldap_pvt_thread_mutex_init( &connections_mutex );
110 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
112 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
114 if( connections == NULL ) {
115 Debug( LDAP_DEBUG_ANY,
116 "connections_init: allocation (%d*%ld) of connection array failed\n",
117 dtblsize, (long) sizeof(Connection), 0 );
121 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
122 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
124 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
127 * per entry initialization of the Connection array initialization
128 * will be done by connection_init()
135 * Destroy connection management infrastructure.
137 int connections_destroy(void)
141 /* should check return of every call */
143 if( connections == NULL) {
144 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
149 for ( i = 0; i < dtblsize; i++ ) {
150 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
151 ber_sockbuf_free( connections[i].c_sb );
152 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
153 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
154 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
156 if ( slapi_plugins_used ) {
157 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &connections[i] );
166 ldap_pvt_thread_mutex_destroy( &connections_mutex );
167 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
172 * shutdown all connections
174 int connections_shutdown(void)
178 ldap_pvt_thread_mutex_lock( &connections_mutex );
180 for ( i = 0; i < dtblsize; i++ ) {
181 if( connections[i].c_struct_state != SLAP_C_USED ) {
184 /* give persistent clients a chance to cleanup */
185 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
186 ldap_pvt_thread_pool_submit( &connection_pool,
187 connections[i].c_clientfunc, connections[i].c_clientarg );
191 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
193 /* connections_mutex and c_mutex are locked */
194 connection_closing( &connections[i] );
195 connection_close( &connections[i] );
197 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
200 ldap_pvt_thread_mutex_unlock( &connections_mutex );
206 * Timeout idle connections.
208 int connections_timeout_idle(time_t now)
214 for( c = connection_first( &connindex );
216 c = connection_next( c, &connindex ) )
218 /* Don't timeout a slow-running request or a persistent
219 * outbound connection */
220 if( c->c_n_ops_executing ||
221 c->c_conn_state == SLAP_C_CLIENT ) continue;
223 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
225 connection_closing( c );
226 connection_close( c );
230 connection_done( c );
235 static Connection* connection_get( ber_socket_t s )
237 /* connections_mutex should be locked by caller */
241 Debug( LDAP_DEBUG_ARGS,
242 "connection_get(%ld)\n",
245 assert( connections != NULL );
247 if(s == AC_SOCKET_INVALID) {
254 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
261 for(i=0; i<dtblsize; i++) {
262 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
263 assert( connections[i].c_conn_state == SLAP_C_INVALID );
264 assert( connections[i].c_sb == 0 );
268 ber_sockbuf_ctrl( connections[i].c_sb,
269 LBER_SB_OPT_GET_FD, &sd );
271 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
272 assert( connections[i].c_conn_state == SLAP_C_INVALID );
273 assert( sd == AC_SOCKET_INVALID );
277 /* state can actually change from used -> unused by resched,
278 * so don't assert details here.
292 ldap_pvt_thread_mutex_lock( &c->c_mutex );
294 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
295 if( c->c_struct_state != SLAP_C_USED ) {
296 /* connection must have been closed due to resched */
298 assert( c->c_conn_state == SLAP_C_INVALID );
299 assert( sd == AC_SOCKET_INVALID );
301 Debug( LDAP_DEBUG_TRACE,
302 "connection_get(%d): connection not used\n",
305 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
309 Debug( LDAP_DEBUG_TRACE,
310 "connection_get(%d): got connid=%lu\n",
315 assert( c->c_struct_state == SLAP_C_USED );
316 assert( c->c_conn_state != SLAP_C_INVALID );
317 assert( sd != AC_SOCKET_INVALID );
319 #ifndef SLAPD_MONITOR
320 if ( global_idletimeout > 0 )
321 #endif /* ! SLAPD_MONITOR */
323 c->c_activitytime = slap_get_time();
330 static void connection_return( Connection *c )
332 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
335 long connection_init(
339 const char* peername,
342 struct berval *authid )
347 assert( connections != NULL );
349 assert( listener != NULL );
350 assert( dnsname != NULL );
351 assert( peername != NULL );
354 assert( flags != CONN_IS_TLS );
357 if( s == AC_SOCKET_INVALID ) {
358 Debug( LDAP_DEBUG_ANY,
359 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
365 assert( s < dtblsize );
368 ldap_pvt_thread_mutex_lock( &connections_mutex );
378 for( i=0; i < dtblsize; i++) {
381 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
382 assert( connections[i].c_sb == 0 );
387 sd = AC_SOCKET_INVALID;
388 if (connections[i].c_sb != NULL) {
389 ber_sockbuf_ctrl( connections[i].c_sb,
390 LBER_SB_OPT_GET_FD, &sd );
393 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
394 assert( sd == AC_SOCKET_INVALID );
399 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
403 assert( connections[i].c_struct_state == SLAP_C_USED );
404 assert( connections[i].c_conn_state != SLAP_C_INVALID );
405 assert( sd != AC_SOCKET_INVALID );
409 Debug( LDAP_DEBUG_ANY,
410 "connection_init(%d): connection table full "
411 "(%d/%d)\n", s, i, dtblsize);
412 ldap_pvt_thread_mutex_unlock( &connections_mutex );
420 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
421 c->c_send_ldap_result = slap_send_ldap_result;
422 c->c_send_search_entry = slap_send_search_entry;
423 c->c_send_search_reference = slap_send_search_reference;
424 c->c_send_ldap_extended = slap_send_ldap_extended;
425 #ifdef LDAP_RES_INTERMEDIATE
426 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
429 BER_BVZERO( &c->c_authmech );
430 BER_BVZERO( &c->c_dn );
431 BER_BVZERO( &c->c_ndn );
433 c->c_listener = NULL;
434 BER_BVZERO( &c->c_peer_domain );
435 BER_BVZERO( &c->c_peer_name );
437 LDAP_STAILQ_INIT(&c->c_ops);
438 LDAP_STAILQ_INIT(&c->c_pending_ops);
440 BER_BVZERO( &c->c_sasl_bind_mech );
442 c->c_sasl_authctx = NULL;
443 c->c_sasl_sockctx = NULL;
444 c->c_sasl_extra = NULL;
445 c->c_sasl_bindop = NULL;
447 c->c_sb = ber_sockbuf_alloc( );
450 ber_len_t max = sockbuf_max_incoming;
451 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
454 c->c_currentber = NULL;
456 /* should check status of thread calls */
457 ldap_pvt_thread_mutex_init( &c->c_mutex );
458 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
459 ldap_pvt_thread_cond_init( &c->c_write_cv );
462 if ( slapi_plugins_used ) {
463 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
467 c->c_struct_state = SLAP_C_UNUSED;
470 ldap_pvt_thread_mutex_lock( &c->c_mutex );
472 assert( c->c_struct_state == SLAP_C_UNUSED );
473 assert( BER_BVISNULL( &c->c_authmech ) );
474 assert( BER_BVISNULL( &c->c_dn ) );
475 assert( BER_BVISNULL( &c->c_ndn ) );
476 assert( c->c_listener == NULL );
477 assert( BER_BVISNULL( &c->c_peer_domain ) );
478 assert( BER_BVISNULL( &c->c_peer_name ) );
479 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
480 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
481 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
482 assert( c->c_sasl_done == 0 );
483 assert( c->c_sasl_authctx == NULL );
484 assert( c->c_sasl_sockctx == NULL );
485 assert( c->c_sasl_extra == NULL );
486 assert( c->c_sasl_bindop == NULL );
487 assert( c->c_currentber == NULL );
488 assert( c->c_writewaiter == 0);
490 c->c_listener = listener;
492 if ( flags == CONN_IS_CLIENT ) {
493 c->c_conn_state = SLAP_C_CLIENT;
494 c->c_struct_state = SLAP_C_USED;
495 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
496 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
497 ldap_pvt_thread_mutex_unlock( &connections_mutex );
502 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
503 ber_str2bv( peername, 0, 1, &c->c_peer_name );
505 c->c_n_ops_received = 0;
506 c->c_n_ops_executing = 0;
507 c->c_n_ops_pending = 0;
508 c->c_n_ops_completed = 0;
514 /* set to zero until bind, implies LDAP_VERSION3 */
517 #ifndef SLAPD_MONITOR
518 if ( global_idletimeout > 0 )
519 #endif /* ! SLAPD_MONITOR */
521 c->c_activitytime = c->c_starttime = slap_get_time();
524 #ifdef LDAP_CONNECTIONLESS
526 if( flags == CONN_IS_UDP ) {
529 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
530 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
532 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
533 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
534 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
535 LBER_SBIOD_LEVEL_PROVIDER, NULL );
540 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
541 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
543 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
544 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
548 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
549 INT_MAX, (void*)"ldap_" );
552 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
553 c /* non-NULL */ ) < 0 )
555 Debug( LDAP_DEBUG_ANY,
556 "connection_init(%d, %s): set nonblocking failed\n",
557 s, c->c_peer_name.bv_val, 0 );
560 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
561 id = c->c_connid = conn_nextid++;
562 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
564 c->c_conn_state = SLAP_C_INACTIVE;
565 c->c_struct_state = SLAP_C_USED;
567 c->c_ssf = c->c_transport_ssf = ssf;
571 if ( flags == CONN_IS_TLS ) {
573 c->c_needs_tls_accept = 1;
576 c->c_needs_tls_accept = 0;
580 slap_sasl_open( c, 0 );
581 slap_sasl_external( c, ssf, authid );
583 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
584 ldap_pvt_thread_mutex_unlock( &connections_mutex );
586 backend_connection_init(c);
591 void connection2anonymous( Connection *c )
593 assert( connections != NULL );
597 ber_len_t max = sockbuf_max_incoming;
598 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
601 if(c->c_authmech.bv_val != NULL ) {
602 free(c->c_authmech.bv_val);
604 BER_BVZERO( &c->c_authmech );
606 if(c->c_dn.bv_val != NULL) {
607 free(c->c_dn.bv_val);
609 BER_BVZERO( &c->c_dn );
610 if(c->c_ndn.bv_val != NULL) {
611 free(c->c_ndn.bv_val);
613 BER_BVZERO( &c->c_ndn );
614 if(c->c_sasl_authz_dn.bv_val != NULL) {
615 free(c->c_sasl_authz_dn.bv_val);
617 BER_BVZERO( &c->c_sasl_authz_dn );
619 c->c_authz_backend = NULL;
623 connection_destroy( Connection *c )
625 /* note: connections_mutex should be locked by caller */
627 unsigned long connid;
629 assert( connections != NULL );
631 assert( c->c_struct_state != SLAP_C_UNUSED );
632 assert( c->c_conn_state != SLAP_C_INVALID );
633 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
634 assert( c->c_writewaiter == 0);
636 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
637 connid = c->c_connid;
639 backend_connection_destroy(c);
644 c->c_activitytime = c->c_starttime = 0;
646 connection2anonymous( c );
647 c->c_listener = NULL;
649 if(c->c_peer_domain.bv_val != NULL) {
650 free(c->c_peer_domain.bv_val);
652 BER_BVZERO( &c->c_peer_domain );
653 if(c->c_peer_name.bv_val != NULL) {
654 free(c->c_peer_name.bv_val);
656 BER_BVZERO( &c->c_peer_name );
658 c->c_sasl_bind_in_progress = 0;
659 if(c->c_sasl_bind_mech.bv_val != NULL) {
660 free(c->c_sasl_bind_mech.bv_val);
662 BER_BVZERO( &c->c_sasl_bind_mech );
664 slap_sasl_close( c );
666 if ( c->c_currentber != NULL ) {
667 ber_free( c->c_currentber, 1 );
668 c->c_currentber = NULL;
671 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
672 if ( sd != AC_SOCKET_INVALID ) {
673 slapd_remove( sd, 1, 0 );
675 Statslog( LDAP_DEBUG_STATS,
676 "conn=%lu fd=%ld closed\n",
677 connid, (long) sd, 0, 0, 0 );
680 ber_sockbuf_free( c->c_sb );
682 c->c_sb = ber_sockbuf_alloc( );
685 ber_len_t max = sockbuf_max_incoming;
686 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
689 c->c_conn_state = SLAP_C_INVALID;
690 c->c_struct_state = SLAP_C_UNUSED;
693 /* call destructors, then constructors; avoids unnecessary allocation */
694 if ( slapi_plugins_used ) {
695 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
700 int connection_state_closing( Connection *c )
702 /* c_mutex must be locked by caller */
706 assert( c->c_struct_state == SLAP_C_USED );
708 state = c->c_conn_state;
710 assert( state != SLAP_C_INVALID );
712 return state == SLAP_C_CLOSING;
715 static void connection_abandon( Connection *c )
717 /* c_mutex must be locked by caller */
719 Operation *o, *next, op = {0};
725 op.o_connid = c->c_connid;
726 op.o_tag = LDAP_REQ_ABANDON;
727 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
728 next = LDAP_STAILQ_NEXT( o, o_next );
729 op.orn_msgid = o->o_msgid;
731 op.o_bd = frontendDB;
732 frontendDB->be_abandon( &op, &rs );
735 /* remove pending operations */
736 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
737 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
738 LDAP_STAILQ_NEXT(o, o_next) = NULL;
743 void connection_closing( Connection *c )
745 assert( connections != NULL );
747 assert( c->c_struct_state == SLAP_C_USED );
748 assert( c->c_conn_state != SLAP_C_INVALID );
750 /* c_mutex must be locked by caller */
752 if( c->c_conn_state != SLAP_C_CLOSING ) {
755 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
756 Debug( LDAP_DEBUG_TRACE,
757 "connection_closing: readying conn=%lu sd=%d for close\n",
758 c->c_connid, sd, 0 );
759 /* update state to closing */
760 c->c_conn_state = SLAP_C_CLOSING;
762 /* don't listen on this port anymore */
763 slapd_clr_read( sd, 1 );
765 /* abandon active operations */
766 connection_abandon( c );
768 /* wake write blocked operations */
769 slapd_clr_write( sd, 1 );
770 if ( c->c_writewaiter ) {
771 ldap_pvt_thread_cond_signal( &c->c_write_cv );
772 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
773 ldap_pvt_thread_yield();
774 ldap_pvt_thread_mutex_lock( &c->c_mutex );
779 static void connection_close( Connection *c )
783 assert( connections != NULL );
785 assert( c->c_struct_state == SLAP_C_USED );
786 assert( c->c_conn_state == SLAP_C_CLOSING );
788 /* note: connections_mutex and c_mutex should be locked by caller */
790 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
791 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
792 Debug( LDAP_DEBUG_TRACE,
793 "connection_close: deferring conn=%lu sd=%d\n",
794 c->c_connid, sd, 0 );
798 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
799 c->c_connid, sd, 0 );
800 connection_destroy( c );
803 unsigned long connections_nextid(void)
806 assert( connections != NULL );
808 ldap_pvt_thread_mutex_lock( &connections_mutex );
812 ldap_pvt_thread_mutex_unlock( &connections_mutex );
817 Connection* connection_first( ber_socket_t *index )
819 assert( connections != NULL );
820 assert( index != NULL );
822 ldap_pvt_thread_mutex_lock( &connections_mutex );
826 return connection_next(NULL, index);
829 Connection* connection_next( Connection *c, ber_socket_t *index )
831 assert( connections != NULL );
832 assert( index != NULL );
833 assert( *index <= dtblsize );
836 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
841 for(; *index < dtblsize; (*index)++) {
842 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
843 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
851 if( connections[*index].c_struct_state == SLAP_C_USED ) {
852 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
853 c = &connections[(*index)++];
857 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
858 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
862 ldap_pvt_thread_mutex_lock( &c->c_mutex );
868 void connection_done( Connection *c )
870 assert( connections != NULL );
873 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
876 ldap_pvt_thread_mutex_unlock( &connections_mutex );
880 * connection_activity - handle the request operation op on connection
881 * conn. This routine figures out what kind of operation it is and
882 * calls the appropriate stub to handle it.
886 /* FIXME: returns 0 in case of failure */
887 #define INCR_OP_INITIATED(index) \
889 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
890 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
891 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
893 #define INCR_OP_COMPLETED(index) \
895 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
896 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
897 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
898 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
900 #else /* !SLAPD_MONITOR */
901 #define INCR_OP_INITIATED(index)
902 #define INCR_OP_COMPLETED(index) \
904 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
905 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
906 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
908 #endif /* !SLAPD_MONITOR */
911 * NOTE: keep in sync with enum in slapd.h
913 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
928 connection_operation( void *ctx, void *arg_v )
931 Operation *op = arg_v;
932 SlapReply rs = {REP_RESULT};
933 ber_tag_t tag = op->o_tag;
935 Connection *conn = op->o_conn;
937 void *memctx_null = NULL;
940 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
941 /* FIXME: returns 0 in case of failure */
942 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
943 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
945 op->o_threadctx = ctx;
949 case LDAP_REQ_UNBIND:
951 case LDAP_REQ_DELETE:
953 case LDAP_REQ_MODIFY:
954 case LDAP_REQ_COMPARE:
955 case LDAP_REQ_SEARCH:
956 case LDAP_REQ_ABANDON:
957 case LDAP_REQ_EXTENDED:
960 Debug( LDAP_DEBUG_ANY, "connection_operation: "
961 "conn %lu unknown LDAP request 0x%lx\n",
962 conn->c_connid, tag, 0 );
963 op->o_tag = LBER_ERROR;
964 rs.sr_err = LDAP_PROTOCOL_ERROR;
965 rs.sr_text = "unknown LDAP request";
966 send_ldap_disconnect( op, &rs );
967 rc = SLAPD_DISCONNECT;
968 goto operations_error;
971 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
972 Debug( LDAP_DEBUG_ANY, "connection_operation: "
973 "error: SASL bind in progress (tag=%ld).\n",
975 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
976 "SASL bind in progress" );
977 rc = LDAP_OPERATIONS_ERROR;
978 goto operations_error;
981 /* We can use Thread-Local storage for most mallocs. We can
982 * also use TL for ber parsing, but not on Add or Modify.
985 memsiz = ber_len( op->o_ber ) * 64;
986 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
988 memsiz = SLAP_SLAB_SIZE;
990 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
991 op->o_tmpmemctx = memctx;
992 op->o_tmpmfuncs = &slap_sl_mfuncs;
993 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
994 /* Note - the ber and its buffer are already allocated from
995 * regular memory; this only affects subsequent mallocs that
996 * ber_scanf may invoke.
998 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
1003 opidx = SLAP_OP_BIND;
1006 case LDAP_REQ_UNBIND:
1007 opidx = SLAP_OP_UNBIND;
1011 opidx = SLAP_OP_ADD;
1014 case LDAP_REQ_DELETE:
1015 opidx = SLAP_OP_DELETE;
1018 case LDAP_REQ_MODRDN:
1019 opidx = SLAP_OP_MODRDN;
1022 case LDAP_REQ_MODIFY:
1023 opidx = SLAP_OP_MODIFY;
1026 case LDAP_REQ_COMPARE:
1027 opidx = SLAP_OP_COMPARE;
1030 case LDAP_REQ_SEARCH:
1031 opidx = SLAP_OP_SEARCH;
1034 case LDAP_REQ_ABANDON:
1035 opidx = SLAP_OP_ABANDON;
1038 case LDAP_REQ_EXTENDED:
1039 opidx = SLAP_OP_EXTENDED;
1047 assert( opidx > -1 );
1048 INCR_OP_INITIATED( opidx );
1049 rc = (*(opfun[opidx]))( op, &rs );
1052 if ( rc == SLAPD_DISCONNECT ) {
1055 } else if ( opidx > -1 ) {
1056 /* increment completed operations count
1057 * only if operation was initiated
1058 * and rc != SLAPD_DISCONNECT */
1059 INCR_OP_COMPLETED( opidx );
1062 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1063 if ( rc == SLAPD_ABANDON )
1064 op->o_cancel = SLAP_CANCEL_ACK;
1066 op->o_cancel = LDAP_TOO_LATE;
1068 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1069 op->o_cancel != SLAP_CANCEL_DONE )
1071 ldap_pvt_thread_yield();
1074 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1076 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1078 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1079 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1081 conn->c_n_ops_executing--;
1082 conn->c_n_ops_completed++;
1086 case LDAP_REQ_UNBIND:
1087 /* c_mutex is locked */
1088 connection_closing( conn );
1092 conn->c_sasl_bind_in_progress =
1093 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1095 if( conn->c_conn_state == SLAP_C_BINDING) {
1096 conn->c_conn_state = SLAP_C_ACTIVE;
1100 connection_resched( conn );
1101 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1105 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1107 int connection_client_setup(
1109 ldap_pvt_thread_start_t *func,
1115 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1116 CONN_IS_CLIENT, 0, NULL );
1117 if ( rc < 0 ) return -1;
1119 c = connection_get( s );
1120 c->c_clientfunc = func;
1121 c->c_clientarg = arg;
1122 connection_return( c );
1123 slapd_add_internal( s, 0 );
1124 slapd_set_read( s, 1 );
1128 void connection_client_enable(
1131 slapd_set_read( s, 1 );
1134 void connection_client_stop(
1139 /* get (locked) connection */
1140 c = connection_get( s );
1142 assert( c->c_conn_state == SLAP_C_CLIENT );
1144 c->c_listener = NULL;
1145 c->c_conn_state = SLAP_C_INVALID;
1146 c->c_struct_state = SLAP_C_UNUSED;
1147 connection_return( c );
1148 slapd_remove( s, 0, 1 );
1151 int connection_read(ber_socket_t s)
1156 assert( connections != NULL );
1158 ldap_pvt_thread_mutex_lock( &connections_mutex );
1160 /* get (locked) connection */
1161 c = connection_get( s );
1164 Debug( LDAP_DEBUG_ANY,
1165 "connection_read(%ld): no connection!\n",
1167 slapd_remove(s, 1, 0);
1169 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1175 if( c->c_conn_state == SLAP_C_CLOSING ) {
1176 Debug( LDAP_DEBUG_TRACE,
1177 "connection_read(%d): closing, ignoring input for id=%lu\n",
1178 s, c->c_connid, 0 );
1179 connection_return( c );
1180 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1184 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1185 slapd_clr_read( s, 0 );
1186 ldap_pvt_thread_pool_submit( &connection_pool,
1187 c->c_clientfunc, c->c_clientarg );
1188 connection_return( c );
1189 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1193 Debug( LDAP_DEBUG_TRACE,
1194 "connection_read(%d): checking for input on id=%lu\n",
1195 s, c->c_connid, 0 );
1198 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1199 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1201 #if 0 /* required by next #if 0 */
1206 Debug( LDAP_DEBUG_TRACE,
1207 "connection_read(%d): TLS accept error "
1208 "error=%d id=%lu, closing\n",
1209 s, rc, c->c_connid );
1210 c->c_needs_tls_accept = 0;
1211 /* connections_mutex and c_mutex are locked */
1212 connection_closing( c );
1215 /* Drain input before close, to allow SSL error codes
1216 * to propagate to client. */
1222 rc = select(s+1, &rfd, NULL, NULL, &tv);
1224 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1228 connection_close( c );
1230 } else if ( rc == 0 ) {
1232 struct berval authid = BER_BVNULL;
1234 c->c_needs_tls_accept = 0;
1236 /* we need to let SASL know */
1237 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1239 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1240 if( c->c_tls_ssf > c->c_ssf ) {
1241 c->c_ssf = c->c_tls_ssf;
1244 rc = dnX509peerNormalize( ssl, &authid );
1245 if ( rc != LDAP_SUCCESS ) {
1246 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1247 "unable to get TLS client DN, error=%d id=%lu\n",
1248 s, rc, c->c_connid );
1250 slap_sasl_external( c, c->c_tls_ssf, &authid );
1251 if ( authid.bv_val ) free( authid.bv_val );
1254 /* if success and data is ready, fall thru to data input loop */
1256 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1258 connection_return( c );
1259 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1265 #ifdef HAVE_CYRUS_SASL
1266 if ( c->c_sasl_layers ) {
1267 /* If previous layer is not removed yet, give up for now */
1268 if ( !c->c_sasl_sockctx ) {
1269 connection_return( c );
1270 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1274 c->c_sasl_layers = 0;
1276 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1278 if( rc != LDAP_SUCCESS ) {
1279 Debug( LDAP_DEBUG_TRACE,
1280 "connection_read(%d): SASL install error "
1281 "error=%d id=%lu, closing\n",
1282 s, rc, c->c_connid );
1283 /* connections_mutex and c_mutex are locked */
1284 connection_closing( c );
1285 connection_close( c );
1286 connection_return( c );
1287 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1293 #define CONNECTION_INPUT_LOOP 1
1294 /* #define DATA_READY_LOOP 1 */
1297 /* How do we do this without getting into a busy loop ? */
1298 rc = connection_input( c );
1300 #ifdef DATA_READY_LOOP
1301 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1302 #elif CONNECTION_INPUT_LOOP
1309 Debug( LDAP_DEBUG_TRACE,
1310 "connection_read(%d): input error=%d id=%lu, closing.\n",
1311 s, rc, c->c_connid );
1312 /* connections_mutex and c_mutex are locked */
1313 connection_closing( c );
1314 connection_close( c );
1315 connection_return( c );
1316 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1320 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1321 slapd_set_read( s, 1 );
1324 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1325 slapd_set_write( s, 1 );
1328 connection_return( c );
1329 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1343 #ifdef LDAP_CONNECTIONLESS
1349 if ( conn->c_currentber == NULL &&
1350 ( conn->c_currentber = ber_alloc()) == NULL )
1352 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1358 #ifdef LDAP_CONNECTIONLESS
1359 if ( conn->c_is_udp ) {
1360 char peername[sizeof("IP=255.255.255.255:65336")];
1361 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1362 sizeof(struct sockaddr));
1363 if (len != sizeof(struct sockaddr))
1365 sprintf( peername, "IP=%s:%d",
1366 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1367 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1368 Statslog( LDAP_DEBUG_STATS,
1369 "conn=%lu UDP request from %s (%s) accepted.\n",
1370 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1373 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1374 if ( tag != LDAP_TAG_MESSAGE ) {
1378 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1380 Debug( LDAP_DEBUG_TRACE,
1381 "ber_get_next on fd %d failed errno=%d (%s)\n",
1382 sd, err, sock_errstr(err) );
1383 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1384 /* log, close and send error */
1385 ber_free( conn->c_currentber, 1 );
1386 conn->c_currentber = NULL;
1393 ber = conn->c_currentber;
1394 conn->c_currentber = NULL;
1396 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1397 /* log, close and send error */
1398 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n",
1404 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1405 /* log, close and send error */
1406 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n",
1413 #ifdef LDAP_CONNECTIONLESS
1414 if( conn->c_is_udp ) {
1415 if( tag == LBER_OCTETSTRING ) {
1416 ber_get_stringa( ber, &cdn );
1417 tag = ber_peek_tag(ber, &len);
1419 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1420 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1426 if(tag == LDAP_REQ_BIND) {
1427 /* immediately abandon all existing operations upon BIND */
1428 connection_abandon( conn );
1431 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1434 /* clear state if the connection is being reused from inactive */
1435 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1436 memset( &conn->c_pagedresults_state, 0, sizeof( conn->c_pagedresults_state ) );
1439 op->o_res_ber = NULL;
1441 #ifdef LDAP_CONNECTIONLESS
1442 if (conn->c_is_udp) {
1444 ber_str2bv( cdn, 0, 1, &op->o_dn );
1445 op->o_protocol = LDAP_VERSION2;
1447 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1448 if (op->o_res_ber == NULL) return 1;
1450 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1451 sizeof(struct sockaddr), 0 );
1453 if (rc != sizeof(struct sockaddr)) {
1454 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1458 if (op->o_protocol == LDAP_VERSION2) {
1459 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1461 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1466 #endif /* LDAP_CONNECTIONLESS */
1470 /* Don't process requests when the conn is in the middle of a
1471 * Bind, or if it's closing. Also, don't let any single conn
1472 * use up all the available threads, and don't execute if we're
1473 * currently blocked on output. And don't execute if there are
1474 * already pending ops, let them go first. Abandon operations
1475 * get exceptions to some, but not all, cases.
1477 if (tag != LDAP_REQ_ABANDON && conn->c_conn_state == SLAP_C_CLOSING) {
1479 } else if (tag != LDAP_REQ_ABANDON && conn->c_writewaiter) {
1480 defer = "awaiting write";
1481 } else if (conn->c_n_ops_executing >= connection_pool_max/2) {
1482 defer = "too many executing";
1483 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1485 } else if (tag != LDAP_REQ_ABANDON && conn->c_n_ops_pending) {
1486 defer = "pending operations";
1490 int max = conn->c_dn.bv_len
1491 ? slap_conn_max_pending_auth
1492 : slap_conn_max_pending;
1494 Debug( LDAP_DEBUG_ANY,
1495 "connection_input: conn=%lu deferring operation: %s\n",
1496 conn->c_connid, defer, 0 );
1497 conn->c_n_ops_pending++;
1498 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1499 if ( conn->c_n_ops_pending > max ) {
1505 conn->c_n_ops_executing++;
1506 connection_op_activate( op );
1510 if ( conn->c_struct_state != SLAP_C_USED ) {
1511 /* connection must have got closed underneath us */
1515 assert( conn->c_struct_state == SLAP_C_USED );
1521 connection_resched( Connection *conn )
1525 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1528 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1530 /* use trylock to avoid possible deadlock */
1531 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1534 Debug( LDAP_DEBUG_TRACE,
1535 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1536 conn->c_connid, sd, 0 );
1538 * reaquire locks in the right order...
1539 * this may allow another thread to close this connection,
1540 * so recheck state below.
1542 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1543 ldap_pvt_thread_mutex_lock( &connections_mutex );
1544 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1547 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1548 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1549 "closed by other thread conn=%lu sd=%d\n",
1550 conn->c_connid, sd, 0 );
1552 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1553 "attempting closing conn=%lu sd=%d\n",
1554 conn->c_connid, sd, 0 );
1555 connection_close( conn );
1558 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1562 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1563 /* other states need different handling */
1567 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1568 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1571 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1572 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1573 /* pending operations should not be marked for abandonment */
1574 assert(!op->o_abandon);
1576 conn->c_n_ops_pending--;
1577 conn->c_n_ops_executing++;
1579 connection_op_activate( op );
1581 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1589 connection_init_log_prefix( Operation *op )
1591 if ( op->o_connid == (unsigned long)(-1) ) {
1592 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1593 "conn=-1 op=%lu", op->o_opid );
1596 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1597 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1601 static int connection_op_activate( Operation *op )
1604 ber_tag_t tag = op->o_tag;
1606 if(tag == LDAP_REQ_BIND) {
1607 op->o_conn->c_conn_state = SLAP_C_BINDING;
1610 if (!op->o_dn.bv_len) {
1611 op->o_authz = op->o_conn->c_authz;
1612 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1613 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1614 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1616 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1617 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1620 op->o_authtype = op->o_conn->c_authtype;
1621 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1623 if (!op->o_protocol) {
1624 op->o_protocol = op->o_conn->c_protocol
1625 ? op->o_conn->c_protocol : LDAP_VERSION3;
1627 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1628 && op->o_protocol > LDAP_VERSION2)
1630 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1633 op->o_connid = op->o_conn->c_connid;
1634 connection_init_log_prefix( op );
1636 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1638 status = ldap_pvt_thread_pool_submit( &connection_pool,
1639 connection_operation, (void *) op );
1641 if ( status != 0 ) {
1642 Debug( LDAP_DEBUG_ANY,
1643 "ldap_pvt_thread_pool_submit: failed (%d) for conn=%lu\n",
1644 status, op->o_connid, 0 );
1645 /* should move op to pending list */
1651 int connection_write(ber_socket_t s)
1656 assert( connections != NULL );
1658 ldap_pvt_thread_mutex_lock( &connections_mutex );
1660 c = connection_get( s );
1663 Debug( LDAP_DEBUG_ANY,
1664 "connection_write(%ld): no connection!\n",
1666 slapd_remove(s, 1, 0);
1667 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1671 slapd_clr_write( s, 0);
1675 Debug( LDAP_DEBUG_TRACE,
1676 "connection_write(%d): waking output for id=%lu\n",
1677 s, c->c_connid, 0 );
1678 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1680 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1681 slapd_set_read( s, 1 );
1683 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1684 slapd_set_write( s, 1 );
1686 /* If there are ops pending because of a writewaiter, start
1689 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1690 if ( !c->c_writewaiter ) break;
1691 if ( c->c_n_ops_executing > connection_pool_max/2 ) {
1694 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1695 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1696 /* pending operations should not be marked for abandonment */
1697 assert(!op->o_abandon);
1699 c->c_n_ops_pending--;
1700 c->c_n_ops_executing++;
1702 connection_op_activate( op );
1706 connection_return( c );
1707 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1712 connection_fake_init(
1717 conn->c_connid = -1;
1718 conn->c_send_ldap_result = slap_send_ldap_result;
1719 conn->c_send_search_entry = slap_send_search_entry;
1720 conn->c_send_search_reference = slap_send_search_reference;
1721 conn->c_listener = (Listener *)&dummy_list;
1722 conn->c_peer_domain = slap_empty_bv;
1723 conn->c_peer_name = slap_empty_bv;
1725 memset(op, 0, OPERATION_BUFFER_SIZE);
1726 op->o_hdr = (Opheader *)(op+1);
1727 op->o_controls = (void **)(op->o_hdr+1);
1728 /* set memory context */
1729 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
1730 op->o_tmpmfuncs = &slap_sl_mfuncs;
1731 op->o_threadctx = ctx;
1734 op->o_connid = op->o_conn->c_connid;
1735 connection_init_log_prefix( op );
1737 op->o_time = slap_get_time();
1741 connection_assign_nextid( Connection *conn )
1743 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1744 conn->c_connid = conn_nextid++;
1745 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );