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 );
112 ldap_pvt_thread_mutex_destroy( &connections_mutex );
117 * shutdown all connections
119 int connections_shutdown(void)
123 ldap_pvt_thread_mutex_lock( &connections_mutex );
125 for ( i = 0; i < dtblsize; i++ ) {
126 if( connections[i].c_struct_state != SLAP_C_USED ) {
130 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
131 connection_closing( &connections[i] );
132 connection_close( &connections[i] );
133 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
136 ldap_pvt_thread_mutex_unlock( &connections_mutex );
141 static Connection* connection_get( int s )
143 Connection *c = NULL;
145 assert( connections != NULL );
152 assert( connections[s].c_struct_state == SLAP_C_USED );
153 assert( connections[s].c_conn_state != SLAP_C_INVALID );
154 assert( ber_pvt_sb_in_use( connections[s].c_sb ) );
161 for(i=0; i<dtblsize; i++) {
162 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
163 assert( connections[i].c_conn_state == SLAP_C_INVALID );
164 assert( connections[i].c_sb == 0 );
168 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
169 assert( connections[i].c_conn_state == SLAP_C_INVALID );
170 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
174 assert( connections[i].c_struct_state == SLAP_C_USED );
175 assert( connections[i].c_conn_state != SLAP_C_INVALID );
176 assert( ber_pvt_sb_in_use( connections[i].c_sb ) );
178 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
187 /* we do this BEFORE locking to aid in debugging */
188 Debug( LDAP_DEBUG_TRACE,
189 "connection_get(%d): got connid=%ld\n",
192 ldap_pvt_thread_mutex_lock( &c->c_mutex );
197 static void connection_return( Connection *c )
199 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
202 long connection_init(
209 assert( connections != NULL );
217 assert( s < dtblsize );
220 ldap_pvt_thread_mutex_lock( &connections_mutex );
229 for( i=0; i < dtblsize; i++) {
230 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
231 assert( connections[i].c_sb == 0 );
236 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
237 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
242 assert( connections[i].c_struct_state == SLAP_C_USED );
243 assert( connections[i].c_conn_state != SLAP_C_INVALID );
244 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
248 ldap_pvt_thread_mutex_unlock( &connections_mutex );
255 assert( c->c_struct_state != SLAP_C_USED );
256 assert( c->c_conn_state == SLAP_C_INVALID );
258 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
261 c->c_client_name = NULL;
262 c->c_client_addr = NULL;
264 c->c_pending_ops = NULL;
266 c->c_sb = ber_sockbuf_alloc( );
268 /* should check status of thread calls */
269 ldap_pvt_thread_mutex_init( &c->c_mutex );
270 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
271 ldap_pvt_thread_cond_init( &c->c_write_cv );
273 c->c_struct_state = SLAP_C_UNUSED;
276 ldap_pvt_thread_mutex_lock( &c->c_mutex );
278 assert( c->c_struct_state == SLAP_C_UNUSED );
279 assert( c->c_dn == NULL );
280 assert( c->c_cdn == NULL );
281 assert( c->c_client_name == NULL );
282 assert( c->c_client_addr == NULL );
283 assert( c->c_ops == NULL );
284 assert( c->c_pending_ops == NULL );
286 c->c_client_name = ch_strdup( name == NULL ? "" : name );
287 c->c_client_addr = ch_strdup( addr );
289 c->c_n_ops_received = 0;
291 c->c_n_ops_executing = 0;
292 c->c_n_ops_pending = 0;
293 c->c_n_ops_completed = 0;
296 c->c_starttime = slap_get_time();
298 ber_pvt_sb_set_desc( c->c_sb, s );
299 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
301 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
302 Debug( LDAP_DEBUG_ANY,
303 "connection_init(%d, %s, %s): set nonblocking failed\n",
304 s, c->c_client_name, c->c_client_addr);
307 id = c->c_connid = conn_nextid++;
309 c->c_conn_state = SLAP_C_INACTIVE;
310 c->c_struct_state = SLAP_C_USED;
312 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
313 ldap_pvt_thread_mutex_unlock( &connections_mutex );
319 connection_destroy( Connection *c )
321 /* note: connections_mutex should be locked by caller */
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 c->c_struct_state = SLAP_C_UNUSED;
330 c->c_conn_state = SLAP_C_INVALID;
339 if(c->c_dn != NULL) {
343 if(c->c_cdn != NULL) {
347 if(c->c_client_name != NULL) {
348 free(c->c_client_name);
349 c->c_client_name = NULL;
351 if(c->c_client_addr != NULL) {
352 free(c->c_client_addr);
353 c->c_client_addr = NULL;
356 if ( ber_pvt_sb_in_use(c->c_sb) ) {
357 int sd = ber_pvt_sb_get_desc(c->c_sb);
360 ber_pvt_sb_close( c->c_sb );
362 Statslog( LDAP_DEBUG_STATS,
363 "conn=%d fd=%d closed.\n",
364 c->c_connid, sd, 0, 0, 0 );
367 ber_pvt_sb_destroy( c->c_sb );
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 /* note: connections_mutex should be locked by caller */
436 if( c->c_ops != NULL ) {
437 Debug( LDAP_DEBUG_TRACE,
438 "connection_close: deferring conn=%ld sd=%d.\n",
439 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
444 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
445 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
447 connection_destroy( c );
450 long connections_nextid(void)
453 assert( connections != NULL );
455 ldap_pvt_thread_mutex_lock( &connections_mutex );
459 ldap_pvt_thread_mutex_unlock( &connections_mutex );
464 Connection* connection_first(void)
466 assert( connections != NULL );
468 ldap_pvt_thread_mutex_lock( &connections_mutex );
470 assert( conn_index == -1 );
473 return connection_next(NULL);
476 Connection* connection_next(Connection *c)
478 assert( connections != NULL );
479 assert( conn_index != -1 );
480 assert( conn_index <= dtblsize );
483 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
488 for(; conn_index < dtblsize; conn_index++) {
489 if( connections[conn_index].c_struct_state == SLAP_C_UNINITIALIZED ) {
490 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
498 if( connections[conn_index].c_struct_state == SLAP_C_USED ) {
499 assert( connections[conn_index].c_conn_state != SLAP_C_INVALID );
500 c = &connections[conn_index++];
504 assert( connections[conn_index].c_struct_state == SLAP_C_UNUSED );
505 assert( connections[conn_index].c_conn_state == SLAP_C_INVALID );
509 ldap_pvt_thread_mutex_lock( &c->c_mutex );
515 void connection_done(Connection *c)
517 assert( connections != NULL );
518 assert( conn_index != -1 );
519 assert( conn_index <= dtblsize );
522 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
526 ldap_pvt_thread_mutex_unlock( &connections_mutex );
530 * connection_activity - handle the request operation op on connection
531 * conn. This routine figures out what kind of operation it is and
532 * calls the appropriate stub to handle it.
536 connection_operation( void *arg_v )
538 struct co_arg *arg = arg_v;
539 int tag = arg->co_op->o_tag;
540 Connection *conn = arg->co_conn;
543 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
545 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
550 do_bind( conn, arg->co_op );
554 case LDAP_REQ_UNBIND_30:
556 case LDAP_REQ_UNBIND:
557 do_unbind( conn, arg->co_op );
561 do_add( conn, arg->co_op );
565 case LDAP_REQ_DELETE_30:
567 case LDAP_REQ_DELETE:
568 do_delete( conn, arg->co_op );
571 case LDAP_REQ_MODRDN:
572 do_modrdn( conn, arg->co_op );
575 case LDAP_REQ_MODIFY:
576 do_modify( conn, arg->co_op );
579 case LDAP_REQ_COMPARE:
580 do_compare( conn, arg->co_op );
583 case LDAP_REQ_SEARCH:
584 do_search( conn, arg->co_op );
588 case LDAP_REQ_ABANDON_30:
590 case LDAP_REQ_ABANDON:
591 do_abandon( conn, arg->co_op );
595 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
596 arg->co_op->o_tag, 0, 0 );
601 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
603 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
606 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
609 conn->c_n_ops_completed++;
612 slap_op_remove( &conn->c_ops, arg->co_op );
613 slap_op_free( arg->co_op );
616 free( (char *) arg );
621 case LDAP_REQ_UNBIND_30:
623 case LDAP_REQ_UNBIND:
624 connection_closing( conn );
628 if( conn->c_conn_state == SLAP_C_BINDING) {
629 conn->c_conn_state = SLAP_C_ACTIVE;
633 if( conn->c_conn_state == SLAP_C_CLOSING ) {
634 Debug( LDAP_DEBUG_TRACE,
635 "connection_operation: attempting closing conn=%ld sd=%d.\n",
636 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
638 connection_close( conn );
641 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
643 if( active_threads < 1 ) {
644 ldap_pvt_thread_cond_signal(&active_threads_cond);
646 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
648 connection_resched( conn );
650 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
655 int connection_read(int s)
659 assert( connections != NULL );
661 ldap_pvt_thread_mutex_lock( &connections_mutex );
663 c = connection_get( s );
665 Debug( LDAP_DEBUG_ANY,
666 "connection_read(%d): no connection!\n",
668 ldap_pvt_thread_mutex_unlock( &connections_mutex );
672 if( c->c_conn_state == SLAP_C_CLOSING ) {
673 Debug( LDAP_DEBUG_TRACE,
674 "connection_read(%d): closing, ignoring input for id=%ld\n",
677 connection_return( c );
678 ldap_pvt_thread_mutex_unlock( &connections_mutex );
682 Debug( LDAP_DEBUG_TRACE,
683 "connection_read(%d): checking for input on id=%ld\n",
686 #define CONNECTION_INPUT_LOOP 1
688 #ifdef DATA_READY_LOOP
689 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
690 #elif CONNECTION_INPUT_LOOP
694 rc = connection_input( c );
698 Debug( LDAP_DEBUG_TRACE,
699 "connection_read(%d): input error=%d id=%ld, closing.\n",
700 s, rc, c->c_connid );
702 connection_closing( c );
703 connection_close( c );
706 connection_return( c );
707 ldap_pvt_thread_mutex_unlock( &connections_mutex );
717 unsigned long tag, len;
721 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
723 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
728 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
729 != LDAP_TAG_MESSAGE )
733 Debug( LDAP_DEBUG_TRACE,
734 "ber_get_next on fd %d failed errno %d (%s)\n",
735 ber_pvt_sb_get_desc( conn->c_sb ), err,
736 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
737 Debug( LDAP_DEBUG_TRACE,
738 "\t*** got %ld of %lu so far\n",
739 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
740 conn->c_currentber->ber_len, 0 );
742 if ( err != EWOULDBLOCK && err != EAGAIN ) {
743 /* log, close and send error */
744 ber_free( conn->c_currentber, 1 );
745 conn->c_currentber = NULL;
752 ber = conn->c_currentber;
753 conn->c_currentber = NULL;
755 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
756 /* log, close and send error */
757 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
763 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
764 /* log, close and send error */
765 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
773 if ( conn->c_version == 30 ) {
774 (void) ber_skip_tag( ber, &len );
778 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
780 if ( conn->c_conn_state == SLAP_C_BINDING
781 || conn->c_conn_state == SLAP_C_CLOSING )
783 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
784 slap_op_add( &conn->c_pending_ops, op );
787 connection_op_activate( conn, op );
791 if ( conn->c_struct_state != SLAP_C_USED ) {
792 /* connection must have got closed underneath us */
796 assert( conn->c_struct_state == SLAP_C_USED );
802 connection_resched( Connection *conn )
806 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
807 /* other states need different handling */
811 for( op = slap_op_pop( &conn->c_pending_ops );
813 op = slap_op_pop( &conn->c_pending_ops ) )
815 /* pending operations should not be marked for abandonment */
816 assert(!op->o_abandon);
818 connection_op_activate( conn, op );
820 if ( conn->c_conn_state == SLAP_C_BINDING ) {
827 static int connection_op_activate( Connection *conn, Operation *op )
832 unsigned long tag = op->o_tag;
834 if ( conn->c_dn != NULL ) {
835 tmpdn = ch_strdup( conn->c_dn );
840 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
844 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
845 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
847 slap_op_add( &conn->c_ops, arg->co_op );
849 if(tag == LDAP_REQ_BIND) {
850 conn->c_conn_state = SLAP_C_BINDING;
853 if ( tmpdn != NULL ) {
857 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
859 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
861 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
862 connection_operation, (void *) arg );
865 Debug( LDAP_DEBUG_ANY,
866 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
868 /* should move op to pending list */
874 int connection_write(int s)
877 assert( connections != NULL );
879 ldap_pvt_thread_mutex_lock( &connections_mutex );
881 c = connection_get( s );
883 Debug( LDAP_DEBUG_ANY,
884 "connection_write(%d): no connection!\n",
886 ldap_pvt_thread_mutex_unlock( &connections_mutex );
890 Debug( LDAP_DEBUG_TRACE,
891 "connection_write(%d): waking output for id=%ld\n",
894 ldap_pvt_thread_cond_signal( &c->c_write_cv );
896 connection_return( c );
897 ldap_pvt_thread_mutex_unlock( &connections_mutex );