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 );
234 for( i=0; i < dtblsize; i++) {
235 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
236 assert( connections[i].c_sb == 0 );
241 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
242 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
247 assert( connections[i].c_struct_state == SLAP_C_USED );
248 assert( connections[i].c_conn_state != SLAP_C_INVALID );
249 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
253 Debug( LDAP_DEBUG_ANY,
254 "connection_init(%d): connection table full (%d/%d).\n",
256 ldap_pvt_thread_mutex_unlock( &connections_mutex );
263 assert( c->c_struct_state != SLAP_C_USED );
264 assert( c->c_conn_state == SLAP_C_INVALID );
266 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
269 c->c_client_name = NULL;
270 c->c_client_addr = NULL;
272 c->c_pending_ops = NULL;
274 c->c_sb = ber_sockbuf_alloc( );
276 /* should check status of thread calls */
277 ldap_pvt_thread_mutex_init( &c->c_mutex );
278 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
279 ldap_pvt_thread_cond_init( &c->c_write_cv );
281 c->c_struct_state = SLAP_C_UNUSED;
284 ldap_pvt_thread_mutex_lock( &c->c_mutex );
286 assert( c->c_struct_state == SLAP_C_UNUSED );
287 assert( c->c_dn == NULL );
288 assert( c->c_cdn == NULL );
289 assert( c->c_client_name == NULL );
290 assert( c->c_client_addr == NULL );
291 assert( c->c_ops == NULL );
292 assert( c->c_pending_ops == NULL );
294 c->c_client_name = ch_strdup( name == NULL ? "" : name );
295 c->c_client_addr = ch_strdup( addr );
297 c->c_n_ops_received = 0;
299 c->c_n_ops_executing = 0;
300 c->c_n_ops_pending = 0;
301 c->c_n_ops_completed = 0;
304 c->c_starttime = slap_get_time();
306 ber_pvt_sb_set_desc( c->c_sb, s );
307 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
309 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
310 Debug( LDAP_DEBUG_ANY,
311 "connection_init(%d, %s, %s): set nonblocking failed\n",
312 s, c->c_client_name, c->c_client_addr);
315 id = c->c_connid = conn_nextid++;
317 c->c_conn_state = SLAP_C_INACTIVE;
318 c->c_struct_state = SLAP_C_USED;
320 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
321 ldap_pvt_thread_mutex_unlock( &connections_mutex );
327 connection_destroy( Connection *c )
329 /* note: connections_mutex should be locked by caller */
331 assert( connections != NULL );
333 assert( c->c_struct_state != SLAP_C_UNUSED );
334 assert( c->c_conn_state != SLAP_C_INVALID );
335 assert( c->c_ops == NULL );
337 c->c_struct_state = SLAP_C_UNUSED;
338 c->c_conn_state = SLAP_C_INVALID;
347 if(c->c_dn != NULL) {
351 if(c->c_cdn != NULL) {
355 if(c->c_client_name != NULL) {
356 free(c->c_client_name);
357 c->c_client_name = NULL;
359 if(c->c_client_addr != NULL) {
360 free(c->c_client_addr);
361 c->c_client_addr = NULL;
364 if ( ber_pvt_sb_in_use(c->c_sb) ) {
365 int sd = ber_pvt_sb_get_desc(c->c_sb);
368 ber_pvt_sb_close( c->c_sb );
370 Statslog( LDAP_DEBUG_STATS,
371 "conn=%d fd=%d closed.\n",
372 c->c_connid, sd, 0, 0, 0 );
375 ber_pvt_sb_destroy( c->c_sb );
378 int connection_state_closing( Connection *c )
380 /* c_mutex must be locked by caller */
384 assert( c->c_struct_state == SLAP_C_USED );
386 state = c->c_conn_state;
388 assert( state != SLAP_C_INVALID );
390 return state == SLAP_C_CLOSING;
393 void connection_closing( Connection *c )
395 assert( connections != NULL );
397 assert( c->c_struct_state == SLAP_C_USED );
398 assert( c->c_conn_state != SLAP_C_INVALID );
400 /* c_mutex must be locked by caller */
402 if( c->c_conn_state != SLAP_C_CLOSING ) {
405 Debug( LDAP_DEBUG_TRACE,
406 "connection_closing: readying conn=%ld sd=%d for close.\n",
407 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
409 /* update state to closing */
410 c->c_conn_state = SLAP_C_CLOSING;
412 /* don't listen on this port anymore */
413 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
415 /* shutdown I/O -- not yet implemented */
417 /* abandon active operations */
418 for( o = c->c_ops; o != NULL; o = o->o_next ) {
419 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
421 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
424 /* remove pending operations */
425 for( o = slap_op_pop( &c->c_pending_ops );
427 o = slap_op_pop( &c->c_pending_ops ) )
432 /* wake write blocked operations */
433 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
434 ldap_pvt_thread_cond_signal( &c->c_write_cv );
438 static void connection_close( Connection *c )
440 assert( connections != NULL );
442 assert( c->c_struct_state == SLAP_C_USED );
443 assert( c->c_conn_state == SLAP_C_CLOSING );
445 /* note: connections_mutex and c_mutex should be locked by caller */
447 if( c->c_ops != NULL ) {
448 Debug( LDAP_DEBUG_TRACE,
449 "connection_close: deferring conn=%ld sd=%d.\n",
450 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
455 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
456 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
458 connection_destroy( c );
461 long connections_nextid(void)
464 assert( connections != NULL );
466 ldap_pvt_thread_mutex_lock( &connections_mutex );
470 ldap_pvt_thread_mutex_unlock( &connections_mutex );
475 Connection* connection_first( int *index )
477 assert( connections != NULL );
478 assert( index != NULL );
480 ldap_pvt_thread_mutex_lock( &connections_mutex );
484 return connection_next(NULL, index);
487 Connection* connection_next( Connection *c, int *index )
489 assert( connections != NULL );
490 assert( index != NULL );
491 assert( *index <= dtblsize );
494 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
499 for(; *index < dtblsize; (*index)++) {
500 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
501 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
509 if( connections[*index].c_struct_state == SLAP_C_USED ) {
510 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
511 c = &connections[(*index)++];
515 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
516 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
520 ldap_pvt_thread_mutex_lock( &c->c_mutex );
526 void connection_done( Connection *c )
528 assert( connections != NULL );
531 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
534 ldap_pvt_thread_mutex_unlock( &connections_mutex );
538 * connection_activity - handle the request operation op on connection
539 * conn. This routine figures out what kind of operation it is and
540 * calls the appropriate stub to handle it.
544 connection_operation( void *arg_v )
546 struct co_arg *arg = arg_v;
547 int tag = arg->co_op->o_tag;
548 Connection *conn = arg->co_conn;
551 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
553 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
558 do_bind( conn, arg->co_op );
562 case LDAP_REQ_UNBIND_30:
564 case LDAP_REQ_UNBIND:
565 do_unbind( conn, arg->co_op );
569 do_add( conn, arg->co_op );
573 case LDAP_REQ_DELETE_30:
575 case LDAP_REQ_DELETE:
576 do_delete( conn, arg->co_op );
579 case LDAP_REQ_MODRDN:
580 do_modrdn( conn, arg->co_op );
583 case LDAP_REQ_MODIFY:
584 do_modify( conn, arg->co_op );
587 case LDAP_REQ_COMPARE:
588 do_compare( conn, arg->co_op );
591 case LDAP_REQ_SEARCH:
592 do_search( conn, arg->co_op );
596 case LDAP_REQ_ABANDON_30:
598 case LDAP_REQ_ABANDON:
599 do_abandon( conn, arg->co_op );
603 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
604 arg->co_op->o_tag, 0, 0 );
609 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
611 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
614 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
617 conn->c_n_ops_completed++;
620 slap_op_remove( &conn->c_ops, arg->co_op );
621 slap_op_free( arg->co_op );
624 free( (char *) arg );
629 case LDAP_REQ_UNBIND_30:
631 case LDAP_REQ_UNBIND:
632 /* c_mutex is locked */
633 connection_closing( conn );
637 if( conn->c_conn_state == SLAP_C_BINDING) {
638 conn->c_conn_state = SLAP_C_ACTIVE;
642 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
644 if( active_threads < 1 ) {
645 ldap_pvt_thread_cond_signal(&active_threads_cond);
647 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
649 connection_resched( conn );
651 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
656 int connection_read(int s)
660 assert( connections != NULL );
662 ldap_pvt_thread_mutex_lock( &connections_mutex );
664 /* get (locked) connection */
665 c = connection_get( s );
668 Debug( LDAP_DEBUG_ANY,
669 "connection_read(%d): no connection!\n",
671 ldap_pvt_thread_mutex_unlock( &connections_mutex );
675 if( c->c_conn_state == SLAP_C_CLOSING ) {
676 Debug( LDAP_DEBUG_TRACE,
677 "connection_read(%d): closing, ignoring input for id=%ld\n",
680 connection_return( c );
681 ldap_pvt_thread_mutex_unlock( &connections_mutex );
685 Debug( LDAP_DEBUG_TRACE,
686 "connection_read(%d): checking for input on id=%ld\n",
689 #define CONNECTION_INPUT_LOOP 1
691 #ifdef DATA_READY_LOOP
692 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
693 #elif CONNECTION_INPUT_LOOP
697 rc = connection_input( c );
701 Debug( LDAP_DEBUG_TRACE,
702 "connection_read(%d): input error=%d id=%ld, closing.\n",
703 s, rc, c->c_connid );
705 /* connections_mutex and c_mutex are locked */
706 connection_closing( c );
707 connection_close( c );
710 connection_return( c );
711 ldap_pvt_thread_mutex_unlock( &connections_mutex );
721 unsigned long tag, len;
725 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
727 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
732 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
733 != LDAP_TAG_MESSAGE )
737 Debug( LDAP_DEBUG_TRACE,
738 "ber_get_next on fd %d failed errno %d (%s)\n",
739 ber_pvt_sb_get_desc( conn->c_sb ), err,
740 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
741 Debug( LDAP_DEBUG_TRACE,
742 "\t*** got %ld of %lu so far\n",
743 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
744 conn->c_currentber->ber_len, 0 );
746 if ( err != EWOULDBLOCK && err != EAGAIN ) {
747 /* log, close and send error */
748 ber_free( conn->c_currentber, 1 );
749 conn->c_currentber = NULL;
756 ber = conn->c_currentber;
757 conn->c_currentber = NULL;
759 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
760 /* log, close and send error */
761 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
767 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
768 /* log, close and send error */
769 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
777 if ( conn->c_version == 30 ) {
778 (void) ber_skip_tag( ber, &len );
782 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
784 if ( conn->c_conn_state == SLAP_C_BINDING
785 || conn->c_conn_state == SLAP_C_CLOSING )
787 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
788 slap_op_add( &conn->c_pending_ops, op );
791 connection_op_activate( conn, op );
795 if ( conn->c_struct_state != SLAP_C_USED ) {
796 /* connection must have got closed underneath us */
800 assert( conn->c_struct_state == SLAP_C_USED );
806 connection_resched( Connection *conn )
810 if( conn->c_conn_state == SLAP_C_CLOSING ) {
811 Debug( LDAP_DEBUG_TRACE,
812 "connection_resched: attempting closing conn=%ld sd=%d.\n",
813 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
815 connection_close( conn );
819 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
820 /* other states need different handling */
824 for( op = slap_op_pop( &conn->c_pending_ops );
826 op = slap_op_pop( &conn->c_pending_ops ) )
828 /* pending operations should not be marked for abandonment */
829 assert(!op->o_abandon);
831 connection_op_activate( conn, op );
833 if ( conn->c_conn_state == SLAP_C_BINDING ) {
840 static int connection_op_activate( Connection *conn, Operation *op )
845 unsigned long tag = op->o_tag;
847 if ( conn->c_dn != NULL ) {
848 tmpdn = ch_strdup( conn->c_dn );
853 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
857 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
858 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
860 slap_op_add( &conn->c_ops, arg->co_op );
862 if(tag == LDAP_REQ_BIND) {
863 conn->c_conn_state = SLAP_C_BINDING;
866 if ( tmpdn != NULL ) {
870 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
872 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
874 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
875 connection_operation, (void *) arg );
878 Debug( LDAP_DEBUG_ANY,
879 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
881 /* should move op to pending list */
887 int connection_write(int s)
890 assert( connections != NULL );
892 ldap_pvt_thread_mutex_lock( &connections_mutex );
894 c = connection_get( s );
896 Debug( LDAP_DEBUG_ANY,
897 "connection_write(%d): no connection!\n",
899 ldap_pvt_thread_mutex_unlock( &connections_mutex );
903 Debug( LDAP_DEBUG_TRACE,
904 "connection_write(%d): waking output for id=%ld\n",
907 ldap_pvt_thread_cond_signal( &c->c_write_cv );
909 connection_return( c );
910 ldap_pvt_thread_mutex_unlock( &connections_mutex );