stream.c 22.4 KB
Newer Older
1 2 3
/*
 * stream.c: APIs for managing client streams
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2009, 2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */


#include <config.h>

#include "stream.h"
27
#include "remote.h"
28
#include "viralloc.h"
29
#include "virlog.h"
30
#include "virnetserverclient.h"
31
#include "virerror.h"
32 33

#define VIR_FROM_THIS VIR_FROM_STREAMS
34

35 36
struct daemonClientStream {
    daemonClientPrivatePtr priv;
37
    int refs;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

    virNetServerProgramPtr prog;

    virStreamPtr st;
    int procedure;
    int serial;

    unsigned int recvEOF : 1;
    unsigned int closed : 1;

    int filterID;

    virNetMessagePtr rx;
    int tx;

    daemonClientStreamPtr next;
};

56
static int
57 58
daemonStreamHandleWrite(virNetServerClientPtr client,
                        daemonClientStream *stream);
59
static int
60 61
daemonStreamHandleRead(virNetServerClientPtr client,
                       daemonClientStream *stream);
62
static int
63 64 65
daemonStreamHandleFinish(virNetServerClientPtr client,
                         daemonClientStream *stream,
                         virNetMessagePtr msg);
66
static int
67 68 69
daemonStreamHandleAbort(virNetServerClientPtr client,
                        daemonClientStream *stream,
                        virNetMessagePtr msg);
70 71 72 73



static void
74
daemonStreamUpdateEvents(daemonClientStream *stream)
75 76 77 78
{
    int newEvents = 0;
    if (stream->rx)
        newEvents |= VIR_STREAM_EVENT_WRITABLE;
79 80
    if (stream->tx && !stream->recvEOF)
        newEvents |= VIR_STREAM_EVENT_READABLE;
81 82 83 84

    virStreamEventUpdateCallback(stream->st, newEvents);
}

85 86 87 88 89 90 91 92
/*
 * Invoked when an outgoing data packet message has been fully sent.
 * This simply re-enables TX of further data.
 *
 * The idea is to stop the daemon growing without bound due to
 * fast stream, but slow client
 */
static void
93
daemonStreamMessageFinished(virNetMessagePtr msg ATTRIBUTE_UNUSED,
94 95 96 97 98 99 100 101
                            void *opaque)
{
    daemonClientStream *stream = opaque;
    VIR_DEBUG("stream=%p proc=%d serial=%d",
              stream, msg->header.proc, msg->header.serial);

    stream->tx = 1;
    daemonStreamUpdateEvents(stream);
102 103

    daemonFreeClientStream(NULL, stream);
104
}
105

106

107 108 109 110
/*
 * Callback that gets invoked when a stream becomes writable/readable
 */
static void
111
daemonStreamEvent(virStreamPtr st, int events, void *opaque)
112
{
113 114 115
    virNetServerClientPtr client = opaque;
    daemonClientStream *stream;
    daemonClientPrivatePtr priv = virNetServerClientGetPrivateData(client);
116

117
    virMutexLock(&priv->lock);
118

119 120 121 122 123 124
    stream = priv->streams;
    while (stream) {
        if (stream->st == st)
            break;
        stream = stream->next;
    }
125 126 127 128 129 130 131

    if (!stream) {
        VIR_WARN("event for client=%p stream st=%p, but missing stream state", client, st);
        virStreamEventRemoveCallback(st);
        goto cleanup;
    }

132
    VIR_DEBUG("st=%p events=%d EOF=%d closed=%d", st, events, stream->recvEOF, stream->closed);
133

D
Daniel P. Berrange 已提交
134 135
    if (!stream->closed &&
        (events & VIR_STREAM_EVENT_WRITABLE)) {
136 137 138
        if (daemonStreamHandleWrite(client, stream) < 0) {
            daemonRemoveClientStream(client, stream);
            virNetServerClientClose(client);
139 140 141 142
            goto cleanup;
        }
    }

D
Daniel P. Berrange 已提交
143 144 145
    if (!stream->closed && !stream->recvEOF &&
        (events & (VIR_STREAM_EVENT_READABLE))) {
        events = events & ~(VIR_STREAM_EVENT_READABLE);
146 147 148
        if (daemonStreamHandleRead(client, stream) < 0) {
            daemonRemoveClientStream(client, stream);
            virNetServerClientClose(client);
149 150
            goto cleanup;
        }
151 152 153 154 155 156 157 158
        /* If we detected EOF during read processing,
         * then clear hangup/error conditions, since
         * we want the client to see the EOF message
         * we just sent them
         */
        if (stream->recvEOF)
            events = events & ~(VIR_STREAM_EVENT_HANGUP |
                                VIR_STREAM_EVENT_ERROR);
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
    /* If we have a completion/abort message, always process it */
    if (stream->rx) {
        virNetMessagePtr msg = stream->rx;
        switch (msg->header.status) {
        case VIR_NET_CONTINUE:
            /* nada */
            break;
        case VIR_NET_OK:
            virNetMessageQueueServe(&stream->rx);
            if (daemonStreamHandleFinish(client, stream, msg) < 0) {
                virNetMessageFree(msg);
                daemonRemoveClientStream(client, stream);
                virNetServerClientClose(client);
                goto cleanup;
            }
            break;
        case VIR_NET_ERROR:
        default:
            virNetMessageQueueServe(&stream->rx);
            if (daemonStreamHandleAbort(client, stream, msg) < 0) {
                virNetMessageFree(msg);
                daemonRemoveClientStream(client, stream);
                virNetServerClientClose(client);
                goto cleanup;
            }
            break;
        }
    }

D
Daniel P. Berrange 已提交
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

    /* If we got HANGUP, we need to only send an empty
     * packet so the client sees an EOF and cleans up
     */
    if (!stream->closed && !stream->recvEOF &&
        (events & VIR_STREAM_EVENT_HANGUP)) {
        virNetMessagePtr msg;
        events &= ~(VIR_STREAM_EVENT_HANGUP);
        stream->tx = 0;
        stream->recvEOF = 1;
        if (!(msg = virNetMessageNew(false))) {
            daemonRemoveClientStream(client, stream);
            virNetServerClientClose(client);
            goto cleanup;
        }
        msg->cb = daemonStreamMessageFinished;
        msg->opaque = stream;
        stream->refs++;
        if (virNetServerProgramSendStreamData(remoteProgram,
                                              client,
                                              msg,
                                              stream->procedure,
                                              stream->serial,
                                              "", 0) < 0) {
            virNetMessageFree(msg);
            daemonRemoveClientStream(client, stream);
            virNetServerClientClose(client);
            goto cleanup;
        }
    }

221 222 223
    if (!stream->closed &&
        (events & (VIR_STREAM_EVENT_ERROR | VIR_STREAM_EVENT_HANGUP))) {
        int ret;
224 225 226 227
        virNetMessagePtr msg;
        virNetMessageError rerr;

        memset(&rerr, 0, sizeof(rerr));
228
        stream->closed = 1;
229
        virStreamEventRemoveCallback(stream->st);
230 231
        virStreamAbort(stream->st);
        if (events & VIR_STREAM_EVENT_HANGUP)
232 233
            virReportError(VIR_ERR_RPC,
                           "%s", _("stream had unexpected termination"));
234
        else
235 236
            virReportError(VIR_ERR_RPC,
                           "%s", _("stream had I/O failure"));
237

238
        msg = virNetMessageNew(false);
239 240 241 242 243 244 245 246 247 248 249
        if (!msg) {
            ret = -1;
        } else {
            ret = virNetServerProgramSendStreamError(remoteProgram,
                                                     client,
                                                     msg,
                                                     &rerr,
                                                     stream->procedure,
                                                     stream->serial);
        }
        daemonRemoveClientStream(client, stream);
250
        if (ret < 0)
251
            virNetServerClientClose(client);
252 253 254 255
        goto cleanup;
    }

    if (stream->closed) {
256
        daemonRemoveClientStream(client, stream);
257
    } else {
258
        daemonStreamUpdateEvents(stream);
259 260 261
    }

cleanup:
262
    virMutexUnlock(&priv->lock);
263 264
}

265 266 267 268 269 270 271 272 273 274

/*
 * @client: a locked client object
 *
 * Invoked by the main loop when filtering incoming messages.
 *
 * Returns 1 if the message was processed, 0 if skipped,
 * -1 on fatal client error
 */
static int
275
daemonStreamFilter(virNetServerClientPtr client ATTRIBUTE_UNUSED,
276 277
                   virNetMessagePtr msg,
                   void *opaque)
278
{
279 280
    daemonClientStream *stream = opaque;
    int ret = 0;
281

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    virMutexLock(&stream->priv->lock);

    if (msg->header.type != VIR_NET_STREAM)
        goto cleanup;

    if (!virNetServerProgramMatches(stream->prog, msg))
        goto cleanup;

    if (msg->header.proc != stream->procedure ||
        msg->header.serial != stream->serial)
        goto cleanup;

    VIR_DEBUG("Incoming client=%p, rx=%p, serial=%d, proc=%d, status=%d",
              client, stream->rx, msg->header.proc,
              msg->header.serial, msg->header.status);

    virNetMessageQueuePush(&stream->rx, msg);
    daemonStreamUpdateEvents(stream);
    ret = 1;

cleanup:
    virMutexUnlock(&stream->priv->lock);
    return ret;
305 306 307 308 309
}


/*
 * @conn: a connection object to associate the stream with
310
 * @header: the method call to associate with the stream
311 312 313 314 315
 *
 * Creates a new stream for this conn
 *
 * Returns a new stream object, or NULL upon OOM
 */
316 317 318 319 320
daemonClientStream *
daemonCreateClientStream(virNetServerClientPtr client,
                         virStreamPtr st,
                         virNetServerProgramPtr prog,
                         virNetMessageHeaderPtr header)
321
{
322 323
    daemonClientStream *stream;
    daemonClientPrivatePtr priv = virNetServerClientGetPrivateData(client);
324

325 326
    VIR_DEBUG("client=%p, proc=%d, serial=%d, st=%p",
              client, header->proc, header->serial, st);
327

328 329
    if (VIR_ALLOC(stream) < 0) {
        virReportOOMError();
330
        return NULL;
331
    }
332

333
    stream->refs = 1;
334
    stream->priv = priv;
335
    stream->prog = virObjectRef(prog);
336 337 338 339
    stream->procedure = header->proc;
    stream->serial = header->serial;
    stream->filterID = -1;
    stream->st = st;
340 341 342 343 344 345 346 347 348 349

    return stream;
}

/*
 * @stream: an unused client stream
 *
 * Frees the memory associated with this inactive client
 * stream
 */
350 351
int daemonFreeClientStream(virNetServerClientPtr client,
                           daemonClientStream *stream)
352
{
353 354
    virNetMessagePtr msg;
    int ret = 0;
355 356

    if (!stream)
357 358
        return 0;

359 360 361 362
    stream->refs--;
    if (stream->refs)
        return 0;

363 364
    VIR_DEBUG("client=%p, proc=%d, serial=%d",
              client, stream->procedure, stream->serial);
365

366
    virObjectUnref(stream->prog);
367 368 369

    msg = stream->rx;
    while (msg) {
370
        virNetMessagePtr tmp = msg->next;
371 372
        if (client) {
            /* Send a dummy reply to free up 'msg' & unblock client rx */
373
            virNetMessageClear(msg);
374 375 376 377 378 379 380
            msg->header.type = VIR_NET_REPLY;
            if (virNetServerClientSendMessage(client, msg) < 0) {
                virNetServerClientImmediateClose(client);
                virNetMessageFree(msg);
                ret = -1;
            }
        } else {
381 382
            virNetMessageFree(msg);
        }
383 384 385 386 387
        msg = tmp;
    }

    virStreamFree(stream->st);
    VIR_FREE(stream);
388 389

    return ret;
390 391 392 393 394 395 396
}


/*
 * @client: a locked client to add the stream to
 * @stream: a stream to add
 */
397 398 399
int daemonAddClientStream(virNetServerClientPtr client,
                          daemonClientStream *stream,
                          bool transmit)
400
{
401 402 403
    VIR_DEBUG("client=%p, proc=%d, serial=%d, st=%p, transmit=%d",
              client, stream->procedure, stream->serial, stream->st, transmit);
    daemonClientPrivatePtr priv = virNetServerClientGetPrivateData(client);
404

405 406 407 408
    if (stream->filterID != -1) {
        VIR_WARN("Filter already added to client %p", client);
        return -1;
    }
409

410
    if (virStreamEventAddCallback(stream->st, 0,
411
                                  daemonStreamEvent, client,
412
                                  virObjectFreeCallback) < 0)
413 414
        return -1;

415 416
    virObjectRef(client);

417 418 419 420 421
    if ((stream->filterID = virNetServerClientAddFilter(client,
                                                        daemonStreamFilter,
                                                        stream)) < 0) {
        virStreamEventRemoveCallback(stream->st);
        return -1;
422 423
    }

424 425
    if (transmit)
        stream->tx = 1;
426

427 428 429
    virMutexLock(&priv->lock);
    stream->next = priv->streams;
    priv->streams = stream;
430

431
    daemonStreamUpdateEvents(stream);
432

433
    virMutexUnlock(&priv->lock);
434

435
    return 0;
436 437 438 439 440 441 442 443 444 445 446 447
}


/*
 * @client: a locked client object
 * @stream: an inactive, closed stream object
 *
 * Removes a stream from the list of active streams for the client
 *
 * Returns 0 if the stream was removd, -1 if it doesn't exist
 */
int
448 449
daemonRemoveClientStream(virNetServerClientPtr client,
                         daemonClientStream *stream)
450
{
451 452 453 454 455 456 457 458 459 460
    VIR_DEBUG("client=%p, proc=%d, serial=%d, st=%p",
              client, stream->procedure, stream->serial, stream->st);
    daemonClientPrivatePtr priv = virNetServerClientGetPrivateData(client);
    daemonClientStream *curr = priv->streams;
    daemonClientStream *prev = NULL;

    if (stream->filterID != -1) {
        virNetServerClientRemoveFilter(client,
                                       stream->filterID);
        stream->filterID = -1;
461 462
    }

463 464
    if (!stream->closed) {
        virStreamEventRemoveCallback(stream->st);
465
        virStreamAbort(stream->st);
466
    }
467 468 469 470 471 472

    while (curr) {
        if (curr == stream) {
            if (prev)
                prev->next = curr->next;
            else
473 474
                priv->streams = curr->next;
            return daemonFreeClientStream(client, stream);
475 476 477 478 479 480
        }
        prev = curr;
        curr = curr->next;
    }
    return -1;
}
481 482


483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
void
daemonRemoveAllClientStreams(daemonClientStream *stream)
{
    daemonClientStream *tmp;

    VIR_DEBUG("stream=%p", stream);

    while (stream) {
        tmp = stream->next;

        if (!stream->closed) {
            virStreamEventRemoveCallback(stream->st);
            virStreamAbort(stream->st);
        }

        daemonFreeClientStream(NULL, stream);

        VIR_DEBUG("next stream=%p", tmp);
        stream = tmp;
    }
}

505 506 507 508 509 510 511
/*
 * Returns:
 *   -1  if fatal error occurred
 *    0  if message was fully processed
 *    1  if message is still being processed
 */
static int
512 513 514
daemonStreamHandleWriteData(virNetServerClientPtr client,
                            daemonClientStream *stream,
                            virNetMessagePtr msg)
515 516 517
{
    int ret;

518 519 520
    VIR_DEBUG("client=%p, stream=%p, proc=%d, serial=%d, len=%zu, offset=%zu",
              client, stream, msg->header.proc, msg->header.serial,
              msg->bufferLength, msg->bufferOffset);
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

    ret = virStreamSend(stream->st,
                        msg->buffer + msg->bufferOffset,
                        msg->bufferLength - msg->bufferOffset);

    if (ret > 0) {
        msg->bufferOffset += ret;

        /* Partial write, so indicate we have more todo later */
        if (msg->bufferOffset < msg->bufferLength)
            return 1;
    } else if (ret == -2) {
        /* Blocking, so indicate we have more todo later */
        return 1;
    } else {
536 537 538 539
        virNetMessageError rerr;

        memset(&rerr, 0, sizeof(rerr));

540
        VIR_INFO("Stream send failed");
541
        stream->closed = 1;
542 543 544 545 546
        return virNetServerProgramSendReplyError(stream->prog,
                                                 client,
                                                 msg,
                                                 &rerr,
                                                 &msg->header);
547 548 549 550 551 552 553
    }

    return 0;
}


/*
E
Eric Blake 已提交
554
 * Process a finish handshake from the client.
555
 *
556
 * Returns a VIR_NET_OK confirmation if successful, or a VIR_NET_ERROR
557 558 559 560 561
 * if there was a stream error
 *
 * Returns 0 if successfully sent RPC reply, -1 upon fatal error
 */
static int
562 563 564
daemonStreamHandleFinish(virNetServerClientPtr client,
                         daemonClientStream *stream,
                         virNetMessagePtr msg)
565 566 567
{
    int ret;

568 569
    VIR_DEBUG("client=%p, stream=%p, proc=%d, serial=%d",
              client, stream, msg->header.proc, msg->header.serial);
570 571

    stream->closed = 1;
572
    virStreamEventRemoveCallback(stream->st);
573 574 575
    ret = virStreamFinish(stream->st);

    if (ret < 0) {
576 577 578 579 580 581 582
        virNetMessageError rerr;
        memset(&rerr, 0, sizeof(rerr));
        return virNetServerProgramSendReplyError(stream->prog,
                                                 client,
                                                 msg,
                                                 &rerr,
                                                 &msg->header);
583 584
    } else {
        /* Send zero-length confirm */
585 586 587 588 589 590
        return virNetServerProgramSendStreamData(stream->prog,
                                                 client,
                                                 msg,
                                                 stream->procedure,
                                                 stream->serial,
                                                 NULL, 0);
591 592 593 594 595 596 597 598 599 600
    }
}


/*
 * Process an abort request from the client.
 *
 * Returns 0 if successfully aborted, -1 upon error
 */
static int
601 602 603
daemonStreamHandleAbort(virNetServerClientPtr client,
                        daemonClientStream *stream,
                        virNetMessagePtr msg)
604
{
605 606 607
    VIR_DEBUG("client=%p, stream=%p, proc=%d, serial=%d",
              client, stream, msg->header.proc, msg->header.serial);
    virNetMessageError rerr;
608

609
    memset(&rerr, 0, sizeof(rerr));
610 611

    stream->closed = 1;
612
    virStreamEventRemoveCallback(stream->st);
613 614
    virStreamAbort(stream->st);

615
    if (msg->header.status == VIR_NET_ERROR)
616 617
        virReportError(VIR_ERR_RPC,
                       "%s", _("stream aborted at client request"));
618
    else {
619
        VIR_WARN("unexpected stream status %d", msg->header.status);
620 621 622
        virReportError(VIR_ERR_RPC,
                       _("stream aborted with unexpected status %d"),
                       msg->header.status);
623 624
    }

625 626 627 628 629
    return virNetServerProgramSendReplyError(remoteProgram,
                                             client,
                                             msg,
                                             &rerr,
                                             &msg->header);
630 631 632 633 634 635 636 637 638 639 640 641
}



/*
 * Called when the stream is signalled has being able to accept
 * data writes. Will process all pending incoming messages
 * until they're all gone, or I/O blocks
 *
 * Returns 0 on success, or -1 upon fatal error
 */
static int
642 643
daemonStreamHandleWrite(virNetServerClientPtr client,
                        daemonClientStream *stream)
644
{
645
    VIR_DEBUG("client=%p, stream=%p", client, stream);
646

647 648
    while (stream->rx && !stream->closed) {
        virNetMessagePtr msg = stream->rx;
649
        int ret;
650 651 652 653

        switch (msg->header.status) {
        case VIR_NET_OK:
            ret = daemonStreamHandleFinish(client, stream, msg);
654 655
            break;

656 657
        case VIR_NET_CONTINUE:
            ret = daemonStreamHandleWriteData(client, stream, msg);
658 659
            break;

660
        case VIR_NET_ERROR:
661
        default:
662
            ret = daemonStreamHandleAbort(client, stream, msg);
663 664 665
            break;
        }

666 667
        if (ret > 0)
            break;  /* still processing data from msg */
668

669 670 671
        virNetMessageQueueServe(&stream->rx);
        if (ret < 0) {
            virNetMessageFree(msg);
672
            virNetServerClientImmediateClose(client);
673 674
            return -1;
        }
675 676 677 678 679 680 681 682

        /* 'CONTINUE' messages don't send a reply (unless error
         * occurred), so to release the 'msg' object we need to
         * send a fake zero-length reply. Nothing actually gets
         * onto the wire, but this causes the client to reset
         * its active request count / throttling
         */
        if (msg->header.status == VIR_NET_CONTINUE) {
683
            virNetMessageClear(msg);
684 685 686
            msg->header.type = VIR_NET_REPLY;
            if (virNetServerClientSendMessage(client, msg) < 0) {
                virNetMessageFree(msg);
687
                virNetServerClientImmediateClose(client);
688 689 690
                return -1;
            }
        }
691 692 693 694
    }

    return 0;
}
695 696 697 698 699



/*
 * Invoked when a stream is signalled as having data
J
Ján Tomko 已提交
700
 * available to read. This reads up to one message
701 702 703 704 705 706 707 708
 * worth of data, and then queues that for transmission
 * to the client.
 *
 * Returns 0 if data was queued for TX, or a error RPC
 * was sent, or -1 on fatal error, indicating client should
 * be killed
 */
static int
709 710
daemonStreamHandleRead(virNetServerClientPtr client,
                       daemonClientStream *stream)
711 712
{
    char *buffer;
713
    size_t bufferLen = VIR_NET_MESSAGE_PAYLOAD_MAX;
714 715
    int ret;

716 717 718 719 720 721 722 723 724
    VIR_DEBUG("client=%p, stream=%p tx=%d closed=%d",
              client, stream, stream->tx, stream->closed);

    /* We might have had an event pending before we shut
     * down the stream, so if we're marked as closed,
     * then do nothing
     */
    if (stream->closed)
        return 0;
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

    /* Shouldn't ever be called unless we're marked able to
     * transmit, but doesn't hurt to check */
    if (!stream->tx)
        return 0;

    if (VIR_ALLOC_N(buffer, bufferLen) < 0)
        return -1;

    ret = virStreamRecv(stream->st, buffer, bufferLen);
    if (ret == -2) {
        /* Should never get this, since we're only called when we know
         * we're readable, but hey things change... */
        ret = 0;
    } else if (ret < 0) {
740 741 742 743
        virNetMessagePtr msg;
        virNetMessageError rerr;

        memset(&rerr, 0, sizeof(rerr));
744

745
        if (!(msg = virNetMessageNew(false)))
746 747 748 749 750 751 752 753
            ret = -1;
        else
            ret = virNetServerProgramSendStreamError(remoteProgram,
                                                     client,
                                                     msg,
                                                     &rerr,
                                                     stream->procedure,
                                                     stream->serial);
754
    } else {
755
        virNetMessagePtr msg;
756 757 758
        stream->tx = 0;
        if (ret == 0)
            stream->recvEOF = 1;
759
        if (!(msg = virNetMessageNew(false)))
760 761 762 763 764
            ret = -1;

        if (msg) {
            msg->cb = daemonStreamMessageFinished;
            msg->opaque = stream;
765
            stream->refs++;
766 767 768 769 770 771 772
            ret = virNetServerProgramSendStreamData(remoteProgram,
                                                    client,
                                                    msg,
                                                    stream->procedure,
                                                    stream->serial,
                                                    buffer, ret);
        }
773 774 775 776 777
    }

    VIR_FREE(buffer);
    return ret;
}