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 */
39 connection_state2str( int state )
42 case SLAP_C_INVALID: return "!";
43 case SLAP_C_INACTIVE: return "|";
44 case SLAP_C_ACTIVE: return "";
45 case SLAP_C_BINDING: return "B";
46 case SLAP_C_CLOSING: return "C";
52 static Connection* connection_get( ber_socket_t s );
54 static int connection_input( Connection *c );
55 static void connection_close( Connection *c );
57 static int connection_op_activate( Connection *conn, Operation *op );
58 static int connection_resched( Connection *conn );
59 static void connection_abandon( Connection *conn );
60 static void connection_destroy( Connection *c );
68 * Initialize connection management infrastructure.
70 int connections_init(void)
72 assert( connections == NULL );
74 if( connections != NULL) {
75 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
80 /* should check return of every call */
81 ldap_pvt_thread_mutex_init( &connections_mutex );
83 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
85 if( connections == NULL ) {
86 Debug( LDAP_DEBUG_ANY,
87 "connections_init: allocation (%d*%ld) of connection array failed\n",
88 dtblsize, (long) sizeof(Connection), 0 );
92 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
93 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
96 * per entry initialization of the Connection array initialization
97 * will be done by connection_init()
104 * Destroy connection management infrastructure.
106 int connections_destroy(void)
110 /* should check return of every call */
112 if( connections == NULL) {
113 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
118 for ( i = 0; i < dtblsize; i++ ) {
119 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
120 ber_sockbuf_free( connections[i].c_sb );
121 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
122 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
123 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
130 ldap_pvt_thread_mutex_destroy( &connections_mutex );
135 * shutdown all connections
137 int connections_shutdown(void)
141 ldap_pvt_thread_mutex_lock( &connections_mutex );
143 for ( i = 0; i < dtblsize; i++ ) {
144 if( connections[i].c_struct_state != SLAP_C_USED ) {
148 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
150 /* connections_mutex and c_mutex are locked */
151 connection_closing( &connections[i] );
152 connection_close( &connections[i] );
154 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
157 ldap_pvt_thread_mutex_unlock( &connections_mutex );
163 * Timeout idle connections.
165 int connections_timeout_idle(time_t now)
171 ldap_pvt_thread_mutex_lock( &connections_mutex );
173 for( c = connection_first( &connindex );
175 c = connection_next( c, &connindex ) )
177 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
179 connection_closing( c );
180 connection_close( c );
184 connection_done( c );
186 ldap_pvt_thread_mutex_unlock( &connections_mutex );
191 static Connection* connection_get( ber_socket_t s )
193 /* connections_mutex should be locked by caller */
197 Debug( LDAP_DEBUG_ARGS,
198 "connection_get(%ld)\n",
201 assert( connections != NULL );
203 if(s == AC_SOCKET_INVALID) {
210 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
217 for(i=0; i<dtblsize; i++) {
218 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
219 assert( connections[i].c_conn_state == SLAP_C_INVALID );
220 assert( connections[i].c_sb == 0 );
224 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
225 assert( connections[i].c_conn_state == SLAP_C_INVALID );
226 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
230 /* state can actually change from used -> unused by resched,
231 * so don't assert details here.
234 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
243 ldap_pvt_thread_mutex_lock( &c->c_mutex );
245 if( c->c_struct_state != SLAP_C_USED ) {
246 /* connection must have been closed due to resched */
248 assert( c->c_conn_state == SLAP_C_INVALID );
249 assert( !ber_pvt_sb_in_use( c->c_sb ) );
251 Debug( LDAP_DEBUG_TRACE,
252 "connection_get(%d): connection not used\n",
255 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
259 Debug( LDAP_DEBUG_TRACE,
260 "connection_get(%d): got connid=%ld\n",
265 assert( c->c_struct_state == SLAP_C_USED );
266 assert( c->c_conn_state != SLAP_C_INVALID );
267 assert( ber_pvt_sb_in_use( c->c_sb ) );
269 c->c_activitytime = slap_get_time();
275 static void connection_return( Connection *c )
277 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
280 long connection_init(
284 const char* peername,
285 const char* sockname,
291 assert( connections != NULL );
293 assert( dnsname != NULL );
294 assert( peername != NULL );
295 assert( sockname != NULL );
301 if( s == AC_SOCKET_INVALID ) {
302 Debug( LDAP_DEBUG_ANY,
303 "connection_init(%ld): invalid.\n",
310 assert( s < dtblsize );
313 ldap_pvt_thread_mutex_lock( &connections_mutex );
324 for( i=0; i < dtblsize; i++) {
325 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
326 assert( connections[i].c_sb == 0 );
331 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
332 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
337 assert( connections[i].c_struct_state == SLAP_C_USED );
338 assert( connections[i].c_conn_state != SLAP_C_INVALID );
339 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
343 Debug( LDAP_DEBUG_ANY,
344 "connection_init(%d): connection table full (%d/%d)\n",
346 ldap_pvt_thread_mutex_unlock( &connections_mutex );
354 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
358 c->c_listener_url = NULL;
359 c->c_peer_domain = NULL;
360 c->c_peer_name = NULL;
361 c->c_sock_name = NULL;
364 c->c_pending_ops = NULL;
365 c->c_authmech = NULL;
366 c->c_authstate = NULL;
368 #ifdef HAVE_CYRUS_SASL
369 c->c_sasl_context = NULL;
372 c->c_sb = ber_sockbuf_alloc( );
373 c->c_currentber = NULL;
375 /* should check status of thread calls */
376 ldap_pvt_thread_mutex_init( &c->c_mutex );
377 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
378 ldap_pvt_thread_cond_init( &c->c_write_cv );
380 c->c_struct_state = SLAP_C_UNUSED;
383 ldap_pvt_thread_mutex_lock( &c->c_mutex );
385 assert( c->c_struct_state == SLAP_C_UNUSED );
386 assert( c->c_dn == NULL );
387 assert( c->c_cdn == NULL );
388 assert( c->c_listener_url == NULL );
389 assert( c->c_peer_domain == NULL );
390 assert( c->c_peer_name == NULL );
391 assert( c->c_sock_name == NULL );
392 assert( c->c_ops == NULL );
393 assert( c->c_pending_ops == NULL );
394 assert( c->c_authmech == NULL );
395 assert( c->c_authstate == NULL );
396 #ifdef HAVE_CYRUS_SASL
397 assert( c->c_sasl_context == NULL );
399 assert( c->c_currentber == NULL );
401 c->c_listener_url = ch_strdup( url );
402 c->c_peer_domain = ch_strdup( dnsname );
403 c->c_peer_name = ch_strdup( peername );
404 c->c_sock_name = ch_strdup( sockname );
406 c->c_n_ops_received = 0;
407 c->c_n_ops_executing = 0;
408 c->c_n_ops_pending = 0;
409 c->c_n_ops_completed = 0;
415 /* assume LDAPv3 until bind */
416 c->c_protocol = LDAP_VERSION3;
418 c->c_activitytime = c->c_starttime = slap_get_time();
420 ber_pvt_sb_set_desc( c->c_sb, s );
421 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
423 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
424 Debug( LDAP_DEBUG_ANY,
425 "connection_init(%d, %s): set nonblocking failed\n",
426 s, c->c_peer_name,0 );
429 id = c->c_connid = conn_nextid++;
431 c->c_conn_state = SLAP_C_INACTIVE;
432 c->c_struct_state = SLAP_C_USED;
437 c->c_needs_tls_accept = 1;
441 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
442 ldap_pvt_thread_mutex_unlock( &connections_mutex );
444 backend_connection_init(c);
450 connection_destroy( Connection *c )
452 /* note: connections_mutex should be locked by caller */
454 assert( connections != NULL );
456 assert( c->c_struct_state != SLAP_C_UNUSED );
457 assert( c->c_conn_state != SLAP_C_INVALID );
458 assert( c->c_ops == NULL );
460 backend_connection_destroy(c);
465 c->c_activitytime = c->c_starttime = 0;
467 if(c->c_dn != NULL) {
471 if(c->c_cdn != NULL) {
475 if(c->c_listener_url != NULL) {
476 free(c->c_listener_url);
477 c->c_listener_url = NULL;
479 if(c->c_peer_domain != NULL) {
480 free(c->c_peer_domain);
481 c->c_peer_domain = NULL;
483 if(c->c_peer_name != NULL) {
484 free(c->c_peer_name);
485 c->c_peer_name = NULL;
487 if(c->c_sock_name != NULL) {
488 free(c->c_sock_name);
489 c->c_sock_name = NULL;
491 if(c->c_authmech != NULL ) {
493 c->c_authmech = NULL;
495 if(c->c_authstate != NULL ) {
496 free(c->c_authstate);
497 c->c_authstate = NULL;
500 #ifdef HAVE_CYRUS_SASL
501 if(c->c_sasl_context != NULL ) {
502 sasl_dispose( &c->c_sasl_context );
503 c->c_sasl_context = NULL;
507 if ( c->c_currentber != NULL ) {
508 ber_free( c->c_currentber, 1 );
509 c->c_currentber = NULL;
512 if ( ber_pvt_sb_in_use(c->c_sb) ) {
513 int sd = ber_pvt_sb_get_desc(c->c_sb);
515 slapd_remove( sd, 0 );
516 ber_pvt_sb_close( c->c_sb );
518 Statslog( LDAP_DEBUG_STATS,
519 "conn=%ld fd=%d closed\n",
520 c->c_connid, sd, 0, 0, 0 );
523 ber_pvt_sb_destroy( c->c_sb );
525 c->c_conn_state = SLAP_C_INVALID;
526 c->c_struct_state = SLAP_C_UNUSED;
529 int connection_state_closing( Connection *c )
531 /* c_mutex must be locked by caller */
535 assert( c->c_struct_state == SLAP_C_USED );
537 state = c->c_conn_state;
539 assert( state != SLAP_C_INVALID );
541 return state == SLAP_C_CLOSING;
544 static void connection_abandon( Connection *c )
546 /* c_mutex must be locked by caller */
550 for( o = c->c_ops; o != NULL; o = o->o_next ) {
551 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
553 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
556 /* remove pending operations */
557 for( o = slap_op_pop( &c->c_pending_ops );
559 o = slap_op_pop( &c->c_pending_ops ) )
565 void connection_closing( Connection *c )
567 assert( connections != NULL );
569 assert( c->c_struct_state == SLAP_C_USED );
570 assert( c->c_conn_state != SLAP_C_INVALID );
572 /* c_mutex must be locked by caller */
574 if( c->c_conn_state != SLAP_C_CLOSING ) {
576 Debug( LDAP_DEBUG_TRACE,
577 "connection_closing: readying conn=%ld sd=%d for close\n",
578 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
580 /* update state to closing */
581 c->c_conn_state = SLAP_C_CLOSING;
583 /* don't listen on this port anymore */
584 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
586 /* shutdown I/O -- not yet implemented */
588 /* abandon active operations */
589 connection_abandon( c );
591 /* wake write blocked operations */
592 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
593 ldap_pvt_thread_cond_signal( &c->c_write_cv );
597 static void connection_close( Connection *c )
599 assert( connections != NULL );
601 assert( c->c_struct_state == SLAP_C_USED );
602 assert( c->c_conn_state == SLAP_C_CLOSING );
604 /* note: connections_mutex and c_mutex should be locked by caller */
606 if( c->c_ops != NULL ) {
607 Debug( LDAP_DEBUG_TRACE,
608 "connection_close: deferring conn=%ld sd=%d\n",
609 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
614 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
615 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
617 connection_destroy( c );
620 unsigned long connections_nextid(void)
623 assert( connections != NULL );
625 ldap_pvt_thread_mutex_lock( &connections_mutex );
629 ldap_pvt_thread_mutex_unlock( &connections_mutex );
634 Connection* connection_first( ber_socket_t *index )
636 assert( connections != NULL );
637 assert( index != NULL );
639 ldap_pvt_thread_mutex_lock( &connections_mutex );
643 return connection_next(NULL, index);
646 Connection* connection_next( Connection *c, ber_socket_t *index )
648 assert( connections != NULL );
649 assert( index != NULL );
650 assert( *index <= dtblsize );
653 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
658 for(; *index < dtblsize; (*index)++) {
659 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
660 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
668 if( connections[*index].c_struct_state == SLAP_C_USED ) {
669 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
670 c = &connections[(*index)++];
674 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
675 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
679 ldap_pvt_thread_mutex_lock( &c->c_mutex );
685 void connection_done( Connection *c )
687 assert( connections != NULL );
690 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
693 ldap_pvt_thread_mutex_unlock( &connections_mutex );
697 * connection_activity - handle the request operation op on connection
698 * conn. This routine figures out what kind of operation it is and
699 * calls the appropriate stub to handle it.
703 connection_operation( void *arg_v )
706 struct co_arg *arg = arg_v;
707 ber_tag_t tag = arg->co_op->o_tag;
708 Connection *conn = arg->co_conn;
710 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
712 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
716 rc = do_bind( conn, arg->co_op );
719 case LDAP_REQ_UNBIND:
720 rc = do_unbind( conn, arg->co_op );
724 rc = do_add( conn, arg->co_op );
727 case LDAP_REQ_DELETE:
728 rc = do_delete( conn, arg->co_op );
731 case LDAP_REQ_MODRDN:
732 rc = do_modrdn( conn, arg->co_op );
735 case LDAP_REQ_MODIFY:
736 rc = do_modify( conn, arg->co_op );
739 case LDAP_REQ_COMPARE:
740 rc = do_compare( conn, arg->co_op );
743 case LDAP_REQ_SEARCH:
744 rc = do_search( conn, arg->co_op );
747 case LDAP_REQ_ABANDON:
748 rc = do_abandon( conn, arg->co_op );
751 case LDAP_REQ_EXTENDED:
752 rc = do_extended( conn, arg->co_op );
756 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
758 arg->co_op->o_tag = LBER_ERROR;
759 send_ldap_disconnect( conn, arg->co_op,
760 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
765 if( rc == -1 ) tag = LBER_ERROR;
767 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
769 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
771 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
773 conn->c_n_ops_executing--;
774 conn->c_n_ops_completed++;
776 slap_op_remove( &conn->c_ops, arg->co_op );
777 slap_op_free( arg->co_op );
780 free( (char *) arg );
785 case LDAP_REQ_UNBIND:
786 /* c_mutex is locked */
787 connection_closing( conn );
791 if( conn->c_conn_state == SLAP_C_BINDING) {
792 conn->c_conn_state = SLAP_C_ACTIVE;
794 conn->c_bind_in_progress = ( rc == LDAP_SASL_BIND_IN_PROGRESS );
797 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
799 if( active_threads < 1 ) {
800 ldap_pvt_thread_cond_signal(&active_threads_cond);
802 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
804 connection_resched( conn );
806 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
811 int connection_read(ber_socket_t s)
815 assert( connections != NULL );
817 ldap_pvt_thread_mutex_lock( &connections_mutex );
819 /* get (locked) connection */
820 c = connection_get( s );
823 Debug( LDAP_DEBUG_ANY,
824 "connection_read(%ld): no connection!\n",
829 ldap_pvt_thread_mutex_unlock( &connections_mutex );
835 if( c->c_conn_state == SLAP_C_CLOSING ) {
836 Debug( LDAP_DEBUG_TRACE,
837 "connection_read(%d): closing, ignoring input for id=%ld\n",
840 connection_return( c );
841 ldap_pvt_thread_mutex_unlock( &connections_mutex );
845 Debug( LDAP_DEBUG_TRACE,
846 "connection_read(%d): checking for input on id=%ld\n",
850 if ( c->c_is_tls && c->c_needs_tls_accept ) {
851 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
853 Debug( LDAP_DEBUG_TRACE,
854 "connection_read(%d): TLS accept error error=%d id=%ld, closing\n",
855 s, rc, c->c_connid );
857 c->c_needs_tls_accept = 0;
858 /* connections_mutex and c_mutex are locked */
859 connection_closing( c );
860 connection_close( c );
861 } else if ( rc == 0 ) {
862 c->c_needs_tls_accept = 0;
864 connection_return( c );
865 ldap_pvt_thread_mutex_unlock( &connections_mutex );
870 #define CONNECTION_INPUT_LOOP 1
872 #ifdef DATA_READY_LOOP
873 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
874 #elif CONNECTION_INPUT_LOOP
878 /* How do we do this without getting into a busy loop ? */
879 rc = connection_input( c );
883 Debug( LDAP_DEBUG_TRACE,
884 "connection_read(%d): input error=%d id=%ld, closing.\n",
885 s, rc, c->c_connid );
887 /* connections_mutex and c_mutex are locked */
888 connection_closing( c );
889 connection_close( c );
892 if ( ber_pvt_sb_needs_read( c->c_sb ) )
893 slapd_set_read( s, 1 );
894 if ( ber_pvt_sb_needs_write( c->c_sb ) )
895 slapd_set_write( s, 1 );
896 connection_return( c );
897 ldap_pvt_thread_mutex_unlock( &connections_mutex );
912 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
914 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
919 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
920 != LDAP_TAG_MESSAGE )
924 Debug( LDAP_DEBUG_TRACE,
925 "ber_get_next on fd %d failed errno=%d (%s)\n",
926 ber_pvt_sb_get_desc( conn->c_sb ), err, STRERROR(err) );
927 Debug( LDAP_DEBUG_TRACE,
928 "\t*** got %ld of %lu so far\n",
929 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
930 conn->c_currentber->ber_len, 0 );
932 if ( err != EWOULDBLOCK && err != EAGAIN ) {
933 /* log, close and send error */
934 ber_free( conn->c_currentber, 1 );
935 conn->c_currentber = NULL;
942 ber = conn->c_currentber;
943 conn->c_currentber = NULL;
945 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
946 /* log, close and send error */
947 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
953 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
954 /* log, close and send error */
955 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
962 if(tag == LDAP_REQ_BIND) {
963 /* immediately abandon all exiting operations upon BIND */
964 connection_abandon( conn );
967 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
969 if ( conn->c_conn_state == SLAP_C_BINDING
970 || conn->c_conn_state == SLAP_C_CLOSING )
972 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
973 conn->c_n_ops_pending++;
974 slap_op_add( &conn->c_pending_ops, op );
977 conn->c_n_ops_executing++;
978 connection_op_activate( conn, op );
982 if ( conn->c_struct_state != SLAP_C_USED ) {
983 /* connection must have got closed underneath us */
987 assert( conn->c_struct_state == SLAP_C_USED );
993 connection_resched( Connection *conn )
997 if( conn->c_conn_state == SLAP_C_CLOSING ) {
998 Debug( LDAP_DEBUG_TRACE,
999 "connection_resched: attempting closing conn=%ld sd=%d\n",
1000 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
1002 connection_close( conn );
1006 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1007 /* other states need different handling */
1011 for( op = slap_op_pop( &conn->c_pending_ops );
1013 op = slap_op_pop( &conn->c_pending_ops ) )
1015 /* pending operations should not be marked for abandonment */
1016 assert(!op->o_abandon);
1018 conn->c_n_ops_pending--;
1019 conn->c_n_ops_executing++;
1021 connection_op_activate( conn, op );
1023 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1030 static int connection_op_activate( Connection *conn, Operation *op )
1035 ber_tag_t tag = op->o_tag;
1037 if(tag == LDAP_REQ_BIND) {
1038 conn->c_conn_state = SLAP_C_BINDING;
1041 if ( conn->c_dn != NULL ) {
1042 tmpdn = ch_strdup( conn->c_dn );
1047 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1048 arg->co_conn = conn;
1051 arg->co_op->o_bind_in_progress = conn->c_bind_in_progress;
1053 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
1054 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1055 (void) dn_normalize_case( arg->co_op->o_ndn );
1057 arg->co_op->o_protocol = conn->c_protocol;
1058 arg->co_op->o_connid = conn->c_connid;
1060 arg->co_op->o_authtype = conn->c_authtype;
1061 arg->co_op->o_authmech = conn->c_authmech != NULL
1062 ? ch_strdup( conn->c_authmech ) : NULL;
1064 slap_op_add( &conn->c_ops, arg->co_op );
1066 if( tmpdn != NULL ) {
1070 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
1072 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
1074 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
1075 connection_operation, (void *) arg );
1077 if ( status != 0 ) {
1078 Debug( LDAP_DEBUG_ANY,
1079 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
1081 /* should move op to pending list */
1087 int connection_write(ber_socket_t s)
1090 assert( connections != NULL );
1092 ldap_pvt_thread_mutex_lock( &connections_mutex );
1094 c = connection_get( s );
1096 slapd_clr_write( s, 0);
1099 Debug( LDAP_DEBUG_ANY,
1100 "connection_write(%ld): no connection!\n",
1103 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1109 Debug( LDAP_DEBUG_TRACE,
1110 "connection_write(%d): waking output for id=%ld\n",
1111 s, c->c_connid, 0 );
1113 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1115 if ( ber_pvt_sb_needs_read( c->c_sb ) )
1116 slapd_set_read( s, 1 );
1117 if ( ber_pvt_sb_needs_write( c->c_sb ) )
1118 slapd_set_write( s, 1 );
1119 connection_return( c );
1120 ldap_pvt_thread_mutex_unlock( &connections_mutex );