]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Cleanup "/sys/class/net" usage
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 15 Apr 2015 09:45:47 +0000 (11:45 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 15 Apr 2015 14:43:28 +0000 (16:43 +0200)
Throughout the code, we have several places need to construct a path
somewhere in /sys/class/net/... They are not consistent and nearly
each code piece invents its own way how to do it. So unify this by:

1) use virNetDevSysfsFile() wherever possible

2) At least use common macro SYSFS_NET_DIR declared in virnetdev.h at
   the rest of places which can't go with 1)

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
src/Makefile.am
src/parallels/parallels_network.c
src/util/virnetdev.c
src/util/virnetdev.h
src/util/virnetdevbridge.c
src/util/virnetdevmacvlan.c
src/util/virnetdevveth.c

index 91a4c17cbaf4137fe6e05242c48f42b5f262d8fe..3c9eac6bc4f23050dc2a091096042f581c6bd430 100644 (file)
@@ -1383,8 +1383,8 @@ noinst_LTLIBRARIES += libvirt_driver_parallels.la
 libvirt_la_BUILT_LIBADD += libvirt_driver_parallels.la
 libvirt_driver_parallels_la_CFLAGS = \
                -I$(srcdir)/conf $(AM_CFLAGS) \
-               $(PARALLELS_SDK_CFLAGS)
-libvirt_driver_parallels_la_LIBADD = $(PARALLELS_SDK_LIBS)
+               $(PARALLELS_SDK_CFLAGS) $(LIBNL_CFLAGS)
+libvirt_driver_parallels_la_LIBADD = $(PARALLELS_SDK_LIBS) $(LIBNL_LIBS)
 libvirt_driver_parallels_la_SOURCES = $(PARALLELS_DRIVER_SOURCES)
 endif WITH_PARALLELS
 
index 47f4886e9fe3efcaf122f98367e1356608d51bd8..12c5642a7e1be344a4a3f14dda0bab12989d6d6b 100644 (file)
@@ -28,6 +28,7 @@
 #include "viralloc.h"
 #include "virerror.h"
 #include "virfile.h"
+#include "virnetdev.h"
 #include "md5.h"
 #include "parallels_utils.h"
 #include "virstring.h"
@@ -39,8 +40,6 @@
     virReportErrorHelper(VIR_FROM_TEST, VIR_ERR_OPERATION_FAILED, __FILE__,    \
                      __FUNCTION__, __LINE__, _("Can't parse prlctl output"))
 
-#define SYSFS_NET_DIR "/sys/class/net"
-
 static int parallelsGetBridgedNetInfo(virNetworkDefPtr def, virJSONValuePtr jobj)
 {
     const char *ifname;
@@ -56,8 +55,7 @@ static int parallelsGetBridgedNetInfo(virNetworkDefPtr def, virJSONValuePtr jobj
         goto cleanup;
     }
 
-    if (virAsprintf(&bridgeLink, "%s/%s/brport/bridge",
-                    SYSFS_NET_DIR, ifname) < 0)
+    if (virAsprintf(&bridgeLink, SYSFS_NET_DIR "%s/brport/bridge", ifname) < 0)
         goto cleanup;
 
     if (virFileResolveLink(bridgeLink, &bridgePath) < 0) {
@@ -68,8 +66,8 @@ static int parallelsGetBridgedNetInfo(virNetworkDefPtr def, virJSONValuePtr jobj
     if (VIR_STRDUP(def->bridge, last_component(bridgePath)) < 0)
         goto cleanup;
 
-    if (virAsprintf(&bridgeAddressPath, "%s/%s/brport/bridge/address",
-                    SYSFS_NET_DIR, ifname) < 0)
+    if (virAsprintf(&bridgeAddressPath, SYSFS_NET_DIR "%s/brport/bridge/address",
+                    ifname) < 0)
         goto cleanup;
 
     if ((len = virFileReadAll(bridgeAddressPath, 18, &bridgeAddress)) < 0) {
index 9ef75f2a76d1cd81990d9e5c269a1b2181c792f6..a816e5d9da779130f3eaa9933757cdae55f81cd4 100644 (file)
@@ -1612,14 +1612,13 @@ int virNetDevValidateConfig(const char *ifname ATTRIBUTE_UNUSED,
 
 
 #ifdef __linux__
-# define NET_SYSFS "/sys/class/net/"
 
 int
 virNetDevSysfsFile(char **pf_sysfs_device_link, const char *ifname,
                    const char *file)
 {
 
-    if (virAsprintf(pf_sysfs_device_link, NET_SYSFS "%s/%s", ifname, file) < 0)
+    if (virAsprintf(pf_sysfs_device_link, SYSFS_NET_DIR "%s/%s", ifname, file) < 0)
         return -1;
     return 0;
 }
@@ -1629,7 +1628,7 @@ virNetDevSysfsDeviceFile(char **pf_sysfs_device_link, const char *ifname,
                      const char *file)
 {
 
-    if (virAsprintf(pf_sysfs_device_link, NET_SYSFS "%s/device/%s", ifname,
+    if (virAsprintf(pf_sysfs_device_link, SYSFS_NET_DIR "%s/device/%s", ifname,
                     file) < 0)
         return -1;
     return 0;
index 35353192429681bbb5a6bde8854b8ae89a5ef18f..72acda5ce47725eb0f6e0caeea14725edaa0d595 100644 (file)
@@ -219,6 +219,8 @@ int virNetDevSetRcvAllMulti(const char *ifname, bool receive)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
 int virNetDevGetRcvAllMulti(const char *ifname, bool *receive)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
+
+# define SYSFS_NET_DIR "/sys/class/net/"
 int virNetDevSysfsFile(char **pf_sysfs_device_link,
                        const char *ifname,
                        const char *file)
index 6be8aa37e95e8d4b568e823a5f87d200b20699e1..aa255d63f1ff48f1b5139fc23093ac2a34e26b19 100644 (file)
@@ -114,7 +114,6 @@ static int virNetDevBridgeCmd(const char *brname,
 #endif
 
 #if defined(HAVE_STRUCT_IFREQ) && defined(__linux__)
-# define SYSFS_NET_DIR "/sys/class/net"
 /*
  * Bridge parameters can be set via sysfs on newish kernels,
  * or by  ioctl on older kernels. Perhaps we could just use
@@ -130,7 +129,7 @@ static int virNetDevBridgeSet(const char *brname,
     char *path = NULL;
     int ret = -1;
 
-    if (virAsprintf(&path, "%s/%s/bridge/%s", SYSFS_NET_DIR, brname, paramname) < 0)
+    if (virAsprintf(&path, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname) < 0)
         return -1;
 
     if (virFileExists(path)) {
@@ -177,7 +176,7 @@ static int virNetDevBridgeGet(const char *brname,
     char *path = NULL;
     int ret = -1;
 
-    if (virAsprintf(&path, "%s/%s/bridge/%s", SYSFS_NET_DIR, brname, paramname) < 0)
+    if (virAsprintf(&path, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname) < 0)
         return -1;
 
     if (virFileExists(path)) {
@@ -235,8 +234,8 @@ virNetDevBridgePortSet(const char *brname,
 
     snprintf(valuestr, sizeof(valuestr), "%lu", value);
 
-    if (virAsprintf(&path, "%s/%s/brif/%s/%s",
-                    SYSFS_NET_DIR, brname, ifname, paramname) < 0)
+    if (virAsprintf(&path, SYSFS_NET_DIR "%s/brif/%s/%s",
+                    brname, ifname, paramname) < 0)
         return -1;
 
     if (!virFileExists(path))
@@ -265,8 +264,8 @@ virNetDevBridgePortGet(const char *brname,
     char *valuestr = NULL;
     int ret = -1;
 
-    if (virAsprintf(&path, "%s/%s/brif/%s/%s",
-                    SYSFS_NET_DIR, brname, ifname, paramname) < 0)
+    if (virAsprintf(&path, SYSFS_NET_DIR "%s/brif/%s/%s",
+                    brname, ifname, paramname) < 0)
         return -1;
 
     if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long), &valuestr) < 0)
index 5fd2097e6fc2d6676fef01fb06a787118911eb68..57b03d08b5473e9d14da62e065bc89c8a3aee05e 100644 (file)
@@ -236,19 +236,15 @@ static
 int virNetDevMacVLanTapOpen(const char *ifname,
                             int retries)
 {
-    FILE *file;
-    char path[64];
+    int ret = -1;
+    FILE *file = NULL;
+    char *path;
     int ifindex;
     char tapname[50];
     int tapfd;
 
-    if (snprintf(path, sizeof(path),
-                 "/sys/class/net/%s/ifindex", ifname) >= sizeof(path)) {
-        virReportSystemError(errno,
-                             "%s",
-                             _("buffer for ifindex path is too small"));
+    if (virNetDevSysfsFile(&path, ifname, "ifindex") < 0)
         return -1;
-    }
 
     file = fopen(path, "r");
 
@@ -256,15 +252,14 @@ int virNetDevMacVLanTapOpen(const char *ifname,
         virReportSystemError(errno,
                              _("cannot open macvtap file %s to determine "
                                "interface index"), path);
-        return -1;
+        goto cleanup;
     }
 
     if (fscanf(file, "%d", &ifindex) != 1) {
         virReportSystemError(errno,
                              "%s", _("cannot determine macvtap's tap device "
                              "interface index"));
-        VIR_FORCE_FCLOSE(file);
-        return -1;
+        goto cleanup;
     }
 
     VIR_FORCE_FCLOSE(file);
@@ -274,7 +269,7 @@ int virNetDevMacVLanTapOpen(const char *ifname,
         virReportSystemError(errno,
                              "%s",
                              _("internal buffer for tap device is too small"));
-        return -1;
+        goto cleanup;
     }
 
     while (1) {
@@ -288,12 +283,17 @@ int virNetDevMacVLanTapOpen(const char *ifname,
         break;
     }
 
-    if (tapfd < 0)
+    if (tapfd < 0) {
         virReportSystemError(errno,
                              _("cannot open macvtap tap device %s"),
                              tapname);
-
-    return tapfd;
+        goto cleanup;
+    }
+    ret = tapfd;
+ cleanup:
+    VIR_FREE(path);
+    VIR_FORCE_FCLOSE(file);
+    return ret;
 }
 
 
index e9d6f9c514d4e4566ea16f8277726b0136693a11..6905168120445a8cc0d6712a21a1ffb6c63acb3c 100644 (file)
@@ -47,7 +47,7 @@ static int virNetDevVethExists(int devNum)
 {
     int ret;
     char *path = NULL;
-    if (virAsprintf(&path, "/sys/class/net/vnet%d/", devNum) < 0)
+    if (virAsprintf(&path, SYSFS_NET_DIR "vnet%d/", devNum) < 0)
         return -1;
     ret = virFileExists(path) ? 1 : 0;
     VIR_DEBUG("Checked dev vnet%d usage: %d", devNum, ret);