]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/daemon.c
Import ITS#3542 fix from HEAD
[openldap] / servers / slapd / daemon.c
index ae5a754f7d05f0873bf8f0cd2ffb11c73f6e6fd1..12c3c4b490e4c444c16a554453138f68c8a2120b 100644 (file)
@@ -1,7 +1,26 @@
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2005 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* Portions Copyright (c) 1995 Regents of the University of Michigan.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that this notice is preserved and that due credit is given
+ * to the University of Michigan at Ann Arbor. The name of the University
+ * may not be used to endorse or promote products derived from this
+ * software without specific prior written permission. This software
+ * is provided ``as is'' without express or implied warranty.
  */
 
 #include "portable.h"
@@ -20,9 +39,7 @@
 #include "lutil.h"
 #include "slap.h"
 
-#ifdef LDAP_SYNCREPL
 #include "ldap_rq.h"
-#endif
 
 #ifdef HAVE_TCPD
 #include <tcpd.h>
@@ -55,6 +72,7 @@ Listener **slap_listeners = NULL;
 #define SLAPD_LISTEN 10
 
 static ber_socket_t wake_sds[2];
+static int emfile;
 
 #if defined(NO_THREADS) || defined(HAVE_GNU_PTH)
 static int waking;
@@ -65,10 +83,8 @@ static int waking;
 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
 #endif
 
-#ifndef HAVE_WINSOCK
-static
-#endif
 volatile sig_atomic_t slapd_shutdown = 0, slapd_gentle_shutdown = 0;
+volatile sig_atomic_t slapd_abrupt_shutdown = 0;
 
 static struct slap_daemon {
        ldap_pvt_thread_mutex_t sd_mutex;
@@ -195,8 +211,13 @@ void slapd_slp_dereg() {
 
 /*
  * Add a descriptor to daemon control
+ *
+ * If isactive, the descriptor is a live server session and is subject
+ * to idletimeout control. Otherwise, the descriptor is a passive
+ * listener or an outbound client session, and not subject to
+ * idletimeout.
  */
-static void slapd_add(ber_socket_t s) {
+static void slapd_add(ber_socket_t s, int isactive) {
        ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 
        assert( !FD_ISSET( s, &slap_daemon.sd_actives ));
@@ -209,7 +230,9 @@ static void slapd_add(ber_socket_t s) {
        }
 #endif
 
-       slap_daemon.sd_nactives++;
+       if ( isactive ) {
+               slap_daemon.sd_nactives++;
+       }
 
        FD_SET( s, &slap_daemon.sd_actives );
        FD_SET( s, &slap_daemon.sd_readers );
@@ -231,10 +254,12 @@ static void slapd_add(ber_socket_t s) {
 /*
  * Remove the descriptor from daemon control
  */
-void slapd_remove(ber_socket_t s, int wake) {
+void slapd_remove(ber_socket_t s, int wasactive, int wake) {
        ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 
-       slap_daemon.sd_nactives--;
+       if ( wasactive ) {
+               slap_daemon.sd_nactives--;
+       }
 
 #ifdef NEW_LOGGING
        LDAP_LOG( CONNECTION, DETAIL1, 
@@ -251,6 +276,28 @@ void slapd_remove(ber_socket_t s, int wake) {
        FD_CLR( s, &slap_daemon.sd_readers );
        FD_CLR( s, &slap_daemon.sd_writers );
 
+       /* 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.
+        */
+       if ( emfile ) {
+               int i;
+               for ( i = 0; slap_listeners[i] != NULL; i++ ) {
+                       if ( slap_listeners[i]->sl_sd != AC_SOCKET_INVALID ) {
+                               if ( slap_listeners[i]->sl_sd == s ) continue;
+                               if ( slap_listeners[i]->sl_is_mute ) {
+                                       slap_listeners[i]->sl_is_mute = 0;
+                                       emfile--;
+                                       break;
+                               }
+                       }
+               }
+               /* Walked the entire list without enabling anything; emfile
+                * counter is stale. Reset it.
+                */
+               if ( slap_listeners[i] == NULL )
+                       emfile = 0;
+       }
        ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
        WAKE_LISTENER(wake || slapd_gentle_shutdown == 2);
 }
@@ -621,6 +668,7 @@ static int slap_open_listener(
        }
 
        l.sl_url.bv_val = NULL;
+       l.sl_is_mute = 0;
 
 #ifndef HAVE_TLS
        if( ldap_pvt_url_scheme2tls( lud->lud_scheme ) ) {
@@ -707,15 +755,21 @@ static int slap_open_listener(
 
        psal = sal;
        while ( *sal != NULL ) {
+               char *af;
                switch( (*sal)->sa_family ) {
                case AF_INET:
+                       af = "IPv4";
+                       break;
 #ifdef LDAP_PF_INET6
                case AF_INET6:
+                       af = "IPv6";
+                       break;
 #endif
 #ifdef LDAP_PF_LOCAL
                case AF_LOCAL:
-#endif
+                       af = "Local";
                        break;
+#endif
                default:
                        sal++;
                        continue;
@@ -728,12 +782,12 @@ static int slap_open_listener(
                        int err = sock_errno();
 #ifdef NEW_LOGGING
                        LDAP_LOG( CONNECTION, ERR, 
-                               "slap_open_listener: socket() failed errno=%d (%s)\n",
-                               err, sock_errstr(err), 0 );
+                               "slap_open_listener: %s socket() failed errno=%d (%s)\n",
+                               af, err, sock_errstr(err) );
 #else
                        Debug( LDAP_DEBUG_ANY,
-                               "daemon: socket() failed errno=%d (%s)\n", err,
-                               sock_errstr(err), 0 );
+                               "daemon: %s socket() failed errno=%d (%s)\n",
+                               af, err, sock_errstr(err) );
 #endif
                        sal++;
                        continue;
@@ -746,8 +800,8 @@ static int slap_open_listener(
                                "great %ld\n", (long)l.sl_sd, (long)dtblsize, 0 );
 #else
                        Debug( LDAP_DEBUG_ANY,
-                              "daemon: listener descriptor %ld is too great %ld\n",
-                              (long) l.sl_sd, (long) dtblsize, 0 );
+                               "daemon: listener descriptor %ld is too great %ld\n",
+                               (long) l.sl_sd, (long) dtblsize, 0 );
 #endif
                        tcp_close( l.sl_sd );
                        sal++;
@@ -764,7 +818,7 @@ static int slap_open_listener(
                        /* enable address reuse */
                        tmp = 1;
                        rc = setsockopt( l.sl_sd, SOL_SOCKET, SO_REUSEADDR,
-                                        (char *) &tmp, sizeof(tmp) );
+                               (char *) &tmp, sizeof(tmp) );
                        if ( rc == AC_SOCKET_ERROR ) {
                                int err = sock_errno();
 #ifdef NEW_LOGGING
@@ -1008,7 +1062,7 @@ int slapd_daemon_init( const char *urls )
        for( i=0; u[i] != NULL; i++ ) {
 #ifdef NEW_LOGGING
                LDAP_LOG( CONNECTION, DETAIL1, 
-                       "slap_daemon_init: listen on %s\n.", u[i], 0, 0 );
+                       "slap_daemon_init: listen on %s\n", u[i], 0, 0 );
 #else
                Debug( LDAP_DEBUG_TRACE, "daemon_init: listen on %s\n",
                        u[i], 0, 0 );
@@ -1090,7 +1144,7 @@ close_listeners(
        for ( l = 0; slap_listeners[l] != NULL; l++ ) {
                if ( slap_listeners[l]->sl_sd != AC_SOCKET_INVALID ) {
                        if ( remove )
-                               slapd_remove( slap_listeners[l]->sl_sd, 0 );
+                               slapd_remove( slap_listeners[l]->sl_sd, 0, 0 );
 #ifdef LDAP_PF_LOCAL
                        if ( slap_listeners[l]->sl_sa.sa_addr.sa_family == AF_LOCAL ) {
                                unlink( slap_listeners[l]->sl_sa.sa_un_addr.sun_path );
@@ -1115,11 +1169,25 @@ slapd_daemon_task(
 {
        int l;
        time_t  last_idle_check = 0;
-       time( &starttime );
+       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;
        }
+
        for ( l = 0; slap_listeners[l] != NULL; l++ ) {
                if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
                        continue;
@@ -1129,7 +1197,7 @@ slapd_daemon_task(
                 * are unnecessary.
                 */
                if ( slap_listeners[l]->sl_is_udp ) {
-                       slapd_add( slap_listeners[l]->sl_sd );
+                       slapd_add( slap_listeners[l]->sl_sd, 1 );
                        continue;
                }
 #endif
@@ -1186,7 +1254,7 @@ slapd_daemon_task(
                        return( (void*)-1 );
                }
 
-               slapd_add( slap_listeners[l]->sl_sd );
+               slapd_add( slap_listeners[l]->sl_sd, 0 );
        }
 
 #ifdef HAVE_NT_SERVICE_MANAGER
@@ -1202,38 +1270,28 @@ slapd_daemon_task(
                int at;
                ber_socket_t nfds;
 #define SLAPD_EBADF_LIMIT 16
-               int ebadf = 0;
-               int emfile = 0;
 
-#define SLAPD_IDLE_CHECK_LIMIT 4
                time_t  now;
 
-
                fd_set                  readfds;
                fd_set                  writefds;
                Sockaddr                from;
 
-               struct timeval          zero;
+               struct timeval          tv;
                struct timeval          *tvp;
 
-#ifdef LDAP_SYNCREPL
-               struct timeval          diff;
                struct timeval          *cat;
                time_t                          tdelta = 1;
                struct re_s*            rtask;
-#endif
-
                now = slap_get_time();
 
-               if( emfile ) {
+               if( ( global_idletimeout > 0 ) &&
+                       difftime( last_idle_check +
+                       global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
                        connections_timeout_idle( now );
-
-               } else if ( global_idletimeout > 0 ) {
-                       if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
-                               connections_timeout_idle( now );
-                               last_idle_check = now;
-                       }
+                       last_idle_check = now;
                }
+               tv = idle;
 
 #ifdef SIGHUP
                if( slapd_gentle_shutdown ) {
@@ -1259,8 +1317,7 @@ slapd_daemon_task(
                FD_ZERO( &writefds );
                FD_ZERO( &readfds );
 
-               zero.tv_sec = 0;
-               zero.tv_usec = 0;
+               at = 0;
 
                ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
 
@@ -1283,6 +1340,9 @@ slapd_daemon_task(
                for ( l = 0; slap_listeners[l] != NULL; l++ ) {
                        if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
                                continue;
+                       if ( slap_listeners[l]->sl_is_mute )
+                               FD_CLR( slap_listeners[l]->sl_sd, &readfds );
+                       else
                        if (!FD_ISSET(slap_listeners[l]->sl_sd, &readfds))
                            FD_SET( slap_listeners[l]->sl_sd, &readfds );
                }
@@ -1292,52 +1352,51 @@ slapd_daemon_task(
 #else
                nfds = dtblsize;
 #endif
+               if ( global_idletimeout && slap_daemon.sd_nactives )
+                       at = 1;
 
                ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
 
-               at = ldap_pvt_thread_pool_backload(&connection_pool);
+               if ( at 
+#if defined(HAVE_YIELDING_SELECT) || defined(NO_THREADS)
+                       &&  ( tv.tv_sec || tv.tv_usec )
+#endif
+                       )
+                       tvp = &tv;
+               else
+                       tvp = NULL;
 
-#ifdef LDAP_SYNCREPL
                ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
                rtask = ldap_pvt_runqueue_next_sched( &syncrepl_rq, &cat );
                while ( cat && cat->tv_sec && cat->tv_sec <= now ) {
                        if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
-                               ldap_pvt_runqueue_resched( &syncrepl_rq, rtask );
+                               ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
                        } else {
                                ldap_pvt_runqueue_runtask( &syncrepl_rq, rtask );
-                               ldap_pvt_runqueue_resched( &syncrepl_rq, rtask );
+                               ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
                                ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
                                ldap_pvt_thread_pool_submit( &connection_pool,
                                                                                        rtask->routine, (void *) rtask );
+                               ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
                        }
                        rtask = ldap_pvt_runqueue_next_sched( &syncrepl_rq, &cat );
                }
-               rtask = ldap_pvt_runqueue_next_sched( &syncrepl_rq, &cat );
                ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
 
-               if ( cat != NULL ) {
-                       diff.tv_sec = difftime( cat->tv_sec, now );
-                       if ( diff.tv_sec == 0 )
-                               diff.tv_sec = tdelta;
-               }
-#endif
-
-#if defined( HAVE_YIELDING_SELECT ) || defined( NO_THREADS )
-               tvp = NULL;
-#else
-               tvp = at ? &zero : NULL;
-#endif
-
-#ifdef LDAP_SYNCREPL
-               if ( cat != NULL ) {
-                       if ( tvp == NULL ) {
-                               tvp = &diff;
+               if ( cat && 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;
+                               tv.tv_usec = 0;
+                               tvp = &tv;
                        }
                }
-#endif
 
                for ( l = 0; slap_listeners[l] != NULL; l++ ) {
-                       if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
+                       if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ||
+                           slap_listeners[l]->sl_is_mute )
                                continue;
 
 #ifdef NEW_LOGGING
@@ -1392,6 +1451,7 @@ slapd_daemon_task(
 
                case 0:         /* timeout - let threads run */
                        ebadf = 0;
+#ifndef HAVE_YIELDING_SELECT
 #ifdef NEW_LOGGING
                        LDAP_LOG( CONNECTION, DETAIL2,
                                   "slapd_daemon_task: select timeout - yielding\n", 0, 0, 0 );
@@ -1401,6 +1461,7 @@ slapd_daemon_task(
 #endif
 
                        ldap_pvt_thread_yield();
+#endif
                        continue;
 
                default:        /* something happened - deal with it */
@@ -1431,7 +1492,7 @@ slapd_daemon_task(
                        socklen_t len = sizeof(from);
                        long id;
                        slap_ssf_t ssf = 0;
-                       char *authid = NULL;
+                       struct berval authid = BER_BVNULL;
 #ifdef SLAPD_RLOOKUPS
                        char hbuf[NI_MAXHOST];
 #endif
@@ -1439,11 +1500,12 @@ slapd_daemon_task(
                        char    *dnsname = NULL;
                        char    *peeraddr = NULL;
 #ifdef LDAP_PF_LOCAL
-                       char    peername[MAXPATHLEN + sizeof("PATH=")];
+                       char peername[MAXPATHLEN + sizeof("PATH=")];
 #elif defined(LDAP_PF_INET6)
-                       char    peername[sizeof("IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
+                       char peername[sizeof(
+                               "IP=ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff 65535")];
 #else
-                       char    peername[sizeof("IP=255.255.255.255:65336")];
+                       char peername[sizeof("IP=255.255.255.255:65336")];
 #endif /* LDAP_PF_LOCAL */
 
                        peername[0] = '\0';
@@ -1453,7 +1515,7 @@ slapd_daemon_task(
 
                        if ( !FD_ISSET( slap_listeners[l]->sl_sd, &readfds ) )
                                continue;
-
+                       
 #ifdef LDAP_CONNECTIONLESS
                        if ( slap_listeners[l]->sl_is_udp ) {
                                /* The first time we receive a query, we set this
@@ -1461,60 +1523,60 @@ slapd_daemon_task(
                                 * of the slapd.
                                 */
                                if ( slap_listeners[l]->sl_is_udp < 2 ) {
-                                   id = connection_init(
-                                       slap_listeners[l]->sl_sd,
-                                       slap_listeners[l], "", "",
-                                       2, ssf, authid );
+                                       id = connection_init(
+                                               slap_listeners[l]->sl_sd,
+                                               slap_listeners[l], "", "",
+                                               CONN_IS_UDP, ssf, NULL );
                                    slap_listeners[l]->sl_is_udp++;
                                }
                                continue;
                        }
 #endif
 
+                       /* Don't need to look at this in the data loops */
+                       FD_CLR( slap_listeners[l]->sl_sd, &readfds );
+                       FD_CLR( slap_listeners[l]->sl_sd, &writefds );
+
+#  ifdef LDAP_PF_LOCAL
+                       /* FIXME: apparently accept doesn't fill
+                        * the sun_path sun_path member */
+                       from.sa_un_addr.sun_path[0] = '\0';
+#  endif /* LDAP_PF_LOCAL */
                        s = accept( slap_listeners[l]->sl_sd,
                                (struct sockaddr *) &from, &len );
                        if ( s == AC_SOCKET_INVALID ) {
                                int err = sock_errno();
 
+                               if(
 #ifdef EMFILE
-                               if( err == EMFILE ) {
-                                       emfile++;
-                               } else
+                                   err == EMFILE ||
 #endif
 #ifdef ENFILE
-                               if( err == ENFILE ) {
-                                       emfile++;
-                               } else 
+                                   err == ENFILE ||
 #endif
+                                   0 )
                                {
-                                       emfile=0;
+                                       ldap_pvt_thread_mutex_lock( &slap_daemon.sd_mutex );
+                                       emfile++;
+                                       /* Stop listening until an existing session closes */
+                                       slap_listeners[l]->sl_is_mute = 1;
+                                       ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
                                }
 
-                               if( emfile < 3 ) {
 #ifdef NEW_LOGGING
-                                       LDAP_LOG( CONNECTION, ERR, 
-                                               "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
-                                               (long)slap_listeners[l]->sl_sd, 
-                                               err, sock_errstr(err) );
+                               LDAP_LOG( CONNECTION, ERR, 
+                                       "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
+                                       (long)slap_listeners[l]->sl_sd, 
+                                       err, sock_errstr(err) );
 #else
-                                       Debug( LDAP_DEBUG_ANY,
-                                           "daemon: accept(%ld) failed errno=%d (%s)\n",
-                                           (long) slap_listeners[l]->sl_sd, err,
-                                           sock_errstr(err) );
-#endif
-                               } else {
-                                       /* prevent busy loop */
-#  ifdef HAVE_USLEEP
-                                       if( emfile % 4 == 3 ) usleep( 250 );
-#  else
-                                       if( emfile % 8 == 7 ) sleep( 1 );
-#  endif
-                               }
-
+                               Debug( LDAP_DEBUG_ANY,
+                                       "daemon: accept(%ld) failed errno=%d (%s)\n",
+                                       (long) slap_listeners[l]->sl_sd, err,
+                                       sock_errstr(err) );
+#endif
                                ldap_pvt_thread_yield();
                                continue;
                        }
-                       emfile = 0;
 
 #ifndef HAVE_WINSOCK
                        /* make sure descriptor number isn't too great */
@@ -1605,6 +1667,14 @@ slapd_daemon_task(
                        switch ( from.sa_addr.sa_family ) {
 #  ifdef LDAP_PF_LOCAL
                        case AF_LOCAL:
+                               /* FIXME: apparently accept doesn't fill
+                                * the sun_path sun_path member */
+                               if ( from.sa_un_addr.sun_path[0] == '\0' ) {
+                                       AC_MEMCPY( from.sa_un_addr.sun_path,
+                                                       slap_listeners[l]->sl_sa.sa_un_addr.sun_path,
+                                                       sizeof( from.sa_un_addr.sun_path ) );
+                               }
+
                                sprintf( peername, "PATH=%s", from.sa_un_addr.sun_path );
                                ssf = LDAP_PVT_SASL_LOCAL_SSF;
                                {
@@ -1612,10 +1682,11 @@ slapd_daemon_task(
                                        gid_t gid;
 
                                        if( getpeereid( s, &uid, &gid ) == 0 ) {
-                                               authid = ch_malloc(
+                                               authid.bv_val = ch_malloc(
                                                        sizeof("uidnumber=4294967295+gidnumber=4294967295,"
-                                                               "cn=peercred,cn=external,cn=auth"));
-                                               sprintf(authid, "uidnumber=%d+gidnumber=%d,"
+                                                       "cn=peercred,cn=external,cn=auth"));
+                                               authid.bv_len = sprintf( authid.bv_val,
+                                                       "uidnumber=%d+gidnumber=%d,"
                                                        "cn=peercred,cn=external,cn=auth",
                                                        (int) uid, (int) gid);
                                        }
@@ -1683,7 +1754,7 @@ slapd_daemon_task(
                                {
                                        /* DENY ACCESS */
                                        Statslog( LDAP_DEBUG_STATS,
-                                               "fd=%ld DENIED from %s (%s)",
+                                               "fd=%ld DENIED from %s (%s)\n",
                                                (long) s,
                                                dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
                                                peeraddr != NULL ? peeraddr : SLAP_STRING_UNKNOWN,
@@ -1699,14 +1770,14 @@ slapd_daemon_task(
                                dnsname != NULL ? dnsname : SLAP_STRING_UNKNOWN,
                                peername,
 #ifdef HAVE_TLS
-                               slap_listeners[l]->sl_is_tls,
+                               slap_listeners[l]->sl_is_tls ? CONN_IS_TLS : 0,
 #else
                                0,
 #endif
                                ssf,
-                               authid );
+                               authid.bv_val ? &authid : NULL );
 
-                       if( authid ) ch_free(authid);
+                       if( authid.bv_val ) ch_free(authid.bv_val);
 
                        if( id < 0 ) {
 #ifdef NEW_LOGGING
@@ -1735,7 +1806,7 @@ slapd_daemon_task(
                                slap_listeners[l]->sl_name.bv_val,
                                0 );
 
-                       slapd_add( s );
+                       slapd_add( s, 1 );
                        continue;
                }
 
@@ -1769,23 +1840,7 @@ slapd_daemon_task(
 #else
                for ( i = 0; i < nfds; i++ ) {
                        int     r, w;
-                       int     is_listener = 0;
 
-                       for ( l = 0; slap_listeners[l] != NULL; l++ ) {
-                               if ( i == slap_listeners[l]->sl_sd ) {
-#ifdef LDAP_CONNECTIONLESS
-                               /* The listener is the data port. Don't
-                                * skip it.
-                                */
-                                       if (slap_listeners[l]->sl_is_udp) continue;
-#endif
-                                       is_listener = 1;
-                                       break;
-                               }
-                       }
-                       if ( is_listener ) {
-                               continue;
-                       }
                        r = FD_ISSET( i, &readfds );
                        w = FD_ISSET( i, &writefds );
                        if ( r || w ) {
@@ -1815,7 +1870,6 @@ slapd_daemon_task(
 #endif
                {
                        ber_socket_t wd;
-                       int is_listener = 0;
 #ifdef HAVE_WINSOCK
                        wd = writefds.fd_array[i];
 #else
@@ -1825,18 +1879,6 @@ slapd_daemon_task(
                        wd = i;
 #endif
 
-                       for ( l = 0; slap_listeners[l] != NULL; l++ ) {
-                               if ( i == slap_listeners[l]->sl_sd ) {
-#ifdef LDAP_CONNECTIONLESS
-                                       if (slap_listeners[l]->sl_is_udp) continue;
-#endif
-                                       is_listener = 1;
-                                       break;
-                               }
-                       }
-                       if ( is_listener ) {
-                               continue;
-                       }
 #ifdef NEW_LOGGING
                        LDAP_LOG( CONNECTION, DETAIL2, 
                                "slapd_daemon_task: write active on %d\n", wd, 0, 0 );
@@ -1865,8 +1907,6 @@ slapd_daemon_task(
 #endif
                {
                        ber_socket_t rd;
-                       int is_listener = 0;
-
 #ifdef HAVE_WINSOCK
                        rd = readfds.fd_array[i];
 #else
@@ -1876,19 +1916,6 @@ slapd_daemon_task(
                        rd = i;
 #endif
 
-                       for ( l = 0; slap_listeners[l] != NULL; l++ ) {
-                               if ( rd == slap_listeners[l]->sl_sd ) {
-#ifdef LDAP_CONNECTIONLESS
-                                       if (slap_listeners[l]->sl_is_udp) continue;
-#endif
-                                       is_listener = 1;
-                                       break;
-                               }
-                       }
-                       if ( is_listener ) {
-                               continue;
-                       }
-
 #ifdef NEW_LOGGING
                        LDAP_LOG( CONNECTION, DETAIL2, 
                                "slapd_daemon_task: read activity on %d\n", rd, 0, 0 );
@@ -1907,7 +1934,9 @@ slapd_daemon_task(
                                slapd_close( rd );
                        }
                }
+#ifndef HAVE_YIELDING_SELECT
                ldap_pvt_thread_yield();
+#endif
        }
 
        if( slapd_shutdown == 1 ) {
@@ -1962,6 +1991,7 @@ slapd_daemon_task(
        slap_listeners = NULL;
 
        if( !slapd_gentle_shutdown ) {
+               slapd_abrupt_shutdown = 1;
                connections_shutdown();
        }
 
@@ -2071,11 +2101,13 @@ int sockdestroy(void)
 RETSIGTYPE
 slap_sig_shutdown( int sig )
 {
+#if 0
 #ifdef NEW_LOGGING
        LDAP_LOG( CONNECTION, CRIT, 
                "slap_sig_shutdown: signal %d\n", sig, 0, 0 );
 #else
        Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: signal %d\n", sig, 0, 0);
+#endif
 #endif
 
        /*
@@ -2086,13 +2118,7 @@ slap_sig_shutdown( int sig )
 
 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
        if (is_NT_Service && sig == SIGBREAK)
-#ifdef NEW_LOGGING
-           LDAP_LOG( CONNECTION, CRIT,
-                   "slap_sig_shutdown: SIGBREAK ignored.\n", 0, 0, 0 );
-#else
-           Debug(LDAP_DEBUG_TRACE, "slap_sig_shutdown: SIGBREAK ignored.\n",
-                 0, 0, 0);
-#endif
+               ;
        else
 #endif
 #ifdef SIGHUP
@@ -2118,10 +2144,15 @@ slap_sig_wake( int sig )
 }
 
 
-void slapd_add_internal(ber_socket_t s) {
-       slapd_add(s);
+void slapd_add_internal(ber_socket_t s, int isactive) {
+       slapd_add(s, isactive);
 }
 
 Listener ** slapd_get_listeners(void) {
        return slap_listeners;
 }
+
+void slap_wake_listener()
+{
+       WAKE_LISTENER(1);
+}