]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/daemon.c
ITS#2884 silence warning. We don't dereference this pointer, we just use
[openldap] / servers / slapd / daemon.c
index a361be936afc9bdac4f583d3107073e9a17317a9..e8bab2673664b792cd065a3fca1f41676604fb0e 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-2003 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>
@@ -70,6 +87,7 @@ do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
 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;
@@ -196,8 +214,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 ));
@@ -210,7 +233,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 );
@@ -232,10 +257,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, 
@@ -731,15 +758,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;
@@ -752,12 +785,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;
@@ -770,8 +803,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++;
@@ -788,7 +821,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
@@ -1114,7 +1147,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 );
@@ -1166,7 +1199,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
@@ -1223,7 +1256,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
@@ -1250,11 +1283,9 @@ slapd_daemon_task(
                struct timeval          tv;
                struct timeval          *tvp;
 
-#ifdef LDAP_SYNCREPL
                struct timeval          *cat;
                time_t                          tdelta = 1;
                struct re_s*            rtask;
-#endif
                now = slap_get_time();
 
                if( ( global_idletimeout > 0 ) &&
@@ -1329,29 +1360,30 @@ slapd_daemon_task(
 
                ldap_pvt_thread_mutex_unlock( &slap_daemon.sd_mutex );
 
-               if ( !at ) {
-                       at = ldap_pvt_thread_pool_backload(&connection_pool) -
-                                ldap_pvt_runqueue_persistent_backload( &syncrepl_rq );
-               }
-
-               tvp = at ? &tv : NULL;
+               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 ) {
@@ -1364,7 +1396,6 @@ slapd_daemon_task(
                                tvp = &tv;
                        }
                }
-#endif
 
                for ( l = 0; slap_listeners[l] != NULL; l++ ) {
                        if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID ||
@@ -1495,7 +1526,7 @@ slapd_daemon_task(
                                    id = connection_init(
                                        slap_listeners[l]->sl_sd,
                                        slap_listeners[l], "", "",
-                                       2, ssf, authid );
+                                       CONN_IS_UDP, ssf, authid );
                                    slap_listeners[l]->sl_is_udp++;
                                }
                                continue;
@@ -1725,7 +1756,7 @@ 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
@@ -1761,7 +1792,7 @@ slapd_daemon_task(
                                slap_listeners[l]->sl_name.bv_val,
                                0 );
 
-                       slapd_add( s );
+                       slapd_add( s, 1 );
                        continue;
                }
 
@@ -1944,6 +1975,7 @@ slapd_daemon_task(
        slap_listeners = NULL;
 
        if( !slapd_gentle_shutdown ) {
+               slapd_abrupt_shutdown = 1;
                connections_shutdown();
        }
 
@@ -2070,15 +2102,7 @@ slap_sig_shutdown( int sig )
 
 #if HAVE_NT_SERVICE_MANAGER && SIGBREAK
        if (is_NT_Service && sig == SIGBREAK)
-#if 0
-#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
-#endif
+               ;
        else
 #endif
 #ifdef SIGHUP
@@ -2104,8 +2128,8 @@ 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) {