14 #define EWOULDBLOCK WSAEWOULDBLOCK
17 /* protected by connections_mutex */
18 static ldap_pvt_thread_mutex_t connections_mutex;
19 static Connection *connections = NULL;
20 static int conn_index = -1;
21 static long conn_nextid = 0;
23 /* structure state (protected by connections_mutex) */
24 #define SLAP_C_UNINITIALIZED 0x0 /* MUST BE ZERO (0) */
25 #define SLAP_C_UNUSED 0x1
26 #define SLAP_C_USED 0x2
28 /* connection state (protected by c_mutex ) */
29 #define SLAP_C_INVALID 0x0 /* MUST BE ZERO (0) */
30 #define SLAP_C_INACTIVE 0x1 /* zero threads */
31 #define SLAP_C_ACTIVE 0x2 /* one or more threads */
32 #define SLAP_C_BINDING 0x3 /* binding */
33 #define SLAP_C_CLOSING 0x4 /* closing */
35 void slapd_remove(int s);
36 static Connection* connection_get( int s );
38 static int connection_input( Connection *c );
39 static void connection_close( Connection *c );
41 static int connection_op_activate( Connection *conn, Operation *op );
42 static int connection_resched( Connection *conn );
50 * Initialize connection management infrastructure.
52 int connections_init(void)
56 assert( connections == NULL );
58 if( connections != NULL) {
59 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
64 /* should check return of every call */
65 ldap_pvt_thread_mutex_init( &connections_mutex );
67 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
69 if( connections == NULL ) {
70 Debug( LDAP_DEBUG_ANY,
71 "connections_init: allocation (%d*%ld) of connection array failed.\n",
72 dtblsize, (long) sizeof(Connection), 0 );
76 for ( i = 0; i < dtblsize; i++ )
77 memset( &connections[i], 0, sizeof(Connection) );
80 * per entry initialization of the Connection array initialization
81 * will be done by connection_init()
88 * Destroy connection management infrastructure.
90 int connections_destroy(void)
94 /* should check return of every call */
96 if( connections == NULL) {
97 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
102 for ( i = 0; i < dtblsize; i++ ) {
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 );
107 free( &connections[i] );
113 ldap_pvt_thread_mutex_destroy( &connections_mutex );
118 * shutdown all connections
120 int connections_shutdown(void)
124 ldap_pvt_thread_mutex_lock( &connections_mutex );
126 for ( i = 0; i < dtblsize; i++ ) {
127 if( connections[i].c_struct_state != SLAP_C_USED ) {
131 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
132 connection_closing( &connections[i] );
133 connection_close( &connections[i] );
134 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
137 ldap_pvt_thread_mutex_unlock( &connections_mutex );
140 static Connection* connection_get( int s )
142 Connection *c = NULL;
144 assert( connections != NULL );
151 assert( connections[s].c_struct_state == SLAP_C_USED );
152 assert( connections[s].c_conn_state != SLAP_C_INVALID );
153 assert( connections[s].c_sb.sb_sd != -1 );
160 for(i=0; i<dtblsize; i++) {
161 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
162 assert( connections[i].c_conn_state == SLAP_C_INVALID );
163 assert( connections[i].c_sb.sb_sd == 0 );
167 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
168 assert( connections[i].c_conn_state == SLAP_C_INVALID );
169 assert( connections[i].c_sb.sb_sd == -1 );
173 assert( connections[i].c_struct_state == SLAP_C_USED );
174 assert( connections[i].c_conn_state != SLAP_C_INVALID );
175 assert( connections[i].c_sb.sb_sd != -1 );
177 if( connections[i].c_sb.sb_sd == s ) {
186 /* we do this BEFORE locking to aid in debugging */
187 Debug( LDAP_DEBUG_TRACE,
188 "connection_get(%d): got connid=%ld\n",
191 ldap_pvt_thread_mutex_lock( &c->c_mutex );
196 static void connection_return( Connection *c )
198 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
201 long connection_init(
208 assert( connections != NULL );
216 assert( s < dtblsize );
219 ldap_pvt_thread_mutex_lock( &connections_mutex );
228 for( i=0; i < dtblsize; i++) {
229 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
230 assert( connections[i].c_sb.sb_sd == 0 );
235 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
236 assert( connections[i].c_sb.sb_sd == -1 );
241 assert( connections[i].c_struct_state == SLAP_C_USED );
242 assert( connections[i].c_conn_state != SLAP_C_INVALID );
243 assert( connections[i].c_sb.sb_sd != -1 );
247 ldap_pvt_thread_mutex_unlock( &connections_mutex );
254 assert( c->c_struct_state != SLAP_C_USED );
255 assert( c->c_conn_state == SLAP_C_INVALID );
257 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
260 c->c_client_name = NULL;
261 c->c_client_addr = NULL;
263 c->c_pending_ops = NULL;
265 lber_pvt_sb_init( &c->c_sb );
267 /* should check status of thread calls */
268 ldap_pvt_thread_mutex_init( &c->c_mutex );
269 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
270 ldap_pvt_thread_cond_init( &c->c_write_cv );
272 c->c_struct_state = SLAP_C_UNUSED;
275 ldap_pvt_thread_mutex_lock( &c->c_mutex );
277 assert( c->c_struct_state == SLAP_C_UNUSED );
278 assert( c->c_dn == NULL );
279 assert( c->c_cdn == NULL );
280 assert( c->c_client_name == NULL );
281 assert( c->c_client_addr == NULL );
282 assert( c->c_ops == NULL );
283 assert( c->c_pending_ops == NULL );
285 c->c_client_name = ch_strdup( name == NULL ? "" : name );
286 c->c_client_addr = ch_strdup( addr );
288 c->c_n_ops_received = 0;
290 c->c_n_ops_executing = 0;
291 c->c_n_ops_pending = 0;
292 c->c_n_ops_completed = 0;
295 c->c_starttime = slap_get_time();
297 lber_pvt_sb_set_desc( &c->c_sb, s );
298 lber_pvt_sb_set_io( &c->c_sb, &lber_pvt_sb_io_tcp, NULL );
300 if( lber_pvt_sb_set_nonblock( &c->c_sb, 1 ) < 0 ) {
301 Debug( LDAP_DEBUG_ANY,
302 "connection_init(%d, %s, %s): set nonblocking failed\n",
303 s, c->c_client_name, c->c_client_addr);
306 id = c->c_connid = conn_nextid++;
308 c->c_conn_state = SLAP_C_INACTIVE;
309 c->c_struct_state = SLAP_C_USED;
311 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
312 ldap_pvt_thread_mutex_unlock( &connections_mutex );
318 connection_destroy( Connection *c )
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;
334 if(c->c_dn != NULL) {
338 if(c->c_cdn != NULL) {
342 if(c->c_client_name != NULL) {
343 free(c->c_client_name);
344 c->c_client_name = NULL;
346 if(c->c_client_addr != NULL) {
347 free(c->c_client_addr);
348 c->c_client_addr = NULL;
351 if ( lber_pvt_sb_in_use(&c->c_sb) ) {
352 int sd = lber_pvt_sb_get_desc(&c->c_sb);
355 lber_pvt_sb_close( &c->c_sb );
357 Statslog( LDAP_DEBUG_STATS,
358 "conn=%d fd=%d closed.\n",
359 c->c_connid, sd, 0, 0, 0 );
362 lber_pvt_sb_destroy( &c->c_sb );
365 int connection_state_closing( Connection *c )
367 /* connection must be locked by caller */
370 assert( c->c_struct_state == SLAP_C_USED );
372 state = c->c_conn_state;
374 assert( state != SLAP_C_INVALID );
376 return state == SLAP_C_CLOSING;
379 void connection_closing( Connection *c )
381 assert( connections != NULL );
383 assert( c->c_struct_state == SLAP_C_USED );
384 assert( c->c_conn_state != SLAP_C_INVALID );
386 if( c->c_conn_state != SLAP_C_CLOSING ) {
389 Debug( LDAP_DEBUG_TRACE,
390 "connection_closing: readying conn=%ld sd=%d for close.\n",
391 c->c_connid, c->c_sb.sb_sd, 0 );
393 /* update state to closing */
394 c->c_conn_state = SLAP_C_CLOSING;
396 /* don't listen on this port anymore */
397 slapd_clr_read( c->c_sb.sb_sd, 1 );
399 /* shutdown I/O -- not yet implemented */
401 /* abandon active operations */
402 for( o = c->c_ops; o != NULL; o = o->o_next ) {
403 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
405 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
408 /* remove pending operations */
409 for( o = slap_op_pop( &c->c_pending_ops );
411 o = slap_op_pop( &c->c_pending_ops ) )
416 /* wake write blocked operations */
417 slapd_clr_write( c->c_sb.sb_sd, 1 );
418 ldap_pvt_thread_cond_signal( &c->c_write_cv );
422 static void connection_close( Connection *c )
424 assert( connections != NULL );
426 assert( c->c_struct_state == SLAP_C_USED );
427 assert( c->c_conn_state == SLAP_C_CLOSING );
429 if( c->c_ops != NULL ) {
430 Debug( LDAP_DEBUG_TRACE,
431 "connection_close: deferring conn=%ld sd=%d.\n",
432 c->c_connid, c->c_sb.sb_sd, 0 );
437 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
438 c->c_connid, c->c_sb.sb_sd, 0 );
440 connection_destroy( c );
443 long connections_nextid(void)
446 assert( connections != NULL );
448 ldap_pvt_thread_mutex_lock( &connections_mutex );
452 ldap_pvt_thread_mutex_unlock( &connections_mutex );
457 Connection* connection_first(void)
459 assert( connections != NULL );
461 ldap_pvt_thread_mutex_lock( &connections_mutex );
463 assert( conn_index == -1 );
466 return connection_next(NULL);
469 Connection* connection_next(Connection *c)
471 assert( connections != NULL );
472 assert( conn_index != -1 );
473 assert( conn_index <= dtblsize );
476 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
481 for(; conn_index < dtblsize; conn_index++) {
482 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
483 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
491 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
492 assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
493 c = &connections[conn_index++];
497 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
498 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
502 ldap_pvt_thread_mutex_lock( &c->c_mutex );
508 void connection_done(Connection *c)
510 assert( connections != NULL );
511 assert( conn_index != -1 );
512 assert( conn_index <= dtblsize );
515 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
519 ldap_pvt_thread_mutex_unlock( &connections_mutex );
523 * connection_activity - handle the request operation op on connection
524 * conn. This routine figures out what kind of operation it is and
525 * calls the appropriate stub to handle it.
529 connection_operation( void *arg_v )
531 struct co_arg *arg = arg_v;
532 int tag = arg->co_op->o_tag;
533 Connection *conn = arg->co_conn;
536 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
538 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
543 do_bind( conn, arg->co_op );
547 case LDAP_REQ_UNBIND_30:
549 case LDAP_REQ_UNBIND:
550 do_unbind( conn, arg->co_op );
554 do_add( conn, arg->co_op );
558 case LDAP_REQ_DELETE_30:
560 case LDAP_REQ_DELETE:
561 do_delete( conn, arg->co_op );
564 case LDAP_REQ_MODRDN:
565 do_modrdn( conn, arg->co_op );
568 case LDAP_REQ_MODIFY:
569 do_modify( conn, arg->co_op );
572 case LDAP_REQ_COMPARE:
573 do_compare( conn, arg->co_op );
576 case LDAP_REQ_SEARCH:
577 do_search( conn, arg->co_op );
581 case LDAP_REQ_ABANDON_30:
583 case LDAP_REQ_ABANDON:
584 do_abandon( conn, arg->co_op );
588 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
589 arg->co_op->o_tag, 0, 0 );
594 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
596 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
599 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
602 conn->c_n_ops_completed++;
605 slap_op_remove( &conn->c_ops, arg->co_op );
606 slap_op_free( arg->co_op );
609 free( (char *) arg );
614 case LDAP_REQ_UNBIND_30:
616 case LDAP_REQ_UNBIND:
617 connection_closing( conn );
621 if( conn->c_conn_state == SLAP_C_BINDING) {
622 conn->c_conn_state = SLAP_C_ACTIVE;
626 if( conn->c_conn_state == SLAP_C_CLOSING ) {
627 Debug( LDAP_DEBUG_TRACE,
628 "connection_operation: attempting closing conn=%ld sd=%d.\n",
629 conn->c_connid, conn->c_sb.sb_sd, 0 );
631 connection_close( conn );
634 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
636 if( active_threads < 1 ) {
637 ldap_pvt_thread_cond_signal(&active_threads_cond);
639 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
641 connection_resched( conn );
643 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
648 int connection_read(int s)
652 assert( connections != NULL );
654 ldap_pvt_thread_mutex_lock( &connections_mutex );
656 c = connection_get( s );
658 Debug( LDAP_DEBUG_ANY,
659 "connection_read(%d): no connection!\n",
661 ldap_pvt_thread_mutex_unlock( &connections_mutex );
665 if( c->c_conn_state == SLAP_C_CLOSING ) {
666 Debug( LDAP_DEBUG_TRACE,
667 "connection_read(%d): closing, ignoring input for id=%ld\n",
670 connection_return( c );
671 ldap_pvt_thread_mutex_unlock( &connections_mutex );
675 Debug( LDAP_DEBUG_TRACE,
676 "connection_read(%d): checking for input on id=%ld\n",
679 #define CONNECTION_INPUT_LOOP 1
681 #ifdef DATA_READY_LOOP
682 while(!rc && lber_pvt_sb_data_ready(&c->c_sb))
683 #elif CONNECTION_INPUT_LOOP
687 rc = connection_input( c );
691 Debug( LDAP_DEBUG_TRACE,
692 "connection_read(%d): input error=%d id=%ld, closing.\n",
693 s, rc, c->c_connid );
695 connection_closing( c );
696 connection_close( c );
699 connection_return( c );
700 ldap_pvt_thread_mutex_unlock( &connections_mutex );
710 unsigned long tag, len;
714 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
716 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
721 if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
722 != LDAP_TAG_MESSAGE )
726 Debug( LDAP_DEBUG_TRACE,
727 "ber_get_next on fd %d failed errno %d (%s)\n",
728 lber_pvt_sb_get_desc(&conn->c_sb), err,
729 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
730 Debug( LDAP_DEBUG_TRACE,
731 "\t*** got %ld of %lu so far\n",
732 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
733 conn->c_currentber->ber_len, 0 );
735 if ( err != EWOULDBLOCK && err != EAGAIN ) {
736 /* log, close and send error */
737 ber_free( conn->c_currentber, 1 );
738 conn->c_currentber = NULL;
745 ber = conn->c_currentber;
746 conn->c_currentber = NULL;
748 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
749 /* log, close and send error */
750 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
756 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
757 /* log, close and send error */
758 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
766 if ( conn->c_version == 30 ) {
767 (void) ber_skip_tag( ber, &len );
771 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
773 if ( conn->c_conn_state == SLAP_C_BINDING
774 || conn->c_conn_state == SLAP_C_CLOSING )
776 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
777 slap_op_add( &conn->c_pending_ops, op );
780 connection_op_activate( conn, op );
784 if ( conn->c_struct_state != SLAP_C_USED ) {
785 /* connection must have got closed underneath us */
789 assert( conn->c_struct_state == SLAP_C_USED );
795 connection_resched( Connection *conn )
799 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
800 /* other states need different handling */
804 for( op = slap_op_pop( &conn->c_pending_ops );
806 op = slap_op_pop( &conn->c_pending_ops ) )
808 /* pending operations should not be marked for abandonment */
809 assert(!op->o_abandon);
811 connection_op_activate( conn, op );
813 if ( conn->c_conn_state == SLAP_C_BINDING ) {
820 static int connection_op_activate( Connection *conn, Operation *op )
825 unsigned long tag = op->o_tag;
827 if ( conn->c_dn != NULL ) {
828 tmpdn = ch_strdup( conn->c_dn );
833 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
837 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
838 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
840 slap_op_add( &conn->c_ops, arg->co_op );
842 if(tag == LDAP_REQ_BIND) {
843 conn->c_conn_state = SLAP_C_BINDING;
846 if ( tmpdn != NULL ) {
850 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
852 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
854 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
855 connection_operation, (void *) arg );
858 Debug( LDAP_DEBUG_ANY,
859 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
861 /* should move op to pending list */
867 int connection_write(int s)
870 assert( connections != NULL );
872 ldap_pvt_thread_mutex_lock( &connections_mutex );
874 c = connection_get( s );
876 Debug( LDAP_DEBUG_ANY,
877 "connection_write(%d): no connection!\n",
879 ldap_pvt_thread_mutex_unlock( &connections_mutex );
883 Debug( LDAP_DEBUG_TRACE,
884 "connection_write(%d): waking output for id=%ld\n",
887 ldap_pvt_thread_cond_signal( &c->c_write_cv );
889 connection_return( c );
890 ldap_pvt_thread_mutex_unlock( &connections_mutex );