2005-12-13 16:22:05 +00:00
|
|
|
/*
|
|
|
|
* xml.c: XML based interfaces for the libvir library
|
|
|
|
*
|
2008-02-04 19:31:30 +00:00
|
|
|
* Copyright (C) 2005, 2007, 2008 Red Hat, Inc.
|
2005-12-13 16:22:05 +00:00
|
|
|
*
|
|
|
|
* See COPYING.LIB for the License of this software
|
|
|
|
*
|
|
|
|
* Daniel Veillard <veillard@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-01-29 18:15:54 +00:00
|
|
|
#include <config.h>
|
2007-11-26 11:40:28 +00:00
|
|
|
|
2005-12-13 16:22:05 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2007-11-26 11:40:28 +00:00
|
|
|
#include <limits.h>
|
2007-10-24 14:22:25 +00:00
|
|
|
#include <math.h> /* for isnan() */
|
2008-05-16 14:18:38 +00:00
|
|
|
#include "c-ctype.h"
|
2005-12-13 16:22:05 +00:00
|
|
|
#include "internal.h"
|
2006-02-16 22:50:52 +00:00
|
|
|
#include "xml.h"
|
2007-06-26 22:33:22 +00:00
|
|
|
#include "buf.h"
|
2008-02-27 04:35:08 +00:00
|
|
|
#include "util.h"
|
2008-05-29 19:20:22 +00:00
|
|
|
#include "memory.h"
|
2008-05-07 14:04:40 +00:00
|
|
|
#include "xend_internal.h" /* for is_sound_* functions */
|
2005-12-13 16:22:05 +00:00
|
|
|
|
2008-07-25 13:17:27 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
/**
|
|
|
|
* virXMLError:
|
|
|
|
* @conn: a connection if any
|
|
|
|
* @error: the error number
|
|
|
|
* @info: information/format string
|
|
|
|
* @value: extra integer parameter for the error string
|
|
|
|
*
|
|
|
|
* Report an error coming from the XML module.
|
|
|
|
*/
|
2006-02-27 22:32:54 +00:00
|
|
|
static void
|
2007-10-24 14:22:25 +00:00
|
|
|
virXMLError(virConnectPtr conn, virErrorNumber error, const char *info,
|
|
|
|
int value)
|
2006-03-15 12:13:25 +00:00
|
|
|
{
|
2006-02-27 22:32:54 +00:00
|
|
|
const char *errmsg;
|
2006-03-15 12:13:25 +00:00
|
|
|
|
2006-02-27 22:32:54 +00:00
|
|
|
if (error == VIR_ERR_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
errmsg = __virErrorMsg(error, info);
|
2007-02-14 16:16:13 +00:00
|
|
|
__virRaiseError(conn, NULL, NULL, VIR_FROM_XML, error, VIR_ERR_ERROR,
|
2006-03-15 12:13:25 +00:00
|
|
|
errmsg, info, NULL, value, 0, errmsg, info, value);
|
2006-02-27 22:32:54 +00:00
|
|
|
}
|
|
|
|
|
2007-10-22 20:28:55 +00:00
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* *
|
|
|
|
* Wrappers around libxml2 XPath specific functions *
|
|
|
|
* *
|
|
|
|
************************************************************************/
|
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
/**
|
|
|
|
* virXPathString:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath string
|
|
|
|
*
|
|
|
|
* Returns a new string which must be deallocated by the caller or NULL
|
|
|
|
* if the evaluation failed.
|
|
|
|
*/
|
|
|
|
char *
|
2007-10-24 14:22:25 +00:00
|
|
|
virXPathString(const char *xpath, xmlXPathContextPtr ctxt)
|
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL)) {
|
2007-10-24 14:22:25 +00:00
|
|
|
virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-02-04 19:31:30 +00:00
|
|
|
_("Invalid parameter to virXPathString()"), 0);
|
2007-10-24 14:22:25 +00:00
|
|
|
return (NULL);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
|
|
|
if ((obj == NULL) || (obj->type != XPATH_STRING) ||
|
2007-07-31 14:27:12 +00:00
|
|
|
(obj->stringval == NULL) || (obj->stringval[0] == 0)) {
|
2008-02-07 16:49:29 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
2007-10-24 14:22:25 +00:00
|
|
|
return (NULL);
|
2007-07-31 14:27:12 +00:00
|
|
|
}
|
2007-04-06 12:28:24 +00:00
|
|
|
ret = strdup((char *) obj->stringval);
|
|
|
|
xmlXPathFreeObject(obj);
|
|
|
|
if (ret == NULL) {
|
2008-02-04 19:31:30 +00:00
|
|
|
virXMLError(NULL, VIR_ERR_NO_MEMORY, _("strdup failed"), 0);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-09 08:35:09 +00:00
|
|
|
ctxt->node = relnode;
|
2007-10-24 14:22:25 +00:00
|
|
|
return (ret);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virXPathNumber:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned double value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
|
|
|
* or -1 if the evaluation failed.
|
|
|
|
*/
|
|
|
|
int
|
2007-10-24 14:22:25 +00:00
|
|
|
virXPathNumber(const char *xpath, xmlXPathContextPtr ctxt, double *value)
|
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
|
2007-10-24 14:22:25 +00:00
|
|
|
virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-02-04 19:31:30 +00:00
|
|
|
_("Invalid parameter to virXPathNumber()"), 0);
|
2007-10-24 14:22:25 +00:00
|
|
|
return (-1);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
|
|
|
if ((obj == NULL) || (obj->type != XPATH_NUMBER) ||
|
|
|
|
(isnan(obj->floatval))) {
|
2007-10-24 14:22:25 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
2008-07-09 08:35:09 +00:00
|
|
|
ctxt->node = relnode;
|
2007-10-24 14:22:25 +00:00
|
|
|
return (-1);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
*value = obj->floatval;
|
|
|
|
xmlXPathFreeObject(obj);
|
2008-07-09 08:35:09 +00:00
|
|
|
ctxt->node = relnode;
|
2007-10-24 14:22:25 +00:00
|
|
|
return (0);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virXPathLong:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned long value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
2007-04-06 15:34:09 +00:00
|
|
|
* or -1 if the XPath evaluation failed or -2 if the
|
|
|
|
* value doesn't have a long format.
|
2007-04-06 12:28:24 +00:00
|
|
|
*/
|
|
|
|
int
|
2007-10-24 14:22:25 +00:00
|
|
|
virXPathLong(const char *xpath, xmlXPathContextPtr ctxt, long *value)
|
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
|
2007-10-24 14:22:25 +00:00
|
|
|
virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-02-04 19:31:30 +00:00
|
|
|
_("Invalid parameter to virXPathNumber()"), 0);
|
2007-10-24 14:22:25 +00:00
|
|
|
return (-1);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
|
|
|
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
|
|
|
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
|
|
|
char *conv = NULL;
|
2007-10-24 14:22:25 +00:00
|
|
|
long val;
|
2007-04-06 12:28:24 +00:00
|
|
|
|
2007-10-24 14:22:25 +00:00
|
|
|
val = strtol((const char *) obj->stringval, &conv, 10);
|
|
|
|
if (conv == (const char *) obj->stringval) {
|
2007-04-06 12:28:24 +00:00
|
|
|
ret = -2;
|
|
|
|
} else {
|
2007-10-24 14:22:25 +00:00
|
|
|
*value = val;
|
|
|
|
}
|
2007-04-06 12:28:24 +00:00
|
|
|
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
|
|
|
(!(isnan(obj->floatval)))) {
|
2007-10-24 14:22:25 +00:00
|
|
|
*value = (long) obj->floatval;
|
|
|
|
if (*value != obj->floatval) {
|
|
|
|
ret = -2;
|
|
|
|
}
|
2007-04-06 12:28:24 +00:00
|
|
|
} else {
|
2007-10-24 14:22:25 +00:00
|
|
|
ret = -1;
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
2008-07-09 08:35:09 +00:00
|
|
|
ctxt->node = relnode;
|
2007-10-24 14:22:25 +00:00
|
|
|
return (ret);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
|
2008-07-11 10:48:34 +00:00
|
|
|
/**
|
|
|
|
* virXPathULong:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @value: the returned long value
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath number
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success in which case @value is set,
|
|
|
|
* or -1 if the XPath evaluation failed or -2 if the
|
|
|
|
* value doesn't have a long format.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virXPathULong(const char *xpath, xmlXPathContextPtr ctxt, unsigned long *value)
|
|
|
|
{
|
|
|
|
xmlXPathObjectPtr obj;
|
|
|
|
xmlNodePtr relnode;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL) || (value == NULL)) {
|
|
|
|
virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid parameter to virXPathNumber()"), 0);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
relnode = ctxt->node;
|
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
|
|
|
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
|
|
|
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
|
|
|
char *conv = NULL;
|
|
|
|
long val;
|
|
|
|
|
|
|
|
val = strtoul((const char *) obj->stringval, &conv, 10);
|
|
|
|
if (conv == (const char *) obj->stringval) {
|
|
|
|
ret = -2;
|
|
|
|
} else {
|
|
|
|
*value = val;
|
|
|
|
}
|
|
|
|
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
|
|
|
(!(isnan(obj->floatval)))) {
|
|
|
|
*value = (unsigned long) obj->floatval;
|
|
|
|
if (*value != obj->floatval) {
|
|
|
|
ret = -2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlXPathFreeObject(obj);
|
|
|
|
ctxt->node = relnode;
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2008-07-11 16:23:36 +00:00
|
|
|
char *
|
|
|
|
virXMLPropString(xmlNodePtr node,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
return (char *)xmlGetProp(node, BAD_CAST name);
|
|
|
|
}
|
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
/**
|
|
|
|
* virXPathBoolean:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath boolean
|
|
|
|
*
|
|
|
|
* Returns 0 if false, 1 if true, or -1 if the evaluation failed.
|
|
|
|
*/
|
|
|
|
int
|
2007-10-24 14:22:25 +00:00
|
|
|
virXPathBoolean(const char *xpath, xmlXPathContextPtr ctxt)
|
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL)) {
|
2007-10-24 14:22:25 +00:00
|
|
|
virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-02-04 19:31:30 +00:00
|
|
|
_("Invalid parameter to virXPathBoolean()"), 0);
|
2007-10-24 14:22:25 +00:00
|
|
|
return (-1);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
|
|
|
if ((obj == NULL) || (obj->type != XPATH_BOOLEAN) ||
|
|
|
|
(obj->boolval < 0) || (obj->boolval > 1)) {
|
2007-10-24 14:22:25 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
|
|
|
return (-1);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
ret = obj->boolval;
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
2008-07-09 08:35:09 +00:00
|
|
|
ctxt->node = relnode;
|
2007-10-24 14:22:25 +00:00
|
|
|
return (ret);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virXPathNode:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath node set and returning
|
|
|
|
* only one node, the first one in the set if any
|
|
|
|
*
|
|
|
|
* Returns a pointer to the node or NULL if the evaluation failed.
|
|
|
|
*/
|
|
|
|
xmlNodePtr
|
2007-10-24 14:22:25 +00:00
|
|
|
virXPathNode(const char *xpath, xmlXPathContextPtr ctxt)
|
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlNodePtr ret;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL)) {
|
2007-10-24 14:22:25 +00:00
|
|
|
virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-02-04 19:31:30 +00:00
|
|
|
_("Invalid parameter to virXPathNode()"), 0);
|
2007-10-24 14:22:25 +00:00
|
|
|
return (NULL);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
|
|
|
if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
|
|
|
|
(obj->nodesetval == NULL) || (obj->nodesetval->nodeNr <= 0) ||
|
2007-10-24 14:22:25 +00:00
|
|
|
(obj->nodesetval->nodeTab == NULL)) {
|
|
|
|
xmlXPathFreeObject(obj);
|
2008-07-09 08:35:09 +00:00
|
|
|
ctxt->node = relnode;
|
2007-10-24 14:22:25 +00:00
|
|
|
return (NULL);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
ret = obj->nodesetval->nodeTab[0];
|
|
|
|
xmlXPathFreeObject(obj);
|
2008-07-09 08:35:09 +00:00
|
|
|
ctxt->node = relnode;
|
2007-10-24 14:22:25 +00:00
|
|
|
return (ret);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
/**
|
|
|
|
* virXPathNodeSet:
|
|
|
|
* @xpath: the XPath string to evaluate
|
|
|
|
* @ctxt: an XPath context
|
|
|
|
* @list: the returned list of nodes (or NULL if only count matters)
|
|
|
|
*
|
|
|
|
* Convenience function to evaluate an XPath node set
|
|
|
|
*
|
|
|
|
* Returns the number of nodes found in which case @list is set (and
|
|
|
|
* must be freed) or -1 if the evaluation failed.
|
|
|
|
*/
|
|
|
|
int
|
2007-10-24 14:22:25 +00:00
|
|
|
virXPathNodeSet(const char *xpath, xmlXPathContextPtr ctxt,
|
|
|
|
xmlNodePtr ** list)
|
|
|
|
{
|
2007-04-06 12:28:24 +00:00
|
|
|
xmlXPathObjectPtr obj;
|
2008-07-09 08:35:09 +00:00
|
|
|
xmlNodePtr relnode;
|
2007-04-06 12:28:24 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ctxt == NULL) || (xpath == NULL)) {
|
2007-10-24 14:22:25 +00:00
|
|
|
virXMLError(NULL, VIR_ERR_INTERNAL_ERROR,
|
2008-02-04 19:31:30 +00:00
|
|
|
_("Invalid parameter to virXPathNodeSet()"), 0);
|
2007-10-24 14:22:25 +00:00
|
|
|
return (-1);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2008-07-11 16:23:36 +00:00
|
|
|
|
|
|
|
if (list != NULL)
|
|
|
|
*list = NULL;
|
|
|
|
|
2008-07-09 08:35:09 +00:00
|
|
|
relnode = ctxt->node;
|
2007-04-06 12:28:24 +00:00
|
|
|
obj = xmlXPathEval(BAD_CAST xpath, ctxt);
|
|
|
|
if ((obj == NULL) || (obj->type != XPATH_NODESET) ||
|
2008-07-11 16:23:36 +00:00
|
|
|
(obj->nodesetval == NULL) || (obj->nodesetval->nodeNr < 0)) {
|
2007-10-24 14:22:25 +00:00
|
|
|
xmlXPathFreeObject(obj);
|
2008-07-09 08:35:09 +00:00
|
|
|
ctxt->node = relnode;
|
2007-10-24 14:22:25 +00:00
|
|
|
return (-1);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
2007-10-24 14:22:25 +00:00
|
|
|
|
2007-04-06 12:28:24 +00:00
|
|
|
ret = obj->nodesetval->nodeNr;
|
2008-07-11 16:23:36 +00:00
|
|
|
if (list != NULL && ret) {
|
2008-05-29 19:20:22 +00:00
|
|
|
if (VIR_ALLOC_N(*list, ret) < 0) {
|
2007-10-24 14:22:25 +00:00
|
|
|
virXMLError(NULL, VIR_ERR_NO_MEMORY,
|
|
|
|
_("allocate string array"),
|
2007-12-11 21:57:29 +00:00
|
|
|
ret * sizeof(**list));
|
2008-05-29 19:20:22 +00:00
|
|
|
ret = -1;
|
2007-10-24 14:22:25 +00:00
|
|
|
} else {
|
|
|
|
memcpy(*list, obj->nodesetval->nodeTab,
|
|
|
|
ret * sizeof(xmlNodePtr));
|
|
|
|
}
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
xmlXPathFreeObject(obj);
|
2008-07-09 08:35:09 +00:00
|
|
|
ctxt->node = relnode;
|
2007-10-24 14:22:25 +00:00
|
|
|
return (ret);
|
2007-04-06 12:28:24 +00:00
|
|
|
}
|
|
|
|
|
2007-11-30 22:51:54 +00:00
|
|
|
#if WITH_XEN
|
2008-07-25 13:17:27 +00:00
|
|
|
#ifndef PROXY
|
2006-08-26 15:30:44 +00:00
|
|
|
/**
|
2008-07-25 13:17:27 +00:00
|
|
|
* virConvertCpuSet:
|
|
|
|
* @conn: connection
|
|
|
|
* @str: pointer to a Xen or user provided CPU set string pointer
|
|
|
|
* @maxcpu: number of CPUs on the node, if 0 4096 will be used
|
2006-08-26 15:30:44 +00:00
|
|
|
*
|
2008-07-25 13:17:27 +00:00
|
|
|
* Parse the given CPU set string and convert it to a range based
|
|
|
|
* string.
|
2006-08-26 15:30:44 +00:00
|
|
|
*
|
2008-07-25 13:17:27 +00:00
|
|
|
* Returns a new string which must be freed by the caller or NULL in
|
|
|
|
* case of error.
|
2006-08-26 15:30:44 +00:00
|
|
|
*/
|
2008-07-25 13:17:27 +00:00
|
|
|
char *
|
|
|
|
virConvertCpuSet(virConnectPtr conn, const char *str, int maxcpu) {
|
|
|
|
int ret;
|
|
|
|
char *res, *cpuset;
|
|
|
|
const char *cur = str;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return(NULL);
|
|
|
|
|
|
|
|
if (maxcpu <= 0)
|
|
|
|
maxcpu = 4096;
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(cpuset, maxcpu) < 0) {
|
|
|
|
virXMLError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"), 0);
|
|
|
|
return(NULL);
|
2006-08-26 15:30:44 +00:00
|
|
|
}
|
2008-07-25 13:17:27 +00:00
|
|
|
|
|
|
|
ret = virDomainCpuSetParse(conn, &cur, 0, cpuset, maxcpu);
|
|
|
|
if (ret < 0) {
|
|
|
|
VIR_FREE(cpuset);
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
res = virDomainCpuSetFormat(conn, cpuset, maxcpu);
|
|
|
|
VIR_FREE(cpuset);
|
|
|
|
return (res);
|
2006-08-26 15:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-13 14:08:51 +00:00
|
|
|
/**
|
2008-07-25 13:17:27 +00:00
|
|
|
* virBuildSoundStringFromXML
|
|
|
|
* @sound buffer to populate
|
|
|
|
* @len size of preallocated buffer 'sound'
|
|
|
|
* @ctxt xml context to pull sound info from
|
2006-12-13 14:08:51 +00:00
|
|
|
*
|
2008-07-25 13:17:27 +00:00
|
|
|
* Builds a string of the form m1,m2,m3 from the different sound models
|
|
|
|
* in the xml. String must be free'd by caller.
|
2006-12-13 14:08:51 +00:00
|
|
|
*
|
2008-07-25 13:17:27 +00:00
|
|
|
* Returns string on success, NULL on error
|
2006-12-13 14:08:51 +00:00
|
|
|
*/
|
2008-07-25 13:17:27 +00:00
|
|
|
char * virBuildSoundStringFromXML(virConnectPtr conn,
|
|
|
|
xmlXPathContextPtr ctxt) {
|
|
|
|
|
|
|
|
int nb_nodes, size = 256;
|
|
|
|
char *sound;
|
|
|
|
xmlNodePtr *nodes = NULL;
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(sound, size + 1) < 0) {
|
|
|
|
virXMLError(conn, VIR_ERR_NO_MEMORY,
|
|
|
|
_("failed to allocate sound string"), 0);
|
|
|
|
return NULL;
|
2006-12-13 14:08:51 +00:00
|
|
|
}
|
|
|
|
|
2008-07-25 13:17:27 +00:00
|
|
|
nb_nodes = virXPathNodeSet("/domain/devices/sound", ctxt, &nodes);
|
|
|
|
if (nb_nodes > 0) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < nb_nodes && size > 0; i++) {
|
|
|
|
char *model = NULL;
|
|
|
|
int collision = 0;
|
2006-12-13 14:08:51 +00:00
|
|
|
|
2008-07-25 13:17:27 +00:00
|
|
|
model = (char *) xmlGetProp(nodes[i], (xmlChar *) "model");
|
|
|
|
if (!model) {
|
|
|
|
virXMLError(conn, VIR_ERR_XML_ERROR,
|
|
|
|
_("no model for sound device"), 0);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_sound_model_valid(model)) {
|
|
|
|
virXMLError(conn, VIR_ERR_XML_ERROR,
|
|
|
|
_("unknown sound model type"), 0);
|
|
|
|
VIR_FREE(model);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for duplicates in currently built string
|
|
|
|
if (*sound)
|
|
|
|
collision = is_sound_model_conflict(model, sound);
|
|
|
|
|
|
|
|
// If no collision, add to string
|
|
|
|
if (!collision) {
|
|
|
|
if (*sound && (size >= (strlen(model) + 1))) {
|
|
|
|
strncat(sound, ",", size--);
|
|
|
|
} else if (*sound || size < strlen(model)) {
|
|
|
|
VIR_FREE(model);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strncat(sound, model, size);
|
|
|
|
size -= strlen(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VIR_FREE(nodes);
|
|
|
|
return sound;
|
|
|
|
error:
|
|
|
|
VIR_FREE(nodes);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virDomainParseXMLOSDescHVMChar(virConnectPtr conn,
|
|
|
|
char *buf,
|
|
|
|
size_t buflen,
|
|
|
|
xmlNodePtr node)
|
|
|
|
{
|
2008-04-26 14:22:02 +00:00
|
|
|
xmlChar *type = NULL;
|
|
|
|
xmlChar *path = NULL;
|
|
|
|
xmlChar *bindHost = NULL;
|
|
|
|
xmlChar *bindService = NULL;
|
|
|
|
xmlChar *connectHost = NULL;
|
|
|
|
xmlChar *connectService = NULL;
|
|
|
|
xmlChar *mode = NULL;
|
|
|
|
xmlChar *protocol = NULL;
|
|
|
|
xmlNodePtr cur;
|
|
|
|
|
|
|
|
type = xmlGetProp(node, BAD_CAST "type");
|
|
|
|
|
|
|
|
if (type != NULL) {
|
|
|
|
cur = node->children;
|
|
|
|
while (cur != NULL) {
|
|
|
|
if (cur->type == XML_ELEMENT_NODE) {
|
|
|
|
if (xmlStrEqual(cur->name, BAD_CAST "source")) {
|
|
|
|
if (mode == NULL)
|
|
|
|
mode = xmlGetProp(cur, BAD_CAST "mode");
|
|
|
|
|
|
|
|
if (STREQ((const char *)type, "dev") ||
|
|
|
|
STREQ((const char *)type, "file") ||
|
|
|
|
STREQ((const char *)type, "pipe") ||
|
|
|
|
STREQ((const char *)type, "unix")) {
|
|
|
|
if (path == NULL)
|
|
|
|
path = xmlGetProp(cur, BAD_CAST "path");
|
|
|
|
|
|
|
|
} else if (STREQ((const char *)type, "udp") ||
|
|
|
|
STREQ((const char *)type, "tcp")) {
|
|
|
|
if (mode == NULL ||
|
|
|
|
STREQ((const char *)mode, "connect")) {
|
|
|
|
|
|
|
|
if (connectHost == NULL)
|
|
|
|
connectHost = xmlGetProp(cur, BAD_CAST "host");
|
|
|
|
if (connectService == NULL)
|
|
|
|
connectService = xmlGetProp(cur, BAD_CAST "service");
|
|
|
|
} else {
|
|
|
|
if (bindHost == NULL)
|
|
|
|
bindHost = xmlGetProp(cur, BAD_CAST "host");
|
|
|
|
if (bindService == NULL)
|
|
|
|
bindService = xmlGetProp(cur, BAD_CAST "service");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STREQ((const char*)type, "udp")) {
|
|
|
|
xmlFree(mode);
|
|
|
|
mode = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (xmlStrEqual(cur->name, BAD_CAST "protocol")) {
|
|
|
|
if (protocol == NULL)
|
|
|
|
protocol = xmlGetProp(cur, BAD_CAST "type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == NULL ||
|
|
|
|
STREQ((const char *)type, "pty")) {
|
|
|
|
strncpy(buf, "pty", buflen);
|
|
|
|
} else if (STREQ((const char *)type, "null") ||
|
|
|
|
STREQ((const char *)type, "stdio") ||
|
|
|
|
STREQ((const char *)type, "vc")) {
|
|
|
|
snprintf(buf, buflen, "%s", type);
|
|
|
|
} else if (STREQ((const char *)type, "file") ||
|
|
|
|
STREQ((const char *)type, "dev") ||
|
|
|
|
STREQ((const char *)type, "pipe")) {
|
|
|
|
if (path == NULL) {
|
|
|
|
virXMLError(conn, VIR_ERR_XML_ERROR,
|
|
|
|
_("Missing source path attribute for char device"), 0);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STREQ((const char *)type, "dev"))
|
|
|
|
strncpy(buf, (const char *)path, buflen);
|
|
|
|
else
|
|
|
|
snprintf(buf, buflen, "%s:%s", type, path);
|
|
|
|
} else if (STREQ((const char *)type, "tcp")) {
|
|
|
|
int telnet = 0;
|
|
|
|
if (protocol != NULL &&
|
|
|
|
STREQ((const char *)protocol, "telnet"))
|
|
|
|
telnet = 1;
|
|
|
|
|
|
|
|
if (mode == NULL ||
|
|
|
|
STREQ((const char *)mode, "connect")) {
|
|
|
|
if (connectHost == NULL) {
|
|
|
|
virXMLError(conn, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Missing source host attribute for char device"), 0);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (connectService == NULL) {
|
|
|
|
virXMLError(conn, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Missing source service attribute for char device"), 0);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buf, buflen, "%s:%s:%s",
|
|
|
|
(telnet ? "telnet" : "tcp"),
|
|
|
|
connectHost, connectService);
|
|
|
|
} else {
|
|
|
|
if (bindHost == NULL) {
|
|
|
|
virXMLError(conn, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Missing source host attribute for char device"), 0);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (bindService == NULL) {
|
|
|
|
virXMLError(conn, VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Missing source service attribute for char device"), 0);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buf, buflen, "%s:%s:%s,listen",
|
|
|
|
(telnet ? "telnet" : "tcp"),
|
|
|
|
bindHost, bindService);
|
|
|
|
}
|
|
|
|
} else if (STREQ((const char *)type, "udp")) {
|
|
|
|
if (connectService == NULL) {
|
|
|
|
virXMLError(conn, VIR_ERR_XML_ERROR,
|
|
|
|
_("Missing source service attribute for char device"), 0);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buf, buflen, "udp:%s:%s@%s:%s",
|
|
|
|
connectHost ? (const char *)connectHost : "",
|
|
|
|
connectService,
|
|
|
|
bindHost ? (const char *)bindHost : "",
|
|
|
|
bindService ? (const char *)bindService : "");
|
|
|
|
} else if (STREQ((const char *)type, "unix")) {
|
|
|
|
if (path == NULL) {
|
|
|
|
virXMLError(conn, VIR_ERR_XML_ERROR,
|
|
|
|
_("Missing source path attribute for char device"), 0);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode == NULL ||
|
|
|
|
STREQ((const char *)mode, "connect")) {
|
|
|
|
snprintf(buf, buflen, "%s:%s", type, path);
|
|
|
|
} else {
|
|
|
|
snprintf(buf, buflen, "%s:%s,listen", type, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf[buflen-1] = '\0';
|
|
|
|
|
|
|
|
xmlFree(mode);
|
|
|
|
xmlFree(protocol);
|
|
|
|
xmlFree(type);
|
|
|
|
xmlFree(bindHost);
|
|
|
|
xmlFree(bindService);
|
|
|
|
xmlFree(connectHost);
|
|
|
|
xmlFree(connectService);
|
|
|
|
xmlFree(path);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
xmlFree(mode);
|
|
|
|
xmlFree(protocol);
|
|
|
|
xmlFree(type);
|
|
|
|
xmlFree(bindHost);
|
|
|
|
xmlFree(bindService);
|
|
|
|
xmlFree(connectHost);
|
|
|
|
xmlFree(connectService);
|
|
|
|
xmlFree(path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-07-25 13:17:27 +00:00
|
|
|
#endif /* !PROXY */
|
2007-02-07 13:50:18 +00:00
|
|
|
|
2007-11-30 22:51:54 +00:00
|
|
|
#endif /* WITH_XEN */
|