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 static Connection* connection_get( ber_socket_t s );
35 static int connection_input( Connection *c );
36 static void connection_close( Connection *c );
38 static int connection_op_activate( Connection *conn, Operation *op );
39 static int connection_resched( Connection *conn );
47 * Initialize connection management infrastructure.
49 int connections_init(void)
51 assert( connections == NULL );
53 if( connections != NULL) {
54 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
59 /* should check return of every call */
60 ldap_pvt_thread_mutex_init( &connections_mutex );
62 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
64 if( connections == NULL ) {
65 Debug( LDAP_DEBUG_ANY,
66 "connections_init: allocation (%d*%ld) of connection array failed.\n",
67 dtblsize, (long) sizeof(Connection), 0 );
71 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
72 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
75 * per entry initialization of the Connection array initialization
76 * will be done by connection_init()
83 * Destroy connection management infrastructure.
85 int connections_destroy(void)
89 /* should check return of every call */
91 if( connections == NULL) {
92 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
97 for ( i = 0; i < dtblsize; i++ ) {
98 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
99 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
100 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
101 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
108 ldap_pvt_thread_mutex_destroy( &connections_mutex );
113 * shutdown all connections
115 int connections_shutdown(void)
119 ldap_pvt_thread_mutex_lock( &connections_mutex );
121 for ( i = 0; i < dtblsize; i++ ) {
122 if( connections[i].c_struct_state != SLAP_C_USED ) {
126 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
128 /* connections_mutex and c_mutex are locked */
129 connection_closing( &connections[i] );
130 connection_close( &connections[i] );
132 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
135 ldap_pvt_thread_mutex_unlock( &connections_mutex );
141 * Timeout idle connections.
143 int connections_timeout_idle(time_t now)
149 ldap_pvt_thread_mutex_lock( &connections_mutex );
151 for( c = connection_first( &connindex );
153 c = connection_next( c, &connindex ) )
155 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
157 connection_closing( c );
158 connection_close( c );
162 connection_done( c );
164 ldap_pvt_thread_mutex_unlock( &connections_mutex );
169 static Connection* connection_get( ber_socket_t s )
171 /* connections_mutex should be locked by caller */
175 Debug( LDAP_DEBUG_ARGS,
176 "connection_get(%ld)\n",
179 assert( connections != NULL );
181 if(s == AC_SOCKET_INVALID) {
188 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
195 for(i=0; i<dtblsize; i++) {
196 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
197 assert( connections[i].c_conn_state == SLAP_C_INVALID );
198 assert( connections[i].c_sb == 0 );
202 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
203 assert( connections[i].c_conn_state == SLAP_C_INVALID );
204 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
208 /* state can actually change from used -> unused by resched,
209 * so don't assert details here.
212 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
221 ldap_pvt_thread_mutex_lock( &c->c_mutex );
223 if( c->c_struct_state != SLAP_C_USED ) {
224 /* connection must have been closed due to resched */
226 assert( c->c_conn_state == SLAP_C_INVALID );
227 assert( !ber_pvt_sb_in_use( c->c_sb ) );
229 Debug( LDAP_DEBUG_TRACE,
230 "connection_get(%d): connection not used.\n",
233 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
237 Debug( LDAP_DEBUG_TRACE,
238 "connection_get(%d): got connid=%ld\n",
243 assert( c->c_struct_state == SLAP_C_USED );
244 assert( c->c_conn_state != SLAP_C_INVALID );
245 assert( ber_pvt_sb_in_use( c->c_sb ) );
247 c->c_activitytime = slap_get_time();
253 static void connection_return( Connection *c )
255 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
258 long connection_init(
265 assert( connections != NULL );
267 if( s == AC_SOCKET_INVALID ) {
268 Debug( LDAP_DEBUG_ANY,
269 "connection_init(%ld): invalid.\n",
276 assert( s < dtblsize );
279 ldap_pvt_thread_mutex_lock( &connections_mutex );
290 for( i=0; i < dtblsize; i++) {
291 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
292 assert( connections[i].c_sb == 0 );
297 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
298 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
303 assert( connections[i].c_struct_state == SLAP_C_USED );
304 assert( connections[i].c_conn_state != SLAP_C_INVALID );
305 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
309 Debug( LDAP_DEBUG_ANY,
310 "connection_init(%d): connection table full (%d/%d).\n",
312 ldap_pvt_thread_mutex_unlock( &connections_mutex );
319 assert( c->c_struct_state != SLAP_C_USED );
320 assert( c->c_conn_state == SLAP_C_INVALID );
322 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
325 c->c_client_name = NULL;
326 c->c_client_addr = NULL;
328 c->c_pending_ops = NULL;
330 c->c_sb = ber_sockbuf_alloc( );
332 /* should check status of thread calls */
333 ldap_pvt_thread_mutex_init( &c->c_mutex );
334 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
335 ldap_pvt_thread_cond_init( &c->c_write_cv );
337 c->c_struct_state = SLAP_C_UNUSED;
340 ldap_pvt_thread_mutex_lock( &c->c_mutex );
342 assert( c->c_struct_state == SLAP_C_UNUSED );
343 assert( c->c_dn == NULL );
344 assert( c->c_cdn == NULL );
345 assert( c->c_client_name == NULL );
346 assert( c->c_client_addr == NULL );
347 assert( c->c_ops == NULL );
348 assert( c->c_pending_ops == NULL );
350 c->c_client_name = ch_strdup( name == NULL ? "" : name );
351 c->c_client_addr = ch_strdup( addr );
353 c->c_n_ops_received = 0;
354 c->c_n_ops_executing = 0;
355 c->c_n_ops_pending = 0;
356 c->c_n_ops_completed = 0;
362 c->c_activitytime = c->c_starttime = slap_get_time();
364 ber_pvt_sb_set_desc( c->c_sb, s );
365 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
367 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
368 Debug( LDAP_DEBUG_ANY,
369 "connection_init(%d, %s, %s): set nonblocking failed\n",
370 s, c->c_client_name, c->c_client_addr);
373 id = c->c_connid = conn_nextid++;
375 c->c_conn_state = SLAP_C_INACTIVE;
376 c->c_struct_state = SLAP_C_USED;
378 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
379 ldap_pvt_thread_mutex_unlock( &connections_mutex );
381 backend_connection_init(c);
387 connection_destroy( Connection *c )
389 /* note: connections_mutex should be locked by caller */
391 assert( connections != NULL );
393 assert( c->c_struct_state != SLAP_C_UNUSED );
394 assert( c->c_conn_state != SLAP_C_INVALID );
395 assert( c->c_ops == NULL );
397 backend_connection_destroy(c);
401 c->c_activitytime = c->c_starttime = 0;
403 if(c->c_dn != NULL) {
407 if(c->c_cdn != NULL) {
411 if(c->c_client_name != NULL) {
412 free(c->c_client_name);
413 c->c_client_name = NULL;
415 if(c->c_client_addr != NULL) {
416 free(c->c_client_addr);
417 c->c_client_addr = NULL;
420 if ( ber_pvt_sb_in_use(c->c_sb) ) {
421 int sd = ber_pvt_sb_get_desc(c->c_sb);
423 slapd_remove( sd, 0 );
424 ber_pvt_sb_close( c->c_sb );
426 Statslog( LDAP_DEBUG_STATS,
427 "conn=%d fd=%d closed.\n",
428 c->c_connid, sd, 0, 0, 0 );
431 ber_pvt_sb_destroy( c->c_sb );
433 c->c_conn_state = SLAP_C_INVALID;
434 c->c_struct_state = SLAP_C_UNUSED;
437 int connection_state_closing( Connection *c )
439 /* c_mutex must be locked by caller */
443 assert( c->c_struct_state == SLAP_C_USED );
445 state = c->c_conn_state;
447 assert( state != SLAP_C_INVALID );
449 return state == SLAP_C_CLOSING;
452 void connection_closing( Connection *c )
454 assert( connections != NULL );
456 assert( c->c_struct_state == SLAP_C_USED );
457 assert( c->c_conn_state != SLAP_C_INVALID );
459 /* c_mutex must be locked by caller */
461 if( c->c_conn_state != SLAP_C_CLOSING ) {
464 Debug( LDAP_DEBUG_TRACE,
465 "connection_closing: readying conn=%ld sd=%d for close.\n",
466 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
468 /* update state to closing */
469 c->c_conn_state = SLAP_C_CLOSING;
471 /* don't listen on this port anymore */
472 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
474 /* shutdown I/O -- not yet implemented */
476 /* abandon active operations */
477 for( o = c->c_ops; o != NULL; o = o->o_next ) {
478 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
480 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
483 /* remove pending operations */
484 for( o = slap_op_pop( &c->c_pending_ops );
486 o = slap_op_pop( &c->c_pending_ops ) )
491 /* wake write blocked operations */
492 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
493 ldap_pvt_thread_cond_signal( &c->c_write_cv );
497 static void connection_close( Connection *c )
499 assert( connections != NULL );
501 assert( c->c_struct_state == SLAP_C_USED );
502 assert( c->c_conn_state == SLAP_C_CLOSING );
504 /* note: connections_mutex and c_mutex should be locked by caller */
506 if( c->c_ops != NULL ) {
507 Debug( LDAP_DEBUG_TRACE,
508 "connection_close: deferring conn=%ld sd=%d.\n",
509 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
514 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
515 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
517 connection_destroy( c );
520 unsigned long connections_nextid(void)
523 assert( connections != NULL );
525 ldap_pvt_thread_mutex_lock( &connections_mutex );
529 ldap_pvt_thread_mutex_unlock( &connections_mutex );
534 Connection* connection_first( ber_socket_t *index )
536 assert( connections != NULL );
537 assert( index != NULL );
539 ldap_pvt_thread_mutex_lock( &connections_mutex );
543 return connection_next(NULL, index);
546 Connection* connection_next( Connection *c, ber_socket_t *index )
548 assert( connections != NULL );
549 assert( index != NULL );
550 assert( *index <= dtblsize );
553 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
558 for(; *index < dtblsize; (*index)++) {
559 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
560 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
568 if( connections[*index].c_struct_state == SLAP_C_USED ) {
569 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
570 c = &connections[(*index)++];
574 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
575 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
579 ldap_pvt_thread_mutex_lock( &c->c_mutex );
585 void connection_done( Connection *c )
587 assert( connections != NULL );
590 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
593 ldap_pvt_thread_mutex_unlock( &connections_mutex );
597 * connection_activity - handle the request operation op on connection
598 * conn. This routine figures out what kind of operation it is and
599 * calls the appropriate stub to handle it.
603 connection_operation( void *arg_v )
605 struct co_arg *arg = arg_v;
606 ber_tag_t tag = arg->co_op->o_tag;
607 Connection *conn = arg->co_conn;
609 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
611 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
615 do_bind( conn, arg->co_op );
618 case LDAP_REQ_UNBIND:
619 do_unbind( conn, arg->co_op );
623 do_add( conn, arg->co_op );
626 case LDAP_REQ_DELETE:
627 do_delete( conn, arg->co_op );
630 case LDAP_REQ_MODRDN:
631 do_modrdn( conn, arg->co_op );
634 case LDAP_REQ_MODIFY:
635 do_modify( conn, arg->co_op );
638 case LDAP_REQ_COMPARE:
639 do_compare( conn, arg->co_op );
642 case LDAP_REQ_SEARCH:
643 do_search( conn, arg->co_op );
646 case LDAP_REQ_ABANDON:
647 do_abandon( conn, arg->co_op );
651 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
652 arg->co_op->o_tag, 0, 0 );
656 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
658 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
660 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
662 conn->c_n_ops_executing--;
663 conn->c_n_ops_completed++;
665 slap_op_remove( &conn->c_ops, arg->co_op );
666 slap_op_free( arg->co_op );
669 free( (char *) arg );
673 case LDAP_REQ_UNBIND:
674 /* c_mutex is locked */
675 connection_closing( conn );
679 if( conn->c_conn_state == SLAP_C_BINDING) {
680 conn->c_conn_state = SLAP_C_ACTIVE;
684 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
686 if( active_threads < 1 ) {
687 ldap_pvt_thread_cond_signal(&active_threads_cond);
689 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
691 connection_resched( conn );
693 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
698 int connection_read(ber_socket_t s)
702 assert( connections != NULL );
704 ldap_pvt_thread_mutex_lock( &connections_mutex );
706 /* get (locked) connection */
707 c = connection_get( s );
710 Debug( LDAP_DEBUG_ANY,
711 "connection_read(%ld): no connection!\n",
716 ldap_pvt_thread_mutex_unlock( &connections_mutex );
722 if( c->c_conn_state == SLAP_C_CLOSING ) {
723 Debug( LDAP_DEBUG_TRACE,
724 "connection_read(%d): closing, ignoring input for id=%ld\n",
727 connection_return( c );
728 ldap_pvt_thread_mutex_unlock( &connections_mutex );
732 Debug( LDAP_DEBUG_TRACE,
733 "connection_read(%d): checking for input on id=%ld\n",
736 #define CONNECTION_INPUT_LOOP 1
738 #ifdef DATA_READY_LOOP
739 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
740 #elif CONNECTION_INPUT_LOOP
744 rc = connection_input( c );
748 Debug( LDAP_DEBUG_TRACE,
749 "connection_read(%d): input error=%d id=%ld, closing.\n",
750 s, rc, c->c_connid );
752 /* connections_mutex and c_mutex are locked */
753 connection_closing( c );
754 connection_close( c );
757 connection_return( c );
758 ldap_pvt_thread_mutex_unlock( &connections_mutex );
773 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
775 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
780 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
781 != LDAP_TAG_MESSAGE )
785 Debug( LDAP_DEBUG_TRACE,
786 "ber_get_next on fd %d failed errno %d (%s)\n",
787 ber_pvt_sb_get_desc( conn->c_sb ), err,
788 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
789 Debug( LDAP_DEBUG_TRACE,
790 "\t*** got %ld of %lu so far\n",
791 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
792 conn->c_currentber->ber_len, 0 );
794 if ( err != EWOULDBLOCK && err != EAGAIN ) {
795 /* log, close and send error */
796 ber_free( conn->c_currentber, 1 );
797 conn->c_currentber = NULL;
804 ber = conn->c_currentber;
805 conn->c_currentber = NULL;
807 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
808 /* log, close and send error */
809 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
815 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
816 /* log, close and send error */
817 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
824 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
826 if ( conn->c_conn_state == SLAP_C_BINDING
827 || conn->c_conn_state == SLAP_C_CLOSING )
829 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
830 conn->c_n_ops_pending++;
831 slap_op_add( &conn->c_pending_ops, op );
834 conn->c_n_ops_executing++;
835 connection_op_activate( conn, op );
839 if ( conn->c_struct_state != SLAP_C_USED ) {
840 /* connection must have got closed underneath us */
844 assert( conn->c_struct_state == SLAP_C_USED );
850 connection_resched( Connection *conn )
854 if( conn->c_conn_state == SLAP_C_CLOSING ) {
855 Debug( LDAP_DEBUG_TRACE,
856 "connection_resched: attempting closing conn=%ld sd=%d.\n",
857 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
859 connection_close( conn );
863 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
864 /* other states need different handling */
868 for( op = slap_op_pop( &conn->c_pending_ops );
870 op = slap_op_pop( &conn->c_pending_ops ) )
872 /* pending operations should not be marked for abandonment */
873 assert(!op->o_abandon);
875 conn->c_n_ops_pending--;
876 conn->c_n_ops_executing++;
878 connection_op_activate( conn, op );
880 if ( conn->c_conn_state == SLAP_C_BINDING ) {
887 static int connection_op_activate( Connection *conn, Operation *op )
892 ber_tag_t tag = op->o_tag;
894 if ( conn->c_dn != NULL ) {
895 tmpdn = ch_strdup( conn->c_dn );
900 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
904 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
905 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
907 arg->co_op->o_protocol = conn->c_protocol;
909 slap_op_add( &conn->c_ops, arg->co_op );
911 if(tag == LDAP_REQ_BIND) {
912 conn->c_conn_state = SLAP_C_BINDING;
915 if ( tmpdn != NULL ) {
919 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
921 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
923 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
924 connection_operation, (void *) arg );
927 Debug( LDAP_DEBUG_ANY,
928 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
930 /* should move op to pending list */
936 int connection_write(ber_socket_t s)
939 assert( connections != NULL );
941 ldap_pvt_thread_mutex_lock( &connections_mutex );
943 c = connection_get( s );
945 slapd_clr_write( s, 0);
948 Debug( LDAP_DEBUG_ANY,
949 "connection_write(%ld): no connection!\n",
952 ldap_pvt_thread_mutex_unlock( &connections_mutex );
958 Debug( LDAP_DEBUG_TRACE,
959 "connection_write(%d): waking output for id=%ld\n",
962 ldap_pvt_thread_cond_signal( &c->c_write_cv );
964 connection_return( c );
965 ldap_pvt_thread_mutex_unlock( &connections_mutex );