Streamlined Winsock connection management

This commit is contained in:
Howard Chu 2007-11-26 17:03:26 +00:00
parent 53b87d1dbe
commit eb16972d23
4 changed files with 260 additions and 218 deletions

View file

@ -254,57 +254,14 @@ static Connection* connection_get( ber_socket_t s )
if(s == AC_SOCKET_INVALID) return NULL;
#ifndef HAVE_WINSOCK
assert( s < dtblsize );
c = &connections[s];
#else
c = NULL;
{
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;
}
if( connections[i].c_struct_state == SLAP_C_UNUSED ) {
assert( connections[i].c_conn_state == SLAP_C_INVALID );
assert( connections[i].c_sd == AC_SOCKET_INVALID );
continue;
}
/* state can actually change from used -> unused by resched,
* so don't assert details here.
*/
if( connections[i].c_sd == s ) {
c = &connections[i];
break;
}
}
ldap_pvt_thread_mutex_unlock( &connections_mutex );
}
#endif
if( c != NULL ) {
ldap_pvt_thread_mutex_lock( &c->c_mutex );
assert( c->c_struct_state != SLAP_C_UNINITIALIZED );
#ifdef HAVE_WINSOCK
/* Avoid race condition after releasing
* connections_mutex
*/
if ( c->c_sd != s ) {
ldap_pvt_thread_mutex_unlock( &c->c_mutex );
return NULL;
}
#endif
if( c->c_struct_state != SLAP_C_USED ) {
/* connection must have been closed due to resched */
@ -358,6 +315,7 @@ Connection * connection_init(
unsigned long id;
Connection *c;
int doinit = 0;
ber_socket_t sfd = SLAP_FD2SOCK(s);
assert( connections != NULL );
@ -376,7 +334,6 @@ Connection * connection_init(
}
assert( s >= 0 );
#ifndef HAVE_WINSOCK
assert( s < dtblsize );
c = &connections[s];
if( c->c_struct_state == SLAP_C_UNINITIALIZED ) {
@ -384,45 +341,6 @@ Connection * connection_init(
} 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++) {
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 ) {
c = &connections[i];
c->c_struct_state = SLAP_C_PENDING;
break;
}
if( connections[i].c_conn_state == SLAP_C_CLIENT ) continue;
assert( connections[i].c_struct_state == SLAP_C_USED );
assert( connections[i].c_conn_state != SLAP_C_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);
return NULL;
}
}
#endif
if( doinit ) {
c->c_send_ldap_result = slap_send_ldap_result;
@ -508,7 +426,7 @@ Connection * connection_init(
c->c_conn_state = SLAP_C_CLIENT;
c->c_struct_state = SLAP_C_USED;
c->c_close_reason = "?"; /* should never be needed */
ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &s );
ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_SET_FD, &sfd );
ldap_pvt_thread_mutex_unlock( &c->c_mutex );
return c;
@ -545,7 +463,7 @@ Connection * connection_init(
LBER_SBIOD_LEVEL_PROVIDER, (void*)"udp_" );
#endif
ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_udp,
LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_readahead,
LBER_SBIOD_LEVEL_PROVIDER, NULL );
} else
@ -557,7 +475,7 @@ Connection * connection_init(
LBER_SBIOD_LEVEL_PROVIDER, (void*)"ipc_" );
#endif
ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_fd,
LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
#ifdef LDAP_PF_LOCAL_SENDMSG
if ( !BER_BVISEMPTY( peerbv ))
ber_sockbuf_ctrl( c->c_sb, LBER_SB_OPT_UNGET_BUF, peerbv );
@ -570,7 +488,7 @@ Connection * connection_init(
LBER_SBIOD_LEVEL_PROVIDER, (void*)"tcp_" );
#endif
ber_sockbuf_add_io( c->c_sb, &ber_sockbuf_io_tcp,
LBER_SBIOD_LEVEL_PROVIDER, (void *)&s );
LBER_SBIOD_LEVEL_PROVIDER, (void *)&sfd );
}
#ifdef LDAP_DEBUG
@ -926,11 +844,7 @@ Connection* connection_next( Connection *c, ber_socket_t *index )
int c_struct;
if( connections[*index].c_struct_state == SLAP_C_UNINITIALIZED ) {
assert( connections[*index].c_conn_state == SLAP_C_INVALID );
#ifdef HAVE_WINSOCK
break;
#else
continue;
#endif
}
if( connections[*index].c_struct_state == SLAP_C_USED ) {
@ -1219,37 +1133,38 @@ operations_error:
static const Listener dummy_list = { BER_BVC(""), BER_BVC("") };
int connection_client_setup(
Connection *connection_client_setup(
ber_socket_t s,
ldap_pvt_thread_start_t *func,
void *arg )
{
Connection *c;
ber_socket_t sfd = SLAP_SOCKNEW( s );
c = connection_init( s, (Listener *)&dummy_list, "", "",
c = connection_init( sfd, (Listener *)&dummy_list, "", "",
CONN_IS_CLIENT, 0, NULL
LDAP_PF_LOCAL_SENDMSG_ARG(NULL));
if ( !c ) return -1;
if ( c ) {
c->c_clientfunc = func;
c->c_clientarg = arg;
c->c_clientfunc = func;
c->c_clientarg = arg;
slapd_add_internal( s, 0 );
slapd_set_read( s, 1 );
return 0;
slapd_add_internal( sfd, 0 );
slapd_set_read( sfd, 1 );
}
return c;
}
void connection_client_enable(
ber_socket_t s )
Connection *c )
{
slapd_set_read( s, 1 );
slapd_set_read( c->c_sd, 1 );
}
void connection_client_stop(
ber_socket_t s )
Connection *c )
{
Connection *c;
Sockbuf *sb;
ber_socket_t s = c->c_sd;
/* get (locked) connection */
c = connection_get( s );
@ -1277,7 +1192,7 @@ static int connection_read( ber_socket_t s, conn_readinfo *cri );
static void* connection_read_thread( void* ctx, void* argv )
{
int rc ;
conn_readinfo cri = { NULL, NULL, NULL, 0 };
conn_readinfo cri = { NULL, NULL, NULL, NULL, 0 };
ber_socket_t s = (long)argv;
/*

View file

@ -2,6 +2,7 @@
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 1998-2007 The OpenLDAP Foundation.
* Portions Copyright 2007 by Howard Chu, Symas Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -85,13 +86,13 @@ static volatile int waking;
#ifdef NO_THREADS
#define WAKE_LISTENER(w) do { \
if ((w) && ++waking < 5) { \
tcp_write( wake_sds[1], "0", 1 ); \
tcp_write( SLAP_FD2SOCK(wake_sds[1]), "0", 1 ); \
} \
} while (0)
#else /* ! NO_THREADS */
#define WAKE_LISTENER(w) do { \
if (w) { \
tcp_write( wake_sds[1], "0", 1 ); \
tcp_write( SLAP_FD2SOCK(wake_sds[1]), "0", 1 ); \
} \
} while (0)
#endif /* ! NO_THREADS */
@ -100,6 +101,15 @@ volatile sig_atomic_t slapd_shutdown = 0;
volatile sig_atomic_t slapd_gentle_shutdown = 0;
volatile sig_atomic_t slapd_abrupt_shutdown = 0;
#ifdef HAVE_WINSOCK
ldap_pvt_thread_mutex_t slapd_ws_mutex;
SOCKET *slapd_ws_sockets;
#define SD_READ 1
#define SD_WRITE 2
#define SD_ACTIVE 4
#define SD_LISTENER 8
#endif
static struct slap_daemon {
ldap_pvt_thread_mutex_t sd_mutex;
#ifdef HAVE_TCPD
@ -108,28 +118,27 @@ static struct slap_daemon {
ber_socket_t sd_nactives;
int sd_nwriters;
int sd_nfds;
#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 /* ! HAVE_WINSOCK */
#ifdef HAVE_WINSOCK
char *sd_flags;
char *sd_rflags;
#else /* ! HAVE_WINSOCK */
fd_set sd_actives;
fd_set sd_readers;
fd_set sd_writers;
#endif /* ! HAVE_WINSOCK */
#endif /* ! epoll && ! /dev/poll */
} slap_daemon;
@ -457,13 +466,119 @@ static struct slap_daemon {
} while (0)
#else /* ! epoll && ! /dev/poll */
# ifdef HAVE_WINSOCK
# define SLAP_EVENT_FNAME "WSselect"
/* Winsock provides a "select" function but its fd_sets are
* actually arrays of sockets. Since these sockets are handles
* and not a contiguous range of small integers, we manage our
* own "fd" table of socket handles and use their indices as
* descriptors.
*
* All of our listener/connection structures use fds; the actual
* I/O functions use sockets. The SLAP_FD2SOCK macro in proto-slap.h
* handles the mapping.
*
* Despite the mapping overhead, this is about 45% more efficient
* than just using Winsock's select and FD_ISSET directly.
*
* Unfortunately Winsock's select implementation doesn't scale well
* as the number of connections increases. This probably needs to be
* rewritten to use the Winsock overlapped/asynchronous I/O functions.
*/
# define SLAP_EVENTS_ARE_INDEXED 1
# define SLAP_EVENT_DECL fd_set readfds, writefds
# define SLAP_EVENT_INIT do { \
int i; \
FD_ZERO( &readfds ); \
FD_ZERO( &writefds ); \
memset( slap_daemon.sd_rflags, 0, slap_daemon.sd_nfds ); \
for ( i=0; i<slap_daemon.sd_nfds; i++ ) { \
if ( slap_daemon.sd_flags[i] & SD_READ ) \
FD_SET( slapd_ws_sockets[i], &readfds );\
if ( slap_daemon.sd_flags[i] & SD_WRITE ) \
FD_SET( slapd_ws_sockets[i], &writefds ); \
} } while ( 0 )
# define SLAP_EVENT_MAX slap_daemon.sd_nfds
# define SLAP_EVENT_WAIT(tvp, nsp) do { \
int i; \
*(nsp) = select( SLAP_EVENT_MAX, &readfds, \
nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
for ( i=0; i<readfds.fd_count; i++) { \
int fd = slapd_sock2fd(readfds.fd_array[i]); \
if ( fd >= 0 ) { \
slap_daemon.sd_rflags[fd] = SD_READ; \
if ( fd >= *(nsp)) *(nsp) = fd+1; \
} \
} \
for ( i=0; i<writefds.fd_count; i++) { \
int fd = slapd_sock2fd(writefds.fd_array[i]); \
if ( fd >= 0 ) { \
slap_daemon.sd_rflags[fd] = SD_WRITE; \
if ( fd >= *(nsp)) *(nsp) = fd+1; \
} \
} \
} while (0)
# define SLAP_EVENT_IS_READ(fd) (slap_daemon.sd_rflags[fd] & SD_READ)
# define SLAP_EVENT_IS_WRITE(fd) (slap_daemon.sd_rflags[fd] & SD_WRITE)
# define SLAP_EVENT_CLR_READ(fd) slap_daemon.sd_rflags[fd] &= ~SD_READ;
# define SLAP_EVENT_CLR_WRITE(fd) slap_daemon.sd_rflags[fd] &= ~SD_WRITE;
# define SLAP_SOCK_INIT do { \
ldap_pvt_thread_mutex_init( &slapd_ws_mutex ); \
slapd_ws_sockets = ch_malloc( dtblsize * ( sizeof(SOCKET) + 2)); \
slap_daemon.sd_flags = (char *)(slapd_ws_sockets + dtblsize); \
slap_daemon.sd_rflags = slap_daemon.sd_flags + dtblsize; \
memset( slapd_ws_sockets, -1, dtblsize * sizeof(SOCKET) ); \
slapd_ws_sockets[0] = wake_sds[0]; \
slapd_ws_sockets[1] = wake_sds[1]; \
wake_sds[0] = 0; \
wake_sds[1] = 1; \
slap_daemon.sd_nfds = 2; \
} while ( 0 )
# define SLAP_SOCK_DESTROY do { \
ch_free( slapd_ws_sockets ); slapd_ws_sockets = NULL; \
slap_daemon.sd_flags = NULL; \
slap_daemon.sd_rflags = NULL; \
ldap_pvt_thread_mutex_destroy( &slapd_ws_mutex ); \
} while ( 0 )
# define SLAP_SOCK_IS_ACTIVE(fd) ( slap_daemon.sd_flags[fd] & SD_ACTIVE )
# define SLAP_SOCK_IS_READ(fd) ( slap_daemon.sd_flags[fd] & SD_READ )
# define SLAP_SOCK_IS_WRITE(fd) ( slap_daemon.sd_flags[fd] & SD_WRITE )
# define SLAP_SOCK_NOT_ACTIVE(fd) (!slap_daemon.sd_flags[fd])
# define SLAP_SOCK_SET_READ(fd) ( slap_daemon.sd_flags[fd] |= SD_READ )
# define SLAP_SOCK_SET_WRITE(fd) ( slap_daemon.sd_flags[fd] |= SD_WRITE )
# define SLAP_SELECT_ADDTEST(s) do { \
if ((s) >= slap_daemon.sd_nfds) slap_daemon.sd_nfds = (s)+1; \
} while (0)
# define SLAP_SOCK_CLR_READ(fd) ( slap_daemon.sd_flags[fd] &= ~SD_READ )
# define SLAP_SOCK_CLR_WRITE(fd) ( slap_daemon.sd_flags[fd] &= ~SD_WRITE )
# define SLAP_SOCK_ADD(s, l) do { \
SLAP_SELECT_ADDTEST((s)); \
slap_daemon.sd_flags[s] = SD_ACTIVE|SD_READ; \
} while ( 0 )
# define SLAP_SOCK_DEL(s) do { \
slap_daemon.sd_flags[s] = 0; \
slapd_sockdel( s ); \
} while ( 0 )
# else /* !HAVE_WINSOCK */
/**************************************
* 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
@ -500,25 +615,13 @@ static struct slap_daemon {
# define SLAP_SOCK_NOT_ACTIVE(fd) (!SLAP_SOCK_IS_ACTIVE(fd) && \
!SLAP_SOCK_IS_READ(fd) && !SLAP_SOCK_IS_WRITE(fd))
# 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)
# define SLAP_SOCK_SET_WRITE(fd) do { \
if (!SLAP_SOCK_IS_WRITE(fd)) { FD_SET((fd), &slap_daemon.sd_writers); } \
} while (0)
# 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_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_SELECT_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 /* ! 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)
@ -545,6 +648,7 @@ static struct slap_daemon {
*(nsp) = select( SLAP_EVENT_MAX, &readfds, \
nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
} while (0)
# endif /* !HAVE_WINSOCK */
#endif /* ! epoll && ! /dev/poll */
#ifdef HAVE_SLP
@ -675,6 +779,42 @@ slapd_slp_dereg( void )
}
#endif /* HAVE_SLP */
#ifdef HAVE_WINSOCK
/* Manage the descriptor to socket table */
ber_socket_t
slapd_socknew( ber_socket_t s )
{
ber_socket_t i;
ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
for ( i = 0; i < dtblsize && slapd_ws_sockets[i] != INVALID_SOCKET; i++ );
if ( i == dtblsize ) {
WSASetLastError( WSAEMFILE );
} else {
slapd_ws_sockets[i] = s;
}
ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
return i;
}
void
slapd_sockdel( ber_socket_t s )
{
ldap_pvt_thread_mutex_lock( &slapd_ws_mutex );
slapd_ws_sockets[s] = INVALID_SOCKET;
ldap_pvt_thread_mutex_unlock( &slapd_ws_mutex );
}
ber_socket_t
slapd_sock2fd( ber_socket_t s )
{
ber_socket_t i;
for ( i=0; i<dtblsize && slapd_ws_sockets[i] != s; i++);
if ( i == dtblsize )
i = -1;
return i;
}
#endif
/*
* Add a descriptor to daemon control
*
@ -848,7 +988,10 @@ slapd_close( ber_socket_t s )
{
Debug( LDAP_DEBUG_CONNS, "daemon: closing %ld\n",
(long) s, 0, 0 );
tcp_close(s);
tcp_close( SLAP_FD2SOCK(s) );
#ifdef HAVE_WINSOCK
slapd_sockdel( s );
#endif
}
static void
@ -1103,6 +1246,7 @@ slap_open_listener(
int err, addrlen = 0;
struct sockaddr **sal, **psal;
int socktype = SOCK_STREAM; /* default to COTS */
ber_socket_t s;
#if defined(LDAP_PF_LOCAL) || defined(SLAP_X_LISTENER_MOD)
/*
@ -1220,8 +1364,8 @@ slap_open_listener(
if( l.sl_is_udp ) socktype = SOCK_DGRAM;
#endif /* LDAP_CONNECTIONLESS */
l.sl_sd = socket( (*sal)->sa_family, socktype, 0);
if ( l.sl_sd == AC_SOCKET_INVALID ) {
s = socket( (*sal)->sa_family, socktype, 0);
if ( s == AC_SOCKET_INVALID ) {
int err = sock_errno();
Debug( LDAP_DEBUG_ANY,
"daemon: %s socket() failed errno=%d (%s)\n",
@ -1229,17 +1373,16 @@ slap_open_listener(
sal++;
continue;
}
l.sl_sd = SLAP_SOCKNEW( s );
#ifndef HAVE_WINSOCK
if ( l.sl_sd >= dtblsize ) {
Debug( LDAP_DEBUG_ANY,
"daemon: listener descriptor %ld is too great %ld\n",
(long) l.sl_sd, (long) dtblsize, 0 );
tcp_close( l.sl_sd );
tcp_close( s );
sal++;
continue;
}
#endif /* ! HAVE_WINSOCK */
#ifdef LDAP_PF_LOCAL
if ( (*sal)->sa_family == AF_LOCAL ) {
@ -1250,7 +1393,7 @@ slap_open_listener(
#ifdef SO_REUSEADDR
/* enable address reuse */
tmp = 1;
rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
rc = setsockopt( s, SOL_SOCKET, SO_REUSEADDR,
(char *) &tmp, sizeof(tmp) );
if ( rc == AC_SOCKET_ERROR ) {
int err = sock_errno();
@ -1270,7 +1413,7 @@ slap_open_listener(
#ifdef IPV6_V6ONLY
/* Try to use IPv6 sockets for IPv6 only */
tmp = 1;
rc = setsockopt( l.sl_sd, IPPROTO_IPV6, IPV6_V6ONLY,
rc = setsockopt( s , IPPROTO_IPV6, IPV6_V6ONLY,
(char *) &tmp, sizeof(tmp) );
if ( rc == AC_SOCKET_ERROR ) {
int err = sock_errno();
@ -1288,7 +1431,7 @@ slap_open_listener(
#ifdef LOCAL_CREDS
{
int one = 1;
setsockopt( l.sl_sd, 0, LOCAL_CREDS, &one, sizeof( one ) );
setsockopt( s, 0, LOCAL_CREDS, &one, sizeof( one ) );
}
#endif /* LOCAL_CREDS */
@ -1312,7 +1455,7 @@ slap_open_listener(
old_umask = umask( 0 );
}
#endif /* LDAP_PF_LOCAL */
rc = bind( l.sl_sd, *sal, addrlen );
rc = bind( s, *sal, addrlen );
#ifdef LDAP_PF_LOCAL
if ( (*sal)->sa_family == AF_LOCAL ) {
umask( old_umask );
@ -1324,7 +1467,7 @@ slap_open_listener(
Debug( LDAP_DEBUG_ANY,
"daemon: bind(%ld) failed errno=%d (%s)\n",
(long)l.sl_sd, err, sock_errstr( err ) );
tcp_close( l.sl_sd );
tcp_close( s );
sal++;
continue;
}
@ -1498,8 +1641,8 @@ int
slapd_daemon_destroy( void )
{
connections_destroy();
tcp_close( wake_sds[1] );
tcp_close( wake_sds[0] );
tcp_close( SLAP_FD2SOCK(wake_sds[1]) );
tcp_close( SLAP_FD2SOCK(wake_sds[0]) );
sockdestroy();
#ifdef HAVE_SLP
@ -1558,7 +1701,7 @@ slap_listener(
{
Sockaddr from;
ber_socket_t s;
ber_socket_t s, sfd;
ber_socklen_t len = sizeof(from);
Connection *c;
slap_ssf_t ssf = 0;
@ -1598,7 +1741,7 @@ slap_listener(
from.sa_un_addr.sun_path[0] = '\0';
# endif /* LDAP_PF_LOCAL */
s = accept( sl->sl_sd, (struct sockaddr *) &from, &len );
s = accept( SLAP_FD2SOCK( sl->sl_sd ), (struct sockaddr *) &from, &len );
/* Resume the listener FD to allow concurrent-processing of
* additional incoming connections.
@ -1631,24 +1774,23 @@ slap_listener(
ldap_pvt_thread_yield();
return 0;
}
sfd = SLAP_SOCKNEW( s );
#ifndef HAVE_WINSOCK
/* make sure descriptor number isn't too great */
if ( s >= dtblsize ) {
if ( sfd >= dtblsize ) {
Debug( LDAP_DEBUG_ANY,
"daemon: %ld beyond descriptor table size %ld\n",
(long) s, (long) dtblsize, 0 );
(long) sfd, (long) dtblsize, 0 );
slapd_close(s);
tcp_close(s);
ldap_pvt_thread_yield();
return 0;
}
#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 ));
assert( SLAP_SOCK_NOT_ACTIVE( sfd ));
ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
#endif /* LDAP_DEBUG */
@ -1669,7 +1811,7 @@ slap_listener(
int err = sock_errno();
Debug( LDAP_DEBUG_ANY,
"slapd(%ld): setsockopt(SO_KEEPALIVE) failed "
"errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
"errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
}
#endif /* SO_KEEPALIVE */
#ifdef TCP_NODELAY
@ -1681,7 +1823,7 @@ slap_listener(
int err = sock_errno();
Debug( LDAP_DEBUG_ANY,
"slapd(%ld): setsockopt(TCP_NODELAY) failed "
"errno=%d (%s)\n", (long) s, err, sock_errstr(err) );
"errno=%d (%s)\n", (long) sfd, err, sock_errstr(err) );
}
#endif /* TCP_NODELAY */
}
@ -1689,7 +1831,7 @@ slap_listener(
Debug( LDAP_DEBUG_CONNS,
"daemon: listen=%ld, new connection on %ld\n",
(long) sl->sl_sd, (long) s, 0 );
(long) sl->sl_sd, (long) sfd, 0 );
cflag = 0;
switch ( from.sa_addr.sa_family ) {
@ -1761,7 +1903,7 @@ slap_listener(
break;
default:
slapd_close(s);
slapd_close(sfd);
return 0;
}
@ -1796,11 +1938,11 @@ slap_listener(
/* DENY ACCESS */
Statslog( LDAP_DEBUG_STATS,
"fd=%ld DENIED from %s (%s)\n",
(long) s,
(long) sfd,
dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
0, 0 );
slapd_close(s);
slapd_close(sfd);
return 0;
}
}
@ -1810,7 +1952,7 @@ slap_listener(
#ifdef HAVE_TLS
if ( sl->sl_is_tls ) cflag |= CONN_IS_TLS;
#endif
c = connection_init(s, sl,
c = connection_init(sfd, sl,
dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
peername, cflag, ssf,
authid.bv_val ? &authid : NULL
@ -1821,14 +1963,14 @@ slap_listener(
if( !c ) {
Debug( LDAP_DEBUG_ANY,
"daemon: connection_init(%ld, %s, %s) failed.\n",
(long) s, peername, sl->sl_name.bv_val );
slapd_close(s);
(long) sfd, peername, sl->sl_name.bv_val );
slapd_close(sfd);
return 0;
}
Statslog( LDAP_DEBUG_STATS,
"conn=%ld fd=%ld ACCEPT from %s (%s)\n",
c->c_connid, (long) s, peername, sl->sl_name.bv_val,
c->c_connid, (long) sfd, peername, sl->sl_name.bv_val,
0 );
return 0;
@ -1915,7 +2057,7 @@ slapd_daemon_task(
continue;
#endif /* LDAP_CONNECTIONLESS */
if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN_BACKLOG ) == -1 ) {
if ( listen( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), SLAPD_LISTEN_BACKLOG ) == -1 ) {
int err = sock_errno();
#ifdef LDAP_PF_INET6
@ -1961,7 +2103,7 @@ slapd_daemon_task(
}
/* make the listening socket non-blocking */
if ( ber_pvt_socket_set_nonblock( slap_listeners[l]->sl_sd, 1 ) < 0 ) {
if ( ber_pvt_socket_set_nonblock( SLAP_FD2SOCK( slap_listeners[l]->sl_sd ), 1 ) < 0 ) {
Debug( LDAP_DEBUG_ANY, "slapd_daemon_task: "
"set nonblocking on a listening socket failed\n",
0, 0, 0 );
@ -2187,7 +2329,7 @@ slapd_daemon_task(
char c[BUFSIZ];
SLAP_EVENT_CLR_READ( wake_sds[0] );
waking = 0;
tcp_read( wake_sds[0], c, sizeof(c) );
tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
continue;
}
@ -2223,19 +2365,6 @@ slapd_daemon_task(
}
Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
#ifdef HAVE_WINSOCK
nrfds = readfds.fd_count;
nwfds = writefds.fd_count;
for ( i = 0; i < readfds.fd_count; i++ ) {
Debug( LDAP_DEBUG_CONNS, " %d%s",
readfds.fd_array[i], "r", 0 );
}
for ( i = 0; i < writefds.fd_count; i++ ) {
Debug( LDAP_DEBUG_CONNS, " %d%s",
writefds.fd_array[i], "w", 0 );
}
#else /* ! HAVE_WINSOCK */
nrfds = 0;
nwfds = 0;
for ( i = 0; i < nfds; i++ ) {
@ -2258,18 +2387,13 @@ slapd_daemon_task(
}
if ( ns <= 0 ) break;
}
#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 /* ! HAVE_WINSOCK */
if ( ! SLAP_EVENT_IS_WRITE( i ) ) continue;
wd = i;
#endif /* ! HAVE_WINSOCK */
SLAP_EVENT_CLR_WRITE( wd );
nwfds--;
@ -2297,12 +2421,8 @@ slapd_daemon_task(
for ( i = 0; nrfds > 0; i++ ) {
ber_socket_t rd;
#ifdef HAVE_WINSOCK
rd = readfds.fd_array[i];
#else /* ! HAVE_WINSOCK */
if ( ! SLAP_EVENT_IS_READ( i ) ) continue;
rd = i;
#endif /* ! HAVE_WINSOCK */
SLAP_EVENT_CLR_READ( rd );
nrfds--;
@ -2338,7 +2458,7 @@ slapd_daemon_task(
Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
for ( i = 0; i < ns; i++ ) {
int r, w;
int r, w, fd;
/* Don't log listener events */
if ( SLAP_EVENT_IS_LISTENER( i )
@ -2350,13 +2470,14 @@ slapd_daemon_task(
continue;
}
fd = SLAP_EVENT_FD( i );
/* Don't log internal wake events */
if ( SLAP_EVENT_FD( i ) == wake_sds[0] ) continue;
if ( fd == wake_sds[0] ) continue;
r = SLAP_EVENT_IS_READ( i );
w = SLAP_EVENT_IS_WRITE( i );
if ( r || w ) {
Debug( LDAP_DEBUG_CONNS, " %d%s%s", SLAP_EVENT_FD(i),
Debug( LDAP_DEBUG_CONNS, " %d%s%s", fd,
r ? "r" : "", w ? "w" : "" );
}
}
@ -2381,7 +2502,7 @@ slapd_daemon_task(
if ( fd == wake_sds[0] ) {
char c[BUFSIZ];
waking = 0;
tcp_read( wake_sds[0], c, sizeof(c) );
tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
break;
}

View file

@ -707,12 +707,12 @@ LDAP_SLAPD_F (int) connections_shutdown LDAP_P((void));
LDAP_SLAPD_F (int) connections_destroy LDAP_P((void));
LDAP_SLAPD_F (int) connections_timeout_idle LDAP_P((time_t));
LDAP_SLAPD_F (int) connection_client_setup LDAP_P((
LDAP_SLAPD_F (Connection *) connection_client_setup LDAP_P((
ber_socket_t s,
ldap_pvt_thread_start_t *func,
void *arg ));
LDAP_SLAPD_F (void) connection_client_enable LDAP_P(( ber_socket_t s ));
LDAP_SLAPD_F (void) connection_client_stop LDAP_P(( ber_socket_t s ));
LDAP_SLAPD_F (void) connection_client_enable LDAP_P(( Connection *c ));
LDAP_SLAPD_F (void) connection_client_stop LDAP_P(( Connection *c ));
#ifdef LDAP_PF_LOCAL_SENDMSG
#define LDAP_PF_LOCAL_SENDMSG_ARG(arg) , arg
@ -822,6 +822,19 @@ LDAP_SLAPD_V (const char *) slapd_slp_attrs;
LDAP_SLAPD_V (slap_ssf_t) local_ssf;
LDAP_SLAPD_V (struct runqueue_s) slapd_rq;
#ifdef HAVE_WINSOCK
LDAP_SLAPD_F (ber_socket_t) slapd_socknew(ber_socket_t s);
LDAP_SLAPD_F (ber_socket_t) slapd_sock2fd(ber_socket_t s);
LDAP_SLAPD_V (SOCKET *) slapd_ws_sockets;
#define SLAP_FD2SOCK(s) slapd_ws_sockets[s]
#define SLAP_SOCK2FD(s) slapd_sock2fd(s)
#define SLAP_SOCKNEW(s) slapd_socknew(s)
#else
#define SLAP_FD2SOCK(s) s
#define SLAP_SOCK2FD(s) s
#define SLAP_SOCKNEW(s) s
#endif
/*
* dn.c
*/

View file

@ -96,9 +96,9 @@ typedef struct syncinfo_s {
int si_refreshDone;
int si_syncdata;
int si_logstate;
int si_conn_setup;
Avlnode *si_presentlist;
LDAP *si_ld;
Connection *si_conn;
LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
ldap_pvt_thread_mutex_t si_mutex;
} syncinfo_t;
@ -1113,11 +1113,9 @@ done:
if ( res ) ldap_msgfree( res );
if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
if ( si->si_conn_setup ) {
ber_socket_t s;
ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
connection_client_stop( s );
si->si_conn_setup = 0;
if ( si->si_conn ) {
connection_client_stop( si->si_conn );
si->si_conn = NULL;
}
ldap_unbind_ext( si->si_ld, NULL, NULL );
si->si_ld = NULL;
@ -1160,10 +1158,9 @@ do_syncrepl(
if ( slapd_shutdown ) {
if ( si->si_ld ) {
if ( si->si_conn_setup ) {
ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
connection_client_stop( s );
si->si_conn_setup = 0;
if ( si->si_conn ) {
connection_client_stop( si->si_conn );
si->si_conn = NULL;
}
ldap_unbind_ext( si->si_ld, NULL, NULL );
si->si_ld = NULL;
@ -1215,7 +1212,7 @@ reload:
/* We got deleted while running on cn=config */
if ( !si->si_ctype ) {
if ( si->si_conn_setup )
if ( si->si_conn )
dostop = 1;
rc = -1;
}
@ -1225,14 +1222,12 @@ reload:
* If we failed, tear down the connection and reschedule.
*/
if ( rc == LDAP_SUCCESS ) {
if ( si->si_conn_setup ) {
connection_client_enable( s );
if ( si->si_conn ) {
connection_client_enable( si->si_conn );
} else {
rc = connection_client_setup( s, do_syncrepl, arg );
if ( rc == 0 )
si->si_conn_setup = 1;
si->si_conn = connection_client_setup( s, do_syncrepl, arg );
}
} else if ( si->si_conn_setup ) {
} else if ( si->si_conn ) {
dostop = 1;
}
} else {
@ -1253,7 +1248,8 @@ reload:
}
if ( dostop ) {
connection_client_stop( s );
connection_client_stop( si->si_conn );
si->si_conn = NULL;
}
if ( rc == LDAP_SUCCESS ) {
@ -3224,11 +3220,9 @@ syncinfo_free( syncinfo_t *sie, int free_all )
si_next = sie->si_next;
if ( sie->si_ld ) {
if ( sie->si_conn_setup ) {
ber_socket_t s;
ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
connection_client_stop( s );
sie->si_conn_setup = 0;
if ( sie->si_conn ) {
connection_client_stop( sie->si_conn );
sie->si_conn = NULL;
}
ldap_unbind_ext( sie->si_ld, NULL, NULL );
}
@ -3844,7 +3838,6 @@ add_syncrepl(
si->si_manageDSAit = 0;
si->si_tlimit = 0;
si->si_slimit = 0;
si->si_conn_setup = 0;
si->si_presentlist = NULL;
LDAP_LIST_INIT( &si->si_nonpresentlist );