]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
add virhostdev files to maintain global state of host devices
authorChunyan Liu <cyliu@suse.com>
Sat, 1 Mar 2014 06:28:59 +0000 (14:28 +0800)
committerDaniel P. Berrange <berrange@redhat.com>
Tue, 4 Mar 2014 12:28:45 +0000 (12:28 +0000)
Signed-off-by: Chunyan Liu <cyliu@suse.com>
po/POTFILES.in
src/Makefile.am
src/libvirt_private.syms
src/util/virhostdev.c [new file with mode: 0644]
src/util/virhostdev.h [new file with mode: 0644]

index c565771880420b78d49380e7715833f7eb594ffa..bcc0ee01f92ef5620b5166382dfb8cb7d1550c11 100644 (file)
@@ -164,6 +164,7 @@ src/util/vireventpoll.c
 src/util/virfile.c
 src/util/virhash.c
 src/util/virhook.c
+src/util/virhostdev.c
 src/util/viridentity.c
 src/util/virinitctl.c
 src/util/viriptables.c
index 6ef32eed9f8e7e55957809667a902eb9106962c9..25d0370f0f8a7a3efc04ddf706eaf01025ea2ba5 100644 (file)
@@ -104,6 +104,7 @@ UTIL_SOURCES =                                                      \
                util/virhash.c util/virhash.h                   \
                util/virhashcode.c util/virhashcode.h           \
                util/virhook.c util/virhook.h                   \
+               util/virhostdev.c util/virhostdev.h             \
                util/viridentity.c util/viridentity.h           \
                util/virinitctl.c util/virinitctl.h             \
                util/viriptables.c util/viriptables.h           \
index 37f3fac1c1b8d6060c76e259b7e832066e3178cf..80070c5ea2edf2190c64086e35b3a90655c4af3d 100644 (file)
@@ -1292,6 +1292,10 @@ virHookInitialize;
 virHookPresent;
 
 
+#util/virhostdev.h
+virHostdevManagerGetDefault;
+
+
 # util/viridentity.h
 virIdentityGetAttr;
 virIdentityGetCurrent;
diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c
new file mode 100644 (file)
index 0000000..cc8ae78
--- /dev/null
@@ -0,0 +1,104 @@
+/* virhostdev.c: hostdev management
+ *
+ * Copyright (C) 2014 SUSE LINUX Products GmbH, Nuernberg, Germany.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Chunyan Liu <cyliu@suse.com>
+ */
+
+#include <config.h>
+
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "virhostdev.h"
+#include "viralloc.h"
+#include "virstring.h"
+#include "virfile.h"
+#include "virerror.h"
+#include "virlog.h"
+#include "virutil.h"
+#include "configmake.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+#define HOSTDEV_STATE_DIR LOCALSTATEDIR "/run/libvirt/hostdevmgr"
+
+static virHostdevManagerPtr hostdevMgr;
+
+static void
+virHostdevManagerCleanup(void)
+{
+    if (!hostdevMgr)
+        return;
+
+    virObjectUnref(hostdevMgr->activePciHostdevs);
+    virObjectUnref(hostdevMgr->inactivePciHostdevs);
+    virObjectUnref(hostdevMgr->activeUsbHostdevs);
+    virObjectUnref(hostdevMgr->activeScsiHostdevs);
+    VIR_FREE(hostdevMgr->stateDir);
+
+    VIR_FREE(hostdevMgr);
+}
+
+static int
+virHostdevOnceInit(void)
+{
+    if (VIR_ALLOC(hostdevMgr) < 0)
+        goto error;
+
+    if ((hostdevMgr->activePciHostdevs = virPCIDeviceListNew()) == NULL)
+        goto error;
+
+    if ((hostdevMgr->activeUsbHostdevs = virUSBDeviceListNew()) == NULL)
+        goto error;
+
+    if ((hostdevMgr->inactivePciHostdevs = virPCIDeviceListNew()) == NULL)
+        goto error;
+
+    if ((hostdevMgr->activeScsiHostdevs = virSCSIDeviceListNew()) == NULL)
+        goto error;
+
+    if (VIR_STRDUP(hostdevMgr->stateDir, HOSTDEV_STATE_DIR) < 0)
+        goto error;
+
+    if (virFileMakePath(hostdevMgr->stateDir) < 0) {
+        virReportError(VIR_ERR_OPERATION_FAILED,
+                       _("Failed to create state dir '%s'"),
+                       hostdevMgr->stateDir);
+        goto error;
+    }
+
+    return 0;
+
+error:
+    virHostdevManagerCleanup();
+    return -1;
+}
+
+VIR_ONCE_GLOBAL_INIT(virHostdev)
+
+virHostdevManagerPtr
+virHostdevManagerGetDefault(void)
+{
+    if (virHostdevInitialize() < 0)
+        return NULL;
+    return hostdevMgr;
+}
diff --git a/src/util/virhostdev.h b/src/util/virhostdev.h
new file mode 100644 (file)
index 0000000..d4b1b82
--- /dev/null
@@ -0,0 +1,44 @@
+/* virhostdev.h: hostdev management
+ *
+ * Copyright (C) 2014 SUSE LINUX Products GmbH, Nuernberg, Germany.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Chunyan Liu <cyliu@suse.com>
+ */
+
+#ifndef __VIR_HOSTDEV_H__
+# define __VIR_HOSTDEV_H__
+
+# include "internal.h"
+
+# include "virpci.h"
+# include "virusb.h"
+# include "virscsi.h"
+
+typedef struct _virHostdevManager virHostdevManager;
+typedef virHostdevManager *virHostdevManagerPtr;
+struct _virHostdevManager {
+    char *stateDir;
+
+    virPCIDeviceListPtr activePciHostdevs;
+    virPCIDeviceListPtr inactivePciHostdevs;
+    virUSBDeviceListPtr activeUsbHostdevs;
+    virSCSIDeviceListPtr activeScsiHostdevs;
+};
+
+virHostdevManagerPtr virHostdevManagerGetDefault(void);
+
+#endif /* __VIR_HOSTDEV_H__ */