/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * Copyright 1998-2007 The OpenLDAP Foundation.
+ * Copyright 1998-2009 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
#endif /* ! epoll && ! /dev/poll */
#ifdef HAVE_TCPD
-# include <tcpd.h>
int allow_severity = LOG_INFO;
int deny_severity = LOG_NOTICE;
-
-# define SLAP_STRING_UNKNOWN STRING_UNKNOWN
-#else /* ! TCP Wrappers */
-# define SLAP_STRING_UNKNOWN "unknown"
-#endif /* ! TCP Wrappers */
+#endif /* TCP Wrappers */
#ifdef LDAP_PF_LOCAL
# include <sys/stat.h>
#define SLAPD_LISTEN_BACKLOG 1024
#endif /* ! SLAPD_LISTEN_BACKLOG */
-static ber_socket_t wake_sds[2];
+static ber_socket_t wake_sds[2]
+#ifdef HAVE_WINSOCK
+ = { INVALID_SOCKET, INVALID_SOCKET }
+#endif /* HAVE_WINSOCK */
+ ;
static int emfile;
+static time_t chk_writetime;
+
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 */
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
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;
# 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_EPOLL_SOCK_IX(s)] = 1 )
# define SLAP_SOCK_CLR_SUSPEND(s) \
( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] = 0 )
# define SLAP_SOCK_IS_SUSPEND(s) \
( slap_daemon.sd_suspend[SLAP_EPOLL_SOCK_IX(s)] == 1 )
-# endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
# define SLAP_EPOLL_EVENT_CLR(i, mode) (revents[(i)].events &= ~(mode))
# 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))
} 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) ); \
+ memset( slap_daemon.sd_flags, 0, dtblsize ); \
+ 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
# 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_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_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)
*(nsp) = select( SLAP_EVENT_MAX, &readfds, \
nwriters > 0 ? &writefds : NULL, NULL, (tvp) ); \
} while (0)
+# endif /* !HAVE_WINSOCK */
#endif /* ! epoll && ! /dev/poll */
#ifdef HAVE_SLP
for ( i = 0; slapd_srvurls[i] != NULL; i++ ) {
if ( strcmp( slapd_srvurls[i], "ldap:///" ) == 0 ) {
slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
- strlen( global_host ) +
+ global_host_bv.bv_len +
sizeof( LDAP_SRVTYPE_PREFIX ) );
strcpy( lutil_strcopy(slapd_srvurls[i],
- LDAP_SRVTYPE_PREFIX ), global_host );
+ LDAP_SRVTYPE_PREFIX ), global_host_bv.bv_val );
} else if ( strcmp( slapd_srvurls[i], "ldaps:///" ) == 0 ) {
slapd_srvurls[i] = (char *) ch_realloc( slapd_srvurls[i],
- strlen( global_host ) +
+ global_host_bv.bv_len +
sizeof( LDAPS_SRVTYPE_PREFIX ) );
strcpy( lutil_strcopy(slapd_srvurls[i],
- LDAPS_SRVTYPE_PREFIX ), global_host );
+ LDAPS_SRVTYPE_PREFIX ), global_host_bv.bv_val );
}
}
}
#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
*
ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
WAKE_LISTENER(1);
-#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
}
/*
{
ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
- assert( SLAP_SOCK_IS_ACTIVE( s ));
-
if ( SLAP_SOCK_IS_WRITE( s )) {
+ assert( SLAP_SOCK_IS_ACTIVE( s ));
+
SLAP_SOCK_CLR_WRITE( s );
slap_daemon.sd_nwriters--;
}
SLAP_SOCK_SET_WRITE( s );
slap_daemon.sd_nwriters++;
}
+ if (( wake & 2 ) && global_writetimeout ) {
+ chk_writetime = slap_get_time();
+ }
ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
WAKE_LISTENER(wake);
WAKE_LISTENER(wake);
}
+time_t
+slapd_get_writetime()
+{
+ time_t cur;
+ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+ cur = chk_writetime;
+ ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
+ return cur;
+}
+
+void
+slapd_clr_writetime( time_t old )
+{
+ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+ if ( chk_writetime == old )
+ chk_writetime = 0;
+ ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
+}
+
static void
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
S_IRGRP, S_IWGRP, S_IXGRP,
S_IROTH, S_IWOTH, S_IXOTH
};
- static char c[] = "-rwxrwxrwx";
+ static const char c[] = "-rwxrwxrwx";
if ( value[ j ] == c[ j ] ) {
p |= m[ j ];
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)
/*
l.sl_url.bv_val = NULL;
l.sl_mute = 0;
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
l.sl_busy = 0;
-#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
#ifndef HAVE_TLS
if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
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",
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 ) {
#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();
#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();
#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 */
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 );
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;
}
slapd_daemon_destroy( void )
{
connections_destroy();
- tcp_close( wake_sds[1] );
- tcp_close( wake_sds[0] );
+#ifdef HAVE_WINSOCK
+ if ( wake_sds[1] != INVALID_SOCKET &&
+ SLAP_FD2SOCK( wake_sds[1] ) != SLAP_FD2SOCK( wake_sds[0] ))
+#endif /* HAVE_WINSOCK */
+ tcp_close( SLAP_FD2SOCK(wake_sds[1]) );
+#ifdef HAVE_WINSOCK
+ if ( wake_sds[0] != INVALID_SOCKET )
+#endif /* HAVE_WINSOCK */
+ tcp_close( SLAP_FD2SOCK(wake_sds[0]) );
sockdestroy();
#ifdef HAVE_SLP
{
Sockaddr from;
- ber_socket_t s;
+ ber_socket_t s, sfd;
ber_socklen_t len = sizeof(from);
Connection *c;
slap_ssf_t ssf = 0;
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 );
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
/* Resume the listener FD to allow concurrent-processing of
* additional incoming connections.
*/
sl->sl_busy = 0;
WAKE_LISTENER(1);
-#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
if ( s == AC_SOCKET_INVALID ) {
int err = sock_errno();
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 */
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
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 */
}
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 ) {
break;
default:
- slapd_close(s);
+ slapd_close(sfd);
return 0;
}
/* 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;
}
}
#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
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;
}
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
static void*
slap_listener_thread(
void* ctx,
}
return rc;
}
-#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
static void *
slapd_daemon_task(
{
int l;
time_t last_idle_check = 0;
- struct timeval idle;
int ebadf = 0;
#define SLAPD_IDLE_CHECK_LIMIT 4
if ( global_idletimeout > 0 ) {
last_idle_check = slap_get_time();
- /* Set the select timeout.
- * Don't just truncate, preserve the fractions of
- * seconds to prevent sleeping for zero time.
- */
- idle.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
- idle.tv_usec = global_idletimeout - \
- ( idle.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
- idle.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
- } else {
- idle.tv_sec = 0;
- idle.tv_usec = 0;
}
slapd_add( wake_sds[0], 0, NULL );
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
return (void*)-1;
}
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
/* 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 );
slapd_shutdown = 2;
return (void*)-1;
}
-#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
slapd_add( slap_listeners[l]->sl_sd, 0, slap_listeners[l] );
}
now = slap_get_time();
- if ( ( global_idletimeout > 0 ) &&
- difftime( last_idle_check +
- global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
- {
- connections_timeout_idle( now );
- last_idle_check = now;
+ if ( global_idletimeout > 0 || chk_writetime ) {
+ int check = 0;
+ /* Set the select timeout.
+ * Don't just truncate, preserve the fractions of
+ * seconds to prevent sleeping for zero time.
+ */
+ if ( chk_writetime ) {
+ tv.tv_sec = global_writetimeout;
+ tv.tv_usec = global_writetimeout;
+ if ( difftime( chk_writetime, now ) < 0 )
+ check = 2;
+ } else {
+ tv.tv_sec = global_idletimeout / SLAPD_IDLE_CHECK_LIMIT;
+ tv.tv_usec = global_idletimeout - \
+ ( tv.tv_sec * SLAPD_IDLE_CHECK_LIMIT );
+ tv.tv_usec *= 1000000 / SLAPD_IDLE_CHECK_LIMIT;
+ if ( difftime( last_idle_check +
+ global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 )
+ check = 1;
+ }
+ if ( check ) {
+ connections_timeout_idle( now );
+ last_idle_check = now;
+ }
+ } else {
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
}
- tv = idle;
#ifdef SIGHUP
if ( slapd_gentle_shutdown ) {
if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
if ( lr->sl_mute || lr->sl_busy )
-#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
- if ( lr->sl_mute )
-#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
{
SLAP_SOCK_CLR_READ( lr->sl_sd );
} else {
continue;
}
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
if ( lr->sl_busy ) {
Debug( LDAP_DEBUG_CONNS,
"daemon: " SLAP_EVENT_FNAME ": "
lr->sl_sd, 0, 0 );
continue;
}
-#endif /* SLAP_LIGHTWEIGHT_DISPATCHER */
Debug( LDAP_DEBUG_CONNS,
"daemon: " SLAP_EVENT_FNAME ": "
Debug( LDAP_DEBUG_ANY,
"daemon: "
SLAP_EVENT_FNAME
- "failed count %d "
+ " failed count %d "
"err (%d): %s\n",
ebadf, err,
sock_errstr( err ) );
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;
}
SLAP_EVENT_CLR_WRITE( slap_listeners[l]->sl_sd );
ns--;
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
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 */
}
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++ ) {
}
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--;
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--;
* active.
*/
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
connection_read_activate( rd );
-#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
- connection_read( rd );
-#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
}
#else /* !SLAP_EVENTS_ARE_INDEXED */
/* FIXME */
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 )
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" : "" );
}
}
#endif /* LDAP_DEBUG */
for ( i = 0; i < ns; i++ ) {
- int rc = 1, fd;
+ int rc = 1, fd, w = 0, r = 0;
if ( SLAP_EVENT_IS_LISTENER( i ) ) {
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
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
if ( fd == wake_sds[0] ) {
char c[BUFSIZ];
waking = 0;
- tcp_read( wake_sds[0], c, sizeof(c) );
- break;
+ tcp_read( SLAP_FD2SOCK(wake_sds[0]), c, sizeof(c) );
+ continue;
}
if ( SLAP_EVENT_IS_WRITE( i ) ) {
fd, 0, 0 );
SLAP_EVENT_CLR_WRITE( i );
+ w = 1;
/*
* NOTE: it is possible that the connection was closed
}
}
/* If event is a read */
- if ( SLAP_EVENT_IS_READ( i ) ) {
+ if ( SLAP_EVENT_IS_READ( i ))
+ r = 1;
+ if ( r || !w ) {
Debug( LDAP_DEBUG_CONNS,
"daemon: read active on %d\n",
fd, 0, 0 );
- SLAP_EVENT_CLR_READ( i );
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
+ if ( r ) {
+ SLAP_EVENT_CLR_READ( i );
+ } else {
+#ifdef HAVE_EPOLL
+ /* Don't keep reporting the hangup
+ */
+ if ( SLAP_SOCK_IS_ACTIVE( fd )) {
+ SLAP_EPOLL_SOCK_SET( fd, EPOLLET );
+ }
+#endif
+ }
connection_read_activate( fd );
-#else /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
- /*
- * NOTE: it is possible that the connection was closed
- * and that the stream is now inactive.
- * connection_read() must valid the stream is still
- * active.
- */
- connection_read( fd );
-#endif /* ! SLAP_LIGHTWEIGHT_DISPATCHER */
- } else {
- Debug( LDAP_DEBUG_CONNS,
- "daemon: hangup on %d\n", fd, 0, 0 );
}
}
}
connections_shutdown();
}
- Debug( LDAP_DEBUG_ANY,
- "slapd shutdown: waiting for %d threads to terminate\n",
- ldap_pvt_thread_pool_backload( &connection_pool ), 0, 0 );
+ if ( LogTest( LDAP_DEBUG_ANY )) {
+ int t = ldap_pvt_thread_pool_backload( &connection_pool );
+ Debug( LDAP_DEBUG_ANY,
+ "slapd shutdown: waiting for %d operations/tasks to finish\n",
+ t, 0, 0 );
+ }
ldap_pvt_thread_pool_destroy( &connection_pool, 1 );
free( slap_listeners );
{
int rc;
- connections_init();
#ifdef LDAP_CONNECTIONLESS
connectionless_init();
#endif /* LDAP_CONNECTIONLESS */
RETSIGTYPE
slap_sig_shutdown( int sig )
{
+ int save_errno = errno;
+
#if 0
Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
#endif
/* reinstall self */
(void) SIGNAL_REINSTALL( sig, slap_sig_shutdown );
+
+ errno = save_errno;
}
RETSIGTYPE
slap_sig_wake( int sig )
{
+ int save_errno = errno;
+
WAKE_LISTENER(1);
/* reinstall self */
(void) SIGNAL_REINSTALL( sig, slap_sig_wake );
+
+ errno = save_errno;
}