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 unsigned 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 static Connection* connection_get( ber_socket_t s );
35 static int connection_input( Connection *c );
36 static void connection_close( Connection *c );
38 static int connection_op_activate( Connection *conn, Operation *op );
39 static int connection_resched( Connection *conn );
47 * Initialize connection management infrastructure.
49 int connections_init(void)
51 assert( connections == NULL );
53 if( connections != NULL) {
54 Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n",
59 /* should check return of every call */
60 ldap_pvt_thread_mutex_init( &connections_mutex );
62 connections = (Connection *) calloc( dtblsize, sizeof(Connection) );
64 if( connections == NULL ) {
65 Debug( LDAP_DEBUG_ANY,
66 "connections_init: allocation (%d*%ld) of connection array failed.\n",
67 dtblsize, (long) sizeof(Connection), 0 );
71 assert( connections[0].c_struct_state == SLAP_C_UNINITIALIZED );
72 assert( connections[dtblsize-1].c_struct_state == SLAP_C_UNINITIALIZED );
75 * per entry initialization of the Connection array initialization
76 * will be done by connection_init()
83 * Destroy connection management infrastructure.
85 int connections_destroy(void)
89 /* should check return of every call */
91 if( connections == NULL) {
92 Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n",
97 for ( i = 0; i < dtblsize; i++ ) {
98 if( connections[i].c_struct_state != SLAP_C_UNINITIALIZED ) {
99 ldap_pvt_thread_mutex_destroy( &connections[i].c_mutex );
100 ldap_pvt_thread_mutex_destroy( &connections[i].c_write_mutex );
101 ldap_pvt_thread_cond_destroy( &connections[i].c_write_cv );
108 ldap_pvt_thread_mutex_destroy( &connections_mutex );
113 * shutdown all connections
115 int connections_shutdown(void)
119 ldap_pvt_thread_mutex_lock( &connections_mutex );
121 for ( i = 0; i < dtblsize; i++ ) {
122 if( connections[i].c_struct_state != SLAP_C_USED ) {
126 ldap_pvt_thread_mutex_lock( &connections[i].c_mutex );
128 /* connections_mutex and c_mutex are locked */
129 connection_closing( &connections[i] );
130 connection_close( &connections[i] );
132 ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex );
135 ldap_pvt_thread_mutex_unlock( &connections_mutex );
141 * Timeout idle connections.
143 int connections_timeout_idle(time_t now)
149 ldap_pvt_thread_mutex_lock( &connections_mutex );
151 for( c = connection_first( &connindex );
153 c = connection_next( c, &connindex ) )
155 if( difftime( c->c_activitytime+global_idletimeout, now) < 0 ) {
157 connection_closing( c );
158 connection_close( c );
162 connection_done( c );
164 ldap_pvt_thread_mutex_unlock( &connections_mutex );
169 static Connection* connection_get( ber_socket_t s )
171 /* connections_mutex should be locked by caller */
175 Debug( LDAP_DEBUG_ARGS,
176 "connection_get(%ld)\n",
179 assert( connections != NULL );
181 if(s == AC_SOCKET_INVALID) {
188 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
195 for(i=0; i<dtblsize; i++) {
196 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
197 assert( connections[i].c_conn_state == SLAP_C_INVALID );
198 assert( connections[i].c_sb == 0 );
202 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
203 assert( connections[i].c_conn_state == SLAP_C_INVALID );
204 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
208 /* state can actually change from used -> unused by resched,
209 * so don't assert details here.
212 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
221 ldap_pvt_thread_mutex_lock( &c->c_mutex );
223 if( c->c_struct_state != SLAP_C_USED ) {
224 /* connection must have been closed due to resched */
226 assert( c->c_conn_state == SLAP_C_INVALID );
227 assert( !ber_pvt_sb_in_use( c->c_sb ) );
229 Debug( LDAP_DEBUG_TRACE,
230 "connection_get(%d): connection not used.\n",
233 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
237 Debug( LDAP_DEBUG_TRACE,
238 "connection_get(%d): got connid=%ld\n",
241 assert( c->c_struct_state == SLAP_C_USED );
242 assert( c->c_conn_state != SLAP_C_INVALID );
243 assert( ber_pvt_sb_in_use( c->c_sb ) );
245 c->c_activitytime = slap_get_time();
251 static void connection_return( Connection *c )
253 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
256 long connection_init(
263 assert( connections != NULL );
265 if( s == AC_SOCKET_INVALID ) {
266 Debug( LDAP_DEBUG_ANY,
267 "connection_init(%ld): invalid.\n",
274 assert( s < dtblsize );
277 ldap_pvt_thread_mutex_lock( &connections_mutex );
288 for( i=0; i < dtblsize; i++) {
289 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
290 assert( connections[i].c_sb == 0 );
295 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
296 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
301 assert( connections[i].c_struct_state == SLAP_C_USED );
302 assert( connections[i].c_conn_state != SLAP_C_INVALID );
303 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
307 Debug( LDAP_DEBUG_ANY,
308 "connection_init(%d): connection table full (%d/%d).\n",
310 ldap_pvt_thread_mutex_unlock( &connections_mutex );
317 assert( c->c_struct_state != SLAP_C_USED );
318 assert( c->c_conn_state == SLAP_C_INVALID );
320 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
323 c->c_client_name = NULL;
324 c->c_client_addr = NULL;
326 c->c_pending_ops = NULL;
328 c->c_sb = ber_sockbuf_alloc( );
330 /* should check status of thread calls */
331 ldap_pvt_thread_mutex_init( &c->c_mutex );
332 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
333 ldap_pvt_thread_cond_init( &c->c_write_cv );
335 c->c_struct_state = SLAP_C_UNUSED;
338 ldap_pvt_thread_mutex_lock( &c->c_mutex );
340 assert( c->c_struct_state == SLAP_C_UNUSED );
341 assert( c->c_dn == NULL );
342 assert( c->c_cdn == NULL );
343 assert( c->c_client_name == NULL );
344 assert( c->c_client_addr == NULL );
345 assert( c->c_ops == NULL );
346 assert( c->c_pending_ops == NULL );
348 c->c_client_name = ch_strdup( name == NULL ? "" : name );
349 c->c_client_addr = ch_strdup( addr );
351 c->c_n_ops_received = 0;
353 c->c_n_ops_executing = 0;
354 c->c_n_ops_pending = 0;
355 c->c_n_ops_completed = 0;
358 c->c_activitytime = c->c_starttime = slap_get_time();
360 ber_pvt_sb_set_desc( c->c_sb, s );
361 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
363 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
364 Debug( LDAP_DEBUG_ANY,
365 "connection_init(%d, %s, %s): set nonblocking failed\n",
366 s, c->c_client_name, c->c_client_addr);
369 id = c->c_connid = conn_nextid++;
371 c->c_conn_state = SLAP_C_INACTIVE;
372 c->c_struct_state = SLAP_C_USED;
374 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
375 ldap_pvt_thread_mutex_unlock( &connections_mutex );
377 backend_connection_init(c);
383 connection_destroy( Connection *c )
385 /* note: connections_mutex should be locked by caller */
387 assert( connections != NULL );
389 assert( c->c_struct_state != SLAP_C_UNUSED );
390 assert( c->c_conn_state != SLAP_C_INVALID );
391 assert( c->c_ops == NULL );
393 backend_connection_destroy(c);
397 c->c_activitytime = c->c_starttime = 0;
399 if(c->c_dn != NULL) {
403 if(c->c_cdn != NULL) {
407 if(c->c_client_name != NULL) {
408 free(c->c_client_name);
409 c->c_client_name = NULL;
411 if(c->c_client_addr != NULL) {
412 free(c->c_client_addr);
413 c->c_client_addr = NULL;
416 if ( ber_pvt_sb_in_use(c->c_sb) ) {
417 int sd = ber_pvt_sb_get_desc(c->c_sb);
419 slapd_remove( sd, 0 );
420 ber_pvt_sb_close( c->c_sb );
422 Statslog( LDAP_DEBUG_STATS,
423 "conn=%d fd=%d closed.\n",
424 c->c_connid, sd, 0, 0, 0 );
427 ber_pvt_sb_destroy( c->c_sb );
429 c->c_conn_state = SLAP_C_INVALID;
430 c->c_struct_state = SLAP_C_UNUSED;
433 int connection_state_closing( Connection *c )
435 /* c_mutex must be locked by caller */
439 assert( c->c_struct_state == SLAP_C_USED );
441 state = c->c_conn_state;
443 assert( state != SLAP_C_INVALID );
445 return state == SLAP_C_CLOSING;
448 void connection_closing( Connection *c )
450 assert( connections != NULL );
452 assert( c->c_struct_state == SLAP_C_USED );
453 assert( c->c_conn_state != SLAP_C_INVALID );
455 /* c_mutex must be locked by caller */
457 if( c->c_conn_state != SLAP_C_CLOSING ) {
460 Debug( LDAP_DEBUG_TRACE,
461 "connection_closing: readying conn=%ld sd=%d for close.\n",
462 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
464 /* update state to closing */
465 c->c_conn_state = SLAP_C_CLOSING;
467 /* don't listen on this port anymore */
468 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
470 /* shutdown I/O -- not yet implemented */
472 /* abandon active operations */
473 for( o = c->c_ops; o != NULL; o = o->o_next ) {
474 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
476 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
479 /* remove pending operations */
480 for( o = slap_op_pop( &c->c_pending_ops );
482 o = slap_op_pop( &c->c_pending_ops ) )
487 /* wake write blocked operations */
488 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
489 ldap_pvt_thread_cond_signal( &c->c_write_cv );
493 static void connection_close( Connection *c )
495 assert( connections != NULL );
497 assert( c->c_struct_state == SLAP_C_USED );
498 assert( c->c_conn_state == SLAP_C_CLOSING );
500 /* note: connections_mutex and c_mutex should be locked by caller */
502 if( c->c_ops != NULL ) {
503 Debug( LDAP_DEBUG_TRACE,
504 "connection_close: deferring conn=%ld sd=%d.\n",
505 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
510 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
511 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
513 connection_destroy( c );
516 unsigned long connections_nextid(void)
519 assert( connections != NULL );
521 ldap_pvt_thread_mutex_lock( &connections_mutex );
525 ldap_pvt_thread_mutex_unlock( &connections_mutex );
530 Connection* connection_first( ber_socket_t *index )
532 assert( connections != NULL );
533 assert( index != NULL );
535 ldap_pvt_thread_mutex_lock( &connections_mutex );
539 return connection_next(NULL, index);
542 Connection* connection_next( Connection *c, ber_socket_t *index )
544 assert( connections != NULL );
545 assert( index != NULL );
546 assert( *index <= dtblsize );
549 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
554 for(; *index < dtblsize; (*index)++) {
555 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
556 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
564 if( connections[*index].c_struct_state == SLAP_C_USED ) {
565 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
566 c = &connections[(*index)++];
570 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
571 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
575 ldap_pvt_thread_mutex_lock( &c->c_mutex );
581 void connection_done( Connection *c )
583 assert( connections != NULL );
586 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
589 ldap_pvt_thread_mutex_unlock( &connections_mutex );
593 * connection_activity - handle the request operation op on connection
594 * conn. This routine figures out what kind of operation it is and
595 * calls the appropriate stub to handle it.
599 connection_operation( void *arg_v )
601 struct co_arg *arg = arg_v;
602 ber_tag_t tag = arg->co_op->o_tag;
603 Connection *conn = arg->co_conn;
606 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
608 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
613 do_bind( conn, arg->co_op );
616 case LDAP_REQ_UNBIND:
617 do_unbind( conn, arg->co_op );
621 do_add( conn, arg->co_op );
624 case LDAP_REQ_DELETE:
625 do_delete( conn, arg->co_op );
628 case LDAP_REQ_MODRDN:
629 do_modrdn( conn, arg->co_op );
632 case LDAP_REQ_MODIFY:
633 do_modify( conn, arg->co_op );
636 case LDAP_REQ_COMPARE:
637 do_compare( conn, arg->co_op );
640 case LDAP_REQ_SEARCH:
641 do_search( conn, arg->co_op );
644 case LDAP_REQ_ABANDON:
645 do_abandon( conn, arg->co_op );
649 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
650 arg->co_op->o_tag, 0, 0 );
655 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
657 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
660 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
663 conn->c_n_ops_completed++;
666 slap_op_remove( &conn->c_ops, arg->co_op );
667 slap_op_free( arg->co_op );
670 free( (char *) arg );
674 case LDAP_REQ_UNBIND:
675 /* c_mutex is locked */
676 connection_closing( conn );
680 if( conn->c_conn_state == SLAP_C_BINDING) {
681 conn->c_conn_state = SLAP_C_ACTIVE;
685 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
687 if( active_threads < 1 ) {
688 ldap_pvt_thread_cond_signal(&active_threads_cond);
690 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
692 connection_resched( conn );
694 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
699 int connection_read(ber_socket_t s)
703 assert( connections != NULL );
705 ldap_pvt_thread_mutex_lock( &connections_mutex );
707 /* get (locked) connection */
708 c = connection_get( s );
711 Debug( LDAP_DEBUG_ANY,
712 "connection_read(%ld): no connection!\n",
717 ldap_pvt_thread_mutex_unlock( &connections_mutex );
721 if( c->c_conn_state == SLAP_C_CLOSING ) {
722 Debug( LDAP_DEBUG_TRACE,
723 "connection_read(%d): closing, ignoring input for id=%ld\n",
726 connection_return( c );
727 ldap_pvt_thread_mutex_unlock( &connections_mutex );
731 Debug( LDAP_DEBUG_TRACE,
732 "connection_read(%d): checking for input on id=%ld\n",
735 #define CONNECTION_INPUT_LOOP 1
737 #ifdef DATA_READY_LOOP
738 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
739 #elif CONNECTION_INPUT_LOOP
743 rc = connection_input( c );
747 Debug( LDAP_DEBUG_TRACE,
748 "connection_read(%d): input error=%d id=%ld, closing.\n",
749 s, rc, c->c_connid );
751 /* connections_mutex and c_mutex are locked */
752 connection_closing( c );
753 connection_close( c );
756 connection_return( c );
757 ldap_pvt_thread_mutex_unlock( &connections_mutex );
772 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
774 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
779 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
780 != LDAP_TAG_MESSAGE )
784 Debug( LDAP_DEBUG_TRACE,
785 "ber_get_next on fd %d failed errno %d (%s)\n",
786 ber_pvt_sb_get_desc( conn->c_sb ), err,
787 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
788 Debug( LDAP_DEBUG_TRACE,
789 "\t*** got %ld of %lu so far\n",
790 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
791 conn->c_currentber->ber_len, 0 );
793 if ( err != EWOULDBLOCK && err != EAGAIN ) {
794 /* log, close and send error */
795 ber_free( conn->c_currentber, 1 );
796 conn->c_currentber = NULL;
803 ber = conn->c_currentber;
804 conn->c_currentber = NULL;
806 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
807 /* log, close and send error */
808 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
814 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
815 /* log, close and send error */
816 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
823 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
825 if ( conn->c_conn_state == SLAP_C_BINDING
826 || conn->c_conn_state == SLAP_C_CLOSING )
828 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
829 slap_op_add( &conn->c_pending_ops, op );
832 connection_op_activate( conn, op );
836 if ( conn->c_struct_state != SLAP_C_USED ) {
837 /* connection must have got closed underneath us */
841 assert( conn->c_struct_state == SLAP_C_USED );
847 connection_resched( Connection *conn )
851 if( conn->c_conn_state == SLAP_C_CLOSING ) {
852 Debug( LDAP_DEBUG_TRACE,
853 "connection_resched: attempting closing conn=%ld sd=%d.\n",
854 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
856 connection_close( conn );
860 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
861 /* other states need different handling */
865 for( op = slap_op_pop( &conn->c_pending_ops );
867 op = slap_op_pop( &conn->c_pending_ops ) )
869 /* pending operations should not be marked for abandonment */
870 assert(!op->o_abandon);
872 connection_op_activate( conn, op );
874 if ( conn->c_conn_state == SLAP_C_BINDING ) {
881 static int connection_op_activate( Connection *conn, Operation *op )
886 ber_tag_t tag = op->o_tag;
888 if ( conn->c_dn != NULL ) {
889 tmpdn = ch_strdup( conn->c_dn );
894 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
898 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
899 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
901 arg->co_op->o_protocol = conn->c_protocol;
903 slap_op_add( &conn->c_ops, arg->co_op );
905 if(tag == LDAP_REQ_BIND) {
906 conn->c_conn_state = SLAP_C_BINDING;
909 if ( tmpdn != NULL ) {
913 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
915 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
917 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
918 connection_operation, (void *) arg );
921 Debug( LDAP_DEBUG_ANY,
922 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
924 /* should move op to pending list */
930 int connection_write(ber_socket_t s)
933 assert( connections != NULL );
935 ldap_pvt_thread_mutex_lock( &connections_mutex );
937 c = connection_get( s );
939 slapd_clr_write( s, 0);
942 Debug( LDAP_DEBUG_ANY,
943 "connection_write(%ld): no connection!\n",
946 ldap_pvt_thread_mutex_unlock( &connections_mutex );
950 Debug( LDAP_DEBUG_TRACE,
951 "connection_write(%d): waking output for id=%ld\n",
954 ldap_pvt_thread_cond_signal( &c->c_write_cv );
956 connection_return( c );
957 ldap_pvt_thread_mutex_unlock( &connections_mutex );