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 /* assume LDAPv3 until bind */
413 c->c_protocol = LDAP_VERSION3;
415 c->c_activitytime = c->c_starttime = slap_get_time();
417 ber_pvt_sb_set_desc( c->c_sb, s );
418 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
420 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
421 Debug( LDAP_DEBUG_ANY,
422 "connection_init(%d, %s): set nonblocking failed\n",
423 s, c->c_peer_name,0 );
426 id = c->c_connid = conn_nextid++;
428 c->c_conn_state = SLAP_C_INACTIVE;
429 c->c_struct_state = SLAP_C_USED;
434 c->c_needs_tls_accept = 1;
438 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
439 ldap_pvt_thread_mutex_unlock( &connections_mutex );
441 backend_connection_init(c);
447 connection_destroy( Connection *c )
449 /* note: connections_mutex should be locked by caller */
451 assert( connections != NULL );
453 assert( c->c_struct_state != SLAP_C_UNUSED );
454 assert( c->c_conn_state != SLAP_C_INVALID );
455 assert( c->c_ops == NULL );
457 backend_connection_destroy(c);
462 c->c_activitytime = c->c_starttime = 0;
464 if(c->c_dn != NULL) {
468 if(c->c_cdn != NULL) {
472 if(c->c_listener_url != NULL) {
473 free(c->c_listener_url);
474 c->c_listener_url = NULL;
476 if(c->c_peer_domain != NULL) {
477 free(c->c_peer_domain);
478 c->c_peer_domain = NULL;
480 if(c->c_peer_name != NULL) {
481 free(c->c_peer_name);
482 c->c_peer_name = NULL;
484 if(c->c_sock_name != NULL) {
485 free(c->c_sock_name);
486 c->c_sock_name = NULL;
488 if(c->c_authmech != NULL ) {
490 c->c_authmech = NULL;
492 if(c->c_authstate != NULL ) {
493 free(c->c_authstate);
494 c->c_authstate = NULL;
497 #ifdef HAVE_CYRUS_SASL
498 if(c->c_sasl_context != NULL ) {
499 sasl_dispose( &c->c_sasl_context );
500 c->c_sasl_context = NULL;
504 if ( ber_pvt_sb_in_use(c->c_sb) ) {
505 int sd = ber_pvt_sb_get_desc(c->c_sb);
507 slapd_remove( sd, 0 );
508 ber_pvt_sb_close( c->c_sb );
510 Statslog( LDAP_DEBUG_STATS,
511 "conn=%ld fd=%d closed.\n",
512 c->c_connid, sd, 0, 0, 0 );
515 ber_pvt_sb_destroy( c->c_sb );
517 c->c_conn_state = SLAP_C_INVALID;
518 c->c_struct_state = SLAP_C_UNUSED;
521 int connection_state_closing( Connection *c )
523 /* c_mutex must be locked by caller */
527 assert( c->c_struct_state == SLAP_C_USED );
529 state = c->c_conn_state;
531 assert( state != SLAP_C_INVALID );
533 return state == SLAP_C_CLOSING;
536 static void connection_abandon( Connection *c )
538 /* c_mutex must be locked by caller */
542 for( o = c->c_ops; o != NULL; o = o->o_next ) {
543 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
545 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
548 /* remove pending operations */
549 for( o = slap_op_pop( &c->c_pending_ops );
551 o = slap_op_pop( &c->c_pending_ops ) )
557 void connection_closing( Connection *c )
559 assert( connections != NULL );
561 assert( c->c_struct_state == SLAP_C_USED );
562 assert( c->c_conn_state != SLAP_C_INVALID );
564 /* c_mutex must be locked by caller */
566 if( c->c_conn_state != SLAP_C_CLOSING ) {
568 Debug( LDAP_DEBUG_TRACE,
569 "connection_closing: readying conn=%ld sd=%d for close.\n",
570 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
572 /* update state to closing */
573 c->c_conn_state = SLAP_C_CLOSING;
575 /* don't listen on this port anymore */
576 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
578 /* shutdown I/O -- not yet implemented */
580 /* abandon active operations */
581 connection_abandon( c );
583 /* wake write blocked operations */
584 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
585 ldap_pvt_thread_cond_signal( &c->c_write_cv );
589 static void connection_close( Connection *c )
591 assert( connections != NULL );
593 assert( c->c_struct_state == SLAP_C_USED );
594 assert( c->c_conn_state == SLAP_C_CLOSING );
596 /* note: connections_mutex and c_mutex should be locked by caller */
598 if( c->c_ops != NULL ) {
599 Debug( LDAP_DEBUG_TRACE,
600 "connection_close: deferring conn=%ld sd=%d.\n",
601 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
606 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
607 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
609 connection_destroy( c );
612 unsigned long connections_nextid(void)
615 assert( connections != NULL );
617 ldap_pvt_thread_mutex_lock( &connections_mutex );
621 ldap_pvt_thread_mutex_unlock( &connections_mutex );
626 Connection* connection_first( ber_socket_t *index )
628 assert( connections != NULL );
629 assert( index != NULL );
631 ldap_pvt_thread_mutex_lock( &connections_mutex );
635 return connection_next(NULL, index);
638 Connection* connection_next( Connection *c, ber_socket_t *index )
640 assert( connections != NULL );
641 assert( index != NULL );
642 assert( *index <= dtblsize );
645 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
650 for(; *index < dtblsize; (*index)++) {
651 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
652 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
660 if( connections[*index].c_struct_state == SLAP_C_USED ) {
661 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
662 c = &connections[(*index)++];
666 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
667 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
671 ldap_pvt_thread_mutex_lock( &c->c_mutex );
677 void connection_done( Connection *c )
679 assert( connections != NULL );
682 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
685 ldap_pvt_thread_mutex_unlock( &connections_mutex );
689 * connection_activity - handle the request operation op on connection
690 * conn. This routine figures out what kind of operation it is and
691 * calls the appropriate stub to handle it.
695 connection_operation( void *arg_v )
698 struct co_arg *arg = arg_v;
699 ber_tag_t tag = arg->co_op->o_tag;
700 Connection *conn = arg->co_conn;
702 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
704 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
708 rc = do_bind( conn, arg->co_op );
711 case LDAP_REQ_UNBIND:
712 rc = do_unbind( conn, arg->co_op );
716 rc = do_add( conn, arg->co_op );
719 case LDAP_REQ_DELETE:
720 rc = do_delete( conn, arg->co_op );
723 case LDAP_REQ_MODRDN:
724 rc = do_modrdn( conn, arg->co_op );
727 case LDAP_REQ_MODIFY:
728 rc = do_modify( conn, arg->co_op );
731 case LDAP_REQ_COMPARE:
732 rc = do_compare( conn, arg->co_op );
735 case LDAP_REQ_SEARCH:
736 rc = do_search( conn, arg->co_op );
739 case LDAP_REQ_ABANDON:
740 rc = do_abandon( conn, arg->co_op );
743 case LDAP_REQ_EXTENDED:
744 rc = do_extended( conn, arg->co_op );
748 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
750 arg->co_op->o_tag = LBER_ERROR;
751 send_ldap_disconnect( conn, arg->co_op,
752 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
757 if( rc == -1 ) tag = LBER_ERROR;
759 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
761 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
763 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
765 conn->c_n_ops_executing--;
766 conn->c_n_ops_completed++;
768 slap_op_remove( &conn->c_ops, arg->co_op );
769 slap_op_free( arg->co_op );
772 free( (char *) arg );
777 case LDAP_REQ_UNBIND:
778 /* c_mutex is locked */
779 connection_closing( conn );
783 if( conn->c_conn_state == SLAP_C_BINDING) {
784 conn->c_conn_state = SLAP_C_ACTIVE;
786 conn->c_bind_in_progress = ( rc == LDAP_SASL_BIND_IN_PROGRESS );
789 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
791 if( active_threads < 1 ) {
792 ldap_pvt_thread_cond_signal(&active_threads_cond);
794 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
796 connection_resched( conn );
798 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
803 int connection_read(ber_socket_t s)
807 assert( connections != NULL );
809 ldap_pvt_thread_mutex_lock( &connections_mutex );
811 /* get (locked) connection */
812 c = connection_get( s );
815 Debug( LDAP_DEBUG_ANY,
816 "connection_read(%ld): no connection!\n",
821 ldap_pvt_thread_mutex_unlock( &connections_mutex );
827 if( c->c_conn_state == SLAP_C_CLOSING ) {
828 Debug( LDAP_DEBUG_TRACE,
829 "connection_read(%d): closing, ignoring input for id=%ld\n",
832 connection_return( c );
833 ldap_pvt_thread_mutex_unlock( &connections_mutex );
837 Debug( LDAP_DEBUG_TRACE,
838 "connection_read(%d): checking for input on id=%ld\n",
842 if ( c->c_is_tls && c->c_needs_tls_accept ) {
843 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
845 Debug( LDAP_DEBUG_TRACE,
846 "connection_read(%d): TLS accept error error=%d id=%ld, closing.\n",
847 s, rc, c->c_connid );
849 c->c_needs_tls_accept = 0;
850 /* connections_mutex and c_mutex are locked */
851 connection_closing( c );
852 connection_close( c );
853 } else if ( rc == 0 ) {
854 c->c_needs_tls_accept = 0;
856 connection_return( c );
857 ldap_pvt_thread_mutex_unlock( &connections_mutex );
862 #define CONNECTION_INPUT_LOOP 1
864 #ifdef DATA_READY_LOOP
865 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
866 #elif CONNECTION_INPUT_LOOP
870 /* How do we do this without getting into a busy loop ? */
871 rc = connection_input( c );
875 Debug( LDAP_DEBUG_TRACE,
876 "connection_read(%d): input error=%d id=%ld, closing.\n",
877 s, rc, c->c_connid );
879 /* connections_mutex and c_mutex are locked */
880 connection_closing( c );
881 connection_close( c );
884 if ( ber_pvt_sb_needs_read( c->c_sb ) )
885 slapd_set_read( s, 1 );
886 if ( ber_pvt_sb_needs_write( c->c_sb ) )
887 slapd_set_write( s, 1 );
888 connection_return( c );
889 ldap_pvt_thread_mutex_unlock( &connections_mutex );
904 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
906 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
911 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
912 != LDAP_TAG_MESSAGE )
916 Debug( LDAP_DEBUG_TRACE,
917 "ber_get_next on fd %d failed errno %d (%s)\n",
918 ber_pvt_sb_get_desc( conn->c_sb ), err,
919 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
920 Debug( LDAP_DEBUG_TRACE,
921 "\t*** got %ld of %lu so far\n",
922 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
923 conn->c_currentber->ber_len, 0 );
925 if ( err != EWOULDBLOCK && err != EAGAIN ) {
926 /* log, close and send error */
927 ber_free( conn->c_currentber, 1 );
928 conn->c_currentber = NULL;
935 ber = conn->c_currentber;
936 conn->c_currentber = NULL;
938 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
939 /* log, close and send error */
940 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
946 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
947 /* log, close and send error */
948 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
955 if(tag == LDAP_REQ_BIND) {
956 /* immediately abandon all exiting operations upon BIND */
957 connection_abandon( conn );
960 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
962 if ( conn->c_conn_state == SLAP_C_BINDING
963 || conn->c_conn_state == SLAP_C_CLOSING )
965 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
966 conn->c_n_ops_pending++;
967 slap_op_add( &conn->c_pending_ops, op );
970 conn->c_n_ops_executing++;
971 connection_op_activate( conn, op );
975 if ( conn->c_struct_state != SLAP_C_USED ) {
976 /* connection must have got closed underneath us */
980 assert( conn->c_struct_state == SLAP_C_USED );
986 connection_resched( Connection *conn )
990 if( conn->c_conn_state == SLAP_C_CLOSING ) {
991 Debug( LDAP_DEBUG_TRACE,
992 "connection_resched: attempting closing conn=%ld sd=%d.\n",
993 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
995 connection_close( conn );
999 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1000 /* other states need different handling */
1004 for( op = slap_op_pop( &conn->c_pending_ops );
1006 op = slap_op_pop( &conn->c_pending_ops ) )
1008 /* pending operations should not be marked for abandonment */
1009 assert(!op->o_abandon);
1011 conn->c_n_ops_pending--;
1012 conn->c_n_ops_executing++;
1014 connection_op_activate( conn, op );
1016 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1023 static int connection_op_activate( Connection *conn, Operation *op )
1028 ber_tag_t tag = op->o_tag;
1030 if(tag == LDAP_REQ_BIND) {
1031 conn->c_conn_state = SLAP_C_BINDING;
1034 if ( conn->c_dn != NULL ) {
1035 tmpdn = ch_strdup( conn->c_dn );
1040 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1041 arg->co_conn = conn;
1044 arg->co_op->o_bind_in_progress = conn->c_bind_in_progress;
1046 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
1047 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1048 (void) dn_normalize_case( arg->co_op->o_ndn );
1050 arg->co_op->o_protocol = conn->c_protocol;
1051 arg->co_op->o_connid = conn->c_connid;
1053 arg->co_op->o_authtype = conn->c_authtype;
1054 arg->co_op->o_authmech = conn->c_authmech != NULL
1055 ? ch_strdup( conn->c_authmech ) : NULL;
1057 slap_op_add( &conn->c_ops, arg->co_op );
1059 if( tmpdn != NULL ) {
1063 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
1065 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
1067 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
1068 connection_operation, (void *) arg );
1070 if ( status != 0 ) {
1071 Debug( LDAP_DEBUG_ANY,
1072 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
1074 /* should move op to pending list */
1080 int connection_write(ber_socket_t s)
1083 assert( connections != NULL );
1085 ldap_pvt_thread_mutex_lock( &connections_mutex );
1087 c = connection_get( s );
1089 slapd_clr_write( s, 0);
1092 Debug( LDAP_DEBUG_ANY,
1093 "connection_write(%ld): no connection!\n",
1096 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1102 Debug( LDAP_DEBUG_TRACE,
1103 "connection_write(%d): waking output for id=%ld\n",
1104 s, c->c_connid, 0 );
1106 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1108 if ( ber_pvt_sb_needs_read( c->c_sb ) )
1109 slapd_set_read( s, 1 );
1110 if ( ber_pvt_sb_needs_write( c->c_sb ) )
1111 slapd_set_write( s, 1 );
1112 connection_return( c );
1113 ldap_pvt_thread_mutex_unlock( &connections_mutex );