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

[libvirt] [PATCH 2/6] Add helper APIs for iterating over PCI device resource files



* src/pci.h, src/pci.c: Helper for iterating over PCI device
  resource files
* src/libvirt_private.syms: Export pciDeviceFileIterate
---
 src/libvirt_private.syms |    2 +
 src/pci.c                |   52 ++++++++++++++++++++++++++++++++++++++++++++++
 src/pci.h                |    9 +++++++-
 3 files changed, 62 insertions(+), 1 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 67f7aa2..93d55ca 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -291,6 +291,8 @@ pciDeviceListNew;
 pciDeviceListFree;
 pciDeviceListAdd;
 pciDeviceListDel;
+pciDeviceFileIterate;
+
 
 # qparams.h
 qparam_get_query;
diff --git a/src/pci.c b/src/pci.c
index 96e5d6d..feaa6e8 100644
--- a/src/pci.c
+++ b/src/pci.c
@@ -1022,3 +1022,55 @@ pciDeviceListFind(pciDeviceList *list, pciDevice *dev)
             return list->devs[i];
     return NULL;
 }
+
+
+int pciDeviceFileIterate(virConnectPtr conn,
+                         pciDevice *dev,
+                         pciDeviceFileActor actor,
+                         void *opaque)
+{
+    char *pcidir = NULL;
+    char *file = NULL;
+    DIR *dir = NULL;
+    int ret = -1;
+    struct dirent *ent;
+
+    if (virAsprintf(&pcidir, "/sys/bus/pci/devices/%04x:%02x:%02x.%x",
+                    dev->domain, dev->bus, dev->slot, dev->function) < 0) {
+        virReportOOMError(conn);
+        goto cleanup;
+    }
+
+    if (!(dir = opendir(pcidir))) {
+        virReportSystemError(conn, errno,
+                             _("cannot open %s"), pcidir);
+        goto cleanup;
+    }
+
+    while ((ent = readdir(dir)) != NULL) {
+        /* Device assignment requires:
+         *   $PCIDIR/config, $PCIDIR/resource, $PCIDIR/resourceNNN, $PCIDIR/rom
+         */
+        if (STREQ(ent->d_name, "config") ||
+            STRPREFIX(ent->d_name, "resource") ||
+            STREQ(ent->d_name, "rom")) {
+            if (virAsprintf(&file, "%s/%s", pcidir, ent->d_name) < 0) {
+                virReportOOMError(conn);
+                goto cleanup;
+            }
+            if ((actor)(conn, dev, file, opaque) < 0)
+                goto cleanup;
+
+            VIR_FREE(file);
+        }
+    }
+
+    ret = 0;
+
+cleanup:
+    if (dir)
+        closedir(dir);
+    VIR_FREE(file);
+    VIR_FREE(pcidir);
+    return ret;
+}
diff --git a/src/pci.h b/src/pci.h
index 685b0af..d23ee0e 100644
--- a/src/pci.h
+++ b/src/pci.h
@@ -22,7 +22,6 @@
 #ifndef __VIR_PCI_H__
 #define __VIR_PCI_H__
 
-#include <config.h>
 #include "internal.h"
 
 typedef struct _pciDevice pciDevice;
@@ -62,4 +61,12 @@ void           pciDeviceListDel  (virConnectPtr conn,
 pciDevice *    pciDeviceListFind (pciDeviceList *list,
                                   pciDevice *dev);
 
+typedef int (*pciDeviceFileActor)(virConnectPtr conn, pciDevice *dev,
+                                  const char *path, void *opaque);
+
+int pciDeviceFileIterate(virConnectPtr conn,
+                         pciDevice *dev,
+                         pciDeviceFileActor actor,
+                         void *opaque);
+
 #endif /* __VIR_PCI_H__ */
-- 
1.6.2.5


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