/*
* libvir.c: this modules implements the main part of the glue of the
* libvir library and the Python interpreter. It provides the
* entry points where an automatically generated stub is
* unpractical
*
* Copyright (C) 2005, 2007-2012 Red Hat, Inc.
*
* Daniel Veillard <veillard@redhat.com>
*/
#include <config.h>
/* Horrible kludge to work around even more horrible name-space pollution
via Python.h. That file includes /usr/include/python2.5/pyconfig*.h,
which has over 180 autoconf-style HAVE_* definitions. Shame on them. */
#undef HAVE_PTHREAD_H
/* We want to see *_LAST enums. */
#define VIR_ENUM_SENTINELS
#include <Python.h>
#include "libvirt/libvirt.h"
#include "libvirt/virterror.h"
#include "typewrappers.h"
#include "libvirt.h"
#include "memory.h"
#include "virtypedparam.h"
#include "ignore-value.h"
#include "util.h"
#ifndef __CYGWIN__
extern void initlibvirtmod(void);
#else
extern void initcygvirtmod(void);
#endif
#if 0
# define DEBUG_ERROR 1
#endif
#if DEBUG_ERROR
# define DEBUG(fmt, ...) \
printf(fmt, __VA_ARGS__)
#else
# define DEBUG(fmt, ...) \
do {} while (0)
#endif
/* The two-statement sequence "Py_INCREF(Py_None); return Py_None;"
is so common that we encapsulate it here. Now, each use is simply
return VIR_PY_NONE; */
#define VIR_PY_NONE (Py_INCREF (Py_None), Py_None)
#define VIR_PY_INT_FAIL (libvirt_intWrap(-1))
#define VIR_PY_INT_SUCCESS (libvirt_intWrap(0))
/* We don't want to free() returned value. As written in doc:
* PyString_AsString returns pointer to 'internal buffer of string,
* not a copy' and 'It must not be deallocated'. */
static char *py_str(PyObject *obj)
{
PyObject *str = PyObject_Str(obj);
if (!str) {
PyErr_Print();
PyErr_Clear();
return NULL;
};
return PyString_AsString(str);
}
/* Helper function to convert a virTypedParameter output array into a
* Python dictionary for return to the user. Return NULL on failure,
* after raising a python exception. */
static PyObject * ATTRIBUTE_NONNULL(1)
getPyVirTypedParameter(const virTypedParameterPtr params, int nparams)
{
PyObject *key, *val, *info;
int i;
if ((info = PyDict_New()) == NULL)
return NULL;
for (i = 0 ; i < nparams ; i++) {
switch (params[i].type) {
case VIR_TYPED_PARAM_INT:
val = PyInt_FromLong(params[i].value.i);
break;
case VIR_TYPED_PARAM_UINT:
val = PyInt_FromLong(params[i].value.ui);
break;
case VIR_TYPED_PARAM_LLONG:
val = PyLong_FromLongLong(params[i].value.l);
break;
case VIR_TYPED_PARAM_ULLONG:
val = PyLong_FromUnsignedLongLong(params[i].value.ul);
break;
case VIR_TYPED_PARAM_DOUBLE:
val = PyFloat_FromDouble(params[i].value.d);
break;
case VIR_TYPED_PARAM_BOOLEAN:
val = PyBool_FromLong(params[i].value.b);
break;
case VIR_TYPED_PARAM_STRING:
val = libvirt_constcharPtrWrap(params[i].value.s);
break;
default:
/* Possible if a newer server has a bug and sent stuff we
* don't recognize. */
PyErr_Format(PyExc_LookupError,
"Type value \"%d\" not recognized",
params[i].type);
val = NULL;
break;
}
key = libvirt_constcharPtrWrap(params[i].field);
if (!key || !val)
goto cleanup;
if (PyDict_SetItem(info, key, val) < 0) {
Py_DECREF(info);
goto cleanup;
}
Py_DECREF(key);
Py_DECREF(val);
}
return info;
cleanup:
Py_XDECREF(key);
Py_XDECREF(val);
return NULL;
}
/* Allocate a new typed parameter array with the same contents and
* length as info, and using the array params of length nparams as
* hints on what types to use when creating the new array. The caller
* must NOT clear the array before freeing it, as it points into info
* rather than allocated strings. Return NULL on failure, after
* raising a python exception. */
static virTypedParameterPtr ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
setPyVirTypedParameter(PyObject *info,
const virTypedParameterPtr params, int nparams)
{
PyObject *key, *value;
Py_ssize_t pos = 0;
virTypedParameterPtr temp = NULL, ret = NULL;
Py_ssize_t size;
int i;
if ((size = PyDict_Size(info)) < 0)
return NULL;
/* Libvirt APIs use NULL array and 0 size as a special case;
* setting should have at least one parameter. */
if (size == 0) {
PyErr_Format(PyExc_LookupError, "Dictionary must not be empty");
return NULL;
}
if (VIR_ALLOC_N(ret, size) < 0) {
PyErr_NoMemory();
return NULL;
}
temp = &ret[0];
while (PyDict_Next(info, &pos, &key, &value)) {
char *keystr = NULL;
if ((keystr = PyString_AsString(key)) == NULL)
goto cleanup;
for (i = 0; i < nparams; i++) {
if (STREQ(params[i].field, keystr))
break;
}
if (i == nparams) {
PyErr_Format(PyExc_LookupError,
"Attribute name \"%s\" could not be recognized",
keystr);
goto cleanup;
}
ignore_value(virStrcpyStatic(temp->field, keystr));
temp->type = params[i].type;
switch(params[i].type) {
case VIR_TYPED_PARAM_INT:
if (libvirt_intUnwrap(value, &temp->value.i) < 0)
goto cleanup;
break;
case VIR_TYPED_PARAM_UINT:
if (libvirt_uintUnwrap(value, &temp->value.ui) < 0)
goto cleanup;
break;
case VIR_TYPED_PARAM_LLONG:
if (libvirt_longlongUnwrap(value, &temp->value.l) < 0)
goto cleanup;
break;
case VIR_TYPED_PARAM_ULLONG:
if (libvirt_ulonglongUnwrap(value, &temp->value.ul) < 0)
goto cleanup;
break;
case VIR_TYPED_PARAM_DOUBLE:
if (libvirt_doubleUnwrap(value, &temp->value.d) < 0)
goto cleanup;
break;
case VIR_TYPED_PARAM_BOOLEAN:
{
bool b;
if (libvirt_boolUnwrap(value, &b) < 0)
goto cleanup;
temp->value.b = b;
break;
}
case VIR_TYPED_PARAM_STRING:
{
char *string_val = PyString_AsString(value);
if (!string_val)
goto cleanup;
temp->value.s = string_val;
break;
}
default:
/* Possible if a newer server has a bug and sent stuff we
* don't recognize. */
PyErr_Format(PyExc_LookupError,
"Type value \"%d\" not recognized",
params[i].type);
goto cleanup;
}
temp++;
}
return ret;
cleanup:
VIR_FREE(ret);
return NULL;
}
/************************************************************************
* *
* Statistics *
* *
************************************************************************/
static PyObject *
libvirt_virDomainBlockStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
virDomainPtr domain;
PyObject *pyobj_domain;
char * path;
int c_retval;
virDomainBlockStatsStruct stats;
PyObject *info;
if (!PyArg_ParseTuple(args, (char *)"Oz:virDomainBlockStats",
&pyobj_domain,&path))
return NULL;
domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
LIBVIRT_BEGIN_ALLOW_THREADS;
c_retval = virDomainBlockStats(domain, path, &stats, sizeof(stats));
LIBVIRT_END_ALLOW_THREADS;
if (c_retval < 0)
return VIR_PY_NONE;
/* convert to a Python tuple of long objects */
if ((info = PyTuple_New(5)) == NULL)
return VIR_PY_NONE;
|