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 /* 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
60 *******************************************/
61 #if LIBVIR_CHECK_VERSION(1, 2, 3)
62 static PyObject
*libvirt_qemu_module
;
63 static PyObject
*libvirt_qemu_dict
;
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__
);
80 return libvirt_qemu_module
;
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__
);
97 Py_INCREF(libvirt_qemu_dict
);
98 return libvirt_qemu_dict
;
103 libvirt_qemu_lookupPythonFunc(const char *funcname
)
107 /* Lookup the python callback */
108 python_cb
= PyDict_GetItemString(getLibvirtQemuDictObject(), funcname
);
111 DEBUG("%s: Error finding %s\n", __FUNCTION__
, funcname
);
117 if (!PyCallable_Check(python_cb
)) {
118 DEBUG("%s: %s is not callable\n", __FUNCTION__
, funcname
);
124 #endif /* LIBVIR_CHECK_VERSION(1, 2, 3) */
127 /************************************************************************
131 ************************************************************************/
134 libvirt_qemu_virDomainQemuMonitorCommand(PyObject
*self ATTRIBUTE_UNUSED
,
139 PyObject
*pyobj_domain
;
144 if (!PyArg_ParseTuple(args
, (char *)"Ozi:virDomainQemuMonitorCommand",
145 &pyobj_domain
, &cmd
, &flags
))
147 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
151 LIBVIRT_BEGIN_ALLOW_THREADS
;
152 c_retval
= virDomainQemuMonitorCommand(domain
, cmd
, &result
, flags
);
153 LIBVIRT_END_ALLOW_THREADS
;
158 py_retval
= libvirt_constcharPtrWrap(result
);
163 #if LIBVIR_CHECK_VERSION(0, 10, 0)
165 libvirt_qemu_virDomainQemuAgentCommand(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
)
170 PyObject
*pyobj_domain
;
175 if (!PyArg_ParseTuple(args
, (char *)"Ozii:virDomainQemuAgentCommand",
176 &pyobj_domain
, &cmd
, &timeout
, &flags
))
178 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
182 LIBVIRT_BEGIN_ALLOW_THREADS
;
183 result
= virDomainQemuAgentCommand(domain
, cmd
, timeout
, flags
);
184 LIBVIRT_END_ALLOW_THREADS
;
189 py_retval
= libvirt_constcharPtrWrap(result
);
193 #endif /* LIBVIR_CHECK_VERSION(0, 10, 0) */
196 #if LIBVIR_CHECK_VERSION(1, 2, 3)
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
;
207 libvirt_qemu_virConnectDomainQemuMonitorEventCallback(virConnectPtr conn ATTRIBUTE_UNUSED
,
215 PyObject
*pyobj_cbData
= (PyObject
*)opaque
;
217 PyObject
*pyobj_ret
= NULL
;
218 PyObject
*pyobj_conn
;
222 LIBVIRT_ENSURE_THREAD_STATE
;
224 pyobj_cb
= libvirt_qemu_lookupPythonFunc("_dispatchQemuMonitorEventCallback");
228 dictKey
= libvirt_constcharPtrWrap("conn");
231 pyobj_conn
= PyDict_GetItem(pyobj_cbData
, dictKey
);
234 /* Create a python instance of this virDomainPtr */
236 if (!(pyobj_dom
= libvirt_virDomainPtrWrap(dom
))) {
240 Py_INCREF(pyobj_cbData
);
242 /* Call the Callback Dispatcher */
243 pyobj_ret
= PyObject_CallFunction(pyobj_cb
,
245 pyobj_conn
, pyobj_dom
, event
, seconds
,
246 micros
, details
, pyobj_cbData
);
248 Py_DECREF(pyobj_cbData
);
249 Py_DECREF(pyobj_dom
);
253 DEBUG("%s - ret:%p\n", __FUNCTION__
, pyobj_ret
);
256 Py_DECREF(pyobj_ret
);
259 LIBVIRT_RELEASE_THREAD_STATE
;
264 libvirt_qemu_virConnectDomainQemuMonitorEventRegister(PyObject
*self ATTRIBUTE_UNUSED
,
268 PyObject
*pyobj_conn
;
270 PyObject
*pyobj_cbData
;
274 virConnectDomainQemuMonitorEventCallback cb
= NULL
;
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
)
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
,
300 libvirt_qemu_virConnectDomainQemuMonitorEventFreeFunc
,
302 LIBVIRT_END_ALLOW_THREADS
;
305 Py_DECREF(pyobj_cbData
);
307 py_retval
= libvirt_intWrap(ret
);
313 libvirt_qemu_virConnectDomainQemuMonitorEventDeregister(PyObject
*self ATTRIBUTE_UNUSED
,
317 PyObject
*pyobj_conn
;
322 if (!PyArg_ParseTuple
323 (args
, (char *) "Oi:virConnectDomainQemuMonitorEventDeregister",
324 &pyobj_conn
, &callbackID
))
327 DEBUG("libvirt_qemu_virConnectDomainQemuMonitorEventDeregister(%p) called\n",
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
);
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
,
379 PyInit_libvirtmod_qemu
381 PyInit_cygvirtmod_qemu
387 if (virInitialize() < 0)
390 module
= PyModule_Create(&moduledef
);
394 #else /* ! PY_MAJOR_VERSION > 2 */
403 if (virInitialize() < 0)
406 /* initialize the python extension module */
407 Py_InitModule((char *)
415 #endif /* ! PY_MAJOR_VERSION > 2 */