fdstream.c 20.7 KB
Newer Older
1
/*
2
 * fdstream.c: generic streams impl for file descriptors
3
 *
4
 * Copyright (C) 2009-2012, 2014 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 27 28
 *
 */

#include <config.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
29
#include <sys/wait.h>
30 31 32 33
#if HAVE_SYS_UN_H
# include <sys/un.h>
#endif
#include <netinet/in.h>
R
Roman Bogorodskiy 已提交
34
#include <termios.h>
35 36

#include "fdstream.h"
37
#include "virerror.h"
38
#include "datatypes.h"
39
#include "virlog.h"
40
#include "viralloc.h"
41
#include "virutil.h"
E
Eric Blake 已提交
42
#include "virfile.h"
43
#include "configmake.h"
44
#include "virstring.h"
45 46 47

#define VIR_FROM_THIS VIR_FROM_STREAMS

48 49
VIR_LOG_INIT("fdstream");

50 51 52
/* Tunnelled migration stream support */
struct virFDStreamData {
    int fd;
53 54 55 56
    int errfd;
    virCommandPtr cmd;
    unsigned long long offset;
    unsigned long long length;
57 58

    int watch;
59
    int events;         /* events the stream callback is subscribed for */
60 61 62
    bool cbRemoved;
    bool dispatching;
    bool closed;
63 64 65 66
    virStreamEventCallback cb;
    void *opaque;
    virFreeCallback ff;

67 68 69 70
    /* don't call the abort callback more than once */
    bool abortCallbackCalled;
    bool abortCallbackDispatching;

71 72 73 74 75 76
    /* internal callback, as the regular one (from generic streams) gets
     * eaten up by the server stream driver */
    virFDStreamInternalCloseCb icbCb;
    virFDStreamInternalCloseCbFreeOpaque icbFreeOpaque;
    void *icbOpaque;

77 78 79
    virMutex lock;
};

80 81 82 83 84 85 86 87 88 89 90 91

static const char *iohelper_path = LIBEXECDIR "/libvirt_iohelper";

void virFDStreamSetIOHelper(const char *path)
{
    if (path == NULL)
        iohelper_path = LIBEXECDIR "/libvirt_iohelper";
    else
        iohelper_path = path;
}


92 93 94 95 96 97
static int virFDStreamRemoveCallback(virStreamPtr stream)
{
    struct virFDStreamData *fdst = stream->privateData;
    int ret = -1;

    if (!fdst) {
98 99
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("stream is not open"));
100 101 102 103 104
        return -1;
    }

    virMutexLock(&fdst->lock);
    if (fdst->watch == 0) {
105 106
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("stream does not have a callback registered"));
107 108 109 110 111
        goto cleanup;
    }

    virEventRemoveHandle(fdst->watch);
    if (fdst->dispatching)
112
        fdst->cbRemoved = true;
113 114 115 116 117 118
    else if (fdst->ff)
        (fdst->ff)(fdst->opaque);

    fdst->watch = 0;
    fdst->ff = NULL;
    fdst->cb = NULL;
119
    fdst->events = 0;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    fdst->opaque = NULL;

    ret = 0;

cleanup:
    virMutexUnlock(&fdst->lock);
    return ret;
}

static int virFDStreamUpdateCallback(virStreamPtr stream, int events)
{
    struct virFDStreamData *fdst = stream->privateData;
    int ret = -1;

    if (!fdst) {
135 136
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("stream is not open"));
137 138 139 140 141
        return -1;
    }

    virMutexLock(&fdst->lock);
    if (fdst->watch == 0) {
142 143
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("stream does not have a callback registered"));
144 145 146 147
        goto cleanup;
    }

    virEventUpdateHandle(fdst->watch, events);
148
    fdst->events = events;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

    ret = 0;

cleanup:
    virMutexUnlock(&fdst->lock);
    return ret;
}

static void virFDStreamEvent(int watch ATTRIBUTE_UNUSED,
                             int fd ATTRIBUTE_UNUSED,
                             int events,
                             void *opaque)
{
    virStreamPtr stream = opaque;
    struct virFDStreamData *fdst = stream->privateData;
    virStreamEventCallback cb;
    void *cbopaque;
    virFreeCallback ff;
167
    bool closed;
168 169 170 171 172 173 174 175 176 177 178 179 180

    if (!fdst)
        return;

    virMutexLock(&fdst->lock);
    if (!fdst->cb) {
        virMutexUnlock(&fdst->lock);
        return;
    }

    cb = fdst->cb;
    cbopaque = fdst->opaque;
    ff = fdst->ff;
181
    fdst->dispatching = true;
182 183 184 185 186
    virMutexUnlock(&fdst->lock);

    cb(stream, events, cbopaque);

    virMutexLock(&fdst->lock);
187
    fdst->dispatching = false;
188 189
    if (fdst->cbRemoved && ff)
        (ff)(cbopaque);
190
    closed = fdst->closed;
191
    virMutexUnlock(&fdst->lock);
192 193 194 195 196

    if (closed) {
        virMutexDestroy(&fdst->lock);
        VIR_FREE(fdst);
    }
197 198
}

199 200 201 202 203 204 205
static void virFDStreamCallbackFree(void *opaque)
{
    virStreamPtr st = opaque;
    virStreamFree(st);
}


206 207 208 209 210 211 212 213 214 215 216
static int
virFDStreamAddCallback(virStreamPtr st,
                       int events,
                       virStreamEventCallback cb,
                       void *opaque,
                       virFreeCallback ff)
{
    struct virFDStreamData *fdst = st->privateData;
    int ret = -1;

    if (!fdst) {
217 218
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("stream is not open"));
219 220 221 222 223
        return -1;
    }

    virMutexLock(&fdst->lock);
    if (fdst->watch != 0) {
224 225
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("stream already has a callback registered"));
226 227 228 229 230 231 232
        goto cleanup;
    }

    if ((fdst->watch = virEventAddHandle(fdst->fd,
                                           events,
                                           virFDStreamEvent,
                                           st,
233
                                           virFDStreamCallbackFree)) < 0) {
234 235
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("cannot register file watch on stream"));
236 237 238
        goto cleanup;
    }

239
    fdst->cbRemoved = false;
240 241 242
    fdst->cb = cb;
    fdst->opaque = opaque;
    fdst->ff = ff;
243 244
    fdst->events = events;
    fdst->abortCallbackCalled = false;
245 246 247 248 249 250 251 252 253
    virStreamRef(st);

    ret = 0;

cleanup:
    virMutexUnlock(&fdst->lock);
    return ret;
}

E
Eric Blake 已提交
254 255

static int
256
virFDStreamCloseInt(virStreamPtr st, bool streamAbort)
257
{
258 259 260
    struct virFDStreamData *fdst;
    virStreamEventCallback cb;
    void *opaque;
261
    int ret;
E
Eric Blake 已提交
262 263 264

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

265
    if (!st || !(fdst = st->privateData) || fdst->abortCallbackDispatching)
E
Eric Blake 已提交
266 267 268 269
        return 0;

    virMutexLock(&fdst->lock);

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
    /* aborting the stream, ensure the callback is called if it's
     * registered for stream error event */
    if (streamAbort &&
        fdst->cb &&
        (fdst->events & (VIR_STREAM_EVENT_READABLE |
                         VIR_STREAM_EVENT_WRITABLE))) {
        /* don't enter this function accidentally from the callback again */
        if (fdst->abortCallbackCalled) {
            virMutexUnlock(&fdst->lock);
            return 0;
        }

        fdst->abortCallbackCalled = true;
        fdst->abortCallbackDispatching = true;

        /* cache the pointers */
        cb = fdst->cb;
        opaque = fdst->opaque;
        virMutexUnlock(&fdst->lock);

        /* call failure callback, poll reports nothing on closed fd */
        (cb)(st, VIR_STREAM_EVENT_ERROR, opaque);

        virMutexLock(&fdst->lock);
        fdst->abortCallbackDispatching = false;
    }

    /* mutex locked */
298
    ret = VIR_CLOSE(fdst->fd);
299 300 301 302 303 304 305 306 307
    if (fdst->cmd) {
        char buf[1024];
        ssize_t len;
        int status;
        if ((len = saferead(fdst->errfd, buf, sizeof(buf)-1)) < 0)
            buf[0] = '\0';
        else
            buf[len] = '\0';

308
        virCommandRawStatus(fdst->cmd);
309 310 311 312 313
        if (virCommandWait(fdst->cmd, &status) < 0) {
            ret = -1;
        } else if (status != 0) {
            if (buf[0] == '\0') {
                if (WIFEXITED(status)) {
314 315 316
                    virReportError(VIR_ERR_INTERNAL_ERROR,
                                   _("I/O helper exited with status %d"),
                                   WEXITSTATUS(status));
317
                } else {
318 319
                    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                                   _("I/O helper exited abnormally"));
320 321
                }
            } else {
322 323
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               buf);
324 325 326 327
            }
            ret = -1;
        }
        virCommandFree(fdst->cmd);
328
        fdst->cmd = NULL;
329
    }
330 331 332 333

    if (VIR_CLOSE(fdst->errfd) < 0)
        VIR_DEBUG("ignoring failed close on fd %d", fdst->errfd);

334 335
    st->privateData = NULL;

336 337 338 339 340 341 342 343
    /* call the internal stream closing callback */
    if (fdst->icbCb) {
        /* the mutex is not accessible anymore, as private data is null */
        (fdst->icbCb)(st, fdst->icbOpaque);
        if (fdst->icbFreeOpaque)
            (fdst->icbFreeOpaque)(fdst->icbOpaque);
    }

344 345 346 347 348 349 350 351
    if (fdst->dispatching) {
        fdst->closed = true;
        virMutexUnlock(&fdst->lock);
    } else {
        virMutexUnlock(&fdst->lock);
        virMutexDestroy(&fdst->lock);
        VIR_FREE(fdst);
    }
352 353 354 355

    return ret;
}

356 357 358 359 360 361 362 363 364 365 366 367
static int
virFDStreamClose(virStreamPtr st)
{
    return virFDStreamCloseInt(st, false);
}

static int
virFDStreamAbort(virStreamPtr st)
{
    return virFDStreamCloseInt(st, true);
}

368 369 370 371 372 373 374 375 376 377 378 379
static int virFDStreamWrite(virStreamPtr st, const char *bytes, size_t nbytes)
{
    struct virFDStreamData *fdst = st->privateData;
    int ret;

    if (nbytes > INT_MAX) {
        virReportSystemError(ERANGE, "%s",
                             _("Too many bytes to write to stream"));
        return -1;
    }

    if (!fdst) {
380 381
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("stream is not open"));
382 383 384 385 386
        return -1;
    }

    virMutexLock(&fdst->lock);

387 388 389 390 391 392 393 394 395 396 397 398
    if (fdst->length) {
        if (fdst->length == fdst->offset) {
            virReportSystemError(ENOSPC, "%s",
                                 _("cannot write to stream"));
            virMutexUnlock(&fdst->lock);
            return -1;
        }

        if ((fdst->length - fdst->offset) < nbytes)
            nbytes = fdst->length - fdst->offset;
    }

399 400 401 402 403 404 405 406 407 408 409 410
retry:
    ret = write(fdst->fd, bytes, nbytes);
    if (ret < 0) {
        if (errno == EAGAIN || errno == EWOULDBLOCK) {
            ret = -2;
        } else if (errno == EINTR) {
            goto retry;
        } else {
            ret = -1;
            virReportSystemError(errno, "%s",
                                 _("cannot write to stream"));
        }
411 412
    } else if (fdst->length) {
        fdst->offset += ret;
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
    }

    virMutexUnlock(&fdst->lock);
    return ret;
}


static int virFDStreamRead(virStreamPtr st, char *bytes, size_t nbytes)
{
    struct virFDStreamData *fdst = st->privateData;
    int ret;

    if (nbytes > INT_MAX) {
        virReportSystemError(ERANGE, "%s",
                             _("Too many bytes to read from stream"));
        return -1;
    }

    if (!fdst) {
432 433
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("stream is not open"));
434 435 436 437 438
        return -1;
    }

    virMutexLock(&fdst->lock);

439 440 441 442 443 444 445 446 447 448
    if (fdst->length) {
        if (fdst->length == fdst->offset) {
            virMutexUnlock(&fdst->lock);
            return 0;
        }

        if ((fdst->length - fdst->offset) < nbytes)
            nbytes = fdst->length - fdst->offset;
    }

449 450 451 452 453 454 455 456 457 458 459 460
retry:
    ret = read(fdst->fd, bytes, nbytes);
    if (ret < 0) {
        if (errno == EAGAIN || errno == EWOULDBLOCK) {
            ret = -2;
        } else if (errno == EINTR) {
            goto retry;
        } else {
            ret = -1;
            virReportSystemError(errno, "%s",
                                 _("cannot read from stream"));
        }
461 462
    } else if (fdst->length) {
        fdst->offset += ret;
463 464 465 466 467 468 469 470 471 472 473
    }

    virMutexUnlock(&fdst->lock);
    return ret;
}


static virStreamDriver virFDStreamDrv = {
    .streamSend = virFDStreamWrite,
    .streamRecv = virFDStreamRead,
    .streamFinish = virFDStreamClose,
474
    .streamAbort = virFDStreamAbort,
475 476 477
    .streamEventAddCallback = virFDStreamAddCallback,
    .streamEventUpdateCallback = virFDStreamUpdateCallback,
    .streamEventRemoveCallback = virFDStreamRemoveCallback
478 479
};

480 481 482 483 484
static int virFDStreamOpenInternal(virStreamPtr st,
                                   int fd,
                                   virCommandPtr cmd,
                                   int errfd,
                                   unsigned long long length)
485 486 487
{
    struct virFDStreamData *fdst;

488 489 490
    VIR_DEBUG("st=%p fd=%d cmd=%p errfd=%d length=%llu",
              st, fd, cmd, errfd, length);

491 492 493 494
    if ((st->flags & VIR_STREAM_NONBLOCK) &&
        virSetNonBlock(fd) < 0)
        return -1;

495
    if (VIR_ALLOC(fdst) < 0)
496 497 498
        return -1;

    fdst->fd = fd;
499 500 501
    fdst->cmd = cmd;
    fdst->errfd = errfd;
    fdst->length = length;
502 503
    if (virMutexInit(&fdst->lock) < 0) {
        VIR_FREE(fdst);
504 505
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to initialize mutex"));
506 507 508 509 510 511 512 513 514 515
        return -1;
    }

    st->driver = &virFDStreamDrv;
    st->privateData = fdst;

    return 0;
}


516 517 518 519 520 521 522
int virFDStreamOpen(virStreamPtr st,
                    int fd)
{
    return virFDStreamOpenInternal(st, fd, NULL, -1, 0);
}


523 524 525 526 527 528
#if HAVE_SYS_UN_H
int virFDStreamConnectUNIX(virStreamPtr st,
                           const char *path,
                           bool abstract)
{
    struct sockaddr_un sa;
529
    size_t i = 0;
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
    int timeout = 3;
    int ret;

    int fd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (fd < 0) {
        virReportSystemError(errno, "%s", _("Unable to open UNIX socket"));
        goto error;
    }

    memset(&sa, 0, sizeof(sa));
    sa.sun_family = AF_UNIX;
    if (abstract) {
        if (virStrcpy(sa.sun_path+1, path, sizeof(sa.sun_path)-1) == NULL)
            goto error;
        sa.sun_path[0] = '\0';
    } else {
        if (virStrcpy(sa.sun_path, path, sizeof(sa.sun_path)) == NULL)
            goto error;
    }

    do {
        ret = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
        if (ret == 0)
            break;

        if (errno == ENOENT || errno == ECONNREFUSED) {
            /* ENOENT       : Socket may not have shown up yet
             * ECONNREFUSED : Leftover socket hasn't been removed yet */
            continue;
        }

        goto error;
    } while ((++i <= timeout*5) && (usleep(.2 * 1000000) <= 0));

564
    if (virFDStreamOpenInternal(st, fd, NULL, -1, 0) < 0)
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
        goto error;
    return 0;

error:
    VIR_FORCE_CLOSE(fd);
    return -1;
}
#else
int virFDStreamConnectUNIX(virStreamPtr st ATTRIBUTE_UNUSED,
                           const char *path ATTRIBUTE_UNUSED,
                           bool abstract ATTRIBUTE_UNUSED)
{
    virReportSystemError(ENOSYS, "%s",
                         _("UNIX domain sockets are not supported on this platform"));
    return -1;
}
#endif

583 584 585 586 587
static int
virFDStreamOpenFileInternal(virStreamPtr st,
                            const char *path,
                            unsigned long long offset,
                            unsigned long long length,
588
                            int oflags,
E
Eric Blake 已提交
589
                            int mode)
590
{
591
    int fd = -1;
E
Eric Blake 已提交
592
    int childfd = -1;
593
    struct stat sb;
594 595
    virCommandPtr cmd = NULL;
    int errfd = -1;
596

E
Eric Blake 已提交
597 598
    VIR_DEBUG("st=%p path=%s oflags=%x offset=%llu length=%llu mode=%o",
              st, path, oflags, offset, length, mode);
599

600
    oflags |= O_NOCTTY | O_BINARY;
601

602 603
    if (oflags & O_CREAT)
        fd = open(path, oflags, mode);
604
    else
605
        fd = open(path, oflags);
606
    if (fd < 0) {
607 608 609 610 611 612 613 614 615 616 617 618 619
        virReportSystemError(errno,
                             _("Unable to open stream for '%s'"),
                             path);
        return -1;
    }

    if (fstat(fd, &sb) < 0) {
        virReportSystemError(errno,
                             _("Unable to access stream for '%s'"),
                             path);
        goto error;
    }

620 621 622 623 624 625 626 627
    if (offset &&
        lseek(fd, offset, SEEK_SET) != offset) {
        virReportSystemError(errno,
                             _("Unable to seek %s to %llu"),
                             path, offset);
        goto error;
    }

628 629
    /* Thanks to the POSIX i/o model, we can't reliably get
     * non-blocking I/O on block devs/regular files. To
630
     * support those we need to fork a helper process to do
631 632 633 634 635
     * the I/O so we just have a fifo. Or use AIO :-(
     */
    if ((st->flags & VIR_STREAM_NONBLOCK) &&
        (!S_ISCHR(sb.st_mode) &&
         !S_ISFIFO(sb.st_mode))) {
E
Eric Blake 已提交
636
        int fds[2] = { -1, -1 };
637

638
        if ((oflags & O_ACCMODE) == O_RDWR) {
639 640 641
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("%s: Cannot request read and write flags together"),
                           path);
642 643 644 645 646 647 648 649 650
            goto error;
        }

        if (pipe(fds) < 0) {
            virReportSystemError(errno, "%s",
                                 _("Unable to create pipe"));
            goto error;
        }

651
        cmd = virCommandNewArgList(iohelper_path,
652 653 654
                                   path,
                                   NULL);
        virCommandAddArgFormat(cmd, "%llu", length);
655 656
        virCommandPassFD(cmd, fd,
                         VIR_COMMAND_PASS_FD_CLOSE_PARENT);
657
        virCommandAddArgFormat(cmd, "%d", fd);
658

659
        if ((oflags & O_ACCMODE) == O_RDONLY) {
660 661 662 663 664 665 666 667 668 669
            childfd = fds[1];
            fd = fds[0];
            virCommandSetOutputFD(cmd, &childfd);
        } else {
            childfd = fds[0];
            fd = fds[1];
            virCommandSetInputFD(cmd, childfd);
        }
        virCommandSetErrorFD(cmd, &errfd);

670
        if (virCommandRunAsync(cmd, NULL) < 0)
671 672 673
            goto error;

        VIR_FORCE_CLOSE(childfd);
674 675
    }

676
    if (virFDStreamOpenInternal(st, fd, cmd, errfd, length) < 0)
677 678 679 680 681
        goto error;

    return 0;

error:
682
    virCommandFree(cmd);
E
Eric Blake 已提交
683
    VIR_FORCE_CLOSE(fd);
E
Eric Blake 已提交
684
    VIR_FORCE_CLOSE(childfd);
W
Wen Congyang 已提交
685
    VIR_FORCE_CLOSE(errfd);
E
Eric Blake 已提交
686 687
    if (oflags & O_CREAT)
        unlink(path);
688 689 690
    return -1;
}

691 692 693 694
int virFDStreamOpenFile(virStreamPtr st,
                        const char *path,
                        unsigned long long offset,
                        unsigned long long length,
E
Eric Blake 已提交
695
                        int oflags)
696
{
697
    if (oflags & O_CREAT) {
698 699 700
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Attempt to create %s without specifying mode"),
                       path);
701
        return -1;
702
    }
703 704
    return virFDStreamOpenFileInternal(st, path,
                                       offset, length,
E
Eric Blake 已提交
705
                                       oflags, 0);
706
}
707

708 709 710 711
int virFDStreamCreateFile(virStreamPtr st,
                          const char *path,
                          unsigned long long offset,
                          unsigned long long length,
712
                          int oflags,
E
Eric Blake 已提交
713
                          mode_t mode)
714 715 716
{
    return virFDStreamOpenFileInternal(st, path,
                                       offset, length,
E
Eric Blake 已提交
717
                                       oflags | O_CREAT, mode);
718
}
719

R
Roman Bogorodskiy 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
#ifdef HAVE_CFMAKERAW
int virFDStreamOpenPTY(virStreamPtr st,
                       const char *path,
                       unsigned long long offset,
                       unsigned long long length,
                       int oflags)
{
    struct virFDStreamData *fdst = NULL;
    struct termios rawattr;

    if (virFDStreamOpenFileInternal(st, path,
                                    offset, length,
                                    oflags | O_CREAT, 0) < 0)
        return -1;

    fdst = st->privateData;

    if (tcgetattr(fdst->fd, &rawattr) < 0) {
        virReportSystemError(errno,
                             _("unable to get tty attributes: %s"),
                             path);
        goto cleanup;
    }

    cfmakeraw(&rawattr);

    if (tcsetattr(fdst->fd, TCSANOW, &rawattr) < 0) {
        virReportSystemError(errno,
                             _("unable to set tty attributes: %s"),
                             path);
        goto cleanup;
    }

    return 0;

cleanup:
    virFDStreamClose(st);
    return -1;
}
#else /* !HAVE_CFMAKERAW */
int virFDStreamOpenPTY(virStreamPtr st,
                       const char *path,
                       unsigned long long offset,
                       unsigned long long length,
                       int oflags)
{
    return virFDStreamOpenFileInternal(st, path,
                                       offset, length,
                                       oflags | O_CREAT, 0);
}
#endif /* !HAVE_CFMAKERAW */

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
int virFDStreamSetInternalCloseCb(virStreamPtr st,
                                  virFDStreamInternalCloseCb cb,
                                  void *opaque,
                                  virFDStreamInternalCloseCbFreeOpaque fcb)
{
    struct virFDStreamData *fdst = st->privateData;

    virMutexLock(&fdst->lock);

    if (fdst->icbFreeOpaque)
        (fdst->icbFreeOpaque)(fdst->icbOpaque);

    fdst->icbCb = cb;
    fdst->icbOpaque = opaque;
    fdst->icbFreeOpaque = fcb;

    virMutexUnlock(&fdst->lock);
    return 0;
}