r12531: 'make quicktest' was taking 15 minutes on my system due to failing DNS
authorAndrew Tridgell <tridge@samba.org>
Wed, 28 Dec 2005 03:04:40 +0000 (03:04 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:47:52 +0000 (13:47 -0500)
lookups in load_interfaces(). The reason was my eth0 interface was
down, and it was being interpreted as a DNS name.

This patch changes load_interfaces() to happening automatically when
interfaces are first needed instead of on the startup of every samba
binary. This means that (for example) ldbadd doesn't call
load_interfaces(), which means no slow DNS lookups.

I also reduced the number of static globals in interface.c to 1, and
changed from malloc to talloc

When you want to force a reload of the interfaces list, you now call
unload_interfaces(), which means the next call that needs the
interfaces list will reload it

13 files changed:
source/client/smbmount.c
source/client/smbspool.c
source/gtk/tools/gepdump.c
source/gtk/tools/gregedit.c
source/gtk/tools/gwcrontab.c
source/gtk/tools/gwsam.c
source/lib/cmdline/popt_common.c
source/lib/netif/interface.c
source/scripting/ejs/smbcalls_config.c
source/torture/gentest.c
source/torture/locktest.c
source/torture/locktest2.c
source/torture/masktest.c

index b2e6b9677f659e0df847598c6bc3300aa5419bfd..4bfeae1e24e4bf980cad7c5a1826cbc460ea919e 100644 (file)
@@ -915,7 +915,6 @@ static void parse_mount_smb(int argc, char **argv)
                pstrcpy(workgroup,lp_workgroup());
        }
 
-       load_interfaces();
        if (!*my_netbios_name) {
                pstrcpy(my_netbios_name, myhostname());
        }
index cb95c0d5a3151e2636d926dd143e6e7a26c2eb3d..26a42cb144d27853f194d709ac44fb3f2b858a4c 100644 (file)
@@ -185,8 +185,6 @@ static int          smb_print(struct smbcli_state *, char *, FILE *);
   if (workgroup == NULL)
     workgroup = lp_workgroup();
 
-  load_interfaces();
-
   do
   {
     if ((cli = smb_connect(workgroup, server, printer, username, password)) == NULL)
index 622d4b99aa14b044fd55365d41dfab1f8c039452..9275bc1ebd0bdd7e8a36360af6b123d6bf7f3aef 100644 (file)
@@ -464,7 +464,6 @@ int main(int argc, char **argv)
 {
        gepdump_init_subsystems;
        lp_load();
-       load_interfaces();
        setup_logging(argv[0], DEBUG_STDERR);
 
        gtk_init(&argc, &argv);
index cb0d927439dcf4800584efa5dce989d28b6df5c6..f98a366e9b6d4dfcd685302685c9430e176fdd98 100644 (file)
@@ -969,7 +969,6 @@ int main(int argc, char *argv[])
        gregedit_init_subsystems;
        registry_init();
        lp_load();
-       load_interfaces();
        setup_logging(argv[0], DEBUG_STDERR);
 
        mem_ctx = talloc_init("gregedit");
index fd4f41d06fa25f3ebf0530247c5b3656b67920ae..391110c56d82af7623bac937696485062234ed34 100644 (file)
@@ -495,7 +495,6 @@ static GtkWidget*create_new_job_dialog (void)
 {
        gwcrontab_init_subsystems;
        lp_load();
-       load_interfaces();
        setup_logging(argv[0], DEBUG_STDERR);
 
        gtk_init(&argc, &argv);
index f1cf8d5e857628ede6a449d713d3f57ba4d9a824..963397810c0af2f18fdaaaeb74d2fc43c797196c 100644 (file)
@@ -404,7 +404,6 @@ static GtkWidget* create_mainwindow (void)
 {
        gwsam_init_subsystems;
        lp_load();
-       load_interfaces();
        setup_logging(argv[0], DEBUG_STDERR);
 
        gtk_init(&argc, &argv);
index 0aa4a634fc83c9e0bd528e9cec1802a4819aee11..b1f4563fb13062bb151e0f1c2c9f71e29bc7f453 100644 (file)
@@ -52,7 +52,6 @@ static void popt_common_callback(poptContext con,
                /* Hook any 'every Samba program must do this, after
                 * the smb.conf is setup' functions here */
                lp_load();
-               load_interfaces();
                return;
        }
 
index 2abb08e090b4daeefe6eea5f6df212beb0181078..ce94f482f5386d381fe2753581060485a94d052e 100644 (file)
 #include "lib/netif/netif.h"
 #include "dlinklist.h"
 
-static struct iface_struct *probed_ifaces;
-static int total_probed;
-
-static struct ipv4_addr allones_ip;
-struct ipv4_addr loopback_ip;
-
 /* used for network interfaces */
 struct interface {
        struct interface *next, *prev;
@@ -80,12 +74,12 @@ static void add_interface(struct in_addr ip, struct in_addr nmask)
                return;
        }
 
-       if (nmask.s_addr == allones_ip.addr) {
+       if (nmask.s_addr == ~0) {
                DEBUG(3,("not adding non-broadcast interface %s\n",inet_ntoa(ip)));
                return;
        }
 
-       iface = malloc_p(struct interface);
+       iface = talloc(local_interfaces, struct interface);
        if (!iface) return;
        
        ZERO_STRUCTPN(iface);
@@ -114,7 +108,9 @@ This handles the following different forms:
 4) ip/mask
 5) bcast/mask
 ****************************************************************************/
-static void interpret_interface(TALLOC_CTX *mem_ctx, const char *token)
+static void interpret_interface(const char *token, 
+                               struct iface_struct *probed_ifaces, 
+                               int total_probed)
 {
        struct in_addr ip, nmask;
        char *p;
@@ -143,7 +139,7 @@ static void interpret_interface(TALLOC_CTX *mem_ctx, const char *token)
                ip.s_addr = interpret_addr2(token).addr;
                for (i=0;i<total_probed;i++) {
                        if (ip.s_addr == probed_ifaces[i].ip.s_addr &&
-                           allones_ip.addr != probed_ifaces[i].netmask.s_addr) {
+                           probed_ifaces[i].netmask.s_addr != ~0) {
                                add_interface(probed_ifaces[i].ip,
                                              probed_ifaces[i].netmask);
                                return;
@@ -184,40 +180,24 @@ static void interpret_interface(TALLOC_CTX *mem_ctx, const char *token)
 /****************************************************************************
 load the list of network interfaces
 ****************************************************************************/
-void load_interfaces(void)
+static void load_interfaces(void)
 {
        const char **ptr;
        int i;
        struct iface_struct ifaces[MAX_INTERFACES];
-       TALLOC_CTX *mem_ctx;
+       struct ipv4_addr loopback_ip;
+       int total_probed;
 
-       ptr = lp_interfaces();
-       mem_ctx = talloc_init("load_interfaces");
-       if (!mem_ctx) {
-               DEBUG(2,("no memory to load interfaces \n"));
+       if (local_interfaces != NULL) {
                return;
        }
 
-       allones_ip = interpret_addr2("255.255.255.255");
+       ptr = lp_interfaces();
        loopback_ip = interpret_addr2("127.0.0.1");
 
-       SAFE_FREE(probed_ifaces);
-
-       /* dump the current interfaces if any */
-       while (local_interfaces) {
-               struct interface *iface = local_interfaces;
-               DLIST_REMOVE(local_interfaces, local_interfaces);
-               ZERO_STRUCTPN(iface);
-               SAFE_FREE(iface);
-       }
-
        /* probe the kernel for interfaces */
        total_probed = get_interfaces(ifaces, MAX_INTERFACES);
 
-       if (total_probed > 0) {
-               probed_ifaces = memdup(ifaces, sizeof(ifaces[0])*total_probed);
-       }
-
        /* if we don't have a interfaces line then use all broadcast capable 
           interfaces except loopback */
        if (!ptr || !*ptr || !**ptr) {
@@ -225,56 +205,43 @@ void load_interfaces(void)
                        DEBUG(0,("ERROR: Could not determine network interfaces, you must use a interfaces config line\n"));
                }
                for (i=0;i<total_probed;i++) {
-                       if (probed_ifaces[i].netmask.s_addr != allones_ip.addr &&
-                           probed_ifaces[i].ip.s_addr != loopback_ip.addr) {
-                               add_interface(probed_ifaces[i].ip, 
-                                             probed_ifaces[i].netmask);
+                       if (ifaces[i].netmask.s_addr != ~0 &&
+                           ifaces[i].ip.s_addr != loopback_ip.addr) {
+                               add_interface(ifaces[i].ip, 
+                                             ifaces[i].netmask);
                        }
                }
-               goto exit;
        }
 
-       if (ptr) {
-               while (*ptr) {
-                       interpret_interface(mem_ctx, *ptr);
-                       ptr++;
-               }
+       while (ptr && *ptr) {
+               interpret_interface(*ptr, ifaces, total_probed);
+               ptr++;
        }
 
        if (!local_interfaces) {
                DEBUG(0,("WARNING: no network interfaces found\n"));
        }
-       
-exit:
-       talloc_free(mem_ctx);
 }
 
 
-/****************************************************************************
-return True if the list of probed interfaces has changed
-****************************************************************************/
-BOOL interfaces_changed(void)
+/*
+  unload the interfaces list, so it can be reloaded when needed
+*/
+void unload_interfaces(void)
 {
-       int n;
-       struct iface_struct ifaces[MAX_INTERFACES];
-
-       n = get_interfaces(ifaces, MAX_INTERFACES);
-
-       if ((n > 0 )&& (n != total_probed ||
-           memcmp(ifaces, probed_ifaces, sizeof(ifaces[0])*n))) {
-               return True;
-       }
-       
-       return False;
+       talloc_free(local_interfaces);
+       local_interfaces = NULL;
 }
 
-
 /****************************************************************************
   check if an IP is one of mine
   **************************************************************************/
 BOOL ismyip(struct ipv4_addr ip)
 {
        struct interface *i;
+
+       load_interfaces();
+
        for (i=local_interfaces;i;i=i->next) {
                if (i->ip.addr == ip.addr) return True;
        }
@@ -289,6 +256,8 @@ int iface_count(void)
        int ret = 0;
        struct interface *i;
 
+       load_interfaces();
+
        for (i=local_interfaces;i;i=i->next)
                ret++;
        return ret;
@@ -301,6 +270,8 @@ const char *iface_n_ip(int n)
 {
        struct interface *i;
   
+       load_interfaces();
+
        for (i=local_interfaces;i && n;i=i->next)
                n--;
 
@@ -317,6 +288,8 @@ const char *iface_n_bcast(int n)
 {
        struct interface *i;
   
+       load_interfaces();
+
        for (i=local_interfaces;i && n;i=i->next)
                n--;
 
@@ -333,6 +306,8 @@ const char *iface_n_netmask(int n)
 {
        struct interface *i;
   
+       load_interfaces();
+
        for (i=local_interfaces;i && n;i=i->next)
                n--;
 
@@ -350,6 +325,9 @@ const char *iface_best_ip(const char *dest)
 {
        struct interface *iface;
        struct in_addr ip;
+
+       load_interfaces();
+
        ip.s_addr = interpret_addr(dest);
        iface = iface_find(ip, True);
        if (iface) {
@@ -364,6 +342,9 @@ const char *iface_best_ip(const char *dest)
 BOOL iface_is_local(const char *dest)
 {
        struct in_addr ip;
+
+       load_interfaces();
+
        ip.s_addr = interpret_addr(dest);
        if (iface_find(ip, True)) {
                return True;
index a64d57a359649fe9048fac56d1465ffe5cfe3de9..b0b1420ba2c32b1e0ac0df762733e8efd2eda857 100644 (file)
@@ -188,7 +188,7 @@ static int ejs_lpReload(MprVarHandle eid, int argc, char **argv)
 {
        BOOL ret = lp_load();
        if (ret) {
-               load_interfaces();
+               unload_interfaces();
        }
        mpr_Return(eid, mprCreateBoolVar(ret));
        return 0;
index 9f2bb64b60ce005332802af668911537d6d3a6d0..abd992156962a5c9cf855cc31ad36f52439d1f48 100644 (file)
@@ -2149,7 +2149,6 @@ static void usage(void)
        argv += NSERVERS;
 
        lp_load();
-       load_interfaces();
 
        servers[0].credentials = cli_credentials_init(talloc_autofree_context());
        servers[1].credentials = cli_credentials_init(talloc_autofree_context());
index f302240fa28698c8bbce2a3e4b651f25e9b016c7..a94a6f1ec9ebec1d409681f1fa6317b30aad82f6 100644 (file)
@@ -478,7 +478,6 @@ static void usage(void)
        argv += NSERVERS;
 
        lp_load();
-       load_interfaces();
 
        servers[0] = cli_credentials_init(talloc_autofree_context());
        servers[1] = cli_credentials_init(talloc_autofree_context());
index 062861a75fcec9e59a692fe93f8914f1eca8cab6..9b1a44e72715dea8bb1ccdfdc3c49921232bcc28 100644 (file)
@@ -485,7 +485,6 @@ static void usage(void)
        argv += 4;
 
        lp_load();
-       load_interfaces();
 
        if (getenv("USER")) {
                fstrcpy(username,getenv("USER"));
index db54a157e143924ca0d76c10837a06916199c18a..5e8f973c4c7cbb341a3e4234b2e02d8b76e04ee2 100644 (file)
@@ -298,7 +298,6 @@ static void usage(void)
        argv += 1;
 
        lp_load();
-       load_interfaces();
 
        credentials = cli_credentials_init(talloc_autofree_context());
        cli_credentials_guess(credentials);