Debian Codegraph @

libvirt-utils.c

raw symbols SCIP
1 /*
2 * libvirt-utils.c: misc helper APIs for python binding
3 *
4 * Copyright (C) 2013-2019 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 #include <Python.h>
23
24 #include <string.h>
25
26 /* Ugly python defines that, which is also defined in errno.h */
27 #undef _POSIC_C_SOURCE
28
29 /* We want to see *_LAST enums. */
30 #define VIR_ENUM_SENTINELS
31
32 #include <errno.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <libvirt/libvirt.h>
37 #include "libvirt-utils.h"
38 #include "typewrappers.h"
39
40 /**
41 * virAlloc:
42 * @ptrptr: pointer to pointer for address of allocated memory
43 * @size: number of bytes to allocate
44 *
45 * Allocate 'size' bytes of memory. Return the address of the
46 * allocated memory in 'ptrptr'. The newly allocated memory is
47 * filled with zeros.
48 *
49 * Returns -1 on failure to allocate, zero on success
50 */
51 int
52 virAlloc(void *ptrptr,
53 size_t size)
54 {
55 *(void **)ptrptr = calloc(1, size);
56 if (*(void **)ptrptr == NULL) {
57 return -1;
58 }
59 return 0;
60 }
61
62 /**
63 * virAllocN:
64 * @ptrptr: pointer to pointer for address of allocated memory
65 * @size: number of bytes to allocate
66 * @count: number of elements to allocate
67 *
68 * Allocate an array of memory 'count' elements long,
69 * each with 'size' bytes. Return the address of the
70 * allocated memory in 'ptrptr'. The newly allocated
71 * memory is filled with zeros.
72 *
73 * Returns -1 on failure to allocate, zero on success
74 */
75 int
76 virAllocN(void *ptrptr,
77 size_t size,
78 size_t count)
79 {
80 *(void**)ptrptr = calloc(count, size);
81 if (*(void**)ptrptr == NULL) {
82 return -1;
83 }
84 return 0;
85 }
86
87 /**
88 * virReallocN:
89 * @ptrptr: pointer to pointer for address of allocated memory
90 * @size: number of bytes to allocate
91 * @count: number of elements in array
92 *
93 * Resize the block of memory in 'ptrptr' to be an array of
94 * 'count' elements, each 'size' bytes in length. Update 'ptrptr'
95 * with the address of the newly allocated memory. On failure,
96 * 'ptrptr' is not changed and still points to the original memory
97 * block. Any newly allocated memory in 'ptrptr' is uninitialized.
98 *
99 * Returns -1 on failure to allocate, zero on success
100 */
101 int
102 virReallocN(void *ptrptr,
103 size_t size,
104 size_t count)
105 {
106 void *tmp;
107
108 if (xalloc_oversized(count, size)) {
109 errno = ENOMEM;
110 return -1;
111 }
112 tmp = realloc(*(void**)ptrptr, size * count);
113 if (!tmp && ((size * count) != 0)) {
114 return -1;
115 }
116 *(void**)ptrptr = tmp;
117 return 0;
118 }
119
120
121 /**
122 * virFree:
123 * @ptrptr: pointer to pointer for address of memory to be freed
124 *
125 * Release the chunk of memory in the pointer pointed to by
126 * the 'ptrptr' variable. After release, 'ptrptr' will be
127 * updated to point to NULL.
128 */
129 void
130 virFree(void *ptrptr)
131 {
132 int save_errno = errno;
133
134 free(*(void**)ptrptr);
135 *(void**)ptrptr = NULL;
136 errno = save_errno;
137 }
138
139
140 int
141 virFileClose(int *fdptr)
142 {
143 int saved_errno = 0;
144 int rc = 0;
145
146 saved_errno = errno;
147
148 if (*fdptr < 0)
149 return 0;
150
151 rc = close(*fdptr);
152 *fdptr = -1;
153
154 errno = saved_errno;
155
156 return rc;
157 }
158
159 #if ! LIBVIR_CHECK_VERSION(1, 0, 2)
160 /**
161 * virTypedParamsClear:
162 * @params: the array of the typed parameters
163 * @nparams: number of parameters in the @params array
164 *
165 * Frees all memory used by string parameters. The memory occupied by @params
166 * is not free; use virTypedParamsFree if you want it to be freed too.
167 *
168 * Returns nothing.
169 */
170 void
171 virTypedParamsClear(virTypedParameterPtr params,
172 int nparams)
173 {
174 size_t i;
175
176 if (!params)
177 return;
178
179 for (i = 0; i < nparams; i++) {
180 if (params[i].type == VIR_TYPED_PARAM_STRING)
181 VIR_FREE(params[i].value.s);
182 }
183 }
184
185 /**
186 * virTypedParamsFree:
187 * @params: the array of the typed parameters
188 * @nparams: number of parameters in the @params array
189 *
190 * Frees all memory used by string parameters and the memory occuiped by
191 * @params.
192 *
193 * Returns nothing.
194 */
195 void
196 virTypedParamsFree(virTypedParameterPtr params,
197 int nparams)
198 {
199 virTypedParamsClear(params, nparams);
200 VIR_FREE(params);
201 }
202 #endif /* ! LIBVIR_CHECK_VERSION(1, 0, 2) */
203
204 /* Helper function to convert a virTypedParameter output array into a
205 * Python dictionary for return to the user. Return NULL on failure,
206 * after raising a python exception. */
207 PyObject *
208 getPyVirTypedParameter(const virTypedParameter *params,
209 int nparams)
210 {
211 PyObject *key, *val, *info;
212 ssize_t i;
213
214 if ((info = PyDict_New()) == NULL)
215 return NULL;
216
217 for (i = 0; i < nparams; i++) {
218 switch (params[i].type) {
219 case VIR_TYPED_PARAM_INT:
220 val = libvirt_intWrap(params[i].value.i);
221 break;
222
223 case VIR_TYPED_PARAM_UINT:
224 val = libvirt_intWrap(params[i].value.ui);
225 break;
226
227 case VIR_TYPED_PARAM_LLONG:
228 val = libvirt_longlongWrap(params[i].value.l);
229 break;
230
231 case VIR_TYPED_PARAM_ULLONG:
232 val = libvirt_ulonglongWrap(params[i].value.ul);
233 break;
234
235 case VIR_TYPED_PARAM_DOUBLE:
236 val = PyFloat_FromDouble(params[i].value.d);
237 break;
238
239 case VIR_TYPED_PARAM_BOOLEAN:
240 val = PyBool_FromLong(params[i].value.b);
241 break;
242
243 case VIR_TYPED_PARAM_STRING:
244 val = libvirt_constcharPtrWrap(params[i].value.s);
245 break;
246
247 default:
248 /* Possible if a newer server has a bug and sent stuff we
249 * don't recognize. */
250 PyErr_Format(PyExc_LookupError,
251 "Type value \"%d\" not recognized",
252 params[i].type);
253 val = NULL;
254 break;
255 }
256
257 key = libvirt_constcharPtrWrap(params[i].field);
258
259 VIR_PY_DICT_SET_GOTO(info, key, val, cleanup);
260 }
261 return info;
262
263 cleanup:
264 Py_DECREF(info);
265 return NULL;
266 }
267
268 /* Allocate a new typed parameter array with the same contents and
269 * length as info, and using the array params of length nparams as
270 * hints on what types to use when creating the new array. The caller
271 * must clear the array before freeing it. Return NULL on failure,
272 * after raising a python exception. */
273 virTypedParameterPtr
274 setPyVirTypedParameter(PyObject *info,
275 const virTypedParameter *params,
276 int nparams)
277 {
278 PyObject *key, *value;
279 Py_ssize_t pos = 0;
280 virTypedParameterPtr temp = NULL, ret = NULL;
281 Py_ssize_t size;
282 ssize_t i;
283
284 if ((size = PyDict_Size(info)) < 0)
285 return NULL;
286
287 /* Libvirt APIs use NULL array and 0 size as a special case;
288 * setting should have at least one parameter. */
289 if (size == 0) {
290 PyErr_Format(PyExc_LookupError, "Dictionary must not be empty");
291 return NULL;
292 }
293
294 if (VIR_ALLOC_N(ret, size) < 0) {
295 PyErr_NoMemory();
296 return NULL;
297 }
298
299 temp = &ret[0];
300 while (PyDict_Next(info, &pos, &key, &value)) {
301 char *keystr = NULL;
302
303 if (libvirt_charPtrUnwrap(key, &keystr) < 0)
304 goto cleanup;
305
306 for (i = 0; i < nparams; i++) {
307 if (STREQ(params[i].field, keystr))
308 break;
309 }
310 if (i == nparams) {
311 PyErr_Format(PyExc_LookupError,
312 "Attribute name \"%s\" could not be recognized",
313 keystr);
314 VIR_FREE(keystr);
315 goto cleanup;
316 }
317
318 strncpy(temp->field, keystr, VIR_TYPED_PARAM_FIELD_LENGTH - 1);
319 temp->type = params[i].type;
320 VIR_FREE(keystr);
321
322 switch (params[i].type) {
323 case VIR_TYPED_PARAM_INT:
324 if (libvirt_intUnwrap(value, &temp->value.i) < 0)
325 goto cleanup;
326 break;
327
328 case VIR_TYPED_PARAM_UINT:
329 if (libvirt_uintUnwrap(value, &temp->value.ui) < 0)
330 goto cleanup;
331 break;
332
333 case VIR_TYPED_PARAM_LLONG:
334 if (libvirt_longlongUnwrap(value, &temp->value.l) < 0)
335 goto cleanup;
336 break;
337
338 case VIR_TYPED_PARAM_ULLONG:
339 if (libvirt_ulonglongUnwrap(value, &temp->value.ul) < 0)
340 goto cleanup;
341 break;
342
343 case VIR_TYPED_PARAM_DOUBLE:
344 if (libvirt_doubleUnwrap(value, &temp->value.d) < 0)
345 goto cleanup;
346 break;
347
348 case VIR_TYPED_PARAM_BOOLEAN:
349 {
350 bool b;
351 if (libvirt_boolUnwrap(value, &b) < 0)
352 goto cleanup;
353 temp->value.b = b;
354 break;
355 }
356 case VIR_TYPED_PARAM_STRING:
357 {
358 char *string_val;
359 if (libvirt_charPtrUnwrap(value, &string_val) < 0)
360 goto cleanup;
361 temp->value.s = string_val;
362 break;
363 }
364
365 default:
366 /* Possible if a newer server has a bug and sent stuff we
367 * don't recognize. */
368 PyErr_Format(PyExc_LookupError,
369 "Type value \"%d\" not recognized",
370 params[i].type);
371 goto cleanup;
372 }
373
374 temp++;
375 }
376 return ret;
377
378 cleanup:
379 virTypedParamsFree(ret, size);
380 return NULL;
381 }
382
383
384 /* While these appeared in libvirt in 1.0.2, we only
385 * need them in the python from 1.1.0 onwards */
386 #if LIBVIR_CHECK_VERSION(1, 1, 0)
387 int
388 virPyDictToTypedParamOne(virTypedParameterPtr *params,
389 int *n,
390 int *max,
391 virPyTypedParamsHintPtr hints,
392 int nhints,
393 const char *keystr,
394 PyObject *value)
395 {
396 int rv = -1, type = -1;
397 ssize_t i;
398
399 for (i = 0; i < nhints; i++) {
400 if (STREQ(hints[i].name, keystr)) {
401 type = hints[i].type;
402 break;
403 }
404 }
405
406 if (type == -1) {
407 if (libvirt_PyString_Check(value)) {
408 type = VIR_TYPED_PARAM_STRING;
409 } else if (PyBool_Check(value)) {
410 type = VIR_TYPED_PARAM_BOOLEAN;
411 } else if (PyLong_Check(value)) {
412 unsigned long long ull = PyLong_AsUnsignedLongLong(value);
413 if (ull == (unsigned long long) -1 && PyErr_Occurred()) {
414 type = VIR_TYPED_PARAM_LLONG;
415 PyErr_Clear();
416 } else {
417 type = VIR_TYPED_PARAM_ULLONG;
418 }
419 } else if (PyFloat_Check(value)) {
420 type = VIR_TYPED_PARAM_DOUBLE;
421 }
422 }
423
424 if (type == -1) {
425 PyErr_Format(PyExc_TypeError,
426 "Unknown type of \"%s\" field", keystr);
427 goto cleanup;
428 }
429
430 switch ((virTypedParameterType) type) {
431 case VIR_TYPED_PARAM_INT:
432 {
433 int val;
434 if (libvirt_intUnwrap(value, &val) < 0 ||
435 virTypedParamsAddInt(params, n, max, keystr, val) < 0)
436 goto cleanup;
437 break;
438 }
439 case VIR_TYPED_PARAM_UINT:
440 {
441 unsigned int val;
442 if (libvirt_uintUnwrap(value, &val) < 0 ||
443 virTypedParamsAddUInt(params, n, max, keystr, val) < 0)
444 goto cleanup;
445 break;
446 }
447 case VIR_TYPED_PARAM_LLONG:
448 {
449 long long val;
450 if (libvirt_longlongUnwrap(value, &val) < 0 ||
451 virTypedParamsAddLLong(params, n, max, keystr, val) < 0)
452 goto cleanup;
453 break;
454 }
455 case VIR_TYPED_PARAM_ULLONG:
456 {
457 unsigned long long val;
458 if (libvirt_ulonglongUnwrap(value, &val) < 0 ||
459 virTypedParamsAddULLong(params, n, max, keystr, val) < 0)
460 goto cleanup;
461 break;
462 }
463 case VIR_TYPED_PARAM_DOUBLE:
464 {
465 double val;
466 if (libvirt_doubleUnwrap(value, &val) < 0 ||
467 virTypedParamsAddDouble(params, n, max, keystr, val) < 0)
468 goto cleanup;
469 break;
470 }
471 case VIR_TYPED_PARAM_BOOLEAN:
472 {
473 bool val;
474 if (libvirt_boolUnwrap(value, &val) < 0 ||
475 virTypedParamsAddBoolean(params, n, max, keystr, val) < 0)
476 goto cleanup;
477 break;
478 }
479 case VIR_TYPED_PARAM_STRING:
480 {
481 char *val;;
482 if (libvirt_charPtrUnwrap(value, &val) < 0 ||
483 virTypedParamsAddString(params, n, max, keystr, val) < 0) {
484 VIR_FREE(val);
485 goto cleanup;
486 }
487 VIR_FREE(val);
488 break;
489 }
490 case VIR_TYPED_PARAM_LAST:
491 break; /* unreachable */
492 }
493
494 rv = 0;
495
496 cleanup:
497 return rv;
498 }
499
500
501 /* Automatically convert dict into type parameters based on types reported
502 * by python. All integer types are converted into LLONG (in case of a negative
503 * value) or ULLONG (in case of a positive value). If you need different
504 * handling, use @hints to explicitly specify what types should be used for
505 * specific parameters.
506 */
507 int
508 virPyDictToTypedParams(PyObject *dict,
509 virTypedParameterPtr *ret_params,
510 int *ret_nparams,
511 virPyTypedParamsHintPtr hints,
512 int nhints)
513 {
514 PyObject *key;
515 PyObject *value;
516 Py_ssize_t pos = 0;
517 virTypedParameterPtr params = NULL;
518 int n = 0;
519 int max = 0;
520 int ret = -1;
521 char *keystr = NULL;
522
523 *ret_params = NULL;
524 *ret_nparams = 0;
525
526 if (PyDict_Size(dict) < 0)
527 return -1;
528
529 while (PyDict_Next(dict, &pos, &key, &value)) {
530 if (libvirt_charPtrUnwrap(key, &keystr) < 0)
531 goto cleanup;
532
533 if (PyList_Check(value) || PyTuple_Check(value)) {
534 Py_ssize_t i, size = PySequence_Size(value);
535
536 for (i = 0; i < size; i++) {
537 PyObject *v = PySequence_GetItem(value, i);
538 if (virPyDictToTypedParamOne(&params, &n, &max,
539 hints, nhints, keystr, v) < 0)
540 goto cleanup;
541 }
542 } else if (virPyDictToTypedParamOne(&params, &n, &max,
543 hints, nhints, keystr, value) < 0)
544 goto cleanup;
545
546 VIR_FREE(keystr);
547 }
548
549 *ret_params = params;
550 *ret_nparams = n;
551 params = NULL;
552 ret = 0;
553
554 cleanup:
555 VIR_FREE(keystr);
556 virTypedParamsFree(params, n);
557 return ret;
558 }
559 #endif /* LIBVIR_CHECK_VERSION(1, 1, 0) */
560
561
562 /* virPyCpumapConvert
563 * @cpunum: the number of physical cpus of the host.
564 * @pycpumap: source cpu map, python tuple of bools.
565 * @cpumapptr: destination cpu map.
566 * @cpumaplen: destination cpu map length.
567 *
568 * Helper function to convert a pycpumap to char*.
569 *
570 * Returns 0 on success, -1 on failure with error set.
571 */
572 int
573 virPyCpumapConvert(int cpunum,
574 PyObject *pycpumap,
575 unsigned char **cpumapptr,
576 int *cpumaplen)
577 {
578 int tuple_size;
579 ssize_t i;
580 *cpumapptr = NULL;
581
582 if (!PyTuple_Check(pycpumap)) {
583 PyErr_SetString(PyExc_TypeError, "Unexpected type, tuple is required");
584 return -1;
585 }
586
587 *cpumaplen = VIR_CPU_MAPLEN(cpunum);
588
589 if ((tuple_size = PyTuple_Size(pycpumap)) == -1)
590 return -1;
591
592 if (VIR_ALLOC_N(*cpumapptr, *cpumaplen) < 0) {
593 PyErr_NoMemory();
594 return -1;
595 }
596
597 for (i = 0; i < cpunum && i < tuple_size; i++) {
598 PyObject *flag = PyTuple_GetItem(pycpumap, i);
599 bool b;
600
601 if (!flag || libvirt_boolUnwrap(flag, &b) < 0) {
602 VIR_FREE(*cpumapptr);
603 return -1;
604 }
605
606 if (b)
607 VIR_USE_CPU(*cpumapptr, i);
608 }
609
610 return 0;
611 }