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",
241 assert( c->c_struct_state == SLAP_C_USED );
242 assert( c->c_conn_state != SLAP_C_INVALID );
243 assert( ber_pvt_sb_in_use( c->c_sb ) );
245 c->c_activitytime = slap_get_time();
251 static void connection_return( Connection *c )
253 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
256 long connection_init(
263 assert( connections != NULL );
265 if( s == AC_SOCKET_INVALID ) {
266 Debug( LDAP_DEBUG_ANY,
267 "connection_init(%ld): invalid.\n",
274 assert( s < dtblsize );
277 ldap_pvt_thread_mutex_lock( &connections_mutex );
288 for( i=0; i < dtblsize; i++) {
289 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
290 assert( connections[i].c_sb == 0 );
295 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
296 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
301 assert( connections[i].c_struct_state == SLAP_C_USED );
302 assert( connections[i].c_conn_state != SLAP_C_INVALID );
303 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
307 Debug( LDAP_DEBUG_ANY,
308 "connection_init(%d): connection table full (%d/%d).\n",
310 ldap_pvt_thread_mutex_unlock( &connections_mutex );
317 assert( c->c_struct_state != SLAP_C_USED );
318 assert( c->c_conn_state == SLAP_C_INVALID );
320 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
323 c->c_client_name = NULL;
324 c->c_client_addr = NULL;
326 c->c_pending_ops = NULL;
328 c->c_sb = ber_sockbuf_alloc( );
330 /* should check status of thread calls */
331 ldap_pvt_thread_mutex_init( &c->c_mutex );
332 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
333 ldap_pvt_thread_cond_init( &c->c_write_cv );
335 c->c_struct_state = SLAP_C_UNUSED;
338 ldap_pvt_thread_mutex_lock( &c->c_mutex );
340 assert( c->c_struct_state == SLAP_C_UNUSED );
341 assert( c->c_dn == NULL );
342 assert( c->c_cdn == NULL );
343 assert( c->c_client_name == NULL );
344 assert( c->c_client_addr == NULL );
345 assert( c->c_ops == NULL );
346 assert( c->c_pending_ops == NULL );
348 c->c_client_name = ch_strdup( name == NULL ? "" : name );
349 c->c_client_addr = ch_strdup( addr );
351 c->c_n_ops_received = 0;
353 c->c_n_ops_executing = 0;
354 c->c_n_ops_pending = 0;
355 c->c_n_ops_completed = 0;
358 c->c_activitytime = c->c_starttime = slap_get_time();
360 ber_pvt_sb_set_desc( c->c_sb, s );
361 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
363 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
364 Debug( LDAP_DEBUG_ANY,
365 "connection_init(%d, %s, %s): set nonblocking failed\n",
366 s, c->c_client_name, c->c_client_addr);
369 id = c->c_connid = conn_nextid++;
371 c->c_conn_state = SLAP_C_INACTIVE;
372 c->c_struct_state = SLAP_C_USED;
374 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
375 ldap_pvt_thread_mutex_unlock( &connections_mutex );
377 backend_connection_init(c);
383 connection_destroy( Connection *c )
385 /* note: connections_mutex should be locked by caller */
387 assert( connections != NULL );
389 assert( c->c_struct_state != SLAP_C_UNUSED );
390 assert( c->c_conn_state != SLAP_C_INVALID );
391 assert( c->c_ops == NULL );
393 backend_connection_destroy(c);
400 c->c_activitytime = c->c_starttime = 0;
402 if(c->c_dn != NULL) {
406 if(c->c_cdn != NULL) {
410 if(c->c_client_name != NULL) {
411 free(c->c_client_name);
412 c->c_client_name = NULL;
414 if(c->c_client_addr != NULL) {
415 free(c->c_client_addr);
416 c->c_client_addr = NULL;
419 if ( ber_pvt_sb_in_use(c->c_sb) ) {
420 int sd = ber_pvt_sb_get_desc(c->c_sb);
422 slapd_remove( sd, 0 );
423 ber_pvt_sb_close( c->c_sb );
425 Statslog( LDAP_DEBUG_STATS,
426 "conn=%d fd=%d closed.\n",
427 c->c_connid, sd, 0, 0, 0 );
430 ber_pvt_sb_destroy( c->c_sb );
432 c->c_conn_state = SLAP_C_INVALID;
433 c->c_struct_state = SLAP_C_UNUSED;
436 int connection_state_closing( Connection *c )
438 /* c_mutex must be locked by caller */
442 assert( c->c_struct_state == SLAP_C_USED );
444 state = c->c_conn_state;
446 assert( state != SLAP_C_INVALID );
448 return state == SLAP_C_CLOSING;
451 void connection_closing( Connection *c )
453 assert( connections != NULL );
455 assert( c->c_struct_state == SLAP_C_USED );
456 assert( c->c_conn_state != SLAP_C_INVALID );
458 /* c_mutex must be locked by caller */
460 if( c->c_conn_state != SLAP_C_CLOSING ) {
463 Debug( LDAP_DEBUG_TRACE,
464 "connection_closing: readying conn=%ld sd=%d for close.\n",
465 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
467 /* update state to closing */
468 c->c_conn_state = SLAP_C_CLOSING;
470 /* don't listen on this port anymore */
471 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
473 /* shutdown I/O -- not yet implemented */
475 /* abandon active operations */
476 for( o = c->c_ops; o != NULL; o = o->o_next ) {
477 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
479 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
482 /* remove pending operations */
483 for( o = slap_op_pop( &c->c_pending_ops );
485 o = slap_op_pop( &c->c_pending_ops ) )
490 /* wake write blocked operations */
491 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
492 ldap_pvt_thread_cond_signal( &c->c_write_cv );
496 static void connection_close( Connection *c )
498 assert( connections != NULL );
500 assert( c->c_struct_state == SLAP_C_USED );
501 assert( c->c_conn_state == SLAP_C_CLOSING );
503 /* note: connections_mutex and c_mutex should be locked by caller */
505 if( c->c_ops != NULL ) {
506 Debug( LDAP_DEBUG_TRACE,
507 "connection_close: deferring conn=%ld sd=%d.\n",
508 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
513 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
514 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
516 connection_destroy( c );
519 unsigned long connections_nextid(void)
522 assert( connections != NULL );
524 ldap_pvt_thread_mutex_lock( &connections_mutex );
528 ldap_pvt_thread_mutex_unlock( &connections_mutex );
533 Connection* connection_first( ber_socket_t *index )
535 assert( connections != NULL );
536 assert( index != NULL );
538 ldap_pvt_thread_mutex_lock( &connections_mutex );
542 return connection_next(NULL, index);
545 Connection* connection_next( Connection *c, ber_socket_t *index )
547 assert( connections != NULL );
548 assert( index != NULL );
549 assert( *index <= dtblsize );
552 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
557 for(; *index < dtblsize; (*index)++) {
558 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
559 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
567 if( connections[*index].c_struct_state == SLAP_C_USED ) {
568 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
569 c = &connections[(*index)++];
573 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
574 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
578 ldap_pvt_thread_mutex_lock( &c->c_mutex );
584 void connection_done( Connection *c )
586 assert( connections != NULL );
589 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
592 ldap_pvt_thread_mutex_unlock( &connections_mutex );
596 * connection_activity - handle the request operation op on connection
597 * conn. This routine figures out what kind of operation it is and
598 * calls the appropriate stub to handle it.
602 connection_operation( void *arg_v )
604 struct co_arg *arg = arg_v;
605 ber_tag_t tag = arg->co_op->o_tag;
606 Connection *conn = arg->co_conn;
609 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
611 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
616 do_bind( conn, arg->co_op );
620 case LDAP_REQ_UNBIND_30:
622 case LDAP_REQ_UNBIND:
623 do_unbind( conn, arg->co_op );
627 do_add( conn, arg->co_op );
631 case LDAP_REQ_DELETE_30:
633 case LDAP_REQ_DELETE:
634 do_delete( conn, arg->co_op );
637 case LDAP_REQ_MODRDN:
638 do_modrdn( conn, arg->co_op );
641 case LDAP_REQ_MODIFY:
642 do_modify( conn, arg->co_op );
645 case LDAP_REQ_COMPARE:
646 do_compare( conn, arg->co_op );
649 case LDAP_REQ_SEARCH:
650 do_search( conn, arg->co_op );
654 case LDAP_REQ_ABANDON_30:
656 case LDAP_REQ_ABANDON:
657 do_abandon( conn, arg->co_op );
661 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
662 arg->co_op->o_tag, 0, 0 );
667 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
669 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
672 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
675 conn->c_n_ops_completed++;
678 slap_op_remove( &conn->c_ops, arg->co_op );
679 slap_op_free( arg->co_op );
682 free( (char *) arg );
687 case LDAP_REQ_UNBIND_30:
689 case LDAP_REQ_UNBIND:
690 /* c_mutex is locked */
691 connection_closing( conn );
695 if( conn->c_conn_state == SLAP_C_BINDING) {
696 conn->c_conn_state = SLAP_C_ACTIVE;
700 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
702 if( active_threads < 1 ) {
703 ldap_pvt_thread_cond_signal(&active_threads_cond);
705 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
707 connection_resched( conn );
709 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
714 int connection_read(ber_socket_t s)
718 assert( connections != NULL );
720 ldap_pvt_thread_mutex_lock( &connections_mutex );
722 /* get (locked) connection */
723 c = connection_get( s );
726 Debug( LDAP_DEBUG_ANY,
727 "connection_read(%ld): no connection!\n",
732 ldap_pvt_thread_mutex_unlock( &connections_mutex );
736 if( c->c_conn_state == SLAP_C_CLOSING ) {
737 Debug( LDAP_DEBUG_TRACE,
738 "connection_read(%d): closing, ignoring input for id=%ld\n",
741 connection_return( c );
742 ldap_pvt_thread_mutex_unlock( &connections_mutex );
746 Debug( LDAP_DEBUG_TRACE,
747 "connection_read(%d): checking for input on id=%ld\n",
750 #define CONNECTION_INPUT_LOOP 1
752 #ifdef DATA_READY_LOOP
753 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
754 #elif CONNECTION_INPUT_LOOP
758 rc = connection_input( c );
762 Debug( LDAP_DEBUG_TRACE,
763 "connection_read(%d): input error=%d id=%ld, closing.\n",
764 s, rc, c->c_connid );
766 /* connections_mutex and c_mutex are locked */
767 connection_closing( c );
768 connection_close( c );
771 connection_return( c );
772 ldap_pvt_thread_mutex_unlock( &connections_mutex );
787 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
789 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
794 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
795 != LDAP_TAG_MESSAGE )
799 Debug( LDAP_DEBUG_TRACE,
800 "ber_get_next on fd %d failed errno %d (%s)\n",
801 ber_pvt_sb_get_desc( conn->c_sb ), err,
802 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
803 Debug( LDAP_DEBUG_TRACE,
804 "\t*** got %ld of %lu so far\n",
805 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
806 conn->c_currentber->ber_len, 0 );
808 if ( err != EWOULDBLOCK && err != EAGAIN ) {
809 /* log, close and send error */
810 ber_free( conn->c_currentber, 1 );
811 conn->c_currentber = NULL;
818 ber = conn->c_currentber;
819 conn->c_currentber = NULL;
821 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
822 /* log, close and send error */
823 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
829 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
830 /* log, close and send error */
831 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
839 if ( conn->c_version == 30 ) {
840 (void) ber_skip_tag( ber, &len );
844 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
846 if ( conn->c_conn_state == SLAP_C_BINDING
847 || conn->c_conn_state == SLAP_C_CLOSING )
849 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
850 slap_op_add( &conn->c_pending_ops, op );
853 connection_op_activate( conn, op );
857 if ( conn->c_struct_state != SLAP_C_USED ) {
858 /* connection must have got closed underneath us */
862 assert( conn->c_struct_state == SLAP_C_USED );
868 connection_resched( Connection *conn )
872 if( conn->c_conn_state == SLAP_C_CLOSING ) {
873 Debug( LDAP_DEBUG_TRACE,
874 "connection_resched: attempting closing conn=%ld sd=%d.\n",
875 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
877 connection_close( conn );
881 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
882 /* other states need different handling */
886 for( op = slap_op_pop( &conn->c_pending_ops );
888 op = slap_op_pop( &conn->c_pending_ops ) )
890 /* pending operations should not be marked for abandonment */
891 assert(!op->o_abandon);
893 connection_op_activate( conn, op );
895 if ( conn->c_conn_state == SLAP_C_BINDING ) {
902 static int connection_op_activate( Connection *conn, Operation *op )
907 ber_tag_t tag = op->o_tag;
909 if ( conn->c_dn != NULL ) {
910 tmpdn = ch_strdup( conn->c_dn );
915 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
919 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
920 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
922 slap_op_add( &conn->c_ops, arg->co_op );
924 if(tag == LDAP_REQ_BIND) {
925 conn->c_conn_state = SLAP_C_BINDING;
928 if ( tmpdn != NULL ) {
932 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
934 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
936 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
937 connection_operation, (void *) arg );
940 Debug( LDAP_DEBUG_ANY,
941 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
943 /* should move op to pending list */
949 int connection_write(ber_socket_t s)
952 assert( connections != NULL );
954 ldap_pvt_thread_mutex_lock( &connections_mutex );
956 c = connection_get( s );
958 slapd_clr_write( s, 0);
961 Debug( LDAP_DEBUG_ANY,
962 "connection_write(%ld): no connection!\n",
965 ldap_pvt_thread_mutex_unlock( &connections_mutex );
969 Debug( LDAP_DEBUG_TRACE,
970 "connection_write(%d): waking output for id=%ld\n",
973 ldap_pvt_thread_cond_signal( &c->c_write_cv );
975 connection_return( c );
976 ldap_pvt_thread_mutex_unlock( &connections_mutex );