3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
13 #include <ac/signal.h>
14 #include <ac/string.h>
19 /* we need LBER internals */
20 #include "../../libraries/liblber/lber-int.h"
22 /* protected by connections_mutex */
23 static ldap_pvt_thread_mutex_t connections_mutex;
24 static Connection *connections = NULL;
25 static unsigned long conn_nextid = 0;
27 /* structure state (protected by connections_mutex) */
28 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
29 #define SLAP_C_UNUSED 0x01
30 #define SLAP_C_USED 0x02
32 /* connection state (protected by c_mutex ) */
33 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
34 #define SLAP_C_INACTIVE 0x01 /* zero threads */
35 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
36 #define SLAP_C_BINDING 0x03 /* binding */
37 #define SLAP_C_CLOSING 0x04 /* closing */
40 connection_state2str( int state )
43 case SLAP_C_INVALID: return "!";
44 case SLAP_C_INACTIVE: return "|";
45 case SLAP_C_ACTIVE: return "";
46 case SLAP_C_BINDING: return "B";
47 case SLAP_C_CLOSING: return "C";
53 static Connection* connection_get( ber_socket_t s );
55 static int connection_input( Connection *c );
56 static void connection_close( Connection *c );
58 static int connection_op_activate( Connection *conn, Operation *op );
59 static int connection_resched( Connection *conn );
60 static void connection_abandon( Connection *conn );
61 static void connection_destroy( Connection *c );
69 * Initialize connection management infrastructure.
71 int connections_init(void)
73 assert( connections == NULL );
75 if( connections != NULL) {
76 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
81 /* should check return of every call */
82 ldap_pvt_thread_mutex_init( &connections_mutex );
84 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
86 if( connections == NULL ) {
87 Debug( LDAP_DEBUG_ANY,
88 "connections_init: allocation (%d*%ld) of connection array failed\n",
89 dtblsize, (long) sizeof(Connection), 0 );
93 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
94 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
97 * per entry initialization of the Connection array initialization
98 * will be done by connection_init()
105 * Destroy connection management infrastructure.
107 int connections_destroy(void)
111 /* should check return of every call */
113 if( connections == NULL) {
114 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
119 for ( i = 0; i < dtblsize; i++ ) {
120 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
121 ber_sockbuf_free( connections[i].c_sb );
122 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
123 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
124 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
131 ldap_pvt_thread_mutex_destroy( &connections_mutex );
136 * shutdown all connections
138 int connections_shutdown(void)
142 ldap_pvt_thread_mutex_lock( &connections_mutex );
144 for ( i = 0; i < dtblsize; i++ ) {
145 if( connections[i].c_struct_state != SLAP_C_USED ) {
149 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
151 /* connections_mutex and c_mutex are locked */
152 connection_closing( &connections[i] );
153 connection_close( &connections[i] );
155 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
158 ldap_pvt_thread_mutex_unlock( &connections_mutex );
164 * Timeout idle connections.
166 int connections_timeout_idle(time_t now)
172 ldap_pvt_thread_mutex_lock( &connections_mutex );
174 for( c = connection_first( &connindex );
176 c = connection_next( c, &connindex ) )
178 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
180 connection_closing( c );
181 connection_close( c );
185 connection_done( c );
187 ldap_pvt_thread_mutex_unlock( &connections_mutex );
192 static Connection* connection_get( ber_socket_t s )
194 /* connections_mutex should be locked by caller */
198 Debug( LDAP_DEBUG_ARGS,
199 "connection_get(%ld)\n",
202 assert( connections != NULL );
204 if(s == AC_SOCKET_INVALID) {
211 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
218 for(i=0; i<dtblsize; i++) {
219 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
220 assert( connections[i].c_conn_state == SLAP_C_INVALID );
221 assert( connections[i].c_sb == 0 );
225 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
226 assert( connections[i].c_conn_state == SLAP_C_INVALID );
227 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
231 /* state can actually change from used -> unused by resched,
232 * so don't assert details here.
235 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
244 ldap_pvt_thread_mutex_lock( &c->c_mutex );
246 if( c->c_struct_state != SLAP_C_USED ) {
247 /* connection must have been closed due to resched */
249 assert( c->c_conn_state == SLAP_C_INVALID );
250 assert( !ber_pvt_sb_in_use( c->c_sb ) );
252 Debug( LDAP_DEBUG_TRACE,
253 "connection_get(%d): connection not used\n",
256 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
260 Debug( LDAP_DEBUG_TRACE,
261 "connection_get(%d): got connid=%ld\n",
266 assert( c->c_struct_state == SLAP_C_USED );
267 assert( c->c_conn_state != SLAP_C_INVALID );
268 assert( ber_pvt_sb_in_use( c->c_sb ) );
270 c->c_activitytime = slap_get_time();
276 static void connection_return( Connection *c )
278 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
281 long connection_init(
285 const char* peername,
286 const char* sockname,
292 assert( connections != NULL );
294 assert( dnsname != NULL );
295 assert( peername != NULL );
296 assert( sockname != NULL );
302 if( s == AC_SOCKET_INVALID ) {
303 Debug( LDAP_DEBUG_ANY,
304 "connection_init(%ld): invalid.\n",
311 assert( s < dtblsize );
314 ldap_pvt_thread_mutex_lock( &connections_mutex );
325 for( i=0; i < dtblsize; i++) {
326 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
327 assert( connections[i].c_sb == 0 );
332 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
333 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
338 assert( connections[i].c_struct_state == SLAP_C_USED );
339 assert( connections[i].c_conn_state != SLAP_C_INVALID );
340 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
344 Debug( LDAP_DEBUG_ANY,
345 "connection_init(%d): connection table full (%d/%d)\n",
347 ldap_pvt_thread_mutex_unlock( &connections_mutex );
355 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
356 c->c_authmech = NULL;
360 c->c_listener_url = NULL;
361 c->c_peer_domain = NULL;
362 c->c_peer_name = NULL;
363 c->c_sock_name = NULL;
366 c->c_pending_ops = NULL;
368 c->c_sasl_bind_mech = NULL;
369 #ifdef HAVE_CYRUS_SASL
370 c->c_sasl_bind_context = NULL;
373 c->c_sb = ber_sockbuf_alloc( );
374 c->c_currentber = NULL;
376 /* should check status of thread calls */
377 ldap_pvt_thread_mutex_init( &c->c_mutex );
378 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
379 ldap_pvt_thread_cond_init( &c->c_write_cv );
381 c->c_struct_state = SLAP_C_UNUSED;
384 ldap_pvt_thread_mutex_lock( &c->c_mutex );
386 assert( c->c_struct_state == SLAP_C_UNUSED );
387 assert( c->c_authmech == NULL );
388 assert( c->c_dn == NULL );
389 assert( c->c_cdn == NULL );
390 assert( c->c_listener_url == NULL );
391 assert( c->c_peer_domain == NULL );
392 assert( c->c_peer_name == NULL );
393 assert( c->c_sock_name == NULL );
394 assert( c->c_ops == NULL );
395 assert( c->c_pending_ops == NULL );
396 assert( c->c_sasl_bind_mech == NULL );
397 #ifdef HAVE_CYRUS_SASL
398 assert( c->c_sasl_bind_context == NULL );
400 assert( c->c_currentber == NULL );
402 c->c_listener_url = ch_strdup( url );
403 c->c_peer_domain = ch_strdup( dnsname );
404 c->c_peer_name = ch_strdup( peername );
405 c->c_sock_name = ch_strdup( sockname );
407 c->c_n_ops_received = 0;
408 c->c_n_ops_executing = 0;
409 c->c_n_ops_pending = 0;
410 c->c_n_ops_completed = 0;
416 /* assume LDAPv3 until bind */
417 c->c_protocol = LDAP_VERSION3;
419 c->c_activitytime = c->c_starttime = slap_get_time();
421 ber_pvt_sb_set_desc( c->c_sb, s );
422 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
424 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
425 Debug( LDAP_DEBUG_ANY,
426 "connection_init(%d, %s): set nonblocking failed\n",
427 s, c->c_peer_name,0 );
430 id = c->c_connid = conn_nextid++;
432 c->c_conn_state = SLAP_C_INACTIVE;
433 c->c_struct_state = SLAP_C_USED;
438 c->c_needs_tls_accept = 1;
441 c->c_needs_tls_accept = 0;
445 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
446 ldap_pvt_thread_mutex_unlock( &connections_mutex );
448 backend_connection_init(c);
454 connection_destroy( Connection *c )
456 /* note: connections_mutex should be locked by caller */
458 assert( connections != NULL );
460 assert( c->c_struct_state != SLAP_C_UNUSED );
461 assert( c->c_conn_state != SLAP_C_INVALID );
462 assert( c->c_ops == NULL );
464 backend_connection_destroy(c);
469 c->c_activitytime = c->c_starttime = 0;
471 if(c->c_authmech != NULL ) {
473 c->c_authmech = NULL;
475 if(c->c_dn != NULL) {
479 if(c->c_cdn != NULL) {
483 if(c->c_listener_url != NULL) {
484 free(c->c_listener_url);
485 c->c_listener_url = NULL;
487 if(c->c_peer_domain != NULL) {
488 free(c->c_peer_domain);
489 c->c_peer_domain = NULL;
491 if(c->c_peer_name != NULL) {
494 * If peer was a domain socket, unlink. Mind you,
495 * they may be un-named. Should we leave this to
498 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
499 char *path = c->c_peer_name + 5;
504 #endif /* LDAP_PF_LOCAL */
505 free(c->c_peer_name);
506 c->c_peer_name = NULL;
508 if(c->c_sock_name != NULL) {
509 free(c->c_sock_name);
510 c->c_sock_name = NULL;
513 c->c_sasl_bind_in_progress = 0;
514 if(c->c_sasl_bind_mech != NULL) {
515 free(c->c_sasl_bind_mech);
516 c->c_sasl_bind_mech = NULL;
518 #ifdef HAVE_CYRUS_SASL
519 if(c->c_sasl_bind_context != NULL ) {
520 sasl_dispose( &c->c_sasl_bind_context );
521 c->c_sasl_bind_context = NULL;
525 if ( c->c_currentber != NULL ) {
526 ber_free( c->c_currentber, 1 );
527 c->c_currentber = NULL;
530 if ( ber_pvt_sb_in_use(c->c_sb) ) {
531 int sd = ber_pvt_sb_get_desc(c->c_sb);
533 slapd_remove( sd, 0 );
534 ber_pvt_sb_close( c->c_sb );
536 Statslog( LDAP_DEBUG_STATS,
537 "conn=%ld fd=%d closed\n",
538 c->c_connid, sd, 0, 0, 0 );
541 ber_pvt_sb_destroy( c->c_sb );
543 c->c_conn_state = SLAP_C_INVALID;
544 c->c_struct_state = SLAP_C_UNUSED;
547 int connection_state_closing( Connection *c )
549 /* c_mutex must be locked by caller */
553 assert( c->c_struct_state == SLAP_C_USED );
555 state = c->c_conn_state;
557 assert( state != SLAP_C_INVALID );
559 return state == SLAP_C_CLOSING;
562 static void connection_abandon( Connection *c )
564 /* c_mutex must be locked by caller */
568 for( o = c->c_ops; o != NULL; o = o->o_next ) {
569 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
571 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
574 /* remove pending operations */
575 for( o = slap_op_pop( &c->c_pending_ops );
577 o = slap_op_pop( &c->c_pending_ops ) )
583 void connection_closing( Connection *c )
585 assert( connections != NULL );
587 assert( c->c_struct_state == SLAP_C_USED );
588 assert( c->c_conn_state != SLAP_C_INVALID );
590 /* c_mutex must be locked by caller */
592 if( c->c_conn_state != SLAP_C_CLOSING ) {
593 Debug( LDAP_DEBUG_TRACE,
594 "connection_closing: readying conn=%ld sd=%d for close\n",
595 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
597 /* update state to closing */
598 c->c_conn_state = SLAP_C_CLOSING;
600 /* don't listen on this port anymore */
601 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
603 /* abandon active operations */
604 connection_abandon( c );
606 /* wake write blocked operations */
607 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
608 ldap_pvt_thread_cond_signal( &c->c_write_cv );
612 static void connection_close( Connection *c )
614 assert( connections != NULL );
616 assert( c->c_struct_state == SLAP_C_USED );
617 assert( c->c_conn_state == SLAP_C_CLOSING );
619 /* note: connections_mutex and c_mutex should be locked by caller */
621 if( c->c_ops != NULL ) {
622 Debug( LDAP_DEBUG_TRACE,
623 "connection_close: deferring conn=%ld sd=%d\n",
624 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
629 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
630 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
632 connection_destroy( c );
635 unsigned long connections_nextid(void)
638 assert( connections != NULL );
640 ldap_pvt_thread_mutex_lock( &connections_mutex );
644 ldap_pvt_thread_mutex_unlock( &connections_mutex );
649 Connection* connection_first( ber_socket_t *index )
651 assert( connections != NULL );
652 assert( index != NULL );
654 ldap_pvt_thread_mutex_lock( &connections_mutex );
658 return connection_next(NULL, index);
661 Connection* connection_next( Connection *c, ber_socket_t *index )
663 assert( connections != NULL );
664 assert( index != NULL );
665 assert( *index <= dtblsize );
668 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
673 for(; *index < dtblsize; (*index)++) {
674 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
675 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
683 if( connections[*index].c_struct_state == SLAP_C_USED ) {
684 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
685 c = &connections[(*index)++];
689 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
690 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
694 ldap_pvt_thread_mutex_lock( &c->c_mutex );
700 void connection_done( Connection *c )
702 assert( connections != NULL );
705 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
708 ldap_pvt_thread_mutex_unlock( &connections_mutex );
712 * connection_activity - handle the request operation op on connection
713 * conn. This routine figures out what kind of operation it is and
714 * calls the appropriate stub to handle it.
718 connection_operation( void *arg_v )
721 struct co_arg *arg = arg_v;
722 ber_tag_t tag = arg->co_op->o_tag;
723 Connection *conn = arg->co_conn;
725 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
727 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
729 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
730 Debug( LDAP_DEBUG_ANY, "connection_operation: "
731 "error: SASL bind in progress (tag=%ld).\n",
733 send_ldap_result( conn, arg->co_op,
734 rc = LDAP_OPERATIONS_ERROR,
735 NULL, "SASL bind in progress", NULL, NULL );
736 goto operations_error;
741 rc = do_bind( conn, arg->co_op );
744 case LDAP_REQ_UNBIND:
745 rc = do_unbind( conn, arg->co_op );
749 rc = do_add( conn, arg->co_op );
752 case LDAP_REQ_DELETE:
753 rc = do_delete( conn, arg->co_op );
756 case LDAP_REQ_MODRDN:
757 rc = do_modrdn( conn, arg->co_op );
760 case LDAP_REQ_MODIFY:
761 rc = do_modify( conn, arg->co_op );
764 case LDAP_REQ_COMPARE:
765 rc = do_compare( conn, arg->co_op );
768 case LDAP_REQ_SEARCH:
769 rc = do_search( conn, arg->co_op );
772 case LDAP_REQ_ABANDON:
773 rc = do_abandon( conn, arg->co_op );
776 case LDAP_REQ_EXTENDED:
777 rc = do_extended( conn, arg->co_op );
781 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
783 arg->co_op->o_tag = LBER_ERROR;
784 send_ldap_disconnect( conn, arg->co_op,
785 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
790 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
793 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
795 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
797 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
799 conn->c_n_ops_executing--;
800 conn->c_n_ops_completed++;
802 slap_op_remove( &conn->c_ops, arg->co_op );
803 slap_op_free( arg->co_op );
806 free( (char *) arg );
811 case LDAP_REQ_UNBIND:
812 /* c_mutex is locked */
813 connection_closing( conn );
817 conn->c_sasl_bind_in_progress =
818 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
820 if( conn->c_conn_state == SLAP_C_BINDING) {
821 conn->c_conn_state = SLAP_C_ACTIVE;
825 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
827 if( active_threads < 1 ) {
828 ldap_pvt_thread_cond_signal(&active_threads_cond);
830 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
832 connection_resched( conn );
834 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
839 int connection_read(ber_socket_t s)
843 assert( connections != NULL );
845 ldap_pvt_thread_mutex_lock( &connections_mutex );
847 /* get (locked) connection */
848 c = connection_get( s );
851 Debug( LDAP_DEBUG_ANY,
852 "connection_read(%ld): no connection!\n",
857 ldap_pvt_thread_mutex_unlock( &connections_mutex );
863 if( c->c_conn_state == SLAP_C_CLOSING ) {
864 Debug( LDAP_DEBUG_TRACE,
865 "connection_read(%d): closing, ignoring input for id=%ld\n",
868 connection_return( c );
869 ldap_pvt_thread_mutex_unlock( &connections_mutex );
873 Debug( LDAP_DEBUG_TRACE,
874 "connection_read(%d): checking for input on id=%ld\n",
878 if ( c->c_is_tls && c->c_needs_tls_accept ) {
879 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
884 Debug( LDAP_DEBUG_TRACE,
885 "connection_read(%d): TLS accept error error=%d id=%ld, closing\n",
886 s, rc, c->c_connid );
888 c->c_needs_tls_accept = 0;
889 /* connections_mutex and c_mutex are locked */
890 connection_closing( c );
892 /* Drain input before close, to allow SSL error codes
893 * to propagate to client. */
896 ber_pvt_sb_set_readahead(c->c_sb, 0);
902 rc = select(s+1, &rfd, NULL, NULL, &tv);
904 rc = ber_pvt_sb_read(c->c_sb, buf, sizeof(buf));
906 connection_close( c );
907 } else if ( rc == 0 ) {
908 c->c_needs_tls_accept = 0;
910 connection_return( c );
911 ldap_pvt_thread_mutex_unlock( &connections_mutex );
916 #define CONNECTION_INPUT_LOOP 1
918 #ifdef DATA_READY_LOOP
919 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
920 #elif CONNECTION_INPUT_LOOP
924 /* How do we do this without getting into a busy loop ? */
925 rc = connection_input( c );
929 Debug( LDAP_DEBUG_TRACE,
930 "connection_read(%d): input error=%d id=%ld, closing.\n",
931 s, rc, c->c_connid );
933 /* connections_mutex and c_mutex are locked */
934 connection_closing( c );
935 connection_close( c );
938 if ( ber_pvt_sb_needs_read( c->c_sb ) )
939 slapd_set_read( s, 1 );
940 if ( ber_pvt_sb_needs_write( c->c_sb ) )
941 slapd_set_write( s, 1 );
942 connection_return( c );
943 ldap_pvt_thread_mutex_unlock( &connections_mutex );
958 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
960 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
965 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
966 != LDAP_TAG_MESSAGE )
970 Debug( LDAP_DEBUG_TRACE,
971 "ber_get_next on fd %d failed errno=%d (%s)\n",
972 ber_pvt_sb_get_desc( conn->c_sb ), err, sock_errstr(err) );
973 Debug( LDAP_DEBUG_TRACE,
974 "\t*** got %ld of %lu so far\n",
975 (long) ( conn->c_currentber->ber_buf
976 ? conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf
978 (long) conn->c_currentber->ber_len, 0 );
980 if ( err != EWOULDBLOCK && err != EAGAIN ) {
981 /* log, close and send error */
982 ber_free( conn->c_currentber, 1 );
983 conn->c_currentber = NULL;
990 ber = conn->c_currentber;
991 conn->c_currentber = NULL;
993 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
994 /* log, close and send error */
995 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1001 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1002 /* log, close and send error */
1003 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1010 if(tag == LDAP_REQ_BIND) {
1011 /* immediately abandon all exiting operations upon BIND */
1012 connection_abandon( conn );
1015 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1017 if ( conn->c_conn_state == SLAP_C_BINDING
1018 || conn->c_conn_state == SLAP_C_CLOSING )
1020 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1021 conn->c_n_ops_pending++;
1022 slap_op_add( &conn->c_pending_ops, op );
1025 conn->c_n_ops_executing++;
1026 connection_op_activate( conn, op );
1030 if ( conn->c_struct_state != SLAP_C_USED ) {
1031 /* connection must have got closed underneath us */
1035 assert( conn->c_struct_state == SLAP_C_USED );
1041 connection_resched( Connection *conn )
1045 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1046 Debug( LDAP_DEBUG_TRACE,
1047 "connection_resched: attempting closing conn=%ld sd=%d\n",
1048 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
1050 connection_close( conn );
1054 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1055 /* other states need different handling */
1059 for( op = slap_op_pop( &conn->c_pending_ops );
1061 op = slap_op_pop( &conn->c_pending_ops ) )
1063 /* pending operations should not be marked for abandonment */
1064 assert(!op->o_abandon);
1066 conn->c_n_ops_pending--;
1067 conn->c_n_ops_executing++;
1069 connection_op_activate( conn, op );
1071 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1078 static int connection_op_activate( Connection *conn, Operation *op )
1083 ber_tag_t tag = op->o_tag;
1085 if(tag == LDAP_REQ_BIND) {
1086 conn->c_conn_state = SLAP_C_BINDING;
1089 if ( conn->c_dn != NULL ) {
1090 tmpdn = ch_strdup( conn->c_dn );
1095 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1096 arg->co_conn = conn;
1099 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
1100 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1101 (void) dn_normalize( arg->co_op->o_ndn );
1103 arg->co_op->o_protocol = conn->c_protocol;
1104 arg->co_op->o_connid = conn->c_connid;
1106 arg->co_op->o_authtype = conn->c_authtype;
1107 arg->co_op->o_authmech = conn->c_authmech != NULL
1108 ? ch_strdup( conn->c_authmech ) : NULL;
1110 slap_op_add( &conn->c_ops, arg->co_op );
1112 if( tmpdn != NULL ) {
1116 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
1118 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
1120 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
1121 connection_operation, (void *) arg );
1123 if ( status != 0 ) {
1124 Debug( LDAP_DEBUG_ANY,
1125 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
1127 /* should move op to pending list */
1133 int connection_write(ber_socket_t s)
1136 assert( connections != NULL );
1138 ldap_pvt_thread_mutex_lock( &connections_mutex );
1140 c = connection_get( s );
1142 slapd_clr_write( s, 0);
1145 Debug( LDAP_DEBUG_ANY,
1146 "connection_write(%ld): no connection!\n",
1149 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1155 Debug( LDAP_DEBUG_TRACE,
1156 "connection_write(%d): waking output for id=%ld\n",
1157 s, c->c_connid, 0 );
1159 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1161 if ( ber_pvt_sb_needs_read( c->c_sb ) )
1162 slapd_set_read( s, 1 );
1163 if ( ber_pvt_sb_needs_write( c->c_sb ) )
1164 slapd_set_write( s, 1 );
1165 connection_return( c );
1166 ldap_pvt_thread_mutex_unlock( &connections_mutex );