13 /* we need LBER internals */
14 #include "../../libraries/liblber/lber-int.h"
16 /* protected by connections_mutex */
17 static ldap_pvt_thread_mutex_t connections_mutex;
18 static Connection *connections = NULL;
19 static unsigned long conn_nextid = 0;
21 /* structure state (protected by connections_mutex) */
22 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
23 #define SLAP_C_UNUSED 0x01
24 #define SLAP_C_USED 0x02
26 /* connection state (protected by c_mutex ) */
27 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
28 #define SLAP_C_INACTIVE 0x01 /* zero threads */
29 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
30 #define SLAP_C_BINDING 0x03 /* binding */
31 #define SLAP_C_CLOSING 0x04 /* closing */
33 char* connection_state2str( int state ) {
35 case SLAP_C_INVALID: return "!";
36 case SLAP_C_INACTIVE: return "|";
37 case SLAP_C_ACTIVE: return "";
38 case SLAP_C_BINDING: return "B";
39 case SLAP_C_CLOSING: return "C";
45 static Connection* connection_get( ber_socket_t s );
47 static int connection_input( Connection *c );
48 static void connection_close( Connection *c );
50 static int connection_op_activate( Connection *conn, Operation *op );
51 static int connection_resched( Connection *conn );
59 * Initialize connection management infrastructure.
61 int connections_init(void)
63 assert( connections == NULL );
65 if( connections != NULL) {
66 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
71 /* should check return of every call */
72 ldap_pvt_thread_mutex_init( &connections_mutex );
74 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
76 if( connections == NULL ) {
77 Debug( LDAP_DEBUG_ANY,
78 "connections_init: allocation (%d*%ld) of connection array failed.\n",
79 dtblsize, (long) sizeof(Connection), 0 );
83 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
84 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
87 * per entry initialization of the Connection array initialization
88 * will be done by connection_init()
95 * Destroy connection management infrastructure.
97 int connections_destroy(void)
101 /* should check return of every call */
103 if( connections == NULL) {
104 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
109 for ( i = 0; i < dtblsize; i++ ) {
110 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
111 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
112 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
113 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
120 ldap_pvt_thread_mutex_destroy( &connections_mutex );
125 * shutdown all connections
127 int connections_shutdown(void)
131 ldap_pvt_thread_mutex_lock( &connections_mutex );
133 for ( i = 0; i < dtblsize; i++ ) {
134 if( connections[i].c_struct_state != SLAP_C_USED ) {
138 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
140 /* connections_mutex and c_mutex are locked */
141 connection_closing( &connections[i] );
142 connection_close( &connections[i] );
144 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
147 ldap_pvt_thread_mutex_unlock( &connections_mutex );
153 * Timeout idle connections.
155 int connections_timeout_idle(time_t now)
161 ldap_pvt_thread_mutex_lock( &connections_mutex );
163 for( c = connection_first( &connindex );
165 c = connection_next( c, &connindex ) )
167 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
169 connection_closing( c );
170 connection_close( c );
174 connection_done( c );
176 ldap_pvt_thread_mutex_unlock( &connections_mutex );
181 static Connection* connection_get( ber_socket_t s )
183 /* connections_mutex should be locked by caller */
187 Debug( LDAP_DEBUG_ARGS,
188 "connection_get(%ld)\n",
191 assert( connections != NULL );
193 if(s == AC_SOCKET_INVALID) {
200 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
207 for(i=0; i<dtblsize; i++) {
208 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
209 assert( connections[i].c_conn_state == SLAP_C_INVALID );
210 assert( connections[i].c_sb == 0 );
214 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
215 assert( connections[i].c_conn_state == SLAP_C_INVALID );
216 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
220 /* state can actually change from used -> unused by resched,
221 * so don't assert details here.
224 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
233 ldap_pvt_thread_mutex_lock( &c->c_mutex );
235 if( c->c_struct_state != SLAP_C_USED ) {
236 /* connection must have been closed due to resched */
238 assert( c->c_conn_state == SLAP_C_INVALID );
239 assert( !ber_pvt_sb_in_use( c->c_sb ) );
241 Debug( LDAP_DEBUG_TRACE,
242 "connection_get(%d): connection not used.\n",
245 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
249 Debug( LDAP_DEBUG_TRACE,
250 "connection_get(%d): got connid=%ld\n",
255 assert( c->c_struct_state == SLAP_C_USED );
256 assert( c->c_conn_state != SLAP_C_INVALID );
257 assert( ber_pvt_sb_in_use( c->c_sb ) );
259 c->c_activitytime = slap_get_time();
265 static void connection_return( Connection *c )
267 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
270 long connection_init(
277 assert( connections != NULL );
279 if( s == AC_SOCKET_INVALID ) {
280 Debug( LDAP_DEBUG_ANY,
281 "connection_init(%ld): invalid.\n",
288 assert( s < dtblsize );
291 ldap_pvt_thread_mutex_lock( &connections_mutex );
302 for( i=0; i < dtblsize; i++) {
303 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
304 assert( connections[i].c_sb == 0 );
309 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
310 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
315 assert( connections[i].c_struct_state == SLAP_C_USED );
316 assert( connections[i].c_conn_state != SLAP_C_INVALID );
317 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
321 Debug( LDAP_DEBUG_ANY,
322 "connection_init(%d): connection table full (%d/%d).\n",
324 ldap_pvt_thread_mutex_unlock( &connections_mutex );
331 assert( c->c_struct_state != SLAP_C_USED );
332 assert( c->c_conn_state == SLAP_C_INVALID );
334 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
337 c->c_client_name = NULL;
338 c->c_client_addr = NULL;
340 c->c_pending_ops = NULL;
341 c->c_authmech = NULL;
342 c->c_authstate = NULL;
344 c->c_sb = ber_sockbuf_alloc( );
346 /* should check status of thread calls */
347 ldap_pvt_thread_mutex_init( &c->c_mutex );
348 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
349 ldap_pvt_thread_cond_init( &c->c_write_cv );
351 c->c_struct_state = SLAP_C_UNUSED;
354 ldap_pvt_thread_mutex_lock( &c->c_mutex );
356 assert( c->c_struct_state == SLAP_C_UNUSED );
357 assert( c->c_dn == NULL );
358 assert( c->c_cdn == NULL );
359 assert( c->c_client_name == NULL );
360 assert( c->c_client_addr == NULL );
361 assert( c->c_ops == NULL );
362 assert( c->c_pending_ops == NULL );
363 assert( c->c_authmech == NULL );
364 assert( c->c_authstate == NULL );
366 c->c_client_name = ch_strdup( name == NULL ? "" : name );
367 c->c_client_addr = ch_strdup( addr );
369 c->c_n_ops_received = 0;
370 c->c_n_ops_executing = 0;
371 c->c_n_ops_pending = 0;
372 c->c_n_ops_completed = 0;
378 c->c_activitytime = c->c_starttime = slap_get_time();
380 ber_pvt_sb_set_desc( c->c_sb, s );
381 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
383 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
384 Debug( LDAP_DEBUG_ANY,
385 "connection_init(%d, %s, %s): set nonblocking failed\n",
386 s, c->c_client_name, c->c_client_addr);
389 id = c->c_connid = conn_nextid++;
391 c->c_conn_state = SLAP_C_INACTIVE;
392 c->c_struct_state = SLAP_C_USED;
394 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
395 ldap_pvt_thread_mutex_unlock( &connections_mutex );
397 backend_connection_init(c);
403 connection_destroy( Connection *c )
405 /* note: connections_mutex should be locked by caller */
407 assert( connections != NULL );
409 assert( c->c_struct_state != SLAP_C_UNUSED );
410 assert( c->c_conn_state != SLAP_C_INVALID );
411 assert( c->c_ops == NULL );
413 backend_connection_destroy(c);
417 c->c_activitytime = c->c_starttime = 0;
419 if(c->c_dn != NULL) {
423 if(c->c_cdn != NULL) {
427 if(c->c_client_name != NULL) {
428 free(c->c_client_name);
429 c->c_client_name = NULL;
431 if(c->c_client_addr != NULL) {
432 free(c->c_client_addr);
433 c->c_client_addr = NULL;
435 if(c->c_authmech != NULL ) {
437 c->c_authmech = NULL;
439 if(c->c_authstate != NULL ) {
440 free(c->c_authstate);
441 c->c_authstate = NULL;
444 if ( ber_pvt_sb_in_use(c->c_sb) ) {
445 int sd = ber_pvt_sb_get_desc(c->c_sb);
447 slapd_remove( sd, 0 );
448 ber_pvt_sb_close( c->c_sb );
450 Statslog( LDAP_DEBUG_STATS,
451 "conn=%d fd=%d closed.\n",
452 c->c_connid, sd, 0, 0, 0 );
455 ber_pvt_sb_destroy( c->c_sb );
457 c->c_conn_state = SLAP_C_INVALID;
458 c->c_struct_state = SLAP_C_UNUSED;
461 int connection_state_closing( Connection *c )
463 /* c_mutex must be locked by caller */
467 assert( c->c_struct_state == SLAP_C_USED );
469 state = c->c_conn_state;
471 assert( state != SLAP_C_INVALID );
473 return state == SLAP_C_CLOSING;
476 void connection_closing( Connection *c )
478 assert( connections != NULL );
480 assert( c->c_struct_state == SLAP_C_USED );
481 assert( c->c_conn_state != SLAP_C_INVALID );
483 /* c_mutex must be locked by caller */
485 if( c->c_conn_state != SLAP_C_CLOSING ) {
488 Debug( LDAP_DEBUG_TRACE,
489 "connection_closing: readying conn=%ld sd=%d for close.\n",
490 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
492 /* update state to closing */
493 c->c_conn_state = SLAP_C_CLOSING;
495 /* don't listen on this port anymore */
496 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
498 /* shutdown I/O -- not yet implemented */
500 /* abandon active operations */
501 for( o = c->c_ops; o != NULL; o = o->o_next ) {
502 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
504 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
507 /* remove pending operations */
508 for( o = slap_op_pop( &c->c_pending_ops );
510 o = slap_op_pop( &c->c_pending_ops ) )
515 /* wake write blocked operations */
516 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
517 ldap_pvt_thread_cond_signal( &c->c_write_cv );
521 static void connection_close( Connection *c )
523 assert( connections != NULL );
525 assert( c->c_struct_state == SLAP_C_USED );
526 assert( c->c_conn_state == SLAP_C_CLOSING );
528 /* note: connections_mutex and c_mutex should be locked by caller */
530 if( c->c_ops != NULL ) {
531 Debug( LDAP_DEBUG_TRACE,
532 "connection_close: deferring conn=%ld sd=%d.\n",
533 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
538 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
539 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
541 connection_destroy( c );
544 unsigned long connections_nextid(void)
547 assert( connections != NULL );
549 ldap_pvt_thread_mutex_lock( &connections_mutex );
553 ldap_pvt_thread_mutex_unlock( &connections_mutex );
558 Connection* connection_first( ber_socket_t *index )
560 assert( connections != NULL );
561 assert( index != NULL );
563 ldap_pvt_thread_mutex_lock( &connections_mutex );
567 return connection_next(NULL, index);
570 Connection* connection_next( Connection *c, ber_socket_t *index )
572 assert( connections != NULL );
573 assert( index != NULL );
574 assert( *index <= dtblsize );
577 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
582 for(; *index < dtblsize; (*index)++) {
583 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
584 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
592 if( connections[*index].c_struct_state == SLAP_C_USED ) {
593 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
594 c = &connections[(*index)++];
598 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
599 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
603 ldap_pvt_thread_mutex_lock( &c->c_mutex );
609 void connection_done( Connection *c )
611 assert( connections != NULL );
614 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
617 ldap_pvt_thread_mutex_unlock( &connections_mutex );
621 * connection_activity - handle the request operation op on connection
622 * conn. This routine figures out what kind of operation it is and
623 * calls the appropriate stub to handle it.
627 connection_operation( void *arg_v )
630 struct co_arg *arg = arg_v;
631 ber_tag_t tag = arg->co_op->o_tag;
632 Connection *conn = arg->co_conn;
634 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
636 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
640 rc = do_bind( conn, arg->co_op );
643 case LDAP_REQ_UNBIND:
644 rc = do_unbind( conn, arg->co_op );
648 rc = do_add( conn, arg->co_op );
651 case LDAP_REQ_DELETE:
652 rc = do_delete( conn, arg->co_op );
655 case LDAP_REQ_MODRDN:
656 rc = do_modrdn( conn, arg->co_op );
659 case LDAP_REQ_MODIFY:
660 rc = do_modify( conn, arg->co_op );
663 case LDAP_REQ_COMPARE:
664 rc = do_compare( conn, arg->co_op );
667 case LDAP_REQ_SEARCH:
668 rc = do_search( conn, arg->co_op );
671 case LDAP_REQ_ABANDON:
672 rc = do_abandon( conn, arg->co_op );
676 case LDAP_REQ_EXTENDED:
677 rc = do_extended( conn, arg->co_op );
682 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
683 arg->co_op->o_tag, 0, 0 );
687 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
689 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
691 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
693 conn->c_n_ops_executing--;
694 conn->c_n_ops_completed++;
696 slap_op_remove( &conn->c_ops, arg->co_op );
697 slap_op_free( arg->co_op );
700 free( (char *) arg );
704 case LDAP_REQ_UNBIND:
705 /* c_mutex is locked */
706 connection_closing( conn );
710 if( conn->c_conn_state == SLAP_C_BINDING) {
711 conn->c_conn_state = SLAP_C_ACTIVE;
713 conn->c_bind_in_progress = ( rc == LDAP_SASL_BIND_IN_PROGRESS );
716 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
718 if( active_threads < 1 ) {
719 ldap_pvt_thread_cond_signal(&active_threads_cond);
721 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
723 connection_resched( conn );
725 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
730 int connection_read(ber_socket_t s)
734 assert( connections != NULL );
736 ldap_pvt_thread_mutex_lock( &connections_mutex );
738 /* get (locked) connection */
739 c = connection_get( s );
742 Debug( LDAP_DEBUG_ANY,
743 "connection_read(%ld): no connection!\n",
748 ldap_pvt_thread_mutex_unlock( &connections_mutex );
754 if( c->c_conn_state == SLAP_C_CLOSING ) {
755 Debug( LDAP_DEBUG_TRACE,
756 "connection_read(%d): closing, ignoring input for id=%ld\n",
759 connection_return( c );
760 ldap_pvt_thread_mutex_unlock( &connections_mutex );
764 Debug( LDAP_DEBUG_TRACE,
765 "connection_read(%d): checking for input on id=%ld\n",
768 #define CONNECTION_INPUT_LOOP 1
770 #ifdef DATA_READY_LOOP
771 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
772 #elif CONNECTION_INPUT_LOOP
776 rc = connection_input( c );
780 Debug( LDAP_DEBUG_TRACE,
781 "connection_read(%d): input error=%d id=%ld, closing.\n",
782 s, rc, c->c_connid );
784 /* connections_mutex and c_mutex are locked */
785 connection_closing( c );
786 connection_close( c );
789 connection_return( c );
790 ldap_pvt_thread_mutex_unlock( &connections_mutex );
805 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
807 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
812 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
813 != LDAP_TAG_MESSAGE )
817 Debug( LDAP_DEBUG_TRACE,
818 "ber_get_next on fd %d failed errno %d (%s)\n",
819 ber_pvt_sb_get_desc( conn->c_sb ), err,
820 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
821 Debug( LDAP_DEBUG_TRACE,
822 "\t*** got %ld of %lu so far\n",
823 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
824 conn->c_currentber->ber_len, 0 );
826 if ( err != EWOULDBLOCK && err != EAGAIN ) {
827 /* log, close and send error */
828 ber_free( conn->c_currentber, 1 );
829 conn->c_currentber = NULL;
836 ber = conn->c_currentber;
837 conn->c_currentber = NULL;
839 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
840 /* log, close and send error */
841 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
847 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
848 /* log, close and send error */
849 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
856 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
858 if ( conn->c_conn_state == SLAP_C_BINDING
859 || conn->c_conn_state == SLAP_C_CLOSING )
861 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
862 conn->c_n_ops_pending++;
863 slap_op_add( &conn->c_pending_ops, op );
866 conn->c_n_ops_executing++;
867 connection_op_activate( conn, op );
871 if ( conn->c_struct_state != SLAP_C_USED ) {
872 /* connection must have got closed underneath us */
876 assert( conn->c_struct_state == SLAP_C_USED );
882 connection_resched( Connection *conn )
886 if( conn->c_conn_state == SLAP_C_CLOSING ) {
887 Debug( LDAP_DEBUG_TRACE,
888 "connection_resched: attempting closing conn=%ld sd=%d.\n",
889 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
891 connection_close( conn );
895 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
896 /* other states need different handling */
900 for( op = slap_op_pop( &conn->c_pending_ops );
902 op = slap_op_pop( &conn->c_pending_ops ) )
904 /* pending operations should not be marked for abandonment */
905 assert(!op->o_abandon);
907 conn->c_n_ops_pending--;
908 conn->c_n_ops_executing++;
910 connection_op_activate( conn, op );
912 if ( conn->c_conn_state == SLAP_C_BINDING ) {
919 static int connection_op_activate( Connection *conn, Operation *op )
924 ber_tag_t tag = op->o_tag;
926 if ( conn->c_dn != NULL ) {
927 tmpdn = ch_strdup( conn->c_dn );
932 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
936 arg->co_op->o_bind_in_progress = conn->c_bind_in_progress;
938 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
939 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
941 arg->co_op->o_protocol = conn->c_protocol;
943 arg->co_op->o_authtype = conn->c_authtype;
944 arg->co_op->o_authmech = conn->c_authmech != NULL
945 ? ch_strdup( conn->c_authmech ) : NULL;
947 slap_op_add( &conn->c_ops, arg->co_op );
949 if(tag == LDAP_REQ_BIND) {
950 conn->c_conn_state = SLAP_C_BINDING;
953 if( tmpdn != NULL ) {
957 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
959 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
961 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
962 connection_operation, (void *) arg );
965 Debug( LDAP_DEBUG_ANY,
966 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
968 /* should move op to pending list */
974 int connection_write(ber_socket_t s)
977 assert( connections != NULL );
979 ldap_pvt_thread_mutex_lock( &connections_mutex );
981 c = connection_get( s );
983 slapd_clr_write( s, 0);
986 Debug( LDAP_DEBUG_ANY,
987 "connection_write(%ld): no connection!\n",
990 ldap_pvt_thread_mutex_unlock( &connections_mutex );
996 Debug( LDAP_DEBUG_TRACE,
997 "connection_write(%d): waking output for id=%ld\n",
1000 ldap_pvt_thread_cond_signal( &c->c_write_cv );
1002 connection_return( c );
1003 ldap_pvt_thread_mutex_unlock( &connections_mutex );