daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / python / guestfs-py-byhand.c
1 /* libguestfs python bindings
2  * Copyright (C) 2009-2011 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /* This file contains a small number of functions that are written by
20  * hand.  The majority of the bindings are generated (see
21  * guestfs-py.c).
22  */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include "guestfs-py.h"
31
32 static PyObject **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn);
33
34 PyObject *
35 py_guestfs_create (PyObject *self, PyObject *args)
36 {
37   guestfs_h *g;
38
39   g = guestfs_create ();
40   if (g == NULL) {
41     PyErr_SetString (PyExc_RuntimeError,
42                      "guestfs.create: failed to allocate handle");
43     return NULL;
44   }
45   guestfs_set_error_handler (g, NULL, NULL);
46   /* This can return NULL, but in that case put_handle will have
47    * set the Python error string.
48    */
49   return put_handle (g);
50 }
51
52 PyObject *
53 py_guestfs_close (PyObject *self, PyObject *args)
54 {
55   PyThreadState *py_save = NULL;
56   PyObject *py_g;
57   guestfs_h *g;
58   size_t i, len;
59   PyObject **callbacks;
60
61   if (!PyArg_ParseTuple (args, (char *) "O:guestfs_close", &py_g))
62     return NULL;
63   g = get_handle (py_g);
64
65   /* As in the OCaml bindings, there is a hard to solve case where the
66    * caller can delete a callback from within the callback, resulting
67    * in a double-free here.  XXX
68    */
69   callbacks = get_all_event_callbacks (g, &len);
70
71   if (PyEval_ThreadsInitialized ())
72     py_save = PyEval_SaveThread ();
73   guestfs_close (g);
74   if (PyEval_ThreadsInitialized ())
75     PyEval_RestoreThread (py_save);
76
77   for (i = 0; i < len; ++i)
78     Py_XDECREF (callbacks[i]);
79   free (callbacks);
80
81   Py_INCREF (Py_None);
82   return Py_None;
83 }
84
85 /* http://docs.python.org/release/2.5.2/ext/callingPython.html */
86 static void
87 py_guestfs_event_callback_wrapper (guestfs_h *g,
88                                    void *callback,
89                                    uint64_t event,
90                                    int event_handle,
91                                    int flags,
92                                    const char *buf, size_t buf_len,
93                                    const uint64_t *array, size_t array_len)
94 {
95   PyGILState_STATE py_save = PyGILState_UNLOCKED;
96   PyObject *py_callback = callback;
97   PyObject *py_array;
98   PyObject *args;
99   PyObject *a;
100   size_t i;
101   PyObject *py_r;
102
103   py_array = PyList_New (array_len);
104   for (i = 0; i < array_len; ++i) {
105     a = PyLong_FromLongLong (array[i]);
106     PyList_SET_ITEM (py_array, i, a);
107   }
108
109   /* XXX As with Perl we don't pass the guestfs_h handle here. */
110   args = Py_BuildValue ("(Kis#O)",
111                         (unsigned PY_LONG_LONG) event, event_handle,
112                         buf, buf_len, py_array);
113
114   if (PyEval_ThreadsInitialized ())
115     py_save = PyGILState_Ensure ();
116
117   py_r = PyEval_CallObject (py_callback, args);
118
119   if (PyEval_ThreadsInitialized ())
120     PyGILState_Release (py_save);
121
122   Py_DECREF (args);
123
124   if (py_r != NULL)
125     Py_DECREF (py_r);
126   else
127     /* Callback threw an exception: print it. */
128     PyErr_PrintEx (0);
129 }
130
131 PyObject *
132 py_guestfs_set_event_callback (PyObject *self, PyObject *args)
133 {
134   PyObject *py_g;
135   guestfs_h *g;
136   PyObject *py_callback;
137   unsigned PY_LONG_LONG events;
138   int eh;
139   PyObject *py_eh;
140   char key[64];
141
142   if (!PyArg_ParseTuple (args, (char *) "OOK:guestfs_set_event_callback",
143                          &py_g, &py_callback, &events))
144     return NULL;
145
146   if (!PyCallable_Check (py_callback)) {
147     PyErr_SetString (PyExc_TypeError,
148                      "callback parameter is not callable "
149                      "(eg. lambda or function)");
150     return NULL;
151   }
152
153   g = get_handle (py_g);
154
155   eh = guestfs_set_event_callback (g, py_guestfs_event_callback_wrapper,
156                                    events, 0, py_callback);
157   if (eh == -1) {
158     PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));
159     return NULL;
160   }
161
162   /* Increase the refcount for this callback since we are storing it
163    * in the opaque C libguestfs handle.  We need to remember that we
164    * did this, so we can decrease the refcount for all undeleted
165    * callbacks left around at close time (see py_guestfs_close).
166    */
167   Py_XINCREF (py_callback);
168
169   snprintf (key, sizeof key, "_python_event_%d", eh);
170   guestfs_set_private (g, key, py_callback);
171
172   py_eh = PyLong_FromLong ((long) eh);
173   return py_eh;
174 }
175
176 PyObject *
177 py_guestfs_delete_event_callback (PyObject *self, PyObject *args)
178 {
179   PyObject *py_g;
180   guestfs_h *g;
181   int eh;
182   PyObject *py_callback;
183   char key[64];
184
185   if (!PyArg_ParseTuple (args, (char *) "Oi:guestfs_delete_event_callback",
186                          &py_g, &eh))
187     return NULL;
188   g = get_handle (py_g);
189
190   snprintf (key, sizeof key, "_python_event_%d", eh);
191   py_callback = guestfs_get_private (g, key);
192   if (py_callback) {
193     Py_XDECREF (py_callback);
194     guestfs_set_private (g, key, NULL);
195     guestfs_delete_event_callback (g, eh);
196   }
197
198   Py_INCREF (Py_None);
199   return Py_None;
200 }
201
202 static PyObject **
203 get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
204 {
205   PyObject **r;
206   size_t i;
207   const char *key;
208   PyObject *cb;
209
210   /* Count the length of the array that will be needed. */
211   *len_rtn = 0;
212   cb = guestfs_first_private (g, &key);
213   while (cb != NULL) {
214     if (strncmp (key, "_python_event_", strlen ("_python_event_")) == 0)
215       (*len_rtn)++;
216     cb = guestfs_next_private (g, &key);
217   }
218
219   /* Copy them into the return array. */
220   r = guestfs_safe_malloc (g, sizeof (PyObject *) * (*len_rtn));
221
222   i = 0;
223   cb = guestfs_first_private (g, &key);
224   while (cb != NULL) {
225     if (strncmp (key, "_python_event_", strlen ("_python_event_")) == 0) {
226       r[i] = cb;
227       i++;
228     }
229     cb = guestfs_next_private (g, &key);
230   }
231
232   return r;
233 }