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 */
145 Connection *c = NULL;
147 assert( connections != NULL );
154 assert( connections[s].c_struct_state == SLAP_C_USED );
155 assert( connections[s].c_conn_state != SLAP_C_INVALID );
156 assert( ber_pvt_sb_in_use( connections[s].c_sb ) );
163 for(i=0; i<dtblsize; i++) {
164 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
165 assert( connections[i].c_conn_state == SLAP_C_INVALID );
166 assert( connections[i].c_sb == 0 );
170 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
171 assert( connections[i].c_conn_state == SLAP_C_INVALID );
172 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
176 assert( connections[i].c_struct_state == SLAP_C_USED );
177 assert( connections[i].c_conn_state != SLAP_C_INVALID );
178 assert( ber_pvt_sb_in_use( connections[i].c_sb ) );
180 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
189 /* we do this BEFORE locking to aid in debugging */
190 Debug( LDAP_DEBUG_TRACE,
191 "connection_get(%d): got connid=%ld\n",
194 ldap_pvt_thread_mutex_lock( &c->c_mutex );
199 static void connection_return( Connection *c )
201 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
204 long connection_init(
211 assert( connections != NULL );
214 Debug( LDAP_DEBUG_ANY,
215 "connection_init(%d): invalid.\n",
222 assert( s < dtblsize );
225 ldap_pvt_thread_mutex_lock( &connections_mutex );
236 for( i=0; i < dtblsize; i++) {
237 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
238 assert( connections[i].c_sb == 0 );
243 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
244 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
249 assert( connections[i].c_struct_state == SLAP_C_USED );
250 assert( connections[i].c_conn_state != SLAP_C_INVALID );
251 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
255 Debug( LDAP_DEBUG_ANY,
256 "connection_init(%d): connection table full (%d/%d).\n",
258 ldap_pvt_thread_mutex_unlock( &connections_mutex );
265 assert( c->c_struct_state != SLAP_C_USED );
266 assert( c->c_conn_state == SLAP_C_INVALID );
268 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
271 c->c_client_name = NULL;
272 c->c_client_addr = NULL;
274 c->c_pending_ops = NULL;
276 c->c_sb = ber_sockbuf_alloc( );
278 /* should check status of thread calls */
279 ldap_pvt_thread_mutex_init( &c->c_mutex );
280 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
281 ldap_pvt_thread_cond_init( &c->c_write_cv );
283 c->c_struct_state = SLAP_C_UNUSED;
286 ldap_pvt_thread_mutex_lock( &c->c_mutex );
288 assert( c->c_struct_state == SLAP_C_UNUSED );
289 assert( c->c_dn == NULL );
290 assert( c->c_cdn == NULL );
291 assert( c->c_client_name == NULL );
292 assert( c->c_client_addr == NULL );
293 assert( c->c_ops == NULL );
294 assert( c->c_pending_ops == NULL );
296 c->c_client_name = ch_strdup( name == NULL ? "" : name );
297 c->c_client_addr = ch_strdup( addr );
299 c->c_n_ops_received = 0;
301 c->c_n_ops_executing = 0;
302 c->c_n_ops_pending = 0;
303 c->c_n_ops_completed = 0;
306 c->c_starttime = slap_get_time();
308 ber_pvt_sb_set_desc( c->c_sb, s );
309 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
311 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
312 Debug( LDAP_DEBUG_ANY,
313 "connection_init(%d, %s, %s): set nonblocking failed\n",
314 s, c->c_client_name, c->c_client_addr);
317 id = c->c_connid = conn_nextid++;
319 c->c_conn_state = SLAP_C_INACTIVE;
320 c->c_struct_state = SLAP_C_USED;
322 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
323 ldap_pvt_thread_mutex_unlock( &connections_mutex );
329 connection_destroy( Connection *c )
331 /* note: connections_mutex should be locked by caller */
333 assert( connections != NULL );
335 assert( c->c_struct_state != SLAP_C_UNUSED );
336 assert( c->c_conn_state != SLAP_C_INVALID );
337 assert( c->c_ops == NULL );
339 c->c_struct_state = SLAP_C_UNUSED;
340 c->c_conn_state = SLAP_C_INVALID;
349 if(c->c_dn != NULL) {
353 if(c->c_cdn != NULL) {
357 if(c->c_client_name != NULL) {
358 free(c->c_client_name);
359 c->c_client_name = NULL;
361 if(c->c_client_addr != NULL) {
362 free(c->c_client_addr);
363 c->c_client_addr = NULL;
366 if ( ber_pvt_sb_in_use(c->c_sb) ) {
367 int sd = ber_pvt_sb_get_desc(c->c_sb);
370 ber_pvt_sb_close( c->c_sb );
372 Statslog( LDAP_DEBUG_STATS,
373 "conn=%d fd=%d closed.\n",
374 c->c_connid, sd, 0, 0, 0 );
377 ber_pvt_sb_destroy( c->c_sb );
380 int connection_state_closing( Connection *c )
382 /* c_mutex must be locked by caller */
386 assert( c->c_struct_state == SLAP_C_USED );
388 state = c->c_conn_state;
390 assert( state != SLAP_C_INVALID );
392 return state == SLAP_C_CLOSING;
395 void connection_closing( Connection *c )
397 assert( connections != NULL );
399 assert( c->c_struct_state == SLAP_C_USED );
400 assert( c->c_conn_state != SLAP_C_INVALID );
402 /* c_mutex must be locked by caller */
404 if( c->c_conn_state != SLAP_C_CLOSING ) {
407 Debug( LDAP_DEBUG_TRACE,
408 "connection_closing: readying conn=%ld sd=%d for close.\n",
409 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
411 /* update state to closing */
412 c->c_conn_state = SLAP_C_CLOSING;
414 /* don't listen on this port anymore */
415 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
417 /* shutdown I/O -- not yet implemented */
419 /* abandon active operations */
420 for( o = c->c_ops; o != NULL; o = o->o_next ) {
421 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
423 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
426 /* remove pending operations */
427 for( o = slap_op_pop( &c->c_pending_ops );
429 o = slap_op_pop( &c->c_pending_ops ) )
434 /* wake write blocked operations */
435 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
436 ldap_pvt_thread_cond_signal( &c->c_write_cv );
440 static void connection_close( Connection *c )
442 assert( connections != NULL );
444 assert( c->c_struct_state == SLAP_C_USED );
445 assert( c->c_conn_state == SLAP_C_CLOSING );
447 /* note: connections_mutex and c_mutex should be locked by caller */
449 if( c->c_ops != NULL ) {
450 Debug( LDAP_DEBUG_TRACE,
451 "connection_close: deferring conn=%ld sd=%d.\n",
452 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
457 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
458 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
460 connection_destroy( c );
463 long connections_nextid(void)
466 assert( connections != NULL );
468 ldap_pvt_thread_mutex_lock( &connections_mutex );
472 ldap_pvt_thread_mutex_unlock( &connections_mutex );
477 Connection* connection_first( int *index )
479 assert( connections != NULL );
480 assert( index != NULL );
482 ldap_pvt_thread_mutex_lock( &connections_mutex );
486 return connection_next(NULL, index);
489 Connection* connection_next( Connection *c, int *index )
491 assert( connections != NULL );
492 assert( index != NULL );
493 assert( *index <= dtblsize );
496 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
501 for(; *index < dtblsize; (*index)++) {
502 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
503 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
511 if( connections[*index].c_struct_state == SLAP_C_USED ) {
512 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
513 c = &connections[(*index)++];
517 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
518 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
522 ldap_pvt_thread_mutex_lock( &c->c_mutex );
528 void connection_done( Connection *c )
530 assert( connections != NULL );
533 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
536 ldap_pvt_thread_mutex_unlock( &connections_mutex );
540 * connection_activity - handle the request operation op on connection
541 * conn. This routine figures out what kind of operation it is and
542 * calls the appropriate stub to handle it.
546 connection_operation( void *arg_v )
548 struct co_arg *arg = arg_v;
549 int tag = arg->co_op->o_tag;
550 Connection *conn = arg->co_conn;
553 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
555 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
560 do_bind( conn, arg->co_op );
564 case LDAP_REQ_UNBIND_30:
566 case LDAP_REQ_UNBIND:
567 do_unbind( conn, arg->co_op );
571 do_add( conn, arg->co_op );
575 case LDAP_REQ_DELETE_30:
577 case LDAP_REQ_DELETE:
578 do_delete( conn, arg->co_op );
581 case LDAP_REQ_MODRDN:
582 do_modrdn( conn, arg->co_op );
585 case LDAP_REQ_MODIFY:
586 do_modify( conn, arg->co_op );
589 case LDAP_REQ_COMPARE:
590 do_compare( conn, arg->co_op );
593 case LDAP_REQ_SEARCH:
594 do_search( conn, arg->co_op );
598 case LDAP_REQ_ABANDON_30:
600 case LDAP_REQ_ABANDON:
601 do_abandon( conn, arg->co_op );
605 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
606 arg->co_op->o_tag, 0, 0 );
611 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
613 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
616 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
619 conn->c_n_ops_completed++;
622 slap_op_remove( &conn->c_ops, arg->co_op );
623 slap_op_free( arg->co_op );
626 free( (char *) arg );
631 case LDAP_REQ_UNBIND_30:
633 case LDAP_REQ_UNBIND:
634 /* c_mutex is locked */
635 connection_closing( conn );
639 if( conn->c_conn_state == SLAP_C_BINDING) {
640 conn->c_conn_state = SLAP_C_ACTIVE;
644 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
646 if( active_threads < 1 ) {
647 ldap_pvt_thread_cond_signal(&active_threads_cond);
649 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
651 connection_resched( conn );
653 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
658 int connection_read(int s)
662 assert( connections != NULL );
664 ldap_pvt_thread_mutex_lock( &connections_mutex );
666 /* get (locked) connection */
667 c = connection_get( s );
670 Debug( LDAP_DEBUG_ANY,
671 "connection_read(%d): no connection!\n",
673 ldap_pvt_thread_mutex_unlock( &connections_mutex );
677 if( c->c_conn_state == SLAP_C_CLOSING ) {
678 Debug( LDAP_DEBUG_TRACE,
679 "connection_read(%d): closing, ignoring input for id=%ld\n",
682 connection_return( c );
683 ldap_pvt_thread_mutex_unlock( &connections_mutex );
687 Debug( LDAP_DEBUG_TRACE,
688 "connection_read(%d): checking for input on id=%ld\n",
691 #define CONNECTION_INPUT_LOOP 1
693 #ifdef DATA_READY_LOOP
694 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
695 #elif CONNECTION_INPUT_LOOP
699 rc = connection_input( c );
703 Debug( LDAP_DEBUG_TRACE,
704 "connection_read(%d): input error=%d id=%ld, closing.\n",
705 s, rc, c->c_connid );
707 /* connections_mutex and c_mutex are locked */
708 connection_closing( c );
709 connection_close( c );
712 connection_return( c );
713 ldap_pvt_thread_mutex_unlock( &connections_mutex );
723 unsigned long tag, len;
727 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
729 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
734 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
735 != LDAP_TAG_MESSAGE )
739 Debug( LDAP_DEBUG_TRACE,
740 "ber_get_next on fd %d failed errno %d (%s)\n",
741 ber_pvt_sb_get_desc( conn->c_sb ), err,
742 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
743 Debug( LDAP_DEBUG_TRACE,
744 "\t*** got %ld of %lu so far\n",
745 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
746 conn->c_currentber->ber_len, 0 );
748 if ( err != EWOULDBLOCK && err != EAGAIN ) {
749 /* log, close and send error */
750 ber_free( conn->c_currentber, 1 );
751 conn->c_currentber = NULL;
758 ber = conn->c_currentber;
759 conn->c_currentber = NULL;
761 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
762 /* log, close and send error */
763 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
769 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
770 /* log, close and send error */
771 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
779 if ( conn->c_version == 30 ) {
780 (void) ber_skip_tag( ber, &len );
784 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
786 if ( conn->c_conn_state == SLAP_C_BINDING
787 || conn->c_conn_state == SLAP_C_CLOSING )
789 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
790 slap_op_add( &conn->c_pending_ops, op );
793 connection_op_activate( conn, op );
797 if ( conn->c_struct_state != SLAP_C_USED ) {
798 /* connection must have got closed underneath us */
802 assert( conn->c_struct_state == SLAP_C_USED );
808 connection_resched( Connection *conn )
812 if( conn->c_conn_state == SLAP_C_CLOSING ) {
813 Debug( LDAP_DEBUG_TRACE,
814 "connection_resched: attempting closing conn=%ld sd=%d.\n",
815 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
817 connection_close( conn );
821 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
822 /* other states need different handling */
826 for( op = slap_op_pop( &conn->c_pending_ops );
828 op = slap_op_pop( &conn->c_pending_ops ) )
830 /* pending operations should not be marked for abandonment */
831 assert(!op->o_abandon);
833 connection_op_activate( conn, op );
835 if ( conn->c_conn_state == SLAP_C_BINDING ) {
842 static int connection_op_activate( Connection *conn, Operation *op )
847 unsigned long tag = op->o_tag;
849 if ( conn->c_dn != NULL ) {
850 tmpdn = ch_strdup( conn->c_dn );
855 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
859 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
860 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
862 slap_op_add( &conn->c_ops, arg->co_op );
864 if(tag == LDAP_REQ_BIND) {
865 conn->c_conn_state = SLAP_C_BINDING;
868 if ( tmpdn != NULL ) {
872 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
874 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
876 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
877 connection_operation, (void *) arg );
880 Debug( LDAP_DEBUG_ANY,
881 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
883 /* should move op to pending list */
889 int connection_write(int s)
892 assert( connections != NULL );
894 ldap_pvt_thread_mutex_lock( &connections_mutex );
896 c = connection_get( s );
898 Debug( LDAP_DEBUG_ANY,
899 "connection_write(%d): no connection!\n",
901 ldap_pvt_thread_mutex_unlock( &connections_mutex );
905 Debug( LDAP_DEBUG_TRACE,
906 "connection_write(%d): waking output for id=%ld\n",
909 ldap_pvt_thread_cond_signal( &c->c_write_cv );
911 connection_return( c );
912 ldap_pvt_thread_mutex_unlock( &connections_mutex );