Python bindings return values fixes
[libvirt-python/ericb.git] / libvir.c
blob67d6252f8e06c06678dec0ffe0f32310ff2d4615
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 Red Hat, Inc.
9 * Daniel Veillard <veillard@redhat.com>
12 #include <config.h>
14 #include <Python.h>
15 #include "libvirt/libvirt.h"
16 #include "libvirt/virterror.h"
17 #include "libvirt_wrap.h"
18 #include "libvirt-py.h"
20 #ifndef __CYGWIN__
21 extern void initlibvirtmod(void);
22 #else
23 extern void initcygvirtmod(void);
24 #endif
26 /* The two-statement sequence "Py_INCREF(Py_None); return Py_None;"
27 is so common that we encapsulate it here. Now, each use is simply
28 return VIR_PY_NONE; */
29 #define VIR_PY_NONE (Py_INCREF (Py_None), Py_None)
30 #define VIR_PY_INT_FAIL (libvirt_intWrap(-1))
31 #define VIR_PY_INT_SUCCESS (libvirt_intWrap(0))
33 /************************************************************************
34 * *
35 * Statistics *
36 * *
37 ************************************************************************/
39 static PyObject *
40 libvirt_virDomainBlockStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
41 virDomainPtr domain;
42 PyObject *pyobj_domain;
43 char * path;
44 int c_retval;
45 virDomainBlockStatsStruct stats;
46 PyObject *info;
48 if (!PyArg_ParseTuple(args, (char *)"Oz:virDomainBlockStats",
49 &pyobj_domain,&path))
50 return(NULL);
51 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
53 c_retval = virDomainBlockStats(domain, path, &stats, sizeof(stats));
54 if (c_retval < 0)
55 return VIR_PY_NONE;
57 /* convert to a Python tuple of long objects */
58 if ((info = PyTuple_New(5)) == NULL)
59 return VIR_PY_NONE;
60 PyTuple_SetItem(info, 0, PyLong_FromLongLong(stats.rd_req));
61 PyTuple_SetItem(info, 1, PyLong_FromLongLong(stats.rd_bytes));
62 PyTuple_SetItem(info, 2, PyLong_FromLongLong(stats.wr_req));
63 PyTuple_SetItem(info, 3, PyLong_FromLongLong(stats.wr_bytes));
64 PyTuple_SetItem(info, 4, PyLong_FromLongLong(stats.errs));
65 return(info);
68 static PyObject *
69 libvirt_virDomainInterfaceStats(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
70 virDomainPtr domain;
71 PyObject *pyobj_domain;
72 char * path;
73 int c_retval;
74 virDomainInterfaceStatsStruct stats;
75 PyObject *info;
77 if (!PyArg_ParseTuple(args, (char *)"Oz:virDomainInterfaceStats",
78 &pyobj_domain,&path))
79 return(NULL);
80 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
82 c_retval = virDomainInterfaceStats(domain, path, &stats, sizeof(stats));
83 if (c_retval < 0)
84 return VIR_PY_NONE;
86 /* convert to a Python tuple of long objects */
87 if ((info = PyTuple_New(8)) == NULL)
88 return VIR_PY_NONE;
89 PyTuple_SetItem(info, 0, PyLong_FromLongLong(stats.rx_bytes));
90 PyTuple_SetItem(info, 1, PyLong_FromLongLong(stats.rx_packets));
91 PyTuple_SetItem(info, 2, PyLong_FromLongLong(stats.rx_errs));
92 PyTuple_SetItem(info, 3, PyLong_FromLongLong(stats.rx_drop));
93 PyTuple_SetItem(info, 4, PyLong_FromLongLong(stats.tx_bytes));
94 PyTuple_SetItem(info, 5, PyLong_FromLongLong(stats.tx_packets));
95 PyTuple_SetItem(info, 6, PyLong_FromLongLong(stats.tx_errs));
96 PyTuple_SetItem(info, 7, PyLong_FromLongLong(stats.tx_drop));
97 return(info);
101 static PyObject *
102 libvirt_virDomainGetSchedulerType(PyObject *self ATTRIBUTE_UNUSED,
103 PyObject *args) {
104 virDomainPtr domain;
105 PyObject *pyobj_domain, *info;
106 char *c_retval;
107 int nparams;
109 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetScedulerType",
110 &pyobj_domain))
111 return(NULL);
112 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
114 c_retval = virDomainGetSchedulerType(domain, &nparams);
115 if (c_retval == NULL)
116 return VIR_PY_NONE;
118 /* convert to a Python tupple of long objects */
119 if ((info = PyTuple_New(2)) == NULL) {
120 free(c_retval);
121 return VIR_PY_NONE;
124 PyTuple_SetItem(info, 0, libvirt_constcharPtrWrap(c_retval));
125 PyTuple_SetItem(info, 1, PyInt_FromLong((long)nparams));
126 free(c_retval);
127 return(info);
130 static PyObject *
131 libvirt_virDomainGetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
132 PyObject *args) {
133 virDomainPtr domain;
134 PyObject *pyobj_domain, *info;
135 char *c_retval;
136 int nparams, i;
137 virSchedParameterPtr params;
139 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetScedulerParameters",
140 &pyobj_domain))
141 return(NULL);
142 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
144 c_retval = virDomainGetSchedulerType(domain, &nparams);
145 if (c_retval == NULL)
146 return VIR_PY_NONE;
147 free(c_retval);
149 if ((params = malloc(sizeof(*params)*nparams)) == NULL)
150 return VIR_PY_NONE;
152 if (virDomainGetSchedulerParameters(domain, params, &nparams) < 0) {
153 free(params);
154 return VIR_PY_NONE;
157 /* convert to a Python tupple of long objects */
158 if ((info = PyDict_New()) == NULL) {
159 free(params);
160 return VIR_PY_NONE;
162 for (i = 0 ; i < nparams ; i++) {
163 PyObject *key, *val;
165 switch (params[i].type) {
166 case VIR_DOMAIN_SCHED_FIELD_INT:
167 val = PyInt_FromLong((long)params[i].value.i);
168 break;
170 case VIR_DOMAIN_SCHED_FIELD_UINT:
171 val = PyInt_FromLong((long)params[i].value.ui);
172 break;
174 case VIR_DOMAIN_SCHED_FIELD_LLONG:
175 val = PyLong_FromLongLong((long long)params[i].value.l);
176 break;
178 case VIR_DOMAIN_SCHED_FIELD_ULLONG:
179 val = PyLong_FromLongLong((long long)params[i].value.ul);
180 break;
182 case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
183 val = PyFloat_FromDouble((double)params[i].value.d);
184 break;
186 case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
187 val = PyBool_FromLong((long)params[i].value.b);
188 break;
190 default:
191 free(params);
192 Py_DECREF(info);
193 return VIR_PY_NONE;
196 key = libvirt_constcharPtrWrap(params[i].field);
197 PyDict_SetItem(info, key, val);
199 free(params);
200 return(info);
203 static PyObject *
204 libvirt_virDomainSetSchedulerParameters(PyObject *self ATTRIBUTE_UNUSED,
205 PyObject *args) {
206 virDomainPtr domain;
207 PyObject *pyobj_domain, *info;
208 char *c_retval;
209 int nparams, i;
210 virSchedParameterPtr params;
212 if (!PyArg_ParseTuple(args, (char *)"OO:virDomainSetScedulerParameters",
213 &pyobj_domain, &info))
214 return(NULL);
215 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
217 c_retval = virDomainGetSchedulerType(domain, &nparams);
218 if (c_retval == NULL)
219 return VIR_PY_INT_FAIL;
220 free(c_retval);
222 if ((params = malloc(sizeof(*params)*nparams)) == NULL)
223 return VIR_PY_INT_FAIL;
225 if (virDomainGetSchedulerParameters(domain, params, &nparams) < 0) {
226 free(params);
227 return VIR_PY_INT_FAIL;
230 /* convert to a Python tupple of long objects */
231 for (i = 0 ; i < nparams ; i++) {
232 PyObject *key, *val;
233 key = libvirt_constcharPtrWrap(params[i].field);
234 val = PyDict_GetItem(info, key);
235 Py_DECREF(key);
237 if (val == NULL)
238 continue;
240 switch (params[i].type) {
241 case VIR_DOMAIN_SCHED_FIELD_INT:
242 params[i].value.i = (int)PyInt_AS_LONG(val);
243 break;
245 case VIR_DOMAIN_SCHED_FIELD_UINT:
246 params[i].value.ui = (unsigned int)PyInt_AS_LONG(val);
247 break;
249 case VIR_DOMAIN_SCHED_FIELD_LLONG:
250 params[i].value.l = (long long)PyLong_AsLongLong(val);
251 break;
253 case VIR_DOMAIN_SCHED_FIELD_ULLONG:
254 params[i].value.ul = (unsigned long long)PyLong_AsLongLong(val);
255 break;
257 case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
258 params[i].value.d = (double)PyFloat_AsDouble(val);
259 break;
261 case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
263 /* Hack - Python's definition of Py_True breaks strict
264 * aliasing rules, so can't directly compare :-(
266 PyObject *hacktrue = PyBool_FromLong(1);
267 params[i].value.b = hacktrue == val ? 1 : 0;
268 Py_DECREF(hacktrue);
270 break;
272 default:
273 free(params);
274 return VIR_PY_INT_FAIL;
278 if (virDomainSetSchedulerParameters(domain, params, nparams) < 0) {
279 free(params);
280 return VIR_PY_INT_FAIL;
283 free(params);
284 return VIR_PY_INT_SUCCESS;
287 static PyObject *
288 libvirt_virDomainGetVcpus(PyObject *self ATTRIBUTE_UNUSED,
289 PyObject *args) {
290 virDomainPtr domain;
291 PyObject *pyobj_domain, *pyretval = NULL, *pycpuinfo = NULL, *pycpumap = NULL;
292 virNodeInfo nodeinfo;
293 virDomainInfo dominfo;
294 virVcpuInfoPtr cpuinfo = NULL;
295 unsigned char *cpumap = NULL;
296 int cpumaplen, i;
298 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetVcpus",
299 &pyobj_domain))
300 return(NULL);
301 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
303 if (virNodeGetInfo(virDomainGetConnect(domain), &nodeinfo) != 0)
304 return VIR_PY_NONE;
306 if (virDomainGetInfo(domain, &dominfo) != 0)
307 return VIR_PY_NONE;
309 if ((cpuinfo = malloc(sizeof(*cpuinfo)*dominfo.nrVirtCpu)) == NULL)
310 return VIR_PY_NONE;
312 cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
313 if ((cpumap = malloc(dominfo.nrVirtCpu * cpumaplen)) == NULL)
314 goto cleanup;
316 if (virDomainGetVcpus(domain,
317 cpuinfo, dominfo.nrVirtCpu,
318 cpumap, cpumaplen) < 0)
319 goto cleanup;
321 /* convert to a Python tupple of long objects */
322 if ((pyretval = PyTuple_New(2)) == NULL)
323 goto cleanup;
324 if ((pycpuinfo = PyList_New(dominfo.nrVirtCpu)) == NULL)
325 goto cleanup;
326 if ((pycpumap = PyList_New(dominfo.nrVirtCpu)) == NULL)
327 goto cleanup;
329 for (i = 0 ; i < dominfo.nrVirtCpu ; i++) {
330 PyObject *info = PyTuple_New(4);
331 if (info == NULL)
332 goto cleanup;
333 PyTuple_SetItem(info, 0, PyInt_FromLong((long)cpuinfo[i].number));
334 PyTuple_SetItem(info, 1, PyInt_FromLong((long)cpuinfo[i].state));
335 PyTuple_SetItem(info, 2, PyLong_FromLongLong((long long)cpuinfo[i].cpuTime));
336 PyTuple_SetItem(info, 3, PyInt_FromLong((long)cpuinfo[i].cpu));
337 PyList_SetItem(pycpuinfo, i, info);
339 for (i = 0 ; i < dominfo.nrVirtCpu ; i++) {
340 PyObject *info = PyTuple_New(VIR_NODEINFO_MAXCPUS(nodeinfo));
341 int j;
342 if (info == NULL)
343 goto cleanup;
344 for (j = 0 ; j < VIR_NODEINFO_MAXCPUS(nodeinfo) ; j++) {
345 PyTuple_SetItem(info, j, PyBool_FromLong(VIR_CPU_USABLE(cpumap, cpumaplen, i, j)));
347 PyList_SetItem(pycpumap, i, info);
349 PyTuple_SetItem(pyretval, 0, pycpuinfo);
350 PyTuple_SetItem(pyretval, 1, pycpumap);
352 free(cpuinfo);
353 free(cpumap);
355 return(pyretval);
357 cleanup:
358 free(cpuinfo);
359 free(cpumap);
360 /* NB, Py_DECREF is a badly defined macro, so we require
361 * braces here to avoid 'ambiguous else' warnings from
362 * the compiler.
363 * NB. this comment is true at of time of writing wrt to
364 * at least python2.5.
366 if (pyretval) { Py_DECREF(pyretval); }
367 if (pycpuinfo) { Py_DECREF(pycpuinfo); }
368 if (pycpumap) { Py_DECREF(pycpumap); }
369 return VIR_PY_NONE;
373 static PyObject *
374 libvirt_virDomainPinVcpu(PyObject *self ATTRIBUTE_UNUSED,
375 PyObject *args) {
376 virDomainPtr domain;
377 PyObject *pyobj_domain, *pycpumap, *truth;
378 virNodeInfo nodeinfo;
379 unsigned char *cpumap;
380 int cpumaplen, i, vcpu;
382 if (!PyArg_ParseTuple(args, (char *)"OiO:virDomainPinVcpu",
383 &pyobj_domain, &vcpu, &pycpumap))
384 return(NULL);
385 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
387 if (virNodeGetInfo(virDomainGetConnect(domain), &nodeinfo) != 0)
388 return VIR_PY_INT_FAIL;
390 cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
391 if ((cpumap = malloc(cpumaplen)) == NULL)
392 return VIR_PY_INT_FAIL;
393 memset(cpumap, 0, cpumaplen);
395 truth = PyBool_FromLong(1);
396 for (i = 0 ; i < VIR_NODEINFO_MAXCPUS(nodeinfo) ; i++) {
397 PyObject *flag = PyTuple_GetItem(pycpumap, i);
398 if (flag == truth)
399 VIR_USE_CPU(cpumap, i);
400 else
401 VIR_UNUSE_CPU(cpumap, i);
404 virDomainPinVcpu(domain, vcpu, cpumap, cpumaplen);
405 Py_DECREF(truth);
406 free(cpumap);
408 return VIR_PY_INT_SUCCESS;
412 /************************************************************************
414 * Global error handler at the Python level *
416 ************************************************************************/
418 static PyObject *libvirt_virPythonErrorFuncHandler = NULL;
419 static PyObject *libvirt_virPythonErrorFuncCtxt = NULL;
421 static PyObject *
422 libvirt_virGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args ATTRIBUTE_UNUSED)
424 virError err;
425 PyObject *info;
427 if (virCopyLastError(&err) <= 0)
428 return VIR_PY_NONE;
430 if ((info = PyTuple_New(9)) == NULL)
431 return VIR_PY_NONE;
432 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err.code));
433 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err.domain));
434 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err.message));
435 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err.level));
436 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err.str1));
437 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err.str2));
438 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err.str3));
439 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err.int1));
440 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err.int2));
442 return info;
445 static PyObject *
446 libvirt_virConnGetLastError(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
448 virError err;
449 PyObject *info;
450 virConnectPtr conn;
451 PyObject *pyobj_conn;
453 if (!PyArg_ParseTuple(args, (char *)"O:virConGetLastError", &pyobj_conn))
454 return(NULL);
455 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
457 if (virConnCopyLastError(conn, &err) <= 0)
458 return VIR_PY_NONE;
460 if ((info = PyTuple_New(9)) == NULL)
461 return VIR_PY_NONE;
462 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err.code));
463 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err.domain));
464 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err.message));
465 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err.level));
466 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err.str1));
467 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err.str2));
468 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err.str3));
469 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err.int1));
470 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err.int2));
472 return info;
475 static void
476 libvirt_virErrorFuncHandler(ATTRIBUTE_UNUSED void *ctx, virErrorPtr err)
478 PyObject *list, *info;
479 PyObject *result;
481 #ifdef DEBUG_ERROR
482 printf("libvirt_virErrorFuncHandler(%p, %s, ...) called\n", ctx, msg);
483 #endif
485 if ((err == NULL) || (err->code == VIR_ERR_OK))
486 return;
488 LIBVIRT_ENSURE_THREAD_STATE;
490 if ((libvirt_virPythonErrorFuncHandler == NULL) ||
491 (libvirt_virPythonErrorFuncHandler == Py_None)) {
492 virDefaultErrorFunc(err);
493 } else {
494 list = PyTuple_New(2);
495 info = PyTuple_New(9);
496 PyTuple_SetItem(list, 0, libvirt_virPythonErrorFuncCtxt);
497 PyTuple_SetItem(list, 1, info);
498 Py_XINCREF(libvirt_virPythonErrorFuncCtxt);
499 PyTuple_SetItem(info, 0, PyInt_FromLong((long) err->code));
500 PyTuple_SetItem(info, 1, PyInt_FromLong((long) err->domain));
501 PyTuple_SetItem(info, 2, libvirt_constcharPtrWrap(err->message));
502 PyTuple_SetItem(info, 3, PyInt_FromLong((long) err->level));
503 PyTuple_SetItem(info, 4, libvirt_constcharPtrWrap(err->str1));
504 PyTuple_SetItem(info, 5, libvirt_constcharPtrWrap(err->str2));
505 PyTuple_SetItem(info, 6, libvirt_constcharPtrWrap(err->str3));
506 PyTuple_SetItem(info, 7, PyInt_FromLong((long) err->int1));
507 PyTuple_SetItem(info, 8, PyInt_FromLong((long) err->int2));
508 /* TODO pass conn and dom if available */
509 result = PyEval_CallObject(libvirt_virPythonErrorFuncHandler, list);
510 Py_XDECREF(list);
511 Py_XDECREF(result);
514 LIBVIRT_RELEASE_THREAD_STATE;
517 static PyObject *
518 libvirt_virRegisterErrorHandler(ATTRIBUTE_UNUSED PyObject * self,
519 PyObject * args)
521 PyObject *py_retval;
522 PyObject *pyobj_f;
523 PyObject *pyobj_ctx;
525 if (!PyArg_ParseTuple
526 (args, (char *) "OO:xmlRegisterErrorHandler", &pyobj_f,
527 &pyobj_ctx))
528 return (NULL);
530 #ifdef DEBUG_ERROR
531 printf("libvirt_virRegisterErrorHandler(%p, %p) called\n", pyobj_ctx,
532 pyobj_f);
533 #endif
535 virSetErrorFunc(NULL, libvirt_virErrorFuncHandler);
536 if (libvirt_virPythonErrorFuncHandler != NULL) {
537 Py_XDECREF(libvirt_virPythonErrorFuncHandler);
539 if (libvirt_virPythonErrorFuncCtxt != NULL) {
540 Py_XDECREF(libvirt_virPythonErrorFuncCtxt);
543 if ((pyobj_f == Py_None) && (pyobj_ctx == Py_None)) {
544 libvirt_virPythonErrorFuncHandler = NULL;
545 libvirt_virPythonErrorFuncCtxt = NULL;
546 } else {
547 Py_XINCREF(pyobj_ctx);
548 Py_XINCREF(pyobj_f);
550 /* TODO: check f is a function ! */
551 libvirt_virPythonErrorFuncHandler = pyobj_f;
552 libvirt_virPythonErrorFuncCtxt = pyobj_ctx;
555 py_retval = libvirt_intWrap(1);
556 return (py_retval);
559 static int virConnectCredCallbackWrapper(virConnectCredentialPtr cred,
560 unsigned int ncred,
561 void *cbdata) {
562 PyObject *list;
563 PyObject *pycred;
564 PyObject *pyauth = (PyObject *)cbdata;
565 PyObject *pycbdata;
566 PyObject *pycb;
567 PyObject *pyret;
568 int ret = -1, i;
570 LIBVIRT_ENSURE_THREAD_STATE;
572 pycb = PyList_GetItem(pyauth, 1);
573 pycbdata = PyList_GetItem(pyauth, 2);
575 list = PyTuple_New(2);
576 pycred = PyTuple_New(ncred);
577 for (i = 0 ; i < ncred ; i++) {
578 PyObject *pycreditem;
579 pycreditem = PyList_New(5);
580 Py_INCREF(Py_None);
581 PyTuple_SetItem(pycred, i, pycreditem);
582 PyList_SetItem(pycreditem, 0, PyInt_FromLong((long) cred[i].type));
583 PyList_SetItem(pycreditem, 1, PyString_FromString(cred[i].prompt));
584 if (cred[i].challenge) {
585 PyList_SetItem(pycreditem, 2, PyString_FromString(cred[i].challenge));
586 } else {
587 Py_INCREF(Py_None);
588 PyList_SetItem(pycreditem, 2, Py_None);
590 if (cred[i].defresult) {
591 PyList_SetItem(pycreditem, 3, PyString_FromString(cred[i].defresult));
592 } else {
593 Py_INCREF(Py_None);
594 PyList_SetItem(pycreditem, 3, Py_None);
596 PyList_SetItem(pycreditem, 4, Py_None);
599 PyTuple_SetItem(list, 0, pycred);
600 Py_XINCREF(pycbdata);
601 PyTuple_SetItem(list, 1, pycbdata);
603 PyErr_Clear();
604 pyret = PyEval_CallObject(pycb, list);
605 if (PyErr_Occurred())
606 goto cleanup;
608 ret = PyLong_AsLong(pyret);
609 if (ret == 0) {
610 for (i = 0 ; i < ncred ; i++) {
611 PyObject *pycreditem;
612 PyObject *pyresult;
613 char *result = NULL;
614 pycreditem = PyTuple_GetItem(pycred, i);
615 pyresult = PyList_GetItem(pycreditem, 4);
616 if (pyresult != Py_None)
617 result = PyString_AsString(pyresult);
618 if (result != NULL) {
619 cred[i].result = strdup(result);
620 cred[i].resultlen = strlen(result);
621 } else {
622 cred[i].result = NULL;
623 cred[i].resultlen = 0;
628 cleanup:
629 Py_XDECREF(list);
630 Py_XDECREF(pyret);
632 LIBVIRT_RELEASE_THREAD_STATE;
634 return ret;
638 static PyObject *
639 libvirt_virConnectOpenAuth(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
640 PyObject *py_retval;
641 virConnectPtr c_retval;
642 char * name;
643 int flags;
644 PyObject *pyauth;
645 PyObject *pycredcb;
646 PyObject *pycredtype;
647 virConnectAuth auth;
649 if (!PyArg_ParseTuple(args, (char *)"zOi:virConnectOpenAuth", &name, &pyauth, &flags))
650 return(NULL);
652 pycredtype = PyList_GetItem(pyauth, 0);
653 pycredcb = PyList_GetItem(pyauth, 1);
655 auth.ncredtype = PyList_Size(pycredtype);
656 if (auth.ncredtype) {
657 int i;
658 auth.credtype = malloc(sizeof(*auth.credtype) * auth.ncredtype);
659 if (auth.credtype == NULL)
660 return VIR_PY_NONE;
661 for (i = 0 ; i < auth.ncredtype ; i++) {
662 PyObject *val;
663 val = PyList_GetItem(pycredtype, i);
664 auth.credtype[i] = (int)PyLong_AsLong(val);
667 auth.cb = pycredcb ? virConnectCredCallbackWrapper : NULL;
668 auth.cbdata = pyauth;
670 LIBVIRT_BEGIN_ALLOW_THREADS;
672 c_retval = virConnectOpenAuth(name, &auth, flags);
673 LIBVIRT_END_ALLOW_THREADS;
674 py_retval = libvirt_virConnectPtrWrap((virConnectPtr) c_retval);
675 return(py_retval);
679 /************************************************************************
681 * Wrappers for functions where generator fails *
683 ************************************************************************/
685 static PyObject *
686 libvirt_virGetVersion (PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
688 char *type = NULL;
689 unsigned long libVer, typeVer = 0;
690 int c_retval;
692 if (!PyArg_ParseTuple (args, (char *) "|s", &type))
693 return NULL;
695 LIBVIRT_BEGIN_ALLOW_THREADS;
697 if (type == NULL)
698 c_retval = virGetVersion (&libVer, NULL, NULL);
699 else
700 c_retval = virGetVersion (&libVer, type, &typeVer);
702 LIBVIRT_END_ALLOW_THREADS;
704 if (c_retval == -1)
705 return VIR_PY_NONE;
707 if (type == NULL)
708 return PyInt_FromLong (libVer);
709 else
710 return Py_BuildValue ((char *) "kk", libVer, typeVer);
714 static PyObject *
715 libvirt_virConnectListDomainsID(PyObject *self ATTRIBUTE_UNUSED,
716 PyObject *args) {
717 PyObject *py_retval;
718 int ids[500], c_retval, i;
719 virConnectPtr conn;
720 PyObject *pyobj_conn;
723 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDomains", &pyobj_conn))
724 return(NULL);
725 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
727 LIBVIRT_BEGIN_ALLOW_THREADS;
728 c_retval = virConnectListDomains(conn, &ids[0], 500);
729 LIBVIRT_END_ALLOW_THREADS;
730 if (c_retval < 0)
731 return VIR_PY_NONE;
732 py_retval = PyList_New(c_retval);
733 for (i = 0;i < c_retval;i++) {
734 PyList_SetItem(py_retval, i, libvirt_intWrap(ids[i]));
736 return(py_retval);
739 static PyObject *
740 libvirt_virConnectListDefinedDomains(PyObject *self ATTRIBUTE_UNUSED,
741 PyObject *args) {
742 PyObject *py_retval;
743 char **names = NULL;
744 int c_retval, i;
745 virConnectPtr conn;
746 PyObject *pyobj_conn;
749 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedDomains", &pyobj_conn))
750 return(NULL);
751 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
753 c_retval = virConnectNumOfDefinedDomains(conn);
754 if (c_retval < 0)
755 return VIR_PY_NONE;
757 if (c_retval) {
758 names = malloc(sizeof(*names) * c_retval);
759 if (!names)
760 return VIR_PY_NONE;
761 c_retval = virConnectListDefinedDomains(conn, names, c_retval);
762 if (c_retval < 0) {
763 free(names);
764 return VIR_PY_NONE;
767 py_retval = PyList_New(c_retval);
769 if (names) {
770 for (i = 0;i < c_retval;i++) {
771 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
772 free(names[i]);
774 free(names);
777 return(py_retval);
780 static PyObject *
781 libvirt_virDomainGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
782 PyObject *py_retval;
783 int c_retval;
784 virDomainPtr domain;
785 PyObject *pyobj_domain;
786 virDomainInfo info;
788 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetInfo", &pyobj_domain))
789 return(NULL);
790 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
792 LIBVIRT_BEGIN_ALLOW_THREADS;
793 c_retval = virDomainGetInfo(domain, &info);
794 LIBVIRT_END_ALLOW_THREADS;
795 if (c_retval < 0)
796 return VIR_PY_NONE;
797 py_retval = PyList_New(5);
798 PyList_SetItem(py_retval, 0, libvirt_intWrap((int) info.state));
799 PyList_SetItem(py_retval, 1, libvirt_ulongWrap(info.maxMem));
800 PyList_SetItem(py_retval, 2, libvirt_ulongWrap(info.memory));
801 PyList_SetItem(py_retval, 3, libvirt_intWrap((int) info.nrVirtCpu));
802 PyList_SetItem(py_retval, 4,
803 libvirt_longlongWrap((unsigned long long) info.cpuTime));
804 return(py_retval);
807 static PyObject *
808 libvirt_virNodeGetInfo(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
809 PyObject *py_retval;
810 int c_retval;
811 virConnectPtr conn;
812 PyObject *pyobj_conn;
813 virNodeInfo info;
815 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetInfo", &pyobj_conn))
816 return(NULL);
817 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
819 LIBVIRT_BEGIN_ALLOW_THREADS;
820 c_retval = virNodeGetInfo(conn, &info);
821 LIBVIRT_END_ALLOW_THREADS;
822 if (c_retval < 0)
823 return VIR_PY_NONE;
824 py_retval = PyList_New(8);
825 PyList_SetItem(py_retval, 0, libvirt_constcharPtrWrap(&info.model[0]));
826 PyList_SetItem(py_retval, 1, libvirt_longWrap((long) info.memory >> 10));
827 PyList_SetItem(py_retval, 2, libvirt_intWrap((int) info.cpus));
828 PyList_SetItem(py_retval, 3, libvirt_intWrap((int) info.mhz));
829 PyList_SetItem(py_retval, 4, libvirt_intWrap((int) info.nodes));
830 PyList_SetItem(py_retval, 5, libvirt_intWrap((int) info.sockets));
831 PyList_SetItem(py_retval, 6, libvirt_intWrap((int) info.cores));
832 PyList_SetItem(py_retval, 7, libvirt_intWrap((int) info.threads));
833 return(py_retval);
836 static PyObject *
837 libvirt_virDomainGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
838 PyObject *py_retval;
839 unsigned char uuid[VIR_UUID_BUFLEN];
840 virDomainPtr domain;
841 PyObject *pyobj_domain;
842 int c_retval;
844 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetUUID", &pyobj_domain))
845 return(NULL);
846 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
848 if (domain == NULL)
849 return VIR_PY_NONE;
850 LIBVIRT_BEGIN_ALLOW_THREADS;
851 c_retval = virDomainGetUUID(domain, &uuid[0]);
852 LIBVIRT_END_ALLOW_THREADS;
854 if (c_retval < 0)
855 return VIR_PY_NONE;
856 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
858 return(py_retval);
861 static PyObject *
862 libvirt_virDomainLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
863 PyObject *py_retval;
864 virDomainPtr c_retval;
865 virConnectPtr conn;
866 PyObject *pyobj_conn;
867 unsigned char * uuid;
868 int len;
870 if (!PyArg_ParseTuple(args, (char *)"Oz#:virDomainLookupByUUID", &pyobj_conn, &uuid, &len))
871 return(NULL);
872 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
874 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
875 return VIR_PY_NONE;
877 LIBVIRT_BEGIN_ALLOW_THREADS;
878 c_retval = virDomainLookupByUUID(conn, uuid);
879 LIBVIRT_END_ALLOW_THREADS;
880 py_retval = libvirt_virDomainPtrWrap((virDomainPtr) c_retval);
881 return(py_retval);
885 static PyObject *
886 libvirt_virConnectListNetworks(PyObject *self ATTRIBUTE_UNUSED,
887 PyObject *args) {
888 PyObject *py_retval;
889 char **names = NULL;
890 int c_retval, i;
891 virConnectPtr conn;
892 PyObject *pyobj_conn;
895 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListNetworks", &pyobj_conn))
896 return(NULL);
897 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
899 c_retval = virConnectNumOfNetworks(conn);
900 if (c_retval < 0)
901 return VIR_PY_NONE;
903 if (c_retval) {
904 names = malloc(sizeof(*names) * c_retval);
905 if (!names)
906 return VIR_PY_NONE;
907 c_retval = virConnectListNetworks(conn, names, c_retval);
908 if (c_retval < 0) {
909 free(names);
910 return VIR_PY_NONE;
913 py_retval = PyList_New(c_retval);
915 if (names) {
916 for (i = 0;i < c_retval;i++) {
917 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
918 free(names[i]);
920 free(names);
923 return(py_retval);
927 static PyObject *
928 libvirt_virConnectListDefinedNetworks(PyObject *self ATTRIBUTE_UNUSED,
929 PyObject *args) {
930 PyObject *py_retval;
931 char **names = NULL;
932 int c_retval, i;
933 virConnectPtr conn;
934 PyObject *pyobj_conn;
937 if (!PyArg_ParseTuple(args, (char *)"O:virConnectListDefinedNetworks", &pyobj_conn))
938 return(NULL);
939 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
941 c_retval = virConnectNumOfDefinedNetworks(conn);
942 if (c_retval < 0)
943 return VIR_PY_NONE;
945 if (c_retval) {
946 names = malloc(sizeof(*names) * c_retval);
947 if (!names)
948 return VIR_PY_NONE;
949 c_retval = virConnectListDefinedNetworks(conn, names, c_retval);
950 if (c_retval < 0) {
951 free(names);
952 return VIR_PY_NONE;
955 py_retval = PyList_New(c_retval);
957 if (names) {
958 for (i = 0;i < c_retval;i++) {
959 PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
960 free(names[i]);
962 free(names);
965 return(py_retval);
969 static PyObject *
970 libvirt_virNetworkGetUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
971 PyObject *py_retval;
972 unsigned char uuid[VIR_UUID_BUFLEN];
973 virNetworkPtr domain;
974 PyObject *pyobj_domain;
975 int c_retval;
977 if (!PyArg_ParseTuple(args, (char *)"O:virNetworkGetUUID", &pyobj_domain))
978 return(NULL);
979 domain = (virNetworkPtr) PyvirNetwork_Get(pyobj_domain);
981 if (domain == NULL)
982 return VIR_PY_NONE;
983 LIBVIRT_BEGIN_ALLOW_THREADS;
984 c_retval = virNetworkGetUUID(domain, &uuid[0]);
985 LIBVIRT_END_ALLOW_THREADS;
987 if (c_retval < 0)
988 return VIR_PY_NONE;
989 py_retval = PyString_FromStringAndSize((char *) &uuid[0], VIR_UUID_BUFLEN);
991 return(py_retval);
994 static PyObject *
995 libvirt_virNetworkLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
996 PyObject *py_retval;
997 virNetworkPtr c_retval;
998 virConnectPtr conn;
999 PyObject *pyobj_conn;
1000 unsigned char * uuid;
1001 int len;
1003 if (!PyArg_ParseTuple(args, (char *)"Oz#:virNetworkLookupByUUID", &pyobj_conn, &uuid, &len))
1004 return(NULL);
1005 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1007 if ((uuid == NULL) || (len != VIR_UUID_BUFLEN))
1008 return VIR_PY_NONE;
1010 LIBVIRT_BEGIN_ALLOW_THREADS;
1011 c_retval = virNetworkLookupByUUID(conn, uuid);
1012 LIBVIRT_END_ALLOW_THREADS;
1013 py_retval = libvirt_virNetworkPtrWrap((virNetworkPtr) c_retval);
1014 return(py_retval);
1018 static PyObject *
1019 libvirt_virDomainGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1020 PyObject *py_retval;
1021 int c_retval, autostart;
1022 virDomainPtr domain;
1023 PyObject *pyobj_domain;
1025 if (!PyArg_ParseTuple(args, (char *)"O:virDomainGetAutostart", &pyobj_domain))
1026 return(NULL);
1028 domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
1030 LIBVIRT_BEGIN_ALLOW_THREADS;
1031 c_retval = virDomainGetAutostart(domain, &autostart);
1032 LIBVIRT_END_ALLOW_THREADS;
1034 if (c_retval < 0)
1035 return VIR_PY_INT_FAIL;
1036 py_retval = libvirt_intWrap(autostart);
1037 return(py_retval);
1041 static PyObject *
1042 libvirt_virNetworkGetAutostart(PyObject *self ATTRIBUTE_UNUSED, PyObject *args) {
1043 PyObject *py_retval;
1044 int c_retval, autostart;
1045 virNetworkPtr network;
1046 PyObject *pyobj_network;
1048 if (!PyArg_ParseTuple(args, (char *)"O:virNetworkGetAutostart", &pyobj_network))
1049 return(NULL);
1051 network = (virNetworkPtr) PyvirNetwork_Get(pyobj_network);
1053 LIBVIRT_BEGIN_ALLOW_THREADS;
1054 c_retval = virNetworkGetAutostart(network, &autostart);
1055 LIBVIRT_END_ALLOW_THREADS;
1057 if (c_retval < 0)
1058 return VIR_PY_INT_FAIL;
1059 py_retval = libvirt_intWrap(autostart);
1060 return(py_retval);
1063 static PyObject *
1064 libvirt_virNodeGetCellsFreeMemory(PyObject *self ATTRIBUTE_UNUSED, PyObject *args)
1066 PyObject *py_retval;
1067 PyObject *pyobj_conn;
1068 int startCell, maxCells, c_retval, i;
1069 virConnectPtr conn;
1070 unsigned long long *freeMems;
1072 if (!PyArg_ParseTuple(args, (char *)"Oii:virNodeGetCellsFreeMemory", &pyobj_conn, &startCell, &maxCells))
1073 return(NULL);
1075 if ((startCell < 0) || (maxCells <= 0) || (startCell + maxCells > 10000))
1076 goto error;
1078 conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
1079 freeMems =
1080 malloc(maxCells * sizeof(*freeMems));
1081 if (freeMems == NULL)
1082 goto error;
1084 LIBVIRT_BEGIN_ALLOW_THREADS;
1085 c_retval = virNodeGetCellsFreeMemory(conn, freeMems, startCell, maxCells);
1086 LIBVIRT_END_ALLOW_THREADS;
1088 if (c_retval < 0) {
1089 free(freeMems);
1090 error:
1091 return VIR_PY_NONE;
1093 py_retval = PyList_New(c_retval);
1094 for (i = 0;i < c_retval;i++) {
1095 PyList_SetItem(py_retval, i,
1096 libvirt_longlongWrap((long long) freeMems[i]));
1098 free(freeMems);
1099 return(py_retval);
1102 /************************************************************************
1104 * The registration stuff *
1106 ************************************************************************/
1107 static PyMethodDef libvirtMethods[] = {
1108 #include "libvirt-export.c"
1109 {(char *) "virGetVersion", libvirt_virGetVersion, METH_VARARGS, NULL},
1110 {(char *) "virConnectOpenAuth", libvirt_virConnectOpenAuth, METH_VARARGS, NULL},
1111 {(char *) "virConnectListDomainsID", libvirt_virConnectListDomainsID, METH_VARARGS, NULL},
1112 {(char *) "virConnectListDefinedDomains", libvirt_virConnectListDefinedDomains, METH_VARARGS, NULL},
1113 {(char *) "virDomainGetInfo", libvirt_virDomainGetInfo, METH_VARARGS, NULL},
1114 {(char *) "virNodeGetInfo", libvirt_virNodeGetInfo, METH_VARARGS, NULL},
1115 {(char *) "virDomainGetUUID", libvirt_virDomainGetUUID, METH_VARARGS, NULL},
1116 {(char *) "virDomainLookupByUUID", libvirt_virDomainLookupByUUID, METH_VARARGS, NULL},
1117 {(char *) "virRegisterErrorHandler", libvirt_virRegisterErrorHandler, METH_VARARGS, NULL},
1118 {(char *) "virGetLastError", libvirt_virGetLastError, METH_VARARGS, NULL},
1119 {(char *) "virConnGetLastError", libvirt_virConnGetLastError, METH_VARARGS, NULL},
1120 {(char *) "virConnectListNetworks", libvirt_virConnectListNetworks, METH_VARARGS, NULL},
1121 {(char *) "virConnectListDefinedNetworks", libvirt_virConnectListDefinedNetworks, METH_VARARGS, NULL},
1122 {(char *) "virNetworkGetUUID", libvirt_virNetworkGetUUID, METH_VARARGS, NULL},
1123 {(char *) "virNetworkLookupByUUID", libvirt_virNetworkLookupByUUID, METH_VARARGS, NULL},
1124 {(char *) "virDomainGetAutostart", libvirt_virDomainGetAutostart, METH_VARARGS, NULL},
1125 {(char *) "virNetworkGetAutostart", libvirt_virNetworkGetAutostart, METH_VARARGS, NULL},
1126 {(char *) "virDomainBlockStats", libvirt_virDomainBlockStats, METH_VARARGS, NULL},
1127 {(char *) "virDomainInterfaceStats", libvirt_virDomainInterfaceStats, METH_VARARGS, NULL},
1128 {(char *) "virNodeGetCellsFreeMemory", libvirt_virNodeGetCellsFreeMemory, METH_VARARGS, NULL},
1129 {(char *) "virDomainGetSchedulerType", libvirt_virDomainGetSchedulerType, METH_VARARGS, NULL},
1130 {(char *) "virDomainGetSchedulerParameters", libvirt_virDomainGetSchedulerParameters, METH_VARARGS, NULL},
1131 {(char *) "virDomainSetSchedulerParameters", libvirt_virDomainSetSchedulerParameters, METH_VARARGS, NULL},
1132 {(char *) "virDomainGetVcpus", libvirt_virDomainGetVcpus, METH_VARARGS, NULL},
1133 {(char *) "virDomainPinVcpu", libvirt_virDomainPinVcpu, METH_VARARGS, NULL},
1134 {NULL, NULL, 0, NULL}
1137 void
1138 #ifndef __CYGWIN__
1139 initlibvirtmod
1140 #else
1141 initcygvirtmod
1142 #endif
1143 (void)
1145 static int initialized = 0;
1147 if (initialized != 0)
1148 return;
1150 virInitialize();
1152 /* intialize the python extension module */
1153 Py_InitModule((char *)
1154 #ifndef __CYGWIN__
1155 "libvirtmod"
1156 #else
1157 "cygvirtmod"
1158 #endif
1159 , libvirtMethods);
1161 initialized = 1;
1165 * vim: set tabstop=4:
1166 * vim: set shiftwidth=4:
1167 * vim: set expandtab:
1170 * Local variables:
1171 * indent-tabs-mode: nil
1172 * c-indent-level: 4
1173 * c-basic-offset: 4
1174 * tab-width: 4
1175 * End: