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 static Connection* connection_get( int 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 );
140 static Connection* connection_get( int s )
142 /* connections_mutex should be locked by caller */
146 Debug( LDAP_DEBUG_ARGS,
147 "connection_get(%d)\n",
150 assert( connections != NULL );
159 assert( c->c_struct_state == SLAP_C_USED );
161 ldap_pvt_thread_mutex_lock( &c->c_mutex );
163 assert( c->c_struct_state == SLAP_C_USED );
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);
377 slapd_remove( sd, 0 );
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",
684 ldap_pvt_thread_mutex_unlock( &connections_mutex );
688 if( c->c_conn_state == SLAP_C_CLOSING ) {
689 Debug( LDAP_DEBUG_TRACE,
690 "connection_read(%d): closing, ignoring input for id=%ld\n",
693 connection_return( c );
694 ldap_pvt_thread_mutex_unlock( &connections_mutex );
698 Debug( LDAP_DEBUG_TRACE,
699 "connection_read(%d): checking for input on id=%ld\n",
702 #define CONNECTION_INPUT_LOOP 1
704 #ifdef DATA_READY_LOOP
705 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
706 #elif CONNECTION_INPUT_LOOP
710 rc = connection_input( c );
714 Debug( LDAP_DEBUG_TRACE,
715 "connection_read(%d): input error=%d id=%ld, closing.\n",
716 s, rc, c->c_connid );
718 /* connections_mutex and c_mutex are locked */
719 connection_closing( c );
720 connection_close( c );
723 connection_return( c );
724 ldap_pvt_thread_mutex_unlock( &connections_mutex );
734 unsigned long tag, len;
738 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
740 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
745 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
746 != LDAP_TAG_MESSAGE )
750 Debug( LDAP_DEBUG_TRACE,
751 "ber_get_next on fd %d failed errno %d (%s)\n",
752 ber_pvt_sb_get_desc( conn->c_sb ), err,
753 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
754 Debug( LDAP_DEBUG_TRACE,
755 "\t*** got %ld of %lu so far\n",
756 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
757 conn->c_currentber->ber_len, 0 );
759 if ( err != EWOULDBLOCK && err != EAGAIN ) {
760 /* log, close and send error */
761 ber_free( conn->c_currentber, 1 );
762 conn->c_currentber = NULL;
769 ber = conn->c_currentber;
770 conn->c_currentber = NULL;
772 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
773 /* log, close and send error */
774 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
780 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
781 /* log, close and send error */
782 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
790 if ( conn->c_version == 30 ) {
791 (void) ber_skip_tag( ber, &len );
795 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
797 if ( conn->c_conn_state == SLAP_C_BINDING
798 || conn->c_conn_state == SLAP_C_CLOSING )
800 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
801 slap_op_add( &conn->c_pending_ops, op );
804 connection_op_activate( conn, op );
808 if ( conn->c_struct_state != SLAP_C_USED ) {
809 /* connection must have got closed underneath us */
813 assert( conn->c_struct_state == SLAP_C_USED );
819 connection_resched( Connection *conn )
823 if( conn->c_conn_state == SLAP_C_CLOSING ) {
824 Debug( LDAP_DEBUG_TRACE,
825 "connection_resched: attempting closing conn=%ld sd=%d.\n",
826 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
828 connection_close( conn );
832 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
833 /* other states need different handling */
837 for( op = slap_op_pop( &conn->c_pending_ops );
839 op = slap_op_pop( &conn->c_pending_ops ) )
841 /* pending operations should not be marked for abandonment */
842 assert(!op->o_abandon);
844 connection_op_activate( conn, op );
846 if ( conn->c_conn_state == SLAP_C_BINDING ) {
853 static int connection_op_activate( Connection *conn, Operation *op )
858 unsigned long tag = op->o_tag;
860 if ( conn->c_dn != NULL ) {
861 tmpdn = ch_strdup( conn->c_dn );
866 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
870 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
871 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
873 slap_op_add( &conn->c_ops, arg->co_op );
875 if(tag == LDAP_REQ_BIND) {
876 conn->c_conn_state = SLAP_C_BINDING;
879 if ( tmpdn != NULL ) {
883 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
885 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
887 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
888 connection_operation, (void *) arg );
891 Debug( LDAP_DEBUG_ANY,
892 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
894 /* should move op to pending list */
900 int connection_write(int s)
903 assert( connections != NULL );
905 ldap_pvt_thread_mutex_lock( &connections_mutex );
907 c = connection_get( s );
909 slapd_clr_write( s, 0);
912 Debug( LDAP_DEBUG_ANY,
913 "connection_write(%d): no connection!\n",
916 ldap_pvt_thread_mutex_unlock( &connections_mutex );
920 Debug( LDAP_DEBUG_TRACE,
921 "connection_write(%d): waking output for id=%ld\n",
924 ldap_pvt_thread_cond_signal( &c->c_write_cv );
926 connection_return( c );
927 ldap_pvt_thread_mutex_unlock( &connections_mutex );