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 static Connection* connection_get( int 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 );
140 static Connection* connection_get( int s )
142 /* connections_mutex should be locked by caller */
146 Debug( LDAP_DEBUG_ARGS,
147 "connection_get(%d)\n",
150 assert( connections != NULL );
159 assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
166 for(i=0; i<dtblsize; i++) {
167 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
168 assert( connections[i].c_conn_state == SLAP_C_INVALID );
169 assert( connections[i].c_sb == 0 );
173 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
174 assert( connections[i].c_conn_state == SLAP_C_INVALID );
175 assert( !ber_pvt_sb_in_use( connections[i].c_sb ) );
179 assert( connections[i].c_struct_state == SLAP_C_USED );
180 assert( connections[i].c_conn_state != SLAP_C_INVALID );
181 assert( ber_pvt_sb_in_use( connections[i].c_sb ) );
183 if( ber_pvt_sb_get_desc( connections[i].c_sb ) == s ) {
192 ldap_pvt_thread_mutex_lock( &c->c_mutex );
194 if( c->c_struct_state != SLAP_C_USED ) {
195 /* connection must have been closed due to resched */
196 assert( c->c_conn_state == SLAP_C_INVALID );
197 assert( !ber_pvt_sb_in_use( c->c_sb ) );
199 Debug( LDAP_DEBUG_TRACE,
200 "connection_get(%d): connection not used.\n",
203 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
207 Debug( LDAP_DEBUG_TRACE,
208 "connection_get(%d): got connid=%ld\n",
211 assert( c->c_struct_state == SLAP_C_USED );
212 assert( c->c_conn_state != SLAP_C_INVALID );
213 assert( ber_pvt_sb_in_use( c->c_sb ) );
219 static void connection_return( Connection *c )
221 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
224 long connection_init(
231 assert( connections != NULL );
234 Debug( LDAP_DEBUG_ANY,
235 "connection_init(%d): invalid.\n",
242 assert( s < dtblsize );
245 ldap_pvt_thread_mutex_lock( &connections_mutex );
256 for( i=0; i < dtblsize; i++) {
257 if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
258 assert( connections[i].c_sb == 0 );
263 if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
264 assert( !ber_pvt_sb_in_use( connections[i].c_sb ));
269 assert( connections[i].c_struct_state == SLAP_C_USED );
270 assert( connections[i].c_conn_state != SLAP_C_INVALID );
271 assert( ber_pvt_sb_in_use( connections[i].c_sb ));
275 Debug( LDAP_DEBUG_ANY,
276 "connection_init(%d): connection table full (%d/%d).\n",
278 ldap_pvt_thread_mutex_unlock( &connections_mutex );
285 assert( c->c_struct_state != SLAP_C_USED );
286 assert( c->c_conn_state == SLAP_C_INVALID );
288 if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
291 c->c_client_name = NULL;
292 c->c_client_addr = NULL;
294 c->c_pending_ops = NULL;
296 c->c_sb = ber_sockbuf_alloc( );
298 /* should check status of thread calls */
299 ldap_pvt_thread_mutex_init( &c->c_mutex );
300 ldap_pvt_thread_mutex_init( &c->c_write_mutex );
301 ldap_pvt_thread_cond_init( &c->c_write_cv );
303 c->c_struct_state = SLAP_C_UNUSED;
306 ldap_pvt_thread_mutex_lock( &c->c_mutex );
308 assert( c->c_struct_state == SLAP_C_UNUSED );
309 assert( c->c_dn == NULL );
310 assert( c->c_cdn == NULL );
311 assert( c->c_client_name == NULL );
312 assert( c->c_client_addr == NULL );
313 assert( c->c_ops == NULL );
314 assert( c->c_pending_ops == NULL );
316 c->c_client_name = ch_strdup( name == NULL ? "" : name );
317 c->c_client_addr = ch_strdup( addr );
319 c->c_n_ops_received = 0;
321 c->c_n_ops_executing = 0;
322 c->c_n_ops_pending = 0;
323 c->c_n_ops_completed = 0;
326 c->c_starttime = slap_get_time();
328 ber_pvt_sb_set_desc( c->c_sb, s );
329 ber_pvt_sb_set_io( c->c_sb, &ber_pvt_sb_io_tcp, NULL );
331 if( ber_pvt_sb_set_nonblock( c->c_sb, 1 ) < 0 ) {
332 Debug( LDAP_DEBUG_ANY,
333 "connection_init(%d, %s, %s): set nonblocking failed\n",
334 s, c->c_client_name, c->c_client_addr);
337 id = c->c_connid = conn_nextid++;
339 c->c_conn_state = SLAP_C_INACTIVE;
340 c->c_struct_state = SLAP_C_USED;
342 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
343 ldap_pvt_thread_mutex_unlock( &connections_mutex );
349 connection_destroy( Connection *c )
351 /* note: connections_mutex should be locked by caller */
353 assert( connections != NULL );
355 assert( c->c_struct_state != SLAP_C_UNUSED );
356 assert( c->c_conn_state != SLAP_C_INVALID );
357 assert( c->c_ops == NULL );
359 c->c_struct_state = SLAP_C_UNUSED;
360 c->c_conn_state = SLAP_C_INVALID;
369 if(c->c_dn != NULL) {
373 if(c->c_cdn != NULL) {
377 if(c->c_client_name != NULL) {
378 free(c->c_client_name);
379 c->c_client_name = NULL;
381 if(c->c_client_addr != NULL) {
382 free(c->c_client_addr);
383 c->c_client_addr = NULL;
386 if ( ber_pvt_sb_in_use(c->c_sb) ) {
387 int sd = ber_pvt_sb_get_desc(c->c_sb);
389 slapd_remove( sd, 0 );
390 ber_pvt_sb_close( c->c_sb );
392 Statslog( LDAP_DEBUG_STATS,
393 "conn=%d fd=%d closed.\n",
394 c->c_connid, sd, 0, 0, 0 );
397 ber_pvt_sb_destroy( c->c_sb );
400 int connection_state_closing( Connection *c )
402 /* c_mutex must be locked by caller */
406 assert( c->c_struct_state == SLAP_C_USED );
408 state = c->c_conn_state;
410 assert( state != SLAP_C_INVALID );
412 return state == SLAP_C_CLOSING;
415 void connection_closing( Connection *c )
417 assert( connections != NULL );
419 assert( c->c_struct_state == SLAP_C_USED );
420 assert( c->c_conn_state != SLAP_C_INVALID );
422 /* c_mutex must be locked by caller */
424 if( c->c_conn_state != SLAP_C_CLOSING ) {
427 Debug( LDAP_DEBUG_TRACE,
428 "connection_closing: readying conn=%ld sd=%d for close.\n",
429 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
431 /* update state to closing */
432 c->c_conn_state = SLAP_C_CLOSING;
434 /* don't listen on this port anymore */
435 slapd_clr_read( ber_pvt_sb_get_desc( c->c_sb ), 1 );
437 /* shutdown I/O -- not yet implemented */
439 /* abandon active operations */
440 for( o = c->c_ops; o != NULL; o = o->o_next ) {
441 ldap_pvt_thread_mutex_lock( &o->o_abandonmutex );
443 ldap_pvt_thread_mutex_unlock( &o->o_abandonmutex );
446 /* remove pending operations */
447 for( o = slap_op_pop( &c->c_pending_ops );
449 o = slap_op_pop( &c->c_pending_ops ) )
454 /* wake write blocked operations */
455 slapd_clr_write( ber_pvt_sb_get_desc(c->c_sb), 1 );
456 ldap_pvt_thread_cond_signal( &c->c_write_cv );
460 static void connection_close( Connection *c )
462 assert( connections != NULL );
464 assert( c->c_struct_state == SLAP_C_USED );
465 assert( c->c_conn_state == SLAP_C_CLOSING );
467 /* note: connections_mutex and c_mutex should be locked by caller */
469 if( c->c_ops != NULL ) {
470 Debug( LDAP_DEBUG_TRACE,
471 "connection_close: deferring conn=%ld sd=%d.\n",
472 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
477 Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%ld sd=%d.\n",
478 c->c_connid, ber_pvt_sb_get_desc( c->c_sb ), 0 );
480 connection_destroy( c );
483 long connections_nextid(void)
486 assert( connections != NULL );
488 ldap_pvt_thread_mutex_lock( &connections_mutex );
492 ldap_pvt_thread_mutex_unlock( &connections_mutex );
497 Connection* connection_first( int *index )
499 assert( connections != NULL );
500 assert( index != NULL );
502 ldap_pvt_thread_mutex_lock( &connections_mutex );
506 return connection_next(NULL, index);
509 Connection* connection_next( Connection *c, int *index )
511 assert( connections != NULL );
512 assert( index != NULL );
513 assert( *index <= dtblsize );
516 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
521 for(; *index < dtblsize; (*index)++) {
522 if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
523 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
531 if( connections[*index].c_struct_state == SLAP_C_USED ) {
532 assert( connections[*index].c_conn_state != SLAP_C_INVALID );
533 c = &connections[(*index)++];
537 assert( connections[*index].c_struct_state == SLAP_C_UNUSED );
538 assert( connections[*index].c_conn_state == SLAP_C_INVALID );
542 ldap_pvt_thread_mutex_lock( &c->c_mutex );
548 void connection_done( Connection *c )
550 assert( connections != NULL );
553 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
556 ldap_pvt_thread_mutex_unlock( &connections_mutex );
560 * connection_activity - handle the request operation op on connection
561 * conn. This routine figures out what kind of operation it is and
562 * calls the appropriate stub to handle it.
566 connection_operation( void *arg_v )
568 struct co_arg *arg = arg_v;
569 int tag = arg->co_op->o_tag;
570 Connection *conn = arg->co_conn;
573 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
575 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
580 do_bind( conn, arg->co_op );
584 case LDAP_REQ_UNBIND_30:
586 case LDAP_REQ_UNBIND:
587 do_unbind( conn, arg->co_op );
591 do_add( conn, arg->co_op );
595 case LDAP_REQ_DELETE_30:
597 case LDAP_REQ_DELETE:
598 do_delete( conn, arg->co_op );
601 case LDAP_REQ_MODRDN:
602 do_modrdn( conn, arg->co_op );
605 case LDAP_REQ_MODIFY:
606 do_modify( conn, arg->co_op );
609 case LDAP_REQ_COMPARE:
610 do_compare( conn, arg->co_op );
613 case LDAP_REQ_SEARCH:
614 do_search( conn, arg->co_op );
618 case LDAP_REQ_ABANDON_30:
620 case LDAP_REQ_ABANDON:
621 do_abandon( conn, arg->co_op );
625 Debug( LDAP_DEBUG_ANY, "unknown request 0x%lx\n",
626 arg->co_op->o_tag, 0, 0 );
631 ldap_pvt_thread_mutex_lock( &num_ops_mutex );
633 ldap_pvt_thread_mutex_unlock( &num_ops_mutex );
636 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
639 conn->c_n_ops_completed++;
642 slap_op_remove( &conn->c_ops, arg->co_op );
643 slap_op_free( arg->co_op );
646 free( (char *) arg );
651 case LDAP_REQ_UNBIND_30:
653 case LDAP_REQ_UNBIND:
654 /* c_mutex is locked */
655 connection_closing( conn );
659 if( conn->c_conn_state == SLAP_C_BINDING) {
660 conn->c_conn_state = SLAP_C_ACTIVE;
664 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
666 if( active_threads < 1 ) {
667 ldap_pvt_thread_cond_signal(&active_threads_cond);
669 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
671 connection_resched( conn );
673 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
678 int connection_read(int s)
682 assert( connections != NULL );
684 ldap_pvt_thread_mutex_lock( &connections_mutex );
686 /* get (locked) connection */
687 c = connection_get( s );
690 Debug( LDAP_DEBUG_ANY,
691 "connection_read(%d): no connection!\n",
696 ldap_pvt_thread_mutex_unlock( &connections_mutex );
700 if( c->c_conn_state == SLAP_C_CLOSING ) {
701 Debug( LDAP_DEBUG_TRACE,
702 "connection_read(%d): closing, ignoring input for id=%ld\n",
705 connection_return( c );
706 ldap_pvt_thread_mutex_unlock( &connections_mutex );
710 Debug( LDAP_DEBUG_TRACE,
711 "connection_read(%d): checking for input on id=%ld\n",
714 #define CONNECTION_INPUT_LOOP 1
716 #ifdef DATA_READY_LOOP
717 while(!rc && ber_pvt_sb_data_ready(&c->c_sb))
718 #elif CONNECTION_INPUT_LOOP
722 rc = connection_input( c );
726 Debug( LDAP_DEBUG_TRACE,
727 "connection_read(%d): input error=%d id=%ld, closing.\n",
728 s, rc, c->c_connid );
730 /* connections_mutex and c_mutex are locked */
731 connection_closing( c );
732 connection_close( c );
735 connection_return( c );
736 ldap_pvt_thread_mutex_unlock( &connections_mutex );
746 unsigned long tag, len;
750 if ( conn->c_currentber == NULL && (conn->c_currentber = ber_alloc())
752 Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
757 if ( (tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ))
758 != LDAP_TAG_MESSAGE )
762 Debug( LDAP_DEBUG_TRACE,
763 "ber_get_next on fd %d failed errno %d (%s)\n",
764 ber_pvt_sb_get_desc( conn->c_sb ), err,
765 err > -1 && err < sys_nerr ? sys_errlist[err] : "unknown" );
766 Debug( LDAP_DEBUG_TRACE,
767 "\t*** got %ld of %lu so far\n",
768 (long)(conn->c_currentber->ber_rwptr - conn->c_currentber->ber_buf),
769 conn->c_currentber->ber_len, 0 );
771 if ( err != EWOULDBLOCK && err != EAGAIN ) {
772 /* log, close and send error */
773 ber_free( conn->c_currentber, 1 );
774 conn->c_currentber = NULL;
781 ber = conn->c_currentber;
782 conn->c_currentber = NULL;
784 if ( (tag = ber_get_int( ber, &msgid )) != LDAP_TAG_MSGID ) {
785 /* log, close and send error */
786 Debug( LDAP_DEBUG_ANY, "ber_get_int returns 0x%lx\n", tag, 0,
792 if ( (tag = ber_peek_tag( ber, &len )) == LBER_ERROR ) {
793 /* log, close and send error */
794 Debug( LDAP_DEBUG_ANY, "ber_peek_tag returns 0x%lx\n", tag, 0,
802 if ( conn->c_version == 30 ) {
803 (void) ber_skip_tag( ber, &len );
807 op = slap_op_alloc( ber, msgid, tag, conn->c_n_ops_received++ );
809 if ( conn->c_conn_state == SLAP_C_BINDING
810 || conn->c_conn_state == SLAP_C_CLOSING )
812 Debug( LDAP_DEBUG_ANY, "deferring operation\n", 0, 0, 0 );
813 slap_op_add( &conn->c_pending_ops, op );
816 connection_op_activate( conn, op );
820 if ( conn->c_struct_state != SLAP_C_USED ) {
821 /* connection must have got closed underneath us */
825 assert( conn->c_struct_state == SLAP_C_USED );
831 connection_resched( Connection *conn )
835 if( conn->c_conn_state == SLAP_C_CLOSING ) {
836 Debug( LDAP_DEBUG_TRACE,
837 "connection_resched: attempting closing conn=%ld sd=%d.\n",
838 conn->c_connid, ber_pvt_sb_get_desc( conn->c_sb ), 0 );
840 connection_close( conn );
844 if( conn->c_conn_state != SLAP_C_ACTIVE ) {
845 /* other states need different handling */
849 for( op = slap_op_pop( &conn->c_pending_ops );
851 op = slap_op_pop( &conn->c_pending_ops ) )
853 /* pending operations should not be marked for abandonment */
854 assert(!op->o_abandon);
856 connection_op_activate( conn, op );
858 if ( conn->c_conn_state == SLAP_C_BINDING ) {
865 static int connection_op_activate( Connection *conn, Operation *op )
870 unsigned long tag = op->o_tag;
872 if ( conn->c_dn != NULL ) {
873 tmpdn = ch_strdup( conn->c_dn );
878 arg = (struct co_arg *) ch_malloc( sizeof(struct co_arg) );
882 arg->co_op->o_dn = ch_strdup( tmpdn != NULL ? tmpdn : "" );
883 arg->co_op->o_ndn = dn_normalize_case( ch_strdup( arg->co_op->o_dn ) );
885 slap_op_add( &conn->c_ops, arg->co_op );
887 if(tag == LDAP_REQ_BIND) {
888 conn->c_conn_state = SLAP_C_BINDING;
891 if ( tmpdn != NULL ) {
895 ldap_pvt_thread_mutex_lock( &active_threads_mutex );
897 ldap_pvt_thread_mutex_unlock( &active_threads_mutex );
899 status = ldap_pvt_thread_create( &arg->co_op->o_tid, 1,
900 connection_operation, (void *) arg );
903 Debug( LDAP_DEBUG_ANY,
904 "ldap_pvt_thread_create failed (%d)\n", status, 0, 0 );
906 /* should move op to pending list */
912 int connection_write(int s)
915 assert( connections != NULL );
917 ldap_pvt_thread_mutex_lock( &connections_mutex );
919 c = connection_get( s );
921 slapd_clr_write( s, 0);
924 Debug( LDAP_DEBUG_ANY,
925 "connection_write(%d): no connection!\n",
928 ldap_pvt_thread_mutex_unlock( &connections_mutex );
932 Debug( LDAP_DEBUG_TRACE,
933 "connection_write(%d): waking output for id=%ld\n",
936 ldap_pvt_thread_cond_signal( &c->c_write_cv );
938 connection_return( c );
939 ldap_pvt_thread_mutex_unlock( &connections_mutex );