remote.c 52.8 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
/*
 * remote.c: code handling remote requests (from remote_internal.c)
 *
 * Copyright (C) 2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Richard W.M. Jones <rjones@redhat.com>
 */

#include <config.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <paths.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/poll.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <pwd.h>
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <ctype.h>
#include <assert.h>

#include <libvirt/virterror.h>

#include "internal.h"
#include "../src/internal.h"

#define DEBUG 0

static void remoteDispatchError (struct qemud_client *client,
                                 remote_message_header *req,
                                 const char *fmt, ...);
static virDomainPtr get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network (virConnectPtr conn, remote_nonnull_network network);
static void make_nonnull_domain (remote_nonnull_domain *dom_dst, virDomainPtr dom_src);
static void make_nonnull_network (remote_nonnull_network *net_dst, virNetworkPtr net_src);

#include "remote_dispatch_prototypes.h"

typedef int (*dispatch_fn) (struct qemud_client *client, remote_message_header *req, char *args, char *ret);

/* This function gets called from qemud when it detects an incoming
 * remote protocol message.  At this point, client->buffer contains
 * the full call message (including length word which we skip).
 */
void
remoteDispatchClientRequest (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client)
{
    XDR xdr;
    remote_message_header req, rep;
    dispatch_fn fn;
    xdrproc_t args_filter = (xdrproc_t) xdr_void;
    xdrproc_t ret_filter = (xdrproc_t) xdr_void;
    char *args = NULL, *ret = NULL;
    int rv, len;

#include "remote_dispatch_localvars.h"

    /* Parse the header. */
    xdrmem_create (&xdr, client->buffer+4, client->bufferLength-4, XDR_DECODE);

    if (!xdr_remote_message_header (&xdr, &req)) {
        remoteDispatchError (client, NULL, "xdr_remote_message_header");
        xdr_destroy (&xdr);
        return;
    }

    /* Check version, etc. */
    if (req.prog != REMOTE_PROGRAM) {
        remoteDispatchError (client, &req, "program mismatch (actual %x, expected %x)",
                             req.prog, REMOTE_PROGRAM);
        xdr_destroy (&xdr);
        return;
    }
    if (req.vers != REMOTE_PROTOCOL_VERSION) {
        remoteDispatchError (client, &req, "version mismatch (actual %x, expected %x)",
                             req.vers, REMOTE_PROTOCOL_VERSION);
        xdr_destroy (&xdr);
        return;
    }
    if (req.direction != REMOTE_CALL) {
        remoteDispatchError (client, &req, "direction (%d) != REMOTE_CALL",
                             (int) req.direction);
        xdr_destroy (&xdr);
        return;
    }
    if (req.status != REMOTE_OK) {
        remoteDispatchError (client, &req, "status (%d) != REMOTE_OK",
                             (int) req.status);
        xdr_destroy (&xdr);
        return;
    }

    /* Based on the procedure number, dispatch.  In future we may base
     * this on the version number as well.
     */
    switch (req.proc) {
#include "remote_dispatch_proc_switch.h"

    default:
        remoteDispatchError (client, &req, "unknown procedure: %d",
                             req.proc);
        xdr_destroy (&xdr);
        return;
    }

    /* Parse args. */
    if (!(*args_filter) (&xdr, args)) {
        remoteDispatchError (client, &req, "parse args failed");
        xdr_destroy (&xdr);
        return;
    }

    xdr_destroy (&xdr);

    /* Call function. */
    rv = fn (client, &req, args, ret);
    xdr_free (args_filter, args);

    /* Dispatch function must return -2, -1 or 0.  Anything else is
     * an internal error.
     */
    if (rv < -2 || rv > 0) {
        remoteDispatchError (client, &req, "internal error - dispatch function returned invalid code %d", rv);
        return;
    }

    /* Dispatch error?  If so then the function has already set up the
     * return buffer, so just return immediately.
     */
    if (rv == -2) return;

    /* Return header. */
    rep.prog = req.prog;
    rep.vers = req.vers;
    rep.proc = req.proc;
    rep.direction = REMOTE_REPLY;
    rep.serial = req.serial;
    rep.status = rv == 0 ? REMOTE_OK : REMOTE_ERROR;

    /* Serialise the return header. */
    xdrmem_create (&xdr, client->buffer, sizeof client->buffer, XDR_ENCODE);

    len = 0; /* We'll come back and write this later. */
    if (!xdr_int (&xdr, &len)) {
        remoteDispatchError (client, &req, "dummy length");
        xdr_destroy (&xdr);
        if (rv == 0) xdr_free (ret_filter, ret);
        return;
    }

    if (!xdr_remote_message_header (&xdr, &rep)) {
        remoteDispatchError (client, &req, "serialise reply header");
        xdr_destroy (&xdr);
        if (rv == 0) xdr_free (ret_filter, ret);
        return;
    }

    /* If OK, serialise return structure, if error serialise error. */
    if (rv == 0) {
        if (!(*ret_filter) (&xdr, ret)) {
            remoteDispatchError (client, &req, "serialise return struct");
            xdr_destroy (&xdr);
            return;
        }
        xdr_free (ret_filter, ret);
    } else /* error */ {
        virErrorPtr verr;
        remote_error error;
        remote_nonnull_domain dom;
        remote_nonnull_network net;

        verr = client->conn
            ? virConnGetLastError (client->conn)
            : virGetLastError ();

        if (verr) {
            error.code = verr->code;
            error.domain = verr->domain;
            error.message = verr->message ? &verr->message : NULL;
            error.level = verr->level;
            if (verr->dom) {
                dom.name = verr->dom->name;
                memcpy (dom.uuid, verr->dom->uuid, VIR_UUID_BUFLEN);
                dom.id = verr->dom->id;
            }
            error.dom = verr->dom ? &dom : NULL;
            error.str1 = verr->str1 ? &verr->str1 : NULL;
            error.str2 = verr->str2 ? &verr->str2 : NULL;
            error.str3 = verr->str3 ? &verr->str3 : NULL;
            error.int1 = verr->int1;
            error.int2 = verr->int2;
            if (verr->net) {
                net.name = verr->net->name;
                memcpy (net.uuid, verr->net->uuid, VIR_UUID_BUFLEN);
            }
            error.net = verr->net ? &net : NULL;
        } else {
            /* Error was NULL so synthesize an error. */
            char msgbuf[] = "remoteDispatchClientRequest: internal error: library function returned error but did not set virterror";
            char *msg = msgbuf;

            error.code = VIR_ERR_RPC;
            error.domain = VIR_FROM_REMOTE;
            error.message = &msg;
            error.level = VIR_ERR_ERROR;
            error.dom = NULL;
            error.str1 = &msg;
            error.str2 = NULL;
            error.str3 = NULL;
            error.int1 = 0;
            error.int2 = 0;
            error.net = NULL;
        }

        if (!xdr_remote_error (&xdr, &error)) {
            remoteDispatchError (client, &req, "serialise return error");
            xdr_destroy (&xdr);
            return;
        }
    }

    /* Write the length word. */
    len = xdr_getpos (&xdr);
    if (xdr_setpos (&xdr, 0) == 0) {
        remoteDispatchError (client, &req, "xdr_setpos");
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_int (&xdr, &len)) {
        remoteDispatchError (client, &req, "serialise return length");
        xdr_destroy (&xdr);
        return;
    }

    xdr_destroy (&xdr);

    /* Set up the output buffer. */
    client->mode = QEMUD_MODE_TX_PACKET;
    client->bufferLength = len;
    client->bufferOffset = 0;
    if (client->tls) client->direction = QEMUD_TLS_DIRECTION_WRITE;
}

/* An error occurred during the dispatching process itself (ie. not
 * an error from the function being called).  We return an error
 * reply.
 */
static void
remoteDispatchError (struct qemud_client *client,
                     remote_message_header *req,
                     const char *fmt, ...)
{
    remote_message_header rep;
    remote_error error;
    va_list args;
    char msgbuf[1024];
    char *msg = msgbuf;
    XDR xdr;
    int len;

    va_start (args, fmt);
    vsnprintf (msgbuf, sizeof msgbuf, fmt, args);
    va_end (args);

    qemudDebug ("%s", msgbuf);

    /* Future versions of the protocol may use different vers or prog.  Try
     * our hardest to send back a message that such clients could see.
     */
    if (req) {
        rep.prog = req->prog;
        rep.vers = req->vers;
        rep.proc = req->proc;
        rep.direction = REMOTE_REPLY;
        rep.serial = req->serial;
        rep.status = REMOTE_ERROR;
    } else {
        rep.prog = REMOTE_PROGRAM;
        rep.vers = REMOTE_PROTOCOL_VERSION;
        rep.proc = REMOTE_PROC_OPEN;
        rep.direction = REMOTE_REPLY;
        rep.serial = 1;
        rep.status = REMOTE_ERROR;
    }

    /* Construct the error. */
    error.code = VIR_ERR_RPC;
    error.domain = VIR_FROM_REMOTE;
    error.message = &msg;
    error.level = VIR_ERR_ERROR;
    error.dom = NULL;
    error.str1 = &msg;
    error.str2 = NULL;
    error.str3 = NULL;
    error.int1 = 0;
    error.int2 = 0;
    error.net = NULL;

    /* Serialise the return header and error. */
    xdrmem_create (&xdr, client->buffer, sizeof client->buffer, XDR_ENCODE);

    len = 0; /* We'll come back and write this later. */
    if (!xdr_int (&xdr, &len)) {
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_remote_message_header (&xdr, &rep)) {
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_remote_error (&xdr, &error)) {
        xdr_destroy (&xdr);
        return;
    }

    len = xdr_getpos (&xdr);
    if (xdr_setpos (&xdr, 0) == 0) {
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_int (&xdr, &len)) {
        xdr_destroy (&xdr);
        return;
    }

    xdr_destroy (&xdr);

    /* Send it. */
    client->mode = QEMUD_MODE_TX_PACKET;
    client->bufferLength = len;
    client->bufferOffset = 0;
    if (client->tls) client->direction = QEMUD_TLS_DIRECTION_WRITE;
}

/*----- Functions. -----*/

static int
remoteDispatchOpen (struct qemud_client *client, remote_message_header *req,
                    struct remote_open_args *args, void *ret ATTRIBUTE_UNUSED)
{
    const char *name;
    int flags;

    /* Already opened? */
    if (client->conn) {
        remoteDispatchError (client, req, "connection already open");
        return -2;
    }

    name = args->name ? *args->name : NULL;

#if DEBUG
    fprintf (stderr, "remoteDispatchOpen: name = %s\n", name);
#endif

    /* If this connection arrived on a readonly socket, force
     * the connection to be readonly.
     */
    flags = args->flags;
    if (client->readonly) flags |= VIR_CONNECT_RO;

    client->conn =
        flags & VIR_CONNECT_RO
        ? virConnectOpenReadOnly (name)
        : virConnectOpen (name);

    return client->conn ? 0 : -1;
}

#define CHECK_CONN(client)                      \
    if (!client->conn) {                        \
        remoteDispatchError (client, req, "connection not open");   \
        return -2;                                                  \
    }

static int
remoteDispatchClose (struct qemud_client *client, remote_message_header *req,
                     void *args ATTRIBUTE_UNUSED, void *ret ATTRIBUTE_UNUSED)
{
    int rv;
    CHECK_CONN(client);

    rv = virConnectClose (client->conn);
    if (rv == 0) client->conn = NULL;

    return rv;
}

static int
remoteDispatchGetType (struct qemud_client *client, remote_message_header *req,
                       void *args ATTRIBUTE_UNUSED, remote_get_type_ret *ret)
{
    const char *type;
    CHECK_CONN(client);

    type = virConnectGetType (client->conn);
    if (type == NULL) return -1;

    /* We have to strdup because remoteDispatchClientRequest will
     * free this string after it's been serialised.
     */
    ret->type = strdup (type);
    if (!ret->type) {
        remoteDispatchError (client, req, "out of memory in strdup");
        return -2;
    }

    return 0;
}

static int
remoteDispatchGetVersion (struct qemud_client *client,
                          remote_message_header *req,
                          void *args ATTRIBUTE_UNUSED,
                          remote_get_version_ret *ret)
{
    unsigned long hvVer;
    CHECK_CONN(client);

    if (virConnectGetVersion (client->conn, &hvVer) == -1)
        return -1;

    ret->hv_ver = hvVer;
    return 0;
}

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
static int
remoteDispatchGetHostname (struct qemud_client *client,
                           remote_message_header *req,
                           void *args ATTRIBUTE_UNUSED,
                           remote_get_hostname_ret *ret)
{
    char *hostname;
    CHECK_CONN(client);

    hostname = virConnectGetHostname (client->conn);
    if (hostname == NULL) return -1;

    ret->hostname = hostname;
    return 0;
}

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
static int
remoteDispatchGetMaxVcpus (struct qemud_client *client,
                           remote_message_header *req,
                           remote_get_max_vcpus_args *args,
                           remote_get_max_vcpus_ret *ret)
{
    char *type;
    CHECK_CONN(client);

    type = args->type ? *args->type : NULL;
    ret->max_vcpus = virConnectGetMaxVcpus (client->conn, type);
    if (ret->max_vcpus == -1) return -1;

    return 0;
}

static int
remoteDispatchNodeGetInfo (struct qemud_client *client,
                           remote_message_header *req,
                           void *args ATTRIBUTE_UNUSED,
                           remote_node_get_info_ret *ret)
{
    virNodeInfo info;
    CHECK_CONN(client);

    if (virNodeGetInfo (client->conn, &info) == -1)
        return -1;

    memcpy (ret->model, info.model, sizeof ret->model);
    ret->memory = info.memory;
    ret->cpus = info.cpus;
    ret->mhz = info.mhz;
    ret->nodes = info.nodes;
    ret->sockets = info.sockets;
    ret->cores = info.cores;
    ret->threads = info.threads;

    return 0;
}

static int
remoteDispatchGetCapabilities (struct qemud_client *client,
                               remote_message_header *req,
                               void *args ATTRIBUTE_UNUSED,
                               remote_get_capabilities_ret *ret)
{
    char *caps;
    CHECK_CONN(client);

    caps = virConnectGetCapabilities (client->conn);
    if (caps == NULL) return -1;

    ret->capabilities = caps;
    return 0;
}

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
static int
remoteDispatchDomainGetSchedulerType (struct qemud_client *client,
                                      remote_message_header *req,
                                      remote_domain_get_scheduler_type_args *args,
                                      remote_domain_get_scheduler_type_ret *ret)
{
    virDomainPtr dom;
    char *type;
    int nparams;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    type = virDomainGetSchedulerType (dom, &nparams);
548 549 550 551
    if (type == NULL) {
        virDomainFree(dom);
        return -1;
    }
552 553 554

    ret->type = type;
    ret->nparams = nparams;
555
    virDomainFree(dom);
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    return 0;
}

static int
remoteDispatchDomainGetSchedulerParameters (struct qemud_client *client,
                                            remote_message_header *req,
                                            remote_domain_get_scheduler_parameters_args *args,
                                            remote_domain_get_scheduler_parameters_ret *ret)
{
    virDomainPtr dom;
    virSchedParameterPtr params;
    int i, r, nparams;
    CHECK_CONN(client);

    nparams = args->nparams;

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
        remoteDispatchError (client, req, "nparams too large");
        return -2;
    }
    params = malloc (sizeof (virSchedParameter) * nparams);
    if (params == NULL) {
        remoteDispatchError (client, req, "out of memory allocating array");
        return -2;
    }

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        free (params);
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    r = virDomainGetSchedulerParameters (dom, params, &nparams);
    if (r == -1) {
591
        virDomainFree(dom);
592 593 594 595 596 597 598 599 600
        free (params);
        return -1;
    }

    /* Serialise the scheduler parameters. */
    ret->params.params_len = nparams;
    ret->params.params_val = malloc (sizeof (struct remote_sched_param)
                                     * nparams);
    if (ret->params.params_val == NULL) {
601
        virDomainFree(dom);
602 603 604 605 606 607 608 609 610 611
        free (params);
        remoteDispatchError (client, req,
                             "out of memory allocating return array");
        return -2;
    }

    for (i = 0; i < nparams; ++i) {
        // remoteDispatchClientRequest will free this:
        ret->params.params_val[i].field = strdup (params[i].field);
        if (ret->params.params_val[i].field == NULL) {
612
            virDomainFree(dom);
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
            free (params);
            remoteDispatchError (client, req,
                                 "out of memory allocating return array");
            return -2;
        }
        ret->params.params_val[i].value.type = params[i].type;
        switch (params[i].type) {
        case VIR_DOMAIN_SCHED_FIELD_INT:
            ret->params.params_val[i].value.remote_sched_param_value_u.i = params[i].value.i; break;
        case VIR_DOMAIN_SCHED_FIELD_UINT:
            ret->params.params_val[i].value.remote_sched_param_value_u.ui = params[i].value.ui; break;
        case VIR_DOMAIN_SCHED_FIELD_LLONG:
            ret->params.params_val[i].value.remote_sched_param_value_u.l = params[i].value.l; break;
        case VIR_DOMAIN_SCHED_FIELD_ULLONG:
            ret->params.params_val[i].value.remote_sched_param_value_u.ul = params[i].value.ul; break;
        case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
            ret->params.params_val[i].value.remote_sched_param_value_u.d = params[i].value.d; break;
        case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
            ret->params.params_val[i].value.remote_sched_param_value_u.b = params[i].value.b; break;
        default:
633
            virDomainFree(dom);
634 635 636 637 638
            free (params);
            remoteDispatchError (client, req, "unknown type");
            return -2;
        }
    }
639
    virDomainFree(dom);
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
    free (params);

    return 0;
}

static int
remoteDispatchDomainSetSchedulerParameters (struct qemud_client *client,
                                            remote_message_header *req,
                                            remote_domain_set_scheduler_parameters_args *args,
                                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int i, r, nparams;
    virSchedParameterPtr params;
    CHECK_CONN(client);

    nparams = args->params.params_len;

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
        remoteDispatchError (client, req, "nparams too large");
        return -2;
    }
    params = malloc (sizeof (virSchedParameter) * nparams);
    if (params == NULL) {
        remoteDispatchError (client, req, "out of memory allocating array");
        return -2;
    }

    /* Deserialise parameters. */
    for (i = 0; i < nparams; ++i) {
        strncpy (params[i].field, args->params.params_val[i].field,
                 VIR_DOMAIN_SCHED_FIELD_LENGTH);
        params[i].field[VIR_DOMAIN_SCHED_FIELD_LENGTH-1] = '\0';
        params[i].type = args->params.params_val[i].value.type;
        switch (params[i].type) {
        case VIR_DOMAIN_SCHED_FIELD_INT:
            params[i].value.i = args->params.params_val[i].value.remote_sched_param_value_u.i; break;
        case VIR_DOMAIN_SCHED_FIELD_UINT:
            params[i].value.ui = args->params.params_val[i].value.remote_sched_param_value_u.ui; break;
        case VIR_DOMAIN_SCHED_FIELD_LLONG:
            params[i].value.l = args->params.params_val[i].value.remote_sched_param_value_u.l; break;
        case VIR_DOMAIN_SCHED_FIELD_ULLONG:
            params[i].value.ul = args->params.params_val[i].value.remote_sched_param_value_u.ul; break;
        case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
            params[i].value.d = args->params.params_val[i].value.remote_sched_param_value_u.d; break;
        case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
            params[i].value.b = args->params.params_val[i].value.remote_sched_param_value_u.b; break;
        }
    }

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        free (params);
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    r = virDomainSetSchedulerParameters (dom, params, nparams);
698
    virDomainFree(dom);
699 700 701 702 703 704
    free (params);
    if (r == -1) return -1;

    return 0;
}

705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
static int
remoteDispatchDomainAttachDevice (struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_domain_attach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

720 721
    if (virDomainAttachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
722
        return -1;
723 724
    }
    virDomainFree(dom);
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
    return 0;
}

static int
remoteDispatchDomainCreate (struct qemud_client *client,
                            remote_message_header *req,
                            remote_domain_create_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

743 744
    if (virDomainCreate (dom) == -1) {
        virDomainFree(dom);
745
        return -1;
746 747
    }
    virDomainFree(dom);
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
    return 0;
}

static int
remoteDispatchDomainCreateLinux (struct qemud_client *client,
                                 remote_message_header *req,
                                 remote_domain_create_linux_args *args,
                                 remote_domain_create_linux_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = virDomainCreateLinux (client->conn, args->xml_desc, args->flags);
    if (dom == NULL) return -1;

    make_nonnull_domain (&ret->dom, dom);
764
    virDomainFree(dom);
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781

    return 0;
}

static int
remoteDispatchDomainDefineXml (struct qemud_client *client,
                               remote_message_header *req,
                               remote_domain_define_xml_args *args,
                               remote_domain_define_xml_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = virDomainDefineXML (client->conn, args->xml);
    if (dom == NULL) return -1;

    make_nonnull_domain (&ret->dom, dom);
782
    virDomainFree(dom);
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

    return 0;
}

static int
remoteDispatchDomainDestroy (struct qemud_client *client,
                             remote_message_header *req,
                             remote_domain_destroy_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    if (virDomainDestroy (dom) == -1)
        return -1;
804
    /* No need to free dom - destroy does it for us */
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    return 0;
}

static int
remoteDispatchDomainDetachDevice (struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_domain_detach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

823 824
    if (virDomainDetachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
825
        return -1;
826
    }
827

828
    virDomainFree(dom);
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
    return 0;
}

static int
remoteDispatchDomainDumpXml (struct qemud_client *client,
                             remote_message_header *req,
                             remote_domain_dump_xml_args *args,
                             remote_domain_dump_xml_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainGetXMLDesc (dom, args->flags);
849 850 851 852 853
    if (!ret->xml) {
            virDomainFree(dom);
            return -1;
    }
    virDomainFree(dom);
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
    return 0;
}

static int
remoteDispatchDomainGetAutostart (struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_domain_get_autostart_args *args,
                                  remote_domain_get_autostart_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

872 873
    if (virDomainGetAutostart (dom, &ret->autostart) == -1) {
        virDomainFree(dom);
874
        return -1;
875 876
    }
    virDomainFree(dom);
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
    return 0;
}

static int
remoteDispatchDomainGetInfo (struct qemud_client *client,
                             remote_message_header *req,
                             remote_domain_get_info_args *args,
                             remote_domain_get_info_ret *ret)
{
    virDomainPtr dom;
    virDomainInfo info;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

896 897
    if (virDomainGetInfo (dom, &info) == -1) {
        virDomainFree(dom);
898
        return -1;
899
    }
900 901 902 903 904 905 906

    ret->state = info.state;
    ret->max_mem = info.maxMem;
    ret->memory = info.memory;
    ret->nr_virt_cpu = info.nrVirtCpu;
    ret->cpu_time = info.cpuTime;

907 908
    virDomainFree(dom);

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
    return 0;
}

static int
remoteDispatchDomainGetMaxMemory (struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_domain_get_max_memory_args *args,
                                  remote_domain_get_max_memory_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    ret->memory = virDomainGetMaxMemory (dom);
928 929 930 931 932
    if (ret->memory == 0) {
        virDomainFree(dom);
        return -1;
    }
    virDomainFree(dom);
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
    return 0;
}

static int
remoteDispatchDomainGetMaxVcpus (struct qemud_client *client,
                                 remote_message_header *req,
                                 remote_domain_get_max_vcpus_args *args,
                                 remote_domain_get_max_vcpus_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    ret->num = virDomainGetMaxVcpus (dom);
952 953 954 955 956
    if (ret->num == -1) {
        virDomainFree(dom);
        return -1;
    }
    virDomainFree(dom);
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
    return 0;
}

static int
remoteDispatchDomainGetOsType (struct qemud_client *client,
                               remote_message_header *req,
                               remote_domain_get_os_type_args *args,
                               remote_domain_get_os_type_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    /* remoteDispatchClientRequest will free this */
    ret->type = virDomainGetOSType (dom);
977 978 979 980 981
    if (ret->type == NULL) {
            virDomainFree(dom);
            return -1;
    }
    virDomainFree(dom);
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
    return 0;
}

static int
remoteDispatchDomainGetVcpus (struct qemud_client *client,
                              remote_message_header *req,
                              remote_domain_get_vcpus_args *args,
                              remote_domain_get_vcpus_ret *ret)
{
    virDomainPtr dom;
    virVcpuInfoPtr info;
    unsigned char *cpumaps;
    int info_len, i;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1004
        virDomainFree(dom);
1005 1006 1007 1008
        remoteDispatchError (client, req, "maxinfo > REMOTE_VCPUINFO_MAX");
        return -2;
    }

1009
    if (args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1010
        virDomainFree(dom);
1011
        remoteDispatchError (client, req, "maxinfo * maplen > REMOTE_CPUMAPS_MAX");
1012 1013 1014 1015 1016
        return -2;
    }

    /* Allocate buffers to take the results. */
    info = calloc (args->maxinfo, sizeof (virVcpuInfo));
1017
    cpumaps = calloc (args->maxinfo * args->maplen, sizeof (unsigned char));
1018 1019 1020 1021

    info_len = virDomainGetVcpus (dom,
                                  info, args->maxinfo,
                                  cpumaps, args->maplen);
1022 1023 1024 1025
    if (info_len == -1) {
        virDomainFree(dom);
        return -1;
    }
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
    ret->info.info_val = calloc (info_len, sizeof (remote_vcpu_info));

    for (i = 0; i < info_len; ++i) {
        ret->info.info_val[i].number = info[i].number;
        ret->info.info_val[i].state = info[i].state;
        ret->info.info_val[i].cpu_time = info[i].cpuTime;
        ret->info.info_val[i].cpu = info[i].cpu;
    }

    /* Don't need to allocate/copy the cpumaps if we make the reasonable
     * assumption that unsigned char and char are the same size.
     * Note that remoteDispatchClientRequest will free.
     */
1042
    ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
1043 1044
    ret->cpumaps.cpumaps_val = (char *) cpumaps;

1045
    virDomainFree(dom);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    return 0;
}

static int
remoteDispatchListDefinedDomains (struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_list_defined_domains_args *args,
                                  remote_list_defined_domains_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_DOMAIN_NAME_LIST_MAX) {
        remoteDispatchError (client, req,
                             "maxnames > REMOTE_DOMAIN_NAME_LIST_MAX");
        return -2;
    }

    /* Allocate return buffer. */
    ret->names.names_val = calloc (args->maxnames, sizeof (char *));

    ret->names.names_len =
        virConnectListDefinedDomains (client->conn,
                                      ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) return -1;

    return 0;
}

static int
remoteDispatchDomainLookupById (struct qemud_client *client,
                                remote_message_header *req,
                                remote_domain_lookup_by_id_args *args,
                                remote_domain_lookup_by_id_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = virDomainLookupByID (client->conn, args->id);
    if (dom == NULL) return -1;

    make_nonnull_domain (&ret->dom, dom);
1087
    virDomainFree(dom);
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    return 0;
}

static int
remoteDispatchDomainLookupByName (struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_domain_lookup_by_name_args *args,
                                  remote_domain_lookup_by_name_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = virDomainLookupByName (client->conn, args->name);
    if (dom == NULL) return -1;

    make_nonnull_domain (&ret->dom, dom);
1104
    virDomainFree(dom);
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
    return 0;
}

static int
remoteDispatchDomainLookupByUuid (struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_domain_lookup_by_uuid_args *args,
                                  remote_domain_lookup_by_uuid_ret *ret)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = virDomainLookupByUUID (client->conn, (unsigned char *) args->uuid);
    if (dom == NULL) return -1;

    make_nonnull_domain (&ret->dom, dom);
1121
    virDomainFree(dom);
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
    return 0;
}

static int
remoteDispatchNumOfDefinedDomains (struct qemud_client *client,
                                   remote_message_header *req,
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_num_of_defined_domains_ret *ret)
{
    CHECK_CONN(client);

    ret->num = virConnectNumOfDefinedDomains (client->conn);
    if (ret->num == -1) return -1;

    return 0;
}

static int
remoteDispatchDomainPinVcpu (struct qemud_client *client,
                             remote_message_header *req,
                             remote_domain_pin_vcpu_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int rv;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
1156
        virDomainFree(dom);
1157 1158 1159 1160 1161 1162 1163
        remoteDispatchError (client, req, "cpumap_len > REMOTE_CPUMAP_MAX");
        return -2;
    }

    rv = virDomainPinVcpu (dom, args->vcpu,
                           (unsigned char *) args->cpumap.cpumap_val,
                           args->cpumap.cpumap_len);
1164 1165 1166 1167 1168
    if (rv == -1) {
        virDomainFree(dom);
        return -1;
    }
    virDomainFree(dom);
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
    return 0;
}

static int
remoteDispatchDomainReboot (struct qemud_client *client,
                            remote_message_header *req,
                            remote_domain_reboot_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1187 1188
    if (virDomainReboot (dom, args->flags) == -1) {
        virDomainFree(dom);
1189
        return -1;
1190 1191
    }
    virDomainFree(dom);
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
    return 0;
}

static int
remoteDispatchDomainRestore (struct qemud_client *client,
                             remote_message_header *req,
                             remote_domain_restore_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

    if (virDomainRestore (client->conn, args->from) == -1)
        return -1;

    return 0;
}

static int
remoteDispatchDomainResume (struct qemud_client *client,
                            remote_message_header *req,
                            remote_domain_resume_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1224 1225
    if (virDomainResume (dom) == -1) {
        virDomainFree(dom);
1226
        return -1;
1227 1228
    }
    virDomainFree(dom);
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
    return 0;
}

static int
remoteDispatchDomainSave (struct qemud_client *client,
                          remote_message_header *req,
                          remote_domain_save_args *args,
                          void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1247 1248
    if (virDomainSave (dom, args->to) == -1) {
        virDomainFree(dom);
1249
        return -1;
1250 1251
    }
    virDomainFree(dom);
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
    return 0;
}

static int
remoteDispatchDomainCoreDump (struct qemud_client *client,
                              remote_message_header *req,
                              remote_domain_core_dump_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1270 1271
    if (virDomainCoreDump (dom, args->to, args->flags) == -1) {
        virDomainFree(dom);
1272
        return -1;
1273 1274
    }
    virDomainFree(dom);
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
    return 0;
}

static int
remoteDispatchDomainSetAutostart (struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_domain_set_autostart_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1293 1294
    if (virDomainSetAutostart (dom, args->autostart) == -1) {
        virDomainFree(dom);
1295
        return -1;
1296 1297
    }
    virDomainFree(dom);
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
    return 0;
}

static int
remoteDispatchDomainSetMaxMemory (struct qemud_client *client,
                                  remote_message_header *req,
                                  remote_domain_set_max_memory_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1316 1317
    if (virDomainSetMaxMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
1318
        return -1;
1319 1320
    }
    virDomainFree(dom);
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
    return 0;
}

static int
remoteDispatchDomainSetMemory (struct qemud_client *client,
                               remote_message_header *req,
                               remote_domain_set_memory_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1339 1340
    if (virDomainSetMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
1341
        return -1;
1342 1343
    }
    virDomainFree(dom);
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
    return 0;
}

static int
remoteDispatchDomainSetVcpus (struct qemud_client *client,
                              remote_message_header *req,
                              remote_domain_set_vcpus_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1362 1363
    if (virDomainSetVcpus (dom, args->nvcpus) == -1) {
        virDomainFree(dom);
1364
        return -1;
1365 1366
    }
    virDomainFree(dom);
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
    return 0;
}

static int
remoteDispatchDomainShutdown (struct qemud_client *client,
                              remote_message_header *req,
                              remote_domain_shutdown_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1385 1386
    if (virDomainShutdown (dom) == -1) {
        virDomainFree(dom);
1387
        return -1;
1388 1389
    }
    virDomainFree(dom);
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
    return 0;
}

static int
remoteDispatchDomainSuspend (struct qemud_client *client,
                             remote_message_header *req,
                             remote_domain_suspend_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1408 1409
    if (virDomainSuspend (dom) == -1) {
        virDomainFree(dom);
1410
        return -1;
1411 1412
    }
    virDomainFree(dom);
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
    return 0;
}

static int
remoteDispatchDomainUndefine (struct qemud_client *client,
                              remote_message_header *req,
                              remote_domain_undefine_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    CHECK_CONN(client);

    dom = get_nonnull_domain (client->conn, args->dom);
    if (dom == NULL) {
        remoteDispatchError (client, req, "domain not found");
        return -2;
    }

1431 1432
    if (virDomainUndefine (dom) == -1) {
        virDomainFree(dom);
1433
        return -1;
1434 1435
    }
    virDomainFree(dom);
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 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 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
    return 0;
}

static int
remoteDispatchListDefinedNetworks (struct qemud_client *client,
                                   remote_message_header *req,
                                   remote_list_defined_networks_args *args,
                                   remote_list_defined_networks_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
        remoteDispatchError (client, req,
                             "maxnames > REMOTE_NETWORK_NAME_LIST_MAX");
        return -2;
    }

    /* Allocate return buffer. */
    ret->names.names_val = calloc (args->maxnames, sizeof (char *));

    ret->names.names_len =
        virConnectListDefinedNetworks (client->conn,
                                       ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) return -1;

    return 0;
}

static int
remoteDispatchListDomains (struct qemud_client *client,
                           remote_message_header *req,
                           remote_list_domains_args *args,
                           remote_list_domains_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
        remoteDispatchError (client, req,
                             "maxids > REMOTE_DOMAIN_ID_LIST_MAX");
        return -2;
    }

    /* Allocate return buffer. */
    ret->ids.ids_val = calloc (args->maxids, sizeof (int));

    ret->ids.ids_len = virConnectListDomains (client->conn,
                                              ret->ids.ids_val, args->maxids);
    if (ret->ids.ids_len == -1) return -1;

    return 0;
}

static int
remoteDispatchListNetworks (struct qemud_client *client,
                            remote_message_header *req,
                            remote_list_networks_args *args,
                            remote_list_networks_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
        remoteDispatchError (client, req,
                             "maxnames > REMOTE_NETWORK_NAME_LIST_MAX");
        return -2;
    }

    /* Allocate return buffer. */
    ret->names.names_val = calloc (args->maxnames, sizeof (char *));

    ret->names.names_len =
        virConnectListNetworks (client->conn,
                                ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) return -1;

    return 0;
}

static int
remoteDispatchNetworkCreate (struct qemud_client *client,
                             remote_message_header *req,
                             remote_network_create_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

1528 1529
    if (virNetworkCreate (net) == -1) {
        virNetworkFree(net);
1530
        return -1;
1531 1532
    }
    virNetworkFree(net);
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
    return 0;
}

static int
remoteDispatchNetworkCreateXml (struct qemud_client *client,
                                remote_message_header *req,
                                remote_network_create_xml_args *args,
                                remote_network_create_xml_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = virNetworkCreateXML (client->conn, args->xml);
    if (net == NULL) return -1;

    make_nonnull_network (&ret->net, net);
1549
    virNetworkFree(net);
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
    return 0;
}

static int
remoteDispatchNetworkDefineXml (struct qemud_client *client,
                                remote_message_header *req,
                                remote_network_define_xml_args *args,
                                remote_network_define_xml_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = virNetworkDefineXML (client->conn, args->xml);
    if (net == NULL) return -1;

    make_nonnull_network (&ret->net, net);
1566
    virNetworkFree(net);
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
    return 0;
}

static int
remoteDispatchNetworkDestroy (struct qemud_client *client,
                              remote_message_header *req,
                              remote_network_destroy_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

1585 1586
    if (virNetworkDestroy (net) == -1) {
        virNetworkFree(net);
1587
        return -1;
1588 1589
    }
    virNetworkFree(net);
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
    return 0;
}

static int
remoteDispatchNetworkDumpXml (struct qemud_client *client,
                              remote_message_header *req,
                              remote_network_dump_xml_args *args,
                              remote_network_dump_xml_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNetworkGetXMLDesc (net, args->flags);
1610 1611 1612 1613 1614
    if (!ret->xml) {
        virNetworkFree(net);
        return -1;
    }
    virNetworkFree(net);
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
    return 0;
}

static int
remoteDispatchNetworkGetAutostart (struct qemud_client *client,
                                   remote_message_header *req,
                                   remote_network_get_autostart_args *args,
                                   remote_network_get_autostart_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

1633 1634
    if (virNetworkGetAutostart (net, &ret->autostart) == -1) {
        virNetworkFree(net);
1635
        return -1;
1636 1637
    }
    virNetworkFree(net);
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
    return 0;
}

static int
remoteDispatchNetworkGetBridgeName (struct qemud_client *client,
                                    remote_message_header *req,
                                    remote_network_get_bridge_name_args *args,
                                    remote_network_get_bridge_name_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virNetworkGetBridgeName (net);
1658 1659 1660 1661 1662
    if (!ret->name) {
        virNetworkFree(net);
        return -1;
    }
    virNetworkFree(net);
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
    return 0;
}

static int
remoteDispatchNetworkLookupByName (struct qemud_client *client,
                                   remote_message_header *req,
                                   remote_network_lookup_by_name_args *args,
                                   remote_network_lookup_by_name_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = virNetworkLookupByName (client->conn, args->name);
    if (net == NULL) return -1;

    make_nonnull_network (&ret->net, net);
1679
    virNetworkFree(net);
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
    return 0;
}

static int
remoteDispatchNetworkLookupByUuid (struct qemud_client *client,
                                   remote_message_header *req,
                                   remote_network_lookup_by_uuid_args *args,
                                   remote_network_lookup_by_uuid_ret *ret)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = virNetworkLookupByUUID (client->conn, (unsigned char *) args->uuid);
    if (net == NULL) return -1;

    make_nonnull_network (&ret->net, net);
1696
    virNetworkFree(net);
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
    return 0;
}

static int
remoteDispatchNetworkSetAutostart (struct qemud_client *client,
                                   remote_message_header *req,
                                   remote_network_set_autostart_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

1715 1716
    if (virNetworkSetAutostart (net, args->autostart) == -1) {
        virNetworkFree(net);
1717
        return -1;
1718 1719
    }
    virNetworkFree(net);
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
    return 0;
}

static int
remoteDispatchNetworkUndefine (struct qemud_client *client,
                               remote_message_header *req,
                               remote_network_undefine_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;
    CHECK_CONN(client);

    net = get_nonnull_network (client->conn, args->net);
    if (net == NULL) {
        remoteDispatchError (client, req, "network not found");
        return -2;
    }

1738 1739
    if (virNetworkUndefine (net) == -1) {
        virNetworkFree(net);
1740
        return -1;
1741 1742
    }
    virNetworkFree(net);
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837
    return 0;
}

static int
remoteDispatchNumOfDefinedNetworks (struct qemud_client *client,
                                    remote_message_header *req,
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_num_of_defined_networks_ret *ret)
{
    CHECK_CONN(client);

    ret->num = virConnectNumOfDefinedNetworks (client->conn);
    if (ret->num == -1) return -1;

    return 0;
}

static int
remoteDispatchNumOfDomains (struct qemud_client *client,
                            remote_message_header *req,
                            void *args ATTRIBUTE_UNUSED,
                            remote_num_of_domains_ret *ret)
{
    CHECK_CONN(client);

    ret->num = virConnectNumOfDomains (client->conn);
    if (ret->num == -1) return -1;

    return 0;
}

static int
remoteDispatchNumOfNetworks (struct qemud_client *client,
                             remote_message_header *req,
                             void *args ATTRIBUTE_UNUSED,
                             remote_num_of_networks_ret *ret)
{
    CHECK_CONN(client);

    ret->num = virConnectNumOfNetworks (client->conn);
    if (ret->num == -1) return -1;

    return 0;
}

/*----- Helpers. -----*/

/* get_nonnull_domain and get_nonnull_network turn an on-wire
 * (name, uuid) pair into virDomainPtr or virNetworkPtr object.
 * virDomainPtr or virNetworkPtr cannot be NULL.
 *
 * NB. If these return NULL then the caller must return an error.
 */
static virDomainPtr
get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain)
{
    virDomainPtr dom;
    dom = virGetDomain (conn, domain.name, BAD_CAST domain.uuid);
    /* Should we believe the domain.id sent by the client?  Maybe
     * this should be a check rather than an assignment? XXX
     */
    if (dom) dom->id = domain.id;
    return dom;
}

static virNetworkPtr
get_nonnull_network (virConnectPtr conn, remote_nonnull_network network)
{
    return virGetNetwork (conn, network.name, BAD_CAST network.uuid);
}

/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
make_nonnull_domain (remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
{
    dom_dst->id = dom_src->id;
    dom_dst->name = strdup (dom_src->name);
    memcpy (dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
}

static void
make_nonnull_network (remote_nonnull_network *net_dst, virNetworkPtr net_src)
{
    net_dst->name = strdup (net_src->name);
    memcpy (net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
}

/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */