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 int conn_index = -1;
20 static long conn_nextid = 0;
22 /* structure state (protected by connections_mutex) */
23 #define SLAP_C_UNINITIALIZED 0x0 /* MUST BE ZERO (0) */
24 #define SLAP_C_UNUSED 0x1
25 #define SLAP_C_USED 0x2
27 /* connection state (protected by c_mutex ) */
28 #define SLAP_C_INVALID 0x0 /* MUST BE ZERO (0) */
29 #define SLAP_C_INACTIVE 0x1 /* zero threads */
30 #define SLAP_C_ACTIVE 0x2 /* one or more threads */
31 #define SLAP_C_BINDING 0x3 /* binding */
32 #define SLAP_C_CLOSING 0x4 /* closing */
34 void slapd_remove(int s);
35 static Connection* connection_get( int s );
37 static int connection_input( Connection *c );
38 static void connection_close( Connection *c );
40 static int connection_op_activate( Connection *conn, Operation *op );
41 static int connection_resched( Connection *conn );
49 * Initialize connection management infrastructure.
51 int connections_init(void)
55 assert( connections == NULL );
57 if( connections != NULL) {
58 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
63 /* should check return of every call */
64 ldap_pvt_thread_mutex_init( &connections_mutex );
66 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
68 if( connections == NULL ) {
69 Debug( LDAP_DEBUG_ANY,
70 "connections_init: allocation (%d*%ld) of connection array failed.\n",
71 dtblsize, (long) sizeof(Connection), 0 );
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 );
128 connection_closing( &connections[i] );
129 connection_close( &connections[i] );
130 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
133 ldap_pvt_thread_mutex_unlock( &connections_mutex );
138 static Connection* connection_get( int s )
140 Connection *c = NULL;
142 assert( connections != NULL );
149 assert( connections[s].c_struct_state == SLAP_C_USED );
150 assert( connections[s].c_conn_state != SLAP_C_INVALID );
151 assert( ber_pvt_sb_in_use( connections[s].c_sb ) );
158 for(i=0; i<dtblsize; i++) {
159 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
160 assert( connections[i].c_conn_state == SLAP_C_INVALID );
161 assert( connections[i].c_sb == 0 );
165 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
166 assert( connections[i].c_conn_state == SLAP_C_INVALID );
167 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
171 assert( connections[i].c_struct_state == SLAP_C_USED );
172 assert( connections[i].c_conn_state != SLAP_C_INVALID );
173 assert( ber_pvt_sb_in_use( connections[i].c_sb ) );
175 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
184 /* we do this BEFORE locking to aid in debugging */
185 Debug( LDAP_DEBUG_TRACE,
186 "connection_get(%d): got connid=%ld\n",
189 ldap_pvt_thread_mutex_lock( &c->c_mutex );
194 static void connection_return( Connection *c )
196 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
199 long connection_init(
206 assert( connections != NULL );
214 assert( s < dtblsize );
217 ldap_pvt_thread_mutex_lock( &connections_mutex );
226 for( i=0; i < dtblsize; i++) {
227 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
228 assert( connections[i].c_sb == 0 );
233 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
234 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
239 assert( connections[i].c_struct_state == SLAP_C_USED );
240 assert( connections[i].c_conn_state != SLAP_C_INVALID );
241 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
245 ldap_pvt_thread_mutex_unlock( &connections_mutex );
252 assert( c->c_struct_state != SLAP_C_USED );
253 assert( c->c_conn_state == SLAP_C_INVALID );
255 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
258 c->c_client_name = NULL;
259 c->c_client_addr = NULL;
261 c->c_pending_ops = NULL;
263 c->c_sb = ber_sockbuf_alloc( );
265 /* should check status of thread calls */
266 ldap_pvt_thread_mutex_init( &c->c_mutex );
267 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
268 ldap_pvt_thread_cond_init( &c->c_write_cv );
270 c->c_struct_state = SLAP_C_UNUSED;
273 ldap_pvt_thread_mutex_lock( &c->c_mutex );
275 assert( c->c_struct_state == SLAP_C_UNUSED );
276 assert( c->c_dn == NULL );
277 assert( c->c_cdn == NULL );
278 assert( c->c_client_name == NULL );
279 assert( c->c_client_addr == NULL );
280 assert( c->c_ops == NULL );
281 assert( c->c_pending_ops == NULL );
283 c->c_client_name = ch_strdup( name == NULL ? "" : name );
284 c->c_client_addr = ch_strdup( addr );
286 c->c_n_ops_received = 0;
288 c->c_n_ops_executing = 0;
289 c->c_n_ops_pending = 0;
290 c->c_n_ops_completed = 0;
293 c->c_starttime = slap_get_time();
295 ber_pvt_sb_set_desc( c->c_sb, s );
296 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
298 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
299 Debug( LDAP_DEBUG_ANY,
300 "connection_init(%d, %s, %s): set nonblocking failed\n",
301 s, c->c_client_name, c->c_client_addr);
304 id = c->c_connid = conn_nextid++;
306 c->c_conn_state = SLAP_C_INACTIVE;
307 c->c_struct_state = SLAP_C_USED;
309 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
310 ldap_pvt_thread_mutex_unlock( &connections_mutex );
316 connection_destroy( Connection *c )
318 /* note: connections_mutex should be locked by caller */
320 assert( connections != NULL );
322 assert( c->c_struct_state != SLAP_C_UNUSED );
323 assert( c->c_conn_state != SLAP_C_INVALID );
324 assert( c->c_ops == NULL );
326 c->c_struct_state = SLAP_C_UNUSED;
327 c->c_conn_state = SLAP_C_INVALID;
336 if(c->c_dn != NULL) {
340 if(c->c_cdn != NULL) {
344 if(c->c_client_name != NULL) {
345 free(c->c_client_name);
346 c->c_client_name = NULL;
348 if(c->c_client_addr != NULL) {
349 free(c->c_client_addr);
350 c->c_client_addr = NULL;
353 if ( ber_pvt_sb_in_use(c->c_sb) ) {
354 int sd = ber_pvt_sb_get_desc(c->c_sb);
357 ber_pvt_sb_close( c->c_sb );
359 Statslog( LDAP_DEBUG_STATS,
360 "conn=%d fd=%d closed.\n",
361 c->c_connid, sd, 0, 0, 0 );
364 ber_pvt_sb_destroy( c->c_sb );
367 int connection_state_closing( Connection *c )
369 /* connection must be locked by caller */
372 assert( c->c_struct_state == SLAP_C_USED );
374 state = c->c_conn_state;
376 assert( state != SLAP_C_INVALID );
378 return state == SLAP_C_CLOSING;
381 void connection_closing( Connection *c )
383 assert( connections != NULL );
385 assert( c->c_struct_state == SLAP_C_USED );
386 assert( c->c_conn_state != SLAP_C_INVALID );
388 if( c->c_conn_state != SLAP_C_CLOSING ) {
391 Debug( LDAP_DEBUG_TRACE,
392 "connection_closing: readying conn=%ld sd=%d for close.\n",
393 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
395 /* update state to closing */
396 c->c_conn_state = SLAP_C_CLOSING;
398 /* don't listen on this port anymore */
399 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
401 /* shutdown I/O -- not yet implemented */
403 /* abandon active operations */
404 for( o = c->c_ops; o != NULL; o = o->o_next ) {
405 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
407 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
410 /* remove pending operations */
411 for( o = slap_op_pop( &c->c_pending_ops );
413 o = slap_op_pop( &c->c_pending_ops ) )
418 /* wake write blocked operations */
419 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
420 ldap_pvt_thread_cond_signal( &c->c_write_cv );
424 static void connection_close( Connection *c )
426 assert( connections != NULL );
428 assert( c->c_struct_state == SLAP_C_USED );
429 assert( c->c_conn_state == SLAP_C_CLOSING );
431 /* note: connections_mutex should be locked by caller */
433 if( c->c_ops != NULL ) {
434 Debug( LDAP_DEBUG_TRACE,
435 "connection_close: deferring conn=%ld sd=%d.\n",
436 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
441 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
442 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
444 connection_destroy( c );
447 long connections_nextid(void)
450 assert( connections != NULL );
452 ldap_pvt_thread_mutex_lock( &connections_mutex );
456 ldap_pvt_thread_mutex_unlock( &connections_mutex );
461 Connection* connection_first(void)
463 assert( connections != NULL );
465 ldap_pvt_thread_mutex_lock( &connections_mutex );
467 assert( conn_index == -1 );
470 return connection_next(NULL);
473 Connection* connection_next(Connection *c)
475 assert( connections != NULL );
476 assert( conn_index != -1 );
477 assert( conn_index <= dtblsize );
480 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
485 for(; conn_index < dtblsize; conn_index++) {
486 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
487 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
495 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
496 assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
497 c = &connections[conn_index++];
501 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
502 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
506 ldap_pvt_thread_mutex_lock( &c->c_mutex );
512 void connection_done(Connection *c)
514 assert( connections != NULL );
515 assert( conn_index != -1 );
516 assert( conn_index <= dtblsize );
519 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
523 ldap_pvt_thread_mutex_unlock( &connections_mutex );
527 * connection_activity - handle the request operation op on connection
528 * conn. This routine figures out what kind of operation it is and
529 * calls the appropriate stub to handle it.
533 connection_operation( void *arg_v )
535 struct co_arg *arg = arg_v;
536 int tag = arg->co_op->o_tag;
537 Connection *conn = arg->co_conn;
540 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
542 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
547 do_bind( conn, arg->co_op );
551 case LDAP_REQ_UNBIND_30:
553 case LDAP_REQ_UNBIND:
554 do_unbind( conn, arg->co_op );
558 do_add( conn, arg->co_op );
562 case LDAP_REQ_DELETE_30:
564 case LDAP_REQ_DELETE:
565 do_delete( conn, arg->co_op );
568 case LDAP_REQ_MODRDN:
569 do_modrdn( conn, arg->co_op );
572 case LDAP_REQ_MODIFY:
573 do_modify( conn, arg->co_op );
576 case LDAP_REQ_COMPARE:
577 do_compare( conn, arg->co_op );
580 case LDAP_REQ_SEARCH:
581 do_search( conn, arg->co_op );
585 case LDAP_REQ_ABANDON_30:
587 case LDAP_REQ_ABANDON:
588 do_abandon( conn, arg->co_op );
592 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
593 arg->co_op->o_tag, 0, 0 );
598 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
600 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
603 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
606 conn->c_n_ops_completed++;
609 slap_op_remove( &conn->c_ops, arg->co_op );
610 slap_op_free( arg->co_op );
613 free( (char *) arg );
618 case LDAP_REQ_UNBIND_30:
620 case LDAP_REQ_UNBIND:
621 connection_closing( conn );
625 if( conn->c_conn_state == SLAP_C_BINDING) {
626 conn->c_conn_state = SLAP_C_ACTIVE;
630 if( conn->c_conn_state == SLAP_C_CLOSING ) {
631 Debug( LDAP_DEBUG_TRACE,
632 "connection_operation: attempting closing conn=%ld sd=%d.\n",
633 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
635 connection_close( conn );
638 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
640 if( active_threads < 1 ) {
641 ldap_pvt_thread_cond_signal(&active_threads_cond);
643 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
645 connection_resched( conn );
647 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
652 int connection_read(int s)
656 assert( connections != NULL );
658 ldap_pvt_thread_mutex_lock( &connections_mutex );
660 c = connection_get( s );
662 Debug( LDAP_DEBUG_ANY,
663 "connection_read(%d): no connection!\n",
665 ldap_pvt_thread_mutex_unlock( &connections_mutex );
669 if( c->c_conn_state == SLAP_C_CLOSING ) {
670 Debug( LDAP_DEBUG_TRACE,
671 "connection_read(%d): closing, ignoring input for id=%ld\n",
674 connection_return( c );
675 ldap_pvt_thread_mutex_unlock( &connections_mutex );
679 Debug( LDAP_DEBUG_TRACE,
680 "connection_read(%d): checking for input on id=%ld\n",
683 #define CONNECTION_INPUT_LOOP 1
685 #ifdef DATA_READY_LOOP
686 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
687 #elif CONNECTION_INPUT_LOOP
691 rc = connection_input( c );
695 Debug( LDAP_DEBUG_TRACE,
696 "connection_read(%d): input error=%d id=%ld, closing.\n",
697 s, rc, c->c_connid );
699 connection_closing( c );
700 connection_close( c );
703 connection_return( c );
704 ldap_pvt_thread_mutex_unlock( &connections_mutex );
714 unsigned long tag, len;
718 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
720 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
725 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
726 != LDAP_TAG_MESSAGE )
730 Debug( LDAP_DEBUG_TRACE,
731 "ber_get_next on fd %d failed errno %d (%s)\n",
732 ber_pvt_sb_get_desc( conn->c_sb ), err,
733 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
734 Debug( LDAP_DEBUG_TRACE,
735 "\t*** got %ld of %lu so far\n",
736 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
737 conn->c_currentber->ber_len, 0 );
739 if ( err != EWOULDBLOCK && err != EAGAIN ) {
740 /* log, close and send error */
741 ber_free( conn->c_currentber, 1 );
742 conn->c_currentber = NULL;
749 ber = conn->c_currentber;
750 conn->c_currentber = NULL;
752 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
753 /* log, close and send error */
754 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
760 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
761 /* log, close and send error */
762 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
770 if ( conn->c_version == 30 ) {
771 (void) ber_skip_tag( ber, &len );
775 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
777 if ( conn->c_conn_state == SLAP_C_BINDING
778 || conn->c_conn_state == SLAP_C_CLOSING )
780 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
781 slap_op_add( &conn->c_pending_ops, op );
784 connection_op_activate( conn, op );
788 if ( conn->c_struct_state != SLAP_C_USED ) {
789 /* connection must have got closed underneath us */
793 assert( conn->c_struct_state == SLAP_C_USED );
799 connection_resched( Connection *conn )
803 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
804 /* other states need different handling */
808 for( op = slap_op_pop( &conn->c_pending_ops );
810 op = slap_op_pop( &conn->c_pending_ops ) )
812 /* pending operations should not be marked for abandonment */
813 assert(!op->o_abandon);
815 connection_op_activate( conn, op );
817 if ( conn->c_conn_state == SLAP_C_BINDING ) {
824 static int connection_op_activate( Connection *conn, Operation *op )
829 unsigned long tag = op->o_tag;
831 if ( conn->c_dn != NULL ) {
832 tmpdn = ch_strdup( conn->c_dn );
837 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
841 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
842 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
844 slap_op_add( &conn->c_ops, arg->co_op );
846 if(tag == LDAP_REQ_BIND) {
847 conn->c_conn_state = SLAP_C_BINDING;
850 if ( tmpdn != NULL ) {
854 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
856 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
858 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
859 connection_operation, (void *) arg );
862 Debug( LDAP_DEBUG_ANY,
863 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
865 /* should move op to pending list */
871 int connection_write(int s)
874 assert( connections != NULL );
876 ldap_pvt_thread_mutex_lock( &connections_mutex );
878 c = connection_get( s );
880 Debug( LDAP_DEBUG_ANY,
881 "connection_write(%d): no connection!\n",
883 ldap_pvt_thread_mutex_unlock( &connections_mutex );
887 Debug( LDAP_DEBUG_TRACE,
888 "connection_write(%d): waking output for id=%ld\n",
891 ldap_pvt_thread_cond_signal( &c->c_write_cv );
893 connection_return( c );
894 ldap_pvt_thread_mutex_unlock( &connections_mutex );