13 /* protected by connections_mutex */
14 static ldap_pvt_thread_mutex_t connections_mutex;
15 static Connection *connections = NULL;
16 static int conn_index = -1;
17 static long conn_nextid = 0;
19 /* structure state (protected by connections_mutex) */
20 #define SLAP_C_UNINITIALIZED 0x0 /* MUST BE ZERO (0) */
21 #define SLAP_C_UNUSED 0x1
22 #define SLAP_C_USED 0x2
24 /* connection state (protected by c_mutex ) */
25 #define SLAP_C_INVALID 0x0 /* MUST BE ZERO (0) */
26 #define SLAP_C_INACTIVE 0x1 /* zero threads */
27 #define SLAP_C_ACTIVE 0x2 /* one or more threads */
28 #define SLAP_C_BINDING 0x3 /* binding */
29 #define SLAP_C_CLOSING 0x4 /* closing */
31 void slapd_remove(int s);
32 static Connection* connection_get( int s );
34 static int connection_input( Connection *c );
35 static void connection_close( Connection *c );
37 static int connection_op_activate( Connection *conn, Operation *op );
38 static int connection_resched( Connection *conn );
46 * Initialize connection management infrastructure.
48 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 for ( i = 0; i < dtblsize; i++ )
73 memset( &connections[i], 0, sizeof(Connection) );
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 );
105 free( &connections[i] );
111 ldap_pvt_thread_mutex_destroy( &connections_mutex );
116 * shutdown all connections
118 int connections_shutdown(void)
122 ldap_pvt_thread_mutex_lock( &connections_mutex );
124 for ( i = 0; i < dtblsize; i++ ) {
125 if( connections[i].c_struct_state != SLAP_C_USED ) {
129 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
130 connection_closing( &connections[i] );
131 connection_close( &connections[i] );
132 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
135 ldap_pvt_thread_mutex_unlock( &connections_mutex );
139 static Connection* connection_get( int s )
141 Connection *c = NULL;
143 assert( connections != NULL );
150 assert( connections[s].c_struct_state == SLAP_C_USED );
151 assert( connections[s].c_conn_state != SLAP_C_INVALID );
152 assert( connections[s].c_sb.sb_sd != -1 );
159 for(i=0; i<dtblsize; i++) {
160 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
161 assert( connections[i].c_conn_state == SLAP_C_INVALID );
162 assert( connections[i].c_sb.sb_sd == 0 );
166 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
167 assert( connections[i].c_conn_state == SLAP_C_INVALID );
168 assert( connections[i].c_sb.sb_sd == -1 );
172 assert( connections[i].c_struct_state == SLAP_C_USED );
173 assert( connections[i].c_conn_state != SLAP_C_INVALID );
174 assert( connections[i].c_sb.sb_sd != -1 );
176 if( connections[i].c_sb.sb_sd == s ) {
185 /* we do this BEFORE locking to aid in debugging */
186 Debug( LDAP_DEBUG_TRACE,
187 "connection_get(%d): got connid=%ld\n",
190 ldap_pvt_thread_mutex_lock( &c->c_mutex );
195 static void connection_return( Connection *c )
197 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
200 long connection_init(
207 assert( connections != NULL );
215 assert( s < dtblsize );
218 ldap_pvt_thread_mutex_lock( &connections_mutex );
227 for( i=0; i < dtblsize; i++) {
228 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
229 assert( connections[i].c_sb.sb_sd == 0 );
234 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
235 assert( connections[i].c_sb.sb_sd == -1 );
240 assert( connections[i].c_struct_state == SLAP_C_USED );
241 assert( connections[i].c_conn_state != SLAP_C_INVALID );
242 assert( connections[i].c_sb.sb_sd != -1 );
246 ldap_pvt_thread_mutex_unlock( &connections_mutex );
253 assert( c->c_struct_state != SLAP_C_USED );
254 assert( c->c_conn_state == SLAP_C_INVALID );
256 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
259 c->c_client_name = NULL;
260 c->c_client_addr = NULL;
262 c->c_pending_ops = NULL;
264 lber_pvt_sb_init( &c->c_sb );
266 /* should check status of thread calls */
267 ldap_pvt_thread_mutex_init( &c->c_mutex );
268 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
269 ldap_pvt_thread_cond_init( &c->c_write_cv );
271 c->c_struct_state = SLAP_C_UNUSED;
274 ldap_pvt_thread_mutex_lock( &c->c_mutex );
276 assert( c->c_struct_state == SLAP_C_UNUSED );
277 assert( c->c_dn == NULL );
278 assert( c->c_cdn == NULL );
279 assert( c->c_client_name == NULL );
280 assert( c->c_client_addr == NULL );
281 assert( c->c_ops == NULL );
282 assert( c->c_pending_ops == NULL );
284 c->c_client_name = ch_strdup( name == NULL ? "" : name );
285 c->c_client_addr = ch_strdup( addr );
287 c->c_n_ops_received = 0;
289 c->c_n_ops_executing = 0;
290 c->c_n_ops_pending = 0;
291 c->c_n_ops_completed = 0;
294 c->c_starttime = slap_get_time();
296 lber_pvt_sb_set_desc( &c->c_sb, s );
297 lber_pvt_sb_set_io( &c->c_sb, &lber_pvt_sb_io_tcp, NULL );
299 if( lber_pvt_sb_set_nonblock( &c->c_sb, 1 ) < 0 ) {
300 Debug( LDAP_DEBUG_ANY,
301 "connection_init(%d, %s, %s): set nonblocking failed\n",
302 s, c->c_client_name, c->c_client_addr);
305 id = c->c_connid = conn_nextid++;
307 c->c_conn_state = SLAP_C_INACTIVE;
308 c->c_struct_state = SLAP_C_USED;
310 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
311 ldap_pvt_thread_mutex_unlock( &connections_mutex );
317 connection_destroy( Connection *c )
319 assert( connections != NULL );
321 assert( c->c_struct_state != SLAP_C_UNUSED );
322 assert( c->c_conn_state != SLAP_C_INVALID );
323 assert( c->c_ops == NULL );
325 c->c_struct_state = SLAP_C_UNUSED;
326 c->c_conn_state = SLAP_C_INVALID;
333 if(c->c_dn != NULL) {
337 if(c->c_cdn != NULL) {
341 if(c->c_client_name != NULL) {
342 free(c->c_client_name);
343 c->c_client_name = NULL;
345 if(c->c_client_addr != NULL) {
346 free(c->c_client_addr);
347 c->c_client_addr = NULL;
350 if ( lber_pvt_sb_in_use(&c->c_sb) ) {
351 int sd = lber_pvt_sb_get_desc(&c->c_sb);
354 lber_pvt_sb_close( &c->c_sb );
356 Statslog( LDAP_DEBUG_STATS,
357 "conn=%d fd=%d closed.\n",
358 c->c_connid, sd, 0, 0, 0 );
361 lber_pvt_sb_destroy( &c->c_sb );
364 int connection_state_closing( Connection *c )
366 /* connection must be locked by caller */
369 assert( c->c_struct_state == SLAP_C_USED );
371 state = c->c_conn_state;
373 assert( state != SLAP_C_INVALID );
375 return state == SLAP_C_CLOSING;
378 void connection_closing( Connection *c )
380 assert( connections != NULL );
382 assert( c->c_struct_state == SLAP_C_USED );
383 assert( c->c_conn_state != SLAP_C_INVALID );
385 if( c->c_conn_state != SLAP_C_CLOSING ) {
388 Debug( LDAP_DEBUG_TRACE,
389 "connection_closing: readying conn=%ld sd=%d for close.\n",
390 c->c_connid, c->c_sb.sb_sd, 0 );
392 /* update state to closing */
393 c->c_conn_state = SLAP_C_CLOSING;
395 /* don't listen on this port anymore */
396 slapd_clr_read( c->c_sb.sb_sd, 1 );
398 /* shutdown I/O -- not yet implemented */
400 /* abandon active operations */
401 for( o = c->c_ops; o != NULL; o = o->o_next ) {
402 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
404 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
407 /* remove pending operations */
408 for( o = slap_op_pop( &c->c_pending_ops );
410 o = slap_op_pop( &c->c_pending_ops ) )
415 /* wake write blocked operations */
416 slapd_clr_write( c->c_sb.sb_sd, 1 );
417 ldap_pvt_thread_cond_signal( &c->c_write_cv );
421 static void connection_close( Connection *c )
423 assert( connections != NULL );
425 assert( c->c_struct_state == SLAP_C_USED );
426 assert( c->c_conn_state == SLAP_C_CLOSING );
428 if( c->c_ops != NULL ) {
429 Debug( LDAP_DEBUG_TRACE,
430 "connection_close: deferring conn=%ld sd=%d.\n",
431 c->c_connid, c->c_sb.sb_sd, 0 );
436 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
437 c->c_connid, c->c_sb.sb_sd, 0 );
439 connection_destroy( c );
442 long connections_nextid(void)
445 assert( connections != NULL );
447 ldap_pvt_thread_mutex_lock( &connections_mutex );
451 ldap_pvt_thread_mutex_unlock( &connections_mutex );
456 Connection* connection_first(void)
458 assert( connections != NULL );
460 ldap_pvt_thread_mutex_lock( &connections_mutex );
462 assert( conn_index == -1 );
465 return connection_next(NULL);
468 Connection* connection_next(Connection *c)
470 assert( connections != NULL );
471 assert( conn_index != -1 );
472 assert( conn_index <= dtblsize );
475 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
480 for(; conn_index < dtblsize; conn_index++) {
481 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
482 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
490 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
491 assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
492 c = &connections[conn_index++];
496 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
497 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
501 ldap_pvt_thread_mutex_lock( &c->c_mutex );
507 void connection_done(Connection *c)
509 assert( connections != NULL );
510 assert( conn_index != -1 );
511 assert( conn_index <= dtblsize );
514 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
518 ldap_pvt_thread_mutex_unlock( &connections_mutex );
522 * connection_activity - handle the request operation op on connection
523 * conn. This routine figures out what kind of operation it is and
524 * calls the appropriate stub to handle it.
528 connection_operation( void *arg_v )
530 struct co_arg *arg = arg_v;
531 int tag = arg->co_op->o_tag;
532 Connection *conn = arg->co_conn;
535 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
537 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
542 do_bind( conn, arg->co_op );
546 case LDAP_REQ_UNBIND_30:
548 case LDAP_REQ_UNBIND:
549 do_unbind( conn, arg->co_op );
553 do_add( conn, arg->co_op );
557 case LDAP_REQ_DELETE_30:
559 case LDAP_REQ_DELETE:
560 do_delete( conn, arg->co_op );
563 case LDAP_REQ_MODRDN:
564 do_modrdn( conn, arg->co_op );
567 case LDAP_REQ_MODIFY:
568 do_modify( conn, arg->co_op );
571 case LDAP_REQ_COMPARE:
572 do_compare( conn, arg->co_op );
575 case LDAP_REQ_SEARCH:
576 do_search( conn, arg->co_op );
580 case LDAP_REQ_ABANDON_30:
582 case LDAP_REQ_ABANDON:
583 do_abandon( conn, arg->co_op );
587 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
588 arg->co_op->o_tag, 0, 0 );
593 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
595 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
598 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
601 conn->c_n_ops_completed++;
604 slap_op_remove( &conn->c_ops, arg->co_op );
605 slap_op_free( arg->co_op );
608 free( (char *) arg );
613 case LDAP_REQ_UNBIND_30:
615 case LDAP_REQ_UNBIND:
616 connection_closing( conn );
620 if( conn->c_conn_state == SLAP_C_BINDING) {
621 conn->c_conn_state = SLAP_C_ACTIVE;
625 if( conn->c_conn_state == SLAP_C_CLOSING ) {
626 Debug( LDAP_DEBUG_TRACE,
627 "connection_operation: attempting closing conn=%ld sd=%d.\n",
628 conn->c_connid, conn->c_sb.sb_sd, 0 );
630 connection_close( conn );
633 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
635 if( active_threads < 1 ) {
636 ldap_pvt_thread_cond_signal(&active_threads_cond);
638 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
640 connection_resched( conn );
642 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
647 int connection_read(int s)
651 assert( connections != NULL );
653 ldap_pvt_thread_mutex_lock( &connections_mutex );
655 c = connection_get( s );
657 Debug( LDAP_DEBUG_ANY,
658 "connection_read(%d): no connection!\n",
660 ldap_pvt_thread_mutex_unlock( &connections_mutex );
664 if( c->c_conn_state == SLAP_C_CLOSING ) {
665 Debug( LDAP_DEBUG_TRACE,
666 "connection_read(%d): closing, ignoring input for id=%ld\n",
669 connection_return( c );
670 ldap_pvt_thread_mutex_unlock( &connections_mutex );
674 Debug( LDAP_DEBUG_TRACE,
675 "connection_read(%d): checking for input on id=%ld\n",
678 #define CONNECTION_INPUT_LOOP 1
680 #ifdef DATA_READY_LOOP
681 while(!rc && lber_pvt_sb_data_ready(&c->c_sb))
682 #elif CONNECTION_INPUT_LOOP
686 rc = connection_input( c );
690 Debug( LDAP_DEBUG_TRACE,
691 "connection_read(%d): input error=%d id=%ld, closing.\n",
692 s, rc, c->c_connid );
694 connection_closing( c );
695 connection_close( c );
698 connection_return( c );
699 ldap_pvt_thread_mutex_unlock( &connections_mutex );
709 unsigned long tag, len;
713 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
715 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
720 if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
721 != LDAP_TAG_MESSAGE )
725 Debug( LDAP_DEBUG_TRACE,
726 "ber_get_next on fd %d failed errno %d (%s)\n",
727 lber_pvt_sb_get_desc(&conn->c_sb), err,
728 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
729 Debug( LDAP_DEBUG_TRACE,
730 "\t*** got %ld of %lu so far\n",
731 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
732 conn->c_currentber->ber_len, 0 );
734 if ( err != EWOULDBLOCK && err != EAGAIN ) {
735 /* log, close and send error */
736 ber_free( conn->c_currentber, 1 );
737 conn->c_currentber = NULL;
744 ber = conn->c_currentber;
745 conn->c_currentber = NULL;
747 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
748 /* log, close and send error */
749 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
755 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
756 /* log, close and send error */
757 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
765 if ( conn->c_version == 30 ) {
766 (void) ber_skip_tag( ber, &len );
770 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
772 if ( conn->c_conn_state == SLAP_C_BINDING
773 || conn->c_conn_state == SLAP_C_CLOSING )
775 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
776 slap_op_add( &conn->c_pending_ops, op );
779 connection_op_activate( conn, op );
783 if ( conn->c_struct_state != SLAP_C_USED ) {
784 /* connection must have got closed underneath us */
788 assert( conn->c_struct_state == SLAP_C_USED );
794 connection_resched( Connection *conn )
798 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
799 /* other states need different handling */
803 for( op = slap_op_pop( &conn->c_pending_ops );
805 op = slap_op_pop( &conn->c_pending_ops ) )
807 /* pending operations should not be marked for abandonment */
808 assert(!op->o_abandon);
810 connection_op_activate( conn, op );
812 if ( conn->c_conn_state == SLAP_C_BINDING ) {
819 static int connection_op_activate( Connection *conn, Operation *op )
824 unsigned long tag = op->o_tag;
826 if ( conn->c_dn != NULL ) {
827 tmpdn = ch_strdup( conn->c_dn );
832 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
836 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
837 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
839 slap_op_add( &conn->c_ops, arg->co_op );
841 if(tag == LDAP_REQ_BIND) {
842 conn->c_conn_state = SLAP_C_BINDING;
845 if ( tmpdn != NULL ) {
849 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
851 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
853 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
854 connection_operation, (void *) arg );
857 Debug( LDAP_DEBUG_ANY,
858 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
860 /* should move op to pending list */
866 int connection_write(int s)
869 assert( connections != NULL );
871 ldap_pvt_thread_mutex_lock( &connections_mutex );
873 c = connection_get( s );
875 Debug( LDAP_DEBUG_ANY,
876 "connection_write(%d): no connection!\n",
878 ldap_pvt_thread_mutex_unlock( &connections_mutex );
882 Debug( LDAP_DEBUG_TRACE,
883 "connection_write(%d): waking output for id=%ld\n",
886 ldap_pvt_thread_cond_signal( &c->c_write_cv );
888 connection_return( c );
889 ldap_pvt_thread_mutex_unlock( &connections_mutex );