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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
|
/*
* libvirt-gobject-stream.c: libvirt glib integration
*
* Copyright (C) 2008 Daniel P. Berrange
* Copyright (C) 2010-2011 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.1 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, see
* <http://www.gnu.org/licenses/>.
*
* Authors: Daniel P. Berrange <berrange@redhat.com>
* Marc-André Lureau <marcandre.lureau@redhat.com>
*/
#include <config.h>
#include <libvirt/virterror.h>
#include <string.h>
#include <glib/gi18n-lib.h>
#include "libvirt-glib/libvirt-glib.h"
#include "libvirt-gobject/libvirt-gobject.h"
#include "libvirt-gobject-compat.h"
#include "libvirt-gobject/libvirt-gobject-input-stream.h"
#include "libvirt-gobject/libvirt-gobject-output-stream.h"
#define GVIR_STREAM_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE((obj), GVIR_TYPE_STREAM, GVirStreamPrivate))
struct _GVirStreamPrivate
{
virStreamPtr handle;
GInputStream *input_stream;
GOutputStream *output_stream;
gboolean eventRegistered;
int eventLast;
GList *sources;
};
typedef struct {
GSource source;
GVirStreamIOCondition cond;
GVirStreamIOCondition newCond;
GVirStream *stream;
} GVirStreamSource;
G_DEFINE_TYPE(GVirStream, gvir_stream, G_TYPE_IO_STREAM);
enum {
PROP_0,
PROP_HANDLE,
};
#define GVIR_STREAM_ERROR gvir_stream_error_quark()
static void gvir_stream_update_events(GVirStream *stream);
static GQuark
gvir_stream_error_quark(void)
{
return g_quark_from_static_string("gvir-stream");
}
static GInputStream* gvir_stream_get_input_stream(GIOStream *io_stream)
{
GVirStream *self = GVIR_STREAM(io_stream);
if (self->priv->input_stream == NULL)
self->priv->input_stream = (GInputStream *)_gvir_input_stream_new(self);
return self->priv->input_stream;
}
static GOutputStream* gvir_stream_get_output_stream(GIOStream *io_stream)
{
GVirStream *self = GVIR_STREAM(io_stream);
if (self->priv->output_stream == NULL)
self->priv->output_stream = (GOutputStream *)_gvir_output_stream_new(self);
return self->priv->output_stream;
}
static gboolean gvir_stream_close(GIOStream *io_stream,
GCancellable *cancellable,
GError **error)
{
GVirStream *self = GVIR_STREAM(io_stream);
GError *local_error = NULL;
gboolean i_ret = TRUE, o_ret = TRUE;
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
if (self->priv->input_stream)
i_ret = g_input_stream_close(self->priv->input_stream, cancellable, &local_error);
if (local_error)
g_propagate_error(error, local_error);
if (self->priv->output_stream)
o_ret = g_output_stream_close(self->priv->output_stream, cancellable, &local_error);
if (local_error) {
if (i_ret)
g_propagate_error(error, local_error);
else
g_error_free(local_error);
}
return (i_ret && o_ret);
}
static void gvir_stream_close_async(GIOStream *stream,
int io_priority G_GNUC_UNUSED,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *res;
GIOStreamClass *class;
GError *error;
class = G_IO_STREAM_GET_CLASS(stream);
/* close is not blocked, just do it! */
error = NULL;
if (class->close_fn &&
!class->close_fn(stream, cancellable, &error)) {
g_simple_async_report_take_gerror_in_idle(G_OBJECT (stream),
callback, user_data,
error);
return;
}
res = g_simple_async_result_new(G_OBJECT (stream),
callback,
user_data,
gvir_stream_close_async);
g_simple_async_result_complete_in_idle(res);
g_object_unref (res);
}
static gboolean
gvir_stream_close_finish(GIOStream *stream G_GNUC_UNUSED,
GAsyncResult *result G_GNUC_UNUSED,
GError **error G_GNUC_UNUSED)
{
return TRUE;
}
static void gvir_stream_get_property(GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GVirStream *self = GVIR_STREAM(object);
GVirStreamPrivate *priv = self->priv;
switch (prop_id) {
case PROP_HANDLE:
g_value_set_boxed(value, priv->handle);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}
static void gvir_stream_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GVirStream *self = GVIR_STREAM(object);
GVirStreamPrivate *priv = self->priv;
switch (prop_id) {
case PROP_HANDLE:
if (priv->handle)
virStreamFree(priv->handle);
priv->handle = g_value_get_boxed(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}
static void gvir_stream_finalize(GObject *object)
{
GVirStream *self = GVIR_STREAM(object);
GVirStreamPrivate *priv = self->priv;
GList *tmp;
g_debug("Finalize GVirStream=%p", self);
if (self->priv->input_stream)
g_object_unref(self->priv->input_stream);
tmp = priv->sources;
while (tmp) {
GVirStreamSource *source = tmp->data;
g_source_destroy((GSource*)source);
tmp = tmp->next;
}
g_list_free(priv->sources);
priv->sources = NULL;
if (priv->handle) {
gvir_stream_update_events(self);
if (virStreamFinish(priv->handle) < 0)
gvir_critical("cannot finish stream");
virStreamFree(priv->handle);
}
G_OBJECT_CLASS(gvir_stream_parent_class)->finalize(object);
}
static void gvir_stream_class_init(GVirStreamClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
GIOStreamClass *stream_class = G_IO_STREAM_CLASS(klass);
object_class->finalize = gvir_stream_finalize;
object_class->get_property = gvir_stream_get_property;
object_class->set_property = gvir_stream_set_property;
stream_class->get_input_stream = gvir_stream_get_input_stream;
stream_class->get_output_stream = gvir_stream_get_output_stream;
stream_class->close_fn = gvir_stream_close;
stream_class->close_async = gvir_stream_close_async;
stream_class->close_finish = gvir_stream_close_finish;
g_object_class_install_property(object_class,
PROP_HANDLE,
g_param_spec_boxed("handle",
"Handle",
"The stream handle",
GVIR_TYPE_STREAM_HANDLE,
G_PARAM_READABLE |
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_type_class_add_private(klass, sizeof(GVirStreamPrivate));
}
static void gvir_stream_init(GVirStream *self)
{
self->priv = GVIR_STREAM_GET_PRIVATE(self);
}
typedef struct virStream GVirStreamHandle;
static GVirStreamHandle*
gvir_stream_handle_copy(GVirStreamHandle *src)
{
virStreamRef((virStreamPtr)src);
return src;
}
static void
gvir_stream_handle_free(GVirStreamHandle *src)
{
virStreamFree((virStreamPtr)src);
}
G_DEFINE_BOXED_TYPE(GVirStreamHandle, gvir_stream_handle,
gvir_stream_handle_copy, gvir_stream_handle_free)
/**
* gvir_stream_receive:
* @stream: the stream
* @buffer: (array length=size) (element-type guint8): a buffer
* to read data into (which should be at least @size bytes long).
* @size: the number of bytes you want to read from the stream
* @cancellable: (allow-none): a %GCancellable or %NULL
* @error: #GError for error reporting, or %NULL to ignore.
*
* Receive data (up to @size bytes) from a stream.
* On error -1 is returned and @error is set accordingly.
*
* gvir_stream_receive() can return any number of bytes, up to
* @size. If more than @size bytes have been received, the additional
* data will be returned in future calls to gvir_stream_receive().
*
* If there is no data available, a %G_IO_ERROR_WOULD_BLOCK error will be
* returned.
*
* Returns: Number of bytes read, or 0 if the end of stream reached,
* or -1 on error.
*/
gssize gvir_stream_receive(GVirStream *self,
gchar *buffer,
gsize size,
GCancellable *cancellable,
GError **error)
{
int got;
g_return_val_if_fail(GVIR_IS_STREAM(self), -1);
g_return_val_if_fail(buffer != NULL, -1);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return -1;
got = virStreamRecv(self->priv->handle, buffer, size);
if (got == -2) { /* blocking */
g_set_error_literal(error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK,
_("virStreamRecv call would block"));
} else if (got < 0) {
g_set_error(error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
_("Got virStreamRecv error in %s"), G_STRFUNC);
}
return got;
}
struct stream_sink_helper {
GVirStream *self;
GVirStreamSinkFunc func;
gpointer user_data;
GCancellable *cancellable;
};
static int
stream_sink(virStreamPtr st G_GNUC_UNUSED,
const char *bytes,
size_t nbytes,
void *opaque)
{
struct stream_sink_helper *helper = opaque;
if (g_cancellable_is_cancelled(helper->cancellable))
return -1;
return helper->func(helper->self, bytes, nbytes, helper->user_data);
}
/**
* gvir_stream_receive_all:
* @stream: the stream
* @cancellable: cancellation notifier
* @func: (scope notified): the callback for writing data to application
* @user_data: (closure): data to be passed to @callback
* @error: #GError for error reporting, or %NULL to ignore.
*
* Receive the entire data stream, sending the data to the
* requested data sink. This is simply a convenient alternative
* to virStreamRecv, for apps that do blocking-I/o.
*
* Returns: the number of bytes consumed or -1 upon error
*/
gssize
gvir_stream_receive_all(GVirStream *self,
GCancellable *cancellable,
GVirStreamSinkFunc func,
gpointer user_data,
GError **error)
{
struct stream_sink_helper helper = {
.self = self,
.func = func,
.user_data = user_data,
.cancellable = cancellable,
};
int r;
g_return_val_if_fail(GVIR_IS_STREAM(self), -1);
g_return_val_if_fail((cancellable == NULL) || G_IS_CANCELLABLE(cancellable), -1);
g_return_val_if_fail(func != NULL, -1);
g_return_val_if_fail(error == NULL || *error == NULL, -1);
r = virStreamRecvAll(self->priv->handle, stream_sink, &helper);
if (r < 0) {
gvir_set_error_literal(error, GVIR_STREAM_ERROR,
0,
_("Unable to perform RecvAll"));
}
return r;
}
/**
* gvir_stream_send:
* @stream: the stream
* @buffer: a buffer to write data from (which should be at least @size
* bytes long).
* @size: the number of bytes you want to write to the stream
* @cancellable: (allow-none): a %GCancellable or %NULL
* @error: #GError for error reporting, or %NULL to ignore.
*
* Send data (up to @size bytes) from a stream.
* On error -1 is returned and @error is set accordingly.
*
* gvir_stream_send() can return any number of bytes, up to
* @size. If more than @size bytes have been sendd, the additional
* data will be returned in future calls to gvir_stream_send().
*
* If there is no data available, a %G_IO_ERROR_WOULD_BLOCK error will be
* returned.
*
* Returns: Number of bytes written.
*/
gssize gvir_stream_send(GVirStream *self,
const gchar *buffer,
gsize size,
GCancellable *cancellable,
GError **error)
{
int got;
g_return_val_if_fail(GVIR_IS_STREAM(self), -1);
g_return_val_if_fail(buffer != NULL, -1);
g_return_val_if_fail((cancellable == NULL) || G_IS_CANCELLABLE(cancellable), -1);
g_return_val_if_fail(error == NULL || *error == NULL, -1);
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return -1;
got = virStreamSend(self->priv->handle, buffer, size);
if (got == -2) { /* blocking */
g_set_error_literal(error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK,
_("virStreamSend call would block"));
} else if (got < 0) {
g_set_error(error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
_("Got virStreamRecv error in %s"), G_STRFUNC);
}
return got;
}
struct stream_source_helper {
GVirStream *self;
GVirStreamSourceFunc func;
gpointer user_data;
GCancellable *cancellable;
};
static int
stream_source(virStreamPtr st G_GNUC_UNUSED,
char *bytes,
size_t nbytes,
void *opaque)
{
struct stream_source_helper *helper = opaque;
if (g_cancellable_is_cancelled(helper->cancellable))
return -1;
return helper->func(helper->self, bytes, nbytes, helper->user_data);
}
/**
* gvir_stream_send_all:
* @stream: the stream
* @cancellable: cancellation notifier
* @func: (scope notified): the callback for writing data to application
* @user_data: (closure): data to be passed to @callback
* @error: #GError for error reporting, or %NULL to ignore.
*
* Send the entire data stream, sending the data to the
* requested data source. This is simply a convenient alternative
* to virStreamRecv, for apps that do blocking-I/o.
*
* Returns: the number of bytes consumed or -1 upon error
*/
gssize
gvir_stream_send_all(GVirStream *self,
GCancellable *cancellable,
GVirStreamSourceFunc func,
gpointer user_data,
GError **error)
{
struct stream_source_helper helper = {
.self = self,
.func = func,
.user_data = user_data,
.cancellable = cancellable,
};
int r;
g_return_val_if_fail(GVIR_IS_STREAM(self), -1);
g_return_val_if_fail((cancellable == NULL) || G_IS_CANCELLABLE(cancellable), -1);
g_return_val_if_fail(func != NULL, -1);
g_return_val_if_fail(error == NULL || *error == NULL, -1);
r = virStreamSendAll(self->priv->handle, stream_source, &helper);
if (r < 0) {
gvir_set_error_literal(error, GVIR_STREAM_ERROR,
0,
_("Unable to perform SendAll"));
}
return r;
}
static void gvir_stream_handle_events(virStreamPtr st G_GNUC_UNUSED,
int events,
void *opaque)
{
GVirStream *stream = GVIR_STREAM(opaque);
GVirStreamPrivate *priv = stream->priv;
GList *tmp = priv->sources;
while (tmp) {
GVirStreamSource *source = tmp->data;
source->newCond = 0;
if (source->cond & GVIR_STREAM_IO_CONDITION_READABLE) {
if (events & VIR_STREAM_EVENT_READABLE)
source->newCond |= GVIR_STREAM_IO_CONDITION_READABLE;
if (events & VIR_STREAM_EVENT_HANGUP)
source->newCond |= GVIR_STREAM_IO_CONDITION_HANGUP;
if (events & VIR_STREAM_EVENT_ERROR)
source->newCond |= GVIR_STREAM_IO_CONDITION_ERROR;
}
if (source->cond & GVIR_STREAM_IO_CONDITION_WRITABLE) {
if (events & VIR_STREAM_EVENT_WRITABLE)
source->newCond |= GVIR_STREAM_IO_CONDITION_WRITABLE;
if (events & VIR_STREAM_EVENT_HANGUP)
source->newCond |= GVIR_STREAM_IO_CONDITION_HANGUP;
if (events & VIR_STREAM_EVENT_ERROR)
source->newCond |= GVIR_STREAM_IO_CONDITION_ERROR;
}
tmp = tmp->next;
}
}
static void gvir_stream_update_events(GVirStream *stream)
{
GVirStreamPrivate *priv = stream->priv;
int mask = 0;
GList *tmp = priv->sources;
while (tmp) {
GVirStreamSource *source = tmp->data;
if (source->cond & GVIR_STREAM_IO_CONDITION_READABLE)
mask |= VIR_STREAM_EVENT_READABLE;
if (source->cond & GVIR_STREAM_IO_CONDITION_WRITABLE)
mask |= VIR_STREAM_EVENT_WRITABLE;
tmp = tmp->next;
}
if (mask) {
if (priv->eventRegistered) {
virStreamEventUpdateCallback(priv->handle, mask);
} else {
virStreamEventAddCallback(priv->handle, mask,
gvir_stream_handle_events,
stream,
NULL);
priv->eventRegistered = TRUE;
}
} else {
if (priv->eventRegistered) {
virStreamEventRemoveCallback(priv->handle);
priv->eventRegistered = FALSE;
}
}
}
static gboolean gvir_stream_source_prepare(GSource *source,
gint *timeout)
{
GVirStreamSource *gsource = (GVirStreamSource*)source;
if (gsource->newCond) {
*timeout = 0;
return TRUE;
}
*timeout = -1;
return FALSE;
}
static gboolean gvir_stream_source_check(GSource *source)
{
GVirStreamSource *gsource = (GVirStreamSource*)source;
if (gsource->newCond)
return TRUE;
return FALSE;
}
static gboolean gvir_stream_source_dispatch(GSource *source,
GSourceFunc callback,
gpointer user_data)
{
GVirStreamSource *gsource = (GVirStreamSource*)source;
GVirStreamIOFunc func = (GVirStreamIOFunc)callback;
gboolean ret;
ret = func(gsource->stream, gsource->newCond, user_data);
gsource->newCond = 0;
return ret;
}
static void gvir_stream_source_finalize(GSource *source)
{
GVirStreamSource *gsource = (GVirStreamSource*)source;
GVirStreamPrivate *priv = gsource->stream->priv;
priv->sources = g_list_remove(priv->sources, source);
gvir_stream_update_events(gsource->stream);
g_clear_object(&gsource->stream);
}
GSourceFuncs gvir_stream_source_funcs = {
.prepare = gvir_stream_source_prepare,
.check = gvir_stream_source_check,
.dispatch = gvir_stream_source_dispatch,
.finalize = gvir_stream_source_finalize,
};
/**
* gvir_stream_add_watch: (skip)
* @stream: the stream
* @cond: the conditions to watch for (bitfield of #GVirStreamIOCondition)
* @func: (closure opaque): the function to call when the condition is satisfied
* @opaque: (closure): user data to pass to @func
*
* Adds a watch for @stream to the mainloop
*
* Returns: the event source id
*/
guint gvir_stream_add_watch(GVirStream *stream,
GVirStreamIOCondition cond,
GVirStreamIOFunc func,
gpointer opaque)
{
return gvir_stream_add_watch_full(stream,
G_PRIORITY_DEFAULT,
cond,
func,
opaque,
NULL);
}
/**
* gvir_stream_add_watch_full:
* @stream: the stream
* @priority: the priority of the #GVirStream source
* @cond: the conditions to watch for (bitfield of #GVirStreamIOCondition)
* @func: (closure opaque): the function to call when the condition is satisfied
* @opaque: (closure): user data to pass to @func
* @notify: the function to call when the source is removed
*
* Adds a watch for @stream to the mainloop
*
* Returns: the event source id
* Rename to: gvir_stream_add_watch
*/
guint gvir_stream_add_watch_full(GVirStream *stream,
gint priority,
GVirStreamIOCondition cond,
GVirStreamIOFunc func,
gpointer opaque,
GDestroyNotify notify)
{
g_return_val_if_fail(GVIR_IS_STREAM(stream), 0);
GVirStreamPrivate *priv = stream->priv;
GVirStreamSource *source = (GVirStreamSource*)g_source_new(&gvir_stream_source_funcs,
sizeof(GVirStreamSource));
guint ret;
source->stream = g_object_ref(stream);
source->cond = cond;
if (priority != G_PRIORITY_DEFAULT)
g_source_set_priority((GSource*)source, priority);
priv->sources = g_list_append(priv->sources, source);
gvir_stream_update_events(source->stream);
g_source_set_callback((GSource*)source, (GSourceFunc)func, opaque, notify);
ret = g_source_attach((GSource*)source, g_main_context_default());
g_source_unref((GSource*)source);
return ret;
}
|