13 /* we need LBER internals */
14 #include "../../libraries/liblber/lber-int.h"
16 /* protected by connections_mutex */
17 static ldap_pvt_thread_mutex_t connections_mutex;
18 static Connection *connections = NULL;
19 static int conn_index = -1;
20 static long conn_nextid = 0;
22 /* structure state (protected by connections_mutex) */
23 #define SLAP_C_UNINITIALIZED 0x0 /* MUST BE ZERO (0) */
24 #define SLAP_C_UNUSED 0x1
25 #define SLAP_C_USED 0x2
27 /* connection state (protected by c_mutex ) */
28 #define SLAP_C_INVALID 0x0 /* MUST BE ZERO (0) */
29 #define SLAP_C_INACTIVE 0x1 /* zero threads */
30 #define SLAP_C_ACTIVE 0x2 /* one or more threads */
31 #define SLAP_C_BINDING 0x3 /* binding */
32 #define SLAP_C_CLOSING 0x4 /* closing */
34 void slapd_remove(int s);
35 static Connection* connection_get( int s );
37 static int connection_input( Connection *c );
38 static void connection_close( Connection *c );
40 static int connection_op_activate( Connection *conn, Operation *op );
41 static int connection_resched( Connection *conn );
49 * Initialize connection management infrastructure.
51 int connections_init(void)
55 assert( connections == NULL );
57 if( connections != NULL) {
58 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
63 /* should check return of every call */
64 ldap_pvt_thread_mutex_init( &connections_mutex );
66 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
68 if( connections == NULL ) {
69 Debug( LDAP_DEBUG_ANY,
70 "connections_init: allocation (%d*%ld) of connection array failed.\n",
71 dtblsize, (long) sizeof(Connection), 0 );
75 for ( i = 0; i < dtblsize; i++ )
76 memset( &connections[i], 0, sizeof(Connection) );
79 * per entry initialization of the Connection array initialization
80 * will be done by connection_init()
87 * Destroy connection management infrastructure.
89 int connections_destroy(void)
93 /* should check return of every call */
95 if( connections == NULL) {
96 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
101 for ( i = 0; i < dtblsize; i++ ) {
102 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
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 );
108 free( &connections[i] );
114 ldap_pvt_thread_mutex_destroy( &connections_mutex );
119 * shutdown all connections
121 int connections_shutdown(void)
125 ldap_pvt_thread_mutex_lock( &connections_mutex );
127 for ( i = 0; i < dtblsize; i++ ) {
128 if( connections[i].c_struct_state != SLAP_C_USED ) {
132 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
133 connection_closing( &connections[i] );
134 connection_close( &connections[i] );
135 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
138 ldap_pvt_thread_mutex_unlock( &connections_mutex );
143 static Connection* connection_get( int s )
145 Connection *c = NULL;
147 assert( connections != NULL );
154 assert( connections[s].c_struct_state == SLAP_C_USED );
155 assert( connections[s].c_conn_state != SLAP_C_INVALID );
156 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
163 for(i=0; i<dtblsize; i++) {
164 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
165 assert( connections[i].c_conn_state == SLAP_C_INVALID );
166 assert( connections[i].c_sb == 0 );
170 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
171 assert( connections[i].c_conn_state == SLAP_C_INVALID );
172 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
176 assert( connections[i].c_struct_state == SLAP_C_USED );
177 assert( connections[i].c_conn_state != SLAP_C_INVALID );
178 assert( ber_pvt_sb_in_use( connections[i].c_sb ) );
180 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
189 /* we do this BEFORE locking to aid in debugging */
190 Debug( LDAP_DEBUG_TRACE,
191 "connection_get(%d): got connid=%ld\n",
194 ldap_pvt_thread_mutex_lock( &c->c_mutex );
199 static void connection_return( Connection *c )
201 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
204 long connection_init(
211 assert( connections != NULL );
219 assert( s < dtblsize );
222 ldap_pvt_thread_mutex_lock( &connections_mutex );
231 for( i=0; i < dtblsize; i++) {
232 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
233 assert( connections[i].c_sb == 0 );
238 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
239 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
244 assert( connections[i].c_struct_state == SLAP_C_USED );
245 assert( connections[i].c_conn_state != SLAP_C_INVALID );
246 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
250 ldap_pvt_thread_mutex_unlock( &connections_mutex );
257 assert( c->c_struct_state != SLAP_C_USED );
258 assert( c->c_conn_state == SLAP_C_INVALID );
260 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
263 c->c_client_name = NULL;
264 c->c_client_addr = NULL;
266 c->c_pending_ops = NULL;
268 c->c_sb = ber_sockbuf_alloc( );
270 /* should check status of thread calls */
271 ldap_pvt_thread_mutex_init( &c->c_mutex );
272 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
273 ldap_pvt_thread_cond_init( &c->c_write_cv );
275 c->c_struct_state = SLAP_C_UNUSED;
278 ldap_pvt_thread_mutex_lock( &c->c_mutex );
280 assert( c->c_struct_state == SLAP_C_UNUSED );
281 assert( c->c_dn == NULL );
282 assert( c->c_cdn == NULL );
283 assert( c->c_client_name == NULL );
284 assert( c->c_client_addr == NULL );
285 assert( c->c_ops == NULL );
286 assert( c->c_pending_ops == NULL );
288 c->c_client_name = ch_strdup( name == NULL ? "" : name );
289 c->c_client_addr = ch_strdup( addr );
291 c->c_n_ops_received = 0;
293 c->c_n_ops_executing = 0;
294 c->c_n_ops_pending = 0;
295 c->c_n_ops_completed = 0;
298 c->c_starttime = slap_get_time();
300 ber_pvt_sb_set_desc( c->c_sb, s );
301 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
303 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
304 Debug( LDAP_DEBUG_ANY,
305 "connection_init(%d, %s, %s): set nonblocking failed\n",
306 s, c->c_client_name, c->c_client_addr);
309 id = c->c_connid = conn_nextid++;
311 c->c_conn_state = SLAP_C_INACTIVE;
312 c->c_struct_state = SLAP_C_USED;
314 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
315 ldap_pvt_thread_mutex_unlock( &connections_mutex );
321 connection_destroy( Connection *c )
323 assert( connections != NULL );
325 assert( c->c_struct_state != SLAP_C_UNUSED );
326 assert( c->c_conn_state != SLAP_C_INVALID );
327 assert( c->c_ops == NULL );
329 ldap_pvt_thread_mutex_lock( &connections_mutex );
330 c->c_struct_state = SLAP_C_UNUSED;
331 c->c_conn_state = SLAP_C_INVALID;
338 if(c->c_dn != NULL) {
342 if(c->c_cdn != NULL) {
346 if(c->c_client_name != NULL) {
347 free(c->c_client_name);
348 c->c_client_name = NULL;
350 if(c->c_client_addr != NULL) {
351 free(c->c_client_addr);
352 c->c_client_addr = NULL;
355 if ( ber_pvt_sb_in_use(c->c_sb) ) {
356 int sd = ber_pvt_sb_get_desc(c->c_sb);
359 ber_pvt_sb_close( c->c_sb );
361 Statslog( LDAP_DEBUG_STATS,
362 "conn=%d fd=%d closed.\n",
363 c->c_connid, sd, 0, 0, 0 );
366 ber_pvt_sb_destroy( c->c_sb );
367 ldap_pvt_thread_mutex_unlock( &connections_mutex );
370 int connection_state_closing( Connection *c )
372 /* connection must be locked by caller */
375 assert( c->c_struct_state == SLAP_C_USED );
377 state = c->c_conn_state;
379 assert( state != SLAP_C_INVALID );
381 return state == SLAP_C_CLOSING;
384 void connection_closing( Connection *c )
386 assert( connections != NULL );
388 assert( c->c_struct_state == SLAP_C_USED );
389 assert( c->c_conn_state != SLAP_C_INVALID );
391 if( c->c_conn_state != SLAP_C_CLOSING ) {
394 Debug( LDAP_DEBUG_TRACE,
395 "connection_closing: readying conn=%ld sd=%d for close.\n",
396 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
398 /* update state to closing */
399 c->c_conn_state = SLAP_C_CLOSING;
401 /* don't listen on this port anymore */
402 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
404 /* shutdown I/O -- not yet implemented */
406 /* abandon active operations */
407 for( o = c->c_ops; o != NULL; o = o->o_next ) {
408 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
410 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
413 /* remove pending operations */
414 for( o = slap_op_pop( &c->c_pending_ops );
416 o = slap_op_pop( &c->c_pending_ops ) )
421 /* wake write blocked operations */
422 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
423 ldap_pvt_thread_cond_signal( &c->c_write_cv );
427 static void connection_close( Connection *c )
429 assert( connections != NULL );
431 assert( c->c_struct_state == SLAP_C_USED );
432 assert( c->c_conn_state == SLAP_C_CLOSING );
434 if( c->c_ops != NULL ) {
435 Debug( LDAP_DEBUG_TRACE,
436 "connection_close: deferring conn=%ld sd=%d.\n",
437 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
442 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
443 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
445 connection_destroy( c );
448 long connections_nextid(void)
451 assert( connections != NULL );
453 ldap_pvt_thread_mutex_lock( &connections_mutex );
457 ldap_pvt_thread_mutex_unlock( &connections_mutex );
462 Connection* connection_first(void)
464 assert( connections != NULL );
466 ldap_pvt_thread_mutex_lock( &connections_mutex );
468 assert( conn_index == -1 );
471 return connection_next(NULL);
474 Connection* connection_next(Connection *c)
476 assert( connections != NULL );
477 assert( conn_index != -1 );
478 assert( conn_index <= dtblsize );
481 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
486 for(; conn_index < dtblsize; conn_index++) {
487 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
488 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
496 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
497 assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
498 c = &connections[conn_index++];
502 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
503 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
507 ldap_pvt_thread_mutex_lock( &c->c_mutex );
513 void connection_done(Connection *c)
515 assert( connections != NULL );
516 assert( conn_index != -1 );
517 assert( conn_index <= dtblsize );
520 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
524 ldap_pvt_thread_mutex_unlock( &connections_mutex );
528 * connection_activity - handle the request operation op on connection
529 * conn. This routine figures out what kind of operation it is and
530 * calls the appropriate stub to handle it.
534 connection_operation( void *arg_v )
536 struct co_arg *arg = arg_v;
537 int tag = arg->co_op->o_tag;
538 Connection *conn = arg->co_conn;
541 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
543 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
548 do_bind( conn, arg->co_op );
552 case LDAP_REQ_UNBIND_30:
554 case LDAP_REQ_UNBIND:
555 do_unbind( conn, arg->co_op );
559 do_add( conn, arg->co_op );
563 case LDAP_REQ_DELETE_30:
565 case LDAP_REQ_DELETE:
566 do_delete( conn, arg->co_op );
569 case LDAP_REQ_MODRDN:
570 do_modrdn( conn, arg->co_op );
573 case LDAP_REQ_MODIFY:
574 do_modify( conn, arg->co_op );
577 case LDAP_REQ_COMPARE:
578 do_compare( conn, arg->co_op );
581 case LDAP_REQ_SEARCH:
582 do_search( conn, arg->co_op );
586 case LDAP_REQ_ABANDON_30:
588 case LDAP_REQ_ABANDON:
589 do_abandon( conn, arg->co_op );
593 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
594 arg->co_op->o_tag, 0, 0 );
599 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
601 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
604 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
607 conn->c_n_ops_completed++;
610 slap_op_remove( &conn->c_ops, arg->co_op );
611 slap_op_free( arg->co_op );
614 free( (char *) arg );
619 case LDAP_REQ_UNBIND_30:
621 case LDAP_REQ_UNBIND:
622 connection_closing( conn );
626 if( conn->c_conn_state == SLAP_C_BINDING) {
627 conn->c_conn_state = SLAP_C_ACTIVE;
631 if( conn->c_conn_state == SLAP_C_CLOSING ) {
632 Debug( LDAP_DEBUG_TRACE,
633 "connection_operation: attempting closing conn=%ld sd=%d.\n",
634 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
636 connection_close( conn );
639 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
641 if( active_threads < 1 ) {
642 ldap_pvt_thread_cond_signal(&active_threads_cond);
644 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
646 connection_resched( conn );
648 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
653 int connection_read(int s)
657 assert( connections != NULL );
659 ldap_pvt_thread_mutex_lock( &connections_mutex );
661 c = connection_get( s );
663 Debug( LDAP_DEBUG_ANY,
664 "connection_read(%d): no connection!\n",
666 ldap_pvt_thread_mutex_unlock( &connections_mutex );
670 if( c->c_conn_state == SLAP_C_CLOSING ) {
671 Debug( LDAP_DEBUG_TRACE,
672 "connection_read(%d): closing, ignoring input for id=%ld\n",
675 connection_return( c );
676 ldap_pvt_thread_mutex_unlock( &connections_mutex );
680 Debug( LDAP_DEBUG_TRACE,
681 "connection_read(%d): checking for input on id=%ld\n",
684 #define CONNECTION_INPUT_LOOP 1
686 #ifdef DATA_READY_LOOP
687 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
688 #elif CONNECTION_INPUT_LOOP
692 rc = connection_input( c );
696 Debug( LDAP_DEBUG_TRACE,
697 "connection_read(%d): input error=%d id=%ld, closing.\n",
698 s, rc, c->c_connid );
700 connection_closing( c );
701 connection_close( c );
704 connection_return( c );
705 ldap_pvt_thread_mutex_unlock( &connections_mutex );
715 unsigned long tag, len;
719 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
721 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
726 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
727 != LDAP_TAG_MESSAGE )
731 Debug( LDAP_DEBUG_TRACE,
732 "ber_get_next on fd %d failed errno %d (%s)\n",
733 ber_pvt_sb_get_desc( conn->c_sb ), err,
734 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
735 Debug( LDAP_DEBUG_TRACE,
736 "\t*** got %ld of %lu so far\n",
737 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
738 conn->c_currentber->ber_len, 0 );
740 if ( err != EWOULDBLOCK && err != EAGAIN ) {
741 /* log, close and send error */
742 ber_free( conn->c_currentber, 1 );
743 conn->c_currentber = NULL;
750 ber = conn->c_currentber;
751 conn->c_currentber = NULL;
753 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
754 /* log, close and send error */
755 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
761 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
762 /* log, close and send error */
763 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
771 if ( conn->c_version == 30 ) {
772 (void) ber_skip_tag( ber, &len );
776 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
778 if ( conn->c_conn_state == SLAP_C_BINDING
779 || conn->c_conn_state == SLAP_C_CLOSING )
781 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
782 slap_op_add( &conn->c_pending_ops, op );
785 connection_op_activate( conn, op );
789 if ( conn->c_struct_state != SLAP_C_USED ) {
790 /* connection must have got closed underneath us */
794 assert( conn->c_struct_state == SLAP_C_USED );
800 connection_resched( Connection *conn )
804 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
805 /* other states need different handling */
809 for( op = slap_op_pop( &conn->c_pending_ops );
811 op = slap_op_pop( &conn->c_pending_ops ) )
813 /* pending operations should not be marked for abandonment */
814 assert(!op->o_abandon);
816 connection_op_activate( conn, op );
818 if ( conn->c_conn_state == SLAP_C_BINDING ) {
825 static int connection_op_activate( Connection *conn, Operation *op )
830 unsigned long tag = op->o_tag;
832 if ( conn->c_dn != NULL ) {
833 tmpdn = ch_strdup( conn->c_dn );
838 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
842 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
843 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
845 slap_op_add( &conn->c_ops, arg->co_op );
847 if(tag == LDAP_REQ_BIND) {
848 conn->c_conn_state = SLAP_C_BINDING;
851 if ( tmpdn != NULL ) {
855 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
857 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
859 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
860 connection_operation, (void *) arg );
863 Debug( LDAP_DEBUG_ANY,
864 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
866 /* should move op to pending list */
872 int connection_write(int s)
875 assert( connections != NULL );
877 ldap_pvt_thread_mutex_lock( &connections_mutex );
879 c = connection_get( s );
881 Debug( LDAP_DEBUG_ANY,
882 "connection_write(%d): no connection!\n",
884 ldap_pvt_thread_mutex_unlock( &connections_mutex );
888 Debug( LDAP_DEBUG_TRACE,
889 "connection_write(%d): waking output for id=%ld\n",
892 ldap_pvt_thread_cond_signal( &c->c_write_cv );
894 connection_return( c );
895 ldap_pvt_thread_mutex_unlock( &connections_mutex );