#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;
#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) */
* 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;
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;
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;
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.
static Connection* connection_get( ber_socket_t s )
{
- /* connections_mutex should be locked by caller */
-
Connection *c;
Debug( LDAP_DEBUG_ARGS,
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; i<dtblsize; i++) {
+ if( connections[i].c_struct_state == SLAP_C_PENDING )
+ continue;
+
if( connections[i].c_struct_state == SLAP_C_UNINITIALIZED ) {
assert( connections[i].c_conn_state == SLAP_C_INVALID );
assert( connections[i].c_sb == 0 );
break;
}
}
+ ldap_pvt_thread_mutex_unlock( &connections_mutex );
}
#endif
if( c != NULL ) {
ber_socket_t sd;
+ assert( c->c_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 );
{
unsigned long id;
Connection *c;
+ int doinit = 0;
assert( connections != NULL );
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;
}
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;
}
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;
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;
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 ) );
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 );
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;
}
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);
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;
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 )
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;
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 );
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 );
Debug( LDAP_DEBUG_TRACE, "connection_close: conn=%lu sd=%d\n",
c->c_connid, sd, 0 );
+
connection_destroy( c );
}
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 );
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
}
/*
/*
* 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,
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;
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:
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.
*/
*/
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 );
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 */
op->o_cancel = LDAP_TOO_LATE;
}
}
+
while ( op->o_cancel != SLAP_CANCEL_NONE &&
op->o_cancel != SLAP_CANCEL_DONE )
{
ber_socket_t s )
{
Connection *c;
+ Sockbuf *sb;
/* get (locked) connection */
c = connection_get( s );
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 );
}
assert( connections != NULL );
- ldap_pvt_thread_mutex_lock( MCA_GET_CONN_MUTEX(s) );
-
/* get (locked) connection */
c = connection_get( s );
"connection_read(%ld): no connection!\n",
(long) s, 0, 0 );
- ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
return -1;
}
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;
}
c->c_clientfunc, c->c_clientarg );
#endif
connection_return( c );
- ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
return 0;
}
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 ) {
}
/* 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;
}
}
#endif
connection_return( c );
- ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
return 0;
}
"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;
}
}
#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;
}
#endif
connection_return( c );
- ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
return 0;
}
return -1;
}
- errno = 0;
+ sock_errset(0);
#ifdef LDAP_CONNECTIONLESS
if ( conn->c_is_udp ) {
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 );
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;
}
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 =
}
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;
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;
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,
}
connection_return( c );
- ldap_pvt_thread_mutex_unlock( MCA_GET_CONN_MUTEX(s) );
return 0;
}
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;
#if defined(HAVE_SYS_EPOLL_H) && defined(HAVE_EPOLL)
# include <sys/epoll.h>
-#endif
+#elif defined(SLAP_X_DEVPOLL) && defined(HAVE_SYS_DEVPOLL_H) && defined(HAVE_DEVPOLL)
+# include <sys/types.h>
+# include <sys/stat.h>
+# include <fcntl.h>
+# include <sys/devpoll.h>
+#endif /* ! epoll && ! /dev/poll */
#ifdef HAVE_TCPD
# include <tcpd.h>
#ifdef LDAP_PF_INET6
int slap_inet4or6 = AF_UNSPEC;
-#else
+#else /* ! INETv6 */
int slap_inet4or6 = AF_INET;
-#endif
+#endif /* ! INETv6 */
/* globals */
time_t starttime;
#ifndef SLAPD_LISTEN_BACKLOG
#define SLAPD_LISTEN_BACKLOG 1024
-#endif
+#endif /* ! SLAPD_LISTEN_BACKLOG */
static ber_socket_t wake_sds[2];
static int emfile;
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;
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_<type>_* 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 { \
} \
} 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; i<dtblsize; i++) slap_daemon.sd_index[i] = -1; \
+ for ( i = 0; i < dtblsize; i++ ) slap_daemon.sd_index[i] = -1; \
} while (0)
-# define SLAP_EVENT_DECL struct epoll_event *revents
+# define SLAP_SOCK_DESTROY do { \
+ if ( slap_daemon.sd_epolls != NULL ) { \
+ ch_free( slap_daemon.sd_epolls ); \
+ slap_daemon.sd_epolls = NULL; \
+ slap_daemon.sd_index = NULL; \
+ close( slap_daemon.sd_epfd ); \
+ } \
+} while ( 0 )
+
+# define SLAP_EVENT_DECL struct epoll_event *revents
-# define SLAP_EVENT_INIT do { \
+# define SLAP_EVENT_INIT do { \
revents = slap_daemon.sd_epolls + dtblsize; \
} while (0)
-# define SLAP_EVENT_WAIT(tvp) \
- epoll_wait( slap_daemon.sd_epfd, revents, \
- dtblsize, (tvp) ? (tvp)->tv_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) ); \
} 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)
# 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)
# 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
/*
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],
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],
}
/* 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;
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 */
* 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 );
}
/*
* Remove the descriptor from daemon control
*/
-void slapd_remove(
+void
+slapd_remove(
ber_socket_t s,
+ Sockbuf *sb,
int wasactive,
int wake,
int locked )
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
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 ));
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 ));
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 );
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 ));
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);
}
#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 )
#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;
(*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;
*(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) {
freeaddrinfo(res);
-#else
+#else /* ! HAVE_GETADDRINFO */
int i, n = 1;
struct in_addr in;
struct hostent *he = NULL;
sizeof(struct in_addr) );
}
sap[i] = NULL;
-#endif
+#endif /* ! HAVE_GETADDRINFO */
}
return 0;
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;
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 ) ) {
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;
} 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 )
#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 ) {
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;
#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 ) {
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 */
"setsockopt(SO_REUSEADDR) failed errno=%d (%s)\n",
(long) l.sl_sd, err, sock_errstr(err) );
}
-#endif
+#endif /* SO_REUSEADDR */
}
switch( (*sal)->sa_family ) {
"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;
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") );
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;
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;
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
return rc;
}
- SLAP_SOCK_SET_INIT;
+ SLAP_SOCK_INIT;
if( urls == NULL ) urls = "ldap:///";
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;
}
slapd_slp_init( urls );
slapd_slp_reg();
}
-#endif
+#endif /* HAVE_SLP */
ldap_charray_free( u );
int
-slapd_daemon_destroy(void)
+slapd_daemon_destroy( void )
{
connections_destroy();
tcp_close( wake_sds[1] );
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;
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 ) {
struct berval authid = BER_BVNULL;
#ifdef SLAPD_RLOOKUPS
char hbuf[NI_MAXHOST];
-#endif
+#endif /* SLAPD_RLOOKUPS */
char *dnsname = NULL;
char *peeraddr = NULL;
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
*/
sl->sl_busy = 0;
WAKE_LISTENER(1);
-#endif
+#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
if ( s == AC_SOCKET_INVALID ) {
int err = sock_errno();
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 );
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
"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;
"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",
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;
#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,
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 );
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;
}
return rc;
}
-#endif
+#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
static void *
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();
}
}
}
-#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,
slapd_shutdown = 2;
return (void*)-1;
}
-#endif
+#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
}
if ( started_event != NULL ) {
ldap_pvt_thread_cond_signal( &started_event );
}
-#endif
+#endif /* HAVE_NT_SERVICE_MANAGER */
#ifdef SLAP_SEM_LOAD_CONTROL
/*
(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;
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 );
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 );
#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 );
}
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;
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 );
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;
}
#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;
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,
}
#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;
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 );
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;
}
writefds.fd_array[i], "w", 0 );
}
-#else
+#else /* ! HAVE_WINSOCK */
nrfds = 0;
nwfds = 0;
for ( i = 0; i < nfds; i++ ) {
}
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--;
"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.
* 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--;
#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 */
#ifdef LDAP_DEBUG
Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
- for (i=0; i<ns; i++) {
+ for ( i = 0; i < ns; i++ ) {
int r, w;
/* Don't log listener events */
- if ( SLAP_EVENT_IS_LISTENER(i)
+ if ( SLAP_EVENT_IS_LISTENER( i )
#ifdef LDAP_CONNECTIONLESS
- && !((SLAP_EVENT_LISTENER(i))->sl_is_udp)
-#endif
+ && !( (SLAP_EVENT_LISTENER( i ))->sl_is_udp )
+#endif /* LDAP_CONNECTIONLESS */
)
{
continue;
}
}
Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
-#endif
+#endif /* LDAP_DEBUG */
- for (i=0; i<ns; i++) {
- int rc = 1, fd, waswrite = 0;
+ for ( i = 0; i < ns; i++ ) {
+ int rc = 1, fd;
- if ( SLAP_EVENT_IS_LISTENER(i) ) {
+ if ( SLAP_EVENT_IS_LISTENER( i ) ) {
#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
- rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ));
-#else
- rc = slap_listener( SLAP_EVENT_LISTENER( i ));
-#endif
+ rc = slap_listener_activate( SLAP_EVENT_LISTENER( i ) );
+#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
+ rc = slap_listener( SLAP_EVENT_LISTENER( i ) );
+#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
}
/* If we found a regular listener, rc is now zero, and we
break;
}
- if( SLAP_EVENT_IS_WRITE( i ) ) {
+ if ( SLAP_EVENT_IS_WRITE( i ) ) {
Debug( LDAP_DEBUG_CONNS,
"daemon: write active on %d\n",
fd, 0, 0 );
- waswrite = 1;
+ SLAP_EVENT_CLR_WRITE( i );
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
- connection_write_activate( fd );
-#else
/*
* NOTE: it is possible that the connection was closed
* and that the stream is now inactive.
if ( connection_write( fd ) < 0 ) {
continue;
}
-#endif
}
- /* If event is a read or an error */
- if( SLAP_EVENT_IS_READ( i ) || !waswrite ) {
+ /* If event is a read */
+ if ( SLAP_EVENT_IS_READ( i ) ) {
Debug( LDAP_DEBUG_CONNS,
"daemon: read active on %d\n",
fd, 0, 0 );
+ SLAP_EVENT_CLR_READ( i );
#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
connection_read_activate( fd );
-#else
+#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
/*
* NOTE: it is possible that the connection was closed
* and that the stream is now inactive.
* active.
*/
connection_read( fd );
-#endif
+#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
+ } else {
+ Debug( LDAP_DEBUG_CONNS,
+ "daemon: hangup on %d\n", fd, 0, 0 );
}
}
}
#ifndef HAVE_YIELDING_SELECT
ldap_pvt_thread_yield();
-#endif
+#endif /* ! HAVE_YIELDING_SELECT */
}
- if( slapd_shutdown == 1 ) {
+ if ( slapd_shutdown == 1 ) {
Debug( LDAP_DEBUG_ANY,
"daemon: shutdown requested and initiated.\n",
0, 0, 0 );
0, 0, 0 );
}
- if( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
+ if ( slapd_gentle_shutdown != 2 ) close_listeners ( 0 );
- if( !slapd_gentle_shutdown ) {
+ if ( !slapd_gentle_shutdown ) {
slapd_abrupt_shutdown = 1;
connections_shutdown();
}
Debug( LDAP_DEBUG_ANY,
- "slapd shutdown: waiting for %d threads to terminate\n",
- ldap_pvt_thread_pool_backload(&connection_pool), 0, 0 );
- ldap_pvt_thread_pool_destroy(&connection_pool, 1);
+ "slapd shutdown: waiting for %d threads to terminate\n",
+ ldap_pvt_thread_pool_backload( &connection_pool ), 0, 0 );
+ ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
- free ( slap_listeners );
+ free( slap_listeners );
slap_listeners = NULL;
return NULL;
#ifdef LDAP_CONNECTIONLESS
-static int connectionless_init(void)
+static int
+connectionless_init( void )
{
int l;
Listener *lr = slap_listeners[l];
long id;
- if( !lr->sl_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++;
}
#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 )
}
/* 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;
#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;
}
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;
}
}
-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);
}