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 ber_sockbuf_free( connections[i].c_sb );
119 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
120 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
121 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
128 ldap_pvt_thread_mutex_destroy( &connections_mutex );
133 * shutdown all connections
135 int connections_shutdown(void)
139 ldap_pvt_thread_mutex_lock( &connections_mutex );
141 for ( i = 0; i < dtblsize; i++ ) {
142 if( connections[i].c_struct_state != SLAP_C_USED ) {
146 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
148 /* connections_mutex and c_mutex are locked */
149 connection_closing( &connections[i] );
150 connection_close( &connections[i] );
152 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
155 ldap_pvt_thread_mutex_unlock( &connections_mutex );
161 * Timeout idle connections.
163 int connections_timeout_idle(time_t now)
169 ldap_pvt_thread_mutex_lock( &connections_mutex );
171 for( c = connection_first( &connindex );
173 c = connection_next( c, &connindex ) )
175 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
177 connection_closing( c );
178 connection_close( c );
182 connection_done( c );
184 ldap_pvt_thread_mutex_unlock( &connections_mutex );
189 static Connection* connection_get( ber_socket_t s )
191 /* connections_mutex should be locked by caller */
195 Debug( LDAP_DEBUG_ARGS,
196 "connection_get(%ld)\n",
199 assert( connections != NULL );
201 if(s == AC_SOCKET_INVALID) {
208 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
215 for(i=0; i<dtblsize; i++) {
216 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
217 assert( connections[i].c_conn_state == SLAP_C_INVALID );
218 assert( connections[i].c_sb == 0 );
222 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
223 assert( connections[i].c_conn_state == SLAP_C_INVALID );
224 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
228 /* state can actually change from used -> unused by resched,
229 * so don't assert details here.
232 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
241 ldap_pvt_thread_mutex_lock( &c->c_mutex );
243 if( c->c_struct_state != SLAP_C_USED ) {
244 /* connection must have been closed due to resched */
246 assert( c->c_conn_state == SLAP_C_INVALID );
247 assert( !ber_pvt_sb_in_use( c->c_sb ) );
249 Debug( LDAP_DEBUG_TRACE,
250 "connection_get(%d): connection not used\n",
253 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
257 Debug( LDAP_DEBUG_TRACE,
258 "connection_get(%d): got connid=%ld\n",
263 assert( c->c_struct_state == SLAP_C_USED );
264 assert( c->c_conn_state != SLAP_C_INVALID );
265 assert( ber_pvt_sb_in_use( c->c_sb ) );
267 c->c_activitytime = slap_get_time();
273 static void connection_return( Connection *c )
275 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
278 long connection_init(
282 const char* peername,
283 const char* sockname,
289 assert( connections != NULL );
291 assert( dnsname != NULL );
292 assert( peername != NULL );
293 assert( sockname != NULL );
299 if( s == AC_SOCKET_INVALID ) {
300 Debug( LDAP_DEBUG_ANY,
301 "connection_init(%ld): invalid.\n",
308 assert( s < dtblsize );
311 ldap_pvt_thread_mutex_lock( &connections_mutex );
322 for( i=0; i < dtblsize; i++) {
323 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
324 assert( connections[i].c_sb == 0 );
329 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
330 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
335 assert( connections[i].c_struct_state == SLAP_C_USED );
336 assert( connections[i].c_conn_state != SLAP_C_INVALID );
337 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
341 Debug( LDAP_DEBUG_ANY,
342 "connection_init(%d): connection table full (%d/%d)\n",
344 ldap_pvt_thread_mutex_unlock( &connections_mutex );
352 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
356 c->c_listener_url = NULL;
357 c->c_peer_domain = NULL;
358 c->c_peer_name = NULL;
359 c->c_sock_name = NULL;
362 c->c_pending_ops = NULL;
363 c->c_authmech = NULL;
364 c->c_authstate = NULL;
366 #ifdef HAVE_CYRUS_SASL
367 c->c_sasl_context = NULL;
370 c->c_sb = ber_sockbuf_alloc( );
371 c->c_currentber = NULL;
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 );
397 assert( c->c_currentber == NULL );
399 c->c_listener_url = ch_strdup( url );
400 c->c_peer_domain = ch_strdup( dnsname );
401 c->c_peer_name = ch_strdup( peername );
402 c->c_sock_name = ch_strdup( sockname );
404 c->c_n_ops_received = 0;
405 c->c_n_ops_executing = 0;
406 c->c_n_ops_pending = 0;
407 c->c_n_ops_completed = 0;
413 /* assume LDAPv3 until bind */
414 c->c_protocol = LDAP_VERSION3;
416 c->c_activitytime = c->c_starttime = slap_get_time();
418 ber_pvt_sb_set_desc( c->c_sb, s );
419 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
421 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
422 Debug( LDAP_DEBUG_ANY,
423 "connection_init(%d, %s): set nonblocking failed\n",
424 s, c->c_peer_name,0 );
427 id = c->c_connid = conn_nextid++;
429 c->c_conn_state = SLAP_C_INACTIVE;
430 c->c_struct_state = SLAP_C_USED;
435 c->c_needs_tls_accept = 1;
439 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
440 ldap_pvt_thread_mutex_unlock( &connections_mutex );
442 backend_connection_init(c);
448 connection_destroy( Connection *c )
450 /* note: connections_mutex should be locked by caller */
452 assert( connections != NULL );
454 assert( c->c_struct_state != SLAP_C_UNUSED );
455 assert( c->c_conn_state != SLAP_C_INVALID );
456 assert( c->c_ops == NULL );
458 backend_connection_destroy(c);
463 c->c_activitytime = c->c_starttime = 0;
465 if(c->c_dn != NULL) {
469 if(c->c_cdn != NULL) {
473 if(c->c_listener_url != NULL) {
474 free(c->c_listener_url);
475 c->c_listener_url = NULL;
477 if(c->c_peer_domain != NULL) {
478 free(c->c_peer_domain);
479 c->c_peer_domain = NULL;
481 if(c->c_peer_name != NULL) {
482 free(c->c_peer_name);
483 c->c_peer_name = NULL;
485 if(c->c_sock_name != NULL) {
486 free(c->c_sock_name);
487 c->c_sock_name = NULL;
489 if(c->c_authmech != NULL ) {
491 c->c_authmech = NULL;
493 if(c->c_authstate != NULL ) {
494 free(c->c_authstate);
495 c->c_authstate = NULL;
498 #ifdef HAVE_CYRUS_SASL
499 if(c->c_sasl_context != NULL ) {
500 sasl_dispose( &c->c_sasl_context );
501 c->c_sasl_context = NULL;
505 if ( c->c_currentber != NULL ) {
506 ber_free( c->c_currentber, 1 );
507 c->c_currentber = NULL;
510 if ( ber_pvt_sb_in_use(c->c_sb) ) {
511 int sd = ber_pvt_sb_get_desc(c->c_sb);
513 slapd_remove( sd, 0 );
514 ber_pvt_sb_close( c->c_sb );
516 Statslog( LDAP_DEBUG_STATS,
517 "conn=%ld fd=%d closed\n",
518 c->c_connid, sd, 0, 0, 0 );
521 ber_pvt_sb_destroy( c->c_sb );
523 c->c_conn_state = SLAP_C_INVALID;
524 c->c_struct_state = SLAP_C_UNUSED;
527 int connection_state_closing( Connection *c )
529 /* c_mutex must be locked by caller */
533 assert( c->c_struct_state == SLAP_C_USED );
535 state = c->c_conn_state;
537 assert( state != SLAP_C_INVALID );
539 return state == SLAP_C_CLOSING;
542 static void connection_abandon( Connection *c )
544 /* c_mutex must be locked by caller */
548 for( o = c->c_ops; o != NULL; o = o->o_next ) {
549 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
551 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
554 /* remove pending operations */
555 for( o = slap_op_pop( &c->c_pending_ops );
557 o = slap_op_pop( &c->c_pending_ops ) )
563 void connection_closing( Connection *c )
565 assert( connections != NULL );
567 assert( c->c_struct_state == SLAP_C_USED );
568 assert( c->c_conn_state != SLAP_C_INVALID );
570 /* c_mutex must be locked by caller */
572 if( c->c_conn_state != SLAP_C_CLOSING ) {
574 Debug( LDAP_DEBUG_TRACE,
575 "connection_closing: readying conn=%ld sd=%d for close\n",
576 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
578 /* update state to closing */
579 c->c_conn_state = SLAP_C_CLOSING;
581 /* don't listen on this port anymore */
582 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
584 /* shutdown I/O -- not yet implemented */
586 /* abandon active operations */
587 connection_abandon( c );
589 /* wake write blocked operations */
590 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
591 ldap_pvt_thread_cond_signal( &c->c_write_cv );
595 static void connection_close( Connection *c )
597 assert( connections != NULL );
599 assert( c->c_struct_state == SLAP_C_USED );
600 assert( c->c_conn_state == SLAP_C_CLOSING );
602 /* note: connections_mutex and c_mutex should be locked by caller */
604 if( c->c_ops != NULL ) {
605 Debug( LDAP_DEBUG_TRACE,
606 "connection_close: deferring conn=%ld sd=%d\n",
607 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
612 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
613 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
615 connection_destroy( c );
618 unsigned long connections_nextid(void)
621 assert( connections != NULL );
623 ldap_pvt_thread_mutex_lock( &connections_mutex );
627 ldap_pvt_thread_mutex_unlock( &connections_mutex );
632 Connection* connection_first( ber_socket_t *index )
634 assert( connections != NULL );
635 assert( index != NULL );
637 ldap_pvt_thread_mutex_lock( &connections_mutex );
641 return connection_next(NULL, index);
644 Connection* connection_next( Connection *c, ber_socket_t *index )
646 assert( connections != NULL );
647 assert( index != NULL );
648 assert( *index <= dtblsize );
651 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
656 for(; *index < dtblsize; (*index)++) {
657 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
658 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
666 if( connections[*index].c_struct_state == SLAP_C_USED ) {
667 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
668 c = &connections[(*index)++];
672 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
673 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
677 ldap_pvt_thread_mutex_lock( &c->c_mutex );
683 void connection_done( Connection *c )
685 assert( connections != NULL );
688 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
691 ldap_pvt_thread_mutex_unlock( &connections_mutex );
695 * connection_activity - handle the request operation op on connection
696 * conn. This routine figures out what kind of operation it is and
697 * calls the appropriate stub to handle it.
701 connection_operation( void *arg_v )
704 struct co_arg *arg = arg_v;
705 ber_tag_t tag = arg->co_op->o_tag;
706 Connection *conn = arg->co_conn;
708 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
710 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
714 rc = do_bind( conn, arg->co_op );
717 case LDAP_REQ_UNBIND:
718 rc = do_unbind( conn, arg->co_op );
722 rc = do_add( conn, arg->co_op );
725 case LDAP_REQ_DELETE:
726 rc = do_delete( conn, arg->co_op );
729 case LDAP_REQ_MODRDN:
730 rc = do_modrdn( conn, arg->co_op );
733 case LDAP_REQ_MODIFY:
734 rc = do_modify( conn, arg->co_op );
737 case LDAP_REQ_COMPARE:
738 rc = do_compare( conn, arg->co_op );
741 case LDAP_REQ_SEARCH:
742 rc = do_search( conn, arg->co_op );
745 case LDAP_REQ_ABANDON:
746 rc = do_abandon( conn, arg->co_op );
749 case LDAP_REQ_EXTENDED:
750 rc = do_extended( conn, arg->co_op );
754 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
756 arg->co_op->o_tag = LBER_ERROR;
757 send_ldap_disconnect( conn, arg->co_op,
758 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
763 if( rc == -1 ) tag = LBER_ERROR;
765 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
767 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
769 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
771 conn->c_n_ops_executing--;
772 conn->c_n_ops_completed++;
774 slap_op_remove( &conn->c_ops, arg->co_op );
775 slap_op_free( arg->co_op );
778 free( (char *) arg );
783 case LDAP_REQ_UNBIND:
784 /* c_mutex is locked */
785 connection_closing( conn );
789 if( conn->c_conn_state == SLAP_C_BINDING) {
790 conn->c_conn_state = SLAP_C_ACTIVE;
792 conn->c_bind_in_progress = ( rc == LDAP_SASL_BIND_IN_PROGRESS );
795 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
797 if( active_threads < 1 ) {
798 ldap_pvt_thread_cond_signal(&active_threads_cond);
800 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
802 connection_resched( conn );
804 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
809 int connection_read(ber_socket_t s)
813 assert( connections != NULL );
815 ldap_pvt_thread_mutex_lock( &connections_mutex );
817 /* get (locked) connection */
818 c = connection_get( s );
821 Debug( LDAP_DEBUG_ANY,
822 "connection_read(%ld): no connection!\n",
827 ldap_pvt_thread_mutex_unlock( &connections_mutex );
833 if( c->c_conn_state == SLAP_C_CLOSING ) {
834 Debug( LDAP_DEBUG_TRACE,
835 "connection_read(%d): closing, ignoring input for id=%ld\n",
838 connection_return( c );
839 ldap_pvt_thread_mutex_unlock( &connections_mutex );
843 Debug( LDAP_DEBUG_TRACE,
844 "connection_read(%d): checking for input on id=%ld\n",
848 if ( c->c_is_tls && c->c_needs_tls_accept ) {
849 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
851 Debug( LDAP_DEBUG_TRACE,
852 "connection_read(%d): TLS accept error error=%d id=%ld, closing\n",
853 s, rc, c->c_connid );
855 c->c_needs_tls_accept = 0;
856 /* connections_mutex and c_mutex are locked */
857 connection_closing( c );
858 connection_close( c );
859 } else if ( rc == 0 ) {
860 c->c_needs_tls_accept = 0;
862 connection_return( c );
863 ldap_pvt_thread_mutex_unlock( &connections_mutex );
868 #define CONNECTION_INPUT_LOOP 1
870 #ifdef DATA_READY_LOOP
871 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
872 #elif CONNECTION_INPUT_LOOP
876 /* How do we do this without getting into a busy loop ? */
877 rc = connection_input( c );
881 Debug( LDAP_DEBUG_TRACE,
882 "connection_read(%d): input error=%d id=%ld, closing.\n",
883 s, rc, c->c_connid );
885 /* connections_mutex and c_mutex are locked */
886 connection_closing( c );
887 connection_close( c );
890 if ( ber_pvt_sb_needs_read( c->c_sb ) )
891 slapd_set_read( s, 1 );
892 if ( ber_pvt_sb_needs_write( c->c_sb ) )
893 slapd_set_write( s, 1 );
894 connection_return( c );
895 ldap_pvt_thread_mutex_unlock( &connections_mutex );
910 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
912 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
917 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
918 != LDAP_TAG_MESSAGE )
922 Debug( LDAP_DEBUG_TRACE,
923 "ber_get_next on fd %d failed errno=%d (%s)\n",
924 ber_pvt_sb_get_desc( conn->c_sb ), err, STRERROR(err) );
925 Debug( LDAP_DEBUG_TRACE,
926 "\t*** got %ld of %lu so far\n",
927 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
928 conn->c_currentber->ber_len, 0 );
930 if ( err != EWOULDBLOCK && err != EAGAIN ) {
931 /* log, close and send error */
932 ber_free( conn->c_currentber, 1 );
933 conn->c_currentber = NULL;
940 ber = conn->c_currentber;
941 conn->c_currentber = NULL;
943 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
944 /* log, close and send error */
945 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
951 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
952 /* log, close and send error */
953 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
960 if(tag == LDAP_REQ_BIND) {
961 /* immediately abandon all exiting operations upon BIND */
962 connection_abandon( conn );
965 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
967 if ( conn->c_conn_state == SLAP_C_BINDING
968 || conn->c_conn_state == SLAP_C_CLOSING )
970 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
971 conn->c_n_ops_pending++;
972 slap_op_add( &conn->c_pending_ops, op );
975 conn->c_n_ops_executing++;
976 connection_op_activate( conn, op );
980 if ( conn->c_struct_state != SLAP_C_USED ) {
981 /* connection must have got closed underneath us */
985 assert( conn->c_struct_state == SLAP_C_USED );
991 connection_resched( Connection *conn )
995 if( conn->c_conn_state == SLAP_C_CLOSING ) {
996 Debug( LDAP_DEBUG_TRACE,
997 "connection_resched: attempting closing conn=%ld sd=%d\n",
998 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
1000 connection_close( conn );
1004 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1005 /* other states need different handling */
1009 for( op = slap_op_pop( &conn->c_pending_ops );
1011 op = slap_op_pop( &conn->c_pending_ops ) )
1013 /* pending operations should not be marked for abandonment */
1014 assert(!op->o_abandon);
1016 conn->c_n_ops_pending--;
1017 conn->c_n_ops_executing++;
1019 connection_op_activate( conn, op );
1021 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1028 static int connection_op_activate( Connection *conn, Operation *op )
1033 ber_tag_t tag = op->o_tag;
1035 if(tag == LDAP_REQ_BIND) {
1036 conn->c_conn_state = SLAP_C_BINDING;
1039 if ( conn->c_dn != NULL ) {
1040 tmpdn = ch_strdup( conn->c_dn );
1045 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1046 arg->co_conn = conn;
1049 arg->co_op->o_bind_in_progress = conn->c_bind_in_progress;
1051 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
1052 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1053 (void) dn_normalize_case( arg->co_op->o_ndn );
1055 arg->co_op->o_protocol = conn->c_protocol;
1056 arg->co_op->o_connid = conn->c_connid;
1058 arg->co_op->o_authtype = conn->c_authtype;
1059 arg->co_op->o_authmech = conn->c_authmech != NULL
1060 ? ch_strdup( conn->c_authmech ) : NULL;
1062 slap_op_add( &conn->c_ops, arg->co_op );
1064 if( tmpdn != NULL ) {
1068 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
1070 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
1072 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
1073 connection_operation, (void *) arg );
1075 if ( status != 0 ) {
1076 Debug( LDAP_DEBUG_ANY,
1077 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
1079 /* should move op to pending list */
1085 int connection_write(ber_socket_t s)
1088 assert( connections != NULL );
1090 ldap_pvt_thread_mutex_lock( &connections_mutex );
1092 c = connection_get( s );
1094 slapd_clr_write( s, 0);
1097 Debug( LDAP_DEBUG_ANY,
1098 "connection_write(%ld): no connection!\n",
1101 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1107 Debug( LDAP_DEBUG_TRACE,
1108 "connection_write(%d): waking output for id=%ld\n",
1109 s, c->c_connid, 0 );
1111 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1113 if ( ber_pvt_sb_needs_read( c->c_sb ) )
1114 slapd_set_read( s, 1 );
1115 if ( ber_pvt_sb_needs_write( c->c_sb ) )
1116 slapd_set_write( s, 1 );
1117 connection_return( c );
1118 ldap_pvt_thread_mutex_unlock( &connections_mutex );