aa30fd2e4ee67f74657d89a997e222e8d1302b31
[libguestfs.git] / src / events.c
1 /* libguestfs
2  * Copyright (C) 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 #include <config.h>
20
21 #define _BSD_SOURCE /* for mkdtemp, usleep */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <inttypes.h>
27 #include <unistd.h>
28 #include <assert.h>
29 #include <string.h>
30
31 #include "c-ctype.h"
32 #include "ignore-value.h"
33
34 #include "guestfs.h"
35 #include "guestfs-internal.h"
36
37 int
38 guestfs_set_event_callback (guestfs_h *g,
39                             guestfs_event_callback cb,
40                             uint64_t event_bitmask,
41                             int flags,
42                             void *opaque)
43 {
44   if (flags != 0) {
45     error (g, "flags parameter should be passed as 0 to this function");
46     return -1;
47   }
48
49   /* We cast size_t to int which is not always safe for large numbers,
50    * and in any case if a program is registering a huge number of
51    * callbacks then we'd want to look at using an alternate data
52    * structure in place of a linear list.
53    */
54   if (g->nr_events >= 1000) {
55     error (g, "too many event callbacks registered");
56     return -1;
57   }
58
59   int event_handle = (int) g->nr_events;
60   g->events =
61     guestfs_safe_realloc (g, g->events,
62                           (g->nr_events+1) * sizeof (struct event));
63   g->nr_events++;
64
65   g->events[event_handle].event_bitmask = event_bitmask;
66   g->events[event_handle].cb = cb;
67   g->events[event_handle].opaque = opaque;
68   g->events[event_handle].opaque2 = NULL;
69
70   return event_handle;
71 }
72
73 void
74 guestfs_delete_event_callback (guestfs_h *g, int event_handle)
75 {
76   if (event_handle < 0 || event_handle >= (int) g->nr_events)
77     return;
78
79   /* Set the event_bitmask to 0, which will ensure that this callback
80    * cannot match any event and therefore cannot be called.
81    */
82   g->events[event_handle].event_bitmask = 0;
83 }
84
85 /* Functions to generate an event with various payloads. */
86
87 void
88 guestfs___call_callbacks_void (guestfs_h *g, uint64_t event)
89 {
90   size_t i;
91
92   for (i = 0; i < g->nr_events; ++i)
93     if ((g->events[i].event_bitmask & event) != 0)
94       g->events[i].cb (g, g->events[i].opaque, event, i, 0, NULL, 0, NULL, 0);
95
96   /* All events with payload type void are discarded if no callback
97    * was registered.
98    */
99 }
100
101 void
102 guestfs___call_callbacks_message (guestfs_h *g, uint64_t event,
103                                   const char *buf, size_t buf_len)
104 {
105   size_t i, count = 0;
106
107   for (i = 0; i < g->nr_events; ++i)
108     if ((g->events[i].event_bitmask & event) != 0) {
109       g->events[i].cb (g, g->events[i].opaque, event, i, 0,
110                        buf, buf_len, NULL, 0);
111       count++;
112     }
113
114   /* If nothing was registered and we're verbose or tracing, then we
115    * print the message on stderr.  This essentially emulates the
116    * behaviour of the old-style handlers, while allowing callers to
117    * override print-on-stderr simply by registering a callback.
118    */
119   if (count == 0 && (g->verbose || event == GUESTFS_EVENT_TRACE)) {
120     int from_appliance = event == GUESTFS_EVENT_APPLIANCE;
121     size_t i;
122     char c;
123
124     /* APPLIANCE =>  <buf>
125      * LIBRARY =>    libguestfs: <buf>\n
126      * TRACE =>      libguestfs: trace: <buf>\n  (RHBZ#673479)
127      */
128
129     if (event != GUESTFS_EVENT_APPLIANCE)
130       fputs ("libguestfs: ", stderr);
131
132     if (event == GUESTFS_EVENT_TRACE)
133       fputs ("trace: ", stderr);
134
135     /* Special or non-printing characters in the buffer must be
136      * escaped (RHBZ#731744).  The buffer can contain any 8 bit
137      * character, even \0.
138      *
139      * Handling of \n and \r characters is complex:
140      *
141      * Case 1: Messages from the appliance: These messages already
142      * contain \n and \r characters at logical positions, so we just
143      * echo those out directly.
144      *
145      * Case 2: Messages from other sources: These messages should NOT
146      * contain \n or \r.  If they do, it is escaped.  However we also
147      * need to print a real end of line after these messages.
148      */
149     for (i = 0; i < buf_len; ++i) {
150       c = buf[i];
151       if (c_isprint (c) || (from_appliance && (c == '\n' || c == '\r')))
152         putc (c, stderr);
153       else {
154         switch (c) {
155         case '\0': fputs ("\\0", stderr); break;
156         case '\a': fputs ("\\a", stderr); break;
157         case '\b': fputs ("\\b", stderr); break;
158         case '\f': fputs ("\\f", stderr); break;
159         case '\n': fputs ("\\n", stderr); break;
160         case '\r': fputs ("\\r", stderr); break;
161         case '\t': fputs ("\\t", stderr); break;
162         case '\v': fputs ("\\v", stderr); break;
163         default:
164           fprintf (stderr, "\\x%x", (unsigned) c);
165         }
166       }
167     }
168
169     if (!from_appliance)
170       putc ('\n', stderr);
171   }
172 }
173
174 void
175 guestfs___call_callbacks_array (guestfs_h *g, uint64_t event,
176                                 const uint64_t *array, size_t array_len)
177 {
178   size_t i;
179
180   for (i = 0; i < g->nr_events; ++i)
181     if ((g->events[i].event_bitmask & event) != 0)
182       g->events[i].cb (g, g->events[i].opaque, event, i, 0,
183                        NULL, 0, array, array_len);
184
185   /* All events with payload type array are discarded if no callback
186    * was registered.
187    */
188 }
189
190 /* Emulate old-style callback API.
191  *
192  * There were no event handles, so multiple callbacks per event were
193  * not supported.  Calling the same 'guestfs_set_*_callback' function
194  * would replace the existing event.  Calling it with cb == NULL meant
195  * that the caller wanted to remove the callback.
196  */
197
198 static void
199 replace_old_style_event_callback (guestfs_h *g,
200                                   guestfs_event_callback cb,
201                                   uint64_t event_bitmask,
202                                   void *opaque,
203                                   void *opaque2)
204 {
205   size_t i;
206
207   /* Use 'cb' pointer as a sentinel to replace the existing callback
208    * for this event if one was registered previously.  Else append a
209    * new event.
210    */
211
212   for (i = 0; i < g->nr_events; ++i)
213     if (g->events[i].cb == cb) {
214       if (opaque2 == NULL) {
215         /* opaque2 (the original callback) is NULL, which in the
216          * old-style API meant remove the callback.
217          */
218         guestfs_delete_event_callback (g, i);
219         return;
220       }
221
222       goto replace;
223     }
224
225   if (opaque2 == NULL)
226     return; /* see above */
227
228   /* i == g->nr_events */
229   g->events =
230     guestfs_safe_realloc (g, g->events,
231                           (g->nr_events+1) * sizeof (struct event));
232   g->nr_events++;
233
234  replace:
235   g->events[i].event_bitmask = event_bitmask;
236   g->events[i].cb = cb;
237   g->events[i].opaque = opaque;
238   g->events[i].opaque2 = opaque2;
239 }
240
241 static void
242 log_message_callback_wrapper (guestfs_h *g,
243                               void *opaque,
244                               uint64_t event,
245                               int event_handle,
246                               int flags,
247                               const char *buf, size_t buf_len,
248                               const uint64_t *array, size_t array_len)
249 {
250   guestfs_log_message_cb cb = g->events[event_handle].opaque2;
251   /* Note that the old callback declared the message buffer as
252    * (char *, int).  I sure hope message buffers aren't too large
253    * and that callers aren't writing to them. XXX
254    */
255   cb (g, opaque, (char *) buf, (int) buf_len);
256 }
257
258 void
259 guestfs_set_log_message_callback (guestfs_h *g,
260                                   guestfs_log_message_cb cb, void *opaque)
261 {
262   replace_old_style_event_callback (g, log_message_callback_wrapper,
263                                     GUESTFS_EVENT_APPLIANCE,
264                                     opaque, cb);
265 }
266
267 static void
268 subprocess_quit_callback_wrapper (guestfs_h *g,
269                                   void *opaque,
270                                   uint64_t event,
271                                   int event_handle,
272                                   int flags,
273                                   const char *buf, size_t buf_len,
274                                   const uint64_t *array, size_t array_len)
275 {
276   guestfs_subprocess_quit_cb cb = g->events[event_handle].opaque2;
277   cb (g, opaque);
278 }
279
280 void
281 guestfs_set_subprocess_quit_callback (guestfs_h *g,
282                                       guestfs_subprocess_quit_cb cb, void *opaque)
283 {
284   replace_old_style_event_callback (g, subprocess_quit_callback_wrapper,
285                                     GUESTFS_EVENT_SUBPROCESS_QUIT,
286                                     opaque, cb);
287 }
288
289 static void
290 launch_done_callback_wrapper (guestfs_h *g,
291                               void *opaque,
292                               uint64_t event,
293                               int event_handle,
294                               int flags,
295                               const char *buf, size_t buf_len,
296                               const uint64_t *array, size_t array_len)
297 {
298   guestfs_launch_done_cb cb = g->events[event_handle].opaque2;
299   cb (g, opaque);
300 }
301
302 void
303 guestfs_set_launch_done_callback (guestfs_h *g,
304                                   guestfs_launch_done_cb cb, void *opaque)
305 {
306   replace_old_style_event_callback (g, launch_done_callback_wrapper,
307                                     GUESTFS_EVENT_LAUNCH_DONE,
308                                     opaque, cb);
309 }
310
311 static void
312 close_callback_wrapper (guestfs_h *g,
313                         void *opaque,
314                         uint64_t event,
315                         int event_handle,
316                         int flags,
317                         const char *buf, size_t buf_len,
318                         const uint64_t *array, size_t array_len)
319 {
320   guestfs_close_cb cb = g->events[event_handle].opaque2;
321   cb (g, opaque);
322 }
323
324 void
325 guestfs_set_close_callback (guestfs_h *g,
326                             guestfs_close_cb cb, void *opaque)
327 {
328   replace_old_style_event_callback (g, close_callback_wrapper,
329                                     GUESTFS_EVENT_CLOSE,
330                                     opaque, cb);
331 }
332
333 static void
334 progress_callback_wrapper (guestfs_h *g,
335                            void *opaque,
336                            uint64_t event,
337                            int event_handle,
338                            int flags,
339                            const char *buf, size_t buf_len,
340                            const uint64_t *array, size_t array_len)
341 {
342   guestfs_progress_cb cb = g->events[event_handle].opaque2;
343   assert (array_len >= 4);
344   cb (g, opaque, (int) array[0], (int) array[1], array[2], array[3]);
345 }
346
347 void
348 guestfs_set_progress_callback (guestfs_h *g,
349                                guestfs_progress_cb cb, void *opaque)
350 {
351   replace_old_style_event_callback (g, progress_callback_wrapper,
352                                     GUESTFS_EVENT_PROGRESS,
353                                     opaque, cb);
354 }