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 );
75 for ( i = 0; i < dtblsize; i++ )
76 memset( &connections[i], 0, sizeof(Connection) );
79 * per entry initialization of the Connection array initialization
80 * will be done by connection_init()
87 * Destroy connection management infrastructure.
89 int connections_destroy(void)
93 /* should check return of every call */
95 if( connections == NULL) {
96 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
101 for ( i = 0; i < dtblsize; i++ ) {
102 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
103 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
104 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
105 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
112 ldap_pvt_thread_mutex_destroy( &connections_mutex );
117 * shutdown all connections
119 int connections_shutdown(void)
123 ldap_pvt_thread_mutex_lock( &connections_mutex );
125 for ( i = 0; i < dtblsize; i++ ) {
126 if( connections[i].c_struct_state != SLAP_C_USED ) {
130 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
131 connection_closing( &connections[i] );
132 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 Connection *c = NULL;
145 assert( connections != NULL );
152 assert( connections[s].c_struct_state == SLAP_C_USED );
153 assert( connections[s].c_conn_state != SLAP_C_INVALID );
154 assert( ber_pvt_sb_in_use( connections[s].c_sb ) );
161 for(i=0; i<dtblsize; i++) {
162 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
163 assert( connections[i].c_conn_state == SLAP_C_INVALID );
164 assert( connections[i].c_sb == 0 );
168 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
169 assert( connections[i].c_conn_state == SLAP_C_INVALID );
170 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
174 assert( connections[i].c_struct_state == SLAP_C_USED );
175 assert( connections[i].c_conn_state != SLAP_C_INVALID );
176 assert( ber_pvt_sb_in_use( connections[i].c_sb ) );
178 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
187 /* we do this BEFORE locking to aid in debugging */
188 Debug( LDAP_DEBUG_TRACE,
189 "connection_get(%d): got connid=%ld\n",
192 ldap_pvt_thread_mutex_lock( &c->c_mutex );
197 static void connection_return( Connection *c )
199 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
202 long connection_init(
209 assert( connections != NULL );
217 assert( s < dtblsize );
220 ldap_pvt_thread_mutex_lock( &connections_mutex );
229 for( i=0; i < dtblsize; i++) {
230 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
231 assert( connections[i].c_sb == 0 );
236 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
237 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
242 assert( connections[i].c_struct_state == SLAP_C_USED );
243 assert( connections[i].c_conn_state != SLAP_C_INVALID );
244 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
248 ldap_pvt_thread_mutex_unlock( &connections_mutex );
255 assert( c->c_struct_state != SLAP_C_USED );
256 assert( c->c_conn_state == SLAP_C_INVALID );
258 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
261 c->c_client_name = NULL;
262 c->c_client_addr = NULL;
264 c->c_pending_ops = NULL;
266 c->c_sb = ber_sockbuf_alloc( );
268 /* should check status of thread calls */
269 ldap_pvt_thread_mutex_init( &c->c_mutex );
270 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
271 ldap_pvt_thread_cond_init( &c->c_write_cv );
273 c->c_struct_state = SLAP_C_UNUSED;
276 ldap_pvt_thread_mutex_lock( &c->c_mutex );
278 assert( c->c_struct_state == SLAP_C_UNUSED );
279 assert( c->c_dn == NULL );
280 assert( c->c_cdn == NULL );
281 assert( c->c_client_name == NULL );
282 assert( c->c_client_addr == NULL );
283 assert( c->c_ops == NULL );
284 assert( c->c_pending_ops == NULL );
286 c->c_client_name = ch_strdup( name == NULL ? "" : name );
287 c->c_client_addr = ch_strdup( addr );
289 c->c_n_ops_received = 0;
291 c->c_n_ops_executing = 0;
292 c->c_n_ops_pending = 0;
293 c->c_n_ops_completed = 0;
296 c->c_starttime = slap_get_time();
298 ber_pvt_sb_set_desc( c->c_sb, s );
299 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
301 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
302 Debug( LDAP_DEBUG_ANY,
303 "connection_init(%d, %s, %s): set nonblocking failed\n",
304 s, c->c_client_name, c->c_client_addr);
307 id = c->c_connid = conn_nextid++;
309 c->c_conn_state = SLAP_C_INACTIVE;
310 c->c_struct_state = SLAP_C_USED;
312 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
313 ldap_pvt_thread_mutex_unlock( &connections_mutex );
319 connection_destroy( Connection *c )
321 assert( connections != NULL );
323 assert( c->c_struct_state != SLAP_C_UNUSED );
324 assert( c->c_conn_state != SLAP_C_INVALID );
325 assert( c->c_ops == NULL );
327 ldap_pvt_thread_mutex_lock( &connections_mutex );
328 c->c_struct_state = SLAP_C_UNUSED;
329 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 );
365 ldap_pvt_thread_mutex_unlock( &connections_mutex );
368 int connection_state_closing( Connection *c )
370 /* connection must be locked by caller */
373 assert( c->c_struct_state == SLAP_C_USED );
375 state = c->c_conn_state;
377 assert( state != SLAP_C_INVALID );
379 return state == SLAP_C_CLOSING;
382 void connection_closing( Connection *c )
384 assert( connections != NULL );
386 assert( c->c_struct_state == SLAP_C_USED );
387 assert( c->c_conn_state != SLAP_C_INVALID );
389 if( c->c_conn_state != SLAP_C_CLOSING ) {
392 Debug( LDAP_DEBUG_TRACE,
393 "connection_closing: readying conn=%ld sd=%d for close.\n",
394 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
396 /* update state to closing */
397 c->c_conn_state = SLAP_C_CLOSING;
399 /* don't listen on this port anymore */
400 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
402 /* shutdown I/O -- not yet implemented */
404 /* abandon active operations */
405 for( o = c->c_ops; o != NULL; o = o->o_next ) {
406 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
408 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
411 /* remove pending operations */
412 for( o = slap_op_pop( &c->c_pending_ops );
414 o = slap_op_pop( &c->c_pending_ops ) )
419 /* wake write blocked operations */
420 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
421 ldap_pvt_thread_cond_signal( &c->c_write_cv );
425 static void connection_close( Connection *c )
427 assert( connections != NULL );
429 assert( c->c_struct_state == SLAP_C_USED );
430 assert( c->c_conn_state == SLAP_C_CLOSING );
432 if( c->c_ops != NULL ) {
433 Debug( LDAP_DEBUG_TRACE,
434 "connection_close: deferring conn=%ld sd=%d.\n",
435 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
440 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
441 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
443 connection_destroy( c );
446 long connections_nextid(void)
449 assert( connections != NULL );
451 ldap_pvt_thread_mutex_lock( &connections_mutex );
455 ldap_pvt_thread_mutex_unlock( &connections_mutex );
460 Connection* connection_first(void)
462 assert( connections != NULL );
464 ldap_pvt_thread_mutex_lock( &connections_mutex );
466 assert( conn_index == -1 );
469 return connection_next(NULL);
472 Connection* connection_next(Connection *c)
474 assert( connections != NULL );
475 assert( conn_index != -1 );
476 assert( conn_index <= dtblsize );
479 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
484 for(; conn_index < dtblsize; conn_index++) {
485 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
486 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
494 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
495 assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
496 c = &connections[conn_index++];
500 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
501 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
505 ldap_pvt_thread_mutex_lock( &c->c_mutex );
511 void connection_done(Connection *c)
513 assert( connections != NULL );
514 assert( conn_index != -1 );
515 assert( conn_index <= dtblsize );
518 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
522 ldap_pvt_thread_mutex_unlock( &connections_mutex );
526 * connection_activity - handle the request operation op on connection
527 * conn. This routine figures out what kind of operation it is and
528 * calls the appropriate stub to handle it.
532 connection_operation( void *arg_v )
534 struct co_arg *arg = arg_v;
535 int tag = arg->co_op->o_tag;
536 Connection *conn = arg->co_conn;
539 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
541 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
546 do_bind( conn, arg->co_op );
550 case LDAP_REQ_UNBIND_30:
552 case LDAP_REQ_UNBIND:
553 do_unbind( conn, arg->co_op );
557 do_add( conn, arg->co_op );
561 case LDAP_REQ_DELETE_30:
563 case LDAP_REQ_DELETE:
564 do_delete( conn, arg->co_op );
567 case LDAP_REQ_MODRDN:
568 do_modrdn( conn, arg->co_op );
571 case LDAP_REQ_MODIFY:
572 do_modify( conn, arg->co_op );
575 case LDAP_REQ_COMPARE:
576 do_compare( conn, arg->co_op );
579 case LDAP_REQ_SEARCH:
580 do_search( conn, arg->co_op );
584 case LDAP_REQ_ABANDON_30:
586 case LDAP_REQ_ABANDON:
587 do_abandon( conn, arg->co_op );
591 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
592 arg->co_op->o_tag, 0, 0 );
597 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
599 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
602 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
605 conn->c_n_ops_completed++;
608 slap_op_remove( &conn->c_ops, arg->co_op );
609 slap_op_free( arg->co_op );
612 free( (char *) arg );
617 case LDAP_REQ_UNBIND_30:
619 case LDAP_REQ_UNBIND:
620 connection_closing( conn );
624 if( conn->c_conn_state == SLAP_C_BINDING) {
625 conn->c_conn_state = SLAP_C_ACTIVE;
629 if( conn->c_conn_state == SLAP_C_CLOSING ) {
630 Debug( LDAP_DEBUG_TRACE,
631 "connection_operation: attempting closing conn=%ld sd=%d.\n",
632 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
634 connection_close( conn );
637 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
639 if( active_threads < 1 ) {
640 ldap_pvt_thread_cond_signal(&active_threads_cond);
642 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
644 connection_resched( conn );
646 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
651 int connection_read(int s)
655 assert( connections != NULL );
657 ldap_pvt_thread_mutex_lock( &connections_mutex );
659 c = connection_get( s );
661 Debug( LDAP_DEBUG_ANY,
662 "connection_read(%d): no connection!\n",
664 ldap_pvt_thread_mutex_unlock( &connections_mutex );
668 if( c->c_conn_state == SLAP_C_CLOSING ) {
669 Debug( LDAP_DEBUG_TRACE,
670 "connection_read(%d): closing, ignoring input for id=%ld\n",
673 connection_return( c );
674 ldap_pvt_thread_mutex_unlock( &connections_mutex );
678 Debug( LDAP_DEBUG_TRACE,
679 "connection_read(%d): checking for input on id=%ld\n",
682 #define CONNECTION_INPUT_LOOP 1
684 #ifdef DATA_READY_LOOP
685 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
686 #elif CONNECTION_INPUT_LOOP
690 rc = connection_input( c );
694 Debug( LDAP_DEBUG_TRACE,
695 "connection_read(%d): input error=%d id=%ld, closing.\n",
696 s, rc, c->c_connid );
698 connection_closing( c );
699 connection_close( c );
702 connection_return( c );
703 ldap_pvt_thread_mutex_unlock( &connections_mutex );
713 unsigned long tag, len;
717 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
719 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
724 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
725 != LDAP_TAG_MESSAGE )
729 Debug( LDAP_DEBUG_TRACE,
730 "ber_get_next on fd %d failed errno %d (%s)\n",
731 ber_pvt_sb_get_desc( conn->c_sb ), err,
732 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
733 Debug( LDAP_DEBUG_TRACE,
734 "\t*** got %ld of %lu so far\n",
735 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
736 conn->c_currentber->ber_len, 0 );
738 if ( err != EWOULDBLOCK && err != EAGAIN ) {
739 /* log, close and send error */
740 ber_free( conn->c_currentber, 1 );
741 conn->c_currentber = NULL;
748 ber = conn->c_currentber;
749 conn->c_currentber = NULL;
751 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
752 /* log, close and send error */
753 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
759 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
760 /* log, close and send error */
761 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
769 if ( conn->c_version == 30 ) {
770 (void) ber_skip_tag( ber, &len );
774 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
776 if ( conn->c_conn_state == SLAP_C_BINDING
777 || conn->c_conn_state == SLAP_C_CLOSING )
779 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
780 slap_op_add( &conn->c_pending_ops, op );
783 connection_op_activate( conn, op );
787 if ( conn->c_struct_state != SLAP_C_USED ) {
788 /* connection must have got closed underneath us */
792 assert( conn->c_struct_state == SLAP_C_USED );
798 connection_resched( Connection *conn )
802 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
803 /* other states need different handling */
807 for( op = slap_op_pop( &conn->c_pending_ops );
809 op = slap_op_pop( &conn->c_pending_ops ) )
811 /* pending operations should not be marked for abandonment */
812 assert(!op->o_abandon);
814 connection_op_activate( conn, op );
816 if ( conn->c_conn_state == SLAP_C_BINDING ) {
823 static int connection_op_activate( Connection *conn, Operation *op )
828 unsigned long tag = op->o_tag;
830 if ( conn->c_dn != NULL ) {
831 tmpdn = ch_strdup( conn->c_dn );
836 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
840 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
841 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
843 slap_op_add( &conn->c_ops, arg->co_op );
845 if(tag == LDAP_REQ_BIND) {
846 conn->c_conn_state = SLAP_C_BINDING;
849 if ( tmpdn != NULL ) {
853 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
855 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
857 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
858 connection_operation, (void *) arg );
861 Debug( LDAP_DEBUG_ANY,
862 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
864 /* should move op to pending list */
870 int connection_write(int s)
873 assert( connections != NULL );
875 ldap_pvt_thread_mutex_lock( &connections_mutex );
877 c = connection_get( s );
879 Debug( LDAP_DEBUG_ANY,
880 "connection_write(%d): no connection!\n",
882 ldap_pvt_thread_mutex_unlock( &connections_mutex );
886 Debug( LDAP_DEBUG_TRACE,
887 "connection_write(%d): waking output for id=%ld\n",
890 ldap_pvt_thread_cond_signal( &c->c_write_cv );
892 connection_return( c );
893 ldap_pvt_thread_mutex_unlock( &connections_mutex );