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 long conn_nextid = 0;
21 /* structure state (protected by connections_mutex) */
22 #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */
23 #define SLAP_C_UNUSED 0x01
24 #define SLAP_C_USED 0x02
26 /* connection state (protected by c_mutex ) */
27 #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */
28 #define SLAP_C_INACTIVE 0x01 /* zero threads */
29 #define SLAP_C_ACTIVE 0x02 /* one or more threads */
30 #define SLAP_C_BINDING 0x03 /* binding */
31 #define SLAP_C_CLOSING 0x04 /* closing */
33 void slapd_remove(int s);
34 static Connection* connection_get( int s );
36 static int connection_input( Connection *c );
37 static void connection_close( Connection *c );
39 static int connection_op_activate( Connection *conn, Operation *op );
40 static int connection_resched( Connection *conn );
48 * Initialize connection management infrastructure.
50 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 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
73 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
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 );
109 ldap_pvt_thread_mutex_destroy( &connections_mutex );
114 * shutdown all connections
116 int connections_shutdown(void)
120 ldap_pvt_thread_mutex_lock( &connections_mutex );
122 for ( i = 0; i < dtblsize; i++ ) {
123 if( connections[i].c_struct_state != SLAP_C_USED ) {
127 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
128 connection_closing( &connections[i] );
129 connection_close( &connections[i] );
130 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
133 ldap_pvt_thread_mutex_unlock( &connections_mutex );
138 static Connection* connection_get( int s )
140 /* connections_mutex should be locked by caller */
142 Connection *c = NULL;
144 assert( connections != NULL );
151 assert( connections[s].c_struct_state == SLAP_C_USED );
152 assert( connections[s].c_conn_state != SLAP_C_INVALID );
153 assert( ber_pvt_sb_in_use( connections[s].c_sb ) );
160 for(i=0; i<dtblsize; i++) {
161 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
162 assert( connections[i].c_conn_state == SLAP_C_INVALID );
163 assert( connections[i].c_sb == 0 );
167 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
168 assert( connections[i].c_conn_state == SLAP_C_INVALID );
169 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
173 assert( connections[i].c_struct_state == SLAP_C_USED );
174 assert( connections[i].c_conn_state != SLAP_C_INVALID );
175 assert( ber_pvt_sb_in_use( connections[i].c_sb ) );
177 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
186 /* we do this BEFORE locking to aid in debugging */
187 Debug( LDAP_DEBUG_TRACE,
188 "connection_get(%d): got connid=%ld\n",
191 ldap_pvt_thread_mutex_lock( &c->c_mutex );
196 static void connection_return( Connection *c )
198 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
201 long connection_init(
208 assert( connections != NULL );
216 assert( s < dtblsize );
219 ldap_pvt_thread_mutex_lock( &connections_mutex );
228 for( i=0; i < dtblsize; i++) {
229 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
230 assert( connections[i].c_sb == 0 );
235 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
236 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
241 assert( connections[i].c_struct_state == SLAP_C_USED );
242 assert( connections[i].c_conn_state != SLAP_C_INVALID );
243 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
247 ldap_pvt_thread_mutex_unlock( &connections_mutex );
254 assert( c->c_struct_state != SLAP_C_USED );
255 assert( c->c_conn_state == SLAP_C_INVALID );
257 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
260 c->c_client_name = NULL;
261 c->c_client_addr = NULL;
263 c->c_pending_ops = NULL;
265 c->c_sb = ber_sockbuf_alloc( );
267 /* should check status of thread calls */
268 ldap_pvt_thread_mutex_init( &c->c_mutex );
269 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
270 ldap_pvt_thread_cond_init( &c->c_write_cv );
272 c->c_struct_state = SLAP_C_UNUSED;
275 ldap_pvt_thread_mutex_lock( &c->c_mutex );
277 assert( c->c_struct_state == SLAP_C_UNUSED );
278 assert( c->c_dn == NULL );
279 assert( c->c_cdn == NULL );
280 assert( c->c_client_name == NULL );
281 assert( c->c_client_addr == NULL );
282 assert( c->c_ops == NULL );
283 assert( c->c_pending_ops == NULL );
285 c->c_client_name = ch_strdup( name == NULL ? "" : name );
286 c->c_client_addr = ch_strdup( addr );
288 c->c_n_ops_received = 0;
290 c->c_n_ops_executing = 0;
291 c->c_n_ops_pending = 0;
292 c->c_n_ops_completed = 0;
295 c->c_starttime = slap_get_time();
297 ber_pvt_sb_set_desc( c->c_sb, s );
298 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
300 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
301 Debug( LDAP_DEBUG_ANY,
302 "connection_init(%d, %s, %s): set nonblocking failed\n",
303 s, c->c_client_name, c->c_client_addr);
306 id = c->c_connid = conn_nextid++;
308 c->c_conn_state = SLAP_C_INACTIVE;
309 c->c_struct_state = SLAP_C_USED;
311 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
312 ldap_pvt_thread_mutex_unlock( &connections_mutex );
318 connection_destroy( Connection *c )
320 /* note: connections_mutex should be locked by caller */
322 assert( connections != NULL );
324 assert( c->c_struct_state != SLAP_C_UNUSED );
325 assert( c->c_conn_state != SLAP_C_INVALID );
326 assert( c->c_ops == NULL );
328 c->c_struct_state = SLAP_C_UNUSED;
329 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 );
369 int connection_state_closing( Connection *c )
371 /* c_mutex 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 /* c_mutex must be locked by caller */
393 if( c->c_conn_state != SLAP_C_CLOSING ) {
396 Debug( LDAP_DEBUG_TRACE,
397 "connection_closing: readying conn=%ld sd=%d for close.\n",
398 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
400 /* update state to closing */
401 c->c_conn_state = SLAP_C_CLOSING;
403 /* don't listen on this port anymore */
404 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
406 /* shutdown I/O -- not yet implemented */
408 /* abandon active operations */
409 for( o = c->c_ops; o != NULL; o = o->o_next ) {
410 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
412 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
415 /* remove pending operations */
416 for( o = slap_op_pop( &c->c_pending_ops );
418 o = slap_op_pop( &c->c_pending_ops ) )
423 /* wake write blocked operations */
424 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
425 ldap_pvt_thread_cond_signal( &c->c_write_cv );
429 static void connection_close( Connection *c )
431 assert( connections != NULL );
433 assert( c->c_struct_state == SLAP_C_USED );
434 assert( c->c_conn_state == SLAP_C_CLOSING );
436 /* note: connections_mutex and c_mutex should be locked by caller */
438 if( c->c_ops != NULL ) {
439 Debug( LDAP_DEBUG_TRACE,
440 "connection_close: deferring conn=%ld sd=%d.\n",
441 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
446 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
447 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
449 connection_destroy( c );
452 long connections_nextid(void)
455 assert( connections != NULL );
457 ldap_pvt_thread_mutex_lock( &connections_mutex );
461 ldap_pvt_thread_mutex_unlock( &connections_mutex );
466 Connection* connection_first( int *index )
468 assert( connections != NULL );
469 assert( index != NULL );
471 ldap_pvt_thread_mutex_lock( &connections_mutex );
475 return connection_next(NULL, index);
478 Connection* connection_next( Connection *c, int *index )
480 assert( connections != NULL );
481 assert( index != NULL );
482 assert( *index <= dtblsize );
485 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
490 for(; *index < dtblsize; (*index)++) {
491 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
492 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
500 if( connections[*index].c_struct_state == SLAP_C_USED ) {
501 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
502 c = &connections[(*index)++];
506 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
507 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
511 ldap_pvt_thread_mutex_lock( &c->c_mutex );
517 void connection_done( Connection *c )
519 assert( connections != NULL );
522 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
525 ldap_pvt_thread_mutex_unlock( &connections_mutex );
529 * connection_activity - handle the request operation op on connection
530 * conn. This routine figures out what kind of operation it is and
531 * calls the appropriate stub to handle it.
535 connection_operation( void *arg_v )
537 struct co_arg *arg = arg_v;
538 int tag = arg->co_op->o_tag;
539 Connection *conn = arg->co_conn;
542 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
544 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
549 do_bind( conn, arg->co_op );
553 case LDAP_REQ_UNBIND_30:
555 case LDAP_REQ_UNBIND:
556 do_unbind( conn, arg->co_op );
560 do_add( conn, arg->co_op );
564 case LDAP_REQ_DELETE_30:
566 case LDAP_REQ_DELETE:
567 do_delete( conn, arg->co_op );
570 case LDAP_REQ_MODRDN:
571 do_modrdn( conn, arg->co_op );
574 case LDAP_REQ_MODIFY:
575 do_modify( conn, arg->co_op );
578 case LDAP_REQ_COMPARE:
579 do_compare( conn, arg->co_op );
582 case LDAP_REQ_SEARCH:
583 do_search( conn, arg->co_op );
587 case LDAP_REQ_ABANDON_30:
589 case LDAP_REQ_ABANDON:
590 do_abandon( conn, arg->co_op );
594 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
595 arg->co_op->o_tag, 0, 0 );
600 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
602 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
605 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
608 conn->c_n_ops_completed++;
611 slap_op_remove( &conn->c_ops, arg->co_op );
612 slap_op_free( arg->co_op );
615 free( (char *) arg );
620 case LDAP_REQ_UNBIND_30:
622 case LDAP_REQ_UNBIND:
623 connection_closing( conn );
627 if( conn->c_conn_state == SLAP_C_BINDING) {
628 conn->c_conn_state = SLAP_C_ACTIVE;
632 if( conn->c_conn_state == SLAP_C_CLOSING ) {
633 Debug( LDAP_DEBUG_TRACE,
634 "connection_operation: attempting closing conn=%ld sd=%d.\n",
635 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
637 connection_close( conn );
640 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
642 if( active_threads < 1 ) {
643 ldap_pvt_thread_cond_signal(&active_threads_cond);
645 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
647 connection_resched( conn );
649 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
654 int connection_read(int s)
658 assert( connections != NULL );
660 ldap_pvt_thread_mutex_lock( &connections_mutex );
662 c = connection_get( s );
664 Debug( LDAP_DEBUG_ANY,
665 "connection_read(%d): no connection!\n",
667 ldap_pvt_thread_mutex_unlock( &connections_mutex );
671 if( c->c_conn_state == SLAP_C_CLOSING ) {
672 Debug( LDAP_DEBUG_TRACE,
673 "connection_read(%d): closing, ignoring input for id=%ld\n",
676 connection_return( c );
677 ldap_pvt_thread_mutex_unlock( &connections_mutex );
681 Debug( LDAP_DEBUG_TRACE,
682 "connection_read(%d): checking for input on id=%ld\n",
685 #define CONNECTION_INPUT_LOOP 1
687 #ifdef DATA_READY_LOOP
688 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
689 #elif CONNECTION_INPUT_LOOP
693 rc = connection_input( c );
697 Debug( LDAP_DEBUG_TRACE,
698 "connection_read(%d): input error=%d id=%ld, closing.\n",
699 s, rc, c->c_connid );
701 connection_closing( c );
702 connection_close( c );
705 connection_return( c );
706 ldap_pvt_thread_mutex_unlock( &connections_mutex );
716 unsigned long tag, len;
720 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
722 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
727 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
728 != LDAP_TAG_MESSAGE )
732 Debug( LDAP_DEBUG_TRACE,
733 "ber_get_next on fd %d failed errno %d (%s)\n",
734 ber_pvt_sb_get_desc( conn->c_sb ), err,
735 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
736 Debug( LDAP_DEBUG_TRACE,
737 "\t*** got %ld of %lu so far\n",
738 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
739 conn->c_currentber->ber_len, 0 );
741 if ( err != EWOULDBLOCK && err != EAGAIN ) {
742 /* log, close and send error */
743 ber_free( conn->c_currentber, 1 );
744 conn->c_currentber = NULL;
751 ber = conn->c_currentber;
752 conn->c_currentber = NULL;
754 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
755 /* log, close and send error */
756 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
762 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
763 /* log, close and send error */
764 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
772 if ( conn->c_version == 30 ) {
773 (void) ber_skip_tag( ber, &len );
777 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
779 if ( conn->c_conn_state == SLAP_C_BINDING
780 || conn->c_conn_state == SLAP_C_CLOSING )
782 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
783 slap_op_add( &conn->c_pending_ops, op );
786 connection_op_activate( conn, op );
790 if ( conn->c_struct_state != SLAP_C_USED ) {
791 /* connection must have got closed underneath us */
795 assert( conn->c_struct_state == SLAP_C_USED );
801 connection_resched( Connection *conn )
805 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
806 /* other states need different handling */
810 for( op = slap_op_pop( &conn->c_pending_ops );
812 op = slap_op_pop( &conn->c_pending_ops ) )
814 /* pending operations should not be marked for abandonment */
815 assert(!op->o_abandon);
817 connection_op_activate( conn, op );
819 if ( conn->c_conn_state == SLAP_C_BINDING ) {
826 static int connection_op_activate( Connection *conn, Operation *op )
831 unsigned long tag = op->o_tag;
833 if ( conn->c_dn != NULL ) {
834 tmpdn = ch_strdup( conn->c_dn );
839 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
843 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
844 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
846 slap_op_add( &conn->c_ops, arg->co_op );
848 if(tag == LDAP_REQ_BIND) {
849 conn->c_conn_state = SLAP_C_BINDING;
852 if ( tmpdn != NULL ) {
856 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
858 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
860 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
861 connection_operation, (void *) arg );
864 Debug( LDAP_DEBUG_ANY,
865 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
867 /* should move op to pending list */
873 int connection_write(int s)
876 assert( connections != NULL );
878 ldap_pvt_thread_mutex_lock( &connections_mutex );
880 c = connection_get( s );
882 Debug( LDAP_DEBUG_ANY,
883 "connection_write(%d): no connection!\n",
885 ldap_pvt_thread_mutex_unlock( &connections_mutex );
889 Debug( LDAP_DEBUG_TRACE,
890 "connection_write(%d): waking output for id=%ld\n",
893 ldap_pvt_thread_cond_signal( &c->c_write_cv );
895 connection_return( c );
896 ldap_pvt_thread_mutex_unlock( &connections_mutex );