[libvirt] [PATCH 12/17] nss: refactor code for processing mac addresses

Daniel P. Berrangé berrange at redhat.com
Thu Aug 1 15:00:14 UTC 2019


Build a list of mac addresses immediately, so that later code
searching for leases can be simplified and avoid needing to
use the virMacMap object.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 cfg.mk                  |  4 +--
 tools/nss/libvirt_nss.c | 73 ++++++++++++++++++++++++-----------------
 2 files changed, 45 insertions(+), 32 deletions(-)

diff --git a/cfg.mk b/cfg.mk
index f4cd215abc..8c352d7b9a 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -1226,7 +1226,7 @@ exclude_file_name_regexp--sc_prohibit_asprintf = \
   ^(cfg\.mk|bootstrap.conf$$|examples/|src/util/virstring\.[ch]$$|tests/vircgroupmock\.c|tools/virt-login-shell\.c|tools/nss/libvirt_nss\.c$$)
 
 exclude_file_name_regexp--sc_prohibit_strdup = \
-  ^(docs/|examples/|src/util/virstring\.c|tests/vir(netserverclient|cgroup)mock.c|tests/commandhelper\.c$$)
+  ^(docs/|examples/|src/util/virstring\.c|tests/vir(netserverclient|cgroup)mock.c|tests/commandhelper\.c|tools/nss/libvirt_nss\.c$$)
 
 exclude_file_name_regexp--sc_prohibit_close = \
   (\.p[yl]$$|\.spec\.in$$|^docs/|^(src/util/virfile\.c|src/libvirt-stream\.c|tests/(vir.+mock\.c|commandhelper\.c|qemusecuritymock\.c))$$)
@@ -1259,7 +1259,7 @@ exclude_file_name_regexp--sc_prohibit_canonicalize_file_name = \
   ^(cfg\.mk|tests/virfilemock\.c)$$
 
 exclude_file_name_regexp--sc_prohibit_raw_allocation = \
-  ^(docs/hacking\.html\.in|src/util/viralloc\.[ch]|examples/.*|tests/(securityselinuxhelper|(vircgroup|nss)mock|commandhelper)\.c|tools/wireshark/src/packet-libvirt\.c)$$
+  ^(docs/hacking\.html\.in|src/util/viralloc\.[ch]|examples/.*|tests/(securityselinuxhelper|(vircgroup|nss)mock|commandhelper)\.c|tools/wireshark/src/packet-libvirt\.c|tools/nss/libvirt_nss\.c)$$
 
 exclude_file_name_regexp--sc_prohibit_readlink = \
   ^src/(util/virutil|lxc/lxc_container)\.c$$
diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c
index ed2ad956e9..a849b8e5f7 100644
--- a/tools/nss/libvirt_nss.c
+++ b/tools/nss/libvirt_nss.c
@@ -274,8 +274,10 @@ findLease(const char *name,
     ssize_t nleases;
     VIR_AUTOFREE(leaseAddress *) tmpAddress = NULL;
     size_t ntmpAddress = 0;
-    VIR_AUTOFREE(virMacMapPtr *) macmaps = NULL;
-    size_t nMacmaps = 0;
+    virMacMapPtr map = NULL;
+    char **macs = NULL;
+    size_t nmacs = 0;
+    size_t i;
 
     *address = NULL;
     *naddress = 0;
@@ -313,23 +315,43 @@ findLease(const char *name,
                 goto cleanup;
             }
             VIR_FREE(path);
+#if defined(LIBVIRT_NSS_GUEST)
         } else if (dlen >= 5 && STREQ(entry->d_name + dlen - 5, ".macs")) {
+            const char * const *newmacs;
             if (asprintf(&path, "%s/%s", leaseDir, entry->d_name) < 0)
                 goto cleanup;
 
-            if (VIR_REALLOC_N_QUIET(macmaps, nMacmaps + 1) < 0) {
-                VIR_FREE(path);
-                goto cleanup;
-            }
-
             DEBUG("Processing %s", path);
-            if (!(macmaps[nMacmaps] = virMacMapNew(path))) {
+            if (!(map = virMacMapNew(path))) {
                 ERROR("Unable to parse %s", path);
                 VIR_FREE(path);
                 goto cleanup;
             }
-            nMacmaps++;
             VIR_FREE(path);
+
+            DEBUG("Looking up macs in %p for %s", map, name);
+            newmacs = virMacMapLookup(map, name);
+            for (i = 0; newmacs && newmacs[i] != NULL; i++)
+                ;
+
+            DEBUG("Got %zu macs", i);
+            if (i > 0) {
+                if (VIR_REALLOC_N_QUIET(macs, nmacs + i + 1) < 0)
+                    goto cleanup;
+
+                for (i = 0; newmacs[i] != NULL; i++) {
+                    char *macdup;
+                    if (!(macdup = strdup(newmacs[i])))
+                        goto cleanup;
+                    DEBUG("Capture mac %s", macdup);
+                    macs[nmacs++] = macdup;
+                }
+                macs[nmacs] = NULL;
+            }
+
+            virObjectUnref(map);
+            map = NULL;
+#endif /* LIBVIRT_NSS_GUEST */
         }
 
         errno = 0;
@@ -340,29 +362,18 @@ findLease(const char *name,
     nleases = virJSONValueArraySize(leases_array);
     DEBUG("Read %zd leases", nleases);
 
-#if !defined(LIBVIRT_NSS_GUEST)
+#if defined(LIBVIRT_NSS_GUEST)
+    DEBUG("Finding with %zu macs", nmacs);
+    if (!nmacs)
+        goto cleanup;
+#endif
+
     if (findLeaseInJSON(&tmpAddress, &ntmpAddress,
                         leases_array, nleases,
-                        name, NULL, af, found) < 0)
+                        name, (const char**)macs, af, found) < 0)
         goto cleanup;
 
-#else /* defined(LIBVIRT_NSS_GUEST) */
-
-    size_t i;
-    for (i = 0; i < nMacmaps; i++) {
-        const char **macs = (const char **) virMacMapLookup(macmaps[i], name);
-
-        if (!macs)
-            continue;
-
-        if (findLeaseInJSON(&tmpAddress, &ntmpAddress,
-                            leases_array, nleases,
-                            name, macs, af, found) < 0)
-            goto cleanup;
-    }
-
-#endif /* defined(LIBVIRT_NSS_GUEST) */
-
+    DEBUG("Found %zu addresses", ntmpAddress);
     sortAddr(tmpAddress, ntmpAddress);
 
     VIR_STEAL_PTR(*address, tmpAddress);
@@ -372,11 +383,13 @@ findLease(const char *name,
     ret = 0;
 
  cleanup:
+    virObjectUnref(map);
     *errnop = errno;
+    for (i = 0; i < nmacs; i++)
+        free(macs[i]);
+    free(macs);
     if (dir)
         closedir(dir);
-    while (nMacmaps)
-        virObjectUnref(macmaps[--nMacmaps]);
     return ret;
 }
 
-- 
2.21.0




More information about the libvir-list mailing list