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 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
104 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
105 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
106 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
109 free( &connections[i] );
115 ldap_pvt_thread_mutex_destroy( &connections_mutex );
120 * shutdown all connections
122 int connections_shutdown(void)
126 ldap_pvt_thread_mutex_lock( &connections_mutex );
128 for ( i = 0; i < dtblsize; i++ ) {
129 if( connections[i].c_struct_state != SLAP_C_USED ) {
133 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
134 connection_closing( &connections[i] );
135 connection_close( &connections[i] );
136 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
139 ldap_pvt_thread_mutex_unlock( &connections_mutex );
142 static Connection* connection_get( int s )
144 Connection *c = NULL;
146 assert( connections != NULL );
153 assert( connections[s].c_struct_state == SLAP_C_USED );
154 assert( connections[s].c_conn_state != SLAP_C_INVALID );
155 assert( connections[s].c_sb.sb_sd != -1 );
162 for(i=0; i<dtblsize; i++) {
163 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
164 assert( connections[i].c_conn_state == SLAP_C_INVALID );
165 assert( connections[i].c_sb.sb_sd == 0 );
169 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
170 assert( connections[i].c_conn_state == SLAP_C_INVALID );
171 assert( connections[i].c_sb.sb_sd == -1 );
175 assert( connections[i].c_struct_state == SLAP_C_USED );
176 assert( connections[i].c_conn_state != SLAP_C_INVALID );
177 assert( connections[i].c_sb.sb_sd != -1 );
179 if( connections[i].c_sb.sb_sd == s ) {
188 /* we do this BEFORE locking to aid in debugging */
189 Debug( LDAP_DEBUG_TRACE,
190 "connection_get(%d): got connid=%ld\n",
193 ldap_pvt_thread_mutex_lock( &c->c_mutex );
198 static void connection_return( Connection *c )
200 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
203 long connection_init(
210 assert( connections != NULL );
218 assert( s < dtblsize );
221 ldap_pvt_thread_mutex_lock( &connections_mutex );
230 for( i=0; i < dtblsize; i++) {
231 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
232 assert( connections[i].c_sb.sb_sd == 0 );
237 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
238 assert( connections[i].c_sb.sb_sd == -1 );
243 assert( connections[i].c_struct_state == SLAP_C_USED );
244 assert( connections[i].c_conn_state != SLAP_C_INVALID );
245 assert( connections[i].c_sb.sb_sd != -1 );
249 ldap_pvt_thread_mutex_unlock( &connections_mutex );
256 assert( c->c_struct_state != SLAP_C_USED );
257 assert( c->c_conn_state == SLAP_C_INVALID );
259 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
262 c->c_client_name = NULL;
263 c->c_client_addr = NULL;
265 c->c_pending_ops = NULL;
267 lber_pvt_sb_init( &c->c_sb );
269 /* should check status of thread calls */
270 ldap_pvt_thread_mutex_init( &c->c_mutex );
271 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
272 ldap_pvt_thread_cond_init( &c->c_write_cv );
274 c->c_struct_state = SLAP_C_UNUSED;
277 ldap_pvt_thread_mutex_lock( &c->c_mutex );
279 assert( c->c_struct_state == SLAP_C_UNUSED );
280 assert( c->c_dn == NULL );
281 assert( c->c_cdn == NULL );
282 assert( c->c_client_name == NULL );
283 assert( c->c_client_addr == NULL );
284 assert( c->c_ops == NULL );
285 assert( c->c_pending_ops == NULL );
287 c->c_client_name = ch_strdup( name == NULL ? "" : name );
288 c->c_client_addr = ch_strdup( addr );
290 c->c_n_ops_received = 0;
292 c->c_n_ops_executing = 0;
293 c->c_n_ops_pending = 0;
294 c->c_n_ops_completed = 0;
297 c->c_starttime = slap_get_time();
299 lber_pvt_sb_set_desc( &c->c_sb, s );
300 lber_pvt_sb_set_io( &c->c_sb, &lber_pvt_sb_io_tcp, NULL );
302 if( lber_pvt_sb_set_nonblock( &c->c_sb, 1 ) < 0 ) {
303 Debug( LDAP_DEBUG_ANY,
304 "connection_init(%d, %s, %s): set nonblocking failed\n",
305 s, c->c_client_name, c->c_client_addr);
308 id = c->c_connid = conn_nextid++;
310 c->c_conn_state = SLAP_C_INACTIVE;
311 c->c_struct_state = SLAP_C_USED;
313 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
314 ldap_pvt_thread_mutex_unlock( &connections_mutex );
320 connection_destroy( Connection *c )
322 assert( connections != NULL );
324 assert( c->c_struct_state != SLAP_C_UNUSED );
325 assert( c->c_conn_state != SLAP_C_INVALID );
326 assert( c->c_ops == NULL );
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 ( lber_pvt_sb_in_use(&c->c_sb) ) {
354 int sd = lber_pvt_sb_get_desc(&c->c_sb);
357 lber_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 lber_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, c->c_sb.sb_sd, 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( c->c_sb.sb_sd, 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( c->c_sb.sb_sd, 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 if( c->c_ops != NULL ) {
432 Debug( LDAP_DEBUG_TRACE,
433 "connection_close: deferring conn=%ld sd=%d.\n",
434 c->c_connid, c->c_sb.sb_sd, 0 );
439 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
440 c->c_connid, c->c_sb.sb_sd, 0 );
442 connection_destroy( c );
445 long connections_nextid(void)
448 assert( connections != NULL );
450 ldap_pvt_thread_mutex_lock( &connections_mutex );
454 ldap_pvt_thread_mutex_unlock( &connections_mutex );
459 Connection* connection_first(void)
461 assert( connections != NULL );
463 ldap_pvt_thread_mutex_lock( &connections_mutex );
465 assert( conn_index == -1 );
468 return connection_next(NULL);
471 Connection* connection_next(Connection *c)
473 assert( connections != NULL );
474 assert( conn_index != -1 );
475 assert( conn_index <= dtblsize );
478 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
483 for(; conn_index < dtblsize; conn_index++) {
484 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
485 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
493 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
494 assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
495 c = &connections[conn_index++];
499 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
500 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
504 ldap_pvt_thread_mutex_lock( &c->c_mutex );
510 void connection_done(Connection *c)
512 assert( connections != NULL );
513 assert( conn_index != -1 );
514 assert( conn_index <= dtblsize );
517 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
521 ldap_pvt_thread_mutex_unlock( &connections_mutex );
525 * connection_activity - handle the request operation op on connection
526 * conn. This routine figures out what kind of operation it is and
527 * calls the appropriate stub to handle it.
531 connection_operation( void *arg_v )
533 struct co_arg *arg = arg_v;
534 int tag = arg->co_op->o_tag;
535 Connection *conn = arg->co_conn;
538 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
540 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
545 do_bind( conn, arg->co_op );
549 case LDAP_REQ_UNBIND_30:
551 case LDAP_REQ_UNBIND:
552 do_unbind( conn, arg->co_op );
556 do_add( conn, arg->co_op );
560 case LDAP_REQ_DELETE_30:
562 case LDAP_REQ_DELETE:
563 do_delete( conn, arg->co_op );
566 case LDAP_REQ_MODRDN:
567 do_modrdn( conn, arg->co_op );
570 case LDAP_REQ_MODIFY:
571 do_modify( conn, arg->co_op );
574 case LDAP_REQ_COMPARE:
575 do_compare( conn, arg->co_op );
578 case LDAP_REQ_SEARCH:
579 do_search( conn, arg->co_op );
583 case LDAP_REQ_ABANDON_30:
585 case LDAP_REQ_ABANDON:
586 do_abandon( conn, arg->co_op );
590 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
591 arg->co_op->o_tag, 0, 0 );
596 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
598 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
601 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
604 conn->c_n_ops_completed++;
607 slap_op_remove( &conn->c_ops, arg->co_op );
608 slap_op_free( arg->co_op );
611 free( (char *) arg );
616 case LDAP_REQ_UNBIND_30:
618 case LDAP_REQ_UNBIND:
619 connection_closing( conn );
623 if( conn->c_conn_state == SLAP_C_BINDING) {
624 conn->c_conn_state = SLAP_C_ACTIVE;
628 if( conn->c_conn_state == SLAP_C_CLOSING ) {
629 Debug( LDAP_DEBUG_TRACE,
630 "connection_operation: attempting closing conn=%ld sd=%d.\n",
631 conn->c_connid, conn->c_sb.sb_sd, 0 );
633 connection_close( conn );
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 c = connection_get( s );
660 Debug( LDAP_DEBUG_ANY,
661 "connection_read(%d): no connection!\n",
663 ldap_pvt_thread_mutex_unlock( &connections_mutex );
667 if( c->c_conn_state == SLAP_C_CLOSING ) {
668 Debug( LDAP_DEBUG_TRACE,
669 "connection_read(%d): closing, ignoring input for id=%ld\n",
672 connection_return( c );
673 ldap_pvt_thread_mutex_unlock( &connections_mutex );
677 Debug( LDAP_DEBUG_TRACE,
678 "connection_read(%d): checking for input on id=%ld\n",
681 #define CONNECTION_INPUT_LOOP 1
683 #ifdef DATA_READY_LOOP
684 while(!rc && lber_pvt_sb_data_ready(&c->c_sb))
685 #elif CONNECTION_INPUT_LOOP
689 rc = connection_input( c );
693 Debug( LDAP_DEBUG_TRACE,
694 "connection_read(%d): input error=%d id=%ld, closing.\n",
695 s, rc, c->c_connid );
697 connection_closing( c );
698 connection_close( c );
701 connection_return( c );
702 ldap_pvt_thread_mutex_unlock( &connections_mutex );
712 unsigned long tag, len;
716 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
718 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
723 if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
724 != LDAP_TAG_MESSAGE )
728 Debug( LDAP_DEBUG_TRACE,
729 "ber_get_next on fd %d failed errno %d (%s)\n",
730 lber_pvt_sb_get_desc(&conn->c_sb), err,
731 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
732 Debug( LDAP_DEBUG_TRACE,
733 "\t*** got %ld of %lu so far\n",
734 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
735 conn->c_currentber->ber_len, 0 );
737 if ( err != EWOULDBLOCK && err != EAGAIN ) {
738 /* log, close and send error */
739 ber_free( conn->c_currentber, 1 );
740 conn->c_currentber = NULL;
747 ber = conn->c_currentber;
748 conn->c_currentber = NULL;
750 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
751 /* log, close and send error */
752 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
758 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
759 /* log, close and send error */
760 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
768 if ( conn->c_version == 30 ) {
769 (void) ber_skip_tag( ber, &len );
773 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
775 if ( conn->c_conn_state == SLAP_C_BINDING
776 || conn->c_conn_state == SLAP_C_CLOSING )
778 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
779 slap_op_add( &conn->c_pending_ops, op );
782 connection_op_activate( conn, op );
786 if ( conn->c_struct_state != SLAP_C_USED ) {
787 /* connection must have got closed underneath us */
791 assert( conn->c_struct_state == SLAP_C_USED );
797 connection_resched( Connection *conn )
801 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
802 /* other states need different handling */
806 for( op = slap_op_pop( &conn->c_pending_ops );
808 op = slap_op_pop( &conn->c_pending_ops ) )
810 /* pending operations should not be marked for abandonment */
811 assert(!op->o_abandon);
813 connection_op_activate( conn, op );
815 if ( conn->c_conn_state == SLAP_C_BINDING ) {
822 static int connection_op_activate( Connection *conn, Operation *op )
827 unsigned long tag = op->o_tag;
829 if ( conn->c_dn != NULL ) {
830 tmpdn = ch_strdup( conn->c_dn );
835 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
839 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
840 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
842 slap_op_add( &conn->c_ops, arg->co_op );
844 if(tag == LDAP_REQ_BIND) {
845 conn->c_conn_state = SLAP_C_BINDING;
848 if ( tmpdn != NULL ) {
852 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
854 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
856 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
857 connection_operation, (void *) arg );
860 Debug( LDAP_DEBUG_ANY,
861 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
863 /* should move op to pending list */
869 int connection_write(int s)
872 assert( connections != NULL );
874 ldap_pvt_thread_mutex_lock( &connections_mutex );
876 c = connection_get( s );
878 Debug( LDAP_DEBUG_ANY,
879 "connection_write(%d): no connection!\n",
881 ldap_pvt_thread_mutex_unlock( &connections_mutex );
885 Debug( LDAP_DEBUG_TRACE,
886 "connection_write(%d): waking output for id=%ld\n",
889 ldap_pvt_thread_cond_signal( &c->c_write_cv );
891 connection_return( c );
892 ldap_pvt_thread_mutex_unlock( &connections_mutex );