python: let libvirt_virConnectDomainEventCallback indicate success
[libvirt-python/ericb.git] / libvir.c
blob0c00b806b052bd837b914163337c64224b4e6a10
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 "libvirt_wrap.h"
23 #include "libvirt-py.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_virConnectListSecrets(PyObject *self ATTRIBUTE_UNUSED,
1567 PyObject *args) {
1568 PyObject *py_retval;
1569 char **uuids = NULL;
1570 virConnectPtr conn;
1571 int c_retval, i;
1572 PyObject *pyobj_conn;
1574 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListSecrets", &pyobj_conn))
1575 return NULL;
1576 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1578 LIBVIRT_BEGIN_ALLOW_THREADS;
1579 c_retval = virConnectNumOfSecrets(conn);
1580 LIBVIRT_END_ALLOW_THREADS;
1581 if (c_retval < 0)
1582 return VIR_PY_NONE;
1584 if (c_retval) {
1585 uuids = malloc(sizeof(*uuids) * c_retval);
1586 if (!uuids)
1587 return VIR_PY_NONE;
1588 LIBVIRT_BEGIN_ALLOW_THREADS;
1589 c_retval = virConnectListSecrets(conn, uuids, c_retval);
1590 LIBVIRT_END_ALLOW_THREADS;
1591 if (c_retval < 0) {
1592 free(uuids);
1593 return VIR_PY_NONE;
1596 py_retval = PyList_New(c_retval);
1598 if (uuids) {
1599 for (i = 0;i < c_retval;i++) {
1600 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(uuids[i]));
1601 free(uuids[i]);
1603 free(uuids);
1606 return py_retval;
1609 static PyObject *
1610 libvirt_virSecretGetValue(PyObject *self ATTRIBUTE_UNUSED,
1611 PyObject *args) {
1612 PyObject *py_retval;
1613 unsigned char *c_retval;
1614 size_t size;
1615 virSecretPtr secret;
1616 PyObject *pyobj_secret;
1617 unsigned int flags;
1619 if (!PyArg_ParseTuple(args, (char *)"Oi:virSecretGetValue", &pyobj_secret,
1620 &flags))
1621 return NULL;
1622 secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
1624 LIBVIRT_BEGIN_ALLOW_THREADS;
1625 c_retval = virSecretGetValue(secret, &size, flags);
1626 LIBVIRT_END_ALLOW_THREADS;
1628 if (c_retval == NULL)
1629 return VIR_PY_NONE;
1631 py_retval = PyString_FromStringAndSize((const char *)c_retval, size);
1632 memset(c_retval, 0, size);
1633 free(c_retval);
1635 return py_retval;
1638 static PyObject *
1639 libvirt_virSecretSetValue(PyObject *self ATTRIBUTE_UNUSED,
1640 PyObject *args) {
1641 PyObject *py_retval;
1642 int c_retval;
1643 virSecretPtr secret;
1644 PyObject *pyobj_secret;
1645 const char *value;
1646 int size;
1647 unsigned int flags;
1649 if (!PyArg_ParseTuple(args, (char *)"Oz#i:virSecretSetValue", &pyobj_secret,
1650 &value, &size, &flags))
1651 return NULL;
1652 secret = (virSecretPtr) PyvirSecret_Get(pyobj_secret);
1654 LIBVIRT_BEGIN_ALLOW_THREADS;
1655 c_retval = virSecretSetValue(secret, (const unsigned char *)value, size,
1656 flags);
1657 LIBVIRT_END_ALLOW_THREADS;
1659 py_retval = libvirt_intWrap(c_retval);
1660 return py_retval;
1663 /*******************************************
1664 * Helper functions to avoid importing modules
1665 * for every callback
1666 *******************************************/
1667 static PyObject *libvirt_module = NULL;
1668 static PyObject *libvirt_dict = NULL;
1669 static PyObject *libvirt_dom_class = NULL;
1671 static PyObject *
1672 getLibvirtModuleObject (void) {
1673 if(libvirt_module)
1674 return libvirt_module;
1676 // PyImport_ImportModule returns a new reference
1677 /* Bogus (char *) cast for RHEL-5 python API brokenness */
1678 libvirt_module = PyImport_ImportModule((char *)"libvirt");
1679 if(!libvirt_module) {
1680 #if DEBUG_ERROR
1681 printf("%s Error importing libvirt module\n", __FUNCTION__);
1682 #endif
1683 PyErr_Print();
1684 return NULL;
1687 return libvirt_module;
1690 static PyObject *
1691 getLibvirtDictObject (void) {
1692 if(libvirt_dict)
1693 return libvirt_dict;
1695 // PyModule_GetDict returns a borrowed reference
1696 libvirt_dict = PyModule_GetDict(getLibvirtModuleObject());
1697 if(!libvirt_dict) {
1698 #if DEBUG_ERROR
1699 printf("%s Error importing libvirt dictionary\n", __FUNCTION__);
1700 #endif
1701 PyErr_Print();
1702 return NULL;
1705 Py_INCREF(libvirt_dict);
1706 return libvirt_dict;
1709 static PyObject *
1710 getLibvirtDomainClassObject (void) {
1711 if(libvirt_dom_class)
1712 return libvirt_dom_class;
1714 // PyDict_GetItemString returns a borrowed reference
1715 libvirt_dom_class = PyDict_GetItemString(getLibvirtDictObject(),
1716 "virDomain");
1717 if(!libvirt_dom_class) {
1718 #if DEBUG_ERROR
1719 printf("%s Error importing virDomain class\n", __FUNCTION__);
1720 #endif
1721 PyErr_Print();
1722 return NULL;
1725 Py_INCREF(libvirt_dom_class);
1726 return libvirt_dom_class;
1728 /*******************************************
1729 * Domain Events
1730 *******************************************/
1732 static int
1733 libvirt_virConnectDomainEventCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
1734 virDomainPtr dom,
1735 int event,
1736 int detail,
1737 void *opaque)
1739 PyObject *pyobj_ret;
1741 PyObject *pyobj_conn_inst = (PyObject*)opaque;
1742 PyObject *pyobj_dom;
1744 PyObject *pyobj_dom_args;
1745 PyObject *pyobj_dom_inst;
1747 PyObject *dom_class;
1748 int ret = -1;
1750 LIBVIRT_ENSURE_THREAD_STATE;
1752 /* Create a python instance of this virDomainPtr */
1753 virDomainRef(dom);
1754 pyobj_dom = libvirt_virDomainPtrWrap(dom);
1755 pyobj_dom_args = PyTuple_New(2);
1756 if(PyTuple_SetItem(pyobj_dom_args, 0, pyobj_conn_inst)!=0) {
1757 #if DEBUG_ERROR
1758 printf("%s error creating tuple",__FUNCTION__);
1759 #endif
1760 goto cleanup;
1762 if(PyTuple_SetItem(pyobj_dom_args, 1, pyobj_dom)!=0) {
1763 #if DEBUG_ERROR
1764 printf("%s error creating tuple",__FUNCTION__);
1765 #endif
1766 goto cleanup;
1768 Py_INCREF(pyobj_conn_inst);
1770 dom_class = getLibvirtDomainClassObject();
1771 if(!PyClass_Check(dom_class)) {
1772 #if DEBUG_ERROR
1773 printf("%s dom_class is not a class!\n", __FUNCTION__);
1774 #endif
1775 goto cleanup;
1778 pyobj_dom_inst = PyInstance_New(dom_class,
1779 pyobj_dom_args,
1780 NULL);
1782 Py_DECREF(pyobj_dom_args);
1784 if(!pyobj_dom_inst) {
1785 #if DEBUG_ERROR
1786 printf("%s Error creating a python instance of virDomain\n", __FUNCTION__);
1787 #endif
1788 PyErr_Print();
1789 goto cleanup;
1792 /* Call the Callback Dispatcher */
1793 pyobj_ret = PyObject_CallMethod(pyobj_conn_inst,
1794 (char*)"dispatchDomainEventCallbacks",
1795 (char*)"Oii",
1796 pyobj_dom_inst,
1797 event,
1798 detail);
1800 Py_DECREF(pyobj_dom_inst);
1802 if(!pyobj_ret) {
1803 #if DEBUG_ERROR
1804 printf("%s - ret:%p\n", __FUNCTION__, pyobj_ret);
1805 #endif
1806 PyErr_Print();
1807 } else {
1808 Py_DECREF(pyobj_ret);
1809 ret = 0;
1813 cleanup:
1814 LIBVIRT_RELEASE_THREAD_STATE;
1815 return ret;
1818 static PyObject *
1819 libvirt_virConnectDomainEventRegister(ATTRIBUTE_UNUSED PyObject * self,
1820 PyObject * args)
1822 PyObject *py_retval; /* return value */
1823 PyObject *pyobj_conn; /* virConnectPtr */
1824 PyObject *pyobj_conn_inst; /* virConnect Python object */
1826 virConnectPtr conn;
1827 int ret = 0;
1829 if (!PyArg_ParseTuple
1830 (args, (char *) "OO:virConnectDomainEventRegister",
1831 &pyobj_conn, &pyobj_conn_inst)) {
1832 #if DEBUG_ERROR
1833 printf("%s failed parsing tuple\n", __FUNCTION__);
1834 #endif
1835 return VIR_PY_INT_FAIL;
1838 #ifdef DEBUG_ERROR
1839 printf("libvirt_virConnectDomainEventRegister(%p %p) called\n",
1840 pyobj_conn, pyobj_conn_inst);
1841 #endif
1842 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1844 Py_INCREF(pyobj_conn_inst);
1846 LIBVIRT_BEGIN_ALLOW_THREADS;
1848 ret = virConnectDomainEventRegister(conn,
1849 libvirt_virConnectDomainEventCallback,
1850 (void *)pyobj_conn_inst, NULL);
1852 LIBVIRT_END_ALLOW_THREADS;
1854 py_retval = libvirt_intWrap(ret);
1855 return (py_retval);
1858 static PyObject *
1859 libvirt_virConnectDomainEventDeregister(ATTRIBUTE_UNUSED PyObject * self,
1860 PyObject * args)
1862 PyObject *py_retval;
1863 PyObject *pyobj_conn;
1864 PyObject *pyobj_conn_inst;
1866 virConnectPtr conn;
1867 int ret = 0;
1869 if (!PyArg_ParseTuple
1870 (args, (char *) "OO:virConnectDomainEventDeregister",
1871 &pyobj_conn, &pyobj_conn_inst))
1872 return (NULL);
1874 #ifdef DEBUG_ERROR
1875 printf("libvirt_virConnectDomainEventDeregister(%p) called\n", pyobj_conn);
1876 #endif
1878 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1880 LIBVIRT_BEGIN_ALLOW_THREADS;
1882 ret = virConnectDomainEventDeregister(conn, libvirt_virConnectDomainEventCallback);
1884 LIBVIRT_END_ALLOW_THREADS;
1886 Py_DECREF(pyobj_conn_inst);
1887 py_retval = libvirt_intWrap(ret);
1888 return (py_retval);
1891 /*******************************************
1892 * Event Impl
1893 *******************************************/
1894 static PyObject *addHandleObj = NULL;
1895 static char *addHandleName = NULL;
1896 static PyObject *updateHandleObj = NULL;
1897 static char *updateHandleName = NULL;
1898 static PyObject *removeHandleObj = NULL;
1899 static char *removeHandleName = NULL;
1900 static PyObject *addTimeoutObj = NULL;
1901 static char *addTimeoutName = NULL;
1902 static PyObject *updateTimeoutObj = NULL;
1903 static char *updateTimeoutName = NULL;
1904 static PyObject *removeTimeoutObj = NULL;
1905 static char *removeTimeoutName = NULL;
1907 #define NAME(fn) ( fn ## Name ? fn ## Name : # fn )
1909 static int
1910 libvirt_virEventAddHandleFunc (int fd,
1911 int event,
1912 virEventHandleCallback cb,
1913 void *opaque,
1914 virFreeCallback ff)
1916 PyObject *result;
1917 PyObject *python_cb;
1918 PyObject *cb_obj;
1919 PyObject *ff_obj;
1920 PyObject *opaque_obj;
1921 PyObject *cb_args;
1922 PyObject *pyobj_args;
1923 int retval = -1;
1925 LIBVIRT_ENSURE_THREAD_STATE;
1927 /* Lookup the python callback */
1928 python_cb = PyDict_GetItemString(getLibvirtDictObject(),
1929 "eventInvokeHandleCallback");
1930 if(!python_cb) {
1931 #if DEBUG_ERROR
1932 printf("%s: Error finding eventInvokeHandleCallback\n", __FUNCTION__);
1933 #endif
1934 PyErr_Print();
1935 PyErr_Clear();
1936 goto cleanup;
1938 if (!PyCallable_Check(python_cb)) {
1939 #if DEBUG_ERROR
1940 char *name = py_str(python_cb);
1941 printf("%s: %s is not callable\n", __FUNCTION__,
1942 name ? name : "libvirt.eventInvokeHandleCallback");
1943 free(name);
1944 #endif
1945 goto cleanup;
1947 Py_INCREF(python_cb);
1949 /* create tuple for cb */
1950 cb_obj = libvirt_virEventHandleCallbackWrap(cb);
1951 ff_obj = libvirt_virFreeCallbackWrap(ff);
1952 opaque_obj = libvirt_virVoidPtrWrap(opaque);
1954 cb_args = PyTuple_New(3);
1955 PyTuple_SetItem(cb_args, 0, cb_obj);
1956 PyTuple_SetItem(cb_args, 1, opaque_obj);
1957 PyTuple_SetItem(cb_args, 2, ff_obj);
1959 pyobj_args = PyTuple_New(4);
1960 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(fd));
1961 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(event));
1962 PyTuple_SetItem(pyobj_args, 2, python_cb);
1963 PyTuple_SetItem(pyobj_args, 3, cb_args);
1965 result = PyEval_CallObject(addHandleObj, pyobj_args);
1966 if (!result) {
1967 PyErr_Print();
1968 PyErr_Clear();
1969 } else if (!PyInt_Check(result)) {
1970 #if DEBUG_ERROR
1971 printf("%s: %s should return an int\n", __FUNCTION__, NAME(addHandle));
1972 #endif
1973 } else {
1974 retval = (int)PyInt_AsLong(result);
1977 Py_XDECREF(result);
1978 Py_DECREF(pyobj_args);
1980 cleanup:
1981 LIBVIRT_RELEASE_THREAD_STATE;
1983 return retval;
1986 static void
1987 libvirt_virEventUpdateHandleFunc(int watch, int event)
1989 PyObject *result;
1990 PyObject *pyobj_args;
1992 LIBVIRT_ENSURE_THREAD_STATE;
1994 pyobj_args = PyTuple_New(2);
1995 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(watch));
1996 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(event));
1998 result = PyEval_CallObject(updateHandleObj, pyobj_args);
1999 if (!result) {
2000 PyErr_Print();
2001 PyErr_Clear();
2004 Py_XDECREF(result);
2005 Py_DECREF(pyobj_args);
2007 LIBVIRT_RELEASE_THREAD_STATE;
2011 static int
2012 libvirt_virEventRemoveHandleFunc(int watch)
2014 PyObject *result;
2015 PyObject *pyobj_args;
2016 PyObject *opaque;
2017 PyObject *ff;
2018 int retval = -1;
2019 virFreeCallback cff;
2021 LIBVIRT_ENSURE_THREAD_STATE;
2023 pyobj_args = PyTuple_New(1);
2024 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(watch));
2026 result = PyEval_CallObject(removeHandleObj, pyobj_args);
2027 if (!result) {
2028 PyErr_Print();
2029 PyErr_Clear();
2030 } else if (!PyTuple_Check(result) || PyTuple_Size(result) != 3) {
2031 #if DEBUG_ERROR
2032 printf("%s: %s must return opaque obj registered with %s"
2033 "to avoid leaking libvirt memory\n",
2034 __FUNCTION__, NAME(removeHandle), NAME(addHandle));
2035 #endif
2036 } else {
2037 opaque = PyTuple_GetItem(result, 1);
2038 ff = PyTuple_GetItem(result, 2);
2039 cff = PyvirFreeCallback_Get(ff);
2040 if (cff)
2041 (*cff)(PyvirVoidPtr_Get(opaque));
2042 retval = 0;
2045 Py_XDECREF(result);
2046 Py_DECREF(pyobj_args);
2048 LIBVIRT_RELEASE_THREAD_STATE;
2050 return retval;
2053 static int
2054 libvirt_virEventAddTimeoutFunc(int timeout,
2055 virEventTimeoutCallback cb,
2056 void *opaque,
2057 virFreeCallback ff)
2059 PyObject *result;
2061 PyObject *python_cb;
2063 PyObject *cb_obj;
2064 PyObject *ff_obj;
2065 PyObject *opaque_obj;
2066 PyObject *cb_args;
2067 PyObject *pyobj_args;
2068 int retval = -1;
2070 LIBVIRT_ENSURE_THREAD_STATE;
2072 /* Lookup the python callback */
2073 python_cb = PyDict_GetItemString(getLibvirtDictObject(),
2074 "eventInvokeTimeoutCallback");
2075 if(!python_cb) {
2076 #if DEBUG_ERROR
2077 printf("%s: Error finding eventInvokeTimeoutCallback\n", __FUNCTION__);
2078 #endif
2079 PyErr_Print();
2080 PyErr_Clear();
2081 goto cleanup;
2083 if (!PyCallable_Check(python_cb)) {
2084 #if DEBUG_ERROR
2085 char *name = py_str(python_cb);
2086 printf("%s: %s is not callable\n", __FUNCTION__,
2087 name ? name : "libvirt.eventInvokeTimeoutCallback");
2088 free(name);
2089 #endif
2090 goto cleanup;
2092 Py_INCREF(python_cb);
2094 /* create tuple for cb */
2095 cb_obj = libvirt_virEventTimeoutCallbackWrap(cb);
2096 ff_obj = libvirt_virFreeCallbackWrap(ff);
2097 opaque_obj = libvirt_virVoidPtrWrap(opaque);
2099 cb_args = PyTuple_New(3);
2100 PyTuple_SetItem(cb_args, 0, cb_obj);
2101 PyTuple_SetItem(cb_args, 1, opaque_obj);
2102 PyTuple_SetItem(cb_args, 2, ff_obj);
2104 pyobj_args = PyTuple_New(3);
2106 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timeout));
2107 PyTuple_SetItem(pyobj_args, 1, python_cb);
2108 PyTuple_SetItem(pyobj_args, 2, cb_args);
2110 result = PyEval_CallObject(addTimeoutObj, pyobj_args);
2111 if (!result) {
2112 PyErr_Print();
2113 PyErr_Clear();
2114 } else if (!PyInt_Check(result)) {
2115 #if DEBUG_ERROR
2116 printf("%s: %s should return an int\n", __FUNCTION__, NAME(addTimeout));
2117 #endif
2118 } else {
2119 retval = (int)PyInt_AsLong(result);
2122 Py_XDECREF(result);
2123 Py_DECREF(pyobj_args);
2125 cleanup:
2126 LIBVIRT_RELEASE_THREAD_STATE;
2127 return retval;
2130 static void
2131 libvirt_virEventUpdateTimeoutFunc(int timer, int timeout)
2133 PyObject *result = NULL;
2134 PyObject *pyobj_args;
2136 LIBVIRT_ENSURE_THREAD_STATE;
2138 pyobj_args = PyTuple_New(2);
2139 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timer));
2140 PyTuple_SetItem(pyobj_args, 1, libvirt_intWrap(timeout));
2142 result = PyEval_CallObject(updateTimeoutObj, pyobj_args);
2143 if (!result) {
2144 PyErr_Print();
2145 PyErr_Clear();
2148 Py_XDECREF(result);
2149 Py_DECREF(pyobj_args);
2151 LIBVIRT_RELEASE_THREAD_STATE;
2154 static int
2155 libvirt_virEventRemoveTimeoutFunc(int timer)
2157 PyObject *result = NULL;
2158 PyObject *pyobj_args;
2159 PyObject *opaque;
2160 PyObject *ff;
2161 int retval = -1;
2162 virFreeCallback cff;
2164 LIBVIRT_ENSURE_THREAD_STATE;
2166 pyobj_args = PyTuple_New(1);
2167 PyTuple_SetItem(pyobj_args, 0, libvirt_intWrap(timer));
2169 result = PyEval_CallObject(removeTimeoutObj, pyobj_args);
2170 if (!result) {
2171 PyErr_Print();
2172 PyErr_Clear();
2173 } else if (!PyTuple_Check(result) || PyTuple_Size(result) != 3) {
2174 #if DEBUG_ERROR
2175 printf("%s: %s must return opaque obj registered with %s"
2176 "to avoid leaking libvirt memory\n",
2177 __FUNCTION__, NAME(removeTimeout), NAME(addTimeout));
2178 #endif
2179 } else {
2180 opaque = PyTuple_GetItem(result, 1);
2181 ff = PyTuple_GetItem(result, 2);
2182 cff = PyvirFreeCallback_Get(ff);
2183 if (cff)
2184 (*cff)(PyvirVoidPtr_Get(opaque));
2185 retval = 0;
2188 Py_XDECREF(result);
2189 Py_DECREF(pyobj_args);
2191 LIBVIRT_RELEASE_THREAD_STATE;
2193 return retval;
2196 static PyObject *
2197 libvirt_virEventRegisterImpl(ATTRIBUTE_UNUSED PyObject * self,
2198 PyObject * args)
2200 /* Unref the previously-registered impl (if any) */
2201 Py_XDECREF(addHandleObj);
2202 free(addHandleName);
2203 Py_XDECREF(updateHandleObj);
2204 free(updateHandleName);
2205 Py_XDECREF(removeHandleObj);
2206 free(removeHandleName);
2207 Py_XDECREF(addTimeoutObj);
2208 free(addTimeoutName);
2209 Py_XDECREF(updateTimeoutObj);
2210 free(updateTimeoutName);
2211 Py_XDECREF(removeTimeoutObj);
2212 free(removeTimeoutName);
2214 /* Parse and check arguments */
2215 if (!PyArg_ParseTuple(args, (char *) "OOOOOO:virEventRegisterImpl",
2216 &addHandleObj, &updateHandleObj,
2217 &removeHandleObj, &addTimeoutObj,
2218 &updateTimeoutObj, &removeTimeoutObj) ||
2219 !PyCallable_Check(addHandleObj) ||
2220 !PyCallable_Check(updateHandleObj) ||
2221 !PyCallable_Check(removeHandleObj) ||
2222 !PyCallable_Check(addTimeoutObj) ||
2223 !PyCallable_Check(updateTimeoutObj) ||
2224 !PyCallable_Check(removeTimeoutObj))
2225 return VIR_PY_INT_FAIL;
2227 /* Get argument string representations (for error reporting) */
2228 addHandleName = py_str(addTimeoutObj);
2229 updateHandleName = py_str(updateHandleObj);
2230 removeHandleName = py_str(removeHandleObj);
2231 addTimeoutName = py_str(addTimeoutObj);
2232 updateTimeoutName = py_str(updateTimeoutObj);
2233 removeTimeoutName = py_str(removeTimeoutObj);
2235 /* Inc refs since we're holding onto these objects until
2236 * the next call (if any) to this function.
2238 Py_INCREF(addHandleObj);
2239 Py_INCREF(updateHandleObj);
2240 Py_INCREF(removeHandleObj);
2241 Py_INCREF(addTimeoutObj);
2242 Py_INCREF(updateTimeoutObj);
2243 Py_INCREF(removeTimeoutObj);
2245 LIBVIRT_BEGIN_ALLOW_THREADS;
2247 /* Now register our C EventImpl, which will dispatch
2248 * to the Python callbacks passed in as args.
2250 virEventRegisterImpl(libvirt_virEventAddHandleFunc,
2251 libvirt_virEventUpdateHandleFunc,
2252 libvirt_virEventRemoveHandleFunc,
2253 libvirt_virEventAddTimeoutFunc,
2254 libvirt_virEventUpdateTimeoutFunc,
2255 libvirt_virEventRemoveTimeoutFunc);
2257 LIBVIRT_END_ALLOW_THREADS;
2259 return VIR_PY_INT_SUCCESS;
2262 static PyObject *
2263 libvirt_virEventInvokeHandleCallback(PyObject *self ATTRIBUTE_UNUSED,
2264 PyObject *args)
2266 int watch, fd, event;
2267 PyObject *py_f;
2268 PyObject *py_opaque;
2269 virEventHandleCallback cb;
2270 void *opaque;
2272 if (!PyArg_ParseTuple
2273 (args, (char *) "iiiOO:virEventInvokeHandleCallback",
2274 &watch, &fd, &event, &py_f, &py_opaque
2276 return VIR_PY_INT_FAIL;
2278 cb = (virEventHandleCallback) PyvirEventHandleCallback_Get(py_f);
2279 opaque = (void *) PyvirVoidPtr_Get(py_opaque);
2281 if(cb)
2282 cb (watch, fd, event, opaque);
2284 return VIR_PY_INT_SUCCESS;
2287 static PyObject *
2288 libvirt_virEventInvokeTimeoutCallback(PyObject *self ATTRIBUTE_UNUSED,
2289 PyObject *args)
2291 int timer;
2292 PyObject *py_f;
2293 PyObject *py_opaque;
2294 virEventTimeoutCallback cb;
2295 void *opaque;
2297 if (!PyArg_ParseTuple
2298 (args, (char *) "iOO:virEventInvokeTimeoutCallback",
2299 &timer, &py_f, &py_opaque
2301 return VIR_PY_INT_FAIL;
2303 cb = (virEventTimeoutCallback) PyvirEventTimeoutCallback_Get(py_f);
2304 opaque = (void *) PyvirVoidPtr_Get(py_opaque);
2305 if(cb)
2306 cb (timer, opaque);
2308 return VIR_PY_INT_SUCCESS;
2311 /************************************************************************
2313 * The registration stuff *
2315 ************************************************************************/
2316 static PyMethodDef libvirtMethods[] = {
2317 #include "libvirt-export.c"
2318 {(char *) "virGetVersion", libvirt_virGetVersion, METH_VARARGS, NULL},
2319 {(char *) "virConnectOpenAuth", libvirt_virConnectOpenAuth, METH_VARARGS, NULL},
2320 {(char *) "virConnectListDomainsID", libvirt_virConnectListDomainsID, METH_VARARGS, NULL},
2321 {(char *) "virConnectListDefinedDomains", libvirt_virConnectListDefinedDomains, METH_VARARGS, NULL},
2322 {(char *) "virConnectDomainEventRegister", libvirt_virConnectDomainEventRegister, METH_VARARGS, NULL},
2323 {(char *) "virConnectDomainEventDeregister", libvirt_virConnectDomainEventDeregister, METH_VARARGS, NULL},
2324 {(char *) "virDomainGetInfo", libvirt_virDomainGetInfo, METH_VARARGS, NULL},
2325 {(char *) "virNodeGetInfo", libvirt_virNodeGetInfo, METH_VARARGS, NULL},
2326 {(char *) "virDomainGetUUID", libvirt_virDomainGetUUID, METH_VARARGS, NULL},
2327 {(char *) "virDomainGetUUIDString", libvirt_virDomainGetUUIDString, METH_VARARGS, NULL},
2328 {(char *) "virDomainLookupByUUID", libvirt_virDomainLookupByUUID, METH_VARARGS, NULL},
2329 {(char *) "virRegisterErrorHandler", libvirt_virRegisterErrorHandler, METH_VARARGS, NULL},
2330 {(char *) "virGetLastError", libvirt_virGetLastError, METH_VARARGS, NULL},
2331 {(char *) "virConnGetLastError", libvirt_virConnGetLastError, METH_VARARGS, NULL},
2332 {(char *) "virConnectListNetworks", libvirt_virConnectListNetworks, METH_VARARGS, NULL},
2333 {(char *) "virConnectListDefinedNetworks", libvirt_virConnectListDefinedNetworks, METH_VARARGS, NULL},
2334 {(char *) "virNetworkGetUUID", libvirt_virNetworkGetUUID, METH_VARARGS, NULL},
2335 {(char *) "virNetworkGetUUIDString", libvirt_virNetworkGetUUIDString, METH_VARARGS, NULL},
2336 {(char *) "virNetworkLookupByUUID", libvirt_virNetworkLookupByUUID, METH_VARARGS, NULL},
2337 {(char *) "virDomainGetAutostart", libvirt_virDomainGetAutostart, METH_VARARGS, NULL},
2338 {(char *) "virNetworkGetAutostart", libvirt_virNetworkGetAutostart, METH_VARARGS, NULL},
2339 {(char *) "virDomainBlockStats", libvirt_virDomainBlockStats, METH_VARARGS, NULL},
2340 {(char *) "virDomainInterfaceStats", libvirt_virDomainInterfaceStats, METH_VARARGS, NULL},
2341 {(char *) "virNodeGetCellsFreeMemory", libvirt_virNodeGetCellsFreeMemory, METH_VARARGS, NULL},
2342 {(char *) "virDomainGetSchedulerType", libvirt_virDomainGetSchedulerType, METH_VARARGS, NULL},
2343 {(char *) "virDomainGetSchedulerParameters", libvirt_virDomainGetSchedulerParameters, METH_VARARGS, NULL},
2344 {(char *) "virDomainSetSchedulerParameters", libvirt_virDomainSetSchedulerParameters, METH_VARARGS, NULL},
2345 {(char *) "virDomainGetVcpus", libvirt_virDomainGetVcpus, METH_VARARGS, NULL},
2346 {(char *) "virDomainPinVcpu", libvirt_virDomainPinVcpu, METH_VARARGS, NULL},
2347 {(char *) "virConnectListStoragePools", libvirt_virConnectListStoragePools, METH_VARARGS, NULL},
2348 {(char *) "virConnectListDefinedStoragePools", libvirt_virConnectListDefinedStoragePools, METH_VARARGS, NULL},
2349 {(char *) "virStoragePoolGetAutostart", libvirt_virStoragePoolGetAutostart, METH_VARARGS, NULL},
2350 {(char *) "virStoragePoolListVolumes", libvirt_virStoragePoolListVolumes, METH_VARARGS, NULL},
2351 {(char *) "virStoragePoolGetInfo", libvirt_virStoragePoolGetInfo, METH_VARARGS, NULL},
2352 {(char *) "virStorageVolGetInfo", libvirt_virStorageVolGetInfo, METH_VARARGS, NULL},
2353 {(char *) "virStoragePoolGetUUID", libvirt_virStoragePoolGetUUID, METH_VARARGS, NULL},
2354 {(char *) "virStoragePoolGetUUIDString", libvirt_virStoragePoolGetUUIDString, METH_VARARGS, NULL},
2355 {(char *) "virStoragePoolLookupByUUID", libvirt_virStoragePoolLookupByUUID, METH_VARARGS, NULL},
2356 {(char *) "virEventRegisterImpl", libvirt_virEventRegisterImpl, METH_VARARGS, NULL},
2357 {(char *) "virEventInvokeHandleCallback", libvirt_virEventInvokeHandleCallback, METH_VARARGS, NULL},
2358 {(char *) "virEventInvokeTimeoutCallback", libvirt_virEventInvokeTimeoutCallback, METH_VARARGS, NULL},
2359 {(char *) "virNodeListDevices", libvirt_virNodeListDevices, METH_VARARGS, NULL},
2360 {(char *) "virNodeDeviceListCaps", libvirt_virNodeDeviceListCaps, METH_VARARGS, NULL},
2361 {(char *) "virConnectListSecrets", libvirt_virConnectListSecrets, METH_VARARGS, NULL},
2362 {(char *) "virSecretGetValue", libvirt_virSecretGetValue, METH_VARARGS, NULL},
2363 {(char *) "virSecretSetValue", libvirt_virSecretSetValue, METH_VARARGS, NULL},
2364 {NULL, NULL, 0, NULL}
2367 void
2368 #ifndef __CYGWIN__
2369 initlibvirtmod
2370 #else
2371 initcygvirtmod
2372 #endif
2373 (void)
2375 static int initialized = 0;
2377 if (initialized != 0)
2378 return;
2380 virInitialize();
2382 /* initialize the python extension module */
2383 Py_InitModule((char *)
2384 #ifndef __CYGWIN__
2385 "libvirtmod"
2386 #else
2387 "cygvirtmod"
2388 #endif
2389 , libvirtMethods);
2391 initialized = 1;