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.
31 #include <ac/socket.h>
33 #include <ac/string.h>
35 #include <ac/unistd.h>
41 #include "slapi/slapi.h"
44 /* protected by connections_mutex */
45 static ldap_pvt_thread_mutex_t connections_mutex;
46 static Connection *connections = NULL;
48 static ldap_pvt_thread_mutex_t conn_nextid_mutex;
49 static unsigned long conn_nextid = 0;
51 /* structure state (protected by connections_mutex) */
52 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
53 #define SLAP_C_UNUSED 0x01
54 #define SLAP_C_USED 0x02
56 /* connection state (protected by c_mutex ) */
57 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
58 #define SLAP_C_INACTIVE 0x01 /* zero threads */
59 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
60 #define SLAP_C_BINDING 0x03 /* binding */
61 #define SLAP_C_CLOSING 0x04 /* closing */
62 #define SLAP_C_CLIENT 0x05 /* outbound client conn */
65 connection_state2str( int state )
68 case SLAP_C_INVALID: return "!";
69 case SLAP_C_INACTIVE: return "|";
70 case SLAP_C_ACTIVE: return "";
71 case SLAP_C_BINDING: return "B";
72 case SLAP_C_CLOSING: return "C";
73 case SLAP_C_CLIENT: return "L";
79 static Connection* connection_get( ber_socket_t s );
81 static int connection_input( Connection *c );
82 static void connection_close( Connection *c );
84 static int connection_op_activate( Operation *op );
85 static int connection_resched( Connection *conn );
86 static void connection_abandon( Connection *conn );
87 static void connection_destroy( Connection *c );
89 static ldap_pvt_thread_start_t connection_operation;
92 * Initialize connection management infrastructure.
94 int connections_init(void)
98 assert( connections == NULL );
100 if( connections != NULL) {
101 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
106 /* should check return of every call */
107 ldap_pvt_thread_mutex_init( &connections_mutex );
108 ldap_pvt_thread_mutex_init( &conn_nextid_mutex );
110 connections = (Connection *) ch_calloc( dtblsize, sizeof(Connection) );
112 if( connections == NULL ) {
113 Debug( LDAP_DEBUG_ANY,
114 "connections_init: allocation (%d*%ld) of connection array failed\n",
115 dtblsize, (long) sizeof(Connection), 0 );
119 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
120 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
122 for (i=0; i<dtblsize; i++) connections[i].c_conn_idx = i;
125 * per entry initialization of the Connection array initialization
126 * will be done by connection_init()
133 * Destroy connection management infrastructure.
135 int connections_destroy(void)
139 /* should check return of every call */
141 if( connections == NULL) {
142 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
147 for ( i = 0; i < dtblsize; i++ ) {
148 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
149 ber_sockbuf_free( connections[i].c_sb );
150 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
151 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
152 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
154 if ( slapi_plugins_used ) {
155 slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, &connections[i] );
164 ldap_pvt_thread_mutex_destroy( &connections_mutex );
165 ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex );
170 * shutdown all connections
172 int connections_shutdown(void)
176 ldap_pvt_thread_mutex_lock( &connections_mutex );
178 for ( i = 0; i < dtblsize; i++ ) {
179 if( connections[i].c_struct_state != SLAP_C_USED ) {
182 /* give persistent clients a chance to cleanup */
183 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
184 ldap_pvt_thread_pool_submit( &connection_pool,
185 connections[i].c_clientfunc, connections[i].c_clientarg );
189 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
191 /* connections_mutex and c_mutex are locked */
192 connection_closing( &connections[i] );
193 connection_close( &connections[i] );
195 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
198 ldap_pvt_thread_mutex_unlock( &connections_mutex );
204 * Timeout idle connections.
206 int connections_timeout_idle(time_t now)
212 for( c = connection_first( &connindex );
214 c = connection_next( c, &connindex ) )
216 /* Don't timeout a slow-running request or a persistent
217 * outbound connection */
218 if( c->c_n_ops_executing ||
219 c->c_conn_state == SLAP_C_CLIENT ) continue;
221 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
223 connection_closing( c );
224 connection_close( c );
228 connection_done( c );
233 static Connection* connection_get( ber_socket_t s )
235 /* connections_mutex should be locked by caller */
239 Debug( LDAP_DEBUG_ARGS,
240 "connection_get(%ld)\n",
243 assert( connections != NULL );
245 if(s == AC_SOCKET_INVALID) {
252 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
259 for(i=0; i<dtblsize; i++) {
260 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
261 assert( connections[i].c_conn_state == SLAP_C_INVALID );
262 assert( connections[i].c_sb == 0 );
266 ber_sockbuf_ctrl( connections[i].c_sb,
267 LBER_SB_OPT_GET_FD, &sd );
269 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
270 assert( connections[i].c_conn_state == SLAP_C_INVALID );
271 assert( sd == AC_SOCKET_INVALID );
275 /* state can actually change from used -> unused by resched,
276 * so don't assert details here.
290 ldap_pvt_thread_mutex_lock( &c->c_mutex );
292 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
293 if( c->c_struct_state != SLAP_C_USED ) {
294 /* connection must have been closed due to resched */
296 assert( c->c_conn_state == SLAP_C_INVALID );
297 assert( sd == AC_SOCKET_INVALID );
299 Debug( LDAP_DEBUG_TRACE,
300 "connection_get(%d): connection not used\n",
303 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
307 Debug( LDAP_DEBUG_TRACE,
308 "connection_get(%d): got connid=%lu\n",
313 assert( c->c_struct_state == SLAP_C_USED );
314 assert( c->c_conn_state != SLAP_C_INVALID );
315 assert( sd != AC_SOCKET_INVALID );
317 #ifndef SLAPD_MONITOR
318 if ( global_idletimeout > 0 )
319 #endif /* ! SLAPD_MONITOR */
321 c->c_activitytime = slap_get_time();
328 static void connection_return( Connection *c )
330 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
333 long connection_init(
337 const char* peername,
340 struct berval *authid )
345 assert( connections != NULL );
347 assert( listener != NULL );
348 assert( dnsname != NULL );
349 assert( peername != NULL );
352 assert( flags != CONN_IS_TLS );
355 if( s == AC_SOCKET_INVALID ) {
356 Debug( LDAP_DEBUG_ANY,
357 "connection_init: init of socket %ld invalid.\n", (long)s, 0, 0 );
363 assert( s < dtblsize );
366 ldap_pvt_thread_mutex_lock( &connections_mutex );
376 for( i=0; i < dtblsize; i++) {
379 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
380 assert( connections[i].c_sb == 0 );
385 sd = AC_SOCKET_INVALID;
386 if (connections[i].c_sb != NULL) {
387 ber_sockbuf_ctrl( connections[i].c_sb,
388 LBER_SB_OPT_GET_FD, &sd );
391 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
392 assert( sd == AC_SOCKET_INVALID );
397 if( connections[i].c_conn_state == SLAP_C_CLIENT ) {
401 assert( connections[i].c_struct_state == SLAP_C_USED );
402 assert( connections[i].c_conn_state != SLAP_C_INVALID );
403 assert( sd != AC_SOCKET_INVALID );
407 Debug( LDAP_DEBUG_ANY,
408 "connection_init(%d): connection table full "
409 "(%d/%d)\n", s, i, dtblsize);
410 ldap_pvt_thread_mutex_unlock( &connections_mutex );
418 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
419 c->c_send_ldap_result = slap_send_ldap_result;
420 c->c_send_search_entry = slap_send_search_entry;
421 c->c_send_search_reference = slap_send_search_reference;
422 c->c_send_ldap_extended = slap_send_ldap_extended;
423 #ifdef LDAP_RES_INTERMEDIATE
424 c->c_send_ldap_intermediate = slap_send_ldap_intermediate;
427 BER_BVZERO( &c->c_authmech );
428 BER_BVZERO( &c->c_dn );
429 BER_BVZERO( &c->c_ndn );
431 c->c_listener = NULL;
432 BER_BVZERO( &c->c_peer_domain );
433 BER_BVZERO( &c->c_peer_name );
435 LDAP_STAILQ_INIT(&c->c_ops);
436 LDAP_STAILQ_INIT(&c->c_pending_ops);
438 BER_BVZERO( &c->c_sasl_bind_mech );
440 c->c_sasl_authctx = NULL;
441 c->c_sasl_sockctx = NULL;
442 c->c_sasl_extra = NULL;
443 c->c_sasl_bindop = NULL;
445 c->c_sb = ber_sockbuf_alloc( );
448 ber_len_t max = sockbuf_max_incoming;
449 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
452 c->c_currentber = NULL;
454 /* should check status of thread calls */
455 ldap_pvt_thread_mutex_init( &c->c_mutex );
456 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
457 ldap_pvt_thread_cond_init( &c->c_write_cv );
460 if ( slapi_plugins_used ) {
461 slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c );
465 c->c_struct_state = SLAP_C_UNUSED;
468 ldap_pvt_thread_mutex_lock( &c->c_mutex );
470 assert( c->c_struct_state == SLAP_C_UNUSED );
471 assert( BER_BVISNULL( &c->c_authmech ) );
472 assert( BER_BVISNULL( &c->c_dn ) );
473 assert( BER_BVISNULL( &c->c_ndn ) );
474 assert( c->c_listener == NULL );
475 assert( BER_BVISNULL( &c->c_peer_domain ) );
476 assert( BER_BVISNULL( &c->c_peer_name ) );
477 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
478 assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) );
479 assert( BER_BVISNULL( &c->c_sasl_bind_mech ) );
480 assert( c->c_sasl_done == 0 );
481 assert( c->c_sasl_authctx == NULL );
482 assert( c->c_sasl_sockctx == NULL );
483 assert( c->c_sasl_extra == NULL );
484 assert( c->c_sasl_bindop == NULL );
485 assert( c->c_currentber == NULL );
486 assert( c->c_writewaiter == 0);
488 c->c_listener = listener;
490 if ( flags == CONN_IS_CLIENT ) {
491 c->c_conn_state = SLAP_C_CLIENT;
492 c->c_struct_state = SLAP_C_USED;
493 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
494 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
495 ldap_pvt_thread_mutex_unlock( &connections_mutex );
500 ber_str2bv( dnsname, 0, 1, &c->c_peer_domain );
501 ber_str2bv( peername, 0, 1, &c->c_peer_name );
503 c->c_n_ops_received = 0;
504 c->c_n_ops_executing = 0;
505 c->c_n_ops_pending = 0;
506 c->c_n_ops_completed = 0;
512 /* set to zero until bind, implies LDAP_VERSION3 */
515 #ifndef SLAPD_MONITOR
516 if ( global_idletimeout > 0 )
517 #endif /* ! SLAPD_MONITOR */
519 c->c_activitytime = c->c_starttime = slap_get_time();
522 #ifdef LDAP_CONNECTIONLESS
524 if( flags == CONN_IS_UDP ) {
527 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
528 LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
530 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
531 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
532 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
533 LBER_SBIOD_LEVEL_PROVIDER, NULL );
538 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
539 LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
541 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
542 LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
546 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug,
547 INT_MAX, (void*)"ldap_" );
550 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK,
551 c /* non-NULL */ ) < 0 )
553 Debug( LDAP_DEBUG_ANY,
554 "connection_init(%d, %s): set nonblocking failed\n",
555 s, c->c_peer_name.bv_val, 0 );
558 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
559 id = c->c_connid = conn_nextid++;
560 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );
562 c->c_conn_state = SLAP_C_INACTIVE;
563 c->c_struct_state = SLAP_C_USED;
565 c->c_ssf = c->c_transport_ssf = ssf;
569 if ( flags == CONN_IS_TLS ) {
571 c->c_needs_tls_accept = 1;
574 c->c_needs_tls_accept = 0;
578 slap_sasl_open( c, 0 );
579 slap_sasl_external( c, ssf, authid );
581 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
582 ldap_pvt_thread_mutex_unlock( &connections_mutex );
584 backend_connection_init(c);
589 void connection2anonymous( Connection *c )
591 assert( connections != NULL );
595 ber_len_t max = sockbuf_max_incoming;
596 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
599 if(c->c_authmech.bv_val != NULL ) {
600 free(c->c_authmech.bv_val);
602 BER_BVZERO( &c->c_authmech );
604 if(c->c_dn.bv_val != NULL) {
605 free(c->c_dn.bv_val);
607 BER_BVZERO( &c->c_dn );
608 if(c->c_ndn.bv_val != NULL) {
609 free(c->c_ndn.bv_val);
611 BER_BVZERO( &c->c_ndn );
612 if(c->c_sasl_authz_dn.bv_val != NULL) {
613 free(c->c_sasl_authz_dn.bv_val);
615 BER_BVZERO( &c->c_sasl_authz_dn );
617 c->c_authz_backend = NULL;
621 connection_destroy( Connection *c )
623 /* note: connections_mutex should be locked by caller */
625 unsigned long connid;
627 assert( connections != NULL );
629 assert( c->c_struct_state != SLAP_C_UNUSED );
630 assert( c->c_conn_state != SLAP_C_INVALID );
631 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
632 assert( c->c_writewaiter == 0);
634 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
635 connid = c->c_connid;
637 backend_connection_destroy(c);
642 c->c_activitytime = c->c_starttime = 0;
644 connection2anonymous( c );
645 c->c_listener = NULL;
647 if(c->c_peer_domain.bv_val != NULL) {
648 free(c->c_peer_domain.bv_val);
650 BER_BVZERO( &c->c_peer_domain );
651 if(c->c_peer_name.bv_val != NULL) {
652 free(c->c_peer_name.bv_val);
654 BER_BVZERO( &c->c_peer_name );
656 c->c_sasl_bind_in_progress = 0;
657 if(c->c_sasl_bind_mech.bv_val != NULL) {
658 free(c->c_sasl_bind_mech.bv_val);
660 BER_BVZERO( &c->c_sasl_bind_mech );
662 slap_sasl_close( c );
664 if ( c->c_currentber != NULL ) {
665 ber_free( c->c_currentber, 1 );
666 c->c_currentber = NULL;
669 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
670 if ( sd != AC_SOCKET_INVALID ) {
671 slapd_remove( sd, 1, 0 );
673 Statslog( LDAP_DEBUG_STATS,
674 "conn=%lu fd=%ld closed\n",
675 connid, (long) sd, 0, 0, 0 );
678 ber_sockbuf_free( c->c_sb );
680 c->c_sb = ber_sockbuf_alloc( );
683 ber_len_t max = sockbuf_max_incoming;
684 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
687 c->c_conn_state = SLAP_C_INVALID;
688 c->c_struct_state = SLAP_C_UNUSED;
691 /* call destructors, then constructors; avoids unnecessary allocation */
692 if ( slapi_plugins_used ) {
693 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
698 int connection_state_closing( Connection *c )
700 /* c_mutex must be locked by caller */
704 assert( c->c_struct_state == SLAP_C_USED );
706 state = c->c_conn_state;
708 assert( state != SLAP_C_INVALID );
710 return state == SLAP_C_CLOSING;
713 static void connection_abandon( Connection *c )
715 /* c_mutex must be locked by caller */
717 Operation *o, *next, op = {0};
723 op.o_connid = c->c_connid;
724 op.o_tag = LDAP_REQ_ABANDON;
725 for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
726 next = LDAP_STAILQ_NEXT( o, o_next );
727 op.orn_msgid = o->o_msgid;
729 op.o_bd = frontendDB;
730 frontendDB->be_abandon( &op, &rs );
733 /* remove pending operations */
734 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
735 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
736 LDAP_STAILQ_NEXT(o, o_next) = NULL;
741 void connection_closing( Connection *c )
743 assert( connections != NULL );
745 assert( c->c_struct_state == SLAP_C_USED );
746 assert( c->c_conn_state != SLAP_C_INVALID );
748 /* c_mutex must be locked by caller */
750 if( c->c_conn_state != SLAP_C_CLOSING ) {
753 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
754 Debug( LDAP_DEBUG_TRACE,
755 "connection_closing: readying conn=%lu sd=%d for close\n",
756 c->c_connid, sd, 0 );
757 /* update state to closing */
758 c->c_conn_state = SLAP_C_CLOSING;
760 /* don't listen on this port anymore */
761 slapd_clr_read( sd, 1 );
763 /* abandon active operations */
764 connection_abandon( c );
766 /* wake write blocked operations */
767 slapd_clr_write( sd, 1 );
768 ldap_pvt_thread_cond_signal( &c->c_write_cv );
772 static void connection_close( Connection *c )
776 assert( connections != NULL );
778 assert( c->c_struct_state == SLAP_C_USED );
779 assert( c->c_conn_state == SLAP_C_CLOSING );
781 /* note: connections_mutex and c_mutex should be locked by caller */
783 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
784 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
785 Debug( LDAP_DEBUG_TRACE,
786 "connection_close: deferring conn=%lu sd=%d\n",
787 c->c_connid, sd, 0 );
791 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
792 c->c_connid, sd, 0 );
793 connection_destroy( c );
796 unsigned long connections_nextid(void)
799 assert( connections != NULL );
801 ldap_pvt_thread_mutex_lock( &connections_mutex );
805 ldap_pvt_thread_mutex_unlock( &connections_mutex );
810 Connection* connection_first( ber_socket_t *index )
812 assert( connections != NULL );
813 assert( index != NULL );
815 ldap_pvt_thread_mutex_lock( &connections_mutex );
819 return connection_next(NULL, index);
822 Connection* connection_next( Connection *c, ber_socket_t *index )
824 assert( connections != NULL );
825 assert( index != NULL );
826 assert( *index <= dtblsize );
829 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
834 for(; *index < dtblsize; (*index)++) {
835 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
836 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
844 if( connections[*index].c_struct_state == SLAP_C_USED ) {
845 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
846 c = &connections[(*index)++];
850 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
851 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
855 ldap_pvt_thread_mutex_lock( &c->c_mutex );
861 void connection_done( Connection *c )
863 assert( connections != NULL );
866 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
869 ldap_pvt_thread_mutex_unlock( &connections_mutex );
873 * connection_activity - handle the request operation op on connection
874 * conn. This routine figures out what kind of operation it is and
875 * calls the appropriate stub to handle it.
879 /* FIXME: returns 0 in case of failure */
880 #define INCR_OP_INITIATED(index) \
882 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
883 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
884 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
886 #define INCR_OP_COMPLETED(index) \
888 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
889 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
890 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
891 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
893 #else /* !SLAPD_MONITOR */
894 #define INCR_OP_INITIATED(index)
895 #define INCR_OP_COMPLETED(index) \
897 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
898 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
899 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
901 #endif /* !SLAPD_MONITOR */
904 * NOTE: keep in sync with enum in slapd.h
906 static int (*opfun[])( Operation *op, SlapReply *rs ) = {
921 connection_operation( void *ctx, void *arg_v )
924 Operation *op = arg_v;
925 SlapReply rs = {REP_RESULT};
926 ber_tag_t tag = op->o_tag;
928 Connection *conn = op->o_conn;
930 void *memctx_null = NULL;
933 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
934 /* FIXME: returns 0 in case of failure */
935 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
936 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
938 op->o_threadctx = ctx;
942 case LDAP_REQ_UNBIND:
944 case LDAP_REQ_DELETE:
946 case LDAP_REQ_MODIFY:
947 case LDAP_REQ_COMPARE:
948 case LDAP_REQ_SEARCH:
949 case LDAP_REQ_ABANDON:
950 case LDAP_REQ_EXTENDED:
953 Debug( LDAP_DEBUG_ANY, "connection_operation: "
954 "conn %lu unknown LDAP request 0x%lx\n",
955 conn->c_connid, tag, 0 );
956 op->o_tag = LBER_ERROR;
957 rs.sr_err = LDAP_PROTOCOL_ERROR;
958 rs.sr_text = "unknown LDAP request";
959 send_ldap_disconnect( op, &rs );
960 rc = SLAPD_DISCONNECT;
961 goto operations_error;
964 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
965 Debug( LDAP_DEBUG_ANY, "connection_operation: "
966 "error: SASL bind in progress (tag=%ld).\n",
968 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
969 "SASL bind in progress" );
970 rc = LDAP_OPERATIONS_ERROR;
971 goto operations_error;
974 /* We can use Thread-Local storage for most mallocs. We can
975 * also use TL for ber parsing, but not on Add or Modify.
978 memsiz = ber_len( op->o_ber ) * 64;
979 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
981 memsiz = SLAP_SLAB_SIZE;
983 memctx = slap_sl_mem_create( memsiz, SLAP_SLAB_STACK, ctx );
984 op->o_tmpmemctx = memctx;
985 op->o_tmpmfuncs = &slap_sl_mfuncs;
986 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
987 /* Note - the ber and its buffer are already allocated from
988 * regular memory; this only affects subsequent mallocs that
989 * ber_scanf may invoke.
991 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
996 opidx = SLAP_OP_BIND;
999 case LDAP_REQ_UNBIND:
1000 opidx = SLAP_OP_UNBIND;
1004 opidx = SLAP_OP_ADD;
1007 case LDAP_REQ_DELETE:
1008 opidx = SLAP_OP_DELETE;
1011 case LDAP_REQ_MODRDN:
1012 opidx = SLAP_OP_MODRDN;
1015 case LDAP_REQ_MODIFY:
1016 opidx = SLAP_OP_MODIFY;
1019 case LDAP_REQ_COMPARE:
1020 opidx = SLAP_OP_COMPARE;
1023 case LDAP_REQ_SEARCH:
1024 opidx = SLAP_OP_SEARCH;
1027 case LDAP_REQ_ABANDON:
1028 opidx = SLAP_OP_ABANDON;
1031 case LDAP_REQ_EXTENDED:
1032 opidx = SLAP_OP_EXTENDED;
1040 assert( opidx > -1 );
1041 INCR_OP_INITIATED( opidx );
1042 rc = (*(opfun[opidx]))( op, &rs );
1045 if ( rc == SLAPD_DISCONNECT ) {
1048 } else if ( opidx > -1 ) {
1049 /* increment completed operations count
1050 * only if operation was initiated
1051 * and rc != SLAPD_DISCONNECT */
1052 INCR_OP_COMPLETED( opidx );
1055 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
1056 if ( rc == SLAPD_ABANDON )
1057 op->o_cancel = SLAP_CANCEL_ACK;
1059 op->o_cancel = LDAP_TOO_LATE;
1061 while ( op->o_cancel != SLAP_CANCEL_NONE &&
1062 op->o_cancel != SLAP_CANCEL_DONE )
1064 ldap_pvt_thread_yield();
1067 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1069 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx_null );
1071 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1072 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1074 conn->c_n_ops_executing--;
1075 conn->c_n_ops_completed++;
1079 case LDAP_REQ_UNBIND:
1080 /* c_mutex is locked */
1081 connection_closing( conn );
1085 conn->c_sasl_bind_in_progress =
1086 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1088 if( conn->c_conn_state == SLAP_C_BINDING) {
1089 conn->c_conn_state = SLAP_C_ACTIVE;
1093 connection_resched( conn );
1094 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1098 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1100 int connection_client_setup(
1102 ldap_pvt_thread_start_t *func,
1108 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1109 CONN_IS_CLIENT, 0, NULL );
1110 if ( rc < 0 ) return -1;
1112 c = connection_get( s );
1113 c->c_clientfunc = func;
1114 c->c_clientarg = arg;
1115 connection_return( c );
1116 slapd_add_internal( s, 0 );
1117 slapd_set_read( s, 1 );
1121 void connection_client_enable(
1124 slapd_set_read( s, 1 );
1127 void connection_client_stop(
1132 /* get (locked) connection */
1133 c = connection_get( s );
1135 assert( c->c_conn_state == SLAP_C_CLIENT );
1137 c->c_listener = NULL;
1138 c->c_conn_state = SLAP_C_INVALID;
1139 c->c_struct_state = SLAP_C_UNUSED;
1140 connection_return( c );
1141 slapd_remove( s, 0, 1 );
1144 int connection_read(ber_socket_t s)
1149 assert( connections != NULL );
1151 ldap_pvt_thread_mutex_lock( &connections_mutex );
1153 /* get (locked) connection */
1154 c = connection_get( s );
1157 Debug( LDAP_DEBUG_ANY,
1158 "connection_read(%ld): no connection!\n",
1160 slapd_remove(s, 1, 0);
1162 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1168 if( c->c_conn_state == SLAP_C_CLOSING ) {
1169 Debug( LDAP_DEBUG_TRACE,
1170 "connection_read(%d): closing, ignoring input for id=%lu\n",
1171 s, c->c_connid, 0 );
1172 connection_return( c );
1173 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1177 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1178 slapd_clr_read( s, 0 );
1179 ldap_pvt_thread_pool_submit( &connection_pool,
1180 c->c_clientfunc, c->c_clientarg );
1181 connection_return( c );
1182 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1186 Debug( LDAP_DEBUG_TRACE,
1187 "connection_read(%d): checking for input on id=%lu\n",
1188 s, c->c_connid, 0 );
1191 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1192 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1194 #if 0 /* required by next #if 0 */
1199 Debug( LDAP_DEBUG_TRACE,
1200 "connection_read(%d): TLS accept error "
1201 "error=%d id=%lu, closing\n",
1202 s, rc, c->c_connid );
1203 c->c_needs_tls_accept = 0;
1204 /* connections_mutex and c_mutex are locked */
1205 connection_closing( c );
1208 /* Drain input before close, to allow SSL error codes
1209 * to propagate to client. */
1215 rc = select(s+1, &rfd, NULL, NULL, &tv);
1217 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1221 connection_close( c );
1223 } else if ( rc == 0 ) {
1225 struct berval authid = BER_BVNULL;
1227 c->c_needs_tls_accept = 0;
1229 /* we need to let SASL know */
1230 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1232 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1233 if( c->c_tls_ssf > c->c_ssf ) {
1234 c->c_ssf = c->c_tls_ssf;
1237 rc = dnX509peerNormalize( ssl, &authid );
1238 if ( rc != LDAP_SUCCESS ) {
1239 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1240 "unable to get TLS client DN, error=%d id=%lu\n",
1241 s, rc, c->c_connid );
1243 slap_sasl_external( c, c->c_tls_ssf, &authid );
1244 if ( authid.bv_val ) free( authid.bv_val );
1247 /* if success and data is ready, fall thru to data input loop */
1249 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1251 connection_return( c );
1252 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1258 #ifdef HAVE_CYRUS_SASL
1259 if ( c->c_sasl_layers ) {
1260 /* If previous layer is not removed yet, give up for now */
1261 if ( !c->c_sasl_sockctx ) {
1262 connection_return( c );
1263 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1267 c->c_sasl_layers = 0;
1269 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1271 if( rc != LDAP_SUCCESS ) {
1272 Debug( LDAP_DEBUG_TRACE,
1273 "connection_read(%d): SASL install error "
1274 "error=%d id=%lu, closing\n",
1275 s, rc, c->c_connid );
1276 /* connections_mutex and c_mutex are locked */
1277 connection_closing( c );
1278 connection_close( c );
1279 connection_return( c );
1280 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1286 #define CONNECTION_INPUT_LOOP 1
1287 /* #define DATA_READY_LOOP 1 */
1290 /* How do we do this without getting into a busy loop ? */
1291 rc = connection_input( c );
1293 #ifdef DATA_READY_LOOP
1294 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1295 #elif CONNECTION_INPUT_LOOP
1302 Debug( LDAP_DEBUG_TRACE,
1303 "connection_read(%d): input error=%d id=%lu, closing.\n",
1304 s, rc, c->c_connid );
1305 /* connections_mutex and c_mutex are locked */
1306 connection_closing( c );
1307 connection_close( c );
1308 connection_return( c );
1309 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1313 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1314 slapd_set_read( s, 1 );
1317 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1318 slapd_set_write( s, 1 );
1321 connection_return( c );
1322 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1336 #ifdef LDAP_CONNECTIONLESS
1342 if ( conn->c_currentber == NULL &&
1343 ( conn->c_currentber = ber_alloc()) == NULL )
1345 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1351 #ifdef LDAP_CONNECTIONLESS
1352 if ( conn->c_is_udp ) {
1353 char peername[sizeof("IP=255.255.255.255:65336")];
1354 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1355 sizeof(struct sockaddr));
1356 if (len != sizeof(struct sockaddr))
1358 sprintf( peername, "IP=%s:%d",
1359 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1360 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1361 Statslog( LDAP_DEBUG_STATS,
1362 "conn=%lu UDP request from %s (%s) accepted.\n",
1363 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1366 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1367 if ( tag != LDAP_TAG_MESSAGE ) {
1371 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1373 Debug( LDAP_DEBUG_TRACE,
1374 "ber_get_next on fd %d failed errno=%d (%s)\n",
1375 sd, err, sock_errstr(err) );
1376 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1377 /* log, close and send error */
1378 ber_free( conn->c_currentber, 1 );
1379 conn->c_currentber = NULL;
1386 ber = conn->c_currentber;
1387 conn->c_currentber = NULL;
1389 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1390 /* log, close and send error */
1391 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n",
1397 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1398 /* log, close and send error */
1399 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n",
1406 #ifdef LDAP_CONNECTIONLESS
1407 if( conn->c_is_udp ) {
1408 if( tag == LBER_OCTETSTRING ) {
1409 ber_get_stringa( ber, &cdn );
1410 tag = ber_peek_tag(ber, &len);
1412 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1413 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1419 if(tag == LDAP_REQ_BIND) {
1420 /* immediately abandon all existing operations upon BIND */
1421 connection_abandon( conn );
1424 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1427 /* clear state if the connection is being reused from inactive */
1428 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1429 memset( &conn->c_pagedresults_state, 0, sizeof( conn->c_pagedresults_state ) );
1432 op->o_res_ber = NULL;
1434 #ifdef LDAP_CONNECTIONLESS
1435 if (conn->c_is_udp) {
1437 ber_str2bv( cdn, 0, 1, &op->o_dn );
1438 op->o_protocol = LDAP_VERSION2;
1440 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1441 if (op->o_res_ber == NULL) return 1;
1443 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1444 sizeof(struct sockaddr), 0 );
1446 if (rc != sizeof(struct sockaddr)) {
1447 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1451 if (op->o_protocol == LDAP_VERSION2) {
1452 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1454 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1459 #endif /* LDAP_CONNECTIONLESS */
1463 /* Don't process requests when the conn is in the middle of a
1464 * Bind, or if it's closing. Also, don't let any single conn
1465 * use up all the available threads, and don't execute if we're
1466 * currently blocked on output. And don't execute if there are
1467 * already pending ops, let them go first. Abandon operations
1468 * get exceptions to some, but not all, cases.
1470 if (tag != LDAP_REQ_ABANDON && conn->c_conn_state == SLAP_C_CLOSING) {
1472 } else if (tag != LDAP_REQ_ABANDON && conn->c_writewaiter) {
1473 defer = "awaiting write";
1474 } else if (conn->c_n_ops_executing >= connection_pool_max/2) {
1475 defer = "too many executing";
1476 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1478 } else if (tag != LDAP_REQ_ABANDON && conn->c_n_ops_pending) {
1479 defer = "pending operations";
1483 int max = conn->c_dn.bv_len
1484 ? slap_conn_max_pending_auth
1485 : slap_conn_max_pending;
1487 Debug( LDAP_DEBUG_ANY,
1488 "connection_input: conn=%lu deferring operation: %s\n",
1489 conn->c_connid, defer, 0 );
1490 conn->c_n_ops_pending++;
1491 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1492 if ( conn->c_n_ops_pending > max ) {
1498 conn->c_n_ops_executing++;
1499 connection_op_activate( op );
1503 if ( conn->c_struct_state != SLAP_C_USED ) {
1504 /* connection must have got closed underneath us */
1508 assert( conn->c_struct_state == SLAP_C_USED );
1514 connection_resched( Connection *conn )
1518 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1521 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1523 /* use trylock to avoid possible deadlock */
1524 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1527 Debug( LDAP_DEBUG_TRACE,
1528 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1529 conn->c_connid, sd, 0 );
1531 * reaquire locks in the right order...
1532 * this may allow another thread to close this connection,
1533 * so recheck state below.
1535 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1536 ldap_pvt_thread_mutex_lock( &connections_mutex );
1537 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1540 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1541 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1542 "closed by other thread conn=%lu sd=%d\n",
1543 conn->c_connid, sd, 0 );
1545 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1546 "attempting closing conn=%lu sd=%d\n",
1547 conn->c_connid, sd, 0 );
1548 connection_close( conn );
1551 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1555 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1556 /* other states need different handling */
1560 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1561 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1564 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1565 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1566 /* pending operations should not be marked for abandonment */
1567 assert(!op->o_abandon);
1569 conn->c_n_ops_pending--;
1570 conn->c_n_ops_executing++;
1572 connection_op_activate( op );
1574 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1582 connection_init_log_prefix( Operation *op )
1584 if ( op->o_connid == (unsigned long)(-1) ) {
1585 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1586 "conn=-1 op=%lu", op->o_opid );
1589 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1590 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1594 static int connection_op_activate( Operation *op )
1597 ber_tag_t tag = op->o_tag;
1599 if(tag == LDAP_REQ_BIND) {
1600 op->o_conn->c_conn_state = SLAP_C_BINDING;
1603 if (!op->o_dn.bv_len) {
1604 op->o_authz = op->o_conn->c_authz;
1605 if ( BER_BVISNULL( &op->o_conn->c_sasl_authz_dn )) {
1606 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1607 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1609 ber_dupbv( &op->o_dn, &op->o_conn->c_sasl_authz_dn );
1610 ber_dupbv( &op->o_ndn, &op->o_conn->c_sasl_authz_dn );
1613 op->o_authtype = op->o_conn->c_authtype;
1614 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1616 if (!op->o_protocol) {
1617 op->o_protocol = op->o_conn->c_protocol
1618 ? op->o_conn->c_protocol : LDAP_VERSION3;
1620 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1621 && op->o_protocol > LDAP_VERSION2)
1623 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1626 op->o_connid = op->o_conn->c_connid;
1627 connection_init_log_prefix( op );
1629 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1631 status = ldap_pvt_thread_pool_submit( &connection_pool,
1632 connection_operation, (void *) op );
1634 if ( status != 0 ) {
1635 Debug( LDAP_DEBUG_ANY,
1636 "ldap_pvt_thread_pool_submit: failed (%d) for conn=%lu\n",
1637 status, op->o_connid, 0 );
1638 /* should move op to pending list */
1644 int connection_write(ber_socket_t s)
1649 assert( connections != NULL );
1651 ldap_pvt_thread_mutex_lock( &connections_mutex );
1653 c = connection_get( s );
1656 Debug( LDAP_DEBUG_ANY,
1657 "connection_write(%ld): no connection!\n",
1659 slapd_remove(s, 1, 0);
1660 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1664 slapd_clr_write( s, 0);
1668 Debug( LDAP_DEBUG_TRACE,
1669 "connection_write(%d): waking output for id=%lu\n",
1670 s, c->c_connid, 0 );
1671 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1673 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1674 slapd_set_read( s, 1 );
1676 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1677 slapd_set_write( s, 1 );
1679 /* If there are ops pending because of a writewaiter, start
1682 while ((op = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
1683 if ( !c->c_writewaiter ) break;
1684 if ( c->c_n_ops_executing > connection_pool_max/2 ) {
1687 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
1688 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1689 /* pending operations should not be marked for abandonment */
1690 assert(!op->o_abandon);
1692 c->c_n_ops_pending--;
1693 c->c_n_ops_executing++;
1695 connection_op_activate( op );
1699 connection_return( c );
1700 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1705 connection_fake_init(
1710 conn->c_connid = -1;
1711 conn->c_send_ldap_result = slap_send_ldap_result;
1712 conn->c_send_search_entry = slap_send_search_entry;
1713 conn->c_send_search_reference = slap_send_search_reference;
1714 conn->c_listener = (Listener *)&dummy_list;
1715 conn->c_peer_domain = slap_empty_bv;
1716 conn->c_peer_name = slap_empty_bv;
1718 memset(op, 0, OPERATION_BUFFER_SIZE);
1719 op->o_hdr = (Opheader *)(op+1);
1720 op->o_controls = (void **)(op->o_hdr+1);
1721 /* set memory context */
1722 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx);
1723 op->o_tmpmfuncs = &slap_sl_mfuncs;
1724 op->o_threadctx = ctx;
1727 op->o_connid = op->o_conn->c_connid;
1728 connection_init_log_prefix( op );
1730 op->o_time = slap_get_time();
1734 connection_assign_nextid( Connection *conn )
1736 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1737 conn->c_connid = conn_nextid++;
1738 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );