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 );
219 assert( s < dtblsize );
222 ldap_pvt_thread_mutex_lock( &connections_mutex );
231 for( i=0; i < dtblsize; i++) {
232 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
233 assert( connections[i].c_sb == 0 );
238 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
239 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
244 assert( connections[i].c_struct_state == SLAP_C_USED );
245 assert( connections[i].c_conn_state != SLAP_C_INVALID );
246 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
250 ldap_pvt_thread_mutex_unlock( &connections_mutex );
257 assert( c->c_struct_state != SLAP_C_USED );
258 assert( c->c_conn_state == SLAP_C_INVALID );
260 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
263 c->c_client_name = NULL;
264 c->c_client_addr = NULL;
266 c->c_pending_ops = NULL;
268 c->c_sb = ber_sockbuf_alloc( );
270 /* should check status of thread calls */
271 ldap_pvt_thread_mutex_init( &c->c_mutex );
272 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
273 ldap_pvt_thread_cond_init( &c->c_write_cv );
275 c->c_struct_state = SLAP_C_UNUSED;
278 ldap_pvt_thread_mutex_lock( &c->c_mutex );
280 assert( c->c_struct_state == SLAP_C_UNUSED );
281 assert( c->c_dn == NULL );
282 assert( c->c_cdn == NULL );
283 assert( c->c_client_name == NULL );
284 assert( c->c_client_addr == NULL );
285 assert( c->c_ops == NULL );
286 assert( c->c_pending_ops == NULL );
288 c->c_client_name = ch_strdup( name == NULL ? "" : name );
289 c->c_client_addr = ch_strdup( addr );
291 c->c_n_ops_received = 0;
293 c->c_n_ops_executing = 0;
294 c->c_n_ops_pending = 0;
295 c->c_n_ops_completed = 0;
298 c->c_starttime = slap_get_time();
300 ber_pvt_sb_set_desc( c->c_sb, s );
301 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
303 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
304 Debug( LDAP_DEBUG_ANY,
305 "connection_init(%d, %s, %s): set nonblocking failed\n",
306 s, c->c_client_name, c->c_client_addr);
309 id = c->c_connid = conn_nextid++;
311 c->c_conn_state = SLAP_C_INACTIVE;
312 c->c_struct_state = SLAP_C_USED;
314 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
315 ldap_pvt_thread_mutex_unlock( &connections_mutex );
321 connection_destroy( Connection *c )
323 /* note: connections_mutex should be locked by caller */
325 assert( connections != NULL );
327 assert( c->c_struct_state != SLAP_C_UNUSED );
328 assert( c->c_conn_state != SLAP_C_INVALID );
329 assert( c->c_ops == NULL );
331 c->c_struct_state = SLAP_C_UNUSED;
332 c->c_conn_state = SLAP_C_INVALID;
341 if(c->c_dn != NULL) {
345 if(c->c_cdn != NULL) {
349 if(c->c_client_name != NULL) {
350 free(c->c_client_name);
351 c->c_client_name = NULL;
353 if(c->c_client_addr != NULL) {
354 free(c->c_client_addr);
355 c->c_client_addr = NULL;
358 if ( ber_pvt_sb_in_use(c->c_sb) ) {
359 int sd = ber_pvt_sb_get_desc(c->c_sb);
362 ber_pvt_sb_close( c->c_sb );
364 Statslog( LDAP_DEBUG_STATS,
365 "conn=%d fd=%d closed.\n",
366 c->c_connid, sd, 0, 0, 0 );
369 ber_pvt_sb_destroy( c->c_sb );
372 int connection_state_closing( Connection *c )
374 /* c_mutex must be locked by caller */
378 assert( c->c_struct_state == SLAP_C_USED );
380 state = c->c_conn_state;
382 assert( state != SLAP_C_INVALID );
384 return state == SLAP_C_CLOSING;
387 void connection_closing( Connection *c )
389 assert( connections != NULL );
391 assert( c->c_struct_state == SLAP_C_USED );
392 assert( c->c_conn_state != SLAP_C_INVALID );
394 /* c_mutex must be locked by caller */
396 if( c->c_conn_state != SLAP_C_CLOSING ) {
399 Debug( LDAP_DEBUG_TRACE,
400 "connection_closing: readying conn=%ld sd=%d for close.\n",
401 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
403 /* update state to closing */
404 c->c_conn_state = SLAP_C_CLOSING;
406 /* don't listen on this port anymore */
407 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
409 /* shutdown I/O -- not yet implemented */
411 /* abandon active operations */
412 for( o = c->c_ops; o != NULL; o = o->o_next ) {
413 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
415 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
418 /* remove pending operations */
419 for( o = slap_op_pop( &c->c_pending_ops );
421 o = slap_op_pop( &c->c_pending_ops ) )
426 /* wake write blocked operations */
427 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
428 ldap_pvt_thread_cond_signal( &c->c_write_cv );
432 static void connection_close( Connection *c )
434 assert( connections != NULL );
436 assert( c->c_struct_state == SLAP_C_USED );
437 assert( c->c_conn_state == SLAP_C_CLOSING );
439 /* note: connections_mutex and c_mutex should be locked by caller */
441 if( c->c_ops != NULL ) {
442 Debug( LDAP_DEBUG_TRACE,
443 "connection_close: deferring conn=%ld sd=%d.\n",
444 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
449 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
450 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
452 connection_destroy( c );
455 long connections_nextid(void)
458 assert( connections != NULL );
460 ldap_pvt_thread_mutex_lock( &connections_mutex );
464 ldap_pvt_thread_mutex_unlock( &connections_mutex );
469 Connection* connection_first( int *index )
471 assert( connections != NULL );
472 assert( index != NULL );
474 ldap_pvt_thread_mutex_lock( &connections_mutex );
478 return connection_next(NULL, index);
481 Connection* connection_next( Connection *c, int *index )
483 assert( connections != NULL );
484 assert( index != NULL );
485 assert( *index <= dtblsize );
488 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
493 for(; *index < dtblsize; (*index)++) {
494 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
495 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
503 if( connections[*index].c_struct_state == SLAP_C_USED ) {
504 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
505 c = &connections[(*index)++];
509 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
510 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
514 ldap_pvt_thread_mutex_lock( &c->c_mutex );
520 void connection_done( Connection *c )
522 assert( connections != NULL );
525 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
528 ldap_pvt_thread_mutex_unlock( &connections_mutex );
532 * connection_activity - handle the request operation op on connection
533 * conn. This routine figures out what kind of operation it is and
534 * calls the appropriate stub to handle it.
538 connection_operation( void *arg_v )
540 struct co_arg *arg = arg_v;
541 int tag = arg->co_op->o_tag;
542 Connection *conn = arg->co_conn;
545 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
547 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
552 do_bind( conn, arg->co_op );
556 case LDAP_REQ_UNBIND_30:
558 case LDAP_REQ_UNBIND:
559 do_unbind( conn, arg->co_op );
563 do_add( conn, arg->co_op );
567 case LDAP_REQ_DELETE_30:
569 case LDAP_REQ_DELETE:
570 do_delete( conn, arg->co_op );
573 case LDAP_REQ_MODRDN:
574 do_modrdn( conn, arg->co_op );
577 case LDAP_REQ_MODIFY:
578 do_modify( conn, arg->co_op );
581 case LDAP_REQ_COMPARE:
582 do_compare( conn, arg->co_op );
585 case LDAP_REQ_SEARCH:
586 do_search( conn, arg->co_op );
590 case LDAP_REQ_ABANDON_30:
592 case LDAP_REQ_ABANDON:
593 do_abandon( conn, arg->co_op );
597 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
598 arg->co_op->o_tag, 0, 0 );
603 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
605 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
608 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
611 conn->c_n_ops_completed++;
614 slap_op_remove( &conn->c_ops, arg->co_op );
615 slap_op_free( arg->co_op );
618 free( (char *) arg );
623 case LDAP_REQ_UNBIND_30:
625 case LDAP_REQ_UNBIND:
626 /* c_mutex is locked */
627 connection_closing( conn );
631 if( conn->c_conn_state == SLAP_C_BINDING) {
632 conn->c_conn_state = SLAP_C_ACTIVE;
636 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
638 if( active_threads < 1 ) {
639 ldap_pvt_thread_cond_signal(&active_threads_cond);
641 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
643 connection_resched( conn );
645 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
650 int connection_read(int s)
654 assert( connections != NULL );
656 ldap_pvt_thread_mutex_lock( &connections_mutex );
658 /* get (locked) connection */
659 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 /* connections_mutex and c_mutex are locked */
700 connection_closing( c );
701 connection_close( c );
704 connection_return( c );
705 ldap_pvt_thread_mutex_unlock( &connections_mutex );
715 unsigned long tag, len;
719 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
721 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
726 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
727 != LDAP_TAG_MESSAGE )
731 Debug( LDAP_DEBUG_TRACE,
732 "ber_get_next on fd %d failed errno %d (%s)\n",
733 ber_pvt_sb_get_desc( conn->c_sb ), err,
734 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
735 Debug( LDAP_DEBUG_TRACE,
736 "\t*** got %ld of %lu so far\n",
737 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
738 conn->c_currentber->ber_len, 0 );
740 if ( err != EWOULDBLOCK && err != EAGAIN ) {
741 /* log, close and send error */
742 ber_free( conn->c_currentber, 1 );
743 conn->c_currentber = NULL;
750 ber = conn->c_currentber;
751 conn->c_currentber = NULL;
753 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
754 /* log, close and send error */
755 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
761 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
762 /* log, close and send error */
763 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
771 if ( conn->c_version == 30 ) {
772 (void) ber_skip_tag( ber, &len );
776 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
778 if ( conn->c_conn_state == SLAP_C_BINDING
779 || conn->c_conn_state == SLAP_C_CLOSING )
781 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
782 slap_op_add( &conn->c_pending_ops, op );
785 connection_op_activate( conn, op );
789 if ( conn->c_struct_state != SLAP_C_USED ) {
790 /* connection must have got closed underneath us */
794 assert( conn->c_struct_state == SLAP_C_USED );
800 connection_resched( Connection *conn )
804 if( conn->c_conn_state == SLAP_C_CLOSING ) {
805 Debug( LDAP_DEBUG_TRACE,
806 "connection_resched: attempting closing conn=%ld sd=%d.\n",
807 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
809 connection_close( conn );
813 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
814 /* other states need different handling */
818 for( op = slap_op_pop( &conn->c_pending_ops );
820 op = slap_op_pop( &conn->c_pending_ops ) )
822 /* pending operations should not be marked for abandonment */
823 assert(!op->o_abandon);
825 connection_op_activate( conn, op );
827 if ( conn->c_conn_state == SLAP_C_BINDING ) {
834 static int connection_op_activate( Connection *conn, Operation *op )
839 unsigned long tag = op->o_tag;
841 if ( conn->c_dn != NULL ) {
842 tmpdn = ch_strdup( conn->c_dn );
847 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
851 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
852 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
854 slap_op_add( &conn->c_ops, arg->co_op );
856 if(tag == LDAP_REQ_BIND) {
857 conn->c_conn_state = SLAP_C_BINDING;
860 if ( tmpdn != NULL ) {
864 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
866 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
868 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
869 connection_operation, (void *) arg );
872 Debug( LDAP_DEBUG_ANY,
873 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
875 /* should move op to pending list */
881 int connection_write(int s)
884 assert( connections != NULL );
886 ldap_pvt_thread_mutex_lock( &connections_mutex );
888 c = connection_get( s );
890 Debug( LDAP_DEBUG_ANY,
891 "connection_write(%d): no connection!\n",
893 ldap_pvt_thread_mutex_unlock( &connections_mutex );
897 Debug( LDAP_DEBUG_TRACE,
898 "connection_write(%d): waking output for id=%ld\n",
901 ldap_pvt_thread_cond_signal( &c->c_write_cv );
903 connection_return( c );
904 ldap_pvt_thread_mutex_unlock( &connections_mutex );