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 static Connection* connection_get( int s );
21 static int connection_input( Connection *c );
23 static int connection_op_activate( Connection *conn, Operation *op );
24 static int connection_resched( Connection *conn );
32 * Initialize connection management infrastructure.
34 int connections_init(void)
38 assert( connections == NULL );
40 if( connections != NULL) { /* probably should assert this */
41 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
46 /* should check return of every call */
47 ldap_pvt_thread_mutex_init( &connections_mutex );
49 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
51 if( connections == NULL ) {
52 Debug( LDAP_DEBUG_ANY,
53 "connections_init: allocation (%d*%ld) of connection array failed.\n",
54 dtblsize, (long) sizeof(Connection), 0 );
59 * per entry initialization of the Connection array initialization
60 * will be done by connection_init()
66 static Connection* connection_get( int s )
70 assert( connections != NULL );
77 assert( connections[s].c_struct_state == SLAP_C_USED );
78 assert( connections[s].c_conn_state != SLAP_C_INVALID );
79 assert( connections[s].c_sb.sb_sd != -1 );
86 for(i=0; i<dtblsize; i++) {
87 if( connections[i].c_struct_state == SLAP_C_STRUCT_UNINITIALIZED ) {
88 assert( connections[i].c_conn_state == SLAP_C_INVALID );
89 assert( connections[s].c_sb.sb_sd == 0 );
93 if( connections[i].c_struct_state == SLAP_C_STRUCT_UNUSED ) {
94 assert( connections[i].c_conn_state == SLAP_C_INVALID );
95 assert( connections[s].c_sb.sb_sd == -1 );
99 assert( connections[i].c_struct_state == SLAP_C_STRUCT_USED );
100 assert( connections[i].c_conn_state != SLAP_C_INVALID );
101 assert( connections[s].c_sb.sb_sd != -1 );
103 if( connections[i].c_sb.sb_sd == s ) {
112 /* we do this BEFORE locking to aid in debugging */
113 Debug( LDAP_DEBUG_TRACE,
114 "connection_get(%d): got connid=%ld\n",
117 ldap_pvt_thread_mutex_lock( &c->c_mutex );
122 static void connection_return( Connection *c )
124 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
127 long connection_init(
134 assert( connections != NULL );
142 assert( s < dtblsize );
145 ldap_pvt_thread_mutex_lock( &connections_mutex );
154 for( i=0; i < dtblsize; i++ {
155 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
156 assert( connections[i].c_sb.sb_sd == 0 );
161 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
162 assert( connections[i].c_sb.sb_sd == -1 );
167 assert( connections[i].c_struct_state == SLAP_C_USED );
168 assert( connections[i].c_conn_state != SLAP_C_INVALID );
169 assert( connections[i].c_sb.sb_sd != -1 );
173 ldap_pvt_thread_mutex_unlock( &connections_mutex );
180 assert( c->c_struct_state != SLAP_C_USED );
181 assert( c->c_conn_state == SLAP_C_INVALID );
183 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
186 c->c_client_name = NULL;
187 c->c_client_addr = NULL;
189 c->c_pending_ops = NULL;
191 lber_pvt_sb_init( &c->c_sb );
193 /* should check status of thread calls */
194 ldap_pvt_thread_mutex_init( &c->c_mutex );
195 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
196 ldap_pvt_thread_cond_init( &c->c_write_cv );
198 c->c_struct_state = SLAP_C_UNUSED;
201 ldap_pvt_thread_mutex_lock( &c->c_mutex );
203 assert( c->c_struct_state == SLAP_C_UNUSED );
204 assert( c->c_dn == NULL );
205 assert( c->c_cdn == NULL );
206 assert( c->c_client_name == NULL );
207 assert( c->c_client_addr == NULL );
208 assert( c->c_ops == NULL );
209 assert( c->c_pending_ops == NULL );
211 c->c_client_name = ch_strdup( name == NULL ? "" : name );
212 c->c_client_addr = ch_strdup( addr );
214 c->c_n_ops_received = 0;
216 c->c_n_ops_executing = 0;
217 c->c_n_ops_pending = 0;
218 c->c_n_ops_completed = 0;
221 c->c_starttime = slap_get_time();
223 lber_pvt_sb_set_desc( &c->c_sb, s );
224 lber_pvt_sb_set_io( &c->c_sb, &lber_pvt_sb_io_tcp, NULL );
226 if( lber_pvt_sb_set_nonblock( &c->c_sb, 1 ) < 0 ) {
227 Debug( LDAP_DEBUG_ANY,
228 "connection_init(%d, %s, %s): set nonblocking failed\n",
229 s, c->c_client_name, c->c_client_addr);
232 id = c->c_connid = conn_nextid++;
234 c->c_conn_state = SLAP_C_INACTIVE;
235 c->c_struct_state = SLAP_C_USED;
237 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
238 ldap_pvt_thread_mutex_unlock( &connections_mutex );
244 connection_destroy( Connection *c )
246 assert( connections != NULL );
248 assert( c->c_struct_state != SLAP_C_UNUSED );
249 assert( c->c_conn_state != SLAP_C_INVALID );
250 assert( c->c_ops == NULL );
252 c->c_struct_state = SLAP_C_UNUSED;
253 c->c_conn_state = SLAP_C_INVALID;
260 if(c->c_dn != NULL) {
264 if(c->c_cdn != NULL) {
268 if(c->c_client_name != NULL) {
269 free(c->c_client_name);
270 c->c_client_name = NULL;
272 if(c->c_client_addr != NULL) {
273 free(c->c_client_addr);
274 c->c_client_addr = NULL;
277 if ( lber_pvt_sb_in_use(&c->c_sb) ) {
278 int sd = lber_pvt_sb_get_desc(&c->c_sb);
281 lber_pvt_sb_close( &c->c_sb );
283 Statslog( LDAP_DEBUG_STATS,
284 "conn=%d fd=%d closed.\n",
285 c->c_connid, sd, 0, 0, 0 );
288 lber_pvt_sb_destroy( &c->c_sb );
291 static void connection_close( Connection *c )
293 assert( connections != NULL );
295 assert( c->c_struct_state == SLAP_C_USED );
296 assert( c->c_conn_state == SLAP_C_CLOSING );
298 if( c->c_ops != NULL ) {
299 Debug( LDAP_DEBUG_TRACE,
300 "connection_close: deferring conn=%ld sd=%d.\n",
301 c->c_connid, c->c_sb.sb_sd, 0 );
306 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
307 c->c_connid, c->c_sb.sb_sd, 0 );
309 connection_destroy( c );
312 long connections_nextid(void)
315 assert( connections != NULL );
317 ldap_pvt_thread_mutex_lock( &connections_mutex );
321 ldap_pvt_thread_mutex_unlock( &connections_mutex );
326 Connection* connection_first(void)
328 assert( connections != NULL );
330 ldap_pvt_thread_mutex_lock( &connections_mutex );
332 assert( conn_index == -1 );
335 return connection_next(NULL);
338 Connection* connection_next(Connection *c)
340 assert( connections != NULL );
341 assert( conn_index != -1 );
342 assert( conn_index <= dtblsize );
345 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
350 for(; conn_index < dtblsize; conn_index++) {
351 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
352 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
360 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
361 assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
362 c = &connections[conn_index++];
366 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
367 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
371 ldap_pvt_thread_mutex_lock( &c->c_mutex );
377 void connection_done(Connection *c)
379 assert( connections != NULL );
380 assert( conn_index != -1 );
381 assert( conn_index <= dtblsize );
384 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
388 ldap_pvt_thread_mutex_unlock( &connections_mutex );
392 * connection_activity - handle the request operation op on connection
393 * conn. This routine figures out what kind of operation it is and
394 * calls the appropriate stub to handle it.
398 connection_operation( void *arg_v )
400 struct co_arg *arg = arg_v;
401 int tag = arg->co_op->o_tag;
402 Connection *conn = arg->co_conn;
405 ldap_pvt_thread_mutex_lock( &ops_mutex );
407 ldap_pvt_thread_mutex_unlock( &ops_mutex );
412 do_bind( conn, arg->co_op );
416 case LDAP_REQ_UNBIND_30:
418 case LDAP_REQ_UNBIND:
419 do_unbind( conn, arg->co_op );
423 do_add( conn, arg->co_op );
427 case LDAP_REQ_DELETE_30:
429 case LDAP_REQ_DELETE:
430 do_delete( conn, arg->co_op );
433 case LDAP_REQ_MODRDN:
434 do_modrdn( conn, arg->co_op );
437 case LDAP_REQ_MODIFY:
438 do_modify( conn, arg->co_op );
441 case LDAP_REQ_COMPARE:
442 do_compare( conn, arg->co_op );
445 case LDAP_REQ_SEARCH:
446 do_search( conn, arg->co_op );
450 case LDAP_REQ_ABANDON_30:
452 case LDAP_REQ_ABANDON:
453 do_abandon( conn, arg->co_op );
457 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
458 arg->co_op->o_tag, 0, 0 );
463 ldap_pvt_thread_mutex_lock( &ops_mutex );
465 ldap_pvt_thread_mutex_unlock( &ops_mutex );
468 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
471 conn->c_ops_completed++;
474 slap_op_remove( &conn->c_ops, arg->co_op );
475 slap_op_free( arg->co_op );
478 free( (char *) arg );
483 case LDAP_REQ_UNBIND_30:
485 case LDAP_REQ_UNBIND:
486 conn->c_conn_state = SLAP_C_CLOSING;
490 if( conn->c_conn_state == SLAP_C_BINDING) {
491 conn->c_conn_state = SLAP_C_ACTIVE;
495 if( conn->c_conn_state == SLAP_C_CLOSING ) {
496 Debug( LDAP_DEBUG_TRACE,
497 "connection_operation: attempting closing conn=%ld sd=%d.\n",
498 conn->c_connid, conn->c_sb.sb_sd, 0 );
500 connection_close( conn );
503 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
505 if( active_threads < 1 ) {
506 ldap_pvt_thread_cond_signal(&active_threads_cond);
508 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
510 connection_resched( conn );
512 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
517 int connection_read(int s)
521 assert( connections != NULL );
523 ldap_pvt_thread_mutex_lock( &connections_mutex );
525 c = connection_get( s );
527 Debug( LDAP_DEBUG_ANY,
528 "connection_read(%d): no connection!\n",
533 Debug( LDAP_DEBUG_TRACE,
534 "connection_read(%d): checking for input on id=%ld\n",
537 #define CONNECTION_INPUT_LOOP 1
539 #ifdef DATA_READY_LOOP
540 while(!rc && lber_pvt_sb_data_ready(&c->c_sb))
541 #elif CONNECTION_INPUT_LOOP
545 rc = connection_input( c );
549 Debug( LDAP_DEBUG_TRACE,
550 "connection_read(%d): input error id=%ld, closing.\n",
553 c->c_conn_state = SLAP_C_CLOSING;
554 connection_close( c );
557 connection_return( c );
558 ldap_pvt_thread_mutex_unlock( &connections_mutex );
568 unsigned long tag, len;
572 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
574 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
579 if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
580 != LDAP_TAG_MESSAGE )
582 Debug( LDAP_DEBUG_TRACE,
583 "ber_get_next on fd %d failed errno %d (%s)\n",
584 lber_pvt_sb_get_desc(&conn->c_sb), errno,
585 errno > -1 && errno < sys_nerr ? sys_errlist[errno] : "unknown" );
586 Debug( LDAP_DEBUG_TRACE, "\t*** got %ld of %lu so far\n",
587 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
588 conn->c_currentber->ber_len, 0 );
590 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
591 /* log, close and send error */
592 ber_free( conn->c_currentber, 1 );
593 conn->c_currentber = NULL;
601 ber = conn->c_currentber;
602 conn->c_currentber = NULL;
604 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
605 /* log, close and send error */
606 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
612 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
613 /* log, close and send error */
614 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
622 if ( conn->c_version == 30 ) {
623 (void) ber_skip_tag( ber, &len );
627 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
629 if ( conn->c_conn_state == SLAP_C_BINDING
630 || conn->c_conn_state == SLAP_C_CLOSING )
632 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
633 slap_op_add( &conn->c_pending_ops, op );
636 connection_op_activate( conn, op );
640 if ( conn->c_struct_state != SLAP_C_USED ) {
641 /* connection must have got closed underneath us */
645 assert( conn->c_struct_state == SLAP_C_USED );
651 connection_resched( Connection *conn )
655 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
656 /* other states need different handling */
660 for( op = slap_op_pop( &conn->c_pending_ops );
662 op = slap_op_pop( &conn->c_pending_ops ) )
664 connection_op_activate( conn, op );
666 if ( conn->c_conn_state == SLAP_C_BINDING ) {
672 static int connection_op_activate( Connection *conn, Operation *op )
677 unsigned long tag = op->o_tag;
679 if ( conn->c_dn != NULL ) {
680 tmpdn = ch_strdup( conn->c_dn );
685 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
689 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
690 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
692 slap_op_add( &conn->c_ops, arg->co_op );
694 if(tag == LDAP_REQ_BIND) {
695 conn->c_conn_state = SLAP_C_BINDING;
698 if ( tmpdn != NULL ) {
702 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
704 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
706 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
707 connection_operation, (void *) arg );
710 Debug( LDAP_DEBUG_ANY,
711 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
713 /* should move op to pending list */
719 int connection_write(int s)
722 assert( connections != NULL );
724 ldap_pvt_thread_mutex_lock( &connections_mutex );
726 c = connection_get( s );
728 Debug( LDAP_DEBUG_ANY,
729 "connection_write(%d): no connection!\n",
734 Debug( LDAP_DEBUG_TRACE,
735 "connection_write(%d): waking output for id=%ld\n",
738 ldap_pvt_thread_cond_signal( &c->c_write_cv );
740 connection_return( c );
741 ldap_pvt_thread_mutex_unlock( &connections_mutex );