qemu_monitor.c 34.9 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 QEMU_DEBUG_RAW_IO 0

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

48
    int refs;
49

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

    virDomainObjPtr vm;

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

    /* 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;

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

D
Daniel P. Berrange 已提交
77
    unsigned json: 1;
78 79
};

80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
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);
}


168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
#if QEMU_DEBUG_RAW_IO
#include <c-ctype.h>
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

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

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

196

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

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
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;
}

225 226

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

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

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

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

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

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

272
    return monfd;
273 274 275 276 277 278 279

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

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

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

290
    return monfd;
291
}
292

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

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;

305 306 307 308 309 310 311
#if QEMU_DEBUG_RAW_IO
    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
312
    VIR_DEBUG("Process %d", (int)mon->bufferOffset);
313
#endif
D
Daniel P. Berrange 已提交
314 315 316 317 318 319 320 321
    if (mon->json)
        len = qemuMonitorJSONIOProcess(mon,
                                       mon->buffer, mon->bufferOffset,
                                       msg);
    else
        len = qemuMonitorTextIOProcess(mon,
                                       mon->buffer, mon->bufferOffset,
                                       msg);
322 323 324 325 326 327 328 329 330 331 332 333 334

    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;
    }
335
    VIR_DEBUG("Process done %d used %d", (int)mon->bufferOffset, len);
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    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';
    }

458
    VIR_DEBUG("Now read %d bytes of data", (int)mon->bufferOffset);
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477

    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);
478 479
}

480 481 482 483 484 485

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

486
    qemuMonitorLock(mon);
487
    qemuMonitorRef(mon);
488 489
    VIR_DEBUG("Monitor %p I/O on watch %d fd %d events %d", mon, watch, fd, events);

490
    if (mon->fd != fd || mon->watch != watch) {
491
        VIR_ERROR("event from unexpected fd %d!=%d / watch %d!=%d", mon->fd, fd, mon->watch, watch);
492 493
        failed = 1;
    } else {
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
        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);
            }
535
            quit = 1;
536
        } else if (events) {
537 538 539 540 541 542
            VIR_ERROR(_("unhandled fd event %d for monitor fd %d"),
                      events, mon->fd);
            failed = 1;
        }
    }

543 544 545 546
    /* 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) {
547 548
        void (*eofNotify)(qemuMonitorPtr, virDomainObjPtr, int)
            = mon->cb->eofNotify;
549
        virDomainObjPtr vm = mon->vm;
550 551
        /* Make sure anyone waiting wakes up now */
        virCondSignal(&mon->notify);
552
        if (qemuMonitorUnref(mon) > 0)
553
            qemuMonitorUnlock(mon);
554
        VIR_DEBUG("Triggering EOF callback error? %d", failed);
555
        (eofNotify)(mon, vm, failed);
556
    } else {
557 558
        if (qemuMonitorUnref(mon) > 0)
            qemuMonitorUnlock(mon);
559
    }
560 561 562 563
}


qemuMonitorPtr
564
qemuMonitorOpen(virDomainObjPtr vm,
565
                virDomainChrDefPtr config,
D
Daniel P. Berrange 已提交
566
                int json,
567
                qemuMonitorCallbacksPtr cb)
568
{
569 570
    qemuMonitorPtr mon;

571
    if (!cb || !cb->eofNotify) {
572 573
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("EOF notify callback must be supplied"));
574 575 576
        return NULL;
    }

577
    if (VIR_ALLOC(mon) < 0) {
578
        virReportOOMError();
579 580 581
        return NULL;
    }

582
    if (virMutexInit(&mon->lock) < 0) {
583 584
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("cannot initialize monitor mutex"));
585 586 587
        VIR_FREE(mon);
        return NULL;
    }
588
    if (virCondInit(&mon->notify) < 0) {
589 590
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("cannot initialize monitor condition"));
591 592 593 594
        virMutexDestroy(&mon->lock);
        VIR_FREE(mon);
        return NULL;
    }
595
    mon->fd = -1;
596
    mon->refs = 1;
597
    mon->vm = vm;
D
Daniel P. Berrange 已提交
598
    mon->json = json;
599
    mon->cb = cb;
600
    qemuMonitorLock(mon);
601

602
    switch (config->type) {
603
    case VIR_DOMAIN_CHR_TYPE_UNIX:
604
        mon->hasSendFD = 1;
605
        mon->fd = qemuMonitorOpenUnix(config->data.nix.path);
606 607
        break;

608
    case VIR_DOMAIN_CHR_TYPE_PTY:
609
        mon->fd = qemuMonitorOpenPty(config->data.file.path);
610 611
        break;

612
    default:
613 614 615
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        _("unable to handle monitor type: %s"),
                        virDomainChrTypeToString(config->type));
616 617 618
        goto cleanup;
    }

619 620
    if (mon->fd == -1) goto cleanup;

621
    if (virSetCloseExec(mon->fd) < 0) {
622 623
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("Unable to set monitor close-on-exec flag"));
624 625 626
        goto cleanup;
    }
    if (virSetNonBlock(mon->fd) < 0) {
627 628
        qemuReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("Unable to put monitor into non-blocking mode"));
629 630 631 632
        goto cleanup;
    }


633
    if ((mon->watch = virEventAddHandle(mon->fd,
634 635 636
                                        VIR_EVENT_HANDLE_HANGUP |
                                        VIR_EVENT_HANDLE_ERROR |
                                        VIR_EVENT_HANDLE_READABLE,
637 638
                                        qemuMonitorIO,
                                        mon, NULL)) < 0) {
639 640
        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("unable to register monitor events"));
641 642 643
        goto cleanup;
    }

644 645
    VIR_DEBUG("New mon %p fd =%d watch=%d", mon, mon->fd, mon->watch);
    qemuMonitorUnlock(mon);
646

647 648 649
    return mon;

cleanup:
650
    qemuMonitorUnlock(mon);
651 652 653 654 655
    qemuMonitorClose(mon);
    return NULL;
}


656
int qemuMonitorClose(qemuMonitorPtr mon)
657
{
658 659
    int refs;

660
    if (!mon)
661 662 663
        return 0;

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

665 666 667 668 669 670
    qemuMonitorLock(mon);
    if (!mon->closed) {
        if (mon->watch)
            virEventRemoveHandle(mon->watch);
        if (mon->fd != -1)
            close(mon->fd);
671 672 673 674 675
        /* 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 */
676 677
        mon->closed = 1;
    }
678

679
    if ((refs = qemuMonitorUnref(mon)) > 0)
680
        qemuMonitorUnlock(mon);
681
    return refs;
682 683 684
}


685 686
int qemuMonitorSend(qemuMonitorPtr mon,
                    qemuMonitorMessagePtr msg)
687
{
688
    int ret = -1;
689

690 691
    if (mon->eofcb) {
        msg->lastErrno = EIO;
692 693
        return -1;
    }
694

695 696
    mon->msg = msg;
    qemuMonitorUpdateWatch(mon);
697

698 699 700 701
    while (!mon->msg->finished) {
        if (virCondWait(&mon->notify, &mon->lock) < 0)
            goto cleanup;
    }
702

703 704
    if (mon->lastErrno == 0)
        ret = 0;
705

706 707 708
cleanup:
    mon->msg = NULL;
    qemuMonitorUpdateWatch(mon);
709

710
    return ret;
711
}
712 713 714 715 716 717 718 719


int qemuMonitorGetDiskSecret(qemuMonitorPtr mon,
                             virConnectPtr conn,
                             const char *path,
                             char **secret,
                             size_t *secretLen)
{
720
    int ret = -1;
721 722 723
    *secret = NULL;
    *secretLen = 0;

724 725 726 727 728 729 730
    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;
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 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
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;
}


794 795 796 797
int
qemuMonitorStartCPUs(qemuMonitorPtr mon,
                     virConnectPtr conn)
{
D
Daniel P. Berrange 已提交
798
    int ret;
799 800
    DEBUG("mon=%p, fd=%d", mon, mon->fd);

D
Daniel P. Berrange 已提交
801 802 803 804 805
    if (mon->json)
        ret = qemuMonitorJSONStartCPUs(mon, conn);
    else
        ret = qemuMonitorTextStartCPUs(mon, conn);
    return ret;
806 807 808 809 810 811
}


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

D
Daniel P. Berrange 已提交
815 816 817 818 819
    if (mon->json)
        ret = qemuMonitorJSONStopCPUs(mon);
    else
        ret = qemuMonitorTextStopCPUs(mon);
    return ret;
820 821 822 823 824
}


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

D
Daniel P. Berrange 已提交
828 829 830 831 832
    if (mon->json)
        ret = qemuMonitorJSONSystemPowerdown(mon);
    else
        ret = qemuMonitorTextSystemPowerdown(mon);
    return ret;
833 834 835 836 837 838
}


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

D
Daniel P. Berrange 已提交
842 843 844 845 846
    if (mon->json)
        ret = qemuMonitorJSONGetCPUInfo(mon, pids);
    else
        ret = qemuMonitorTextGetCPUInfo(mon, pids);
    return ret;
847 848 849 850 851
}

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

D
Daniel P. Berrange 已提交
855 856 857 858 859
    if (mon->json)
        ret = qemuMonitorJSONGetBalloonInfo(mon, currmem);
    else
        ret = qemuMonitorTextGetBalloonInfo(mon, currmem);
    return ret;
860 861 862 863 864 865 866 867 868 869 870
}


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 已提交
871
    int ret;
872 873
    DEBUG("mon=%p, fd=%d dev=%s", mon, mon->fd, devname);

D
Daniel P. Berrange 已提交
874 875 876 877 878 879 880 881 882 883 884
    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;
885 886 887 888 889 890
}


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

D
Daniel P. Berrange 已提交
894 895 896 897 898
    if (mon->json)
        ret = qemuMonitorJSONSetVNCPassword(mon, password);
    else
        ret = qemuMonitorTextSetVNCPassword(mon, password);
    return ret;
899 900 901 902 903 904
}


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

D
Daniel P. Berrange 已提交
908 909 910 911 912
    if (mon->json)
        ret = qemuMonitorJSONSetBalloon(mon, newmem);
    else
        ret = qemuMonitorTextSetBalloon(mon, newmem);
    return ret;
913 914 915 916 917
}

int qemuMonitorEjectMedia(qemuMonitorPtr mon,
                          const char *devname)
{
D
Daniel P. Berrange 已提交
918
    int ret;
919 920
    DEBUG("mon=%p, fd=%d devname=%s", mon, mon->fd, devname);

D
Daniel P. Berrange 已提交
921 922 923 924 925
    if (mon->json)
        ret = qemuMonitorJSONEjectMedia(mon, devname);
    else
        ret = qemuMonitorTextEjectMedia(mon, devname);
    return ret;
926 927 928 929 930
}


int qemuMonitorChangeMedia(qemuMonitorPtr mon,
                           const char *devname,
931 932
                           const char *newmedia,
                           const char *format)
933
{
D
Daniel P. Berrange 已提交
934
    int ret;
935 936
    DEBUG("mon=%p, fd=%d devname=%s newmedia=%s format=%s",
          mon, mon->fd, devname, newmedia, format);
937

D
Daniel P. Berrange 已提交
938 939 940 941 942
    if (mon->json)
        ret = qemuMonitorJSONChangeMedia(mon, devname, newmedia, format);
    else
        ret = qemuMonitorTextChangeMedia(mon, devname, newmedia, format);
    return ret;
943 944 945 946 947 948 949 950
}


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

D
Daniel P. Berrange 已提交
955 956 957 958 959
    if (mon->json)
        ret = qemuMonitorJSONSaveVirtualMemory(mon, offset, length, path);
    else
        ret = qemuMonitorTextSaveVirtualMemory(mon, offset, length, path);
    return ret;
960 961 962 963 964 965 966
}

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

D
Daniel P. Berrange 已提交
971 972 973 974 975
    if (mon->json)
        ret = qemuMonitorJSONSavePhysicalMemory(mon, offset, length, path);
    else
        ret = qemuMonitorTextSavePhysicalMemory(mon, offset, length, path);
    return ret;
976 977 978 979 980 981
}


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

D
Daniel P. Berrange 已提交
985 986 987 988 989
    if (mon->json)
        ret = qemuMonitorJSONSetMigrationSpeed(mon, bandwidth);
    else
        ret = qemuMonitorTextSetMigrationSpeed(mon, bandwidth);
    return ret;
990 991 992 993 994 995 996 997
}

int qemuMonitorGetMigrationStatus(qemuMonitorPtr mon,
                                  int *status,
                                  unsigned long long *transferred,
                                  unsigned long long *remaining,
                                  unsigned long long *total)
{
D
Daniel P. Berrange 已提交
998
    int ret;
999 1000
    DEBUG("mon=%p, fd=%d", mon, mon->fd);

D
Daniel P. Berrange 已提交
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    if (mon->json)
        ret = qemuMonitorJSONGetMigrationStatus(mon, status,
                                                transferred,
                                                remaining,
                                                total);
    else
        ret = qemuMonitorTextGetMigrationStatus(mon, status,
                                                transferred,
                                                remaining,
                                                total);
    return ret;
1012 1013 1014 1015 1016 1017 1018 1019
}


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

D
Daniel P. Berrange 已提交
1024 1025 1026 1027 1028
    if (mon->json)
        ret = qemuMonitorJSONMigrateToHost(mon, background, hostname, port);
    else
        ret = qemuMonitorTextMigrateToHost(mon, background, hostname, port);
    return ret;
1029 1030 1031 1032 1033 1034 1035 1036
}


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

D
Daniel P. Berrange 已提交
1041 1042 1043 1044 1045
    if (mon->json)
        ret = qemuMonitorJSONMigrateToCommand(mon, background, argv, target);
    else
        ret = qemuMonitorTextMigrateToCommand(mon, background, argv, target);
    return ret;
1046 1047 1048 1049 1050 1051
}

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

D
Daniel P. Berrange 已提交
1056 1057 1058 1059 1060
    if (mon->json)
        ret = qemuMonitorJSONMigrateToUnix(mon, background, unixfile);
    else
        ret = qemuMonitorTextMigrateToUnix(mon, background, unixfile);
    return ret;
1061 1062 1063 1064
}

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

D
Daniel P. Berrange 已提交
1068 1069 1070 1071 1072
    if (mon->json)
        ret = qemuMonitorJSONMigrateCancel(mon);
    else
        ret = qemuMonitorTextMigrateCancel(mon);
    return ret;
1073 1074 1075 1076 1077
}

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

D
Daniel P. Berrange 已提交
1081 1082 1083 1084 1085
    if (mon->json)
        ret = qemuMonitorJSONAddUSBDisk(mon, path);
    else
        ret = qemuMonitorTextAddUSBDisk(mon, path);
    return ret;
1086 1087 1088 1089 1090 1091 1092
}


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

D
Daniel P. Berrange 已提交
1096 1097 1098 1099 1100
    if (mon->json)
        ret = qemuMonitorJSONAddUSBDeviceExact(mon, bus, dev);
    else
        ret = qemuMonitorTextAddUSBDeviceExact(mon, bus, dev);
    return ret;
1101 1102 1103 1104 1105 1106
}

int qemuMonitorAddUSBDeviceMatch(qemuMonitorPtr mon,
                                 int vendor,
                                 int product)
{
D
Daniel P. Berrange 已提交
1107
    int ret;
1108 1109 1110
    DEBUG("mon=%p, fd=%d vendor=%d product=%d",
          mon, mon->fd, vendor, product);

D
Daniel P. Berrange 已提交
1111 1112 1113 1114 1115
    if (mon->json)
        ret = qemuMonitorJSONAddUSBDeviceMatch(mon, vendor, product);
    else
        ret = qemuMonitorTextAddUSBDeviceMatch(mon, vendor, product);
    return ret;
1116 1117 1118 1119
}


int qemuMonitorAddPCIHostDevice(qemuMonitorPtr mon,
1120 1121
                                virDomainDevicePCIAddress *hostAddr,
                                virDomainDevicePCIAddress *guestAddr)
1122
{
D
Daniel P. Berrange 已提交
1123
    int ret;
1124 1125
    DEBUG("mon=%p, fd=%d domain=%d bus=%d slot=%d function=%d",
          mon, mon->fd,
1126
          hostAddr->domain, hostAddr->bus, hostAddr->slot, hostAddr->function);
1127

D
Daniel P. Berrange 已提交
1128
    if (mon->json)
1129
        ret = qemuMonitorJSONAddPCIHostDevice(mon, hostAddr, guestAddr);
D
Daniel P. Berrange 已提交
1130
    else
1131
        ret = qemuMonitorTextAddPCIHostDevice(mon, hostAddr, guestAddr);
D
Daniel P. Berrange 已提交
1132
    return ret;
1133 1134 1135 1136 1137 1138
}


int qemuMonitorAddPCIDisk(qemuMonitorPtr mon,
                          const char *path,
                          const char *bus,
1139
                          virDomainDevicePCIAddress *guestAddr)
1140
{
D
Daniel P. Berrange 已提交
1141
    int ret;
1142 1143 1144
    DEBUG("mon=%p, fd=%d path=%s bus=%s",
          mon, mon->fd, path, bus);

D
Daniel P. Berrange 已提交
1145
    if (mon->json)
1146
        ret = qemuMonitorJSONAddPCIDisk(mon, path, bus, guestAddr);
D
Daniel P. Berrange 已提交
1147
    else
1148
        ret = qemuMonitorTextAddPCIDisk(mon, path, bus, guestAddr);
D
Daniel P. Berrange 已提交
1149
    return ret;
1150 1151 1152 1153 1154
}


int qemuMonitorAddPCINetwork(qemuMonitorPtr mon,
                             const char *nicstr,
1155
                             virDomainDevicePCIAddress *guestAddr)
1156
{
D
Daniel P. Berrange 已提交
1157
    int ret;
1158 1159
    DEBUG("mon=%p, fd=%d nicstr=%s", mon, mon->fd, nicstr);

D
Daniel P. Berrange 已提交
1160
    if (mon->json)
1161
        ret = qemuMonitorJSONAddPCINetwork(mon, nicstr, guestAddr);
D
Daniel P. Berrange 已提交
1162
    else
1163
        ret = qemuMonitorTextAddPCINetwork(mon, nicstr, guestAddr);
D
Daniel P. Berrange 已提交
1164
    return ret;
1165 1166 1167 1168
}


int qemuMonitorRemovePCIDevice(qemuMonitorPtr mon,
1169
                               virDomainDevicePCIAddress *guestAddr)
1170
{
D
Daniel P. Berrange 已提交
1171
    int ret;
1172 1173 1174
    DEBUG("mon=%p, fd=%d domain=%d bus=%d slot=%d function=%d",
          mon, mon->fd, guestAddr->domain, guestAddr->bus,
          guestAddr->slot, guestAddr->function);
1175

D
Daniel P. Berrange 已提交
1176
    if (mon->json)
1177
        ret = qemuMonitorJSONRemovePCIDevice(mon, guestAddr);
D
Daniel P. Berrange 已提交
1178
    else
1179
        ret = qemuMonitorTextRemovePCIDevice(mon, guestAddr);
D
Daniel P. Berrange 已提交
1180
    return ret;
1181 1182 1183 1184 1185 1186 1187
}


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

D
Daniel P. Berrange 已提交
1192 1193 1194 1195 1196
    if (mon->json)
        ret = qemuMonitorJSONSendFileHandle(mon, fdname, fd);
    else
        ret = qemuMonitorTextSendFileHandle(mon, fdname, fd);
    return ret;
1197 1198 1199 1200 1201 1202
}


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

D
Daniel P. Berrange 已提交
1207 1208 1209 1210 1211
    if (mon->json)
        ret = qemuMonitorJSONCloseFileHandle(mon, fdname);
    else
        ret = qemuMonitorTextCloseFileHandle(mon, fdname);
    return ret;
1212 1213 1214 1215 1216 1217
}


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

D
Daniel P. Berrange 已提交
1222 1223 1224 1225 1226
    if (mon->json)
        ret = qemuMonitorJSONAddHostNetwork(mon, netstr);
    else
        ret = qemuMonitorTextAddHostNetwork(mon, netstr);
    return ret;
1227 1228 1229 1230 1231 1232 1233
}


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

D
Daniel P. Berrange 已提交
1238 1239 1240 1241 1242
    if (mon->json)
        ret = qemuMonitorJSONRemoveHostNetwork(mon, vlan, netname);
    else
        ret = qemuMonitorTextRemoveHostNetwork(mon, vlan, netname);
    return ret;
1243
}
1244 1245 1246 1247

int qemuMonitorGetPtyPaths(qemuMonitorPtr mon,
                           virHashTablePtr paths)
{
1248
    int ret;
1249 1250 1251
    DEBUG("mon=%p, fd=%d",
          mon, mon->fd);

1252 1253 1254 1255 1256
    if (mon->json)
        ret = qemuMonitorJSONGetPtyPaths(mon, paths);
    else
        ret = qemuMonitorTextGetPtyPaths(mon, paths);
    return ret;
1257
}
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273


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;
}
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293


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;
}
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306

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;
}
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333


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;
}
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348


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