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;
383 c->c_sb = ber_sockbuf_alloc( );
384 c->c_currentber = NULL;
386 /* should check status of thread calls */
387 ldap_pvt_thread_mutex_init( &c->c_mutex );
388 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
389 ldap_pvt_thread_cond_init( &c->c_write_cv );
391 c->c_struct_state = SLAP_C_UNUSED;
394 ldap_pvt_thread_mutex_lock( &c->c_mutex );
396 assert( c->c_struct_state == SLAP_C_UNUSED );
397 assert( c->c_authmech == NULL );
398 assert( c->c_dn == NULL );
399 assert( c->c_cdn == NULL );
400 assert( c->c_listener_url == NULL );
401 assert( c->c_peer_domain == NULL );
402 assert( c->c_peer_name == NULL );
403 assert( c->c_sock_name == NULL );
404 assert( c->c_ops == NULL );
405 assert( c->c_pending_ops == NULL );
406 assert( c->c_sasl_bind_mech == NULL );
407 assert( c->c_sasl_context == NULL );
408 assert( c->c_currentber == NULL );
410 c->c_listener_url = ch_strdup( url );
411 c->c_peer_domain = ch_strdup( dnsname );
412 c->c_peer_name = ch_strdup( peername );
413 c->c_sock_name = ch_strdup( sockname );
415 c->c_n_ops_received = 0;
416 c->c_n_ops_executing = 0;
417 c->c_n_ops_pending = 0;
418 c->c_n_ops_completed = 0;
424 /* assume LDAPv3 until bind */
425 c->c_protocol = LDAP_VERSION3;
427 c->c_activitytime = c->c_starttime = slap_get_time();
429 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp, LBER_SBIOD_LEVEL_PROVIDER,
431 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
432 LBER_SBIOD_LEVEL_PROVIDER, NULL );
434 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
436 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK, c /* non-NULL */ ) < 0 ) {
437 Debug( LDAP_DEBUG_ANY,
438 "connection_init(%d, %s): set nonblocking failed\n",
439 s, c->c_peer_name, 0 );
443 id = c->c_connid = conn_nextid++;
445 c->c_conn_state = SLAP_C_INACTIVE;
446 c->c_struct_state = SLAP_C_USED;
451 c->c_needs_tls_accept = 1;
454 c->c_needs_tls_accept = 0;
458 slap_sasl_external( c, ssf, authid );
460 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
461 ldap_pvt_thread_mutex_unlock( &connections_mutex );
463 backend_connection_init(c);
469 connection_destroy( Connection *c )
471 /* note: connections_mutex should be locked by caller */
474 assert( connections != NULL );
476 assert( c->c_struct_state != SLAP_C_UNUSED );
477 assert( c->c_conn_state != SLAP_C_INVALID );
478 assert( c->c_ops == NULL );
480 backend_connection_destroy(c);
485 c->c_activitytime = c->c_starttime = 0;
487 if(c->c_authmech != NULL ) {
489 c->c_authmech = NULL;
491 if(c->c_dn != NULL) {
495 if(c->c_cdn != NULL) {
499 if(c->c_listener_url != NULL) {
500 free(c->c_listener_url);
501 c->c_listener_url = NULL;
503 if(c->c_peer_domain != NULL) {
504 free(c->c_peer_domain);
505 c->c_peer_domain = NULL;
507 if(c->c_peer_name != NULL) {
510 * If peer was a domain socket, unlink. Mind you,
511 * they may be un-named. Should we leave this to
514 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
515 char *path = c->c_peer_name + 5;
520 #endif /* LDAP_PF_LOCAL */
522 free(c->c_peer_name);
523 c->c_peer_name = NULL;
525 if(c->c_sock_name != NULL) {
526 free(c->c_sock_name);
527 c->c_sock_name = NULL;
530 c->c_sasl_bind_in_progress = 0;
531 if(c->c_sasl_bind_mech != NULL) {
532 free(c->c_sasl_bind_mech);
533 c->c_sasl_bind_mech = NULL;
536 slap_sasl_close( c );
538 if ( c->c_currentber != NULL ) {
539 ber_free( c->c_currentber, 1 );
540 c->c_currentber = NULL;
543 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
544 if ( sd != AC_SOCKET_INVALID ) {
545 slapd_remove( sd, 0 );
547 Statslog( LDAP_DEBUG_STATS,
548 "conn=%ld fd=%d closed\n",
549 c->c_connid, sd, 0, 0, 0 );
552 ber_sockbuf_free( c->c_sb );
553 c->c_sb = ber_sockbuf_alloc( );
555 c->c_conn_state = SLAP_C_INVALID;
556 c->c_struct_state = SLAP_C_UNUSED;
559 int connection_state_closing( Connection *c )
561 /* c_mutex must be locked by caller */
565 assert( c->c_struct_state == SLAP_C_USED );
567 state = c->c_conn_state;
569 assert( state != SLAP_C_INVALID );
571 return state == SLAP_C_CLOSING;
574 static void connection_abandon( Connection *c )
576 /* c_mutex must be locked by caller */
580 for( o = c->c_ops; o != NULL; o = o->o_next ) {
581 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
583 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
586 /* remove pending operations */
587 for( o = slap_op_pop( &c->c_pending_ops );
589 o = slap_op_pop( &c->c_pending_ops ) )
595 void connection_closing( Connection *c )
597 assert( connections != NULL );
599 assert( c->c_struct_state == SLAP_C_USED );
600 assert( c->c_conn_state != SLAP_C_INVALID );
602 /* c_mutex must be locked by caller */
604 if( c->c_conn_state != SLAP_C_CLOSING ) {
607 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
608 Debug( LDAP_DEBUG_TRACE,
609 "connection_closing: readying conn=%ld sd=%d for close\n",
610 c->c_connid, sd, 0 );
612 /* update state to closing */
613 c->c_conn_state = SLAP_C_CLOSING;
615 /* don't listen on this port anymore */
616 slapd_clr_read( sd, 1 );
618 /* abandon active operations */
619 connection_abandon( c );
621 /* wake write blocked operations */
622 slapd_clr_write( sd, 1 );
623 ldap_pvt_thread_cond_signal( &c->c_write_cv );
627 static void connection_close( Connection *c )
631 assert( connections != NULL );
633 assert( c->c_struct_state == SLAP_C_USED );
634 assert( c->c_conn_state == SLAP_C_CLOSING );
636 /* note: connections_mutex and c_mutex should be locked by caller */
638 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
639 if( c->c_ops != NULL ) {
640 Debug( LDAP_DEBUG_TRACE,
641 "connection_close: deferring conn=%ld sd=%d\n",
642 c->c_connid, sd, 0 );
647 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
648 c->c_connid, sd, 0 );
650 connection_destroy( c );
653 unsigned long connections_nextid(void)
656 assert( connections != NULL );
658 ldap_pvt_thread_mutex_lock( &connections_mutex );
662 ldap_pvt_thread_mutex_unlock( &connections_mutex );
667 Connection* connection_first( ber_socket_t *index )
669 assert( connections != NULL );
670 assert( index != NULL );
672 ldap_pvt_thread_mutex_lock( &connections_mutex );
676 return connection_next(NULL, index);
679 Connection* connection_next( Connection *c, ber_socket_t *index )
681 assert( connections != NULL );
682 assert( index != NULL );
683 assert( *index <= dtblsize );
686 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
691 for(; *index < dtblsize; (*index)++) {
692 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
693 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
701 if( connections[*index].c_struct_state == SLAP_C_USED ) {
702 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
703 c = &connections[(*index)++];
707 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
708 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
712 ldap_pvt_thread_mutex_lock( &c->c_mutex );
718 void connection_done( Connection *c )
720 assert( connections != NULL );
723 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
726 ldap_pvt_thread_mutex_unlock( &connections_mutex );
730 * connection_activity - handle the request operation op on connection
731 * conn. This routine figures out what kind of operation it is and
732 * calls the appropriate stub to handle it.
736 connection_operation( void *arg_v )
739 struct co_arg *arg = arg_v;
740 ber_tag_t tag = arg->co_op->o_tag;
741 Connection *conn = arg->co_conn;
743 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
745 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
747 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
748 Debug( LDAP_DEBUG_ANY, "connection_operation: "
749 "error: SASL bind in progress (tag=%ld).\n",
751 send_ldap_result( conn, arg->co_op,
752 rc = LDAP_OPERATIONS_ERROR,
753 NULL, "SASL bind in progress", NULL, NULL );
754 goto operations_error;
759 rc = do_bind( conn, arg->co_op );
762 case LDAP_REQ_UNBIND:
763 rc = do_unbind( conn, arg->co_op );
767 rc = do_add( conn, arg->co_op );
770 case LDAP_REQ_DELETE:
771 rc = do_delete( conn, arg->co_op );
774 case LDAP_REQ_MODRDN:
775 rc = do_modrdn( conn, arg->co_op );
778 case LDAP_REQ_MODIFY:
779 rc = do_modify( conn, arg->co_op );
782 case LDAP_REQ_COMPARE:
783 rc = do_compare( conn, arg->co_op );
786 case LDAP_REQ_SEARCH:
787 rc = do_search( conn, arg->co_op );
790 case LDAP_REQ_ABANDON:
791 rc = do_abandon( conn, arg->co_op );
794 case LDAP_REQ_EXTENDED:
795 rc = do_extended( conn, arg->co_op );
799 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
801 arg->co_op->o_tag = LBER_ERROR;
802 send_ldap_disconnect( conn, arg->co_op,
803 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
808 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
811 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
813 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
815 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
817 conn->c_n_ops_executing--;
818 conn->c_n_ops_completed++;
820 slap_op_remove( &conn->c_ops, arg->co_op );
821 slap_op_free( arg->co_op );
824 free( (char *) arg );
829 case LDAP_REQ_UNBIND:
830 /* c_mutex is locked */
831 connection_closing( conn );
835 conn->c_sasl_bind_in_progress =
836 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
838 if( conn->c_conn_state == SLAP_C_BINDING) {
839 conn->c_conn_state = SLAP_C_ACTIVE;
843 connection_resched( conn );
845 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
850 int connection_read(ber_socket_t s)
854 assert( connections != NULL );
856 ldap_pvt_thread_mutex_lock( &connections_mutex );
858 /* get (locked) connection */
859 c = connection_get( s );
862 Debug( LDAP_DEBUG_ANY,
863 "connection_read(%ld): no connection!\n",
868 ldap_pvt_thread_mutex_unlock( &connections_mutex );
874 if( c->c_conn_state == SLAP_C_CLOSING ) {
875 Debug( LDAP_DEBUG_TRACE,
876 "connection_read(%d): closing, ignoring input for id=%ld\n",
879 connection_return( c );
880 ldap_pvt_thread_mutex_unlock( &connections_mutex );
884 Debug( LDAP_DEBUG_TRACE,
885 "connection_read(%d): checking for input on id=%ld\n",
889 if ( c->c_is_tls && c->c_needs_tls_accept ) {
890 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
895 Debug( LDAP_DEBUG_TRACE,
896 "connection_read(%d): TLS accept error error=%d id=%ld, closing\n",
897 s, rc, c->c_connid );
899 c->c_needs_tls_accept = 0;
900 /* connections_mutex and c_mutex are locked */
901 connection_closing( c );
903 /* Drain input before close, to allow SSL error codes
904 * to propagate to client. */
911 rc = select(s+1, &rfd, NULL, NULL, &tv);
913 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
916 connection_close( c );
918 } else if ( rc == 0 ) {
919 c->c_needs_tls_accept = 0;
922 /* we need to let SASL know */
923 slap_sasl_external( c, ssf, authid );
926 connection_return( c );
927 ldap_pvt_thread_mutex_unlock( &connections_mutex );
932 #define CONNECTION_INPUT_LOOP 1
934 #ifdef DATA_READY_LOOP
935 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
936 #elif CONNECTION_INPUT_LOOP
940 /* How do we do this without getting into a busy loop ? */
941 rc = connection_input( c );
945 Debug( LDAP_DEBUG_TRACE,
946 "connection_read(%d): input error=%d id=%ld, closing.\n",
947 s, rc, c->c_connid );
949 /* connections_mutex and c_mutex are locked */
950 connection_closing( c );
951 connection_close( c );
954 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
955 slapd_set_read( s, 1 );
956 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
957 slapd_set_write( s, 1 );
958 connection_return( c );
959 ldap_pvt_thread_mutex_unlock( &connections_mutex );
974 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
976 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
982 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
983 if ( tag != LDAP_TAG_MESSAGE ) {
987 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
989 Debug( LDAP_DEBUG_TRACE,
990 "ber_get_next on fd %d failed errno=%d (%s)\n",
991 sd, err, sock_errstr(err) );
993 if ( err != EWOULDBLOCK && err != EAGAIN ) {
994 /* log, close and send error */
995 ber_free( conn->c_currentber, 1 );
996 conn->c_currentber = NULL;
1003 ber = conn->c_currentber;
1004 conn->c_currentber = NULL;
1006 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1007 /* log, close and send error */
1008 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1014 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1015 /* log, close and send error */
1016 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1023 if(tag == LDAP_REQ_BIND) {
1024 /* immediately abandon all exiting operations upon BIND */
1025 connection_abandon( conn );
1028 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1030 if ( conn->c_conn_state == SLAP_C_BINDING
1031 || conn->c_conn_state == SLAP_C_CLOSING )
1033 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1034 conn->c_n_ops_pending++;
1035 slap_op_add( &conn->c_pending_ops, op );
1038 conn->c_n_ops_executing++;
1039 connection_op_activate( conn, op );
1043 if ( conn->c_struct_state != SLAP_C_USED ) {
1044 /* connection must have got closed underneath us */
1048 assert( conn->c_struct_state == SLAP_C_USED );
1054 connection_resched( Connection *conn )
1058 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1061 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1062 Debug( LDAP_DEBUG_TRACE,
1063 "connection_resched: attempting closing conn=%ld sd=%d\n",
1064 conn->c_connid, sd, 0 );
1066 ldap_pvt_thread_mutex_lock( &connections_mutex );
1067 connection_close( conn );
1068 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1072 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1073 /* other states need different handling */
1077 for( op = slap_op_pop( &conn->c_pending_ops );
1079 op = slap_op_pop( &conn->c_pending_ops ) )
1081 /* pending operations should not be marked for abandonment */
1082 assert(!op->o_abandon);
1084 conn->c_n_ops_pending--;
1085 conn->c_n_ops_executing++;
1087 connection_op_activate( conn, op );
1089 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1096 static int connection_op_activate( Connection *conn, Operation *op )
1101 ber_tag_t tag = op->o_tag;
1103 if(tag == LDAP_REQ_BIND) {
1104 conn->c_conn_state = SLAP_C_BINDING;
1107 if ( conn->c_dn != NULL ) {
1108 tmpdn = ch_strdup( conn->c_dn );
1113 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1114 arg->co_conn = conn;
1117 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
1118 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1119 (void) dn_normalize( arg->co_op->o_ndn );
1121 arg->co_op->o_protocol = conn->c_protocol;
1122 arg->co_op->o_connid = conn->c_connid;
1124 arg->co_op->o_authtype = conn->c_authtype;
1125 arg->co_op->o_authmech = conn->c_authmech != NULL
1126 ? ch_strdup( conn->c_authmech ) : NULL;
1128 slap_op_add( &conn->c_ops, arg->co_op );
1130 if( tmpdn != NULL ) {
1134 status = ldap_pvt_thread_pool_submit( &connection_pool,
1135 connection_operation, (void *) arg );
1137 if ( status != 0 ) {
1138 Debug( LDAP_DEBUG_ANY,
1139 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
1141 /* should move op to pending list */
1147 int connection_write(ber_socket_t s)
1150 assert( connections != NULL );
1152 ldap_pvt_thread_mutex_lock( &connections_mutex );
1154 c = connection_get( s );
1156 slapd_clr_write( s, 0);
1159 Debug( LDAP_DEBUG_ANY,
1160 "connection_write(%ld): no connection!\n",
1163 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1169 Debug( LDAP_DEBUG_TRACE,
1170 "connection_write(%d): waking output for id=%ld\n",
1171 s, c->c_connid, 0 );
1173 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1175 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1176 slapd_set_read( s, 1 );
1177 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1178 slapd_set_write( s, 1 );
1179 connection_return( c );
1180 ldap_pvt_thread_mutex_unlock( &connections_mutex );