Add missing impl of virNetworkListAllPorts
[libvirt-python/ericb.git] / libvirt-qemu-override.c
blobf166f6e0e227b2ca6a3f26f0e4057de2ccd8b278
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 /*******************************************
51 * Helper functions to avoid importing modules
52 * for every callback
53 *******************************************/
54 #if LIBVIR_CHECK_VERSION(1, 2, 3)
55 static PyObject *libvirt_qemu_module;
56 static PyObject *libvirt_qemu_dict;
58 static PyObject *
59 getLibvirtQemuModuleObject(void)
61 if (libvirt_qemu_module)
62 return libvirt_qemu_module;
64 // PyImport_ImportModule returns a new reference
65 /* Bogus (char *) cast for RHEL-5 python API brokenness */
66 libvirt_qemu_module = PyImport_ImportModule((char *)"libvirt_qemu");
67 if (!libvirt_qemu_module) {
68 DEBUG("%s Error importing libvirt_qemu module\n", __FUNCTION__);
69 PyErr_Print();
70 return NULL;
73 return libvirt_qemu_module;
76 static PyObject *
77 getLibvirtQemuDictObject(void)
79 if (libvirt_qemu_dict)
80 return libvirt_qemu_dict;
82 // PyModule_GetDict returns a borrowed reference
83 libvirt_qemu_dict = PyModule_GetDict(getLibvirtQemuModuleObject());
84 if (!libvirt_qemu_dict) {
85 DEBUG("%s Error importing libvirt_qemu dictionary\n", __FUNCTION__);
86 PyErr_Print();
87 return NULL;
90 Py_INCREF(libvirt_qemu_dict);
91 return libvirt_qemu_dict;
95 static PyObject *
96 libvirt_qemu_lookupPythonFunc(const char *funcname)
98 PyObject *python_cb;
100 /* Lookup the python callback */
101 python_cb = PyDict_GetItemString(getLibvirtQemuDictObject(), funcname);
103 if (!python_cb) {
104 DEBUG("%s: Error finding %s\n", __FUNCTION__, funcname);
105 PyErr_Print();
106 PyErr_Clear();
107 return NULL;
110 if (!PyCallable_Check(python_cb)) {
111 DEBUG("%s: %s is not callable\n", __FUNCTION__, funcname);
112 return NULL;
115 return python_cb;
117 #endif /* LIBVIR_CHECK_VERSION(1, 2, 3) */
120 /************************************************************************
122 * Statistics *
124 ************************************************************************/
126 static PyObject *
127 libvirt_qemu_virDomainQemuMonitorCommand(PyObject *self ATTRIBUTE_UNUSED,
128 PyObject *args)
130 PyObject *py_retval;
131 char *result = NULL;
132 virDomainPtr domain;
133 PyObject *pyobj_domain;
134 unsigned int flags;
135 char *cmd;
136 int c_retval;
138 if (!PyArg_ParseTuple(args, (char *)"OzI:virDomainQemuMonitorCommand",
139 &pyobj_domain, &cmd, &flags))
140 return NULL;
141 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
143 if (domain == NULL)
144 return VIR_PY_NONE;
145 LIBVIRT_BEGIN_ALLOW_THREADS;
146 c_retval = virDomainQemuMonitorCommand(domain, cmd, &result, flags);
147 LIBVIRT_END_ALLOW_THREADS;
149 if (c_retval < 0)
150 return VIR_PY_NONE;
152 py_retval = libvirt_constcharPtrWrap(result);
153 VIR_FREE(result);
154 return py_retval;
157 #if LIBVIR_CHECK_VERSION(0, 10, 0)
158 static PyObject *
159 libvirt_qemu_virDomainQemuAgentCommand(PyObject *self ATTRIBUTE_UNUSED,
160 PyObject *args)
162 PyObject *py_retval;
163 char *result = NULL;
164 virDomainPtr domain;
165 PyObject *pyobj_domain;
166 int timeout;
167 unsigned int flags;
168 char *cmd;
170 if (!PyArg_ParseTuple(args, (char *)"OziI:virDomainQemuAgentCommand",
171 &pyobj_domain, &cmd, &timeout, &flags))
172 return NULL;
173 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
175 if (domain == NULL)
176 return VIR_PY_NONE;
177 LIBVIRT_BEGIN_ALLOW_THREADS;
178 result = virDomainQemuAgentCommand(domain, cmd, timeout, flags);
179 LIBVIRT_END_ALLOW_THREADS;
181 if (!result)
182 return VIR_PY_NONE;
184 py_retval = libvirt_constcharPtrWrap(result);
185 VIR_FREE(result);
186 return py_retval;
188 #endif /* LIBVIR_CHECK_VERSION(0, 10, 0) */
191 #if LIBVIR_CHECK_VERSION(1, 2, 3)
192 static void
193 libvirt_qemu_virConnectDomainQemuMonitorEventFreeFunc(void *opaque)
195 PyObject *pyobj_conn = (PyObject*)opaque;
196 LIBVIRT_ENSURE_THREAD_STATE;
197 Py_DECREF(pyobj_conn);
198 LIBVIRT_RELEASE_THREAD_STATE;
201 static void
202 libvirt_qemu_virConnectDomainQemuMonitorEventCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
203 virDomainPtr dom,
204 const char *event,
205 long long seconds,
206 unsigned int micros,
207 const char *details,
208 void *opaque)
210 PyObject *pyobj_cbData = (PyObject*)opaque;
211 PyObject *pyobj_dom;
212 PyObject *pyobj_ret = NULL;
213 PyObject *pyobj_conn;
214 PyObject *dictKey;
215 PyObject *pyobj_cb;
217 LIBVIRT_ENSURE_THREAD_STATE;
219 pyobj_cb = libvirt_qemu_lookupPythonFunc("_dispatchQemuMonitorEventCallback");
220 if (!pyobj_cb)
221 goto cleanup;
223 dictKey = libvirt_constcharPtrWrap("conn");
224 if (!dictKey)
225 goto cleanup;
226 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
227 Py_DECREF(dictKey);
229 /* Create a python instance of this virDomainPtr */
230 virDomainRef(dom);
231 if (!(pyobj_dom = libvirt_virDomainPtrWrap(dom))) {
232 virDomainFree(dom);
233 goto cleanup;
235 Py_INCREF(pyobj_cbData);
237 /* Call the Callback Dispatcher */
238 pyobj_ret = PyObject_CallFunction(pyobj_cb,
239 (char *)"OOsLIsO",
240 pyobj_conn, pyobj_dom, event, seconds,
241 micros, details, pyobj_cbData);
243 Py_DECREF(pyobj_cbData);
244 Py_DECREF(pyobj_dom);
246 cleanup:
247 if (!pyobj_ret) {
248 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
249 PyErr_Print();
250 } else {
251 Py_DECREF(pyobj_ret);
254 LIBVIRT_RELEASE_THREAD_STATE;
258 static PyObject *
259 libvirt_qemu_virConnectDomainQemuMonitorEventRegister(PyObject *self ATTRIBUTE_UNUSED,
260 PyObject *args)
262 PyObject *py_retval;
263 PyObject *pyobj_conn;
264 PyObject *pyobj_dom;
265 PyObject *pyobj_cbData;
266 const char *event;
267 virConnectPtr conn;
268 int ret = 0;
269 virConnectDomainQemuMonitorEventCallback cb = NULL;
270 virDomainPtr dom;
271 unsigned int flags;
273 if (!PyArg_ParseTuple(args, (char *) "OOzOI:virConnectDomainQemuMonitorEventRegister",
274 &pyobj_conn, &pyobj_dom,
275 &event, &pyobj_cbData, &flags))
276 return NULL;
278 DEBUG("libvirt_qemu_virConnectDomainQemuMonitorEventRegister(%p %p %s %p %x) called\n",
279 pyobj_conn, pyobj_dom, NULLSTR(event), pyobj_cbData, flags);
280 conn = PyvirConnect_Get(pyobj_conn);
281 if (pyobj_dom == Py_None)
282 dom = NULL;
283 else
284 dom = PyvirDomain_Get(pyobj_dom);
286 cb = libvirt_qemu_virConnectDomainQemuMonitorEventCallback;
288 Py_INCREF(pyobj_cbData);
290 LIBVIRT_BEGIN_ALLOW_THREADS;
291 ret = virConnectDomainQemuMonitorEventRegister(conn, dom, event,
292 cb, pyobj_cbData,
293 libvirt_qemu_virConnectDomainQemuMonitorEventFreeFunc,
294 flags);
295 LIBVIRT_END_ALLOW_THREADS;
297 if (ret < 0) {
298 Py_DECREF(pyobj_cbData);
301 py_retval = libvirt_intWrap(ret);
302 return py_retval;
306 static PyObject *
307 libvirt_qemu_virConnectDomainQemuMonitorEventDeregister(PyObject *self ATTRIBUTE_UNUSED,
308 PyObject *args)
310 PyObject *py_retval;
311 PyObject *pyobj_conn;
312 int callbackID;
313 virConnectPtr conn;
314 int ret = 0;
316 if (!PyArg_ParseTuple(args,
317 (char *) "Oi:virConnectDomainQemuMonitorEventDeregister",
318 &pyobj_conn, &callbackID))
319 return NULL;
321 DEBUG("libvirt_qemu_virConnectDomainQemuMonitorEventDeregister(%p) called\n",
322 pyobj_conn);
324 conn = PyvirConnect_Get(pyobj_conn);
326 LIBVIRT_BEGIN_ALLOW_THREADS;
328 ret = virConnectDomainQemuMonitorEventDeregister(conn, callbackID);
330 LIBVIRT_END_ALLOW_THREADS;
331 py_retval = libvirt_intWrap(ret);
332 return py_retval;
334 #endif /* LIBVIR_CHECK_VERSION(1, 2, 3) */
336 /************************************************************************
338 * The registration stuff *
340 ************************************************************************/
341 static PyMethodDef libvirtQemuMethods[] = {
342 #include "build/libvirt-qemu-export.c"
343 {(char *) "virDomainQemuMonitorCommand", libvirt_qemu_virDomainQemuMonitorCommand, METH_VARARGS, NULL},
344 #if LIBVIR_CHECK_VERSION(0, 10, 0)
345 {(char *) "virDomainQemuAgentCommand", libvirt_qemu_virDomainQemuAgentCommand, METH_VARARGS, NULL},
346 #endif /* LIBVIR_CHECK_VERSION(0, 10, 0) */
347 #if LIBVIR_CHECK_VERSION(1, 2, 3)
348 {(char *) "virConnectDomainQemuMonitorEventRegister", libvirt_qemu_virConnectDomainQemuMonitorEventRegister, METH_VARARGS, NULL},
349 {(char *) "virConnectDomainQemuMonitorEventDeregister", libvirt_qemu_virConnectDomainQemuMonitorEventDeregister, METH_VARARGS, NULL},
350 #endif /* LIBVIR_CHECK_VERSION(1, 2, 3) */
351 {NULL, NULL, 0, NULL}
354 #if PY_MAJOR_VERSION > 2
355 static struct PyModuleDef moduledef = {
356 PyModuleDef_HEAD_INIT,
357 # ifndef __CYGWIN__
358 "libvirtmod_qemu",
359 # else
360 "cygvirtmod_qemu",
361 # endif
362 NULL,
364 libvirtQemuMethods,
365 NULL,
366 NULL,
367 NULL,
368 NULL
371 PyObject *
372 # ifndef __CYGWIN__
373 PyInit_libvirtmod_qemu
374 # else
375 PyInit_cygvirtmod_qemu
376 # endif
377 (void)
379 PyObject *module;
381 if (virInitialize() < 0)
382 return NULL;
384 module = PyModule_Create(&moduledef);
386 return module;
388 #else /* ! PY_MAJOR_VERSION > 2 */
389 void
390 # ifndef __CYGWIN__
391 initlibvirtmod_qemu
392 # else
393 initcygvirtmod_qemu
394 # endif
395 (void)
397 if (virInitialize() < 0)
398 return;
400 /* initialize the python extension module */
401 Py_InitModule((char *)
402 # ifndef __CYGWIN__
403 "libvirtmod_qemu",
404 # else
405 "cygvirtmod_qemu",
406 # endif
407 libvirtQemuMethods);
409 #endif /* ! PY_MAJOR_VERSION > 2 */