3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
20 /* protected by connections_mutex */
21 static ldap_pvt_thread_mutex_t connections_mutex;
22 static Connection *connections = NULL;
23 static unsigned long conn_nextid = 0;
25 /* structure state (protected by connections_mutex) */
26 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
27 #define SLAP_C_UNUSED 0x01
28 #define SLAP_C_USED 0x02
30 /* connection state (protected by c_mutex ) */
31 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
32 #define SLAP_C_INACTIVE 0x01 /* zero threads */
33 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
34 #define SLAP_C_BINDING 0x03 /* binding */
35 #define SLAP_C_CLOSING 0x04 /* closing */
38 connection_state2str( int state )
41 case SLAP_C_INVALID: return "!";
42 case SLAP_C_INACTIVE: return "|";
43 case SLAP_C_ACTIVE: return "";
44 case SLAP_C_BINDING: return "B";
45 case SLAP_C_CLOSING: return "C";
51 static Connection* connection_get( ber_socket_t s );
53 static int connection_input( Connection *c );
54 static void connection_close( Connection *c );
56 static int connection_op_activate( Connection *conn, Operation *op );
57 static int connection_resched( Connection *conn );
58 static void connection_abandon( Connection *conn );
59 static void connection_destroy( Connection *c );
67 * Initialize connection management infrastructure.
69 int connections_init(void)
71 assert( connections == NULL );
73 if( connections != NULL) {
74 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
79 /* should check return of every call */
80 ldap_pvt_thread_mutex_init( &connections_mutex );
82 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
84 if( connections == NULL ) {
85 Debug( LDAP_DEBUG_ANY,
86 "connections_init: allocation (%d*%ld) of connection array failed\n",
87 dtblsize, (long) sizeof(Connection), 0 );
91 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
92 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
95 * per entry initialization of the Connection array initialization
96 * will be done by connection_init()
103 * Destroy connection management infrastructure.
105 int connections_destroy(void)
109 /* should check return of every call */
111 if( connections == NULL) {
112 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
117 for ( i = 0; i < dtblsize; i++ ) {
118 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
119 ber_sockbuf_free( connections[i].c_sb );
120 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
121 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
122 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
129 ldap_pvt_thread_mutex_destroy( &connections_mutex );
134 * shutdown all connections
136 int connections_shutdown(void)
140 ldap_pvt_thread_mutex_lock( &connections_mutex );
142 for ( i = 0; i < dtblsize; i++ ) {
143 if( connections[i].c_struct_state != SLAP_C_USED ) {
147 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
149 /* connections_mutex and c_mutex are locked */
150 connection_closing( &connections[i] );
151 connection_close( &connections[i] );
153 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
156 ldap_pvt_thread_mutex_unlock( &connections_mutex );
162 * Timeout idle connections.
164 int connections_timeout_idle(time_t now)
170 ldap_pvt_thread_mutex_lock( &connections_mutex );
172 for( c = connection_first( &connindex );
174 c = connection_next( c, &connindex ) )
176 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
178 connection_closing( c );
179 connection_close( c );
183 connection_done( c );
185 ldap_pvt_thread_mutex_unlock( &connections_mutex );
190 static Connection* connection_get( ber_socket_t s )
192 /* connections_mutex should be locked by caller */
196 Debug( LDAP_DEBUG_ARGS,
197 "connection_get(%ld)\n",
200 assert( connections != NULL );
202 if(s == AC_SOCKET_INVALID) {
209 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
216 for(i=0; i<dtblsize; i++) {
217 ber_sockbuf_ctrl( connections[i].c_sb,
218 LBER_SB_OPT_GET_FD, &sd );
220 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
221 assert( connections[i].c_conn_state == SLAP_C_INVALID );
222 assert( connections[i].c_sb == 0 );
226 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
227 assert( connections[i].c_conn_state == SLAP_C_INVALID );
228 assert( sd == AC_SOCKET_INVALID );
232 /* state can actually change from used -> unused by resched,
233 * so don't assert details here.
247 ldap_pvt_thread_mutex_lock( &c->c_mutex );
249 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
250 if( c->c_struct_state != SLAP_C_USED ) {
251 /* connection must have been closed due to resched */
253 assert( c->c_conn_state == SLAP_C_INVALID );
254 assert( sd == AC_SOCKET_INVALID );
256 Debug( LDAP_DEBUG_TRACE,
257 "connection_get(%d): connection not used\n",
260 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
264 Debug( LDAP_DEBUG_TRACE,
265 "connection_get(%d): got connid=%ld\n",
270 assert( c->c_struct_state == SLAP_C_USED );
271 assert( c->c_conn_state != SLAP_C_INVALID );
272 assert( sd != AC_SOCKET_INVALID );
274 c->c_activitytime = slap_get_time();
280 static void connection_return( Connection *c )
282 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
285 long connection_init(
289 const char* peername,
290 const char* sockname,
298 assert( connections != NULL );
300 assert( dnsname != NULL );
301 assert( peername != NULL );
302 assert( sockname != NULL );
308 if( s == AC_SOCKET_INVALID ) {
309 Debug( LDAP_DEBUG_ANY,
310 "connection_init(%ld): invalid.\n",
317 assert( s < dtblsize );
320 ldap_pvt_thread_mutex_lock( &connections_mutex );
331 for( i=0; i < dtblsize; i++) {
334 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
335 assert( connections[i].c_sb == 0 );
340 sd = AC_SOCKET_INVALID;
341 if (connections[i].c_sb != NULL)
342 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
344 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
345 assert( sd == AC_SOCKET_INVALID );
350 assert( connections[i].c_struct_state == SLAP_C_USED );
351 assert( connections[i].c_conn_state != SLAP_C_INVALID );
352 assert( sd != AC_SOCKET_INVALID );
356 Debug( LDAP_DEBUG_ANY,
357 "connection_init(%d): connection table full (%d/%d)\n",
359 ldap_pvt_thread_mutex_unlock( &connections_mutex );
367 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
368 c->c_authmech = NULL;
372 c->c_listener_url = NULL;
373 c->c_peer_domain = NULL;
374 c->c_peer_name = NULL;
375 c->c_sock_name = NULL;
378 c->c_pending_ops = NULL;
380 c->c_sasl_bind_mech = NULL;
381 c->c_sasl_context = NULL;
382 c->c_sasl_extra = NULL;
384 c->c_sb = ber_sockbuf_alloc( );
385 c->c_currentber = NULL;
387 /* should check status of thread calls */
388 ldap_pvt_thread_mutex_init( &c->c_mutex );
389 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
390 ldap_pvt_thread_cond_init( &c->c_write_cv );
392 c->c_struct_state = SLAP_C_UNUSED;
395 ldap_pvt_thread_mutex_lock( &c->c_mutex );
397 assert( c->c_struct_state == SLAP_C_UNUSED );
398 assert( c->c_authmech == NULL );
399 assert( c->c_dn == NULL );
400 assert( c->c_cdn == NULL );
401 assert( c->c_listener_url == NULL );
402 assert( c->c_peer_domain == NULL );
403 assert( c->c_peer_name == NULL );
404 assert( c->c_sock_name == NULL );
405 assert( c->c_ops == NULL );
406 assert( c->c_pending_ops == NULL );
407 assert( c->c_sasl_bind_mech == NULL );
408 assert( c->c_sasl_context == NULL );
409 assert( c->c_sasl_extra == NULL );
410 assert( c->c_currentber == NULL );
412 c->c_listener_url = ch_strdup( url );
413 c->c_peer_domain = ch_strdup( dnsname );
414 c->c_peer_name = ch_strdup( peername );
415 c->c_sock_name = ch_strdup( sockname );
417 c->c_n_ops_received = 0;
418 c->c_n_ops_executing = 0;
419 c->c_n_ops_pending = 0;
420 c->c_n_ops_completed = 0;
426 /* assume LDAPv3 until bind */
427 c->c_protocol = LDAP_VERSION3;
429 c->c_activitytime = c->c_starttime = slap_get_time();
431 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp, LBER_SBIOD_LEVEL_PROVIDER,
433 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
434 LBER_SBIOD_LEVEL_PROVIDER, NULL );
436 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
438 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK, c /* non-NULL */ ) < 0 ) {
439 Debug( LDAP_DEBUG_ANY,
440 "connection_init(%d, %s): set nonblocking failed\n",
441 s, c->c_peer_name, 0 );
445 id = c->c_connid = conn_nextid++;
447 c->c_conn_state = SLAP_C_INACTIVE;
448 c->c_struct_state = SLAP_C_USED;
453 c->c_needs_tls_accept = 1;
456 c->c_needs_tls_accept = 0;
460 slap_sasl_external( c, ssf, authid );
462 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
463 ldap_pvt_thread_mutex_unlock( &connections_mutex );
465 backend_connection_init(c);
471 connection_destroy( Connection *c )
473 /* note: connections_mutex should be locked by caller */
476 assert( connections != NULL );
478 assert( c->c_struct_state != SLAP_C_UNUSED );
479 assert( c->c_conn_state != SLAP_C_INVALID );
480 assert( c->c_ops == NULL );
482 backend_connection_destroy(c);
487 c->c_activitytime = c->c_starttime = 0;
489 if(c->c_authmech != NULL ) {
491 c->c_authmech = NULL;
493 if(c->c_dn != NULL) {
497 if(c->c_cdn != NULL) {
501 if(c->c_listener_url != NULL) {
502 free(c->c_listener_url);
503 c->c_listener_url = NULL;
505 if(c->c_peer_domain != NULL) {
506 free(c->c_peer_domain);
507 c->c_peer_domain = NULL;
509 if(c->c_peer_name != NULL) {
512 * If peer was a domain socket, unlink. Mind you,
513 * they may be un-named. Should we leave this to
516 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
517 char *path = c->c_peer_name + 5;
522 #endif /* LDAP_PF_LOCAL */
524 free(c->c_peer_name);
525 c->c_peer_name = NULL;
527 if(c->c_sock_name != NULL) {
528 free(c->c_sock_name);
529 c->c_sock_name = NULL;
532 c->c_sasl_bind_in_progress = 0;
533 if(c->c_sasl_bind_mech != NULL) {
534 free(c->c_sasl_bind_mech);
535 c->c_sasl_bind_mech = NULL;
538 slap_sasl_close( c );
540 if ( c->c_currentber != NULL ) {
541 ber_free( c->c_currentber, 1 );
542 c->c_currentber = NULL;
545 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
546 if ( sd != AC_SOCKET_INVALID ) {
547 slapd_remove( sd, 0 );
549 Statslog( LDAP_DEBUG_STATS,
550 "conn=%ld fd=%d closed\n",
551 c->c_connid, sd, 0, 0, 0 );
554 ber_sockbuf_free( c->c_sb );
555 c->c_sb = ber_sockbuf_alloc( );
557 c->c_conn_state = SLAP_C_INVALID;
558 c->c_struct_state = SLAP_C_UNUSED;
561 int connection_state_closing( Connection *c )
563 /* c_mutex must be locked by caller */
567 assert( c->c_struct_state == SLAP_C_USED );
569 state = c->c_conn_state;
571 assert( state != SLAP_C_INVALID );
573 return state == SLAP_C_CLOSING;
576 static void connection_abandon( Connection *c )
578 /* c_mutex must be locked by caller */
582 for( o = c->c_ops; o != NULL; o = o->o_next ) {
583 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
585 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
588 /* remove pending operations */
589 for( o = slap_op_pop( &c->c_pending_ops );
591 o = slap_op_pop( &c->c_pending_ops ) )
597 void connection_closing( Connection *c )
599 assert( connections != NULL );
601 assert( c->c_struct_state == SLAP_C_USED );
602 assert( c->c_conn_state != SLAP_C_INVALID );
604 /* c_mutex must be locked by caller */
606 if( c->c_conn_state != SLAP_C_CLOSING ) {
609 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
610 Debug( LDAP_DEBUG_TRACE,
611 "connection_closing: readying conn=%ld sd=%d for close\n",
612 c->c_connid, sd, 0 );
614 /* update state to closing */
615 c->c_conn_state = SLAP_C_CLOSING;
617 /* don't listen on this port anymore */
618 slapd_clr_read( sd, 1 );
620 /* abandon active operations */
621 connection_abandon( c );
623 /* wake write blocked operations */
624 slapd_clr_write( sd, 1 );
625 ldap_pvt_thread_cond_signal( &c->c_write_cv );
629 static void connection_close( Connection *c )
633 assert( connections != NULL );
635 assert( c->c_struct_state == SLAP_C_USED );
636 assert( c->c_conn_state == SLAP_C_CLOSING );
638 /* note: connections_mutex and c_mutex should be locked by caller */
640 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
641 if( c->c_ops != NULL ) {
642 Debug( LDAP_DEBUG_TRACE,
643 "connection_close: deferring conn=%ld sd=%d\n",
644 c->c_connid, sd, 0 );
649 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
650 c->c_connid, sd, 0 );
652 connection_destroy( c );
655 unsigned long connections_nextid(void)
658 assert( connections != NULL );
660 ldap_pvt_thread_mutex_lock( &connections_mutex );
664 ldap_pvt_thread_mutex_unlock( &connections_mutex );
669 Connection* connection_first( ber_socket_t *index )
671 assert( connections != NULL );
672 assert( index != NULL );
674 ldap_pvt_thread_mutex_lock( &connections_mutex );
678 return connection_next(NULL, index);
681 Connection* connection_next( Connection *c, ber_socket_t *index )
683 assert( connections != NULL );
684 assert( index != NULL );
685 assert( *index <= dtblsize );
688 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
693 for(; *index < dtblsize; (*index)++) {
694 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
695 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
703 if( connections[*index].c_struct_state == SLAP_C_USED ) {
704 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
705 c = &connections[(*index)++];
709 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
710 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
714 ldap_pvt_thread_mutex_lock( &c->c_mutex );
720 void connection_done( Connection *c )
722 assert( connections != NULL );
725 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
728 ldap_pvt_thread_mutex_unlock( &connections_mutex );
732 * connection_activity - handle the request operation op on connection
733 * conn. This routine figures out what kind of operation it is and
734 * calls the appropriate stub to handle it.
738 connection_operation( void *arg_v )
741 struct co_arg *arg = arg_v;
742 ber_tag_t tag = arg->co_op->o_tag;
743 Connection *conn = arg->co_conn;
745 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
747 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
749 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
750 Debug( LDAP_DEBUG_ANY, "connection_operation: "
751 "error: SASL bind in progress (tag=%ld).\n",
753 send_ldap_result( conn, arg->co_op,
754 rc = LDAP_OPERATIONS_ERROR,
755 NULL, "SASL bind in progress", NULL, NULL );
756 goto operations_error;
761 rc = do_bind( conn, arg->co_op );
764 case LDAP_REQ_UNBIND:
765 rc = do_unbind( conn, arg->co_op );
769 rc = do_add( conn, arg->co_op );
772 case LDAP_REQ_DELETE:
773 rc = do_delete( conn, arg->co_op );
776 case LDAP_REQ_MODRDN:
777 rc = do_modrdn( conn, arg->co_op );
780 case LDAP_REQ_MODIFY:
781 rc = do_modify( conn, arg->co_op );
784 case LDAP_REQ_COMPARE:
785 rc = do_compare( conn, arg->co_op );
788 case LDAP_REQ_SEARCH:
789 rc = do_search( conn, arg->co_op );
792 case LDAP_REQ_ABANDON:
793 rc = do_abandon( conn, arg->co_op );
796 case LDAP_REQ_EXTENDED:
797 rc = do_extended( conn, arg->co_op );
801 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
803 arg->co_op->o_tag = LBER_ERROR;
804 send_ldap_disconnect( conn, arg->co_op,
805 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
810 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
813 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
815 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
817 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
819 conn->c_n_ops_executing--;
820 conn->c_n_ops_completed++;
822 slap_op_remove( &conn->c_ops, arg->co_op );
823 slap_op_free( arg->co_op );
826 free( (char *) arg );
831 case LDAP_REQ_UNBIND:
832 /* c_mutex is locked */
833 connection_closing( conn );
837 conn->c_sasl_bind_in_progress =
838 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
840 if( conn->c_conn_state == SLAP_C_BINDING) {
841 conn->c_conn_state = SLAP_C_ACTIVE;
845 connection_resched( conn );
847 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
852 int connection_read(ber_socket_t s)
856 assert( connections != NULL );
858 ldap_pvt_thread_mutex_lock( &connections_mutex );
860 /* get (locked) connection */
861 c = connection_get( s );
864 Debug( LDAP_DEBUG_ANY,
865 "connection_read(%ld): no connection!\n",
870 ldap_pvt_thread_mutex_unlock( &connections_mutex );
876 if( c->c_conn_state == SLAP_C_CLOSING ) {
877 Debug( LDAP_DEBUG_TRACE,
878 "connection_read(%d): closing, ignoring input for id=%ld\n",
881 connection_return( c );
882 ldap_pvt_thread_mutex_unlock( &connections_mutex );
886 Debug( LDAP_DEBUG_TRACE,
887 "connection_read(%d): checking for input on id=%ld\n",
891 if ( c->c_is_tls && c->c_needs_tls_accept ) {
892 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
897 Debug( LDAP_DEBUG_TRACE,
898 "connection_read(%d): TLS accept error error=%d id=%ld, closing\n",
899 s, rc, c->c_connid );
901 c->c_needs_tls_accept = 0;
902 /* connections_mutex and c_mutex are locked */
903 connection_closing( c );
905 /* Drain input before close, to allow SSL error codes
906 * to propagate to client. */
913 rc = select(s+1, &rfd, NULL, NULL, &tv);
915 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
918 connection_close( c );
920 } else if ( rc == 0 ) {
921 c->c_needs_tls_accept = 0;
924 /* we need to let SASL know */
925 slap_sasl_external( c, ssf, authid );
928 connection_return( c );
929 ldap_pvt_thread_mutex_unlock( &connections_mutex );
934 #define CONNECTION_INPUT_LOOP 1
936 #ifdef DATA_READY_LOOP
937 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
938 #elif CONNECTION_INPUT_LOOP
942 /* How do we do this without getting into a busy loop ? */
943 rc = connection_input( c );
947 Debug( LDAP_DEBUG_TRACE,
948 "connection_read(%d): input error=%d id=%ld, closing.\n",
949 s, rc, c->c_connid );
951 /* connections_mutex and c_mutex are locked */
952 connection_closing( c );
953 connection_close( c );
956 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
957 slapd_set_read( s, 1 );
958 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
959 slapd_set_write( s, 1 );
960 connection_return( c );
961 ldap_pvt_thread_mutex_unlock( &connections_mutex );
976 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
978 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
984 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
985 if ( tag != LDAP_TAG_MESSAGE ) {
989 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
991 Debug( LDAP_DEBUG_TRACE,
992 "ber_get_next on fd %d failed errno=%d (%s)\n",
993 sd, err, sock_errstr(err) );
995 if ( err != EWOULDBLOCK && err != EAGAIN ) {
996 /* log, close and send error */
997 ber_free( conn->c_currentber, 1 );
998 conn->c_currentber = NULL;
1005 ber = conn->c_currentber;
1006 conn->c_currentber = NULL;
1008 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1009 /* log, close and send error */
1010 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1016 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1017 /* log, close and send error */
1018 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1025 if(tag == LDAP_REQ_BIND) {
1026 /* immediately abandon all exiting operations upon BIND */
1027 connection_abandon( conn );
1030 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1032 if ( conn->c_conn_state == SLAP_C_BINDING
1033 || conn->c_conn_state == SLAP_C_CLOSING )
1035 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1036 conn->c_n_ops_pending++;
1037 slap_op_add( &conn->c_pending_ops, op );
1040 conn->c_n_ops_executing++;
1041 connection_op_activate( conn, op );
1045 if ( conn->c_struct_state != SLAP_C_USED ) {
1046 /* connection must have got closed underneath us */
1050 assert( conn->c_struct_state == SLAP_C_USED );
1056 connection_resched( Connection *conn )
1060 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1063 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1064 Debug( LDAP_DEBUG_TRACE,
1065 "connection_resched: attempting closing conn=%ld sd=%d\n",
1066 conn->c_connid, sd, 0 );
1068 ldap_pvt_thread_mutex_lock( &connections_mutex );
1069 connection_close( conn );
1070 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1074 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1075 /* other states need different handling */
1079 for( op = slap_op_pop( &conn->c_pending_ops );
1081 op = slap_op_pop( &conn->c_pending_ops ) )
1083 /* pending operations should not be marked for abandonment */
1084 assert(!op->o_abandon);
1086 conn->c_n_ops_pending--;
1087 conn->c_n_ops_executing++;
1089 connection_op_activate( conn, op );
1091 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1098 static int connection_op_activate( Connection *conn, Operation *op )
1103 ber_tag_t tag = op->o_tag;
1105 if(tag == LDAP_REQ_BIND) {
1106 conn->c_conn_state = SLAP_C_BINDING;
1109 if ( conn->c_dn != NULL ) {
1110 tmpdn = ch_strdup( conn->c_dn );
1115 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1116 arg->co_conn = conn;
1119 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
1120 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1121 (void) dn_normalize( arg->co_op->o_ndn );
1123 arg->co_op->o_protocol = conn->c_protocol;
1124 arg->co_op->o_connid = conn->c_connid;
1126 arg->co_op->o_authtype = conn->c_authtype;
1127 arg->co_op->o_authmech = conn->c_authmech != NULL
1128 ? ch_strdup( conn->c_authmech ) : NULL;
1130 slap_op_add( &conn->c_ops, arg->co_op );
1132 if( tmpdn != NULL ) {
1136 status = ldap_pvt_thread_pool_submit( &connection_pool,
1137 connection_operation, (void *) arg );
1139 if ( status != 0 ) {
1140 Debug( LDAP_DEBUG_ANY,
1141 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1142 /* should move op to pending list */
1148 int connection_write(ber_socket_t s)
1151 assert( connections != NULL );
1153 ldap_pvt_thread_mutex_lock( &connections_mutex );
1155 c = connection_get( s );
1157 slapd_clr_write( s, 0);
1160 Debug( LDAP_DEBUG_ANY,
1161 "connection_write(%ld): no connection!\n",
1164 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1170 Debug( LDAP_DEBUG_TRACE,
1171 "connection_write(%d): waking output for id=%ld\n",
1172 s, c->c_connid, 0 );
1174 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1176 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1177 slapd_set_read( s, 1 );
1178 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1179 slapd_set_write( s, 1 );
1180 connection_return( c );
1181 ldap_pvt_thread_mutex_unlock( &connections_mutex );