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_UNINITIALIZED );
161 if( c->c_struct_state != SLAP_C_USED ) {
162 /* connection must have been closed */
163 assert( c->c_conn_state == SLAP_C_INVALID );
164 assert( !ber_pvt_sb_in_use( c->c_sb ) );
174 for(i=0; i<dtblsize; i++) {
175 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
176 assert( connections[i].c_conn_state == SLAP_C_INVALID );
177 assert( connections[i].c_sb == 0 );
181 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
182 assert( connections[i].c_conn_state == SLAP_C_INVALID );
183 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
187 assert( connections[i].c_struct_state == SLAP_C_USED );
188 assert( connections[i].c_conn_state != SLAP_C_INVALID );
189 assert( ber_pvt_sb_in_use( connections[i].c_sb ) );
191 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
200 ldap_pvt_thread_mutex_lock( &c->c_mutex );
202 /* we do this AFTER locking to aid in debugging */
203 Debug( LDAP_DEBUG_TRACE,
204 "connection_get(%d): got connid=%ld\n",
207 assert( c->c_struct_state == SLAP_C_USED );
208 assert( c->c_conn_state != SLAP_C_INVALID );
209 assert( ber_pvt_sb_in_use( c->c_sb ) );
215 static void connection_return( Connection *c )
217 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
220 long connection_init(
227 assert( connections != NULL );
230 Debug( LDAP_DEBUG_ANY,
231 "connection_init(%d): invalid.\n",
238 assert( s < dtblsize );
241 ldap_pvt_thread_mutex_lock( &connections_mutex );
252 for( i=0; i < dtblsize; i++) {
253 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
254 assert( connections[i].c_sb == 0 );
259 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
260 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
265 assert( connections[i].c_struct_state == SLAP_C_USED );
266 assert( connections[i].c_conn_state != SLAP_C_INVALID );
267 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
271 Debug( LDAP_DEBUG_ANY,
272 "connection_init(%d): connection table full (%d/%d).\n",
274 ldap_pvt_thread_mutex_unlock( &connections_mutex );
281 assert( c->c_struct_state != SLAP_C_USED );
282 assert( c->c_conn_state == SLAP_C_INVALID );
284 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
287 c->c_client_name = NULL;
288 c->c_client_addr = NULL;
290 c->c_pending_ops = NULL;
292 c->c_sb = ber_sockbuf_alloc( );
294 /* should check status of thread calls */
295 ldap_pvt_thread_mutex_init( &c->c_mutex );
296 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
297 ldap_pvt_thread_cond_init( &c->c_write_cv );
299 c->c_struct_state = SLAP_C_UNUSED;
302 ldap_pvt_thread_mutex_lock( &c->c_mutex );
304 assert( c->c_struct_state == SLAP_C_UNUSED );
305 assert( c->c_dn == NULL );
306 assert( c->c_cdn == NULL );
307 assert( c->c_client_name == NULL );
308 assert( c->c_client_addr == NULL );
309 assert( c->c_ops == NULL );
310 assert( c->c_pending_ops == NULL );
312 c->c_client_name = ch_strdup( name == NULL ? "" : name );
313 c->c_client_addr = ch_strdup( addr );
315 c->c_n_ops_received = 0;
317 c->c_n_ops_executing = 0;
318 c->c_n_ops_pending = 0;
319 c->c_n_ops_completed = 0;
322 c->c_starttime = slap_get_time();
324 ber_pvt_sb_set_desc( c->c_sb, s );
325 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
327 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
328 Debug( LDAP_DEBUG_ANY,
329 "connection_init(%d, %s, %s): set nonblocking failed\n",
330 s, c->c_client_name, c->c_client_addr);
333 id = c->c_connid = conn_nextid++;
335 c->c_conn_state = SLAP_C_INACTIVE;
336 c->c_struct_state = SLAP_C_USED;
338 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
339 ldap_pvt_thread_mutex_unlock( &connections_mutex );
345 connection_destroy( Connection *c )
347 /* note: connections_mutex should be locked by caller */
349 assert( connections != NULL );
351 assert( c->c_struct_state != SLAP_C_UNUSED );
352 assert( c->c_conn_state != SLAP_C_INVALID );
353 assert( c->c_ops == NULL );
355 c->c_struct_state = SLAP_C_UNUSED;
356 c->c_conn_state = SLAP_C_INVALID;
365 if(c->c_dn != NULL) {
369 if(c->c_cdn != NULL) {
373 if(c->c_client_name != NULL) {
374 free(c->c_client_name);
375 c->c_client_name = NULL;
377 if(c->c_client_addr != NULL) {
378 free(c->c_client_addr);
379 c->c_client_addr = NULL;
382 if ( ber_pvt_sb_in_use(c->c_sb) ) {
383 int sd = ber_pvt_sb_get_desc(c->c_sb);
385 slapd_remove( sd, 0 );
386 ber_pvt_sb_close( c->c_sb );
388 Statslog( LDAP_DEBUG_STATS,
389 "conn=%d fd=%d closed.\n",
390 c->c_connid, sd, 0, 0, 0 );
393 ber_pvt_sb_destroy( c->c_sb );
396 int connection_state_closing( Connection *c )
398 /* c_mutex must be locked by caller */
402 assert( c->c_struct_state == SLAP_C_USED );
404 state = c->c_conn_state;
406 assert( state != SLAP_C_INVALID );
408 return state == SLAP_C_CLOSING;
411 void connection_closing( Connection *c )
413 assert( connections != NULL );
415 assert( c->c_struct_state == SLAP_C_USED );
416 assert( c->c_conn_state != SLAP_C_INVALID );
418 /* c_mutex must be locked by caller */
420 if( c->c_conn_state != SLAP_C_CLOSING ) {
423 Debug( LDAP_DEBUG_TRACE,
424 "connection_closing: readying conn=%ld sd=%d for close.\n",
425 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
427 /* update state to closing */
428 c->c_conn_state = SLAP_C_CLOSING;
430 /* don't listen on this port anymore */
431 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
433 /* shutdown I/O -- not yet implemented */
435 /* abandon active operations */
436 for( o = c->c_ops; o != NULL; o = o->o_next ) {
437 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
439 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
442 /* remove pending operations */
443 for( o = slap_op_pop( &c->c_pending_ops );
445 o = slap_op_pop( &c->c_pending_ops ) )
450 /* wake write blocked operations */
451 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
452 ldap_pvt_thread_cond_signal( &c->c_write_cv );
456 static void connection_close( Connection *c )
458 assert( connections != NULL );
460 assert( c->c_struct_state == SLAP_C_USED );
461 assert( c->c_conn_state == SLAP_C_CLOSING );
463 /* note: connections_mutex and c_mutex should be locked by caller */
465 if( c->c_ops != NULL ) {
466 Debug( LDAP_DEBUG_TRACE,
467 "connection_close: deferring conn=%ld sd=%d.\n",
468 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
473 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
474 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
476 connection_destroy( c );
479 long connections_nextid(void)
482 assert( connections != NULL );
484 ldap_pvt_thread_mutex_lock( &connections_mutex );
488 ldap_pvt_thread_mutex_unlock( &connections_mutex );
493 Connection* connection_first( int *index )
495 assert( connections != NULL );
496 assert( index != NULL );
498 ldap_pvt_thread_mutex_lock( &connections_mutex );
502 return connection_next(NULL, index);
505 Connection* connection_next( Connection *c, int *index )
507 assert( connections != NULL );
508 assert( index != NULL );
509 assert( *index <= dtblsize );
512 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
517 for(; *index < dtblsize; (*index)++) {
518 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
519 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
527 if( connections[*index].c_struct_state == SLAP_C_USED ) {
528 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
529 c = &connections[(*index)++];
533 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
534 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
538 ldap_pvt_thread_mutex_lock( &c->c_mutex );
544 void connection_done( Connection *c )
546 assert( connections != NULL );
549 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
552 ldap_pvt_thread_mutex_unlock( &connections_mutex );
556 * connection_activity - handle the request operation op on connection
557 * conn. This routine figures out what kind of operation it is and
558 * calls the appropriate stub to handle it.
562 connection_operation( void *arg_v )
564 struct co_arg *arg = arg_v;
565 int tag = arg->co_op->o_tag;
566 Connection *conn = arg->co_conn;
569 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
571 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
576 do_bind( conn, arg->co_op );
580 case LDAP_REQ_UNBIND_30:
582 case LDAP_REQ_UNBIND:
583 do_unbind( conn, arg->co_op );
587 do_add( conn, arg->co_op );
591 case LDAP_REQ_DELETE_30:
593 case LDAP_REQ_DELETE:
594 do_delete( conn, arg->co_op );
597 case LDAP_REQ_MODRDN:
598 do_modrdn( conn, arg->co_op );
601 case LDAP_REQ_MODIFY:
602 do_modify( conn, arg->co_op );
605 case LDAP_REQ_COMPARE:
606 do_compare( conn, arg->co_op );
609 case LDAP_REQ_SEARCH:
610 do_search( conn, arg->co_op );
614 case LDAP_REQ_ABANDON_30:
616 case LDAP_REQ_ABANDON:
617 do_abandon( conn, arg->co_op );
621 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
622 arg->co_op->o_tag, 0, 0 );
627 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
629 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
632 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
635 conn->c_n_ops_completed++;
638 slap_op_remove( &conn->c_ops, arg->co_op );
639 slap_op_free( arg->co_op );
642 free( (char *) arg );
647 case LDAP_REQ_UNBIND_30:
649 case LDAP_REQ_UNBIND:
650 /* c_mutex is locked */
651 connection_closing( conn );
655 if( conn->c_conn_state == SLAP_C_BINDING) {
656 conn->c_conn_state = SLAP_C_ACTIVE;
660 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
662 if( active_threads < 1 ) {
663 ldap_pvt_thread_cond_signal(&active_threads_cond);
665 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
667 connection_resched( conn );
669 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
674 int connection_read(int s)
678 assert( connections != NULL );
680 ldap_pvt_thread_mutex_lock( &connections_mutex );
682 /* get (locked) connection */
683 c = connection_get( s );
686 Debug( LDAP_DEBUG_ANY,
687 "connection_read(%d): no connection!\n",
692 ldap_pvt_thread_mutex_unlock( &connections_mutex );
696 if( c->c_conn_state == SLAP_C_CLOSING ) {
697 Debug( LDAP_DEBUG_TRACE,
698 "connection_read(%d): closing, ignoring input for id=%ld\n",
701 connection_return( c );
702 ldap_pvt_thread_mutex_unlock( &connections_mutex );
706 Debug( LDAP_DEBUG_TRACE,
707 "connection_read(%d): checking for input on id=%ld\n",
710 #define CONNECTION_INPUT_LOOP 1
712 #ifdef DATA_READY_LOOP
713 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
714 #elif CONNECTION_INPUT_LOOP
718 rc = connection_input( c );
722 Debug( LDAP_DEBUG_TRACE,
723 "connection_read(%d): input error=%d id=%ld, closing.\n",
724 s, rc, c->c_connid );
726 /* connections_mutex and c_mutex are locked */
727 connection_closing( c );
728 connection_close( c );
731 connection_return( c );
732 ldap_pvt_thread_mutex_unlock( &connections_mutex );
742 unsigned long tag, len;
746 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
748 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
753 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
754 != LDAP_TAG_MESSAGE )
758 Debug( LDAP_DEBUG_TRACE,
759 "ber_get_next on fd %d failed errno %d (%s)\n",
760 ber_pvt_sb_get_desc( conn->c_sb ), err,
761 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
762 Debug( LDAP_DEBUG_TRACE,
763 "\t*** got %ld of %lu so far\n",
764 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
765 conn->c_currentber->ber_len, 0 );
767 if ( err != EWOULDBLOCK && err != EAGAIN ) {
768 /* log, close and send error */
769 ber_free( conn->c_currentber, 1 );
770 conn->c_currentber = NULL;
777 ber = conn->c_currentber;
778 conn->c_currentber = NULL;
780 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
781 /* log, close and send error */
782 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
788 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
789 /* log, close and send error */
790 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
798 if ( conn->c_version == 30 ) {
799 (void) ber_skip_tag( ber, &len );
803 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
805 if ( conn->c_conn_state == SLAP_C_BINDING
806 || conn->c_conn_state == SLAP_C_CLOSING )
808 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
809 slap_op_add( &conn->c_pending_ops, op );
812 connection_op_activate( conn, op );
816 if ( conn->c_struct_state != SLAP_C_USED ) {
817 /* connection must have got closed underneath us */
821 assert( conn->c_struct_state == SLAP_C_USED );
827 connection_resched( Connection *conn )
831 if( conn->c_conn_state == SLAP_C_CLOSING ) {
832 Debug( LDAP_DEBUG_TRACE,
833 "connection_resched: attempting closing conn=%ld sd=%d.\n",
834 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
836 connection_close( conn );
840 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
841 /* other states need different handling */
845 for( op = slap_op_pop( &conn->c_pending_ops );
847 op = slap_op_pop( &conn->c_pending_ops ) )
849 /* pending operations should not be marked for abandonment */
850 assert(!op->o_abandon);
852 connection_op_activate( conn, op );
854 if ( conn->c_conn_state == SLAP_C_BINDING ) {
861 static int connection_op_activate( Connection *conn, Operation *op )
866 unsigned long tag = op->o_tag;
868 if ( conn->c_dn != NULL ) {
869 tmpdn = ch_strdup( conn->c_dn );
874 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
878 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
879 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
881 slap_op_add( &conn->c_ops, arg->co_op );
883 if(tag == LDAP_REQ_BIND) {
884 conn->c_conn_state = SLAP_C_BINDING;
887 if ( tmpdn != NULL ) {
891 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
893 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
895 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
896 connection_operation, (void *) arg );
899 Debug( LDAP_DEBUG_ANY,
900 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
902 /* should move op to pending list */
908 int connection_write(int s)
911 assert( connections != NULL );
913 ldap_pvt_thread_mutex_lock( &connections_mutex );
915 c = connection_get( s );
917 slapd_clr_write( s, 0);
920 Debug( LDAP_DEBUG_ANY,
921 "connection_write(%d): no connection!\n",
924 ldap_pvt_thread_mutex_unlock( &connections_mutex );
928 Debug( LDAP_DEBUG_TRACE,
929 "connection_write(%d): waking output for id=%ld\n",
932 ldap_pvt_thread_cond_signal( &c->c_write_cv );
934 connection_return( c );
935 ldap_pvt_thread_mutex_unlock( &connections_mutex );