2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/socket.h>
12 #include <ac/signal.h>
13 #include <ac/string.h>
18 /* we need LBER internals */
19 #include "../../libraries/liblber/lber-int.h"
21 /* protected by connections_mutex */
22 static ldap_pvt_thread_mutex_t connections_mutex;
23 static Connection *connections = NULL;
24 static unsigned long conn_nextid = 0;
26 /* structure state (protected by connections_mutex) */
27 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
28 #define SLAP_C_UNUSED 0x01
29 #define SLAP_C_USED 0x02
31 /* connection state (protected by c_mutex ) */
32 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
33 #define SLAP_C_INACTIVE 0x01 /* zero threads */
34 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
35 #define SLAP_C_BINDING 0x03 /* binding */
36 #define SLAP_C_CLOSING 0x04 /* closing */
38 char* connection_state2str( int state ) {
40 case SLAP_C_INVALID: return "!";
41 case SLAP_C_INACTIVE: return "|";
42 case SLAP_C_ACTIVE: return "";
43 case SLAP_C_BINDING: return "B";
44 case SLAP_C_CLOSING: return "C";
50 static Connection* connection_get( ber_socket_t s );
52 static int connection_input( Connection *c );
53 static void connection_close( Connection *c );
55 static int connection_op_activate( Connection *conn, Operation *op );
56 static int connection_resched( Connection *conn );
57 static void connection_abandon( Connection *conn );
58 static void connection_destroy( Connection *c );
66 * Initialize connection management infrastructure.
68 int connections_init(void)
70 assert( connections == NULL );
72 if( connections != NULL) {
73 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
78 /* should check return of every call */
79 ldap_pvt_thread_mutex_init( &connections_mutex );
81 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
83 if( connections == NULL ) {
84 Debug( LDAP_DEBUG_ANY,
85 "connections_init: allocation (%d*%ld) of connection array failed.\n",
86 dtblsize, (long) sizeof(Connection), 0 );
90 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
91 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
94 * per entry initialization of the Connection array initialization
95 * will be done by connection_init()
102 * Destroy connection management infrastructure.
104 int connections_destroy(void)
108 /* should check return of every call */
110 if( connections == NULL) {
111 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
116 for ( i = 0; i < dtblsize; i++ ) {
117 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
118 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
119 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
120 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
127 ldap_pvt_thread_mutex_destroy( &connections_mutex );
132 * shutdown all connections
134 int connections_shutdown(void)
138 ldap_pvt_thread_mutex_lock( &connections_mutex );
140 for ( i = 0; i < dtblsize; i++ ) {
141 if( connections[i].c_struct_state != SLAP_C_USED ) {
145 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
147 /* connections_mutex and c_mutex are locked */
148 connection_closing( &connections[i] );
149 connection_close( &connections[i] );
151 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
154 ldap_pvt_thread_mutex_unlock( &connections_mutex );
160 * Timeout idle connections.
162 int connections_timeout_idle(time_t now)
168 ldap_pvt_thread_mutex_lock( &connections_mutex );
170 for( c = connection_first( &connindex );
172 c = connection_next( c, &connindex ) )
174 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
176 connection_closing( c );
177 connection_close( c );
181 connection_done( c );
183 ldap_pvt_thread_mutex_unlock( &connections_mutex );
188 static Connection* connection_get( ber_socket_t s )
190 /* connections_mutex should be locked by caller */
194 Debug( LDAP_DEBUG_ARGS,
195 "connection_get(%ld)\n",
198 assert( connections != NULL );
200 if(s == AC_SOCKET_INVALID) {
207 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
214 for(i=0; i<dtblsize; i++) {
215 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
216 assert( connections[i].c_conn_state == SLAP_C_INVALID );
217 assert( connections[i].c_sb == 0 );
221 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
222 assert( connections[i].c_conn_state == SLAP_C_INVALID );
223 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
227 /* state can actually change from used -> unused by resched,
228 * so don't assert details here.
231 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
240 ldap_pvt_thread_mutex_lock( &c->c_mutex );
242 if( c->c_struct_state != SLAP_C_USED ) {
243 /* connection must have been closed due to resched */
245 assert( c->c_conn_state == SLAP_C_INVALID );
246 assert( !ber_pvt_sb_in_use( c->c_sb ) );
248 Debug( LDAP_DEBUG_TRACE,
249 "connection_get(%d): connection not used.\n",
252 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
256 Debug( LDAP_DEBUG_TRACE,
257 "connection_get(%d): got connid=%ld\n",
262 assert( c->c_struct_state == SLAP_C_USED );
263 assert( c->c_conn_state != SLAP_C_INVALID );
264 assert( ber_pvt_sb_in_use( c->c_sb ) );
266 c->c_activitytime = slap_get_time();
272 static void connection_return( Connection *c )
274 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
277 long connection_init(
281 const char* peername,
282 const char* sockname,
288 assert( connections != NULL );
290 assert( dnsname != NULL );
291 assert( peername != NULL );
292 assert( sockname != NULL );
298 if( s == AC_SOCKET_INVALID ) {
299 Debug( LDAP_DEBUG_ANY,
300 "connection_init(%ld): invalid.\n",
307 assert( s < dtblsize );
310 ldap_pvt_thread_mutex_lock( &connections_mutex );
321 for( i=0; i < dtblsize; i++) {
322 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
323 assert( connections[i].c_sb == 0 );
328 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
329 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
334 assert( connections[i].c_struct_state == SLAP_C_USED );
335 assert( connections[i].c_conn_state != SLAP_C_INVALID );
336 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
340 Debug( LDAP_DEBUG_ANY,
341 "connection_init(%d): connection table full (%d/%d).\n",
343 ldap_pvt_thread_mutex_unlock( &connections_mutex );
350 assert( c->c_struct_state != SLAP_C_USED );
351 assert( c->c_conn_state == SLAP_C_INVALID );
353 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
357 c->c_listener_url = NULL;
358 c->c_peer_domain = NULL;
359 c->c_peer_name = NULL;
360 c->c_sock_name = NULL;
363 c->c_pending_ops = NULL;
364 c->c_authmech = NULL;
365 c->c_authstate = NULL;
367 #ifdef HAVE_CYRUS_SASL
368 c->c_sasl_context = NULL;
371 c->c_sb = ber_sockbuf_alloc( );
373 /* should check status of thread calls */
374 ldap_pvt_thread_mutex_init( &c->c_mutex );
375 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
376 ldap_pvt_thread_cond_init( &c->c_write_cv );
378 c->c_struct_state = SLAP_C_UNUSED;
381 ldap_pvt_thread_mutex_lock( &c->c_mutex );
383 assert( c->c_struct_state == SLAP_C_UNUSED );
384 assert( c->c_dn == NULL );
385 assert( c->c_cdn == NULL );
386 assert( c->c_listener_url == NULL );
387 assert( c->c_peer_domain == NULL );
388 assert( c->c_peer_name == NULL );
389 assert( c->c_sock_name == NULL );
390 assert( c->c_ops == NULL );
391 assert( c->c_pending_ops == NULL );
392 assert( c->c_authmech == NULL );
393 assert( c->c_authstate == NULL );
394 #ifdef HAVE_CYRUS_SASL
395 assert( c->c_sasl_context == NULL );
398 c->c_listener_url = ch_strdup( url );
399 c->c_peer_domain = ch_strdup( dnsname );
400 c->c_peer_name = ch_strdup( peername );
401 c->c_sock_name = ch_strdup( sockname );
403 c->c_n_ops_received = 0;
404 c->c_n_ops_executing = 0;
405 c->c_n_ops_pending = 0;
406 c->c_n_ops_completed = 0;
412 c->c_activitytime = c->c_starttime = slap_get_time();
414 ber_pvt_sb_set_desc( c->c_sb, s );
415 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
417 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
418 Debug( LDAP_DEBUG_ANY,
419 "connection_init(%d, %s): set nonblocking failed\n",
420 s, c->c_peer_name,0 );
423 id = c->c_connid = conn_nextid++;
425 c->c_conn_state = SLAP_C_INACTIVE;
426 c->c_struct_state = SLAP_C_USED;
431 c->c_needs_tls_accept = 1;
435 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
436 ldap_pvt_thread_mutex_unlock( &connections_mutex );
438 backend_connection_init(c);
444 connection_destroy( Connection *c )
446 /* note: connections_mutex should be locked by caller */
448 assert( connections != NULL );
450 assert( c->c_struct_state != SLAP_C_UNUSED );
451 assert( c->c_conn_state != SLAP_C_INVALID );
452 assert( c->c_ops == NULL );
454 backend_connection_destroy(c);
459 c->c_activitytime = c->c_starttime = 0;
461 if(c->c_dn != NULL) {
465 if(c->c_cdn != NULL) {
469 if(c->c_listener_url != NULL) {
470 free(c->c_listener_url);
471 c->c_listener_url = NULL;
473 if(c->c_peer_domain != NULL) {
474 free(c->c_peer_domain);
475 c->c_peer_domain = NULL;
477 if(c->c_peer_name != NULL) {
478 free(c->c_peer_name);
479 c->c_peer_name = NULL;
481 if(c->c_sock_name != NULL) {
482 free(c->c_sock_name);
483 c->c_sock_name = NULL;
485 if(c->c_authmech != NULL ) {
487 c->c_authmech = NULL;
489 if(c->c_authstate != NULL ) {
490 free(c->c_authstate);
491 c->c_authstate = NULL;
494 #ifdef HAVE_CYRUS_SASL
495 if(c->c_sasl_context != NULL ) {
496 sasl_dispose( &c->c_sasl_context );
497 c->c_sasl_context = NULL;
501 if ( ber_pvt_sb_in_use(c->c_sb) ) {
502 int sd = ber_pvt_sb_get_desc(c->c_sb);
504 slapd_remove( sd, 0 );
505 ber_pvt_sb_close( c->c_sb );
507 Statslog( LDAP_DEBUG_STATS,
508 "conn=%d fd=%d closed.\n",
509 c->c_connid, sd, 0, 0, 0 );
512 ber_pvt_sb_destroy( c->c_sb );
514 c->c_conn_state = SLAP_C_INVALID;
515 c->c_struct_state = SLAP_C_UNUSED;
518 int connection_state_closing( Connection *c )
520 /* c_mutex must be locked by caller */
524 assert( c->c_struct_state == SLAP_C_USED );
526 state = c->c_conn_state;
528 assert( state != SLAP_C_INVALID );
530 return state == SLAP_C_CLOSING;
533 static void connection_abandon( Connection *c )
535 /* c_mutex must be locked by caller */
539 for( o = c->c_ops; o != NULL; o = o->o_next ) {
540 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
542 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
545 /* remove pending operations */
546 for( o = slap_op_pop( &c->c_pending_ops );
548 o = slap_op_pop( &c->c_pending_ops ) )
554 void connection_closing( Connection *c )
556 assert( connections != NULL );
558 assert( c->c_struct_state == SLAP_C_USED );
559 assert( c->c_conn_state != SLAP_C_INVALID );
561 /* c_mutex must be locked by caller */
563 if( c->c_conn_state != SLAP_C_CLOSING ) {
565 Debug( LDAP_DEBUG_TRACE,
566 "connection_closing: readying conn=%ld sd=%d for close.\n",
567 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
569 /* update state to closing */
570 c->c_conn_state = SLAP_C_CLOSING;
572 /* don't listen on this port anymore */
573 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
575 /* shutdown I/O -- not yet implemented */
577 /* abandon active operations */
578 connection_abandon( c );
580 /* wake write blocked operations */
581 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
582 ldap_pvt_thread_cond_signal( &c->c_write_cv );
586 static void connection_close( Connection *c )
588 assert( connections != NULL );
590 assert( c->c_struct_state == SLAP_C_USED );
591 assert( c->c_conn_state == SLAP_C_CLOSING );
593 /* note: connections_mutex and c_mutex should be locked by caller */
595 if( c->c_ops != NULL ) {
596 Debug( LDAP_DEBUG_TRACE,
597 "connection_close: deferring conn=%ld sd=%d.\n",
598 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
603 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
604 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
606 connection_destroy( c );
609 unsigned long connections_nextid(void)
612 assert( connections != NULL );
614 ldap_pvt_thread_mutex_lock( &connections_mutex );
618 ldap_pvt_thread_mutex_unlock( &connections_mutex );
623 Connection* connection_first( ber_socket_t *index )
625 assert( connections != NULL );
626 assert( index != NULL );
628 ldap_pvt_thread_mutex_lock( &connections_mutex );
632 return connection_next(NULL, index);
635 Connection* connection_next( Connection *c, ber_socket_t *index )
637 assert( connections != NULL );
638 assert( index != NULL );
639 assert( *index <= dtblsize );
642 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
647 for(; *index < dtblsize; (*index)++) {
648 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
649 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
657 if( connections[*index].c_struct_state == SLAP_C_USED ) {
658 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
659 c = &connections[(*index)++];
663 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
664 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
668 ldap_pvt_thread_mutex_lock( &c->c_mutex );
674 void connection_done( Connection *c )
676 assert( connections != NULL );
679 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
682 ldap_pvt_thread_mutex_unlock( &connections_mutex );
686 * connection_activity - handle the request operation op on connection
687 * conn. This routine figures out what kind of operation it is and
688 * calls the appropriate stub to handle it.
692 connection_operation( void *arg_v )
695 struct co_arg *arg = arg_v;
696 ber_tag_t tag = arg->co_op->o_tag;
697 Connection *conn = arg->co_conn;
699 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
701 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
705 rc = do_bind( conn, arg->co_op );
708 case LDAP_REQ_UNBIND:
709 rc = do_unbind( conn, arg->co_op );
713 rc = do_add( conn, arg->co_op );
716 case LDAP_REQ_DELETE:
717 rc = do_delete( conn, arg->co_op );
720 case LDAP_REQ_MODRDN:
721 rc = do_modrdn( conn, arg->co_op );
724 case LDAP_REQ_MODIFY:
725 rc = do_modify( conn, arg->co_op );
728 case LDAP_REQ_COMPARE:
729 rc = do_compare( conn, arg->co_op );
732 case LDAP_REQ_SEARCH:
733 rc = do_search( conn, arg->co_op );
736 case LDAP_REQ_ABANDON:
737 rc = do_abandon( conn, arg->co_op );
740 case LDAP_REQ_EXTENDED:
741 rc = do_extended( conn, arg->co_op );
745 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
747 arg->co_op->o_tag = LBER_ERROR;
748 send_ldap_disconnect( conn, arg->co_op,
749 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
754 if( rc == -1 ) tag = LBER_ERROR;
756 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
758 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
760 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
762 conn->c_n_ops_executing--;
763 conn->c_n_ops_completed++;
765 slap_op_remove( &conn->c_ops, arg->co_op );
766 slap_op_free( arg->co_op );
769 free( (char *) arg );
774 case LDAP_REQ_UNBIND:
775 /* c_mutex is locked */
776 connection_closing( conn );
780 if( conn->c_conn_state == SLAP_C_BINDING) {
781 conn->c_conn_state = SLAP_C_ACTIVE;
783 conn->c_bind_in_progress = ( rc == LDAP_SASL_BIND_IN_PROGRESS );
786 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
788 if( active_threads < 1 ) {
789 ldap_pvt_thread_cond_signal(&active_threads_cond);
791 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
793 connection_resched( conn );
795 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
800 int connection_read(ber_socket_t s)
804 assert( connections != NULL );
806 ldap_pvt_thread_mutex_lock( &connections_mutex );
808 /* get (locked) connection */
809 c = connection_get( s );
812 Debug( LDAP_DEBUG_ANY,
813 "connection_read(%ld): no connection!\n",
818 ldap_pvt_thread_mutex_unlock( &connections_mutex );
824 if( c->c_conn_state == SLAP_C_CLOSING ) {
825 Debug( LDAP_DEBUG_TRACE,
826 "connection_read(%d): closing, ignoring input for id=%ld\n",
829 connection_return( c );
830 ldap_pvt_thread_mutex_unlock( &connections_mutex );
834 Debug( LDAP_DEBUG_TRACE,
835 "connection_read(%d): checking for input on id=%ld\n",
839 if ( c->c_is_tls && c->c_needs_tls_accept ) {
840 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
842 Debug( LDAP_DEBUG_TRACE,
843 "connection_read(%d): TLS accept error error=%d id=%ld, closing.\n",
844 s, rc, c->c_connid );
846 c->c_needs_tls_accept = 0;
847 /* connections_mutex and c_mutex are locked */
848 connection_closing( c );
849 connection_close( c );
850 } else if ( rc == 0 ) {
851 c->c_needs_tls_accept = 0;
853 connection_return( c );
854 ldap_pvt_thread_mutex_unlock( &connections_mutex );
859 #define CONNECTION_INPUT_LOOP 1
861 #ifdef DATA_READY_LOOP
862 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
863 #elif CONNECTION_INPUT_LOOP
867 /* How do we do this without getting into a busy loop ? */
868 rc = connection_input( c );
872 Debug( LDAP_DEBUG_TRACE,
873 "connection_read(%d): input error=%d id=%ld, closing.\n",
874 s, rc, c->c_connid );
876 /* connections_mutex and c_mutex are locked */
877 connection_closing( c );
878 connection_close( c );
881 if ( ber_pvt_sb_needs_read( c->c_sb ) )
882 slapd_set_read( s, 1 );
883 if ( ber_pvt_sb_needs_write( c->c_sb ) )
884 slapd_set_write( s, 1 );
885 connection_return( c );
886 ldap_pvt_thread_mutex_unlock( &connections_mutex );
901 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
903 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
908 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
909 != LDAP_TAG_MESSAGE )
913 Debug( LDAP_DEBUG_TRACE,
914 "ber_get_next on fd %d failed errno %d (%s)\n",
915 ber_pvt_sb_get_desc( conn->c_sb ), err,
916 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
917 Debug( LDAP_DEBUG_TRACE,
918 "\t*** got %ld of %lu so far\n",
919 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
920 conn->c_currentber->ber_len, 0 );
922 if ( err != EWOULDBLOCK && err != EAGAIN ) {
923 /* log, close and send error */
924 ber_free( conn->c_currentber, 1 );
925 conn->c_currentber = NULL;
932 ber = conn->c_currentber;
933 conn->c_currentber = NULL;
935 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
936 /* log, close and send error */
937 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
943 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
944 /* log, close and send error */
945 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
952 if(tag == LDAP_REQ_BIND) {
953 /* immediately abandon all exiting operations upon BIND */
954 connection_abandon( conn );
957 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
959 if ( conn->c_conn_state == SLAP_C_BINDING
960 || conn->c_conn_state == SLAP_C_CLOSING )
962 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
963 conn->c_n_ops_pending++;
964 slap_op_add( &conn->c_pending_ops, op );
967 conn->c_n_ops_executing++;
968 connection_op_activate( conn, op );
972 if ( conn->c_struct_state != SLAP_C_USED ) {
973 /* connection must have got closed underneath us */
977 assert( conn->c_struct_state == SLAP_C_USED );
983 connection_resched( Connection *conn )
987 if( conn->c_conn_state == SLAP_C_CLOSING ) {
988 Debug( LDAP_DEBUG_TRACE,
989 "connection_resched: attempting closing conn=%ld sd=%d.\n",
990 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
992 connection_close( conn );
996 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
997 /* other states need different handling */
1001 for( op = slap_op_pop( &conn->c_pending_ops );
1003 op = slap_op_pop( &conn->c_pending_ops ) )
1005 /* pending operations should not be marked for abandonment */
1006 assert(!op->o_abandon);
1008 conn->c_n_ops_pending--;
1009 conn->c_n_ops_executing++;
1011 connection_op_activate( conn, op );
1013 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1020 static int connection_op_activate( Connection *conn, Operation *op )
1025 ber_tag_t tag = op->o_tag;
1027 if(tag == LDAP_REQ_BIND) {
1028 conn->c_conn_state = SLAP_C_BINDING;
1031 if ( conn->c_dn != NULL ) {
1032 tmpdn = ch_strdup( conn->c_dn );
1037 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1038 arg->co_conn = conn;
1041 arg->co_op->o_bind_in_progress = conn->c_bind_in_progress;
1043 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
1044 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1045 (void) dn_normalize_case( arg->co_op->o_ndn );
1047 arg->co_op->o_protocol = conn->c_protocol;
1048 arg->co_op->o_connid = conn->c_connid;
1050 arg->co_op->o_authtype = conn->c_authtype;
1051 arg->co_op->o_authmech = conn->c_authmech != NULL
1052 ? ch_strdup( conn->c_authmech ) : NULL;
1054 slap_op_add( &conn->c_ops, arg->co_op );
1056 if( tmpdn != NULL ) {
1060 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
1062 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
1064 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
1065 connection_operation, (void *) arg );
1067 if ( status != 0 ) {
1068 Debug( LDAP_DEBUG_ANY,
1069 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
1071 /* should move op to pending list */
1077 int connection_write(ber_socket_t s)
1080 assert( connections != NULL );
1082 ldap_pvt_thread_mutex_lock( &connections_mutex );
1084 c = connection_get( s );
1086 slapd_clr_write( s, 0);
1089 Debug( LDAP_DEBUG_ANY,
1090 "connection_write(%ld): no connection!\n",
1093 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1099 Debug( LDAP_DEBUG_TRACE,
1100 "connection_write(%d): waking output for id=%ld\n",
1101 s, c->c_connid, 0 );
1103 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1105 if ( ber_pvt_sb_needs_read( c->c_sb ) )
1106 slapd_set_read( s, 1 );
1107 if ( ber_pvt_sb_needs_write( c->c_sb ) )
1108 slapd_set_write( s, 1 );
1109 connection_return( c );
1110 ldap_pvt_thread_mutex_unlock( &connections_mutex );