]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/daemon.c
Changed search attrs to struct berval **.
[openldap] / servers / slapd / daemon.c
index c3539448969f144113fa8db5ae273f09afeef26f..911db57cd54f68b91af3add7284a032b9244bfbe 100644 (file)
@@ -17,6 +17,7 @@
 #include <ac/unistd.h>
 
 #include "ldap_pvt.h"
+#include "ldap_pvt_thread.h"
 #include "lutil.h"
 #include "slap.h"
 
@@ -64,12 +65,6 @@ static int waking;
 do { if (w) tcp_write( wake_sds[1], "0", 1 ); } while(0)
 #endif
 
-#ifdef HAVE_NT_SERVICE_MANAGER
-/* in nt_main.c */
-extern ldap_pvt_thread_cond_t                  started_event;
-extern int       is_NT_Service;
-#endif
-
 #ifndef HAVE_WINSOCK
 static
 #endif
@@ -374,7 +369,7 @@ static int slap_get_listener_addresses(
                hints.ai_family = AF_UNSPEC;
                snprintf(serv, sizeof serv, "%d", port);
 
-               if (err = getaddrinfo(host, serv, &hints, &res)) {
+               if ( (err = getaddrinfo(host, serv, &hints, &res)) ) {
 #ifdef NEW_LOGGING
                        LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
                                   "slap_get_listener_addresses: getaddrinfo failed: %s\n",
@@ -390,15 +385,24 @@ static int slap_get_listener_addresses(
                for (n=2; (sai = sai->ai_next) != NULL; n++) {
                        /* EMPTY */ ;
                }
-               *sal = ch_malloc(n * sizeof(void *));
+               *sal = ch_calloc(n, sizeof(void *));
                if (*sal == NULL) {
                        return -1;
                }
 
                sap = *sal;
+               *sap = NULL;
 
                for ( sai=res; sai; sai=sai->ai_next ) {
                        if( sai->ai_addr == NULL ) {
+#ifdef NEW_LOGGING
+                               LDAP_LOG(( "connection", LDAP_LEVEL_INFO,
+                                       "slap_get_listener_addresses: "
+                                       "getaddrinfo ai_addr is NULL?\n" ));
+#else
+                               Debug( LDAP_DEBUG_ANY, "slap_get_listener_addresses: "
+                                       "getaddrinfo ai_addr is NULL?\n", 0, 0, 0 );
+#endif
                                freeaddrinfo(res);
                                goto errexit;
                        }
@@ -428,13 +432,14 @@ static int slap_get_listener_addresses(
                                *sap = NULL;
                                break;
                        }
+
                        if (*sap != NULL) {
                                (*sap)->sa_family = sai->ai_family;
                                sap++;
+                               *sap = NULL;
                        }
                }
 
-               *sap = NULL;
                freeaddrinfo(res);
 #else
                struct in_addr in;
@@ -492,7 +497,7 @@ static Listener * slap_open_listener(
        Listener *li;
        LDAPURLDesc *lud;
        unsigned short port;
-       int err, addrlen;
+       int err, addrlen = 0;
        struct sockaddr **sal, **psal;
        int socktype = SOCK_STREAM;     /* default to COTS */
 
@@ -563,8 +568,7 @@ static Listener * slap_open_listener(
 #endif
        } else {
 #ifdef LDAP_CONNECTIONLESS
-               if ( tmp == LDAP_PROTO_UDP )
-                       l.sl_is_udp = 1;
+               l.sl_is_udp = ( tmp == LDAP_PROTO_UDP );
 #endif
                if( lud->lud_host == NULL || lud->lud_host[0] == '\0'
                        || strcmp(lud->lud_host, "*") == 0 )
@@ -729,11 +733,10 @@ static Listener * slap_open_listener(
                inet_ntop( AF_INET, &((struct sockaddr_in *)*sal)->sin_addr,
                           addr, sizeof(addr) );
                s = addr;
-               port = ((struct sockaddr_in *)*sal) ->sin_port;
 #else
-               s = inet_ntoa( l.sl_addr.sin_addr );
-               port = l.sl_addr.sin_port;
+               s = inet_ntoa( ((struct sockaddr_in *) *sal)->sin_addr );
 #endif
+               port = ((struct sockaddr_in *)*sal) ->sin_port;
                l.sl_name = ch_malloc( sizeof("IP=255.255.255.255:65535") );
                sprintf( l.sl_name, "IP=%s:%d",
                         s != NULL ? s : "unknown" , port );
@@ -931,9 +934,12 @@ slapd_daemon_task(
 )
 {
        int l;
-       time_t  last_idle_check = slap_get_time();
+       time_t  last_idle_check = 0;
        time( &starttime );
 
+       if ( global_idletimeout > 0 ) {
+               last_idle_check = slap_get_time();
+       }
        for ( l = 0; slap_listeners[l] != NULL; l++ ) {
                if ( slap_listeners[l]->sl_sd == AC_SOCKET_INVALID )
                        continue;
@@ -981,9 +987,10 @@ slapd_daemon_task(
                ber_socket_t nfds;
 #define SLAPD_EBADF_LIMIT 16
                int ebadf = 0;
+               int emfile = 0;
 
 #define SLAPD_IDLE_CHECK_LIMIT 4
-               time_t  now = slap_get_time();
+               time_t  now;
 
 
                fd_set                  readfds;
@@ -991,16 +998,20 @@ slapd_daemon_task(
                Sockaddr                from;
 
 #if defined(SLAPD_RLOOKUPS)
-       struct hostent          *hp;
+               struct hostent          *hp;
 #endif
                struct timeval          zero;
                struct timeval          *tvp;
 
-               if( global_idletimeout > 0 && difftime(
-                       last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT,
-                       now ) < 0 )
-               {
-                       connections_timeout_idle(now);
+               if( emfile ) {
+                       now = slap_get_time();
+                       connections_timeout_idle( now );
+               }
+               else if ( global_idletimeout > 0 ) {
+                       now = slap_get_time();
+                       if ( difftime( last_idle_check+global_idletimeout/SLAPD_IDLE_CHECK_LIMIT, now ) < 0 ) {
+                               connections_timeout_idle( now );
+                       }
                }
 
                FD_ZERO( &writefds );
@@ -1145,7 +1156,7 @@ slapd_daemon_task(
                        slap_ssf_t ssf = 0;
                        char *authid = NULL;
 
-                       char    *dnsname;
+                       char    *dnsname = NULL;
                        char    *peeraddr;
 #ifdef LDAP_PF_LOCAL
                        char    peername[MAXPATHLEN + sizeof("PATH=")];
@@ -1187,32 +1198,59 @@ slapd_daemon_task(
                                (struct sockaddr *) &from, &len );
                        if ( s == AC_SOCKET_INVALID ) {
                                int err = sock_errno();
+
+#ifdef EMFILE
+                               if( err == EMFILE ) {
+                                       emfile++;
+                               } else
+#endif
+#ifdef ENFILE
+                               if( err == ENFILE ) {
+                                       emfile++;
+                               } else 
+#endif
+                               {
+                                       emfile=0;
+                               }
+
+                               if( emfile < 3 ) {
 #ifdef NEW_LOGGING
-                               LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
-                                          "slapd_daemon_task: accept(%ld) failed errno=%d (%s)\n",
-                                          (long)slap_listeners[l]->sl_sd, err, sock_errstr(err) ));
+                                       LDAP_LOG(( "connection", LDAP_LEVEL_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
+                                       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
+                               }
+
                                ldap_pvt_thread_yield();
                                continue;
                        }
+                       emfile = 0;
 
 #ifndef HAVE_WINSOCK
                        /* make sure descriptor number isn't too great */
                        if ( s >= dtblsize ) {
 #ifdef NEW_LOGGING
                                LDAP_LOG(( "connection", LDAP_LEVEL_ERR,
-                                          "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
-                                          (long)s, (long)dtblsize ));
+                                  "slapd_daemon_task: %ld beyond descriptor table size %ld\n",
+                                  (long)s, (long)dtblsize ));
 #else
                                Debug( LDAP_DEBUG_ANY,
                                        "daemon: %ld beyond descriptor table size %ld\n",
                                        (long) s, (long) dtblsize, 0 );
 #endif
+
                                slapd_close(s);
                                ldap_pvt_thread_yield();
                                continue;
@@ -1636,9 +1674,15 @@ slapd_daemon_task(
                        }
 #endif /* LDAP_PF_LOCAL */
                        slapd_close( slap_listeners[l]->sl_sd );
-                       break;
                }
+               if ( slap_listeners[l]->sl_url )
+                       free ( slap_listeners[l]->sl_url );
+               if ( slap_listeners[l]->sl_name )
+                       free ( slap_listeners[l]->sl_name );
+               free ( slap_listeners[l] );
        }
+       free ( slap_listeners );
+       slap_listeners = NULL;
 
 #ifdef NEW_LOGGING
        LDAP_LOG(( "connection", LDAP_LEVEL_CRIT,