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_conn_state != SLAP_C_INVALID );
164 assert( ber_pvt_sb_in_use( c->c_sb ) );
171 for(i=0; i<dtblsize; i++) {
172 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
173 assert( connections[i].c_conn_state == SLAP_C_INVALID );
174 assert( connections[i].c_sb == 0 );
178 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
179 assert( connections[i].c_conn_state == SLAP_C_INVALID );
180 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
184 assert( connections[i].c_struct_state == SLAP_C_USED );
185 assert( connections[i].c_conn_state != SLAP_C_INVALID );
186 assert( ber_pvt_sb_in_use( connections[i].c_sb ) );
188 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
190 ldap_pvt_thread_mutex_lock( &c->c_mutex );
198 /* we do this AFTER locking to aid in debugging */
199 Debug( LDAP_DEBUG_TRACE,
200 "connection_get(%d): got connid=%ld\n",
206 static void connection_return( Connection *c )
208 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
211 long connection_init(
218 assert( connections != NULL );
221 Debug( LDAP_DEBUG_ANY,
222 "connection_init(%d): invalid.\n",
229 assert( s < dtblsize );
232 ldap_pvt_thread_mutex_lock( &connections_mutex );
243 for( i=0; i < dtblsize; i++) {
244 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
245 assert( connections[i].c_sb == 0 );
250 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
251 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
256 assert( connections[i].c_struct_state == SLAP_C_USED );
257 assert( connections[i].c_conn_state != SLAP_C_INVALID );
258 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
262 Debug( LDAP_DEBUG_ANY,
263 "connection_init(%d): connection table full (%d/%d).\n",
265 ldap_pvt_thread_mutex_unlock( &connections_mutex );
272 assert( c->c_struct_state != SLAP_C_USED );
273 assert( c->c_conn_state == SLAP_C_INVALID );
275 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
278 c->c_client_name = NULL;
279 c->c_client_addr = NULL;
281 c->c_pending_ops = NULL;
283 c->c_sb = ber_sockbuf_alloc( );
285 /* should check status of thread calls */
286 ldap_pvt_thread_mutex_init( &c->c_mutex );
287 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
288 ldap_pvt_thread_cond_init( &c->c_write_cv );
290 c->c_struct_state = SLAP_C_UNUSED;
293 ldap_pvt_thread_mutex_lock( &c->c_mutex );
295 assert( c->c_struct_state == SLAP_C_UNUSED );
296 assert( c->c_dn == NULL );
297 assert( c->c_cdn == NULL );
298 assert( c->c_client_name == NULL );
299 assert( c->c_client_addr == NULL );
300 assert( c->c_ops == NULL );
301 assert( c->c_pending_ops == NULL );
303 c->c_client_name = ch_strdup( name == NULL ? "" : name );
304 c->c_client_addr = ch_strdup( addr );
306 c->c_n_ops_received = 0;
308 c->c_n_ops_executing = 0;
309 c->c_n_ops_pending = 0;
310 c->c_n_ops_completed = 0;
313 c->c_starttime = slap_get_time();
315 ber_pvt_sb_set_desc( c->c_sb, s );
316 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
318 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
319 Debug( LDAP_DEBUG_ANY,
320 "connection_init(%d, %s, %s): set nonblocking failed\n",
321 s, c->c_client_name, c->c_client_addr);
324 id = c->c_connid = conn_nextid++;
326 c->c_conn_state = SLAP_C_INACTIVE;
327 c->c_struct_state = SLAP_C_USED;
329 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
330 ldap_pvt_thread_mutex_unlock( &connections_mutex );
336 connection_destroy( Connection *c )
338 /* note: connections_mutex should be locked by caller */
340 assert( connections != NULL );
342 assert( c->c_struct_state != SLAP_C_UNUSED );
343 assert( c->c_conn_state != SLAP_C_INVALID );
344 assert( c->c_ops == NULL );
346 c->c_struct_state = SLAP_C_UNUSED;
347 c->c_conn_state = SLAP_C_INVALID;
356 if(c->c_dn != NULL) {
360 if(c->c_cdn != NULL) {
364 if(c->c_client_name != NULL) {
365 free(c->c_client_name);
366 c->c_client_name = NULL;
368 if(c->c_client_addr != NULL) {
369 free(c->c_client_addr);
370 c->c_client_addr = NULL;
373 if ( ber_pvt_sb_in_use(c->c_sb) ) {
374 int sd = ber_pvt_sb_get_desc(c->c_sb);
376 slapd_remove( sd, 0 );
377 ber_pvt_sb_close( c->c_sb );
379 Statslog( LDAP_DEBUG_STATS,
380 "conn=%d fd=%d closed.\n",
381 c->c_connid, sd, 0, 0, 0 );
384 ber_pvt_sb_destroy( c->c_sb );
387 int connection_state_closing( Connection *c )
389 /* c_mutex must be locked by caller */
393 assert( c->c_struct_state == SLAP_C_USED );
395 state = c->c_conn_state;
397 assert( state != SLAP_C_INVALID );
399 return state == SLAP_C_CLOSING;
402 void connection_closing( Connection *c )
404 assert( connections != NULL );
406 assert( c->c_struct_state == SLAP_C_USED );
407 assert( c->c_conn_state != SLAP_C_INVALID );
409 /* c_mutex must be locked by caller */
411 if( c->c_conn_state != SLAP_C_CLOSING ) {
414 Debug( LDAP_DEBUG_TRACE,
415 "connection_closing: readying conn=%ld sd=%d for close.\n",
416 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
418 /* update state to closing */
419 c->c_conn_state = SLAP_C_CLOSING;
421 /* don't listen on this port anymore */
422 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
424 /* shutdown I/O -- not yet implemented */
426 /* abandon active operations */
427 for( o = c->c_ops; o != NULL; o = o->o_next ) {
428 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
430 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
433 /* remove pending operations */
434 for( o = slap_op_pop( &c->c_pending_ops );
436 o = slap_op_pop( &c->c_pending_ops ) )
441 /* wake write blocked operations */
442 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
443 ldap_pvt_thread_cond_signal( &c->c_write_cv );
447 static void connection_close( Connection *c )
449 assert( connections != NULL );
451 assert( c->c_struct_state == SLAP_C_USED );
452 assert( c->c_conn_state == SLAP_C_CLOSING );
454 /* note: connections_mutex and c_mutex should be locked by caller */
456 if( c->c_ops != NULL ) {
457 Debug( LDAP_DEBUG_TRACE,
458 "connection_close: deferring conn=%ld sd=%d.\n",
459 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
464 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
465 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
467 connection_destroy( c );
470 long connections_nextid(void)
473 assert( connections != NULL );
475 ldap_pvt_thread_mutex_lock( &connections_mutex );
479 ldap_pvt_thread_mutex_unlock( &connections_mutex );
484 Connection* connection_first( int *index )
486 assert( connections != NULL );
487 assert( index != NULL );
489 ldap_pvt_thread_mutex_lock( &connections_mutex );
493 return connection_next(NULL, index);
496 Connection* connection_next( Connection *c, int *index )
498 assert( connections != NULL );
499 assert( index != NULL );
500 assert( *index <= dtblsize );
503 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
508 for(; *index < dtblsize; (*index)++) {
509 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
510 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
518 if( connections[*index].c_struct_state == SLAP_C_USED ) {
519 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
520 c = &connections[(*index)++];
524 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
525 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
529 ldap_pvt_thread_mutex_lock( &c->c_mutex );
535 void connection_done( Connection *c )
537 assert( connections != NULL );
540 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
543 ldap_pvt_thread_mutex_unlock( &connections_mutex );
547 * connection_activity - handle the request operation op on connection
548 * conn. This routine figures out what kind of operation it is and
549 * calls the appropriate stub to handle it.
553 connection_operation( void *arg_v )
555 struct co_arg *arg = arg_v;
556 int tag = arg->co_op->o_tag;
557 Connection *conn = arg->co_conn;
560 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
562 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
567 do_bind( conn, arg->co_op );
571 case LDAP_REQ_UNBIND_30:
573 case LDAP_REQ_UNBIND:
574 do_unbind( conn, arg->co_op );
578 do_add( conn, arg->co_op );
582 case LDAP_REQ_DELETE_30:
584 case LDAP_REQ_DELETE:
585 do_delete( conn, arg->co_op );
588 case LDAP_REQ_MODRDN:
589 do_modrdn( conn, arg->co_op );
592 case LDAP_REQ_MODIFY:
593 do_modify( conn, arg->co_op );
596 case LDAP_REQ_COMPARE:
597 do_compare( conn, arg->co_op );
600 case LDAP_REQ_SEARCH:
601 do_search( conn, arg->co_op );
605 case LDAP_REQ_ABANDON_30:
607 case LDAP_REQ_ABANDON:
608 do_abandon( conn, arg->co_op );
612 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
613 arg->co_op->o_tag, 0, 0 );
618 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
620 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
623 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
626 conn->c_n_ops_completed++;
629 slap_op_remove( &conn->c_ops, arg->co_op );
630 slap_op_free( arg->co_op );
633 free( (char *) arg );
638 case LDAP_REQ_UNBIND_30:
640 case LDAP_REQ_UNBIND:
641 /* c_mutex is locked */
642 connection_closing( conn );
646 if( conn->c_conn_state == SLAP_C_BINDING) {
647 conn->c_conn_state = SLAP_C_ACTIVE;
651 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
653 if( active_threads < 1 ) {
654 ldap_pvt_thread_cond_signal(&active_threads_cond);
656 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
658 connection_resched( conn );
660 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
665 int connection_read(int s)
669 assert( connections != NULL );
671 ldap_pvt_thread_mutex_lock( &connections_mutex );
673 /* get (locked) connection */
674 c = connection_get( s );
677 Debug( LDAP_DEBUG_ANY,
678 "connection_read(%d): no connection!\n",
683 ldap_pvt_thread_mutex_unlock( &connections_mutex );
687 if( c->c_conn_state == SLAP_C_CLOSING ) {
688 Debug( LDAP_DEBUG_TRACE,
689 "connection_read(%d): closing, ignoring input for id=%ld\n",
692 connection_return( c );
693 ldap_pvt_thread_mutex_unlock( &connections_mutex );
697 Debug( LDAP_DEBUG_TRACE,
698 "connection_read(%d): checking for input on id=%ld\n",
701 #define CONNECTION_INPUT_LOOP 1
703 #ifdef DATA_READY_LOOP
704 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
705 #elif CONNECTION_INPUT_LOOP
709 rc = connection_input( c );
713 Debug( LDAP_DEBUG_TRACE,
714 "connection_read(%d): input error=%d id=%ld, closing.\n",
715 s, rc, c->c_connid );
717 /* connections_mutex and c_mutex are locked */
718 connection_closing( c );
719 connection_close( c );
722 connection_return( c );
723 ldap_pvt_thread_mutex_unlock( &connections_mutex );
733 unsigned long tag, len;
737 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
739 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
744 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
745 != LDAP_TAG_MESSAGE )
749 Debug( LDAP_DEBUG_TRACE,
750 "ber_get_next on fd %d failed errno %d (%s)\n",
751 ber_pvt_sb_get_desc( conn->c_sb ), err,
752 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
753 Debug( LDAP_DEBUG_TRACE,
754 "\t*** got %ld of %lu so far\n",
755 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
756 conn->c_currentber->ber_len, 0 );
758 if ( err != EWOULDBLOCK && err != EAGAIN ) {
759 /* log, close and send error */
760 ber_free( conn->c_currentber, 1 );
761 conn->c_currentber = NULL;
768 ber = conn->c_currentber;
769 conn->c_currentber = NULL;
771 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
772 /* log, close and send error */
773 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
779 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
780 /* log, close and send error */
781 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
789 if ( conn->c_version == 30 ) {
790 (void) ber_skip_tag( ber, &len );
794 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
796 if ( conn->c_conn_state == SLAP_C_BINDING
797 || conn->c_conn_state == SLAP_C_CLOSING )
799 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
800 slap_op_add( &conn->c_pending_ops, op );
803 connection_op_activate( conn, op );
807 if ( conn->c_struct_state != SLAP_C_USED ) {
808 /* connection must have got closed underneath us */
812 assert( conn->c_struct_state == SLAP_C_USED );
818 connection_resched( Connection *conn )
822 if( conn->c_conn_state == SLAP_C_CLOSING ) {
823 Debug( LDAP_DEBUG_TRACE,
824 "connection_resched: attempting closing conn=%ld sd=%d.\n",
825 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
827 connection_close( conn );
831 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
832 /* other states need different handling */
836 for( op = slap_op_pop( &conn->c_pending_ops );
838 op = slap_op_pop( &conn->c_pending_ops ) )
840 /* pending operations should not be marked for abandonment */
841 assert(!op->o_abandon);
843 connection_op_activate( conn, op );
845 if ( conn->c_conn_state == SLAP_C_BINDING ) {
852 static int connection_op_activate( Connection *conn, Operation *op )
857 unsigned long tag = op->o_tag;
859 if ( conn->c_dn != NULL ) {
860 tmpdn = ch_strdup( conn->c_dn );
865 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
869 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
870 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
872 slap_op_add( &conn->c_ops, arg->co_op );
874 if(tag == LDAP_REQ_BIND) {
875 conn->c_conn_state = SLAP_C_BINDING;
878 if ( tmpdn != NULL ) {
882 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
884 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
886 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
887 connection_operation, (void *) arg );
890 Debug( LDAP_DEBUG_ANY,
891 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
893 /* should move op to pending list */
899 int connection_write(int s)
902 assert( connections != NULL );
904 ldap_pvt_thread_mutex_lock( &connections_mutex );
906 c = connection_get( s );
908 slapd_clr_write( s, 0);
911 Debug( LDAP_DEBUG_ANY,
912 "connection_write(%d): no connection!\n",
915 ldap_pvt_thread_mutex_unlock( &connections_mutex );
919 Debug( LDAP_DEBUG_TRACE,
920 "connection_write(%d): waking output for id=%ld\n",
923 ldap_pvt_thread_cond_signal( &c->c_write_cv );
925 connection_return( c );
926 ldap_pvt_thread_mutex_unlock( &connections_mutex );