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 /* assume LDAPv3 until bind */
429 c->c_protocol = 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;
455 c->c_needs_tls_accept = 1;
458 c->c_needs_tls_accept = 0;
462 slap_sasl_external( c, ssf, authid );
464 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
465 ldap_pvt_thread_mutex_unlock( &connections_mutex );
467 backend_connection_init(c);
473 connection_destroy( Connection *c )
475 /* note: connections_mutex should be locked by caller */
478 assert( connections != NULL );
480 assert( c->c_struct_state != SLAP_C_UNUSED );
481 assert( c->c_conn_state != SLAP_C_INVALID );
482 assert( c->c_ops == NULL );
484 backend_connection_destroy(c);
489 c->c_activitytime = c->c_starttime = 0;
491 if(c->c_authmech != NULL ) {
493 c->c_authmech = NULL;
495 if(c->c_dn != NULL) {
499 if(c->c_cdn != NULL) {
503 if(c->c_listener_url != NULL) {
504 free(c->c_listener_url);
505 c->c_listener_url = NULL;
507 if(c->c_peer_domain != NULL) {
508 free(c->c_peer_domain);
509 c->c_peer_domain = NULL;
511 if(c->c_peer_name != NULL) {
514 * If peer was a domain socket, unlink. Mind you,
515 * they may be un-named. Should we leave this to
518 if (strncmp(c->c_peer_name, "PATH=", 5) == 0) {
519 char *path = c->c_peer_name + 5;
524 #endif /* LDAP_PF_LOCAL */
526 free(c->c_peer_name);
527 c->c_peer_name = NULL;
529 if(c->c_sock_name != NULL) {
530 free(c->c_sock_name);
531 c->c_sock_name = NULL;
534 c->c_sasl_bind_in_progress = 0;
535 if(c->c_sasl_bind_mech != NULL) {
536 free(c->c_sasl_bind_mech);
537 c->c_sasl_bind_mech = NULL;
540 slap_sasl_close( c );
542 if ( c->c_currentber != NULL ) {
543 ber_free( c->c_currentber, 1 );
544 c->c_currentber = NULL;
547 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
548 if ( sd != AC_SOCKET_INVALID ) {
549 slapd_remove( sd, 0 );
551 Statslog( LDAP_DEBUG_STATS,
552 "conn=%ld fd=%d closed\n",
553 c->c_connid, sd, 0, 0, 0 );
556 ber_sockbuf_free( c->c_sb );
557 c->c_sb = ber_sockbuf_alloc( );
559 c->c_conn_state = SLAP_C_INVALID;
560 c->c_struct_state = SLAP_C_UNUSED;
563 int connection_state_closing( Connection *c )
565 /* c_mutex must be locked by caller */
569 assert( c->c_struct_state == SLAP_C_USED );
571 state = c->c_conn_state;
573 assert( state != SLAP_C_INVALID );
575 return state == SLAP_C_CLOSING;
578 static void connection_abandon( Connection *c )
580 /* c_mutex must be locked by caller */
584 for( o = c->c_ops; o != NULL; o = o->o_next ) {
585 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
587 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
590 /* remove pending operations */
591 for( o = slap_op_pop( &c->c_pending_ops );
593 o = slap_op_pop( &c->c_pending_ops ) )
599 void connection_closing( Connection *c )
601 assert( connections != NULL );
603 assert( c->c_struct_state == SLAP_C_USED );
604 assert( c->c_conn_state != SLAP_C_INVALID );
606 /* c_mutex must be locked by caller */
608 if( c->c_conn_state != SLAP_C_CLOSING ) {
611 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
612 Debug( LDAP_DEBUG_TRACE,
613 "connection_closing: readying conn=%ld sd=%d for close\n",
614 c->c_connid, sd, 0 );
616 /* update state to closing */
617 c->c_conn_state = SLAP_C_CLOSING;
619 /* don't listen on this port anymore */
620 slapd_clr_read( sd, 1 );
622 /* abandon active operations */
623 connection_abandon( c );
625 /* wake write blocked operations */
626 slapd_clr_write( sd, 1 );
627 ldap_pvt_thread_cond_signal( &c->c_write_cv );
631 static void connection_close( Connection *c )
635 assert( connections != NULL );
637 assert( c->c_struct_state == SLAP_C_USED );
638 assert( c->c_conn_state == SLAP_C_CLOSING );
640 /* note: connections_mutex and c_mutex should be locked by caller */
642 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd );
643 if( c->c_ops != NULL ) {
644 Debug( LDAP_DEBUG_TRACE,
645 "connection_close: deferring conn=%ld sd=%d\n",
646 c->c_connid, sd, 0 );
651 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d\n",
652 c->c_connid, sd, 0 );
654 connection_destroy( c );
657 unsigned long connections_nextid(void)
660 assert( connections != NULL );
662 ldap_pvt_thread_mutex_lock( &connections_mutex );
666 ldap_pvt_thread_mutex_unlock( &connections_mutex );
671 Connection* connection_first( ber_socket_t *index )
673 assert( connections != NULL );
674 assert( index != NULL );
676 ldap_pvt_thread_mutex_lock( &connections_mutex );
680 return connection_next(NULL, index);
683 Connection* connection_next( Connection *c, ber_socket_t *index )
685 assert( connections != NULL );
686 assert( index != NULL );
687 assert( *index <= dtblsize );
690 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
695 for(; *index < dtblsize; (*index)++) {
696 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
697 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
705 if( connections[*index].c_struct_state == SLAP_C_USED ) {
706 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
707 c = &connections[(*index)++];
711 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
712 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
716 ldap_pvt_thread_mutex_lock( &c->c_mutex );
722 void connection_done( Connection *c )
724 assert( connections != NULL );
727 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
730 ldap_pvt_thread_mutex_unlock( &connections_mutex );
734 * connection_activity - handle the request operation op on connection
735 * conn. This routine figures out what kind of operation it is and
736 * calls the appropriate stub to handle it.
740 connection_operation( void *arg_v )
743 struct co_arg *arg = arg_v;
744 ber_tag_t tag = arg->co_op->o_tag;
745 Connection *conn = arg->co_conn;
747 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
749 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
751 if( conn->c_sasl_bind_in_progress && tag != LDAP_REQ_BIND ) {
752 Debug( LDAP_DEBUG_ANY, "connection_operation: "
753 "error: SASL bind in progress (tag=%ld).\n",
755 send_ldap_result( conn, arg->co_op,
756 rc = LDAP_OPERATIONS_ERROR,
757 NULL, "SASL bind in progress", NULL, NULL );
758 goto operations_error;
763 rc = do_bind( conn, arg->co_op );
766 case LDAP_REQ_UNBIND:
767 rc = do_unbind( conn, arg->co_op );
771 rc = do_add( conn, arg->co_op );
774 case LDAP_REQ_DELETE:
775 rc = do_delete( conn, arg->co_op );
778 case LDAP_REQ_MODRDN:
779 rc = do_modrdn( conn, arg->co_op );
782 case LDAP_REQ_MODIFY:
783 rc = do_modify( conn, arg->co_op );
786 case LDAP_REQ_COMPARE:
787 rc = do_compare( conn, arg->co_op );
790 case LDAP_REQ_SEARCH:
791 rc = do_search( conn, arg->co_op );
794 case LDAP_REQ_ABANDON:
795 rc = do_abandon( conn, arg->co_op );
798 case LDAP_REQ_EXTENDED:
799 rc = do_extended( conn, arg->co_op );
803 Debug( LDAP_DEBUG_ANY, "unknown LDAP request 0x%lx\n",
805 arg->co_op->o_tag = LBER_ERROR;
806 send_ldap_disconnect( conn, arg->co_op,
807 LDAP_PROTOCOL_ERROR, "unknown LDAP request" );
812 if( rc == SLAPD_DISCONNECT ) tag = LBER_ERROR;
815 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
817 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
819 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
821 conn->c_n_ops_executing--;
822 conn->c_n_ops_completed++;
824 slap_op_remove( &conn->c_ops, arg->co_op );
825 slap_op_free( arg->co_op );
828 free( (char *) arg );
833 case LDAP_REQ_UNBIND:
834 /* c_mutex is locked */
835 connection_closing( conn );
839 conn->c_sasl_bind_in_progress =
840 rc == LDAP_SASL_BIND_IN_PROGRESS ? 1 : 0;
842 if( conn->c_conn_state == SLAP_C_BINDING) {
843 conn->c_conn_state = SLAP_C_ACTIVE;
847 connection_resched( conn );
849 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
854 int connection_read(ber_socket_t s)
858 assert( connections != NULL );
860 ldap_pvt_thread_mutex_lock( &connections_mutex );
862 /* get (locked) connection */
863 c = connection_get( s );
866 Debug( LDAP_DEBUG_ANY,
867 "connection_read(%ld): no connection!\n",
872 ldap_pvt_thread_mutex_unlock( &connections_mutex );
878 if( c->c_conn_state == SLAP_C_CLOSING ) {
879 Debug( LDAP_DEBUG_TRACE,
880 "connection_read(%d): closing, ignoring input for id=%ld\n",
883 connection_return( c );
884 ldap_pvt_thread_mutex_unlock( &connections_mutex );
888 Debug( LDAP_DEBUG_TRACE,
889 "connection_read(%d): checking for input on id=%ld\n",
893 if ( c->c_is_tls && c->c_needs_tls_accept ) {
894 rc = ldap_pvt_tls_accept( c->c_sb, NULL );
899 Debug( LDAP_DEBUG_TRACE,
900 "connection_read(%d): TLS accept error "
901 "error=%d id=%ld, closing\n",
902 s, rc, c->c_connid );
904 c->c_needs_tls_accept = 0;
905 /* connections_mutex and c_mutex are locked */
906 connection_closing( c );
908 /* Drain input before close, to allow SSL error codes
909 * to propagate to client. */
916 rc = select(s+1, &rfd, NULL, NULL, &tv);
918 ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN,
921 connection_close( c );
923 } else if ( rc == 0 ) {
928 c->c_needs_tls_accept = 0;
930 /* we need to let SASL know */
931 ssl = (void *)ldap_pvt_tls_sb_handle( c->c_sb );
932 ssf = (slap_ssf_t) ldap_pvt_tls_get_strength( ssl );
933 authid = (char *)ldap_pvt_tls_get_peer( ssl );
934 slap_sasl_external( c, ssf, authid );
936 connection_return( c );
937 ldap_pvt_thread_mutex_unlock( &connections_mutex );
942 #ifdef HAVE_CYRUS_SASL
943 if ( c->c_sasl_layers ) {
944 c->c_sasl_layers = 0;
946 rc = ldap_pvt_sasl_install( c->c_sb, c->c_sasl_context );
948 if( rc != LDAP_SUCCESS ) {
949 Debug( LDAP_DEBUG_TRACE,
950 "connection_read(%d): SASL install error "
951 "error=%d id=%ld, closing\n",
952 s, rc, c->c_connid );
954 /* connections_mutex and c_mutex are locked */
955 connection_closing( c );
956 connection_close( c );
957 connection_return( c );
958 ldap_pvt_thread_mutex_unlock( &connections_mutex );
964 #define CONNECTION_INPUT_LOOP 1
966 #ifdef DATA_READY_LOOP
967 while( !rc && ber_sockbuf_ctrl( c->c_sb, LBER_SB_DATA_READY, NULL ) )
968 #elif CONNECTION_INPUT_LOOP
972 /* How do we do this without getting into a busy loop ? */
973 rc = connection_input( c );
977 Debug( LDAP_DEBUG_TRACE,
978 "connection_read(%d): input error=%d id=%ld, closing.\n",
979 s, rc, c->c_connid );
981 /* connections_mutex and c_mutex are locked */
982 connection_closing( c );
983 connection_close( c );
986 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
987 slapd_set_read( s, 1 );
988 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
989 slapd_set_write( s, 1 );
990 connection_return( c );
991 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1006 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
1008 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
1014 tag = ber_get_next( conn->c_sb, &len, conn->c_currentber );
1015 if ( tag != LDAP_TAG_MESSAGE ) {
1019 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1021 Debug( LDAP_DEBUG_TRACE,
1022 "ber_get_next on fd %d failed errno=%d (%s)\n",
1023 sd, err, sock_errstr(err) );
1025 if ( err != EWOULDBLOCK && err != EAGAIN ) {
1026 /* log, close and send error */
1027 ber_free( conn->c_currentber, 1 );
1028 conn->c_currentber = NULL;
1035 ber = conn->c_currentber;
1036 conn->c_currentber = NULL;
1038 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
1039 /* log, close and send error */
1040 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
1046 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
1047 /* log, close and send error */
1048 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
1055 if(tag == LDAP_REQ_BIND) {
1056 /* immediately abandon all exiting operations upon BIND */
1057 connection_abandon( conn );
1060 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
1062 if ( conn->c_conn_state == SLAP_C_BINDING
1063 || conn->c_conn_state == SLAP_C_CLOSING )
1065 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
1066 conn->c_n_ops_pending++;
1067 slap_op_add( &conn->c_pending_ops, op );
1070 conn->c_n_ops_executing++;
1071 connection_op_activate( conn, op );
1075 if ( conn->c_struct_state != SLAP_C_USED ) {
1076 /* connection must have got closed underneath us */
1080 assert( conn->c_struct_state == SLAP_C_USED );
1086 connection_resched( Connection *conn )
1090 if( conn->c_conn_state == SLAP_C_CLOSING ) {
1093 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
1094 Debug( LDAP_DEBUG_TRACE,
1095 "connection_resched: attempting closing conn=%ld sd=%d\n",
1096 conn->c_connid, sd, 0 );
1098 ldap_pvt_thread_mutex_lock( &connections_mutex );
1099 connection_close( conn );
1100 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1104 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
1105 /* other states need different handling */
1109 for( op = slap_op_pop( &conn->c_pending_ops );
1111 op = slap_op_pop( &conn->c_pending_ops ) )
1113 /* pending operations should not be marked for abandonment */
1114 assert(!op->o_abandon);
1116 conn->c_n_ops_pending--;
1117 conn->c_n_ops_executing++;
1119 connection_op_activate( conn, op );
1121 if ( conn->c_conn_state == SLAP_C_BINDING ) {
1128 static int connection_op_activate( Connection *conn, Operation *op )
1132 ber_tag_t tag = op->o_tag;
1134 if(tag == LDAP_REQ_BIND) {
1135 conn->c_conn_state = SLAP_C_BINDING;
1138 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
1139 arg->co_conn = conn;
1142 arg->co_op->o_dn = ch_strdup( conn->c_dn != NULL ? conn->c_dn : "" );
1143 arg->co_op->o_ndn = ch_strdup( arg->co_op->o_dn );
1144 (void) dn_normalize( arg->co_op->o_ndn );
1146 arg->co_op->o_protocol = conn->c_protocol;
1147 arg->co_op->o_connid = conn->c_connid;
1149 arg->co_op->o_authtype = conn->c_authtype;
1150 arg->co_op->o_authmech = conn->c_authmech != NULL
1151 ? ch_strdup( conn->c_authmech ) : NULL;
1153 slap_op_add( &conn->c_ops, arg->co_op );
1155 status = ldap_pvt_thread_pool_submit( &connection_pool,
1156 connection_operation, (void *) arg );
1158 if ( status != 0 ) {
1159 Debug( LDAP_DEBUG_ANY,
1160 "ldap_pvt_thread_pool_submit failed (%d)\n", status, 0, 0 );
1161 /* should move op to pending list */
1167 int connection_write(ber_socket_t s)
1170 assert( connections != NULL );
1172 ldap_pvt_thread_mutex_lock( &connections_mutex );
1174 c = connection_get( s );
1176 slapd_clr_write( s, 0);
1179 Debug( LDAP_DEBUG_ANY,
1180 "connection_write(%ld): no connection!\n",
1183 ldap_pvt_thread_mutex_unlock( &connections_mutex );
1189 Debug( LDAP_DEBUG_TRACE,
1190 "connection_write(%d): waking output for id=%ld\n",
1191 s, c->c_connid, 0 );
1193 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1195 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_READ, NULL ) )
1196 slapd_set_read( s, 1 );
1197 if ( ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_NEEDS_WRITE, NULL ) )
1198 slapd_set_write( s, 1 );
1199 connection_return( c );
1200 ldap_pvt_thread_mutex_unlock( &connections_mutex );