event-test: add missing events
[libvirt-python/ericb.git] / libvirt-qemu-override.c
blob05ead30c92988a6d9de9858428ef74d14c672480
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-2014 Red Hat, Inc.
9 * Daniel Veillard <veillard@redhat.com>
12 /* Horrible kludge to work around even more horrible name-space pollution
13 via Python.h. That file includes /usr/include/python2.5/pyconfig*.h,
14 which has over 180 autoconf-style HAVE_* definitions. Shame on them. */
15 #undef HAVE_PTHREAD_H
17 #include <Python.h>
18 #include <libvirt/libvirt-qemu.h>
19 #include <libvirt/virterror.h>
20 #include "typewrappers.h"
21 #include "libvirt-utils.h"
22 #include "build/libvirt-qemu.h"
24 #if PY_MAJOR_VERSION > 2
25 # ifndef __CYGWIN__
26 extern PyObject *PyInit_libvirtmod_qemu(void);
27 # else
28 extern PyObject *PyInit_cygvirtmod_qemu(void);
29 # endif
30 #else
31 # ifndef __CYGWIN__
32 extern void initlibvirtmod_qemu(void);
33 # else
34 extern void initcygvirtmod_qemu(void);
35 # endif
36 #endif
38 #if 0
39 # define DEBUG_ERROR 1
40 #endif
42 #if DEBUG_ERROR
43 # define DEBUG(fmt, ...) \
44 printf(fmt, __VA_ARGS__)
45 #else
46 # define DEBUG(fmt, ...) \
47 do {} while (0)
48 #endif
50 /* The two-statement sequence "Py_INCREF(Py_None); return Py_None;"
51 is so common that we encapsulate it here. Now, each use is simply
52 return VIR_PY_NONE; */
53 #define VIR_PY_NONE (Py_INCREF (Py_None), Py_None)
54 #define VIR_PY_INT_FAIL (libvirt_intWrap(-1))
55 #define VIR_PY_INT_SUCCESS (libvirt_intWrap(0))
57 /*******************************************
58 * Helper functions to avoid importing modules
59 * for every callback
60 *******************************************/
61 #if LIBVIR_CHECK_VERSION(1, 2, 3)
62 static PyObject *libvirt_qemu_module;
63 static PyObject *libvirt_qemu_dict;
65 static PyObject *
66 getLibvirtQemuModuleObject(void)
68 if (libvirt_qemu_module)
69 return libvirt_qemu_module;
71 // PyImport_ImportModule returns a new reference
72 /* Bogus (char *) cast for RHEL-5 python API brokenness */
73 libvirt_qemu_module = PyImport_ImportModule((char *)"libvirt_qemu");
74 if (!libvirt_qemu_module) {
75 DEBUG("%s Error importing libvirt_qemu module\n", __FUNCTION__);
76 PyErr_Print();
77 return NULL;
80 return libvirt_qemu_module;
83 static PyObject *
84 getLibvirtQemuDictObject(void)
86 if (libvirt_qemu_dict)
87 return libvirt_qemu_dict;
89 // PyModule_GetDict returns a borrowed reference
90 libvirt_qemu_dict = PyModule_GetDict(getLibvirtQemuModuleObject());
91 if (!libvirt_qemu_dict) {
92 DEBUG("%s Error importing libvirt_qemu dictionary\n", __FUNCTION__);
93 PyErr_Print();
94 return NULL;
97 Py_INCREF(libvirt_qemu_dict);
98 return libvirt_qemu_dict;
102 static PyObject *
103 libvirt_qemu_lookupPythonFunc(const char *funcname)
105 PyObject *python_cb;
107 /* Lookup the python callback */
108 python_cb = PyDict_GetItemString(getLibvirtQemuDictObject(), funcname);
110 if (!python_cb) {
111 DEBUG("%s: Error finding %s\n", __FUNCTION__, funcname);
112 PyErr_Print();
113 PyErr_Clear();
114 return NULL;
117 if (!PyCallable_Check(python_cb)) {
118 DEBUG("%s: %s is not callable\n", __FUNCTION__, funcname);
119 return NULL;
122 return python_cb;
124 #endif /* LIBVIR_CHECK_VERSION(1, 2, 3) */
127 /************************************************************************
129 * Statistics *
131 ************************************************************************/
133 static PyObject *
134 libvirt_qemu_virDomainQemuMonitorCommand(PyObject *self ATTRIBUTE_UNUSED,
135 PyObject *args) {
136 PyObject *py_retval;
137 char *result = NULL;
138 virDomainPtr domain;
139 PyObject *pyobj_domain;
140 unsigned int flags;
141 char *cmd;
142 int c_retval;
144 if (!PyArg_ParseTuple(args, (char *)"Ozi:virDomainQemuMonitorCommand",
145 &pyobj_domain, &cmd, &flags))
146 return NULL;
147 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
149 if (domain == NULL)
150 return VIR_PY_NONE;
151 LIBVIRT_BEGIN_ALLOW_THREADS;
152 c_retval = virDomainQemuMonitorCommand(domain, cmd, &result, flags);
153 LIBVIRT_END_ALLOW_THREADS;
155 if (c_retval < 0)
156 return VIR_PY_NONE;
158 py_retval = libvirt_constcharPtrWrap(result);
159 VIR_FREE(result);
160 return py_retval;
163 #if LIBVIR_CHECK_VERSION(0, 10, 0)
164 static PyObject *
165 libvirt_qemu_virDomainQemuAgentCommand(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
167 PyObject *py_retval;
168 char *result = NULL;
169 virDomainPtr domain;
170 PyObject *pyobj_domain;
171 int timeout;
172 unsigned int flags;
173 char *cmd;
175 if (!PyArg_ParseTuple(args, (char *)"Ozii:virDomainQemuAgentCommand",
176 &pyobj_domain, &cmd, &timeout, &flags))
177 return NULL;
178 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
180 if (domain == NULL)
181 return VIR_PY_NONE;
182 LIBVIRT_BEGIN_ALLOW_THREADS;
183 result = virDomainQemuAgentCommand(domain, cmd, timeout, flags);
184 LIBVIRT_END_ALLOW_THREADS;
186 if (!result)
187 return VIR_PY_NONE;
189 py_retval = libvirt_constcharPtrWrap(result);
190 VIR_FREE(result);
191 return py_retval;
193 #endif /* LIBVIR_CHECK_VERSION(0, 10, 0) */
196 #if LIBVIR_CHECK_VERSION(1, 2, 3)
197 static void
198 libvirt_qemu_virConnectDomainQemuMonitorEventFreeFunc(void *opaque)
200 PyObject *pyobj_conn = (PyObject*)opaque;
201 LIBVIRT_ENSURE_THREAD_STATE;
202 Py_DECREF(pyobj_conn);
203 LIBVIRT_RELEASE_THREAD_STATE;
206 static void
207 libvirt_qemu_virConnectDomainQemuMonitorEventCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
208 virDomainPtr dom,
209 const char *event,
210 long long seconds,
211 unsigned int micros,
212 const char *details,
213 void *opaque)
215 PyObject *pyobj_cbData = (PyObject*)opaque;
216 PyObject *pyobj_dom;
217 PyObject *pyobj_ret = NULL;
218 PyObject *pyobj_conn;
219 PyObject *dictKey;
220 PyObject *pyobj_cb;
222 LIBVIRT_ENSURE_THREAD_STATE;
224 pyobj_cb = libvirt_qemu_lookupPythonFunc("_dispatchQemuMonitorEventCallback");
225 if (!pyobj_cb)
226 goto cleanup;
228 dictKey = libvirt_constcharPtrWrap("conn");
229 if (!dictKey)
230 goto cleanup;
231 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
232 Py_DECREF(dictKey);
234 /* Create a python instance of this virDomainPtr */
235 virDomainRef(dom);
236 if (!(pyobj_dom = libvirt_virDomainPtrWrap(dom))) {
237 virDomainFree(dom);
238 goto cleanup;
240 Py_INCREF(pyobj_cbData);
242 /* Call the Callback Dispatcher */
243 pyobj_ret = PyObject_CallFunction(pyobj_cb,
244 (char *)"OOsLIsO",
245 pyobj_conn, pyobj_dom, event, seconds,
246 micros, details, pyobj_cbData);
248 Py_DECREF(pyobj_cbData);
249 Py_DECREF(pyobj_dom);
251 cleanup:
252 if (!pyobj_ret) {
253 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
254 PyErr_Print();
255 } else {
256 Py_DECREF(pyobj_ret);
259 LIBVIRT_RELEASE_THREAD_STATE;
263 static PyObject *
264 libvirt_qemu_virConnectDomainQemuMonitorEventRegister(PyObject *self ATTRIBUTE_UNUSED,
265 PyObject *args)
267 PyObject *py_retval;
268 PyObject *pyobj_conn;
269 PyObject *pyobj_dom;
270 PyObject *pyobj_cbData;
271 const char *event;
272 virConnectPtr conn;
273 int ret = 0;
274 virConnectDomainQemuMonitorEventCallback cb = NULL;
275 virDomainPtr dom;
276 unsigned int flags;
278 if (!PyArg_ParseTuple
279 (args, (char *) "OOzOI",
280 &pyobj_conn, &pyobj_dom, &event, &pyobj_cbData, &flags)) {
281 DEBUG("%s failed parsing tuple\n", __FUNCTION__);
282 return VIR_PY_INT_FAIL;
285 DEBUG("libvirt_qemu_virConnectDomainQemuMonitorEventRegister(%p %p %s %p %x) called\n",
286 pyobj_conn, pyobj_dom, NULLSTR(event), pyobj_cbData, flags);
287 conn = PyvirConnect_Get(pyobj_conn);
288 if (pyobj_dom == Py_None)
289 dom = NULL;
290 else
291 dom = PyvirDomain_Get(pyobj_dom);
293 cb = libvirt_qemu_virConnectDomainQemuMonitorEventCallback;
295 Py_INCREF(pyobj_cbData);
297 LIBVIRT_BEGIN_ALLOW_THREADS;
298 ret = virConnectDomainQemuMonitorEventRegister(conn, dom, event,
299 cb, pyobj_cbData,
300 libvirt_qemu_virConnectDomainQemuMonitorEventFreeFunc,
301 flags);
302 LIBVIRT_END_ALLOW_THREADS;
304 if (ret < 0)
305 Py_DECREF(pyobj_cbData);
307 py_retval = libvirt_intWrap(ret);
308 return py_retval;
312 static PyObject *
313 libvirt_qemu_virConnectDomainQemuMonitorEventDeregister(PyObject *self ATTRIBUTE_UNUSED,
314 PyObject *args)
316 PyObject *py_retval;
317 PyObject *pyobj_conn;
318 int callbackID;
319 virConnectPtr conn;
320 int ret = 0;
322 if (!PyArg_ParseTuple
323 (args, (char *) "Oi:virConnectDomainQemuMonitorEventDeregister",
324 &pyobj_conn, &callbackID))
325 return NULL;
327 DEBUG("libvirt_qemu_virConnectDomainQemuMonitorEventDeregister(%p) called\n",
328 pyobj_conn);
330 conn = PyvirConnect_Get(pyobj_conn);
332 LIBVIRT_BEGIN_ALLOW_THREADS;
334 ret = virConnectDomainQemuMonitorEventDeregister(conn, callbackID);
336 LIBVIRT_END_ALLOW_THREADS;
337 py_retval = libvirt_intWrap(ret);
338 return py_retval;
340 #endif /* LIBVIR_CHECK_VERSION(1, 2, 3) */
342 /************************************************************************
344 * The registration stuff *
346 ************************************************************************/
347 static PyMethodDef libvirtQemuMethods[] = {
348 #include "build/libvirt-qemu-export.c"
349 {(char *) "virDomainQemuMonitorCommand", libvirt_qemu_virDomainQemuMonitorCommand, METH_VARARGS, NULL},
350 #if LIBVIR_CHECK_VERSION(0, 10, 0)
351 {(char *) "virDomainQemuAgentCommand", libvirt_qemu_virDomainQemuAgentCommand, METH_VARARGS, NULL},
352 #endif /* LIBVIR_CHECK_VERSION(0, 10, 0) */
353 #if LIBVIR_CHECK_VERSION(1, 2, 3)
354 {(char *) "virConnectDomainQemuMonitorEventRegister", libvirt_qemu_virConnectDomainQemuMonitorEventRegister, METH_VARARGS, NULL},
355 {(char *) "virConnectDomainQemuMonitorEventDeregister", libvirt_qemu_virConnectDomainQemuMonitorEventDeregister, METH_VARARGS, NULL},
356 #endif /* LIBVIR_CHECK_VERSION(1, 2, 3) */
357 {NULL, NULL, 0, NULL}
360 #if PY_MAJOR_VERSION > 2
361 static struct PyModuleDef moduledef = {
362 PyModuleDef_HEAD_INIT,
363 # ifndef __CYGWIN__
364 "libvirtmod_qemu",
365 # else
366 "cygvirtmod_qemu",
367 # endif
368 NULL,
370 libvirtQemuMethods,
371 NULL,
372 NULL,
373 NULL,
374 NULL
377 PyObject *
378 # ifndef __CYGWIN__
379 PyInit_libvirtmod_qemu
380 # else
381 PyInit_cygvirtmod_qemu
382 # endif
383 (void)
385 PyObject *module;
387 if (virInitialize() < 0)
388 return NULL;
390 module = PyModule_Create(&moduledef);
392 return module;
394 #else /* ! PY_MAJOR_VERSION > 2 */
395 void
396 # ifndef __CYGWIN__
397 initlibvirtmod_qemu
398 # else
399 initcygvirtmod_qemu
400 # endif
401 (void)
403 if (virInitialize() < 0)
404 return;
406 /* initialize the python extension module */
407 Py_InitModule((char *)
408 # ifndef __CYGWIN__
409 "libvirtmod_qemu",
410 # else
411 "cygvirtmod_qemu",
412 # endif
413 libvirtQemuMethods);
415 #endif /* ! PY_MAJOR_VERSION > 2 */