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 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 void slapd_remove(int s);
34 static Connection* connection_get( int s );
36 static int connection_input( Connection *c );
37 static void connection_close( Connection *c );
39 static int connection_op_activate( Connection *conn, Operation *op );
40 static int connection_resched( Connection *conn );
48 * Initialize connection management infrastructure.
50 int connections_init(void)
52 assert( connections == NULL );
54 if( connections != NULL) {
55 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
60 /* should check return of every call */
61 ldap_pvt_thread_mutex_init( &connections_mutex );
63 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
65 if( connections == NULL ) {
66 Debug( LDAP_DEBUG_ANY,
67 "connections_init: allocation (%d*%ld) of connection array failed.\n",
68 dtblsize, (long) sizeof(Connection), 0 );
72 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
73 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
76 * per entry initialization of the Connection array initialization
77 * will be done by connection_init()
84 * Destroy connection management infrastructure.
86 int connections_destroy(void)
90 /* should check return of every call */
92 if( connections == NULL) {
93 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
98 for ( i = 0; i < dtblsize; i++ ) {
99 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
100 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
101 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
102 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
109 ldap_pvt_thread_mutex_destroy( &connections_mutex );
114 * shutdown all connections
116 int connections_shutdown(void)
120 ldap_pvt_thread_mutex_lock( &connections_mutex );
122 for ( i = 0; i < dtblsize; i++ ) {
123 if( connections[i].c_struct_state != SLAP_C_USED ) {
127 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
129 /* connections_mutex and c_mutex are locked */
130 connection_closing( &connections[i] );
131 connection_close( &connections[i] );
133 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
136 ldap_pvt_thread_mutex_unlock( &connections_mutex );
141 static Connection* connection_get( int s )
143 /* connections_mutex should be locked by caller */
147 Debug( LDAP_DEBUG_ARGS,
148 "connection_get(%d)\n",
151 assert( connections != NULL );
160 assert( c->c_struct_state == SLAP_C_USED );
162 ldap_pvt_thread_mutex_lock( &c->c_mutex );
164 assert( c->c_conn_state != SLAP_C_INVALID );
165 assert( ber_pvt_sb_in_use( c->c_sb ) );
172 for(i=0; i<dtblsize; i++) {
173 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
174 assert( connections[i].c_conn_state == SLAP_C_INVALID );
175 assert( connections[i].c_sb == 0 );
179 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
180 assert( connections[i].c_conn_state == SLAP_C_INVALID );
181 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
185 assert( connections[i].c_struct_state == SLAP_C_USED );
186 assert( connections[i].c_conn_state != SLAP_C_INVALID );
187 assert( ber_pvt_sb_in_use( connections[i].c_sb ) );
189 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
191 ldap_pvt_thread_mutex_lock( &c->c_mutex );
199 /* we do this AFTER locking to aid in debugging */
200 Debug( LDAP_DEBUG_TRACE,
201 "connection_get(%d): got connid=%ld\n",
207 static void connection_return( Connection *c )
209 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
212 long connection_init(
219 assert( connections != NULL );
222 Debug( LDAP_DEBUG_ANY,
223 "connection_init(%d): invalid.\n",
230 assert( s < dtblsize );
233 ldap_pvt_thread_mutex_lock( &connections_mutex );
244 for( i=0; i < dtblsize; i++) {
245 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
246 assert( connections[i].c_sb == 0 );
251 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
252 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
257 assert( connections[i].c_struct_state == SLAP_C_USED );
258 assert( connections[i].c_conn_state != SLAP_C_INVALID );
259 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
263 Debug( LDAP_DEBUG_ANY,
264 "connection_init(%d): connection table full (%d/%d).\n",
266 ldap_pvt_thread_mutex_unlock( &connections_mutex );
273 assert( c->c_struct_state != SLAP_C_USED );
274 assert( c->c_conn_state == SLAP_C_INVALID );
276 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
279 c->c_client_name = NULL;
280 c->c_client_addr = NULL;
282 c->c_pending_ops = NULL;
284 c->c_sb = ber_sockbuf_alloc( );
286 /* should check status of thread calls */
287 ldap_pvt_thread_mutex_init( &c->c_mutex );
288 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
289 ldap_pvt_thread_cond_init( &c->c_write_cv );
291 c->c_struct_state = SLAP_C_UNUSED;
294 ldap_pvt_thread_mutex_lock( &c->c_mutex );
296 assert( c->c_struct_state == SLAP_C_UNUSED );
297 assert( c->c_dn == NULL );
298 assert( c->c_cdn == NULL );
299 assert( c->c_client_name == NULL );
300 assert( c->c_client_addr == NULL );
301 assert( c->c_ops == NULL );
302 assert( c->c_pending_ops == NULL );
304 c->c_client_name = ch_strdup( name == NULL ? "" : name );
305 c->c_client_addr = ch_strdup( addr );
307 c->c_n_ops_received = 0;
309 c->c_n_ops_executing = 0;
310 c->c_n_ops_pending = 0;
311 c->c_n_ops_completed = 0;
314 c->c_starttime = slap_get_time();
316 ber_pvt_sb_set_desc( c->c_sb, s );
317 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
319 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
320 Debug( LDAP_DEBUG_ANY,
321 "connection_init(%d, %s, %s): set nonblocking failed\n",
322 s, c->c_client_name, c->c_client_addr);
325 id = c->c_connid = conn_nextid++;
327 c->c_conn_state = SLAP_C_INACTIVE;
328 c->c_struct_state = SLAP_C_USED;
330 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
331 ldap_pvt_thread_mutex_unlock( &connections_mutex );
337 connection_destroy( Connection *c )
339 /* note: connections_mutex should be locked by caller */
341 assert( connections != NULL );
343 assert( c->c_struct_state != SLAP_C_UNUSED );
344 assert( c->c_conn_state != SLAP_C_INVALID );
345 assert( c->c_ops == NULL );
347 c->c_struct_state = SLAP_C_UNUSED;
348 c->c_conn_state = SLAP_C_INVALID;
357 if(c->c_dn != NULL) {
361 if(c->c_cdn != NULL) {
365 if(c->c_client_name != NULL) {
366 free(c->c_client_name);
367 c->c_client_name = NULL;
369 if(c->c_client_addr != NULL) {
370 free(c->c_client_addr);
371 c->c_client_addr = NULL;
374 if ( ber_pvt_sb_in_use(c->c_sb) ) {
375 int sd = ber_pvt_sb_get_desc(c->c_sb);
378 ber_pvt_sb_close( c->c_sb );
380 Statslog( LDAP_DEBUG_STATS,
381 "conn=%d fd=%d closed.\n",
382 c->c_connid, sd, 0, 0, 0 );
385 ber_pvt_sb_destroy( c->c_sb );
388 int connection_state_closing( Connection *c )
390 /* c_mutex must be locked by caller */
394 assert( c->c_struct_state == SLAP_C_USED );
396 state = c->c_conn_state;
398 assert( state != SLAP_C_INVALID );
400 return state == SLAP_C_CLOSING;
403 void connection_closing( Connection *c )
405 assert( connections != NULL );
407 assert( c->c_struct_state == SLAP_C_USED );
408 assert( c->c_conn_state != SLAP_C_INVALID );
410 /* c_mutex must be locked by caller */
412 if( c->c_conn_state != SLAP_C_CLOSING ) {
415 Debug( LDAP_DEBUG_TRACE,
416 "connection_closing: readying conn=%ld sd=%d for close.\n",
417 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
419 /* update state to closing */
420 c->c_conn_state = SLAP_C_CLOSING;
422 /* don't listen on this port anymore */
423 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
425 /* shutdown I/O -- not yet implemented */
427 /* abandon active operations */
428 for( o = c->c_ops; o != NULL; o = o->o_next ) {
429 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
431 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
434 /* remove pending operations */
435 for( o = slap_op_pop( &c->c_pending_ops );
437 o = slap_op_pop( &c->c_pending_ops ) )
442 /* wake write blocked operations */
443 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
444 ldap_pvt_thread_cond_signal( &c->c_write_cv );
448 static void connection_close( Connection *c )
450 assert( connections != NULL );
452 assert( c->c_struct_state == SLAP_C_USED );
453 assert( c->c_conn_state == SLAP_C_CLOSING );
455 /* note: connections_mutex and c_mutex should be locked by caller */
457 if( c->c_ops != NULL ) {
458 Debug( LDAP_DEBUG_TRACE,
459 "connection_close: deferring conn=%ld sd=%d.\n",
460 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
465 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
466 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
468 connection_destroy( c );
471 long connections_nextid(void)
474 assert( connections != NULL );
476 ldap_pvt_thread_mutex_lock( &connections_mutex );
480 ldap_pvt_thread_mutex_unlock( &connections_mutex );
485 Connection* connection_first( int *index )
487 assert( connections != NULL );
488 assert( index != NULL );
490 ldap_pvt_thread_mutex_lock( &connections_mutex );
494 return connection_next(NULL, index);
497 Connection* connection_next( Connection *c, int *index )
499 assert( connections != NULL );
500 assert( index != NULL );
501 assert( *index <= dtblsize );
504 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
509 for(; *index < dtblsize; (*index)++) {
510 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
511 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
519 if( connections[*index].c_struct_state == SLAP_C_USED ) {
520 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
521 c = &connections[(*index)++];
525 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
526 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
530 ldap_pvt_thread_mutex_lock( &c->c_mutex );
536 void connection_done( Connection *c )
538 assert( connections != NULL );
541 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
544 ldap_pvt_thread_mutex_unlock( &connections_mutex );
548 * connection_activity - handle the request operation op on connection
549 * conn. This routine figures out what kind of operation it is and
550 * calls the appropriate stub to handle it.
554 connection_operation( void *arg_v )
556 struct co_arg *arg = arg_v;
557 int tag = arg->co_op->o_tag;
558 Connection *conn = arg->co_conn;
561 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
563 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
568 do_bind( conn, arg->co_op );
572 case LDAP_REQ_UNBIND_30:
574 case LDAP_REQ_UNBIND:
575 do_unbind( conn, arg->co_op );
579 do_add( conn, arg->co_op );
583 case LDAP_REQ_DELETE_30:
585 case LDAP_REQ_DELETE:
586 do_delete( conn, arg->co_op );
589 case LDAP_REQ_MODRDN:
590 do_modrdn( conn, arg->co_op );
593 case LDAP_REQ_MODIFY:
594 do_modify( conn, arg->co_op );
597 case LDAP_REQ_COMPARE:
598 do_compare( conn, arg->co_op );
601 case LDAP_REQ_SEARCH:
602 do_search( conn, arg->co_op );
606 case LDAP_REQ_ABANDON_30:
608 case LDAP_REQ_ABANDON:
609 do_abandon( conn, arg->co_op );
613 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
614 arg->co_op->o_tag, 0, 0 );
619 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
621 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
624 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
627 conn->c_n_ops_completed++;
630 slap_op_remove( &conn->c_ops, arg->co_op );
631 slap_op_free( arg->co_op );
634 free( (char *) arg );
639 case LDAP_REQ_UNBIND_30:
641 case LDAP_REQ_UNBIND:
642 /* c_mutex is locked */
643 connection_closing( conn );
647 if( conn->c_conn_state == SLAP_C_BINDING) {
648 conn->c_conn_state = SLAP_C_ACTIVE;
652 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
654 if( active_threads < 1 ) {
655 ldap_pvt_thread_cond_signal(&active_threads_cond);
657 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
659 connection_resched( conn );
661 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
666 int connection_read(int s)
670 assert( connections != NULL );
672 ldap_pvt_thread_mutex_lock( &connections_mutex );
674 /* get (locked) connection */
675 c = connection_get( s );
678 Debug( LDAP_DEBUG_ANY,
679 "connection_read(%d): no connection!\n",
681 ldap_pvt_thread_mutex_unlock( &connections_mutex );
685 if( c->c_conn_state == SLAP_C_CLOSING ) {
686 Debug( LDAP_DEBUG_TRACE,
687 "connection_read(%d): closing, ignoring input for id=%ld\n",
690 connection_return( c );
691 ldap_pvt_thread_mutex_unlock( &connections_mutex );
695 Debug( LDAP_DEBUG_TRACE,
696 "connection_read(%d): checking for input on id=%ld\n",
699 #define CONNECTION_INPUT_LOOP 1
701 #ifdef DATA_READY_LOOP
702 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
703 #elif CONNECTION_INPUT_LOOP
707 rc = connection_input( c );
711 Debug( LDAP_DEBUG_TRACE,
712 "connection_read(%d): input error=%d id=%ld, closing.\n",
713 s, rc, c->c_connid );
715 /* connections_mutex and c_mutex are locked */
716 connection_closing( c );
717 connection_close( c );
720 connection_return( c );
721 ldap_pvt_thread_mutex_unlock( &connections_mutex );
731 unsigned long tag, len;
735 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
737 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
742 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
743 != LDAP_TAG_MESSAGE )
747 Debug( LDAP_DEBUG_TRACE,
748 "ber_get_next on fd %d failed errno %d (%s)\n",
749 ber_pvt_sb_get_desc( conn->c_sb ), err,
750 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
751 Debug( LDAP_DEBUG_TRACE,
752 "\t*** got %ld of %lu so far\n",
753 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
754 conn->c_currentber->ber_len, 0 );
756 if ( err != EWOULDBLOCK && err != EAGAIN ) {
757 /* log, close and send error */
758 ber_free( conn->c_currentber, 1 );
759 conn->c_currentber = NULL;
766 ber = conn->c_currentber;
767 conn->c_currentber = NULL;
769 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
770 /* log, close and send error */
771 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
777 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
778 /* log, close and send error */
779 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
787 if ( conn->c_version == 30 ) {
788 (void) ber_skip_tag( ber, &len );
792 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
794 if ( conn->c_conn_state == SLAP_C_BINDING
795 || conn->c_conn_state == SLAP_C_CLOSING )
797 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
798 slap_op_add( &conn->c_pending_ops, op );
801 connection_op_activate( conn, op );
805 if ( conn->c_struct_state != SLAP_C_USED ) {
806 /* connection must have got closed underneath us */
810 assert( conn->c_struct_state == SLAP_C_USED );
816 connection_resched( Connection *conn )
820 if( conn->c_conn_state == SLAP_C_CLOSING ) {
821 Debug( LDAP_DEBUG_TRACE,
822 "connection_resched: attempting closing conn=%ld sd=%d.\n",
823 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
825 connection_close( conn );
829 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
830 /* other states need different handling */
834 for( op = slap_op_pop( &conn->c_pending_ops );
836 op = slap_op_pop( &conn->c_pending_ops ) )
838 /* pending operations should not be marked for abandonment */
839 assert(!op->o_abandon);
841 connection_op_activate( conn, op );
843 if ( conn->c_conn_state == SLAP_C_BINDING ) {
850 static int connection_op_activate( Connection *conn, Operation *op )
855 unsigned long tag = op->o_tag;
857 if ( conn->c_dn != NULL ) {
858 tmpdn = ch_strdup( conn->c_dn );
863 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
867 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
868 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
870 slap_op_add( &conn->c_ops, arg->co_op );
872 if(tag == LDAP_REQ_BIND) {
873 conn->c_conn_state = SLAP_C_BINDING;
876 if ( tmpdn != NULL ) {
880 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
882 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
884 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
885 connection_operation, (void *) arg );
888 Debug( LDAP_DEBUG_ANY,
889 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
891 /* should move op to pending list */
897 int connection_write(int s)
900 assert( connections != NULL );
902 ldap_pvt_thread_mutex_lock( &connections_mutex );
904 c = connection_get( s );
906 Debug( LDAP_DEBUG_ANY,
907 "connection_write(%d): no connection!\n",
909 ldap_pvt_thread_mutex_unlock( &connections_mutex );
913 Debug( LDAP_DEBUG_TRACE,
914 "connection_write(%d): waking output for id=%ld\n",
917 ldap_pvt_thread_cond_signal( &c->c_write_cv );
919 connection_return( c );
920 ldap_pvt_thread_mutex_unlock( &connections_mutex );