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>
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 ber_sockbuf_ctrl( connections[i].c_sb,
220 LBER_SB_OPT_GET_FD, &sd );
222 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
223 assert( connections[i].c_conn_state == SLAP_C_INVALID );
224 assert( connections[i].c_sb == 0 );
228 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
229 assert( connections[i].c_conn_state == SLAP_C_INVALID );
230 assert( sd == AC_SOCKET_INVALID );
234 /* state can actually change from used -> unused by resched,
235 * so don't assert details here.
249 ldap_pvt_thread_mutex_lock( &c->c_mutex );
251 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
252 if( c->c_struct_state != SLAP_C_USED ) {
253 /* connection must have been closed due to resched */
255 assert( c->c_conn_state == SLAP_C_INVALID );
256 assert( sd == AC_SOCKET_INVALID );
258 Debug( LDAP_DEBUG_TRACE,
259 "connection_get(%d): connection not used\n",
262 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
266 Debug( LDAP_DEBUG_TRACE,
267 "connection_get(%d): got connid=%ld\n",
272 assert( c->c_struct_state == SLAP_C_USED );
273 assert( c->c_conn_state != SLAP_C_INVALID );
274 assert( sd != AC_SOCKET_INVALID );
276 c->c_activitytime = slap_get_time();
282 static void connection_return( Connection *c )
284 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
287 long connection_init(
291 const char* peername,
292 const char* sockname,
300 assert( connections != NULL );
302 assert( dnsname != NULL );
303 assert( peername != NULL );
304 assert( sockname != NULL );
310 if( s == AC_SOCKET_INVALID ) {
311 Debug( LDAP_DEBUG_ANY,
312 "connection_init(%ld): invalid.\n",
319 assert( s < dtblsize );
322 ldap_pvt_thread_mutex_lock( &connections_mutex );
333 for( i=0; i < dtblsize; i++) {
336 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
337 assert( connections[i].c_sb == 0 );
342 sd = AC_SOCKET_INVALID;
343 if (connections[i].c_sb != NULL)
344 ber_sockbuf_ctrl( connections[i].c_sb, LBER_SB_OPT_GET_FD, &sd );
346 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
347 assert( sd == AC_SOCKET_INVALID );
352 assert( connections[i].c_struct_state == SLAP_C_USED );
353 assert( connections[i].c_conn_state != SLAP_C_INVALID );
354 assert( sd != AC_SOCKET_INVALID );
358 Debug( LDAP_DEBUG_ANY,
359 "connection_init(%d): connection table full (%d/%d)\n",
361 ldap_pvt_thread_mutex_unlock( &connections_mutex );
369 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
370 c->c_authmech = NULL;
374 c->c_listener_url = NULL;
375 c->c_peer_domain = NULL;
376 c->c_peer_name = NULL;
377 c->c_sock_name = NULL;
380 c->c_pending_ops = NULL;
382 c->c_sasl_bind_mech = NULL;
383 c->c_sasl_context = NULL;
384 c->c_sasl_extra = NULL;
386 c->c_sb = ber_sockbuf_alloc( );
387 c->c_currentber = NULL;
389 /* should check status of thread calls */
390 ldap_pvt_thread_mutex_init( &c->c_mutex );
391 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
392 ldap_pvt_thread_cond_init( &c->c_write_cv );
394 c->c_struct_state = SLAP_C_UNUSED;
397 ldap_pvt_thread_mutex_lock( &c->c_mutex );
399 assert( c->c_struct_state == SLAP_C_UNUSED );
400 assert( c->c_authmech == NULL );
401 assert( c->c_dn == NULL );
402 assert( c->c_cdn == NULL );
403 assert( c->c_listener_url == NULL );
404 assert( c->c_peer_domain == NULL );
405 assert( c->c_peer_name == NULL );
406 assert( c->c_sock_name == NULL );
407 assert( c->c_ops == NULL );
408 assert( c->c_pending_ops == NULL );
409 assert( c->c_sasl_bind_mech == NULL );
410 assert( c->c_sasl_context == NULL );
411 assert( c->c_sasl_extra == NULL );
412 assert( c->c_currentber == NULL );
414 c->c_listener_url = ch_strdup( url );
415 c->c_peer_domain = ch_strdup( dnsname );
416 c->c_peer_name = ch_strdup( peername );
417 c->c_sock_name = ch_strdup( sockname );
419 c->c_n_ops_received = 0;
420 c->c_n_ops_executing = 0;
421 c->c_n_ops_pending = 0;
422 c->c_n_ops_completed = 0;
428 /* set to zero until bind, implies LDAP_VERSION3 */
431 c->c_activitytime = c->c_starttime = slap_get_time();
433 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp, LBER_SBIOD_LEVEL_PROVIDER,
435 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
436 LBER_SBIOD_LEVEL_PROVIDER, NULL );
438 ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
440 if( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_NONBLOCK, c /* non-NULL */ ) < 0 ) {
441 Debug( LDAP_DEBUG_ANY,
442 "connection_init(%d, %s): set nonblocking failed\n",
443 s, c->c_peer_name, 0 );
447 id = c->c_connid = conn_nextid++;
449 c->c_conn_state = SLAP_C_INACTIVE;
450 c->c_struct_state = SLAP_C_USED;
452 c->c_ssf = c->c_transport_ssf = ssf;
458 c->c_needs_tls_accept = 1;
461 c->c_needs_tls_accept = 0;
466 slap_sasl_external( c, ssf, authid );
468 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
469 ldap_pvt_thread_mutex_unlock( &connections_mutex );
471 backend_connection_init(c);
476 void connection2anonymous( Connection *c )
478 assert( connections != NULL );
481 if(c->c_authmech != NULL ) {
483 c->c_authmech = NULL;
486 if(c->c_dn != NULL) {
491 if(c->c_cdn != NULL) {
496 c->c_authc_backend = NULL;
497 c->c_authz_backend = NULL;
501 connection_destroy( Connection *c )
503 /* note: connections_mutex should be locked by caller */
506 assert( connections != NULL );
508 assert( c->c_struct_state != SLAP_C_UNUSED );
509 assert( c->c_conn_state != SLAP_C_INVALID );
510 assert( c->c_ops == NULL );
512 backend_connection_destroy(c);
517 c->c_activitytime = c->c_starttime = 0;
519 connection2anonymous( c );
521 if(c->c_listener_url != NULL) {
522 free(c->c_listener_url);
523 c->c_listener_url = NULL;
526 if(c->c_peer_domain != NULL) {
527 free(c->c_peer_domain);
528 c->c_peer_domain = NULL;
530 if(c->c_peer_name != NULL) {
533 * If peer was a domain socket, unlink. Mind you,
534 * they may be un-named. Should we leave this to
537 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
538 char *path = c->c_peer_name + 5;
543 #endif /* LDAP_PF_LOCAL */
545 free(c->c_peer_name);
546 c->c_peer_name = NULL;
548 if(c->c_sock_name != NULL) {
549 free(c->c_sock_name);
550 c->c_sock_name = NULL;
553 c->c_sasl_bind_in_progress = 0;
554 if(c->c_sasl_bind_mech != NULL) {
555 free(c->c_sasl_bind_mech);
556 c->c_sasl_bind_mech = NULL;
559 slap_sasl_close( c );
561 if ( c->c_currentber != NULL ) {
562 ber_free( c->c_currentber, 1 );
563 c->c_currentber = NULL;
566 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
567 if ( sd != AC_SOCKET_INVALID ) {
568 slapd_remove( sd, 0 );
570 Statslog( LDAP_DEBUG_STATS,
571 "conn=%ld fd=%d closed\n",
572 c->c_connid, sd, 0, 0, 0 );
575 ber_sockbuf_free( c->c_sb );
576 c->c_sb = ber_sockbuf_alloc( );
578 c->c_conn_state = SLAP_C_INVALID;
579 c->c_struct_state = SLAP_C_UNUSED;
582 int connection_state_closing( Connection *c )
584 /* c_mutex must be locked by caller */
588 assert( c->c_struct_state == SLAP_C_USED );
590 state = c->c_conn_state;
592 assert( state != SLAP_C_INVALID );
594 return state == SLAP_C_CLOSING;
597 static void connection_abandon( Connection *c )
599 /* c_mutex must be locked by caller */
603 for( o = c->c_ops; o != NULL; o = o->o_next ) {
604 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
606 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
609 /* remove pending operations */
610 for( o = slap_op_pop( &c->c_pending_ops );
612 o = slap_op_pop( &c->c_pending_ops ) )
618 void connection_closing( Connection *c )
620 assert( connections != NULL );
622 assert( c->c_struct_state == SLAP_C_USED );
623 assert( c->c_conn_state != SLAP_C_INVALID );
625 /* c_mutex must be locked by caller */
627 if( c->c_conn_state != SLAP_C_CLOSING ) {
630 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
631 Debug( LDAP_DEBUG_TRACE,
632 "connection_closing: readying conn=%ld sd=%d for close\n",
633 c->c_connid, sd, 0 );
635 /* update state to closing */
636 c->c_conn_state = SLAP_C_CLOSING;
638 /* don't listen on this port anymore */
639 slapd_clr_read( sd, 1 );
641 /* abandon active operations */
642 connection_abandon( c );
644 /* wake write blocked operations */
645 slapd_clr_write( sd, 1 );
646 ldap_pvt_thread_cond_signal( &c->c_write_cv );
650 static void connection_close( Connection *c )
654 assert( connections != NULL );
656 assert( c->c_struct_state == SLAP_C_USED );
657 assert( c->c_conn_state == SLAP_C_CLOSING );
659 /* note: connections_mutex and c_mutex should be locked by caller */
661 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
662 if( c->c_ops != NULL ) {
663 Debug( LDAP_DEBUG_TRACE,
664 "connection_close: deferring conn=%ld sd=%d\n",
665 c->c_connid, sd, 0 );
670 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
671 c->c_connid, sd, 0 );
673 connection_destroy( c );
676 unsigned long connections_nextid(void)
679 assert( connections != NULL );
681 ldap_pvt_thread_mutex_lock( &connections_mutex );
685 ldap_pvt_thread_mutex_unlock( &connections_mutex );
690 Connection* connection_first( ber_socket_t *index )
692 assert( connections != NULL );
693 assert( index != NULL );
695 ldap_pvt_thread_mutex_lock( &connections_mutex );
699 return connection_next(NULL, index);
702 Connection* connection_next( Connection *c, ber_socket_t *index )
704 assert( connections != NULL );
705 assert( index != NULL );
706 assert( *index <= dtblsize );
709 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
714 for(; *index < dtblsize; (*index)++) {
715 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
716 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
724 if( connections[*index].c_struct_state == SLAP_C_USED ) {
725 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
726 c = &connections[(*index)++];
730 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
731 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
735 ldap_pvt_thread_mutex_lock( &c->c_mutex );
741 void connection_done( Connection *c )
743 assert( connections != NULL );
746 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
749 ldap_pvt_thread_mutex_unlock( &connections_mutex );
753 * connection_activity - handle the request operation op on connection
754 * conn. This routine figures out what kind of operation it is and
755 * calls the appropriate stub to handle it.
759 connection_operation( void *arg_v )
762 struct co_arg *arg = arg_v;
763 ber_tag_t tag = arg->co_op->o_tag;
764 Connection *conn = arg->co_conn;
766 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
768 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
770 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
771 Debug( LDAP_DEBUG_ANY, "connection_operation: "
772 "error: SASL bind in progress (tag=%ld).\n",
774 send_ldap_result( conn, arg->co_op,
775 rc = LDAP_OPERATIONS_ERROR,
776 NULL, "SASL bind in progress", NULL, NULL );
777 goto operations_error;
782 rc = do_bind( conn, arg->co_op );
785 case LDAP_REQ_UNBIND:
786 rc = do_unbind( conn, arg->co_op );
790 rc = do_add( conn, arg->co_op );
793 case LDAP_REQ_DELETE:
794 rc = do_delete( conn, arg->co_op );
797 case LDAP_REQ_MODRDN:
798 rc = do_modrdn( conn, arg->co_op );
801 case LDAP_REQ_MODIFY:
802 rc = do_modify( conn, arg->co_op );
805 case LDAP_REQ_COMPARE:
806 rc = do_compare( conn, arg->co_op );
809 case LDAP_REQ_SEARCH:
810 rc = do_search( conn, arg->co_op );
813 case LDAP_REQ_ABANDON:
814 rc = do_abandon( conn, arg->co_op );
817 case LDAP_REQ_EXTENDED:
818 rc = do_extended( conn, arg->co_op );
822 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
824 arg->co_op->o_tag = LBER_ERROR;
825 send_ldap_disconnect( conn, arg->co_op,
826 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
831 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
834 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
836 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
838 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
840 conn->c_n_ops_executing--;
841 conn->c_n_ops_completed++;
843 slap_op_remove( &conn->c_ops, arg->co_op );
844 slap_op_free( arg->co_op );
847 free( (char *) arg );
852 case LDAP_REQ_UNBIND:
853 /* c_mutex is locked */
854 connection_closing( conn );
858 conn->c_sasl_bind_in_progress =
859 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
861 if( conn->c_conn_state == SLAP_C_BINDING) {
862 conn->c_conn_state = SLAP_C_ACTIVE;
866 connection_resched( conn );
868 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
873 int connection_read(ber_socket_t s)
877 assert( connections != NULL );
879 ldap_pvt_thread_mutex_lock( &connections_mutex );
881 /* get (locked) connection */
882 c = connection_get( s );
885 Debug( LDAP_DEBUG_ANY,
886 "connection_read(%ld): no connection!\n",
891 ldap_pvt_thread_mutex_unlock( &connections_mutex );
897 if( c->c_conn_state == SLAP_C_CLOSING ) {
898 Debug( LDAP_DEBUG_TRACE,
899 "connection_read(%d): closing, ignoring input for id=%ld\n",
902 connection_return( c );
903 ldap_pvt_thread_mutex_unlock( &connections_mutex );
907 Debug( LDAP_DEBUG_TRACE,
908 "connection_read(%d): checking for input on id=%ld\n",
912 if ( c->c_is_tls && c->c_needs_tls_accept ) {
913 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
918 Debug( LDAP_DEBUG_TRACE,
919 "connection_read(%d): TLS accept error "
920 "error=%d id=%ld, closing\n",
921 s, rc, c->c_connid );
923 c->c_needs_tls_accept = 0;
924 /* connections_mutex and c_mutex are locked */
925 connection_closing( c );
928 /* Drain input before close, to allow SSL error codes
929 * to propagate to client. */
936 rc = select(s+1, &rfd, NULL, NULL, &tv);
938 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
942 connection_close( c );
944 } else if ( rc == 0 ) {
948 c->c_needs_tls_accept = 0;
950 /* we need to let SASL know */
951 ssl = (void *)ldap_pvt_tls_sb_handle( c->c_sb );
953 c->c_tls_ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
954 if( c->c_tls_ssf > c->c_ssf ) {
955 c->c_ssf = c->c_tls_ssf;
958 authid = (char *)ldap_pvt_tls_get_peer( ssl );
959 slap_sasl_external( c, c->c_tls_ssf, authid );
961 connection_return( c );
962 ldap_pvt_thread_mutex_unlock( &connections_mutex );
967 #ifdef HAVE_CYRUS_SASL
968 if ( c->c_sasl_layers ) {
969 c->c_sasl_layers = 0;
971 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
973 if( rc != LDAP_SUCCESS ) {
974 Debug( LDAP_DEBUG_TRACE,
975 "connection_read(%d): SASL install error "
976 "error=%d id=%ld, closing\n",
977 s, rc, c->c_connid );
979 /* connections_mutex and c_mutex are locked */
980 connection_closing( c );
981 connection_close( c );
982 connection_return( c );
983 ldap_pvt_thread_mutex_unlock( &connections_mutex );
989 #define CONNECTION_INPUT_LOOP 1
991 #ifdef DATA_READY_LOOP
992 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
993 #elif CONNECTION_INPUT_LOOP
997 /* How do we do this without getting into a busy loop ? */
998 rc = connection_input( c );
1002 Debug( LDAP_DEBUG_TRACE,
1003 "connection_read(%d): input error=%d id=%ld, closing.\n",
1004 s, rc, c->c_connid );
1006 /* connections_mutex and c_mutex are locked */
1007 connection_closing( c );
1008 connection_close( c );
1009 connection_return( c );
1010 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1014 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) ) {
1015 slapd_set_read( s, 1 );
1018 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) ) {
1019 slapd_set_write( s, 1 );
1022 connection_return( c );
1023 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1038 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1040 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1046 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1047 if ( tag != LDAP_TAG_MESSAGE ) {
1051 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1053 Debug( LDAP_DEBUG_TRACE,
1054 "ber_get_next on fd %d failed errno=%d (%s)\n",
1055 sd, err, sock_errstr(err) );
1057 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1058 /* log, close and send error */
1059 ber_free( conn->c_currentber, 1 );
1060 conn->c_currentber = NULL;
1067 ber = conn->c_currentber;
1068 conn->c_currentber = NULL;
1070 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1071 /* log, close and send error */
1072 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1078 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1079 /* log, close and send error */
1080 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1087 if(tag == LDAP_REQ_BIND) {
1088 /* immediately abandon all exiting operations upon BIND */
1089 connection_abandon( conn );
1092 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1094 if ( conn->c_conn_state == SLAP_C_BINDING
1095 || conn->c_conn_state == SLAP_C_CLOSING )
1097 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1098 conn->c_n_ops_pending++;
1099 slap_op_add( &conn->c_pending_ops, op );
1102 conn->c_n_ops_executing++;
1103 connection_op_activate( conn, op );
1107 if ( conn->c_struct_state != SLAP_C_USED ) {
1108 /* connection must have got closed underneath us */
1112 assert( conn->c_struct_state == SLAP_C_USED );
1118 connection_resched( Connection *conn )
1122 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1125 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1127 /* us trylock to avoid possible deadlock */
1128 rc = ldap_pvt_thread_mutex_trylock( &connections_mutex );
1131 Debug( LDAP_DEBUG_TRACE,
1132 "connection_resched: reaquiring locks conn=%ld sd=%d\n",
1133 conn->c_connid, sd, 0 );
1135 * reaquire locks in the right order...
1136 * this may allow another thread to close this connection,
1137 * so recheck state below.
1139 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
1140 ldap_pvt_thread_mutex_lock( &connections_mutex );
1141 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
1144 if( conn->c_conn_state != SLAP_C_CLOSING ) {
1145 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1146 "closed by other thread conn=%ld sd=%d\n",
1147 conn->c_connid, sd, 0 );
1150 Debug( LDAP_DEBUG_TRACE, "connection_resched: "
1151 "attempting closing conn=%ld sd=%d\n",
1152 conn->c_connid, sd, 0 );
1154 connection_close( conn );
1157 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1161 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1162 /* other states need different handling */
1166 for( op = slap_op_pop( &conn->c_pending_ops );
1168 op = slap_op_pop( &conn->c_pending_ops ) )
1170 /* pending operations should not be marked for abandonment */
1171 assert(!op->o_abandon);
1173 conn->c_n_ops_pending--;
1174 conn->c_n_ops_executing++;
1176 connection_op_activate( conn, op );
1178 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1185 static int connection_op_activate( Connection *conn, Operation *op )
1189 ber_tag_t tag = op->o_tag;
1191 if(tag == LDAP_REQ_BIND) {
1192 conn->c_conn_state = SLAP_C_BINDING;
1195 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1196 arg->co_conn = conn;
1199 arg->co_op->o_authz = conn->c_authz;
1200 arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1201 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1202 (void) dn_normalize( arg->co_op->o_ndn );
1203 arg->co_op->o_authtype = conn->c_authtype;
1204 arg->co_op->o_authmech = conn->c_authmech != NULL
1205 ? ch_strdup( conn->c_authmech ) : NULL;
1207 arg->co_op->o_protocol = conn->c_protocol
1208 ? conn->c_protocol : LDAP_VERSION3;
1209 arg->co_op->o_connid = conn->c_connid;
1211 slap_op_add( &conn->c_ops, arg->co_op );
1213 status = ldap_pvt_thread_pool_submit( &connection_pool,
1214 connection_operation, (void *) arg );
1216 if ( status != 0 ) {
1217 Debug( LDAP_DEBUG_ANY,
1218 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1219 /* should move op to pending list */
1225 int connection_write(ber_socket_t s)
1228 assert( connections != NULL );
1230 ldap_pvt_thread_mutex_lock( &connections_mutex );
1232 c = connection_get( s );
1234 slapd_clr_write( s, 0);
1237 Debug( LDAP_DEBUG_ANY,
1238 "connection_write(%ld): no connection!\n",
1241 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1247 Debug( LDAP_DEBUG_TRACE,
1248 "connection_write(%d): waking output for id=%ld\n",
1249 s, c->c_connid, 0 );
1251 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1253 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1254 slapd_set_read( s, 1 );
1255 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1256 slapd_set_write( s, 1 );
1257 connection_return( c );
1258 ldap_pvt_thread_mutex_unlock( &connections_mutex );