viralloc: Report OOM error on failure
[libvirt-python/ericb.git] / libvirt-qemu-override.c
blob8f1ce5e99b98cd11d457d908a9767488311f5322
1 /*
2 * libvir.c: this modules implements the main part of the glue of the
3 * libvir library and the Python interpreter. It provides the
4 * entry points where an automatically generated stub is
5 * unpractical
7 * Copyright (C) 2011-2012 Red Hat, Inc.
9 * Daniel Veillard <veillard@redhat.com>
12 #include <config.h>
14 /* Horrible kludge to work around even more horrible name-space pollution
15 via Python.h. That file includes /usr/include/python2.5/pyconfig*.h,
16 which has over 180 autoconf-style HAVE_* definitions. Shame on them. */
17 #undef HAVE_PTHREAD_H
19 #include <Python.h>
20 #include <libvirt/libvirt-qemu.h>
21 #include <libvirt/virterror.h>
22 #include "typewrappers.h"
23 #include "libvirt-qemu.h"
25 #ifndef __CYGWIN__
26 extern void initlibvirtmod_qemu(void);
27 #else
28 extern void initcygvirtmod_qemu(void);
29 #endif
31 #if 0
32 # define DEBUG_ERROR 1
33 #endif
35 #if DEBUG_ERROR
36 # define DEBUG(fmt, ...) \
37 printf(fmt, __VA_ARGS__)
38 #else
39 # define DEBUG(fmt, ...) \
40 do {} while (0)
41 #endif
43 /* The two-statement sequence "Py_INCREF(Py_None); return Py_None;"
44 is so common that we encapsulate it here. Now, each use is simply
45 return VIR_PY_NONE; */
46 #define VIR_PY_NONE (Py_INCREF (Py_None), Py_None)
47 #define VIR_PY_INT_FAIL (libvirt_intWrap(-1))
48 #define VIR_PY_INT_SUCCESS (libvirt_intWrap(0))
50 /************************************************************************
51 * *
52 * Statistics *
53 * *
54 ************************************************************************/
56 static PyObject *
57 libvirt_qemu_virDomainQemuMonitorCommand(PyObject *self ATTRIBUTE_UNUSED,
58 PyObject *args) {
59 PyObject *py_retval;
60 char *result = NULL;
61 virDomainPtr domain;
62 PyObject *pyobj_domain;
63 unsigned int flags;
64 char *cmd;
65 int c_retval;
67 if (!PyArg_ParseTuple(args, (char *)"Ozi:virDomainQemuMonitorCommand",
68 &pyobj_domain, &cmd, &flags))
69 return NULL;
70 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
72 if (domain == NULL)
73 return VIR_PY_NONE;
74 LIBVIRT_BEGIN_ALLOW_THREADS;
75 c_retval = virDomainQemuMonitorCommand(domain, cmd, &result, flags);
76 LIBVIRT_END_ALLOW_THREADS;
78 if (c_retval < 0)
79 return VIR_PY_NONE;
81 py_retval = PyString_FromString(result);
82 return py_retval;
85 static PyObject *
86 libvirt_qemu_virDomainQemuAgentCommand(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
88 PyObject *py_retval;
89 char *result = NULL;
90 virDomainPtr domain;
91 PyObject *pyobj_domain;
92 int timeout;
93 unsigned int flags;
94 char *cmd;
96 if (!PyArg_ParseTuple(args, (char *)"Ozii:virDomainQemuAgentCommand",
97 &pyobj_domain, &cmd, &timeout, &flags))
98 return NULL;
99 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
101 if (domain == NULL)
102 return VIR_PY_NONE;
103 LIBVIRT_BEGIN_ALLOW_THREADS;
104 result = virDomainQemuAgentCommand(domain, cmd, timeout, flags);
105 LIBVIRT_END_ALLOW_THREADS;
107 if (!result)
108 return VIR_PY_NONE;
110 py_retval = PyString_FromString(result);
111 return py_retval;
113 /************************************************************************
115 * The registration stuff *
117 ************************************************************************/
118 static PyMethodDef libvirtQemuMethods[] = {
119 #include "libvirt-qemu-export.c"
120 {(char *) "virDomainQemuMonitorCommand", libvirt_qemu_virDomainQemuMonitorCommand, METH_VARARGS, NULL},
121 {(char *) "virDomainQemuAgentCommand", libvirt_qemu_virDomainQemuAgentCommand, METH_VARARGS, NULL},
122 {NULL, NULL, 0, NULL}
125 void
126 #ifndef __CYGWIN__
127 initlibvirtmod_qemu
128 #else
129 initcygvirtmod_qemu
130 #endif
131 (void)
133 static int initialized = 0;
135 if (initialized != 0)
136 return;
138 if (virInitialize() < 0)
139 return;
141 /* initialize the python extension module */
142 Py_InitModule((char *)
143 #ifndef __CYGWIN__
144 "libvirtmod_qemu"
145 #else
146 "cygvirtmod_qemu"
147 #endif
148 , libvirtQemuMethods);
150 initialized = 1;