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 );
138 static Connection* connection_get( int s )
140 Connection *c = NULL;
142 assert( connections != NULL );
149 assert( connections[s].c_struct_state == SLAP_C_USED );
150 assert( connections[s].c_conn_state != SLAP_C_INVALID );
151 assert( connections[s].c_sb.sb_sd != -1 );
158 for(i=0; i<dtblsize; i++) {
159 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
160 assert( connections[i].c_conn_state == SLAP_C_INVALID );
161 assert( connections[i].c_sb.sb_sd == 0 );
165 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
166 assert( connections[i].c_conn_state == SLAP_C_INVALID );
167 assert( connections[i].c_sb.sb_sd == -1 );
171 assert( connections[i].c_struct_state == SLAP_C_USED );
172 assert( connections[i].c_conn_state != SLAP_C_INVALID );
173 assert( connections[i].c_sb.sb_sd != -1 );
175 if( connections[i].c_sb.sb_sd == s ) {
184 /* we do this BEFORE locking to aid in debugging */
185 Debug( LDAP_DEBUG_TRACE,
186 "connection_get(%d): got connid=%ld\n",
189 ldap_pvt_thread_mutex_lock( &c->c_mutex );
194 static void connection_return( Connection *c )
196 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
199 long connection_init(
206 assert( connections != NULL );
214 assert( s < dtblsize );
217 ldap_pvt_thread_mutex_lock( &connections_mutex );
226 for( i=0; i < dtblsize; i++) {
227 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
228 assert( connections[i].c_sb.sb_sd == 0 );
233 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
234 assert( connections[i].c_sb.sb_sd == -1 );
239 assert( connections[i].c_struct_state == SLAP_C_USED );
240 assert( connections[i].c_conn_state != SLAP_C_INVALID );
241 assert( connections[i].c_sb.sb_sd != -1 );
245 ldap_pvt_thread_mutex_unlock( &connections_mutex );
252 assert( c->c_struct_state != SLAP_C_USED );
253 assert( c->c_conn_state == SLAP_C_INVALID );
255 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
258 c->c_client_name = NULL;
259 c->c_client_addr = NULL;
261 c->c_pending_ops = NULL;
263 lber_pvt_sb_init( &c->c_sb );
265 /* should check status of thread calls */
266 ldap_pvt_thread_mutex_init( &c->c_mutex );
267 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
268 ldap_pvt_thread_cond_init( &c->c_write_cv );
270 c->c_struct_state = SLAP_C_UNUSED;
273 ldap_pvt_thread_mutex_lock( &c->c_mutex );
275 assert( c->c_struct_state == SLAP_C_UNUSED );
276 assert( c->c_dn == NULL );
277 assert( c->c_cdn == NULL );
278 assert( c->c_client_name == NULL );
279 assert( c->c_client_addr == NULL );
280 assert( c->c_ops == NULL );
281 assert( c->c_pending_ops == NULL );
283 c->c_client_name = ch_strdup( name == NULL ? "" : name );
284 c->c_client_addr = ch_strdup( addr );
286 c->c_n_ops_received = 0;
288 c->c_n_ops_executing = 0;
289 c->c_n_ops_pending = 0;
290 c->c_n_ops_completed = 0;
293 c->c_starttime = slap_get_time();
295 lber_pvt_sb_set_desc( &c->c_sb, s );
296 lber_pvt_sb_set_io( &c->c_sb, &lber_pvt_sb_io_tcp, NULL );
298 if( lber_pvt_sb_set_nonblock( &c->c_sb, 1 ) < 0 ) {
299 Debug( LDAP_DEBUG_ANY,
300 "connection_init(%d, %s, %s): set nonblocking failed\n",
301 s, c->c_client_name, c->c_client_addr);
304 id = c->c_connid = conn_nextid++;
306 c->c_conn_state = SLAP_C_INACTIVE;
307 c->c_struct_state = SLAP_C_USED;
309 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
310 ldap_pvt_thread_mutex_unlock( &connections_mutex );
316 connection_destroy( Connection *c )
318 assert( connections != NULL );
320 assert( c->c_struct_state != SLAP_C_UNUSED );
321 assert( c->c_conn_state != SLAP_C_INVALID );
322 assert( c->c_ops == NULL );
324 c->c_struct_state = SLAP_C_UNUSED;
325 c->c_conn_state = SLAP_C_INVALID;
332 if(c->c_dn != NULL) {
336 if(c->c_cdn != NULL) {
340 if(c->c_client_name != NULL) {
341 free(c->c_client_name);
342 c->c_client_name = NULL;
344 if(c->c_client_addr != NULL) {
345 free(c->c_client_addr);
346 c->c_client_addr = NULL;
349 if ( lber_pvt_sb_in_use(&c->c_sb) ) {
350 int sd = lber_pvt_sb_get_desc(&c->c_sb);
353 lber_pvt_sb_close( &c->c_sb );
355 Statslog( LDAP_DEBUG_STATS,
356 "conn=%d fd=%d closed.\n",
357 c->c_connid, sd, 0, 0, 0 );
360 lber_pvt_sb_destroy( &c->c_sb );
363 int connection_state_closing( Connection *c )
365 /* connection must be locked by caller */
368 assert( c->c_struct_state == SLAP_C_USED );
370 state = c->c_conn_state;
372 assert( state != SLAP_C_INVALID );
374 return state == SLAP_C_CLOSING;
377 void connection_closing( Connection *c )
379 assert( connections != NULL );
381 assert( c->c_struct_state == SLAP_C_USED );
382 assert( c->c_conn_state != SLAP_C_INVALID );
384 if( c->c_conn_state != SLAP_C_CLOSING ) {
387 Debug( LDAP_DEBUG_TRACE,
388 "connection_closing: readying conn=%ld sd=%d for close.\n",
389 c->c_connid, c->c_sb.sb_sd, 0 );
391 /* update state to closing */
392 c->c_conn_state = SLAP_C_CLOSING;
394 /* don't listen on this port anymore */
395 slapd_clr_read( c->c_sb.sb_sd, 1 );
397 /* shutdown I/O -- not yet implemented */
399 /* abandon active operations */
400 for( o = c->c_ops; o != NULL; o = o->o_next ) {
401 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
403 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
406 /* remove pending operations */
407 for( o = slap_op_pop( &c->c_pending_ops );
409 o = slap_op_pop( &c->c_pending_ops ) )
414 /* wake write blocked operations */
415 slapd_clr_write( c->c_sb.sb_sd, 1 );
416 ldap_pvt_thread_cond_signal( &c->c_write_cv );
420 static void connection_close( Connection *c )
422 assert( connections != NULL );
424 assert( c->c_struct_state == SLAP_C_USED );
425 assert( c->c_conn_state == SLAP_C_CLOSING );
427 if( c->c_ops != NULL ) {
428 Debug( LDAP_DEBUG_TRACE,
429 "connection_close: deferring conn=%ld sd=%d.\n",
430 c->c_connid, c->c_sb.sb_sd, 0 );
435 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
436 c->c_connid, c->c_sb.sb_sd, 0 );
438 connection_destroy( c );
441 long connections_nextid(void)
444 assert( connections != NULL );
446 ldap_pvt_thread_mutex_lock( &connections_mutex );
450 ldap_pvt_thread_mutex_unlock( &connections_mutex );
455 Connection* connection_first(void)
457 assert( connections != NULL );
459 ldap_pvt_thread_mutex_lock( &connections_mutex );
461 assert( conn_index == -1 );
464 return connection_next(NULL);
467 Connection* connection_next(Connection *c)
469 assert( connections != NULL );
470 assert( conn_index != -1 );
471 assert( conn_index <= dtblsize );
474 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
479 for(; conn_index < dtblsize; conn_index++) {
480 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
481 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
489 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
490 assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
491 c = &connections[conn_index++];
495 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
496 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
500 ldap_pvt_thread_mutex_lock( &c->c_mutex );
506 void connection_done(Connection *c)
508 assert( connections != NULL );
509 assert( conn_index != -1 );
510 assert( conn_index <= dtblsize );
513 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
517 ldap_pvt_thread_mutex_unlock( &connections_mutex );
521 * connection_activity - handle the request operation op on connection
522 * conn. This routine figures out what kind of operation it is and
523 * calls the appropriate stub to handle it.
527 connection_operation( void *arg_v )
529 struct co_arg *arg = arg_v;
530 int tag = arg->co_op->o_tag;
531 Connection *conn = arg->co_conn;
534 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
536 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
541 do_bind( conn, arg->co_op );
545 case LDAP_REQ_UNBIND_30:
547 case LDAP_REQ_UNBIND:
548 do_unbind( conn, arg->co_op );
552 do_add( conn, arg->co_op );
556 case LDAP_REQ_DELETE_30:
558 case LDAP_REQ_DELETE:
559 do_delete( conn, arg->co_op );
562 case LDAP_REQ_MODRDN:
563 do_modrdn( conn, arg->co_op );
566 case LDAP_REQ_MODIFY:
567 do_modify( conn, arg->co_op );
570 case LDAP_REQ_COMPARE:
571 do_compare( conn, arg->co_op );
574 case LDAP_REQ_SEARCH:
575 do_search( conn, arg->co_op );
579 case LDAP_REQ_ABANDON_30:
581 case LDAP_REQ_ABANDON:
582 do_abandon( conn, arg->co_op );
586 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
587 arg->co_op->o_tag, 0, 0 );
592 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
594 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
597 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
600 conn->c_n_ops_completed++;
603 slap_op_remove( &conn->c_ops, arg->co_op );
604 slap_op_free( arg->co_op );
607 free( (char *) arg );
612 case LDAP_REQ_UNBIND_30:
614 case LDAP_REQ_UNBIND:
615 connection_closing( conn );
619 if( conn->c_conn_state == SLAP_C_BINDING) {
620 conn->c_conn_state = SLAP_C_ACTIVE;
624 if( conn->c_conn_state == SLAP_C_CLOSING ) {
625 Debug( LDAP_DEBUG_TRACE,
626 "connection_operation: attempting closing conn=%ld sd=%d.\n",
627 conn->c_connid, conn->c_sb.sb_sd, 0 );
629 connection_close( conn );
632 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
634 if( active_threads < 1 ) {
635 ldap_pvt_thread_cond_signal(&active_threads_cond);
637 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
639 connection_resched( conn );
641 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
646 int connection_read(int s)
650 assert( connections != NULL );
652 ldap_pvt_thread_mutex_lock( &connections_mutex );
654 c = connection_get( s );
656 Debug( LDAP_DEBUG_ANY,
657 "connection_read(%d): no connection!\n",
659 ldap_pvt_thread_mutex_unlock( &connections_mutex );
663 if( c->c_conn_state == SLAP_C_CLOSING ) {
664 Debug( LDAP_DEBUG_TRACE,
665 "connection_read(%d): closing, ignoring input for id=%ld\n",
668 connection_return( c );
669 ldap_pvt_thread_mutex_unlock( &connections_mutex );
673 Debug( LDAP_DEBUG_TRACE,
674 "connection_read(%d): checking for input on id=%ld\n",
677 #define CONNECTION_INPUT_LOOP 1
679 #ifdef DATA_READY_LOOP
680 while(!rc && lber_pvt_sb_data_ready(&c->c_sb))
681 #elif CONNECTION_INPUT_LOOP
685 rc = connection_input( c );
689 Debug( LDAP_DEBUG_TRACE,
690 "connection_read(%d): input error=%d id=%ld, closing.\n",
691 s, rc, c->c_connid );
693 connection_closing( c );
694 connection_close( c );
697 connection_return( c );
698 ldap_pvt_thread_mutex_unlock( &connections_mutex );
708 unsigned long tag, len;
712 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
714 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
719 if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
720 != LDAP_TAG_MESSAGE )
724 Debug( LDAP_DEBUG_TRACE,
725 "ber_get_next on fd %d failed errno %d (%s)\n",
726 lber_pvt_sb_get_desc(&conn->c_sb), err,
727 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
728 Debug( LDAP_DEBUG_TRACE,
729 "\t*** got %ld of %lu so far\n",
730 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
731 conn->c_currentber->ber_len, 0 );
733 if ( err != EWOULDBLOCK && err != EAGAIN ) {
734 /* log, close and send error */
735 ber_free( conn->c_currentber, 1 );
736 conn->c_currentber = NULL;
743 ber = conn->c_currentber;
744 conn->c_currentber = NULL;
746 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
747 /* log, close and send error */
748 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
754 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
755 /* log, close and send error */
756 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
764 if ( conn->c_version == 30 ) {
765 (void) ber_skip_tag( ber, &len );
769 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
771 if ( conn->c_conn_state == SLAP_C_BINDING
772 || conn->c_conn_state == SLAP_C_CLOSING )
774 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
775 slap_op_add( &conn->c_pending_ops, op );
778 connection_op_activate( conn, op );
782 if ( conn->c_struct_state != SLAP_C_USED ) {
783 /* connection must have got closed underneath us */
787 assert( conn->c_struct_state == SLAP_C_USED );
793 connection_resched( Connection *conn )
797 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
798 /* other states need different handling */
802 for( op = slap_op_pop( &conn->c_pending_ops );
804 op = slap_op_pop( &conn->c_pending_ops ) )
806 /* pending operations should not be marked for abandonment */
807 assert(!op->o_abandon);
809 connection_op_activate( conn, op );
811 if ( conn->c_conn_state == SLAP_C_BINDING ) {
818 static int connection_op_activate( Connection *conn, Operation *op )
823 unsigned long tag = op->o_tag;
825 if ( conn->c_dn != NULL ) {
826 tmpdn = ch_strdup( conn->c_dn );
831 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
835 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
836 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
838 slap_op_add( &conn->c_ops, arg->co_op );
840 if(tag == LDAP_REQ_BIND) {
841 conn->c_conn_state = SLAP_C_BINDING;
844 if ( tmpdn != NULL ) {
848 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
850 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
852 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
853 connection_operation, (void *) arg );
856 Debug( LDAP_DEBUG_ANY,
857 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
859 /* should move op to pending list */
865 int connection_write(int s)
868 assert( connections != NULL );
870 ldap_pvt_thread_mutex_lock( &connections_mutex );
872 c = connection_get( s );
874 Debug( LDAP_DEBUG_ANY,
875 "connection_write(%d): no connection!\n",
877 ldap_pvt_thread_mutex_unlock( &connections_mutex );
881 Debug( LDAP_DEBUG_TRACE,
882 "connection_write(%d): waking output for id=%ld\n",
885 ldap_pvt_thread_cond_signal( &c->c_write_cv );
887 connection_return( c );
888 ldap_pvt_thread_mutex_unlock( &connections_mutex );