libvirt/src/util/virhook.c

322 lines
8.4 KiB
C
Raw Normal View History

/*
2012-12-12 17:00:34 +00:00
* virhook.c: implementation of the synchronous hooks support
*
* Copyright (C) 2010-2014 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/>.
*/
#include <config.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include "virerror.h"
2012-12-12 17:00:34 +00:00
#include "virhook.h"
2012-12-13 17:44:57 +00:00
#include "virutil.h"
2012-12-12 17:59:27 +00:00
#include "virlog.h"
2012-12-12 18:06:53 +00:00
#include "viralloc.h"
#include "virfile.h"
maint: use gnulib configmake rather than open-coding things * bootstrap.conf (gnulib_modules): Add configmake. * daemon/Makefile.am (libvirtd_CFLAGS): Drop defines provided by gnulib. * src/Makefile.am (INCLUDES): Likewise. * tests/Makefile.am (INCLUDES): Likewise. * tools/Makefile.am (virsh_CFLAGS): Likewise. * daemon/libvirtd.c (qemudInitPaths, usage, main): Update clients. * src/cpu/cpu_map.c (CPUMAPFILE): Likewise. * src/driver.c (DEFAULT_DRIVER_DIR): Likewise. * src/internal.h (_): Likewise. * src/libvirt.c (virInitialize): Likewise. * src/lxc/lxc_conf.h (LXC_CONFIG_DIR, LXC_STATE_DIR, LXC_LOG_DIR): Likewise. * src/lxc/lxc_conf.c (lxcCapsInit, lxcLoadDriverConfig): Likewise. * src/network/bridge_driver.c (NETWORK_PID_DIR) (NETWORK_STATE_DIR, DNSMASQ_STATE_DIR, networkStartup): Likewise. * src/nwfilter/nwfilter_driver.c (nwfilterDriverStartup): Likewise. * src/qemu/qemu_conf.c (qemudLoadDriverConfig): Likewise. * src/qemu/qemu_driver.c (qemudStartup): Likewise. * src/remote/remote_driver.h (LIBVIRTD_PRIV_UNIX_SOCKET) (LIBVIRTD_PRIV_UNIX_SOCKET_RO, LIBVIRTD_CONFIGURATION_FILE) (LIBVIRT_PKI_DIR): Likewise. * src/secret/secret_driver.c (secretDriverStartup): Likewise. * src/security/security_apparmor.c (VIRT_AA_HELPER): Likewise. * src/security/virt-aa-helper.c (main): Likewise. * src/storage/storage_backend_disk.c (PARTHELPER): Likewise. * src/storage/storage_driver.c (storageDriverStartup): Likewise. * src/uml/uml_driver.c (TEMPDIR, umlStartup): Likewise. * src/util/hooks.c (LIBVIRT_HOOK_DIR): Likewise. * tools/virsh.c (main): Likewise. * docs/hooks.html.in: Likewise.
2010-11-16 14:54:17 +00:00
#include "configmake.h"
#include "vircommand.h"
#define VIR_FROM_THIS VIR_FROM_HOOK
VIR_LOG_INIT("util.hook");
maint: use gnulib configmake rather than open-coding things * bootstrap.conf (gnulib_modules): Add configmake. * daemon/Makefile.am (libvirtd_CFLAGS): Drop defines provided by gnulib. * src/Makefile.am (INCLUDES): Likewise. * tests/Makefile.am (INCLUDES): Likewise. * tools/Makefile.am (virsh_CFLAGS): Likewise. * daemon/libvirtd.c (qemudInitPaths, usage, main): Update clients. * src/cpu/cpu_map.c (CPUMAPFILE): Likewise. * src/driver.c (DEFAULT_DRIVER_DIR): Likewise. * src/internal.h (_): Likewise. * src/libvirt.c (virInitialize): Likewise. * src/lxc/lxc_conf.h (LXC_CONFIG_DIR, LXC_STATE_DIR, LXC_LOG_DIR): Likewise. * src/lxc/lxc_conf.c (lxcCapsInit, lxcLoadDriverConfig): Likewise. * src/network/bridge_driver.c (NETWORK_PID_DIR) (NETWORK_STATE_DIR, DNSMASQ_STATE_DIR, networkStartup): Likewise. * src/nwfilter/nwfilter_driver.c (nwfilterDriverStartup): Likewise. * src/qemu/qemu_conf.c (qemudLoadDriverConfig): Likewise. * src/qemu/qemu_driver.c (qemudStartup): Likewise. * src/remote/remote_driver.h (LIBVIRTD_PRIV_UNIX_SOCKET) (LIBVIRTD_PRIV_UNIX_SOCKET_RO, LIBVIRTD_CONFIGURATION_FILE) (LIBVIRT_PKI_DIR): Likewise. * src/secret/secret_driver.c (secretDriverStartup): Likewise. * src/security/security_apparmor.c (VIRT_AA_HELPER): Likewise. * src/security/virt-aa-helper.c (main): Likewise. * src/storage/storage_backend_disk.c (PARTHELPER): Likewise. * src/storage/storage_driver.c (storageDriverStartup): Likewise. * src/uml/uml_driver.c (TEMPDIR, umlStartup): Likewise. * src/util/hooks.c (LIBVIRT_HOOK_DIR): Likewise. * tools/virsh.c (main): Likewise. * docs/hooks.html.in: Likewise.
2010-11-16 14:54:17 +00:00
#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_DECL(virHookNetworkOp);
VIR_ENUM_DECL(virHookLibxlOp);
VIR_ENUM_IMPL(virHookDriver,
VIR_HOOK_DRIVER_LAST,
"daemon",
"qemu",
"lxc",
"network",
"libxl",
);
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",
"restore",
);
VIR_ENUM_IMPL(virHookLxcOp, VIR_HOOK_LXC_OP_LAST,
"start",
"stopped",
"prepare",
"release",
"started",
"reconnect",
);
VIR_ENUM_IMPL(virHookNetworkOp, VIR_HOOK_NETWORK_OP_LAST,
"start",
"started",
"stopped",
"plugged",
"unplugged",
"updated",
);
VIR_ENUM_IMPL(virHookLibxlOp, VIR_HOOK_LIBXL_OP_LAST,
"start",
"stopped",
"prepare",
"release",
"migrate",
"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)
{
VIR_AUTOFREE(char *) path = NULL;
if (driver == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Invalid hook name for #%d"), no);
return -1;
}
if (virBuildPath(&path, LIBVIRT_HOOK_DIR, driver) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to build path for %s hook"),
driver);
return -1;
}
if (!virFileExists(path)) {
VIR_DEBUG("No hook script %s", path);
return 0;
}
if (!virFileIsExecutable(path)) {
VIR_WARN("Non-executable hook script %s", path);
return 0;
}
VIR_DEBUG("Found hook script %s", path);
return 1;
}
/*
* 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)
{
size_t i;
int 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;
VIR_AUTOFREE(char *) path = NULL;
VIR_AUTOPTR(virCommand) cmd = NULL;
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;
case VIR_HOOK_DRIVER_LIBXL:
opstr = virHookLibxlOpTypeToString(op);
break;
case VIR_HOOK_DRIVER_NETWORK:
opstr = virHookNetworkOpTypeToString(op);
}
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 = "-";
if (virBuildPath(&path, LIBVIRT_HOOK_DIR, drvstr) < 0) {
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. */
virReportError(VIR_ERR_HOOK_SCRIPT_FAILED, "%s",
virGetLastErrorMessage());
}
return ret;
}