2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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 );
613 c->c_authz_backend = NULL;
617 connection_destroy( Connection *c )
619 /* note: connections_mutex should be locked by caller */
621 unsigned long connid;
623 assert( connections != NULL );
625 assert( c->c_struct_state != SLAP_C_UNUSED );
626 assert( c->c_conn_state != SLAP_C_INVALID );
627 assert( LDAP_STAILQ_EMPTY(&c->c_ops) );
628 assert( c->c_writewaiter == 0);
630 /* only for stats (print -1 as "%lu" may give unexpected results ;) */
631 connid = c->c_connid;
633 backend_connection_destroy(c);
638 c->c_activitytime = c->c_starttime = 0;
640 connection2anonymous( c );
641 c->c_listener = NULL;
643 if(c->c_peer_domain.bv_val != NULL) {
644 free(c->c_peer_domain.bv_val);
646 BER_BVZERO( &c->c_peer_domain );
647 if(c->c_peer_name.bv_val != NULL) {
648 free(c->c_peer_name.bv_val);
650 BER_BVZERO( &c->c_peer_name );
652 c->c_sasl_bind_in_progress = 0;
653 if(c->c_sasl_bind_mech.bv_val != NULL) {
654 free(c->c_sasl_bind_mech.bv_val);
656 BER_BVZERO( &c->c_sasl_bind_mech );
658 slap_sasl_close( c );
660 if ( c->c_currentber != NULL ) {
661 ber_free( c->c_currentber, 1 );
662 c->c_currentber = NULL;
665 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
666 if ( sd != AC_SOCKET_INVALID ) {
667 slapd_remove( sd, 1, 0 );
669 Statslog( LDAP_DEBUG_STATS,
670 "conn=%lu fd=%ld closed\n",
671 connid, (long) sd, 0, 0, 0 );
674 ber_sockbuf_free( c->c_sb );
676 c->c_sb = ber_sockbuf_alloc( );
679 ber_len_t max = sockbuf_max_incoming;
680 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max );
683 c->c_conn_state = SLAP_C_INVALID;
684 c->c_struct_state = SLAP_C_UNUSED;
687 /* call destructors, then constructors; avoids unnecessary allocation */
688 if ( slapi_plugins_used ) {
689 slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c );
694 int connection_state_closing( Connection *c )
696 /* c_mutex must be locked by caller */
700 assert( c->c_struct_state == SLAP_C_USED );
702 state = c->c_conn_state;
704 assert( state != SLAP_C_INVALID );
706 return state == SLAP_C_CLOSING;
709 static void connection_abandon( Connection *c )
711 /* c_mutex must be locked by caller */
715 LDAP_STAILQ_FOREACH(o, &c->c_ops, o_next) {
719 /* remove pending operations */
720 while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) {
721 LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next );
722 LDAP_STAILQ_NEXT(o, o_next) = NULL;
727 void connection_closing( Connection *c )
729 assert( connections != NULL );
731 assert( c->c_struct_state == SLAP_C_USED );
732 assert( c->c_conn_state != SLAP_C_INVALID );
734 /* c_mutex must be locked by caller */
736 if( c->c_conn_state != SLAP_C_CLOSING ) {
739 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
740 Debug( LDAP_DEBUG_TRACE,
741 "connection_closing: readying conn=%lu sd=%d for close\n",
742 c->c_connid, sd, 0 );
743 /* update state to closing */
744 c->c_conn_state = SLAP_C_CLOSING;
746 /* don't listen on this port anymore */
747 slapd_clr_read( sd, 1 );
749 /* abandon active operations */
750 connection_abandon( c );
752 /* wake write blocked operations */
753 slapd_clr_write( sd, 1 );
754 ldap_pvt_thread_cond_signal( &c->c_write_cv );
758 static void connection_close( Connection *c )
762 assert( connections != NULL );
764 assert( c->c_struct_state == SLAP_C_USED );
765 assert( c->c_conn_state == SLAP_C_CLOSING );
767 /* note: connections_mutex and c_mutex should be locked by caller */
769 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
770 if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) {
771 Debug( LDAP_DEBUG_TRACE,
772 "connection_close: deferring conn=%lu sd=%d\n",
773 c->c_connid, sd, 0 );
777 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
778 c->c_connid, sd, 0 );
779 connection_destroy( c );
782 unsigned long connections_nextid(void)
785 assert( connections != NULL );
787 ldap_pvt_thread_mutex_lock( &connections_mutex );
791 ldap_pvt_thread_mutex_unlock( &connections_mutex );
796 Connection* connection_first( ber_socket_t *index )
798 assert( connections != NULL );
799 assert( index != NULL );
801 ldap_pvt_thread_mutex_lock( &connections_mutex );
805 return connection_next(NULL, index);
808 Connection* connection_next( Connection *c, ber_socket_t *index )
810 assert( connections != NULL );
811 assert( index != NULL );
812 assert( *index <= dtblsize );
815 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
820 for(; *index < dtblsize; (*index)++) {
821 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
822 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
830 if( connections[*index].c_struct_state == SLAP_C_USED ) {
831 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
832 c = &connections[(*index)++];
836 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
837 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
841 ldap_pvt_thread_mutex_lock( &c->c_mutex );
847 void connection_done( Connection *c )
849 assert( connections != NULL );
852 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
855 ldap_pvt_thread_mutex_unlock( &connections_mutex );
859 * connection_activity - handle the request operation op on connection
860 * conn. This routine figures out what kind of operation it is and
861 * calls the appropriate stub to handle it.
865 /* FIXME: returns 0 in case of failure */
866 #define INCR_OP_INITIATED(index) \
868 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
869 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated_[(index)], 1); \
870 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
872 #define INCR_OP_COMPLETED(index) \
874 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex ); \
875 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed, 1); \
876 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_completed_[(index)], 1); \
877 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); \
879 #else /* !SLAPD_MONITOR */
880 #define INCR_OP_INITIATED(index)
881 #define INCR_OP_COMPLETED(index)
882 #endif /* !SLAPD_MONITOR */
885 connection_operation( void *ctx, void *arg_v )
888 Operation *op = arg_v;
889 SlapReply rs = {REP_RESULT};
890 ber_tag_t tag = op->o_tag;
892 ber_tag_t oldtag = tag;
893 #endif /* SLAPD_MONITOR */
894 Connection *conn = op->o_conn;
896 void *memctx_null = NULL;
899 ldap_pvt_thread_mutex_lock( &slap_counters.sc_ops_mutex );
900 /* FIXME: returns 0 in case of failure */
901 ldap_pvt_mp_add_ulong(slap_counters.sc_ops_initiated, 1);
902 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
904 op->o_threadctx = ctx;
908 case LDAP_REQ_UNBIND:
910 case LDAP_REQ_DELETE:
911 case LDAP_REQ_MODRDN:
912 case LDAP_REQ_MODIFY:
913 case LDAP_REQ_COMPARE:
914 case LDAP_REQ_SEARCH:
915 case LDAP_REQ_ABANDON:
916 case LDAP_REQ_EXTENDED:
919 Debug( LDAP_DEBUG_ANY, "connection_operation: "
920 "conn %lu unknown LDAP request 0x%lx\n",
921 conn->c_connid, tag, 0 );
922 op->o_tag = LBER_ERROR;
923 rs.sr_err = LDAP_PROTOCOL_ERROR;
924 rs.sr_text = "unknown LDAP request";
925 send_ldap_disconnect( op, &rs );
926 rc = SLAPD_DISCONNECT;
927 goto operations_error;
930 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
931 Debug( LDAP_DEBUG_ANY, "connection_operation: "
932 "error: SASL bind in progress (tag=%ld).\n",
934 send_ldap_error( op, &rs, LDAP_OPERATIONS_ERROR,
935 "SASL bind in progress" );
936 rc = LDAP_OPERATIONS_ERROR;
937 goto operations_error;
940 /* We can use Thread-Local storage for most mallocs. We can
941 * also use TL for ber parsing, but not on Add or Modify.
944 memsiz = ber_len( op->o_ber ) * 64;
945 if ( SLAP_SLAB_SIZE > memsiz ) memsiz = SLAP_SLAB_SIZE;
947 memsiz = SLAP_SLAB_SIZE;
949 memctx = slap_sl_mem_create( memsiz, ctx );
950 op->o_tmpmemctx = memctx;
951 op->o_tmpmfuncs = &slap_sl_mfuncs;
952 if ( tag != LDAP_REQ_ADD && tag != LDAP_REQ_MODIFY ) {
953 /* Note - the ber and its buffer are already allocated from
954 * regular memory; this only affects subsequent mallocs that
955 * ber_scanf may invoke.
957 ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx );
962 INCR_OP_INITIATED(SLAP_OP_BIND);
963 rc = do_bind( op, &rs );
966 case LDAP_REQ_UNBIND:
967 INCR_OP_INITIATED(SLAP_OP_UNBIND);
968 rc = do_unbind( op, &rs );
972 INCR_OP_INITIATED(SLAP_OP_ADD);
973 rc = do_add( op, &rs );
976 case LDAP_REQ_DELETE:
977 INCR_OP_INITIATED(SLAP_OP_DELETE);
978 rc = do_delete( op, &rs );
981 case LDAP_REQ_MODRDN:
982 INCR_OP_INITIATED(SLAP_OP_MODRDN);
983 rc = do_modrdn( op, &rs );
986 case LDAP_REQ_MODIFY:
987 INCR_OP_INITIATED(SLAP_OP_MODIFY);
988 rc = do_modify( op, &rs );
991 case LDAP_REQ_COMPARE:
992 INCR_OP_INITIATED(SLAP_OP_COMPARE);
993 rc = do_compare( op, &rs );
996 case LDAP_REQ_SEARCH:
997 INCR_OP_INITIATED(SLAP_OP_SEARCH);
998 rc = do_search( op, &rs );
1001 case LDAP_REQ_ABANDON:
1002 INCR_OP_INITIATED(SLAP_OP_ABANDON);
1003 rc = do_abandon( op, &rs );
1006 case LDAP_REQ_EXTENDED:
1007 INCR_OP_INITIATED(SLAP_OP_EXTENDED);
1008 rc = do_extended( op, &rs );
1017 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
1019 #ifdef SLAPD_MONITOR
1022 INCR_OP_COMPLETED(SLAP_OP_BIND);
1024 case LDAP_REQ_UNBIND:
1025 INCR_OP_COMPLETED(SLAP_OP_UNBIND);
1028 INCR_OP_COMPLETED(SLAP_OP_ADD);
1030 case LDAP_REQ_DELETE:
1031 INCR_OP_COMPLETED(SLAP_OP_DELETE);
1033 case LDAP_REQ_MODRDN:
1034 INCR_OP_COMPLETED(SLAP_OP_MODRDN);
1036 case LDAP_REQ_MODIFY:
1037 INCR_OP_COMPLETED(SLAP_OP_MODIFY);
1039 case LDAP_REQ_COMPARE:
1040 INCR_OP_COMPLETED(SLAP_OP_COMPARE);
1042 case LDAP_REQ_SEARCH:
1043 INCR_OP_COMPLETED(SLAP_OP_SEARCH);
1045 case LDAP_REQ_ABANDON:
1046 INCR_OP_COMPLETED(SLAP_OP_ABANDON);
1048 case LDAP_REQ_EXTENDED:
1049 INCR_OP_COMPLETED(SLAP_OP_EXTENDED);
1055 #endif /* SLAPD_MONITOR */
1056 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex );
1058 if ( op->o_cancel == SLAP_CANCEL_REQ ) {
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 if ( op->o_cancel != SLAP_CANCEL_ACK &&
1072 ( op->o_sync_mode & SLAP_SYNC_PERSIST ) )
1074 slap_sl_mem_detach( ctx, memctx );
1076 } else if ( op->o_sync_slog_size != -1 ) {
1077 slap_sl_mem_detach( ctx, memctx );
1078 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1079 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1080 conn->c_n_ops_executing--;
1081 conn->c_n_ops_completed++;
1084 LDAP_STAILQ_REMOVE( &conn->c_ops, op, slap_op, o_next);
1085 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1087 conn->c_n_ops_executing--;
1088 conn->c_n_ops_completed++;
1093 case LDAP_REQ_UNBIND:
1094 /* c_mutex is locked */
1095 connection_closing( conn );
1099 conn->c_sasl_bind_in_progress =
1100 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
1102 if( conn->c_conn_state == SLAP_C_BINDING) {
1103 conn->c_conn_state = SLAP_C_ACTIVE;
1107 connection_resched( conn );
1108 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1112 static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
1114 int connection_client_setup(
1116 ldap_pvt_thread_start_t *func,
1122 rc = connection_init( s, (Listener *)&dummy_list, "", "",
1123 CONN_IS_CLIENT, 0, NULL );
1124 if ( rc < 0 ) return -1;
1126 c = connection_get( s );
1127 c->c_clientfunc = func;
1128 c->c_clientarg = arg;
1129 connection_return( c );
1130 slapd_add_internal( s, 0 );
1131 slapd_set_read( s, 1 );
1135 void connection_client_enable(
1138 slapd_set_read( s, 1 );
1141 void connection_client_stop(
1146 /* get (locked) connection */
1147 c = connection_get( s );
1149 assert( c->c_conn_state == SLAP_C_CLIENT );
1151 c->c_listener = NULL;
1152 c->c_conn_state = SLAP_C_INVALID;
1153 c->c_struct_state = SLAP_C_UNUSED;
1154 connection_return( c );
1155 slapd_remove( s, 0, 1 );
1158 int connection_read(ber_socket_t s)
1163 assert( connections != NULL );
1165 ldap_pvt_thread_mutex_lock( &connections_mutex );
1167 /* get (locked) connection */
1168 c = connection_get( s );
1171 Debug( LDAP_DEBUG_ANY,
1172 "connection_read(%ld): no connection!\n",
1174 slapd_remove(s, 1, 0);
1176 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1182 if( c->c_conn_state == SLAP_C_CLOSING ) {
1183 Debug( LDAP_DEBUG_TRACE,
1184 "connection_read(%d): closing, ignoring input for id=%lu\n",
1185 s, c->c_connid, 0 );
1186 connection_return( c );
1187 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1191 if ( c->c_conn_state == SLAP_C_CLIENT ) {
1192 slapd_clr_read( s, 0 );
1193 ldap_pvt_thread_pool_submit( &connection_pool,
1194 c->c_clientfunc, c->c_clientarg );
1195 connection_return( c );
1196 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1200 Debug( LDAP_DEBUG_TRACE,
1201 "connection_read(%d): checking for input on id=%lu\n",
1202 s, c->c_connid, 0 );
1205 if ( c->c_is_tls && c->c_needs_tls_accept ) {
1206 rc = ldap_pvt_tls_accept( c->c_sb, slap_tls_ctx );
1208 #if 0 /* required by next #if 0 */
1213 Debug( LDAP_DEBUG_TRACE,
1214 "connection_read(%d): TLS accept error "
1215 "error=%d id=%lu, closing\n",
1216 s, rc, c->c_connid );
1217 c->c_needs_tls_accept = 0;
1218 /* connections_mutex and c_mutex are locked */
1219 connection_closing( c );
1222 /* Drain input before close, to allow SSL error codes
1223 * to propagate to client. */
1229 rc = select(s+1, &rfd, NULL, NULL, &tv);
1231 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL);
1235 connection_close( c );
1237 } else if ( rc == 0 ) {
1239 struct berval authid = BER_BVNULL;
1241 c->c_needs_tls_accept = 0;
1243 /* we need to let SASL know */
1244 ssl = ldap_pvt_tls_sb_ctx( c->c_sb );
1246 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
1247 if( c->c_tls_ssf > c->c_ssf ) {
1248 c->c_ssf = c->c_tls_ssf;
1251 rc = dnX509peerNormalize( ssl, &authid );
1252 if ( rc != LDAP_SUCCESS ) {
1253 Debug( LDAP_DEBUG_TRACE, "connection_read(%d): "
1254 "unable to get TLS client DN, error=%d id=%lu\n",
1255 s, rc, c->c_connid );
1257 slap_sasl_external( c, c->c_tls_ssf, &authid );
1258 if ( authid.bv_val ) free( authid.bv_val );
1261 /* if success and data is ready, fall thru to data input loop */
1263 !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) )
1265 connection_return( c );
1266 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1272 #ifdef HAVE_CYRUS_SASL
1273 if ( c->c_sasl_layers ) {
1274 /* If previous layer is not removed yet, give up for now */
1275 if ( !c->c_sasl_sockctx ) {
1276 connection_return( c );
1277 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1281 c->c_sasl_layers = 0;
1283 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_sockctx );
1285 if( rc != LDAP_SUCCESS ) {
1286 Debug( LDAP_DEBUG_TRACE,
1287 "connection_read(%d): SASL install error "
1288 "error=%d id=%lu, closing\n",
1289 s, rc, c->c_connid );
1290 /* connections_mutex and c_mutex are locked */
1291 connection_closing( c );
1292 connection_close( c );
1293 connection_return( c );
1294 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1300 #define CONNECTION_INPUT_LOOP 1
1301 /* #define DATA_READY_LOOP 1 */
1304 /* How do we do this without getting into a busy loop ? */
1305 rc = connection_input( c );
1307 #ifdef DATA_READY_LOOP
1308 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ));
1309 #elif CONNECTION_INPUT_LOOP
1316 Debug( LDAP_DEBUG_TRACE,
1317 "connection_read(%d): input error=%d id=%lu, closing.\n",
1318 s, rc, c->c_connid );
1319 /* connections_mutex and c_mutex are locked */
1320 connection_closing( c );
1321 connection_close( c );
1322 connection_return( c );
1323 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1327 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1328 slapd_set_read( s, 1 );
1331 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1332 slapd_set_write( s, 1 );
1335 connection_return( c );
1336 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1350 #ifdef LDAP_CONNECTIONLESS
1356 if ( conn->c_currentber == NULL &&
1357 ( conn->c_currentber = ber_alloc()) == NULL )
1359 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1365 #ifdef LDAP_CONNECTIONLESS
1366 if ( conn->c_is_udp ) {
1367 char peername[sizeof("IP=255.255.255.255:65336")];
1368 len = ber_int_sb_read(conn->c_sb, &peeraddr,
1369 sizeof(struct sockaddr));
1370 if (len != sizeof(struct sockaddr))
1372 sprintf( peername, "IP=%s:%d",
1373 inet_ntoa( peeraddr.sa_in_addr.sin_addr ),
1374 (unsigned) ntohs( peeraddr.sa_in_addr.sin_port ) );
1375 Statslog( LDAP_DEBUG_STATS,
1376 "conn=%lu UDP request from %s (%s) accepted.\n",
1377 conn->c_connid, peername, conn->c_sock_name.bv_val, 0, 0 );
1380 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1381 if ( tag != LDAP_TAG_MESSAGE ) {
1385 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1387 Debug( LDAP_DEBUG_TRACE,
1388 "ber_get_next on fd %d failed errno=%d (%s)\n",
1389 sd, err, sock_errstr(err) );
1390 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1391 /* log, close and send error */
1392 ber_free( conn->c_currentber, 1 );
1393 conn->c_currentber = NULL;
1400 ber = conn->c_currentber;
1401 conn->c_currentber = NULL;
1403 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1404 /* log, close and send error */
1405 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n",
1411 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1412 /* log, close and send error */
1413 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n",
1420 #ifdef LDAP_CONNECTIONLESS
1421 if( conn->c_is_udp ) {
1422 if( tag == LBER_OCTETSTRING ) {
1423 ber_get_stringa( ber, &cdn );
1424 tag = ber_peek_tag(ber, &len);
1426 if( tag != LDAP_REQ_ABANDON && tag != LDAP_REQ_SEARCH ) {
1427 Debug( LDAP_DEBUG_ANY, "invalid req for UDP 0x%lx\n", tag, 0, 0 );
1433 if(tag == LDAP_REQ_BIND) {
1434 /* immediately abandon all exiting operations upon BIND */
1435 connection_abandon( conn );
1438 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1441 op->o_assertion = NULL;
1442 op->o_preread_attrs = NULL;
1443 op->o_postread_attrs = NULL;
1444 op->o_vrFilter = NULL;
1445 /* clear state if the connection is being reused from inactive */
1446 if ( conn->c_conn_state == SLAP_C_INACTIVE ) {
1447 memset( &conn->c_pagedresults_state, 0, sizeof( conn->c_pagedresults_state ) );
1449 op->o_pagedresults_state = conn->c_pagedresults_state;
1451 op->o_res_ber = NULL;
1453 #ifdef LDAP_CONNECTIONLESS
1454 if (conn->c_is_udp) {
1456 ber_str2bv( cdn, 0, 1, &op->o_dn );
1457 op->o_protocol = LDAP_VERSION2;
1459 op->o_res_ber = ber_alloc_t( LBER_USE_DER );
1460 if (op->o_res_ber == NULL) return 1;
1462 rc = ber_write( op->o_res_ber, (char *)&peeraddr,
1463 sizeof(struct sockaddr), 0 );
1465 if (rc != sizeof(struct sockaddr)) {
1466 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1470 if (op->o_protocol == LDAP_VERSION2) {
1471 rc = ber_printf(op->o_res_ber, "{is{" /*}}*/, op->o_msgid, "");
1473 Debug( LDAP_DEBUG_ANY, "ber_write failed\n", 0, 0, 0 );
1478 #endif /* LDAP_CONNECTIONLESS */
1482 /* Don't process requests when the conn is in the middle of a
1483 * Bind, or if it's closing. Also, don't let any single conn
1484 * use up all the available threads, and don't execute if we're
1485 * currently blocked on output. And don't execute if there are
1486 * already pending ops, let them go first. Abandon operations
1487 * get exceptions to some, but not all, cases.
1489 if (tag != LDAP_REQ_ABANDON && conn->c_conn_state == SLAP_C_CLOSING) {
1491 } else if (tag != LDAP_REQ_ABANDON && conn->c_writewaiter) {
1492 defer = "awaiting write";
1493 } else if (conn->c_n_ops_executing >= connection_pool_max/2) {
1494 defer = "too many executing";
1495 } else if (conn->c_conn_state == SLAP_C_BINDING) {
1497 } else if (tag != LDAP_REQ_ABANDON && conn->c_n_ops_pending) {
1498 defer = "pending operations";
1502 int max = conn->c_dn.bv_len
1503 ? slap_conn_max_pending_auth
1504 : slap_conn_max_pending;
1506 Debug( LDAP_DEBUG_ANY,
1507 "connection_input: conn=%lu deferring operation: %s\n",
1508 conn->c_connid, defer, 0 );
1509 conn->c_n_ops_pending++;
1510 LDAP_STAILQ_INSERT_TAIL( &conn->c_pending_ops, op, o_next );
1511 if ( conn->c_n_ops_pending > max ) {
1517 conn->c_n_ops_executing++;
1518 connection_op_activate( op );
1522 if ( conn->c_struct_state != SLAP_C_USED ) {
1523 /* connection must have got closed underneath us */
1527 assert( conn->c_struct_state == SLAP_C_USED );
1533 connection_resched( Connection *conn )
1537 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1540 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1542 /* us trylock to avoid possible deadlock */
1543 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1546 Debug( LDAP_DEBUG_TRACE,
1547 "connection_resched: reaquiring locks conn=%lu sd=%d\n",
1548 conn->c_connid, sd, 0 );
1550 * reaquire locks in the right order...
1551 * this may allow another thread to close this connection,
1552 * so recheck state below.
1554 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1555 ldap_pvt_thread_mutex_lock( &connections_mutex );
1556 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1559 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1560 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1561 "closed by other thread conn=%lu sd=%d\n",
1562 conn->c_connid, sd, 0 );
1564 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1565 "attempting closing conn=%lu sd=%d\n",
1566 conn->c_connid, sd, 0 );
1567 connection_close( conn );
1570 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1574 if( conn->c_conn_state != SLAP_C_ACTIVE || conn->c_writewaiter ) {
1575 /* other states need different handling */
1579 while ((op = LDAP_STAILQ_FIRST( &conn->c_pending_ops )) != NULL) {
1580 if ( conn->c_n_ops_executing > connection_pool_max/2 ) {
1583 LDAP_STAILQ_REMOVE_HEAD( &conn->c_pending_ops, o_next );
1584 LDAP_STAILQ_NEXT(op, o_next) = NULL;
1585 /* pending operations should not be marked for abandonment */
1586 assert(!op->o_abandon);
1588 conn->c_n_ops_pending--;
1589 conn->c_n_ops_executing++;
1591 connection_op_activate( op );
1593 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1601 connection_init_log_prefix( Operation *op )
1603 if ( op->o_connid == (unsigned long)(-1) ) {
1604 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1605 "conn=-1 op=%lu", op->o_opid );
1608 snprintf( op->o_log_prefix, sizeof( op->o_log_prefix ),
1609 "conn=%lu op=%lu", op->o_connid, op->o_opid );
1613 static int connection_op_activate( Operation *op )
1616 ber_tag_t tag = op->o_tag;
1618 if(tag == LDAP_REQ_BIND) {
1619 op->o_conn->c_conn_state = SLAP_C_BINDING;
1622 if (!op->o_dn.bv_len) {
1623 op->o_authz = op->o_conn->c_authz;
1624 ber_dupbv( &op->o_dn, &op->o_conn->c_dn );
1625 ber_dupbv( &op->o_ndn, &op->o_conn->c_ndn );
1627 op->o_authtype = op->o_conn->c_authtype;
1628 ber_dupbv( &op->o_authmech, &op->o_conn->c_authmech );
1630 if (!op->o_protocol) {
1631 op->o_protocol = op->o_conn->c_protocol
1632 ? op->o_conn->c_protocol : LDAP_VERSION3;
1634 if (op->o_conn->c_conn_state == SLAP_C_INACTIVE
1635 && op->o_protocol > LDAP_VERSION2)
1637 op->o_conn->c_conn_state = SLAP_C_ACTIVE;
1640 op->o_connid = op->o_conn->c_connid;
1641 connection_init_log_prefix( op );
1643 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op, o_next );
1645 status = ldap_pvt_thread_pool_submit( &connection_pool,
1646 connection_operation, (void *) op );
1648 if ( status != 0 ) {
1649 Debug( LDAP_DEBUG_ANY,
1650 "ldap_pvt_thread_pool_submit: failed (%d) for conn=%lu\n",
1651 status, op->o_connid, 0 );
1652 /* should move op to pending list */
1658 int connection_write(ber_socket_t s)
1662 assert( connections != NULL );
1664 ldap_pvt_thread_mutex_lock( &connections_mutex );
1666 c = connection_get( s );
1668 slapd_clr_write( s, 0);
1671 Debug( LDAP_DEBUG_ANY,
1672 "connection_write(%ld): no connection!\n",
1674 slapd_remove(s, 1, 0);
1675 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1681 Debug( LDAP_DEBUG_TRACE,
1682 "connection_write(%d): waking output for id=%lu\n",
1683 s, c->c_connid, 0 );
1684 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1686 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1687 slapd_set_read( s, 1 );
1689 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1690 slapd_set_write( s, 1 );
1692 connection_return( c );
1693 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1698 connection_fake_init(
1703 conn->c_connid = -1;
1704 conn->c_send_ldap_result = slap_send_ldap_result;
1705 conn->c_send_search_entry = slap_send_search_entry;
1706 conn->c_send_search_reference = slap_send_search_reference;
1707 conn->c_listener = (Listener *)&dummy_list;
1708 conn->c_peer_domain = slap_empty_bv;
1709 conn->c_peer_name = slap_empty_bv;
1711 /* set memory context */
1712 op->o_tmpmemctx = slap_sl_mem_create( SLAP_SLAB_SIZE, ctx );
1713 op->o_tmpmfuncs = &slap_sl_mfuncs;
1714 op->o_threadctx = ctx;
1717 op->o_connid = op->o_conn->c_connid;
1718 connection_init_log_prefix( op );
1720 op->o_time = slap_get_time();
1724 connection_assign_nextid( Connection *conn )
1726 ldap_pvt_thread_mutex_lock( &conn_nextid_mutex );
1727 conn->c_connid = conn_nextid++;
1728 ldap_pvt_thread_mutex_unlock( &conn_nextid_mutex );