[PATCH v2 2/7] virNetDevMacVLanTapOpen: Slightly rework

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



There are few outdated things. Firstly, we don't need to undergo
the torture of fopen, fscanf and fclose just to get the interface
index when we have nice wrapper over that: virNetDevGetIndex.
Secondly, we don't need to have statically allocated buffer for
the path we are opening.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/util/virnetdevmacvlan.c | 34 ++++------------------------------
 1 file changed, 4 insertions(+), 30 deletions(-)

diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
index 9384b9f..4657ff2 100644
--- a/src/util/virnetdevmacvlan.c
+++ b/src/util/virnetdevmacvlan.c
@@ -237,40 +237,15 @@ int virNetDevMacVLanTapOpen(const char *ifname,
                             int retries)
 {
     int ret = -1;
-    FILE *file = NULL;
-    char *path;
     int ifindex;
-    char tapname[50];
+    char *tapname = NULL;
     int tapfd;
 
-    if (virNetDevSysfsFile(&path, ifname, "ifindex") < 0)
+    if (virNetDevGetIndex(ifname, &ifindex) < 0)
         return -1;
 
-    file = fopen(path, "r");
-
-    if (!file) {
-        virReportSystemError(errno,
-                             _("cannot open macvtap file %s to determine "
-                               "interface index"), path);
+    if (virAsprintf(&tapname, "/dev/tap%d", ifindex) < 0)
         goto cleanup;
-    }
-
-    if (fscanf(file, "%d", &ifindex) != 1) {
-        virReportSystemError(errno,
-                             "%s", _("cannot determine macvtap's tap device "
-                             "interface index"));
-        goto cleanup;
-    }
-
-    VIR_FORCE_FCLOSE(file);
-
-    if (snprintf(tapname, sizeof(tapname),
-                 "/dev/tap%d", ifindex) >= sizeof(tapname)) {
-        virReportSystemError(errno,
-                             "%s",
-                             _("internal buffer for tap device is too small"));
-        goto cleanup;
-    }
 
     while (1) {
         /* may need to wait for udev to be done */
@@ -291,8 +266,7 @@ int virNetDevMacVLanTapOpen(const char *ifname,
     }
     ret = tapfd;
  cleanup:
-    VIR_FREE(path);
-    VIR_FORCE_FCLOSE(file);
+    VIR_FREE(tapname);
     return ret;
 }
 
-- 
2.4.10

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]