1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/* libguestfs python bindings
* Copyright (C) 2009-2023 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* This file contains a small number of functions that are written by
* hand. The majority of the bindings are generated (see
* F<python/actions-*.c>).
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include "actions.h"
static PyObject **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn);
void
guestfs_int_py_extend_module (PyObject *module)
{
PyModule_AddIntMacro(module, GUESTFS_CREATE_NO_ENVIRONMENT);
PyModule_AddIntMacro(module, GUESTFS_CREATE_NO_CLOSE_ON_EXIT);
}
PyObject *
guestfs_int_py_create (PyObject *self, PyObject *args)
{
guestfs_h *g;
unsigned flags;
if (!PyArg_ParseTuple (args, (char *) "I:guestfs_create", &flags))
return NULL;
g = guestfs_create_flags (flags);
if (g == NULL) {
PyErr_SetString (PyExc_RuntimeError,
"guestfs.create: failed to allocate handle");
return NULL;
}
guestfs_set_error_handler (g, NULL, NULL);
/* This can return NULL, but in that case put_handle will have
* set the Python error string.
*/
return put_handle (g);
}
PyObject *
guestfs_int_py_close (PyObject *self, PyObject *args)
{
PyObject *py_g;
guestfs_h *g;
size_t len;
PyObject **callbacks;
if (!PyArg_ParseTuple (args, (char *) "O:guestfs_close", &py_g))
return NULL;
g = get_handle (py_g);
/* As in the OCaml bindings, there is a hard to solve case where the
* caller can delete a callback from within the callback, resulting
* in a double-free here. XXX
*
* Take care of the result of get_all_event_callbacks: NULL can be
* both an error (and some PyErr_* was called), and no events.
* 'len' is specifically 0 only in the latter case, so filter that
* out.
*/
callbacks = get_all_event_callbacks (g, &len);
if (len != 0 && callbacks == NULL)
return NULL;
Py_BEGIN_ALLOW_THREADS;
guestfs_close (g);
Py_END_ALLOW_THREADS;
if (callbacks && len > 0) {
size_t i;
for (i = 0; i < len; ++i)
Py_XDECREF (callbacks[i]);
free (callbacks);
}
Py_INCREF (Py_None);
return Py_None;
}
/* http://docs.python.org/release/2.5.2/ext/callingPython.html */
static void
guestfs_int_py_event_callback_wrapper (guestfs_h *g,
void *callback,
uint64_t event,
int event_handle,
int flags,
const char *buf, size_t buf_len,
const uint64_t *array, size_t array_len)
{
PyGILState_STATE py_save;
PyObject *py_callback = callback;
PyObject *py_array;
PyObject *args;
PyObject *a;
size_t i;
PyObject *py_r;
#ifdef HAVE_PY_ISFINALIZING
if (_Py_IsFinalizing ())
return;
#endif
py_save = PyGILState_Ensure ();
py_array = PyList_New (array_len);
for (i = 0; i < array_len; ++i) {
a = PyLong_FromLongLong (array[i]);
PyList_SET_ITEM (py_array, i, a);
}
/* XXX As with Perl we don't pass the guestfs_h handle here. */
args = Py_BuildValue ("(Kiy#O)",
(unsigned PY_LONG_LONG) event, event_handle,
buf, buf_len, py_array);
Py_DECREF (py_array);
if (args == NULL) {
PyErr_PrintEx (0);
goto out;
}
py_r = PyObject_CallObject (py_callback, args);
Py_DECREF (args);
if (py_r != NULL)
Py_DECREF (py_r);
else
/* Callback threw an exception: print it. */
PyErr_PrintEx (0);
out:
PyGILState_Release (py_save);
}
PyObject *
guestfs_int_py_set_event_callback (PyObject *self, PyObject *args)
{
PyObject *py_g;
guestfs_h *g;
PyObject *py_callback;
unsigned PY_LONG_LONG events;
int eh;
PyObject *py_eh;
char key[64];
if (!PyArg_ParseTuple (args, (char *) "OOK:guestfs_set_event_callback",
&py_g, &py_callback, &events))
return NULL;
if (!PyCallable_Check (py_callback)) {
PyErr_SetString (PyExc_TypeError,
"callback parameter is not callable "
"(eg. lambda or function)");
return NULL;
}
g = get_handle (py_g);
eh = guestfs_set_event_callback (g, guestfs_int_py_event_callback_wrapper,
events, 0, py_callback);
if (eh == -1) {
PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));
return NULL;
}
/* Increase the refcount for this callback since we are storing it
* in the opaque C libguestfs handle. We need to remember that we
* did this, so we can decrease the refcount for all undeleted
* callbacks left around at close time (see guestfs_int_py_close).
*/
Py_XINCREF (py_callback);
snprintf (key, sizeof key, "_python_event_%d", eh);
guestfs_set_private (g, key, py_callback);
py_eh = PyLong_FromLong ((long) eh);
return py_eh;
}
PyObject *
guestfs_int_py_delete_event_callback (PyObject *self, PyObject *args)
{
PyObject *py_g;
guestfs_h *g;
int eh;
PyObject *py_callback;
char key[64];
if (!PyArg_ParseTuple (args, (char *) "Oi:guestfs_delete_event_callback",
&py_g, &eh))
return NULL;
g = get_handle (py_g);
snprintf (key, sizeof key, "_python_event_%d", eh);
py_callback = guestfs_get_private (g, key);
if (py_callback) {
Py_XDECREF (py_callback);
guestfs_set_private (g, key, NULL);
guestfs_delete_event_callback (g, eh);
}
Py_INCREF (Py_None);
return Py_None;
}
PyObject *
guestfs_int_py_event_to_string (PyObject *self, PyObject *args)
{
unsigned PY_LONG_LONG events;
char *str;
PyObject *py_r;
if (!PyArg_ParseTuple (args, (char *) "K", &events))
return NULL;
str = guestfs_event_to_string (events);
if (str == NULL) {
PyErr_SetFromErrno (PyExc_RuntimeError);
return NULL;
}
py_r = guestfs_int_py_fromstring (str);
free (str);
return py_r;
}
static PyObject **
get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
{
PyObject **r;
size_t i;
const char *key;
PyObject *cb;
/* Count the length of the array that will be needed. */
*len_rtn = 0;
cb = guestfs_first_private (g, &key);
while (cb != NULL) {
if (strncmp (key, "_python_event_", strlen ("_python_event_")) == 0)
(*len_rtn)++;
cb = guestfs_next_private (g, &key);
}
/* No events, so no need to allocate anything. */
if (*len_rtn == 0)
return NULL;
/* Copy them into the return array. */
r = malloc (sizeof (PyObject *) * (*len_rtn));
if (r == NULL) {
PyErr_NoMemory ();
return NULL;
}
i = 0;
cb = guestfs_first_private (g, &key);
while (cb != NULL) {
if (strncmp (key, "_python_event_", strlen ("_python_event_")) == 0) {
r[i] = cb;
i++;
}
cb = guestfs_next_private (g, &key);
}
return r;
}
/* This list should be freed (but not the strings) after use. */
char **
guestfs_int_py_get_string_list (PyObject *obj)
{
size_t i, len;
char **r;
assert (obj);
if (!PyList_Check (obj)) {
PyErr_SetString (PyExc_TypeError, "expecting a list parameter");
return NULL;
}
Py_ssize_t slen = PyList_Size (obj);
if (slen == -1) {
PyErr_SetString (PyExc_RuntimeError, "get_string_list: PyList_Size failure");
return NULL;
}
len = (size_t) slen;
r = malloc (sizeof (char *) * (len+1));
if (r == NULL) {
PyErr_NoMemory ();
return NULL;
}
for (i = 0; i < len; ++i)
r[i] = guestfs_int_py_asstring (PyList_GetItem (obj, i));
r[len] = NULL;
return r;
}
PyObject *
guestfs_int_py_put_string_list (char * const * const argv)
{
PyObject *list, *item;
size_t argc, i;
for (argc = 0; argv[argc] != NULL; ++argc)
;
list = PyList_New (argc);
if (list == NULL)
return NULL;
for (i = 0; i < argc; ++i) {
item = guestfs_int_py_fromstring (argv[i]);
if (item == NULL) {
Py_CLEAR (list);
return NULL;
}
PyList_SetItem (list, i, item);
}
return list;
}
PyObject *
guestfs_int_py_put_table (char * const * const argv)
{
PyObject *list, *tuple, *item;
size_t argc, i;
for (argc = 0; argv[argc] != NULL; ++argc)
;
list = PyList_New (argc >> 1);
if (list == NULL)
return NULL;
for (i = 0; i < argc; i += 2) {
tuple = PyTuple_New (2);
if (tuple == NULL)
goto err;
item = guestfs_int_py_fromstring (argv[i]);
if (item == NULL)
goto err;
PyTuple_SetItem (tuple, 0, item);
item = guestfs_int_py_fromstring (argv[i+1]);
if (item == NULL)
goto err;
PyTuple_SetItem (tuple, 1, item);
PyList_SetItem (list, i >> 1, tuple);
}
return list;
err:
Py_CLEAR (list);
Py_CLEAR (tuple);
return NULL;
}
PyObject *
guestfs_int_py_fromstring (const char *str)
{
return PyUnicode_FromString (str);
}
PyObject *
guestfs_int_py_fromstringsize (const char *str, size_t size)
{
return PyUnicode_FromStringAndSize (str, size);
}
char *
guestfs_int_py_asstring (PyObject *obj)
{
PyObject *bytes = PyUnicode_AsUTF8String (obj);
return PyBytes_AS_STRING (bytes);
}
|