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 ) {
111 ldap_pvt_thread_mutex_lock( &c->c_mutex );
116 static void connection_return( Connection *c )
118 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
121 long connection_init(
128 assert( connections != NULL );
136 assert( s < dtblsize );
139 ldap_pvt_thread_mutex_lock( &connections_mutex );
148 for( i=0; i < dtblsize; i++ {
149 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
150 assert( connections[i].c_sb.sb_sd == 0 );
155 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
156 assert( connections[i].c_sb.sb_sd == -1 );
161 assert( connections[i].c_struct_state == SLAP_C_USED );
162 assert( connections[i].c_conn_state != SLAP_C_INVALID );
163 assert( connections[i].c_sb.sb_sd != -1 );
167 ldap_pvt_thread_mutex_unlock( &connections_mutex );
174 assert( c->c_struct_state != SLAP_C_USED );
175 assert( c->c_conn_state == SLAP_C_INVALID );
177 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
180 c->c_client_name = NULL;
181 c->c_client_addr = NULL;
183 c->c_pending_ops = NULL;
185 lber_pvt_sb_init( &c->c_sb );
187 /* should check status of thread calls */
188 ldap_pvt_thread_mutex_init( &c->c_mutex );
189 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
190 ldap_pvt_thread_cond_init( &c->c_write_cv );
192 c->c_struct_state = SLAP_C_UNUSED;
195 ldap_pvt_thread_mutex_lock( &c->c_mutex );
197 assert( c->c_struct_state == SLAP_C_UNUSED );
198 assert( c->c_dn == NULL );
199 assert( c->c_cdn == NULL );
200 assert( c->c_client_name == NULL );
201 assert( c->c_client_addr == NULL );
202 assert( c->c_ops == NULL );
203 assert( c->c_pending_ops == NULL );
205 c->c_client_name = ch_strdup( name == NULL ? "" : name );
206 c->c_client_addr = ch_strdup( addr );
208 c->c_n_ops_received = 0;
210 c->c_n_ops_executing = 0;
211 c->c_n_ops_pending = 0;
212 c->c_n_ops_completed = 0;
215 c->c_starttime = slap_get_time();
217 lber_pvt_sb_set_desc( &c->c_sb, s );
218 lber_pvt_sb_set_io( &c->c_sb, &lber_pvt_sb_io_tcp, NULL );
220 if( lber_pvt_sb_set_nonblock( &c->c_sb, 1 ) < 0 ) {
221 Debug( LDAP_DEBUG_ANY,
222 "connection_init(%d, %s, %s): set nonblocking failed\n",
223 s, c->c_client_name, c->c_client_addr);
226 id = c->c_connid = conn_nextid++;
228 c->c_conn_state = SLAP_C_INACTIVE;
229 c->c_struct_state = SLAP_C_USED;
231 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
232 ldap_pvt_thread_mutex_unlock( &connections_mutex );
238 connection_destroy( Connection *c )
240 assert( connections != NULL );
242 assert( c->c_struct_state != SLAP_C_UNUSED );
243 assert( c->c_conn_state != SLAP_C_INVALID );
244 assert( c->c_ops == NULL );
246 c->c_struct_state = SLAP_C_UNUSED;
247 c->c_conn_state = SLAP_C_INVALID;
254 if(c->c_dn != NULL) {
258 if(c->c_cdn != NULL) {
262 if(c->c_client_name != NULL) {
263 free(c->c_client_name);
264 c->c_client_name = NULL;
266 if(c->c_client_addr != NULL) {
267 free(c->c_client_addr);
268 c->c_client_addr = NULL;
271 if ( lber_pvt_sb_in_use(&c->c_sb) ) {
272 int sd = lber_pvt_sb_get_desc(&c->c_sb);
275 lber_pvt_sb_close( &c->c_sb );
277 Statslog( LDAP_DEBUG_STATS,
278 "conn=%d fd=%d closed.\n",
279 c->c_connid, sd, 0, 0, 0 );
282 lber_pvt_sb_destroy( &c->c_sb );
285 static void connection_close( Connection *c )
287 assert( connections != NULL );
289 assert( c->c_struct_state == SLAP_C_USED );
290 assert( c->c_conn_state == SLAP_C_CLOSING );
292 if( c->c_ops != NULL ) {
293 Debug( LDAP_DEBUG_TRACE,
294 "connection_close: deferring conn=%ld sd=%d.\n",
295 c->c_connid, c->c_sb.sb_sd, 0 );
300 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
301 c->c_connid, c->c_sb.sb_sd, 0 );
303 connection_destroy( c );
306 long connections_nextid(void)
309 assert( connections != NULL );
311 ldap_pvt_thread_mutex_lock( &connections_mutex );
315 ldap_pvt_thread_mutex_unlock( &connections_mutex );
320 Connection* connection_first(void)
322 assert( connections != NULL );
324 ldap_pvt_thread_mutex_lock( &connections_mutex );
326 assert( conn_index == -1 );
329 return connection_next(NULL);
332 Connection* connection_next(Connection *c)
334 assert( connections != NULL );
335 assert( conn_index != -1 );
336 assert( conn_index <= dtblsize );
339 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
344 for(; conn_index < dtblsize; conn_index++) {
345 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
346 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
354 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
355 assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
356 c = &connections[conn_index++];
360 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
361 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
365 ldap_pvt_thread_mutex_lock( &c->c_mutex );
371 void connection_done(Connection *c)
373 assert( connections != NULL );
374 assert( conn_index != -1 );
375 assert( conn_index <= dtblsize );
378 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
382 ldap_pvt_thread_mutex_unlock( &connections_mutex );
386 * connection_activity - handle the request operation op on connection
387 * conn. This routine figures out what kind of operation it is and
388 * calls the appropriate stub to handle it.
392 connection_operation( void *arg_v )
394 struct co_arg *arg = arg_v;
395 int tag = arg->co_op->o_tag;
396 Connection *conn = arg->co_conn;
399 ldap_pvt_thread_mutex_lock( &ops_mutex );
401 ldap_pvt_thread_mutex_unlock( &ops_mutex );
406 do_bind( conn, arg->co_op );
410 case LDAP_REQ_UNBIND_30:
412 case LDAP_REQ_UNBIND:
413 do_unbind( conn, arg->co_op );
417 do_add( conn, arg->co_op );
421 case LDAP_REQ_DELETE_30:
423 case LDAP_REQ_DELETE:
424 do_delete( conn, arg->co_op );
427 case LDAP_REQ_MODRDN:
428 do_modrdn( conn, arg->co_op );
431 case LDAP_REQ_MODIFY:
432 do_modify( conn, arg->co_op );
435 case LDAP_REQ_COMPARE:
436 do_compare( conn, arg->co_op );
439 case LDAP_REQ_SEARCH:
440 do_search( conn, arg->co_op );
444 case LDAP_REQ_ABANDON_30:
446 case LDAP_REQ_ABANDON:
447 do_abandon( conn, arg->co_op );
451 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
452 arg->co_op->o_tag, 0, 0 );
457 ldap_pvt_thread_mutex_lock( &ops_mutex );
459 ldap_pvt_thread_mutex_unlock( &ops_mutex );
462 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
465 conn->c_ops_completed++;
468 slap_op_remove( &conn->c_ops, arg->co_op );
469 slap_op_free( arg->co_op );
472 free( (char *) arg );
477 case LDAP_REQ_UNBIND_30:
479 case LDAP_REQ_UNBIND:
480 conn->c_conn_state = SLAP_C_CLOSING;
484 if( conn->c_conn_state == SLAP_C_BINDING) {
485 conn->c_conn_state = SLAP_C_ACTIVE;
489 if( conn->c_conn_state == SLAP_C_CLOSING ) {
490 Debug( LDAP_DEBUG_TRACE,
491 "connection_operation: attempting closing conn=%ld sd=%d.\n",
492 conn->c_connid, conn->c_sb.sb_sd, 0 );
494 connection_close( conn );
497 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
499 if( active_threads < 1 ) {
500 ldap_pvt_thread_cond_signal(&active_threads_cond);
502 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
504 connection_resched( conn );
506 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
511 int connection_read(int s)
515 assert( connections != NULL );
517 ldap_pvt_thread_mutex_lock( &connections_mutex );
519 c = connection_get( s );
521 Debug( LDAP_DEBUG_ANY,
522 "connection_read(%d): no connection!\n",
527 Debug( LDAP_DEBUG_TRACE,
528 "connection_read(%d): checking for input on id=%ld\n",
531 #define CONNECTION_INPUT_LOOP 1
533 #ifdef DATA_READY_LOOP
534 while(!rc && lber_pvt_sb_data_ready(&c->c_sb))
535 #elif CONNECTION_INPUT_LOOP
539 rc = connection_input( c );
543 Debug( LDAP_DEBUG_TRACE,
544 "connection_read(%d): input error id=%ld, closing.\n",
547 c->c_conn_state = SLAP_C_CLOSING;
548 connection_close( c );
551 connection_return( c );
552 ldap_pvt_thread_mutex_unlock( &connections_mutex );
562 unsigned long tag, len;
566 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
568 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
573 if ( (tag = ber_get_next( &conn->c_sb, &len, conn->c_currentber ))
574 != LDAP_TAG_MESSAGE )
576 Debug( LDAP_DEBUG_TRACE,
577 "ber_get_next on fd %d failed errno %d (%s)\n",
578 lber_pvt_sb_get_desc(&conn->c_sb), errno,
579 errno > -1 && errno < sys_nerr ? sys_errlist[errno] : "unknown" );
580 Debug( LDAP_DEBUG_TRACE, "\t*** got %ld of %lu so far\n",
581 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
582 conn->c_currentber->ber_len, 0 );
584 if ( errno != EWOULDBLOCK && errno != EAGAIN ) {
585 /* log, close and send error */
586 ber_free( conn->c_currentber, 1 );
587 conn->c_currentber = NULL;
595 ber = conn->c_currentber;
596 conn->c_currentber = NULL;
598 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
599 /* log, close and send error */
600 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
606 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
607 /* log, close and send error */
608 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
616 if ( conn->c_version == 30 ) {
617 (void) ber_skip_tag( ber, &len );
621 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
623 if ( conn->c_conn_state == SLAP_C_BINDING
624 || conn->c_conn_state == SLAP_C_CLOSING )
626 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
627 slap_op_add( &conn->c_pending_ops, op );
630 connection_op_activate( conn, op );
634 if ( conn->c_struct_state != SLAP_C_USED ) {
635 /* connection must have got closed underneath us */
639 assert( conn->c_struct_state == SLAP_C_USED );
645 connection_resched( Connection *conn )
649 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
650 /* other states need different handling */
654 for( op = slap_op_pop( &conn->c_pending_ops );
656 op = slap_op_pop( &conn->c_pending_ops ) )
658 connection_op_activate( conn, op );
660 if ( conn->c_conn_state == SLAP_C_BINDING ) {
666 static int connection_op_activate( Connection *conn, Operation *op )
671 unsigned long tag = op->o_tag;
673 if ( conn->c_dn != NULL ) {
674 tmpdn = ch_strdup( conn->c_dn );
679 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
683 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
684 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
686 slap_op_add( &conn->c_ops, arg->co_op );
688 if(tag == LDAP_REQ_BIND) {
689 conn->c_conn_state = SLAP_C_BINDING;
692 if ( tmpdn != NULL ) {
696 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
698 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
700 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
701 connection_operation, (void *) arg );
704 Debug( LDAP_DEBUG_ANY,
705 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
707 /* should move op to pending list */
713 int connection_write(int s)
716 assert( connections != NULL );
718 ldap_pvt_thread_mutex_lock( &connections_mutex );
720 c = connection_get( s );
722 Debug( LDAP_DEBUG_ANY,
723 "connection_write(%d): no connection!\n",
728 Debug( LDAP_DEBUG_TRACE,
729 "connection_write(%d): waking output for id=%ld\n",
732 ldap_pvt_thread_cond_signal( &c->c_write_cv );
734 connection_return( c );
735 ldap_pvt_thread_mutex_unlock( &connections_mutex );