From 75a983f546b39e9ec690f044b8e3ea58af1821c9 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Tue, 7 Nov 2006 05:25:01 +0000 Subject: [PATCH] Partial sync with HEAD, fix ITS#4736, #4709 --- servers/slapd/connection.c | 650 +++++++---------------- servers/slapd/daemon.c | 1024 ++++++++++++++++++++++++------------ servers/slapd/operation.c | 29 + servers/slapd/proto-slap.h | 5 +- servers/slapd/slap.h | 4 +- 5 files changed, 900 insertions(+), 812 deletions(-) diff --git a/servers/slapd/connection.c b/servers/slapd/connection.c index 7482ed4ce2..9b393fabab 100644 --- a/servers/slapd/connection.c +++ b/servers/slapd/connection.c @@ -43,33 +43,10 @@ #include "slapi/slapi.h" #endif -#ifdef SLAP_MULTI_CONN_ARRAY -/* for Multiple Connection Arrary (MCA) Support */ -static ldap_pvt_thread_mutex_t* connections_mutex; -static Connection **connections = NULL; - -/* set to the number of processors (round up to a power of 2) */ -# define NUM_CONNECTION_ARRAY 4 - -/* partition the array in a modulo manner */ -# define MCA_conn_array_id(fd) ((int)(fd)%NUM_CONNECTION_ARRAY) -# define MCA_conn_array_element_id(fd) ((int)(fd)/NUM_CONNECTION_ARRAY) -# define MCA_ARRAY_SIZE ((int)(MCA_conn_array_element_id(dtblsize) + (MCA_conn_array_id(dtblsize) ? 1 : 0))) -# define MCA_conn_check(fd) (dtblsize > 0 && (fd) >= 0 && (fd) < (MCA_ARRAY_SIZE*NUM_CONNECTION_ARRAY)) -# define MCA_GET_CONNECTION(fd) (&(connections[MCA_conn_array_id(fd)]) \ - [MCA_conn_array_element_id(fd)]) -# define MCA_GET_CONN_MUTEX(fd) (&connections_mutex[MCA_conn_array_id(fd)]) - -#else /* protected by connections_mutex */ static ldap_pvt_thread_mutex_t connections_mutex; static Connection *connections = NULL; -# define MCA_conn_check(fd) (dtblsize > 0 && (fd) < dtblsize) -# define MCA_GET_CONNECTION(fd) (&connections[s]) -# define MCA_GET_CONN_MUTEX(fd) (&connections_mutex) -#endif - static ldap_pvt_thread_mutex_t conn_nextid_mutex; static unsigned long conn_nextid = 0; @@ -79,6 +56,7 @@ static const char conn_lost_str[] = "connection lost"; #define SLAP_C_UNINITIALIZED 0x00 /* MUST BE ZERO (0) */ #define SLAP_C_UNUSED 0x01 #define SLAP_C_USED 0x02 +#define SLAP_C_PENDING 0x03 /* connection state (protected by c_mutex ) */ #define SLAP_C_INVALID 0x00 /* MUST BE ZERO (0) */ @@ -134,69 +112,6 @@ static ldap_pvt_thread_start_t connection_operation; * Initialize connection management infrastructure. */ int connections_init(void) -#ifdef SLAP_MULTI_CONN_ARRAY -{ - int i, j; - Connection* conn; - - assert( connections == NULL ); - - if( connections != NULL) { - Debug( LDAP_DEBUG_ANY, "connections_init: already initialized.\n", - 0, 0, 0 ); - return -1; - } - - connections_mutex = (ldap_pvt_thread_mutex_t*) ch_calloc( - NUM_CONNECTION_ARRAY, sizeof(ldap_pvt_thread_mutex_t) ); - if( connections_mutex == NULL ) { - Debug( LDAP_DEBUG_ANY, "connections_init: " - "allocation of connection mutexes failed\n", 0, 0, 0 ); - return -1; - } - - connections = (Connection**) ch_calloc( - NUM_CONNECTION_ARRAY, sizeof(Connection*)); - if( connections == NULL ) { - Debug( LDAP_DEBUG_ANY, "connections_init: " - "allocation of connection[%d] failed\n", 0, 0, 0 ); - return -1; - } - - for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) { - ldap_pvt_thread_mutex_init( connections_mutex+i ); - connections[i] = (Connection*) ch_calloc( - MCA_ARRAY_SIZE, sizeof(Connection) ); - if( connections[i] == NULL ) { - Debug( LDAP_DEBUG_ANY, "connections_init: " - "allocation (%d*%ld) of connection array[%d] failed\n", - dtblsize, (long) sizeof(Connection), i ); - return -1; - } - } - - /* should check return of every call */ - ldap_pvt_thread_mutex_init( &conn_nextid_mutex ); - - assert( connections[0]->c_struct_state == SLAP_C_UNINITIALIZED ); - assert( connections[NUM_CONNECTION_ARRAY-1]->c_struct_state == - SLAP_C_UNINITIALIZED ); - - for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) { - conn = connections[i]; - for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) { - conn[j].c_conn_idx = j; - } - } - - /* - * per entry initialization of the Connection array initialization - * will be done by connection_init() - */ - - return 0; -} -#else { int i; @@ -233,58 +148,12 @@ int connections_init(void) return 0; } -#endif /* * Destroy connection management infrastructure. */ int connections_destroy(void) -#ifdef SLAP_MULTI_CONN_ARRAY -{ - int i; - ber_socket_t j; - - if( connections == NULL) { - Debug( LDAP_DEBUG_ANY, "connections_destroy: nothing to destroy.\n", - 0, 0, 0 ); - return -1; - } - - for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) { - Connection* conn = connections[i]; - for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) { - if( conn[j].c_struct_state != SLAP_C_UNINITIALIZED ) { - ber_sockbuf_free( conn[j].c_sb ); - ldap_pvt_thread_mutex_destroy( &conn[j].c_mutex ); - ldap_pvt_thread_mutex_destroy( &conn[j].c_write_mutex ); - ldap_pvt_thread_cond_destroy( &conn[j].c_write_cv ); -#ifdef LDAP_SLAPI - /* FIX ME!! */ - if ( slapi_plugins_used ) { - slapi_int_free_object_extensions( SLAPI_X_EXT_CONNECTION, - &connections[i] ); - } -#endif - } - } - } - - for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) { - free( connections[i] ); - connections[i] = NULL; - ldap_pvt_thread_mutex_destroy( &connections_mutex[i] ); - } - - free( connections ); - free( connections_mutex ); - - ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex ); - - return 0; - -} -#else { ber_socket_t i; @@ -318,50 +187,14 @@ int connections_destroy(void) ldap_pvt_thread_mutex_destroy( &conn_nextid_mutex ); return 0; } -#endif /* * shutdown all connections */ int connections_shutdown(void) -#ifdef SLAP_MULTI_CONN_ARRAY -{ - int i; - ber_socket_t j; - - for ( i = 0; i < NUM_CONNECTION_ARRAY; i++ ) { - Connection* conn = connections[i]; - ldap_pvt_thread_mutex_lock( &connections_mutex[i] ); - for ( j = 0; j < MCA_ARRAY_SIZE; j++ ) { - if( conn[j].c_struct_state != SLAP_C_USED ) { - continue; - } - /* give persistent clients a chance to cleanup */ - if( conn[j].c_conn_state == SLAP_C_CLIENT ) { - ldap_pvt_thread_pool_submit( &connection_pool, - conn[j].c_clientfunc, conn[j].c_clientarg ); - continue; - } - - ldap_pvt_thread_mutex_lock( &conn[j].c_mutex ); - /* connections_mutex and c_mutex are locked */ - connection_closing( &conn[j], "connection shutdown" ); - connection_close( &conn[j] ); - ldap_pvt_thread_mutex_unlock( &conn[j].c_mutex ); - } - - ldap_pvt_thread_mutex_unlock( &connections_mutex[i] ); - } - - return 0; - -} -#else { ber_socket_t i; - ldap_pvt_thread_mutex_lock( &connections_mutex ); - for ( i = 0; i < dtblsize; i++ ) { if( connections[i].c_struct_state != SLAP_C_USED ) { continue; @@ -375,18 +208,15 @@ int connections_shutdown(void) ldap_pvt_thread_mutex_lock( &connections[i].c_mutex ); - /* connections_mutex and c_mutex are locked */ + /* c_mutex is locked */ connection_closing( &connections[i], "slapd shutdown" ); connection_close( &connections[i] ); ldap_pvt_thread_mutex_unlock( &connections[i].c_mutex ); } - ldap_pvt_thread_mutex_unlock( &connections_mutex ); - return 0; } -#endif /* * Timeout idle connections. @@ -421,8 +251,6 @@ int connections_timeout_idle(time_t now) static Connection* connection_get( ber_socket_t s ) { - /* connections_mutex should be locked by caller */ - Connection *c; Debug( LDAP_DEBUG_ARGS, @@ -434,17 +262,19 @@ static Connection* connection_get( ber_socket_t s ) if(s == AC_SOCKET_INVALID) return NULL; #ifndef HAVE_WINSOCK - assert( MCA_conn_check( s ) ); - c = MCA_GET_CONNECTION(s); - - assert( c->c_struct_state != SLAP_C_UNINITIALIZED ); + assert( s < dtblsize ); + c = &connections[s]; #else c = NULL; { ber_socket_t i, sd; + ldap_pvt_thread_mutex_lock( &connections_mutex ); for(i=0; ic_struct_state != SLAP_C_UNINITIALIZED ); + ldap_pvt_thread_mutex_lock( &c->c_mutex ); ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd ); @@ -529,6 +362,7 @@ long connection_init( { unsigned long id; Connection *c; + int doinit = 0; assert( connections != NULL ); @@ -549,24 +383,29 @@ long connection_init( assert( s >= 0 ); #ifndef HAVE_WINSOCK assert( s < dtblsize ); -#endif - - ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) ); - -#ifndef HAVE_WINSOCK - assert( MCA_conn_check( s ) ); - c = MCA_GET_CONNECTION(s); + c = &connections[s]; + if( c->c_struct_state == SLAP_C_UNINITIALIZED ) { + doinit = 1; + } else { + assert( c->c_struct_state == SLAP_C_UNUSED ); + } #else { ber_socket_t i; c = NULL; + ldap_pvt_thread_mutex_lock( &connections_mutex ); for( i=0; i < dtblsize; i++) { ber_socket_t sd; + if ( connections[i].c_struct_state == SLAP_C_PENDING ) + continue; + if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) { assert( connections[i].c_sb == 0 ); c = &connections[i]; + c->c_struct_state = SLAP_C_PENDING; + doinit = 1; break; } @@ -579,6 +418,7 @@ long connection_init( if( connections[i].c_struct_state == SLAP_C_UNUSED ) { assert( sd == AC_SOCKET_INVALID ); c = &connections[i]; + c->c_struct_state = SLAP_C_PENDING; break; } @@ -588,20 +428,18 @@ long connection_init( assert( connections[i].c_conn_state != SLAP_C_INVALID ); assert( sd != AC_SOCKET_INVALID ); } + ldap_pvt_thread_mutex_unlock( &connections_mutex ); if( c == NULL ) { Debug( LDAP_DEBUG_ANY, "connection_init(%d): connection table full " "(%d/%d)\n", s, i, dtblsize); - ldap_pvt_thread_mutex_unlock( &connections_mutex ); return -1; } } #endif - assert( c != NULL ); - - if( c->c_struct_state == SLAP_C_UNINITIALIZED ) { + if( doinit ) { c->c_send_ldap_result = slap_send_ldap_result; c->c_send_search_entry = slap_send_search_entry; c->c_send_search_reference = slap_send_search_reference; @@ -619,6 +457,12 @@ long connection_init( LDAP_STAILQ_INIT(&c->c_ops); LDAP_STAILQ_INIT(&c->c_pending_ops); +#ifdef LDAP_X_TXN + c->c_txn = CONN_TXN_INACTIVE; + c->c_txn_backend = NULL; + LDAP_STAILQ_INIT(&c->c_txn_ops); +#endif + BER_BVZERO( &c->c_sasl_bind_mech ); c->c_sasl_done = 0; c->c_sasl_authctx = NULL; @@ -645,13 +489,10 @@ long connection_init( slapi_int_create_object_extensions( SLAPI_X_EXT_CONNECTION, c ); } #endif - - c->c_struct_state = SLAP_C_UNUSED; } ldap_pvt_thread_mutex_lock( &c->c_mutex ); - assert( c->c_struct_state == SLAP_C_UNUSED ); assert( BER_BVISNULL( &c->c_authmech ) ); assert( BER_BVISNULL( &c->c_dn ) ); assert( BER_BVISNULL( &c->c_ndn ) ); @@ -660,6 +501,11 @@ long connection_init( assert( BER_BVISNULL( &c->c_peer_name ) ); assert( LDAP_STAILQ_EMPTY(&c->c_ops) ); assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) ); +#ifdef LDAP_X_TXN + assert( c->c_txn == CONN_TXN_INACTIVE ); + assert( c->c_txn_backend == NULL ); + assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) ); +#endif assert( BER_BVISNULL( &c->c_sasl_bind_mech ) ); assert( c->c_sasl_done == 0 ); assert( c->c_sasl_authctx == NULL ); @@ -677,7 +523,6 @@ long connection_init( c->c_close_reason = "?"; /* should never be needed */ ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s ); ldap_pvt_thread_mutex_unlock( &c->c_mutex ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); return 0; } @@ -766,7 +611,6 @@ long connection_init( slapd_add_internal( s, 1 ); ldap_pvt_thread_mutex_unlock( &c->c_mutex ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); backend_connection_init(c); @@ -809,22 +653,32 @@ void connection2anonymous( Connection *c ) static void connection_destroy( Connection *c ) { - /* note: connections_mutex should be locked by caller */ ber_socket_t sd; unsigned long connid; const char *close_reason; + Sockbuf *sb; assert( connections != NULL ); assert( c != NULL ); assert( c->c_struct_state != SLAP_C_UNUSED ); assert( c->c_conn_state != SLAP_C_INVALID ); assert( LDAP_STAILQ_EMPTY(&c->c_ops) ); + assert( LDAP_STAILQ_EMPTY(&c->c_pending_ops) ); +#ifdef LDAP_X_TXN + assert( c->c_txn == CONN_TXN_INACTIVE ); + assert( c->c_txn_backend == NULL ); + assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) ); +#endif assert( c->c_writewaiter == 0); /* only for stats (print -1 as "%lu" may give unexpected results ;) */ connid = c->c_connid; close_reason = c->c_close_reason; + ldap_pvt_thread_mutex_lock( &connections_mutex ); + c->c_struct_state = SLAP_C_PENDING; + ldap_pvt_thread_mutex_unlock( &connections_mutex ); + backend_connection_destroy(c); c->c_protocol = 0; @@ -857,37 +711,38 @@ connection_destroy( Connection *c ) c->c_currentber = NULL; } - ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd ); - slapd_sd_lock(); - ber_sockbuf_free( c->c_sb ); - if ( sd != AC_SOCKET_INVALID ) { - slapd_remove( sd, 1, 0, 1 ); - Statslog( LDAP_DEBUG_STATS, (close_reason - ? "conn=%lu fd=%ld closed (%s)\n" - : "conn=%lu fd=%ld closed\n"), - connid, (long) sd, close_reason, 0, 0 ); - } else { - slapd_sd_unlock(); +#ifdef LDAP_SLAPI + /* call destructors, then constructors; avoids unnecessary allocation */ + if ( slapi_plugins_used ) { + slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c ); } +#endif - c->c_sb = ber_sockbuf_alloc( ); + c->c_conn_state = SLAP_C_INVALID; + c->c_struct_state = SLAP_C_UNUSED; + c->c_close_reason = "?"; /* should never be needed */ + sb = c->c_sb; + c->c_sb = ber_sockbuf_alloc( ); { ber_len_t max = sockbuf_max_incoming; ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max ); } - c->c_conn_state = SLAP_C_INVALID; - c->c_struct_state = SLAP_C_UNUSED; - c->c_close_reason = "?"; /* should never be needed */ + ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd ); -#ifdef LDAP_SLAPI - /* call destructors, then constructors; avoids unnecessary allocation */ - if ( slapi_plugins_used ) { - slapi_int_clear_object_extensions( SLAPI_X_EXT_CONNECTION, c ); + /* c must be fully reset by this point; when we call slapd_remove + * it may get immediately reused by a new connection. + */ + if ( sd != AC_SOCKET_INVALID ) { + slapd_remove( sd, sb, 1, 0, 0 ); + + Statslog( LDAP_DEBUG_STATS, (close_reason + ? "conn=%lu fd=%ld closed (%s)\n" + : "conn=%lu fd=%ld closed\n"), + connid, (long) sd, close_reason, 0, 0 ); } -#endif } int connection_state_closing( Connection *c ) @@ -917,6 +772,7 @@ static void connection_abandon( Connection *c ) op.o_conn = c; op.o_connid = c->c_connid; op.o_tag = LDAP_REQ_ABANDON; + for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) { next = LDAP_STAILQ_NEXT( o, o_next ); op.orn_msgid = o->o_msgid; @@ -925,6 +781,19 @@ static void connection_abandon( Connection *c ) frontendDB->be_abandon( &op, &rs ); } +#ifdef LDAP_X_TXN + /* remove operations in pending transaction */ + while ( (o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL) { + LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next ); + LDAP_STAILQ_NEXT(o, o_next) = NULL; + slap_op_free( o ); + } + + /* clear transaction */ + c->c_txn_backend = NULL; + c->c_txn = CONN_TXN_INACTIVE; +#endif + /* remove pending operations */ while ( (o = LDAP_STAILQ_FIRST( &c->c_pending_ops )) != NULL) { LDAP_STAILQ_REMOVE_HEAD( &c->c_pending_ops, o_next ); @@ -954,38 +823,60 @@ void connection_closing( Connection *c, const char *why ) c->c_close_reason = why; /* don't listen on this port anymore */ - slapd_clr_read( sd, 1 ); + slapd_clr_read( sd, 0 ); /* abandon active operations */ connection_abandon( c ); /* wake write blocked operations */ - slapd_clr_write( sd, 1 ); if ( c->c_writewaiter ) { ldap_pvt_thread_cond_signal( &c->c_write_cv ); + /* ITS#4667 this may allow another thread to drop into + * connection_resched / connection_close before we + * finish, but that's OK. + */ + slapd_clr_write( sd, 1 ); ldap_pvt_thread_mutex_unlock( &c->c_mutex ); - ldap_pvt_thread_yield(); + ldap_pvt_thread_mutex_lock( &c->c_write_mutex ); ldap_pvt_thread_mutex_lock( &c->c_mutex ); + ldap_pvt_thread_mutex_unlock( &c->c_write_mutex ); + } else { + slapd_clr_write( sd, 1 ); } + } else if( why == NULL && c->c_close_reason == conn_lost_str ) { /* Client closed connection after doing Unbind. */ c->c_close_reason = NULL; } } -static void connection_close( Connection *c ) +static void +connection_close( Connection *c ) { - ber_socket_t sd; + ber_socket_t sd = AC_SOCKET_INVALID; assert( connections != NULL ); assert( c != NULL ); + + /* ITS#4667 we may have gotten here twice */ + if ( c->c_conn_state == SLAP_C_INVALID ) + return; + assert( c->c_struct_state == SLAP_C_USED ); assert( c->c_conn_state == SLAP_C_CLOSING ); - /* note: connections_mutex and c_mutex should be locked by caller */ + /* NOTE: c_mutex should be locked by caller */ - ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd ); - if( !LDAP_STAILQ_EMPTY(&c->c_ops) ) { + /* NOTE: don't get the file descriptor if not needed */ +#ifdef LDAP_DEBUG + if ( slap_debug & LDAP_DEBUG_TRACE ) { + ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_GET_FD, &sd ); + } +#endif /* LDAP_DEBUG */ + + if ( !LDAP_STAILQ_EMPTY(&c->c_ops) || + !LDAP_STAILQ_EMPTY(&c->c_pending_ops) ) + { Debug( LDAP_DEBUG_TRACE, "connection_close: deferring conn=%lu sd=%d\n", c->c_connid, sd, 0 ); @@ -994,6 +885,7 @@ static void connection_close( Connection *c ) Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n", c->c_connid, sd, 0 ); + connection_destroy( c ); } @@ -1013,71 +905,21 @@ unsigned long connections_nextid(void) Connection* connection_first( ber_socket_t *index ) { -#ifdef SLAP_MULTI_CONN_ARRAY - int conn_array_id; -#endif - assert( connections != NULL ); assert( index != NULL ); -#ifdef SLAP_MULTI_CONN_ARRAY - for ( conn_array_id = 0; - conn_array_id < NUM_CONNECTION_ARRAY; - conn_array_id++ ) - { - ldap_pvt_thread_mutex_lock( &connections_mutex[ conn_array_id ] ); - } -#else ldap_pvt_thread_mutex_lock( &connections_mutex ); -#endif - - *index = 0; - - return connection_next(NULL, index); -} - -Connection* connection_next( Connection *c, ber_socket_t *index ) -#ifdef SLAP_MULTI_CONN_ARRAY -{ - Connection* conn; - - assert( connections != NULL ); - assert( index != NULL ); - assert( *index >= 0 && *index <= dtblsize ); - - if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex ); - - c = NULL; - - for(; *index < dtblsize; (*index)++) { - assert( MCA_conn_check( *index ) ); - conn = MCA_GET_CONNECTION(*index); - if( conn->c_struct_state == SLAP_C_UNINITIALIZED ) { - assert( conn->c_conn_state == SLAP_C_INVALID ); -#ifndef HAVE_WINSOCK - continue; -#else - break; -#endif - } - - if( conn->c_struct_state == SLAP_C_USED ) { - assert( conn->c_conn_state != SLAP_C_INVALID ); - c = conn; - (*index)++; + for( *index = 0; *index < dtblsize; (*index)++) { + if( connections[*index].c_struct_state != SLAP_C_UNINITIALIZED ) { break; } - - assert( conn->c_struct_state == SLAP_C_UNUSED ); - assert( conn->c_conn_state == SLAP_C_INVALID ); } + ldap_pvt_thread_mutex_unlock( &connections_mutex ); - if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex ); - - return c; - + return connection_next(NULL, index); } -#else + +Connection* connection_next( Connection *c, ber_socket_t *index ) { assert( connections != NULL ); assert( index != NULL ); @@ -1087,51 +929,51 @@ Connection* connection_next( Connection *c, ber_socket_t *index ) c = NULL; + ldap_pvt_thread_mutex_lock( &connections_mutex ); for(; *index < dtblsize; (*index)++) { + int c_struct; if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) { assert( connections[*index].c_conn_state == SLAP_C_INVALID ); -#ifndef HAVE_WINSOCK - continue; -#else +#ifdef HAVE_WINSOCK break; +#else + continue; #endif } if( connections[*index].c_struct_state == SLAP_C_USED ) { assert( connections[*index].c_conn_state != SLAP_C_INVALID ); c = &connections[(*index)++]; + if ( ldap_pvt_thread_mutex_trylock( &c->c_mutex )) { + /* avoid deadlock */ + ldap_pvt_thread_mutex_unlock( &connections_mutex ); + ldap_pvt_thread_mutex_lock( &c->c_mutex ); + ldap_pvt_thread_mutex_lock( &connections_mutex ); + if ( c->c_struct_state != SLAP_C_USED ) { + ldap_pvt_thread_mutex_unlock( &c->c_mutex ); + c = NULL; + continue; + } + } break; } - assert( connections[*index].c_struct_state == SLAP_C_UNUSED ); + c_struct = connections[*index].c_struct_state; + if ( c_struct == SLAP_C_PENDING ) + continue; + assert( c_struct == SLAP_C_UNUSED ); assert( connections[*index].c_conn_state == SLAP_C_INVALID ); } - if( c != NULL ) ldap_pvt_thread_mutex_lock( &c->c_mutex ); + ldap_pvt_thread_mutex_unlock( &connections_mutex ); return c; } -#endif void connection_done( Connection *c ) { -#ifdef SLAP_MULTI_CONN_ARRAY - int conn_array_id; -#endif - assert( connections != NULL ); if( c != NULL ) ldap_pvt_thread_mutex_unlock( &c->c_mutex ); - -#ifdef SLAP_MULTI_CONN_ARRAY - for ( conn_array_id = 0; - conn_array_id < NUM_CONNECTION_ARRAY; - conn_array_id++ ) - { - ldap_pvt_thread_mutex_unlock( &connections_mutex[ conn_array_id ] ); - } -#else - ldap_pvt_thread_mutex_unlock( &connections_mutex ); -#endif } /* @@ -1168,7 +1010,7 @@ void connection_done( Connection *c ) /* * NOTE: keep in sync with enum in slapd.h */ -static int (*opfun[])( Operation *op, SlapReply *rs ) = { +static BI_op_func *opfun[] = { do_bind, do_unbind, do_add, @@ -1189,7 +1031,7 @@ connection_operation( void *ctx, void *arg_v ) Operation *op = arg_v; SlapReply rs = {REP_RESULT}; ber_tag_t tag = op->o_tag; - int opidx = -1; + slap_op_t opidx = SLAP_OP_LAST; Connection *conn = op->o_conn; void *memctx = NULL; void *memctx_null = NULL; @@ -1201,6 +1043,9 @@ connection_operation( void *ctx, void *arg_v ) ldap_pvt_thread_mutex_unlock( &slap_counters.sc_ops_mutex ); op->o_threadctx = ctx; +#ifdef LDAP_DEVEL + op->o_tid = ldap_pvt_thread_pool_tid( ctx ); +#endif /* LDAP_DEVEL */ switch ( tag ) { case LDAP_REQ_BIND: @@ -1236,6 +1081,20 @@ connection_operation( void *ctx, void *arg_v ) goto operations_error; } +#ifdef LDAP_X_TXN + if (( conn->c_txn == CONN_TXN_SPECIFY ) && ( + ( tag == LDAP_REQ_ADD ) || + ( tag == LDAP_REQ_DELETE ) || + ( tag == LDAP_REQ_MODIFY ) || + ( tag == LDAP_REQ_MODRDN ))) + { + /* Disable SLAB allocator for all update operations + issued inside of a transaction */ + op->o_tmpmemctx = NULL; + op->o_tmpmfuncs = &ch_mfuncs; + } else +#endif + { /* We can use Thread-Local storage for most mallocs. We can * also use TL for ber parsing, but not on Add or Modify. */ @@ -1255,54 +1114,10 @@ connection_operation( void *ctx, void *arg_v ) */ ber_set_option( op->o_ber, LBER_OPT_BER_MEMCTX, &memctx ); } - - switch ( tag ) { - case LDAP_REQ_BIND: - opidx = SLAP_OP_BIND; - break; - - case LDAP_REQ_UNBIND: - opidx = SLAP_OP_UNBIND; - break; - - case LDAP_REQ_ADD: - opidx = SLAP_OP_ADD; - break; - - case LDAP_REQ_DELETE: - opidx = SLAP_OP_DELETE; - break; - - case LDAP_REQ_MODRDN: - opidx = SLAP_OP_MODRDN; - break; - - case LDAP_REQ_MODIFY: - opidx = SLAP_OP_MODIFY; - break; - - case LDAP_REQ_COMPARE: - opidx = SLAP_OP_COMPARE; - break; - - case LDAP_REQ_SEARCH: - opidx = SLAP_OP_SEARCH; - break; - - case LDAP_REQ_ABANDON: - opidx = SLAP_OP_ABANDON; - break; - - case LDAP_REQ_EXTENDED: - opidx = SLAP_OP_EXTENDED; - break; - - default: - /* not reachable */ - assert( 0 ); } - assert( opidx > -1 ); + opidx = slap_req2op( tag ); + assert( opidx != SLAP_OP_LAST ); INCR_OP_INITIATED( opidx ); rc = (*(opfun[opidx]))( op, &rs ); @@ -1310,7 +1125,7 @@ operations_error: if ( rc == SLAPD_DISCONNECT ) { tag = LBER_ERROR; - } else if ( opidx > -1 ) { + } else if ( opidx != SLAP_OP_LAST ) { /* increment completed operations count * only if operation was initiated * and rc != SLAPD_DISCONNECT */ @@ -1324,6 +1139,7 @@ operations_error: op->o_cancel = LDAP_TOO_LATE; } } + while ( op->o_cancel != SLAP_CANCEL_NONE && op->o_cancel != SLAP_CANCEL_DONE ) { @@ -1388,6 +1204,7 @@ void connection_client_stop( ber_socket_t s ) { Connection *c; + Sockbuf *sb; /* get (locked) connection */ c = connection_get( s ); @@ -1398,14 +1215,13 @@ void connection_client_stop( c->c_conn_state = SLAP_C_INVALID; c->c_struct_state = SLAP_C_UNUSED; c->c_close_reason = "?"; /* should never be needed */ - slapd_sd_lock(); - ber_sockbuf_free( c->c_sb ); - slapd_remove( s, 0, 1, 1 ); + sb = c->c_sb; c->c_sb = ber_sockbuf_alloc( ); { ber_len_t max = sockbuf_max_incoming; ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_MAX_INCOMING, &max ); } + slapd_remove( s, sb, 0, 1, 0 ); connection_return( c ); } @@ -1477,8 +1293,6 @@ int connection_read(ber_socket_t s) assert( connections != NULL ); - ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) ); - /* get (locked) connection */ c = connection_get( s ); @@ -1487,7 +1301,6 @@ int connection_read(ber_socket_t s) "connection_read(%ld): no connection!\n", (long) s, 0, 0 ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); return -1; } @@ -1497,12 +1310,7 @@ int connection_read(ber_socket_t s) Debug( LDAP_DEBUG_TRACE, "connection_read(%d): closing, ignoring input for id=%lu\n", s, c->c_connid, 0 ); - -#ifdef SLAP_LIGHTWEIGHT_DISPATCHER - slapd_set_read( s, 1 ); -#endif connection_return( c ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); return 0; } @@ -1517,7 +1325,6 @@ int connection_read(ber_socket_t s) c->c_clientfunc, c->c_clientarg ); #endif connection_return( c ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); return 0; } @@ -1535,31 +1342,10 @@ int connection_read(ber_socket_t s) s, rc, c->c_connid ); c->c_needs_tls_accept = 0; - /* connections_mutex and c_mutex are locked */ + /* c_mutex is locked */ connection_closing( c, "TLS negotiation failure" ); - -#if 0 - { - struct timeval tv; - fd_set rfd; - /* Drain input before close, to allow SSL error codes - * to propagate to client. */ - FD_ZERO(&rfd); - FD_SET(s, &rfd); - for (rc=1; rc>0;) { - tv.tv_sec = 1; - tv.tv_usec = 0; - rc = select(s+1, &rfd, NULL, NULL, &tv); - if (rc == 1) { - ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DRAIN, NULL); - } - } - } -#endif - connection_close( c ); connection_return( c ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); return 0; } else if ( rc == 0 ) { @@ -1590,15 +1376,13 @@ int connection_read(ber_socket_t s) } /* if success and data is ready, fall thru to data input loop */ - if( rc != 0 || - !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) ) + if( !ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_DATA_READY, NULL ) ) { #ifdef SLAP_LIGHTWEIGHT_DISPATCHER slapd_set_read( s, 1 ); #endif connection_return( c ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); return 0; } } @@ -1613,7 +1397,6 @@ int connection_read(ber_socket_t s) #endif connection_return( c ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); return 0; } @@ -1626,11 +1409,10 @@ int connection_read(ber_socket_t s) "error=%d id=%lu, closing\n", s, rc, c->c_connid ); - /* connections_mutex and c_mutex are locked */ + /* c_mutex is locked */ connection_closing( c, "SASL layer install failure" ); connection_close( c ); connection_return( c ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); return 0; } } @@ -1656,15 +1438,14 @@ int connection_read(ber_socket_t s) #endif if( rc < 0 ) { - Debug( LDAP_DEBUG_TRACE, + Debug( LDAP_DEBUG_CONNS, "connection_read(%d): input error=%d id=%lu, closing.\n", s, rc, c->c_connid ); - /* connections_mutex and c_mutex are locked */ + /* c_mutex is locked */ connection_closing( c, conn_lost_str ); connection_close( c ); connection_return( c ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); return 0; } @@ -1685,7 +1466,6 @@ int connection_read(ber_socket_t s) #endif connection_return( c ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); return 0; } @@ -1716,7 +1496,7 @@ connection_input( Connection *conn ) return -1; } - errno = 0; + sock_errset(0); #ifdef LDAP_CONNECTIONLESS if ( conn->c_is_udp ) { @@ -1736,7 +1516,7 @@ connection_input( Connection *conn ) tag = ber_get_next( conn->c_sb, &len, conn->c_currentber ); if ( tag != LDAP_TAG_MESSAGE ) { - int err = errno; + int err = sock_errno(); ber_socket_t sd; ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd ); @@ -1922,39 +1702,13 @@ connection_resched( Connection *conn ) Operation *op; if( conn->c_conn_state == SLAP_C_CLOSING ) { - int rc; ber_socket_t sd; ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd ); - /* use trylock to avoid possible deadlock */ - rc = ldap_pvt_thread_mutex_trylock( MCA_GET_CONN_MUTEX( sd ) ); - - if( rc ) { - Debug( LDAP_DEBUG_TRACE, - "connection_resched: reaquiring locks conn=%lu sd=%d\n", - conn->c_connid, sd, 0 ); - /* - * reaquire locks in the right order... - * this may allow another thread to close this connection, - * so recheck state below. - */ - ldap_pvt_thread_mutex_unlock( &conn->c_mutex ); - ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX ( sd ) ); - ldap_pvt_thread_mutex_lock( &conn->c_mutex ); - } - - if( conn->c_conn_state != SLAP_C_CLOSING ) { - Debug( LDAP_DEBUG_TRACE, "connection_resched: " - "closed by other thread conn=%lu sd=%d\n", - conn->c_connid, sd, 0 ); - } else { - Debug( LDAP_DEBUG_TRACE, "connection_resched: " - "attempting closing conn=%lu sd=%d\n", - conn->c_connid, sd, 0 ); - connection_close( conn ); - } - - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( sd ) ); + Debug( LDAP_DEBUG_TRACE, "connection_resched: " + "attempting closing conn=%lu sd=%d\n", + conn->c_connid, sd, 0 ); + connection_close( conn ); return 0; } @@ -2004,9 +1758,6 @@ static int connection_bind_cleanup_cb( Operation *op, SlapReply *rs ) static int connection_bind_cb( Operation *op, SlapReply *rs ) { - slap_callback *cb = op->o_callback; - op->o_callback = cb->sc_next; - ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex ); op->o_conn->c_conn_state = SLAP_C_ACTIVE; op->o_conn->c_sasl_bind_in_progress = @@ -2059,7 +1810,7 @@ static int connection_bind_cb( Operation *op, SlapReply *rs ) } ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex ); - ch_free( cb ); + ch_free( op->o_callback ); op->o_callback = NULL; return SLAP_CB_CONTINUE; @@ -2128,37 +1879,6 @@ static int connection_op_activate( Operation *op ) return rc; } -#ifdef SLAP_LIGHTWEIGHT_DISPATCHER -static int connection_write( ber_socket_t s ); -static void *connection_write_thread( void *ctx, void *arg ) -{ - return (void *)(long)connection_write((long)arg); -} - -int connection_write_activate( ber_socket_t s ) -{ - int rc; - - /* - * suspend reading on this file descriptor until a connection processing - * thread write data on it. Otherwise the listener thread will repeatedly - * submit the same event on it to the pool. - */ - slapd_clr_write( s, 0); - - rc = ldap_pvt_thread_pool_submit( &connection_pool, - connection_write_thread, (void *)(long)s ); - - if( rc != 0 ) { - Debug( LDAP_DEBUG_ANY, - "connection_write_activate(%d): submit failed (%d)\n", - (int) s, rc, 0 ); - } - return rc; -} - -static -#endif int connection_write(ber_socket_t s) { Connection *c; @@ -2166,20 +1886,16 @@ int connection_write(ber_socket_t s) assert( connections != NULL ); - ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX( s ) ); + slapd_clr_write( s, 0 ); c = connection_get( s ); if( c == NULL ) { Debug( LDAP_DEBUG_ANY, "connection_write(%ld): no connection!\n", (long)s, 0, 0 ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX( s ) ); return -1; } -#ifndef SLAP_LIGHTWEIGHT_DISPATCHER - slapd_clr_write( s, 0); -#endif c->c_n_write++; Debug( LDAP_DEBUG_TRACE, @@ -2216,7 +1932,6 @@ int connection_write(ber_socket_t s) } connection_return( c ); - ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) ); return 0; } @@ -2241,6 +1956,9 @@ connection_fake_init( op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx); op->o_tmpmfuncs = &slap_sl_mfuncs; op->o_threadctx = ctx; +#ifdef LDAP_DEVEL + op->o_tid = ldap_pvt_thread_pool_tid( ctx ); +#endif /* LDAP_DEVEL */ op->o_conn = conn; op->o_connid = op->o_conn->c_connid; diff --git a/servers/slapd/daemon.c b/servers/slapd/daemon.c index 6b98e105ef..d9ea1fd04d 100644 --- a/servers/slapd/daemon.c +++ b/servers/slapd/daemon.c @@ -42,7 +42,12 @@ #if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL) # include -#endif +#elif defined(SLAP_X_DEVPOLL) && defined(HAVE_SYS_DEVPOLL_H) && defined(HAVE_DEVPOLL) +# include +# include +# include +# include +#endif /* ! epoll && ! /dev/poll */ #ifdef HAVE_TCPD # include @@ -62,9 +67,9 @@ int deny_severity = LOG_NOTICE; #ifdef LDAP_PF_INET6 int slap_inet4or6 = AF_UNSPEC; -#else +#else /* ! INETv6 */ int slap_inet4or6 = AF_INET; -#endif +#endif /* ! INETv6 */ /* globals */ time_t starttime; @@ -76,7 +81,7 @@ Listener **slap_listeners = NULL; #ifndef SLAPD_LISTEN_BACKLOG #define SLAPD_LISTEN_BACKLOG 1024 -#endif +#endif /* ! SLAPD_LISTEN_BACKLOG */ static ber_socket_t wake_sds[2]; static int emfile; @@ -87,14 +92,14 @@ static volatile int waking; if ((w) && ++waking < 5) { \ tcp_write( wake_sds[1], "0", 1 ); \ } \ -} while(0) -#else +} while (0) +#else /* ! NO_THREADS */ #define WAKE_LISTENER(w) do { \ if (w) { \ tcp_write( wake_sds[1], "0", 1 ); \ } \ -} while(0) -#endif +} while (0) +#endif /* ! NO_THREADS */ volatile sig_atomic_t slapd_shutdown = 0; volatile sig_atomic_t slapd_gentle_shutdown = 0; @@ -103,89 +108,110 @@ volatile sig_atomic_t slapd_abrupt_shutdown = 0; static struct slap_daemon { ldap_pvt_thread_mutex_t sd_mutex; #ifdef HAVE_TCPD - ldap_pvt_thread_mutex_t tcpd_mutex; -#endif - - ber_socket_t sd_nactives; - int sd_nwriters; - -#ifdef HAVE_EPOLL - struct epoll_event *sd_epolls; - int sd_nepolls; - int *sd_index; - int sd_epfd; - int sd_nfds; -#else + ldap_pvt_thread_mutex_t sd_tcpd_mutex; +#endif /* TCP Wrappers */ + + ber_socket_t sd_nactives; + int sd_nwriters; + +#if defined(HAVE_EPOLL) + struct epoll_event *sd_epolls; + int *sd_index; + int sd_epfd; + int sd_nfds; +#elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL) + /* eXperimental */ + struct pollfd *sd_pollfd; + int *sd_index; + Listener **sd_l; + int sd_dpfd; + int sd_nfds; +#else /* ! epoll && ! /dev/poll */ #ifndef HAVE_WINSOCK /* In winsock, accept() returns values higher than dtblsize so don't bother with this optimization */ - int sd_nfds; -#endif - fd_set sd_actives; - fd_set sd_readers; - fd_set sd_writers; -#endif + int sd_nfds; +#endif /* ! HAVE_WINSOCK */ + fd_set sd_actives; + fd_set sd_readers; + fd_set sd_writers; +#endif /* ! epoll && ! /dev/poll */ } slap_daemon; -#ifdef HAVE_EPOLL +/* + * NOTE: naming convention for macros: + * + * - SLAP_SOCK_* and SLAP_EVENT_* for public interface that deals + * with file descriptors and events respectively + * + * - SLAP__* for private interface; type by now is one of + * EPOLL, DEVPOLL, SELECT + * + * private interface should not be used in the code. + */ +#if defined(HAVE_EPOLL) +/*************************************** + * Use epoll infrastructure - epoll(4) * + ***************************************/ +# define SLAP_EVENT_FNAME "epoll" # define SLAP_EVENTS_ARE_INDEXED 0 -# define SLAP_SOCK_IX(s) (slap_daemon.sd_index[(s)]) -# define SLAP_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_SOCK_IX(s)]) -# define SLAP_SOCK_EV(s) (SLAP_SOCK_EP(s).events) -# define SLAP_SOCK_IS_ACTIVE(s) (SLAP_SOCK_IX(s) != -1) -# define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_SOCK_IX(s) == -1) -# define SLAP_SOCK_IS_SET(s, mode) (SLAP_SOCK_EV(s) & (mode)) - -# define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET((s), EPOLLIN) -# define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET((s), EPOLLOUT) - -# define SLAP_SET_SOCK(s, mode) do { \ - if ((SLAP_SOCK_EV(s) & (mode)) != (mode)) { \ - SLAP_SOCK_EV(s) |= (mode); \ - epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, (s), \ - &SLAP_SOCK_EP(s)); \ +# define SLAP_EPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)]) +# define SLAP_EPOLL_SOCK_EP(s) (slap_daemon.sd_epolls[SLAP_EPOLL_SOCK_IX(s)]) +# define SLAP_EPOLL_SOCK_EV(s) (SLAP_EPOLL_SOCK_EP(s).events) +# define SLAP_SOCK_IS_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) != -1) +# define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_EPOLL_SOCK_IX(s) == -1) +# define SLAP_EPOLL_SOCK_IS_SET(s, mode) (SLAP_EPOLL_SOCK_EV(s) & (mode)) + +# define SLAP_SOCK_IS_READ(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLIN) +# define SLAP_SOCK_IS_WRITE(s) SLAP_EPOLL_SOCK_IS_SET((s), EPOLLOUT) + +# define SLAP_EPOLL_SOCK_SET(s, mode) do { \ + if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) != (mode) ) { \ + SLAP_EPOLL_SOCK_EV(s) |= (mode); \ + epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, (s), \ + &SLAP_EPOLL_SOCK_EP(s) ); \ } \ -} while(0) +} while (0) -# define SLAP_CLR_SOCK(s, mode) do { \ - if ((SLAP_SOCK_EV(s) & (mode))) { \ - SLAP_SOCK_EV(s) &= ~(mode); \ - epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \ - &SLAP_SOCK_EP(s)); \ +# define SLAP_EPOLL_SOCK_CLR(s, mode) do { \ + if ( (SLAP_EPOLL_SOCK_EV(s) & (mode)) ) { \ + SLAP_EPOLL_SOCK_EV(s) &= ~(mode); \ + epoll_ctl( slap_daemon.sd_epfd, EPOLL_CTL_MOD, s, \ + &SLAP_EPOLL_SOCK_EP(s) ); \ } \ -} while(0) +} while (0) -# define SLAP_SOCK_SET_READ(s) SLAP_SET_SOCK(s, EPOLLIN) -# define SLAP_SOCK_SET_WRITE(s) SLAP_SET_SOCK(s, EPOLLOUT) +# define SLAP_SOCK_SET_READ(s) SLAP_EPOLL_SOCK_SET(s, EPOLLIN) +# define SLAP_SOCK_SET_WRITE(s) SLAP_EPOLL_SOCK_SET(s, EPOLLOUT) + +# define SLAP_SOCK_CLR_READ(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLIN) +# define SLAP_SOCK_CLR_WRITE(s) SLAP_EPOLL_SOCK_CLR((s), EPOLLOUT) # ifdef SLAP_LIGHTWEIGHT_DISPATCHER # define SLAP_SOCK_SET_SUSPEND(s) \ - ( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] = 1 ) + ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 1 ) # define SLAP_SOCK_CLR_SUSPEND(s) \ - ( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] = 0 ) + ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 0 ) # define SLAP_SOCK_IS_SUSPEND(s) \ - ( slap_daemon.sd_suspend[SLAP_SOCK_IX(s)] == 1 ) -# endif - -# define SLAP_SOCK_CLR_READ(s) SLAP_CLR_SOCK((s), EPOLLIN) -# define SLAP_SOCK_CLR_WRITE(s) SLAP_CLR_SOCK((s), EPOLLOUT) + ( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] == 1 ) +# endif /* SLAP_LIGHTWEIGHT_DISPATCHER */ -# define SLAP_CLR_EVENT(i, mode) (revents[(i)].events &= ~(mode)) +# define SLAP_EPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode)) -# define SLAP_EVENT_MAX slap_daemon.sd_nfds +# define SLAP_EVENT_MAX slap_daemon.sd_nfds /* If a Listener address is provided, store that as the epoll data. * Otherwise, store the address of this socket's slot in the * index array. If we can't do this add, the system is out of * resources and we need to shutdown. */ -# define SLAP_ADD_SOCK(s, l) do { \ +# define SLAP_SOCK_ADD(s, l) do { \ int rc; \ - SLAP_SOCK_IX((s)) = slap_daemon.sd_nfds; \ - SLAP_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_SOCK_IX(s)); \ - SLAP_SOCK_EV((s)) = EPOLLIN; \ + SLAP_EPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \ + SLAP_EPOLL_SOCK_EP((s)).data.ptr = (l) ? (l) : (void *)(&SLAP_EPOLL_SOCK_IX(s)); \ + SLAP_EPOLL_SOCK_EV((s)) = EPOLLIN; \ rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_ADD, \ - (s), &SLAP_SOCK_EP((s))); \ + (s), &SLAP_EPOLL_SOCK_EP((s))); \ if ( rc == 0 ) { \ slap_daemon.sd_nfds++; \ } else { \ @@ -196,63 +222,260 @@ static struct slap_daemon { } \ } while (0) -# define SLAP_EV_LISTENER(ptr) (((int *)(ptr) >= slap_daemon.sd_index && \ - (int *)(ptr) <= (slap_daemon.sd_index+dtblsize)) ? 0 : 1 ) +# define SLAP_EPOLL_EV_LISTENER(ptr) \ + (((int *)(ptr) >= slap_daemon.sd_index && \ + (int *)(ptr) <= &slap_daemon.sd_index[dtblsize]) ? 0 : 1 ) -# define SLAP_EV_PTRFD(ptr) (SLAP_EV_LISTENER(ptr) ? \ +# define SLAP_EPOLL_EV_PTRFD(ptr) (SLAP_EPOLL_EV_LISTENER(ptr) ? \ ((Listener *)ptr)->sl_sd : (int *)(ptr) - slap_daemon.sd_index) -# define SLAP_DEL_SOCK(s) do { \ - int fd, rc, index = SLAP_SOCK_IX((s)); \ +# define SLAP_SOCK_DEL(s) do { \ + int fd, rc, index = SLAP_EPOLL_SOCK_IX((s)); \ if ( index < 0 ) break; \ rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \ - (s), &SLAP_SOCK_EP((s))); \ + (s), &SLAP_EPOLL_SOCK_EP((s))); \ slap_daemon.sd_epolls[index] = \ slap_daemon.sd_epolls[slap_daemon.sd_nfds-1]; \ - fd = SLAP_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \ + fd = SLAP_EPOLL_EV_PTRFD(slap_daemon.sd_epolls[index].data.ptr); \ slap_daemon.sd_index[fd] = index; \ slap_daemon.sd_index[(s)] = -1; \ slap_daemon.sd_nfds--; \ } while (0) -# define SLAP_EVENT_CLR_READ(i) SLAP_CLR_EVENT((i), EPOLLIN) -# define SLAP_EVENT_CLR_WRITE(i) SLAP_CLR_EVENT((i), EPOLLOUT) +# define SLAP_EVENT_CLR_READ(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLIN) +# define SLAP_EVENT_CLR_WRITE(i) SLAP_EPOLL_EVENT_CLR((i), EPOLLOUT) -# define SLAP_CHK_EVENT(i, mode) (revents[(i)].events & mode) +# define SLAP_EPOLL_EVENT_CHK(i, mode) (revents[(i)].events & mode) -# define SLAP_EVENT_IS_READ(i) SLAP_CHK_EVENT((i), EPOLLIN) -# define SLAP_EVENT_IS_WRITE(i) SLAP_CHK_EVENT((i), EPOLLOUT) -# define SLAP_EVENT_IS_LISTENER(i) SLAP_EV_LISTENER(revents[(i)].data.ptr) -# define SLAP_EVENT_LISTENER(i) ((Listener *)(revents[(i)].data.ptr)) +# define SLAP_EVENT_IS_READ(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLIN) +# define SLAP_EVENT_IS_WRITE(i) SLAP_EPOLL_EVENT_CHK((i), EPOLLOUT) +# define SLAP_EVENT_IS_LISTENER(i) SLAP_EPOLL_EV_LISTENER(revents[(i)].data.ptr) +# define SLAP_EVENT_LISTENER(i) ((Listener *)(revents[(i)].data.ptr)) -# define SLAP_EVENT_FD(i) SLAP_EV_PTRFD(revents[(i)].data.ptr) +# define SLAP_EVENT_FD(i) SLAP_EPOLL_EV_PTRFD(revents[(i)].data.ptr) -# define SLAP_SOCK_SET_INIT do { \ +# define SLAP_SOCK_INIT do { \ slap_daemon.sd_epolls = ch_calloc(1, \ - sizeof(struct epoll_event) * dtblsize * 2); \ - slap_daemon.sd_index = ch_malloc(sizeof(int) * dtblsize); \ + ( sizeof(struct epoll_event) * 2 \ + + sizeof(int) ) * dtblsize * 2); \ + slap_daemon.sd_index = (int *)&slap_daemon.sd_epolls[ 2 * dtblsize ]; \ slap_daemon.sd_epfd = epoll_create( dtblsize ); \ - for (i=0; itv_sec * 1000 : -1 ) +# define SLAP_EVENT_WAIT(tvp, nsp) do { \ + *(nsp) = epoll_wait( slap_daemon.sd_epfd, revents, \ + dtblsize, (tvp) ? (tvp)->tv_sec * 1000 : -1 ); \ +} while (0) + +#elif defined(SLAP_X_DEVPOLL) && defined(HAVE_DEVPOLL) -#else +/************************************************************* + * Use Solaris' (>= 2.7) /dev/poll infrastructure - poll(7d) * + *************************************************************/ +# define SLAP_EVENT_FNAME "/dev/poll" +# define SLAP_EVENTS_ARE_INDEXED 0 +/* + * - sd_index is used much like with epoll() + * - sd_l is maintained as an array containing the address + * of the listener; the index is the fd itself + * - sd_pollfd is used to keep track of what data has been + * registered in /dev/poll + */ +# define SLAP_DEVPOLL_SOCK_IX(s) (slap_daemon.sd_index[(s)]) +# define SLAP_DEVPOLL_SOCK_LX(s) (slap_daemon.sd_l[(s)]) +# define SLAP_DEVPOLL_SOCK_EP(s) (slap_daemon.sd_pollfd[SLAP_DEVPOLL_SOCK_IX((s))]) +# define SLAP_DEVPOLL_SOCK_FD(s) (SLAP_DEVPOLL_SOCK_EP((s)).fd) +# define SLAP_DEVPOLL_SOCK_EV(s) (SLAP_DEVPOLL_SOCK_EP((s)).events) +# define SLAP_SOCK_IS_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) != -1) +# define SLAP_SOCK_NOT_ACTIVE(s) (SLAP_DEVPOLL_SOCK_IX((s)) == -1) +# define SLAP_SOCK_IS_SET(s, mode) (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) + +# define SLAP_SOCK_IS_READ(s) SLAP_SOCK_IS_SET((s), POLLIN) +# define SLAP_SOCK_IS_WRITE(s) SLAP_SOCK_IS_SET((s), POLLOUT) + +/* as far as I understand, any time we need to communicate with the kernel + * about the number and/or properties of a file descriptor we need it to + * wait for, we have to rewrite the whole set */ +# define SLAP_DEVPOLL_WRITE_POLLFD(s, pfd, n, what, shdn) do { \ + int rc; \ + size_t size = (n) * sizeof( struct pollfd ); \ + /* FIXME: use pwrite? */ \ + rc = write( slap_daemon.sd_dpfd, (pfd), size ); \ + if ( rc != size ) { \ + Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \ + "%s fd=%d failed errno=%d\n", \ + (what), (s), errno ); \ + if ( (shdn) ) { \ + slapd_shutdown = 2; \ + } \ + } \ +} while (0) + +# define SLAP_DEVPOLL_SOCK_SET(s, mode) do { \ + Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_SET_%s(%d)=%d\n", \ + (mode) == POLLIN ? "READ" : "WRITE", (s), \ + ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) ); \ + if ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) != (mode) ) { \ + struct pollfd pfd; \ + SLAP_DEVPOLL_SOCK_EV((s)) |= (mode); \ + pfd.fd = SLAP_DEVPOLL_SOCK_FD((s)); \ + pfd.events = /* (mode) */ SLAP_DEVPOLL_SOCK_EV((s)); \ + SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd, 1, "SET", 0); \ + } \ +} while (0) + +# define SLAP_DEVPOLL_SOCK_CLR(s, mode) do { \ + Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_CLR_%s(%d)=%d\n", \ + (mode) == POLLIN ? "READ" : "WRITE", (s), \ + ( (SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) ); \ + if ((SLAP_DEVPOLL_SOCK_EV((s)) & (mode)) == (mode) ) { \ + struct pollfd pfd[2]; \ + SLAP_DEVPOLL_SOCK_EV((s)) &= ~(mode); \ + pfd[0].fd = SLAP_DEVPOLL_SOCK_FD((s)); \ + pfd[0].events = POLLREMOVE; \ + pfd[1] = SLAP_DEVPOLL_SOCK_EP((s)); \ + SLAP_DEVPOLL_WRITE_POLLFD((s), &pfd[0], 2, "CLR", 0); \ + } \ +} while (0) + +# define SLAP_SOCK_SET_READ(s) SLAP_DEVPOLL_SOCK_SET(s, POLLIN) +# define SLAP_SOCK_SET_WRITE(s) SLAP_DEVPOLL_SOCK_SET(s, POLLOUT) + +# define SLAP_SOCK_CLR_READ(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLIN) +# define SLAP_SOCK_CLR_WRITE(s) SLAP_DEVPOLL_SOCK_CLR((s), POLLOUT) + +# ifdef SLAP_LIGHTWEIGHT_DISPATCHER +# define SLAP_SOCK_SET_SUSPEND(s) \ + ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 1 ) +# define SLAP_SOCK_CLR_SUSPEND(s) \ + ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] = 0 ) +# define SLAP_SOCK_IS_SUSPEND(s) \ + ( slap_daemon.sd_suspend[SLAP_DEVPOLL_SOCK_IX((s))] == 1 ) +# endif /* SLAP_LIGHTWEIGHT_DISPATCHER */ + +# define SLAP_DEVPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode)) + +# define SLAP_EVENT_MAX slap_daemon.sd_nfds + +/* If a Listener address is provided, store that in the sd_l array. + * If we can't do this add, the system is out of resources and we + * need to shutdown. + */ +# define SLAP_SOCK_ADD(s, l) do { \ + Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_ADD(%d, %p)\n", (s), (l), 0 ); \ + SLAP_DEVPOLL_SOCK_IX((s)) = slap_daemon.sd_nfds; \ + SLAP_DEVPOLL_SOCK_LX((s)) = (l); \ + SLAP_DEVPOLL_SOCK_FD((s)) = (s); \ + SLAP_DEVPOLL_SOCK_EV((s)) = POLLIN; \ + SLAP_DEVPOLL_WRITE_POLLFD((s), &SLAP_DEVPOLL_SOCK_EP((s)), 1, "ADD", 1); \ + slap_daemon.sd_nfds++; \ +} while (0) + +# define SLAP_DEVPOLL_EV_LISTENER(ptr) ((ptr) != NULL) + +# define SLAP_SOCK_DEL(s) do { \ + int fd, index = SLAP_DEVPOLL_SOCK_IX((s)); \ + Debug( LDAP_DEBUG_CONNS, "SLAP_SOCK_DEL(%d)\n", (s), 0, 0 ); \ + if ( index < 0 ) break; \ + if ( index < slap_daemon.sd_nfds - 1 ) { \ + struct pollfd pfd = slap_daemon.sd_pollfd[index]; \ + fd = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].fd; \ + slap_daemon.sd_pollfd[index] = slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1]; \ + slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1] = pfd; \ + slap_daemon.sd_index[fd] = index; \ + } \ + slap_daemon.sd_index[(s)] = -1; \ + slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = POLLREMOVE; \ + SLAP_DEVPOLL_WRITE_POLLFD((s), &slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1], 1, "DEL", 0); \ + slap_daemon.sd_pollfd[slap_daemon.sd_nfds - 1].events = 0; \ + slap_daemon.sd_nfds--; \ +} while (0) + +# define SLAP_EVENT_CLR_READ(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLIN) +# define SLAP_EVENT_CLR_WRITE(i) SLAP_DEVPOLL_EVENT_CLR((i), POLLOUT) + +# define SLAP_DEVPOLL_EVENT_CHK(i, mode) (revents[(i)].events & (mode)) + +# define SLAP_EVENT_FD(i) (revents[(i)].fd) + +# define SLAP_EVENT_IS_READ(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLIN) +# define SLAP_EVENT_IS_WRITE(i) SLAP_DEVPOLL_EVENT_CHK((i), POLLOUT) +# define SLAP_EVENT_IS_LISTENER(i) SLAP_DEVPOLL_EV_LISTENER(SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i)))) +# define SLAP_EVENT_LISTENER(i) SLAP_DEVPOLL_SOCK_LX(SLAP_EVENT_FD((i))) + +# define SLAP_SOCK_INIT do { \ + slap_daemon.sd_pollfd = ch_calloc( 1, \ + ( sizeof(struct pollfd) * 2 \ + + sizeof( int ) \ + + sizeof( Listener * ) ) * dtblsize ); \ + slap_daemon.sd_index = (int *)&slap_daemon.sd_pollfd[ 2 * dtblsize ]; \ + slap_daemon.sd_l = (Listener **)&slap_daemon.sd_index[ dtblsize ]; \ + slap_daemon.sd_dpfd = open( SLAP_EVENT_FNAME, O_RDWR ); \ + if ( slap_daemon.sd_dpfd == -1 ) { \ + Debug( LDAP_DEBUG_ANY, "daemon: " SLAP_EVENT_FNAME ": " \ + "open(\"" SLAP_EVENT_FNAME "\") failed errno=%d\n", \ + errno, 0, 0 ); \ + SLAP_SOCK_DESTROY; \ + return -1; \ + } \ + for ( i = 0; i < dtblsize; i++ ) { \ + slap_daemon.sd_pollfd[i].fd = -1; \ + slap_daemon.sd_index[i] = -1; \ + } \ +} while (0) + +# define SLAP_SOCK_DESTROY do { \ + if ( slap_daemon.sd_pollfd != NULL ) { \ + ch_free( slap_daemon.sd_pollfd ); \ + slap_daemon.sd_pollfd = NULL; \ + slap_daemon.sd_index = NULL; \ + slap_daemon.sd_l = NULL; \ + close( slap_daemon.sd_dpfd ); \ + } \ +} while ( 0 ) + +# define SLAP_EVENT_DECL struct pollfd *revents + +# define SLAP_EVENT_INIT do { \ + revents = &slap_daemon.sd_pollfd[ dtblsize ]; \ +} while (0) + +# define SLAP_EVENT_WAIT(tvp, nsp) do { \ + struct dvpoll sd_dvpoll; \ + sd_dvpoll.dp_timeout = (tvp) ? (tvp)->tv_sec * 1000 : -1; \ + sd_dvpoll.dp_nfds = dtblsize; \ + sd_dvpoll.dp_fds = revents; \ + *(nsp) = ioctl( slap_daemon.sd_dpfd, DP_POLL, &sd_dvpoll ); \ +} while (0) + +#else /* ! epoll && ! /dev/poll */ + +/************************************** + * Use select system call - select(2) * + **************************************/ +# define SLAP_EVENT_FNAME "select" /* select */ -# define SLAP_EVENTS_ARE_INDEXED 1 -# define SLAP_EVENT_DECL \ - fd_set readfds, writefds +# define SLAP_EVENTS_ARE_INDEXED 1 +# define SLAP_EVENT_DECL fd_set readfds, writefds -# define SLAP_EVENT_INIT do { \ +# define SLAP_EVENT_INIT do { \ AC_MEMCPY( &readfds, &slap_daemon.sd_readers, sizeof(fd_set) ); \ if ( nwriters ) { \ AC_MEMCPY( &writefds, &slap_daemon.sd_writers, sizeof(fd_set) ); \ @@ -262,19 +485,21 @@ static struct slap_daemon { } while (0) # ifdef FD_SETSIZE -# define CHK_SETSIZE do { \ +# define SLAP_SELECT_CHK_SETSIZE do { \ if (dtblsize > FD_SETSIZE) dtblsize = FD_SETSIZE; \ } while (0) -# else -# define CHK_SETSIZE do { ; } while (0) -# endif +# else /* ! FD_SETSIZE */ +# define SLAP_SELECT_CHK_SETSIZE do { ; } while (0) +# endif /* ! FD_SETSIZE */ -# define SLAP_SOCK_SET_INIT do { \ - CHK_SETSIZE; \ +# define SLAP_SOCK_INIT do { \ + SLAP_SELECT_CHK_SETSIZE; \ FD_ZERO(&slap_daemon.sd_readers); \ FD_ZERO(&slap_daemon.sd_writers); \ } while (0) +# define SLAP_SOCK_DESTROY + # define SLAP_SOCK_IS_ACTIVE(fd) FD_ISSET((fd), &slap_daemon.sd_actives) # define SLAP_SOCK_IS_READ(fd) FD_ISSET((fd), &slap_daemon.sd_readers) # define SLAP_SOCK_IS_WRITE(fd) FD_ISSET((fd), &slap_daemon.sd_writers) @@ -285,37 +510,37 @@ static struct slap_daemon { # ifdef HAVE_WINSOCK # define SLAP_SOCK_SET_READ(fd) do { \ if (!SLAP_SOCK_IS_READ(fd)) { FD_SET((fd), &slap_daemon.sd_readers); } \ -} while(0) +} while (0) # define SLAP_SOCK_SET_WRITE(fd) do { \ if (!SLAP_SOCK_IS_WRITE(fd)) { FD_SET((fd), &slap_daemon.sd_writers); } \ -} while(0) +} while (0) -# define SLAP_ADDTEST(s) -# define SLAP_EVENT_MAX dtblsize -# else +# define SLAP_SELECT_ADDTEST(s) +# define SLAP_EVENT_MAX dtblsize +# else /* ! HAVE_WINSOCK */ # define SLAP_SOCK_SET_READ(fd) FD_SET((fd), &slap_daemon.sd_readers) # define SLAP_SOCK_SET_WRITE(fd) FD_SET((fd), &slap_daemon.sd_writers) -# define SLAP_EVENT_MAX slap_daemon.sd_nfds -# define SLAP_ADDTEST(s) do { \ +# define SLAP_EVENT_MAX slap_daemon.sd_nfds +# define SLAP_SELECT_ADDTEST(s) do { \ if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \ } while (0) -# endif +# endif /* ! HAVE_WINSOCK */ # define SLAP_SOCK_CLR_READ(fd) FD_CLR((fd), &slap_daemon.sd_readers) # define SLAP_SOCK_CLR_WRITE(fd) FD_CLR((fd), &slap_daemon.sd_writers) -# define SLAP_ADD_SOCK(s, l) do { \ - SLAP_ADDTEST((s)); \ +# define SLAP_SOCK_ADD(s, l) do { \ + SLAP_SELECT_ADDTEST((s)); \ FD_SET((s), &slap_daemon.sd_actives); \ FD_SET((s), &slap_daemon.sd_readers); \ -} while(0) +} while (0) -# define SLAP_DEL_SOCK(s) do { \ +# define SLAP_SOCK_DEL(s) do { \ FD_CLR((s), &slap_daemon.sd_actives); \ FD_CLR((s), &slap_daemon.sd_readers); \ FD_CLR((s), &slap_daemon.sd_writers); \ -} while(0) +} while (0) # define SLAP_EVENT_IS_READ(fd) FD_ISSET((fd), &readfds) # define SLAP_EVENT_IS_WRITE(fd) FD_ISSET((fd), &writefds) @@ -323,10 +548,11 @@ static struct slap_daemon { # define SLAP_EVENT_CLR_READ(fd) FD_CLR((fd), &readfds) # define SLAP_EVENT_CLR_WRITE(fd) FD_CLR((fd), &writefds) -# define SLAP_EVENT_WAIT(tvp) \ - select( SLAP_EVENT_MAX, &readfds, \ - nwriters > 0 ? &writefds : NULL, NULL, (tvp) ) -#endif +# define SLAP_EVENT_WAIT(tvp, nsp) do { \ + *(nsp) = select( SLAP_EVENT_MAX, &readfds, \ + nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \ +} while (0) +#endif /* ! epoll && ! /dev/poll */ #ifdef HAVE_SLP /* @@ -339,17 +565,23 @@ static struct slap_daemon { static char** slapd_srvurls = NULL; static SLPHandle slapd_hslp = 0; int slapd_register_slp = 0; +const char *slapd_slp_attrs = NULL; -void slapd_slp_init( const char* urls ) { +static SLPError slapd_slp_cookie; + +static void +slapd_slp_init( const char* urls ) +{ int i; + SLPError err; slapd_srvurls = ldap_str2charray( urls, " " ); - if( slapd_srvurls == NULL ) return; + if ( slapd_srvurls == NULL ) return; /* find and expand INADDR_ANY URLs */ - for( i=0; slapd_srvurls[i] != NULL; i++ ) { - if( strcmp( slapd_srvurls[i], "ldap:///" ) == 0) { + for ( i = 0; slapd_srvurls[i] != NULL; i++ ) { + if ( strcmp( slapd_srvurls[i], "ldap:///" ) == 0 ) { char *host = ldap_pvt_get_fqdn( NULL ); if ( host != NULL ) { slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i], @@ -361,7 +593,7 @@ void slapd_slp_init( const char* urls ) { ch_free( host ); } - } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0) { + } else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) { char *host = ldap_pvt_get_fqdn( NULL ); if ( host != NULL ) { slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i], @@ -376,11 +608,18 @@ void slapd_slp_init( const char* urls ) { } /* open the SLP handle */ - SLPOpen( "en", 0, &slapd_hslp ); + err = SLPOpen( "en", 0, &slapd_hslp ); + + if ( err != SLP_OK ) { + Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n", + (long)err, 0, 0 ); + } } -void slapd_slp_deinit() { - if( slapd_srvurls == NULL ) return; +static void +slapd_slp_deinit( void ) +{ + if ( slapd_srvurls == NULL ) return; ldap_charray_free( slapd_srvurls ); slapd_srvurls = NULL; @@ -389,47 +628,67 @@ void slapd_slp_deinit() { SLPClose( slapd_hslp ); } -void slapd_slp_regreport( - SLPHandle hslp, - SLPError errcode, - void* cookie ) +static void +slapd_slp_regreport( + SLPHandle hslp, + SLPError errcode, + void *cookie ) { - /* empty report */ + /* return the error code in the cookie */ + *(SLPError*)cookie = errcode; } -void slapd_slp_reg() { +static void +slapd_slp_reg() +{ int i; + SLPError err; - if( slapd_srvurls == NULL ) return; + if ( slapd_srvurls == NULL ) return; - for( i=0; slapd_srvurls[i] != NULL; i++ ) { - if( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX, + for ( i = 0; slapd_srvurls[i] != NULL; i++ ) { + if ( strncmp( slapd_srvurls[i], LDAP_SRVTYPE_PREFIX, sizeof( LDAP_SRVTYPE_PREFIX ) - 1 ) == 0 || - strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX, + strncmp( slapd_srvurls[i], LDAPS_SRVTYPE_PREFIX, sizeof( LDAPS_SRVTYPE_PREFIX ) - 1 ) == 0 ) { - SLPReg( slapd_hslp, + err = SLPReg( slapd_hslp, slapd_srvurls[i], SLP_LIFETIME_MAXIMUM, "ldap", - "", - 1, + (slapd_slp_attrs) ? slapd_slp_attrs : "", + SLP_TRUE, slapd_slp_regreport, - NULL ); + &slapd_slp_cookie ); + + if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) { + Debug( LDAP_DEBUG_CONNS, + "daemon: SLPReg(%s) failed with %ld, cookie = %ld\n", + slapd_srvurls[i], (long)err, (long)slapd_slp_cookie ); + } } } } -void slapd_slp_dereg() { +static void +slapd_slp_dereg( void ) +{ int i; + SLPError err; - if( slapd_srvurls == NULL ) return; + if ( slapd_srvurls == NULL ) return; - for( i=0; slapd_srvurls[i] != NULL; i++ ) { - SLPDereg( slapd_hslp, + for ( i = 0; slapd_srvurls[i] != NULL; i++ ) { + err = SLPDereg( slapd_hslp, slapd_srvurls[i], slapd_slp_regreport, - NULL ); + &slapd_slp_cookie ); + + if ( err != SLP_OK || slapd_slp_cookie != SLP_OK ) { + Debug( LDAP_DEBUG_CONNS, + "daemon: SLPDereg(%s) failed with %ld, cookie = %ld\n", + slapd_srvurls[i], (long)err, (long)slapd_slp_cookie ); + } } } #endif /* HAVE_SLP */ @@ -443,31 +702,41 @@ void slapd_slp_dereg() { * idletimeout. The underlying event handler may record the Listener * argument to differentiate Listener's from real sessions. */ -static void slapd_add(ber_socket_t s, int isactive, Listener *sl) { +static void +slapd_add( ber_socket_t s, int isactive, Listener *sl ) +{ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex ); assert( SLAP_SOCK_NOT_ACTIVE(s) ); if ( isactive ) slap_daemon.sd_nactives++; - SLAP_ADD_SOCK(s, sl); + SLAP_SOCK_ADD(s, sl); - Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr\n", - (long) s, 0, 0 ); + Debug( LDAP_DEBUG_CONNS, "daemon: added %ldr%s listener=%p\n", + (long) s, isactive ? " (active)" : "", (void *)sl ); ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex ); #ifdef SLAP_LIGHTWEIGHT_DISPATCHER WAKE_LISTENER(1); -#endif +#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */ } -void slapd_sd_lock() +/* + * NOTE: unused + */ +void +slapd_sd_lock( void ) { ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex ); } -void slapd_sd_unlock() +/* + * NOTE: unused + */ +void +slapd_sd_unlock( void ) { ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex ); } @@ -475,8 +744,10 @@ void slapd_sd_unlock() /* * Remove the descriptor from daemon control */ -void slapd_remove( +void +slapd_remove( ber_socket_t s, + Sockbuf *sb, int wasactive, int wake, int locked ) @@ -501,7 +772,10 @@ void slapd_remove( if ( waswriter ) slap_daemon.sd_nwriters--; - SLAP_DEL_SOCK(s); + SLAP_SOCK_DEL(s); + + if ( sb ) + ber_sockbuf_free(sb); /* If we ran out of file descriptors, we dropped a listener from * the select() loop. Now that we're removing a session from our @@ -529,7 +803,9 @@ void slapd_remove( WAKE_LISTENER(wake || slapd_gentle_shutdown == 2); } -void slapd_clr_write(ber_socket_t s, int wake) { +void +slapd_clr_write( ber_socket_t s, int wake ) +{ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex ); assert( SLAP_SOCK_IS_ACTIVE( s )); @@ -543,7 +819,9 @@ void slapd_clr_write(ber_socket_t s, int wake) { WAKE_LISTENER(wake); } -void slapd_set_write(ber_socket_t s, int wake) { +void +slapd_set_write( ber_socket_t s, int wake ) +{ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex ); assert( SLAP_SOCK_IS_ACTIVE( s )); @@ -557,7 +835,9 @@ void slapd_set_write(ber_socket_t s, int wake) { WAKE_LISTENER(wake); } -int slapd_clr_read(ber_socket_t s, int wake) { +int +slapd_clr_read( ber_socket_t s, int wake ) +{ int rc = 1; ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex ); @@ -571,7 +851,9 @@ int slapd_clr_read(ber_socket_t s, int wake) { return rc; } -void slapd_set_read(ber_socket_t s, int wake) { +void +slapd_set_read( ber_socket_t s, int wake ) +{ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex ); assert( SLAP_SOCK_IS_ACTIVE( s )); @@ -581,13 +863,17 @@ void slapd_set_read(ber_socket_t s, int wake) { WAKE_LISTENER(wake); } -static void slapd_close(ber_socket_t s) { +static void +slapd_close( ber_socket_t s ) +{ Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n", (long) s, 0, 0 ); tcp_close(s); } -static void slap_free_listener_addresses(struct sockaddr **sal) { +static void +slap_free_listener_addresses( struct sockaddr **sal ) +{ struct sockaddr **sap; if (sal == NULL) return; for (sap = sal; *sap != NULL; sap++) ch_free(*sap); @@ -595,7 +881,8 @@ static void slap_free_listener_addresses(struct sockaddr **sal) { } #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD) -static int get_url_perms( +static int +get_url_perms( char **exts, mode_t *perms, int *crit ) @@ -675,10 +962,11 @@ static int get_url_perms( #endif /* LDAP_PF_LOCAL || SLAP_X_LISTENER_MOD */ /* port = 0 indicates AF_LOCAL */ -static int slap_get_listener_addresses( +static int +slap_get_listener_addresses( const char *host, unsigned short port, - struct sockaddr ***sal) + struct sockaddr ***sal ) { struct sockaddr **sap; @@ -705,7 +993,7 @@ static int slap_get_listener_addresses( (*sap)->sa_family = AF_LOCAL; strcpy( ((struct sockaddr_un *)*sap)->sun_path, host ); } else -#endif +#endif /* LDAP_PF_LOCAL */ { #ifdef HAVE_GETADDRINFO struct addrinfo hints, *res, *sai; @@ -753,7 +1041,7 @@ static int slap_get_listener_addresses( *(struct sockaddr_in6 *)*sap = *((struct sockaddr_in6 *)sai->ai_addr); break; -# endif +# endif /* LDAP_PF_INET6 */ case AF_INET: *sap = ch_malloc(sizeof(struct sockaddr_in)); if (*sap == NULL) { @@ -777,7 +1065,7 @@ static int slap_get_listener_addresses( freeaddrinfo(res); -#else +#else /* ! HAVE_GETADDRINFO */ int i, n = 1; struct in_addr in; struct hostent *he = NULL; @@ -811,7 +1099,7 @@ static int slap_get_listener_addresses( sizeof(struct in_addr) ); } sap[i] = NULL; -#endif +#endif /* ! HAVE_GETADDRINFO */ } return 0; @@ -821,11 +1109,11 @@ errexit: return -1; } -static int slap_open_listener( +static int +slap_open_listener( const char* url, int *listeners, - int *cur - ) + int *cur ) { int num, tmp, rc; Listener l; @@ -856,7 +1144,7 @@ static int slap_open_listener( l.sl_mute = 0; #ifdef SLAP_LIGHTWEIGHT_DISPATCHER l.sl_busy = 0; -#endif +#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */ #ifndef HAVE_TLS if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) { @@ -868,13 +1156,13 @@ static int slap_open_listener( if(! lud->lud_port ) lud->lud_port = LDAP_PORT; -#else +#else /* HAVE_TLS */ l.sl_is_tls = ldap_pvt_url_scheme2tls( lud->lud_scheme ); if(! lud->lud_port ) { lud->lud_port = l.sl_is_tls ? LDAPS_PORT : LDAP_PORT; } -#endif +#endif /* HAVE_TLS */ port = (unsigned short) lud->lud_port; @@ -886,13 +1174,13 @@ static int slap_open_listener( } else { err = slap_get_listener_addresses(lud->lud_host, 0, &sal); } -#else +#else /* ! LDAP_PF_LOCAL */ Debug( LDAP_DEBUG_ANY, "daemon: URL scheme not supported: %s", url, 0, 0); ldap_free_urldesc( lud ); return -1; -#endif +#endif /* ! LDAP_PF_LOCAL */ } else { if( lud->lud_host == NULL || lud->lud_host[0] == '\0' || strcmp(lud->lud_host, "*") == 0 ) @@ -905,7 +1193,7 @@ static int slap_open_listener( #ifdef LDAP_CONNECTIONLESS l.sl_is_udp = ( tmp == LDAP_PROTO_UDP ); -#endif +#endif /* LDAP_CONNECTIONLESS */ #if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD) if ( lud->lud_exts ) { @@ -939,12 +1227,12 @@ static int slap_open_listener( case AF_INET6: af = "IPv6"; break; -#endif +#endif /* LDAP_PF_INET6 */ #ifdef LDAP_PF_LOCAL case AF_LOCAL: af = "Local"; break; -#endif +#endif /* LDAP_PF_LOCAL */ default: sal++; continue; @@ -952,7 +1240,7 @@ static int slap_open_listener( #ifdef LDAP_CONNECTIONLESS if( l.sl_is_udp ) socktype = SOCK_DGRAM; -#endif +#endif /* LDAP_CONNECTIONLESS */ l.sl_sd = socket( (*sal)->sa_family, socktype, 0); if ( l.sl_sd == AC_SOCKET_INVALID ) { @@ -973,13 +1261,13 @@ static int slap_open_listener( sal++; continue; } -#endif +#endif /* ! HAVE_WINSOCK */ #ifdef LDAP_PF_LOCAL if ( (*sal)->sa_family == AF_LOCAL ) { unlink( ((struct sockaddr_un *)*sal)->sun_path ); } else -#endif +#endif /* LDAP_PF_LOCAL */ { #ifdef SO_REUSEADDR /* enable address reuse */ @@ -992,7 +1280,7 @@ static int slap_open_listener( "setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n", (long) l.sl_sd, err, sock_errstr(err) ); } -#endif +#endif /* SO_REUSEADDR */ } switch( (*sal)->sa_family ) { @@ -1012,29 +1300,52 @@ static int slap_open_listener( "setsockopt(IPV6_V6ONLY) failed errno=%d (%s)\n", (long) l.sl_sd, err, sock_errstr(err) ); } -#endif +#endif /* IPV6_V6ONLY */ addrlen = sizeof(struct sockaddr_in6); break; -#endif +#endif /* LDAP_PF_INET6 */ #ifdef LDAP_PF_LOCAL case AF_LOCAL: #ifdef LOCAL_CREDS - { - int one = 1; - setsockopt(l.sl_sd, 0, LOCAL_CREDS, &one, sizeof one); - } -#endif - addrlen = sizeof(struct sockaddr_un); - break; -#endif + { + int one = 1; + setsockopt( l.sl_sd, 0, LOCAL_CREDS, &one, sizeof( one ) ); + } +#endif /* LOCAL_CREDS */ + + addrlen = sizeof( struct sockaddr_un ); + break; +#endif /* LDAP_PF_LOCAL */ } - if (bind(l.sl_sd, *sal, addrlen)) { +#ifdef LDAP_PF_LOCAL + /* create socket with all permissions set for those systems + * that honor permissions on sockets (e.g. Linux); typically, + * only write is required. To exploit filesystem permissions, + * place the socket in a directory and use directory's + * permissions. Need write perms to the directory to + * create/unlink the socket; likely need exec perms to access + * the socket (ITS#4709) */ + { + mode_t old_umask; + + if ( (*sal)->sa_family == AF_LOCAL ) { + old_umask = umask( 0 ); + } +#endif /* LDAP_PF_LOCAL */ + rc = bind( l.sl_sd, *sal, addrlen ); +#ifdef LDAP_PF_LOCAL + if ( (*sal)->sa_family == AF_LOCAL ) { + umask( old_umask ); + } + } +#endif /* LDAP_PF_LOCAL */ + if ( rc ) { err = sock_errno(); Debug( LDAP_DEBUG_ANY, "daemon: bind(%ld) failed errno=%d (%s)\n", - (long) l.sl_sd, err, sock_errstr(err) ); + (long)l.sl_sd, err, sock_errstr( err ) ); tcp_close( l.sl_sd ); sal++; continue; @@ -1058,9 +1369,9 @@ static int slap_open_listener( inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr, addr, sizeof(addr) ); s = addr; -#else +#else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */ s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr ); -#endif +#endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */ port = ntohs( ((struct sockaddr_in *)*sal) ->sin_port ); l.sl_name.bv_val = ber_memalloc( sizeof("IP=255.255.255.255:65535") ); @@ -1115,7 +1426,8 @@ static int slap_open_listener( static int sockinit(void); static int sockdestroy(void); -int slapd_daemon_init( const char *urls ) +int +slapd_daemon_init( const char *urls ) { int i, j, n, rc; char **u; @@ -1125,8 +1437,8 @@ int slapd_daemon_init( const char *urls ) ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex ); #ifdef HAVE_TCPD - ldap_pvt_thread_mutex_init( &slap_daemon.tcpd_mutex ); -#endif + ldap_pvt_thread_mutex_init( &slap_daemon.sd_tcpd_mutex ); +#endif /* TCP Wrappers */ if( (rc = sockinit()) != 0 ) return rc; @@ -1134,9 +1446,9 @@ int slapd_daemon_init( const char *urls ) dtblsize = sysconf( _SC_OPEN_MAX ); #elif HAVE_GETDTABLESIZE dtblsize = getdtablesize(); -#else +#else /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */ dtblsize = FD_SETSIZE; -#endif +#endif /* ! HAVE_SYSCONF && ! HAVE_GETDTABLESIZE */ /* open a pipe (or something equivalent connected to itself). * we write a byte on this fd whenever we catch a signal. The main @@ -1149,7 +1461,7 @@ int slapd_daemon_init( const char *urls ) return rc; } - SLAP_SOCK_SET_INIT; + SLAP_SOCK_INIT; if( urls == NULL ) urls = "ldap:///"; @@ -1158,6 +1470,8 @@ int slapd_daemon_init( const char *urls ) if( u == NULL || u[0] == NULL ) { Debug( LDAP_DEBUG_ANY, "daemon_init: no urls (%s) provided.\n", urls, 0, 0 ); + if ( u ) + ldap_charray_free( u ); return -1; } @@ -1194,7 +1508,7 @@ int slapd_daemon_init( const char *urls ) slapd_slp_init( urls ); slapd_slp_reg(); } -#endif +#endif /* HAVE_SLP */ ldap_charray_free( u ); @@ -1203,7 +1517,7 @@ int slapd_daemon_init( const char *urls ) int -slapd_daemon_destroy(void) +slapd_daemon_destroy( void ) { connections_destroy(); tcp_close( wake_sds[1] ); @@ -1215,11 +1529,11 @@ slapd_daemon_destroy(void) slapd_slp_dereg(); slapd_slp_deinit(); } -#endif +#endif /* HAVE_SLP */ #ifdef HAVE_TCPD - ldap_pvt_thread_mutex_destroy( &slap_daemon.tcpd_mutex ); -#endif + ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_tcpd_mutex ); +#endif /* TCP Wrappers */ ldap_pvt_thread_mutex_destroy( &slap_daemon.sd_mutex ); return 0; @@ -1236,7 +1550,7 @@ close_listeners( Listener *lr = slap_listeners[l]; if ( lr->sl_sd != AC_SOCKET_INVALID ) { - if ( remove ) slapd_remove( lr->sl_sd, 0, 0, 0 ); + if ( remove ) slapd_remove( lr->sl_sd, NULL, 0, 0, 0 ); #ifdef LDAP_PF_LOCAL if ( lr->sl_sa.sa_addr.sa_family == AF_LOCAL ) { @@ -1273,7 +1587,7 @@ slap_listener( struct berval authid = BER_BVNULL; #ifdef SLAPD_RLOOKUPS char hbuf[NI_MAXHOST]; -#endif +#endif /* SLAPD_RLOOKUPS */ char *dnsname = NULL; char *peeraddr = NULL; @@ -1281,15 +1595,19 @@ slap_listener( char peername[MAXPATHLEN + sizeof("PATH=")]; #elif defined(LDAP_PF_INET6) char peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")]; -#else +#else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */ char peername[sizeof("IP=255.255.255.255:65336")]; #endif /* LDAP_PF_LOCAL */ + Debug( LDAP_DEBUG_TRACE, + ">>> slap_listener(%s)", + sl->sl_url.bv_val, 0, 0 ); + peername[0] = '\0'; #ifdef LDAP_CONNECTIONLESS if ( sl->sl_is_udp ) return 1; -#endif +#endif /* LDAP_CONNECTIONLESS */ # ifdef LDAP_PF_LOCAL /* FIXME: apparently accept doesn't fill @@ -1305,7 +1623,7 @@ slap_listener( */ sl->sl_busy = 0; WAKE_LISTENER(1); -#endif +#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */ if ( s == AC_SOCKET_INVALID ) { int err = sock_errno(); @@ -1313,10 +1631,10 @@ slap_listener( if( #ifdef EMFILE err == EMFILE || -#endif +#endif /* EMFILE */ #ifdef ENFILE err == ENFILE || -#endif +#endif /* ENFILE */ 0 ) { ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex ); @@ -1344,14 +1662,14 @@ slap_listener( ldap_pvt_thread_yield(); return 0; } -#endif +#endif /* ! HAVE_WINSOCK */ #ifdef LDAP_DEBUG ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex ); /* newly accepted stream should not be in any of the FD SETS */ assert( SLAP_SOCK_NOT_ACTIVE( s )); ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex ); -#endif +#endif /* LDAP_DEBUG */ #if defined( SO_KEEPALIVE ) || defined( TCP_NODELAY ) #ifdef LDAP_PF_LOCAL @@ -1372,7 +1690,7 @@ slap_listener( "slapd(%ld): setsockopt(SO_KEEPALIVE) failed " "errno=%d (%s)\n", (long) s, err, sock_errstr(err) ); } -#endif +#endif /* SO_KEEPALIVE */ #ifdef TCP_NODELAY /* enable no delay */ tmp = 1; @@ -1384,9 +1702,9 @@ slap_listener( "slapd(%ld): setsockopt(TCP_NODELAY) failed " "errno=%d (%s)\n", (long) s, err, sock_errstr(err) ); } -#endif +#endif /* TCP_NODELAY */ } -#endif +#endif /* SO_KEEPALIVE || TCP_NODELAY */ Debug( LDAP_DEBUG_CONNS, "daemon: listen=%ld, new connection on %ld\n", @@ -1462,7 +1780,7 @@ slap_listener( if ( ( from.sa_addr.sa_family == AF_INET ) #ifdef LDAP_PF_INET6 || ( from.sa_addr.sa_family == AF_INET6 ) -#endif +#endif /* LDAP_PF_INET6 */ ) { dnsname = NULL; @@ -1480,12 +1798,12 @@ slap_listener( #ifdef HAVE_TCPD { int rc; - ldap_pvt_thread_mutex_lock( &slap_daemon.tcpd_mutex ); + ldap_pvt_thread_mutex_lock( &slap_daemon.sd_tcpd_mutex ); rc = hosts_ctl("slapd", dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN, peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN, SLAP_STRING_UNKNOWN ); - ldap_pvt_thread_mutex_unlock( &slap_daemon.tcpd_mutex ); + ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_tcpd_mutex ); if ( !rc ) { /* DENY ACCESS */ Statslog( LDAP_DEBUG_STATS, @@ -1506,9 +1824,9 @@ slap_listener( peername, #ifdef HAVE_TLS sl->sl_is_tls ? CONN_IS_TLS : 0, -#else +#else /* ! HAVE_TLS */ 0, -#endif +#endif /* ! HAVE_TLS */ ssf, authid.bv_val ? &authid : NULL ); @@ -1536,13 +1854,15 @@ slap_listener_thread( void* ctx, void* ptr ) { - int rc; + int rc; + Listener *sl = (Listener *)ptr; - rc = slap_listener( (Listener*)ptr ); + rc = slap_listener( sl ); if( rc != LDAP_SUCCESS ) { Debug( LDAP_DEBUG_ANY, - "listener_thread: failed %d", rc, 0, 0 ); + "slap_listener_thread(%s): failed err=%d", + sl->sl_url.bv_val, rc, 0 ); } return (void*)NULL; @@ -1569,7 +1889,7 @@ slap_listener_activate( } return rc; } -#endif +#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */ static void * slapd_daemon_task( @@ -1609,7 +1929,7 @@ slapd_daemon_task( */ if ( slap_listeners[l]->sl_is_udp ) continue; -#endif +#endif /* LDAP_CONNECTIONLESS */ if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN_BACKLOG ) == -1 ) { int err = sock_errno(); @@ -1648,7 +1968,7 @@ slapd_daemon_task( } } } -#endif +#endif /* LDAP_PF_INET6 */ Debug( LDAP_DEBUG_ANY, "daemon: listen(%s, 5) failed errno=%d (%s)\n", slap_listeners[l]->sl_url.bv_val, err, @@ -1665,7 +1985,7 @@ slapd_daemon_task( slapd_shutdown = 2; return (void*)-1; } -#endif +#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */ slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] ); } @@ -1674,7 +1994,7 @@ slapd_daemon_task( if ( started_event != NULL ) { ldap_pvt_thread_cond_signal( &started_event ); } -#endif +#endif /* HAVE_NT_SERVICE_MANAGER */ #ifdef SLAP_SEM_LOAD_CONTROL /* @@ -1683,21 +2003,21 @@ slapd_daemon_task( (void) ldap_lazy_sem_init( SLAP_MAX_WORKER_THREADS + 4 /* max workers + margin */, 4 /* lazyness */ ); -#endif +#endif /* SLAP_SEM_LOAD_CONTROL */ /* initialization complete. Here comes the loop. */ while ( !slapd_shutdown ) { - ber_socket_t i; - int ns, nwriters; - int at; - ber_socket_t nfds; + ber_socket_t i; + int ns, nwriters; + int at; + ber_socket_t nfds; #if SLAP_EVENTS_ARE_INDEXED - ber_socket_t nrfds, nwfds; -#endif + ber_socket_t nrfds, nwfds; +#endif /* SLAP_EVENTS_ARE_INDEXED */ #define SLAPD_EBADF_LIMIT 16 - time_t now; + time_t now; SLAP_EVENT_DECL; @@ -1705,23 +2025,25 @@ slapd_daemon_task( struct timeval *tvp; struct timeval cat; - time_t tdelta = 1; + time_t tdelta = 1; struct re_s* rtask; + now = slap_get_time(); - if( ( global_idletimeout > 0 ) && + if ( ( global_idletimeout > 0 ) && difftime( last_idle_check + - global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) { + global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) + { connections_timeout_idle( now ); last_idle_check = now; } tv = idle; #ifdef SIGHUP - if( slapd_gentle_shutdown ) { + if ( slapd_gentle_shutdown ) { ber_socket_t active; - if( slapd_gentle_shutdown == 1 ) { + if ( slapd_gentle_shutdown == 1 ) { BackendDB *be; Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 ); close_listeners( 1 ); @@ -1735,12 +2057,12 @@ slapd_daemon_task( ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex ); active = slap_daemon.sd_nactives; ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex ); - if( active == 0 ) { + if ( active == 0 ) { slapd_shutdown = 1; break; } } -#endif +#endif /* SIGHUP */ at = 0; ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex ); @@ -1754,11 +2076,11 @@ slapd_daemon_task( #ifdef SLAP_LIGHTWEIGHT_DISPATCHER if ( lr->sl_mute || lr->sl_busy ) -#else +#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */ if ( lr->sl_mute ) -#endif +#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */ { - SLAP_SOCK_CLR_READ( lr->sl_sd ); + SLAP_SOCK_CLR_READ( lr->sl_sd ); } else { SLAP_SOCK_SET_READ( lr->sl_sd ); } @@ -1775,7 +2097,7 @@ slapd_daemon_task( if ( at #if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS) && ( tv.tv_sec || tv.tv_usec ) -#endif +#endif /* HAVE_YIELDING_SELECT || NO_THREADS */ ) { tvp = &tv; @@ -1793,7 +2115,7 @@ slapd_daemon_task( ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 ); ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex ); ldap_pvt_thread_pool_submit( &connection_pool, - rtask->routine, (void *) rtask ); + rtask->routine, (void *) rtask ); ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex ); } rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat ); @@ -1819,7 +2141,8 @@ slapd_daemon_task( if ( lr->sl_mute ) { Debug( LDAP_DEBUG_CONNS, - "daemon: select: listen=%d muted\n", + "daemon: " SLAP_EVENT_FNAME ": " + "listen=%d muted\n", lr->sl_sd, 0, 0 ); continue; } @@ -1827,32 +2150,40 @@ slapd_daemon_task( #ifdef SLAP_LIGHTWEIGHT_DISPATCHER if ( lr->sl_busy ) { Debug( LDAP_DEBUG_CONNS, - "daemon: select: listen=%d busy\n", + "daemon: " SLAP_EVENT_FNAME ": " + "listen=%d busy\n", lr->sl_sd, 0, 0 ); continue; } -#endif +#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */ Debug( LDAP_DEBUG_CONNS, - "daemon: select: listen=%d active_threads=%d tvp=%s\n", + "daemon: " SLAP_EVENT_FNAME ": " + "listen=%d active_threads=%d tvp=%s\n", lr->sl_sd, at, tvp == NULL ? "NULL" : "zero" ); } - switch(ns = SLAP_EVENT_WAIT(tvp)) { + SLAP_EVENT_WAIT( tvp, &ns ); + switch ( ns ) { case -1: { /* failure - try again */ int err = sock_errno(); - if( err != EINTR ) { + if ( err != EINTR ) { ebadf++; /* Don't log unless we got it twice in a row */ - if ( !( ebadf & 1 )) { + if ( !( ebadf & 1 ) ) { Debug( LDAP_DEBUG_ANY, - "daemon: select failed count %d err (%d): %s\n", - ebadf, err, sock_errstr(err) ); + "daemon: " + SLAP_EVENT_FNAME + "failed count %d " + "err (%d): %s\n", + ebadf, err, + sock_errstr( err ) ); } - if ( ebadf >= SLAPD_EBADF_LIMIT ) + if ( ebadf >= SLAPD_EBADF_LIMIT ) { slapd_shutdown = 2; + } } } continue; @@ -1860,15 +2191,16 @@ slapd_daemon_task( case 0: /* timeout - let threads run */ ebadf = 0; #ifndef HAVE_YIELDING_SELECT - Debug( LDAP_DEBUG_CONNS, "daemon: select timeout - yielding\n", - 0, 0, 0 ); + Debug( LDAP_DEBUG_CONNS, "daemon: " SLAP_EVENT_FNAME + "timeout - yielding\n", + 0, 0, 0 ); ldap_pvt_thread_yield(); -#endif +#endif /* ! HAVE_YIELDING_SELECT */ continue; default: /* something happened - deal with it */ - if( slapd_shutdown ) continue; + if ( slapd_shutdown ) continue; ebadf = 0; Debug( LDAP_DEBUG_CONNS, @@ -1878,7 +2210,7 @@ slapd_daemon_task( } #if SLAP_EVENTS_ARE_INDEXED - if ( SLAP_EVENT_IS_READ( wake_sds[0] )) { + if ( SLAP_EVENT_IS_READ( wake_sds[0] ) ) { char c[BUFSIZ]; SLAP_EVENT_CLR_READ( wake_sds[0] ); waking = 0; @@ -1898,8 +2230,8 @@ slapd_daemon_task( if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ) continue; #ifdef LDAP_CONNECTIONLESS if ( slap_listeners[l]->sl_is_udp ) continue; -#endif - if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd )) continue; +#endif /* LDAP_CONNECTIONLESS */ + if ( !SLAP_EVENT_IS_READ( slap_listeners[l]->sl_sd ) ) continue; /* clear events */ SLAP_EVENT_CLR_READ( slap_listeners[l]->sl_sd ); @@ -1907,17 +2239,17 @@ slapd_daemon_task( ns--; #ifdef SLAP_LIGHTWEIGHT_DISPATCHER - rc = slap_listener_activate(slap_listeners[l]); -#else - rc = slap_listener(slap_listeners[l]); -#endif + rc = slap_listener_activate( slap_listeners[l] ); +#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */ + rc = slap_listener( slap_listeners[l] ); +#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */ } /* bypass the following tests if no descriptors left */ if ( ns <= 0 ) { #ifndef HAVE_YIELDING_SELECT ldap_pvt_thread_yield(); -#endif +#endif /* HAVE_YIELDING_SELECT */ continue; } @@ -1934,7 +2266,7 @@ slapd_daemon_task( writefds.fd_array[i], "w", 0 ); } -#else +#else /* ! HAVE_WINSOCK */ nrfds = 0; nwfds = 0; for ( i = 0; i < nfds; i++ ) { @@ -1957,19 +2289,18 @@ slapd_daemon_task( } if ( ns <= 0 ) break; } -#endif +#endif /* ! HAVE_WINSOCK */ Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 ); - /* loop through the writers */ for ( i = 0; nwfds > 0; i++ ) { ber_socket_t wd; #ifdef HAVE_WINSOCK wd = writefds.fd_array[i]; -#else - if( ! SLAP_EVENT_IS_WRITE( i ) ) continue; +#else /* ! HAVE_WINSOCK */ + if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue; wd = i; -#endif +#endif /* ! HAVE_WINSOCK */ SLAP_EVENT_CLR_WRITE( wd ); nwfds--; @@ -1978,9 +2309,6 @@ slapd_daemon_task( "daemon: write active on %d\n", wd, 0, 0 ); -#ifdef SLAP_LIGHTWEIGHT_DISPATCHER - connection_write_activate( wd ); -#else /* * NOTE: it is possible that the connection was closed * and that the stream is now inactive. @@ -1991,22 +2319,21 @@ slapd_daemon_task( * close it here. It has already been closed in connection.c. */ if ( connection_write( wd ) < 0 ) { - if ( SLAP_EVENT_IS_READ( wd )) { + if ( SLAP_EVENT_IS_READ( wd ) ) { SLAP_EVENT_CLR_READ( (unsigned) wd ); nrfds--; } } -#endif } for ( i = 0; nrfds > 0; i++ ) { ber_socket_t rd; #ifdef HAVE_WINSOCK rd = readfds.fd_array[i]; -#else - if( ! SLAP_EVENT_IS_READ( i ) ) continue; +#else /* ! HAVE_WINSOCK */ + if ( ! SLAP_EVENT_IS_READ( i ) ) continue; rd = i; -#endif +#endif /* ! HAVE_WINSOCK */ SLAP_EVENT_CLR_READ( rd ); nrfds--; @@ -2021,9 +2348,9 @@ slapd_daemon_task( #ifdef SLAP_LIGHTWEIGHT_DISPATCHER connection_read_activate( rd ); -#else +#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */ connection_read( rd ); -#endif +#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */ } #else /* !SLAP_EVENTS_ARE_INDEXED */ /* FIXME */ @@ -2045,14 +2372,14 @@ slapd_daemon_task( #ifdef LDAP_DEBUG Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 ); - for (i=0; isl_is_udp) -#endif + && !( (SLAP_EVENT_LISTENER( i ))->sl_is_udp ) +#endif /* LDAP_CONNECTIONLESS */ ) { continue; @@ -2069,17 +2396,17 @@ slapd_daemon_task( } } Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 ); -#endif +#endif /* LDAP_DEBUG */ - for (i=0; isl_is_udp ) { + if ( !lr->sl_is_udp ) { continue; } - id = connection_init( lr->sl_sd, lr, "", "", CONN_IS_UDP, (slap_ssf_t) 0, NULL ); + id = connection_init( lr->sl_sd, lr, "", "", + CONN_IS_UDP, (slap_ssf_t) 0, NULL ); - if( id < 0 ) { + if ( id < 0 ) { Debug( LDAP_DEBUG_TRACE, - "connectionless_init: failed on %s (%d)\n", lr->sl_url, lr->sl_sd, 0 ); + "connectionless_init: failed on %s (%d)\n", + lr->sl_url, lr->sl_sd, 0 ); return -1; } lr->sl_is_udp++; @@ -2212,14 +2542,15 @@ static int connectionless_init(void) } #endif /* LDAP_CONNECTIONLESS */ -int slapd_daemon( void ) +int +slapd_daemon( void ) { int rc; connections_init(); #ifdef LDAP_CONNECTIONLESS connectionless_init(); -#endif +#endif /* LDAP_CONNECTIONLESS */ #define SLAPD_LISTENER_THREAD 1 #if defined( SLAPD_LISTENER_THREAD ) @@ -2237,21 +2568,21 @@ int slapd_daemon( void ) } /* wait for the listener thread to complete */ - ldap_pvt_thread_join( listener_tid, (void *) NULL ); + ldap_pvt_thread_join( listener_tid, (void *)NULL ); } -#else +#else /* ! SLAPD_LISTENER_THREAD */ /* experimental code */ slapd_daemon_task( NULL ); -#endif +#endif /* ! SLAPD_LISTENER_THREAD */ return 0; - } -static int sockinit(void) +static int +sockinit( void ) { #if defined( HAVE_WINSOCK2 ) - WORD wVersionRequested; + WORD wVersionRequested; WSADATA wsaData; int err; @@ -2283,16 +2614,19 @@ static int sockinit(void) #elif defined( HAVE_WINSOCK ) WSADATA wsaData; if ( WSAStartup( 0x0101, &wsaData ) != 0 ) return -1; -#endif +#endif /* ! HAVE_WINSOCK2 && ! HAVE_WINSOCK */ return 0; } -static int sockdestroy(void) +static int +sockdestroy( void ) { #if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK ) WSACleanup(); -#endif +#endif /* HAVE_WINSOCK2 || HAVE_WINSOCK */ + SLAP_SOCK_DESTROY; + return 0; } @@ -2313,12 +2647,12 @@ slap_sig_shutdown( int sig ) if (is_NT_Service && sig == SIGBREAK) { /* empty */; } else -#endif +#endif /* HAVE_NT_SERVICE_MANAGER && SIGBREAK */ #ifdef SIGHUP if (sig == SIGHUP && global_gentlehup && slapd_gentle_shutdown == 0) { slapd_gentle_shutdown = 1; } else -#endif +#endif /* SIGHUP */ { slapd_shutdown = 1; } @@ -2339,14 +2673,20 @@ slap_sig_wake( int sig ) } -void slapd_add_internal(ber_socket_t s, int isactive) { - slapd_add(s, isactive, NULL); +void +slapd_add_internal( ber_socket_t s, int isactive ) +{ + slapd_add( s, isactive, NULL ); } -Listener ** slapd_get_listeners(void) { +Listener ** +slapd_get_listeners( void ) +{ return slap_listeners; } -void slap_wake_listener() { +void +slap_wake_listener() +{ WAKE_LISTENER(1); } diff --git a/servers/slapd/operation.c b/servers/slapd/operation.c index 28054793f1..b62942d2d2 100644 --- a/servers/slapd/operation.c +++ b/servers/slapd/operation.c @@ -166,3 +166,32 @@ slap_op_alloc( return( op ); } + +slap_op_t +slap_req2op( ber_tag_t tag ) +{ + switch ( tag ) { + case LDAP_REQ_BIND: + return SLAP_OP_BIND; + case LDAP_REQ_UNBIND: + return SLAP_OP_UNBIND; + case LDAP_REQ_ADD: + return SLAP_OP_ADD; + case LDAP_REQ_DELETE: + return SLAP_OP_DELETE; + case LDAP_REQ_MODRDN: + return SLAP_OP_MODRDN; + case LDAP_REQ_MODIFY: + return SLAP_OP_MODIFY; + case LDAP_REQ_COMPARE: + return SLAP_OP_COMPARE; + case LDAP_REQ_SEARCH: + return SLAP_OP_SEARCH; + case LDAP_REQ_ABANDON: + return SLAP_OP_ABANDON; + case LDAP_REQ_EXTENDED: + return SLAP_OP_EXTENDED; + } + + return SLAP_OP_LAST; +} diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h index a983f713bc..41620b4c24 100644 --- a/servers/slapd/proto-slap.h +++ b/servers/slapd/proto-slap.h @@ -720,8 +720,8 @@ LDAP_SLAPD_F (int) slapd_daemon_init( const char *urls ); LDAP_SLAPD_F (int) slapd_daemon_destroy(void); LDAP_SLAPD_F (int) slapd_daemon(void); LDAP_SLAPD_F (Listener **) slapd_get_listeners LDAP_P((void)); -LDAP_SLAPD_F (void) slapd_remove LDAP_P((ber_socket_t s, int wasactive, - int wake, int locked )); +LDAP_SLAPD_F (void) slapd_remove LDAP_P((ber_socket_t s, Sockbuf *sb, + int wasactive, int wake, int locked )); LDAP_SLAPD_F (void) slapd_sd_lock(); LDAP_SLAPD_F (void) slapd_sd_unlock(); @@ -1241,6 +1241,7 @@ LDAP_SLAPD_F (Operation *) slap_op_alloc LDAP_P(( LDAP_SLAPD_F (int) slap_op_add LDAP_P(( Operation **olist, Operation *op )); LDAP_SLAPD_F (int) slap_op_remove LDAP_P(( Operation **olist, Operation *op )); LDAP_SLAPD_F (Operation *) slap_op_pop LDAP_P(( Operation **olist )); +LDAP_SLAPD_F (slap_op_t) slap_req2op LDAP_P(( ber_tag_t tag )); /* * operational.c diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h index 0f2f92d37e..4d9f48dbbb 100644 --- a/servers/slapd/slap.h +++ b/servers/slapd/slap.h @@ -2718,7 +2718,7 @@ struct slap_listener { /* * Operation indices */ -enum { +typedef enum { SLAP_OP_BIND = 0, SLAP_OP_UNBIND, SLAP_OP_ADD, @@ -2730,7 +2730,7 @@ enum { SLAP_OP_ABANDON, SLAP_OP_EXTENDED, SLAP_OP_LAST -}; +} slap_op_t; typedef struct slap_counters_t { ldap_pvt_thread_mutex_t sc_sent_mutex; -- 2.39.5