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
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. */
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
26 extern PyObject
*PyInit_libvirtmod_qemu(void);
28 extern PyObject
*PyInit_cygvirtmod_qemu(void);
32 extern void initlibvirtmod_qemu(void);
34 extern void initcygvirtmod_qemu(void);
39 # define DEBUG_ERROR 1
43 # define DEBUG(fmt, ...) \
44 printf(fmt, __VA_ARGS__)
46 # define DEBUG(fmt, ...) \
50 /*******************************************
51 * Helper functions to avoid importing modules
53 *******************************************/
54 #if LIBVIR_CHECK_VERSION(1, 2, 3)
55 static PyObject
*libvirt_qemu_module
;
56 static PyObject
*libvirt_qemu_dict
;
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__
);
73 return libvirt_qemu_module
;
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__
);
90 Py_INCREF(libvirt_qemu_dict
);
91 return libvirt_qemu_dict
;
96 libvirt_qemu_lookupPythonFunc(const char *funcname
)
100 /* Lookup the python callback */
101 python_cb
= PyDict_GetItemString(getLibvirtQemuDictObject(), funcname
);
104 DEBUG("%s: Error finding %s\n", __FUNCTION__
, funcname
);
110 if (!PyCallable_Check(python_cb
)) {
111 DEBUG("%s: %s is not callable\n", __FUNCTION__
, funcname
);
117 #endif /* LIBVIR_CHECK_VERSION(1, 2, 3) */
120 /************************************************************************
124 ************************************************************************/
127 libvirt_qemu_virDomainQemuMonitorCommand(PyObject
*self ATTRIBUTE_UNUSED
,
133 PyObject
*pyobj_domain
;
138 if (!PyArg_ParseTuple(args
, (char *)"OzI:virDomainQemuMonitorCommand",
139 &pyobj_domain
, &cmd
, &flags
))
141 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
145 LIBVIRT_BEGIN_ALLOW_THREADS
;
146 c_retval
= virDomainQemuMonitorCommand(domain
, cmd
, &result
, flags
);
147 LIBVIRT_END_ALLOW_THREADS
;
152 py_retval
= libvirt_constcharPtrWrap(result
);
157 #if LIBVIR_CHECK_VERSION(0, 10, 0)
159 libvirt_qemu_virDomainQemuAgentCommand(PyObject
*self ATTRIBUTE_UNUSED
,
165 PyObject
*pyobj_domain
;
170 if (!PyArg_ParseTuple(args
, (char *)"OziI:virDomainQemuAgentCommand",
171 &pyobj_domain
, &cmd
, &timeout
, &flags
))
173 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
177 LIBVIRT_BEGIN_ALLOW_THREADS
;
178 result
= virDomainQemuAgentCommand(domain
, cmd
, timeout
, flags
);
179 LIBVIRT_END_ALLOW_THREADS
;
184 py_retval
= libvirt_constcharPtrWrap(result
);
188 #endif /* LIBVIR_CHECK_VERSION(0, 10, 0) */
191 #if LIBVIR_CHECK_VERSION(1, 2, 3)
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
;
202 libvirt_qemu_virConnectDomainQemuMonitorEventCallback(virConnectPtr conn ATTRIBUTE_UNUSED
,
210 PyObject
*pyobj_cbData
= (PyObject
*)opaque
;
212 PyObject
*pyobj_ret
= NULL
;
213 PyObject
*pyobj_conn
;
217 LIBVIRT_ENSURE_THREAD_STATE
;
219 pyobj_cb
= libvirt_qemu_lookupPythonFunc("_dispatchQemuMonitorEventCallback");
223 dictKey
= libvirt_constcharPtrWrap("conn");
226 pyobj_conn
= PyDict_GetItem(pyobj_cbData
, dictKey
);
229 /* Create a python instance of this virDomainPtr */
231 if (!(pyobj_dom
= libvirt_virDomainPtrWrap(dom
))) {
235 Py_INCREF(pyobj_cbData
);
237 /* Call the Callback Dispatcher */
238 pyobj_ret
= PyObject_CallFunction(pyobj_cb
,
240 pyobj_conn
, pyobj_dom
, event
, seconds
,
241 micros
, details
, pyobj_cbData
);
243 Py_DECREF(pyobj_cbData
);
244 Py_DECREF(pyobj_dom
);
248 DEBUG("%s - ret:%p\n", __FUNCTION__
, pyobj_ret
);
251 Py_DECREF(pyobj_ret
);
254 LIBVIRT_RELEASE_THREAD_STATE
;
259 libvirt_qemu_virConnectDomainQemuMonitorEventRegister(PyObject
*self ATTRIBUTE_UNUSED
,
263 PyObject
*pyobj_conn
;
265 PyObject
*pyobj_cbData
;
269 virConnectDomainQemuMonitorEventCallback cb
= NULL
;
273 if (!PyArg_ParseTuple(args
, (char *) "OOzOI:virConnectDomainQemuMonitorEventRegister",
274 &pyobj_conn
, &pyobj_dom
,
275 &event
, &pyobj_cbData
, &flags
))
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
)
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
,
293 libvirt_qemu_virConnectDomainQemuMonitorEventFreeFunc
,
295 LIBVIRT_END_ALLOW_THREADS
;
298 Py_DECREF(pyobj_cbData
);
301 py_retval
= libvirt_intWrap(ret
);
307 libvirt_qemu_virConnectDomainQemuMonitorEventDeregister(PyObject
*self ATTRIBUTE_UNUSED
,
311 PyObject
*pyobj_conn
;
316 if (!PyArg_ParseTuple(args
,
317 (char *) "Oi:virConnectDomainQemuMonitorEventDeregister",
318 &pyobj_conn
, &callbackID
))
321 DEBUG("libvirt_qemu_virConnectDomainQemuMonitorEventDeregister(%p) called\n",
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
);
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
,
373 PyInit_libvirtmod_qemu
375 PyInit_cygvirtmod_qemu
381 if (virInitialize() < 0)
384 module
= PyModule_Create(&moduledef
);
388 #else /* ! PY_MAJOR_VERSION > 2 */
397 if (virInitialize() < 0)
400 /* initialize the python extension module */
401 Py_InitModule((char *)
409 #endif /* ! PY_MAJOR_VERSION > 2 */