python: Add python bindings for virDomainMemoryStats
[libvirt-python/ericb.git] / libvirt-override.c
blobdb4c0e10c139571305e6db1f1dd6c24317e49d16
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-2009 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 #include <Python.h>
20 #include "libvirt/libvirt.h"
21 #include "libvirt/virterror.h"
22 #include "typewrappers.h"
23 #include "libvirt.h"
25 #ifndef __CYGWIN__
26 extern void initlibvirtmod(void);
27 #else
28 extern void initcygvirtmod(void);
29 #endif
31 /* The two-statement sequence "Py_INCREF(Py_None); return Py_None;"
32 is so common that we encapsulate it here. Now, each use is simply
33 return VIR_PY_NONE; */
34 #define VIR_PY_NONE (Py_INCREF (Py_None), Py_None)
35 #define VIR_PY_INT_FAIL (libvirt_intWrap(-1))
36 #define VIR_PY_INT_SUCCESS (libvirt_intWrap(0))
38 static char *py_str(PyObject *obj)
40 PyObject *str = PyObject_Str(obj);
41 if (!str) {
42 PyErr_Print();
43 PyErr_Clear();
44 return NULL;
46 return PyString_AsString(str);
50 /************************************************************************
51 * *
52 * Statistics *
53 * *
54 ************************************************************************/
56 static PyObject *
57 libvirt_virDomainBlockStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
58 virDomainPtr domain;
59 PyObject *pyobj_domain;
60 char * path;
61 int c_retval;
62 virDomainBlockStatsStruct stats;
63 PyObject *info;
65 if (!PyArg_ParseTuple(args, (char *)"Oz:virDomainBlockStats",
66 &pyobj_domain,&path))
67 return(NULL);
68 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
70 LIBVIRT_BEGIN_ALLOW_THREADS;
71 c_retval = virDomainBlockStats(domain, path, &stats, sizeof(stats));
72 LIBVIRT_END_ALLOW_THREADS;
74 if (c_retval < 0)
75 return VIR_PY_NONE;
77 /* convert to a Python tuple of long objects */
78 if ((info = PyTuple_New(5)) == NULL)
79 return VIR_PY_NONE;
80 PyTuple_SetItem(info, 0, PyLong_FromLongLong(stats.rd_req));
81 PyTuple_SetItem(info, 1, PyLong_FromLongLong(stats.rd_bytes));
82 PyTuple_SetItem(info, 2, PyLong_FromLongLong(stats.wr_req));
83 PyTuple_SetItem(info, 3, PyLong_FromLongLong(stats.wr_bytes));
84 PyTuple_SetItem(info, 4, PyLong_FromLongLong(stats.errs));
85 return(info);
88 static PyObject *
89 libvirt_virDomainInterfaceStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
90 virDomainPtr domain;
91 PyObject *pyobj_domain;
92 char * path;
93 int c_retval;
94 virDomainInterfaceStatsStruct stats;
95 PyObject *info;
97 if (!PyArg_ParseTuple(args, (char *)"Oz:virDomainInterfaceStats",
98 &pyobj_domain,&path))
99 return(NULL);
100 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
102 LIBVIRT_BEGIN_ALLOW_THREADS;
103 c_retval = virDomainInterfaceStats(domain, path, &stats, sizeof(stats));
104 LIBVIRT_END_ALLOW_THREADS;
106 if (c_retval < 0)
107 return VIR_PY_NONE;
109 /* convert to a Python tuple of long objects */
110 if ((info = PyTuple_New(8)) == NULL)
111 return VIR_PY_NONE;
112 PyTuple_SetItem(info, 0, PyLong_FromLongLong(stats.rx_bytes));
113 PyTuple_SetItem(info, 1, PyLong_FromLongLong(stats.rx_packets));
114 PyTuple_SetItem(info, 2, PyLong_FromLongLong(stats.rx_errs));
115 PyTuple_SetItem(info, 3, PyLong_FromLongLong(stats.rx_drop));
116 PyTuple_SetItem(info, 4, PyLong_FromLongLong(stats.tx_bytes));
117 PyTuple_SetItem(info, 5, PyLong_FromLongLong(stats.tx_packets));
118 PyTuple_SetItem(info, 6, PyLong_FromLongLong(stats.tx_errs));
119 PyTuple_SetItem(info, 7, PyLong_FromLongLong(stats.tx_drop));
120 return(info);
123 static PyObject *
124 libvirt_virDomainMemoryStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
125 virDomainPtr domain;
126 PyObject *pyobj_domain;
127 unsigned int nr_stats, i;
128 virDomainMemoryStatStruct stats[VIR_DOMAIN_MEMORY_STAT_NR];
129 PyObject *info;
131 if (!PyArg_ParseTuple(args, (char *)"O:virDomainMemoryStats", &pyobj_domain))
132 return(NULL);
133 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
135 nr_stats = virDomainMemoryStats(domain, stats,
136 VIR_DOMAIN_MEMORY_STAT_NR, 0);
137 if (nr_stats == -1)
138 return VIR_PY_NONE;
140 /* convert to a Python dictionary */
141 if ((info = PyDict_New()) == NULL)
142 return VIR_PY_NONE;
144 for (i = 0; i < nr_stats; i++) {
145 if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_SWAP_IN)
146 PyDict_SetItem(info, libvirt_constcharPtrWrap("swap_in"),
147 PyLong_FromUnsignedLongLong(stats[i].val));
148 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_SWAP_OUT)
149 PyDict_SetItem(info, libvirt_constcharPtrWrap("swap_out"),
150 PyLong_FromUnsignedLongLong(stats[i].val));
151 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_MAJOR_FAULT)
152 PyDict_SetItem(info, libvirt_constcharPtrWrap("major_fault"),
153 PyLong_FromUnsignedLongLong(stats[i].val));
154 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_MINOR_FAULT)
155 PyDict_SetItem(info, libvirt_constcharPtrWrap("minor_fault"),
156 PyLong_FromUnsignedLongLong(stats[i].val));
157 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_UNUSED)
158 PyDict_SetItem(info, libvirt_constcharPtrWrap("unused"),
159 PyLong_FromUnsignedLongLong(stats[i].val));
160 else if (stats[i].tag == VIR_DOMAIN_MEMORY_STAT_AVAILABLE)
161 PyDict_SetItem(info, libvirt_constcharPtrWrap("available"),
162 PyLong_FromUnsignedLongLong(stats[i].val));
164 return info;
167 static PyObject *
168 libvirt_virDomainGetSchedulerType(PyObject *self ATTRIBUTE_UNUSED,
169 PyObject *args) {
170 virDomainPtr domain;
171 PyObject *pyobj_domain, *info;
172 char *c_retval;
173 int nparams;
175 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetScedulerType",
176 &pyobj_domain))
177 return(NULL);
178 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
180 LIBVIRT_BEGIN_ALLOW_THREADS;
181 c_retval = virDomainGetSchedulerType(domain, &nparams);
182 LIBVIRT_END_ALLOW_THREADS;
183 if (c_retval == NULL)
184 return VIR_PY_NONE;
186 /* convert to a Python tuple of long objects */
187 if ((info = PyTuple_New(2)) == NULL) {
188 free(c_retval);
189 return VIR_PY_NONE;
192 PyTuple_SetItem(info, 0, libvirt_constcharPtrWrap(c_retval));
193 PyTuple_SetItem(info, 1, PyInt_FromLong((long)nparams));
194 free(c_retval);
195 return(info);
198 static PyObject *
199 libvirt_virDomainGetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
200 PyObject *args) {
201 virDomainPtr domain;
202 PyObject *pyobj_domain, *info;
203 char *c_retval;
204 int i_retval;
205 int nparams, i;
206 virSchedParameterPtr params;
208 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetScedulerParameters",
209 &pyobj_domain))
210 return(NULL);
211 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
213 LIBVIRT_BEGIN_ALLOW_THREADS;
214 c_retval = virDomainGetSchedulerType(domain, &nparams);
215 LIBVIRT_END_ALLOW_THREADS;
217 if (c_retval == NULL)
218 return VIR_PY_NONE;
219 free(c_retval);
221 if ((params = malloc(sizeof(*params)*nparams)) == NULL)
222 return VIR_PY_NONE;
224 LIBVIRT_BEGIN_ALLOW_THREADS;
225 i_retval = virDomainGetSchedulerParameters(domain, params, &nparams);
226 LIBVIRT_END_ALLOW_THREADS;
228 if (i_retval < 0) {
229 free(params);
230 return VIR_PY_NONE;
233 /* convert to a Python tuple of long objects */
234 if ((info = PyDict_New()) == NULL) {
235 free(params);
236 return VIR_PY_NONE;
238 for (i = 0 ; i < nparams ; i++) {
239 PyObject *key, *val;
241 switch (params[i].type) {
242 case VIR_DOMAIN_SCHED_FIELD_INT:
243 val = PyInt_FromLong((long)params[i].value.i);
244 break;
246 case VIR_DOMAIN_SCHED_FIELD_UINT:
247 val = PyInt_FromLong((long)params[i].value.ui);
248 break;
250 case VIR_DOMAIN_SCHED_FIELD_LLONG:
251 val = PyLong_FromLongLong((long long)params[i].value.l);
252 break;
254 case VIR_DOMAIN_SCHED_FIELD_ULLONG:
255 val = PyLong_FromLongLong((long long)params[i].value.ul);
256 break;
258 case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
259 val = PyFloat_FromDouble((double)params[i].value.d);
260 break;
262 case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
263 val = PyBool_FromLong((long)params[i].value.b);
264 break;
266 default:
267 free(params);
268 Py_DECREF(info);
269 return VIR_PY_NONE;
272 key = libvirt_constcharPtrWrap(params[i].field);
273 PyDict_SetItem(info, key, val);
275 free(params);
276 return(info);
279 static PyObject *
280 libvirt_virDomainSetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
281 PyObject *args) {
282 virDomainPtr domain;
283 PyObject *pyobj_domain, *info;
284 char *c_retval;
285 int i_retval;
286 int nparams, i;
287 virSchedParameterPtr params;
289 if (!PyArg_ParseTuple(args, (char *)"OO:virDomainSetScedulerParameters",
290 &pyobj_domain, &info))
291 return(NULL);
292 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
294 LIBVIRT_BEGIN_ALLOW_THREADS;
295 c_retval = virDomainGetSchedulerType(domain, &nparams);
296 LIBVIRT_END_ALLOW_THREADS;
298 if (c_retval == NULL)
299 return VIR_PY_INT_FAIL;
300 free(c_retval);
302 if ((params = malloc(sizeof(*params)*nparams)) == NULL)
303 return VIR_PY_INT_FAIL;
305 LIBVIRT_BEGIN_ALLOW_THREADS;
306 i_retval = virDomainGetSchedulerParameters(domain, params, &nparams);
307 LIBVIRT_END_ALLOW_THREADS;
309 if (i_retval < 0) {
310 free(params);
311 return VIR_PY_INT_FAIL;
314 /* convert to a Python tuple of long objects */
315 for (i = 0 ; i < nparams ; i++) {
316 PyObject *key, *val;
317 key = libvirt_constcharPtrWrap(params[i].field);
318 val = PyDict_GetItem(info, key);
319 Py_DECREF(key);
321 if (val == NULL)
322 continue;
324 switch (params[i].type) {
325 case VIR_DOMAIN_SCHED_FIELD_INT:
326 params[i].value.i = (int)PyInt_AS_LONG(val);
327 break;
329 case VIR_DOMAIN_SCHED_FIELD_UINT:
330 params[i].value.ui = (unsigned int)PyInt_AS_LONG(val);
331 break;
333 case VIR_DOMAIN_SCHED_FIELD_LLONG:
334 params[i].value.l = (long long)PyLong_AsLongLong(val);
335 break;
337 case VIR_DOMAIN_SCHED_FIELD_ULLONG:
338 params[i].value.ul = (unsigned long long)PyLong_AsLongLong(val);
339 break;
341 case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
342 params[i].value.d = (double)PyFloat_AsDouble(val);
343 break;
345 case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
347 /* Hack - Python's definition of Py_True breaks strict
348 * aliasing rules, so can't directly compare :-(
350 PyObject *hacktrue = PyBool_FromLong(1);
351 params[i].value.b = hacktrue == val ? 1 : 0;
352 Py_DECREF(hacktrue);
354 break;
356 default:
357 free(params);
358 return VIR_PY_INT_FAIL;
362 LIBVIRT_BEGIN_ALLOW_THREADS;
363 i_retval = virDomainSetSchedulerParameters(domain, params, nparams);
364 LIBVIRT_END_ALLOW_THREADS;
365 if (i_retval < 0) {
366 free(params);
367 return VIR_PY_INT_FAIL;
370 free(params);
371 return VIR_PY_INT_SUCCESS;
374 static PyObject *
375 libvirt_virDomainGetVcpus(PyObject *self ATTRIBUTE_UNUSED,
376 PyObject *args) {
377 virDomainPtr domain;
378 PyObject *pyobj_domain, *pyretval = NULL, *pycpuinfo = NULL, *pycpumap = NULL;
379 virNodeInfo nodeinfo;
380 virDomainInfo dominfo;
381 virVcpuInfoPtr cpuinfo = NULL;
382 unsigned char *cpumap = NULL;
383 int cpumaplen, i;
384 int i_retval;
386 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetVcpus",
387 &pyobj_domain))
388 return(NULL);
389 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
391 LIBVIRT_BEGIN_ALLOW_THREADS;
392 i_retval = virNodeGetInfo(virDomainGetConnect(domain), &nodeinfo);
393 LIBVIRT_END_ALLOW_THREADS;
394 if (i_retval < 0)
395 return VIR_PY_NONE;
397 LIBVIRT_BEGIN_ALLOW_THREADS;
398 i_retval = virDomainGetInfo(domain, &dominfo);
399 LIBVIRT_END_ALLOW_THREADS;
400 if (i_retval < 0)
401 return VIR_PY_NONE;
403 if ((cpuinfo = malloc(sizeof(*cpuinfo)*dominfo.nrVirtCpu)) == NULL)
404 return VIR_PY_NONE;
406 cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
407 if ((cpumap = malloc(dominfo.nrVirtCpu * cpumaplen)) == NULL)
408 goto cleanup;
410 LIBVIRT_BEGIN_ALLOW_THREADS;
411 i_retval = virDomainGetVcpus(domain,
412 cpuinfo, dominfo.nrVirtCpu,
413 cpumap, cpumaplen);
414 LIBVIRT_END_ALLOW_THREADS;
415 if (i_retval < 0)
416 goto cleanup;
418 /* convert to a Python tuple of long objects */
419 if ((pyretval = PyTuple_New(2)) == NULL)
420 goto cleanup;
421 if ((pycpuinfo = PyList_New(dominfo.nrVirtCpu)) == NULL)
422 goto cleanup;
423 if ((pycpumap = PyList_New(dominfo.nrVirtCpu)) == NULL)
424 goto cleanup;
426 for (i = 0 ; i < dominfo.nrVirtCpu ; i++) {
427 PyObject *info = PyTuple_New(4);
428 if (info == NULL)
429 goto cleanup;
430 PyTuple_SetItem(info, 0, PyInt_FromLong((long)cpuinfo[i].number));
431 PyTuple_SetItem(info, 1, PyInt_FromLong((long)cpuinfo[i].state));
432 PyTuple_SetItem(info, 2, PyLong_FromLongLong((long long)cpuinfo[i].cpuTime));
433 PyTuple_SetItem(info, 3, PyInt_FromLong((long)cpuinfo[i].cpu));
434 PyList_SetItem(pycpuinfo, i, info);
436 for (i = 0 ; i < dominfo.nrVirtCpu ; i++) {
437 PyObject *info = PyTuple_New(VIR_NODEINFO_MAXCPUS(nodeinfo));
438 int j;
439 if (info == NULL)
440 goto cleanup;
441 for (j = 0 ; j < VIR_NODEINFO_MAXCPUS(nodeinfo) ; j++) {
442 PyTuple_SetItem(info, j, PyBool_FromLong(VIR_CPU_USABLE(cpumap, cpumaplen, i, j)));
444 PyList_SetItem(pycpumap, i, info);
446 PyTuple_SetItem(pyretval, 0, pycpuinfo);
447 PyTuple_SetItem(pyretval, 1, pycpumap);
449 free(cpuinfo);
450 free(cpumap);
452 return(pyretval);
454 cleanup:
455 free(cpuinfo);
456 free(cpumap);
457 /* NB, Py_DECREF is a badly defined macro, so we require
458 * braces here to avoid 'ambiguous else' warnings from
459 * the compiler.
460 * NB. this comment is true at of time of writing wrt to
461 * at least python2.5.
463 if (pyretval) { Py_DECREF(pyretval); }
464 if (pycpuinfo) { Py_DECREF(pycpuinfo); }
465 if (pycpumap) { Py_DECREF(pycpumap); }
466 return VIR_PY_NONE;
470 static PyObject *
471 libvirt_virDomainPinVcpu(PyObject *self ATTRIBUTE_UNUSED,
472 PyObject *args) {
473 virDomainPtr domain;
474 PyObject *pyobj_domain, *pycpumap, *truth;
475 virNodeInfo nodeinfo;
476 unsigned char *cpumap;
477 int cpumaplen, i, vcpu;
478 int i_retval;
480 if (!PyArg_ParseTuple(args, (char *)"OiO:virDomainPinVcpu",
481 &pyobj_domain, &vcpu, &pycpumap))
482 return(NULL);
483 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
485 LIBVIRT_BEGIN_ALLOW_THREADS;
486 i_retval = virNodeGetInfo(virDomainGetConnect(domain), &nodeinfo);
487 LIBVIRT_END_ALLOW_THREADS;
488 if (i_retval < 0)
489 return VIR_PY_INT_FAIL;
491 cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
492 if ((cpumap = malloc(cpumaplen)) == NULL)
493 return VIR_PY_INT_FAIL;
494 memset(cpumap, 0, cpumaplen);
496 truth = PyBool_FromLong(1);
497 for (i = 0 ; i < VIR_NODEINFO_MAXCPUS(nodeinfo) ; i++) {
498 PyObject *flag = PyTuple_GetItem(pycpumap, i);
499 if (flag == truth)
500 VIR_USE_CPU(cpumap, i);
501 else
502 VIR_UNUSE_CPU(cpumap, i);
505 LIBVIRT_BEGIN_ALLOW_THREADS;
506 i_retval = virDomainPinVcpu(domain, vcpu, cpumap, cpumaplen);
507 LIBVIRT_END_ALLOW_THREADS;
508 Py_DECREF(truth);
509 free(cpumap);
511 if (i_retval < 0)
512 return VIR_PY_INT_FAIL;
514 return VIR_PY_INT_SUCCESS;
518 /************************************************************************
520 * Global error handler at the Python level *
522 ************************************************************************/
524 static PyObject *libvirt_virPythonErrorFuncHandler = NULL;
525 static PyObject *libvirt_virPythonErrorFuncCtxt = NULL;
527 static PyObject *
528 libvirt_virGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args ATTRIBUTE_UNUSED)
530 virError *err;
531 PyObject *info;
533 if ((err = virGetLastError()) == NULL)
534 return VIR_PY_NONE;
536 if ((info = PyTuple_New(9)) == NULL)
537 return VIR_PY_NONE;
538 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err->code));
539 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err->domain));
540 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err->message));
541 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err->level));
542 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err->str1));
543 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err->str2));
544 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err->str3));
545 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err->int1));
546 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err->int2));
548 return info;
551 static PyObject *
552 libvirt_virConnGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
554 virError *err;
555 PyObject *info;
556 virConnectPtr conn;
557 PyObject *pyobj_conn;
559 if (!PyArg_ParseTuple(args, (char *)"O:virConGetLastError", &pyobj_conn))
560 return(NULL);
561 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
563 LIBVIRT_BEGIN_ALLOW_THREADS;
564 err = virConnGetLastError(conn);
565 LIBVIRT_END_ALLOW_THREADS;
566 if (err == NULL)
567 return VIR_PY_NONE;
569 if ((info = PyTuple_New(9)) == NULL)
570 return VIR_PY_NONE;
571 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err->code));
572 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err->domain));
573 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err->message));
574 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err->level));
575 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err->str1));
576 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err->str2));
577 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err->str3));
578 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err->int1));
579 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err->int2));
581 return info;
584 static void
585 libvirt_virErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, virErrorPtr err)
587 PyObject *list, *info;
588 PyObject *result;
590 #ifdef DEBUG_ERROR
591 printf("libvirt_virErrorFuncHandler(%p, %s, ...) called\n", ctx,
592 err->message);
593 #endif
595 if ((err == NULL) || (err->code == VIR_ERR_OK))
596 return;
598 LIBVIRT_ENSURE_THREAD_STATE;
600 if ((libvirt_virPythonErrorFuncHandler == NULL) ||
601 (libvirt_virPythonErrorFuncHandler == Py_None)) {
602 virDefaultErrorFunc(err);
603 } else {
604 list = PyTuple_New(2);
605 info = PyTuple_New(9);
606 PyTuple_SetItem(list, 0, libvirt_virPythonErrorFuncCtxt);
607 PyTuple_SetItem(list, 1, info);
608 Py_XINCREF(libvirt_virPythonErrorFuncCtxt);
609 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err->code));
610 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err->domain));
611 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err->message));
612 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err->level));
613 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err->str1));
614 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err->str2));
615 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err->str3));
616 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err->int1));
617 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err->int2));
618 /* TODO pass conn and dom if available */
619 result = PyEval_CallObject(libvirt_virPythonErrorFuncHandler, list);
620 Py_XDECREF(list);
621 Py_XDECREF(result);
624 LIBVIRT_RELEASE_THREAD_STATE;
627 static PyObject *
628 libvirt_virRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
629 PyObject * args)
631 PyObject *py_retval;
632 PyObject *pyobj_f;
633 PyObject *pyobj_ctx;
635 if (!PyArg_ParseTuple
636 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
637 &pyobj_ctx))
638 return (NULL);
640 #ifdef DEBUG_ERROR
641 printf("libvirt_virRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
642 pyobj_f);
643 #endif
645 virSetErrorFunc(NULL, libvirt_virErrorFuncHandler);
646 if (libvirt_virPythonErrorFuncHandler != NULL) {
647 Py_XDECREF(libvirt_virPythonErrorFuncHandler);
649 if (libvirt_virPythonErrorFuncCtxt != NULL) {
650 Py_XDECREF(libvirt_virPythonErrorFuncCtxt);
653 if ((pyobj_f == Py_None) && (pyobj_ctx == Py_None)) {
654 libvirt_virPythonErrorFuncHandler = NULL;
655 libvirt_virPythonErrorFuncCtxt = NULL;
656 } else {
657 Py_XINCREF(pyobj_ctx);
658 Py_XINCREF(pyobj_f);
660 /* TODO: check f is a function ! */
661 libvirt_virPythonErrorFuncHandler = pyobj_f;
662 libvirt_virPythonErrorFuncCtxt = pyobj_ctx;
665 py_retval = libvirt_intWrap(1);
666 return (py_retval);
669 static int virConnectCredCallbackWrapper(virConnectCredentialPtr cred,
670 unsigned int ncred,
671 void *cbdata) {
672 PyObject *list;
673 PyObject *pycred;
674 PyObject *pyauth = (PyObject *)cbdata;
675 PyObject *pycbdata;
676 PyObject *pycb;
677 PyObject *pyret;
678 int ret = -1, i;
680 LIBVIRT_ENSURE_THREAD_STATE;
682 pycb = PyList_GetItem(pyauth, 1);
683 pycbdata = PyList_GetItem(pyauth, 2);
685 list = PyTuple_New(2);
686 pycred = PyTuple_New(ncred);
687 for (i = 0 ; i < ncred ; i++) {
688 PyObject *pycreditem;
689 pycreditem = PyList_New(5);
690 Py_INCREF(Py_None);
691 PyTuple_SetItem(pycred, i, pycreditem);
692 PyList_SetItem(pycreditem, 0, PyInt_FromLong((long) cred[i].type));
693 PyList_SetItem(pycreditem, 1, PyString_FromString(cred[i].prompt));
694 if (cred[i].challenge) {
695 PyList_SetItem(pycreditem, 2, PyString_FromString(cred[i].challenge));
696 } else {
697 Py_INCREF(Py_None);
698 PyList_SetItem(pycreditem, 2, Py_None);
700 if (cred[i].defresult) {
701 PyList_SetItem(pycreditem, 3, PyString_FromString(cred[i].defresult));
702 } else {
703 Py_INCREF(Py_None);
704 PyList_SetItem(pycreditem, 3, Py_None);
706 PyList_SetItem(pycreditem, 4, Py_None);
709 PyTuple_SetItem(list, 0, pycred);
710 Py_XINCREF(pycbdata);
711 PyTuple_SetItem(list, 1, pycbdata);
713 PyErr_Clear();
714 pyret = PyEval_CallObject(pycb, list);
715 if (PyErr_Occurred())
716 goto cleanup;
718 ret = PyLong_AsLong(pyret);
719 if (ret == 0) {
720 for (i = 0 ; i < ncred ; i++) {
721 PyObject *pycreditem;
722 PyObject *pyresult;
723 char *result = NULL;
724 pycreditem = PyTuple_GetItem(pycred, i);
725 pyresult = PyList_GetItem(pycreditem, 4);
726 if (pyresult != Py_None)
727 result = PyString_AsString(pyresult);
728 if (result != NULL) {
729 cred[i].result = strdup(result);
730 cred[i].resultlen = strlen(result);
731 } else {
732 cred[i].result = NULL;
733 cred[i].resultlen = 0;
738 cleanup:
739 Py_XDECREF(list);
740 Py_XDECREF(pyret);
742 LIBVIRT_RELEASE_THREAD_STATE;
744 return ret;
748 static PyObject *
749 libvirt_virConnectOpenAuth(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
750 PyObject *py_retval;
751 virConnectPtr c_retval;
752 char * name;
753 int flags;
754 PyObject *pyauth;
755 PyObject *pycredcb;
756 PyObject *pycredtype;
757 virConnectAuth auth;
759 if (!PyArg_ParseTuple(args, (char *)"zOi:virConnectOpenAuth", &name, &pyauth, &flags))
760 return(NULL);
762 pycredtype = PyList_GetItem(pyauth, 0);
763 pycredcb = PyList_GetItem(pyauth, 1);
765 auth.ncredtype = PyList_Size(pycredtype);
766 if (auth.ncredtype) {
767 int i;
768 auth.credtype = malloc(sizeof(*auth.credtype) * auth.ncredtype);
769 if (auth.credtype == NULL)
770 return VIR_PY_NONE;
771 for (i = 0 ; i < auth.ncredtype ; i++) {
772 PyObject *val;
773 val = PyList_GetItem(pycredtype, i);
774 auth.credtype[i] = (int)PyLong_AsLong(val);
777 auth.cb = pycredcb ? virConnectCredCallbackWrapper : NULL;
778 auth.cbdata = pyauth;
780 LIBVIRT_BEGIN_ALLOW_THREADS;
782 c_retval = virConnectOpenAuth(name, &auth, flags);
783 LIBVIRT_END_ALLOW_THREADS;
784 py_retval = libvirt_virConnectPtrWrap((virConnectPtr) c_retval);
785 return(py_retval);
789 /************************************************************************
791 * Wrappers for functions where generator fails *
793 ************************************************************************/
795 static PyObject *
796 libvirt_virGetVersion (PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
798 char *type = NULL;
799 unsigned long libVer, typeVer = 0;
800 int c_retval;
802 if (!PyArg_ParseTuple (args, (char *) "|s", &type))
803 return NULL;
805 LIBVIRT_BEGIN_ALLOW_THREADS;
807 if (type == NULL)
808 c_retval = virGetVersion (&libVer, NULL, NULL);
809 else
810 c_retval = virGetVersion (&libVer, type, &typeVer);
812 LIBVIRT_END_ALLOW_THREADS;
814 if (c_retval == -1)
815 return VIR_PY_NONE;
817 if (type == NULL)
818 return PyInt_FromLong (libVer);
819 else
820 return Py_BuildValue ((char *) "kk", libVer, typeVer);
823 static PyObject *
824 libvirt_virConnectGetLibVersion (PyObject *self ATTRIBUTE_UNUSED,
825 PyObject *args)
827 unsigned long libVer;
828 int c_retval;
829 virConnectPtr conn;
830 PyObject *pyobj_conn;
832 if (!PyArg_ParseTuple(args, (char *)"O:virConnectGetLibVersion",
833 &pyobj_conn))
834 return(NULL);
835 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
837 LIBVIRT_BEGIN_ALLOW_THREADS;
839 c_retval = virConnectGetLibVersion(conn, &libVer);
841 LIBVIRT_END_ALLOW_THREADS;
843 if (c_retval == -1)
844 return VIR_PY_INT_FAIL;
846 return PyInt_FromLong (libVer);
849 static PyObject *
850 libvirt_virConnectListDomainsID(PyObject *self ATTRIBUTE_UNUSED,
851 PyObject *args) {
852 PyObject *py_retval;
853 int ids[500], c_retval, i;
854 virConnectPtr conn;
855 PyObject *pyobj_conn;
858 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDomains", &pyobj_conn))
859 return(NULL);
860 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
862 LIBVIRT_BEGIN_ALLOW_THREADS;
863 c_retval = virConnectListDomains(conn, &ids[0], 500);
864 LIBVIRT_END_ALLOW_THREADS;
865 if (c_retval < 0)
866 return VIR_PY_NONE;
867 py_retval = PyList_New(c_retval);
868 for (i = 0;i < c_retval;i++) {
869 PyList_SetItem(py_retval, i, libvirt_intWrap(ids[i]));
871 return(py_retval);
874 static PyObject *
875 libvirt_virConnectListDefinedDomains(PyObject *self ATTRIBUTE_UNUSED,
876 PyObject *args) {
877 PyObject *py_retval;
878 char **names = NULL;
879 int c_retval, i;
880 virConnectPtr conn;
881 PyObject *pyobj_conn;
884 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedDomains", &pyobj_conn))
885 return(NULL);
886 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
888 LIBVIRT_BEGIN_ALLOW_THREADS;
889 c_retval = virConnectNumOfDefinedDomains(conn);
890 LIBVIRT_END_ALLOW_THREADS;
891 if (c_retval < 0)
892 return VIR_PY_NONE;
894 if (c_retval) {
895 names = malloc(sizeof(*names) * c_retval);
896 if (!names)
897 return VIR_PY_NONE;
898 LIBVIRT_BEGIN_ALLOW_THREADS;
899 c_retval = virConnectListDefinedDomains(conn, names, c_retval);
900 LIBVIRT_END_ALLOW_THREADS;
901 if (c_retval < 0) {
902 free(names);
903 return VIR_PY_NONE;
906 py_retval = PyList_New(c_retval);
908 if (names) {
909 for (i = 0;i < c_retval;i++) {
910 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
911 free(names[i]);
913 free(names);
916 return(py_retval);
919 static PyObject *
920 libvirt_virDomainGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
921 PyObject *py_retval;
922 int c_retval;
923 virDomainPtr domain;
924 PyObject *pyobj_domain;
925 virDomainInfo info;
927 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetInfo", &pyobj_domain))
928 return(NULL);
929 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
931 LIBVIRT_BEGIN_ALLOW_THREADS;
932 c_retval = virDomainGetInfo(domain, &info);
933 LIBVIRT_END_ALLOW_THREADS;
934 if (c_retval < 0)
935 return VIR_PY_NONE;
936 py_retval = PyList_New(5);
937 PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.state));
938 PyList_SetItem(py_retval, 1, libvirt_ulongWrap(info.maxMem));
939 PyList_SetItem(py_retval, 2, libvirt_ulongWrap(info.memory));
940 PyList_SetItem(py_retval, 3, libvirt_intWrap((int) info.nrVirtCpu));
941 PyList_SetItem(py_retval, 4,
942 libvirt_longlongWrap((unsigned long long) info.cpuTime));
943 return(py_retval);
946 static PyObject *
947 libvirt_virNodeGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
948 PyObject *py_retval;
949 int c_retval;
950 virConnectPtr conn;
951 PyObject *pyobj_conn;
952 virNodeInfo info;
954 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetInfo", &pyobj_conn))
955 return(NULL);
956 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
958 LIBVIRT_BEGIN_ALLOW_THREADS;
959 c_retval = virNodeGetInfo(conn, &info);
960 LIBVIRT_END_ALLOW_THREADS;
961 if (c_retval < 0)
962 return VIR_PY_NONE;
963 py_retval = PyList_New(8);
964 PyList_SetItem(py_retval, 0, libvirt_constcharPtrWrap(&info.model[0]));
965 PyList_SetItem(py_retval, 1, libvirt_longWrap((long) info.memory >> 10));
966 PyList_SetItem(py_retval, 2, libvirt_intWrap((int) info.cpus));
967 PyList_SetItem(py_retval, 3, libvirt_intWrap((int) info.mhz));
968 PyList_SetItem(py_retval, 4, libvirt_intWrap((int) info.nodes));
969 PyList_SetItem(py_retval, 5, libvirt_intWrap((int) info.sockets));
970 PyList_SetItem(py_retval, 6, libvirt_intWrap((int) info.cores));
971 PyList_SetItem(py_retval, 7, libvirt_intWrap((int) info.threads));
972 return(py_retval);
975 static PyObject *
976 libvirt_virDomainGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
977 PyObject *py_retval;
978 unsigned char uuid[VIR_UUID_BUFLEN];
979 virDomainPtr domain;
980 PyObject *pyobj_domain;
981 int c_retval;
983 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetUUID", &pyobj_domain))
984 return(NULL);
985 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
987 if (domain == NULL)
988 return VIR_PY_NONE;
989 LIBVIRT_BEGIN_ALLOW_THREADS;
990 c_retval = virDomainGetUUID(domain, &uuid[0]);
991 LIBVIRT_END_ALLOW_THREADS;
993 if (c_retval < 0)
994 return VIR_PY_NONE;
995 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
997 return(py_retval);
1000 static PyObject *
1001 libvirt_virDomainGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
1002 PyObject *args) {
1003 PyObject *py_retval;
1004 char uuidstr[VIR_UUID_STRING_BUFLEN];
1005 virDomainPtr dom;
1006 PyObject *pyobj_dom;
1007 int c_retval;
1009 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetUUIDString",
1010 &pyobj_dom))
1011 return(NULL);
1012 dom = (virDomainPtr) PyvirDomain_Get(pyobj_dom);
1014 if (dom == NULL)
1015 return VIR_PY_NONE;
1016 LIBVIRT_BEGIN_ALLOW_THREADS;
1017 c_retval = virDomainGetUUIDString(dom, &uuidstr[0]);
1018 LIBVIRT_END_ALLOW_THREADS;
1020 if (c_retval < 0)
1021 return VIR_PY_NONE;
1023 py_retval = PyString_FromString((char *) &uuidstr[0]);
1024 return(py_retval);
1027 static PyObject *
1028 libvirt_virDomainLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1029 PyObject *py_retval;
1030 virDomainPtr c_retval;
1031 virConnectPtr conn;
1032 PyObject *pyobj_conn;
1033 unsigned char * uuid;
1034 int len;
1036 if (!PyArg_ParseTuple(args, (char *)"Oz#:virDomainLookupByUUID", &pyobj_conn, &uuid, &len))
1037 return(NULL);
1038 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1040 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
1041 return VIR_PY_NONE;
1043 LIBVIRT_BEGIN_ALLOW_THREADS;
1044 c_retval = virDomainLookupByUUID(conn, uuid);
1045 LIBVIRT_END_ALLOW_THREADS;
1046 py_retval = libvirt_virDomainPtrWrap((virDomainPtr) c_retval);
1047 return(py_retval);
1051 static PyObject *
1052 libvirt_virConnectListNetworks(PyObject *self ATTRIBUTE_UNUSED,
1053 PyObject *args) {
1054 PyObject *py_retval;
1055 char **names = NULL;
1056 int c_retval, i;
1057 virConnectPtr conn;
1058 PyObject *pyobj_conn;
1061 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListNetworks", &pyobj_conn))
1062 return(NULL);
1063 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1065 LIBVIRT_BEGIN_ALLOW_THREADS;
1066 c_retval = virConnectNumOfNetworks(conn);
1067 LIBVIRT_END_ALLOW_THREADS;
1068 if (c_retval < 0)
1069 return VIR_PY_NONE;
1071 if (c_retval) {
1072 names = malloc(sizeof(*names) * c_retval);
1073 if (!names)
1074 return VIR_PY_NONE;
1075 LIBVIRT_BEGIN_ALLOW_THREADS;
1076 c_retval = virConnectListNetworks(conn, names, c_retval);
1077 LIBVIRT_END_ALLOW_THREADS;
1078 if (c_retval < 0) {
1079 free(names);
1080 return VIR_PY_NONE;
1083 py_retval = PyList_New(c_retval);
1085 if (names) {
1086 for (i = 0;i < c_retval;i++) {
1087 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1088 free(names[i]);
1090 free(names);
1093 return(py_retval);
1097 static PyObject *
1098 libvirt_virConnectListDefinedNetworks(PyObject *self ATTRIBUTE_UNUSED,
1099 PyObject *args) {
1100 PyObject *py_retval;
1101 char **names = NULL;
1102 int c_retval, i;
1103 virConnectPtr conn;
1104 PyObject *pyobj_conn;
1107 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedNetworks", &pyobj_conn))
1108 return(NULL);
1109 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1111 LIBVIRT_BEGIN_ALLOW_THREADS;
1112 c_retval = virConnectNumOfDefinedNetworks(conn);
1113 LIBVIRT_END_ALLOW_THREADS;
1114 if (c_retval < 0)
1115 return VIR_PY_NONE;
1117 if (c_retval) {
1118 names = malloc(sizeof(*names) * c_retval);
1119 if (!names)
1120 return VIR_PY_NONE;
1121 LIBVIRT_BEGIN_ALLOW_THREADS;
1122 c_retval = virConnectListDefinedNetworks(conn, names, c_retval);
1123 LIBVIRT_END_ALLOW_THREADS;
1124 if (c_retval < 0) {
1125 free(names);
1126 return VIR_PY_NONE;
1129 py_retval = PyList_New(c_retval);
1131 if (names) {
1132 for (i = 0;i < c_retval;i++) {
1133 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1134 free(names[i]);
1136 free(names);
1139 return(py_retval);
1143 static PyObject *
1144 libvirt_virNetworkGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1145 PyObject *py_retval;
1146 unsigned char uuid[VIR_UUID_BUFLEN];
1147 virNetworkPtr domain;
1148 PyObject *pyobj_domain;
1149 int c_retval;
1151 if (!PyArg_ParseTuple(args, (char *)"O:virNetworkGetUUID", &pyobj_domain))
1152 return(NULL);
1153 domain = (virNetworkPtr) PyvirNetwork_Get(pyobj_domain);
1155 if (domain == NULL)
1156 return VIR_PY_NONE;
1157 LIBVIRT_BEGIN_ALLOW_THREADS;
1158 c_retval = virNetworkGetUUID(domain, &uuid[0]);
1159 LIBVIRT_END_ALLOW_THREADS;
1161 if (c_retval < 0)
1162 return VIR_PY_NONE;
1163 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
1165 return(py_retval);
1168 static PyObject *
1169 libvirt_virNetworkGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
1170 PyObject *args) {
1171 PyObject *py_retval;
1172 char uuidstr[VIR_UUID_STRING_BUFLEN];
1173 virNetworkPtr net;
1174 PyObject *pyobj_net;
1175 int c_retval;
1177 if (!PyArg_ParseTuple(args, (char *)"O:virNetworkGetUUIDString",
1178 &pyobj_net))
1179 return(NULL);
1180 net = (virNetworkPtr) PyvirNetwork_Get(pyobj_net);
1182 if (net == NULL)
1183 return VIR_PY_NONE;
1184 LIBVIRT_BEGIN_ALLOW_THREADS;
1185 c_retval = virNetworkGetUUIDString(net, &uuidstr[0]);
1186 LIBVIRT_END_ALLOW_THREADS;
1188 if (c_retval < 0)
1189 return VIR_PY_NONE;
1191 py_retval = PyString_FromString((char *) &uuidstr[0]);
1192 return(py_retval);
1195 static PyObject *
1196 libvirt_virNetworkLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1197 PyObject *py_retval;
1198 virNetworkPtr c_retval;
1199 virConnectPtr conn;
1200 PyObject *pyobj_conn;
1201 unsigned char * uuid;
1202 int len;
1204 if (!PyArg_ParseTuple(args, (char *)"Oz#:virNetworkLookupByUUID", &pyobj_conn, &uuid, &len))
1205 return(NULL);
1206 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1208 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
1209 return VIR_PY_NONE;
1211 LIBVIRT_BEGIN_ALLOW_THREADS;
1212 c_retval = virNetworkLookupByUUID(conn, uuid);
1213 LIBVIRT_END_ALLOW_THREADS;
1214 py_retval = libvirt_virNetworkPtrWrap((virNetworkPtr) c_retval);
1215 return(py_retval);
1219 static PyObject *
1220 libvirt_virDomainGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1221 PyObject *py_retval;
1222 int c_retval, autostart;
1223 virDomainPtr domain;
1224 PyObject *pyobj_domain;
1226 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetAutostart", &pyobj_domain))
1227 return(NULL);
1229 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1231 LIBVIRT_BEGIN_ALLOW_THREADS;
1232 c_retval = virDomainGetAutostart(domain, &autostart);
1233 LIBVIRT_END_ALLOW_THREADS;
1235 if (c_retval < 0)
1236 return VIR_PY_INT_FAIL;
1237 py_retval = libvirt_intWrap(autostart);
1238 return(py_retval);
1242 static PyObject *
1243 libvirt_virNetworkGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1244 PyObject *py_retval;
1245 int c_retval, autostart;
1246 virNetworkPtr network;
1247 PyObject *pyobj_network;
1249 if (!PyArg_ParseTuple(args, (char *)"O:virNetworkGetAutostart", &pyobj_network))
1250 return(NULL);
1252 network = (virNetworkPtr) PyvirNetwork_Get(pyobj_network);
1254 LIBVIRT_BEGIN_ALLOW_THREADS;
1255 c_retval = virNetworkGetAutostart(network, &autostart);
1256 LIBVIRT_END_ALLOW_THREADS;
1258 if (c_retval < 0)
1259 return VIR_PY_INT_FAIL;
1260 py_retval = libvirt_intWrap(autostart);
1261 return(py_retval);
1264 static PyObject *
1265 libvirt_virNodeGetCellsFreeMemory(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
1267 PyObject *py_retval;
1268 PyObject *pyobj_conn;
1269 int startCell, maxCells, c_retval, i;
1270 virConnectPtr conn;
1271 unsigned long long *freeMems;
1273 if (!PyArg_ParseTuple(args, (char *)"Oii:virNodeGetCellsFreeMemory", &pyobj_conn, &startCell, &maxCells))
1274 return(NULL);
1276 if ((startCell < 0) || (maxCells <= 0) || (startCell + maxCells > 10000))
1277 goto error;
1279 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1280 freeMems =
1281 malloc(maxCells * sizeof(*freeMems));
1282 if (freeMems == NULL)
1283 goto error;
1285 LIBVIRT_BEGIN_ALLOW_THREADS;
1286 c_retval = virNodeGetCellsFreeMemory(conn, freeMems, startCell, maxCells);
1287 LIBVIRT_END_ALLOW_THREADS;
1289 if (c_retval < 0) {
1290 free(freeMems);
1291 error:
1292 return VIR_PY_NONE;
1294 py_retval = PyList_New(c_retval);
1295 for (i = 0;i < c_retval;i++) {
1296 PyList_SetItem(py_retval, i,
1297 libvirt_longlongWrap((long long) freeMems[i]));
1299 free(freeMems);
1300 return(py_retval);
1304 static PyObject *
1305 libvirt_virConnectListStoragePools(PyObject *self ATTRIBUTE_UNUSED,
1306 PyObject *args) {
1307 PyObject *py_retval;
1308 char **names = NULL;
1309 int c_retval, i;
1310 virConnectPtr conn;
1311 PyObject *pyobj_conn;
1314 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListStoragePools", &pyobj_conn))
1315 return(NULL);
1316 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1318 LIBVIRT_BEGIN_ALLOW_THREADS;
1319 c_retval = virConnectNumOfStoragePools(conn);
1320 LIBVIRT_END_ALLOW_THREADS;
1321 if (c_retval < 0)
1322 return VIR_PY_NONE;
1324 if (c_retval) {
1325 names = malloc(sizeof(*names) * c_retval);
1326 if (!names)
1327 return VIR_PY_NONE;
1328 LIBVIRT_BEGIN_ALLOW_THREADS;
1329 c_retval = virConnectListStoragePools(conn, names, c_retval);
1330 LIBVIRT_END_ALLOW_THREADS;
1331 if (c_retval < 0) {
1332 free(names);
1333 return VIR_PY_NONE;
1336 py_retval = PyList_New(c_retval);
1337 if (py_retval == NULL) {
1338 if (names) {
1339 for (i = 0;i < c_retval;i++)
1340 free(names[i]);
1341 free(names);
1343 return VIR_PY_NONE;
1346 if (names) {
1347 for (i = 0;i < c_retval;i++) {
1348 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1349 free(names[i]);
1351 free(names);
1354 return(py_retval);
1358 static PyObject *
1359 libvirt_virConnectListDefinedStoragePools(PyObject *self ATTRIBUTE_UNUSED,
1360 PyObject *args) {
1361 PyObject *py_retval;
1362 char **names = NULL;
1363 int c_retval, i;
1364 virConnectPtr conn;
1365 PyObject *pyobj_conn;
1368 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedStoragePools", &pyobj_conn))
1369 return(NULL);
1370 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1372 LIBVIRT_BEGIN_ALLOW_THREADS;
1373 c_retval = virConnectNumOfDefinedStoragePools(conn);
1374 LIBVIRT_END_ALLOW_THREADS;
1375 if (c_retval < 0)
1376 return VIR_PY_NONE;
1378 if (c_retval) {
1379 names = malloc(sizeof(*names) * c_retval);
1380 if (!names)
1381 return VIR_PY_NONE;
1382 LIBVIRT_BEGIN_ALLOW_THREADS;
1383 c_retval = virConnectListDefinedStoragePools(conn, names, c_retval);
1384 LIBVIRT_END_ALLOW_THREADS;
1385 if (c_retval < 0) {
1386 free(names);
1387 return VIR_PY_NONE;
1390 py_retval = PyList_New(c_retval);
1391 if (py_retval == NULL) {
1392 if (names) {
1393 for (i = 0;i < c_retval;i++)
1394 free(names[i]);
1395 free(names);
1397 return VIR_PY_NONE;
1400 if (names) {
1401 for (i = 0;i < c_retval;i++) {
1402 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1403 free(names[i]);
1405 free(names);
1408 return(py_retval);
1412 static PyObject *
1413 libvirt_virStoragePoolListVolumes(PyObject *self ATTRIBUTE_UNUSED,
1414 PyObject *args) {
1415 PyObject *py_retval;
1416 char **names = NULL;
1417 int c_retval, i;
1418 virStoragePoolPtr pool;
1419 PyObject *pyobj_pool;
1422 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolListVolumes", &pyobj_pool))
1423 return(NULL);
1424 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
1426 LIBVIRT_BEGIN_ALLOW_THREADS;
1427 c_retval = virStoragePoolNumOfVolumes(pool);
1428 LIBVIRT_END_ALLOW_THREADS;
1429 if (c_retval < 0)
1430 return VIR_PY_NONE;
1432 if (c_retval) {
1433 names = malloc(sizeof(*names) * c_retval);
1434 if (!names)
1435 return VIR_PY_NONE;
1436 LIBVIRT_BEGIN_ALLOW_THREADS;
1437 c_retval = virStoragePoolListVolumes(pool, names, c_retval);
1438 LIBVIRT_END_ALLOW_THREADS;
1439 if (c_retval < 0) {
1440 free(names);
1441 return VIR_PY_NONE;
1444 py_retval = PyList_New(c_retval);
1445 if (py_retval == NULL) {
1446 if (names) {
1447 for (i = 0;i < c_retval;i++)
1448 free(names[i]);
1449 free(names);
1451 return VIR_PY_NONE;
1454 if (names) {
1455 for (i = 0;i < c_retval;i++) {
1456 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1457 free(names[i]);
1459 free(names);
1462 return(py_retval);
1465 static PyObject *
1466 libvirt_virStoragePoolGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1467 PyObject *py_retval;
1468 int c_retval, autostart;
1469 virStoragePoolPtr pool;
1470 PyObject *pyobj_pool;
1472 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetAutostart", &pyobj_pool))
1473 return(NULL);
1475 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
1477 LIBVIRT_BEGIN_ALLOW_THREADS;
1478 c_retval = virStoragePoolGetAutostart(pool, &autostart);
1479 LIBVIRT_END_ALLOW_THREADS;
1481 if (c_retval < 0)
1482 return VIR_PY_NONE;
1484 py_retval = libvirt_intWrap(autostart);
1485 return(py_retval);
1488 static PyObject *
1489 libvirt_virStoragePoolGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1490 PyObject *py_retval;
1491 int c_retval;
1492 virStoragePoolPtr pool;
1493 PyObject *pyobj_pool;
1494 virStoragePoolInfo info;
1496 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetInfo", &pyobj_pool))
1497 return(NULL);
1498 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
1500 LIBVIRT_BEGIN_ALLOW_THREADS;
1501 c_retval = virStoragePoolGetInfo(pool, &info);
1502 LIBVIRT_END_ALLOW_THREADS;
1503 if (c_retval < 0)
1504 return VIR_PY_NONE;
1506 if ((py_retval = PyList_New(4)) == NULL)
1507 return VIR_PY_NONE;
1509 PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.state));
1510 PyList_SetItem(py_retval, 1,
1511 libvirt_longlongWrap((unsigned long long) info.capacity));
1512 PyList_SetItem(py_retval, 2,
1513 libvirt_longlongWrap((unsigned long long) info.allocation));
1514 PyList_SetItem(py_retval, 3,
1515 libvirt_longlongWrap((unsigned long long) info.available));
1516 return(py_retval);
1520 static PyObject *
1521 libvirt_virStorageVolGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1522 PyObject *py_retval;
1523 int c_retval;
1524 virStorageVolPtr pool;
1525 PyObject *pyobj_pool;
1526 virStorageVolInfo info;
1528 if (!PyArg_ParseTuple(args, (char *)"O:virStorageVolGetInfo", &pyobj_pool))
1529 return(NULL);
1530 pool = (virStorageVolPtr) PyvirStorageVol_Get(pyobj_pool);
1532 LIBVIRT_BEGIN_ALLOW_THREADS;
1533 c_retval = virStorageVolGetInfo(pool, &info);
1534 LIBVIRT_END_ALLOW_THREADS;
1535 if (c_retval < 0)
1536 return VIR_PY_NONE;
1538 if ((py_retval = PyList_New(3)) == NULL)
1539 return VIR_PY_NONE;
1540 PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.type));
1541 PyList_SetItem(py_retval, 1,
1542 libvirt_longlongWrap((unsigned long long) info.capacity));
1543 PyList_SetItem(py_retval, 2,
1544 libvirt_longlongWrap((unsigned long long) info.allocation));
1545 return(py_retval);
1548 static PyObject *
1549 libvirt_virStoragePoolGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1550 PyObject *py_retval;
1551 unsigned char uuid[VIR_UUID_BUFLEN];
1552 virStoragePoolPtr pool;
1553 PyObject *pyobj_pool;
1554 int c_retval;
1556 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetUUID", &pyobj_pool))
1557 return(NULL);
1558 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
1560 if (pool == NULL)
1561 return VIR_PY_NONE;
1562 LIBVIRT_BEGIN_ALLOW_THREADS;
1563 c_retval = virStoragePoolGetUUID(pool, &uuid[0]);
1564 LIBVIRT_END_ALLOW_THREADS;
1566 if (c_retval < 0)
1567 return VIR_PY_NONE;
1569 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
1571 return(py_retval);
1574 static PyObject *
1575 libvirt_virStoragePoolGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
1576 PyObject *args) {
1577 PyObject *py_retval;
1578 char uuidstr[VIR_UUID_STRING_BUFLEN];
1579 virStoragePoolPtr pool;
1580 PyObject *pyobj_pool;
1581 int c_retval;
1583 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetUUIDString", &pyobj_pool))
1584 return(NULL);
1585 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
1587 if (pool == NULL)
1588 return VIR_PY_NONE;
1589 LIBVIRT_BEGIN_ALLOW_THREADS;
1590 c_retval = virStoragePoolGetUUIDString(pool, &uuidstr[0]);
1591 LIBVIRT_END_ALLOW_THREADS;
1593 if (c_retval < 0)
1594 return VIR_PY_NONE;
1596 py_retval = PyString_FromString((char *) &uuidstr[0]);
1597 return(py_retval);
1600 static PyObject *
1601 libvirt_virStoragePoolLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1602 PyObject *py_retval;
1603 virStoragePoolPtr c_retval;
1604 virConnectPtr conn;
1605 PyObject *pyobj_conn;
1606 unsigned char * uuid;
1607 int len;
1609 if (!PyArg_ParseTuple(args, (char *)"Oz#:virStoragePoolLookupByUUID", &pyobj_conn, &uuid, &len))
1610 return(NULL);
1611 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1613 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
1614 return VIR_PY_NONE;
1616 LIBVIRT_BEGIN_ALLOW_THREADS;
1617 c_retval = virStoragePoolLookupByUUID(conn, uuid);
1618 LIBVIRT_END_ALLOW_THREADS;
1619 py_retval = libvirt_virStoragePoolPtrWrap((virStoragePoolPtr) c_retval);
1620 return(py_retval);
1623 static PyObject *
1624 libvirt_virNodeListDevices(PyObject *self ATTRIBUTE_UNUSED,
1625 PyObject *args) {
1626 PyObject *py_retval;
1627 char **names = NULL;
1628 int c_retval, i;
1629 virConnectPtr conn;
1630 PyObject *pyobj_conn;
1631 char *cap;
1632 unsigned int flags;
1634 if (!PyArg_ParseTuple(args, (char *)"Ozi:virNodeListDevices",
1635 &pyobj_conn, &cap, &flags))
1636 return(NULL);
1637 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1639 LIBVIRT_BEGIN_ALLOW_THREADS;
1640 c_retval = virNodeNumOfDevices(conn, cap, flags);
1641 LIBVIRT_END_ALLOW_THREADS;
1642 if (c_retval < 0)
1643 return VIR_PY_NONE;
1645 if (c_retval) {
1646 names = malloc(sizeof(*names) * c_retval);
1647 if (!names)
1648 return VIR_PY_NONE;
1649 LIBVIRT_BEGIN_ALLOW_THREADS;
1650 c_retval = virNodeListDevices(conn, cap, names, c_retval, flags);
1651 LIBVIRT_END_ALLOW_THREADS;
1652 if (c_retval < 0) {
1653 free(names);
1654 return VIR_PY_NONE;
1657 py_retval = PyList_New(c_retval);
1659 if (names) {
1660 for (i = 0;i < c_retval;i++) {
1661 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1662 free(names[i]);
1664 free(names);
1667 return(py_retval);
1670 static PyObject *
1671 libvirt_virNodeDeviceListCaps(PyObject *self ATTRIBUTE_UNUSED,
1672 PyObject *args) {
1673 PyObject *py_retval;
1674 char **names = NULL;
1675 int c_retval, i;
1676 virNodeDevicePtr dev;
1677 PyObject *pyobj_dev;
1679 if (!PyArg_ParseTuple(args, (char *)"O:virNodeDeviceListCaps", &pyobj_dev))
1680 return(NULL);
1681 dev = (virNodeDevicePtr) PyvirNodeDevice_Get(pyobj_dev);
1683 LIBVIRT_BEGIN_ALLOW_THREADS;
1684 c_retval = virNodeDeviceNumOfCaps(dev);
1685 LIBVIRT_END_ALLOW_THREADS;
1686 if (c_retval < 0)
1687 return VIR_PY_NONE;
1689 if (c_retval) {
1690 names = malloc(sizeof(*names) * c_retval);
1691 if (!names)
1692 return VIR_PY_NONE;
1693 LIBVIRT_BEGIN_ALLOW_THREADS;
1694 c_retval = virNodeDeviceListCaps(dev, names, c_retval);
1695 LIBVIRT_END_ALLOW_THREADS;
1696 if (c_retval < 0) {
1697 free(names);
1698 return VIR_PY_NONE;
1701 py_retval = PyList_New(c_retval);
1703 if (names) {
1704 for (i = 0;i < c_retval;i++) {
1705 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1706 free(names[i]);
1708 free(names);
1711 return(py_retval);
1714 static PyObject *
1715 libvirt_virSecretGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1716 PyObject *py_retval;
1717 unsigned char uuid[VIR_UUID_BUFLEN];
1718 virSecretPtr secret;
1719 PyObject *pyobj_secret;
1720 int c_retval;
1722 if (!PyArg_ParseTuple(args, (char *)"O:virSecretGetUUID", &pyobj_secret))
1723 return(NULL);
1724 secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
1726 if (secret == NULL)
1727 return VIR_PY_NONE;
1728 LIBVIRT_BEGIN_ALLOW_THREADS;
1729 c_retval = virSecretGetUUID(secret, &uuid[0]);
1730 LIBVIRT_END_ALLOW_THREADS;
1732 if (c_retval < 0)
1733 return VIR_PY_NONE;
1734 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
1736 return(py_retval);
1739 static PyObject *
1740 libvirt_virSecretGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
1741 PyObject *args) {
1742 PyObject *py_retval;
1743 char uuidstr[VIR_UUID_STRING_BUFLEN];
1744 virSecretPtr dom;
1745 PyObject *pyobj_dom;
1746 int c_retval;
1748 if (!PyArg_ParseTuple(args, (char *)"O:virSecretGetUUIDString",
1749 &pyobj_dom))
1750 return(NULL);
1751 dom = (virSecretPtr) PyvirSecret_Get(pyobj_dom);
1753 if (dom == NULL)
1754 return VIR_PY_NONE;
1755 LIBVIRT_BEGIN_ALLOW_THREADS;
1756 c_retval = virSecretGetUUIDString(dom, &uuidstr[0]);
1757 LIBVIRT_END_ALLOW_THREADS;
1759 if (c_retval < 0)
1760 return VIR_PY_NONE;
1762 py_retval = PyString_FromString((char *) &uuidstr[0]);
1763 return(py_retval);
1766 static PyObject *
1767 libvirt_virSecretLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1768 PyObject *py_retval;
1769 virSecretPtr c_retval;
1770 virConnectPtr conn;
1771 PyObject *pyobj_conn;
1772 unsigned char * uuid;
1773 int len;
1775 if (!PyArg_ParseTuple(args, (char *)"Oz#:virSecretLookupByUUID", &pyobj_conn, &uuid, &len))
1776 return(NULL);
1777 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1779 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
1780 return VIR_PY_NONE;
1782 LIBVIRT_BEGIN_ALLOW_THREADS;
1783 c_retval = virSecretLookupByUUID(conn, uuid);
1784 LIBVIRT_END_ALLOW_THREADS;
1785 py_retval = libvirt_virSecretPtrWrap((virSecretPtr) c_retval);
1786 return(py_retval);
1790 static PyObject *
1791 libvirt_virConnectListSecrets(PyObject *self ATTRIBUTE_UNUSED,
1792 PyObject *args) {
1793 PyObject *py_retval;
1794 char **uuids = NULL;
1795 virConnectPtr conn;
1796 int c_retval, i;
1797 PyObject *pyobj_conn;
1799 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListSecrets", &pyobj_conn))
1800 return NULL;
1801 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1803 LIBVIRT_BEGIN_ALLOW_THREADS;
1804 c_retval = virConnectNumOfSecrets(conn);
1805 LIBVIRT_END_ALLOW_THREADS;
1806 if (c_retval < 0)
1807 return VIR_PY_NONE;
1809 if (c_retval) {
1810 uuids = malloc(sizeof(*uuids) * c_retval);
1811 if (!uuids)
1812 return VIR_PY_NONE;
1813 LIBVIRT_BEGIN_ALLOW_THREADS;
1814 c_retval = virConnectListSecrets(conn, uuids, c_retval);
1815 LIBVIRT_END_ALLOW_THREADS;
1816 if (c_retval < 0) {
1817 free(uuids);
1818 return VIR_PY_NONE;
1821 py_retval = PyList_New(c_retval);
1823 if (uuids) {
1824 for (i = 0;i < c_retval;i++) {
1825 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(uuids[i]));
1826 free(uuids[i]);
1828 free(uuids);
1831 return py_retval;
1834 static PyObject *
1835 libvirt_virSecretGetValue(PyObject *self ATTRIBUTE_UNUSED,
1836 PyObject *args) {
1837 PyObject *py_retval;
1838 unsigned char *c_retval;
1839 size_t size;
1840 virSecretPtr secret;
1841 PyObject *pyobj_secret;
1842 unsigned int flags;
1844 if (!PyArg_ParseTuple(args, (char *)"Oi:virSecretGetValue", &pyobj_secret,
1845 &flags))
1846 return NULL;
1847 secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
1849 LIBVIRT_BEGIN_ALLOW_THREADS;
1850 c_retval = virSecretGetValue(secret, &size, flags);
1851 LIBVIRT_END_ALLOW_THREADS;
1853 if (c_retval == NULL)
1854 return VIR_PY_NONE;
1856 py_retval = PyString_FromStringAndSize((const char *)c_retval, size);
1857 memset(c_retval, 0, size);
1858 free(c_retval);
1860 return py_retval;
1863 static PyObject *
1864 libvirt_virSecretSetValue(PyObject *self ATTRIBUTE_UNUSED,
1865 PyObject *args) {
1866 PyObject *py_retval;
1867 int c_retval;
1868 virSecretPtr secret;
1869 PyObject *pyobj_secret;
1870 const char *value;
1871 int size;
1872 unsigned int flags;
1874 if (!PyArg_ParseTuple(args, (char *)"Oz#i:virSecretSetValue", &pyobj_secret,
1875 &value, &size, &flags))
1876 return NULL;
1877 secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
1879 LIBVIRT_BEGIN_ALLOW_THREADS;
1880 c_retval = virSecretSetValue(secret, (const unsigned char *)value, size,
1881 flags);
1882 LIBVIRT_END_ALLOW_THREADS;
1884 py_retval = libvirt_intWrap(c_retval);
1885 return py_retval;
1888 static PyObject *
1889 libvirt_virConnectListInterfaces(PyObject *self ATTRIBUTE_UNUSED,
1890 PyObject *args) {
1891 PyObject *py_retval;
1892 char **names = NULL;
1893 int c_retval, i;
1894 virConnectPtr conn;
1895 PyObject *pyobj_conn;
1898 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListInterfaces", &pyobj_conn))
1899 return(NULL);
1900 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1902 LIBVIRT_BEGIN_ALLOW_THREADS;
1903 c_retval = virConnectNumOfInterfaces(conn);
1904 LIBVIRT_END_ALLOW_THREADS;
1905 if (c_retval < 0)
1906 return VIR_PY_NONE;
1908 if (c_retval) {
1909 names = malloc(sizeof(*names) * c_retval);
1910 if (!names)
1911 return VIR_PY_NONE;
1912 LIBVIRT_BEGIN_ALLOW_THREADS;
1913 c_retval = virConnectListInterfaces(conn, names, c_retval);
1914 LIBVIRT_END_ALLOW_THREADS;
1915 if (c_retval < 0) {
1916 free(names);
1917 return VIR_PY_NONE;
1920 py_retval = PyList_New(c_retval);
1921 if (py_retval == NULL) {
1922 if (names) {
1923 for (i = 0;i < c_retval;i++)
1924 free(names[i]);
1925 free(names);
1927 return VIR_PY_NONE;
1930 if (names) {
1931 for (i = 0;i < c_retval;i++) {
1932 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1933 free(names[i]);
1935 free(names);
1938 return(py_retval);
1942 static PyObject *
1943 libvirt_virConnectListDefinedInterfaces(PyObject *self ATTRIBUTE_UNUSED,
1944 PyObject *args) {
1945 PyObject *py_retval;
1946 char **names = NULL;
1947 int c_retval, i;
1948 virConnectPtr conn;
1949 PyObject *pyobj_conn;
1952 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedInterfaces",
1953 &pyobj_conn))
1954 return(NULL);
1955 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1957 LIBVIRT_BEGIN_ALLOW_THREADS;
1958 c_retval = virConnectNumOfDefinedInterfaces(conn);
1959 LIBVIRT_END_ALLOW_THREADS;
1960 if (c_retval < 0)
1961 return VIR_PY_NONE;
1963 if (c_retval) {
1964 names = malloc(sizeof(*names) * c_retval);
1965 if (!names)
1966 return VIR_PY_NONE;
1967 LIBVIRT_BEGIN_ALLOW_THREADS;
1968 c_retval = virConnectListDefinedInterfaces(conn, names, c_retval);
1969 LIBVIRT_END_ALLOW_THREADS;
1970 if (c_retval < 0) {
1971 free(names);
1972 return VIR_PY_NONE;
1975 py_retval = PyList_New(c_retval);
1976 if (py_retval == NULL) {
1977 if (names) {
1978 for (i = 0;i < c_retval;i++)
1979 free(names[i]);
1980 free(names);
1982 return VIR_PY_NONE;
1985 if (names) {
1986 for (i = 0;i < c_retval;i++) {
1987 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1988 free(names[i]);
1990 free(names);
1993 return(py_retval);
1996 /*******************************************
1997 * Helper functions to avoid importing modules
1998 * for every callback
1999 *******************************************/
2000 static PyObject *libvirt_module = NULL;
2001 static PyObject *libvirt_dict = NULL;
2002 static PyObject *libvirt_dom_class = NULL;
2004 static PyObject *
2005 getLibvirtModuleObject (void) {
2006 if(libvirt_module)
2007 return libvirt_module;
2009 // PyImport_ImportModule returns a new reference
2010 /* Bogus (char *) cast for RHEL-5 python API brokenness */
2011 libvirt_module = PyImport_ImportModule((char *)"libvirt");
2012 if(!libvirt_module) {
2013 #if DEBUG_ERROR
2014 printf("%s Error importing libvirt module\n", __FUNCTION__);
2015 #endif
2016 PyErr_Print();
2017 return NULL;
2020 return libvirt_module;
2023 static PyObject *
2024 getLibvirtDictObject (void) {
2025 if(libvirt_dict)
2026 return libvirt_dict;
2028 // PyModule_GetDict returns a borrowed reference
2029 libvirt_dict = PyModule_GetDict(getLibvirtModuleObject());
2030 if(!libvirt_dict) {
2031 #if DEBUG_ERROR
2032 printf("%s Error importing libvirt dictionary\n", __FUNCTION__);
2033 #endif
2034 PyErr_Print();
2035 return NULL;
2038 Py_INCREF(libvirt_dict);
2039 return libvirt_dict;
2042 static PyObject *
2043 getLibvirtDomainClassObject (void) {
2044 if(libvirt_dom_class)
2045 return libvirt_dom_class;
2047 // PyDict_GetItemString returns a borrowed reference
2048 libvirt_dom_class = PyDict_GetItemString(getLibvirtDictObject(),
2049 "virDomain");
2050 if(!libvirt_dom_class) {
2051 #if DEBUG_ERROR
2052 printf("%s Error importing virDomain class\n", __FUNCTION__);
2053 #endif
2054 PyErr_Print();
2055 return NULL;
2058 Py_INCREF(libvirt_dom_class);
2059 return libvirt_dom_class;
2061 /*******************************************
2062 * Domain Events
2063 *******************************************/
2065 static int
2066 libvirt_virConnectDomainEventCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
2067 virDomainPtr dom,
2068 int event,
2069 int detail,
2070 void *opaque)
2072 PyObject *pyobj_ret;
2074 PyObject *pyobj_conn_inst = (PyObject*)opaque;
2075 PyObject *pyobj_dom;
2077 PyObject *pyobj_dom_args;
2078 PyObject *pyobj_dom_inst;
2080 PyObject *dom_class;
2081 int ret = -1;
2083 LIBVIRT_ENSURE_THREAD_STATE;
2085 /* Create a python instance of this virDomainPtr */
2086 virDomainRef(dom);
2087 pyobj_dom = libvirt_virDomainPtrWrap(dom);
2088 pyobj_dom_args = PyTuple_New(2);
2089 if(PyTuple_SetItem(pyobj_dom_args, 0, pyobj_conn_inst)!=0) {
2090 #if DEBUG_ERROR
2091 printf("%s error creating tuple",__FUNCTION__);
2092 #endif
2093 goto cleanup;
2095 if(PyTuple_SetItem(pyobj_dom_args, 1, pyobj_dom)!=0) {
2096 #if DEBUG_ERROR
2097 printf("%s error creating tuple",__FUNCTION__);
2098 #endif
2099 goto cleanup;
2101 Py_INCREF(pyobj_conn_inst);
2103 dom_class = getLibvirtDomainClassObject();
2104 if(!PyClass_Check(dom_class)) {
2105 #if DEBUG_ERROR
2106 printf("%s dom_class is not a class!\n", __FUNCTION__);
2107 #endif
2108 goto cleanup;
2111 pyobj_dom_inst = PyInstance_New(dom_class,
2112 pyobj_dom_args,
2113 NULL);
2115 Py_DECREF(pyobj_dom_args);
2117 if(!pyobj_dom_inst) {
2118 #if DEBUG_ERROR
2119 printf("%s Error creating a python instance of virDomain\n", __FUNCTION__);
2120 #endif
2121 PyErr_Print();
2122 goto cleanup;
2125 /* Call the Callback Dispatcher */
2126 pyobj_ret = PyObject_CallMethod(pyobj_conn_inst,
2127 (char*)"dispatchDomainEventCallbacks",
2128 (char*)"Oii",
2129 pyobj_dom_inst,
2130 event,
2131 detail);
2133 Py_DECREF(pyobj_dom_inst);
2135 if(!pyobj_ret) {
2136 #if DEBUG_ERROR
2137 printf("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
2138 #endif
2139 PyErr_Print();
2140 } else {
2141 Py_DECREF(pyobj_ret);
2142 ret = 0;
2146 cleanup:
2147 LIBVIRT_RELEASE_THREAD_STATE;
2148 return ret;
2151 static PyObject *
2152 libvirt_virConnectDomainEventRegister(ATTRIBUTE_UNUSED PyObject * self,
2153 PyObject * args)
2155 PyObject *py_retval; /* return value */
2156 PyObject *pyobj_conn; /* virConnectPtr */
2157 PyObject *pyobj_conn_inst; /* virConnect Python object */
2159 virConnectPtr conn;
2160 int ret = 0;
2162 if (!PyArg_ParseTuple
2163 (args, (char *) "OO:virConnectDomainEventRegister",
2164 &pyobj_conn, &pyobj_conn_inst)) {
2165 #if DEBUG_ERROR
2166 printf("%s failed parsing tuple\n", __FUNCTION__);
2167 #endif
2168 return VIR_PY_INT_FAIL;
2171 #ifdef DEBUG_ERROR
2172 printf("libvirt_virConnectDomainEventRegister(%p %p) called\n",
2173 pyobj_conn, pyobj_conn_inst);
2174 #endif
2175 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2177 Py_INCREF(pyobj_conn_inst);
2179 LIBVIRT_BEGIN_ALLOW_THREADS;
2181 ret = virConnectDomainEventRegister(conn,
2182 libvirt_virConnectDomainEventCallback,
2183 (void *)pyobj_conn_inst, NULL);
2185 LIBVIRT_END_ALLOW_THREADS;
2187 py_retval = libvirt_intWrap(ret);
2188 return (py_retval);
2191 static PyObject *
2192 libvirt_virConnectDomainEventDeregister(ATTRIBUTE_UNUSED PyObject * self,
2193 PyObject * args)
2195 PyObject *py_retval;
2196 PyObject *pyobj_conn;
2197 PyObject *pyobj_conn_inst;
2199 virConnectPtr conn;
2200 int ret = 0;
2202 if (!PyArg_ParseTuple
2203 (args, (char *) "OO:virConnectDomainEventDeregister",
2204 &pyobj_conn, &pyobj_conn_inst))
2205 return (NULL);
2207 #ifdef DEBUG_ERROR
2208 printf("libvirt_virConnectDomainEventDeregister(%p) called\n", pyobj_conn);
2209 #endif
2211 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
2213 LIBVIRT_BEGIN_ALLOW_THREADS;
2215 ret = virConnectDomainEventDeregister(conn, libvirt_virConnectDomainEventCallback);
2217 LIBVIRT_END_ALLOW_THREADS;
2219 Py_DECREF(pyobj_conn_inst);
2220 py_retval = libvirt_intWrap(ret);
2221 return (py_retval);
2224 /*******************************************
2225 * Event Impl
2226 *******************************************/
2227 static PyObject *addHandleObj = NULL;
2228 static char *addHandleName = NULL;
2229 static PyObject *updateHandleObj = NULL;
2230 static char *updateHandleName = NULL;
2231 static PyObject *removeHandleObj = NULL;
2232 static char *removeHandleName = NULL;
2233 static PyObject *addTimeoutObj = NULL;
2234 static char *addTimeoutName = NULL;
2235 static PyObject *updateTimeoutObj = NULL;
2236 static char *updateTimeoutName = NULL;
2237 static PyObject *removeTimeoutObj = NULL;
2238 static char *removeTimeoutName = NULL;
2240 #define NAME(fn) ( fn ## Name ? fn ## Name : # fn )
2242 static int
2243 libvirt_virEventAddHandleFunc (int fd,
2244 int event,
2245 virEventHandleCallback cb,
2246 void *opaque,
2247 virFreeCallback ff)
2249 PyObject *result;
2250 PyObject *python_cb;
2251 PyObject *cb_obj;
2252 PyObject *ff_obj;
2253 PyObject *opaque_obj;
2254 PyObject *cb_args;
2255 PyObject *pyobj_args;
2256 int retval = -1;
2258 LIBVIRT_ENSURE_THREAD_STATE;
2260 /* Lookup the python callback */
2261 python_cb = PyDict_GetItemString(getLibvirtDictObject(),
2262 "eventInvokeHandleCallback");
2263 if(!python_cb) {
2264 #if DEBUG_ERROR
2265 printf("%s: Error finding eventInvokeHandleCallback\n", __FUNCTION__);
2266 #endif
2267 PyErr_Print();
2268 PyErr_Clear();
2269 goto cleanup;
2271 if (!PyCallable_Check(python_cb)) {
2272 #if DEBUG_ERROR
2273 char *name = py_str(python_cb);
2274 printf("%s: %s is not callable\n", __FUNCTION__,
2275 name ? name : "libvirt.eventInvokeHandleCallback");
2276 free(name);
2277 #endif
2278 goto cleanup;
2280 Py_INCREF(python_cb);
2282 /* create tuple for cb */
2283 cb_obj = libvirt_virEventHandleCallbackWrap(cb);
2284 ff_obj = libvirt_virFreeCallbackWrap(ff);
2285 opaque_obj = libvirt_virVoidPtrWrap(opaque);
2287 cb_args = PyTuple_New(3);
2288 PyTuple_SetItem(cb_args, 0, cb_obj);
2289 PyTuple_SetItem(cb_args, 1, opaque_obj);
2290 PyTuple_SetItem(cb_args, 2, ff_obj);
2292 pyobj_args = PyTuple_New(4);
2293 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(fd));
2294 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(event));
2295 PyTuple_SetItem(pyobj_args, 2, python_cb);
2296 PyTuple_SetItem(pyobj_args, 3, cb_args);
2298 result = PyEval_CallObject(addHandleObj, pyobj_args);
2299 if (!result) {
2300 PyErr_Print();
2301 PyErr_Clear();
2302 } else if (!PyInt_Check(result)) {
2303 #if DEBUG_ERROR
2304 printf("%s: %s should return an int\n", __FUNCTION__, NAME(addHandle));
2305 #endif
2306 } else {
2307 retval = (int)PyInt_AsLong(result);
2310 Py_XDECREF(result);
2311 Py_DECREF(pyobj_args);
2313 cleanup:
2314 LIBVIRT_RELEASE_THREAD_STATE;
2316 return retval;
2319 static void
2320 libvirt_virEventUpdateHandleFunc(int watch, int event)
2322 PyObject *result;
2323 PyObject *pyobj_args;
2325 LIBVIRT_ENSURE_THREAD_STATE;
2327 pyobj_args = PyTuple_New(2);
2328 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(watch));
2329 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(event));
2331 result = PyEval_CallObject(updateHandleObj, pyobj_args);
2332 if (!result) {
2333 PyErr_Print();
2334 PyErr_Clear();
2337 Py_XDECREF(result);
2338 Py_DECREF(pyobj_args);
2340 LIBVIRT_RELEASE_THREAD_STATE;
2344 static int
2345 libvirt_virEventRemoveHandleFunc(int watch)
2347 PyObject *result;
2348 PyObject *pyobj_args;
2349 PyObject *opaque;
2350 PyObject *ff;
2351 int retval = -1;
2352 virFreeCallback cff;
2354 LIBVIRT_ENSURE_THREAD_STATE;
2356 pyobj_args = PyTuple_New(1);
2357 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(watch));
2359 result = PyEval_CallObject(removeHandleObj, pyobj_args);
2360 if (!result) {
2361 PyErr_Print();
2362 PyErr_Clear();
2363 } else if (!PyTuple_Check(result) || PyTuple_Size(result) != 3) {
2364 #if DEBUG_ERROR
2365 printf("%s: %s must return opaque obj registered with %s"
2366 "to avoid leaking libvirt memory\n",
2367 __FUNCTION__, NAME(removeHandle), NAME(addHandle));
2368 #endif
2369 } else {
2370 opaque = PyTuple_GetItem(result, 1);
2371 ff = PyTuple_GetItem(result, 2);
2372 cff = PyvirFreeCallback_Get(ff);
2373 if (cff)
2374 (*cff)(PyvirVoidPtr_Get(opaque));
2375 retval = 0;
2378 Py_XDECREF(result);
2379 Py_DECREF(pyobj_args);
2381 LIBVIRT_RELEASE_THREAD_STATE;
2383 return retval;
2386 static int
2387 libvirt_virEventAddTimeoutFunc(int timeout,
2388 virEventTimeoutCallback cb,
2389 void *opaque,
2390 virFreeCallback ff)
2392 PyObject *result;
2394 PyObject *python_cb;
2396 PyObject *cb_obj;
2397 PyObject *ff_obj;
2398 PyObject *opaque_obj;
2399 PyObject *cb_args;
2400 PyObject *pyobj_args;
2401 int retval = -1;
2403 LIBVIRT_ENSURE_THREAD_STATE;
2405 /* Lookup the python callback */
2406 python_cb = PyDict_GetItemString(getLibvirtDictObject(),
2407 "eventInvokeTimeoutCallback");
2408 if(!python_cb) {
2409 #if DEBUG_ERROR
2410 printf("%s: Error finding eventInvokeTimeoutCallback\n", __FUNCTION__);
2411 #endif
2412 PyErr_Print();
2413 PyErr_Clear();
2414 goto cleanup;
2416 if (!PyCallable_Check(python_cb)) {
2417 #if DEBUG_ERROR
2418 char *name = py_str(python_cb);
2419 printf("%s: %s is not callable\n", __FUNCTION__,
2420 name ? name : "libvirt.eventInvokeTimeoutCallback");
2421 free(name);
2422 #endif
2423 goto cleanup;
2425 Py_INCREF(python_cb);
2427 /* create tuple for cb */
2428 cb_obj = libvirt_virEventTimeoutCallbackWrap(cb);
2429 ff_obj = libvirt_virFreeCallbackWrap(ff);
2430 opaque_obj = libvirt_virVoidPtrWrap(opaque);
2432 cb_args = PyTuple_New(3);
2433 PyTuple_SetItem(cb_args, 0, cb_obj);
2434 PyTuple_SetItem(cb_args, 1, opaque_obj);
2435 PyTuple_SetItem(cb_args, 2, ff_obj);
2437 pyobj_args = PyTuple_New(3);
2439 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timeout));
2440 PyTuple_SetItem(pyobj_args, 1, python_cb);
2441 PyTuple_SetItem(pyobj_args, 2, cb_args);
2443 result = PyEval_CallObject(addTimeoutObj, pyobj_args);
2444 if (!result) {
2445 PyErr_Print();
2446 PyErr_Clear();
2447 } else if (!PyInt_Check(result)) {
2448 #if DEBUG_ERROR
2449 printf("%s: %s should return an int\n", __FUNCTION__, NAME(addTimeout));
2450 #endif
2451 } else {
2452 retval = (int)PyInt_AsLong(result);
2455 Py_XDECREF(result);
2456 Py_DECREF(pyobj_args);
2458 cleanup:
2459 LIBVIRT_RELEASE_THREAD_STATE;
2460 return retval;
2463 static void
2464 libvirt_virEventUpdateTimeoutFunc(int timer, int timeout)
2466 PyObject *result = NULL;
2467 PyObject *pyobj_args;
2469 LIBVIRT_ENSURE_THREAD_STATE;
2471 pyobj_args = PyTuple_New(2);
2472 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timer));
2473 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(timeout));
2475 result = PyEval_CallObject(updateTimeoutObj, pyobj_args);
2476 if (!result) {
2477 PyErr_Print();
2478 PyErr_Clear();
2481 Py_XDECREF(result);
2482 Py_DECREF(pyobj_args);
2484 LIBVIRT_RELEASE_THREAD_STATE;
2487 static int
2488 libvirt_virEventRemoveTimeoutFunc(int timer)
2490 PyObject *result = NULL;
2491 PyObject *pyobj_args;
2492 PyObject *opaque;
2493 PyObject *ff;
2494 int retval = -1;
2495 virFreeCallback cff;
2497 LIBVIRT_ENSURE_THREAD_STATE;
2499 pyobj_args = PyTuple_New(1);
2500 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timer));
2502 result = PyEval_CallObject(removeTimeoutObj, pyobj_args);
2503 if (!result) {
2504 PyErr_Print();
2505 PyErr_Clear();
2506 } else if (!PyTuple_Check(result) || PyTuple_Size(result) != 3) {
2507 #if DEBUG_ERROR
2508 printf("%s: %s must return opaque obj registered with %s"
2509 "to avoid leaking libvirt memory\n",
2510 __FUNCTION__, NAME(removeTimeout), NAME(addTimeout));
2511 #endif
2512 } else {
2513 opaque = PyTuple_GetItem(result, 1);
2514 ff = PyTuple_GetItem(result, 2);
2515 cff = PyvirFreeCallback_Get(ff);
2516 if (cff)
2517 (*cff)(PyvirVoidPtr_Get(opaque));
2518 retval = 0;
2521 Py_XDECREF(result);
2522 Py_DECREF(pyobj_args);
2524 LIBVIRT_RELEASE_THREAD_STATE;
2526 return retval;
2529 static PyObject *
2530 libvirt_virEventRegisterImpl(ATTRIBUTE_UNUSED PyObject * self,
2531 PyObject * args)
2533 /* Unref the previously-registered impl (if any) */
2534 Py_XDECREF(addHandleObj);
2535 free(addHandleName);
2536 Py_XDECREF(updateHandleObj);
2537 free(updateHandleName);
2538 Py_XDECREF(removeHandleObj);
2539 free(removeHandleName);
2540 Py_XDECREF(addTimeoutObj);
2541 free(addTimeoutName);
2542 Py_XDECREF(updateTimeoutObj);
2543 free(updateTimeoutName);
2544 Py_XDECREF(removeTimeoutObj);
2545 free(removeTimeoutName);
2547 /* Parse and check arguments */
2548 if (!PyArg_ParseTuple(args, (char *) "OOOOOO:virEventRegisterImpl",
2549 &addHandleObj, &updateHandleObj,
2550 &removeHandleObj, &addTimeoutObj,
2551 &updateTimeoutObj, &removeTimeoutObj) ||
2552 !PyCallable_Check(addHandleObj) ||
2553 !PyCallable_Check(updateHandleObj) ||
2554 !PyCallable_Check(removeHandleObj) ||
2555 !PyCallable_Check(addTimeoutObj) ||
2556 !PyCallable_Check(updateTimeoutObj) ||
2557 !PyCallable_Check(removeTimeoutObj))
2558 return VIR_PY_INT_FAIL;
2560 /* Get argument string representations (for error reporting) */
2561 addHandleName = py_str(addTimeoutObj);
2562 updateHandleName = py_str(updateHandleObj);
2563 removeHandleName = py_str(removeHandleObj);
2564 addTimeoutName = py_str(addTimeoutObj);
2565 updateTimeoutName = py_str(updateTimeoutObj);
2566 removeTimeoutName = py_str(removeTimeoutObj);
2568 /* Inc refs since we're holding onto these objects until
2569 * the next call (if any) to this function.
2571 Py_INCREF(addHandleObj);
2572 Py_INCREF(updateHandleObj);
2573 Py_INCREF(removeHandleObj);
2574 Py_INCREF(addTimeoutObj);
2575 Py_INCREF(updateTimeoutObj);
2576 Py_INCREF(removeTimeoutObj);
2578 LIBVIRT_BEGIN_ALLOW_THREADS;
2580 /* Now register our C EventImpl, which will dispatch
2581 * to the Python callbacks passed in as args.
2583 virEventRegisterImpl(libvirt_virEventAddHandleFunc,
2584 libvirt_virEventUpdateHandleFunc,
2585 libvirt_virEventRemoveHandleFunc,
2586 libvirt_virEventAddTimeoutFunc,
2587 libvirt_virEventUpdateTimeoutFunc,
2588 libvirt_virEventRemoveTimeoutFunc);
2590 LIBVIRT_END_ALLOW_THREADS;
2592 return VIR_PY_INT_SUCCESS;
2595 static PyObject *
2596 libvirt_virEventInvokeHandleCallback(PyObject *self ATTRIBUTE_UNUSED,
2597 PyObject *args)
2599 int watch, fd, event;
2600 PyObject *py_f;
2601 PyObject *py_opaque;
2602 virEventHandleCallback cb;
2603 void *opaque;
2605 if (!PyArg_ParseTuple
2606 (args, (char *) "iiiOO:virEventInvokeHandleCallback",
2607 &watch, &fd, &event, &py_f, &py_opaque
2609 return VIR_PY_INT_FAIL;
2611 cb = (virEventHandleCallback) PyvirEventHandleCallback_Get(py_f);
2612 opaque = (void *) PyvirVoidPtr_Get(py_opaque);
2614 if(cb) {
2615 LIBVIRT_BEGIN_ALLOW_THREADS;
2616 cb (watch, fd, event, opaque);
2617 LIBVIRT_END_ALLOW_THREADS;
2620 return VIR_PY_INT_SUCCESS;
2623 static PyObject *
2624 libvirt_virEventInvokeTimeoutCallback(PyObject *self ATTRIBUTE_UNUSED,
2625 PyObject *args)
2627 int timer;
2628 PyObject *py_f;
2629 PyObject *py_opaque;
2630 virEventTimeoutCallback cb;
2631 void *opaque;
2633 if (!PyArg_ParseTuple
2634 (args, (char *) "iOO:virEventInvokeTimeoutCallback",
2635 &timer, &py_f, &py_opaque
2637 return VIR_PY_INT_FAIL;
2639 cb = (virEventTimeoutCallback) PyvirEventTimeoutCallback_Get(py_f);
2640 opaque = (void *) PyvirVoidPtr_Get(py_opaque);
2641 if(cb) {
2642 LIBVIRT_BEGIN_ALLOW_THREADS;
2643 cb (timer, opaque);
2644 LIBVIRT_END_ALLOW_THREADS;
2647 return VIR_PY_INT_SUCCESS;
2650 /************************************************************************
2652 * The registration stuff *
2654 ************************************************************************/
2655 static PyMethodDef libvirtMethods[] = {
2656 #include "libvirt-export.c"
2657 {(char *) "virGetVersion", libvirt_virGetVersion, METH_VARARGS, NULL},
2658 {(char *) "virConnectGetLibVersion", libvirt_virConnectGetLibVersion, METH_VARARGS, NULL},
2659 {(char *) "virConnectOpenAuth", libvirt_virConnectOpenAuth, METH_VARARGS, NULL},
2660 {(char *) "virConnectListDomainsID", libvirt_virConnectListDomainsID, METH_VARARGS, NULL},
2661 {(char *) "virConnectListDefinedDomains", libvirt_virConnectListDefinedDomains, METH_VARARGS, NULL},
2662 {(char *) "virConnectDomainEventRegister", libvirt_virConnectDomainEventRegister, METH_VARARGS, NULL},
2663 {(char *) "virConnectDomainEventDeregister", libvirt_virConnectDomainEventDeregister, METH_VARARGS, NULL},
2664 {(char *) "virDomainGetInfo", libvirt_virDomainGetInfo, METH_VARARGS, NULL},
2665 {(char *) "virNodeGetInfo", libvirt_virNodeGetInfo, METH_VARARGS, NULL},
2666 {(char *) "virDomainGetUUID", libvirt_virDomainGetUUID, METH_VARARGS, NULL},
2667 {(char *) "virDomainGetUUIDString", libvirt_virDomainGetUUIDString, METH_VARARGS, NULL},
2668 {(char *) "virDomainLookupByUUID", libvirt_virDomainLookupByUUID, METH_VARARGS, NULL},
2669 {(char *) "virRegisterErrorHandler", libvirt_virRegisterErrorHandler, METH_VARARGS, NULL},
2670 {(char *) "virGetLastError", libvirt_virGetLastError, METH_VARARGS, NULL},
2671 {(char *) "virConnGetLastError", libvirt_virConnGetLastError, METH_VARARGS, NULL},
2672 {(char *) "virConnectListNetworks", libvirt_virConnectListNetworks, METH_VARARGS, NULL},
2673 {(char *) "virConnectListDefinedNetworks", libvirt_virConnectListDefinedNetworks, METH_VARARGS, NULL},
2674 {(char *) "virNetworkGetUUID", libvirt_virNetworkGetUUID, METH_VARARGS, NULL},
2675 {(char *) "virNetworkGetUUIDString", libvirt_virNetworkGetUUIDString, METH_VARARGS, NULL},
2676 {(char *) "virNetworkLookupByUUID", libvirt_virNetworkLookupByUUID, METH_VARARGS, NULL},
2677 {(char *) "virDomainGetAutostart", libvirt_virDomainGetAutostart, METH_VARARGS, NULL},
2678 {(char *) "virNetworkGetAutostart", libvirt_virNetworkGetAutostart, METH_VARARGS, NULL},
2679 {(char *) "virDomainBlockStats", libvirt_virDomainBlockStats, METH_VARARGS, NULL},
2680 {(char *) "virDomainInterfaceStats", libvirt_virDomainInterfaceStats, METH_VARARGS, NULL},
2681 {(char *) "virDomainMemoryStats", libvirt_virDomainMemoryStats, METH_VARARGS, NULL},
2682 {(char *) "virNodeGetCellsFreeMemory", libvirt_virNodeGetCellsFreeMemory, METH_VARARGS, NULL},
2683 {(char *) "virDomainGetSchedulerType", libvirt_virDomainGetSchedulerType, METH_VARARGS, NULL},
2684 {(char *) "virDomainGetSchedulerParameters", libvirt_virDomainGetSchedulerParameters, METH_VARARGS, NULL},
2685 {(char *) "virDomainSetSchedulerParameters", libvirt_virDomainSetSchedulerParameters, METH_VARARGS, NULL},
2686 {(char *) "virDomainGetVcpus", libvirt_virDomainGetVcpus, METH_VARARGS, NULL},
2687 {(char *) "virDomainPinVcpu", libvirt_virDomainPinVcpu, METH_VARARGS, NULL},
2688 {(char *) "virConnectListStoragePools", libvirt_virConnectListStoragePools, METH_VARARGS, NULL},
2689 {(char *) "virConnectListDefinedStoragePools", libvirt_virConnectListDefinedStoragePools, METH_VARARGS, NULL},
2690 {(char *) "virStoragePoolGetAutostart", libvirt_virStoragePoolGetAutostart, METH_VARARGS, NULL},
2691 {(char *) "virStoragePoolListVolumes", libvirt_virStoragePoolListVolumes, METH_VARARGS, NULL},
2692 {(char *) "virStoragePoolGetInfo", libvirt_virStoragePoolGetInfo, METH_VARARGS, NULL},
2693 {(char *) "virStorageVolGetInfo", libvirt_virStorageVolGetInfo, METH_VARARGS, NULL},
2694 {(char *) "virStoragePoolGetUUID", libvirt_virStoragePoolGetUUID, METH_VARARGS, NULL},
2695 {(char *) "virStoragePoolGetUUIDString", libvirt_virStoragePoolGetUUIDString, METH_VARARGS, NULL},
2696 {(char *) "virStoragePoolLookupByUUID", libvirt_virStoragePoolLookupByUUID, METH_VARARGS, NULL},
2697 {(char *) "virEventRegisterImpl", libvirt_virEventRegisterImpl, METH_VARARGS, NULL},
2698 {(char *) "virEventInvokeHandleCallback", libvirt_virEventInvokeHandleCallback, METH_VARARGS, NULL},
2699 {(char *) "virEventInvokeTimeoutCallback", libvirt_virEventInvokeTimeoutCallback, METH_VARARGS, NULL},
2700 {(char *) "virNodeListDevices", libvirt_virNodeListDevices, METH_VARARGS, NULL},
2701 {(char *) "virNodeDeviceListCaps", libvirt_virNodeDeviceListCaps, METH_VARARGS, NULL},
2702 {(char *) "virSecretGetUUID", libvirt_virSecretGetUUID, METH_VARARGS, NULL},
2703 {(char *) "virSecretGetUUIDString", libvirt_virSecretGetUUIDString, METH_VARARGS, NULL},
2704 {(char *) "virSecretLookupByUUID", libvirt_virSecretLookupByUUID, METH_VARARGS, NULL},
2705 {(char *) "virConnectListSecrets", libvirt_virConnectListSecrets, METH_VARARGS, NULL},
2706 {(char *) "virSecretGetValue", libvirt_virSecretGetValue, METH_VARARGS, NULL},
2707 {(char *) "virSecretSetValue", libvirt_virSecretSetValue, METH_VARARGS, NULL},
2708 {(char *) "virConnectListInterfaces", libvirt_virConnectListInterfaces, METH_VARARGS, NULL},
2709 {(char *) "virConnectListDefinedInterfaces", libvirt_virConnectListDefinedInterfaces, METH_VARARGS, NULL},
2710 {NULL, NULL, 0, NULL}
2713 void
2714 #ifndef __CYGWIN__
2715 initlibvirtmod
2716 #else
2717 initcygvirtmod
2718 #endif
2719 (void)
2721 static int initialized = 0;
2723 if (initialized != 0)
2724 return;
2726 virInitialize();
2728 /* initialize the python extension module */
2729 Py_InitModule((char *)
2730 #ifndef __CYGWIN__
2731 "libvirtmod"
2732 #else
2733 "cygvirtmod"
2734 #endif
2735 , libvirtMethods);
2737 initialized = 1;