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) 2005, 2007 Red Hat, Inc.
9 * Daniel Veillard <veillard@redhat.com>
15 #include "libvirt/libvirt.h"
16 #include "libvirt/virterror.h"
17 #include "libvirt_wrap.h"
18 #include "libvirt-py.h"
21 extern void initlibvirtmod(void);
23 extern void initcygvirtmod(void);
26 /* The two-statement sequence "Py_INCREF(Py_None); return Py_None;"
27 is so common that we encapsulate it here. Now, each use is simply
28 return VIR_PY_NONE; */
29 #define VIR_PY_NONE (Py_INCREF (Py_None), Py_None)
30 #define VIR_PY_INT_FAIL (libvirt_intWrap(-1))
31 #define VIR_PY_INT_SUCCESS (libvirt_intWrap(0))
33 /************************************************************************
37 ************************************************************************/
40 libvirt_virDomainBlockStats(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
) {
42 PyObject
*pyobj_domain
;
45 virDomainBlockStatsStruct stats
;
48 if (!PyArg_ParseTuple(args
, (char *)"Oz:virDomainBlockStats",
51 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
53 c_retval
= virDomainBlockStats(domain
, path
, &stats
, sizeof(stats
));
57 /* convert to a Python tuple of long objects */
58 if ((info
= PyTuple_New(5)) == NULL
)
60 PyTuple_SetItem(info
, 0, PyLong_FromLongLong(stats
.rd_req
));
61 PyTuple_SetItem(info
, 1, PyLong_FromLongLong(stats
.rd_bytes
));
62 PyTuple_SetItem(info
, 2, PyLong_FromLongLong(stats
.wr_req
));
63 PyTuple_SetItem(info
, 3, PyLong_FromLongLong(stats
.wr_bytes
));
64 PyTuple_SetItem(info
, 4, PyLong_FromLongLong(stats
.errs
));
69 libvirt_virDomainInterfaceStats(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
) {
71 PyObject
*pyobj_domain
;
74 virDomainInterfaceStatsStruct stats
;
77 if (!PyArg_ParseTuple(args
, (char *)"Oz:virDomainInterfaceStats",
80 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
82 c_retval
= virDomainInterfaceStats(domain
, path
, &stats
, sizeof(stats
));
86 /* convert to a Python tuple of long objects */
87 if ((info
= PyTuple_New(8)) == NULL
)
89 PyTuple_SetItem(info
, 0, PyLong_FromLongLong(stats
.rx_bytes
));
90 PyTuple_SetItem(info
, 1, PyLong_FromLongLong(stats
.rx_packets
));
91 PyTuple_SetItem(info
, 2, PyLong_FromLongLong(stats
.rx_errs
));
92 PyTuple_SetItem(info
, 3, PyLong_FromLongLong(stats
.rx_drop
));
93 PyTuple_SetItem(info
, 4, PyLong_FromLongLong(stats
.tx_bytes
));
94 PyTuple_SetItem(info
, 5, PyLong_FromLongLong(stats
.tx_packets
));
95 PyTuple_SetItem(info
, 6, PyLong_FromLongLong(stats
.tx_errs
));
96 PyTuple_SetItem(info
, 7, PyLong_FromLongLong(stats
.tx_drop
));
102 libvirt_virDomainGetSchedulerType(PyObject
*self ATTRIBUTE_UNUSED
,
105 PyObject
*pyobj_domain
, *info
;
109 if (!PyArg_ParseTuple(args
, (char *)"O:virDomainGetScedulerType",
112 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
114 c_retval
= virDomainGetSchedulerType(domain
, &nparams
);
115 if (c_retval
== NULL
)
118 /* convert to a Python tupple of long objects */
119 if ((info
= PyTuple_New(2)) == NULL
) {
124 PyTuple_SetItem(info
, 0, libvirt_constcharPtrWrap(c_retval
));
125 PyTuple_SetItem(info
, 1, PyInt_FromLong((long)nparams
));
131 libvirt_virDomainGetSchedulerParameters(PyObject
*self ATTRIBUTE_UNUSED
,
134 PyObject
*pyobj_domain
, *info
;
137 virSchedParameterPtr params
;
139 if (!PyArg_ParseTuple(args
, (char *)"O:virDomainGetScedulerParameters",
142 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
144 c_retval
= virDomainGetSchedulerType(domain
, &nparams
);
145 if (c_retval
== NULL
)
149 if ((params
= malloc(sizeof(*params
)*nparams
)) == NULL
)
152 if (virDomainGetSchedulerParameters(domain
, params
, &nparams
) < 0) {
157 /* convert to a Python tupple of long objects */
158 if ((info
= PyDict_New()) == NULL
) {
162 for (i
= 0 ; i
< nparams
; i
++) {
165 switch (params
[i
].type
) {
166 case VIR_DOMAIN_SCHED_FIELD_INT
:
167 val
= PyInt_FromLong((long)params
[i
].value
.i
);
170 case VIR_DOMAIN_SCHED_FIELD_UINT
:
171 val
= PyInt_FromLong((long)params
[i
].value
.ui
);
174 case VIR_DOMAIN_SCHED_FIELD_LLONG
:
175 val
= PyLong_FromLongLong((long long)params
[i
].value
.l
);
178 case VIR_DOMAIN_SCHED_FIELD_ULLONG
:
179 val
= PyLong_FromLongLong((long long)params
[i
].value
.ul
);
182 case VIR_DOMAIN_SCHED_FIELD_DOUBLE
:
183 val
= PyFloat_FromDouble((double)params
[i
].value
.d
);
186 case VIR_DOMAIN_SCHED_FIELD_BOOLEAN
:
187 val
= PyBool_FromLong((long)params
[i
].value
.b
);
196 key
= libvirt_constcharPtrWrap(params
[i
].field
);
197 PyDict_SetItem(info
, key
, val
);
204 libvirt_virDomainSetSchedulerParameters(PyObject
*self ATTRIBUTE_UNUSED
,
207 PyObject
*pyobj_domain
, *info
;
210 virSchedParameterPtr params
;
212 if (!PyArg_ParseTuple(args
, (char *)"OO:virDomainSetScedulerParameters",
213 &pyobj_domain
, &info
))
215 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
217 c_retval
= virDomainGetSchedulerType(domain
, &nparams
);
218 if (c_retval
== NULL
)
219 return VIR_PY_INT_FAIL
;
222 if ((params
= malloc(sizeof(*params
)*nparams
)) == NULL
)
223 return VIR_PY_INT_FAIL
;
225 if (virDomainGetSchedulerParameters(domain
, params
, &nparams
) < 0) {
227 return VIR_PY_INT_FAIL
;
230 /* convert to a Python tupple of long objects */
231 for (i
= 0 ; i
< nparams
; i
++) {
233 key
= libvirt_constcharPtrWrap(params
[i
].field
);
234 val
= PyDict_GetItem(info
, key
);
240 switch (params
[i
].type
) {
241 case VIR_DOMAIN_SCHED_FIELD_INT
:
242 params
[i
].value
.i
= (int)PyInt_AS_LONG(val
);
245 case VIR_DOMAIN_SCHED_FIELD_UINT
:
246 params
[i
].value
.ui
= (unsigned int)PyInt_AS_LONG(val
);
249 case VIR_DOMAIN_SCHED_FIELD_LLONG
:
250 params
[i
].value
.l
= (long long)PyLong_AsLongLong(val
);
253 case VIR_DOMAIN_SCHED_FIELD_ULLONG
:
254 params
[i
].value
.ul
= (unsigned long long)PyLong_AsLongLong(val
);
257 case VIR_DOMAIN_SCHED_FIELD_DOUBLE
:
258 params
[i
].value
.d
= (double)PyFloat_AsDouble(val
);
261 case VIR_DOMAIN_SCHED_FIELD_BOOLEAN
:
263 /* Hack - Python's definition of Py_True breaks strict
264 * aliasing rules, so can't directly compare :-(
266 PyObject
*hacktrue
= PyBool_FromLong(1);
267 params
[i
].value
.b
= hacktrue
== val
? 1 : 0;
274 return VIR_PY_INT_FAIL
;
278 if (virDomainSetSchedulerParameters(domain
, params
, nparams
) < 0) {
280 return VIR_PY_INT_FAIL
;
284 return VIR_PY_INT_SUCCESS
;
288 libvirt_virDomainGetVcpus(PyObject
*self ATTRIBUTE_UNUSED
,
291 PyObject
*pyobj_domain
, *pyretval
= NULL
, *pycpuinfo
= NULL
, *pycpumap
= NULL
;
292 virNodeInfo nodeinfo
;
293 virDomainInfo dominfo
;
294 virVcpuInfoPtr cpuinfo
= NULL
;
295 unsigned char *cpumap
= NULL
;
298 if (!PyArg_ParseTuple(args
, (char *)"O:virDomainGetVcpus",
301 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
303 if (virNodeGetInfo(virDomainGetConnect(domain
), &nodeinfo
) != 0)
306 if (virDomainGetInfo(domain
, &dominfo
) != 0)
309 if ((cpuinfo
= malloc(sizeof(*cpuinfo
)*dominfo
.nrVirtCpu
)) == NULL
)
312 cpumaplen
= VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo
));
313 if ((cpumap
= malloc(dominfo
.nrVirtCpu
* cpumaplen
)) == NULL
)
316 if (virDomainGetVcpus(domain
,
317 cpuinfo
, dominfo
.nrVirtCpu
,
318 cpumap
, cpumaplen
) < 0)
321 /* convert to a Python tupple of long objects */
322 if ((pyretval
= PyTuple_New(2)) == NULL
)
324 if ((pycpuinfo
= PyList_New(dominfo
.nrVirtCpu
)) == NULL
)
326 if ((pycpumap
= PyList_New(dominfo
.nrVirtCpu
)) == NULL
)
329 for (i
= 0 ; i
< dominfo
.nrVirtCpu
; i
++) {
330 PyObject
*info
= PyTuple_New(4);
333 PyTuple_SetItem(info
, 0, PyInt_FromLong((long)cpuinfo
[i
].number
));
334 PyTuple_SetItem(info
, 1, PyInt_FromLong((long)cpuinfo
[i
].state
));
335 PyTuple_SetItem(info
, 2, PyLong_FromLongLong((long long)cpuinfo
[i
].cpuTime
));
336 PyTuple_SetItem(info
, 3, PyInt_FromLong((long)cpuinfo
[i
].cpu
));
337 PyList_SetItem(pycpuinfo
, i
, info
);
339 for (i
= 0 ; i
< dominfo
.nrVirtCpu
; i
++) {
340 PyObject
*info
= PyTuple_New(VIR_NODEINFO_MAXCPUS(nodeinfo
));
344 for (j
= 0 ; j
< VIR_NODEINFO_MAXCPUS(nodeinfo
) ; j
++) {
345 PyTuple_SetItem(info
, j
, PyBool_FromLong(VIR_CPU_USABLE(cpumap
, cpumaplen
, i
, j
)));
347 PyList_SetItem(pycpumap
, i
, info
);
349 PyTuple_SetItem(pyretval
, 0, pycpuinfo
);
350 PyTuple_SetItem(pyretval
, 1, pycpumap
);
360 /* NB, Py_DECREF is a badly defined macro, so we require
361 * braces here to avoid 'ambiguous else' warnings from
363 * NB. this comment is true at of time of writing wrt to
364 * at least python2.5.
366 if (pyretval
) { Py_DECREF(pyretval
); }
367 if (pycpuinfo
) { Py_DECREF(pycpuinfo
); }
368 if (pycpumap
) { Py_DECREF(pycpumap
); }
374 libvirt_virDomainPinVcpu(PyObject
*self ATTRIBUTE_UNUSED
,
377 PyObject
*pyobj_domain
, *pycpumap
, *truth
;
378 virNodeInfo nodeinfo
;
379 unsigned char *cpumap
;
380 int cpumaplen
, i
, vcpu
;
382 if (!PyArg_ParseTuple(args
, (char *)"OiO:virDomainPinVcpu",
383 &pyobj_domain
, &vcpu
, &pycpumap
))
385 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
387 if (virNodeGetInfo(virDomainGetConnect(domain
), &nodeinfo
) != 0)
388 return VIR_PY_INT_FAIL
;
390 cpumaplen
= VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo
));
391 if ((cpumap
= malloc(cpumaplen
)) == NULL
)
392 return VIR_PY_INT_FAIL
;
393 memset(cpumap
, 0, cpumaplen
);
395 truth
= PyBool_FromLong(1);
396 for (i
= 0 ; i
< VIR_NODEINFO_MAXCPUS(nodeinfo
) ; i
++) {
397 PyObject
*flag
= PyTuple_GetItem(pycpumap
, i
);
399 VIR_USE_CPU(cpumap
, i
);
401 VIR_UNUSE_CPU(cpumap
, i
);
404 virDomainPinVcpu(domain
, vcpu
, cpumap
, cpumaplen
);
408 return VIR_PY_INT_SUCCESS
;
412 /************************************************************************
414 * Global error handler at the Python level *
416 ************************************************************************/
418 static PyObject
*libvirt_virPythonErrorFuncHandler
= NULL
;
419 static PyObject
*libvirt_virPythonErrorFuncCtxt
= NULL
;
422 libvirt_virGetLastError(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args ATTRIBUTE_UNUSED
)
427 if (virCopyLastError(&err
) <= 0)
430 if ((info
= PyTuple_New(9)) == NULL
)
432 PyTuple_SetItem(info
, 0, PyInt_FromLong((long) err
.code
));
433 PyTuple_SetItem(info
, 1, PyInt_FromLong((long) err
.domain
));
434 PyTuple_SetItem(info
, 2, libvirt_constcharPtrWrap(err
.message
));
435 PyTuple_SetItem(info
, 3, PyInt_FromLong((long) err
.level
));
436 PyTuple_SetItem(info
, 4, libvirt_constcharPtrWrap(err
.str1
));
437 PyTuple_SetItem(info
, 5, libvirt_constcharPtrWrap(err
.str2
));
438 PyTuple_SetItem(info
, 6, libvirt_constcharPtrWrap(err
.str3
));
439 PyTuple_SetItem(info
, 7, PyInt_FromLong((long) err
.int1
));
440 PyTuple_SetItem(info
, 8, PyInt_FromLong((long) err
.int2
));
446 libvirt_virConnGetLastError(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
)
451 PyObject
*pyobj_conn
;
453 if (!PyArg_ParseTuple(args
, (char *)"O:virConGetLastError", &pyobj_conn
))
455 conn
= (virConnectPtr
) PyvirConnect_Get(pyobj_conn
);
457 if (virConnCopyLastError(conn
, &err
) <= 0)
460 if ((info
= PyTuple_New(9)) == NULL
)
462 PyTuple_SetItem(info
, 0, PyInt_FromLong((long) err
.code
));
463 PyTuple_SetItem(info
, 1, PyInt_FromLong((long) err
.domain
));
464 PyTuple_SetItem(info
, 2, libvirt_constcharPtrWrap(err
.message
));
465 PyTuple_SetItem(info
, 3, PyInt_FromLong((long) err
.level
));
466 PyTuple_SetItem(info
, 4, libvirt_constcharPtrWrap(err
.str1
));
467 PyTuple_SetItem(info
, 5, libvirt_constcharPtrWrap(err
.str2
));
468 PyTuple_SetItem(info
, 6, libvirt_constcharPtrWrap(err
.str3
));
469 PyTuple_SetItem(info
, 7, PyInt_FromLong((long) err
.int1
));
470 PyTuple_SetItem(info
, 8, PyInt_FromLong((long) err
.int2
));
476 libvirt_virErrorFuncHandler(ATTRIBUTE_UNUSED
void *ctx
, virErrorPtr err
)
478 PyObject
*list
, *info
;
482 printf("libvirt_virErrorFuncHandler(%p, %s, ...) called\n", ctx
, msg
);
485 if ((err
== NULL
) || (err
->code
== VIR_ERR_OK
))
488 LIBVIRT_ENSURE_THREAD_STATE
;
490 if ((libvirt_virPythonErrorFuncHandler
== NULL
) ||
491 (libvirt_virPythonErrorFuncHandler
== Py_None
)) {
492 virDefaultErrorFunc(err
);
494 list
= PyTuple_New(2);
495 info
= PyTuple_New(9);
496 PyTuple_SetItem(list
, 0, libvirt_virPythonErrorFuncCtxt
);
497 PyTuple_SetItem(list
, 1, info
);
498 Py_XINCREF(libvirt_virPythonErrorFuncCtxt
);
499 PyTuple_SetItem(info
, 0, PyInt_FromLong((long) err
->code
));
500 PyTuple_SetItem(info
, 1, PyInt_FromLong((long) err
->domain
));
501 PyTuple_SetItem(info
, 2, libvirt_constcharPtrWrap(err
->message
));
502 PyTuple_SetItem(info
, 3, PyInt_FromLong((long) err
->level
));
503 PyTuple_SetItem(info
, 4, libvirt_constcharPtrWrap(err
->str1
));
504 PyTuple_SetItem(info
, 5, libvirt_constcharPtrWrap(err
->str2
));
505 PyTuple_SetItem(info
, 6, libvirt_constcharPtrWrap(err
->str3
));
506 PyTuple_SetItem(info
, 7, PyInt_FromLong((long) err
->int1
));
507 PyTuple_SetItem(info
, 8, PyInt_FromLong((long) err
->int2
));
508 /* TODO pass conn and dom if available */
509 result
= PyEval_CallObject(libvirt_virPythonErrorFuncHandler
, list
);
514 LIBVIRT_RELEASE_THREAD_STATE
;
518 libvirt_virRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject
* self
,
525 if (!PyArg_ParseTuple
526 (args
, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f
,
531 printf("libvirt_virRegisterErrorHandler(%p, %p) called\n", pyobj_ctx
,
535 virSetErrorFunc(NULL
, libvirt_virErrorFuncHandler
);
536 if (libvirt_virPythonErrorFuncHandler
!= NULL
) {
537 Py_XDECREF(libvirt_virPythonErrorFuncHandler
);
539 if (libvirt_virPythonErrorFuncCtxt
!= NULL
) {
540 Py_XDECREF(libvirt_virPythonErrorFuncCtxt
);
543 if ((pyobj_f
== Py_None
) && (pyobj_ctx
== Py_None
)) {
544 libvirt_virPythonErrorFuncHandler
= NULL
;
545 libvirt_virPythonErrorFuncCtxt
= NULL
;
547 Py_XINCREF(pyobj_ctx
);
550 /* TODO: check f is a function ! */
551 libvirt_virPythonErrorFuncHandler
= pyobj_f
;
552 libvirt_virPythonErrorFuncCtxt
= pyobj_ctx
;
555 py_retval
= libvirt_intWrap(1);
559 static int virConnectCredCallbackWrapper(virConnectCredentialPtr cred
,
564 PyObject
*pyauth
= (PyObject
*)cbdata
;
570 LIBVIRT_ENSURE_THREAD_STATE
;
572 pycb
= PyList_GetItem(pyauth
, 1);
573 pycbdata
= PyList_GetItem(pyauth
, 2);
575 list
= PyTuple_New(2);
576 pycred
= PyTuple_New(ncred
);
577 for (i
= 0 ; i
< ncred
; i
++) {
578 PyObject
*pycreditem
;
579 pycreditem
= PyList_New(5);
581 PyTuple_SetItem(pycred
, i
, pycreditem
);
582 PyList_SetItem(pycreditem
, 0, PyInt_FromLong((long) cred
[i
].type
));
583 PyList_SetItem(pycreditem
, 1, PyString_FromString(cred
[i
].prompt
));
584 if (cred
[i
].challenge
) {
585 PyList_SetItem(pycreditem
, 2, PyString_FromString(cred
[i
].challenge
));
588 PyList_SetItem(pycreditem
, 2, Py_None
);
590 if (cred
[i
].defresult
) {
591 PyList_SetItem(pycreditem
, 3, PyString_FromString(cred
[i
].defresult
));
594 PyList_SetItem(pycreditem
, 3, Py_None
);
596 PyList_SetItem(pycreditem
, 4, Py_None
);
599 PyTuple_SetItem(list
, 0, pycred
);
600 Py_XINCREF(pycbdata
);
601 PyTuple_SetItem(list
, 1, pycbdata
);
604 pyret
= PyEval_CallObject(pycb
, list
);
605 if (PyErr_Occurred())
608 ret
= PyLong_AsLong(pyret
);
610 for (i
= 0 ; i
< ncred
; i
++) {
611 PyObject
*pycreditem
;
614 pycreditem
= PyTuple_GetItem(pycred
, i
);
615 pyresult
= PyList_GetItem(pycreditem
, 4);
616 if (pyresult
!= Py_None
)
617 result
= PyString_AsString(pyresult
);
618 if (result
!= NULL
) {
619 cred
[i
].result
= strdup(result
);
620 cred
[i
].resultlen
= strlen(result
);
622 cred
[i
].result
= NULL
;
623 cred
[i
].resultlen
= 0;
632 LIBVIRT_RELEASE_THREAD_STATE
;
639 libvirt_virConnectOpenAuth(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
) {
641 virConnectPtr c_retval
;
646 PyObject
*pycredtype
;
649 if (!PyArg_ParseTuple(args
, (char *)"zOi:virConnectOpenAuth", &name
, &pyauth
, &flags
))
652 pycredtype
= PyList_GetItem(pyauth
, 0);
653 pycredcb
= PyList_GetItem(pyauth
, 1);
655 auth
.ncredtype
= PyList_Size(pycredtype
);
656 if (auth
.ncredtype
) {
658 auth
.credtype
= malloc(sizeof(*auth
.credtype
) * auth
.ncredtype
);
659 if (auth
.credtype
== NULL
)
661 for (i
= 0 ; i
< auth
.ncredtype
; i
++) {
663 val
= PyList_GetItem(pycredtype
, i
);
664 auth
.credtype
[i
] = (int)PyLong_AsLong(val
);
667 auth
.cb
= pycredcb
? virConnectCredCallbackWrapper
: NULL
;
668 auth
.cbdata
= pyauth
;
670 LIBVIRT_BEGIN_ALLOW_THREADS
;
672 c_retval
= virConnectOpenAuth(name
, &auth
, flags
);
673 LIBVIRT_END_ALLOW_THREADS
;
674 py_retval
= libvirt_virConnectPtrWrap((virConnectPtr
) c_retval
);
679 /************************************************************************
681 * Wrappers for functions where generator fails *
683 ************************************************************************/
686 libvirt_virGetVersion (PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
)
689 unsigned long libVer
, typeVer
= 0;
692 if (!PyArg_ParseTuple (args
, (char *) "|s", &type
))
695 LIBVIRT_BEGIN_ALLOW_THREADS
;
698 c_retval
= virGetVersion (&libVer
, NULL
, NULL
);
700 c_retval
= virGetVersion (&libVer
, type
, &typeVer
);
702 LIBVIRT_END_ALLOW_THREADS
;
708 return PyInt_FromLong (libVer
);
710 return Py_BuildValue ((char *) "kk", libVer
, typeVer
);
715 libvirt_virConnectListDomainsID(PyObject
*self ATTRIBUTE_UNUSED
,
718 int ids
[500], c_retval
, i
;
720 PyObject
*pyobj_conn
;
723 if (!PyArg_ParseTuple(args
, (char *)"O:virConnectListDomains", &pyobj_conn
))
725 conn
= (virConnectPtr
) PyvirConnect_Get(pyobj_conn
);
727 LIBVIRT_BEGIN_ALLOW_THREADS
;
728 c_retval
= virConnectListDomains(conn
, &ids
[0], 500);
729 LIBVIRT_END_ALLOW_THREADS
;
732 py_retval
= PyList_New(c_retval
);
733 for (i
= 0;i
< c_retval
;i
++) {
734 PyList_SetItem(py_retval
, i
, libvirt_intWrap(ids
[i
]));
740 libvirt_virConnectListDefinedDomains(PyObject
*self ATTRIBUTE_UNUSED
,
746 PyObject
*pyobj_conn
;
749 if (!PyArg_ParseTuple(args
, (char *)"O:virConnectListDefinedDomains", &pyobj_conn
))
751 conn
= (virConnectPtr
) PyvirConnect_Get(pyobj_conn
);
753 c_retval
= virConnectNumOfDefinedDomains(conn
);
758 names
= malloc(sizeof(*names
) * c_retval
);
761 c_retval
= virConnectListDefinedDomains(conn
, names
, c_retval
);
767 py_retval
= PyList_New(c_retval
);
770 for (i
= 0;i
< c_retval
;i
++) {
771 PyList_SetItem(py_retval
, i
, libvirt_constcharPtrWrap(names
[i
]));
781 libvirt_virDomainGetInfo(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
) {
785 PyObject
*pyobj_domain
;
788 if (!PyArg_ParseTuple(args
, (char *)"O:virDomainGetInfo", &pyobj_domain
))
790 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
792 LIBVIRT_BEGIN_ALLOW_THREADS
;
793 c_retval
= virDomainGetInfo(domain
, &info
);
794 LIBVIRT_END_ALLOW_THREADS
;
797 py_retval
= PyList_New(5);
798 PyList_SetItem(py_retval
, 0, libvirt_intWrap((int) info
.state
));
799 PyList_SetItem(py_retval
, 1, libvirt_ulongWrap(info
.maxMem
));
800 PyList_SetItem(py_retval
, 2, libvirt_ulongWrap(info
.memory
));
801 PyList_SetItem(py_retval
, 3, libvirt_intWrap((int) info
.nrVirtCpu
));
802 PyList_SetItem(py_retval
, 4,
803 libvirt_longlongWrap((unsigned long long) info
.cpuTime
));
808 libvirt_virNodeGetInfo(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
) {
812 PyObject
*pyobj_conn
;
815 if (!PyArg_ParseTuple(args
, (char *)"O:virDomainGetInfo", &pyobj_conn
))
817 conn
= (virConnectPtr
) PyvirConnect_Get(pyobj_conn
);
819 LIBVIRT_BEGIN_ALLOW_THREADS
;
820 c_retval
= virNodeGetInfo(conn
, &info
);
821 LIBVIRT_END_ALLOW_THREADS
;
824 py_retval
= PyList_New(8);
825 PyList_SetItem(py_retval
, 0, libvirt_constcharPtrWrap(&info
.model
[0]));
826 PyList_SetItem(py_retval
, 1, libvirt_longWrap((long) info
.memory
>> 10));
827 PyList_SetItem(py_retval
, 2, libvirt_intWrap((int) info
.cpus
));
828 PyList_SetItem(py_retval
, 3, libvirt_intWrap((int) info
.mhz
));
829 PyList_SetItem(py_retval
, 4, libvirt_intWrap((int) info
.nodes
));
830 PyList_SetItem(py_retval
, 5, libvirt_intWrap((int) info
.sockets
));
831 PyList_SetItem(py_retval
, 6, libvirt_intWrap((int) info
.cores
));
832 PyList_SetItem(py_retval
, 7, libvirt_intWrap((int) info
.threads
));
837 libvirt_virDomainGetUUID(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
) {
839 unsigned char uuid
[VIR_UUID_BUFLEN
];
841 PyObject
*pyobj_domain
;
844 if (!PyArg_ParseTuple(args
, (char *)"O:virDomainGetUUID", &pyobj_domain
))
846 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
850 LIBVIRT_BEGIN_ALLOW_THREADS
;
851 c_retval
= virDomainGetUUID(domain
, &uuid
[0]);
852 LIBVIRT_END_ALLOW_THREADS
;
856 py_retval
= PyString_FromStringAndSize((char *) &uuid
[0], VIR_UUID_BUFLEN
);
862 libvirt_virDomainLookupByUUID(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
) {
864 virDomainPtr c_retval
;
866 PyObject
*pyobj_conn
;
867 unsigned char * uuid
;
870 if (!PyArg_ParseTuple(args
, (char *)"Oz#:virDomainLookupByUUID", &pyobj_conn
, &uuid
, &len
))
872 conn
= (virConnectPtr
) PyvirConnect_Get(pyobj_conn
);
874 if ((uuid
== NULL
) || (len
!= VIR_UUID_BUFLEN
))
877 LIBVIRT_BEGIN_ALLOW_THREADS
;
878 c_retval
= virDomainLookupByUUID(conn
, uuid
);
879 LIBVIRT_END_ALLOW_THREADS
;
880 py_retval
= libvirt_virDomainPtrWrap((virDomainPtr
) c_retval
);
886 libvirt_virConnectListNetworks(PyObject
*self ATTRIBUTE_UNUSED
,
892 PyObject
*pyobj_conn
;
895 if (!PyArg_ParseTuple(args
, (char *)"O:virConnectListNetworks", &pyobj_conn
))
897 conn
= (virConnectPtr
) PyvirConnect_Get(pyobj_conn
);
899 c_retval
= virConnectNumOfNetworks(conn
);
904 names
= malloc(sizeof(*names
) * c_retval
);
907 c_retval
= virConnectListNetworks(conn
, names
, c_retval
);
913 py_retval
= PyList_New(c_retval
);
916 for (i
= 0;i
< c_retval
;i
++) {
917 PyList_SetItem(py_retval
, i
, libvirt_constcharPtrWrap(names
[i
]));
928 libvirt_virConnectListDefinedNetworks(PyObject
*self ATTRIBUTE_UNUSED
,
934 PyObject
*pyobj_conn
;
937 if (!PyArg_ParseTuple(args
, (char *)"O:virConnectListDefinedNetworks", &pyobj_conn
))
939 conn
= (virConnectPtr
) PyvirConnect_Get(pyobj_conn
);
941 c_retval
= virConnectNumOfDefinedNetworks(conn
);
946 names
= malloc(sizeof(*names
) * c_retval
);
949 c_retval
= virConnectListDefinedNetworks(conn
, names
, c_retval
);
955 py_retval
= PyList_New(c_retval
);
958 for (i
= 0;i
< c_retval
;i
++) {
959 PyList_SetItem(py_retval
, i
, libvirt_constcharPtrWrap(names
[i
]));
970 libvirt_virNetworkGetUUID(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
) {
972 unsigned char uuid
[VIR_UUID_BUFLEN
];
973 virNetworkPtr domain
;
974 PyObject
*pyobj_domain
;
977 if (!PyArg_ParseTuple(args
, (char *)"O:virNetworkGetUUID", &pyobj_domain
))
979 domain
= (virNetworkPtr
) PyvirNetwork_Get(pyobj_domain
);
983 LIBVIRT_BEGIN_ALLOW_THREADS
;
984 c_retval
= virNetworkGetUUID(domain
, &uuid
[0]);
985 LIBVIRT_END_ALLOW_THREADS
;
989 py_retval
= PyString_FromStringAndSize((char *) &uuid
[0], VIR_UUID_BUFLEN
);
995 libvirt_virNetworkLookupByUUID(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
) {
997 virNetworkPtr c_retval
;
999 PyObject
*pyobj_conn
;
1000 unsigned char * uuid
;
1003 if (!PyArg_ParseTuple(args
, (char *)"Oz#:virNetworkLookupByUUID", &pyobj_conn
, &uuid
, &len
))
1005 conn
= (virConnectPtr
) PyvirConnect_Get(pyobj_conn
);
1007 if ((uuid
== NULL
) || (len
!= VIR_UUID_BUFLEN
))
1010 LIBVIRT_BEGIN_ALLOW_THREADS
;
1011 c_retval
= virNetworkLookupByUUID(conn
, uuid
);
1012 LIBVIRT_END_ALLOW_THREADS
;
1013 py_retval
= libvirt_virNetworkPtrWrap((virNetworkPtr
) c_retval
);
1019 libvirt_virDomainGetAutostart(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
) {
1020 PyObject
*py_retval
;
1021 int c_retval
, autostart
;
1022 virDomainPtr domain
;
1023 PyObject
*pyobj_domain
;
1025 if (!PyArg_ParseTuple(args
, (char *)"O:virDomainGetAutostart", &pyobj_domain
))
1028 domain
= (virDomainPtr
) PyvirDomain_Get(pyobj_domain
);
1030 LIBVIRT_BEGIN_ALLOW_THREADS
;
1031 c_retval
= virDomainGetAutostart(domain
, &autostart
);
1032 LIBVIRT_END_ALLOW_THREADS
;
1035 return VIR_PY_INT_FAIL
;
1036 py_retval
= libvirt_intWrap(autostart
);
1042 libvirt_virNetworkGetAutostart(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
) {
1043 PyObject
*py_retval
;
1044 int c_retval
, autostart
;
1045 virNetworkPtr network
;
1046 PyObject
*pyobj_network
;
1048 if (!PyArg_ParseTuple(args
, (char *)"O:virNetworkGetAutostart", &pyobj_network
))
1051 network
= (virNetworkPtr
) PyvirNetwork_Get(pyobj_network
);
1053 LIBVIRT_BEGIN_ALLOW_THREADS
;
1054 c_retval
= virNetworkGetAutostart(network
, &autostart
);
1055 LIBVIRT_END_ALLOW_THREADS
;
1058 return VIR_PY_INT_FAIL
;
1059 py_retval
= libvirt_intWrap(autostart
);
1064 libvirt_virNodeGetCellsFreeMemory(PyObject
*self ATTRIBUTE_UNUSED
, PyObject
*args
)
1066 PyObject
*py_retval
;
1067 PyObject
*pyobj_conn
;
1068 int startCell
, maxCells
, c_retval
, i
;
1070 unsigned long long *freeMems
;
1072 if (!PyArg_ParseTuple(args
, (char *)"Oii:virNodeGetCellsFreeMemory", &pyobj_conn
, &startCell
, &maxCells
))
1075 if ((startCell
< 0) || (maxCells
<= 0) || (startCell
+ maxCells
> 10000))
1078 conn
= (virConnectPtr
) PyvirConnect_Get(pyobj_conn
);
1080 malloc(maxCells
* sizeof(*freeMems
));
1081 if (freeMems
== NULL
)
1084 LIBVIRT_BEGIN_ALLOW_THREADS
;
1085 c_retval
= virNodeGetCellsFreeMemory(conn
, freeMems
, startCell
, maxCells
);
1086 LIBVIRT_END_ALLOW_THREADS
;
1093 py_retval
= PyList_New(c_retval
);
1094 for (i
= 0;i
< c_retval
;i
++) {
1095 PyList_SetItem(py_retval
, i
,
1096 libvirt_longlongWrap((long long) freeMems
[i
]));
1102 /************************************************************************
1104 * The registration stuff *
1106 ************************************************************************/
1107 static PyMethodDef libvirtMethods
[] = {
1108 #include "libvirt-export.c"
1109 {(char *) "virGetVersion", libvirt_virGetVersion
, METH_VARARGS
, NULL
},
1110 {(char *) "virConnectOpenAuth", libvirt_virConnectOpenAuth
, METH_VARARGS
, NULL
},
1111 {(char *) "virConnectListDomainsID", libvirt_virConnectListDomainsID
, METH_VARARGS
, NULL
},
1112 {(char *) "virConnectListDefinedDomains", libvirt_virConnectListDefinedDomains
, METH_VARARGS
, NULL
},
1113 {(char *) "virDomainGetInfo", libvirt_virDomainGetInfo
, METH_VARARGS
, NULL
},
1114 {(char *) "virNodeGetInfo", libvirt_virNodeGetInfo
, METH_VARARGS
, NULL
},
1115 {(char *) "virDomainGetUUID", libvirt_virDomainGetUUID
, METH_VARARGS
, NULL
},
1116 {(char *) "virDomainLookupByUUID", libvirt_virDomainLookupByUUID
, METH_VARARGS
, NULL
},
1117 {(char *) "virRegisterErrorHandler", libvirt_virRegisterErrorHandler
, METH_VARARGS
, NULL
},
1118 {(char *) "virGetLastError", libvirt_virGetLastError
, METH_VARARGS
, NULL
},
1119 {(char *) "virConnGetLastError", libvirt_virConnGetLastError
, METH_VARARGS
, NULL
},
1120 {(char *) "virConnectListNetworks", libvirt_virConnectListNetworks
, METH_VARARGS
, NULL
},
1121 {(char *) "virConnectListDefinedNetworks", libvirt_virConnectListDefinedNetworks
, METH_VARARGS
, NULL
},
1122 {(char *) "virNetworkGetUUID", libvirt_virNetworkGetUUID
, METH_VARARGS
, NULL
},
1123 {(char *) "virNetworkLookupByUUID", libvirt_virNetworkLookupByUUID
, METH_VARARGS
, NULL
},
1124 {(char *) "virDomainGetAutostart", libvirt_virDomainGetAutostart
, METH_VARARGS
, NULL
},
1125 {(char *) "virNetworkGetAutostart", libvirt_virNetworkGetAutostart
, METH_VARARGS
, NULL
},
1126 {(char *) "virDomainBlockStats", libvirt_virDomainBlockStats
, METH_VARARGS
, NULL
},
1127 {(char *) "virDomainInterfaceStats", libvirt_virDomainInterfaceStats
, METH_VARARGS
, NULL
},
1128 {(char *) "virNodeGetCellsFreeMemory", libvirt_virNodeGetCellsFreeMemory
, METH_VARARGS
, NULL
},
1129 {(char *) "virDomainGetSchedulerType", libvirt_virDomainGetSchedulerType
, METH_VARARGS
, NULL
},
1130 {(char *) "virDomainGetSchedulerParameters", libvirt_virDomainGetSchedulerParameters
, METH_VARARGS
, NULL
},
1131 {(char *) "virDomainSetSchedulerParameters", libvirt_virDomainSetSchedulerParameters
, METH_VARARGS
, NULL
},
1132 {(char *) "virDomainGetVcpus", libvirt_virDomainGetVcpus
, METH_VARARGS
, NULL
},
1133 {(char *) "virDomainPinVcpu", libvirt_virDomainPinVcpu
, METH_VARARGS
, NULL
},
1134 {NULL
, NULL
, 0, NULL
}
1145 static int initialized
= 0;
1147 if (initialized
!= 0)
1152 /* intialize the python extension module */
1153 Py_InitModule((char *)
1165 * vim: set tabstop=4:
1166 * vim: set shiftwidth=4:
1167 * vim: set expandtab:
1171 * indent-tabs-mode: nil