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 */
30 #include "ignore-value.h"
33 #include "guestfs-internal.h"
36 guestfs_set_event_callback (guestfs_h *g,
37 guestfs_event_callback cb,
38 uint64_t event_bitmask,
43 error (g, "flags parameter should be passed as 0 to this function");
47 /* We cast size_t to int which is not always safe for large numbers,
48 * and in any case if a program is registering a huge number of
49 * callbacks then we'd want to look at using an alternate data
50 * structure in place of a linear list.
52 if (g->nr_events >= 1000) {
53 error (g, "too many event callbacks registered");
57 int event_handle = (int) g->nr_events;
59 guestfs_safe_realloc (g, g->events,
60 (g->nr_events+1) * sizeof (struct event));
63 g->events[event_handle].event_bitmask = event_bitmask;
64 g->events[event_handle].cb = cb;
65 g->events[event_handle].opaque = opaque;
66 g->events[event_handle].opaque2 = NULL;
72 guestfs_delete_event_callback (guestfs_h *g, int event_handle)
74 if (event_handle < 0 || event_handle >= (int) g->nr_events)
77 /* Set the event_bitmask to 0, which will ensure that this callback
78 * cannot match any event and therefore cannot be called.
80 g->events[event_handle].event_bitmask = 0;
83 /* Functions to generate an event with various payloads. */
86 guestfs___call_callbacks_void (guestfs_h *g, uint64_t event)
90 for (i = 0; i < g->nr_events; ++i)
91 if ((g->events[i].event_bitmask & event) != 0)
92 g->events[i].cb (g, g->events[i].opaque, event, i, 0, NULL, 0, NULL, 0);
94 /* All events with payload type void are discarded if no callback
100 guestfs___call_callbacks_message (guestfs_h *g, uint64_t event,
101 const char *buf, size_t buf_len)
105 for (i = 0; i < g->nr_events; ++i)
106 if ((g->events[i].event_bitmask & event) != 0) {
107 g->events[i].cb (g, g->events[i].opaque, event, i, 0,
108 buf, buf_len, NULL, 0);
112 /* If nothing was registered and we're verbose or tracing, then we
113 * print the message on stderr. This essentially emulates the
114 * behaviour of the old-style handlers, while allowing callers to
115 * override print-on-stderr simply by registering a callback.
117 if (count == 0 && (g->verbose || event == GUESTFS_EVENT_TRACE)) {
118 const char *prefix = "libguestfs: ";
119 const char *trace = "trace: ";
120 const char *nl = "\n";
122 /* APPLIANCE => <buf>
123 * LIBRARY => libguestfs: <buf>\n
124 * TRACE => libguestfs: trace: <buf>\n (RHBZ#673479)
127 if (event != GUESTFS_EVENT_APPLIANCE)
128 ignore_value (write (STDERR_FILENO, prefix, strlen (prefix)));
130 if (event == GUESTFS_EVENT_TRACE)
131 ignore_value (write (STDERR_FILENO, trace, strlen (trace)));
133 ignore_value (write (STDERR_FILENO, buf, buf_len));
135 /* Messages from the appliance already contain \n characters, others
136 * need this to be appended.
138 if (event != GUESTFS_EVENT_APPLIANCE)
139 ignore_value (write (STDERR_FILENO, nl, strlen (nl)));
144 guestfs___call_callbacks_array (guestfs_h *g, uint64_t event,
145 const uint64_t *array, size_t array_len)
149 for (i = 0; i < g->nr_events; ++i)
150 if ((g->events[i].event_bitmask & event) != 0)
151 g->events[i].cb (g, g->events[i].opaque, event, i, 0,
152 NULL, 0, array, array_len);
154 /* All events with payload type array are discarded if no callback
159 /* Emulate old-style callback API.
161 * There were no event handles, so multiple callbacks per event were
162 * not supported. Calling the same 'guestfs_set_*_callback' function
163 * would replace the existing event. Calling it with cb == NULL meant
164 * that the caller wanted to remove the callback.
168 replace_old_style_event_callback (guestfs_h *g,
169 guestfs_event_callback cb,
170 uint64_t event_bitmask,
176 /* Use 'cb' pointer as a sentinel to replace the existing callback
177 * for this event if one was registered previously. Else append a
181 for (i = 0; i < g->nr_events; ++i)
182 if (g->events[i].cb == cb) {
183 if (opaque2 == NULL) {
184 /* opaque2 (the original callback) is NULL, which in the
185 * old-style API meant remove the callback.
187 guestfs_delete_event_callback (g, i);
195 return; /* see above */
197 /* i == g->nr_events */
199 guestfs_safe_realloc (g, g->events,
200 (g->nr_events+1) * sizeof (struct event));
204 g->events[i].event_bitmask = event_bitmask;
205 g->events[i].cb = cb;
206 g->events[i].opaque = opaque;
207 g->events[i].opaque2 = opaque2;
211 log_message_callback_wrapper (guestfs_h *g,
216 const char *buf, size_t buf_len,
217 const uint64_t *array, size_t array_len)
219 guestfs_log_message_cb cb = g->events[event_handle].opaque2;
220 /* Note that the old callback declared the message buffer as
221 * (char *, int). I sure hope message buffers aren't too large
222 * and that callers aren't writing to them. XXX
224 cb (g, opaque, (char *) buf, (int) buf_len);
228 guestfs_set_log_message_callback (guestfs_h *g,
229 guestfs_log_message_cb cb, void *opaque)
231 replace_old_style_event_callback (g, log_message_callback_wrapper,
232 GUESTFS_EVENT_APPLIANCE,
237 subprocess_quit_callback_wrapper (guestfs_h *g,
242 const char *buf, size_t buf_len,
243 const uint64_t *array, size_t array_len)
245 guestfs_subprocess_quit_cb cb = g->events[event_handle].opaque2;
250 guestfs_set_subprocess_quit_callback (guestfs_h *g,
251 guestfs_subprocess_quit_cb cb, void *opaque)
253 replace_old_style_event_callback (g, subprocess_quit_callback_wrapper,
254 GUESTFS_EVENT_SUBPROCESS_QUIT,
259 launch_done_callback_wrapper (guestfs_h *g,
264 const char *buf, size_t buf_len,
265 const uint64_t *array, size_t array_len)
267 guestfs_launch_done_cb cb = g->events[event_handle].opaque2;
272 guestfs_set_launch_done_callback (guestfs_h *g,
273 guestfs_launch_done_cb cb, void *opaque)
275 replace_old_style_event_callback (g, launch_done_callback_wrapper,
276 GUESTFS_EVENT_LAUNCH_DONE,
281 close_callback_wrapper (guestfs_h *g,
286 const char *buf, size_t buf_len,
287 const uint64_t *array, size_t array_len)
289 guestfs_close_cb cb = g->events[event_handle].opaque2;
294 guestfs_set_close_callback (guestfs_h *g,
295 guestfs_close_cb cb, void *opaque)
297 replace_old_style_event_callback (g, close_callback_wrapper,
303 progress_callback_wrapper (guestfs_h *g,
308 const char *buf, size_t buf_len,
309 const uint64_t *array, size_t array_len)
311 guestfs_progress_cb cb = g->events[event_handle].opaque2;
312 assert (array_len >= 4);
313 cb (g, opaque, (int) array[0], (int) array[1], array[2], array[3]);
317 guestfs_set_progress_callback (guestfs_h *g,
318 guestfs_progress_cb cb, void *opaque)
320 replace_old_style_event_callback (g, progress_callback_wrapper,
321 GUESTFS_EVENT_PROGRESS,