python: Add a newline after custom classes
[libvirt-python/ericb.git] / libvirt-override.c
blob5d24fd2277d7ea4349c83c96baf1da6cca12aa85
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 c_retval = virDomainBlockStats(domain, path, &stats, sizeof(stats));
71 if (c_retval < 0)
72 return VIR_PY_NONE;
74 /* convert to a Python tuple of long objects */
75 if ((info = PyTuple_New(5)) == NULL)
76 return VIR_PY_NONE;
77 PyTuple_SetItem(info, 0, PyLong_FromLongLong(stats.rd_req));
78 PyTuple_SetItem(info, 1, PyLong_FromLongLong(stats.rd_bytes));
79 PyTuple_SetItem(info, 2, PyLong_FromLongLong(stats.wr_req));
80 PyTuple_SetItem(info, 3, PyLong_FromLongLong(stats.wr_bytes));
81 PyTuple_SetItem(info, 4, PyLong_FromLongLong(stats.errs));
82 return(info);
85 static PyObject *
86 libvirt_virDomainInterfaceStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
87 virDomainPtr domain;
88 PyObject *pyobj_domain;
89 char * path;
90 int c_retval;
91 virDomainInterfaceStatsStruct stats;
92 PyObject *info;
94 if (!PyArg_ParseTuple(args, (char *)"Oz:virDomainInterfaceStats",
95 &pyobj_domain,&path))
96 return(NULL);
97 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
99 c_retval = virDomainInterfaceStats(domain, path, &stats, sizeof(stats));
100 if (c_retval < 0)
101 return VIR_PY_NONE;
103 /* convert to a Python tuple of long objects */
104 if ((info = PyTuple_New(8)) == NULL)
105 return VIR_PY_NONE;
106 PyTuple_SetItem(info, 0, PyLong_FromLongLong(stats.rx_bytes));
107 PyTuple_SetItem(info, 1, PyLong_FromLongLong(stats.rx_packets));
108 PyTuple_SetItem(info, 2, PyLong_FromLongLong(stats.rx_errs));
109 PyTuple_SetItem(info, 3, PyLong_FromLongLong(stats.rx_drop));
110 PyTuple_SetItem(info, 4, PyLong_FromLongLong(stats.tx_bytes));
111 PyTuple_SetItem(info, 5, PyLong_FromLongLong(stats.tx_packets));
112 PyTuple_SetItem(info, 6, PyLong_FromLongLong(stats.tx_errs));
113 PyTuple_SetItem(info, 7, PyLong_FromLongLong(stats.tx_drop));
114 return(info);
118 static PyObject *
119 libvirt_virDomainGetSchedulerType(PyObject *self ATTRIBUTE_UNUSED,
120 PyObject *args) {
121 virDomainPtr domain;
122 PyObject *pyobj_domain, *info;
123 char *c_retval;
124 int nparams;
126 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetScedulerType",
127 &pyobj_domain))
128 return(NULL);
129 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
131 c_retval = virDomainGetSchedulerType(domain, &nparams);
132 if (c_retval == NULL)
133 return VIR_PY_NONE;
135 /* convert to a Python tuple of long objects */
136 if ((info = PyTuple_New(2)) == NULL) {
137 free(c_retval);
138 return VIR_PY_NONE;
141 PyTuple_SetItem(info, 0, libvirt_constcharPtrWrap(c_retval));
142 PyTuple_SetItem(info, 1, PyInt_FromLong((long)nparams));
143 free(c_retval);
144 return(info);
147 static PyObject *
148 libvirt_virDomainGetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
149 PyObject *args) {
150 virDomainPtr domain;
151 PyObject *pyobj_domain, *info;
152 char *c_retval;
153 int nparams, i;
154 virSchedParameterPtr params;
156 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetScedulerParameters",
157 &pyobj_domain))
158 return(NULL);
159 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
161 c_retval = virDomainGetSchedulerType(domain, &nparams);
162 if (c_retval == NULL)
163 return VIR_PY_NONE;
164 free(c_retval);
166 if ((params = malloc(sizeof(*params)*nparams)) == NULL)
167 return VIR_PY_NONE;
169 if (virDomainGetSchedulerParameters(domain, params, &nparams) < 0) {
170 free(params);
171 return VIR_PY_NONE;
174 /* convert to a Python tuple of long objects */
175 if ((info = PyDict_New()) == NULL) {
176 free(params);
177 return VIR_PY_NONE;
179 for (i = 0 ; i < nparams ; i++) {
180 PyObject *key, *val;
182 switch (params[i].type) {
183 case VIR_DOMAIN_SCHED_FIELD_INT:
184 val = PyInt_FromLong((long)params[i].value.i);
185 break;
187 case VIR_DOMAIN_SCHED_FIELD_UINT:
188 val = PyInt_FromLong((long)params[i].value.ui);
189 break;
191 case VIR_DOMAIN_SCHED_FIELD_LLONG:
192 val = PyLong_FromLongLong((long long)params[i].value.l);
193 break;
195 case VIR_DOMAIN_SCHED_FIELD_ULLONG:
196 val = PyLong_FromLongLong((long long)params[i].value.ul);
197 break;
199 case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
200 val = PyFloat_FromDouble((double)params[i].value.d);
201 break;
203 case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
204 val = PyBool_FromLong((long)params[i].value.b);
205 break;
207 default:
208 free(params);
209 Py_DECREF(info);
210 return VIR_PY_NONE;
213 key = libvirt_constcharPtrWrap(params[i].field);
214 PyDict_SetItem(info, key, val);
216 free(params);
217 return(info);
220 static PyObject *
221 libvirt_virDomainSetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
222 PyObject *args) {
223 virDomainPtr domain;
224 PyObject *pyobj_domain, *info;
225 char *c_retval;
226 int nparams, i;
227 virSchedParameterPtr params;
229 if (!PyArg_ParseTuple(args, (char *)"OO:virDomainSetScedulerParameters",
230 &pyobj_domain, &info))
231 return(NULL);
232 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
234 c_retval = virDomainGetSchedulerType(domain, &nparams);
235 if (c_retval == NULL)
236 return VIR_PY_INT_FAIL;
237 free(c_retval);
239 if ((params = malloc(sizeof(*params)*nparams)) == NULL)
240 return VIR_PY_INT_FAIL;
242 if (virDomainGetSchedulerParameters(domain, params, &nparams) < 0) {
243 free(params);
244 return VIR_PY_INT_FAIL;
247 /* convert to a Python tuple of long objects */
248 for (i = 0 ; i < nparams ; i++) {
249 PyObject *key, *val;
250 key = libvirt_constcharPtrWrap(params[i].field);
251 val = PyDict_GetItem(info, key);
252 Py_DECREF(key);
254 if (val == NULL)
255 continue;
257 switch (params[i].type) {
258 case VIR_DOMAIN_SCHED_FIELD_INT:
259 params[i].value.i = (int)PyInt_AS_LONG(val);
260 break;
262 case VIR_DOMAIN_SCHED_FIELD_UINT:
263 params[i].value.ui = (unsigned int)PyInt_AS_LONG(val);
264 break;
266 case VIR_DOMAIN_SCHED_FIELD_LLONG:
267 params[i].value.l = (long long)PyLong_AsLongLong(val);
268 break;
270 case VIR_DOMAIN_SCHED_FIELD_ULLONG:
271 params[i].value.ul = (unsigned long long)PyLong_AsLongLong(val);
272 break;
274 case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
275 params[i].value.d = (double)PyFloat_AsDouble(val);
276 break;
278 case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
280 /* Hack - Python's definition of Py_True breaks strict
281 * aliasing rules, so can't directly compare :-(
283 PyObject *hacktrue = PyBool_FromLong(1);
284 params[i].value.b = hacktrue == val ? 1 : 0;
285 Py_DECREF(hacktrue);
287 break;
289 default:
290 free(params);
291 return VIR_PY_INT_FAIL;
295 if (virDomainSetSchedulerParameters(domain, params, nparams) < 0) {
296 free(params);
297 return VIR_PY_INT_FAIL;
300 free(params);
301 return VIR_PY_INT_SUCCESS;
304 static PyObject *
305 libvirt_virDomainGetVcpus(PyObject *self ATTRIBUTE_UNUSED,
306 PyObject *args) {
307 virDomainPtr domain;
308 PyObject *pyobj_domain, *pyretval = NULL, *pycpuinfo = NULL, *pycpumap = NULL;
309 virNodeInfo nodeinfo;
310 virDomainInfo dominfo;
311 virVcpuInfoPtr cpuinfo = NULL;
312 unsigned char *cpumap = NULL;
313 int cpumaplen, i;
315 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetVcpus",
316 &pyobj_domain))
317 return(NULL);
318 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
320 if (virNodeGetInfo(virDomainGetConnect(domain), &nodeinfo) != 0)
321 return VIR_PY_NONE;
323 if (virDomainGetInfo(domain, &dominfo) != 0)
324 return VIR_PY_NONE;
326 if ((cpuinfo = malloc(sizeof(*cpuinfo)*dominfo.nrVirtCpu)) == NULL)
327 return VIR_PY_NONE;
329 cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
330 if ((cpumap = malloc(dominfo.nrVirtCpu * cpumaplen)) == NULL)
331 goto cleanup;
333 if (virDomainGetVcpus(domain,
334 cpuinfo, dominfo.nrVirtCpu,
335 cpumap, cpumaplen) < 0)
336 goto cleanup;
338 /* convert to a Python tuple of long objects */
339 if ((pyretval = PyTuple_New(2)) == NULL)
340 goto cleanup;
341 if ((pycpuinfo = PyList_New(dominfo.nrVirtCpu)) == NULL)
342 goto cleanup;
343 if ((pycpumap = PyList_New(dominfo.nrVirtCpu)) == NULL)
344 goto cleanup;
346 for (i = 0 ; i < dominfo.nrVirtCpu ; i++) {
347 PyObject *info = PyTuple_New(4);
348 if (info == NULL)
349 goto cleanup;
350 PyTuple_SetItem(info, 0, PyInt_FromLong((long)cpuinfo[i].number));
351 PyTuple_SetItem(info, 1, PyInt_FromLong((long)cpuinfo[i].state));
352 PyTuple_SetItem(info, 2, PyLong_FromLongLong((long long)cpuinfo[i].cpuTime));
353 PyTuple_SetItem(info, 3, PyInt_FromLong((long)cpuinfo[i].cpu));
354 PyList_SetItem(pycpuinfo, i, info);
356 for (i = 0 ; i < dominfo.nrVirtCpu ; i++) {
357 PyObject *info = PyTuple_New(VIR_NODEINFO_MAXCPUS(nodeinfo));
358 int j;
359 if (info == NULL)
360 goto cleanup;
361 for (j = 0 ; j < VIR_NODEINFO_MAXCPUS(nodeinfo) ; j++) {
362 PyTuple_SetItem(info, j, PyBool_FromLong(VIR_CPU_USABLE(cpumap, cpumaplen, i, j)));
364 PyList_SetItem(pycpumap, i, info);
366 PyTuple_SetItem(pyretval, 0, pycpuinfo);
367 PyTuple_SetItem(pyretval, 1, pycpumap);
369 free(cpuinfo);
370 free(cpumap);
372 return(pyretval);
374 cleanup:
375 free(cpuinfo);
376 free(cpumap);
377 /* NB, Py_DECREF is a badly defined macro, so we require
378 * braces here to avoid 'ambiguous else' warnings from
379 * the compiler.
380 * NB. this comment is true at of time of writing wrt to
381 * at least python2.5.
383 if (pyretval) { Py_DECREF(pyretval); }
384 if (pycpuinfo) { Py_DECREF(pycpuinfo); }
385 if (pycpumap) { Py_DECREF(pycpumap); }
386 return VIR_PY_NONE;
390 static PyObject *
391 libvirt_virDomainPinVcpu(PyObject *self ATTRIBUTE_UNUSED,
392 PyObject *args) {
393 virDomainPtr domain;
394 PyObject *pyobj_domain, *pycpumap, *truth;
395 virNodeInfo nodeinfo;
396 unsigned char *cpumap;
397 int cpumaplen, i, vcpu;
399 if (!PyArg_ParseTuple(args, (char *)"OiO:virDomainPinVcpu",
400 &pyobj_domain, &vcpu, &pycpumap))
401 return(NULL);
402 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
404 if (virNodeGetInfo(virDomainGetConnect(domain), &nodeinfo) != 0)
405 return VIR_PY_INT_FAIL;
407 cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
408 if ((cpumap = malloc(cpumaplen)) == NULL)
409 return VIR_PY_INT_FAIL;
410 memset(cpumap, 0, cpumaplen);
412 truth = PyBool_FromLong(1);
413 for (i = 0 ; i < VIR_NODEINFO_MAXCPUS(nodeinfo) ; i++) {
414 PyObject *flag = PyTuple_GetItem(pycpumap, i);
415 if (flag == truth)
416 VIR_USE_CPU(cpumap, i);
417 else
418 VIR_UNUSE_CPU(cpumap, i);
421 virDomainPinVcpu(domain, vcpu, cpumap, cpumaplen);
422 Py_DECREF(truth);
423 free(cpumap);
425 return VIR_PY_INT_SUCCESS;
429 /************************************************************************
431 * Global error handler at the Python level *
433 ************************************************************************/
435 static PyObject *libvirt_virPythonErrorFuncHandler = NULL;
436 static PyObject *libvirt_virPythonErrorFuncCtxt = NULL;
438 static PyObject *
439 libvirt_virGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args ATTRIBUTE_UNUSED)
441 virError *err;
442 PyObject *info;
444 if ((err = virGetLastError()) == NULL)
445 return VIR_PY_NONE;
447 if ((info = PyTuple_New(9)) == NULL)
448 return VIR_PY_NONE;
449 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err->code));
450 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err->domain));
451 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err->message));
452 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err->level));
453 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err->str1));
454 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err->str2));
455 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err->str3));
456 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err->int1));
457 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err->int2));
459 return info;
462 static PyObject *
463 libvirt_virConnGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
465 virError *err;
466 PyObject *info;
467 virConnectPtr conn;
468 PyObject *pyobj_conn;
470 if (!PyArg_ParseTuple(args, (char *)"O:virConGetLastError", &pyobj_conn))
471 return(NULL);
472 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
474 if ((err = virConnGetLastError(conn)) == NULL)
475 return VIR_PY_NONE;
477 if ((info = PyTuple_New(9)) == NULL)
478 return VIR_PY_NONE;
479 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err->code));
480 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err->domain));
481 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err->message));
482 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err->level));
483 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err->str1));
484 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err->str2));
485 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err->str3));
486 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err->int1));
487 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err->int2));
489 return info;
492 static void
493 libvirt_virErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, virErrorPtr err)
495 PyObject *list, *info;
496 PyObject *result;
498 #ifdef DEBUG_ERROR
499 printf("libvirt_virErrorFuncHandler(%p, %s, ...) called\n", ctx,
500 err->message);
501 #endif
503 if ((err == NULL) || (err->code == VIR_ERR_OK))
504 return;
506 LIBVIRT_ENSURE_THREAD_STATE;
508 if ((libvirt_virPythonErrorFuncHandler == NULL) ||
509 (libvirt_virPythonErrorFuncHandler == Py_None)) {
510 virDefaultErrorFunc(err);
511 } else {
512 list = PyTuple_New(2);
513 info = PyTuple_New(9);
514 PyTuple_SetItem(list, 0, libvirt_virPythonErrorFuncCtxt);
515 PyTuple_SetItem(list, 1, info);
516 Py_XINCREF(libvirt_virPythonErrorFuncCtxt);
517 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err->code));
518 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err->domain));
519 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err->message));
520 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err->level));
521 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err->str1));
522 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err->str2));
523 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err->str3));
524 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err->int1));
525 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err->int2));
526 /* TODO pass conn and dom if available */
527 result = PyEval_CallObject(libvirt_virPythonErrorFuncHandler, list);
528 Py_XDECREF(list);
529 Py_XDECREF(result);
532 LIBVIRT_RELEASE_THREAD_STATE;
535 static PyObject *
536 libvirt_virRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
537 PyObject * args)
539 PyObject *py_retval;
540 PyObject *pyobj_f;
541 PyObject *pyobj_ctx;
543 if (!PyArg_ParseTuple
544 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
545 &pyobj_ctx))
546 return (NULL);
548 #ifdef DEBUG_ERROR
549 printf("libvirt_virRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
550 pyobj_f);
551 #endif
553 virSetErrorFunc(NULL, libvirt_virErrorFuncHandler);
554 if (libvirt_virPythonErrorFuncHandler != NULL) {
555 Py_XDECREF(libvirt_virPythonErrorFuncHandler);
557 if (libvirt_virPythonErrorFuncCtxt != NULL) {
558 Py_XDECREF(libvirt_virPythonErrorFuncCtxt);
561 if ((pyobj_f == Py_None) && (pyobj_ctx == Py_None)) {
562 libvirt_virPythonErrorFuncHandler = NULL;
563 libvirt_virPythonErrorFuncCtxt = NULL;
564 } else {
565 Py_XINCREF(pyobj_ctx);
566 Py_XINCREF(pyobj_f);
568 /* TODO: check f is a function ! */
569 libvirt_virPythonErrorFuncHandler = pyobj_f;
570 libvirt_virPythonErrorFuncCtxt = pyobj_ctx;
573 py_retval = libvirt_intWrap(1);
574 return (py_retval);
577 static int virConnectCredCallbackWrapper(virConnectCredentialPtr cred,
578 unsigned int ncred,
579 void *cbdata) {
580 PyObject *list;
581 PyObject *pycred;
582 PyObject *pyauth = (PyObject *)cbdata;
583 PyObject *pycbdata;
584 PyObject *pycb;
585 PyObject *pyret;
586 int ret = -1, i;
588 LIBVIRT_ENSURE_THREAD_STATE;
590 pycb = PyList_GetItem(pyauth, 1);
591 pycbdata = PyList_GetItem(pyauth, 2);
593 list = PyTuple_New(2);
594 pycred = PyTuple_New(ncred);
595 for (i = 0 ; i < ncred ; i++) {
596 PyObject *pycreditem;
597 pycreditem = PyList_New(5);
598 Py_INCREF(Py_None);
599 PyTuple_SetItem(pycred, i, pycreditem);
600 PyList_SetItem(pycreditem, 0, PyInt_FromLong((long) cred[i].type));
601 PyList_SetItem(pycreditem, 1, PyString_FromString(cred[i].prompt));
602 if (cred[i].challenge) {
603 PyList_SetItem(pycreditem, 2, PyString_FromString(cred[i].challenge));
604 } else {
605 Py_INCREF(Py_None);
606 PyList_SetItem(pycreditem, 2, Py_None);
608 if (cred[i].defresult) {
609 PyList_SetItem(pycreditem, 3, PyString_FromString(cred[i].defresult));
610 } else {
611 Py_INCREF(Py_None);
612 PyList_SetItem(pycreditem, 3, Py_None);
614 PyList_SetItem(pycreditem, 4, Py_None);
617 PyTuple_SetItem(list, 0, pycred);
618 Py_XINCREF(pycbdata);
619 PyTuple_SetItem(list, 1, pycbdata);
621 PyErr_Clear();
622 pyret = PyEval_CallObject(pycb, list);
623 if (PyErr_Occurred())
624 goto cleanup;
626 ret = PyLong_AsLong(pyret);
627 if (ret == 0) {
628 for (i = 0 ; i < ncred ; i++) {
629 PyObject *pycreditem;
630 PyObject *pyresult;
631 char *result = NULL;
632 pycreditem = PyTuple_GetItem(pycred, i);
633 pyresult = PyList_GetItem(pycreditem, 4);
634 if (pyresult != Py_None)
635 result = PyString_AsString(pyresult);
636 if (result != NULL) {
637 cred[i].result = strdup(result);
638 cred[i].resultlen = strlen(result);
639 } else {
640 cred[i].result = NULL;
641 cred[i].resultlen = 0;
646 cleanup:
647 Py_XDECREF(list);
648 Py_XDECREF(pyret);
650 LIBVIRT_RELEASE_THREAD_STATE;
652 return ret;
656 static PyObject *
657 libvirt_virConnectOpenAuth(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
658 PyObject *py_retval;
659 virConnectPtr c_retval;
660 char * name;
661 int flags;
662 PyObject *pyauth;
663 PyObject *pycredcb;
664 PyObject *pycredtype;
665 virConnectAuth auth;
667 if (!PyArg_ParseTuple(args, (char *)"zOi:virConnectOpenAuth", &name, &pyauth, &flags))
668 return(NULL);
670 pycredtype = PyList_GetItem(pyauth, 0);
671 pycredcb = PyList_GetItem(pyauth, 1);
673 auth.ncredtype = PyList_Size(pycredtype);
674 if (auth.ncredtype) {
675 int i;
676 auth.credtype = malloc(sizeof(*auth.credtype) * auth.ncredtype);
677 if (auth.credtype == NULL)
678 return VIR_PY_NONE;
679 for (i = 0 ; i < auth.ncredtype ; i++) {
680 PyObject *val;
681 val = PyList_GetItem(pycredtype, i);
682 auth.credtype[i] = (int)PyLong_AsLong(val);
685 auth.cb = pycredcb ? virConnectCredCallbackWrapper : NULL;
686 auth.cbdata = pyauth;
688 LIBVIRT_BEGIN_ALLOW_THREADS;
690 c_retval = virConnectOpenAuth(name, &auth, flags);
691 LIBVIRT_END_ALLOW_THREADS;
692 py_retval = libvirt_virConnectPtrWrap((virConnectPtr) c_retval);
693 return(py_retval);
697 /************************************************************************
699 * Wrappers for functions where generator fails *
701 ************************************************************************/
703 static PyObject *
704 libvirt_virGetVersion (PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
706 char *type = NULL;
707 unsigned long libVer, typeVer = 0;
708 int c_retval;
710 if (!PyArg_ParseTuple (args, (char *) "|s", &type))
711 return NULL;
713 LIBVIRT_BEGIN_ALLOW_THREADS;
715 if (type == NULL)
716 c_retval = virGetVersion (&libVer, NULL, NULL);
717 else
718 c_retval = virGetVersion (&libVer, type, &typeVer);
720 LIBVIRT_END_ALLOW_THREADS;
722 if (c_retval == -1)
723 return VIR_PY_NONE;
725 if (type == NULL)
726 return PyInt_FromLong (libVer);
727 else
728 return Py_BuildValue ((char *) "kk", libVer, typeVer);
732 static PyObject *
733 libvirt_virConnectListDomainsID(PyObject *self ATTRIBUTE_UNUSED,
734 PyObject *args) {
735 PyObject *py_retval;
736 int ids[500], c_retval, i;
737 virConnectPtr conn;
738 PyObject *pyobj_conn;
741 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDomains", &pyobj_conn))
742 return(NULL);
743 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
745 LIBVIRT_BEGIN_ALLOW_THREADS;
746 c_retval = virConnectListDomains(conn, &ids[0], 500);
747 LIBVIRT_END_ALLOW_THREADS;
748 if (c_retval < 0)
749 return VIR_PY_NONE;
750 py_retval = PyList_New(c_retval);
751 for (i = 0;i < c_retval;i++) {
752 PyList_SetItem(py_retval, i, libvirt_intWrap(ids[i]));
754 return(py_retval);
757 static PyObject *
758 libvirt_virConnectListDefinedDomains(PyObject *self ATTRIBUTE_UNUSED,
759 PyObject *args) {
760 PyObject *py_retval;
761 char **names = NULL;
762 int c_retval, i;
763 virConnectPtr conn;
764 PyObject *pyobj_conn;
767 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedDomains", &pyobj_conn))
768 return(NULL);
769 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
771 c_retval = virConnectNumOfDefinedDomains(conn);
772 if (c_retval < 0)
773 return VIR_PY_NONE;
775 if (c_retval) {
776 names = malloc(sizeof(*names) * c_retval);
777 if (!names)
778 return VIR_PY_NONE;
779 c_retval = virConnectListDefinedDomains(conn, names, c_retval);
780 if (c_retval < 0) {
781 free(names);
782 return VIR_PY_NONE;
785 py_retval = PyList_New(c_retval);
787 if (names) {
788 for (i = 0;i < c_retval;i++) {
789 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
790 free(names[i]);
792 free(names);
795 return(py_retval);
798 static PyObject *
799 libvirt_virDomainGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
800 PyObject *py_retval;
801 int c_retval;
802 virDomainPtr domain;
803 PyObject *pyobj_domain;
804 virDomainInfo info;
806 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetInfo", &pyobj_domain))
807 return(NULL);
808 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
810 LIBVIRT_BEGIN_ALLOW_THREADS;
811 c_retval = virDomainGetInfo(domain, &info);
812 LIBVIRT_END_ALLOW_THREADS;
813 if (c_retval < 0)
814 return VIR_PY_NONE;
815 py_retval = PyList_New(5);
816 PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.state));
817 PyList_SetItem(py_retval, 1, libvirt_ulongWrap(info.maxMem));
818 PyList_SetItem(py_retval, 2, libvirt_ulongWrap(info.memory));
819 PyList_SetItem(py_retval, 3, libvirt_intWrap((int) info.nrVirtCpu));
820 PyList_SetItem(py_retval, 4,
821 libvirt_longlongWrap((unsigned long long) info.cpuTime));
822 return(py_retval);
825 static PyObject *
826 libvirt_virNodeGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
827 PyObject *py_retval;
828 int c_retval;
829 virConnectPtr conn;
830 PyObject *pyobj_conn;
831 virNodeInfo info;
833 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetInfo", &pyobj_conn))
834 return(NULL);
835 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
837 LIBVIRT_BEGIN_ALLOW_THREADS;
838 c_retval = virNodeGetInfo(conn, &info);
839 LIBVIRT_END_ALLOW_THREADS;
840 if (c_retval < 0)
841 return VIR_PY_NONE;
842 py_retval = PyList_New(8);
843 PyList_SetItem(py_retval, 0, libvirt_constcharPtrWrap(&info.model[0]));
844 PyList_SetItem(py_retval, 1, libvirt_longWrap((long) info.memory >> 10));
845 PyList_SetItem(py_retval, 2, libvirt_intWrap((int) info.cpus));
846 PyList_SetItem(py_retval, 3, libvirt_intWrap((int) info.mhz));
847 PyList_SetItem(py_retval, 4, libvirt_intWrap((int) info.nodes));
848 PyList_SetItem(py_retval, 5, libvirt_intWrap((int) info.sockets));
849 PyList_SetItem(py_retval, 6, libvirt_intWrap((int) info.cores));
850 PyList_SetItem(py_retval, 7, libvirt_intWrap((int) info.threads));
851 return(py_retval);
854 static PyObject *
855 libvirt_virDomainGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
856 PyObject *py_retval;
857 unsigned char uuid[VIR_UUID_BUFLEN];
858 virDomainPtr domain;
859 PyObject *pyobj_domain;
860 int c_retval;
862 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetUUID", &pyobj_domain))
863 return(NULL);
864 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
866 if (domain == NULL)
867 return VIR_PY_NONE;
868 LIBVIRT_BEGIN_ALLOW_THREADS;
869 c_retval = virDomainGetUUID(domain, &uuid[0]);
870 LIBVIRT_END_ALLOW_THREADS;
872 if (c_retval < 0)
873 return VIR_PY_NONE;
874 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
876 return(py_retval);
879 static PyObject *
880 libvirt_virDomainGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
881 PyObject *args) {
882 PyObject *py_retval;
883 char uuidstr[VIR_UUID_STRING_BUFLEN];
884 virDomainPtr dom;
885 PyObject *pyobj_dom;
886 int c_retval;
888 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetUUIDString",
889 &pyobj_dom))
890 return(NULL);
891 dom = (virDomainPtr) PyvirDomain_Get(pyobj_dom);
893 if (dom == NULL)
894 return VIR_PY_NONE;
895 LIBVIRT_BEGIN_ALLOW_THREADS;
896 c_retval = virDomainGetUUIDString(dom, &uuidstr[0]);
897 LIBVIRT_END_ALLOW_THREADS;
899 if (c_retval < 0)
900 return VIR_PY_NONE;
902 py_retval = PyString_FromString((char *) &uuidstr[0]);
903 return(py_retval);
906 static PyObject *
907 libvirt_virDomainLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
908 PyObject *py_retval;
909 virDomainPtr c_retval;
910 virConnectPtr conn;
911 PyObject *pyobj_conn;
912 unsigned char * uuid;
913 int len;
915 if (!PyArg_ParseTuple(args, (char *)"Oz#:virDomainLookupByUUID", &pyobj_conn, &uuid, &len))
916 return(NULL);
917 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
919 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
920 return VIR_PY_NONE;
922 LIBVIRT_BEGIN_ALLOW_THREADS;
923 c_retval = virDomainLookupByUUID(conn, uuid);
924 LIBVIRT_END_ALLOW_THREADS;
925 py_retval = libvirt_virDomainPtrWrap((virDomainPtr) c_retval);
926 return(py_retval);
930 static PyObject *
931 libvirt_virConnectListNetworks(PyObject *self ATTRIBUTE_UNUSED,
932 PyObject *args) {
933 PyObject *py_retval;
934 char **names = NULL;
935 int c_retval, i;
936 virConnectPtr conn;
937 PyObject *pyobj_conn;
940 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListNetworks", &pyobj_conn))
941 return(NULL);
942 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
944 c_retval = virConnectNumOfNetworks(conn);
945 if (c_retval < 0)
946 return VIR_PY_NONE;
948 if (c_retval) {
949 names = malloc(sizeof(*names) * c_retval);
950 if (!names)
951 return VIR_PY_NONE;
952 c_retval = virConnectListNetworks(conn, names, c_retval);
953 if (c_retval < 0) {
954 free(names);
955 return VIR_PY_NONE;
958 py_retval = PyList_New(c_retval);
960 if (names) {
961 for (i = 0;i < c_retval;i++) {
962 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
963 free(names[i]);
965 free(names);
968 return(py_retval);
972 static PyObject *
973 libvirt_virConnectListDefinedNetworks(PyObject *self ATTRIBUTE_UNUSED,
974 PyObject *args) {
975 PyObject *py_retval;
976 char **names = NULL;
977 int c_retval, i;
978 virConnectPtr conn;
979 PyObject *pyobj_conn;
982 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedNetworks", &pyobj_conn))
983 return(NULL);
984 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
986 c_retval = virConnectNumOfDefinedNetworks(conn);
987 if (c_retval < 0)
988 return VIR_PY_NONE;
990 if (c_retval) {
991 names = malloc(sizeof(*names) * c_retval);
992 if (!names)
993 return VIR_PY_NONE;
994 c_retval = virConnectListDefinedNetworks(conn, names, c_retval);
995 if (c_retval < 0) {
996 free(names);
997 return VIR_PY_NONE;
1000 py_retval = PyList_New(c_retval);
1002 if (names) {
1003 for (i = 0;i < c_retval;i++) {
1004 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1005 free(names[i]);
1007 free(names);
1010 return(py_retval);
1014 static PyObject *
1015 libvirt_virNetworkGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1016 PyObject *py_retval;
1017 unsigned char uuid[VIR_UUID_BUFLEN];
1018 virNetworkPtr domain;
1019 PyObject *pyobj_domain;
1020 int c_retval;
1022 if (!PyArg_ParseTuple(args, (char *)"O:virNetworkGetUUID", &pyobj_domain))
1023 return(NULL);
1024 domain = (virNetworkPtr) PyvirNetwork_Get(pyobj_domain);
1026 if (domain == NULL)
1027 return VIR_PY_NONE;
1028 LIBVIRT_BEGIN_ALLOW_THREADS;
1029 c_retval = virNetworkGetUUID(domain, &uuid[0]);
1030 LIBVIRT_END_ALLOW_THREADS;
1032 if (c_retval < 0)
1033 return VIR_PY_NONE;
1034 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
1036 return(py_retval);
1039 static PyObject *
1040 libvirt_virNetworkGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
1041 PyObject *args) {
1042 PyObject *py_retval;
1043 char uuidstr[VIR_UUID_STRING_BUFLEN];
1044 virNetworkPtr net;
1045 PyObject *pyobj_net;
1046 int c_retval;
1048 if (!PyArg_ParseTuple(args, (char *)"O:virNetworkGetUUIDString",
1049 &pyobj_net))
1050 return(NULL);
1051 net = (virNetworkPtr) PyvirNetwork_Get(pyobj_net);
1053 if (net == NULL)
1054 return VIR_PY_NONE;
1055 LIBVIRT_BEGIN_ALLOW_THREADS;
1056 c_retval = virNetworkGetUUIDString(net, &uuidstr[0]);
1057 LIBVIRT_END_ALLOW_THREADS;
1059 if (c_retval < 0)
1060 return VIR_PY_NONE;
1062 py_retval = PyString_FromString((char *) &uuidstr[0]);
1063 return(py_retval);
1066 static PyObject *
1067 libvirt_virNetworkLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1068 PyObject *py_retval;
1069 virNetworkPtr c_retval;
1070 virConnectPtr conn;
1071 PyObject *pyobj_conn;
1072 unsigned char * uuid;
1073 int len;
1075 if (!PyArg_ParseTuple(args, (char *)"Oz#:virNetworkLookupByUUID", &pyobj_conn, &uuid, &len))
1076 return(NULL);
1077 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1079 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
1080 return VIR_PY_NONE;
1082 LIBVIRT_BEGIN_ALLOW_THREADS;
1083 c_retval = virNetworkLookupByUUID(conn, uuid);
1084 LIBVIRT_END_ALLOW_THREADS;
1085 py_retval = libvirt_virNetworkPtrWrap((virNetworkPtr) c_retval);
1086 return(py_retval);
1090 static PyObject *
1091 libvirt_virDomainGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1092 PyObject *py_retval;
1093 int c_retval, autostart;
1094 virDomainPtr domain;
1095 PyObject *pyobj_domain;
1097 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetAutostart", &pyobj_domain))
1098 return(NULL);
1100 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1102 LIBVIRT_BEGIN_ALLOW_THREADS;
1103 c_retval = virDomainGetAutostart(domain, &autostart);
1104 LIBVIRT_END_ALLOW_THREADS;
1106 if (c_retval < 0)
1107 return VIR_PY_INT_FAIL;
1108 py_retval = libvirt_intWrap(autostart);
1109 return(py_retval);
1113 static PyObject *
1114 libvirt_virNetworkGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1115 PyObject *py_retval;
1116 int c_retval, autostart;
1117 virNetworkPtr network;
1118 PyObject *pyobj_network;
1120 if (!PyArg_ParseTuple(args, (char *)"O:virNetworkGetAutostart", &pyobj_network))
1121 return(NULL);
1123 network = (virNetworkPtr) PyvirNetwork_Get(pyobj_network);
1125 LIBVIRT_BEGIN_ALLOW_THREADS;
1126 c_retval = virNetworkGetAutostart(network, &autostart);
1127 LIBVIRT_END_ALLOW_THREADS;
1129 if (c_retval < 0)
1130 return VIR_PY_INT_FAIL;
1131 py_retval = libvirt_intWrap(autostart);
1132 return(py_retval);
1135 static PyObject *
1136 libvirt_virNodeGetCellsFreeMemory(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
1138 PyObject *py_retval;
1139 PyObject *pyobj_conn;
1140 int startCell, maxCells, c_retval, i;
1141 virConnectPtr conn;
1142 unsigned long long *freeMems;
1144 if (!PyArg_ParseTuple(args, (char *)"Oii:virNodeGetCellsFreeMemory", &pyobj_conn, &startCell, &maxCells))
1145 return(NULL);
1147 if ((startCell < 0) || (maxCells <= 0) || (startCell + maxCells > 10000))
1148 goto error;
1150 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1151 freeMems =
1152 malloc(maxCells * sizeof(*freeMems));
1153 if (freeMems == NULL)
1154 goto error;
1156 LIBVIRT_BEGIN_ALLOW_THREADS;
1157 c_retval = virNodeGetCellsFreeMemory(conn, freeMems, startCell, maxCells);
1158 LIBVIRT_END_ALLOW_THREADS;
1160 if (c_retval < 0) {
1161 free(freeMems);
1162 error:
1163 return VIR_PY_NONE;
1165 py_retval = PyList_New(c_retval);
1166 for (i = 0;i < c_retval;i++) {
1167 PyList_SetItem(py_retval, i,
1168 libvirt_longlongWrap((long long) freeMems[i]));
1170 free(freeMems);
1171 return(py_retval);
1175 static PyObject *
1176 libvirt_virConnectListStoragePools(PyObject *self ATTRIBUTE_UNUSED,
1177 PyObject *args) {
1178 PyObject *py_retval;
1179 char **names = NULL;
1180 int c_retval, i;
1181 virConnectPtr conn;
1182 PyObject *pyobj_conn;
1185 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListStoragePools", &pyobj_conn))
1186 return(NULL);
1187 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1189 c_retval = virConnectNumOfStoragePools(conn);
1190 if (c_retval < 0)
1191 return VIR_PY_NONE;
1193 if (c_retval) {
1194 names = malloc(sizeof(*names) * c_retval);
1195 if (!names)
1196 return VIR_PY_NONE;
1197 c_retval = virConnectListStoragePools(conn, names, c_retval);
1198 if (c_retval < 0) {
1199 free(names);
1200 return VIR_PY_NONE;
1203 py_retval = PyList_New(c_retval);
1204 if (py_retval == NULL) {
1205 if (names) {
1206 for (i = 0;i < c_retval;i++)
1207 free(names[i]);
1208 free(names);
1210 return VIR_PY_NONE;
1213 if (names) {
1214 for (i = 0;i < c_retval;i++) {
1215 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1216 free(names[i]);
1218 free(names);
1221 return(py_retval);
1225 static PyObject *
1226 libvirt_virConnectListDefinedStoragePools(PyObject *self ATTRIBUTE_UNUSED,
1227 PyObject *args) {
1228 PyObject *py_retval;
1229 char **names = NULL;
1230 int c_retval, i;
1231 virConnectPtr conn;
1232 PyObject *pyobj_conn;
1235 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedStoragePools", &pyobj_conn))
1236 return(NULL);
1237 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1239 c_retval = virConnectNumOfDefinedStoragePools(conn);
1240 if (c_retval < 0)
1241 return VIR_PY_NONE;
1243 if (c_retval) {
1244 names = malloc(sizeof(*names) * c_retval);
1245 if (!names)
1246 return VIR_PY_NONE;
1247 c_retval = virConnectListDefinedStoragePools(conn, names, c_retval);
1248 if (c_retval < 0) {
1249 free(names);
1250 return VIR_PY_NONE;
1253 py_retval = PyList_New(c_retval);
1254 if (py_retval == NULL) {
1255 if (names) {
1256 for (i = 0;i < c_retval;i++)
1257 free(names[i]);
1258 free(names);
1260 return VIR_PY_NONE;
1263 if (names) {
1264 for (i = 0;i < c_retval;i++) {
1265 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1266 free(names[i]);
1268 free(names);
1271 return(py_retval);
1275 static PyObject *
1276 libvirt_virStoragePoolListVolumes(PyObject *self ATTRIBUTE_UNUSED,
1277 PyObject *args) {
1278 PyObject *py_retval;
1279 char **names = NULL;
1280 int c_retval, i;
1281 virStoragePoolPtr pool;
1282 PyObject *pyobj_pool;
1285 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolListVolumes", &pyobj_pool))
1286 return(NULL);
1287 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
1289 c_retval = virStoragePoolNumOfVolumes(pool);
1290 if (c_retval < 0)
1291 return VIR_PY_NONE;
1293 if (c_retval) {
1294 names = malloc(sizeof(*names) * c_retval);
1295 if (!names)
1296 return VIR_PY_NONE;
1297 c_retval = virStoragePoolListVolumes(pool, names, c_retval);
1298 if (c_retval < 0) {
1299 free(names);
1300 return VIR_PY_NONE;
1303 py_retval = PyList_New(c_retval);
1304 if (py_retval == NULL) {
1305 if (names) {
1306 for (i = 0;i < c_retval;i++)
1307 free(names[i]);
1308 free(names);
1310 return VIR_PY_NONE;
1313 if (names) {
1314 for (i = 0;i < c_retval;i++) {
1315 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1316 free(names[i]);
1318 free(names);
1321 return(py_retval);
1324 static PyObject *
1325 libvirt_virStoragePoolGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1326 PyObject *py_retval;
1327 int c_retval, autostart;
1328 virStoragePoolPtr pool;
1329 PyObject *pyobj_pool;
1331 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetAutostart", &pyobj_pool))
1332 return(NULL);
1334 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
1336 LIBVIRT_BEGIN_ALLOW_THREADS;
1337 c_retval = virStoragePoolGetAutostart(pool, &autostart);
1338 LIBVIRT_END_ALLOW_THREADS;
1340 if (c_retval < 0)
1341 return VIR_PY_NONE;
1343 py_retval = libvirt_intWrap(autostart);
1344 return(py_retval);
1347 static PyObject *
1348 libvirt_virStoragePoolGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1349 PyObject *py_retval;
1350 int c_retval;
1351 virStoragePoolPtr pool;
1352 PyObject *pyobj_pool;
1353 virStoragePoolInfo info;
1355 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetInfo", &pyobj_pool))
1356 return(NULL);
1357 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
1359 LIBVIRT_BEGIN_ALLOW_THREADS;
1360 c_retval = virStoragePoolGetInfo(pool, &info);
1361 LIBVIRT_END_ALLOW_THREADS;
1362 if (c_retval < 0)
1363 return VIR_PY_NONE;
1365 if ((py_retval = PyList_New(4)) == NULL)
1366 return VIR_PY_NONE;
1368 PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.state));
1369 PyList_SetItem(py_retval, 1,
1370 libvirt_longlongWrap((unsigned long long) info.capacity));
1371 PyList_SetItem(py_retval, 2,
1372 libvirt_longlongWrap((unsigned long long) info.allocation));
1373 PyList_SetItem(py_retval, 3,
1374 libvirt_longlongWrap((unsigned long long) info.available));
1375 return(py_retval);
1379 static PyObject *
1380 libvirt_virStorageVolGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1381 PyObject *py_retval;
1382 int c_retval;
1383 virStorageVolPtr pool;
1384 PyObject *pyobj_pool;
1385 virStorageVolInfo info;
1387 if (!PyArg_ParseTuple(args, (char *)"O:virStorageVolGetInfo", &pyobj_pool))
1388 return(NULL);
1389 pool = (virStorageVolPtr) PyvirStorageVol_Get(pyobj_pool);
1391 LIBVIRT_BEGIN_ALLOW_THREADS;
1392 c_retval = virStorageVolGetInfo(pool, &info);
1393 LIBVIRT_END_ALLOW_THREADS;
1394 if (c_retval < 0)
1395 return VIR_PY_NONE;
1397 if ((py_retval = PyList_New(3)) == NULL)
1398 return VIR_PY_NONE;
1399 PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.type));
1400 PyList_SetItem(py_retval, 1,
1401 libvirt_longlongWrap((unsigned long long) info.capacity));
1402 PyList_SetItem(py_retval, 2,
1403 libvirt_longlongWrap((unsigned long long) info.allocation));
1404 return(py_retval);
1407 static PyObject *
1408 libvirt_virStoragePoolGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1409 PyObject *py_retval;
1410 unsigned char uuid[VIR_UUID_BUFLEN];
1411 virStoragePoolPtr pool;
1412 PyObject *pyobj_pool;
1413 int c_retval;
1415 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetUUID", &pyobj_pool))
1416 return(NULL);
1417 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
1419 if (pool == NULL)
1420 return VIR_PY_NONE;
1421 LIBVIRT_BEGIN_ALLOW_THREADS;
1422 c_retval = virStoragePoolGetUUID(pool, &uuid[0]);
1423 LIBVIRT_END_ALLOW_THREADS;
1425 if (c_retval < 0)
1426 return VIR_PY_NONE;
1428 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
1430 return(py_retval);
1433 static PyObject *
1434 libvirt_virStoragePoolGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
1435 PyObject *args) {
1436 PyObject *py_retval;
1437 char uuidstr[VIR_UUID_STRING_BUFLEN];
1438 virStoragePoolPtr pool;
1439 PyObject *pyobj_pool;
1440 int c_retval;
1442 if (!PyArg_ParseTuple(args, (char *)"O:virStoragePoolGetUUIDString", &pyobj_pool))
1443 return(NULL);
1444 pool = (virStoragePoolPtr) PyvirStoragePool_Get(pyobj_pool);
1446 if (pool == NULL)
1447 return VIR_PY_NONE;
1448 LIBVIRT_BEGIN_ALLOW_THREADS;
1449 c_retval = virStoragePoolGetUUIDString(pool, &uuidstr[0]);
1450 LIBVIRT_END_ALLOW_THREADS;
1452 if (c_retval < 0)
1453 return VIR_PY_NONE;
1455 py_retval = PyString_FromString((char *) &uuidstr[0]);
1456 return(py_retval);
1459 static PyObject *
1460 libvirt_virStoragePoolLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1461 PyObject *py_retval;
1462 virStoragePoolPtr c_retval;
1463 virConnectPtr conn;
1464 PyObject *pyobj_conn;
1465 unsigned char * uuid;
1466 int len;
1468 if (!PyArg_ParseTuple(args, (char *)"Oz#:virStoragePoolLookupByUUID", &pyobj_conn, &uuid, &len))
1469 return(NULL);
1470 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1472 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
1473 return VIR_PY_NONE;
1475 LIBVIRT_BEGIN_ALLOW_THREADS;
1476 c_retval = virStoragePoolLookupByUUID(conn, uuid);
1477 LIBVIRT_END_ALLOW_THREADS;
1478 py_retval = libvirt_virStoragePoolPtrWrap((virStoragePoolPtr) c_retval);
1479 return(py_retval);
1482 static PyObject *
1483 libvirt_virNodeListDevices(PyObject *self ATTRIBUTE_UNUSED,
1484 PyObject *args) {
1485 PyObject *py_retval;
1486 char **names = NULL;
1487 int c_retval, i;
1488 virConnectPtr conn;
1489 PyObject *pyobj_conn;
1490 char *cap;
1491 unsigned int flags;
1493 if (!PyArg_ParseTuple(args, (char *)"Ozi:virNodeListDevices",
1494 &pyobj_conn, &cap, &flags))
1495 return(NULL);
1496 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1498 c_retval = virNodeNumOfDevices(conn, cap, flags);
1499 if (c_retval < 0)
1500 return VIR_PY_NONE;
1502 if (c_retval) {
1503 names = malloc(sizeof(*names) * c_retval);
1504 if (!names)
1505 return VIR_PY_NONE;
1506 c_retval = virNodeListDevices(conn, cap, names, c_retval, flags);
1507 if (c_retval < 0) {
1508 free(names);
1509 return VIR_PY_NONE;
1512 py_retval = PyList_New(c_retval);
1514 if (names) {
1515 for (i = 0;i < c_retval;i++) {
1516 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1517 free(names[i]);
1519 free(names);
1522 return(py_retval);
1525 static PyObject *
1526 libvirt_virNodeDeviceListCaps(PyObject *self ATTRIBUTE_UNUSED,
1527 PyObject *args) {
1528 PyObject *py_retval;
1529 char **names = NULL;
1530 int c_retval, i;
1531 virNodeDevicePtr dev;
1532 PyObject *pyobj_dev;
1534 if (!PyArg_ParseTuple(args, (char *)"O:virNodeDeviceListCaps", &pyobj_dev))
1535 return(NULL);
1536 dev = (virNodeDevicePtr) PyvirNodeDevice_Get(pyobj_dev);
1538 c_retval = virNodeDeviceNumOfCaps(dev);
1539 if (c_retval < 0)
1540 return VIR_PY_NONE;
1542 if (c_retval) {
1543 names = malloc(sizeof(*names) * c_retval);
1544 if (!names)
1545 return VIR_PY_NONE;
1546 c_retval = virNodeDeviceListCaps(dev, names, c_retval);
1547 if (c_retval < 0) {
1548 free(names);
1549 return VIR_PY_NONE;
1552 py_retval = PyList_New(c_retval);
1554 if (names) {
1555 for (i = 0;i < c_retval;i++) {
1556 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
1557 free(names[i]);
1559 free(names);
1562 return(py_retval);
1565 static PyObject *
1566 libvirt_virSecretGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1567 PyObject *py_retval;
1568 unsigned char uuid[VIR_UUID_BUFLEN];
1569 virSecretPtr secret;
1570 PyObject *pyobj_secret;
1571 int c_retval;
1573 if (!PyArg_ParseTuple(args, (char *)"O:virSecretGetUUID", &pyobj_secret))
1574 return(NULL);
1575 secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
1577 if (secret == NULL)
1578 return VIR_PY_NONE;
1579 LIBVIRT_BEGIN_ALLOW_THREADS;
1580 c_retval = virSecretGetUUID(secret, &uuid[0]);
1581 LIBVIRT_END_ALLOW_THREADS;
1583 if (c_retval < 0)
1584 return VIR_PY_NONE;
1585 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
1587 return(py_retval);
1590 static PyObject *
1591 libvirt_virSecretGetUUIDString(PyObject *self ATTRIBUTE_UNUSED,
1592 PyObject *args) {
1593 PyObject *py_retval;
1594 char uuidstr[VIR_UUID_STRING_BUFLEN];
1595 virSecretPtr dom;
1596 PyObject *pyobj_dom;
1597 int c_retval;
1599 if (!PyArg_ParseTuple(args, (char *)"O:virSecretGetUUIDString",
1600 &pyobj_dom))
1601 return(NULL);
1602 dom = (virSecretPtr) PyvirSecret_Get(pyobj_dom);
1604 if (dom == NULL)
1605 return VIR_PY_NONE;
1606 LIBVIRT_BEGIN_ALLOW_THREADS;
1607 c_retval = virSecretGetUUIDString(dom, &uuidstr[0]);
1608 LIBVIRT_END_ALLOW_THREADS;
1610 if (c_retval < 0)
1611 return VIR_PY_NONE;
1613 py_retval = PyString_FromString((char *) &uuidstr[0]);
1614 return(py_retval);
1617 static PyObject *
1618 libvirt_virSecretLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1619 PyObject *py_retval;
1620 virSecretPtr c_retval;
1621 virConnectPtr conn;
1622 PyObject *pyobj_conn;
1623 unsigned char * uuid;
1624 int len;
1626 if (!PyArg_ParseTuple(args, (char *)"Oz#:virSecretLookupByUUID", &pyobj_conn, &uuid, &len))
1627 return(NULL);
1628 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1630 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
1631 return VIR_PY_NONE;
1633 LIBVIRT_BEGIN_ALLOW_THREADS;
1634 c_retval = virSecretLookupByUUID(conn, uuid);
1635 LIBVIRT_END_ALLOW_THREADS;
1636 py_retval = libvirt_virSecretPtrWrap((virSecretPtr) c_retval);
1637 return(py_retval);
1641 static PyObject *
1642 libvirt_virConnectListSecrets(PyObject *self ATTRIBUTE_UNUSED,
1643 PyObject *args) {
1644 PyObject *py_retval;
1645 char **uuids = NULL;
1646 virConnectPtr conn;
1647 int c_retval, i;
1648 PyObject *pyobj_conn;
1650 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListSecrets", &pyobj_conn))
1651 return NULL;
1652 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1654 LIBVIRT_BEGIN_ALLOW_THREADS;
1655 c_retval = virConnectNumOfSecrets(conn);
1656 LIBVIRT_END_ALLOW_THREADS;
1657 if (c_retval < 0)
1658 return VIR_PY_NONE;
1660 if (c_retval) {
1661 uuids = malloc(sizeof(*uuids) * c_retval);
1662 if (!uuids)
1663 return VIR_PY_NONE;
1664 LIBVIRT_BEGIN_ALLOW_THREADS;
1665 c_retval = virConnectListSecrets(conn, uuids, c_retval);
1666 LIBVIRT_END_ALLOW_THREADS;
1667 if (c_retval < 0) {
1668 free(uuids);
1669 return VIR_PY_NONE;
1672 py_retval = PyList_New(c_retval);
1674 if (uuids) {
1675 for (i = 0;i < c_retval;i++) {
1676 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(uuids[i]));
1677 free(uuids[i]);
1679 free(uuids);
1682 return py_retval;
1685 static PyObject *
1686 libvirt_virSecretGetValue(PyObject *self ATTRIBUTE_UNUSED,
1687 PyObject *args) {
1688 PyObject *py_retval;
1689 unsigned char *c_retval;
1690 size_t size;
1691 virSecretPtr secret;
1692 PyObject *pyobj_secret;
1693 unsigned int flags;
1695 if (!PyArg_ParseTuple(args, (char *)"Oi:virSecretGetValue", &pyobj_secret,
1696 &flags))
1697 return NULL;
1698 secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
1700 LIBVIRT_BEGIN_ALLOW_THREADS;
1701 c_retval = virSecretGetValue(secret, &size, flags);
1702 LIBVIRT_END_ALLOW_THREADS;
1704 if (c_retval == NULL)
1705 return VIR_PY_NONE;
1707 py_retval = PyString_FromStringAndSize((const char *)c_retval, size);
1708 memset(c_retval, 0, size);
1709 free(c_retval);
1711 return py_retval;
1714 static PyObject *
1715 libvirt_virSecretSetValue(PyObject *self ATTRIBUTE_UNUSED,
1716 PyObject *args) {
1717 PyObject *py_retval;
1718 int c_retval;
1719 virSecretPtr secret;
1720 PyObject *pyobj_secret;
1721 const char *value;
1722 int size;
1723 unsigned int flags;
1725 if (!PyArg_ParseTuple(args, (char *)"Oz#i:virSecretSetValue", &pyobj_secret,
1726 &value, &size, &flags))
1727 return NULL;
1728 secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
1730 LIBVIRT_BEGIN_ALLOW_THREADS;
1731 c_retval = virSecretSetValue(secret, (const unsigned char *)value, size,
1732 flags);
1733 LIBVIRT_END_ALLOW_THREADS;
1735 py_retval = libvirt_intWrap(c_retval);
1736 return py_retval;
1739 /*******************************************
1740 * Helper functions to avoid importing modules
1741 * for every callback
1742 *******************************************/
1743 static PyObject *libvirt_module = NULL;
1744 static PyObject *libvirt_dict = NULL;
1745 static PyObject *libvirt_dom_class = NULL;
1747 static PyObject *
1748 getLibvirtModuleObject (void) {
1749 if(libvirt_module)
1750 return libvirt_module;
1752 // PyImport_ImportModule returns a new reference
1753 /* Bogus (char *) cast for RHEL-5 python API brokenness */
1754 libvirt_module = PyImport_ImportModule((char *)"libvirt");
1755 if(!libvirt_module) {
1756 #if DEBUG_ERROR
1757 printf("%s Error importing libvirt module\n", __FUNCTION__);
1758 #endif
1759 PyErr_Print();
1760 return NULL;
1763 return libvirt_module;
1766 static PyObject *
1767 getLibvirtDictObject (void) {
1768 if(libvirt_dict)
1769 return libvirt_dict;
1771 // PyModule_GetDict returns a borrowed reference
1772 libvirt_dict = PyModule_GetDict(getLibvirtModuleObject());
1773 if(!libvirt_dict) {
1774 #if DEBUG_ERROR
1775 printf("%s Error importing libvirt dictionary\n", __FUNCTION__);
1776 #endif
1777 PyErr_Print();
1778 return NULL;
1781 Py_INCREF(libvirt_dict);
1782 return libvirt_dict;
1785 static PyObject *
1786 getLibvirtDomainClassObject (void) {
1787 if(libvirt_dom_class)
1788 return libvirt_dom_class;
1790 // PyDict_GetItemString returns a borrowed reference
1791 libvirt_dom_class = PyDict_GetItemString(getLibvirtDictObject(),
1792 "virDomain");
1793 if(!libvirt_dom_class) {
1794 #if DEBUG_ERROR
1795 printf("%s Error importing virDomain class\n", __FUNCTION__);
1796 #endif
1797 PyErr_Print();
1798 return NULL;
1801 Py_INCREF(libvirt_dom_class);
1802 return libvirt_dom_class;
1804 /*******************************************
1805 * Domain Events
1806 *******************************************/
1808 static int
1809 libvirt_virConnectDomainEventCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
1810 virDomainPtr dom,
1811 int event,
1812 int detail,
1813 void *opaque)
1815 PyObject *pyobj_ret;
1817 PyObject *pyobj_conn_inst = (PyObject*)opaque;
1818 PyObject *pyobj_dom;
1820 PyObject *pyobj_dom_args;
1821 PyObject *pyobj_dom_inst;
1823 PyObject *dom_class;
1824 int ret = -1;
1826 LIBVIRT_ENSURE_THREAD_STATE;
1828 /* Create a python instance of this virDomainPtr */
1829 virDomainRef(dom);
1830 pyobj_dom = libvirt_virDomainPtrWrap(dom);
1831 pyobj_dom_args = PyTuple_New(2);
1832 if(PyTuple_SetItem(pyobj_dom_args, 0, pyobj_conn_inst)!=0) {
1833 #if DEBUG_ERROR
1834 printf("%s error creating tuple",__FUNCTION__);
1835 #endif
1836 goto cleanup;
1838 if(PyTuple_SetItem(pyobj_dom_args, 1, pyobj_dom)!=0) {
1839 #if DEBUG_ERROR
1840 printf("%s error creating tuple",__FUNCTION__);
1841 #endif
1842 goto cleanup;
1844 Py_INCREF(pyobj_conn_inst);
1846 dom_class = getLibvirtDomainClassObject();
1847 if(!PyClass_Check(dom_class)) {
1848 #if DEBUG_ERROR
1849 printf("%s dom_class is not a class!\n", __FUNCTION__);
1850 #endif
1851 goto cleanup;
1854 pyobj_dom_inst = PyInstance_New(dom_class,
1855 pyobj_dom_args,
1856 NULL);
1858 Py_DECREF(pyobj_dom_args);
1860 if(!pyobj_dom_inst) {
1861 #if DEBUG_ERROR
1862 printf("%s Error creating a python instance of virDomain\n", __FUNCTION__);
1863 #endif
1864 PyErr_Print();
1865 goto cleanup;
1868 /* Call the Callback Dispatcher */
1869 pyobj_ret = PyObject_CallMethod(pyobj_conn_inst,
1870 (char*)"dispatchDomainEventCallbacks",
1871 (char*)"Oii",
1872 pyobj_dom_inst,
1873 event,
1874 detail);
1876 Py_DECREF(pyobj_dom_inst);
1878 if(!pyobj_ret) {
1879 #if DEBUG_ERROR
1880 printf("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
1881 #endif
1882 PyErr_Print();
1883 } else {
1884 Py_DECREF(pyobj_ret);
1885 ret = 0;
1889 cleanup:
1890 LIBVIRT_RELEASE_THREAD_STATE;
1891 return ret;
1894 static PyObject *
1895 libvirt_virConnectDomainEventRegister(ATTRIBUTE_UNUSED PyObject * self,
1896 PyObject * args)
1898 PyObject *py_retval; /* return value */
1899 PyObject *pyobj_conn; /* virConnectPtr */
1900 PyObject *pyobj_conn_inst; /* virConnect Python object */
1902 virConnectPtr conn;
1903 int ret = 0;
1905 if (!PyArg_ParseTuple
1906 (args, (char *) "OO:virConnectDomainEventRegister",
1907 &pyobj_conn, &pyobj_conn_inst)) {
1908 #if DEBUG_ERROR
1909 printf("%s failed parsing tuple\n", __FUNCTION__);
1910 #endif
1911 return VIR_PY_INT_FAIL;
1914 #ifdef DEBUG_ERROR
1915 printf("libvirt_virConnectDomainEventRegister(%p %p) called\n",
1916 pyobj_conn, pyobj_conn_inst);
1917 #endif
1918 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1920 Py_INCREF(pyobj_conn_inst);
1922 LIBVIRT_BEGIN_ALLOW_THREADS;
1924 ret = virConnectDomainEventRegister(conn,
1925 libvirt_virConnectDomainEventCallback,
1926 (void *)pyobj_conn_inst, NULL);
1928 LIBVIRT_END_ALLOW_THREADS;
1930 py_retval = libvirt_intWrap(ret);
1931 return (py_retval);
1934 static PyObject *
1935 libvirt_virConnectDomainEventDeregister(ATTRIBUTE_UNUSED PyObject * self,
1936 PyObject * args)
1938 PyObject *py_retval;
1939 PyObject *pyobj_conn;
1940 PyObject *pyobj_conn_inst;
1942 virConnectPtr conn;
1943 int ret = 0;
1945 if (!PyArg_ParseTuple
1946 (args, (char *) "OO:virConnectDomainEventDeregister",
1947 &pyobj_conn, &pyobj_conn_inst))
1948 return (NULL);
1950 #ifdef DEBUG_ERROR
1951 printf("libvirt_virConnectDomainEventDeregister(%p) called\n", pyobj_conn);
1952 #endif
1954 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1956 LIBVIRT_BEGIN_ALLOW_THREADS;
1958 ret = virConnectDomainEventDeregister(conn, libvirt_virConnectDomainEventCallback);
1960 LIBVIRT_END_ALLOW_THREADS;
1962 Py_DECREF(pyobj_conn_inst);
1963 py_retval = libvirt_intWrap(ret);
1964 return (py_retval);
1967 /*******************************************
1968 * Event Impl
1969 *******************************************/
1970 static PyObject *addHandleObj = NULL;
1971 static char *addHandleName = NULL;
1972 static PyObject *updateHandleObj = NULL;
1973 static char *updateHandleName = NULL;
1974 static PyObject *removeHandleObj = NULL;
1975 static char *removeHandleName = NULL;
1976 static PyObject *addTimeoutObj = NULL;
1977 static char *addTimeoutName = NULL;
1978 static PyObject *updateTimeoutObj = NULL;
1979 static char *updateTimeoutName = NULL;
1980 static PyObject *removeTimeoutObj = NULL;
1981 static char *removeTimeoutName = NULL;
1983 #define NAME(fn) ( fn ## Name ? fn ## Name : # fn )
1985 static int
1986 libvirt_virEventAddHandleFunc (int fd,
1987 int event,
1988 virEventHandleCallback cb,
1989 void *opaque,
1990 virFreeCallback ff)
1992 PyObject *result;
1993 PyObject *python_cb;
1994 PyObject *cb_obj;
1995 PyObject *ff_obj;
1996 PyObject *opaque_obj;
1997 PyObject *cb_args;
1998 PyObject *pyobj_args;
1999 int retval = -1;
2001 LIBVIRT_ENSURE_THREAD_STATE;
2003 /* Lookup the python callback */
2004 python_cb = PyDict_GetItemString(getLibvirtDictObject(),
2005 "eventInvokeHandleCallback");
2006 if(!python_cb) {
2007 #if DEBUG_ERROR
2008 printf("%s: Error finding eventInvokeHandleCallback\n", __FUNCTION__);
2009 #endif
2010 PyErr_Print();
2011 PyErr_Clear();
2012 goto cleanup;
2014 if (!PyCallable_Check(python_cb)) {
2015 #if DEBUG_ERROR
2016 char *name = py_str(python_cb);
2017 printf("%s: %s is not callable\n", __FUNCTION__,
2018 name ? name : "libvirt.eventInvokeHandleCallback");
2019 free(name);
2020 #endif
2021 goto cleanup;
2023 Py_INCREF(python_cb);
2025 /* create tuple for cb */
2026 cb_obj = libvirt_virEventHandleCallbackWrap(cb);
2027 ff_obj = libvirt_virFreeCallbackWrap(ff);
2028 opaque_obj = libvirt_virVoidPtrWrap(opaque);
2030 cb_args = PyTuple_New(3);
2031 PyTuple_SetItem(cb_args, 0, cb_obj);
2032 PyTuple_SetItem(cb_args, 1, opaque_obj);
2033 PyTuple_SetItem(cb_args, 2, ff_obj);
2035 pyobj_args = PyTuple_New(4);
2036 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(fd));
2037 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(event));
2038 PyTuple_SetItem(pyobj_args, 2, python_cb);
2039 PyTuple_SetItem(pyobj_args, 3, cb_args);
2041 result = PyEval_CallObject(addHandleObj, pyobj_args);
2042 if (!result) {
2043 PyErr_Print();
2044 PyErr_Clear();
2045 } else if (!PyInt_Check(result)) {
2046 #if DEBUG_ERROR
2047 printf("%s: %s should return an int\n", __FUNCTION__, NAME(addHandle));
2048 #endif
2049 } else {
2050 retval = (int)PyInt_AsLong(result);
2053 Py_XDECREF(result);
2054 Py_DECREF(pyobj_args);
2056 cleanup:
2057 LIBVIRT_RELEASE_THREAD_STATE;
2059 return retval;
2062 static void
2063 libvirt_virEventUpdateHandleFunc(int watch, int event)
2065 PyObject *result;
2066 PyObject *pyobj_args;
2068 LIBVIRT_ENSURE_THREAD_STATE;
2070 pyobj_args = PyTuple_New(2);
2071 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(watch));
2072 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(event));
2074 result = PyEval_CallObject(updateHandleObj, pyobj_args);
2075 if (!result) {
2076 PyErr_Print();
2077 PyErr_Clear();
2080 Py_XDECREF(result);
2081 Py_DECREF(pyobj_args);
2083 LIBVIRT_RELEASE_THREAD_STATE;
2087 static int
2088 libvirt_virEventRemoveHandleFunc(int watch)
2090 PyObject *result;
2091 PyObject *pyobj_args;
2092 PyObject *opaque;
2093 PyObject *ff;
2094 int retval = -1;
2095 virFreeCallback cff;
2097 LIBVIRT_ENSURE_THREAD_STATE;
2099 pyobj_args = PyTuple_New(1);
2100 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(watch));
2102 result = PyEval_CallObject(removeHandleObj, pyobj_args);
2103 if (!result) {
2104 PyErr_Print();
2105 PyErr_Clear();
2106 } else if (!PyTuple_Check(result) || PyTuple_Size(result) != 3) {
2107 #if DEBUG_ERROR
2108 printf("%s: %s must return opaque obj registered with %s"
2109 "to avoid leaking libvirt memory\n",
2110 __FUNCTION__, NAME(removeHandle), NAME(addHandle));
2111 #endif
2112 } else {
2113 opaque = PyTuple_GetItem(result, 1);
2114 ff = PyTuple_GetItem(result, 2);
2115 cff = PyvirFreeCallback_Get(ff);
2116 if (cff)
2117 (*cff)(PyvirVoidPtr_Get(opaque));
2118 retval = 0;
2121 Py_XDECREF(result);
2122 Py_DECREF(pyobj_args);
2124 LIBVIRT_RELEASE_THREAD_STATE;
2126 return retval;
2129 static int
2130 libvirt_virEventAddTimeoutFunc(int timeout,
2131 virEventTimeoutCallback cb,
2132 void *opaque,
2133 virFreeCallback ff)
2135 PyObject *result;
2137 PyObject *python_cb;
2139 PyObject *cb_obj;
2140 PyObject *ff_obj;
2141 PyObject *opaque_obj;
2142 PyObject *cb_args;
2143 PyObject *pyobj_args;
2144 int retval = -1;
2146 LIBVIRT_ENSURE_THREAD_STATE;
2148 /* Lookup the python callback */
2149 python_cb = PyDict_GetItemString(getLibvirtDictObject(),
2150 "eventInvokeTimeoutCallback");
2151 if(!python_cb) {
2152 #if DEBUG_ERROR
2153 printf("%s: Error finding eventInvokeTimeoutCallback\n", __FUNCTION__);
2154 #endif
2155 PyErr_Print();
2156 PyErr_Clear();
2157 goto cleanup;
2159 if (!PyCallable_Check(python_cb)) {
2160 #if DEBUG_ERROR
2161 char *name = py_str(python_cb);
2162 printf("%s: %s is not callable\n", __FUNCTION__,
2163 name ? name : "libvirt.eventInvokeTimeoutCallback");
2164 free(name);
2165 #endif
2166 goto cleanup;
2168 Py_INCREF(python_cb);
2170 /* create tuple for cb */
2171 cb_obj = libvirt_virEventTimeoutCallbackWrap(cb);
2172 ff_obj = libvirt_virFreeCallbackWrap(ff);
2173 opaque_obj = libvirt_virVoidPtrWrap(opaque);
2175 cb_args = PyTuple_New(3);
2176 PyTuple_SetItem(cb_args, 0, cb_obj);
2177 PyTuple_SetItem(cb_args, 1, opaque_obj);
2178 PyTuple_SetItem(cb_args, 2, ff_obj);
2180 pyobj_args = PyTuple_New(3);
2182 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timeout));
2183 PyTuple_SetItem(pyobj_args, 1, python_cb);
2184 PyTuple_SetItem(pyobj_args, 2, cb_args);
2186 result = PyEval_CallObject(addTimeoutObj, pyobj_args);
2187 if (!result) {
2188 PyErr_Print();
2189 PyErr_Clear();
2190 } else if (!PyInt_Check(result)) {
2191 #if DEBUG_ERROR
2192 printf("%s: %s should return an int\n", __FUNCTION__, NAME(addTimeout));
2193 #endif
2194 } else {
2195 retval = (int)PyInt_AsLong(result);
2198 Py_XDECREF(result);
2199 Py_DECREF(pyobj_args);
2201 cleanup:
2202 LIBVIRT_RELEASE_THREAD_STATE;
2203 return retval;
2206 static void
2207 libvirt_virEventUpdateTimeoutFunc(int timer, int timeout)
2209 PyObject *result = NULL;
2210 PyObject *pyobj_args;
2212 LIBVIRT_ENSURE_THREAD_STATE;
2214 pyobj_args = PyTuple_New(2);
2215 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timer));
2216 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(timeout));
2218 result = PyEval_CallObject(updateTimeoutObj, pyobj_args);
2219 if (!result) {
2220 PyErr_Print();
2221 PyErr_Clear();
2224 Py_XDECREF(result);
2225 Py_DECREF(pyobj_args);
2227 LIBVIRT_RELEASE_THREAD_STATE;
2230 static int
2231 libvirt_virEventRemoveTimeoutFunc(int timer)
2233 PyObject *result = NULL;
2234 PyObject *pyobj_args;
2235 PyObject *opaque;
2236 PyObject *ff;
2237 int retval = -1;
2238 virFreeCallback cff;
2240 LIBVIRT_ENSURE_THREAD_STATE;
2242 pyobj_args = PyTuple_New(1);
2243 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timer));
2245 result = PyEval_CallObject(removeTimeoutObj, pyobj_args);
2246 if (!result) {
2247 PyErr_Print();
2248 PyErr_Clear();
2249 } else if (!PyTuple_Check(result) || PyTuple_Size(result) != 3) {
2250 #if DEBUG_ERROR
2251 printf("%s: %s must return opaque obj registered with %s"
2252 "to avoid leaking libvirt memory\n",
2253 __FUNCTION__, NAME(removeTimeout), NAME(addTimeout));
2254 #endif
2255 } else {
2256 opaque = PyTuple_GetItem(result, 1);
2257 ff = PyTuple_GetItem(result, 2);
2258 cff = PyvirFreeCallback_Get(ff);
2259 if (cff)
2260 (*cff)(PyvirVoidPtr_Get(opaque));
2261 retval = 0;
2264 Py_XDECREF(result);
2265 Py_DECREF(pyobj_args);
2267 LIBVIRT_RELEASE_THREAD_STATE;
2269 return retval;
2272 static PyObject *
2273 libvirt_virEventRegisterImpl(ATTRIBUTE_UNUSED PyObject * self,
2274 PyObject * args)
2276 /* Unref the previously-registered impl (if any) */
2277 Py_XDECREF(addHandleObj);
2278 free(addHandleName);
2279 Py_XDECREF(updateHandleObj);
2280 free(updateHandleName);
2281 Py_XDECREF(removeHandleObj);
2282 free(removeHandleName);
2283 Py_XDECREF(addTimeoutObj);
2284 free(addTimeoutName);
2285 Py_XDECREF(updateTimeoutObj);
2286 free(updateTimeoutName);
2287 Py_XDECREF(removeTimeoutObj);
2288 free(removeTimeoutName);
2290 /* Parse and check arguments */
2291 if (!PyArg_ParseTuple(args, (char *) "OOOOOO:virEventRegisterImpl",
2292 &addHandleObj, &updateHandleObj,
2293 &removeHandleObj, &addTimeoutObj,
2294 &updateTimeoutObj, &removeTimeoutObj) ||
2295 !PyCallable_Check(addHandleObj) ||
2296 !PyCallable_Check(updateHandleObj) ||
2297 !PyCallable_Check(removeHandleObj) ||
2298 !PyCallable_Check(addTimeoutObj) ||
2299 !PyCallable_Check(updateTimeoutObj) ||
2300 !PyCallable_Check(removeTimeoutObj))
2301 return VIR_PY_INT_FAIL;
2303 /* Get argument string representations (for error reporting) */
2304 addHandleName = py_str(addTimeoutObj);
2305 updateHandleName = py_str(updateHandleObj);
2306 removeHandleName = py_str(removeHandleObj);
2307 addTimeoutName = py_str(addTimeoutObj);
2308 updateTimeoutName = py_str(updateTimeoutObj);
2309 removeTimeoutName = py_str(removeTimeoutObj);
2311 /* Inc refs since we're holding onto these objects until
2312 * the next call (if any) to this function.
2314 Py_INCREF(addHandleObj);
2315 Py_INCREF(updateHandleObj);
2316 Py_INCREF(removeHandleObj);
2317 Py_INCREF(addTimeoutObj);
2318 Py_INCREF(updateTimeoutObj);
2319 Py_INCREF(removeTimeoutObj);
2321 LIBVIRT_BEGIN_ALLOW_THREADS;
2323 /* Now register our C EventImpl, which will dispatch
2324 * to the Python callbacks passed in as args.
2326 virEventRegisterImpl(libvirt_virEventAddHandleFunc,
2327 libvirt_virEventUpdateHandleFunc,
2328 libvirt_virEventRemoveHandleFunc,
2329 libvirt_virEventAddTimeoutFunc,
2330 libvirt_virEventUpdateTimeoutFunc,
2331 libvirt_virEventRemoveTimeoutFunc);
2333 LIBVIRT_END_ALLOW_THREADS;
2335 return VIR_PY_INT_SUCCESS;
2338 static PyObject *
2339 libvirt_virEventInvokeHandleCallback(PyObject *self ATTRIBUTE_UNUSED,
2340 PyObject *args)
2342 int watch, fd, event;
2343 PyObject *py_f;
2344 PyObject *py_opaque;
2345 virEventHandleCallback cb;
2346 void *opaque;
2348 if (!PyArg_ParseTuple
2349 (args, (char *) "iiiOO:virEventInvokeHandleCallback",
2350 &watch, &fd, &event, &py_f, &py_opaque
2352 return VIR_PY_INT_FAIL;
2354 cb = (virEventHandleCallback) PyvirEventHandleCallback_Get(py_f);
2355 opaque = (void *) PyvirVoidPtr_Get(py_opaque);
2357 if(cb)
2358 cb (watch, fd, event, opaque);
2360 return VIR_PY_INT_SUCCESS;
2363 static PyObject *
2364 libvirt_virEventInvokeTimeoutCallback(PyObject *self ATTRIBUTE_UNUSED,
2365 PyObject *args)
2367 int timer;
2368 PyObject *py_f;
2369 PyObject *py_opaque;
2370 virEventTimeoutCallback cb;
2371 void *opaque;
2373 if (!PyArg_ParseTuple
2374 (args, (char *) "iOO:virEventInvokeTimeoutCallback",
2375 &timer, &py_f, &py_opaque
2377 return VIR_PY_INT_FAIL;
2379 cb = (virEventTimeoutCallback) PyvirEventTimeoutCallback_Get(py_f);
2380 opaque = (void *) PyvirVoidPtr_Get(py_opaque);
2381 if(cb)
2382 cb (timer, opaque);
2384 return VIR_PY_INT_SUCCESS;
2387 /************************************************************************
2389 * The registration stuff *
2391 ************************************************************************/
2392 static PyMethodDef libvirtMethods[] = {
2393 #include "libvirt-export.c"
2394 {(char *) "virGetVersion", libvirt_virGetVersion, METH_VARARGS, NULL},
2395 {(char *) "virConnectOpenAuth", libvirt_virConnectOpenAuth, METH_VARARGS, NULL},
2396 {(char *) "virConnectListDomainsID", libvirt_virConnectListDomainsID, METH_VARARGS, NULL},
2397 {(char *) "virConnectListDefinedDomains", libvirt_virConnectListDefinedDomains, METH_VARARGS, NULL},
2398 {(char *) "virConnectDomainEventRegister", libvirt_virConnectDomainEventRegister, METH_VARARGS, NULL},
2399 {(char *) "virConnectDomainEventDeregister", libvirt_virConnectDomainEventDeregister, METH_VARARGS, NULL},
2400 {(char *) "virDomainGetInfo", libvirt_virDomainGetInfo, METH_VARARGS, NULL},
2401 {(char *) "virNodeGetInfo", libvirt_virNodeGetInfo, METH_VARARGS, NULL},
2402 {(char *) "virDomainGetUUID", libvirt_virDomainGetUUID, METH_VARARGS, NULL},
2403 {(char *) "virDomainGetUUIDString", libvirt_virDomainGetUUIDString, METH_VARARGS, NULL},
2404 {(char *) "virDomainLookupByUUID", libvirt_virDomainLookupByUUID, METH_VARARGS, NULL},
2405 {(char *) "virRegisterErrorHandler", libvirt_virRegisterErrorHandler, METH_VARARGS, NULL},
2406 {(char *) "virGetLastError", libvirt_virGetLastError, METH_VARARGS, NULL},
2407 {(char *) "virConnGetLastError", libvirt_virConnGetLastError, METH_VARARGS, NULL},
2408 {(char *) "virConnectListNetworks", libvirt_virConnectListNetworks, METH_VARARGS, NULL},
2409 {(char *) "virConnectListDefinedNetworks", libvirt_virConnectListDefinedNetworks, METH_VARARGS, NULL},
2410 {(char *) "virNetworkGetUUID", libvirt_virNetworkGetUUID, METH_VARARGS, NULL},
2411 {(char *) "virNetworkGetUUIDString", libvirt_virNetworkGetUUIDString, METH_VARARGS, NULL},
2412 {(char *) "virNetworkLookupByUUID", libvirt_virNetworkLookupByUUID, METH_VARARGS, NULL},
2413 {(char *) "virDomainGetAutostart", libvirt_virDomainGetAutostart, METH_VARARGS, NULL},
2414 {(char *) "virNetworkGetAutostart", libvirt_virNetworkGetAutostart, METH_VARARGS, NULL},
2415 {(char *) "virDomainBlockStats", libvirt_virDomainBlockStats, METH_VARARGS, NULL},
2416 {(char *) "virDomainInterfaceStats", libvirt_virDomainInterfaceStats, METH_VARARGS, NULL},
2417 {(char *) "virNodeGetCellsFreeMemory", libvirt_virNodeGetCellsFreeMemory, METH_VARARGS, NULL},
2418 {(char *) "virDomainGetSchedulerType", libvirt_virDomainGetSchedulerType, METH_VARARGS, NULL},
2419 {(char *) "virDomainGetSchedulerParameters", libvirt_virDomainGetSchedulerParameters, METH_VARARGS, NULL},
2420 {(char *) "virDomainSetSchedulerParameters", libvirt_virDomainSetSchedulerParameters, METH_VARARGS, NULL},
2421 {(char *) "virDomainGetVcpus", libvirt_virDomainGetVcpus, METH_VARARGS, NULL},
2422 {(char *) "virDomainPinVcpu", libvirt_virDomainPinVcpu, METH_VARARGS, NULL},
2423 {(char *) "virConnectListStoragePools", libvirt_virConnectListStoragePools, METH_VARARGS, NULL},
2424 {(char *) "virConnectListDefinedStoragePools", libvirt_virConnectListDefinedStoragePools, METH_VARARGS, NULL},
2425 {(char *) "virStoragePoolGetAutostart", libvirt_virStoragePoolGetAutostart, METH_VARARGS, NULL},
2426 {(char *) "virStoragePoolListVolumes", libvirt_virStoragePoolListVolumes, METH_VARARGS, NULL},
2427 {(char *) "virStoragePoolGetInfo", libvirt_virStoragePoolGetInfo, METH_VARARGS, NULL},
2428 {(char *) "virStorageVolGetInfo", libvirt_virStorageVolGetInfo, METH_VARARGS, NULL},
2429 {(char *) "virStoragePoolGetUUID", libvirt_virStoragePoolGetUUID, METH_VARARGS, NULL},
2430 {(char *) "virStoragePoolGetUUIDString", libvirt_virStoragePoolGetUUIDString, METH_VARARGS, NULL},
2431 {(char *) "virStoragePoolLookupByUUID", libvirt_virStoragePoolLookupByUUID, METH_VARARGS, NULL},
2432 {(char *) "virEventRegisterImpl", libvirt_virEventRegisterImpl, METH_VARARGS, NULL},
2433 {(char *) "virEventInvokeHandleCallback", libvirt_virEventInvokeHandleCallback, METH_VARARGS, NULL},
2434 {(char *) "virEventInvokeTimeoutCallback", libvirt_virEventInvokeTimeoutCallback, METH_VARARGS, NULL},
2435 {(char *) "virNodeListDevices", libvirt_virNodeListDevices, METH_VARARGS, NULL},
2436 {(char *) "virNodeDeviceListCaps", libvirt_virNodeDeviceListCaps, METH_VARARGS, NULL},
2437 {(char *) "virSecretGetUUID", libvirt_virSecretGetUUID, METH_VARARGS, NULL},
2438 {(char *) "virSecretGetUUIDString", libvirt_virSecretGetUUIDString, METH_VARARGS, NULL},
2439 {(char *) "virSecretLookupByUUID", libvirt_virSecretLookupByUUID, METH_VARARGS, NULL},
2440 {(char *) "virConnectListSecrets", libvirt_virConnectListSecrets, METH_VARARGS, NULL},
2441 {(char *) "virSecretGetValue", libvirt_virSecretGetValue, METH_VARARGS, NULL},
2442 {(char *) "virSecretSetValue", libvirt_virSecretSetValue, METH_VARARGS, NULL},
2443 {NULL, NULL, 0, NULL}
2446 void
2447 #ifndef __CYGWIN__
2448 initlibvirtmod
2449 #else
2450 initcygvirtmod
2451 #endif
2452 (void)
2454 static int initialized = 0;
2456 if (initialized != 0)
2457 return;
2459 virInitialize();
2461 /* initialize the python extension module */
2462 Py_InitModule((char *)
2463 #ifndef __CYGWIN__
2464 "libvirtmod"
2465 #else
2466 "cygvirtmod"
2467 #endif
2468 , libvirtMethods);
2470 initialized = 1;