/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
- * Copyright 1998-2005 The OpenLDAP Foundation.
+ * Copyright 1998-2006 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
static ber_socket_t wake_sds[2];
static int emfile;
-static int waking;
+static volatile int waking;
+#ifdef NO_THREADS
#define WAKE_LISTENER(w) do { \
- if ((w) && waking < 5) { \
- waking++; \
+ if ((w) && ++waking < 5) { \
tcp_write( wake_sds[1], "0", 1 ); \
} \
} while(0)
+#else
+#define WAKE_LISTENER(w) do { \
+ if (w) { \
+ tcp_write( wake_sds[1], "0", 1 ); \
+ } \
+} while(0)
+#endif
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;
slap_daemon.sd_nfds++; \
} else { \
Debug( LDAP_DEBUG_ANY, \
- "daemon: epoll_ctl ADD failed, errno %d, shutting down\n", \
- errno, 0, 0 ); \
+ "daemon: epoll_ctl(ADD,fd=%d) failed, errno=%d, shutting down\n", \
+ s, errno, 0 ); \
slapd_shutdown = 2; \
} \
} while (0)
# define SLAP_DEL_SOCK(s) do { \
int fd, rc, index = SLAP_SOCK_IX((s)); \
+ if ( index < 0 ) break; \
rc = epoll_ctl(slap_daemon.sd_epfd, EPOLL_CTL_DEL, \
(s), &SLAP_SOCK_EP((s))); \
slap_daemon.sd_epolls[index] = \
# 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) (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)
for (i=0; i<dtblsize; i++) slap_daemon.sd_index[i] = -1; \
} while (0)
+#define SLAP_SOCK_SET_DESTROY do { \
+ if ( slap_daemon.sd_epolls != NULL ) { \
+ ch_free( slap_daemon.sd_epolls ); \
+ slap_daemon.sd_epolls = NULL; \
+ ch_free( slap_daemon.sd_index ); \
+ 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 { \
FD_ZERO(&slap_daemon.sd_writers); \
} while (0)
+#define SLAP_SOCK_SET_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)
if (!SLAP_SOCK_IS_WRITE(fd)) { FD_SET((fd), &slap_daemon.sd_writers); } \
} while(0)
-# define SLAP_ADDTEST(s) do { } while 0
+# define SLAP_ADDTEST(s)
# define SLAP_EVENT_MAX dtblsize
# else
# define SLAP_SOCK_SET_READ(fd) FD_SET((fd), &slap_daemon.sd_readers)
static char** slapd_srvurls = NULL;
static SLPHandle slapd_hslp = 0;
int slapd_register_slp = 0;
+const char *slapd_slp_attrs = NULL;
+
+static SLPError slapd_slp_cookie;
void slapd_slp_init( const char* urls ) {
int i;
+ SLPError err;
slapd_srvurls = ldap_str2charray( urls, " " );
}
/* open the SLP handle */
- SLPOpen( "en", 0, &slapd_hslp );
+ err = SLPOpen( "en", 0, &slapd_hslp );
+
+ if (err != SLP_OK) {
+ Debug( LDAP_DEBUG_CONNS, "daemon: SLPOpen() failed with %ld\n",
+ (long)err, 0, 0 );
+ }
}
void slapd_slp_deinit() {
SLPError errcode,
void* cookie )
{
- /* empty report */
+ /* return the error code in the cookie */
+ *(SLPError*)cookie = errcode;
}
void slapd_slp_reg() {
int i;
+ SLPError err;
if( slapd_srvurls == NULL ) return;
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() {
int i;
+ SLPError err;
if( slapd_srvurls == NULL ) return;
for( i=0; slapd_srvurls[i] != NULL; i++ ) {
- SLPDereg( slapd_hslp,
+ 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 */
#endif
}
+void slapd_sd_lock()
+{
+ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+}
+
+void slapd_sd_unlock()
+{
+ ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
+}
+
/*
* Remove the descriptor from daemon control
*/
void slapd_remove(
ber_socket_t s,
+ Sockbuf *sb,
int wasactive,
- int wake )
+ int wake,
+ int locked )
{
int waswriter;
int wasreader;
- ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+ if ( !locked )
+ ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+
+ assert( SLAP_SOCK_IS_ACTIVE( s ));
if ( wasactive ) slap_daemon.sd_nactives--;
SLAP_DEL_SOCK(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
* control, we can try to resume a dropped listener to use.
WAKE_LISTENER(wake);
}
-void 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 );
- assert( SLAP_SOCK_IS_ACTIVE( s ));
- SLAP_SOCK_CLR_READ( s );
-
+ if ( SLAP_SOCK_IS_ACTIVE( s )) {
+ SLAP_SOCK_CLR_READ( s );
+ rc = 0;
+ }
ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
- WAKE_LISTENER(wake);
+ if ( !rc )
+ WAKE_LISTENER(wake);
+ return rc;
}
void slapd_set_read(ber_socket_t s, int wake) {
snprintf(serv, sizeof serv, "%d", port);
if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
- Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo failed: %s\n",
+ Debug( LDAP_DEBUG_ANY, "daemon: getaddrinfo() failed: %s\n",
AC_GAI_STRERROR(err), 0, 0);
return -1;
}
sap[i]->sa_family = AF_INET;
((struct sockaddr_in *)sap[i])->sin_port = htons(port);
AC_MEMCPY( &((struct sockaddr_in *)sap[i])->sin_addr,
- he ? he->h_addr_list[i] : &in, sizeof(struct in_addr) );
+ he ? (struct in_addr *)he->h_addr_list[i] : &in,
+ sizeof(struct in_addr) );
}
sap[i] = NULL;
#endif
return -1;
}
-#ifdef LDAP_CONNECTIONLESS
- if( l.sl_is_udp ) {
- long id = connection_init( l.sl_sd, &l, "", "", CONN_IS_UDP,
- (slap_ssf_t) 0, NULL );
-
- if( id < 0 ) {
- Debug( LDAP_DEBUG_TRACE,
- "slap_open_listener: connectionless init failed on %s (%d)\n",
- url, l.sl_sd, 0 );
- return -1;
- }
- l.sl_is_udp++;
- }
-#endif
-
Debug( LDAP_DEBUG_TRACE, "daemon: listener initialized %s\n",
l.sl_url.bv_val, 0, 0 );
return 0;
Debug( LDAP_DEBUG_ARGS, "daemon_init: %s\n",
urls ? urls : "<null>", 0, 0 );
+
+ ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
+#ifdef HAVE_TCPD
+ ldap_pvt_thread_mutex_init( &slap_daemon.tcpd_mutex );
+#endif
+
if( (rc = sockinit()) != 0 ) return rc;
#ifdef HAVE_SYSCONF
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;
}
#endif
ldap_charray_free( u );
- ldap_pvt_thread_mutex_init( &slap_daemon.sd_mutex );
return !i;
}
}
#endif
+#ifdef HAVE_TCPD
+ ldap_pvt_thread_mutex_destroy( &slap_daemon.tcpd_mutex );
+#endif
+
+ 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 );
+ 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 ) {
#endif /* SLAPD_RLOOKUPS */
#ifdef HAVE_TCPD
- if ( !hosts_ctl("slapd",
- dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
- peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
- SLAP_STRING_UNKNOWN ))
{
- /* DENY ACCESS */
- Statslog( LDAP_DEBUG_STATS,
- "fd=%ld DENIED from %s (%s)\n",
- (long) s,
+ int rc;
+ ldap_pvt_thread_mutex_lock( &slap_daemon.tcpd_mutex );
+ rc = hosts_ctl("slapd",
dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
- 0, 0 );
- slapd_close(s);
- return 0;
+ SLAP_STRING_UNKNOWN );
+ ldap_pvt_thread_mutex_unlock( &slap_daemon.tcpd_mutex );
+ if ( !rc ) {
+ /* DENY ACCESS */
+ Statslog( LDAP_DEBUG_STATS,
+ "fd=%ld DENIED from %s (%s)\n",
+ (long) s,
+ dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
+ peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
+ 0, 0 );
+ slapd_close(s);
+ return 0;
+ }
}
#endif /* HAVE_TCPD */
}
id, (long) s, peername, sl->sl_name.bv_val,
0 );
- slapd_add( s, 1, NULL );
return 0;
}
* listening port. The listen() and accept() calls
* are unnecessary.
*/
- if ( slap_listeners[l]->sl_is_udp ) {
- slapd_add( slap_listeners[l]->sl_sd, 1, slap_listeners[l] );
+ if ( slap_listeners[l]->sl_is_udp )
continue;
- }
#endif
if ( listen( slap_listeners[l]->sl_sd, SLAPD_LISTEN_BACKLOG ) == -1 ) {
}
#ifdef HAVE_NT_SERVICE_MANAGER
- if ( started_event != NULL ) }
+ if ( started_event != NULL ) {
ldap_pvt_thread_cond_signal( &started_event );
}
#endif
struct timeval tv;
struct timeval *tvp;
- struct timeval *cat;
+ struct timeval cat;
time_t tdelta = 1;
struct re_s* rtask;
now = slap_get_time();
ber_socket_t active;
if( slapd_gentle_shutdown == 1 ) {
+ BackendDB *be;
Debug( LDAP_DEBUG_ANY, "slapd gentle shutdown\n", 0, 0, 0 );
close_listeners( 1 );
frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
+ LDAP_STAILQ_FOREACH(be, &backendDB, be_next) {
+ be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
+ }
slapd_gentle_shutdown = 2;
}
active = slap_daemon.sd_nactives;
ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
if( active == 0 ) {
- slapd_shutdown = 2;
+ slapd_shutdown = 1;
break;
}
}
#endif
-
at = 0;
ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
nwriters = slap_daemon.sd_nwriters;
- SLAP_EVENT_INIT;
for ( l = 0; slap_listeners[l] != NULL; l++ ) {
Listener *lr = slap_listeners[l];
if ( lr->sl_sd == AC_SOCKET_INVALID ) continue;
- if ( lr->sl_mute || lr->sl_busy ) {
+#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
+ if ( lr->sl_mute || lr->sl_busy )
+#else
+ if ( lr->sl_mute )
+#endif
+ {
SLAP_SOCK_CLR_READ( lr->sl_sd );
} else {
SLAP_SOCK_SET_READ( lr->sl_sd );
}
}
+ SLAP_EVENT_INIT;
+
nfds = SLAP_EVENT_MAX;
if ( global_idletimeout && slap_daemon.sd_nactives ) at = 1;
ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
rtask = ldap_pvt_runqueue_next_sched( &slapd_rq, &cat );
- while ( cat && cat->tv_sec && cat->tv_sec <= now ) {
+ while ( rtask && cat.tv_sec && cat.tv_sec <= now ) {
if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
} else {
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 );
}
ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
- if ( cat && cat->tv_sec ) {
- time_t diff = difftime( cat->tv_sec, now );
+ if ( rtask && cat.tv_sec ) {
+ time_t diff = difftime( cat.tv_sec, now );
if ( diff == 0 ) diff = tdelta;
if ( tvp == NULL || diff < tv.tv_sec ) {
tv.tv_sec = diff;
continue;
}
+#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
if ( lr->sl_busy ) {
Debug( LDAP_DEBUG_CONNS,
"daemon: select: listen=%d busy\n",
lr->sl_sd, 0, 0 );
continue;
}
+#endif
Debug( LDAP_DEBUG_CONNS,
"daemon: select: listen=%d active_threads=%d tvp=%s\n",
case -1: { /* failure - try again */
int err = sock_errno();
- if( err == EBADF
-#ifdef WSAENOTSOCK
- /* you'd think this would be EBADF */
- || err == WSAENOTSOCK
-#endif
- ) {
- if (++ebadf < SLAPD_EBADF_LIMIT)
- continue;
- }
-
if( err != EINTR ) {
- Debug( LDAP_DEBUG_CONNS,
- "daemon: select failed (%d): %s\n",
- err, sock_errstr(err), 0 );
- slapd_shutdown = 2;
+ ebadf++;
+
+ /* Don't log unless we got it twice in a row */
+ if ( !( ebadf & 1 )) {
+ Debug( LDAP_DEBUG_ANY,
+ "daemon: select failed count %d err (%d): %s\n",
+ ebadf, err, sock_errstr(err) );
+ }
+ if ( ebadf >= SLAPD_EBADF_LIMIT )
+ slapd_shutdown = 2;
}
}
continue;
#if SLAP_EVENTS_ARE_INDEXED
if ( SLAP_EVENT_IS_READ( wake_sds[0] )) {
+ char c[BUFSIZ];
SLAP_EVENT_CLR_READ( wake_sds[0] );
- ns--;
- {
- char c[BUFSIZ];
- tcp_read( wake_sds[0], c, sizeof(c) );
- }
- Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
waking = 0;
+ tcp_read( wake_sds[0], c, sizeof(c) );
+ Debug( LDAP_DEBUG_CONNS, "daemon: waked\n", 0, 0, 0 );
continue;
}
"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.
- * connection_write() must validitate the stream is still
+ * connection_write() must validate the stream is still
* active.
+ *
+ * ITS#4338: if the stream is invalid, there is no need to
+ * close it here. It has already been closed in connection.c.
*/
if ( connection_write( wd ) < 0 ) {
if ( SLAP_EVENT_IS_READ( wd )) {
SLAP_EVENT_CLR_READ( (unsigned) wd );
nrfds--;
}
- slapd_close( wd );
}
-#endif
}
for ( i = 0; nrfds > 0; i++ ) {
#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
connection_read_activate( rd );
#else
- if ( connection_read( rd ) < 0 ) {
- slapd_close( rd );
- }
+ connection_read( rd );
#endif
}
#else /* !SLAP_EVENTS_ARE_INDEXED */
* an event, so we could use pointers to the listener structure
* instead of just the file descriptor. For /dev/poll we have to
* search the listeners array for a matching descriptor.
+ *
+ * We now handle wake events when we see them; they are not given
+ * higher priority.
*/
- /* if waking is set and we woke up, we'll read whatever
- * we can.
- */
- if ( waking ) {
- char c[BUFSIZ];
- tcp_read( wake_sds[0], c, sizeof(c) );
- waking = 0;
- ns--;
- continue;
- }
-
#ifdef LDAP_DEBUG
Debug( LDAP_DEBUG_CONNS, "daemon: activity on:", 0, 0, 0 );
r ? "r" : "", w ? "w" : "" );
}
}
+ Debug( LDAP_DEBUG_CONNS, "\n", 0, 0, 0 );
#endif
for (i=0; i<ns; i++) {
if ( rc ) {
fd = SLAP_EVENT_FD( i );
- /* Ignore wake events, they were handled above */
- if ( fd == wake_sds[0] ) continue;
+ /* Handle wake events */
+ if ( fd == wake_sds[0] ) {
+ char c[BUFSIZ];
+ waking = 0;
+ tcp_read( wake_sds[0], c, sizeof(c) );
+ break;
+ }
if( SLAP_EVENT_IS_WRITE( i ) ) {
Debug( LDAP_DEBUG_CONNS,
"daemon: write active on %d\n",
fd, 0, 0 );
-#ifdef SLAP_LIGHTWEIGHT_DISPATCHER
- connection_write_activate( fd );
-#else
+ SLAP_EVENT_CLR_WRITE( i );
+
/*
* NOTE: it is possible that the connection was closed
* and that the stream is now inactive.
* active.
*/
if ( connection_write( fd ) < 0 ) {
- slapd_close( fd );
continue;
}
-#endif
}
- if( SLAP_EVENT_IS_READ( i ) ) {
+ /* 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
* connection_read() must valid the stream is still
* active.
*/
- if ( connection_read( fd ) < 0 ) slapd_close( fd );
+ connection_read( fd );
#endif
+ } else {
+ Debug( LDAP_DEBUG_CONNS,
+ "daemon: hangup on %d\n", fd, 0, 0 );
}
}
}
}
+#ifdef LDAP_CONNECTIONLESS
+static int connectionless_init(void)
+{
+ int l;
+
+ for ( l = 0; slap_listeners[l] != NULL; l++ ) {
+ Listener *lr = slap_listeners[l];
+ long id;
+
+ if( !lr->sl_is_udp ) {
+ continue;
+ }
+
+ id = connection_init( lr->sl_sd, lr, "", "", CONN_IS_UDP, (slap_ssf_t) 0, NULL );
+
+ if( id < 0 ) {
+ Debug( LDAP_DEBUG_TRACE,
+ "connectionless_init: failed on %s (%d)\n", lr->sl_url, lr->sl_sd, 0 );
+ return -1;
+ }
+ lr->sl_is_udp++;
+ }
+
+ return 0;
+}
+#endif /* LDAP_CONNECTIONLESS */
+
int slapd_daemon( void )
{
int rc;
connections_init();
+#ifdef LDAP_CONNECTIONLESS
+ connectionless_init();
+#endif
#define SLAPD_LISTENER_THREAD 1
#if defined( SLAPD_LISTENER_THREAD )
#if defined( HAVE_WINSOCK2 ) || defined( HAVE_WINSOCK )
WSACleanup();
#endif
+ SLAP_SOCK_SET_DESTROY;
+
return 0;
}