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 ldap_pvt_thread_cond_signal( &c->c_write_cv );
774 static void connection_close( Connection *c )
778 assert( connections != NULL );
780 assert( c->c_struct_state == SLAP_C_USED );
781 assert( c->c_conn_state == SLAP_C_CLOSING );
783 /* note: connections_mutex and c_mutex should be locked by caller */
785 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
786 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
787 Debug( LDAP_DEBUG_TRACE,
788 "connection_close: deferring conn=%lu sd=%d\n",
789 c->c_connid, sd, 0 );
793 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
794 c->c_connid, sd, 0 );
795 connection_destroy( c );
798 unsigned long connections_nextid(void)
801 assert( connections != NULL );
803 ldap_pvt_thread_mutex_lock( &connections_mutex );
807 ldap_pvt_thread_mutex_unlock( &connections_mutex );
812 Connection* connection_first( ber_socket_t *index )
814 assert( connections != NULL );
815 assert( index != NULL );
817 ldap_pvt_thread_mutex_lock( &connections_mutex );
821 return connection_next(NULL, index);
824 Connection* connection_next( Connection *c, ber_socket_t *index )
826 assert( connections != NULL );
827 assert( index != NULL );
828 assert( *index <= dtblsize );
831 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
836 for(; *index < dtblsize; (*index)++) {
837 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
838 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
846 if( connections[*index].c_struct_state == SLAP_C_USED ) {
847 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
848 c = &connections[(*index)++];
852 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
853 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
857 ldap_pvt_thread_mutex_lock( &c->c_mutex );
863 void connection_done( Connection *c )
865 assert( connections != NULL );
868 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
871 ldap_pvt_thread_mutex_unlock( &connections_mutex );
875 * connection_activity - handle the request operation op on connection
876 * conn. This routine figures out what kind of operation it is and
877 * calls the appropriate stub to handle it.
881 /* FIXME: returns 0 in case of failure */
882 #define INCR_OP_INITIATED(index) \
884 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
885 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
886 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
888 #define INCR_OP_COMPLETED(index) \
890 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
891 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
892 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
893 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
895 #else /* !SLAPD_MONITOR */
896 #define INCR_OP_INITIATED(index)
897 #define INCR_OP_COMPLETED(index) \
899 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
900 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
901 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
903 #endif /* !SLAPD_MONITOR */
906 * NOTE: keep in sync with enum in slapd.h
908 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
923 connection_operation( void *ctx, void *arg_v )
926 Operation *op = arg_v;
927 SlapReply rs = {REP_RESULT};
928 ber_tag_t tag = op->o_tag;
930 Connection *conn = op->o_conn;
932 void *memctx_null = NULL;
935 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
936 /* FIXME: returns 0 in case of failure */
937 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
938 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
940 op->o_threadctx = ctx;
944 case LDAP_REQ_UNBIND:
946 case LDAP_REQ_DELETE:
948 case LDAP_REQ_MODIFY:
949 case LDAP_REQ_COMPARE:
950 case LDAP_REQ_SEARCH:
951 case LDAP_REQ_ABANDON:
952 case LDAP_REQ_EXTENDED:
955 Debug( LDAP_DEBUG_ANY, "connection_operation: "
956 "conn %lu unknown LDAP request 0x%lx\n",
957 conn->c_connid, tag, 0 );
958 op->o_tag = LBER_ERROR;
959 rs.sr_err = LDAP_PROTOCOL_ERROR;
960 rs.sr_text = "unknown LDAP request";
961 send_ldap_disconnect( op, &rs );
962 rc = SLAPD_DISCONNECT;
963 goto operations_error;
966 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
967 Debug( LDAP_DEBUG_ANY, "connection_operation: "
968 "error: SASL bind in progress (tag=%ld).\n",
970 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
971 "SASL bind in progress" );
972 rc = LDAP_OPERATIONS_ERROR;
973 goto operations_error;
976 /* We can use Thread-Local storage for most mallocs. We can
977 * also use TL for ber parsing, but not on Add or Modify.
980 memsiz = ber_len( op->o_ber ) * 64;
981 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
983 memsiz = SLAP_SLAB_SIZE;
985 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
986 op->o_tmpmemctx = memctx;
987 op->o_tmpmfuncs = &slap_sl_mfuncs;
988 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
989 /* Note - the ber and its buffer are already allocated from
990 * regular memory; this only affects subsequent mallocs that
991 * ber_scanf may invoke.
993 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
998 opidx = SLAP_OP_BIND;
1001 case LDAP_REQ_UNBIND:
1002 opidx = SLAP_OP_UNBIND;
1006 opidx = SLAP_OP_ADD;
1009 case LDAP_REQ_DELETE:
1010 opidx = SLAP_OP_DELETE;
1013 case LDAP_REQ_MODRDN:
1014 opidx = SLAP_OP_MODRDN;
1017 case LDAP_REQ_MODIFY:
1018 opidx = SLAP_OP_MODIFY;
1021 case LDAP_REQ_COMPARE:
1022 opidx = SLAP_OP_COMPARE;
1025 case LDAP_REQ_SEARCH:
1026 opidx = SLAP_OP_SEARCH;
1029 case LDAP_REQ_ABANDON:
1030 opidx = SLAP_OP_ABANDON;
1033 case LDAP_REQ_EXTENDED:
1034 opidx = SLAP_OP_EXTENDED;
1042 assert( opidx > -1 );
1043 INCR_OP_INITIATED( opidx );
1044 rc = (*(opfun[opidx]))( op, &rs );
1047 if ( rc == SLAPD_DISCONNECT ) {
1050 } else if ( opidx > -1 ) {
1051 /* increment completed operations count
1052 * only if operation was initiated
1053 * and rc != SLAPD_DISCONNECT */
1054 INCR_OP_COMPLETED( opidx );
1057 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1058 if ( rc == SLAPD_ABANDON )
1059 op->o_cancel = SLAP_CANCEL_ACK;
1061 op->o_cancel = LDAP_TOO_LATE;
1063 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1064 op->o_cancel != SLAP_CANCEL_DONE )
1066 ldap_pvt_thread_yield();
1069 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1071 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1073 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1074 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1076 conn->c_n_ops_executing--;
1077 conn->c_n_ops_completed++;
1081 case LDAP_REQ_UNBIND:
1082 /* c_mutex is locked */
1083 connection_closing( conn );
1087 conn->c_sasl_bind_in_progress =
1088 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1090 if( conn->c_conn_state == SLAP_C_BINDING) {
1091 conn->c_conn_state = SLAP_C_ACTIVE;
1095 connection_resched( conn );
1096 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1100 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1102 int connection_client_setup(
1104 ldap_pvt_thread_start_t *func,
1110 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1111 CONN_IS_CLIENT, 0, NULL );
1112 if ( rc < 0 ) return -1;
1114 c = connection_get( s );
1115 c->c_clientfunc = func;
1116 c->c_clientarg = arg;
1117 connection_return( c );
1118 slapd_add_internal( s, 0 );
1119 slapd_set_read( s, 1 );
1123 void connection_client_enable(
1126 slapd_set_read( s, 1 );
1129 void connection_client_stop(
1134 /* get (locked) connection */
1135 c = connection_get( s );
1137 assert( c->c_conn_state == SLAP_C_CLIENT );
1139 c->c_listener = NULL;
1140 c->c_conn_state = SLAP_C_INVALID;
1141 c->c_struct_state = SLAP_C_UNUSED;
1142 connection_return( c );
1143 slapd_remove( s, 0, 1 );
1146 int connection_read(ber_socket_t s)
1151 assert( connections != NULL );
1153 ldap_pvt_thread_mutex_lock( &connections_mutex );
1155 /* get (locked) connection */
1156 c = connection_get( s );
1159 Debug( LDAP_DEBUG_ANY,
1160 "connection_read(%ld): no connection!\n",
1162 slapd_remove(s, 1, 0);
1164 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1170 if( c->c_conn_state == SLAP_C_CLOSING ) {
1171 Debug( LDAP_DEBUG_TRACE,
1172 "connection_read(%d): closing, ignoring input for id=%lu\n",
1173 s, c->c_connid, 0 );
1174 connection_return( c );
1175 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1179 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1180 slapd_clr_read( s, 0 );
1181 ldap_pvt_thread_pool_submit( &connection_pool,
1182 c->c_clientfunc, c->c_clientarg );
1183 connection_return( c );
1184 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1188 Debug( LDAP_DEBUG_TRACE,
1189 "connection_read(%d): checking for input on id=%lu\n",
1190 s, c->c_connid, 0 );
1193 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1194 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1196 #if 0 /* required by next #if 0 */
1201 Debug( LDAP_DEBUG_TRACE,
1202 "connection_read(%d): TLS accept error "
1203 "error=%d id=%lu, closing\n",
1204 s, rc, c->c_connid );
1205 c->c_needs_tls_accept = 0;
1206 /* connections_mutex and c_mutex are locked */
1207 connection_closing( c );
1210 /* Drain input before close, to allow SSL error codes
1211 * to propagate to client. */
1217 rc = select(s+1, &rfd, NULL, NULL, &tv);
1219 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1223 connection_close( c );
1225 } else if ( rc == 0 ) {
1227 struct berval authid = BER_BVNULL;
1229 c->c_needs_tls_accept = 0;
1231 /* we need to let SASL know */
1232 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1234 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1235 if( c->c_tls_ssf > c->c_ssf ) {
1236 c->c_ssf = c->c_tls_ssf;
1239 rc = dnX509peerNormalize( ssl, &authid );
1240 if ( rc != LDAP_SUCCESS ) {
1241 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1242 "unable to get TLS client DN, error=%d id=%lu\n",
1243 s, rc, c->c_connid );
1245 slap_sasl_external( c, c->c_tls_ssf, &authid );
1246 if ( authid.bv_val ) free( authid.bv_val );
1249 /* if success and data is ready, fall thru to data input loop */
1251 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1253 connection_return( c );
1254 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1260 #ifdef HAVE_CYRUS_SASL
1261 if ( c->c_sasl_layers ) {
1262 /* If previous layer is not removed yet, give up for now */
1263 if ( !c->c_sasl_sockctx ) {
1264 connection_return( c );
1265 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1269 c->c_sasl_layers = 0;
1271 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1273 if( rc != LDAP_SUCCESS ) {
1274 Debug( LDAP_DEBUG_TRACE,
1275 "connection_read(%d): SASL install error "
1276 "error=%d id=%lu, closing\n",
1277 s, rc, c->c_connid );
1278 /* connections_mutex and c_mutex are locked */
1279 connection_closing( c );
1280 connection_close( c );
1281 connection_return( c );
1282 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1288 #define CONNECTION_INPUT_LOOP 1
1289 /* #define DATA_READY_LOOP 1 */
1292 /* How do we do this without getting into a busy loop ? */
1293 rc = connection_input( c );
1295 #ifdef DATA_READY_LOOP
1296 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1297 #elif CONNECTION_INPUT_LOOP
1304 Debug( LDAP_DEBUG_TRACE,
1305 "connection_read(%d): input error=%d id=%lu, closing.\n",
1306 s, rc, c->c_connid );
1307 /* connections_mutex and c_mutex are locked */
1308 connection_closing( c );
1309 connection_close( c );
1310 connection_return( c );
1311 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1315 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1316 slapd_set_read( s, 1 );
1319 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1320 slapd_set_write( s, 1 );
1323 connection_return( c );
1324 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1338 #ifdef LDAP_CONNECTIONLESS
1344 if ( conn->c_currentber == NULL &&
1345 ( conn->c_currentber = ber_alloc()) == NULL )
1347 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1353 #ifdef LDAP_CONNECTIONLESS
1354 if ( conn->c_is_udp ) {
1355 char peername[sizeof("IP=255.255.255.255:65336")];
1356 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1357 sizeof(struct sockaddr));
1358 if (len != sizeof(struct sockaddr))
1360 sprintf( peername, "IP=%s:%d",
1361 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1362 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1363 Statslog( LDAP_DEBUG_STATS,
1364 "conn=%lu UDP request from %s (%s) accepted.\n",
1365 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1368 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1369 if ( tag != LDAP_TAG_MESSAGE ) {
1373 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1375 Debug( LDAP_DEBUG_TRACE,
1376 "ber_get_next on fd %d failed errno=%d (%s)\n",
1377 sd, err, sock_errstr(err) );
1378 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1379 /* log, close and send error */
1380 ber_free( conn->c_currentber, 1 );
1381 conn->c_currentber = NULL;
1388 ber = conn->c_currentber;
1389 conn->c_currentber = NULL;
1391 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1392 /* log, close and send error */
1393 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n",
1399 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1400 /* log, close and send error */
1401 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n",
1408 #ifdef LDAP_CONNECTIONLESS
1409 if( conn->c_is_udp ) {
1410 if( tag == LBER_OCTETSTRING ) {
1411 ber_get_stringa( ber, &cdn );
1412 tag = ber_peek_tag(ber, &len);
1414 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1415 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1421 if(tag == LDAP_REQ_BIND) {
1422 /* immediately abandon all existing operations upon BIND */
1423 connection_abandon( conn );
1426 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1429 /* clear state if the connection is being reused from inactive */
1430 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1431 memset( &conn->c_pagedresults_state, 0, sizeof( conn->c_pagedresults_state ) );
1434 op->o_res_ber = NULL;
1436 #ifdef LDAP_CONNECTIONLESS
1437 if (conn->c_is_udp) {
1439 ber_str2bv( cdn, 0, 1, &op->o_dn );
1440 op->o_protocol = LDAP_VERSION2;
1442 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1443 if (op->o_res_ber == NULL) return 1;
1445 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1446 sizeof(struct sockaddr), 0 );
1448 if (rc != sizeof(struct sockaddr)) {
1449 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1453 if (op->o_protocol == LDAP_VERSION2) {
1454 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1456 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1461 #endif /* LDAP_CONNECTIONLESS */
1465 /* Don't process requests when the conn is in the middle of a
1466 * Bind, or if it's closing. Also, don't let any single conn
1467 * use up all the available threads, and don't execute if we're
1468 * currently blocked on output. And don't execute if there are
1469 * already pending ops, let them go first. Abandon operations
1470 * get exceptions to some, but not all, cases.
1472 if (tag != LDAP_REQ_ABANDON && conn->c_conn_state == SLAP_C_CLOSING) {
1474 } else if (tag != LDAP_REQ_ABANDON && conn->c_writewaiter) {
1475 defer = "awaiting write";
1476 } else if (conn->c_n_ops_executing >= connection_pool_max/2) {
1477 defer = "too many executing";
1478 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1480 } else if (tag != LDAP_REQ_ABANDON && conn->c_n_ops_pending) {
1481 defer = "pending operations";
1485 int max = conn->c_dn.bv_len
1486 ? slap_conn_max_pending_auth
1487 : slap_conn_max_pending;
1489 Debug( LDAP_DEBUG_ANY,
1490 "connection_input: conn=%lu deferring operation: %s\n",
1491 conn->c_connid, defer, 0 );
1492 conn->c_n_ops_pending++;
1493 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1494 if ( conn->c_n_ops_pending > max ) {
1500 conn->c_n_ops_executing++;
1501 connection_op_activate( op );
1505 if ( conn->c_struct_state != SLAP_C_USED ) {
1506 /* connection must have got closed underneath us */
1510 assert( conn->c_struct_state == SLAP_C_USED );
1516 connection_resched( Connection *conn )
1520 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1523 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1525 /* use trylock to avoid possible deadlock */
1526 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1529 Debug( LDAP_DEBUG_TRACE,
1530 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1531 conn->c_connid, sd, 0 );
1533 * reaquire locks in the right order...
1534 * this may allow another thread to close this connection,
1535 * so recheck state below.
1537 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1538 ldap_pvt_thread_mutex_lock( &connections_mutex );
1539 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1542 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1543 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1544 "closed by other thread conn=%lu sd=%d\n",
1545 conn->c_connid, sd, 0 );
1547 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1548 "attempting closing conn=%lu sd=%d\n",
1549 conn->c_connid, sd, 0 );
1550 connection_close( conn );
1553 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1557 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1558 /* other states need different handling */
1562 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1563 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1566 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1567 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1568 /* pending operations should not be marked for abandonment */
1569 assert(!op->o_abandon);
1571 conn->c_n_ops_pending--;
1572 conn->c_n_ops_executing++;
1574 connection_op_activate( op );
1576 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1584 connection_init_log_prefix( Operation *op )
1586 if ( op->o_connid == (unsigned long)(-1) ) {
1587 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1588 "conn=-1 op=%lu", op->o_opid );
1591 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1592 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1596 static int connection_op_activate( Operation *op )
1599 ber_tag_t tag = op->o_tag;
1601 if(tag == LDAP_REQ_BIND) {
1602 op->o_conn->c_conn_state = SLAP_C_BINDING;
1605 if (!op->o_dn.bv_len) {
1606 op->o_authz = op->o_conn->c_authz;
1607 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1608 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1609 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1611 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1612 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1615 op->o_authtype = op->o_conn->c_authtype;
1616 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1618 if (!op->o_protocol) {
1619 op->o_protocol = op->o_conn->c_protocol
1620 ? op->o_conn->c_protocol : LDAP_VERSION3;
1622 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1623 && op->o_protocol > LDAP_VERSION2)
1625 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1628 op->o_connid = op->o_conn->c_connid;
1629 connection_init_log_prefix( op );
1631 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1633 status = ldap_pvt_thread_pool_submit( &connection_pool,
1634 connection_operation, (void *) op );
1636 if ( status != 0 ) {
1637 Debug( LDAP_DEBUG_ANY,
1638 "ldap_pvt_thread_pool_submit: failed (%d) for conn=%lu\n",
1639 status, op->o_connid, 0 );
1640 /* should move op to pending list */
1646 int connection_write(ber_socket_t s)
1651 assert( connections != NULL );
1653 ldap_pvt_thread_mutex_lock( &connections_mutex );
1655 c = connection_get( s );
1658 Debug( LDAP_DEBUG_ANY,
1659 "connection_write(%ld): no connection!\n",
1661 slapd_remove(s, 1, 0);
1662 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1666 slapd_clr_write( s, 0);
1670 Debug( LDAP_DEBUG_TRACE,
1671 "connection_write(%d): waking output for id=%lu\n",
1672 s, c->c_connid, 0 );
1673 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1675 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1676 slapd_set_read( s, 1 );
1678 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1679 slapd_set_write( s, 1 );
1681 /* If there are ops pending because of a writewaiter, start
1684 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1685 if ( !c->c_writewaiter ) break;
1686 if ( c->c_n_ops_executing > connection_pool_max/2 ) {
1689 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1690 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1691 /* pending operations should not be marked for abandonment */
1692 assert(!op->o_abandon);
1694 c->c_n_ops_pending--;
1695 c->c_n_ops_executing++;
1697 connection_op_activate( op );
1701 connection_return( c );
1702 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1707 connection_fake_init(
1712 conn->c_connid = -1;
1713 conn->c_send_ldap_result = slap_send_ldap_result;
1714 conn->c_send_search_entry = slap_send_search_entry;
1715 conn->c_send_search_reference = slap_send_search_reference;
1716 conn->c_listener = (Listener *)&dummy_list;
1717 conn->c_peer_domain = slap_empty_bv;
1718 conn->c_peer_name = slap_empty_bv;
1720 memset(op, 0, OPERATION_BUFFER_SIZE);
1721 op->o_hdr = (Opheader *)(op+1);
1722 op->o_controls = (void **)(op->o_hdr+1);
1723 /* set memory context */
1724 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
1725 op->o_tmpmfuncs = &slap_sl_mfuncs;
1726 op->o_threadctx = ctx;
1729 op->o_connid = op->o_conn->c_connid;
1730 connection_init_log_prefix( op );
1732 op->o_time = slap_get_time();
1736 connection_assign_nextid( Connection *conn )
1738 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1739 conn->c_connid = conn_nextid++;
1740 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );