Add support for event tray moved of removable disks
[libvirt-python/ericb.git] / libvirt-override.c
blob42a441e39a4102d45017d4dd41f54aaaeace90b6
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) 2005, 2007-2012 Red Hat, Inc.
9 * Daniel Veillard <veillard@redhat.com>
12 #include <config.h>
14 /* Horrible kludge to work around even more horrible name-space pollution
15 via Python.h. That file includes /usr/include/python2.5/pyconfig*.h,
16 which has over 180 autoconf-style HAVE_* definitions. Shame on them. */
17 #undef HAVE_PTHREAD_H
19 /* We want to see *_LAST enums. */
20 #define VIR_ENUM_SENTINELS
22 #include <Python.h>
23 #include "libvirt/libvirt.h"
24 #include "libvirt/virterror.h"
25 #include "typewrappers.h"
26 #include "libvirt.h"
27 #include "memory.h"
28 #include "virtypedparam.h"
29 #include "ignore-value.h"
30 #include "util.h"
32 #ifndef __CYGWIN__
33 extern void initlibvirtmod(void);
34 #else
35 extern void initcygvirtmod(void);
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 /* We don't want to free() returned value. As written in doc:
58 * PyString_AsString returns pointer to 'internal buffer of string,
59 * not a copy' and 'It must not be deallocated'. */
60 static char *py_str(PyObject *obj)
62 PyObject *str = PyObject_Str(obj);
63 if (!str) {
64 PyErr_Print();
65 PyErr_Clear();
66 return NULL;
68 return PyString_AsString(str);
71 /* Helper function to convert a virTypedParameter output array into a
72 * Python dictionary for return to the user. Return NULL on failure,
73 * after raising a python exception. */
74 static PyObject * ATTRIBUTE_NONNULL(1)
75 getPyVirTypedParameter(const virTypedParameterPtr params, int nparams)
77 PyObject *key, *val, *info;
78 int i;
80 if ((info = PyDict_New()) == NULL)
81 return NULL;
83 for (i = 0 ; i < nparams ; i++) {
84 switch (params[i].type) {
85 case VIR_TYPED_PARAM_INT:
86 val = PyInt_FromLong(params[i].value.i);
87 break;
89 case VIR_TYPED_PARAM_UINT:
90 val = PyInt_FromLong(params[i].value.ui);
91 break;
93 case VIR_TYPED_PARAM_LLONG:
94 val = PyLong_FromLongLong(params[i].value.l);
95 break;
97 case VIR_TYPED_PARAM_ULLONG:
98 val = PyLong_FromUnsignedLongLong(params[i].value.ul);
99 break;
101 case VIR_TYPED_PARAM_DOUBLE:
102 val = PyFloat_FromDouble(params[i].value.d);
103 break;
105 case VIR_TYPED_PARAM_BOOLEAN:
106 val = PyBool_FromLong(params[i].value.b);
107 break;
109 case VIR_TYPED_PARAM_STRING:
110 val = libvirt_constcharPtrWrap(params[i].value.s);
111 break;
113 default:
114 /* Possible if a newer server has a bug and sent stuff we
115 * don't recognize. */
116 PyErr_Format(PyExc_LookupError,
117 "Type value \"%d\" not recognized",
118 params[i].type);
119 val = NULL;
120 break;
123 key = libvirt_constcharPtrWrap(params[i].field);
124 if (!key || !val)
125 goto cleanup;
127 if (PyDict_SetItem(info, key, val) < 0) {
128 Py_DECREF(info);
129 goto cleanup;
132 Py_DECREF(key);
133 Py_DECREF(val);
135 return info;
137 cleanup:
138 Py_XDECREF(key);
139 Py_XDECREF(val);
140 return NULL;
143 /* Allocate a new typed parameter array with the same contents and
144 * length as info, and using the array params of length nparams as
145 * hints on what types to use when creating the new array. The caller
146 * must NOT clear the array before freeing it, as it points into info
147 * rather than allocated strings. Return NULL on failure, after
148 * raising a python exception. */
149 static virTypedParameterPtr ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
150 setPyVirTypedParameter(PyObject *info,
151 const virTypedParameterPtr params, int nparams)
153 PyObject *key, *value;
154 Py_ssize_t pos = 0;
155 virTypedParameterPtr temp = NULL, ret = NULL;
156 Py_ssize_t size;
157 int i;
159 if ((size = PyDict_Size(info)) < 0)
160 return NULL;
162 /* Libvirt APIs use NULL array and 0 size as a special case;
163 * setting should have at least one parameter. */
164 if (size == 0) {
165 PyErr_Format(PyExc_LookupError, "Dictionary must not be empty");
166 return NULL;
169 if (VIR_ALLOC_N(ret, size) < 0) {
170 PyErr_NoMemory();
171 return NULL;
174 temp = &ret[0];
175 while (PyDict_Next(info, &pos, &key, &value)) {
176 char *keystr = NULL;
178 if ((keystr = PyString_AsString(key)) == NULL)
179 goto cleanup;
181 for (i = 0; i < nparams; i++) {
182 if (STREQ(params[i].field, keystr))
183 break;
185 if (i == nparams) {
186 PyErr_Format(PyExc_LookupError,
187 "Attribute name \"%s\" could not be recognized",
188 keystr);
189 goto cleanup;
192 ignore_value(virStrcpyStatic(temp->field, keystr));
193 temp->type = params[i].type;
195 switch(params[i].type) {
196 case VIR_TYPED_PARAM_INT:
198 long long_val = PyInt_AsLong(value);
199 if ((long_val == -1) && PyErr_Occurred())
200 goto cleanup;
201 if ((int)long_val == long_val) {
202 temp->value.i = long_val;
203 } else {
204 PyErr_Format(PyExc_ValueError,
205 "The value of "
206 "attribute \"%s\" is out of int range", keystr);
207 goto cleanup;
210 break;
211 case VIR_TYPED_PARAM_UINT:
213 long long_val = PyInt_AsLong(value);
214 if ((long_val == -1) && PyErr_Occurred())
215 goto cleanup;
216 if ((unsigned int)long_val == long_val) {
217 temp->value.ui = long_val;
218 } else {
219 PyErr_Format(PyExc_ValueError,
220 "The value of "
221 "attribute \"%s\" is out of int range", keystr);
222 goto cleanup;
225 break;
226 case VIR_TYPED_PARAM_LLONG:
228 long long llong_val = PyLong_AsLongLong(value);
229 if ((llong_val == -1) && PyErr_Occurred())
230 goto cleanup;
231 temp->value.l = llong_val;
233 break;
234 case VIR_TYPED_PARAM_ULLONG:
236 unsigned long long ullong_val = PyLong_AsUnsignedLongLong(value);
237 if ((ullong_val == -1) && PyErr_Occurred())
238 goto cleanup;
239 temp->value.ul = ullong_val;
241 break;
242 case VIR_TYPED_PARAM_DOUBLE:
244 double double_val = PyFloat_AsDouble(value);
245 if ((double_val == -1) && PyErr_Occurred())
246 goto cleanup;
247 temp->value.d = double_val;
249 break;
250 case VIR_TYPED_PARAM_BOOLEAN:
252 /* Hack - Python's definition of Py_True breaks strict
253 * aliasing rules, so can't directly compare
255 if (PyBool_Check(value)) {
256 PyObject *hacktrue = PyBool_FromLong(1);
257 temp->value.b = hacktrue == value ? 1 : 0;
258 Py_DECREF(hacktrue);
259 } else {
260 PyErr_Format(PyExc_TypeError,
261 "The value type of "
262 "attribute \"%s\" must be bool", keystr);
263 goto cleanup;
266 break;
267 case VIR_TYPED_PARAM_STRING:
269 char *string_val = PyString_AsString(value);
270 if (!string_val)
271 goto cleanup;
272 temp->value.s = string_val;
273 break;
276 default:
277 /* Possible if a newer server has a bug and sent stuff we
278 * don't recognize. */
279 PyErr_Format(PyExc_LookupError,
280 "Type value \"%d\" not recognized",
281 params[i].type);
282 goto cleanup;
285 temp++;
287 return ret;
289 cleanup:
290 VIR_FREE(ret);
291 return NULL;
294 /************************************************************************
296 * Statistics *
298 ************************************************************************/
300 static PyObject *
301 libvirt_virDomainBlockStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
302 virDomainPtr domain;
303 PyObject *pyobj_domain;
304 char * path;
305 int c_retval;
306 virDomainBlockStatsStruct stats;
307 PyObject *info;
309 if (!PyArg_ParseTuple(args, (char *)"Oz:virDomainBlockStats",
310 &pyobj_domain,&path))
311 return(NULL);
312 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
314 LIBVIRT_BEGIN_ALLOW_THREADS;
315 c_retval = virDomainBlockStats(domain, path, &stats, sizeof(stats));
316 LIBVIRT_END_ALLOW_THREADS;
318 if (c_retval < 0)
319 return VIR_PY_NONE;
321 /* convert to a Python tuple of long objects */
322 if ((info = PyTuple_New(5)) == NULL)
323 return VIR_PY_NONE;
324 PyTuple_SetItem(info, 0, PyLong_FromLongLong(stats.rd_req));
325 PyTuple_SetItem(info, 1, PyLong_FromLongLong(stats.rd_bytes));
326 PyTuple_SetItem(info, 2, PyLong_FromLongLong(stats.wr_req));
327 PyTuple_SetItem(info, 3, PyLong_FromLongLong(stats.wr_bytes));
328 PyTuple_SetItem(info, 4, PyLong_FromLongLong(stats.errs));
329 return(info);
332 static PyObject *
333 libvirt_virDomainBlockStatsFlags(PyObject *self ATTRIBUTE_UNUSED,
334 PyObject *args)
336 virDomainPtr domain;
337 PyObject *pyobj_domain;
338 PyObject *ret = NULL;
339 int i_retval;
340 int nparams = 0;
341 unsigned int flags;
342 virTypedParameterPtr params;
343 const char *path;
345 if (!PyArg_ParseTuple(args, (char *)"Ozi:virDomainBlockStatsFlags",
346 &pyobj_domain, &path, &flags))
347 return NULL;
348 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
350 LIBVIRT_BEGIN_ALLOW_THREADS;
351 i_retval = virDomainBlockStatsFlags(domain, path, NULL, &nparams, flags);
352 LIBVIRT_END_ALLOW_THREADS;
354 if (i_retval < 0)
355 return VIR_PY_NONE;
357 if (!nparams)
358 return PyDict_New();
360 if (VIR_ALLOC_N(params, nparams) < 0)
361 return PyErr_NoMemory();
363 LIBVIRT_BEGIN_ALLOW_THREADS;
364 i_retval = virDomainBlockStatsFlags(domain, path, params, &nparams, flags);
365 LIBVIRT_END_ALLOW_THREADS;
367 if (i_retval < 0) {
368 ret = VIR_PY_NONE;
369 goto cleanup;
372 ret = getPyVirTypedParameter(params, nparams);
374 cleanup:
375 virTypedParameterArrayClear(params, nparams);
376 VIR_FREE(params);
377 return ret;
380 static PyObject *
381 libvirt_virDomainGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
383 virDomainPtr domain;
384 PyObject *pyobj_domain, *totalbool;
385 PyObject *cpu, *total;
386 PyObject *ret = NULL;
387 PyObject *error = NULL;
388 int ncpus = -1, start_cpu = 0;
389 int sumparams = 0, nparams = -1;
390 int i, i_retval;
391 unsigned int flags, totalflag;
392 virTypedParameterPtr params = NULL, cpuparams;
394 if (!PyArg_ParseTuple(args, (char *)"OOi:virDomainGetCPUStats",
395 &pyobj_domain, &totalbool, &flags))
396 return NULL;
397 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
399 if (!PyBool_Check(totalbool)) {
400 PyErr_Format(PyExc_TypeError,
401 "The \"total\" attribute must be bool");
402 return NULL;
403 } else {
404 /* Hack - Python's definition of Py_True breaks strict
405 * aliasing rules, so can't directly compare
407 PyObject *hacktrue = PyBool_FromLong(1);
408 totalflag = hacktrue == totalbool ? 1 : 0;
409 Py_DECREF(hacktrue);
412 if ((ret = PyList_New(0)) == NULL)
413 return NULL;
415 if (!totalflag) {
416 LIBVIRT_BEGIN_ALLOW_THREADS;
417 ncpus = virDomainGetCPUStats(domain, NULL, 0, 0, 0, flags);
418 LIBVIRT_END_ALLOW_THREADS;
420 if (ncpus < 0) {
421 error = VIR_PY_NONE;
422 goto error;
425 LIBVIRT_BEGIN_ALLOW_THREADS;
426 nparams = virDomainGetCPUStats(domain, NULL, 0, 0, 1, flags);
427 LIBVIRT_END_ALLOW_THREADS;
429 if (nparams < 0) {
430 error = VIR_PY_NONE;
431 goto error;
434 sumparams = nparams * MIN(ncpus, 128);
436 if (VIR_ALLOC_N(params, sumparams) < 0) {
437 error = PyErr_NoMemory();
438 goto error;
441 while (ncpus) {
442 int queried_ncpus = MIN(ncpus, 128);
443 if (nparams) {
445 LIBVIRT_BEGIN_ALLOW_THREADS;
446 i_retval = virDomainGetCPUStats(domain, params,
447 nparams, start_cpu, queried_ncpus, flags);
448 LIBVIRT_END_ALLOW_THREADS;
450 if (i_retval < 0) {
451 error = VIR_PY_NONE;
452 goto error;
454 } else {
455 i_retval = 0;
458 for (i = 0; i < queried_ncpus; i++) {
459 cpuparams = &params[i * nparams];
460 if ((cpu = getPyVirTypedParameter(cpuparams, i_retval)) == NULL) {
461 goto error;
464 if (PyList_Append(ret, cpu) < 0) {
465 Py_DECREF(cpu);
466 goto error;
468 Py_DECREF(cpu);
471 start_cpu += queried_ncpus;
472 ncpus -= queried_ncpus;
473 virTypedParameterArrayClear(params, sumparams);
475 } else {
476 LIBVIRT_BEGIN_ALLOW_THREADS;
477 nparams = virDomainGetCPUStats(domain, NULL, 0, -1, 1, flags);
478 LIBVIRT_END_ALLOW_THREADS;
480 if (nparams < 0) {
481 error = VIR_PY_NONE;
482 goto error;
485 if (nparams) {
486 sumparams = nparams;
488 if (VIR_ALLOC_N(params, nparams) < 0) {
489 error = PyErr_NoMemory();
490 goto error;
493 LIBVIRT_BEGIN_ALLOW_THREADS;
494 i_retval = virDomainGetCPUStats(domain, params, nparams, -1, 1, flags);
495 LIBVIRT_END_ALLOW_THREADS;
497 if (i_retval < 0) {
498 error = VIR_PY_NONE;
499 goto error;
501 } else {
502 i_retval = 0;
505 if ((total = getPyVirTypedParameter(params, i_retval)) == NULL) {
506 goto error;
508 if (PyList_Append(ret, total) < 0) {
509 Py_DECREF(total);
510 goto error;
512 Py_DECREF(total);
515 virTypedParameterArrayClear(params, sumparams);
516 VIR_FREE(params);
517 return ret;
519 error:
520 virTypedParameterArrayClear(params, sumparams);
521 VIR_FREE(params);
522 Py_DECREF(ret);
523 return error;
526 static PyObject *
527 libvirt_virDomainInterfaceStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
528 virDomainPtr domain;
529 PyObject *pyobj_domain;
530 char * path;
531 int c_retval;
532 virDomainInterfaceStatsStruct stats;
533 PyObject *info;
535 if (!PyArg_ParseTuple(args, (char *)"Oz:virDomainInterfaceStats",
536 &pyobj_domain,&path))
537 return(NULL);
538 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
540 LIBVIRT_BEGIN_ALLOW_THREADS;
541 c_retval = virDomainInterfaceStats(domain, path, &stats, sizeof(stats));
542 LIBVIRT_END_ALLOW_THREADS;
544 if (c_retval < 0)
545 return VIR_PY_NONE;
547 /* convert to a Python tuple of long objects */
548 if ((info = PyTuple_New(8)) == NULL)
549 return VIR_PY_NONE;
550 PyTuple_SetItem(info, 0, PyLong_FromLongLong(stats.rx_bytes));
551 PyTuple_SetItem(info, 1, PyLong_FromLongLong(stats.rx_packets));
552 PyTuple_SetItem(info, 2, PyLong_FromLongLong(stats.rx_errs));
553 PyTuple_SetItem(info, 3, PyLong_FromLongLong(stats.rx_drop));
554 PyTuple_SetItem(info, 4, PyLong_FromLongLong(stats.tx_bytes));
555 PyTuple_SetItem(info, 5, PyLong_FromLongLong(stats.tx_packets));
556 PyTuple_SetItem(info, 6, PyLong_FromLongLong(stats.tx_errs));
557 PyTuple_SetItem(info, 7, PyLong_FromLongLong(stats.tx_drop));
558 return(info);
561 static PyObject *
562 libvirt_virDomainMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
563 virDomainPtr domain;
564 PyObject *pyobj_domain;
565 unsigned int nr_stats, i;
566 virDomainMemoryStatStruct stats[VIR_DOMAIN_MEMORY_STAT_NR];
567 PyObject *info;
569 if (!PyArg_ParseTuple(args, (char *)"O:virDomainMemoryStats", &pyobj_domain))
570 return(NULL);
571 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
573 nr_stats = virDomainMemoryStats(domain, stats,
574 VIR_DOMAIN_MEMORY_STAT_NR, 0);
575 if (nr_stats == -1)
576 return VIR_PY_NONE;
578 /* convert to a Python dictionary */
579 if ((info = PyDict_New()) == NULL)
580 return VIR_PY_NONE;
582 for (i = 0; i < nr_stats; i++) {
583 if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_SWAP_IN)
584 PyDict_SetItem(info, libvirt_constcharPtrWrap("swap_in"),
585 PyLong_FromUnsignedLongLong(stats[i].val));
586 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_SWAP_OUT)
587 PyDict_SetItem(info, libvirt_constcharPtrWrap("swap_out"),
588 PyLong_FromUnsignedLongLong(stats[i].val));
589 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_MAJOR_FAULT)
590 PyDict_SetItem(info, libvirt_constcharPtrWrap("major_fault"),
591 PyLong_FromUnsignedLongLong(stats[i].val));
592 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_MINOR_FAULT)
593 PyDict_SetItem(info, libvirt_constcharPtrWrap("minor_fault"),
594 PyLong_FromUnsignedLongLong(stats[i].val));
595 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_UNUSED)
596 PyDict_SetItem(info, libvirt_constcharPtrWrap("unused"),
597 PyLong_FromUnsignedLongLong(stats[i].val));
598 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_AVAILABLE)
599 PyDict_SetItem(info, libvirt_constcharPtrWrap("available"),
600 PyLong_FromUnsignedLongLong(stats[i].val));
601 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_ACTUAL_BALLOON)
602 PyDict_SetItem(info, libvirt_constcharPtrWrap("actual"),
603 PyLong_FromUnsignedLongLong(stats[i].val));
604 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_RSS)
605 PyDict_SetItem(info, libvirt_constcharPtrWrap("rss"),
606 PyLong_FromUnsignedLongLong(stats[i].val));
608 return info;
611 static PyObject *
612 libvirt_virDomainGetSchedulerType(PyObject *self ATTRIBUTE_UNUSED,
613 PyObject *args) {
614 virDomainPtr domain;
615 PyObject *pyobj_domain, *info;
616 char *c_retval;
617 int nparams;
619 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetScedulerType",
620 &pyobj_domain))
621 return(NULL);
622 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
624 LIBVIRT_BEGIN_ALLOW_THREADS;
625 c_retval = virDomainGetSchedulerType(domain, &nparams);
626 LIBVIRT_END_ALLOW_THREADS;
627 if (c_retval == NULL)
628 return VIR_PY_NONE;
630 /* convert to a Python tuple of long objects */
631 if ((info = PyTuple_New(2)) == NULL) {
632 VIR_FREE(c_retval);
633 return VIR_PY_NONE;
636 PyTuple_SetItem(info, 0, libvirt_constcharPtrWrap(c_retval));
637 PyTuple_SetItem(info, 1, PyInt_FromLong((long)nparams));
638 VIR_FREE(c_retval);
639 return(info);
642 static PyObject *
643 libvirt_virDomainGetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
644 PyObject *args)
646 virDomainPtr domain;
647 PyObject *pyobj_domain;
648 PyObject *ret = NULL;
649 char *c_retval;
650 int i_retval;
651 int nparams = 0;
652 virTypedParameterPtr params;
654 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetScedulerParameters",
655 &pyobj_domain))
656 return NULL;
657 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
659 LIBVIRT_BEGIN_ALLOW_THREADS;
660 c_retval = virDomainGetSchedulerType(domain, &nparams);
661 LIBVIRT_END_ALLOW_THREADS;
663 if (c_retval == NULL)
664 return VIR_PY_NONE;
665 VIR_FREE(c_retval);
667 if (!nparams)
668 return PyDict_New();
670 if (VIR_ALLOC_N(params, nparams) < 0)
671 return PyErr_NoMemory();
673 LIBVIRT_BEGIN_ALLOW_THREADS;
674 i_retval = virDomainGetSchedulerParameters(domain, params, &nparams);
675 LIBVIRT_END_ALLOW_THREADS;
677 if (i_retval < 0) {
678 ret = VIR_PY_NONE;
679 goto cleanup;
682 ret = getPyVirTypedParameter(params, nparams);
684 cleanup:
685 virTypedParameterArrayClear(params, nparams);
686 VIR_FREE(params);
687 return ret;
690 static PyObject *
691 libvirt_virDomainGetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
692 PyObject *args)
694 virDomainPtr domain;
695 PyObject *pyobj_domain;
696 PyObject *ret = NULL;
697 char *c_retval;
698 int i_retval;
699 int nparams = 0;
700 unsigned int flags;
701 virTypedParameterPtr params;
703 if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetScedulerParametersFlags",
704 &pyobj_domain, &flags))
705 return NULL;
706 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
708 LIBVIRT_BEGIN_ALLOW_THREADS;
709 c_retval = virDomainGetSchedulerType(domain, &nparams);
710 LIBVIRT_END_ALLOW_THREADS;
712 if (c_retval == NULL)
713 return VIR_PY_NONE;
714 VIR_FREE(c_retval);
716 if (!nparams)
717 return PyDict_New();
719 if (VIR_ALLOC_N(params, nparams) < 0)
720 return PyErr_NoMemory();
722 LIBVIRT_BEGIN_ALLOW_THREADS;
723 i_retval = virDomainGetSchedulerParametersFlags(domain, params, &nparams, flags);
724 LIBVIRT_END_ALLOW_THREADS;
726 if (i_retval < 0) {
727 ret = VIR_PY_NONE;
728 goto cleanup;
731 ret = getPyVirTypedParameter(params, nparams);
733 cleanup:
734 virTypedParameterArrayClear(params, nparams);
735 VIR_FREE(params);
736 return ret;
739 static PyObject *
740 libvirt_virDomainSetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
741 PyObject *args)
743 virDomainPtr domain;
744 PyObject *pyobj_domain, *info;
745 PyObject *ret = NULL;
746 char *c_retval;
747 int i_retval;
748 int nparams = 0;
749 Py_ssize_t size = 0;
750 virTypedParameterPtr params, new_params;
752 if (!PyArg_ParseTuple(args, (char *)"OO:virDomainSetScedulerParameters",
753 &pyobj_domain, &info))
754 return NULL;
755 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
757 if ((size = PyDict_Size(info)) < 0)
758 return NULL;
760 if (size == 0) {
761 PyErr_Format(PyExc_LookupError,
762 "Need non-empty dictionary to set attributes");
763 return NULL;
766 LIBVIRT_BEGIN_ALLOW_THREADS;
767 c_retval = virDomainGetSchedulerType(domain, &nparams);
768 LIBVIRT_END_ALLOW_THREADS;
770 if (c_retval == NULL)
771 return VIR_PY_INT_FAIL;
772 VIR_FREE(c_retval);
774 if (nparams == 0) {
775 PyErr_Format(PyExc_LookupError,
776 "Domain has no settable attributes");
777 return NULL;
780 if (VIR_ALLOC_N(params, nparams) < 0)
781 return PyErr_NoMemory();
783 LIBVIRT_BEGIN_ALLOW_THREADS;
784 i_retval = virDomainGetSchedulerParameters(domain, params, &nparams);
785 LIBVIRT_END_ALLOW_THREADS;
787 if (i_retval < 0) {
788 ret = VIR_PY_INT_FAIL;
789 goto cleanup;
792 new_params = setPyVirTypedParameter(info, params, nparams);
793 if (!new_params)
794 goto cleanup;
796 LIBVIRT_BEGIN_ALLOW_THREADS;
797 i_retval = virDomainSetSchedulerParameters(domain, new_params, size);
798 LIBVIRT_END_ALLOW_THREADS;
800 if (i_retval < 0) {
801 ret = VIR_PY_INT_FAIL;
802 goto cleanup;
805 ret = VIR_PY_INT_SUCCESS;
807 cleanup:
808 virTypedParameterArrayClear(params, nparams);
809 VIR_FREE(params);
810 VIR_FREE(new_params);
811 return ret;
814 static PyObject *
815 libvirt_virDomainSetSchedulerParametersFlags(PyObject *self ATTRIBUTE_UNUSED,
816 PyObject *args)
818 virDomainPtr domain;
819 PyObject *pyobj_domain, *info;
820 PyObject *ret = NULL;
821 char *c_retval;
822 int i_retval;
823 int nparams = 0;
824 Py_ssize_t size = 0;
825 unsigned int flags;
826 virTypedParameterPtr params, new_params;
828 if (!PyArg_ParseTuple(args,
829 (char *)"OOi:virDomainSetScedulerParametersFlags",
830 &pyobj_domain, &info, &flags))
831 return NULL;
832 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
834 if ((size = PyDict_Size(info)) < 0)
835 return NULL;
837 if (size == 0) {
838 PyErr_Format(PyExc_LookupError,
839 "Need non-empty dictionary to set attributes");
840 return NULL;
843 LIBVIRT_BEGIN_ALLOW_THREADS;
844 c_retval = virDomainGetSchedulerType(domain, &nparams);
845 LIBVIRT_END_ALLOW_THREADS;
847 if (c_retval == NULL)
848 return VIR_PY_INT_FAIL;
849 VIR_FREE(c_retval);
851 if (nparams == 0) {
852 PyErr_Format(PyExc_LookupError,
853 "Domain has no settable attributes");
854 return NULL;
857 if (VIR_ALLOC_N(params, nparams) < 0)
858 return PyErr_NoMemory();
860 LIBVIRT_BEGIN_ALLOW_THREADS;
861 i_retval = virDomainGetSchedulerParametersFlags(domain, params, &nparams, flags);
862 LIBVIRT_END_ALLOW_THREADS;
864 if (i_retval < 0) {
865 ret = VIR_PY_INT_FAIL;
866 goto cleanup;
869 new_params = setPyVirTypedParameter(info, params, nparams);
870 if (!new_params)
871 goto cleanup;
873 LIBVIRT_BEGIN_ALLOW_THREADS;
874 i_retval = virDomainSetSchedulerParametersFlags(domain, new_params, size, flags);
875 LIBVIRT_END_ALLOW_THREADS;
877 if (i_retval < 0) {
878 ret = VIR_PY_INT_FAIL;
879 goto cleanup;
882 ret = VIR_PY_INT_SUCCESS;
884 cleanup:
885 virTypedParameterArrayClear(params, nparams);
886 VIR_FREE(params);
887 VIR_FREE(new_params);
888 return ret;
891 static PyObject *
892 libvirt_virDomainSetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
893 PyObject *args)
895 virDomainPtr domain;
896 PyObject *pyobj_domain, *info;
897 PyObject *ret = NULL;
898 int i_retval;
899 int nparams = 0;
900 Py_ssize_t size = 0;
901 unsigned int flags;
902 virTypedParameterPtr params, new_params;
904 if (!PyArg_ParseTuple(args,
905 (char *)"OOi:virDomainSetBlkioParameters",
906 &pyobj_domain, &info, &flags))
907 return NULL;
908 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
910 if ((size = PyDict_Size(info)) < 0)
911 return NULL;
913 if (size == 0) {
914 PyErr_Format(PyExc_LookupError,
915 "Need non-empty dictionary to set attributes");
916 return NULL;
919 LIBVIRT_BEGIN_ALLOW_THREADS;
920 i_retval = virDomainGetBlkioParameters(domain, NULL, &nparams, flags);
921 LIBVIRT_END_ALLOW_THREADS;
923 if (i_retval < 0)
924 return VIR_PY_INT_FAIL;
926 if (nparams == 0) {
927 PyErr_Format(PyExc_LookupError,
928 "Domain has no settable attributes");
929 return NULL;
932 if (VIR_ALLOC_N(params, nparams) < 0)
933 return PyErr_NoMemory();
935 LIBVIRT_BEGIN_ALLOW_THREADS;
936 i_retval = virDomainGetBlkioParameters(domain, params, &nparams, flags);
937 LIBVIRT_END_ALLOW_THREADS;
939 if (i_retval < 0) {
940 ret = VIR_PY_INT_FAIL;
941 goto cleanup;
944 new_params = setPyVirTypedParameter(info, params, nparams);
945 if (!new_params)
946 goto cleanup;
948 LIBVIRT_BEGIN_ALLOW_THREADS;
949 i_retval = virDomainSetBlkioParameters(domain, new_params, size, flags);
950 LIBVIRT_END_ALLOW_THREADS;
952 if (i_retval < 0) {
953 ret = VIR_PY_INT_FAIL;
954 goto cleanup;
957 ret = VIR_PY_INT_SUCCESS;
959 cleanup:
960 virTypedParameterArrayClear(params, nparams);
961 VIR_FREE(params);
962 VIR_FREE(new_params);
963 return ret;
966 static PyObject *
967 libvirt_virDomainGetBlkioParameters(PyObject *self ATTRIBUTE_UNUSED,
968 PyObject *args)
970 virDomainPtr domain;
971 PyObject *pyobj_domain;
972 PyObject *ret = NULL;
973 int i_retval;
974 int nparams = 0;
975 unsigned int flags;
976 virTypedParameterPtr params;
978 if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetBlkioParameters",
979 &pyobj_domain, &flags))
980 return NULL;
981 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
983 LIBVIRT_BEGIN_ALLOW_THREADS;
984 i_retval = virDomainGetBlkioParameters(domain, NULL, &nparams, flags);
985 LIBVIRT_END_ALLOW_THREADS;
987 if (i_retval < 0)
988 return VIR_PY_NONE;
990 if (!nparams)
991 return PyDict_New();
993 if (VIR_ALLOC_N(params, nparams) < 0)
994 return PyErr_NoMemory();
996 LIBVIRT_BEGIN_ALLOW_THREADS;
997 i_retval = virDomainGetBlkioParameters(domain, params, &nparams, flags);
998 LIBVIRT_END_ALLOW_THREADS;
1000 if (i_retval < 0) {
1001 ret = VIR_PY_NONE;
1002 goto cleanup;
1005 ret = getPyVirTypedParameter(params, nparams);
1007 cleanup:
1008 virTypedParameterArrayClear(params, nparams);
1009 VIR_FREE(params);
1010 return ret;
1013 static PyObject *
1014 libvirt_virDomainSetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
1015 PyObject *args)
1017 virDomainPtr domain;
1018 PyObject *pyobj_domain, *info;
1019 PyObject *ret = NULL;
1020 int i_retval;
1021 int nparams = 0;
1022 Py_ssize_t size = 0;
1023 unsigned int flags;
1024 virTypedParameterPtr params, new_params;
1026 if (!PyArg_ParseTuple(args,
1027 (char *)"OOi:virDomainSetMemoryParameters",
1028 &pyobj_domain, &info, &flags))
1029 return NULL;
1030 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1032 if ((size = PyDict_Size(info)) < 0)
1033 return NULL;
1035 if (size == 0) {
1036 PyErr_Format(PyExc_LookupError,
1037 "Need non-empty dictionary to set attributes");
1038 return NULL;
1041 LIBVIRT_BEGIN_ALLOW_THREADS;
1042 i_retval = virDomainGetMemoryParameters(domain, NULL, &nparams, flags);
1043 LIBVIRT_END_ALLOW_THREADS;
1045 if (i_retval < 0)
1046 return VIR_PY_INT_FAIL;
1048 if (nparams == 0) {
1049 PyErr_Format(PyExc_LookupError,
1050 "Domain has no settable attributes");
1051 return NULL;
1054 if (VIR_ALLOC_N(params, nparams) < 0)
1055 return PyErr_NoMemory();
1057 LIBVIRT_BEGIN_ALLOW_THREADS;
1058 i_retval = virDomainGetMemoryParameters(domain, params, &nparams, flags);
1059 LIBVIRT_END_ALLOW_THREADS;
1061 if (i_retval < 0) {
1062 ret = VIR_PY_INT_FAIL;
1063 goto cleanup;
1066 new_params = setPyVirTypedParameter(info, params, nparams);
1067 if (!new_params)
1068 goto cleanup;
1070 LIBVIRT_BEGIN_ALLOW_THREADS;
1071 i_retval = virDomainSetMemoryParameters(domain, new_params, size, flags);
1072 LIBVIRT_END_ALLOW_THREADS;
1074 if (i_retval < 0) {
1075 ret = VIR_PY_INT_FAIL;
1076 goto cleanup;
1079 ret = VIR_PY_INT_SUCCESS;
1081 cleanup:
1082 virTypedParameterArrayClear(params, nparams);
1083 VIR_FREE(params);
1084 VIR_FREE(new_params);
1085 return ret;
1088 static PyObject *
1089 libvirt_virDomainGetMemoryParameters(PyObject *self ATTRIBUTE_UNUSED,
1090 PyObject *args)
1092 virDomainPtr domain;
1093 PyObject *pyobj_domain;
1094 PyObject *ret = NULL;
1095 int i_retval;
1096 int nparams = 0;
1097 unsigned int flags;
1098 virTypedParameterPtr params;
1100 if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetMemoryParameters",
1101 &pyobj_domain, &flags))
1102 return NULL;
1103 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1105 LIBVIRT_BEGIN_ALLOW_THREADS;
1106 i_retval = virDomainGetMemoryParameters(domain, NULL, &nparams, flags);
1107 LIBVIRT_END_ALLOW_THREADS;
1109 if (i_retval < 0)
1110 return VIR_PY_NONE;
1112 if (!nparams)
1113 return PyDict_New();
1115 if (VIR_ALLOC_N(params, nparams) < 0)
1116 return PyErr_NoMemory();
1118 LIBVIRT_BEGIN_ALLOW_THREADS;
1119 i_retval = virDomainGetMemoryParameters(domain, params, &nparams, flags);
1120 LIBVIRT_END_ALLOW_THREADS;
1122 if (i_retval < 0) {
1123 ret = VIR_PY_NONE;
1124 goto cleanup;
1127 ret = getPyVirTypedParameter(params, nparams);
1129 cleanup:
1130 virTypedParameterArrayClear(params, nparams);
1131 VIR_FREE(params);
1132 return ret;
1135 static PyObject *
1136 libvirt_virDomainSetNumaParameters(PyObject *self ATTRIBUTE_UNUSED,
1137 PyObject *args)
1139 virDomainPtr domain;
1140 PyObject *pyobj_domain, *info;
1141 PyObject *ret = NULL;
1142 int i_retval;
1143 int nparams = 0;
1144 Py_ssize_t size = 0;
1145 unsigned int flags;
1146 virTypedParameterPtr params, new_params;
1148 if (!PyArg_ParseTuple(args,
1149 (char *)"OOi:virDomainSetNumaParameters",
1150 &pyobj_domain, &info, &flags))
1151 return NULL;
1152 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1154 if ((size = PyDict_Size(info)) < 0)
1155 return NULL;
1157 if (size == 0) {
1158 PyErr_Format(PyExc_LookupError,
1159 "Need non-empty dictionary to set attributes");
1160 return NULL;
1163 LIBVIRT_BEGIN_ALLOW_THREADS;
1164 i_retval = virDomainGetNumaParameters(domain, NULL, &nparams, flags);
1165 LIBVIRT_END_ALLOW_THREADS;
1167 if (i_retval < 0)
1168 return VIR_PY_INT_FAIL;
1170 if (nparams == 0) {
1171 PyErr_Format(PyExc_LookupError,
1172 "Domain has no settable attributes");
1173 return NULL;
1176 if (VIR_ALLOC_N(params, nparams) < 0)
1177 return PyErr_NoMemory();
1179 LIBVIRT_BEGIN_ALLOW_THREADS;
1180 i_retval = virDomainGetNumaParameters(domain, params, &nparams, flags);
1181 LIBVIRT_END_ALLOW_THREADS;
1183 if (i_retval < 0) {
1184 ret = VIR_PY_INT_FAIL;
1185 goto cleanup;
1188 new_params = setPyVirTypedParameter(info, params, nparams);
1189 if (!new_params)
1190 goto cleanup;
1192 LIBVIRT_BEGIN_ALLOW_THREADS;
1193 i_retval = virDomainSetNumaParameters(domain, new_params, size, flags);
1194 LIBVIRT_END_ALLOW_THREADS;
1196 if (i_retval < 0) {
1197 ret = VIR_PY_INT_FAIL;
1198 goto cleanup;
1201 ret = VIR_PY_INT_SUCCESS;
1203 cleanup:
1204 virTypedParameterArrayClear(params, nparams);
1205 VIR_FREE(params);
1206 VIR_FREE(new_params);
1207 return ret;
1210 static PyObject *
1211 libvirt_virDomainGetNumaParameters(PyObject *self ATTRIBUTE_UNUSED,
1212 PyObject *args)
1214 virDomainPtr domain;
1215 PyObject *pyobj_domain;
1216 PyObject *ret = NULL;
1217 int i_retval;
1218 int nparams = 0;
1219 unsigned int flags;
1220 virTypedParameterPtr params;
1222 if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetNumaParameters",
1223 &pyobj_domain, &flags))
1224 return NULL;
1225 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1227 LIBVIRT_BEGIN_ALLOW_THREADS;
1228 i_retval = virDomainGetNumaParameters(domain, NULL, &nparams, flags);
1229 LIBVIRT_END_ALLOW_THREADS;
1231 if (i_retval < 0)
1232 return VIR_PY_NONE;
1234 if (!nparams)
1235 return PyDict_New();
1237 if (VIR_ALLOC_N(params, nparams) < 0)
1238 return PyErr_NoMemory();
1240 LIBVIRT_BEGIN_ALLOW_THREADS;
1241 i_retval = virDomainGetNumaParameters(domain, params, &nparams, flags);
1242 LIBVIRT_END_ALLOW_THREADS;
1244 if (i_retval < 0) {
1245 ret = VIR_PY_NONE;
1246 goto cleanup;
1249 ret = getPyVirTypedParameter(params, nparams);
1251 cleanup:
1252 virTypedParameterArrayClear(params, nparams);
1253 VIR_FREE(params);
1254 return ret;
1257 static PyObject *
1258 libvirt_virDomainSetInterfaceParameters(PyObject *self ATTRIBUTE_UNUSED,
1259 PyObject *args)
1261 virDomainPtr domain;
1262 PyObject *pyobj_domain, *info;
1263 PyObject *ret = NULL;
1264 int i_retval;
1265 int nparams = 0;
1266 Py_ssize_t size = 0;
1267 unsigned int flags;
1268 const char *device = NULL;
1269 virTypedParameterPtr params, new_params;
1271 if (!PyArg_ParseTuple(args,
1272 (char *)"OzOi:virDomainSetInterfaceParameters",
1273 &pyobj_domain, &device, &info, &flags))
1274 return NULL;
1275 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1277 if ((size = PyDict_Size(info)) < 0)
1278 return NULL;
1280 if (size == 0) {
1281 PyErr_Format(PyExc_LookupError,
1282 "Need non-empty dictionary to set attributes");
1283 return NULL;
1286 LIBVIRT_BEGIN_ALLOW_THREADS;
1287 i_retval = virDomainGetInterfaceParameters(domain, device, NULL, &nparams, flags);
1288 LIBVIRT_END_ALLOW_THREADS;
1290 if (i_retval < 0)
1291 return VIR_PY_INT_FAIL;
1293 if (nparams == 0) {
1294 PyErr_Format(PyExc_LookupError,
1295 "Domain has no settable attributes");
1296 return NULL;
1299 if (VIR_ALLOC_N(params, nparams) < 0)
1300 return PyErr_NoMemory();
1302 LIBVIRT_BEGIN_ALLOW_THREADS;
1303 i_retval = virDomainGetInterfaceParameters(domain, device, params, &nparams, flags);
1304 LIBVIRT_END_ALLOW_THREADS;
1306 if (i_retval < 0) {
1307 ret = VIR_PY_INT_FAIL;
1308 goto cleanup;
1311 new_params = setPyVirTypedParameter(info, params, nparams);
1312 if (!new_params)
1313 goto cleanup;
1315 LIBVIRT_BEGIN_ALLOW_THREADS;
1316 i_retval = virDomainSetInterfaceParameters(domain, device, new_params, size, flags);
1317 LIBVIRT_END_ALLOW_THREADS;
1319 if (i_retval < 0) {
1320 ret = VIR_PY_INT_FAIL;
1321 goto cleanup;
1324 ret = VIR_PY_INT_SUCCESS;
1326 cleanup:
1327 virTypedParameterArrayClear(params, nparams);
1328 VIR_FREE(params);
1329 VIR_FREE(new_params);
1330 return ret;
1333 static PyObject *
1334 libvirt_virDomainGetInterfaceParameters(PyObject *self ATTRIBUTE_UNUSED,
1335 PyObject *args)
1337 virDomainPtr domain;
1338 PyObject *pyobj_domain;
1339 PyObject *ret = NULL;
1340 int i_retval;
1341 int nparams = 0;
1342 unsigned int flags;
1343 const char *device = NULL;
1344 virTypedParameterPtr params;
1346 if (!PyArg_ParseTuple(args, (char *)"Ozi:virDomainGetInterfaceParameters",
1347 &pyobj_domain, &device, &flags))
1348 return NULL;
1349 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1351 LIBVIRT_BEGIN_ALLOW_THREADS;
1352 i_retval = virDomainGetInterfaceParameters(domain, device, NULL, &nparams, flags);
1353 LIBVIRT_END_ALLOW_THREADS;
1355 if (i_retval < 0)
1356 return VIR_PY_NONE;
1358 if (!nparams)
1359 return PyDict_New();
1361 if (VIR_ALLOC_N(params, nparams) < 0)
1362 return PyErr_NoMemory();
1364 LIBVIRT_BEGIN_ALLOW_THREADS;
1365 i_retval = virDomainGetInterfaceParameters(domain, device, params, &nparams, flags);
1366 LIBVIRT_END_ALLOW_THREADS;
1368 if (i_retval < 0) {
1369 ret = VIR_PY_NONE;
1370 goto cleanup;
1373 ret = getPyVirTypedParameter(params, nparams);
1375 cleanup:
1376 virTypedParameterArrayClear(params, nparams);
1377 VIR_FREE(params);
1378 return ret;
1381 static PyObject *
1382 libvirt_virDomainGetVcpus(PyObject *self ATTRIBUTE_UNUSED,
1383 PyObject *args) {
1384 virDomainPtr domain;
1385 PyObject *pyobj_domain, *pyretval = NULL, *pycpuinfo = NULL, *pycpumap = NULL;
1386 virNodeInfo nodeinfo;
1387 virDomainInfo dominfo;
1388 virVcpuInfoPtr cpuinfo = NULL;
1389 unsigned char *cpumap = NULL;
1390 size_t cpumaplen, i;
1391 int i_retval;
1393 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetVcpus",
1394 &pyobj_domain))
1395 return(NULL);
1396 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1398 LIBVIRT_BEGIN_ALLOW_THREADS;
1399 i_retval = virNodeGetInfo(virDomainGetConnect(domain), &nodeinfo);
1400 LIBVIRT_END_ALLOW_THREADS;
1401 if (i_retval < 0)
1402 return VIR_PY_NONE;
1404 LIBVIRT_BEGIN_ALLOW_THREADS;
1405 i_retval = virDomainGetInfo(domain, &dominfo);
1406 LIBVIRT_END_ALLOW_THREADS;
1407 if (i_retval < 0)
1408 return VIR_PY_NONE;
1410 if (VIR_ALLOC_N(cpuinfo, dominfo.nrVirtCpu) < 0)
1411 return VIR_PY_NONE;
1413 cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
1414 if (xalloc_oversized(dominfo.nrVirtCpu, cpumaplen) ||
1415 VIR_ALLOC_N(cpumap, dominfo.nrVirtCpu * cpumaplen) < 0)
1416 goto cleanup;
1418 LIBVIRT_BEGIN_ALLOW_THREADS;
1419 i_retval = virDomainGetVcpus(domain,
1420 cpuinfo, dominfo.nrVirtCpu,
1421 cpumap, cpumaplen);
1422 LIBVIRT_END_ALLOW_THREADS;
1423 if (i_retval < 0)
1424 goto cleanup;
1426 /* convert to a Python tuple of long objects */
1427 if ((pyretval = PyTuple_New(2)) == NULL)
1428 goto cleanup;
1429 if ((pycpuinfo = PyList_New(dominfo.nrVirtCpu)) == NULL)
1430 goto cleanup;
1431 if ((pycpumap = PyList_New(dominfo.nrVirtCpu)) == NULL)
1432 goto cleanup;
1434 for (i = 0 ; i < dominfo.nrVirtCpu ; i++) {
1435 PyObject *info = PyTuple_New(4);
1436 if (info == NULL)
1437 goto cleanup;
1438 PyTuple_SetItem(info, 0, PyInt_FromLong((long)cpuinfo[i].number));
1439 PyTuple_SetItem(info, 1, PyInt_FromLong((long)cpuinfo[i].state));
1440 PyTuple_SetItem(info, 2, PyLong_FromLongLong((long long)cpuinfo[i].cpuTime));
1441 PyTuple_SetItem(info, 3, PyInt_FromLong((long)cpuinfo[i].cpu));
1442 PyList_SetItem(pycpuinfo, i, info);
1444 for (i = 0 ; i < dominfo.nrVirtCpu ; i++) {
1445 PyObject *info = PyTuple_New(VIR_NODEINFO_MAXCPUS(nodeinfo));
1446 int j;
1447 if (info == NULL)
1448 goto cleanup;
1449 for (j = 0 ; j < VIR_NODEINFO_MAXCPUS(nodeinfo) ; j++) {
1450 PyTuple_SetItem(info, j, PyBool_FromLong(VIR_CPU_USABLE(cpumap, cpumaplen, i, j)));
1452 PyList_SetItem(pycpumap, i, info);
1454 PyTuple_SetItem(pyretval, 0, pycpuinfo);
1455 PyTuple_SetItem(pyretval, 1, pycpumap);
1457 VIR_FREE(cpuinfo);
1458 VIR_FREE(cpumap);
1460 return(pyretval);
1462 cleanup:
1463 VIR_FREE(cpuinfo);
1464 VIR_FREE(cpumap);
1465 /* NB, Py_DECREF is a badly defined macro, so we require
1466 * braces here to avoid 'ambiguous else' warnings from
1467 * the compiler.
1468 * NB. this comment is true at of time of writing wrt to
1469 * at least python2.5.
1471 if (pyretval) { Py_DECREF(pyretval); }
1472 if (pycpuinfo) { Py_DECREF(pycpuinfo); }
1473 if (pycpumap) { Py_DECREF(pycpumap); }
1474 return VIR_PY_NONE;
1478 static PyObject *
1479 libvirt_virDomainPinVcpu(PyObject *self ATTRIBUTE_UNUSED,
1480 PyObject *args) {
1481 virDomainPtr domain;
1482 PyObject *pyobj_domain, *pycpumap, *truth;
1483 virNodeInfo nodeinfo;
1484 unsigned char *cpumap;
1485 int cpumaplen, i, vcpu;
1486 int i_retval;
1488 if (!PyArg_ParseTuple(args, (char *)"OiO:virDomainPinVcpu",
1489 &pyobj_domain, &vcpu, &pycpumap))
1490 return(NULL);
1491 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1493 LIBVIRT_BEGIN_ALLOW_THREADS;
1494 i_retval = virNodeGetInfo(virDomainGetConnect(domain), &nodeinfo);
1495 LIBVIRT_END_ALLOW_THREADS;
1496 if (i_retval < 0)
1497 return VIR_PY_INT_FAIL;
1499 cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
1500 if (VIR_ALLOC_N(cpumap, cpumaplen) < 0)
1501 return VIR_PY_INT_FAIL;
1503 truth = PyBool_FromLong(1);
1504 for (i = 0 ; i < VIR_NODEINFO_MAXCPUS(nodeinfo) ; i++) {
1505 PyObject *flag = PyTuple_GetItem(pycpumap, i);
1506 if (flag == truth)
1507 VIR_USE_CPU(cpumap, i);
1508 else
1509 VIR_UNUSE_CPU(cpumap, i);
1512 LIBVIRT_BEGIN_ALLOW_THREADS;
1513 i_retval = virDomainPinVcpu(domain, vcpu, cpumap, cpumaplen);
1514 LIBVIRT_END_ALLOW_THREADS;
1515 Py_DECREF(truth);
1516 VIR_FREE(cpumap);
1518 if (i_retval < 0)
1519 return VIR_PY_INT_FAIL;
1521 return VIR_PY_INT_SUCCESS;
1524 static PyObject *
1525 libvirt_virDomainPinVcpuFlags(PyObject *self ATTRIBUTE_UNUSED,
1526 PyObject *args) {
1527 virDomainPtr domain;
1528 PyObject *pyobj_domain, *pycpumap, *truth;
1529 virNodeInfo nodeinfo;
1530 unsigned char *cpumap;
1531 int cpumaplen, i, vcpu;
1532 unsigned int flags;
1533 int i_retval;
1535 if (!PyArg_ParseTuple(args, (char *)"OiOi:virDomainPinVcpuFlags",
1536 &pyobj_domain, &vcpu, &pycpumap, &flags))
1537 return(NULL);
1538 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1540 LIBVIRT_BEGIN_ALLOW_THREADS;
1541 i_retval = virNodeGetInfo(virDomainGetConnect(domain), &nodeinfo);
1542 LIBVIRT_END_ALLOW_THREADS;
1543 if (i_retval < 0)
1544 return VIR_PY_INT_FAIL;
1546 cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
1547 if (VIR_ALLOC_N(cpumap, cpumaplen) < 0)
1548 return VIR_PY_INT_FAIL;
1550 truth = PyBool_FromLong(1);
1551 for (i = 0 ; i < VIR_NODEINFO_MAXCPUS(nodeinfo) ; i++) {
1552 PyObject *flag = PyTuple_GetItem(pycpumap, i);
1553 if (flag == truth)
1554 VIR_USE_CPU(cpumap, i);
1555 else
1556 VIR_UNUSE_CPU(cpumap, i);
1559 LIBVIRT_BEGIN_ALLOW_THREADS;
1560 i_retval = virDomainPinVcpuFlags(domain, vcpu, cpumap, cpumaplen, flags);
1561 LIBVIRT_END_ALLOW_THREADS;
1562 Py_DECREF(truth);
1563 VIR_FREE(cpumap);
1565 if (i_retval < 0)
1566 return VIR_PY_INT_FAIL;
1568 return VIR_PY_INT_SUCCESS;
1571 static PyObject *
1572 libvirt_virDomainGetVcpuPinInfo(PyObject *self ATTRIBUTE_UNUSED,
1573 PyObject *args) {
1574 virDomainPtr domain;
1575 PyObject *pyobj_domain, *pycpumaps = NULL;
1576 virNodeInfo nodeinfo;
1577 virDomainInfo dominfo;
1578 unsigned char *cpumaps;
1579 size_t cpumaplen, vcpu, pcpu;
1580 unsigned int flags;
1581 int i_retval;
1583 if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetVcpuPinInfo",
1584 &pyobj_domain, &flags))
1585 return(NULL);
1586 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1588 LIBVIRT_BEGIN_ALLOW_THREADS;
1589 i_retval = virNodeGetInfo(virDomainGetConnect(domain), &nodeinfo);
1590 LIBVIRT_END_ALLOW_THREADS;
1591 if (i_retval < 0)
1592 return VIR_PY_NONE;
1594 LIBVIRT_BEGIN_ALLOW_THREADS;
1595 i_retval = virDomainGetInfo(domain, &dominfo);
1596 LIBVIRT_END_ALLOW_THREADS;
1597 if (i_retval < 0)
1598 return VIR_PY_NONE;
1600 cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
1601 if (xalloc_oversized(dominfo.nrVirtCpu, cpumaplen) ||
1602 VIR_ALLOC_N(cpumaps, dominfo.nrVirtCpu * cpumaplen) < 0)
1603 goto cleanup;
1605 LIBVIRT_BEGIN_ALLOW_THREADS;
1606 i_retval = virDomainGetVcpuPinInfo(domain, dominfo.nrVirtCpu,
1607 cpumaps, cpumaplen, flags);
1608 LIBVIRT_END_ALLOW_THREADS;
1609 if (i_retval < 0)
1610 goto cleanup;
1612 if ((pycpumaps = PyList_New(dominfo.nrVirtCpu)) == NULL)
1613 goto cleanup;
1615 for (vcpu = 0; vcpu < dominfo.nrVirtCpu; vcpu++) {
1616 PyObject *mapinfo = PyTuple_New(VIR_NODEINFO_MAXCPUS(nodeinfo));
1617 if (mapinfo == NULL)
1618 goto cleanup;
1620 for (pcpu = 0; pcpu < VIR_NODEINFO_MAXCPUS(nodeinfo); pcpu++) {
1621 PyTuple_SetItem(mapinfo, pcpu,
1622 PyBool_FromLong(VIR_CPU_USABLE(cpumaps, cpumaplen, vcpu, pcpu)));
1624 PyList_SetItem(pycpumaps, vcpu, mapinfo);
1627 VIR_FREE(cpumaps);
1629 return pycpumaps;
1631 cleanup:
1632 VIR_FREE(cpumaps);
1634 if (pycpumaps) { Py_DECREF(pycpumaps);}
1636 return VIR_PY_NONE;
1639 /************************************************************************
1641 * Global error handler at the Python level *
1643 ************************************************************************/
1645 static PyObject *libvirt_virPythonErrorFuncHandler = NULL;
1646 static PyObject *libvirt_virPythonErrorFuncCtxt = NULL;
1648 static PyObject *
1649 libvirt_virGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args ATTRIBUTE_UNUSED)
1651 virError *err;
1652 PyObject *info;
1654 if ((err = virGetLastError()) == NULL)
1655 return VIR_PY_NONE;
1657 if ((info = PyTuple_New(9)) == NULL)
1658 return VIR_PY_NONE;
1659 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err->code));
1660 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err->domain));
1661 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err->message));
1662 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err->level));
1663 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err->str1));
1664 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err->str2));
1665 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err->str3));
1666 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err->int1));
1667 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err->int2));
1669 return info;
1672 static PyObject *
1673 libvirt_virConnGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
1675 virError *err;
1676 PyObject *info;
1677 virConnectPtr conn;
1678 PyObject *pyobj_conn;
1680 if (!PyArg_ParseTuple(args, (char *)"O:virConGetLastError", &pyobj_conn))
1681 return(NULL);
1682 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1684 LIBVIRT_BEGIN_ALLOW_THREADS;
1685 err = virConnGetLastError(conn);
1686 LIBVIRT_END_ALLOW_THREADS;
1687 if (err == NULL)
1688 return VIR_PY_NONE;
1690 if ((info = PyTuple_New(9)) == NULL)
1691 return VIR_PY_NONE;
1692 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err->code));
1693 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err->domain));
1694 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err->message));
1695 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err->level));
1696 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err->str1));
1697 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err->str2));
1698 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err->str3));
1699 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err->int1));
1700 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err->int2));
1702 return info;
1705 static void
1706 libvirt_virErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, virErrorPtr err)
1708 PyObject *list, *info;
1709 PyObject *result;
1711 DEBUG("libvirt_virErrorFuncHandler(%p, %s, ...) called\n", ctx,
1712 err->message);
1714 if ((err == NULL) || (err->code == VIR_ERR_OK))
1715 return;
1717 LIBVIRT_ENSURE_THREAD_STATE;
1719 if ((libvirt_virPythonErrorFuncHandler == NULL) ||
1720 (libvirt_virPythonErrorFuncHandler == Py_None)) {
1721 virDefaultErrorFunc(err);
1722 } else {
1723 list = PyTuple_New(2);
1724 info = PyTuple_New(9);
1725 PyTuple_SetItem(list, 0, libvirt_virPythonErrorFuncCtxt);
1726 PyTuple_SetItem(list, 1, info);
1727 Py_XINCREF(libvirt_virPythonErrorFuncCtxt);
1728 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err->code));
1729 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err->domain));
1730 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err->message));
1731 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err->level));
1732 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err->str1));
1733 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err->str2));
1734 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err->str3));
1735 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err->int1));
1736 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err->int2));
1737 /* TODO pass conn and dom if available */
1738 result = PyEval_CallObject(libvirt_virPythonErrorFuncHandler, list);
1739 Py_XDECREF(list);
1740 Py_XDECREF(result);
1743 LIBVIRT_RELEASE_THREAD_STATE;
1746 static PyObject *
1747 libvirt_virRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
1748 PyObject * args)
1750 PyObject *py_retval;
1751 PyObject *pyobj_f;
1752 PyObject *pyobj_ctx;
1754 if (!PyArg_ParseTuple
1755 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
1756 &pyobj_ctx))
1757 return (NULL);
1759 DEBUG("libvirt_virRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
1760 pyobj_f);
1762 virSetErrorFunc(NULL, libvirt_virErrorFuncHandler);
1763 if (libvirt_virPythonErrorFuncHandler != NULL) {
1764 Py_XDECREF(libvirt_virPythonErrorFuncHandler);
1766 if (libvirt_virPythonErrorFuncCtxt != NULL) {
1767 Py_XDECREF(libvirt_virPythonErrorFuncCtxt);
1770 if ((pyobj_f == Py_None) && (pyobj_ctx == Py_None)) {
1771 libvirt_virPythonErrorFuncHandler = NULL;
1772 libvirt_virPythonErrorFuncCtxt = NULL;
1773 } else {
1774 Py_XINCREF(pyobj_ctx);
1775 Py_XINCREF(pyobj_f);
1777 /* TODO: check f is a function ! */
1778 libvirt_virPythonErrorFuncHandler = pyobj_f;
1779 libvirt_virPythonErrorFuncCtxt = pyobj_ctx;
1782 py_retval = libvirt_intWrap(1);
1783 return (py_retval);
1786 static int virConnectCredCallbackWrapper(virConnectCredentialPtr cred,
1787 unsigned int ncred,
1788 void *cbdata) {
1789 PyObject *list;
1790 PyObject *pycred;
1791 PyObject *pyauth = (PyObject *)cbdata;
1792 PyObject *pycbdata;
1793 PyObject *pycb;
1794 PyObject *pyret;
1795 int ret = -1, i;
1797 LIBVIRT_ENSURE_THREAD_STATE;
1799 pycb = PyList_GetItem(pyauth, 1);
1800 pycbdata = PyList_GetItem(pyauth, 2);
1802 list = PyTuple_New(2);
1803 pycred = PyTuple_New(ncred);
1804 for (i = 0 ; i < ncred ; i++) {
1805 PyObject *pycreditem;
1806 pycreditem = PyList_New(5);
1807 Py_INCREF(Py_None);
1808 PyTuple_SetItem(pycred, i, pycreditem);
1809 PyList_SetItem(pycreditem, 0, PyInt_FromLong((long) cred[i].type));
1810 PyList_SetItem(pycreditem, 1, PyString_FromString(cred[i].prompt));
1811 if (cred[i].challenge) {
1812 PyList_SetItem(pycreditem, 2, PyString_FromString(cred[i].challenge));
1813 } else {
1814 Py_INCREF(Py_None);
1815 PyList_SetItem(pycreditem, 2, Py_None);
1817 if (cred[i].defresult) {
1818 PyList_SetItem(pycreditem, 3, PyString_FromString(cred[i].defresult));
1819 } else {
1820 Py_INCREF(Py_None);
1821 PyList_SetItem(pycreditem, 3, Py_None);
1823 PyList_SetItem(pycreditem, 4, Py_None);
1826 PyTuple_SetItem(list, 0, pycred);
1827 Py_XINCREF(pycbdata);
1828 PyTuple_SetItem(list, 1, pycbdata);
1830 PyErr_Clear();
1831 pyret = PyEval_CallObject(pycb, list);
1832 if (PyErr_Occurred())
1833 goto cleanup;
1835 ret = PyLong_AsLong(pyret);
1836 if (ret == 0) {
1837 for (i = 0 ; i < ncred ; i++) {
1838 PyObject *pycreditem;
1839 PyObject *pyresult;
1840 char *result = NULL;
1841 pycreditem = PyTuple_GetItem(pycred, i);
1842 pyresult = PyList_GetItem(pycreditem, 4);
1843 if (pyresult != Py_None)
1844 result = PyString_AsString(pyresult);
1845 if (result != NULL) {
1846 cred[i].result = strdup(result);
1847 cred[i].resultlen = strlen(result);
1848 } else {
1849 cred[i].result = NULL;
1850 cred[i].resultlen = 0;
1855 cleanup:
1856 Py_XDECREF(list);
1857 Py_XDECREF(pyret);
1859 LIBVIRT_RELEASE_THREAD_STATE;
1861 return ret;
1865 static PyObject *
1866 libvirt_virConnectOpenAuth(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1867 PyObject *py_retval;
1868 virConnectPtr c_retval;
1869 char * name;
1870 unsigned int flags;
1871 PyObject *pyauth;
1872 PyObject *pycredcb;
1873 PyObject *pycredtype;
1874 virConnectAuth auth;
1876 if (!PyArg_ParseTuple(args, (char *)"zOi:virConnectOpenAuth", &name, &pyauth, &flags))
1877 return(NULL);
1879 pycredtype = PyList_GetItem(pyauth, 0);
1880 pycredcb = PyList_GetItem(pyauth, 1);
1882 auth.ncredtype = PyList_Size(pycredtype);
1883 if (auth.ncredtype) {
1884 int i;
1885 if (VIR_ALLOC_N(auth.credtype, auth.ncredtype) < 0)
1886 return VIR_PY_NONE;
1887 for (i = 0 ; i < auth.ncredtype ; i++) {
1888 PyObject *val;
1889 val = PyList_GetItem(pycredtype, i);
1890 auth.credtype[i] = (int)PyLong_AsLong(val);
1893 auth.cb = pycredcb ? virConnectCredCallbackWrapper : NULL;
1894 auth.cbdata = pyauth;
1896 LIBVIRT_BEGIN_ALLOW_THREADS;
1898 c_retval = virConnectOpenAuth(name, &auth, flags);
1899 LIBVIRT_END_ALLOW_THREADS;
1900 VIR_FREE(auth.credtype);
1901 py_retval = libvirt_virConnectPtrWrap((virConnectPtr) c_retval);
1902 return(py_retval);
1906 /************************************************************************
1908 * Wrappers for functions where generator fails *
1910 ************************************************************************/
1912 static PyObject *
1913 libvirt_virGetVersion (PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
1915 char *type = NULL;
1916 unsigned long libVer, typeVer = 0;
1917 int c_retval;
1919 if (!PyArg_ParseTuple (args, (char *) "|s", &type))
1920 return NULL;
1922 LIBVIRT_BEGIN_ALLOW_THREADS;
1924 if (type == NULL)
1925 c_retval = virGetVersion (&libVer, NULL, NULL);
1926 else
1927 c_retval = virGetVersion (&libVer, type, &typeVer);
1929 LIBVIRT_END_ALLOW_THREADS;
1931 if (c_retval == -1)
1932 return VIR_PY_NONE;
1934 if (type == NULL)
1935 return PyInt_FromLong (libVer);
1936 else
1937 return Py_BuildValue ((char *) "kk", libVer, typeVer);
1940 static PyObject *
1941 libvirt_virConnectGetVersion (PyObject *self ATTRIBUTE_UNUSED,
1942 PyObject *args)
1944 unsigned long hvVersion;
1945 int c_retval;
1946 virConnectPtr conn;
1947 PyObject *pyobj_conn;
1949 if (!PyArg_ParseTuple(args, (char *)"O:virConnectGetVersion",
1950 &pyobj_conn))
1951 return(NULL);
1952 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1954 LIBVIRT_BEGIN_ALLOW_THREADS;
1956 c_retval = virConnectGetVersion(conn, &hvVersion);
1958 LIBVIRT_END_ALLOW_THREADS;
1960 if (c_retval == -1)
1961 return VIR_PY_INT_FAIL;
1963 return PyInt_FromLong (hvVersion);
1966 static PyObject *
1967 libvirt_virConnectGetLibVersion (PyObject *self ATTRIBUTE_UNUSED,
1968 PyObject *args)
1970 unsigned long libVer;
1971 int c_retval;
1972 virConnectPtr conn;
1973 PyObject *pyobj_conn;
1975 if (!PyArg_ParseTuple(args, (char *)"O:virConnectGetLibVersion",
1976 &pyobj_conn))
1977 return(NULL);
1978 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1980 LIBVIRT_BEGIN_ALLOW_THREADS;
1982 c_retval = virConnectGetLibVersion(conn, &libVer);
1984 LIBVIRT_END_ALLOW_THREADS;
1986 if (c_retval == -1)
1987 return VIR_PY_INT_FAIL;
1989 return PyInt_FromLong (libVer);
1992 static PyObject *
1993 libvirt_virConnectListDomainsID(PyObject *self ATTRIBUTE_UNUSED,
1994 PyObject *args) {
1995 PyObject *py_retval;
1996 int *ids = NULL, c_retval, i;
1997 virConnectPtr conn;
1998 PyObject *pyobj_conn;
2001 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDomains", &pyobj_conn))
2002 return(NULL);
2003 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2005 LIBVIRT_BEGIN_ALLOW_THREADS;
2006 c_retval = virConnectNumOfDomains(conn);
2007 LIBVIRT_END_ALLOW_THREADS;
2008 if (c_retval < 0)
2009 return VIR_PY_NONE;
2011 if (c_retval) {
2012 if (VIR_ALLOC_N(ids, c_retval) < 0)
2013 return VIR_PY_NONE;
2015 LIBVIRT_BEGIN_ALLOW_THREADS;
2016 c_retval = virConnectListDomains(conn, ids, c_retval);
2017 LIBVIRT_END_ALLOW_THREADS;
2018 if (c_retval < 0) {
2019 VIR_FREE(ids);
2020 return VIR_PY_NONE;
2023 py_retval = PyList_New(c_retval);
2025 if (ids) {
2026 for (i = 0;i < c_retval;i++) {
2027 PyList_SetItem(py_retval, i, libvirt_intWrap(ids[i]));
2029 VIR_FREE(ids);
2032 return(py_retval);
2035 static PyObject *
2036 libvirt_virConnectListDefinedDomains(PyObject *self ATTRIBUTE_UNUSED,
2037 PyObject *args) {
2038 PyObject *py_retval;
2039 char **names = NULL;
2040 int c_retval, i;
2041 virConnectPtr conn;
2042 PyObject *pyobj_conn;
2045 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedDomains", &pyobj_conn))
2046 return(NULL);
2047 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2049 LIBVIRT_BEGIN_ALLOW_THREADS;
2050 c_retval = virConnectNumOfDefinedDomains(conn);
2051 LIBVIRT_END_ALLOW_THREADS;
2052 if (c_retval < 0)
2053 return VIR_PY_NONE;
2055 if (c_retval) {
2056 if (VIR_ALLOC_N(names, c_retval) < 0)
2057 return VIR_PY_NONE;
2058 LIBVIRT_BEGIN_ALLOW_THREADS;
2059 c_retval = virConnectListDefinedDomains(conn, names, c_retval);
2060 LIBVIRT_END_ALLOW_THREADS;
2061 if (c_retval < 0) {
2062 VIR_FREE(names);
2063 return VIR_PY_NONE;
2066 py_retval = PyList_New(c_retval);
2068 if (names) {
2069 for (i = 0;i < c_retval;i++) {
2070 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
2071 VIR_FREE(names[i]);
2073 VIR_FREE(names);
2076 return(py_retval);
2079 static PyObject *
2080 libvirt_virDomainSnapshotListNames(PyObject *self ATTRIBUTE_UNUSED,
2081 PyObject *args) {
2082 PyObject *py_retval;
2083 char **names = NULL;
2084 int c_retval, i;
2085 virDomainPtr dom;
2086 PyObject *pyobj_dom;
2087 unsigned int flags;
2089 if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainSnapshotListNames", &pyobj_dom, &flags))
2090 return(NULL);
2091 dom = (virDomainPtr) PyvirDomain_Get(pyobj_dom);
2093 LIBVIRT_BEGIN_ALLOW_THREADS;
2094 c_retval = virDomainSnapshotNum(dom, flags);
2095 LIBVIRT_END_ALLOW_THREADS;
2096 if (c_retval < 0)
2097 return VIR_PY_NONE;
2099 if (c_retval) {
2100 if (VIR_ALLOC_N(names, c_retval) < 0)
2101 return VIR_PY_NONE;
2102 LIBVIRT_BEGIN_ALLOW_THREADS;
2103 c_retval = virDomainSnapshotListNames(dom, names, c_retval, flags);
2104 LIBVIRT_END_ALLOW_THREADS;
2105 if (c_retval < 0) {
2106 VIR_FREE(names);
2107 return VIR_PY_NONE;
2110 py_retval = PyList_New(c_retval);
2112 if (names) {
2113 for (i = 0;i < c_retval;i++) {
2114 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
2115 VIR_FREE(names[i]);
2117 VIR_FREE(names);
2120 return(py_retval);
2123 static PyObject *
2124 libvirt_virDomainSnapshotListChildrenNames(PyObject *self ATTRIBUTE_UNUSED,
2125 PyObject *args) {
2126 PyObject *py_retval;
2127 char **names = NULL;
2128 int c_retval, i;
2129 virDomainSnapshotPtr snap;
2130 PyObject *pyobj_snap;
2131 unsigned int flags;
2133 if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainSnapshotListChildrenNames", &pyobj_snap, &flags))
2134 return(NULL);
2135 snap = (virDomainSnapshotPtr) PyvirDomainSnapshot_Get(pyobj_snap);
2137 LIBVIRT_BEGIN_ALLOW_THREADS;
2138 c_retval = virDomainSnapshotNumChildren(snap, flags);
2139 LIBVIRT_END_ALLOW_THREADS;
2140 if (c_retval < 0)
2141 return VIR_PY_NONE;
2143 if (c_retval) {
2144 if (VIR_ALLOC_N(names, c_retval) < 0)
2145 return VIR_PY_NONE;
2146 LIBVIRT_BEGIN_ALLOW_THREADS;
2147 c_retval = virDomainSnapshotListChildrenNames(snap, names, c_retval, flags);
2148 LIBVIRT_END_ALLOW_THREADS;
2149 if (c_retval < 0) {
2150 VIR_FREE(names);
2151 return VIR_PY_NONE;
2154 py_retval = PyList_New(c_retval);
2156 if (names) {
2157 for (i = 0;i < c_retval;i++) {
2158 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
2159 VIR_FREE(names[i]);
2161 VIR_FREE(names);
2164 return(py_retval);
2167 static PyObject *
2168 libvirt_virDomainRevertToSnapshot(PyObject *self ATTRIBUTE_UNUSED,
2169 PyObject *args) {
2170 int c_retval;
2171 virDomainSnapshotPtr snap;
2172 PyObject *pyobj_snap;
2173 PyObject *pyobj_dom;
2174 unsigned int flags;
2176 if (!PyArg_ParseTuple(args, (char *)"OOi:virDomainRevertToSnapshot", &pyobj_dom, &pyobj_snap, &flags))
2177 return(NULL);
2178 snap = (virDomainSnapshotPtr) PyvirDomainSnapshot_Get(pyobj_snap);
2180 LIBVIRT_BEGIN_ALLOW_THREADS;
2181 c_retval = virDomainRevertToSnapshot(snap, flags);
2182 LIBVIRT_END_ALLOW_THREADS;
2183 if (c_retval < 0)
2184 return VIR_PY_INT_FAIL;
2186 return PyInt_FromLong(c_retval);
2189 static PyObject *
2190 libvirt_virDomainGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2191 PyObject *py_retval;
2192 int c_retval;
2193 virDomainPtr domain;
2194 PyObject *pyobj_domain;
2195 virDomainInfo info;
2197 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetInfo", &pyobj_domain))
2198 return(NULL);
2199 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
2201 LIBVIRT_BEGIN_ALLOW_THREADS;
2202 c_retval = virDomainGetInfo(domain, &info);
2203 LIBVIRT_END_ALLOW_THREADS;
2204 if (c_retval < 0)
2205 return VIR_PY_NONE;
2206 py_retval = PyList_New(5);
2207 PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.state));
2208 PyList_SetItem(py_retval, 1, libvirt_ulongWrap(info.maxMem));
2209 PyList_SetItem(py_retval, 2, libvirt_ulongWrap(info.memory));
2210 PyList_SetItem(py_retval, 3, libvirt_intWrap((int) info.nrVirtCpu));
2211 PyList_SetItem(py_retval, 4,
2212 libvirt_longlongWrap((unsigned long long) info.cpuTime));
2213 return(py_retval);
2216 static PyObject *
2217 libvirt_virDomainGetState(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
2219 PyObject *py_retval;
2220 int c_retval;
2221 virDomainPtr domain;
2222 PyObject *pyobj_domain;
2223 int state;
2224 int reason;
2225 unsigned int flags;
2227 if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetState",
2228 &pyobj_domain, &flags))
2229 return NULL;
2231 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
2233 LIBVIRT_BEGIN_ALLOW_THREADS;
2234 c_retval = virDomainGetState(domain, &state, &reason, flags);
2235 LIBVIRT_END_ALLOW_THREADS;
2236 if (c_retval < 0)
2237 return VIR_PY_NONE;
2239 py_retval = PyList_New(2);
2240 PyList_SetItem(py_retval, 0, libvirt_intWrap(state));
2241 PyList_SetItem(py_retval, 1, libvirt_intWrap(reason));
2242 return py_retval;
2245 static PyObject *
2246 libvirt_virDomainGetControlInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2247 PyObject *py_retval;
2248 int c_retval;
2249 virDomainPtr domain;
2250 PyObject *pyobj_domain;
2251 virDomainControlInfo info;
2252 unsigned int flags;
2254 if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainGetControlInfo",
2255 &pyobj_domain, &flags))
2256 return NULL;
2257 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
2259 LIBVIRT_BEGIN_ALLOW_THREADS;
2260 c_retval = virDomainGetControlInfo(domain, &info, flags);
2261 LIBVIRT_END_ALLOW_THREADS;
2262 if (c_retval < 0)
2263 return VIR_PY_NONE;
2264 py_retval = PyList_New(3);
2265 PyList_SetItem(py_retval, 0, libvirt_intWrap(info.state));
2266 PyList_SetItem(py_retval, 1, libvirt_intWrap(info.details));
2267 PyList_SetItem(py_retval, 2, libvirt_longlongWrap(info.stateTime));
2268 return py_retval;
2271 static PyObject *
2272 libvirt_virDomainGetBlockInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2273 PyObject *py_retval;
2274 int c_retval;
2275 virDomainPtr domain;
2276 PyObject *pyobj_domain;
2277 virDomainBlockInfo info;
2278 const char *path;
2279 unsigned int flags;
2281 if (!PyArg_ParseTuple(args, (char *)"Ozi:virDomainGetInfo", &pyobj_domain, &path, &flags))
2282 return(NULL);
2283 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
2285 LIBVIRT_BEGIN_ALLOW_THREADS;
2286 c_retval = virDomainGetBlockInfo(domain, path, &info, flags);
2287 LIBVIRT_END_ALLOW_THREADS;
2288 if (c_retval < 0)
2289 return VIR_PY_NONE;
2290 py_retval = PyList_New(3);
2291 PyList_SetItem(py_retval, 0, libvirt_ulonglongWrap(info.capacity));
2292 PyList_SetItem(py_retval, 1, libvirt_ulonglongWrap(info.allocation));
2293 PyList_SetItem(py_retval, 2, libvirt_ulonglongWrap(info.physical));
2294 return(py_retval);
2297 static PyObject *
2298 libvirt_virNodeGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2299 PyObject *py_retval;
2300 int c_retval;
2301 virConnectPtr conn;
2302 PyObject *pyobj_conn;
2303 virNodeInfo info;
2305 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetInfo", &pyobj_conn))
2306 return(NULL);
2307 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2309 LIBVIRT_BEGIN_ALLOW_THREADS;
2310 c_retval = virNodeGetInfo(conn, &info);
2311 LIBVIRT_END_ALLOW_THREADS;
2312 if (c_retval < 0)
2313 return VIR_PY_NONE;
2314 py_retval = PyList_New(8);
2315 PyList_SetItem(py_retval, 0, libvirt_constcharPtrWrap(&info.model[0]));
2316 PyList_SetItem(py_retval, 1, libvirt_longWrap((long) info.memory >> 10));
2317 PyList_SetItem(py_retval, 2, libvirt_intWrap((int) info.cpus));
2318 PyList_SetItem(py_retval, 3, libvirt_intWrap((int) info.mhz));
2319 PyList_SetItem(py_retval, 4, libvirt_intWrap((int) info.nodes));
2320 PyList_SetItem(py_retval, 5, libvirt_intWrap((int) info.sockets));
2321 PyList_SetItem(py_retval, 6, libvirt_intWrap((int) info.cores));
2322 PyList_SetItem(py_retval, 7, libvirt_intWrap((int) info.threads));
2323 return(py_retval);
2326 static PyObject *
2327 libvirt_virDomainGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2328 PyObject *py_retval;
2329 unsigned char uuid[VIR_UUID_BUFLEN];
2330 virDomainPtr domain;
2331 PyObject *pyobj_domain;
2332 int c_retval;
2334 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetUUID", &pyobj_domain))
2335 return(NULL);
2336 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
2338 if (domain == NULL)
2339 return VIR_PY_NONE;
2340 LIBVIRT_BEGIN_ALLOW_THREADS;
2341 c_retval = virDomainGetUUID(domain, &uuid[0]);
2342 LIBVIRT_END_ALLOW_THREADS;
2344 if (c_retval < 0)
2345 return VIR_PY_NONE;
2346 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
2348 return(py_retval);
2351 static PyObject *
2352 libvirt_virDomainGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
2353 PyObject *args) {
2354 PyObject *py_retval;
2355 char uuidstr[VIR_UUID_STRING_BUFLEN];
2356 virDomainPtr dom;
2357 PyObject *pyobj_dom;
2358 int c_retval;
2360 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetUUIDString",
2361 &pyobj_dom))
2362 return(NULL);
2363 dom = (virDomainPtr) PyvirDomain_Get(pyobj_dom);
2365 if (dom == NULL)
2366 return VIR_PY_NONE;
2367 LIBVIRT_BEGIN_ALLOW_THREADS;
2368 c_retval = virDomainGetUUIDString(dom, &uuidstr[0]);
2369 LIBVIRT_END_ALLOW_THREADS;
2371 if (c_retval < 0)
2372 return VIR_PY_NONE;
2374 py_retval = PyString_FromString((char *) &uuidstr[0]);
2375 return(py_retval);
2378 static PyObject *
2379 libvirt_virDomainLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2380 PyObject *py_retval;
2381 virDomainPtr c_retval;
2382 virConnectPtr conn;
2383 PyObject *pyobj_conn;
2384 unsigned char * uuid;
2385 int len;
2387 if (!PyArg_ParseTuple(args, (char *)"Oz#:virDomainLookupByUUID", &pyobj_conn, &uuid, &len))
2388 return(NULL);
2389 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2391 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
2392 return VIR_PY_NONE;
2394 LIBVIRT_BEGIN_ALLOW_THREADS;
2395 c_retval = virDomainLookupByUUID(conn, uuid);
2396 LIBVIRT_END_ALLOW_THREADS;
2397 py_retval = libvirt_virDomainPtrWrap((virDomainPtr) c_retval);
2398 return(py_retval);
2402 static PyObject *
2403 libvirt_virConnectListNetworks(PyObject *self ATTRIBUTE_UNUSED,
2404 PyObject *args) {
2405 PyObject *py_retval;
2406 char **names = NULL;
2407 int c_retval, i;
2408 virConnectPtr conn;
2409 PyObject *pyobj_conn;
2412 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListNetworks", &pyobj_conn))
2413 return(NULL);
2414 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2416 LIBVIRT_BEGIN_ALLOW_THREADS;
2417 c_retval = virConnectNumOfNetworks(conn);
2418 LIBVIRT_END_ALLOW_THREADS;
2419 if (c_retval < 0)
2420 return VIR_PY_NONE;
2422 if (c_retval) {
2423 if (VIR_ALLOC_N(names, c_retval) < 0)
2424 return VIR_PY_NONE;
2425 LIBVIRT_BEGIN_ALLOW_THREADS;
2426 c_retval = virConnectListNetworks(conn, names, c_retval);
2427 LIBVIRT_END_ALLOW_THREADS;
2428 if (c_retval < 0) {
2429 VIR_FREE(names);
2430 return VIR_PY_NONE;
2433 py_retval = PyList_New(c_retval);
2435 if (names) {
2436 for (i = 0;i < c_retval;i++) {
2437 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
2438 VIR_FREE(names[i]);
2440 VIR_FREE(names);
2443 return(py_retval);
2447 static PyObject *
2448 libvirt_virConnectListDefinedNetworks(PyObject *self ATTRIBUTE_UNUSED,
2449 PyObject *args) {
2450 PyObject *py_retval;
2451 char **names = NULL;
2452 int c_retval, i;
2453 virConnectPtr conn;
2454 PyObject *pyobj_conn;
2457 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedNetworks", &pyobj_conn))
2458 return(NULL);
2459 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2461 LIBVIRT_BEGIN_ALLOW_THREADS;
2462 c_retval = virConnectNumOfDefinedNetworks(conn);
2463 LIBVIRT_END_ALLOW_THREADS;
2464 if (c_retval < 0)
2465 return VIR_PY_NONE;
2467 if (c_retval) {
2468 if (VIR_ALLOC_N(names, c_retval) < 0)
2469 return VIR_PY_NONE;
2470 LIBVIRT_BEGIN_ALLOW_THREADS;
2471 c_retval = virConnectListDefinedNetworks(conn, names, c_retval);
2472 LIBVIRT_END_ALLOW_THREADS;
2473 if (c_retval < 0) {
2474 VIR_FREE(names);
2475 return VIR_PY_NONE;
2478 py_retval = PyList_New(c_retval);
2480 if (names) {
2481 for (i = 0;i < c_retval;i++) {
2482 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
2483 VIR_FREE(names[i]);
2485 VIR_FREE(names);
2488 return(py_retval);
2492 static PyObject *
2493 libvirt_virNetworkGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2494 PyObject *py_retval;
2495 unsigned char uuid[VIR_UUID_BUFLEN];
2496 virNetworkPtr domain;
2497 PyObject *pyobj_domain;
2498 int c_retval;
2500 if (!PyArg_ParseTuple(args, (char *)"O:virNetworkGetUUID", &pyobj_domain))
2501 return(NULL);
2502 domain = (virNetworkPtr) PyvirNetwork_Get(pyobj_domain);
2504 if (domain == NULL)
2505 return VIR_PY_NONE;
2506 LIBVIRT_BEGIN_ALLOW_THREADS;
2507 c_retval = virNetworkGetUUID(domain, &uuid[0]);
2508 LIBVIRT_END_ALLOW_THREADS;
2510 if (c_retval < 0)
2511 return VIR_PY_NONE;
2512 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
2514 return(py_retval);
2517 static PyObject *
2518 libvirt_virNetworkGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
2519 PyObject *args) {
2520 PyObject *py_retval;
2521 char uuidstr[VIR_UUID_STRING_BUFLEN];
2522 virNetworkPtr net;
2523 PyObject *pyobj_net;
2524 int c_retval;
2526 if (!PyArg_ParseTuple(args, (char *)"O:virNetworkGetUUIDString",
2527 &pyobj_net))
2528 return(NULL);
2529 net = (virNetworkPtr) PyvirNetwork_Get(pyobj_net);
2531 if (net == NULL)
2532 return VIR_PY_NONE;
2533 LIBVIRT_BEGIN_ALLOW_THREADS;
2534 c_retval = virNetworkGetUUIDString(net, &uuidstr[0]);
2535 LIBVIRT_END_ALLOW_THREADS;
2537 if (c_retval < 0)
2538 return VIR_PY_NONE;
2540 py_retval = PyString_FromString((char *) &uuidstr[0]);
2541 return(py_retval);
2544 static PyObject *
2545 libvirt_virNetworkLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2546 PyObject *py_retval;
2547 virNetworkPtr c_retval;
2548 virConnectPtr conn;
2549 PyObject *pyobj_conn;
2550 unsigned char * uuid;
2551 int len;
2553 if (!PyArg_ParseTuple(args, (char *)"Oz#:virNetworkLookupByUUID", &pyobj_conn, &uuid, &len))
2554 return(NULL);
2555 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2557 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
2558 return VIR_PY_NONE;
2560 LIBVIRT_BEGIN_ALLOW_THREADS;
2561 c_retval = virNetworkLookupByUUID(conn, uuid);
2562 LIBVIRT_END_ALLOW_THREADS;
2563 py_retval = libvirt_virNetworkPtrWrap((virNetworkPtr) c_retval);
2564 return(py_retval);
2568 static PyObject *
2569 libvirt_virDomainGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2570 PyObject *py_retval;
2571 int c_retval, autostart;
2572 virDomainPtr domain;
2573 PyObject *pyobj_domain;
2575 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetAutostart", &pyobj_domain))
2576 return(NULL);
2578 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
2580 LIBVIRT_BEGIN_ALLOW_THREADS;
2581 c_retval = virDomainGetAutostart(domain, &autostart);
2582 LIBVIRT_END_ALLOW_THREADS;
2584 if (c_retval < 0)
2585 return VIR_PY_INT_FAIL;
2586 py_retval = libvirt_intWrap(autostart);
2587 return(py_retval);
2591 static PyObject *
2592 libvirt_virNetworkGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2593 PyObject *py_retval;
2594 int c_retval, autostart;
2595 virNetworkPtr network;
2596 PyObject *pyobj_network;
2598 if (!PyArg_ParseTuple(args, (char *)"O:virNetworkGetAutostart", &pyobj_network))
2599 return(NULL);
2601 network = (virNetworkPtr) PyvirNetwork_Get(pyobj_network);
2603 LIBVIRT_BEGIN_ALLOW_THREADS;
2604 c_retval = virNetworkGetAutostart(network, &autostart);
2605 LIBVIRT_END_ALLOW_THREADS;
2607 if (c_retval < 0)
2608 return VIR_PY_INT_FAIL;
2609 py_retval = libvirt_intWrap(autostart);
2610 return(py_retval);
2613 static PyObject *
2614 libvirt_virNodeGetCellsFreeMemory(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
2616 PyObject *py_retval;
2617 PyObject *pyobj_conn;
2618 int startCell, maxCells, c_retval, i;
2619 virConnectPtr conn;
2620 unsigned long long *freeMems;
2622 if (!PyArg_ParseTuple(args, (char *)"Oii:virNodeGetCellsFreeMemory", &pyobj_conn, &startCell, &maxCells))
2623 return(NULL);
2625 if ((startCell < 0) || (maxCells <= 0) || (startCell + maxCells > 10000))
2626 return VIR_PY_NONE;
2628 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2629 if (VIR_ALLOC_N(freeMems, maxCells) < 0)
2630 return VIR_PY_NONE;
2632 LIBVIRT_BEGIN_ALLOW_THREADS;
2633 c_retval = virNodeGetCellsFreeMemory(conn, freeMems, startCell, maxCells);
2634 LIBVIRT_END_ALLOW_THREADS;
2636 if (c_retval < 0) {
2637 VIR_FREE(freeMems);
2638 return VIR_PY_NONE;
2640 py_retval = PyList_New(c_retval);
2641 for (i = 0;i < c_retval;i++) {
2642 PyList_SetItem(py_retval, i,
2643 libvirt_longlongWrap((long long) freeMems[i]));
2645 VIR_FREE(freeMems);
2646 return(py_retval);
2649 static PyObject *
2650 libvirt_virNodeGetCPUStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
2652 PyObject *ret = NULL;
2653 PyObject *key = NULL;
2654 PyObject *val = NULL;
2655 PyObject *pyobj_conn;
2656 virConnectPtr conn;
2657 unsigned int flags;
2658 int cpuNum, c_retval, i;
2659 int nparams = 0;
2660 virNodeCPUStatsPtr stats = NULL;
2662 if (!PyArg_ParseTuple(args, (char *)"Oii:virNodeGetCPUStats", &pyobj_conn, &cpuNum, &flags))
2663 return ret;
2664 conn = (virConnectPtr)(PyvirConnect_Get(pyobj_conn));
2666 LIBVIRT_BEGIN_ALLOW_THREADS;
2667 c_retval = virNodeGetCPUStats(conn, cpuNum, NULL, &nparams, flags);
2668 LIBVIRT_END_ALLOW_THREADS;
2669 if (c_retval < 0)
2670 return VIR_PY_NONE;
2672 if (nparams) {
2673 if (VIR_ALLOC_N(stats, nparams) < 0)
2674 return PyErr_NoMemory();
2676 LIBVIRT_BEGIN_ALLOW_THREADS;
2677 c_retval = virNodeGetCPUStats(conn, cpuNum, stats, &nparams, flags);
2678 LIBVIRT_END_ALLOW_THREADS;
2679 if (c_retval < 0) {
2680 VIR_FREE(stats);
2681 return VIR_PY_NONE;
2685 if (!(ret = PyDict_New()))
2686 goto error;
2688 for (i = 0; i < nparams; i++) {
2689 key = libvirt_constcharPtrWrap(stats[i].field);
2690 val = libvirt_ulonglongWrap(stats[i].value);
2692 if (!key || !val || PyDict_SetItem(ret, key, val) < 0) {
2693 Py_DECREF(ret);
2694 ret = NULL;
2695 goto error;
2698 Py_DECREF(key);
2699 Py_DECREF(val);
2702 VIR_FREE(stats);
2703 return ret;
2705 error:
2706 VIR_FREE(stats);
2707 Py_XDECREF(key);
2708 Py_XDECREF(val);
2709 return ret;
2712 static PyObject *
2713 libvirt_virNodeGetMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
2715 PyObject *ret = NULL;
2716 PyObject *key = NULL;
2717 PyObject *val = NULL;
2718 PyObject *pyobj_conn;
2719 virConnectPtr conn;
2720 unsigned int flags;
2721 int cellNum, c_retval, i;
2722 int nparams = 0;
2723 virNodeMemoryStatsPtr stats = NULL;
2725 if (!PyArg_ParseTuple(args, (char *)"Oii:virNodeGetMemoryStats", &pyobj_conn, &cellNum, &flags))
2726 return ret;
2727 conn = (virConnectPtr)(PyvirConnect_Get(pyobj_conn));
2729 LIBVIRT_BEGIN_ALLOW_THREADS;
2730 c_retval = virNodeGetMemoryStats(conn, cellNum, NULL, &nparams, flags);
2731 LIBVIRT_END_ALLOW_THREADS;
2732 if (c_retval < 0)
2733 return VIR_PY_NONE;
2735 if (nparams) {
2736 if (VIR_ALLOC_N(stats, nparams) < 0)
2737 return PyErr_NoMemory();
2739 LIBVIRT_BEGIN_ALLOW_THREADS;
2740 c_retval = virNodeGetMemoryStats(conn, cellNum, stats, &nparams, flags);
2741 LIBVIRT_END_ALLOW_THREADS;
2742 if (c_retval < 0) {
2743 VIR_FREE(stats);
2744 return VIR_PY_NONE;
2748 if (!(ret = PyDict_New()))
2749 goto error;
2751 for (i = 0; i < nparams; i++) {
2752 key = libvirt_constcharPtrWrap(stats[i].field);
2753 val = libvirt_ulonglongWrap(stats[i].value);
2755 if (!key || !val || PyDict_SetItem(ret, key, val) < 0) {
2756 Py_DECREF(ret);
2757 ret = NULL;
2758 goto error;
2761 Py_DECREF(key);
2762 Py_DECREF(val);
2765 VIR_FREE(stats);
2766 return ret;
2768 error:
2769 VIR_FREE(stats);
2770 Py_XDECREF(key);
2771 Py_XDECREF(val);
2772 return ret;
2775 static PyObject *
2776 libvirt_virConnectListStoragePools(PyObject *self ATTRIBUTE_UNUSED,
2777 PyObject *args) {
2778 PyObject *py_retval;
2779 char **names = NULL;
2780 int c_retval, i;
2781 virConnectPtr conn;
2782 PyObject *pyobj_conn;
2785 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListStoragePools", &pyobj_conn))
2786 return(NULL);
2787 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2789 LIBVIRT_BEGIN_ALLOW_THREADS;
2790 c_retval = virConnectNumOfStoragePools(conn);
2791 LIBVIRT_END_ALLOW_THREADS;
2792 if (c_retval < 0)
2793 return VIR_PY_NONE;
2795 if (c_retval) {
2796 if (VIR_ALLOC_N(names, c_retval) < 0)
2797 return VIR_PY_NONE;
2798 LIBVIRT_BEGIN_ALLOW_THREADS;
2799 c_retval = virConnectListStoragePools(conn, names, c_retval);
2800 LIBVIRT_END_ALLOW_THREADS;
2801 if (c_retval < 0) {
2802 VIR_FREE(names);
2803 return VIR_PY_NONE;
2806 py_retval = PyList_New(c_retval);
2807 if (py_retval == NULL) {
2808 if (names) {
2809 for (i = 0;i < c_retval;i++)
2810 VIR_FREE(names[i]);
2811 VIR_FREE(names);
2813 return VIR_PY_NONE;
2816 if (names) {
2817 for (i = 0;i < c_retval;i++) {
2818 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
2819 VIR_FREE(names[i]);
2821 VIR_FREE(names);
2824 return(py_retval);
2828 static PyObject *
2829 libvirt_virConnectListDefinedStoragePools(PyObject *self ATTRIBUTE_UNUSED,
2830 PyObject *args) {
2831 PyObject *py_retval;
2832 char **names = NULL;
2833 int c_retval, i;
2834 virConnectPtr conn;
2835 PyObject *pyobj_conn;
2838 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedStoragePools", &pyobj_conn))
2839 return(NULL);
2840 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2842 LIBVIRT_BEGIN_ALLOW_THREADS;
2843 c_retval = virConnectNumOfDefinedStoragePools(conn);
2844 LIBVIRT_END_ALLOW_THREADS;
2845 if (c_retval < 0)
2846 return VIR_PY_NONE;
2848 if (c_retval) {
2849 if (VIR_ALLOC_N(names, c_retval) < 0)
2850 return VIR_PY_NONE;
2851 LIBVIRT_BEGIN_ALLOW_THREADS;
2852 c_retval = virConnectListDefinedStoragePools(conn, names, c_retval);
2853 LIBVIRT_END_ALLOW_THREADS;
2854 if (c_retval < 0) {
2855 VIR_FREE(names);
2856 return VIR_PY_NONE;
2859 py_retval = PyList_New(c_retval);
2860 if (py_retval == NULL) {
2861 if (names) {
2862 for (i = 0;i < c_retval;i++)
2863 VIR_FREE(names[i]);
2864 VIR_FREE(names);
2866 return VIR_PY_NONE;
2869 if (names) {
2870 for (i = 0;i < c_retval;i++) {
2871 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
2872 VIR_FREE(names[i]);
2874 VIR_FREE(names);
2877 return(py_retval);
2881 static PyObject *
2882 libvirt_virStoragePoolListVolumes(PyObject *self ATTRIBUTE_UNUSED,
2883 PyObject *args) {
2884 PyObject *py_retval;
2885 char **names = NULL;
2886 int c_retval, i;
2887 virStoragePoolPtr pool;
2888 PyObject *pyobj_pool;
2891 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolListVolumes", &pyobj_pool))
2892 return(NULL);
2893 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
2895 LIBVIRT_BEGIN_ALLOW_THREADS;
2896 c_retval = virStoragePoolNumOfVolumes(pool);
2897 LIBVIRT_END_ALLOW_THREADS;
2898 if (c_retval < 0)
2899 return VIR_PY_NONE;
2901 if (c_retval) {
2902 if (VIR_ALLOC_N(names, c_retval) < 0)
2903 return VIR_PY_NONE;
2904 LIBVIRT_BEGIN_ALLOW_THREADS;
2905 c_retval = virStoragePoolListVolumes(pool, names, c_retval);
2906 LIBVIRT_END_ALLOW_THREADS;
2907 if (c_retval < 0) {
2908 VIR_FREE(names);
2909 return VIR_PY_NONE;
2912 py_retval = PyList_New(c_retval);
2913 if (py_retval == NULL) {
2914 if (names) {
2915 for (i = 0;i < c_retval;i++)
2916 VIR_FREE(names[i]);
2917 VIR_FREE(names);
2919 return VIR_PY_NONE;
2922 if (names) {
2923 for (i = 0;i < c_retval;i++) {
2924 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
2925 VIR_FREE(names[i]);
2927 VIR_FREE(names);
2930 return(py_retval);
2933 static PyObject *
2934 libvirt_virStoragePoolGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2935 PyObject *py_retval;
2936 int c_retval, autostart;
2937 virStoragePoolPtr pool;
2938 PyObject *pyobj_pool;
2940 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetAutostart", &pyobj_pool))
2941 return(NULL);
2943 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
2945 LIBVIRT_BEGIN_ALLOW_THREADS;
2946 c_retval = virStoragePoolGetAutostart(pool, &autostart);
2947 LIBVIRT_END_ALLOW_THREADS;
2949 if (c_retval < 0)
2950 return VIR_PY_NONE;
2952 py_retval = libvirt_intWrap(autostart);
2953 return(py_retval);
2956 static PyObject *
2957 libvirt_virStoragePoolGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2958 PyObject *py_retval;
2959 int c_retval;
2960 virStoragePoolPtr pool;
2961 PyObject *pyobj_pool;
2962 virStoragePoolInfo info;
2964 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetInfo", &pyobj_pool))
2965 return(NULL);
2966 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
2968 LIBVIRT_BEGIN_ALLOW_THREADS;
2969 c_retval = virStoragePoolGetInfo(pool, &info);
2970 LIBVIRT_END_ALLOW_THREADS;
2971 if (c_retval < 0)
2972 return VIR_PY_NONE;
2974 if ((py_retval = PyList_New(4)) == NULL)
2975 return VIR_PY_NONE;
2977 PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.state));
2978 PyList_SetItem(py_retval, 1,
2979 libvirt_longlongWrap((unsigned long long) info.capacity));
2980 PyList_SetItem(py_retval, 2,
2981 libvirt_longlongWrap((unsigned long long) info.allocation));
2982 PyList_SetItem(py_retval, 3,
2983 libvirt_longlongWrap((unsigned long long) info.available));
2984 return(py_retval);
2988 static PyObject *
2989 libvirt_virStorageVolGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
2990 PyObject *py_retval;
2991 int c_retval;
2992 virStorageVolPtr pool;
2993 PyObject *pyobj_pool;
2994 virStorageVolInfo info;
2996 if (!PyArg_ParseTuple(args, (char *)"O:virStorageVolGetInfo", &pyobj_pool))
2997 return(NULL);
2998 pool = (virStorageVolPtr) PyvirStorageVol_Get(pyobj_pool);
3000 LIBVIRT_BEGIN_ALLOW_THREADS;
3001 c_retval = virStorageVolGetInfo(pool, &info);
3002 LIBVIRT_END_ALLOW_THREADS;
3003 if (c_retval < 0)
3004 return VIR_PY_NONE;
3006 if ((py_retval = PyList_New(3)) == NULL)
3007 return VIR_PY_NONE;
3008 PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.type));
3009 PyList_SetItem(py_retval, 1,
3010 libvirt_longlongWrap((unsigned long long) info.capacity));
3011 PyList_SetItem(py_retval, 2,
3012 libvirt_longlongWrap((unsigned long long) info.allocation));
3013 return(py_retval);
3016 static PyObject *
3017 libvirt_virStoragePoolGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3018 PyObject *py_retval;
3019 unsigned char uuid[VIR_UUID_BUFLEN];
3020 virStoragePoolPtr pool;
3021 PyObject *pyobj_pool;
3022 int c_retval;
3024 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetUUID", &pyobj_pool))
3025 return(NULL);
3026 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
3028 if (pool == NULL)
3029 return VIR_PY_NONE;
3030 LIBVIRT_BEGIN_ALLOW_THREADS;
3031 c_retval = virStoragePoolGetUUID(pool, &uuid[0]);
3032 LIBVIRT_END_ALLOW_THREADS;
3034 if (c_retval < 0)
3035 return VIR_PY_NONE;
3037 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
3039 return(py_retval);
3042 static PyObject *
3043 libvirt_virStoragePoolGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
3044 PyObject *args) {
3045 PyObject *py_retval;
3046 char uuidstr[VIR_UUID_STRING_BUFLEN];
3047 virStoragePoolPtr pool;
3048 PyObject *pyobj_pool;
3049 int c_retval;
3051 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetUUIDString", &pyobj_pool))
3052 return(NULL);
3053 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
3055 if (pool == NULL)
3056 return VIR_PY_NONE;
3057 LIBVIRT_BEGIN_ALLOW_THREADS;
3058 c_retval = virStoragePoolGetUUIDString(pool, &uuidstr[0]);
3059 LIBVIRT_END_ALLOW_THREADS;
3061 if (c_retval < 0)
3062 return VIR_PY_NONE;
3064 py_retval = PyString_FromString((char *) &uuidstr[0]);
3065 return(py_retval);
3068 static PyObject *
3069 libvirt_virStoragePoolLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3070 PyObject *py_retval;
3071 virStoragePoolPtr c_retval;
3072 virConnectPtr conn;
3073 PyObject *pyobj_conn;
3074 unsigned char * uuid;
3075 int len;
3077 if (!PyArg_ParseTuple(args, (char *)"Oz#:virStoragePoolLookupByUUID", &pyobj_conn, &uuid, &len))
3078 return(NULL);
3079 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
3081 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
3082 return VIR_PY_NONE;
3084 LIBVIRT_BEGIN_ALLOW_THREADS;
3085 c_retval = virStoragePoolLookupByUUID(conn, uuid);
3086 LIBVIRT_END_ALLOW_THREADS;
3087 py_retval = libvirt_virStoragePoolPtrWrap((virStoragePoolPtr) c_retval);
3088 return(py_retval);
3091 static PyObject *
3092 libvirt_virNodeListDevices(PyObject *self ATTRIBUTE_UNUSED,
3093 PyObject *args) {
3094 PyObject *py_retval;
3095 char **names = NULL;
3096 int c_retval, i;
3097 virConnectPtr conn;
3098 PyObject *pyobj_conn;
3099 char *cap;
3100 unsigned int flags;
3102 if (!PyArg_ParseTuple(args, (char *)"Ozi:virNodeListDevices",
3103 &pyobj_conn, &cap, &flags))
3104 return(NULL);
3105 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
3107 LIBVIRT_BEGIN_ALLOW_THREADS;
3108 c_retval = virNodeNumOfDevices(conn, cap, flags);
3109 LIBVIRT_END_ALLOW_THREADS;
3110 if (c_retval < 0)
3111 return VIR_PY_NONE;
3113 if (c_retval) {
3114 if (VIR_ALLOC_N(names, c_retval) < 0)
3115 return VIR_PY_NONE;
3116 LIBVIRT_BEGIN_ALLOW_THREADS;
3117 c_retval = virNodeListDevices(conn, cap, names, c_retval, flags);
3118 LIBVIRT_END_ALLOW_THREADS;
3119 if (c_retval < 0) {
3120 VIR_FREE(names);
3121 return VIR_PY_NONE;
3124 py_retval = PyList_New(c_retval);
3126 if (names) {
3127 for (i = 0;i < c_retval;i++) {
3128 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
3129 VIR_FREE(names[i]);
3131 VIR_FREE(names);
3134 return(py_retval);
3137 static PyObject *
3138 libvirt_virNodeDeviceListCaps(PyObject *self ATTRIBUTE_UNUSED,
3139 PyObject *args) {
3140 PyObject *py_retval;
3141 char **names = NULL;
3142 int c_retval, i;
3143 virNodeDevicePtr dev;
3144 PyObject *pyobj_dev;
3146 if (!PyArg_ParseTuple(args, (char *)"O:virNodeDeviceListCaps", &pyobj_dev))
3147 return(NULL);
3148 dev = (virNodeDevicePtr) PyvirNodeDevice_Get(pyobj_dev);
3150 LIBVIRT_BEGIN_ALLOW_THREADS;
3151 c_retval = virNodeDeviceNumOfCaps(dev);
3152 LIBVIRT_END_ALLOW_THREADS;
3153 if (c_retval < 0)
3154 return VIR_PY_NONE;
3156 if (c_retval) {
3157 if (VIR_ALLOC_N(names, c_retval) < 0)
3158 return VIR_PY_NONE;
3159 LIBVIRT_BEGIN_ALLOW_THREADS;
3160 c_retval = virNodeDeviceListCaps(dev, names, c_retval);
3161 LIBVIRT_END_ALLOW_THREADS;
3162 if (c_retval < 0) {
3163 VIR_FREE(names);
3164 return VIR_PY_NONE;
3167 py_retval = PyList_New(c_retval);
3169 if (names) {
3170 for (i = 0;i < c_retval;i++) {
3171 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
3172 VIR_FREE(names[i]);
3174 VIR_FREE(names);
3177 return(py_retval);
3180 static PyObject *
3181 libvirt_virSecretGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3182 PyObject *py_retval;
3183 unsigned char uuid[VIR_UUID_BUFLEN];
3184 virSecretPtr secret;
3185 PyObject *pyobj_secret;
3186 int c_retval;
3188 if (!PyArg_ParseTuple(args, (char *)"O:virSecretGetUUID", &pyobj_secret))
3189 return(NULL);
3190 secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
3192 if (secret == NULL)
3193 return VIR_PY_NONE;
3194 LIBVIRT_BEGIN_ALLOW_THREADS;
3195 c_retval = virSecretGetUUID(secret, &uuid[0]);
3196 LIBVIRT_END_ALLOW_THREADS;
3198 if (c_retval < 0)
3199 return VIR_PY_NONE;
3200 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
3202 return(py_retval);
3205 static PyObject *
3206 libvirt_virSecretGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
3207 PyObject *args) {
3208 PyObject *py_retval;
3209 char uuidstr[VIR_UUID_STRING_BUFLEN];
3210 virSecretPtr dom;
3211 PyObject *pyobj_dom;
3212 int c_retval;
3214 if (!PyArg_ParseTuple(args, (char *)"O:virSecretGetUUIDString",
3215 &pyobj_dom))
3216 return(NULL);
3217 dom = (virSecretPtr) PyvirSecret_Get(pyobj_dom);
3219 if (dom == NULL)
3220 return VIR_PY_NONE;
3221 LIBVIRT_BEGIN_ALLOW_THREADS;
3222 c_retval = virSecretGetUUIDString(dom, &uuidstr[0]);
3223 LIBVIRT_END_ALLOW_THREADS;
3225 if (c_retval < 0)
3226 return VIR_PY_NONE;
3228 py_retval = PyString_FromString((char *) &uuidstr[0]);
3229 return(py_retval);
3232 static PyObject *
3233 libvirt_virSecretLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3234 PyObject *py_retval;
3235 virSecretPtr c_retval;
3236 virConnectPtr conn;
3237 PyObject *pyobj_conn;
3238 unsigned char * uuid;
3239 int len;
3241 if (!PyArg_ParseTuple(args, (char *)"Oz#:virSecretLookupByUUID", &pyobj_conn, &uuid, &len))
3242 return(NULL);
3243 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
3245 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
3246 return VIR_PY_NONE;
3248 LIBVIRT_BEGIN_ALLOW_THREADS;
3249 c_retval = virSecretLookupByUUID(conn, uuid);
3250 LIBVIRT_END_ALLOW_THREADS;
3251 py_retval = libvirt_virSecretPtrWrap((virSecretPtr) c_retval);
3252 return(py_retval);
3256 static PyObject *
3257 libvirt_virConnectListSecrets(PyObject *self ATTRIBUTE_UNUSED,
3258 PyObject *args) {
3259 PyObject *py_retval;
3260 char **uuids = NULL;
3261 virConnectPtr conn;
3262 int c_retval, i;
3263 PyObject *pyobj_conn;
3265 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListSecrets", &pyobj_conn))
3266 return NULL;
3267 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
3269 LIBVIRT_BEGIN_ALLOW_THREADS;
3270 c_retval = virConnectNumOfSecrets(conn);
3271 LIBVIRT_END_ALLOW_THREADS;
3272 if (c_retval < 0)
3273 return VIR_PY_NONE;
3275 if (c_retval) {
3276 if (VIR_ALLOC_N(uuids, c_retval) < 0)
3277 return VIR_PY_NONE;
3278 LIBVIRT_BEGIN_ALLOW_THREADS;
3279 c_retval = virConnectListSecrets(conn, uuids, c_retval);
3280 LIBVIRT_END_ALLOW_THREADS;
3281 if (c_retval < 0) {
3282 VIR_FREE(uuids);
3283 return VIR_PY_NONE;
3286 py_retval = PyList_New(c_retval);
3288 if (uuids) {
3289 for (i = 0;i < c_retval;i++) {
3290 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(uuids[i]));
3291 VIR_FREE(uuids[i]);
3293 VIR_FREE(uuids);
3296 return py_retval;
3299 static PyObject *
3300 libvirt_virSecretGetValue(PyObject *self ATTRIBUTE_UNUSED,
3301 PyObject *args) {
3302 PyObject *py_retval;
3303 unsigned char *c_retval;
3304 size_t size;
3305 virSecretPtr secret;
3306 PyObject *pyobj_secret;
3307 unsigned int flags;
3309 if (!PyArg_ParseTuple(args, (char *)"Oi:virSecretGetValue", &pyobj_secret,
3310 &flags))
3311 return NULL;
3312 secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
3314 LIBVIRT_BEGIN_ALLOW_THREADS;
3315 c_retval = virSecretGetValue(secret, &size, flags);
3316 LIBVIRT_END_ALLOW_THREADS;
3318 if (c_retval == NULL)
3319 return VIR_PY_NONE;
3321 py_retval = PyString_FromStringAndSize((const char *)c_retval, size);
3322 VIR_FREE(c_retval);
3324 return py_retval;
3327 static PyObject *
3328 libvirt_virSecretSetValue(PyObject *self ATTRIBUTE_UNUSED,
3329 PyObject *args) {
3330 PyObject *py_retval;
3331 int c_retval;
3332 virSecretPtr secret;
3333 PyObject *pyobj_secret;
3334 const char *value;
3335 int size;
3336 unsigned int flags;
3338 if (!PyArg_ParseTuple(args, (char *)"Oz#i:virSecretSetValue", &pyobj_secret,
3339 &value, &size, &flags))
3340 return NULL;
3341 secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
3343 LIBVIRT_BEGIN_ALLOW_THREADS;
3344 c_retval = virSecretSetValue(secret, (const unsigned char *)value, size,
3345 flags);
3346 LIBVIRT_END_ALLOW_THREADS;
3348 py_retval = libvirt_intWrap(c_retval);
3349 return py_retval;
3352 static PyObject *
3353 libvirt_virNWFilterGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3354 PyObject *py_retval;
3355 unsigned char uuid[VIR_UUID_BUFLEN];
3356 virNWFilterPtr nwfilter;
3357 PyObject *pyobj_nwfilter;
3358 int c_retval;
3360 if (!PyArg_ParseTuple(args, (char *)"O:virNWFilterGetUUID", &pyobj_nwfilter))
3361 return(NULL);
3362 nwfilter = (virNWFilterPtr) PyvirNWFilter_Get(pyobj_nwfilter);
3364 if (nwfilter == NULL)
3365 return VIR_PY_NONE;
3366 LIBVIRT_BEGIN_ALLOW_THREADS;
3367 c_retval = virNWFilterGetUUID(nwfilter, &uuid[0]);
3368 LIBVIRT_END_ALLOW_THREADS;
3370 if (c_retval < 0)
3371 return VIR_PY_NONE;
3372 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
3374 return(py_retval);
3377 static PyObject *
3378 libvirt_virNWFilterGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
3379 PyObject *args) {
3380 PyObject *py_retval;
3381 char uuidstr[VIR_UUID_STRING_BUFLEN];
3382 virNWFilterPtr nwfilter;
3383 PyObject *pyobj_nwfilter;
3384 int c_retval;
3386 if (!PyArg_ParseTuple(args, (char *)"O:virNWFilterGetUUIDString",
3387 &pyobj_nwfilter))
3388 return(NULL);
3389 nwfilter = (virNWFilterPtr) PyvirNWFilter_Get(pyobj_nwfilter);
3391 if (nwfilter == NULL)
3392 return VIR_PY_NONE;
3393 LIBVIRT_BEGIN_ALLOW_THREADS;
3394 c_retval = virNWFilterGetUUIDString(nwfilter, &uuidstr[0]);
3395 LIBVIRT_END_ALLOW_THREADS;
3397 if (c_retval < 0)
3398 return VIR_PY_NONE;
3400 py_retval = PyString_FromString((char *) &uuidstr[0]);
3401 return(py_retval);
3404 static PyObject *
3405 libvirt_virNWFilterLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3406 PyObject *py_retval;
3407 virNWFilterPtr c_retval;
3408 virConnectPtr conn;
3409 PyObject *pyobj_conn;
3410 unsigned char * uuid;
3411 int len;
3413 if (!PyArg_ParseTuple(args, (char *)"Oz#:virNWFilterLookupByUUID", &pyobj_conn, &uuid, &len))
3414 return(NULL);
3415 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
3417 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
3418 return VIR_PY_NONE;
3420 LIBVIRT_BEGIN_ALLOW_THREADS;
3421 c_retval = virNWFilterLookupByUUID(conn, uuid);
3422 LIBVIRT_END_ALLOW_THREADS;
3423 py_retval = libvirt_virNWFilterPtrWrap((virNWFilterPtr) c_retval);
3424 return(py_retval);
3428 static PyObject *
3429 libvirt_virConnectListNWFilters(PyObject *self ATTRIBUTE_UNUSED,
3430 PyObject *args) {
3431 PyObject *py_retval;
3432 char **uuids = NULL;
3433 virConnectPtr conn;
3434 int c_retval, i;
3435 PyObject *pyobj_conn;
3437 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListNWFilters", &pyobj_conn))
3438 return NULL;
3439 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
3441 LIBVIRT_BEGIN_ALLOW_THREADS;
3442 c_retval = virConnectNumOfNWFilters(conn);
3443 LIBVIRT_END_ALLOW_THREADS;
3444 if (c_retval < 0)
3445 return VIR_PY_NONE;
3447 if (c_retval) {
3448 if (VIR_ALLOC_N(uuids, c_retval) < 0)
3449 return VIR_PY_NONE;
3450 LIBVIRT_BEGIN_ALLOW_THREADS;
3451 c_retval = virConnectListNWFilters(conn, uuids, c_retval);
3452 LIBVIRT_END_ALLOW_THREADS;
3453 if (c_retval < 0) {
3454 VIR_FREE(uuids);
3455 return VIR_PY_NONE;
3458 py_retval = PyList_New(c_retval);
3460 if (uuids) {
3461 for (i = 0;i < c_retval;i++) {
3462 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(uuids[i]));
3463 VIR_FREE(uuids[i]);
3465 VIR_FREE(uuids);
3468 return py_retval;
3471 static PyObject *
3472 libvirt_virConnectListInterfaces(PyObject *self ATTRIBUTE_UNUSED,
3473 PyObject *args) {
3474 PyObject *py_retval;
3475 char **names = NULL;
3476 int c_retval, i;
3477 virConnectPtr conn;
3478 PyObject *pyobj_conn;
3481 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListInterfaces", &pyobj_conn))
3482 return(NULL);
3483 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
3485 LIBVIRT_BEGIN_ALLOW_THREADS;
3486 c_retval = virConnectNumOfInterfaces(conn);
3487 LIBVIRT_END_ALLOW_THREADS;
3488 if (c_retval < 0)
3489 return VIR_PY_NONE;
3491 if (c_retval) {
3492 if (VIR_ALLOC_N(names, c_retval) < 0)
3493 return VIR_PY_NONE;
3494 LIBVIRT_BEGIN_ALLOW_THREADS;
3495 c_retval = virConnectListInterfaces(conn, names, c_retval);
3496 LIBVIRT_END_ALLOW_THREADS;
3497 if (c_retval < 0) {
3498 VIR_FREE(names);
3499 return VIR_PY_NONE;
3502 py_retval = PyList_New(c_retval);
3503 if (py_retval == NULL) {
3504 if (names) {
3505 for (i = 0;i < c_retval;i++)
3506 VIR_FREE(names[i]);
3507 VIR_FREE(names);
3509 return VIR_PY_NONE;
3512 if (names) {
3513 for (i = 0;i < c_retval;i++) {
3514 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
3515 VIR_FREE(names[i]);
3517 VIR_FREE(names);
3520 return(py_retval);
3524 static PyObject *
3525 libvirt_virConnectListDefinedInterfaces(PyObject *self ATTRIBUTE_UNUSED,
3526 PyObject *args) {
3527 PyObject *py_retval;
3528 char **names = NULL;
3529 int c_retval, i;
3530 virConnectPtr conn;
3531 PyObject *pyobj_conn;
3534 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedInterfaces",
3535 &pyobj_conn))
3536 return(NULL);
3537 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
3539 LIBVIRT_BEGIN_ALLOW_THREADS;
3540 c_retval = virConnectNumOfDefinedInterfaces(conn);
3541 LIBVIRT_END_ALLOW_THREADS;
3542 if (c_retval < 0)
3543 return VIR_PY_NONE;
3545 if (c_retval) {
3546 if (VIR_ALLOC_N(names, c_retval) < 0)
3547 return VIR_PY_NONE;
3548 LIBVIRT_BEGIN_ALLOW_THREADS;
3549 c_retval = virConnectListDefinedInterfaces(conn, names, c_retval);
3550 LIBVIRT_END_ALLOW_THREADS;
3551 if (c_retval < 0) {
3552 VIR_FREE(names);
3553 return VIR_PY_NONE;
3556 py_retval = PyList_New(c_retval);
3557 if (py_retval == NULL) {
3558 if (names) {
3559 for (i = 0;i < c_retval;i++)
3560 VIR_FREE(names[i]);
3561 VIR_FREE(names);
3563 return VIR_PY_NONE;
3566 if (names) {
3567 for (i = 0;i < c_retval;i++) {
3568 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
3569 VIR_FREE(names[i]);
3571 VIR_FREE(names);
3574 return(py_retval);
3578 static PyObject *
3579 libvirt_virConnectBaselineCPU(PyObject *self ATTRIBUTE_UNUSED,
3580 PyObject *args) {
3581 PyObject *pyobj_conn;
3582 PyObject *list;
3583 virConnectPtr conn;
3584 unsigned int flags;
3585 const char **xmlcpus = NULL;
3586 int ncpus = 0;
3587 char *base_cpu;
3588 PyObject *pybase_cpu;
3590 if (!PyArg_ParseTuple(args, (char *)"OOi:virConnectBaselineCPU",
3591 &pyobj_conn, &list, &flags))
3592 return(NULL);
3593 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
3595 if (PyList_Check(list)) {
3596 int i;
3598 ncpus = PyList_Size(list);
3599 if (VIR_ALLOC_N(xmlcpus, ncpus) < 0)
3600 return VIR_PY_INT_FAIL;
3602 for (i = 0; i < ncpus; i++) {
3603 xmlcpus[i] = PyString_AsString(PyList_GetItem(list, i));
3604 if (xmlcpus[i] == NULL) {
3605 VIR_FREE(xmlcpus);
3606 return VIR_PY_INT_FAIL;
3611 LIBVIRT_BEGIN_ALLOW_THREADS;
3612 base_cpu = virConnectBaselineCPU(conn, xmlcpus, ncpus, flags);
3613 LIBVIRT_END_ALLOW_THREADS;
3615 VIR_FREE(xmlcpus);
3617 if (base_cpu == NULL)
3618 return VIR_PY_INT_FAIL;
3620 pybase_cpu = PyString_FromString(base_cpu);
3621 VIR_FREE(base_cpu);
3623 if (pybase_cpu == NULL)
3624 return VIR_PY_INT_FAIL;
3626 return pybase_cpu;
3630 static PyObject *
3631 libvirt_virDomainGetJobInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
3632 PyObject *py_retval;
3633 int c_retval;
3634 virDomainPtr domain;
3635 PyObject *pyobj_domain;
3636 virDomainJobInfo info;
3638 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetJobInfo", &pyobj_domain))
3639 return(NULL);
3640 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
3642 LIBVIRT_BEGIN_ALLOW_THREADS;
3643 c_retval = virDomainGetJobInfo(domain, &info);
3644 LIBVIRT_END_ALLOW_THREADS;
3645 if (c_retval < 0)
3646 return VIR_PY_NONE;
3647 py_retval = PyList_New(12);
3648 PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.type));
3649 PyList_SetItem(py_retval, 1, libvirt_ulonglongWrap(info.timeElapsed));
3650 PyList_SetItem(py_retval, 2, libvirt_ulonglongWrap(info.timeRemaining));
3651 PyList_SetItem(py_retval, 3, libvirt_ulonglongWrap(info.dataTotal));
3652 PyList_SetItem(py_retval, 4, libvirt_ulonglongWrap(info.dataProcessed));
3653 PyList_SetItem(py_retval, 5, libvirt_ulonglongWrap(info.dataRemaining));
3654 PyList_SetItem(py_retval, 6, libvirt_ulonglongWrap(info.memTotal));
3655 PyList_SetItem(py_retval, 7, libvirt_ulonglongWrap(info.memProcessed));
3656 PyList_SetItem(py_retval, 8, libvirt_ulonglongWrap(info.memRemaining));
3657 PyList_SetItem(py_retval, 9, libvirt_ulonglongWrap(info.fileTotal));
3658 PyList_SetItem(py_retval, 10, libvirt_ulonglongWrap(info.fileProcessed));
3659 PyList_SetItem(py_retval, 11, libvirt_ulonglongWrap(info.fileRemaining));
3661 return(py_retval);
3664 static PyObject *
3665 libvirt_virDomainGetBlockJobInfo(PyObject *self ATTRIBUTE_UNUSED,
3666 PyObject *args)
3668 virDomainPtr domain;
3669 PyObject *pyobj_domain;
3670 const char *path;
3671 unsigned int flags;
3672 virDomainBlockJobInfo info;
3673 int c_ret;
3674 PyObject *ret;
3676 if (!PyArg_ParseTuple(args, (char *)"Ozi:virDomainGetBlockJobInfo",
3677 &pyobj_domain, &path, &flags))
3678 return(NULL);
3679 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
3681 LIBVIRT_BEGIN_ALLOW_THREADS;
3682 c_ret = virDomainGetBlockJobInfo(domain, path, &info, flags);
3683 LIBVIRT_END_ALLOW_THREADS;
3685 if (c_ret != 1)
3686 return VIR_PY_NONE;
3688 if ((ret = PyDict_New()) == NULL)
3689 return VIR_PY_NONE;
3691 PyDict_SetItem(ret, libvirt_constcharPtrWrap("type"),
3692 libvirt_intWrap(info.type));
3693 PyDict_SetItem(ret, libvirt_constcharPtrWrap("bandwidth"),
3694 libvirt_ulongWrap(info.bandwidth));
3695 PyDict_SetItem(ret, libvirt_constcharPtrWrap("cur"),
3696 libvirt_ulonglongWrap(info.cur));
3697 PyDict_SetItem(ret, libvirt_constcharPtrWrap("end"),
3698 libvirt_ulonglongWrap(info.end));
3700 return ret;
3703 static PyObject *
3704 libvirt_virDomainSetBlockIoTune(PyObject *self ATTRIBUTE_UNUSED,
3705 PyObject *args)
3707 virDomainPtr domain;
3708 PyObject *pyobj_domain, *info;
3709 PyObject *ret = NULL;
3710 int i_retval;
3711 int nparams = 0;
3712 Py_ssize_t size = 0;
3713 const char *disk;
3714 unsigned int flags;
3715 virTypedParameterPtr params, new_params;
3717 if (!PyArg_ParseTuple(args, (char *)"OzOi:virDomainSetBlockIoTune",
3718 &pyobj_domain, &disk, &info, &flags))
3719 return NULL;
3720 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
3722 if ((size = PyDict_Size(info)) < 0)
3723 return NULL;
3725 if (size == 0) {
3726 PyErr_Format(PyExc_LookupError,
3727 "Need non-empty dictionary to set attributes");
3728 return NULL;
3731 LIBVIRT_BEGIN_ALLOW_THREADS;
3732 i_retval = virDomainGetBlockIoTune(domain, disk, NULL, &nparams, flags);
3733 LIBVIRT_END_ALLOW_THREADS;
3735 if (i_retval < 0)
3736 return VIR_PY_INT_FAIL;
3738 if (nparams == 0) {
3739 PyErr_Format(PyExc_LookupError,
3740 "Domain has no settable attributes");
3741 return NULL;
3744 if (VIR_ALLOC_N(params, nparams) < 0)
3745 return PyErr_NoMemory();
3747 LIBVIRT_BEGIN_ALLOW_THREADS;
3748 i_retval = virDomainGetBlockIoTune(domain, disk, params, &nparams, flags);
3749 LIBVIRT_END_ALLOW_THREADS;
3751 if (i_retval < 0) {
3752 ret = VIR_PY_INT_FAIL;
3753 goto cleanup;
3756 new_params = setPyVirTypedParameter(info, params, nparams);
3757 if (!new_params)
3758 goto cleanup;
3760 LIBVIRT_BEGIN_ALLOW_THREADS;
3761 i_retval = virDomainSetBlockIoTune(domain, disk, new_params, size, flags);
3762 LIBVIRT_END_ALLOW_THREADS;
3764 if (i_retval < 0) {
3765 ret = VIR_PY_INT_FAIL;
3766 goto cleanup;
3769 ret = VIR_PY_INT_SUCCESS;
3771 cleanup:
3772 virTypedParameterArrayClear(params, nparams);
3773 VIR_FREE(params);
3774 VIR_FREE(new_params);
3775 return ret;
3778 static PyObject *
3779 libvirt_virDomainGetBlockIoTune(PyObject *self ATTRIBUTE_UNUSED,
3780 PyObject *args)
3782 virDomainPtr domain;
3783 PyObject *pyobj_domain;
3784 PyObject *ret = NULL;
3785 int i_retval;
3786 int nparams = 0;
3787 const char *disk;
3788 unsigned int flags;
3789 virTypedParameterPtr params;
3791 if (!PyArg_ParseTuple(args, (char *)"Ozi:virDomainGetBlockIoTune",
3792 &pyobj_domain, &disk, &flags))
3793 return NULL;
3794 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
3796 LIBVIRT_BEGIN_ALLOW_THREADS;
3797 i_retval = virDomainGetBlockIoTune(domain, disk, NULL, &nparams, flags);
3798 LIBVIRT_END_ALLOW_THREADS;
3800 if (i_retval < 0)
3801 return VIR_PY_NONE;
3803 if (!nparams)
3804 return PyDict_New();
3806 if (VIR_ALLOC_N(params, nparams) < 0)
3807 return PyErr_NoMemory();
3809 LIBVIRT_BEGIN_ALLOW_THREADS;
3810 i_retval = virDomainGetBlockIoTune(domain, disk, params, &nparams, flags);
3811 LIBVIRT_END_ALLOW_THREADS;
3813 if (i_retval < 0) {
3814 ret = VIR_PY_NONE;
3815 goto cleanup;
3818 ret = getPyVirTypedParameter(params, nparams);
3820 cleanup:
3821 virTypedParameterArrayClear(params, nparams);
3822 VIR_FREE(params);
3823 return ret;
3826 static PyObject *
3827 libvirt_virDomainGetDiskErrors(PyObject *self ATTRIBUTE_UNUSED,
3828 PyObject *args)
3830 PyObject *py_retval = VIR_PY_NONE;
3831 virDomainPtr domain;
3832 PyObject *pyobj_domain;
3833 unsigned int flags;
3834 virDomainDiskErrorPtr disks = NULL;
3835 unsigned int ndisks;
3836 int count;
3837 int i;
3839 if (!PyArg_ParseTuple(args, (char *) "Oi:virDomainGetDiskErrors",
3840 &pyobj_domain, &flags))
3841 return NULL;
3843 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
3845 if ((count = virDomainGetDiskErrors(domain, NULL, 0, 0)) < 0)
3846 return VIR_PY_NONE;
3847 ndisks = count;
3849 if (ndisks) {
3850 if (VIR_ALLOC_N(disks, ndisks) < 0)
3851 return VIR_PY_NONE;
3853 LIBVIRT_BEGIN_ALLOW_THREADS;
3854 count = virDomainGetDiskErrors(domain, disks, ndisks, 0);
3855 LIBVIRT_END_ALLOW_THREADS;
3857 if (count < 0)
3858 goto cleanup;
3861 if (!(py_retval = PyDict_New()))
3862 goto cleanup;
3864 for (i = 0; i < count; i++) {
3865 PyDict_SetItem(py_retval,
3866 libvirt_constcharPtrWrap(disks[i].disk),
3867 libvirt_intWrap(disks[i].error));
3870 cleanup:
3871 if (disks) {
3872 for (i = 0; i < count; i++)
3873 VIR_FREE(disks[i].disk);
3874 VIR_FREE(disks);
3876 return py_retval;
3879 /*******************************************
3880 * Helper functions to avoid importing modules
3881 * for every callback
3882 *******************************************/
3883 static PyObject *libvirt_module = NULL;
3884 static PyObject *libvirt_dict = NULL;
3885 static PyObject *libvirt_dom_class = NULL;
3887 static PyObject *
3888 getLibvirtModuleObject (void) {
3889 if(libvirt_module)
3890 return libvirt_module;
3892 // PyImport_ImportModule returns a new reference
3893 /* Bogus (char *) cast for RHEL-5 python API brokenness */
3894 libvirt_module = PyImport_ImportModule((char *)"libvirt");
3895 if(!libvirt_module) {
3896 DEBUG("%s Error importing libvirt module\n", __FUNCTION__);
3897 PyErr_Print();
3898 return NULL;
3901 return libvirt_module;
3904 static PyObject *
3905 getLibvirtDictObject (void) {
3906 if(libvirt_dict)
3907 return libvirt_dict;
3909 // PyModule_GetDict returns a borrowed reference
3910 libvirt_dict = PyModule_GetDict(getLibvirtModuleObject());
3911 if(!libvirt_dict) {
3912 DEBUG("%s Error importing libvirt dictionary\n", __FUNCTION__);
3913 PyErr_Print();
3914 return NULL;
3917 Py_INCREF(libvirt_dict);
3918 return libvirt_dict;
3921 static PyObject *
3922 getLibvirtDomainClassObject (void) {
3923 if(libvirt_dom_class)
3924 return libvirt_dom_class;
3926 // PyDict_GetItemString returns a borrowed reference
3927 libvirt_dom_class = PyDict_GetItemString(getLibvirtDictObject(),
3928 "virDomain");
3929 if(!libvirt_dom_class) {
3930 DEBUG("%s Error importing virDomain class\n", __FUNCTION__);
3931 PyErr_Print();
3932 return NULL;
3935 Py_INCREF(libvirt_dom_class);
3936 return libvirt_dom_class;
3939 static PyObject *
3940 libvirt_lookupPythonFunc(const char *funcname)
3942 PyObject *python_cb;
3944 /* Lookup the python callback */
3945 python_cb = PyDict_GetItemString(getLibvirtDictObject(), funcname);
3947 if (!python_cb) {
3948 DEBUG("%s: Error finding %s\n", __FUNCTION__, funcname);
3949 PyErr_Print();
3950 PyErr_Clear();
3951 return NULL;
3954 if (!PyCallable_Check(python_cb)) {
3955 DEBUG("%s: %s is not callable\n", __FUNCTION__, funcname);
3956 return NULL;
3959 return python_cb;
3962 /*******************************************
3963 * Domain Events
3964 *******************************************/
3966 static int
3967 libvirt_virConnectDomainEventCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
3968 virDomainPtr dom,
3969 int event,
3970 int detail,
3971 void *opaque)
3973 PyObject *pyobj_ret;
3975 PyObject *pyobj_conn_inst = (PyObject*)opaque;
3976 PyObject *pyobj_dom;
3978 PyObject *pyobj_dom_args;
3979 PyObject *pyobj_dom_inst;
3981 PyObject *dom_class;
3982 int ret = -1;
3984 LIBVIRT_ENSURE_THREAD_STATE;
3986 /* Create a python instance of this virDomainPtr */
3987 virDomainRef(dom);
3988 pyobj_dom = libvirt_virDomainPtrWrap(dom);
3989 pyobj_dom_args = PyTuple_New(2);
3990 if(PyTuple_SetItem(pyobj_dom_args, 0, pyobj_conn_inst)!=0) {
3991 DEBUG("%s error creating tuple",__FUNCTION__);
3992 goto cleanup;
3994 if(PyTuple_SetItem(pyobj_dom_args, 1, pyobj_dom)!=0) {
3995 DEBUG("%s error creating tuple",__FUNCTION__);
3996 goto cleanup;
3998 Py_INCREF(pyobj_conn_inst);
4000 dom_class = getLibvirtDomainClassObject();
4001 if(!PyClass_Check(dom_class)) {
4002 DEBUG("%s dom_class is not a class!\n", __FUNCTION__);
4003 goto cleanup;
4006 pyobj_dom_inst = PyInstance_New(dom_class,
4007 pyobj_dom_args,
4008 NULL);
4010 Py_DECREF(pyobj_dom_args);
4012 if(!pyobj_dom_inst) {
4013 DEBUG("%s Error creating a python instance of virDomain\n",
4014 __FUNCTION__);
4015 PyErr_Print();
4016 goto cleanup;
4019 /* Call the Callback Dispatcher */
4020 pyobj_ret = PyObject_CallMethod(pyobj_conn_inst,
4021 (char*)"_dispatchDomainEventCallbacks",
4022 (char*)"Oii",
4023 pyobj_dom_inst,
4024 event,
4025 detail);
4027 Py_DECREF(pyobj_dom_inst);
4029 if(!pyobj_ret) {
4030 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
4031 PyErr_Print();
4032 } else {
4033 Py_DECREF(pyobj_ret);
4034 ret = 0;
4038 cleanup:
4039 LIBVIRT_RELEASE_THREAD_STATE;
4040 return ret;
4043 static PyObject *
4044 libvirt_virConnectDomainEventRegister(ATTRIBUTE_UNUSED PyObject * self,
4045 PyObject * args)
4047 PyObject *py_retval; /* return value */
4048 PyObject *pyobj_conn; /* virConnectPtr */
4049 PyObject *pyobj_conn_inst; /* virConnect Python object */
4051 virConnectPtr conn;
4052 int ret = 0;
4054 if (!PyArg_ParseTuple
4055 (args, (char *) "OO:virConnectDomainEventRegister",
4056 &pyobj_conn, &pyobj_conn_inst)) {
4057 DEBUG("%s failed parsing tuple\n", __FUNCTION__);
4058 return VIR_PY_INT_FAIL;
4061 DEBUG("libvirt_virConnectDomainEventRegister(%p %p) called\n",
4062 pyobj_conn, pyobj_conn_inst);
4063 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
4065 Py_INCREF(pyobj_conn_inst);
4067 LIBVIRT_BEGIN_ALLOW_THREADS;
4069 ret = virConnectDomainEventRegister(conn,
4070 libvirt_virConnectDomainEventCallback,
4071 (void *)pyobj_conn_inst, NULL);
4073 LIBVIRT_END_ALLOW_THREADS;
4075 py_retval = libvirt_intWrap(ret);
4076 return (py_retval);
4079 static PyObject *
4080 libvirt_virConnectDomainEventDeregister(ATTRIBUTE_UNUSED PyObject * self,
4081 PyObject * args)
4083 PyObject *py_retval;
4084 PyObject *pyobj_conn;
4085 PyObject *pyobj_conn_inst;
4087 virConnectPtr conn;
4088 int ret = 0;
4090 if (!PyArg_ParseTuple
4091 (args, (char *) "OO:virConnectDomainEventDeregister",
4092 &pyobj_conn, &pyobj_conn_inst))
4093 return (NULL);
4095 DEBUG("libvirt_virConnectDomainEventDeregister(%p) called\n", pyobj_conn);
4097 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
4099 LIBVIRT_BEGIN_ALLOW_THREADS;
4101 ret = virConnectDomainEventDeregister(conn, libvirt_virConnectDomainEventCallback);
4103 LIBVIRT_END_ALLOW_THREADS;
4105 Py_DECREF(pyobj_conn_inst);
4106 py_retval = libvirt_intWrap(ret);
4107 return (py_retval);
4110 /*******************************************
4111 * Event Impl
4112 *******************************************/
4113 static PyObject *addHandleObj = NULL;
4114 static char *addHandleName = NULL;
4115 static PyObject *updateHandleObj = NULL;
4116 static char *updateHandleName = NULL;
4117 static PyObject *removeHandleObj = NULL;
4118 static char *removeHandleName = NULL;
4119 static PyObject *addTimeoutObj = NULL;
4120 static char *addTimeoutName = NULL;
4121 static PyObject *updateTimeoutObj = NULL;
4122 static char *updateTimeoutName = NULL;
4123 static PyObject *removeTimeoutObj = NULL;
4124 static char *removeTimeoutName = NULL;
4126 #define NAME(fn) ( fn ## Name ? fn ## Name : # fn )
4128 static int
4129 libvirt_virEventAddHandleFunc (int fd,
4130 int event,
4131 virEventHandleCallback cb,
4132 void *opaque,
4133 virFreeCallback ff)
4135 PyObject *result;
4136 PyObject *python_cb;
4137 PyObject *cb_obj;
4138 PyObject *ff_obj;
4139 PyObject *opaque_obj;
4140 PyObject *cb_args;
4141 PyObject *pyobj_args;
4142 int retval = -1;
4144 LIBVIRT_ENSURE_THREAD_STATE;
4146 /* Lookup the python callback */
4147 python_cb = libvirt_lookupPythonFunc("_eventInvokeHandleCallback");
4148 if (!python_cb) {
4149 goto cleanup;
4151 Py_INCREF(python_cb);
4153 /* create tuple for cb */
4154 cb_obj = libvirt_virEventHandleCallbackWrap(cb);
4155 ff_obj = libvirt_virFreeCallbackWrap(ff);
4156 opaque_obj = libvirt_virVoidPtrWrap(opaque);
4158 cb_args = PyTuple_New(3);
4159 PyTuple_SetItem(cb_args, 0, cb_obj);
4160 PyTuple_SetItem(cb_args, 1, opaque_obj);
4161 PyTuple_SetItem(cb_args, 2, ff_obj);
4163 pyobj_args = PyTuple_New(4);
4164 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(fd));
4165 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(event));
4166 PyTuple_SetItem(pyobj_args, 2, python_cb);
4167 PyTuple_SetItem(pyobj_args, 3, cb_args);
4169 result = PyEval_CallObject(addHandleObj, pyobj_args);
4170 if (!result) {
4171 PyErr_Print();
4172 PyErr_Clear();
4173 } else if (!PyInt_Check(result)) {
4174 DEBUG("%s: %s should return an int\n", __FUNCTION__, NAME(addHandle));
4175 } else {
4176 retval = (int)PyInt_AsLong(result);
4179 Py_XDECREF(result);
4180 Py_DECREF(pyobj_args);
4182 cleanup:
4183 LIBVIRT_RELEASE_THREAD_STATE;
4185 return retval;
4188 static void
4189 libvirt_virEventUpdateHandleFunc(int watch, int event)
4191 PyObject *result;
4192 PyObject *pyobj_args;
4194 LIBVIRT_ENSURE_THREAD_STATE;
4196 pyobj_args = PyTuple_New(2);
4197 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(watch));
4198 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(event));
4200 result = PyEval_CallObject(updateHandleObj, pyobj_args);
4201 if (!result) {
4202 PyErr_Print();
4203 PyErr_Clear();
4206 Py_XDECREF(result);
4207 Py_DECREF(pyobj_args);
4209 LIBVIRT_RELEASE_THREAD_STATE;
4213 static int
4214 libvirt_virEventRemoveHandleFunc(int watch)
4216 PyObject *result;
4217 PyObject *pyobj_args;
4218 PyObject *opaque;
4219 PyObject *ff;
4220 int retval = -1;
4221 virFreeCallback cff;
4223 LIBVIRT_ENSURE_THREAD_STATE;
4225 pyobj_args = PyTuple_New(1);
4226 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(watch));
4228 result = PyEval_CallObject(removeHandleObj, pyobj_args);
4229 if (!result) {
4230 PyErr_Print();
4231 PyErr_Clear();
4232 } else if (!PyTuple_Check(result) || PyTuple_Size(result) != 3) {
4233 DEBUG("%s: %s must return opaque obj registered with %s"
4234 "to avoid leaking libvirt memory\n",
4235 __FUNCTION__, NAME(removeHandle), NAME(addHandle));
4236 } else {
4237 opaque = PyTuple_GetItem(result, 1);
4238 ff = PyTuple_GetItem(result, 2);
4239 cff = PyvirFreeCallback_Get(ff);
4240 if (cff)
4241 (*cff)(PyvirVoidPtr_Get(opaque));
4242 retval = 0;
4245 Py_XDECREF(result);
4246 Py_DECREF(pyobj_args);
4248 LIBVIRT_RELEASE_THREAD_STATE;
4250 return retval;
4254 static int
4255 libvirt_virEventAddTimeoutFunc(int timeout,
4256 virEventTimeoutCallback cb,
4257 void *opaque,
4258 virFreeCallback ff)
4260 PyObject *result;
4262 PyObject *python_cb;
4264 PyObject *cb_obj;
4265 PyObject *ff_obj;
4266 PyObject *opaque_obj;
4267 PyObject *cb_args;
4268 PyObject *pyobj_args;
4269 int retval = -1;
4271 LIBVIRT_ENSURE_THREAD_STATE;
4273 /* Lookup the python callback */
4274 python_cb = libvirt_lookupPythonFunc("_eventInvokeTimeoutCallback");
4275 if (!python_cb) {
4276 goto cleanup;
4278 Py_INCREF(python_cb);
4280 /* create tuple for cb */
4281 cb_obj = libvirt_virEventTimeoutCallbackWrap(cb);
4282 ff_obj = libvirt_virFreeCallbackWrap(ff);
4283 opaque_obj = libvirt_virVoidPtrWrap(opaque);
4285 cb_args = PyTuple_New(3);
4286 PyTuple_SetItem(cb_args, 0, cb_obj);
4287 PyTuple_SetItem(cb_args, 1, opaque_obj);
4288 PyTuple_SetItem(cb_args, 2, ff_obj);
4290 pyobj_args = PyTuple_New(3);
4292 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timeout));
4293 PyTuple_SetItem(pyobj_args, 1, python_cb);
4294 PyTuple_SetItem(pyobj_args, 2, cb_args);
4296 result = PyEval_CallObject(addTimeoutObj, pyobj_args);
4297 if (!result) {
4298 PyErr_Print();
4299 PyErr_Clear();
4300 } else if (!PyInt_Check(result)) {
4301 DEBUG("%s: %s should return an int\n", __FUNCTION__, NAME(addTimeout));
4302 } else {
4303 retval = (int)PyInt_AsLong(result);
4306 Py_XDECREF(result);
4307 Py_DECREF(pyobj_args);
4309 cleanup:
4310 LIBVIRT_RELEASE_THREAD_STATE;
4311 return retval;
4314 static void
4315 libvirt_virEventUpdateTimeoutFunc(int timer, int timeout)
4317 PyObject *result = NULL;
4318 PyObject *pyobj_args;
4320 LIBVIRT_ENSURE_THREAD_STATE;
4322 pyobj_args = PyTuple_New(2);
4323 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timer));
4324 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(timeout));
4326 result = PyEval_CallObject(updateTimeoutObj, pyobj_args);
4327 if (!result) {
4328 PyErr_Print();
4329 PyErr_Clear();
4332 Py_XDECREF(result);
4333 Py_DECREF(pyobj_args);
4335 LIBVIRT_RELEASE_THREAD_STATE;
4338 static int
4339 libvirt_virEventRemoveTimeoutFunc(int timer)
4341 PyObject *result = NULL;
4342 PyObject *pyobj_args;
4343 PyObject *opaque;
4344 PyObject *ff;
4345 int retval = -1;
4346 virFreeCallback cff;
4348 LIBVIRT_ENSURE_THREAD_STATE;
4350 pyobj_args = PyTuple_New(1);
4351 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timer));
4353 result = PyEval_CallObject(removeTimeoutObj, pyobj_args);
4354 if (!result) {
4355 PyErr_Print();
4356 PyErr_Clear();
4357 } else if (!PyTuple_Check(result) || PyTuple_Size(result) != 3) {
4358 DEBUG("%s: %s must return opaque obj registered with %s"
4359 "to avoid leaking libvirt memory\n",
4360 __FUNCTION__, NAME(removeTimeout), NAME(addTimeout));
4361 } else {
4362 opaque = PyTuple_GetItem(result, 1);
4363 ff = PyTuple_GetItem(result, 2);
4364 cff = PyvirFreeCallback_Get(ff);
4365 if (cff)
4366 (*cff)(PyvirVoidPtr_Get(opaque));
4367 retval = 0;
4370 Py_XDECREF(result);
4371 Py_DECREF(pyobj_args);
4373 LIBVIRT_RELEASE_THREAD_STATE;
4375 return retval;
4378 static PyObject *
4379 libvirt_virEventRegisterImpl(ATTRIBUTE_UNUSED PyObject * self,
4380 PyObject * args)
4382 /* Unref the previously-registered impl (if any) */
4383 Py_XDECREF(addHandleObj);
4384 Py_XDECREF(updateHandleObj);
4385 Py_XDECREF(removeHandleObj);
4386 Py_XDECREF(addTimeoutObj);
4387 Py_XDECREF(updateTimeoutObj);
4388 Py_XDECREF(removeTimeoutObj);
4390 /* Parse and check arguments */
4391 if (!PyArg_ParseTuple(args, (char *) "OOOOOO:virEventRegisterImpl",
4392 &addHandleObj, &updateHandleObj,
4393 &removeHandleObj, &addTimeoutObj,
4394 &updateTimeoutObj, &removeTimeoutObj) ||
4395 !PyCallable_Check(addHandleObj) ||
4396 !PyCallable_Check(updateHandleObj) ||
4397 !PyCallable_Check(removeHandleObj) ||
4398 !PyCallable_Check(addTimeoutObj) ||
4399 !PyCallable_Check(updateTimeoutObj) ||
4400 !PyCallable_Check(removeTimeoutObj))
4401 return VIR_PY_INT_FAIL;
4403 /* Get argument string representations (for error reporting) */
4404 addHandleName = py_str(addHandleObj);
4405 updateHandleName = py_str(updateHandleObj);
4406 removeHandleName = py_str(removeHandleObj);
4407 addTimeoutName = py_str(addTimeoutObj);
4408 updateTimeoutName = py_str(updateTimeoutObj);
4409 removeTimeoutName = py_str(removeTimeoutObj);
4411 /* Inc refs since we're holding onto these objects until
4412 * the next call (if any) to this function.
4414 Py_INCREF(addHandleObj);
4415 Py_INCREF(updateHandleObj);
4416 Py_INCREF(removeHandleObj);
4417 Py_INCREF(addTimeoutObj);
4418 Py_INCREF(updateTimeoutObj);
4419 Py_INCREF(removeTimeoutObj);
4421 LIBVIRT_BEGIN_ALLOW_THREADS;
4423 /* Now register our C EventImpl, which will dispatch
4424 * to the Python callbacks passed in as args.
4426 virEventRegisterImpl(libvirt_virEventAddHandleFunc,
4427 libvirt_virEventUpdateHandleFunc,
4428 libvirt_virEventRemoveHandleFunc,
4429 libvirt_virEventAddTimeoutFunc,
4430 libvirt_virEventUpdateTimeoutFunc,
4431 libvirt_virEventRemoveTimeoutFunc);
4433 LIBVIRT_END_ALLOW_THREADS;
4435 return VIR_PY_INT_SUCCESS;
4438 static PyObject *
4439 libvirt_virEventInvokeHandleCallback(PyObject *self ATTRIBUTE_UNUSED,
4440 PyObject *args)
4442 int watch, fd, event;
4443 PyObject *py_f;
4444 PyObject *py_opaque;
4445 virEventHandleCallback cb;
4446 void *opaque;
4448 if (!PyArg_ParseTuple
4449 (args, (char *) "iiiOO:virEventInvokeHandleCallback",
4450 &watch, &fd, &event, &py_f, &py_opaque
4452 return VIR_PY_INT_FAIL;
4454 cb = (virEventHandleCallback) PyvirEventHandleCallback_Get(py_f);
4455 opaque = (void *) PyvirVoidPtr_Get(py_opaque);
4457 if(cb) {
4458 LIBVIRT_BEGIN_ALLOW_THREADS;
4459 cb (watch, fd, event, opaque);
4460 LIBVIRT_END_ALLOW_THREADS;
4463 return VIR_PY_INT_SUCCESS;
4466 static PyObject *
4467 libvirt_virEventInvokeTimeoutCallback(PyObject *self ATTRIBUTE_UNUSED,
4468 PyObject *args)
4470 int timer;
4471 PyObject *py_f;
4472 PyObject *py_opaque;
4473 virEventTimeoutCallback cb;
4474 void *opaque;
4476 if (!PyArg_ParseTuple
4477 (args, (char *) "iOO:virEventInvokeTimeoutCallback",
4478 &timer, &py_f, &py_opaque
4480 return VIR_PY_INT_FAIL;
4482 cb = (virEventTimeoutCallback) PyvirEventTimeoutCallback_Get(py_f);
4483 opaque = (void *) PyvirVoidPtr_Get(py_opaque);
4484 if(cb) {
4485 LIBVIRT_BEGIN_ALLOW_THREADS;
4486 cb (timer, opaque);
4487 LIBVIRT_END_ALLOW_THREADS;
4490 return VIR_PY_INT_SUCCESS;
4493 static void
4494 libvirt_virEventHandleCallback(int watch,
4495 int fd,
4496 int events,
4497 void *opaque)
4499 PyObject *pyobj_cbData = (PyObject *)opaque;
4500 PyObject *pyobj_ret;
4501 PyObject *python_cb;
4503 LIBVIRT_ENSURE_THREAD_STATE;
4505 /* Lookup the python callback */
4506 python_cb = libvirt_lookupPythonFunc("_dispatchEventHandleCallback");
4507 if (!python_cb) {
4508 goto cleanup;
4511 Py_INCREF(pyobj_cbData);
4513 /* Call the pure python dispatcher */
4514 pyobj_ret = PyObject_CallFunction(python_cb,
4515 (char *)"iiiO",
4516 watch, fd, events, pyobj_cbData);
4518 Py_DECREF(pyobj_cbData);
4520 if (!pyobj_ret) {
4521 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
4522 PyErr_Print();
4523 } else {
4524 Py_DECREF(pyobj_ret);
4527 cleanup:
4528 LIBVIRT_RELEASE_THREAD_STATE;
4531 static PyObject *
4532 libvirt_virEventAddHandle(PyObject *self ATTRIBUTE_UNUSED,
4533 PyObject *args)
4535 PyObject *py_retval;
4536 PyObject *pyobj_cbData;
4537 virEventHandleCallback cb = libvirt_virEventHandleCallback;
4538 int events;
4539 int fd;
4540 int ret;
4542 if (!PyArg_ParseTuple(args, (char *) "iiO:virEventAddHandle",
4543 &fd, &events, &pyobj_cbData)) {
4544 DEBUG("%s failed to parse tuple\n", __FUNCTION__);
4545 return VIR_PY_INT_FAIL;
4548 Py_INCREF(pyobj_cbData);
4550 LIBVIRT_BEGIN_ALLOW_THREADS;
4551 ret = virEventAddHandle(fd, events, cb, pyobj_cbData, NULL);
4552 LIBVIRT_END_ALLOW_THREADS;
4554 if (ret < 0) {
4555 Py_DECREF(pyobj_cbData);
4558 py_retval = libvirt_intWrap(ret);
4559 return py_retval;
4562 static void
4563 libvirt_virEventTimeoutCallback(int timer,
4564 void *opaque)
4566 PyObject *pyobj_cbData = (PyObject *)opaque;
4567 PyObject *pyobj_ret;
4568 PyObject *python_cb;
4570 LIBVIRT_ENSURE_THREAD_STATE;
4572 /* Lookup the python callback */
4573 python_cb = libvirt_lookupPythonFunc("_dispatchEventTimeoutCallback");
4574 if (!python_cb) {
4575 goto cleanup;
4578 Py_INCREF(pyobj_cbData);
4580 /* Call the pure python dispatcher */
4581 pyobj_ret = PyObject_CallFunction(python_cb,
4582 (char *)"iO",
4583 timer, pyobj_cbData);
4585 Py_DECREF(pyobj_cbData);
4587 if (!pyobj_ret) {
4588 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
4589 PyErr_Print();
4590 } else {
4591 Py_DECREF(pyobj_ret);
4594 cleanup:
4595 LIBVIRT_RELEASE_THREAD_STATE;
4598 static PyObject *
4599 libvirt_virEventAddTimeout(PyObject *self ATTRIBUTE_UNUSED,
4600 PyObject *args)
4602 PyObject *py_retval;
4603 PyObject *pyobj_cbData;
4604 virEventTimeoutCallback cb = libvirt_virEventTimeoutCallback;
4605 int timeout;
4606 int ret;
4608 if (!PyArg_ParseTuple(args, (char *) "iO:virEventAddTimeout",
4609 &timeout, &pyobj_cbData)) {
4610 DEBUG("%s failed to parse tuple\n", __FUNCTION__);
4611 return VIR_PY_INT_FAIL;
4614 Py_INCREF(pyobj_cbData);
4616 LIBVIRT_BEGIN_ALLOW_THREADS;
4617 ret = virEventAddTimeout(timeout, cb, pyobj_cbData, NULL);
4618 LIBVIRT_END_ALLOW_THREADS;
4620 if (ret < 0) {
4621 Py_DECREF(pyobj_cbData);
4624 py_retval = libvirt_intWrap(ret);
4625 return py_retval;
4628 static void
4629 libvirt_virConnectDomainEventFreeFunc(void *opaque)
4631 PyObject *pyobj_conn = (PyObject*)opaque;
4632 LIBVIRT_ENSURE_THREAD_STATE;
4633 Py_DECREF(pyobj_conn);
4634 LIBVIRT_RELEASE_THREAD_STATE;
4637 static int
4638 libvirt_virConnectDomainEventLifecycleCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
4639 virDomainPtr dom,
4640 int event,
4641 int detail,
4642 void *opaque)
4644 PyObject *pyobj_cbData = (PyObject*)opaque;
4645 PyObject *pyobj_dom;
4646 PyObject *pyobj_ret;
4647 PyObject *pyobj_conn;
4648 PyObject *dictKey;
4649 int ret = -1;
4651 LIBVIRT_ENSURE_THREAD_STATE;
4653 /* Create a python instance of this virDomainPtr */
4654 virDomainRef(dom);
4655 pyobj_dom = libvirt_virDomainPtrWrap(dom);
4656 Py_INCREF(pyobj_cbData);
4658 dictKey = libvirt_constcharPtrWrap("conn");
4659 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
4660 Py_DECREF(dictKey);
4662 /* Call the Callback Dispatcher */
4663 pyobj_ret = PyObject_CallMethod(pyobj_conn,
4664 (char*)"_dispatchDomainEventLifecycleCallback",
4665 (char*)"OiiO",
4666 pyobj_dom,
4667 event, detail,
4668 pyobj_cbData);
4670 Py_DECREF(pyobj_cbData);
4671 Py_DECREF(pyobj_dom);
4673 if(!pyobj_ret) {
4674 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
4675 PyErr_Print();
4676 } else {
4677 Py_DECREF(pyobj_ret);
4678 ret = 0;
4681 LIBVIRT_RELEASE_THREAD_STATE;
4682 return ret;
4685 static int
4686 libvirt_virConnectDomainEventGenericCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
4687 virDomainPtr dom,
4688 void *opaque)
4690 PyObject *pyobj_cbData = (PyObject*)opaque;
4691 PyObject *pyobj_dom;
4692 PyObject *pyobj_ret;
4693 PyObject *pyobj_conn;
4694 PyObject *dictKey;
4695 int ret = -1;
4697 LIBVIRT_ENSURE_THREAD_STATE;
4699 /* Create a python instance of this virDomainPtr */
4700 virDomainRef(dom);
4701 pyobj_dom = libvirt_virDomainPtrWrap(dom);
4702 Py_INCREF(pyobj_cbData);
4704 dictKey = libvirt_constcharPtrWrap("conn");
4705 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
4706 Py_DECREF(dictKey);
4708 /* Call the Callback Dispatcher */
4709 pyobj_ret = PyObject_CallMethod(pyobj_conn,
4710 (char*)"_dispatchDomainEventGenericCallback",
4711 (char*)"OO",
4712 pyobj_dom, pyobj_cbData);
4714 Py_DECREF(pyobj_cbData);
4715 Py_DECREF(pyobj_dom);
4717 if(!pyobj_ret) {
4718 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
4719 PyErr_Print();
4720 } else {
4721 Py_DECREF(pyobj_ret);
4722 ret = 0;
4725 LIBVIRT_RELEASE_THREAD_STATE;
4726 return ret;
4729 static int
4730 libvirt_virConnectDomainEventRTCChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
4731 virDomainPtr dom,
4732 long long utcoffset,
4733 void *opaque)
4735 PyObject *pyobj_cbData = (PyObject*)opaque;
4736 PyObject *pyobj_dom;
4737 PyObject *pyobj_ret;
4738 PyObject *pyobj_conn;
4739 PyObject *dictKey;
4740 int ret = -1;
4742 LIBVIRT_ENSURE_THREAD_STATE;
4744 /* Create a python instance of this virDomainPtr */
4745 virDomainRef(dom);
4746 pyobj_dom = libvirt_virDomainPtrWrap(dom);
4747 Py_INCREF(pyobj_cbData);
4749 dictKey = libvirt_constcharPtrWrap("conn");
4750 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
4751 Py_DECREF(dictKey);
4753 /* Call the Callback Dispatcher */
4754 pyobj_ret = PyObject_CallMethod(pyobj_conn,
4755 (char*)"_dispatchDomainEventRTCChangeCallback",
4756 (char*)"OLO",
4757 pyobj_dom,
4758 (PY_LONG_LONG)utcoffset,
4759 pyobj_cbData);
4761 Py_DECREF(pyobj_cbData);
4762 Py_DECREF(pyobj_dom);
4764 if(!pyobj_ret) {
4765 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
4766 PyErr_Print();
4767 } else {
4768 Py_DECREF(pyobj_ret);
4769 ret = 0;
4772 LIBVIRT_RELEASE_THREAD_STATE;
4773 return ret;
4776 static int
4777 libvirt_virConnectDomainEventWatchdogCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
4778 virDomainPtr dom,
4779 int action,
4780 void *opaque)
4782 PyObject *pyobj_cbData = (PyObject*)opaque;
4783 PyObject *pyobj_dom;
4784 PyObject *pyobj_ret;
4785 PyObject *pyobj_conn;
4786 PyObject *dictKey;
4787 int ret = -1;
4789 LIBVIRT_ENSURE_THREAD_STATE;
4791 /* Create a python instance of this virDomainPtr */
4792 virDomainRef(dom);
4793 pyobj_dom = libvirt_virDomainPtrWrap(dom);
4794 Py_INCREF(pyobj_cbData);
4796 dictKey = libvirt_constcharPtrWrap("conn");
4797 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
4798 Py_DECREF(dictKey);
4800 /* Call the Callback Dispatcher */
4801 pyobj_ret = PyObject_CallMethod(pyobj_conn,
4802 (char*)"_dispatchDomainEventWatchdogCallback",
4803 (char*)"OiO",
4804 pyobj_dom,
4805 action,
4806 pyobj_cbData);
4808 Py_DECREF(pyobj_cbData);
4809 Py_DECREF(pyobj_dom);
4811 if(!pyobj_ret) {
4812 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
4813 PyErr_Print();
4814 } else {
4815 Py_DECREF(pyobj_ret);
4816 ret = 0;
4819 LIBVIRT_RELEASE_THREAD_STATE;
4820 return ret;
4823 static int
4824 libvirt_virConnectDomainEventIOErrorCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
4825 virDomainPtr dom,
4826 const char *srcPath,
4827 const char *devAlias,
4828 int action,
4829 void *opaque)
4831 PyObject *pyobj_cbData = (PyObject*)opaque;
4832 PyObject *pyobj_dom;
4833 PyObject *pyobj_ret;
4834 PyObject *pyobj_conn;
4835 PyObject *dictKey;
4836 int ret = -1;
4838 LIBVIRT_ENSURE_THREAD_STATE;
4840 /* Create a python instance of this virDomainPtr */
4841 virDomainRef(dom);
4842 pyobj_dom = libvirt_virDomainPtrWrap(dom);
4843 Py_INCREF(pyobj_cbData);
4845 dictKey = libvirt_constcharPtrWrap("conn");
4846 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
4847 Py_DECREF(dictKey);
4849 /* Call the Callback Dispatcher */
4850 pyobj_ret = PyObject_CallMethod(pyobj_conn,
4851 (char*)"_dispatchDomainEventIOErrorCallback",
4852 (char*)"OssiO",
4853 pyobj_dom,
4854 srcPath, devAlias, action,
4855 pyobj_cbData);
4857 Py_DECREF(pyobj_cbData);
4858 Py_DECREF(pyobj_dom);
4860 if(!pyobj_ret) {
4861 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
4862 PyErr_Print();
4863 } else {
4864 Py_DECREF(pyobj_ret);
4865 ret = 0;
4868 LIBVIRT_RELEASE_THREAD_STATE;
4869 return ret;
4872 static int
4873 libvirt_virConnectDomainEventIOErrorReasonCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
4874 virDomainPtr dom,
4875 const char *srcPath,
4876 const char *devAlias,
4877 int action,
4878 const char *reason,
4879 void *opaque)
4881 PyObject *pyobj_cbData = (PyObject*)opaque;
4882 PyObject *pyobj_dom;
4883 PyObject *pyobj_ret;
4884 PyObject *pyobj_conn;
4885 PyObject *dictKey;
4886 int ret = -1;
4888 LIBVIRT_ENSURE_THREAD_STATE;
4890 /* Create a python instance of this virDomainPtr */
4891 virDomainRef(dom);
4892 pyobj_dom = libvirt_virDomainPtrWrap(dom);
4893 Py_INCREF(pyobj_cbData);
4895 dictKey = libvirt_constcharPtrWrap("conn");
4896 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
4897 Py_DECREF(dictKey);
4899 /* Call the Callback Dispatcher */
4900 pyobj_ret = PyObject_CallMethod(pyobj_conn,
4901 (char*)"_dispatchDomainEventIOErrorReasonCallback",
4902 (char*)"OssisO",
4903 pyobj_dom,
4904 srcPath, devAlias, action, reason,
4905 pyobj_cbData);
4907 Py_DECREF(pyobj_cbData);
4908 Py_DECREF(pyobj_dom);
4910 if(!pyobj_ret) {
4911 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
4912 PyErr_Print();
4913 } else {
4914 Py_DECREF(pyobj_ret);
4915 ret = 0;
4918 LIBVIRT_RELEASE_THREAD_STATE;
4919 return ret;
4922 static int
4923 libvirt_virConnectDomainEventGraphicsCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
4924 virDomainPtr dom,
4925 int phase,
4926 virDomainEventGraphicsAddressPtr local,
4927 virDomainEventGraphicsAddressPtr remote,
4928 const char *authScheme,
4929 virDomainEventGraphicsSubjectPtr subject,
4930 void *opaque)
4932 PyObject *pyobj_cbData = (PyObject*)opaque;
4933 PyObject *pyobj_dom;
4934 PyObject *pyobj_ret;
4935 PyObject *pyobj_conn;
4936 PyObject *dictKey;
4937 PyObject *pyobj_local;
4938 PyObject *pyobj_remote;
4939 PyObject *pyobj_subject;
4940 int ret = -1;
4941 int i;
4943 LIBVIRT_ENSURE_THREAD_STATE;
4945 /* Create a python instance of this virDomainPtr */
4946 virDomainRef(dom);
4947 pyobj_dom = libvirt_virDomainPtrWrap(dom);
4948 Py_INCREF(pyobj_cbData);
4950 dictKey = libvirt_constcharPtrWrap("conn");
4951 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
4952 Py_DECREF(dictKey);
4954 pyobj_local = PyDict_New();
4955 PyDict_SetItem(pyobj_local,
4956 libvirt_constcharPtrWrap("family"),
4957 libvirt_intWrap(local->family));
4958 PyDict_SetItem(pyobj_local,
4959 libvirt_constcharPtrWrap("node"),
4960 libvirt_constcharPtrWrap(local->node));
4961 PyDict_SetItem(pyobj_local,
4962 libvirt_constcharPtrWrap("service"),
4963 libvirt_constcharPtrWrap(local->service));
4965 pyobj_remote = PyDict_New();
4966 PyDict_SetItem(pyobj_remote,
4967 libvirt_constcharPtrWrap("family"),
4968 libvirt_intWrap(remote->family));
4969 PyDict_SetItem(pyobj_remote,
4970 libvirt_constcharPtrWrap("node"),
4971 libvirt_constcharPtrWrap(remote->node));
4972 PyDict_SetItem(pyobj_remote,
4973 libvirt_constcharPtrWrap("service"),
4974 libvirt_constcharPtrWrap(remote->service));
4976 pyobj_subject = PyList_New(subject->nidentity);
4977 for (i = 0 ; i < subject->nidentity ; i++) {
4978 PyObject *pair = PyTuple_New(2);
4979 PyTuple_SetItem(pair, 0, libvirt_constcharPtrWrap(subject->identities[i].type));
4980 PyTuple_SetItem(pair, 1, libvirt_constcharPtrWrap(subject->identities[i].name));
4982 PyList_SetItem(pyobj_subject, i, pair);
4985 /* Call the Callback Dispatcher */
4986 pyobj_ret = PyObject_CallMethod(pyobj_conn,
4987 (char*)"_dispatchDomainEventGraphicsCallback",
4988 (char*)"OiOOsOO",
4989 pyobj_dom,
4990 phase, pyobj_local, pyobj_remote,
4991 authScheme, pyobj_subject,
4992 pyobj_cbData);
4994 Py_DECREF(pyobj_cbData);
4995 Py_DECREF(pyobj_dom);
4997 if(!pyobj_ret) {
4998 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
4999 PyErr_Print();
5000 } else {
5001 Py_DECREF(pyobj_ret);
5002 ret = 0;
5005 LIBVIRT_RELEASE_THREAD_STATE;
5006 return ret;
5009 static int
5010 libvirt_virConnectDomainEventBlockJobCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
5011 virDomainPtr dom,
5012 const char *path,
5013 int type,
5014 int status,
5015 void *opaque)
5017 PyObject *pyobj_cbData = (PyObject*)opaque;
5018 PyObject *pyobj_dom;
5019 PyObject *pyobj_ret;
5020 PyObject *pyobj_conn;
5021 PyObject *dictKey;
5022 int ret = -1;
5024 LIBVIRT_ENSURE_THREAD_STATE;
5026 /* Create a python instance of this virDomainPtr */
5027 virDomainRef(dom);
5028 pyobj_dom = libvirt_virDomainPtrWrap(dom);
5029 Py_INCREF(pyobj_cbData);
5031 dictKey = libvirt_constcharPtrWrap("conn");
5032 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
5033 Py_DECREF(dictKey);
5035 /* Call the Callback Dispatcher */
5036 pyobj_ret = PyObject_CallMethod(pyobj_conn,
5037 (char*)"dispatchDomainEventBlockPullCallback",
5038 (char*)"OsiiO",
5039 pyobj_dom, path, type, status, pyobj_cbData);
5041 Py_DECREF(pyobj_cbData);
5042 Py_DECREF(pyobj_dom);
5044 if(!pyobj_ret) {
5045 #if DEBUG_ERROR
5046 printf("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
5047 #endif
5048 PyErr_Print();
5049 } else {
5050 Py_DECREF(pyobj_ret);
5051 ret = 0;
5054 LIBVIRT_RELEASE_THREAD_STATE;
5055 return ret;
5058 static int
5059 libvirt_virConnectDomainEventDiskChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
5060 virDomainPtr dom,
5061 const char *oldSrcPath,
5062 const char *newSrcPath,
5063 const char *devAlias,
5064 int reason,
5065 void *opaque)
5067 PyObject *pyobj_cbData = (PyObject*)opaque;
5068 PyObject *pyobj_dom;
5069 PyObject *pyobj_ret;
5070 PyObject *pyobj_conn;
5071 PyObject *dictKey;
5072 int ret = -1;
5074 LIBVIRT_ENSURE_THREAD_STATE;
5075 /* Create a python instance of this virDomainPtr */
5076 virDomainRef(dom);
5078 pyobj_dom = libvirt_virDomainPtrWrap(dom);
5079 Py_INCREF(pyobj_cbData);
5081 dictKey = libvirt_constcharPtrWrap("conn");
5082 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
5083 Py_DECREF(dictKey);
5085 /* Call the Callback Dispatcher */
5086 pyobj_ret = PyObject_CallMethod(pyobj_conn,
5087 (char*)"_dispatchDomainEventDiskChangeCallback",
5088 (char*)"OsssiO",
5089 pyobj_dom,
5090 oldSrcPath, newSrcPath,
5091 devAlias, reason, pyobj_cbData);
5093 Py_DECREF(pyobj_cbData);
5094 Py_DECREF(pyobj_dom);
5096 if(!pyobj_ret) {
5097 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
5098 PyErr_Print();
5099 } else {
5100 Py_DECREF(pyobj_ret);
5101 ret = 0;
5104 LIBVIRT_RELEASE_THREAD_STATE;
5105 return ret;
5108 static int
5109 libvirt_virConnectDomainEventTrayChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
5110 virDomainPtr dom,
5111 const char *devAlias,
5112 int reason,
5113 void *opaque)
5115 PyObject *pyobj_cbData = (PyObject*)opaque;
5116 PyObject *pyobj_dom;
5117 PyObject *pyobj_ret;
5118 PyObject *pyobj_conn;
5119 PyObject *dictKey;
5120 int ret = -1;
5122 LIBVIRT_ENSURE_THREAD_STATE;
5123 /* Create a python instance of this virDomainPtr */
5124 virDomainRef(dom);
5126 pyobj_dom = libvirt_virDomainPtrWrap(dom);
5127 Py_INCREF(pyobj_cbData);
5129 dictKey = libvirt_constcharPtrWrap("conn");
5130 pyobj_conn = PyDict_GetItem(pyobj_cbData, dictKey);
5131 Py_DECREF(dictKey);
5133 /* Call the Callback Dispatcher */
5134 pyobj_ret = PyObject_CallMethod(pyobj_conn,
5135 (char*)"_dispatchDomainEventTrayChangeCallback",
5136 (char*)"OsiO",
5137 pyobj_dom,
5138 devAlias, reason, pyobj_cbData);
5140 Py_DECREF(pyobj_cbData);
5141 Py_DECREF(pyobj_dom);
5143 if(!pyobj_ret) {
5144 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
5145 PyErr_Print();
5146 } else {
5147 Py_DECREF(pyobj_ret);
5148 ret = 0;
5151 LIBVIRT_RELEASE_THREAD_STATE;
5152 return ret;
5155 static PyObject *
5156 libvirt_virConnectDomainEventRegisterAny(ATTRIBUTE_UNUSED PyObject * self,
5157 PyObject * args)
5159 PyObject *py_retval; /* return value */
5160 PyObject *pyobj_conn; /* virConnectPtr */
5161 PyObject *pyobj_dom;
5162 PyObject *pyobj_cbData; /* hash of callback data */
5163 int eventID;
5164 virConnectPtr conn;
5165 int ret = 0;
5166 virConnectDomainEventGenericCallback cb = NULL;
5167 virDomainPtr dom;
5169 if (!PyArg_ParseTuple
5170 (args, (char *) "OOiO:virConnectDomainEventRegisterAny",
5171 &pyobj_conn, &pyobj_dom, &eventID, &pyobj_cbData)) {
5172 DEBUG("%s failed parsing tuple\n", __FUNCTION__);
5173 return VIR_PY_INT_FAIL;
5176 DEBUG("libvirt_virConnectDomainEventRegister(%p %p %d %p) called\n",
5177 pyobj_conn, pyobj_dom, eventID, pyobj_cbData);
5178 conn = PyvirConnect_Get(pyobj_conn);
5179 if (pyobj_dom == Py_None)
5180 dom = NULL;
5181 else
5182 dom = PyvirDomain_Get(pyobj_dom);
5184 switch (eventID) {
5185 case VIR_DOMAIN_EVENT_ID_LIFECYCLE:
5186 cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventLifecycleCallback);
5187 break;
5188 case VIR_DOMAIN_EVENT_ID_REBOOT:
5189 cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventGenericCallback);
5190 break;
5191 case VIR_DOMAIN_EVENT_ID_RTC_CHANGE:
5192 cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventRTCChangeCallback);
5193 break;
5194 case VIR_DOMAIN_EVENT_ID_WATCHDOG:
5195 cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventWatchdogCallback);
5196 break;
5197 case VIR_DOMAIN_EVENT_ID_IO_ERROR:
5198 cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventIOErrorCallback);
5199 break;
5200 case VIR_DOMAIN_EVENT_ID_IO_ERROR_REASON:
5201 cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventIOErrorReasonCallback);
5202 break;
5203 case VIR_DOMAIN_EVENT_ID_GRAPHICS:
5204 cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventGraphicsCallback);
5205 break;
5206 case VIR_DOMAIN_EVENT_ID_CONTROL_ERROR:
5207 cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventGenericCallback);
5208 break;
5209 case VIR_DOMAIN_EVENT_ID_BLOCK_JOB:
5210 cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventBlockJobCallback);
5211 break;
5212 case VIR_DOMAIN_EVENT_ID_DISK_CHANGE:
5213 cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventDiskChangeCallback);
5214 break;
5215 case VIR_DOMAIN_EVENT_ID_TRAY_CHANGE:
5216 cb = VIR_DOMAIN_EVENT_CALLBACK(libvirt_virConnectDomainEventTrayChangeCallback);
5217 break;
5220 if (!cb) {
5221 return VIR_PY_INT_FAIL;
5224 Py_INCREF(pyobj_cbData);
5226 LIBVIRT_BEGIN_ALLOW_THREADS;
5227 ret = virConnectDomainEventRegisterAny(conn, dom, eventID,
5228 cb, pyobj_cbData,
5229 libvirt_virConnectDomainEventFreeFunc);
5230 LIBVIRT_END_ALLOW_THREADS;
5232 if (ret < 0) {
5233 Py_DECREF(pyobj_cbData);
5236 py_retval = libvirt_intWrap(ret);
5237 return (py_retval);
5240 static PyObject *
5241 libvirt_virConnectDomainEventDeregisterAny(ATTRIBUTE_UNUSED PyObject * self,
5242 PyObject * args)
5244 PyObject *py_retval;
5245 PyObject *pyobj_conn;
5246 int callbackID;
5247 virConnectPtr conn;
5248 int ret = 0;
5250 if (!PyArg_ParseTuple
5251 (args, (char *) "Oi:virConnectDomainEventDeregister",
5252 &pyobj_conn, &callbackID))
5253 return (NULL);
5255 DEBUG("libvirt_virConnectDomainEventDeregister(%p) called\n", pyobj_conn);
5257 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
5259 LIBVIRT_BEGIN_ALLOW_THREADS;
5261 ret = virConnectDomainEventDeregisterAny(conn, callbackID);
5263 LIBVIRT_END_ALLOW_THREADS;
5264 py_retval = libvirt_intWrap(ret);
5265 return (py_retval);
5268 static void
5269 libvirt_virStreamEventFreeFunc(void *opaque)
5271 PyObject *pyobj_stream = (PyObject*)opaque;
5272 LIBVIRT_ENSURE_THREAD_STATE;
5273 Py_DECREF(pyobj_stream);
5274 LIBVIRT_RELEASE_THREAD_STATE;
5277 static void
5278 libvirt_virStreamEventCallback(virStreamPtr st ATTRIBUTE_UNUSED,
5279 int events,
5280 void *opaque)
5282 PyObject *pyobj_cbData = (PyObject *)opaque;
5283 PyObject *pyobj_stream;
5284 PyObject *pyobj_ret;
5285 PyObject *dictKey;
5287 LIBVIRT_ENSURE_THREAD_STATE;
5289 Py_INCREF(pyobj_cbData);
5290 dictKey = libvirt_constcharPtrWrap("stream");
5291 pyobj_stream = PyDict_GetItem(pyobj_cbData, dictKey);
5292 Py_DECREF(dictKey);
5294 /* Call the pure python dispatcher */
5295 pyobj_ret = PyObject_CallMethod(pyobj_stream,
5296 (char *)"_dispatchStreamEventCallback",
5297 (char *)"iO",
5298 events, pyobj_cbData);
5300 Py_DECREF(pyobj_cbData);
5302 if (!pyobj_ret) {
5303 DEBUG("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
5304 PyErr_Print();
5305 } else {
5306 Py_DECREF(pyobj_ret);
5309 LIBVIRT_RELEASE_THREAD_STATE;
5312 static PyObject *
5313 libvirt_virStreamEventAddCallback(PyObject *self ATTRIBUTE_UNUSED,
5314 PyObject *args)
5316 PyObject *py_retval;
5317 PyObject *pyobj_stream;
5318 PyObject *pyobj_cbData;
5319 virStreamPtr stream;
5320 virStreamEventCallback cb = libvirt_virStreamEventCallback;
5321 int ret;
5322 int events;
5324 if (!PyArg_ParseTuple(args, (char *) "OiO:virStreamEventAddCallback",
5325 &pyobj_stream, &events, &pyobj_cbData)) {
5326 DEBUG("%s failed to parse tuple\n", __FUNCTION__);
5327 return VIR_PY_INT_FAIL;
5330 DEBUG("libvirt_virStreamEventAddCallback(%p, %d, %p) called\n",
5331 pyobj_stream, events, pyobj_cbData);
5332 stream = PyvirStream_Get(pyobj_stream);
5334 Py_INCREF(pyobj_cbData);
5336 LIBVIRT_BEGIN_ALLOW_THREADS;
5337 ret = virStreamEventAddCallback(stream, events, cb, pyobj_cbData,
5338 libvirt_virStreamEventFreeFunc);
5339 LIBVIRT_END_ALLOW_THREADS;
5341 if (ret < 0) {
5342 Py_DECREF(pyobj_cbData);
5345 py_retval = libvirt_intWrap(ret);
5346 return py_retval;
5349 static PyObject *
5350 libvirt_virStreamRecv(PyObject *self ATTRIBUTE_UNUSED,
5351 PyObject *args)
5353 PyObject *pyobj_stream;
5354 virStreamPtr stream;
5355 char *buf = NULL;
5356 int ret;
5357 int nbytes;
5359 if (!PyArg_ParseTuple(args, (char *) "Oi:virStreamRecv",
5360 &pyobj_stream, &nbytes)) {
5361 DEBUG("%s failed to parse tuple\n", __FUNCTION__);
5362 return VIR_PY_NONE;
5364 stream = PyvirStream_Get(pyobj_stream);
5366 if (VIR_ALLOC_N(buf, nbytes+1 > 0 ? nbytes+1 : 1) < 0)
5367 return VIR_PY_NONE;
5369 LIBVIRT_BEGIN_ALLOW_THREADS;
5370 ret = virStreamRecv(stream, buf, nbytes);
5371 LIBVIRT_END_ALLOW_THREADS;
5373 buf[ret > -1 ? ret : 0] = '\0';
5374 DEBUG("StreamRecv ret=%d strlen=%d\n", ret, (int) strlen(buf));
5376 if (ret == -2)
5377 return libvirt_intWrap(ret);
5378 if (ret < 0)
5379 return VIR_PY_NONE;
5380 return libvirt_charPtrSizeWrap((char *) buf, (Py_ssize_t) ret);
5383 static PyObject *
5384 libvirt_virStreamSend(PyObject *self ATTRIBUTE_UNUSED,
5385 PyObject *args)
5387 PyObject *py_retval;
5388 PyObject *pyobj_stream;
5389 virStreamPtr stream;
5390 char *data;
5391 int datalen;
5392 int ret;
5393 int nbytes;
5395 if (!PyArg_ParseTuple(args, (char *) "Oz#i:virStreamRecv",
5396 &pyobj_stream, &data, &datalen, &nbytes)) {
5397 DEBUG("%s failed to parse tuple\n", __FUNCTION__);
5398 return VIR_PY_INT_FAIL;
5400 stream = PyvirStream_Get(pyobj_stream);
5402 LIBVIRT_BEGIN_ALLOW_THREADS;
5403 ret = virStreamSend(stream, data, nbytes);
5404 LIBVIRT_END_ALLOW_THREADS;
5406 DEBUG("StreamSend ret=%d\n", ret);
5408 py_retval = libvirt_intWrap(ret);
5409 return py_retval;
5412 static PyObject *
5413 libvirt_virDomainSendKey(PyObject *self ATTRIBUTE_UNUSED,
5414 PyObject *args)
5416 PyObject *py_retval;
5417 virDomainPtr domain;
5418 PyObject *pyobj_domain;
5419 PyObject *pyobj_list;
5420 int codeset;
5421 int holdtime;
5422 unsigned int flags;
5423 int ret;
5424 int i;
5425 unsigned int keycodes[VIR_DOMAIN_SEND_KEY_MAX_KEYS];
5426 unsigned int nkeycodes;
5428 if (!PyArg_ParseTuple(args, (char *)"OiiOii:virDomainSendKey",
5429 &pyobj_domain, &codeset, &holdtime, &pyobj_list,
5430 &nkeycodes, &flags)) {
5431 DEBUG("%s failed to parse tuple\n", __FUNCTION__);
5432 return VIR_PY_INT_FAIL;
5434 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
5436 if (!PyList_Check(pyobj_list)) {
5437 return VIR_PY_INT_FAIL;
5440 if (nkeycodes != PyList_Size(pyobj_list) ||
5441 nkeycodes > VIR_DOMAIN_SEND_KEY_MAX_KEYS) {
5442 return VIR_PY_INT_FAIL;
5445 for (i = 0; i < nkeycodes; i++) {
5446 keycodes[i] = (int)PyInt_AsLong(PyList_GetItem(pyobj_list, i));
5449 LIBVIRT_BEGIN_ALLOW_THREADS;
5450 ret = virDomainSendKey(domain, codeset, holdtime, keycodes, nkeycodes, flags);
5451 LIBVIRT_END_ALLOW_THREADS;
5453 DEBUG("virDomainSendKey ret=%d\n", ret);
5455 py_retval = libvirt_intWrap(ret);
5456 return py_retval;
5459 static PyObject *
5460 libvirt_virDomainMigrateGetMaxSpeed(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
5461 PyObject *py_retval;
5462 int c_retval;
5463 unsigned long bandwidth;
5464 virDomainPtr domain;
5465 PyObject *pyobj_domain;
5466 unsigned int flags = 0;
5468 if (!PyArg_ParseTuple(args, (char *)"Oi:virDomainMigrateGetMaxSpeed",
5469 &pyobj_domain, &flags))
5470 return(NULL);
5472 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
5474 LIBVIRT_BEGIN_ALLOW_THREADS;
5475 c_retval = virDomainMigrateGetMaxSpeed(domain, &bandwidth, flags);
5476 LIBVIRT_END_ALLOW_THREADS;
5478 if (c_retval < 0)
5479 return VIR_PY_INT_FAIL;
5480 py_retval = libvirt_ulongWrap(bandwidth);
5481 return(py_retval);
5484 static PyObject *
5485 libvirt_virDomainBlockPeek(PyObject *self ATTRIBUTE_UNUSED,
5486 PyObject *args) {
5487 PyObject *py_retval = NULL;
5488 int c_retval;
5489 virDomainPtr domain;
5490 PyObject *pyobj_domain;
5491 const char *disk;
5492 unsigned long long offset;
5493 size_t size;
5494 char *buf;
5495 unsigned int flags;
5497 if (!PyArg_ParseTuple(args, (char *)"OzLni:virDomainBlockPeek", &pyobj_domain,
5498 &disk, &offset, &size, &flags))
5499 return(NULL);
5501 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
5503 if (VIR_ALLOC_N(buf, size) < 0)
5504 return VIR_PY_NONE;
5506 LIBVIRT_BEGIN_ALLOW_THREADS;
5507 c_retval = virDomainBlockPeek(domain, disk, offset, size, buf, flags);
5508 LIBVIRT_END_ALLOW_THREADS;
5510 if (c_retval < 0)
5511 goto cleanup;
5513 py_retval = PyString_FromStringAndSize(buf, size);
5515 cleanup:
5516 VIR_FREE(buf);
5517 return py_retval;
5520 static PyObject *
5521 libvirt_virDomainMemoryPeek(PyObject *self ATTRIBUTE_UNUSED,
5522 PyObject *args) {
5523 PyObject *py_retval = NULL;
5524 int c_retval;
5525 virDomainPtr domain;
5526 PyObject *pyobj_domain;
5527 unsigned long long start;
5528 size_t size;
5529 char *buf;
5530 unsigned int flags;
5532 if (!PyArg_ParseTuple(args, (char *)"OLni:virDomainMemoryPeek", &pyobj_domain,
5533 &start, &size, &flags))
5534 return(NULL);
5536 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
5538 if (VIR_ALLOC_N(buf, size) < 0)
5539 return VIR_PY_NONE;
5541 LIBVIRT_BEGIN_ALLOW_THREADS;
5542 c_retval = virDomainMemoryPeek(domain, start, size, buf, flags);
5543 LIBVIRT_END_ALLOW_THREADS;
5545 if (c_retval < 0)
5546 goto cleanup;
5548 py_retval = PyString_FromStringAndSize(buf, size);
5550 cleanup:
5551 VIR_FREE(buf);
5552 return py_retval;
5555 /************************************************************************
5557 * The registration stuff *
5559 ************************************************************************/
5560 static PyMethodDef libvirtMethods[] = {
5561 #include "libvirt-export.c"
5562 {(char *) "virGetVersion", libvirt_virGetVersion, METH_VARARGS, NULL},
5563 {(char *) "virConnectGetVersion", libvirt_virConnectGetVersion, METH_VARARGS, NULL},
5564 {(char *) "virConnectGetLibVersion", libvirt_virConnectGetLibVersion, METH_VARARGS, NULL},
5565 {(char *) "virConnectOpenAuth", libvirt_virConnectOpenAuth, METH_VARARGS, NULL},
5566 {(char *) "virConnectListDomainsID", libvirt_virConnectListDomainsID, METH_VARARGS, NULL},
5567 {(char *) "virConnectListDefinedDomains", libvirt_virConnectListDefinedDomains, METH_VARARGS, NULL},
5568 {(char *) "virConnectDomainEventRegister", libvirt_virConnectDomainEventRegister, METH_VARARGS, NULL},
5569 {(char *) "virConnectDomainEventDeregister", libvirt_virConnectDomainEventDeregister, METH_VARARGS, NULL},
5570 {(char *) "virConnectDomainEventRegisterAny", libvirt_virConnectDomainEventRegisterAny, METH_VARARGS, NULL},
5571 {(char *) "virConnectDomainEventDeregisterAny", libvirt_virConnectDomainEventDeregisterAny, METH_VARARGS, NULL},
5572 {(char *) "virStreamEventAddCallback", libvirt_virStreamEventAddCallback, METH_VARARGS, NULL},
5573 {(char *) "virStreamRecv", libvirt_virStreamRecv, METH_VARARGS, NULL},
5574 {(char *) "virStreamSend", libvirt_virStreamSend, METH_VARARGS, NULL},
5575 {(char *) "virDomainGetInfo", libvirt_virDomainGetInfo, METH_VARARGS, NULL},
5576 {(char *) "virDomainGetState", libvirt_virDomainGetState, METH_VARARGS, NULL},
5577 {(char *) "virDomainGetControlInfo", libvirt_virDomainGetControlInfo, METH_VARARGS, NULL},
5578 {(char *) "virDomainGetBlockInfo", libvirt_virDomainGetBlockInfo, METH_VARARGS, NULL},
5579 {(char *) "virNodeGetInfo", libvirt_virNodeGetInfo, METH_VARARGS, NULL},
5580 {(char *) "virNodeGetCPUStats", libvirt_virNodeGetCPUStats, METH_VARARGS, NULL},
5581 {(char *) "virNodeGetMemoryStats", libvirt_virNodeGetMemoryStats, METH_VARARGS, NULL},
5582 {(char *) "virDomainGetUUID", libvirt_virDomainGetUUID, METH_VARARGS, NULL},
5583 {(char *) "virDomainGetUUIDString", libvirt_virDomainGetUUIDString, METH_VARARGS, NULL},
5584 {(char *) "virDomainLookupByUUID", libvirt_virDomainLookupByUUID, METH_VARARGS, NULL},
5585 {(char *) "virRegisterErrorHandler", libvirt_virRegisterErrorHandler, METH_VARARGS, NULL},
5586 {(char *) "virGetLastError", libvirt_virGetLastError, METH_VARARGS, NULL},
5587 {(char *) "virConnGetLastError", libvirt_virConnGetLastError, METH_VARARGS, NULL},
5588 {(char *) "virConnectListNetworks", libvirt_virConnectListNetworks, METH_VARARGS, NULL},
5589 {(char *) "virConnectListDefinedNetworks", libvirt_virConnectListDefinedNetworks, METH_VARARGS, NULL},
5590 {(char *) "virNetworkGetUUID", libvirt_virNetworkGetUUID, METH_VARARGS, NULL},
5591 {(char *) "virNetworkGetUUIDString", libvirt_virNetworkGetUUIDString, METH_VARARGS, NULL},
5592 {(char *) "virNetworkLookupByUUID", libvirt_virNetworkLookupByUUID, METH_VARARGS, NULL},
5593 {(char *) "virDomainGetAutostart", libvirt_virDomainGetAutostart, METH_VARARGS, NULL},
5594 {(char *) "virNetworkGetAutostart", libvirt_virNetworkGetAutostart, METH_VARARGS, NULL},
5595 {(char *) "virDomainBlockStats", libvirt_virDomainBlockStats, METH_VARARGS, NULL},
5596 {(char *) "virDomainBlockStatsFlags", libvirt_virDomainBlockStatsFlags, METH_VARARGS, NULL},
5597 {(char *) "virDomainGetCPUStats", libvirt_virDomainGetCPUStats, METH_VARARGS, NULL},
5598 {(char *) "virDomainInterfaceStats", libvirt_virDomainInterfaceStats, METH_VARARGS, NULL},
5599 {(char *) "virDomainMemoryStats", libvirt_virDomainMemoryStats, METH_VARARGS, NULL},
5600 {(char *) "virNodeGetCellsFreeMemory", libvirt_virNodeGetCellsFreeMemory, METH_VARARGS, NULL},
5601 {(char *) "virDomainGetSchedulerType", libvirt_virDomainGetSchedulerType, METH_VARARGS, NULL},
5602 {(char *) "virDomainGetSchedulerParameters", libvirt_virDomainGetSchedulerParameters, METH_VARARGS, NULL},
5603 {(char *) "virDomainGetSchedulerParametersFlags", libvirt_virDomainGetSchedulerParametersFlags, METH_VARARGS, NULL},
5604 {(char *) "virDomainSetSchedulerParameters", libvirt_virDomainSetSchedulerParameters, METH_VARARGS, NULL},
5605 {(char *) "virDomainSetSchedulerParametersFlags", libvirt_virDomainSetSchedulerParametersFlags, METH_VARARGS, NULL},
5606 {(char *) "virDomainSetBlkioParameters", libvirt_virDomainSetBlkioParameters, METH_VARARGS, NULL},
5607 {(char *) "virDomainGetBlkioParameters", libvirt_virDomainGetBlkioParameters, METH_VARARGS, NULL},
5608 {(char *) "virDomainSetMemoryParameters", libvirt_virDomainSetMemoryParameters, METH_VARARGS, NULL},
5609 {(char *) "virDomainGetMemoryParameters", libvirt_virDomainGetMemoryParameters, METH_VARARGS, NULL},
5610 {(char *) "virDomainSetNumaParameters", libvirt_virDomainSetNumaParameters, METH_VARARGS, NULL},
5611 {(char *) "virDomainGetNumaParameters", libvirt_virDomainGetNumaParameters, METH_VARARGS, NULL},
5612 {(char *) "virDomainSetInterfaceParameters", libvirt_virDomainSetInterfaceParameters, METH_VARARGS, NULL},
5613 {(char *) "virDomainGetInterfaceParameters", libvirt_virDomainGetInterfaceParameters, METH_VARARGS, NULL},
5614 {(char *) "virDomainGetVcpus", libvirt_virDomainGetVcpus, METH_VARARGS, NULL},
5615 {(char *) "virDomainPinVcpu", libvirt_virDomainPinVcpu, METH_VARARGS, NULL},
5616 {(char *) "virDomainPinVcpuFlags", libvirt_virDomainPinVcpuFlags, METH_VARARGS, NULL},
5617 {(char *) "virDomainGetVcpuPinInfo", libvirt_virDomainGetVcpuPinInfo, METH_VARARGS, NULL},
5618 {(char *) "virConnectListStoragePools", libvirt_virConnectListStoragePools, METH_VARARGS, NULL},
5619 {(char *) "virConnectListDefinedStoragePools", libvirt_virConnectListDefinedStoragePools, METH_VARARGS, NULL},
5620 {(char *) "virStoragePoolGetAutostart", libvirt_virStoragePoolGetAutostart, METH_VARARGS, NULL},
5621 {(char *) "virStoragePoolListVolumes", libvirt_virStoragePoolListVolumes, METH_VARARGS, NULL},
5622 {(char *) "virStoragePoolGetInfo", libvirt_virStoragePoolGetInfo, METH_VARARGS, NULL},
5623 {(char *) "virStorageVolGetInfo", libvirt_virStorageVolGetInfo, METH_VARARGS, NULL},
5624 {(char *) "virStoragePoolGetUUID", libvirt_virStoragePoolGetUUID, METH_VARARGS, NULL},
5625 {(char *) "virStoragePoolGetUUIDString", libvirt_virStoragePoolGetUUIDString, METH_VARARGS, NULL},
5626 {(char *) "virStoragePoolLookupByUUID", libvirt_virStoragePoolLookupByUUID, METH_VARARGS, NULL},
5627 {(char *) "virEventRegisterImpl", libvirt_virEventRegisterImpl, METH_VARARGS, NULL},
5628 {(char *) "virEventAddHandle", libvirt_virEventAddHandle, METH_VARARGS, NULL},
5629 {(char *) "virEventAddTimeout", libvirt_virEventAddTimeout, METH_VARARGS, NULL},
5630 {(char *) "virEventInvokeHandleCallback", libvirt_virEventInvokeHandleCallback, METH_VARARGS, NULL},
5631 {(char *) "virEventInvokeTimeoutCallback", libvirt_virEventInvokeTimeoutCallback, METH_VARARGS, NULL},
5632 {(char *) "virNodeListDevices", libvirt_virNodeListDevices, METH_VARARGS, NULL},
5633 {(char *) "virNodeDeviceListCaps", libvirt_virNodeDeviceListCaps, METH_VARARGS, NULL},
5634 {(char *) "virSecretGetUUID", libvirt_virSecretGetUUID, METH_VARARGS, NULL},
5635 {(char *) "virSecretGetUUIDString", libvirt_virSecretGetUUIDString, METH_VARARGS, NULL},
5636 {(char *) "virSecretLookupByUUID", libvirt_virSecretLookupByUUID, METH_VARARGS, NULL},
5637 {(char *) "virConnectListSecrets", libvirt_virConnectListSecrets, METH_VARARGS, NULL},
5638 {(char *) "virSecretGetValue", libvirt_virSecretGetValue, METH_VARARGS, NULL},
5639 {(char *) "virSecretSetValue", libvirt_virSecretSetValue, METH_VARARGS, NULL},
5640 {(char *) "virNWFilterGetUUID", libvirt_virNWFilterGetUUID, METH_VARARGS, NULL},
5641 {(char *) "virNWFilterGetUUIDString", libvirt_virNWFilterGetUUIDString, METH_VARARGS, NULL},
5642 {(char *) "virNWFilterLookupByUUID", libvirt_virNWFilterLookupByUUID, METH_VARARGS, NULL},
5643 {(char *) "virConnectListNWFilters", libvirt_virConnectListNWFilters, METH_VARARGS, NULL},
5644 {(char *) "virConnectListInterfaces", libvirt_virConnectListInterfaces, METH_VARARGS, NULL},
5645 {(char *) "virConnectListDefinedInterfaces", libvirt_virConnectListDefinedInterfaces, METH_VARARGS, NULL},
5646 {(char *) "virConnectBaselineCPU", libvirt_virConnectBaselineCPU, METH_VARARGS, NULL},
5647 {(char *) "virDomainGetJobInfo", libvirt_virDomainGetJobInfo, METH_VARARGS, NULL},
5648 {(char *) "virDomainSnapshotListNames", libvirt_virDomainSnapshotListNames, METH_VARARGS, NULL},
5649 {(char *) "virDomainSnapshotListChildrenNames", libvirt_virDomainSnapshotListChildrenNames, METH_VARARGS, NULL},
5650 {(char *) "virDomainRevertToSnapshot", libvirt_virDomainRevertToSnapshot, METH_VARARGS, NULL},
5651 {(char *) "virDomainGetBlockJobInfo", libvirt_virDomainGetBlockJobInfo, METH_VARARGS, NULL},
5652 {(char *) "virDomainSetBlockIoTune", libvirt_virDomainSetBlockIoTune, METH_VARARGS, NULL},
5653 {(char *) "virDomainGetBlockIoTune", libvirt_virDomainGetBlockIoTune, METH_VARARGS, NULL},
5654 {(char *) "virDomainSendKey", libvirt_virDomainSendKey, METH_VARARGS, NULL},
5655 {(char *) "virDomainMigrateGetMaxSpeed", libvirt_virDomainMigrateGetMaxSpeed, METH_VARARGS, NULL},
5656 {(char *) "virDomainBlockPeek", libvirt_virDomainBlockPeek, METH_VARARGS, NULL},
5657 {(char *) "virDomainMemoryPeek", libvirt_virDomainMemoryPeek, METH_VARARGS, NULL},
5658 {(char *) "virDomainGetDiskErrors", libvirt_virDomainGetDiskErrors, METH_VARARGS, NULL},
5659 {NULL, NULL, 0, NULL}
5662 void
5663 #ifndef __CYGWIN__
5664 initlibvirtmod
5665 #else
5666 initcygvirtmod
5667 #endif
5668 (void)
5670 static int initialized = 0;
5672 if (initialized != 0)
5673 return;
5675 if (virInitialize() < 0)
5676 return;
5678 /* initialize the python extension module */
5679 Py_InitModule((char *)
5680 #ifndef __CYGWIN__
5681 "libvirtmod"
5682 #else
5683 "cygvirtmod"
5684 #endif
5685 , libvirtMethods);
5687 initialized = 1;