[libvirt] [PATCH 10/28] Rename hooks.{c,h} to virhooks.{c,h}

Daniel P. Berrange berrange at redhat.com
Mon Dec 17 14:57:43 UTC 2012


From: "Daniel P. Berrange" <berrange at redhat.com>

---
 daemon/libvirtd.c         |   2 +-
 po/POTFILES.in            |   2 +-
 src/Makefile.am           |   2 +-
 src/lxc/lxc_driver.c      |   2 +-
 src/lxc/lxc_process.c     |   2 +-
 src/qemu/qemu_driver.c    |   2 +-
 src/qemu/qemu_migration.c |   2 +-
 src/qemu/qemu_process.c   |   2 +-
 src/util/hooks.c          | 293 ----------------------------------------------
 src/util/hooks.h          |  85 --------------
 src/util/virhooks.c       | 293 ++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virhooks.h       |  85 ++++++++++++++
 12 files changed, 386 insertions(+), 386 deletions(-)
 delete mode 100644 src/util/hooks.c
 delete mode 100644 src/util/hooks.h
 create mode 100644 src/util/virhooks.c
 create mode 100644 src/util/virhooks.h

diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index 9b0e45a..fbf23d8 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -53,7 +53,7 @@
 #include "threads.h"
 #include "remote.h"
 #include "remote_driver.h"
-#include "hooks.h"
+#include "virhooks.h"
 #include "uuid.h"
 #include "viraudit.h"
 #include "locking/lock_manager.h"
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 818bb4e..18d96c8 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -137,7 +137,6 @@ src/storage/storage_driver.c
 src/test/test_driver.c
 src/uml/uml_conf.c
 src/uml/uml_driver.c
-src/util/hooks.c
 src/util/hostusb.c
 src/util/iohelper.c
 src/util/iptables.c
@@ -160,6 +159,7 @@ src/util/virdnsmasq.c
 src/util/vireventpoll.c
 src/util/virfile.c
 src/util/virhash.c
+src/util/virhooks.c
 src/util/virinitctl.c
 src/util/virkeyfile.c
 src/util/virlockspace.c
diff --git a/src/Makefile.am b/src/Makefile.am
index f803de0..07ace32 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -53,7 +53,6 @@ augeastest_DATA =
 # These files are not related to driver APIs. Simply generic
 # helper APIs for various purposes
 UTIL_SOURCES =							\
-		util/hooks.c util/hooks.h			\
 		util/iptables.c util/iptables.h			\
 		util/json.c util/json.h				\
 		util/logging.c util/logging.h			\
@@ -84,6 +83,7 @@ UTIL_SOURCES =							\
 		util/virevent.c util/virevent.h			\
 		util/vireventpoll.c util/vireventpoll.h		\
 		util/virfile.c util/virfile.h			\
+		util/virhooks.c util/virhooks.h			\
 		util/virnodesuspend.c util/virnodesuspend.h	\
 		util/virobject.c util/virobject.h		\
 		util/virpidfile.c util/virpidfile.h		\
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index e0e76e6..9c35406 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -50,7 +50,7 @@
 #include "nodeinfo.h"
 #include "uuid.h"
 #include "stats_linux.h"
-#include "hooks.h"
+#include "virhooks.h"
 #include "virfile.h"
 #include "virpidfile.h"
 #include "fdstream.h"
diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index 7f66dc7..d59bd2e 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -43,7 +43,7 @@
 #include "virterror_internal.h"
 #include "logging.h"
 #include "vircommand.h"
-#include "hooks.h"
+#include "virhooks.h"
 
 #define VIR_FROM_THIS VIR_FROM_LXC
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 0ebbf7d..0733f4e 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -80,7 +80,7 @@
 #include "cpu/cpu.h"
 #include "sysinfo.h"
 #include "domain_nwfilter.h"
-#include "hooks.h"
+#include "virhooks.h"
 #include "storage_file.h"
 #include "virfile.h"
 #include "fdstream.h"
diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c
index 5dcbb07..468a92f 100644
--- a/src/qemu/qemu_migration.c
+++ b/src/qemu/qemu_migration.c
@@ -49,7 +49,7 @@
 #include "rpc/virnetsocket.h"
 #include "storage_file.h"
 #include "viruri.h"
-#include "hooks.h"
+#include "virhooks.h"
 
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 969cbc7..e528d78 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -54,7 +54,7 @@
 #include "logging.h"
 #include "virterror_internal.h"
 #include "memory.h"
-#include "hooks.h"
+#include "virhooks.h"
 #include "virfile.h"
 #include "virpidfile.h"
 #include "util.h"
diff --git a/src/util/hooks.c b/src/util/hooks.c
deleted file mode 100644
index a6c056d..0000000
--- a/src/util/hooks.c
+++ /dev/null
@@ -1,293 +0,0 @@
-/*
- * hooks.c: implementation of the synchronous hooks support
- *
- * Copyright (C) 2010-2012 Red Hat, Inc.
- * Copyright (C) 2010 Daniel Veillard
- *
- * 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: Daniel Veillard <veillard at redhat.com>
- */
-
-#include <config.h>
-
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-#include "virterror_internal.h"
-#include "hooks.h"
-#include "util.h"
-#include "logging.h"
-#include "memory.h"
-#include "virfile.h"
-#include "configmake.h"
-#include "vircommand.h"
-
-#define VIR_FROM_THIS VIR_FROM_HOOK
-
-#define LIBVIRT_HOOK_DIR SYSCONFDIR "/libvirt/hooks"
-
-VIR_ENUM_DECL(virHookDriver)
-VIR_ENUM_DECL(virHookDaemonOp)
-VIR_ENUM_DECL(virHookSubop)
-VIR_ENUM_DECL(virHookQemuOp)
-VIR_ENUM_DECL(virHookLxcOp)
-
-VIR_ENUM_IMPL(virHookDriver,
-              VIR_HOOK_DRIVER_LAST,
-              "daemon",
-              "qemu",
-              "lxc")
-
-VIR_ENUM_IMPL(virHookDaemonOp, VIR_HOOK_DAEMON_OP_LAST,
-              "start",
-              "shutdown",
-              "reload")
-
-VIR_ENUM_IMPL(virHookSubop, VIR_HOOK_SUBOP_LAST,
-              "-",
-              "begin",
-              "end")
-
-VIR_ENUM_IMPL(virHookQemuOp, VIR_HOOK_QEMU_OP_LAST,
-              "start",
-              "stopped",
-              "prepare",
-              "release",
-              "migrate",
-              "started",
-              "reconnect",
-              "attach")
-
-VIR_ENUM_IMPL(virHookLxcOp, VIR_HOOK_LXC_OP_LAST,
-              "start",
-              "stopped",
-              "prepare",
-              "release",
-              "started",
-              "reconnect")
-
-static int virHooksFound = -1;
-
-/**
- * virHookCheck:
- * @driver: the driver name "daemon", "qemu", "lxc"...
- *
- * Check is there is an installed hook for the given driver, if this
- * is the case register it. Then subsequent calls to virHookCall
- * will call the hook if found.
- *
- * Returns 1 if found, 0 if not found, and -1 in case of error
- */
-static int
-virHookCheck(int no, const char *driver) {
-    char *path;
-    int ret;
-
-    if (driver == NULL) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Invalid hook name for #%d"), no);
-        return -1;
-    }
-
-    ret = virBuildPath(&path, LIBVIRT_HOOK_DIR, driver);
-    if ((ret < 0) || (path == NULL)) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Failed to build path for %s hook"),
-                       driver);
-        return -1;
-    }
-
-    if (!virFileExists(path)) {
-        ret = 0;
-        VIR_DEBUG("No hook script %s", path);
-    } else if (!virFileIsExecutable(path)) {
-        ret = 0;
-        VIR_WARN("Non-executable hook script %s", path);
-    } else {
-        ret = 1;
-        VIR_DEBUG("Found hook script %s", path);
-    }
-
-    VIR_FREE(path);
-    return ret;
-}
-
-/*
- * virHookInitialize:
- *
- * Initialize synchronous hooks support.
- * Check is there is an installed hook for all the drivers
- *
- * Returns the number of hooks found or -1 in case of failure
- */
-int
-virHookInitialize(void) {
-    int i, res, ret = 0;
-
-    virHooksFound = 0;
-    for (i = 0;i < VIR_HOOK_DRIVER_LAST;i++) {
-        res = virHookCheck(i, virHookDriverTypeToString(i));
-        if (res < 0)
-            return -1;
-
-        if (res == 1) {
-            virHooksFound |= (1 << i);
-            ret++;
-        }
-    }
-    return ret;
-}
-
-/**
- * virHookPresent:
- * @driver: the driver number (from virHookDriver enum)
- *
- * Check if a hook exists for the given driver, this is needed
- * to avoid unnecessary work if the hook is not present
- *
- * Returns 1 if present, 0 otherwise
- */
-int
-virHookPresent(int driver) {
-    if ((driver < VIR_HOOK_DRIVER_DAEMON) ||
-        (driver >= VIR_HOOK_DRIVER_LAST))
-        return 0;
-    if (virHooksFound == -1)
-        return 0;
-
-    if ((virHooksFound & (1 << driver)) == 0)
-        return 0;
-    return 1;
-}
-
-/**
- * virHookCall:
- * @driver: the driver number (from virHookDriver enum)
- * @id: an id for the object '-' if non available for example on daemon hooks
- * @op: the operation on the id e.g. VIR_HOOK_QEMU_OP_START
- * @sub_op: a sub_operation, currently unused
- * @extra: optional string information
- * @input: extra input given to the script on stdin
- * @output: optional address of variable to store malloced result buffer
- *
- * Implement a hook call, where the external script for the driver is
- * called with the given information. This is a synchronous call, we wait for
- * execution completion. If @output is non-NULL, *output is guaranteed to be
- * allocated after successful virHookCall, and is best-effort allocated after
- * failed virHookCall; the caller is responsible for freeing *output.
- *
- * Returns: 0 if the execution succeeded, 1 if the script was not found or
- *          invalid parameters, and -1 if script returned an error
- */
-int
-virHookCall(int driver,
-            const char *id,
-            int op,
-            int sub_op,
-            const char *extra,
-            const char *input,
-            char **output)
-{
-    int ret;
-    char *path;
-    virCommandPtr cmd;
-    const char *drvstr;
-    const char *opstr;
-    const char *subopstr;
-
-    if (output)
-        *output = NULL;
-
-    if ((driver < VIR_HOOK_DRIVER_DAEMON) ||
-        (driver >= VIR_HOOK_DRIVER_LAST))
-        return 1;
-
-    /*
-     * We cache the availability of the script to minimize impact at
-     * runtime if no script is defined, this is being reset on SIGHUP
-     */
-    if ((virHooksFound == -1) ||
-        ((driver == VIR_HOOK_DRIVER_DAEMON) &&
-         (op == VIR_HOOK_DAEMON_OP_RELOAD ||
-         op == VIR_HOOK_DAEMON_OP_SHUTDOWN)))
-        virHookInitialize();
-
-    if ((virHooksFound & (1 << driver)) == 0)
-        return 1;
-
-    drvstr = virHookDriverTypeToString(driver);
-
-    opstr = NULL;
-    switch (driver) {
-        case VIR_HOOK_DRIVER_DAEMON:
-            opstr = virHookDaemonOpTypeToString(op);
-            break;
-        case VIR_HOOK_DRIVER_QEMU:
-            opstr = virHookQemuOpTypeToString(op);
-            break;
-        case VIR_HOOK_DRIVER_LXC:
-            opstr = virHookLxcOpTypeToString(op);
-            break;
-    }
-    if (opstr == NULL) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Hook for %s, failed to find operation #%d"),
-                       drvstr, op);
-        return 1;
-    }
-    subopstr = virHookSubopTypeToString(sub_op);
-    if (subopstr == NULL)
-        subopstr = "-";
-    if (extra == NULL)
-        extra = "-";
-
-    ret = virBuildPath(&path, LIBVIRT_HOOK_DIR, drvstr);
-    if ((ret < 0) || (path == NULL)) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Failed to build path for %s hook"),
-                       drvstr);
-        return -1;
-    }
-
-    VIR_DEBUG("Calling hook opstr=%s subopstr=%s extra=%s",
-              opstr, subopstr, extra);
-
-    cmd = virCommandNewArgList(path, id, opstr, subopstr, extra, NULL);
-
-    virCommandAddEnvPassCommon(cmd);
-
-    if (input)
-        virCommandSetInputBuffer(cmd, input);
-    if (output)
-        virCommandSetOutputBuffer(cmd, output);
-
-    ret = virCommandRun(cmd, NULL);
-    if (ret < 0) {
-        /* Convert INTERNAL_ERROR into known error.  */
-        virErrorPtr err = virGetLastError();
-        virReportError(VIR_ERR_HOOK_SCRIPT_FAILED, "%s", err->message);
-    }
-
-    virCommandFree(cmd);
-
-    VIR_FREE(path);
-
-    return ret;
-}
diff --git a/src/util/hooks.h b/src/util/hooks.h
deleted file mode 100644
index 0ca376f..0000000
--- a/src/util/hooks.h
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * hook.h: internal entry points needed for synchronous hooks support
- *
- * Copyright (C) 2010 Red Hat, Inc.
- * Copyright (C) 2010 Daniel Veillard
- *
- * 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: Daniel Veillard <veillard at redhat.com>
- */
-
-#ifndef __VIR_HOOKS_H__
-# define __VIR_HOOKS_H__
-
-# include "internal.h"
-# include "util.h"
-
-enum virHookDriverType {
-    VIR_HOOK_DRIVER_DAEMON = 0,        /* Daemon related events */
-    VIR_HOOK_DRIVER_QEMU,              /* QEmu domains related events */
-    VIR_HOOK_DRIVER_LXC,               /* LXC domains related events */
-
-    VIR_HOOK_DRIVER_LAST,
-};
-
-enum virHookDaemonOpType {
-    VIR_HOOK_DAEMON_OP_START,          /* daemon is about to start */
-    VIR_HOOK_DAEMON_OP_SHUTDOWN,       /* daemon is about to shutdown */
-    VIR_HOOK_DAEMON_OP_RELOAD,         /* driver reload with SIGHUP */
-
-    VIR_HOOK_DAEMON_OP_LAST,
-};
-
-enum virHookSubopType {
-    VIR_HOOK_SUBOP_NONE,               /* no sub-operation */
-    VIR_HOOK_SUBOP_BEGIN,              /* beginning of the operation */
-    VIR_HOOK_SUBOP_END,                /* end of the operation */
-
-    VIR_HOOK_SUBOP_LAST,
-};
-
-enum virHookQemuOpType {
-    VIR_HOOK_QEMU_OP_START,            /* domain is about to start */
-    VIR_HOOK_QEMU_OP_STOPPED,          /* domain has stopped */
-    VIR_HOOK_QEMU_OP_PREPARE,          /* domain startup initiated */
-    VIR_HOOK_QEMU_OP_RELEASE,          /* domain destruction is over */
-    VIR_HOOK_QEMU_OP_MIGRATE,          /* domain is being migrated */
-    VIR_HOOK_QEMU_OP_STARTED,          /* domain has started */
-    VIR_HOOK_QEMU_OP_RECONNECT,        /* domain is being reconnected by libvirt */
-    VIR_HOOK_QEMU_OP_ATTACH,           /* domain is being attached to be libvirt */
-
-    VIR_HOOK_QEMU_OP_LAST,
-};
-
-enum virHookLxcOpType {
-    VIR_HOOK_LXC_OP_START,            /* domain is about to start */
-    VIR_HOOK_LXC_OP_STOPPED,          /* domain has stopped */
-    VIR_HOOK_LXC_OP_PREPARE,          /* domain startup initiated */
-    VIR_HOOK_LXC_OP_RELEASE,          /* domain destruction is over */
-    VIR_HOOK_LXC_OP_STARTED,          /* domain has started */
-    VIR_HOOK_LXC_OP_RECONNECT,        /* domain is being reconnected by libvirt */
-
-    VIR_HOOK_LXC_OP_LAST,
-};
-
-int virHookInitialize(void);
-
-int virHookPresent(int driver);
-
-int virHookCall(int driver, const char *id, int op, int sub_op,
-                const char *extra, const char *input, char **output);
-
-#endif /* __VIR_HOOKS_H__ */
diff --git a/src/util/virhooks.c b/src/util/virhooks.c
new file mode 100644
index 0000000..4a732ff
--- /dev/null
+++ b/src/util/virhooks.c
@@ -0,0 +1,293 @@
+/*
+ * hooks.c: implementation of the synchronous hooks support
+ *
+ * Copyright (C) 2010-2012 Red Hat, Inc.
+ * Copyright (C) 2010 Daniel Veillard
+ *
+ * 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: Daniel Veillard <veillard at redhat.com>
+ */
+
+#include <config.h>
+
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "virterror_internal.h"
+#include "virhooks.h"
+#include "util.h"
+#include "logging.h"
+#include "memory.h"
+#include "virfile.h"
+#include "configmake.h"
+#include "vircommand.h"
+
+#define VIR_FROM_THIS VIR_FROM_HOOK
+
+#define LIBVIRT_HOOK_DIR SYSCONFDIR "/libvirt/hooks"
+
+VIR_ENUM_DECL(virHookDriver)
+VIR_ENUM_DECL(virHookDaemonOp)
+VIR_ENUM_DECL(virHookSubop)
+VIR_ENUM_DECL(virHookQemuOp)
+VIR_ENUM_DECL(virHookLxcOp)
+
+VIR_ENUM_IMPL(virHookDriver,
+              VIR_HOOK_DRIVER_LAST,
+              "daemon",
+              "qemu",
+              "lxc")
+
+VIR_ENUM_IMPL(virHookDaemonOp, VIR_HOOK_DAEMON_OP_LAST,
+              "start",
+              "shutdown",
+              "reload")
+
+VIR_ENUM_IMPL(virHookSubop, VIR_HOOK_SUBOP_LAST,
+              "-",
+              "begin",
+              "end")
+
+VIR_ENUM_IMPL(virHookQemuOp, VIR_HOOK_QEMU_OP_LAST,
+              "start",
+              "stopped",
+              "prepare",
+              "release",
+              "migrate",
+              "started",
+              "reconnect",
+              "attach")
+
+VIR_ENUM_IMPL(virHookLxcOp, VIR_HOOK_LXC_OP_LAST,
+              "start",
+              "stopped",
+              "prepare",
+              "release",
+              "started",
+              "reconnect")
+
+static int virHooksFound = -1;
+
+/**
+ * virHookCheck:
+ * @driver: the driver name "daemon", "qemu", "lxc"...
+ *
+ * Check is there is an installed hook for the given driver, if this
+ * is the case register it. Then subsequent calls to virHookCall
+ * will call the hook if found.
+ *
+ * Returns 1 if found, 0 if not found, and -1 in case of error
+ */
+static int
+virHookCheck(int no, const char *driver) {
+    char *path;
+    int ret;
+
+    if (driver == NULL) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Invalid hook name for #%d"), no);
+        return -1;
+    }
+
+    ret = virBuildPath(&path, LIBVIRT_HOOK_DIR, driver);
+    if ((ret < 0) || (path == NULL)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Failed to build path for %s hook"),
+                       driver);
+        return -1;
+    }
+
+    if (!virFileExists(path)) {
+        ret = 0;
+        VIR_DEBUG("No hook script %s", path);
+    } else if (!virFileIsExecutable(path)) {
+        ret = 0;
+        VIR_WARN("Non-executable hook script %s", path);
+    } else {
+        ret = 1;
+        VIR_DEBUG("Found hook script %s", path);
+    }
+
+    VIR_FREE(path);
+    return ret;
+}
+
+/*
+ * virHookInitialize:
+ *
+ * Initialize synchronous hooks support.
+ * Check is there is an installed hook for all the drivers
+ *
+ * Returns the number of hooks found or -1 in case of failure
+ */
+int
+virHookInitialize(void) {
+    int i, res, ret = 0;
+
+    virHooksFound = 0;
+    for (i = 0;i < VIR_HOOK_DRIVER_LAST;i++) {
+        res = virHookCheck(i, virHookDriverTypeToString(i));
+        if (res < 0)
+            return -1;
+
+        if (res == 1) {
+            virHooksFound |= (1 << i);
+            ret++;
+        }
+    }
+    return ret;
+}
+
+/**
+ * virHookPresent:
+ * @driver: the driver number (from virHookDriver enum)
+ *
+ * Check if a hook exists for the given driver, this is needed
+ * to avoid unnecessary work if the hook is not present
+ *
+ * Returns 1 if present, 0 otherwise
+ */
+int
+virHookPresent(int driver) {
+    if ((driver < VIR_HOOK_DRIVER_DAEMON) ||
+        (driver >= VIR_HOOK_DRIVER_LAST))
+        return 0;
+    if (virHooksFound == -1)
+        return 0;
+
+    if ((virHooksFound & (1 << driver)) == 0)
+        return 0;
+    return 1;
+}
+
+/**
+ * virHookCall:
+ * @driver: the driver number (from virHookDriver enum)
+ * @id: an id for the object '-' if non available for example on daemon hooks
+ * @op: the operation on the id e.g. VIR_HOOK_QEMU_OP_START
+ * @sub_op: a sub_operation, currently unused
+ * @extra: optional string information
+ * @input: extra input given to the script on stdin
+ * @output: optional address of variable to store malloced result buffer
+ *
+ * Implement a hook call, where the external script for the driver is
+ * called with the given information. This is a synchronous call, we wait for
+ * execution completion. If @output is non-NULL, *output is guaranteed to be
+ * allocated after successful virHookCall, and is best-effort allocated after
+ * failed virHookCall; the caller is responsible for freeing *output.
+ *
+ * Returns: 0 if the execution succeeded, 1 if the script was not found or
+ *          invalid parameters, and -1 if script returned an error
+ */
+int
+virHookCall(int driver,
+            const char *id,
+            int op,
+            int sub_op,
+            const char *extra,
+            const char *input,
+            char **output)
+{
+    int ret;
+    char *path;
+    virCommandPtr cmd;
+    const char *drvstr;
+    const char *opstr;
+    const char *subopstr;
+
+    if (output)
+        *output = NULL;
+
+    if ((driver < VIR_HOOK_DRIVER_DAEMON) ||
+        (driver >= VIR_HOOK_DRIVER_LAST))
+        return 1;
+
+    /*
+     * We cache the availability of the script to minimize impact at
+     * runtime if no script is defined, this is being reset on SIGHUP
+     */
+    if ((virHooksFound == -1) ||
+        ((driver == VIR_HOOK_DRIVER_DAEMON) &&
+         (op == VIR_HOOK_DAEMON_OP_RELOAD ||
+         op == VIR_HOOK_DAEMON_OP_SHUTDOWN)))
+        virHookInitialize();
+
+    if ((virHooksFound & (1 << driver)) == 0)
+        return 1;
+
+    drvstr = virHookDriverTypeToString(driver);
+
+    opstr = NULL;
+    switch (driver) {
+        case VIR_HOOK_DRIVER_DAEMON:
+            opstr = virHookDaemonOpTypeToString(op);
+            break;
+        case VIR_HOOK_DRIVER_QEMU:
+            opstr = virHookQemuOpTypeToString(op);
+            break;
+        case VIR_HOOK_DRIVER_LXC:
+            opstr = virHookLxcOpTypeToString(op);
+            break;
+    }
+    if (opstr == NULL) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Hook for %s, failed to find operation #%d"),
+                       drvstr, op);
+        return 1;
+    }
+    subopstr = virHookSubopTypeToString(sub_op);
+    if (subopstr == NULL)
+        subopstr = "-";
+    if (extra == NULL)
+        extra = "-";
+
+    ret = virBuildPath(&path, LIBVIRT_HOOK_DIR, drvstr);
+    if ((ret < 0) || (path == NULL)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Failed to build path for %s hook"),
+                       drvstr);
+        return -1;
+    }
+
+    VIR_DEBUG("Calling hook opstr=%s subopstr=%s extra=%s",
+              opstr, subopstr, extra);
+
+    cmd = virCommandNewArgList(path, id, opstr, subopstr, extra, NULL);
+
+    virCommandAddEnvPassCommon(cmd);
+
+    if (input)
+        virCommandSetInputBuffer(cmd, input);
+    if (output)
+        virCommandSetOutputBuffer(cmd, output);
+
+    ret = virCommandRun(cmd, NULL);
+    if (ret < 0) {
+        /* Convert INTERNAL_ERROR into known error.  */
+        virErrorPtr err = virGetLastError();
+        virReportError(VIR_ERR_HOOK_SCRIPT_FAILED, "%s", err->message);
+    }
+
+    virCommandFree(cmd);
+
+    VIR_FREE(path);
+
+    return ret;
+}
diff --git a/src/util/virhooks.h b/src/util/virhooks.h
new file mode 100644
index 0000000..0ca376f
--- /dev/null
+++ b/src/util/virhooks.h
@@ -0,0 +1,85 @@
+/*
+ * hook.h: internal entry points needed for synchronous hooks support
+ *
+ * Copyright (C) 2010 Red Hat, Inc.
+ * Copyright (C) 2010 Daniel Veillard
+ *
+ * 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: Daniel Veillard <veillard at redhat.com>
+ */
+
+#ifndef __VIR_HOOKS_H__
+# define __VIR_HOOKS_H__
+
+# include "internal.h"
+# include "util.h"
+
+enum virHookDriverType {
+    VIR_HOOK_DRIVER_DAEMON = 0,        /* Daemon related events */
+    VIR_HOOK_DRIVER_QEMU,              /* QEmu domains related events */
+    VIR_HOOK_DRIVER_LXC,               /* LXC domains related events */
+
+    VIR_HOOK_DRIVER_LAST,
+};
+
+enum virHookDaemonOpType {
+    VIR_HOOK_DAEMON_OP_START,          /* daemon is about to start */
+    VIR_HOOK_DAEMON_OP_SHUTDOWN,       /* daemon is about to shutdown */
+    VIR_HOOK_DAEMON_OP_RELOAD,         /* driver reload with SIGHUP */
+
+    VIR_HOOK_DAEMON_OP_LAST,
+};
+
+enum virHookSubopType {
+    VIR_HOOK_SUBOP_NONE,               /* no sub-operation */
+    VIR_HOOK_SUBOP_BEGIN,              /* beginning of the operation */
+    VIR_HOOK_SUBOP_END,                /* end of the operation */
+
+    VIR_HOOK_SUBOP_LAST,
+};
+
+enum virHookQemuOpType {
+    VIR_HOOK_QEMU_OP_START,            /* domain is about to start */
+    VIR_HOOK_QEMU_OP_STOPPED,          /* domain has stopped */
+    VIR_HOOK_QEMU_OP_PREPARE,          /* domain startup initiated */
+    VIR_HOOK_QEMU_OP_RELEASE,          /* domain destruction is over */
+    VIR_HOOK_QEMU_OP_MIGRATE,          /* domain is being migrated */
+    VIR_HOOK_QEMU_OP_STARTED,          /* domain has started */
+    VIR_HOOK_QEMU_OP_RECONNECT,        /* domain is being reconnected by libvirt */
+    VIR_HOOK_QEMU_OP_ATTACH,           /* domain is being attached to be libvirt */
+
+    VIR_HOOK_QEMU_OP_LAST,
+};
+
+enum virHookLxcOpType {
+    VIR_HOOK_LXC_OP_START,            /* domain is about to start */
+    VIR_HOOK_LXC_OP_STOPPED,          /* domain has stopped */
+    VIR_HOOK_LXC_OP_PREPARE,          /* domain startup initiated */
+    VIR_HOOK_LXC_OP_RELEASE,          /* domain destruction is over */
+    VIR_HOOK_LXC_OP_STARTED,          /* domain has started */
+    VIR_HOOK_LXC_OP_RECONNECT,        /* domain is being reconnected by libvirt */
+
+    VIR_HOOK_LXC_OP_LAST,
+};
+
+int virHookInitialize(void);
+
+int virHookPresent(int driver);
+
+int virHookCall(int driver, const char *id, int op, int sub_op,
+                const char *extra, const char *input, char **output);
+
+#endif /* __VIR_HOOKS_H__ */
-- 
1.7.11.7




More information about the libvir-list mailing list