qemu_monitor.c 38.4 KB
Newer Older
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
/*
 * qemu_monitor.c: interaction with QEMU monitor console
 *
 * Copyright (C) 2006-2009 Red Hat, Inc.
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <poll.h>
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>

#include "qemu_monitor.h"
32
#include "qemu_monitor_text.h"
D
Daniel P. Berrange 已提交
33
#include "qemu_monitor_json.h"
34 35 36
#include "qemu_conf.h"
#include "event.h"
#include "virterror_internal.h"
37 38
#include "memory.h"
#include "logging.h"
39 40 41

#define VIR_FROM_THIS VIR_FROM_QEMU

42 43
#define DEBUG_IO 0
#define DEBUG_RAW_IO 0
44

45
struct _qemuMonitor {
46
    virMutex lock;
47 48
    virCond notify;

49
    int refs;
50

51 52 53 54 55 56
    int fd;
    int watch;
    int hasSendFD;

    virDomainObjPtr vm;

57
    qemuMonitorCallbacksPtr cb;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

    /* If there's a command being processed this will be
     * non-NULL */
    qemuMonitorMessagePtr msg;

    /* Buffer incoming data ready for Text/QMP monitor
     * code to process & find message boundaries */
    size_t bufferOffset;
    size_t bufferLength;
    char *buffer;

    /* If anything went wrong, this will be fed back
     * the next monitor msg */
    int lastErrno;

73
    /* If the monitor EOF callback is currently active (stops more commands being run) */
74
    unsigned eofcb: 1;
75
    /* If the monitor is in process of shutting down */
76
    unsigned closed: 1;
77

D
Daniel P. Berrange 已提交
78
    unsigned json: 1;
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
VIR_ENUM_IMPL(qemuMonitorMigrationStatus,
              QEMU_MONITOR_MIGRATION_STATUS_LAST,
              "inactive", "active", "completed", "failed", "cancelled")

static char *qemuMonitorEscape(const char *in, int shell)
{
    int len = 0;
    int i, j;
    char *out;

    /* To pass through the QEMU monitor, we need to use escape
       sequences: \r, \n, \", \\

       To pass through both QEMU + the shell, we need to escape
       the single character ' as the five characters '\\''
    */

    for (i = 0; in[i] != '\0'; i++) {
        switch(in[i]) {
        case '\r':
        case '\n':
        case '"':
        case '\\':
            len += 2;
            break;
        case '\'':
            if (shell)
                len += 5;
            else
                len += 1;
            break;
        default:
            len += 1;
            break;
        }
    }

    if (VIR_ALLOC_N(out, len + 1) < 0)
        return NULL;

    for (i = j = 0; in[i] != '\0'; i++) {
        switch(in[i]) {
        case '\r':
            out[j++] = '\\';
            out[j++] = 'r';
            break;
        case '\n':
            out[j++] = '\\';
            out[j++] = 'n';
            break;
        case '"':
        case '\\':
            out[j++] = '\\';
            out[j++] = in[i];
            break;
        case '\'':
            if (shell) {
                out[j++] = '\'';
                out[j++] = '\\';
                out[j++] = '\\';
                out[j++] = '\'';
                out[j++] = '\'';
            } else {
                out[j++] = in[i];
            }
            break;
        default:
            out[j++] = in[i];
            break;
        }
    }
    out[j] = '\0';

    return out;
}

char *qemuMonitorEscapeArg(const char *in)
{
    return qemuMonitorEscape(in, 0);
}

char *qemuMonitorEscapeShell(const char *in)
{
    return qemuMonitorEscape(in, 1);
}


169
#if QEMU_DEBUG_RAW_IO
170
# include <c-ctype.h>
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
static char * qemuMonitorEscapeNonPrintable(const char *text)
{
    int i;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    for (i = 0 ; text[i] != '\0' ; i++) {
        if (c_isprint(text[i]) ||
            text[i] == '\n' ||
            (text[i] == '\r' && text[i+1] == '\n'))
            virBufferVSprintf(&buf,"%c", text[i]);
        else
            virBufferVSprintf(&buf, "0x%02x", text[i]);
    }
    return virBufferContentAndReset(&buf);
}
#endif

187 188 189 190 191 192 193 194 195 196
void qemuMonitorLock(qemuMonitorPtr mon)
{
    virMutexLock(&mon->lock);
}

void qemuMonitorUnlock(qemuMonitorPtr mon)
{
    virMutexUnlock(&mon->lock);
}

197

198
static void qemuMonitorFree(qemuMonitorPtr mon)
199
{
200
    VIR_DEBUG("mon=%p", mon);
201 202 203 204
    if (virCondDestroy(&mon->notify) < 0)
    {}
    virMutexDestroy(&mon->lock);
    VIR_FREE(mon);
205 206
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
int qemuMonitorRef(qemuMonitorPtr mon)
{
    mon->refs++;
    return mon->refs;
}

int qemuMonitorUnref(qemuMonitorPtr mon)
{
    mon->refs--;

    if (mon->refs == 0) {
        qemuMonitorUnlock(mon);
        qemuMonitorFree(mon);
        return 0;
    }

    return mon->refs;
}

226 227

static int
228
qemuMonitorOpenUnix(const char *monitor)
229 230 231 232 233 234 235
{
    struct sockaddr_un addr;
    int monfd;
    int timeout = 3; /* In seconds */
    int ret, i = 0;

    if ((monfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
236
        virReportSystemError(errno,
237 238 239 240 241 242 243
                             "%s", _("failed to create socket"));
        return -1;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    if (virStrcpyStatic(addr.sun_path, monitor) == NULL) {
244 245
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Monitor path %s too big for destination"), monitor);
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        goto error;
    }

    do {
        ret = connect(monfd, (struct sockaddr *) &addr, sizeof(addr));

        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;
        }

261
        virReportSystemError(errno, "%s",
262 263 264 265 266 267
                             _("failed to connect to monitor socket"));
        goto error;

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

    if (ret != 0) {
268
        virReportSystemError(errno, "%s",
269 270 271 272
                             _("monitor socket did not show up."));
        goto error;
    }

273
    return monfd;
274 275 276 277 278 279 280

error:
    close(monfd);
    return -1;
}

static int
281
qemuMonitorOpenPty(const char *monitor)
282 283 284 285
{
    int monfd;

    if ((monfd = open(monitor, O_RDWR)) < 0) {
286 287
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Unable to open monitor path %s"), monitor);
288 289 290
        return -1;
    }

291
    return monfd;
292
}
293

294 295 296 297 298 299 300 301 302 303 304 305

static int
qemuMonitorIOProcess(qemuMonitorPtr mon)
{
    int len;
    qemuMonitorMessagePtr msg = NULL;

    /* See if there's a message & whether its ready for its reply
     * ie whether its completed writing all its data */
    if (mon->msg && mon->msg->txOffset == mon->msg->txLength)
        msg = mon->msg;

306 307
#if DEBUG_IO
#if DEBUG_RAW_IO
308 309 310 311 312 313
    char *str1 = qemuMonitorEscapeNonPrintable(msg ? msg->txBuffer : "");
    char *str2 = qemuMonitorEscapeNonPrintable(mon->buffer);
    VIR_ERROR("Process %d %p %p [[[[%s]]][[[%s]]]", (int)mon->bufferOffset, mon->msg, msg, str1, str2);
    VIR_FREE(str1);
    VIR_FREE(str2);
#else
314
    VIR_DEBUG("Process %d", (int)mon->bufferOffset);
315
#endif
316 317
#endif

D
Daniel P. Berrange 已提交
318 319 320 321 322 323 324 325
    if (mon->json)
        len = qemuMonitorJSONIOProcess(mon,
                                       mon->buffer, mon->bufferOffset,
                                       msg);
    else
        len = qemuMonitorTextIOProcess(mon,
                                       mon->buffer, mon->bufferOffset,
                                       msg);
326 327 328 329 330 331 332 333 334 335 336 337 338

    if (len < 0) {
        mon->lastErrno = errno;
        return -1;
    }

    if (len < mon->bufferOffset) {
        memmove(mon->buffer, mon->buffer + len, mon->bufferOffset - len);
        mon->bufferOffset -= len;
    } else {
        VIR_FREE(mon->buffer);
        mon->bufferOffset = mon->bufferLength = 0;
    }
339
#if DEBUG_IO
340
    VIR_DEBUG("Process done %d used %d", (int)mon->bufferOffset, len);
341
#endif
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
    if (msg && msg->finished)
        virCondBroadcast(&mon->notify);
    return len;
}


static int
qemuMonitorIOWriteWithFD(qemuMonitorPtr mon,
                         const char *data,
                         size_t len,
                         int fd)
{
    struct msghdr msg;
    struct iovec iov[1];
    int ret;
    char control[CMSG_SPACE(sizeof(int))];
    struct cmsghdr *cmsg;

    if (!mon->hasSendFD) {
        errno = EINVAL;
        return -1;
    }

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

    iov[0].iov_base = (void *)data;
    iov[0].iov_len = len;

    msg.msg_iov = iov;
    msg.msg_iovlen = 1;

    msg.msg_control = control;
    msg.msg_controllen = sizeof(control);

    cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_len = CMSG_LEN(sizeof(int));
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));

    do {
        ret = sendmsg(mon->fd, &msg, 0);
    } while (ret < 0 && errno == EINTR);

    return ret;
}

/* Called when the monitor is able to write data */
static int
qemuMonitorIOWrite(qemuMonitorPtr mon)
{
    int done;

    /* If no active message, or fully transmitted, the no-op */
    if (!mon->msg || mon->msg->txOffset == mon->msg->txLength)
        return 0;

    if (mon->msg->txFD == -1)
        done = write(mon->fd,
                     mon->msg->txBuffer + mon->msg->txOffset,
                     mon->msg->txLength - mon->msg->txOffset);
    else
        done = qemuMonitorIOWriteWithFD(mon,
                                        mon->msg->txBuffer + mon->msg->txOffset,
                                        mon->msg->txLength - mon->msg->txOffset,
                                        mon->msg->txFD);

    if (done < 0) {
        if (errno == EAGAIN)
            return 0;

        mon->lastErrno = errno;
        return -1;
    }
    mon->msg->txOffset += done;
    return done;
}

/*
 * Called when the monitor has incoming data to read
 *
 * Returns -1 on error, or number of bytes read
 */
static int
qemuMonitorIORead(qemuMonitorPtr mon)
{
    size_t avail = mon->bufferLength - mon->bufferOffset;
    int ret = 0;

    if (avail < 1024) {
        if (VIR_REALLOC_N(mon->buffer,
                          mon->bufferLength + 1024) < 0) {
            errno = ENOMEM;
            return -1;
        }
        mon->bufferLength += 1024;
        avail += 1024;
    }

    /* Read as much as we can get into our buffer,
       until we block on EAGAIN, or hit EOF */
    while (avail > 1) {
        int got;
        got = read(mon->fd,
                   mon->buffer + mon->bufferOffset,
                   avail - 1);
        if (got < 0) {
            if (errno == EAGAIN)
                break;
            mon->lastErrno = errno;
            ret = -1;
            break;
        }
        if (got == 0)
            break;

        ret += got;
        avail -= got;
        mon->bufferOffset += got;
        mon->buffer[mon->bufferOffset] = '\0';
    }

464
#if DEBUG_IO
465
    VIR_DEBUG("Now read %d bytes of data", (int)mon->bufferOffset);
466
#endif
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485

    return ret;
}


static void qemuMonitorUpdateWatch(qemuMonitorPtr mon)
{
    int events =
        VIR_EVENT_HANDLE_HANGUP |
        VIR_EVENT_HANDLE_ERROR;

    if (!mon->lastErrno) {
        events |= VIR_EVENT_HANDLE_READABLE;

        if (mon->msg && mon->msg->txOffset < mon->msg->txLength)
            events |= VIR_EVENT_HANDLE_WRITABLE;
    }

    virEventUpdateHandle(mon->watch, events);
486 487
}

488 489 490 491 492 493

static void
qemuMonitorIO(int watch, int fd, int events, void *opaque) {
    qemuMonitorPtr mon = opaque;
    int quit = 0, failed = 0;

494
    qemuMonitorLock(mon);
495
    qemuMonitorRef(mon);
496
#if DEBUG_IO
497
    VIR_DEBUG("Monitor %p I/O on watch %d fd %d events %d", mon, watch, fd, events);
498
#endif
499

500
    if (mon->fd != fd || mon->watch != watch) {
501
        VIR_ERROR("event from unexpected fd %d!=%d / watch %d!=%d", mon->fd, fd, mon->watch, watch);
502 503
        failed = 1;
    } else {
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
        if (!mon->lastErrno &&
            events & VIR_EVENT_HANDLE_WRITABLE) {
            int done = qemuMonitorIOWrite(mon);
            if (done < 0)
                failed = 1;
            events &= ~VIR_EVENT_HANDLE_WRITABLE;
        }
        if (!mon->lastErrno &&
            events & VIR_EVENT_HANDLE_READABLE) {
            int got = qemuMonitorIORead(mon);
            if (got < 0)
                failed = 1;
            /* Ignore hangup/error events if we read some data, to
             * give time for that data to be consumed */
            if (got > 0) {
                events = 0;

                if (qemuMonitorIOProcess(mon) < 0)
                    failed = 1;
            } else
                events &= ~VIR_EVENT_HANDLE_READABLE;
        }

        /* If IO process resulted in an error & we have a message,
         * then wakeup that waiter */
        if (mon->lastErrno && mon->msg && !mon->msg->finished) {
            mon->msg->lastErrno = mon->lastErrno;
            mon->msg->finished = 1;
            virCondSignal(&mon->notify);
        }

        qemuMonitorUpdateWatch(mon);

        if (events & VIR_EVENT_HANDLE_HANGUP) {
            /* If IO process resulted in EOF & we have a message,
             * then wakeup that waiter */
            if (mon->msg && !mon->msg->finished) {
                mon->msg->finished = 1;
                mon->msg->lastErrno = EIO;
                virCondSignal(&mon->notify);
            }
545
            quit = 1;
546
        } else if (events) {
547 548 549 550 551 552
            VIR_ERROR(_("unhandled fd event %d for monitor fd %d"),
                      events, mon->fd);
            failed = 1;
        }
    }

553 554 555 556
    /* We have to unlock to avoid deadlock against command thread,
     * but is this safe ?  I think it is, because the callback
     * will try to acquire the virDomainObjPtr mutex next */
    if (failed || quit) {
557 558
        void (*eofNotify)(qemuMonitorPtr, virDomainObjPtr, int)
            = mon->cb->eofNotify;
559
        virDomainObjPtr vm = mon->vm;
560 561
        /* Make sure anyone waiting wakes up now */
        virCondSignal(&mon->notify);
562
        if (qemuMonitorUnref(mon) > 0)
563
            qemuMonitorUnlock(mon);
564
        VIR_DEBUG("Triggering EOF callback error? %d", failed);
565
        (eofNotify)(mon, vm, failed);
566
    } else {
567 568
        if (qemuMonitorUnref(mon) > 0)
            qemuMonitorUnlock(mon);
569
    }
570 571 572 573
}


qemuMonitorPtr
574
qemuMonitorOpen(virDomainObjPtr vm,
575
                virDomainChrDefPtr config,
D
Daniel P. Berrange 已提交
576
                int json,
577
                qemuMonitorCallbacksPtr cb)
578
{
579 580
    qemuMonitorPtr mon;

581
    if (!cb || !cb->eofNotify) {
582 583
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("EOF notify callback must be supplied"));
584 585 586
        return NULL;
    }

587
    if (VIR_ALLOC(mon) < 0) {
588
        virReportOOMError();
589 590 591
        return NULL;
    }

592
    if (virMutexInit(&mon->lock) < 0) {
593 594
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("cannot initialize monitor mutex"));
595 596 597
        VIR_FREE(mon);
        return NULL;
    }
598
    if (virCondInit(&mon->notify) < 0) {
599 600
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("cannot initialize monitor condition"));
601 602 603 604
        virMutexDestroy(&mon->lock);
        VIR_FREE(mon);
        return NULL;
    }
605
    mon->fd = -1;
606
    mon->refs = 1;
607
    mon->vm = vm;
D
Daniel P. Berrange 已提交
608
    mon->json = json;
609
    mon->cb = cb;
610
    qemuMonitorLock(mon);
611

612
    switch (config->type) {
613
    case VIR_DOMAIN_CHR_TYPE_UNIX:
614
        mon->hasSendFD = 1;
615
        mon->fd = qemuMonitorOpenUnix(config->data.nix.path);
616 617
        break;

618
    case VIR_DOMAIN_CHR_TYPE_PTY:
619
        mon->fd = qemuMonitorOpenPty(config->data.file.path);
620 621
        break;

622
    default:
623 624 625
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("unable to handle monitor type: %s"),
                        virDomainChrTypeToString(config->type));
626 627 628
        goto cleanup;
    }

629 630
    if (mon->fd == -1) goto cleanup;

631
    if (virSetCloseExec(mon->fd) < 0) {
632 633
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("Unable to set monitor close-on-exec flag"));
634 635 636
        goto cleanup;
    }
    if (virSetNonBlock(mon->fd) < 0) {
637 638
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("Unable to put monitor into non-blocking mode"));
639 640 641 642
        goto cleanup;
    }


643
    if ((mon->watch = virEventAddHandle(mon->fd,
644 645 646
                                        VIR_EVENT_HANDLE_HANGUP |
                                        VIR_EVENT_HANDLE_ERROR |
                                        VIR_EVENT_HANDLE_READABLE,
647 648
                                        qemuMonitorIO,
                                        mon, NULL)) < 0) {
649 650
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("unable to register monitor events"));
651 652 653
        goto cleanup;
    }

654 655
    VIR_DEBUG("New mon %p fd =%d watch=%d", mon, mon->fd, mon->watch);
    qemuMonitorUnlock(mon);
656

657 658 659
    return mon;

cleanup:
660
    qemuMonitorUnlock(mon);
661 662 663 664 665
    qemuMonitorClose(mon);
    return NULL;
}


666
int qemuMonitorClose(qemuMonitorPtr mon)
667
{
668 669
    int refs;

670
    if (!mon)
671 672 673
        return 0;

    VIR_DEBUG("mon=%p", mon);
674

675 676 677 678 679 680
    qemuMonitorLock(mon);
    if (!mon->closed) {
        if (mon->watch)
            virEventRemoveHandle(mon->watch);
        if (mon->fd != -1)
            close(mon->fd);
681 682 683 684 685
        /* NB: ordinarily one might immediately set mon->watch to -1
         * and mon->fd to -1, but there may be a callback active
         * that is still relying on these fields being valid. So
         * we merely close them, but not clear their values and
         * use this explicit 'closed' flag to track this state */
686 687
        mon->closed = 1;
    }
688

689
    if ((refs = qemuMonitorUnref(mon)) > 0)
690
        qemuMonitorUnlock(mon);
691
    return refs;
692 693 694
}


695 696
int qemuMonitorSend(qemuMonitorPtr mon,
                    qemuMonitorMessagePtr msg)
697
{
698
    int ret = -1;
699

700 701
    if (mon->eofcb) {
        msg->lastErrno = EIO;
702 703
        return -1;
    }
704

705 706
    mon->msg = msg;
    qemuMonitorUpdateWatch(mon);
707

708 709 710 711
    while (!mon->msg->finished) {
        if (virCondWait(&mon->notify, &mon->lock) < 0)
            goto cleanup;
    }
712

713 714
    if (mon->lastErrno == 0)
        ret = 0;
715

716 717 718
cleanup:
    mon->msg = NULL;
    qemuMonitorUpdateWatch(mon);
719

720
    return ret;
721
}
722 723 724 725 726 727 728 729


int qemuMonitorGetDiskSecret(qemuMonitorPtr mon,
                             virConnectPtr conn,
                             const char *path,
                             char **secret,
                             size_t *secretLen)
{
730
    int ret = -1;
731 732 733
    *secret = NULL;
    *secretLen = 0;

734 735 736 737 738 739 740
    qemuMonitorRef(mon);
    qemuMonitorUnlock(mon);
    if (mon->cb && mon->cb->diskSecretLookup)
        ret = mon->cb->diskSecretLookup(mon, conn, mon->vm, path, secret, secretLen);
    qemuMonitorLock(mon);
    qemuMonitorUnref(mon);
    return ret;
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 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
int qemuMonitorEmitShutdown(qemuMonitorPtr mon)
{
    int ret = -1;
    VIR_DEBUG("mon=%p", mon);

    qemuMonitorRef(mon);
    qemuMonitorUnlock(mon);
    if (mon->cb && mon->cb->domainShutdown)
        ret = mon->cb->domainShutdown(mon, mon->vm);
    qemuMonitorLock(mon);
    qemuMonitorUnref(mon);
    return ret;
}


int qemuMonitorEmitReset(qemuMonitorPtr mon)
{
    int ret = -1;
    VIR_DEBUG("mon=%p", mon);

    qemuMonitorRef(mon);
    qemuMonitorUnlock(mon);
    if (mon->cb && mon->cb->domainReset)
        ret = mon->cb->domainReset(mon, mon->vm);
    qemuMonitorLock(mon);
    qemuMonitorUnref(mon);
    return ret;
}


int qemuMonitorEmitPowerdown(qemuMonitorPtr mon)
{
    int ret = -1;
    VIR_DEBUG("mon=%p", mon);

    qemuMonitorRef(mon);
    qemuMonitorUnlock(mon);
    if (mon->cb && mon->cb->domainPowerdown)
        ret = mon->cb->domainPowerdown(mon, mon->vm);
    qemuMonitorLock(mon);
    qemuMonitorUnref(mon);
    return ret;
}


int qemuMonitorEmitStop(qemuMonitorPtr mon)
{
    int ret = -1;
    VIR_DEBUG("mon=%p", mon);

    qemuMonitorRef(mon);
    qemuMonitorUnlock(mon);
    if (mon->cb && mon->cb->domainStop)
        ret = mon->cb->domainStop(mon, mon->vm);
    qemuMonitorLock(mon);
    qemuMonitorUnref(mon);
    return ret;
}


804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
int qemuMonitorEmitRTCChange(qemuMonitorPtr mon, long long offset)
{
    int ret = -1;
    VIR_DEBUG("mon=%p", mon);

    qemuMonitorRef(mon);
    qemuMonitorUnlock(mon);
    if (mon->cb && mon->cb->domainRTCChange)
        ret = mon->cb->domainRTCChange(mon, mon->vm, offset);
    qemuMonitorLock(mon);
    qemuMonitorUnref(mon);
    return ret;
}


819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
int qemuMonitorEmitWatchdog(qemuMonitorPtr mon, int action)
{
    int ret = -1;
    VIR_DEBUG("mon=%p", mon);

    qemuMonitorRef(mon);
    qemuMonitorUnlock(mon);
    if (mon->cb && mon->cb->domainWatchdog)
        ret = mon->cb->domainWatchdog(mon, mon->vm, action);
    qemuMonitorLock(mon);
    qemuMonitorUnref(mon);
    return ret;
}


834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
int qemuMonitorEmitIOError(qemuMonitorPtr mon,
                           const char *diskAlias,
                           int action)
{
    int ret = -1;
    VIR_DEBUG("mon=%p", mon);

    qemuMonitorRef(mon);
    qemuMonitorUnlock(mon);
    if (mon->cb && mon->cb->domainIOError)
        ret = mon->cb->domainIOError(mon, mon->vm, diskAlias, action);
    qemuMonitorLock(mon);
    qemuMonitorUnref(mon);
    return ret;
}


851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
int qemuMonitorEmitGraphics(qemuMonitorPtr mon,
                            int phase,
                            int localFamily,
                            const char *localNode,
                            const char *localService,
                            int remoteFamily,
                            const char *remoteNode,
                            const char *remoteService,
                            const char *authScheme,
                            const char *x509dname,
                            const char *saslUsername)
{
    int ret = -1;
    VIR_DEBUG("mon=%p", mon);

    qemuMonitorRef(mon);
    qemuMonitorUnlock(mon);
    if (mon->cb && mon->cb->domainGraphics)
        ret = mon->cb->domainGraphics(mon, mon->vm,
                                      phase,
                                      localFamily, localNode, localService,
                                      remoteFamily, remoteNode, remoteService,
                                      authScheme, x509dname, saslUsername);
    qemuMonitorLock(mon);
    qemuMonitorUnref(mon);
    return ret;
}



881 882 883 884 885 886 887 888 889 890 891 892 893
int qemuMonitorSetCapabilities(qemuMonitorPtr mon)
{
    int ret;
    DEBUG("mon=%p, fd=%d", mon, mon->fd);

    if (mon->json)
        ret = qemuMonitorJSONSetCapabilities(mon);
    else
        ret = 0;
    return ret;
}


894 895 896 897
int
qemuMonitorStartCPUs(qemuMonitorPtr mon,
                     virConnectPtr conn)
{
D
Daniel P. Berrange 已提交
898
    int ret;
899 900
    DEBUG("mon=%p, fd=%d", mon, mon->fd);

D
Daniel P. Berrange 已提交
901 902 903 904 905
    if (mon->json)
        ret = qemuMonitorJSONStartCPUs(mon, conn);
    else
        ret = qemuMonitorTextStartCPUs(mon, conn);
    return ret;
906 907 908 909 910 911
}


int
qemuMonitorStopCPUs(qemuMonitorPtr mon)
{
D
Daniel P. Berrange 已提交
912
    int ret;
913 914
    DEBUG("mon=%p, fd=%d", mon, mon->fd);

D
Daniel P. Berrange 已提交
915 916 917 918 919
    if (mon->json)
        ret = qemuMonitorJSONStopCPUs(mon);
    else
        ret = qemuMonitorTextStopCPUs(mon);
    return ret;
920 921 922 923 924
}


int qemuMonitorSystemPowerdown(qemuMonitorPtr mon)
{
D
Daniel P. Berrange 已提交
925
    int ret;
926 927
    DEBUG("mon=%p, fd=%d", mon, mon->fd);

D
Daniel P. Berrange 已提交
928 929 930 931 932
    if (mon->json)
        ret = qemuMonitorJSONSystemPowerdown(mon);
    else
        ret = qemuMonitorTextSystemPowerdown(mon);
    return ret;
933 934 935 936 937 938
}


int qemuMonitorGetCPUInfo(qemuMonitorPtr mon,
                          int **pids)
{
D
Daniel P. Berrange 已提交
939
    int ret;
940 941
    DEBUG("mon=%p, fd=%d", mon, mon->fd);

D
Daniel P. Berrange 已提交
942 943 944 945 946
    if (mon->json)
        ret = qemuMonitorJSONGetCPUInfo(mon, pids);
    else
        ret = qemuMonitorTextGetCPUInfo(mon, pids);
    return ret;
947 948 949 950 951
}

int qemuMonitorGetBalloonInfo(qemuMonitorPtr mon,
                              unsigned long *currmem)
{
D
Daniel P. Berrange 已提交
952
    int ret;
953 954
    DEBUG("mon=%p, fd=%d", mon, mon->fd);

D
Daniel P. Berrange 已提交
955 956 957 958 959
    if (mon->json)
        ret = qemuMonitorJSONGetBalloonInfo(mon, currmem);
    else
        ret = qemuMonitorTextGetBalloonInfo(mon, currmem);
    return ret;
960 961 962 963 964 965 966 967 968 969 970
}


int qemuMonitorGetBlockStatsInfo(qemuMonitorPtr mon,
                                 const char *devname,
                                 long long *rd_req,
                                 long long *rd_bytes,
                                 long long *wr_req,
                                 long long *wr_bytes,
                                 long long *errs)
{
D
Daniel P. Berrange 已提交
971
    int ret;
972 973
    DEBUG("mon=%p, fd=%d dev=%s", mon, mon->fd, devname);

D
Daniel P. Berrange 已提交
974 975 976 977 978 979 980 981 982 983 984
    if (mon->json)
        ret = qemuMonitorJSONGetBlockStatsInfo(mon, devname,
                                               rd_req, rd_bytes,
                                               wr_req, wr_bytes,
                                               errs);
    else
        ret = qemuMonitorTextGetBlockStatsInfo(mon, devname,
                                               rd_req, rd_bytes,
                                               wr_req, wr_bytes,
                                               errs);
    return ret;
985 986 987 988 989 990
}


int qemuMonitorSetVNCPassword(qemuMonitorPtr mon,
                              const char *password)
{
D
Daniel P. Berrange 已提交
991
    int ret;
992 993
    DEBUG("mon=%p, fd=%d", mon, mon->fd);

994 995 996
    if (!password)
        password = "";

D
Daniel P. Berrange 已提交
997 998 999 1000 1001
    if (mon->json)
        ret = qemuMonitorJSONSetVNCPassword(mon, password);
    else
        ret = qemuMonitorTextSetVNCPassword(mon, password);
    return ret;
1002 1003 1004 1005 1006 1007
}


int qemuMonitorSetBalloon(qemuMonitorPtr mon,
                          unsigned long newmem)
{
D
Daniel P. Berrange 已提交
1008
    int ret;
1009 1010
    DEBUG("mon=%p, fd=%d newmem=%lu", mon, mon->fd, newmem);

D
Daniel P. Berrange 已提交
1011 1012 1013 1014 1015
    if (mon->json)
        ret = qemuMonitorJSONSetBalloon(mon, newmem);
    else
        ret = qemuMonitorTextSetBalloon(mon, newmem);
    return ret;
1016 1017
}

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

int qemuMonitorSetCPU(qemuMonitorPtr mon, int cpu, int online)
{
    int ret;
    DEBUG("mon=%p, fd=%d cpu=%d online=%d", mon, mon->fd, cpu, online);

    if (mon->json)
        ret = qemuMonitorJSONSetCPU(mon, cpu, online);
    else
        ret = qemuMonitorTextSetCPU(mon, cpu, online);
    return ret;
}


1032 1033 1034
int qemuMonitorEjectMedia(qemuMonitorPtr mon,
                          const char *devname)
{
D
Daniel P. Berrange 已提交
1035
    int ret;
1036 1037
    DEBUG("mon=%p, fd=%d devname=%s", mon, mon->fd, devname);

D
Daniel P. Berrange 已提交
1038 1039 1040 1041 1042
    if (mon->json)
        ret = qemuMonitorJSONEjectMedia(mon, devname);
    else
        ret = qemuMonitorTextEjectMedia(mon, devname);
    return ret;
1043 1044 1045 1046 1047
}


int qemuMonitorChangeMedia(qemuMonitorPtr mon,
                           const char *devname,
1048 1049
                           const char *newmedia,
                           const char *format)
1050
{
D
Daniel P. Berrange 已提交
1051
    int ret;
1052 1053
    DEBUG("mon=%p, fd=%d devname=%s newmedia=%s format=%s",
          mon, mon->fd, devname, newmedia, format);
1054

D
Daniel P. Berrange 已提交
1055 1056 1057 1058 1059
    if (mon->json)
        ret = qemuMonitorJSONChangeMedia(mon, devname, newmedia, format);
    else
        ret = qemuMonitorTextChangeMedia(mon, devname, newmedia, format);
    return ret;
1060 1061 1062 1063 1064 1065 1066 1067
}


int qemuMonitorSaveVirtualMemory(qemuMonitorPtr mon,
                                 unsigned long long offset,
                                 size_t length,
                                 const char *path)
{
D
Daniel P. Berrange 已提交
1068
    int ret;
1069 1070 1071
    DEBUG("mon=%p, fd=%d offset=%llu length=%zu path=%s",
          mon, mon->fd, offset, length, path);

D
Daniel P. Berrange 已提交
1072 1073 1074 1075 1076
    if (mon->json)
        ret = qemuMonitorJSONSaveVirtualMemory(mon, offset, length, path);
    else
        ret = qemuMonitorTextSaveVirtualMemory(mon, offset, length, path);
    return ret;
1077 1078 1079 1080 1081 1082 1083
}

int qemuMonitorSavePhysicalMemory(qemuMonitorPtr mon,
                                  unsigned long long offset,
                                  size_t length,
                                  const char *path)
{
D
Daniel P. Berrange 已提交
1084
    int ret;
1085 1086 1087
    DEBUG("mon=%p, fd=%d offset=%llu length=%zu path=%s",
          mon, mon->fd, offset, length, path);

D
Daniel P. Berrange 已提交
1088 1089 1090 1091 1092
    if (mon->json)
        ret = qemuMonitorJSONSavePhysicalMemory(mon, offset, length, path);
    else
        ret = qemuMonitorTextSavePhysicalMemory(mon, offset, length, path);
    return ret;
1093 1094 1095 1096 1097 1098
}


int qemuMonitorSetMigrationSpeed(qemuMonitorPtr mon,
                                 unsigned long bandwidth)
{
D
Daniel P. Berrange 已提交
1099
    int ret;
1100 1101
    DEBUG("mon=%p, fd=%d bandwidth=%lu", mon, mon->fd, bandwidth);

D
Daniel P. Berrange 已提交
1102 1103 1104 1105 1106
    if (mon->json)
        ret = qemuMonitorJSONSetMigrationSpeed(mon, bandwidth);
    else
        ret = qemuMonitorTextSetMigrationSpeed(mon, bandwidth);
    return ret;
1107 1108
}

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123

int qemuMonitorSetMigrationDowntime(qemuMonitorPtr mon,
                                    unsigned long long downtime)
{
    int ret;
    DEBUG("mon=%p, fd=%d downtime=%llu", mon, mon->fd, downtime);

    if (mon->json)
        ret = qemuMonitorJSONSetMigrationDowntime(mon, downtime);
    else
        ret = qemuMonitorTextSetMigrationDowntime(mon, downtime);
    return ret;
}


1124 1125 1126 1127 1128 1129
int qemuMonitorGetMigrationStatus(qemuMonitorPtr mon,
                                  int *status,
                                  unsigned long long *transferred,
                                  unsigned long long *remaining,
                                  unsigned long long *total)
{
D
Daniel P. Berrange 已提交
1130
    int ret;
1131 1132
    DEBUG("mon=%p, fd=%d", mon, mon->fd);

D
Daniel P. Berrange 已提交
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    if (mon->json)
        ret = qemuMonitorJSONGetMigrationStatus(mon, status,
                                                transferred,
                                                remaining,
                                                total);
    else
        ret = qemuMonitorTextGetMigrationStatus(mon, status,
                                                transferred,
                                                remaining,
                                                total);
    return ret;
1144 1145 1146 1147 1148 1149 1150 1151
}


int qemuMonitorMigrateToHost(qemuMonitorPtr mon,
                             int background,
                             const char *hostname,
                             int port)
{
D
Daniel P. Berrange 已提交
1152
    int ret;
1153 1154 1155
    DEBUG("mon=%p, fd=%d hostname=%s port=%d",
          mon, mon->fd, hostname, port);

D
Daniel P. Berrange 已提交
1156 1157 1158 1159 1160
    if (mon->json)
        ret = qemuMonitorJSONMigrateToHost(mon, background, hostname, port);
    else
        ret = qemuMonitorTextMigrateToHost(mon, background, hostname, port);
    return ret;
1161 1162 1163 1164 1165 1166 1167 1168
}


int qemuMonitorMigrateToCommand(qemuMonitorPtr mon,
                                int background,
                                const char * const *argv,
                                const char *target)
{
D
Daniel P. Berrange 已提交
1169
    int ret;
1170 1171 1172
    DEBUG("mon=%p, fd=%d argv=%p target=%s",
          mon, mon->fd, argv, target);

D
Daniel P. Berrange 已提交
1173 1174 1175 1176 1177
    if (mon->json)
        ret = qemuMonitorJSONMigrateToCommand(mon, background, argv, target);
    else
        ret = qemuMonitorTextMigrateToCommand(mon, background, argv, target);
    return ret;
1178 1179 1180 1181 1182 1183
}

int qemuMonitorMigrateToUnix(qemuMonitorPtr mon,
                             int background,
                             const char *unixfile)
{
D
Daniel P. Berrange 已提交
1184
    int ret;
1185 1186 1187
    DEBUG("mon=%p fd=%d unixfile=%s",
          mon, mon->fd, unixfile);

D
Daniel P. Berrange 已提交
1188 1189 1190 1191 1192
    if (mon->json)
        ret = qemuMonitorJSONMigrateToUnix(mon, background, unixfile);
    else
        ret = qemuMonitorTextMigrateToUnix(mon, background, unixfile);
    return ret;
1193 1194 1195 1196
}

int qemuMonitorMigrateCancel(qemuMonitorPtr mon)
{
D
Daniel P. Berrange 已提交
1197
    int ret;
1198 1199
    DEBUG("mon=%p fd=%d", mon, mon->fd);

D
Daniel P. Berrange 已提交
1200 1201 1202 1203 1204
    if (mon->json)
        ret = qemuMonitorJSONMigrateCancel(mon);
    else
        ret = qemuMonitorTextMigrateCancel(mon);
    return ret;
1205 1206 1207 1208 1209
}

int qemuMonitorAddUSBDisk(qemuMonitorPtr mon,
                          const char *path)
{
D
Daniel P. Berrange 已提交
1210
    int ret;
1211 1212
    DEBUG("mon=%p, fd=%d path=%s", mon, mon->fd, path);

D
Daniel P. Berrange 已提交
1213 1214 1215 1216 1217
    if (mon->json)
        ret = qemuMonitorJSONAddUSBDisk(mon, path);
    else
        ret = qemuMonitorTextAddUSBDisk(mon, path);
    return ret;
1218 1219 1220 1221 1222 1223 1224
}


int qemuMonitorAddUSBDeviceExact(qemuMonitorPtr mon,
                                 int bus,
                                 int dev)
{
D
Daniel P. Berrange 已提交
1225
    int ret;
1226 1227
    DEBUG("mon=%p, fd=%d bus=%d dev=%d", mon, mon->fd, bus, dev);

D
Daniel P. Berrange 已提交
1228 1229 1230 1231 1232
    if (mon->json)
        ret = qemuMonitorJSONAddUSBDeviceExact(mon, bus, dev);
    else
        ret = qemuMonitorTextAddUSBDeviceExact(mon, bus, dev);
    return ret;
1233 1234 1235 1236 1237 1238
}

int qemuMonitorAddUSBDeviceMatch(qemuMonitorPtr mon,
                                 int vendor,
                                 int product)
{
D
Daniel P. Berrange 已提交
1239
    int ret;
1240 1241 1242
    DEBUG("mon=%p, fd=%d vendor=%d product=%d",
          mon, mon->fd, vendor, product);

D
Daniel P. Berrange 已提交
1243 1244 1245 1246 1247
    if (mon->json)
        ret = qemuMonitorJSONAddUSBDeviceMatch(mon, vendor, product);
    else
        ret = qemuMonitorTextAddUSBDeviceMatch(mon, vendor, product);
    return ret;
1248 1249 1250 1251
}


int qemuMonitorAddPCIHostDevice(qemuMonitorPtr mon,
1252 1253
                                virDomainDevicePCIAddress *hostAddr,
                                virDomainDevicePCIAddress *guestAddr)
1254
{
D
Daniel P. Berrange 已提交
1255
    int ret;
1256 1257
    DEBUG("mon=%p, fd=%d domain=%d bus=%d slot=%d function=%d",
          mon, mon->fd,
1258
          hostAddr->domain, hostAddr->bus, hostAddr->slot, hostAddr->function);
1259

D
Daniel P. Berrange 已提交
1260
    if (mon->json)
1261
        ret = qemuMonitorJSONAddPCIHostDevice(mon, hostAddr, guestAddr);
D
Daniel P. Berrange 已提交
1262
    else
1263
        ret = qemuMonitorTextAddPCIHostDevice(mon, hostAddr, guestAddr);
D
Daniel P. Berrange 已提交
1264
    return ret;
1265 1266 1267 1268 1269 1270
}


int qemuMonitorAddPCIDisk(qemuMonitorPtr mon,
                          const char *path,
                          const char *bus,
1271
                          virDomainDevicePCIAddress *guestAddr)
1272
{
D
Daniel P. Berrange 已提交
1273
    int ret;
1274 1275 1276
    DEBUG("mon=%p, fd=%d path=%s bus=%s",
          mon, mon->fd, path, bus);

D
Daniel P. Berrange 已提交
1277
    if (mon->json)
1278
        ret = qemuMonitorJSONAddPCIDisk(mon, path, bus, guestAddr);
D
Daniel P. Berrange 已提交
1279
    else
1280
        ret = qemuMonitorTextAddPCIDisk(mon, path, bus, guestAddr);
D
Daniel P. Berrange 已提交
1281
    return ret;
1282 1283 1284 1285 1286
}


int qemuMonitorAddPCINetwork(qemuMonitorPtr mon,
                             const char *nicstr,
1287
                             virDomainDevicePCIAddress *guestAddr)
1288
{
D
Daniel P. Berrange 已提交
1289
    int ret;
1290 1291
    DEBUG("mon=%p, fd=%d nicstr=%s", mon, mon->fd, nicstr);

D
Daniel P. Berrange 已提交
1292
    if (mon->json)
1293
        ret = qemuMonitorJSONAddPCINetwork(mon, nicstr, guestAddr);
D
Daniel P. Berrange 已提交
1294
    else
1295
        ret = qemuMonitorTextAddPCINetwork(mon, nicstr, guestAddr);
D
Daniel P. Berrange 已提交
1296
    return ret;
1297 1298 1299 1300
}


int qemuMonitorRemovePCIDevice(qemuMonitorPtr mon,
1301
                               virDomainDevicePCIAddress *guestAddr)
1302
{
D
Daniel P. Berrange 已提交
1303
    int ret;
1304 1305 1306
    DEBUG("mon=%p, fd=%d domain=%d bus=%d slot=%d function=%d",
          mon, mon->fd, guestAddr->domain, guestAddr->bus,
          guestAddr->slot, guestAddr->function);
1307

D
Daniel P. Berrange 已提交
1308
    if (mon->json)
1309
        ret = qemuMonitorJSONRemovePCIDevice(mon, guestAddr);
D
Daniel P. Berrange 已提交
1310
    else
1311
        ret = qemuMonitorTextRemovePCIDevice(mon, guestAddr);
D
Daniel P. Berrange 已提交
1312
    return ret;
1313 1314 1315 1316 1317 1318 1319
}


int qemuMonitorSendFileHandle(qemuMonitorPtr mon,
                              const char *fdname,
                              int fd)
{
D
Daniel P. Berrange 已提交
1320
    int ret;
1321 1322 1323
    DEBUG("mon=%p, fd=%d fdname=%s fd=%d",
          mon, mon->fd, fdname, fd);

D
Daniel P. Berrange 已提交
1324 1325 1326 1327 1328
    if (mon->json)
        ret = qemuMonitorJSONSendFileHandle(mon, fdname, fd);
    else
        ret = qemuMonitorTextSendFileHandle(mon, fdname, fd);
    return ret;
1329 1330 1331 1332 1333 1334
}


int qemuMonitorCloseFileHandle(qemuMonitorPtr mon,
                               const char *fdname)
{
D
Daniel P. Berrange 已提交
1335
    int ret;
1336 1337 1338
    DEBUG("mon=%p, fd=%d fdname=%s",
          mon, mon->fd, fdname);

D
Daniel P. Berrange 已提交
1339 1340 1341 1342 1343
    if (mon->json)
        ret = qemuMonitorJSONCloseFileHandle(mon, fdname);
    else
        ret = qemuMonitorTextCloseFileHandle(mon, fdname);
    return ret;
1344 1345 1346 1347 1348 1349
}


int qemuMonitorAddHostNetwork(qemuMonitorPtr mon,
                              const char *netstr)
{
D
Daniel P. Berrange 已提交
1350
    int ret;
1351 1352 1353
    DEBUG("mon=%p, fd=%d netstr=%s",
          mon, mon->fd, netstr);

D
Daniel P. Berrange 已提交
1354 1355 1356 1357 1358
    if (mon->json)
        ret = qemuMonitorJSONAddHostNetwork(mon, netstr);
    else
        ret = qemuMonitorTextAddHostNetwork(mon, netstr);
    return ret;
1359 1360 1361 1362 1363 1364 1365
}


int qemuMonitorRemoveHostNetwork(qemuMonitorPtr mon,
                                 int vlan,
                                 const char *netname)
{
D
Daniel P. Berrange 已提交
1366
    int ret;
1367 1368 1369
    DEBUG("mon=%p, fd=%d netname=%s",
          mon, mon->fd, netname);

D
Daniel P. Berrange 已提交
1370 1371 1372 1373 1374
    if (mon->json)
        ret = qemuMonitorJSONRemoveHostNetwork(mon, vlan, netname);
    else
        ret = qemuMonitorTextRemoveHostNetwork(mon, vlan, netname);
    return ret;
1375
}
1376 1377 1378 1379

int qemuMonitorGetPtyPaths(qemuMonitorPtr mon,
                           virHashTablePtr paths)
{
1380
    int ret;
1381 1382 1383
    DEBUG("mon=%p, fd=%d",
          mon, mon->fd);

1384 1385 1386 1387 1388
    if (mon->json)
        ret = qemuMonitorJSONGetPtyPaths(mon, paths);
    else
        ret = qemuMonitorTextGetPtyPaths(mon, paths);
    return ret;
1389
}
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405


int qemuMonitorAttachPCIDiskController(qemuMonitorPtr mon,
                                       const char *bus,
                                       virDomainDevicePCIAddress *guestAddr)
{
    DEBUG("mon=%p, fd=%d type=%s", mon, mon->fd, bus);
    int ret;

    if (mon->json)
        ret = qemuMonitorJSONAttachPCIDiskController(mon, bus, guestAddr);
    else
        ret = qemuMonitorTextAttachPCIDiskController(mon, bus, guestAddr);

    return ret;
}
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425


int qemuMonitorAttachDrive(qemuMonitorPtr mon,
                           const char *drivestr,
                           virDomainDevicePCIAddress *controllerAddr,
                           virDomainDeviceDriveAddress *driveAddr)
{
    DEBUG("mon=%p, fd=%d drivestr=%s domain=%d bus=%d slot=%d function=%d",
          mon, mon->fd, drivestr,
          controllerAddr->domain, controllerAddr->bus,
          controllerAddr->slot, controllerAddr->function);
    int ret;

    if (mon->json)
        ret = qemuMonitorJSONAttachDrive(mon, drivestr, controllerAddr, driveAddr);
    else
        ret = qemuMonitorTextAttachDrive(mon, drivestr, controllerAddr, driveAddr);

    return ret;
}
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438

int qemuMonitorGetAllPCIAddresses(qemuMonitorPtr mon,
                                  qemuMonitorPCIAddress **addrs)
{
    DEBUG("mon=%p, fd=%d addrs=%p", mon, mon->fd, addrs);
    int ret;

    if (mon->json)
        ret = qemuMonitorJSONGetAllPCIAddresses(mon, addrs);
    else
        ret = qemuMonitorTextGetAllPCIAddresses(mon, addrs);
    return ret;
}
1439

1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
int qemuMonitorDelDevice(qemuMonitorPtr mon,
                         const char *devicestr)
{
    DEBUG("mon=%p, fd=%d device(del)=%s", mon, mon->fd, devicestr);
    int ret;

    if (mon->json)
        ret = qemuMonitorJSONDelDevice(mon, devicestr);
    else
        ret = qemuMonitorTextDelDevice(mon, devicestr);
    return ret;
}

1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478

int qemuMonitorAddDevice(qemuMonitorPtr mon,
                         const char *devicestr)
{
    DEBUG("mon=%p, fd=%d device=%s", mon, mon->fd, devicestr);
    int ret;

    if (mon->json)
        ret = qemuMonitorJSONAddDevice(mon, devicestr);
    else
        ret = qemuMonitorTextAddDevice(mon, devicestr);
    return ret;
}

int qemuMonitorAddDrive(qemuMonitorPtr mon,
                        const char *drivestr)
{
    DEBUG("mon=%p, fd=%d drive=%s", mon, mon->fd, drivestr);
    int ret;

    if (mon->json)
        ret = qemuMonitorJSONAddDrive(mon, drivestr);
    else
        ret = qemuMonitorTextAddDrive(mon, drivestr);
    return ret;
}
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493


int qemuMonitorSetDrivePassphrase(qemuMonitorPtr mon,
                                  const char *alias,
                                  const char *passphrase)
{
    DEBUG("mon=%p, fd=%d alias=%s passphrase=%p(value hidden)", mon, mon->fd, alias, passphrase);
    int ret;

    if (mon->json)
        ret = qemuMonitorJSONSetDrivePassphrase(mon, alias, passphrase);
    else
        ret = qemuMonitorTextSetDrivePassphrase(mon, alias, passphrase);
    return ret;
}