2 * Copyright (C) 2011 Red Hat Inc.
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.
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.
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
21 #define _BSD_SOURCE /* for mkdtemp, usleep */
32 #include "ignore-value.h"
35 #include "guestfs-internal.h"
38 guestfs_set_event_callback (guestfs_h *g,
39 guestfs_event_callback cb,
40 uint64_t event_bitmask,
45 error (g, "flags parameter should be passed as 0 to this function");
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.
54 if (g->nr_events >= 1000) {
55 error (g, "too many event callbacks registered");
59 int event_handle = (int) g->nr_events;
61 guestfs_safe_realloc (g, g->events,
62 (g->nr_events+1) * sizeof (struct event));
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;
74 guestfs_delete_event_callback (guestfs_h *g, int event_handle)
76 if (event_handle < 0 || event_handle >= (int) g->nr_events)
79 /* Set the event_bitmask to 0, which will ensure that this callback
80 * cannot match any event and therefore cannot be called.
82 g->events[event_handle].event_bitmask = 0;
85 /* Functions to generate an event with various payloads. */
88 guestfs___call_callbacks_void (guestfs_h *g, uint64_t event)
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);
96 /* All events with payload type void are discarded if no callback
102 guestfs___call_callbacks_message (guestfs_h *g, uint64_t event,
103 const char *buf, size_t buf_len)
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);
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.
119 if (count == 0 && (g->verbose || event == GUESTFS_EVENT_TRACE)) {
120 int from_appliance = event == GUESTFS_EVENT_APPLIANCE;
124 /* APPLIANCE => <buf>
125 * LIBRARY => libguestfs: <buf>\n
126 * TRACE => libguestfs: trace: <buf>\n (RHBZ#673479)
129 if (event != GUESTFS_EVENT_APPLIANCE)
130 fputs ("libguestfs: ", stderr);
132 if (event == GUESTFS_EVENT_TRACE)
133 fputs ("trace: ", stderr);
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.
139 * Handling of \n and \r characters is complex:
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.
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.
149 for (i = 0; i < buf_len; ++i) {
151 if (c_isprint (c) || (from_appliance && (c == '\n' || c == '\r')))
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;
164 fprintf (stderr, "\\x%x", (unsigned) c);
175 guestfs___call_callbacks_array (guestfs_h *g, uint64_t event,
176 const uint64_t *array, size_t array_len)
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);
185 /* All events with payload type array are discarded if no callback
190 /* Emulate old-style callback API.
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.
199 replace_old_style_event_callback (guestfs_h *g,
200 guestfs_event_callback cb,
201 uint64_t event_bitmask,
207 /* Use 'cb' pointer as a sentinel to replace the existing callback
208 * for this event if one was registered previously. Else append a
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.
218 guestfs_delete_event_callback (g, i);
226 return; /* see above */
228 /* i == g->nr_events */
230 guestfs_safe_realloc (g, g->events,
231 (g->nr_events+1) * sizeof (struct event));
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;
242 log_message_callback_wrapper (guestfs_h *g,
247 const char *buf, size_t buf_len,
248 const uint64_t *array, size_t array_len)
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
255 cb (g, opaque, (char *) buf, (int) buf_len);
259 guestfs_set_log_message_callback (guestfs_h *g,
260 guestfs_log_message_cb cb, void *opaque)
262 replace_old_style_event_callback (g, log_message_callback_wrapper,
263 GUESTFS_EVENT_APPLIANCE,
268 subprocess_quit_callback_wrapper (guestfs_h *g,
273 const char *buf, size_t buf_len,
274 const uint64_t *array, size_t array_len)
276 guestfs_subprocess_quit_cb cb = g->events[event_handle].opaque2;
281 guestfs_set_subprocess_quit_callback (guestfs_h *g,
282 guestfs_subprocess_quit_cb cb, void *opaque)
284 replace_old_style_event_callback (g, subprocess_quit_callback_wrapper,
285 GUESTFS_EVENT_SUBPROCESS_QUIT,
290 launch_done_callback_wrapper (guestfs_h *g,
295 const char *buf, size_t buf_len,
296 const uint64_t *array, size_t array_len)
298 guestfs_launch_done_cb cb = g->events[event_handle].opaque2;
303 guestfs_set_launch_done_callback (guestfs_h *g,
304 guestfs_launch_done_cb cb, void *opaque)
306 replace_old_style_event_callback (g, launch_done_callback_wrapper,
307 GUESTFS_EVENT_LAUNCH_DONE,
312 close_callback_wrapper (guestfs_h *g,
317 const char *buf, size_t buf_len,
318 const uint64_t *array, size_t array_len)
320 guestfs_close_cb cb = g->events[event_handle].opaque2;
325 guestfs_set_close_callback (guestfs_h *g,
326 guestfs_close_cb cb, void *opaque)
328 replace_old_style_event_callback (g, close_callback_wrapper,
334 progress_callback_wrapper (guestfs_h *g,
339 const char *buf, size_t buf_len,
340 const uint64_t *array, size_t array_len)
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]);
348 guestfs_set_progress_callback (guestfs_h *g,
349 guestfs_progress_cb cb, void *opaque)
351 replace_old_style_event_callback (g, progress_callback_wrapper,
352 GUESTFS_EVENT_PROGRESS,