remote.c 135.9 KB
Newer Older
1 2 3
/*
 * remote.c: code handling remote requests (from remote_internal.c)
 *
4
 * Copyright (C) 2007, 2008 Red Hat, Inc.
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
 *
 * 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>
44
#include <fnmatch.h>
45

46 47 48 49 50
#ifdef HAVE_POLKIT
#include <polkit/polkit.h>
#include <polkit-dbus/polkit-dbus.h>
#endif

51 52
#include "libvirt_internal.h"
#include "datatypes.h"
53
#include "qemud.h"
54
#include "memory.h"
55

56 57
#define REMOTE_DEBUG(fmt,...) qemudDebug("REMOTE: " fmt, __VA_ARGS__)

58 59 60
static void remoteDispatchFormatError (remote_error *rerr,
                                       const char *fmt, ...)
    ATTRIBUTE_FORMAT(printf, 2, 3);
61 62
static virDomainPtr get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network (virConnectPtr conn, remote_nonnull_network network);
63 64
static virStoragePoolPtr get_nonnull_storage_pool (virConnectPtr conn, remote_nonnull_storage_pool pool);
static virStorageVolPtr get_nonnull_storage_vol (virConnectPtr conn, remote_nonnull_storage_vol vol);
65 66
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);
67 68
static void make_nonnull_storage_pool (remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src);
static void make_nonnull_storage_vol (remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src);
69
static void make_nonnull_node_device (remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src);
70 71 72

#include "remote_dispatch_prototypes.h"

73 74 75 76 77 78 79 80 81
typedef union {
#include "remote_dispatch_args.h"
} dispatch_args;

typedef union {
#include "remote_dispatch_ret.h"
} dispatch_ret;


82 83 84 85 86 87 88 89 90 91 92 93 94
/**
 * When the RPC handler is called:
 *
 *  - Server object is unlocked
 *  - Client object is unlocked
 *
 * Both must be locked before use. Server lock must
 * be held before attempting to lock client.
 *
 * Without any locking, it is safe to use:
 *
 *   'conn', 'rerr', 'args and 'ret'
 */
95 96
typedef int (*dispatch_fn) (struct qemud_server *server,
                            struct qemud_client *client,
97
                            virConnectPtr conn,
98
                            remote_error *err,
99 100 101 102 103 104 105 106 107 108 109 110
                            dispatch_args *args,
                            dispatch_ret *ret);

typedef struct {
    dispatch_fn fn;
    xdrproc_t args_filter;
    xdrproc_t ret_filter;
} dispatch_data;

static const dispatch_data const dispatch_table[] = {
#include "remote_dispatch_table.h"
};
111

112 113 114 115
/* Prototypes */
static void
remoteDispatchDomainEventSend (struct qemud_client *client,
                               virDomainPtr dom,
116 117
                               int event,
                               int detail);
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

/* Convert a libvirt  virError object into wire format */
static void
remoteDispatchCopyError (remote_error *rerr,
                         virErrorPtr verr)
{
    rerr->code = verr->code;
    rerr->domain = verr->domain;
    rerr->message = verr->message ? malloc(sizeof(char*)) : NULL;
    if (rerr->message) *rerr->message = strdup(verr->message);
    rerr->level = verr->level;
    rerr->str1 = verr->str1 ? malloc(sizeof(char*)) : NULL;
    if (rerr->str1) *rerr->str1 = strdup(verr->str1);
    rerr->str2 = verr->str2 ? malloc(sizeof(char*)) : NULL;
    if (rerr->str2) *rerr->str2 = strdup(verr->str2);
    rerr->str3 = verr->str3 ? malloc(sizeof(char*)) : NULL;
    if (rerr->str3) *rerr->str3 = strdup(verr->str3);
    rerr->int1 = verr->int1;
    rerr->int2 = verr->int2;
}


/* A set of helpers for sending back errors to client
   in various ways .... */

static void
remoteDispatchStringError (remote_error *rerr,
                           int code, const char *msg)
{
    virError verr;

    memset(&verr, 0, sizeof verr);

    /* Construct the dummy libvirt virError. */
    verr.code = code;
    verr.domain = VIR_FROM_REMOTE;
    verr.message = (char *)msg;
    verr.level = VIR_ERR_ERROR;
    verr.str1 = (char *)msg;

    remoteDispatchCopyError(rerr, &verr);
}

static void
remoteDispatchAuthError (remote_error *rerr)
{
    remoteDispatchStringError (rerr, VIR_ERR_AUTH_FAILED, "authentication failed");
}

static void
remoteDispatchFormatError (remote_error *rerr,
                           const char *fmt, ...)
{
    va_list args;
    char msgbuf[1024];
    char *msg = msgbuf;

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

    remoteDispatchStringError (rerr, VIR_ERR_RPC, msg);
}

static void
remoteDispatchGenericError (remote_error *rerr)
{
    remoteDispatchStringError(rerr,
                              VIR_ERR_INTERNAL_ERROR,
                              "library function returned error but did not set virterror");
}

static void
remoteDispatchOOMError (remote_error *rerr)
{
    remoteDispatchStringError(rerr,
                              VIR_ERR_NO_MEMORY,
                              NULL);
}

static void
remoteDispatchConnError (remote_error *rerr,
                         virConnectPtr conn)
{
    virErrorPtr verr;

    if (conn)
        verr = virConnGetLastError(conn);
    else
        verr = virGetLastError();
    if (verr)
        remoteDispatchCopyError(rerr, verr);
    else
        remoteDispatchGenericError(rerr);
}


216 217 218
/* 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).
219 220 221
 *
 * Server object is unlocked
 * Client object is locked
222
 */
223
unsigned int
224
remoteDispatchClientRequest (struct qemud_server *server,
225 226 227 228
                             struct qemud_client *client)
{
    XDR xdr;
    remote_message_header req, rep;
229
    remote_error rerr;
230 231 232
    dispatch_args args;
    dispatch_ret ret;
    const dispatch_data *data = NULL;
233
    int rv = -1, len;
234
    virConnectPtr conn = NULL;
235

236 237
    memset(&args, 0, sizeof args);
    memset(&ret, 0, sizeof ret);
238
    memset(&rerr, 0, sizeof rerr);
239 240

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

243 244
    if (!xdr_remote_message_header (&xdr, &req))
        goto fatal_error;
245 246 247

    /* Check version, etc. */
    if (req.prog != REMOTE_PROGRAM) {
248 249 250 251
        remoteDispatchFormatError (&rerr,
                                   _("program mismatch (actual %x, expected %x)"),
                                   req.prog, REMOTE_PROGRAM);
        goto rpc_error;
252 253
    }
    if (req.vers != REMOTE_PROTOCOL_VERSION) {
254 255 256 257
        remoteDispatchFormatError (&rerr,
                                   _("version mismatch (actual %x, expected %x)"),
                                   req.vers, REMOTE_PROTOCOL_VERSION);
        goto rpc_error;
258 259
    }
    if (req.direction != REMOTE_CALL) {
260 261 262
        remoteDispatchFormatError (&rerr, _("direction (%d) != REMOTE_CALL"),
                                   (int) req.direction);
        goto rpc_error;
263 264
    }
    if (req.status != REMOTE_OK) {
265 266 267
        remoteDispatchFormatError (&rerr, _("status (%d) != REMOTE_OK"),
                                   (int) req.status);
        goto rpc_error;
268 269
    }

270 271 272 273 274 275 276
    /* If client is marked as needing auth, don't allow any RPC ops,
     * except for authentication ones
     */
    if (client->auth) {
        if (req.proc != REMOTE_PROC_AUTH_LIST &&
            req.proc != REMOTE_PROC_AUTH_SASL_INIT &&
            req.proc != REMOTE_PROC_AUTH_SASL_START &&
277 278
            req.proc != REMOTE_PROC_AUTH_SASL_STEP &&
            req.proc != REMOTE_PROC_AUTH_POLKIT
279
            ) {
280 281 282 283 284
            /* Explicitly *NOT* calling  remoteDispatchAuthError() because
               we want back-compatability with libvirt clients which don't
               support the VIR_ERR_AUTH_FAILED error code */
            remoteDispatchFormatError (&rerr, "%s", _("authentication required"));
            goto rpc_error;
285 286 287
        }
    }

288 289
    if (req.proc >= ARRAY_CARDINALITY(dispatch_table) ||
        dispatch_table[req.proc].fn == NULL) {
290 291 292
        remoteDispatchFormatError (&rerr, _("unknown procedure: %d"),
                                   req.proc);
        goto rpc_error;
293 294
    }

295 296 297 298
    data = &(dispatch_table[req.proc]);

    /* De-serialize args off the wire */
    if (!((data->args_filter)(&xdr, &args))) {
299 300
        remoteDispatchFormatError (&rerr, "%s", _("parse args failed"));
        goto rpc_error;
301 302 303
    }

    /* Call function. */
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    conn = client->conn;
    pthread_mutex_unlock(&client->lock);

    /*
     * When the RPC handler is called:
     *
     *  - Server object is unlocked
     *  - Client object is unlocked
     *
     * Without locking, it is safe to use:
     *
     *   'conn', 'rerr', 'args and 'ret'
     */
    rv = (data->fn)(server, client, conn, &rerr, &args, &ret);

    pthread_mutex_lock(&server->lock);
    pthread_mutex_lock(&client->lock);
    pthread_mutex_unlock(&server->lock);

323
    xdr_free (data->args_filter, (char*)&args);
324

325 326
rpc_error:
    xdr_destroy (&xdr);
327 328 329 330 331 332 333

    /* Return header. */
    rep.prog = req.prog;
    rep.vers = req.vers;
    rep.proc = req.proc;
    rep.direction = REMOTE_REPLY;
    rep.serial = req.serial;
334
    rep.status = rv < 0 ? REMOTE_ERROR : REMOTE_OK;
335 336 337 338 339 340

    /* 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)) {
341
        if (rv == 0) xdr_free (data->ret_filter, (char*)&ret);
342
        goto fatal_error;
343 344 345
    }

    if (!xdr_remote_message_header (&xdr, &rep)) {
346
        if (rv == 0) xdr_free (data->ret_filter, (char*)&ret);
347
        goto fatal_error;
348 349 350
    }

    /* If OK, serialise return structure, if error serialise error. */
351 352 353
    if (rv >= 0) {
        if (!((data->ret_filter) (&xdr, &ret)))
            goto fatal_error;
354
        xdr_free (data->ret_filter, (char*)&ret);
355
    } else /* error */ {
356 357 358 359 360
        /* Error was NULL so synthesize an error. */
        if (rerr.code == 0)
            remoteDispatchGenericError(&rerr);
        if (!xdr_remote_error (&xdr, &rerr))
            goto fatal_error;
361 362 363 364
    }

    /* Write the length word. */
    len = xdr_getpos (&xdr);
365 366
    if (xdr_setpos (&xdr, 0) == 0)
        goto fatal_error;
367

368 369
    if (!xdr_int (&xdr, &len))
        goto fatal_error;
370 371

    xdr_destroy (&xdr);
372
    return len;
373

374 375 376
fatal_error:
    /* Seriously bad stuff happened, so we'll kill off this client
       and not send back any RPC error */
377
    xdr_destroy (&xdr);
378
    return 0;
379 380
}

381
int remoteRelayDomainEvent (virConnectPtr conn ATTRIBUTE_UNUSED,
382 383 384 385
                            virDomainPtr dom,
                            int event,
                            int detail,
                            void *opaque)
386 387
{
    struct qemud_client *client = opaque;
388
    REMOTE_DEBUG("Relaying domain event %d %d", event, detail);
389 390

    if(client) {
391
        remoteDispatchDomainEventSend (client, dom, event, detail);
392 393 394 395
        qemudDispatchClientWrite(client->server,client);
    }
    return 0;
}
396 397


398 399 400
/*----- Functions. -----*/

static int
401
remoteDispatchOpen (struct qemud_server *server,
402
                    struct qemud_client *client,
403
                    virConnectPtr conn,
404
                    remote_error *rerr,
405 406 407
                    struct remote_open_args *args, void *ret ATTRIBUTE_UNUSED)
{
    const char *name;
408
    int flags, rc;
409 410

    /* Already opened? */
411
    if (conn) {
412 413
        remoteDispatchFormatError (rerr, "%s", _("connection already open"));
        return -1;
414 415
    }

416 417 418 419
    pthread_mutex_lock(&server->lock);
    pthread_mutex_lock(&client->lock);
    pthread_mutex_unlock(&server->lock);

420 421 422 423 424 425 426 427 428 429 430 431 432
    name = args->name ? *args->name : NULL;

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

433
    if (client->conn == NULL)
434 435
        remoteDispatchConnError(rerr, NULL);

436 437 438
    rc = client->conn ? 0 : -1;
    pthread_mutex_unlock(&client->lock);
    return rc;
439 440
}

441 442 443 444
#define CHECK_CONN(client)                                              \
    if (!client->conn) {                                                \
        remoteDispatchFormatError (rerr, "%s", _("connection not open")); \
        return -1;                                                      \
445 446 447
    }

static int
448
remoteDispatchClose (struct qemud_server *server ATTRIBUTE_UNUSED,
449 450 451
                     struct qemud_client *client ATTRIBUTE_UNUSED,
                     virConnectPtr conn ATTRIBUTE_UNUSED,
                     remote_error *rerr ATTRIBUTE_UNUSED,
452 453
                     void *args ATTRIBUTE_UNUSED, void *ret ATTRIBUTE_UNUSED)
{
454 455 456
    pthread_mutex_lock(&server->lock);
    pthread_mutex_lock(&client->lock);
    pthread_mutex_unlock(&server->lock);
457

458
    client->closing = 1;
459

460
    pthread_mutex_unlock(&client->lock);
461
    return 0;
462 463
}

464
static int
465
remoteDispatchSupportsFeature (struct qemud_server *server ATTRIBUTE_UNUSED,
466 467
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
468
                               remote_error *rerr,
469 470
                               remote_supports_feature_args *args, remote_supports_feature_ret *ret)
{
471
    ret->supported = virDrvSupportsFeature (conn, args->feature);
472 473

    if (ret->supported == -1) {
474
        remoteDispatchConnError(rerr, conn);
475 476
        return -1;
    }
477 478 479 480

    return 0;
}

481
static int
482
remoteDispatchGetType (struct qemud_server *server ATTRIBUTE_UNUSED,
483 484
                       struct qemud_client *client ATTRIBUTE_UNUSED,
                       virConnectPtr conn,
485
                       remote_error *rerr,
486 487 488 489
                       void *args ATTRIBUTE_UNUSED, remote_get_type_ret *ret)
{
    const char *type;

490
    type = virConnectGetType (conn);
491
    if (type == NULL) {
492
        remoteDispatchConnError(rerr, conn);
493 494
        return -1;
    }
495 496 497 498 499 500

    /* We have to strdup because remoteDispatchClientRequest will
     * free this string after it's been serialised.
     */
    ret->type = strdup (type);
    if (!ret->type) {
501 502
        remoteDispatchFormatError (rerr, "%s", _("out of memory in strdup"));
        return -1;
503 504 505 506 507 508
    }

    return 0;
}

static int
509
remoteDispatchGetVersion (struct qemud_server *server ATTRIBUTE_UNUSED,
510 511
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
512
                          remote_error *rerr,
513 514 515 516 517
                          void *args ATTRIBUTE_UNUSED,
                          remote_get_version_ret *ret)
{
    unsigned long hvVer;

518 519
    if (virConnectGetVersion (conn, &hvVer) == -1) {
        remoteDispatchConnError(rerr, conn);
520
        return -1;
521
    }
522 523 524 525 526

    ret->hv_ver = hvVer;
    return 0;
}

527
static int
528
remoteDispatchGetHostname (struct qemud_server *server ATTRIBUTE_UNUSED,
529 530
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
531
                           remote_error *rerr,
532 533 534 535 536
                           void *args ATTRIBUTE_UNUSED,
                           remote_get_hostname_ret *ret)
{
    char *hostname;

537
    hostname = virConnectGetHostname (conn);
538
    if (hostname == NULL) {
539
        remoteDispatchConnError(rerr, conn);
540 541
        return -1;
    }
542 543 544 545 546

    ret->hostname = hostname;
    return 0;
}

547 548
static int
remoteDispatchGetUri (struct qemud_server *server ATTRIBUTE_UNUSED,
549 550
                      struct qemud_client *client ATTRIBUTE_UNUSED,
                      virConnectPtr conn,
551
                      remote_error *rerr,
552 553 554 555 556 557
                      void *args ATTRIBUTE_UNUSED,
                      remote_get_uri_ret *ret)
{
    char *uri;
    CHECK_CONN(client);

558
    uri = virConnectGetURI (conn);
559
    if (uri == NULL) {
560
        remoteDispatchConnError(rerr, conn);
561 562
        return -1;
    }
563 564 565 566 567

    ret->uri = uri;
    return 0;
}

568
static int
569
remoteDispatchGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
570 571
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
572
                           remote_error *rerr,
573 574 575 576 577 578
                           remote_get_max_vcpus_args *args,
                           remote_get_max_vcpus_ret *ret)
{
    char *type;

    type = args->type ? *args->type : NULL;
579
    ret->max_vcpus = virConnectGetMaxVcpus (conn, type);
580
    if (ret->max_vcpus == -1) {
581
        remoteDispatchConnError(rerr, conn);
582 583
        return -1;
    }
584 585 586 587 588

    return 0;
}

static int
589
remoteDispatchNodeGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
590 591
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
592
                           remote_error *rerr,
593 594 595 596 597
                           void *args ATTRIBUTE_UNUSED,
                           remote_node_get_info_ret *ret)
{
    virNodeInfo info;

598 599
    if (virNodeGetInfo (conn, &info) == -1) {
        remoteDispatchConnError(rerr, conn);
600
        return -1;
601
    }
602 603 604 605 606 607 608 609 610 611 612 613 614 615

    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
616
remoteDispatchGetCapabilities (struct qemud_server *server ATTRIBUTE_UNUSED,
617 618
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
619
                               remote_error *rerr,
620 621 622 623 624
                               void *args ATTRIBUTE_UNUSED,
                               remote_get_capabilities_ret *ret)
{
    char *caps;

625
    caps = virConnectGetCapabilities (conn);
626
    if (caps == NULL) {
627
        remoteDispatchConnError(rerr, conn);
628 629
        return -1;
    }
630 631 632 633 634

    ret->capabilities = caps;
    return 0;
}

635 636
static int
remoteDispatchNodeGetCellsFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
637 638
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
639
                                      remote_error *rerr,
640 641 642 643 644
                                      remote_node_get_cells_free_memory_args *args,
                                      remote_node_get_cells_free_memory_ret *ret)
{

    if (args->maxCells > REMOTE_NODE_MAX_CELLS) {
645 646 647
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxCells > REMOTE_NODE_MAX_CELLS"));
        return -1;
648 649 650
    }

    /* Allocate return buffer. */
651
    if (VIR_ALLOC_N(ret->freeMems.freeMems_val, args->maxCells) < 0) {
652 653
        remoteDispatchOOMError(rerr);
        return -1;
654
    }
655

656
    ret->freeMems.freeMems_len = virNodeGetCellsFreeMemory(conn,
657 658 659
                                                           (unsigned long long *)ret->freeMems.freeMems_val,
                                                           args->startCell,
                                                           args->maxCells);
660 661
    if (ret->freeMems.freeMems_len == 0) {
        VIR_FREE(ret->freeMems.freeMems_val);
662
        remoteDispatchConnError(rerr, conn);
663
        return -1;
664
    }
665 666 667 668 669 670 671

    return 0;
}


static int
remoteDispatchNodeGetFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
672 673
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
674
                                 remote_error *rerr,
675 676 677 678 679
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_node_get_free_memory_ret *ret)
{
    unsigned long long freeMem;

680
    freeMem = virNodeGetFreeMemory(conn);
681
    if (freeMem == 0) {
682
        remoteDispatchConnError(rerr, conn);
683 684
        return -1;
    }
685 686 687 688 689
    ret->freeMem = freeMem;
    return 0;
}


690
static int
691
remoteDispatchDomainGetSchedulerType (struct qemud_server *server ATTRIBUTE_UNUSED,
692 693
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
694
                                      remote_error *rerr,
695 696 697 698 699 700 701
                                      remote_domain_get_scheduler_type_args *args,
                                      remote_domain_get_scheduler_type_ret *ret)
{
    virDomainPtr dom;
    char *type;
    int nparams;

702
    dom = get_nonnull_domain (conn, args->dom);
703
    if (dom == NULL) {
704
        remoteDispatchConnError(rerr, conn);
705
        return -1;
706 707 708
    }

    type = virDomainGetSchedulerType (dom, &nparams);
709 710
    if (type == NULL) {
        virDomainFree(dom);
711
        remoteDispatchConnError(rerr, conn);
712 713
        return -1;
    }
714 715 716

    ret->type = type;
    ret->nparams = nparams;
717
    virDomainFree(dom);
718 719 720 721
    return 0;
}

static int
722
remoteDispatchDomainGetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
723 724
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
725
                                            remote_error *rerr,
726 727 728 729 730 731 732 733 734 735
                                            remote_domain_get_scheduler_parameters_args *args,
                                            remote_domain_get_scheduler_parameters_ret *ret)
{
    virDomainPtr dom;
    virSchedParameterPtr params;
    int i, r, nparams;

    nparams = args->nparams;

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
736 737
        remoteDispatchFormatError (rerr, "%s", _("nparams too large"));
        return -1;
738
    }
739
    if (VIR_ALLOC_N(params, nparams) < 0) {
740 741
        remoteDispatchOOMError(rerr);
        return -1;
742 743
    }

744
    dom = get_nonnull_domain (conn, args->dom);
745
    if (dom == NULL) {
746
        VIR_FREE(params);
747
        remoteDispatchConnError(rerr, conn);
748
        return -1;
749 750 751 752
    }

    r = virDomainGetSchedulerParameters (dom, params, &nparams);
    if (r == -1) {
753
        virDomainFree(dom);
754
        VIR_FREE(params);
755
        remoteDispatchConnError(rerr, conn);
756 757 758 759 760
        return -1;
    }

    /* Serialise the scheduler parameters. */
    ret->params.params_len = nparams;
761 762
    if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
        goto oom;
763 764 765 766

    for (i = 0; i < nparams; ++i) {
        // remoteDispatchClientRequest will free this:
        ret->params.params_val[i].field = strdup (params[i].field);
767 768 769
        if (ret->params.params_val[i].field == NULL)
            goto oom;

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
        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:
785
            remoteDispatchFormatError (rerr, "%s", _("unknown type"));
786
            goto cleanup;
787 788
        }
    }
789
    virDomainFree(dom);
790
    VIR_FREE(params);
791 792

    return 0;
793 794

oom:
795
    remoteDispatchOOMError(rerr);
796 797 798 799 800
cleanup:
    virDomainFree(dom);
    for (i = 0 ; i < nparams ; i++)
        VIR_FREE(ret->params.params_val[i].field);
    VIR_FREE(params);
801
    return -1;
802 803 804
}

static int
805
remoteDispatchDomainSetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
806 807
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
808
                                            remote_error *rerr,
809 810 811 812 813 814 815 816 817 818
                                            remote_domain_set_scheduler_parameters_args *args,
                                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int i, r, nparams;
    virSchedParameterPtr params;

    nparams = args->params.params_len;

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
819 820
        remoteDispatchFormatError (rerr, "%s", _("nparams too large"));
        return -1;
821
    }
822
    if (VIR_ALLOC_N(params, nparams)) {
823 824
        remoteDispatchOOMError(rerr);
        return -1;
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
    }

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

849
    dom = get_nonnull_domain (conn, args->dom);
850
    if (dom == NULL) {
851
        VIR_FREE(params);
852
        remoteDispatchConnError(rerr, conn);
853
        return -1;
854 855 856
    }

    r = virDomainSetSchedulerParameters (dom, params, nparams);
857
    virDomainFree(dom);
858
    VIR_FREE(params);
859
    if (r == -1) {
860
        remoteDispatchConnError(rerr, conn);
861 862
        return -1;
    }
863 864 865 866

    return 0;
}

867
static int
868
remoteDispatchDomainBlockStats (struct qemud_server *server ATTRIBUTE_UNUSED,
869 870
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
871
                                remote_error *rerr,
872 873 874 875 876 877 878
                                remote_domain_block_stats_args *args,
                                remote_domain_block_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainBlockStats stats;

879
    dom = get_nonnull_domain (conn, args->dom);
880
    if (dom == NULL) {
881
        remoteDispatchConnError(rerr, conn);
882
        return -1;
883 884 885
    }
    path = args->path;

D
Daniel P. Berrange 已提交
886 887
    if (virDomainBlockStats (dom, path, &stats, sizeof stats) == -1) {
        virDomainFree (dom);
888
        remoteDispatchConnError(rerr, conn);
889
        return -1;
D
Daniel P. Berrange 已提交
890 891
    }
    virDomainFree (dom);
892 893 894 895 896 897 898 899 900 901 902

    ret->rd_req = stats.rd_req;
    ret->rd_bytes = stats.rd_bytes;
    ret->wr_req = stats.wr_req;
    ret->wr_bytes = stats.wr_bytes;
    ret->errs = stats.errs;

    return 0;
}

static int
903
remoteDispatchDomainInterfaceStats (struct qemud_server *server ATTRIBUTE_UNUSED,
904 905
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
906
                                    remote_error *rerr,
907 908 909 910 911 912 913
                                    remote_domain_interface_stats_args *args,
                                    remote_domain_interface_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainInterfaceStats stats;

914
    dom = get_nonnull_domain (conn, args->dom);
915
    if (dom == NULL) {
916
        remoteDispatchConnError(rerr, conn);
917
        return -1;
918 919 920
    }
    path = args->path;

D
Daniel P. Berrange 已提交
921 922
    if (virDomainInterfaceStats (dom, path, &stats, sizeof stats) == -1) {
        virDomainFree (dom);
923
        remoteDispatchConnError(rerr, conn);
924
        return -1;
D
Daniel P. Berrange 已提交
925 926
    }
    virDomainFree (dom);
927 928 929 930 931 932 933 934 935 936 937 938 939

    ret->rx_bytes = stats.rx_bytes;
    ret->rx_packets = stats.rx_packets;
    ret->rx_errs = stats.rx_errs;
    ret->rx_drop = stats.rx_drop;
    ret->tx_bytes = stats.tx_bytes;
    ret->tx_packets = stats.tx_packets;
    ret->tx_errs = stats.tx_errs;
    ret->tx_drop = stats.tx_drop;

    return 0;
}

940 941
static int
remoteDispatchDomainBlockPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
942 943
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
944
                               remote_error *rerr,
945 946 947 948 949 950 951 952 953
                               remote_domain_block_peek_args *args,
                               remote_domain_block_peek_ret *ret)
{
    virDomainPtr dom;
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;

954
    dom = get_nonnull_domain (conn, args->dom);
955
    if (dom == NULL) {
956
        remoteDispatchConnError(rerr, conn);
957
        return -1;
958 959 960 961 962 963 964
    }
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
965
        virDomainFree (dom);
966 967 968
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
        return -1;
969 970 971
    }

    ret->buffer.buffer_len = size;
972 973
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
        virDomainFree (dom);
974 975
        remoteDispatchFormatError (rerr, "%s", strerror (errno));
        return -1;
976 977 978 979 980 981
    }

    if (virDomainBlockPeek (dom, path, offset, size,
                            ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
982
        remoteDispatchConnError(rerr, conn);
983 984 985 986 987 988 989
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

R
Richard W.M. Jones 已提交
990 991
static int
remoteDispatchDomainMemoryPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
992 993
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
994
                                remote_error *rerr,
R
Richard W.M. Jones 已提交
995 996 997 998 999 1000 1001 1002
                                remote_domain_memory_peek_args *args,
                                remote_domain_memory_peek_ret *ret)
{
    virDomainPtr dom;
    unsigned long long offset;
    size_t size;
    unsigned int flags;

1003
    dom = get_nonnull_domain (conn, args->dom);
R
Richard W.M. Jones 已提交
1004
    if (dom == NULL) {
1005
        remoteDispatchConnError(rerr, conn);
1006
        return -1;
R
Richard W.M. Jones 已提交
1007 1008 1009 1010 1011 1012
    }
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1013 1014
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
R
Richard W.M. Jones 已提交
1015
        virDomainFree (dom);
1016
        return -1;
R
Richard W.M. Jones 已提交
1017 1018 1019 1020
    }

    ret->buffer.buffer_len = size;
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
1021
        remoteDispatchFormatError (rerr, "%s", strerror (errno));
R
Richard W.M. Jones 已提交
1022
        virDomainFree (dom);
1023
        return -1;
R
Richard W.M. Jones 已提交
1024 1025 1026 1027 1028 1029
    }

    if (virDomainMemoryPeek (dom, offset, size,
                             ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
1030
        remoteDispatchConnError(rerr, conn);
R
Richard W.M. Jones 已提交
1031 1032 1033 1034 1035 1036 1037
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

1038
static int
1039
remoteDispatchDomainAttachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
1040 1041
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1042
                                  remote_error *rerr,
1043 1044 1045 1046 1047
                                  remote_domain_attach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1048
    dom = get_nonnull_domain (conn, args->dom);
1049
    if (dom == NULL) {
1050
        remoteDispatchConnError(rerr, conn);
1051
        return -1;
1052 1053
    }

1054 1055
    if (virDomainAttachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
1056
        remoteDispatchConnError(rerr, conn);
1057
        return -1;
1058 1059
    }
    virDomainFree(dom);
1060 1061 1062 1063
    return 0;
}

static int
1064
remoteDispatchDomainCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
1065 1066
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
1067
                            remote_error *rerr,
1068 1069 1070 1071 1072
                            remote_domain_create_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1073
    dom = get_nonnull_domain (conn, args->dom);
1074
    if (dom == NULL) {
1075
        remoteDispatchConnError(rerr, conn);
1076
        return -1;
1077 1078
    }

1079 1080
    if (virDomainCreate (dom) == -1) {
        virDomainFree(dom);
1081
        remoteDispatchConnError(rerr, conn);
1082
        return -1;
1083 1084
    }
    virDomainFree(dom);
1085 1086 1087 1088
    return 0;
}

static int
1089
remoteDispatchDomainCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1090 1091 1092 1093 1094
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
                               remote_error *rerr,
                               remote_domain_create_xml_args *args,
                               remote_domain_create_xml_ret *ret)
1095 1096 1097
{
    virDomainPtr dom;

1098
    dom = virDomainCreateXML (conn, args->xml_desc, args->flags);
1099
    if (dom == NULL) {
1100
        remoteDispatchConnError(rerr, conn);
1101 1102
        return -1;
    }
1103 1104

    make_nonnull_domain (&ret->dom, dom);
1105
    virDomainFree(dom);
1106 1107 1108 1109 1110

    return 0;
}

static int
1111
remoteDispatchDomainDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1112 1113
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1114
                               remote_error *rerr,
1115 1116 1117 1118 1119
                               remote_domain_define_xml_args *args,
                               remote_domain_define_xml_ret *ret)
{
    virDomainPtr dom;

1120
    dom = virDomainDefineXML (conn, args->xml);
1121
    if (dom == NULL) {
1122
        remoteDispatchConnError(rerr, conn);
1123 1124
        return -1;
    }
1125 1126

    make_nonnull_domain (&ret->dom, dom);
1127
    virDomainFree(dom);
1128 1129 1130 1131 1132

    return 0;
}

static int
1133
remoteDispatchDomainDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
1134 1135
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1136
                             remote_error *rerr,
1137 1138 1139 1140 1141
                             remote_domain_destroy_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1142
    dom = get_nonnull_domain (conn, args->dom);
1143
    if (dom == NULL) {
1144
        remoteDispatchConnError(rerr, conn);
1145
        return -1;
1146 1147
    }

1148 1149
    if (virDomainDestroy (dom) == -1) {
        virDomainFree(dom);
1150
        remoteDispatchConnError(rerr, conn);
1151
        return -1;
1152 1153
    }
    virDomainFree(dom);
1154 1155 1156 1157
    return 0;
}

static int
1158
remoteDispatchDomainDetachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
1159 1160
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1161
                                  remote_error *rerr,
1162 1163 1164 1165 1166
                                  remote_domain_detach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1167
    dom = get_nonnull_domain (conn, args->dom);
1168
    if (dom == NULL) {
1169
        remoteDispatchConnError(rerr, conn);
1170
        return -1;
1171 1172
    }

1173 1174
    if (virDomainDetachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
1175
        remoteDispatchConnError(rerr, conn);
1176
        return -1;
1177
    }
1178

1179
    virDomainFree(dom);
1180 1181 1182 1183
    return 0;
}

static int
1184
remoteDispatchDomainDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1185 1186
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1187
                             remote_error *rerr,
1188 1189 1190 1191 1192
                             remote_domain_dump_xml_args *args,
                             remote_domain_dump_xml_ret *ret)
{
    virDomainPtr dom;

1193
    dom = get_nonnull_domain (conn, args->dom);
1194
    if (dom == NULL) {
1195
        remoteDispatchConnError(rerr, conn);
1196
        return -1;
1197 1198 1199 1200
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainGetXMLDesc (dom, args->flags);
1201
    if (!ret->xml) {
1202
        virDomainFree(dom);
1203
        remoteDispatchConnError(rerr, conn);
1204
        return -1;
1205 1206
    }
    virDomainFree(dom);
1207 1208 1209 1210
    return 0;
}

static int
1211
remoteDispatchDomainGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
1212 1213
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1214
                                  remote_error *rerr,
1215 1216 1217 1218 1219
                                  remote_domain_get_autostart_args *args,
                                  remote_domain_get_autostart_ret *ret)
{
    virDomainPtr dom;

1220
    dom = get_nonnull_domain (conn, args->dom);
1221
    if (dom == NULL) {
1222
        remoteDispatchConnError(rerr, conn);
1223
        return -1;
1224 1225
    }

1226 1227
    if (virDomainGetAutostart (dom, &ret->autostart) == -1) {
        virDomainFree(dom);
1228
        remoteDispatchConnError(rerr, conn);
1229
        return -1;
1230 1231
    }
    virDomainFree(dom);
1232 1233 1234 1235
    return 0;
}

static int
1236
remoteDispatchDomainGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
1237 1238
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1239
                             remote_error *rerr,
1240 1241 1242 1243 1244 1245
                             remote_domain_get_info_args *args,
                             remote_domain_get_info_ret *ret)
{
    virDomainPtr dom;
    virDomainInfo info;

1246
    dom = get_nonnull_domain (conn, args->dom);
1247
    if (dom == NULL) {
1248
        remoteDispatchConnError(rerr, conn);
1249
        return -1;
1250 1251
    }

1252 1253
    if (virDomainGetInfo (dom, &info) == -1) {
        virDomainFree(dom);
1254
        remoteDispatchConnError(rerr, conn);
1255
        return -1;
1256
    }
1257 1258 1259 1260 1261 1262 1263

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

1264 1265
    virDomainFree(dom);

1266 1267 1268 1269
    return 0;
}

static int
1270
remoteDispatchDomainGetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
1271 1272
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1273
                                  remote_error *rerr,
1274 1275 1276 1277 1278
                                  remote_domain_get_max_memory_args *args,
                                  remote_domain_get_max_memory_ret *ret)
{
    virDomainPtr dom;

1279
    dom = get_nonnull_domain (conn, args->dom);
1280
    if (dom == NULL) {
1281
        remoteDispatchConnError(rerr, conn);
1282
        return -1;
1283 1284 1285
    }

    ret->memory = virDomainGetMaxMemory (dom);
1286 1287
    if (ret->memory == 0) {
        virDomainFree(dom);
1288
        remoteDispatchConnError(rerr, conn);
1289 1290 1291
        return -1;
    }
    virDomainFree(dom);
1292 1293 1294 1295
    return 0;
}

static int
1296
remoteDispatchDomainGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1297 1298
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
1299
                                 remote_error *rerr,
1300 1301 1302 1303 1304
                                 remote_domain_get_max_vcpus_args *args,
                                 remote_domain_get_max_vcpus_ret *ret)
{
    virDomainPtr dom;

1305
    dom = get_nonnull_domain (conn, args->dom);
1306
    if (dom == NULL) {
1307
        remoteDispatchConnError(rerr, conn);
1308
        return -1;
1309 1310 1311
    }

    ret->num = virDomainGetMaxVcpus (dom);
1312 1313
    if (ret->num == -1) {
        virDomainFree(dom);
1314
        remoteDispatchConnError(rerr, conn);
1315 1316 1317
        return -1;
    }
    virDomainFree(dom);
1318 1319 1320 1321
    return 0;
}

static int
1322
remoteDispatchDomainGetOsType (struct qemud_server *server ATTRIBUTE_UNUSED,
1323 1324
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1325
                               remote_error *rerr,
1326 1327 1328 1329 1330
                               remote_domain_get_os_type_args *args,
                               remote_domain_get_os_type_ret *ret)
{
    virDomainPtr dom;

1331
    dom = get_nonnull_domain (conn, args->dom);
1332
    if (dom == NULL) {
1333
        remoteDispatchConnError(rerr, conn);
1334
        return -1;
1335 1336 1337 1338
    }

    /* remoteDispatchClientRequest will free this */
    ret->type = virDomainGetOSType (dom);
1339
    if (ret->type == NULL) {
1340
        virDomainFree(dom);
1341
        remoteDispatchConnError(rerr, conn);
1342
        return -1;
1343 1344
    }
    virDomainFree(dom);
1345 1346 1347 1348
    return 0;
}

static int
1349
remoteDispatchDomainGetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1350 1351
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
1352
                              remote_error *rerr,
1353 1354 1355
                              remote_domain_get_vcpus_args *args,
                              remote_domain_get_vcpus_ret *ret)
{
1356 1357 1358
    virDomainPtr dom = NULL;
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
1359 1360
    int info_len, i;

1361
    dom = get_nonnull_domain (conn, args->dom);
1362
    if (dom == NULL) {
1363
        remoteDispatchConnError(rerr, conn);
1364
        return -1;
1365 1366 1367
    }

    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1368
        virDomainFree(dom);
1369 1370
        remoteDispatchFormatError (rerr, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
        return -1;
1371 1372
    }

1373
    if (args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1374
        virDomainFree(dom);
1375 1376
        remoteDispatchFormatError (rerr, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
        return -1;
1377 1378 1379
    }

    /* Allocate buffers to take the results. */
1380 1381 1382 1383
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
        goto oom;
    if (VIR_ALLOC_N(cpumaps, args->maxinfo) < 0)
        goto oom;
1384 1385 1386 1387

    info_len = virDomainGetVcpus (dom,
                                  info, args->maxinfo,
                                  cpumaps, args->maplen);
1388
    if (info_len == -1) {
1389 1390
        VIR_FREE(info);
        VIR_FREE(cpumaps);
1391
        virDomainFree(dom);
1392
        remoteDispatchConnError(rerr, conn);
1393 1394
        return -1;
    }
1395 1396 1397

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
1398 1399
    if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
        goto oom;
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411

    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.
     */
1412
    ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
1413 1414
    ret->cpumaps.cpumaps_val = (char *) cpumaps;

1415
    VIR_FREE(info);
1416
    virDomainFree(dom);
1417
    return 0;
1418 1419 1420 1421 1422

oom:
    VIR_FREE(info);
    VIR_FREE(cpumaps);
    virDomainFree(dom);
1423 1424
    remoteDispatchOOMError(rerr);
    return -1;
1425 1426
}

1427
static int
1428
remoteDispatchDomainMigratePrepare (struct qemud_server *server ATTRIBUTE_UNUSED,
1429 1430
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1431
                                    remote_error *rerr,
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
                                    remote_domain_migrate_prepare_args *args,
                                    remote_domain_migrate_prepare_ret *ret)
{
    int r;
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;

    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
1446
    if (VIR_ALLOC(uri_out) < 0) {
1447 1448
        remoteDispatchOOMError(rerr);
        return -1;
1449
    }
1450

1451
    r = virDomainMigratePrepare (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1452 1453
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1454
    if (r == -1) {
1455
        VIR_FREE(uri_out);
1456
        remoteDispatchConnError(rerr, conn);
D
Daniel P. Berrange 已提交
1457 1458
        return -1;
    }
1459 1460 1461 1462 1463 1464

    /* remoteDispatchClientRequest will free cookie, uri_out and
     * the string if there is one.
     */
    ret->cookie.cookie_len = cookielen;
    ret->cookie.cookie_val = cookie;
D
Daniel P. Berrange 已提交
1465 1466
    if (*uri_out == NULL) {
        ret->uri_out = NULL;
1467
        VIR_FREE(uri_out);
D
Daniel P. Berrange 已提交
1468 1469 1470
    } else {
        ret->uri_out = uri_out;
    }
1471 1472 1473 1474 1475

    return 0;
}

static int
1476
remoteDispatchDomainMigratePerform (struct qemud_server *server ATTRIBUTE_UNUSED,
1477 1478
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1479
                                    remote_error *rerr,
1480 1481 1482 1483 1484 1485 1486
                                    remote_domain_migrate_perform_args *args,
                                    void *ret ATTRIBUTE_UNUSED)
{
    int r;
    virDomainPtr dom;
    char *dname;

1487
    dom = get_nonnull_domain (conn, args->dom);
1488
    if (dom == NULL) {
1489
        remoteDispatchConnError(rerr, conn);
1490
        return -1;
1491 1492 1493 1494
    }

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

D
Daniel P. Berrange 已提交
1495 1496 1497 1498 1499
    r = virDomainMigratePerform (dom,
                                 args->cookie.cookie_val,
                                 args->cookie.cookie_len,
                                 args->uri,
                                 args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1500
    virDomainFree (dom);
1501
    if (r == -1) {
1502
        remoteDispatchConnError(rerr, conn);
1503 1504
        return -1;
    }
1505 1506 1507 1508 1509

    return 0;
}

static int
1510
remoteDispatchDomainMigrateFinish (struct qemud_server *server ATTRIBUTE_UNUSED,
1511 1512
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1513
                                   remote_error *rerr,
1514 1515 1516 1517 1518 1519
                                   remote_domain_migrate_finish_args *args,
                                   remote_domain_migrate_finish_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

1520
    ddom = virDomainMigrateFinish (conn, args->dname,
D
Daniel P. Berrange 已提交
1521 1522 1523 1524
                                   args->cookie.cookie_val,
                                   args->cookie.cookie_len,
                                   args->uri,
                                   args->flags);
1525
    if (ddom == NULL) {
1526
        remoteDispatchConnError(rerr, conn);
1527 1528
        return -1;
    }
1529 1530

    make_nonnull_domain (&ret->ddom, ddom);
D
Daniel P. Berrange 已提交
1531
    virDomainFree (ddom);
1532 1533 1534
    return 0;
}

D
Daniel Veillard 已提交
1535 1536
static int
remoteDispatchDomainMigratePrepare2 (struct qemud_server *server ATTRIBUTE_UNUSED,
1537 1538
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1539
                                     remote_error *rerr,
D
Daniel Veillard 已提交
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
                                     remote_domain_migrate_prepare2_args *args,
                                     remote_domain_migrate_prepare2_ret *ret)
{
    int r;
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
    CHECK_CONN (client);

    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
    if (VIR_ALLOC(uri_out) < 0) {
1556 1557
        remoteDispatchOOMError(rerr);
        return -1;
D
Daniel Veillard 已提交
1558 1559
    }

1560
    r = virDomainMigratePrepare2 (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1561 1562 1563
                                  uri_in, uri_out,
                                  args->flags, dname, args->resource,
                                  args->dom_xml);
1564
    if (r == -1) {
1565
        remoteDispatchConnError(rerr, conn);
1566 1567
        return -1;
    }
D
Daniel Veillard 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580

    /* remoteDispatchClientRequest will free cookie, uri_out and
     * the string if there is one.
     */
    ret->cookie.cookie_len = cookielen;
    ret->cookie.cookie_val = cookie;
    ret->uri_out = *uri_out == NULL ? NULL : uri_out;

    return 0;
}

static int
remoteDispatchDomainMigrateFinish2 (struct qemud_server *server ATTRIBUTE_UNUSED,
1581 1582
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1583
                                    remote_error *rerr,
D
Daniel Veillard 已提交
1584 1585 1586 1587 1588 1589
                                    remote_domain_migrate_finish2_args *args,
                                    remote_domain_migrate_finish2_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

1590
    ddom = virDomainMigrateFinish2 (conn, args->dname,
D
Daniel P. Berrange 已提交
1591 1592 1593 1594 1595
                                    args->cookie.cookie_val,
                                    args->cookie.cookie_len,
                                    args->uri,
                                    args->flags,
                                    args->retcode);
1596
    if (ddom == NULL) {
1597
        remoteDispatchConnError(rerr, conn);
1598 1599
        return -1;
    }
D
Daniel Veillard 已提交
1600 1601 1602 1603 1604 1605

    make_nonnull_domain (&ret->ddom, ddom);

    return 0;
}

1606
static int
1607
remoteDispatchListDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
1608 1609
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1610
                                  remote_error *rerr,
1611 1612 1613 1614 1615
                                  remote_list_defined_domains_args *args,
                                  remote_list_defined_domains_ret *ret)
{

    if (args->maxnames > REMOTE_DOMAIN_NAME_LIST_MAX) {
1616 1617 1618
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_DOMAIN_NAME_LIST_MAX"));
        return -1;
1619 1620 1621
    }

    /* Allocate return buffer. */
1622
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
1623 1624
        remoteDispatchOOMError(rerr);
        return -1;
1625
    }
1626 1627

    ret->names.names_len =
1628
        virConnectListDefinedDomains (conn,
1629
                                      ret->names.names_val, args->maxnames);
1630 1631
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
1632
        remoteDispatchConnError(rerr, conn);
1633 1634
        return -1;
    }
1635 1636 1637 1638 1639

    return 0;
}

static int
1640
remoteDispatchDomainLookupById (struct qemud_server *server ATTRIBUTE_UNUSED,
1641 1642
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
1643
                                remote_error *rerr,
1644 1645 1646 1647 1648
                                remote_domain_lookup_by_id_args *args,
                                remote_domain_lookup_by_id_ret *ret)
{
    virDomainPtr dom;

1649
    dom = virDomainLookupByID (conn, args->id);
1650
    if (dom == NULL) {
1651
        remoteDispatchConnError(rerr, conn);
1652 1653
        return -1;
    }
1654 1655

    make_nonnull_domain (&ret->dom, dom);
1656
    virDomainFree(dom);
1657 1658 1659 1660
    return 0;
}

static int
1661
remoteDispatchDomainLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
1662 1663
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1664
                                  remote_error *rerr,
1665 1666 1667 1668 1669
                                  remote_domain_lookup_by_name_args *args,
                                  remote_domain_lookup_by_name_ret *ret)
{
    virDomainPtr dom;

1670
    dom = virDomainLookupByName (conn, args->name);
1671
    if (dom == NULL) {
1672
        remoteDispatchConnError(rerr, conn);
1673 1674
        return -1;
    }
1675 1676

    make_nonnull_domain (&ret->dom, dom);
1677
    virDomainFree(dom);
1678 1679 1680 1681
    return 0;
}

static int
1682
remoteDispatchDomainLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
1683 1684
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1685
                                  remote_error *rerr,
1686 1687 1688 1689 1690
                                  remote_domain_lookup_by_uuid_args *args,
                                  remote_domain_lookup_by_uuid_ret *ret)
{
    virDomainPtr dom;

1691
    dom = virDomainLookupByUUID (conn, (unsigned char *) args->uuid);
1692
    if (dom == NULL) {
1693
        remoteDispatchConnError(rerr, conn);
1694 1695
        return -1;
    }
1696 1697

    make_nonnull_domain (&ret->dom, dom);
1698
    virDomainFree(dom);
1699 1700 1701 1702
    return 0;
}

static int
1703
remoteDispatchNumOfDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
1704 1705
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1706
                                   remote_error *rerr,
1707 1708 1709 1710
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_num_of_defined_domains_ret *ret)
{

1711
    ret->num = virConnectNumOfDefinedDomains (conn);
1712
    if (ret->num == -1) {
1713
        remoteDispatchConnError(rerr, conn);
1714 1715
        return -1;
    }
1716 1717 1718 1719 1720

    return 0;
}

static int
1721
remoteDispatchDomainPinVcpu (struct qemud_server *server ATTRIBUTE_UNUSED,
1722 1723
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1724
                             remote_error *rerr,
1725 1726 1727 1728 1729 1730
                             remote_domain_pin_vcpu_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int rv;

1731
    dom = get_nonnull_domain (conn, args->dom);
1732
    if (dom == NULL) {
1733
        remoteDispatchConnError(rerr, conn);
1734
        return -1;
1735 1736 1737
    }

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
1738
        virDomainFree(dom);
1739 1740
        remoteDispatchFormatError (rerr, "%s", _("cpumap_len > REMOTE_CPUMAP_MAX"));
        return -1;
1741 1742 1743 1744 1745
    }

    rv = virDomainPinVcpu (dom, args->vcpu,
                           (unsigned char *) args->cpumap.cpumap_val,
                           args->cpumap.cpumap_len);
1746 1747
    if (rv == -1) {
        virDomainFree(dom);
1748
        remoteDispatchConnError(rerr, conn);
1749 1750 1751
        return -1;
    }
    virDomainFree(dom);
1752 1753 1754 1755
    return 0;
}

static int
1756
remoteDispatchDomainReboot (struct qemud_server *server ATTRIBUTE_UNUSED,
1757 1758
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
1759
                            remote_error *rerr,
1760 1761 1762 1763 1764
                            remote_domain_reboot_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1765
    dom = get_nonnull_domain (conn, args->dom);
1766
    if (dom == NULL) {
1767
        remoteDispatchConnError(rerr, conn);
1768
        return -1;
1769 1770
    }

1771 1772
    if (virDomainReboot (dom, args->flags) == -1) {
        virDomainFree(dom);
1773
        remoteDispatchConnError(rerr, conn);
1774
        return -1;
1775 1776
    }
    virDomainFree(dom);
1777 1778 1779 1780
    return 0;
}

static int
1781
remoteDispatchDomainRestore (struct qemud_server *server ATTRIBUTE_UNUSED,
1782 1783
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1784
                             remote_error *rerr,
1785 1786 1787 1788
                             remote_domain_restore_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{

1789 1790
    if (virDomainRestore (conn, args->from) == -1) {
        remoteDispatchConnError(rerr, conn);
1791
        return -1;
1792
    }
1793 1794 1795 1796 1797

    return 0;
}

static int
1798
remoteDispatchDomainResume (struct qemud_server *server ATTRIBUTE_UNUSED,
1799 1800
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
1801
                            remote_error *rerr,
1802 1803 1804 1805 1806
                            remote_domain_resume_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1807
    dom = get_nonnull_domain (conn, args->dom);
1808
    if (dom == NULL) {
1809
        remoteDispatchConnError(rerr, conn);
1810
        return -1;
1811 1812
    }

1813 1814
    if (virDomainResume (dom) == -1) {
        virDomainFree(dom);
1815
        remoteDispatchConnError(rerr, conn);
1816
        return -1;
1817 1818
    }
    virDomainFree(dom);
1819 1820 1821 1822
    return 0;
}

static int
1823
remoteDispatchDomainSave (struct qemud_server *server ATTRIBUTE_UNUSED,
1824 1825
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
1826
                          remote_error *rerr,
1827 1828 1829 1830 1831
                          remote_domain_save_args *args,
                          void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1832
    dom = get_nonnull_domain (conn, args->dom);
1833
    if (dom == NULL) {
1834
        remoteDispatchConnError(rerr, conn);
1835
        return -1;
1836 1837
    }

1838 1839
    if (virDomainSave (dom, args->to) == -1) {
        virDomainFree(dom);
1840
        remoteDispatchConnError(rerr, conn);
1841
        return -1;
1842 1843
    }
    virDomainFree(dom);
1844 1845 1846 1847
    return 0;
}

static int
1848
remoteDispatchDomainCoreDump (struct qemud_server *server ATTRIBUTE_UNUSED,
1849 1850
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
1851
                              remote_error *rerr,
1852 1853 1854 1855 1856
                              remote_domain_core_dump_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1857
    dom = get_nonnull_domain (conn, args->dom);
1858
    if (dom == NULL) {
1859
        remoteDispatchConnError(rerr, conn);
1860
        return -1;
1861 1862
    }

1863 1864
    if (virDomainCoreDump (dom, args->to, args->flags) == -1) {
        virDomainFree(dom);
1865
        remoteDispatchConnError(rerr, conn);
1866
        return -1;
1867 1868
    }
    virDomainFree(dom);
1869 1870 1871 1872
    return 0;
}

static int
1873
remoteDispatchDomainSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
1874 1875
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1876
                                  remote_error *rerr,
1877 1878 1879 1880 1881
                                  remote_domain_set_autostart_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1882
    dom = get_nonnull_domain (conn, args->dom);
1883
    if (dom == NULL) {
1884
        remoteDispatchConnError(rerr, conn);
1885
        return -1;
1886 1887
    }

1888 1889
    if (virDomainSetAutostart (dom, args->autostart) == -1) {
        virDomainFree(dom);
1890
        remoteDispatchConnError(rerr, conn);
1891
        return -1;
1892 1893
    }
    virDomainFree(dom);
1894 1895 1896 1897
    return 0;
}

static int
1898
remoteDispatchDomainSetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
1899 1900
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1901
                                  remote_error *rerr,
1902 1903 1904 1905 1906
                                  remote_domain_set_max_memory_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1907
    dom = get_nonnull_domain (conn, args->dom);
1908
    if (dom == NULL) {
1909
        remoteDispatchConnError(rerr, conn);
1910
        return -1;
1911 1912
    }

1913 1914
    if (virDomainSetMaxMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
1915
        remoteDispatchConnError(rerr, conn);
1916
        return -1;
1917 1918
    }
    virDomainFree(dom);
1919 1920 1921 1922
    return 0;
}

static int
1923
remoteDispatchDomainSetMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
1924 1925
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1926
                               remote_error *rerr,
1927 1928 1929 1930 1931
                               remote_domain_set_memory_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1932
    dom = get_nonnull_domain (conn, args->dom);
1933
    if (dom == NULL) {
1934
        remoteDispatchConnError(rerr, conn);
1935
        return -1;
1936 1937
    }

1938 1939
    if (virDomainSetMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
1940
        remoteDispatchConnError(rerr, conn);
1941
        return -1;
1942 1943
    }
    virDomainFree(dom);
1944 1945 1946 1947
    return 0;
}

static int
1948
remoteDispatchDomainSetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1949 1950
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
1951
                              remote_error *rerr,
1952 1953 1954 1955 1956
                              remote_domain_set_vcpus_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1957
    dom = get_nonnull_domain (conn, args->dom);
1958
    if (dom == NULL) {
1959
        remoteDispatchConnError(rerr, conn);
1960
        return -1;
1961 1962
    }

1963 1964
    if (virDomainSetVcpus (dom, args->nvcpus) == -1) {
        virDomainFree(dom);
1965
        remoteDispatchConnError(rerr, conn);
1966
        return -1;
1967 1968
    }
    virDomainFree(dom);
1969 1970 1971 1972
    return 0;
}

static int
1973
remoteDispatchDomainShutdown (struct qemud_server *server ATTRIBUTE_UNUSED,
1974 1975
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
1976
                              remote_error *rerr,
1977 1978 1979 1980 1981
                              remote_domain_shutdown_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1982
    dom = get_nonnull_domain (conn, args->dom);
1983
    if (dom == NULL) {
1984
        remoteDispatchConnError(rerr, conn);
1985
        return -1;
1986 1987
    }

1988 1989
    if (virDomainShutdown (dom) == -1) {
        virDomainFree(dom);
1990
        remoteDispatchConnError(rerr, conn);
1991
        return -1;
1992 1993
    }
    virDomainFree(dom);
1994 1995 1996 1997
    return 0;
}

static int
1998
remoteDispatchDomainSuspend (struct qemud_server *server ATTRIBUTE_UNUSED,
1999 2000
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2001
                             remote_error *rerr,
2002 2003 2004 2005 2006
                             remote_domain_suspend_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2007
    dom = get_nonnull_domain (conn, args->dom);
2008
    if (dom == NULL) {
2009
        remoteDispatchConnError(rerr, conn);
2010
        return -1;
2011 2012
    }

2013 2014
    if (virDomainSuspend (dom) == -1) {
        virDomainFree(dom);
2015
        remoteDispatchConnError(rerr, conn);
2016
        return -1;
2017 2018
    }
    virDomainFree(dom);
2019 2020 2021 2022
    return 0;
}

static int
2023
remoteDispatchDomainUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
2024 2025
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2026
                              remote_error *rerr,
2027 2028 2029 2030 2031
                              remote_domain_undefine_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2032
    dom = get_nonnull_domain (conn, args->dom);
2033
    if (dom == NULL) {
2034
        remoteDispatchConnError(rerr, conn);
2035
        return -1;
2036 2037
    }

2038 2039
    if (virDomainUndefine (dom) == -1) {
        virDomainFree(dom);
2040
        remoteDispatchConnError(rerr, conn);
2041
        return -1;
2042 2043
    }
    virDomainFree(dom);
2044 2045 2046 2047
    return 0;
}

static int
2048
remoteDispatchListDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2049 2050
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2051
                                   remote_error *rerr,
2052 2053 2054 2055 2056
                                   remote_list_defined_networks_args *args,
                                   remote_list_defined_networks_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
2057 2058 2059
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
2060 2061 2062
    }

    /* Allocate return buffer. */
2063
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
2064 2065
        remoteDispatchOOMError(rerr);
        return -1;
2066
    }
2067 2068

    ret->names.names_len =
2069
        virConnectListDefinedNetworks (conn,
2070
                                       ret->names.names_val, args->maxnames);
2071 2072
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
2073
        remoteDispatchConnError(rerr, conn);
2074 2075
        return -1;
    }
2076 2077 2078 2079 2080

    return 0;
}

static int
2081
remoteDispatchListDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2082 2083
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
2084
                           remote_error *rerr,
2085 2086 2087 2088 2089
                           remote_list_domains_args *args,
                           remote_list_domains_ret *ret)
{

    if (args->maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
2090 2091 2092
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxids > REMOTE_DOMAIN_ID_LIST_MAX"));
        return -1;
2093 2094 2095
    }

    /* Allocate return buffer. */
2096
    if (VIR_ALLOC_N(ret->ids.ids_val, args->maxids) < 0) {
2097 2098
        remoteDispatchOOMError(rerr);
        return -1;
2099
    }
2100

2101
    ret->ids.ids_len = virConnectListDomains (conn,
2102
                                              ret->ids.ids_val, args->maxids);
2103 2104
    if (ret->ids.ids_len == -1) {
        VIR_FREE(ret->ids.ids_val);
2105
        remoteDispatchConnError(rerr, conn);
2106 2107
        return -1;
    }
2108 2109 2110 2111 2112

    return 0;
}

static int
2113
remoteDispatchListNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2114 2115
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2116
                            remote_error *rerr,
2117 2118 2119 2120 2121
                            remote_list_networks_args *args,
                            remote_list_networks_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
2122 2123 2124
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
2125 2126 2127
    }

    /* Allocate return buffer. */
2128
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
2129 2130
        remoteDispatchOOMError(rerr);
        return -1;
2131
    }
2132 2133

    ret->names.names_len =
2134
        virConnectListNetworks (conn,
2135
                                ret->names.names_val, args->maxnames);
2136 2137
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_len);
2138
        remoteDispatchConnError(rerr, conn);
2139 2140
        return -1;
    }
2141 2142 2143 2144 2145

    return 0;
}

static int
2146
remoteDispatchNetworkCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
2147 2148
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2149
                             remote_error *rerr,
2150 2151 2152 2153 2154
                             remote_network_create_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2155
    net = get_nonnull_network (conn, args->net);
2156
    if (net == NULL) {
2157
        remoteDispatchConnError(rerr, conn);
2158
        return -1;
2159 2160
    }

2161 2162
    if (virNetworkCreate (net) == -1) {
        virNetworkFree(net);
2163
        remoteDispatchConnError(rerr, conn);
2164
        return -1;
2165 2166
    }
    virNetworkFree(net);
2167 2168 2169 2170
    return 0;
}

static int
2171
remoteDispatchNetworkCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2172 2173
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2174
                                remote_error *rerr,
2175 2176 2177 2178 2179
                                remote_network_create_xml_args *args,
                                remote_network_create_xml_ret *ret)
{
    virNetworkPtr net;

2180
    net = virNetworkCreateXML (conn, args->xml);
2181
    if (net == NULL) {
2182
        remoteDispatchConnError(rerr, conn);
2183 2184
        return -1;
    }
2185 2186

    make_nonnull_network (&ret->net, net);
2187
    virNetworkFree(net);
2188 2189 2190 2191
    return 0;
}

static int
2192
remoteDispatchNetworkDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2193 2194
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2195
                                remote_error *rerr,
2196 2197 2198 2199 2200
                                remote_network_define_xml_args *args,
                                remote_network_define_xml_ret *ret)
{
    virNetworkPtr net;

2201
    net = virNetworkDefineXML (conn, args->xml);
2202
    if (net == NULL) {
2203
        remoteDispatchConnError(rerr, conn);
2204 2205
        return -1;
    }
2206 2207

    make_nonnull_network (&ret->net, net);
2208
    virNetworkFree(net);
2209 2210 2211 2212
    return 0;
}

static int
2213
remoteDispatchNetworkDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
2214 2215
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2216
                              remote_error *rerr,
2217 2218 2219 2220 2221
                              remote_network_destroy_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2222
    net = get_nonnull_network (conn, args->net);
2223
    if (net == NULL) {
2224
        remoteDispatchConnError(rerr, conn);
2225
        return -1;
2226 2227
    }

2228 2229
    if (virNetworkDestroy (net) == -1) {
        virNetworkFree(net);
2230
        remoteDispatchConnError(rerr, conn);
2231
        return -1;
2232 2233
    }
    virNetworkFree(net);
2234 2235 2236 2237
    return 0;
}

static int
2238
remoteDispatchNetworkDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2239 2240
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2241
                              remote_error *rerr,
2242 2243 2244 2245 2246
                              remote_network_dump_xml_args *args,
                              remote_network_dump_xml_ret *ret)
{
    virNetworkPtr net;

2247
    net = get_nonnull_network (conn, args->net);
2248
    if (net == NULL) {
2249
        remoteDispatchConnError(rerr, conn);
2250
        return -1;
2251 2252 2253 2254
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNetworkGetXMLDesc (net, args->flags);
2255 2256
    if (!ret->xml) {
        virNetworkFree(net);
2257
        remoteDispatchConnError(rerr, conn);
2258 2259 2260
        return -1;
    }
    virNetworkFree(net);
2261 2262 2263 2264
    return 0;
}

static int
2265
remoteDispatchNetworkGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
2266 2267
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2268
                                   remote_error *rerr,
2269 2270 2271 2272 2273
                                   remote_network_get_autostart_args *args,
                                   remote_network_get_autostart_ret *ret)
{
    virNetworkPtr net;

2274
    net = get_nonnull_network (conn, args->net);
2275
    if (net == NULL) {
2276
        remoteDispatchConnError(rerr, conn);
2277
        return -1;
2278 2279
    }

2280 2281
    if (virNetworkGetAutostart (net, &ret->autostart) == -1) {
        virNetworkFree(net);
2282
        remoteDispatchConnError(rerr, conn);
2283
        return -1;
2284 2285
    }
    virNetworkFree(net);
2286 2287 2288 2289
    return 0;
}

static int
2290
remoteDispatchNetworkGetBridgeName (struct qemud_server *server ATTRIBUTE_UNUSED,
2291 2292
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
2293
                                    remote_error *rerr,
2294 2295 2296 2297 2298
                                    remote_network_get_bridge_name_args *args,
                                    remote_network_get_bridge_name_ret *ret)
{
    virNetworkPtr net;

2299
    net = get_nonnull_network (conn, args->net);
2300
    if (net == NULL) {
2301
        remoteDispatchConnError(rerr, conn);
2302
        return -1;
2303 2304 2305 2306
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virNetworkGetBridgeName (net);
2307 2308
    if (!ret->name) {
        virNetworkFree(net);
2309
        remoteDispatchConnError(rerr, conn);
2310 2311 2312
        return -1;
    }
    virNetworkFree(net);
2313 2314 2315 2316
    return 0;
}

static int
2317
remoteDispatchNetworkLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
2318 2319
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2320
                                   remote_error *rerr,
2321 2322 2323 2324 2325
                                   remote_network_lookup_by_name_args *args,
                                   remote_network_lookup_by_name_ret *ret)
{
    virNetworkPtr net;

2326
    net = virNetworkLookupByName (conn, args->name);
2327
    if (net == NULL) {
2328
        remoteDispatchConnError(rerr, conn);
2329 2330
        return -1;
    }
2331 2332

    make_nonnull_network (&ret->net, net);
2333
    virNetworkFree(net);
2334 2335 2336 2337
    return 0;
}

static int
2338
remoteDispatchNetworkLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
2339 2340
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2341
                                   remote_error *rerr,
2342 2343 2344 2345 2346
                                   remote_network_lookup_by_uuid_args *args,
                                   remote_network_lookup_by_uuid_ret *ret)
{
    virNetworkPtr net;

2347
    net = virNetworkLookupByUUID (conn, (unsigned char *) args->uuid);
2348
    if (net == NULL) {
2349
        remoteDispatchConnError(rerr, conn);
2350 2351
        return -1;
    }
2352 2353

    make_nonnull_network (&ret->net, net);
2354
    virNetworkFree(net);
2355 2356 2357 2358
    return 0;
}

static int
2359
remoteDispatchNetworkSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
2360 2361
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2362
                                   remote_error *rerr,
2363 2364 2365 2366 2367
                                   remote_network_set_autostart_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2368
    net = get_nonnull_network (conn, args->net);
2369
    if (net == NULL) {
2370
        remoteDispatchConnError(rerr, conn);
2371
        return -1;
2372 2373
    }

2374 2375
    if (virNetworkSetAutostart (net, args->autostart) == -1) {
        virNetworkFree(net);
2376
        remoteDispatchConnError(rerr, conn);
2377
        return -1;
2378 2379
    }
    virNetworkFree(net);
2380 2381 2382 2383
    return 0;
}

static int
2384
remoteDispatchNetworkUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
2385 2386
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
2387
                               remote_error *rerr,
2388 2389 2390 2391 2392
                               remote_network_undefine_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2393
    net = get_nonnull_network (conn, args->net);
2394
    if (net == NULL) {
2395
        remoteDispatchConnError(rerr, conn);
2396
        return -1;
2397 2398
    }

2399 2400
    if (virNetworkUndefine (net) == -1) {
        virNetworkFree(net);
2401
        remoteDispatchConnError(rerr, conn);
2402
        return -1;
2403 2404
    }
    virNetworkFree(net);
2405 2406 2407 2408
    return 0;
}

static int
2409
remoteDispatchNumOfDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2410 2411
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
2412
                                    remote_error *rerr,
2413 2414 2415 2416
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_num_of_defined_networks_ret *ret)
{

2417
    ret->num = virConnectNumOfDefinedNetworks (conn);
2418
    if (ret->num == -1) {
2419
        remoteDispatchConnError(rerr, conn);
2420 2421
        return -1;
    }
2422 2423 2424 2425 2426

    return 0;
}

static int
2427
remoteDispatchNumOfDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2428 2429
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2430
                            remote_error *rerr,
2431 2432 2433 2434
                            void *args ATTRIBUTE_UNUSED,
                            remote_num_of_domains_ret *ret)
{

2435
    ret->num = virConnectNumOfDomains (conn);
2436
    if (ret->num == -1) {
2437
        remoteDispatchConnError(rerr, conn);
2438 2439
        return -1;
    }
2440 2441 2442 2443 2444

    return 0;
}

static int
2445
remoteDispatchNumOfNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2446 2447
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2448
                             remote_error *rerr,
2449 2450 2451 2452
                             void *args ATTRIBUTE_UNUSED,
                             remote_num_of_networks_ret *ret)
{

2453
    ret->num = virConnectNumOfNetworks (conn);
2454
    if (ret->num == -1) {
2455
        remoteDispatchConnError(rerr, conn);
2456 2457
        return -1;
    }
2458 2459 2460 2461

    return 0;
}

2462 2463

static int
2464
remoteDispatchAuthList (struct qemud_server *server,
2465
                        struct qemud_client *client,
2466
                        virConnectPtr conn ATTRIBUTE_UNUSED,
2467
                        remote_error *rerr,
2468 2469 2470 2471
                        void *args ATTRIBUTE_UNUSED,
                        remote_auth_list_ret *ret)
{
    ret->types.types_len = 1;
2472
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
2473 2474
        remoteDispatchOOMError(rerr);
        return -1;
2475
    }
2476 2477 2478
    pthread_mutex_lock(&server->lock);
    pthread_mutex_lock(&client->lock);
    pthread_mutex_unlock(&server->lock);
2479
    ret->types.types_val[0] = client->auth;
2480 2481
    pthread_mutex_unlock(&client->lock);

2482 2483 2484 2485 2486 2487 2488 2489
    return 0;
}


#if HAVE_SASL
/*
 * NB, keep in sync with similar method in src/remote_internal.c
 */
2490
static char *addrToString(remote_error *rerr,
2491 2492 2493 2494 2495 2496 2497 2498 2499
                          struct sockaddr_storage *sa, socklen_t salen) {
    char host[1024], port[20];
    char *addr;
    int err;

    if ((err = getnameinfo((struct sockaddr *)sa, salen,
                           host, sizeof(host),
                           port, sizeof(port),
                           NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
2500 2501 2502
        remoteDispatchFormatError(rerr,
                                  _("Cannot resolve address %d: %s"),
                                  err, gai_strerror(err));
2503 2504 2505
        return NULL;
    }

2506
    if (VIR_ALLOC_N(addr, strlen(host) + 1 + strlen(port) + 1) < 0) {
2507
        remoteDispatchOOMError(rerr);
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
        return NULL;
    }

    strcpy(addr, host);
    strcat(addr, ";");
    strcat(addr, port);
    return addr;
}


/*
 * Initializes the SASL session in prepare for authentication
2520
 * and gives the client a list of allowed mechanisms to choose
2521 2522 2523 2524
 *
 * XXX callbacks for stuff like password verification ?
 */
static int
2525
remoteDispatchAuthSaslInit (struct qemud_server *server,
2526
                            struct qemud_client *client,
2527
                            virConnectPtr conn ATTRIBUTE_UNUSED,
2528
                            remote_error *rerr,
2529 2530 2531 2532
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret)
{
    const char *mechlist = NULL;
2533
    sasl_security_properties_t secprops;
2534 2535 2536 2537 2538
    int err;
    struct sockaddr_storage sa;
    socklen_t salen;
    char *localAddr, *remoteAddr;

2539 2540 2541 2542
    pthread_mutex_lock(&server->lock);
    pthread_mutex_lock(&client->lock);
    pthread_mutex_unlock(&server->lock);

2543 2544 2545
    REMOTE_DEBUG("Initialize SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn != NULL) {
J
Jim Meyering 已提交
2546
        qemudLog(QEMUD_ERR, "%s", _("client tried invalid SASL init request"));
2547
        goto authfail;
2548 2549 2550 2551 2552
    }

    /* Get local address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getsockname(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
2553 2554 2555
        remoteDispatchFormatError(rerr,
                                  _("failed to get sock address %d (%s)"),
                                  errno, strerror(errno));
2556
        goto error;
2557
    }
2558
    if ((localAddr = addrToString(rerr, &sa, salen)) == NULL) {
2559
        goto error;
2560 2561 2562 2563 2564
    }

    /* Get remote address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getpeername(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
2565 2566
        remoteDispatchFormatError(rerr, _("failed to get peer address %d (%s)"),
                                  errno, strerror(errno));
2567
        VIR_FREE(localAddr);
2568
        goto error;
2569
    }
2570
    if ((remoteAddr = addrToString(rerr, &sa, salen)) == NULL) {
2571
        VIR_FREE(localAddr);
2572
        goto error;
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582
    }

    err = sasl_server_new("libvirt",
                          NULL, /* FQDN - just delegates to gethostname */
                          NULL, /* User realm */
                          localAddr,
                          remoteAddr,
                          NULL, /* XXX Callbacks */
                          SASL_SUCCESS_DATA,
                          &client->saslconn);
2583 2584
    VIR_FREE(localAddr);
    VIR_FREE(remoteAddr);
2585
    if (err != SASL_OK) {
2586
        qemudLog(QEMUD_ERR, _("sasl context setup failed %d (%s)"),
2587 2588
                 err, sasl_errstring(err, NULL, NULL));
        client->saslconn = NULL;
2589
        goto authfail;
2590 2591
    }

2592 2593 2594 2595 2596 2597 2598
    /* Inform SASL that we've got an external SSF layer from TLS */
    if (client->type == QEMUD_SOCK_TYPE_TLS) {
        gnutls_cipher_algorithm_t cipher;
        sasl_ssf_t ssf;

        cipher = gnutls_cipher_get(client->tlssession);
        if (!(ssf = (sasl_ssf_t)gnutls_cipher_get_key_size(cipher))) {
J
Jim Meyering 已提交
2599
            qemudLog(QEMUD_ERR, "%s", _("cannot TLS get cipher size"));
2600 2601
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
2602
            goto authfail;
2603 2604 2605 2606 2607
        }
        ssf *= 8; /* tls key size is bytes, sasl wants bits */

        err = sasl_setprop(client->saslconn, SASL_SSF_EXTERNAL, &ssf);
        if (err != SASL_OK) {
2608
            qemudLog(QEMUD_ERR, _("cannot set SASL external SSF %d (%s)"),
2609 2610 2611
                     err, sasl_errstring(err, NULL, NULL));
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
2612
            goto authfail;
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
        }
    }

    memset (&secprops, 0, sizeof secprops);
    if (client->type == QEMUD_SOCK_TYPE_TLS ||
        client->type == QEMUD_SOCK_TYPE_UNIX) {
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
        secprops.min_ssf = 0;
        secprops.max_ssf = 0;
        secprops.maxbufsize = 8192;
        secprops.security_flags = 0;
    } else {
        /* Plain TCP, better get an SSF layer */
        secprops.min_ssf = 56; /* Good enough to require kerberos */
        secprops.max_ssf = 100000; /* Arbitrary big number */
        secprops.maxbufsize = 8192;
        /* Forbid any anonymous or trivially crackable auth */
        secprops.security_flags =
            SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT;
    }

    err = sasl_setprop(client->saslconn, SASL_SEC_PROPS, &secprops);
    if (err != SASL_OK) {
2636
        qemudLog(QEMUD_ERR, _("cannot set SASL security props %d (%s)"),
2637 2638 2639
                 err, sasl_errstring(err, NULL, NULL));
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
2640
        goto authfail;
2641 2642
    }

2643 2644 2645 2646 2647 2648 2649 2650 2651
    err = sasl_listmech(client->saslconn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
2652
        qemudLog(QEMUD_ERR, _("cannot list SASL mechanisms %d (%s)"),
2653 2654 2655
                 err, sasl_errdetail(client->saslconn));
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
2656
        goto authfail;
2657 2658 2659 2660
    }
    REMOTE_DEBUG("Available mechanisms for client: '%s'", mechlist);
    ret->mechlist = strdup(mechlist);
    if (!ret->mechlist) {
J
Jim Meyering 已提交
2661
        qemudLog(QEMUD_ERR, "%s", _("cannot allocate mechlist"));
2662 2663
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
2664
        goto authfail;
2665 2666
    }

2667
    pthread_mutex_unlock(&client->lock);
2668
    return 0;
2669 2670 2671 2672 2673 2674

authfail:
    remoteDispatchAuthError(rerr);
error:
    pthread_mutex_unlock(&client->lock);
    return -1;
2675 2676 2677
}


2678
/* We asked for an SSF layer, so sanity check that we actually
2679 2680 2681
 * got what we asked for */
static int
remoteSASLCheckSSF (struct qemud_client *client,
2682
                    remote_error *rerr) {
2683 2684 2685 2686 2687 2688 2689 2690 2691
    const void *val;
    int err, ssf;

    if (client->type == QEMUD_SOCK_TYPE_TLS ||
        client->type == QEMUD_SOCK_TYPE_UNIX)
        return 0; /* TLS or UNIX domain sockets trivially OK */

    err = sasl_getprop(client->saslconn, SASL_SSF, &val);
    if (err != SASL_OK) {
2692
        qemudLog(QEMUD_ERR, _("cannot query SASL ssf on connection %d (%s)"),
2693
                 err, sasl_errstring(err, NULL, NULL));
2694
        remoteDispatchAuthError(rerr);
2695 2696 2697 2698 2699 2700 2701
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    ssf = *(const int *)val;
    REMOTE_DEBUG("negotiated an SSF of %d", ssf);
    if (ssf < 56) { /* 56 is good for Kerberos */
2702
        qemudLog(QEMUD_ERR, _("negotiated SSF %d was not strong enough"), ssf);
2703
        remoteDispatchAuthError(rerr);
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }

    /* Only setup for read initially, because we're about to send an RPC
     * reply which must be in plain text. When the next incoming RPC
     * arrives, we'll switch on writes too
     *
     * cf qemudClientReadSASL  in qemud.c
     */
    client->saslSSF = QEMUD_SASL_SSF_READ;

    /* We have a SSF !*/
    return 0;
}

2721 2722 2723
static int
remoteSASLCheckAccess (struct qemud_server *server,
                       struct qemud_client *client,
2724
                       remote_error *rerr) {
2725 2726 2727 2728 2729 2730
    const void *val;
    int err;
    char **wildcards;

    err = sasl_getprop(client->saslconn, SASL_USERNAME, &val);
    if (err != SASL_OK) {
2731 2732
        qemudLog(QEMUD_ERR,
                 _("cannot query SASL username on connection %d (%s)"),
2733
                 err, sasl_errstring(err, NULL, NULL));
2734
        remoteDispatchAuthError(rerr);
2735 2736 2737 2738 2739
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    if (val == NULL) {
J
Jim Meyering 已提交
2740
        qemudLog(QEMUD_ERR, "%s", _("no client username was found"));
2741
        remoteDispatchAuthError(rerr);
2742 2743 2744 2745 2746 2747 2748 2749
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    REMOTE_DEBUG("SASL client username %s", (const char *)val);

    client->saslUsername = strdup((const char*)val);
    if (client->saslUsername == NULL) {
J
Jim Meyering 已提交
2750
        qemudLog(QEMUD_ERR, "%s", _("out of memory copying username"));
2751
        remoteDispatchAuthError(rerr);
2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }

    /* If the list is not set, allow any DN. */
    wildcards = server->saslUsernameWhitelist;
    if (!wildcards)
        return 0; /* No ACL, allow all */

    while (*wildcards) {
        if (fnmatch (*wildcards, client->saslUsername, 0) == 0)
            return 0; /* Allowed */
        wildcards++;
    }

    /* Denied */
2769 2770
    qemudLog(QEMUD_ERR, _("SASL client %s not allowed in whitelist"),
             client->saslUsername);
2771
    remoteDispatchAuthError(rerr);
2772 2773 2774 2775 2776 2777
    sasl_dispose(&client->saslconn);
    client->saslconn = NULL;
    return -1;
}


2778 2779 2780 2781
/*
 * This starts the SASL authentication negotiation.
 */
static int
2782 2783
remoteDispatchAuthSaslStart (struct qemud_server *server,
                             struct qemud_client *client,
2784
                             virConnectPtr conn ATTRIBUTE_UNUSED,
2785
                             remote_error *rerr,
2786 2787 2788 2789 2790 2791 2792
                             remote_auth_sasl_start_args *args,
                             remote_auth_sasl_start_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

2793 2794 2795 2796
    pthread_mutex_lock(&server->lock);
    pthread_mutex_lock(&client->lock);
    pthread_mutex_unlock(&server->lock);

2797 2798 2799
    REMOTE_DEBUG("Start SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
J
Jim Meyering 已提交
2800
        qemudLog(QEMUD_ERR, "%s", _("client tried invalid SASL start request"));
2801
        goto authfail;
2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814
    }

    REMOTE_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
                 args->mech, args->data.data_len, args->nil);
    err = sasl_server_start(client->saslconn,
                            args->mech,
                            /* NB, distinction of NULL vs "" is *critical* in SASL */
                            args->nil ? NULL : args->data.data_val,
                            args->data.data_len,
                            &serverout,
                            &serveroutlen);
    if (err != SASL_OK &&
        err != SASL_CONTINUE) {
2815
        qemudLog(QEMUD_ERR, _("sasl start failed %d (%s)"),
2816 2817 2818
                 err, sasl_errdetail(client->saslconn));
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
2819
        goto authfail;
2820 2821
    }
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2822 2823
        qemudLog(QEMUD_ERR, _("sasl start reply data too long %d"),
                 serveroutlen);
2824 2825
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
2826
        goto authfail;
2827 2828 2829 2830
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2831
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
2832
            remoteDispatchOOMError(rerr);
2833
            goto error;
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
        }
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

    REMOTE_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
    if (err == SASL_CONTINUE) {
        ret->complete = 0;
    } else {
2846
        if (remoteSASLCheckSSF(client, rerr) < 0)
2847
            goto error;
2848

2849
        /* Check username whitelist ACL */
2850
        if (remoteSASLCheckAccess(server, client, rerr) < 0)
2851
            goto error;
2852

2853 2854 2855 2856 2857
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

2858
    pthread_mutex_unlock(&client->lock);
2859
    return 0;
2860 2861 2862 2863 2864 2865

authfail:
    remoteDispatchAuthError(rerr);
error:
    pthread_mutex_unlock(&client->lock);
    return -1;
2866 2867 2868 2869
}


static int
2870 2871
remoteDispatchAuthSaslStep (struct qemud_server *server,
                            struct qemud_client *client,
2872
                            virConnectPtr conn ATTRIBUTE_UNUSED,
2873
                            remote_error *rerr,
2874 2875 2876 2877 2878 2879 2880
                            remote_auth_sasl_step_args *args,
                            remote_auth_sasl_step_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

2881 2882 2883 2884
    pthread_mutex_lock(&server->lock);
    pthread_mutex_lock(&client->lock);
    pthread_mutex_unlock(&server->lock);

2885 2886 2887
    REMOTE_DEBUG("Step SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
J
Jim Meyering 已提交
2888
        qemudLog(QEMUD_ERR, "%s", _("client tried invalid SASL start request"));
2889
        goto authfail;
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901
    }

    REMOTE_DEBUG("Using SASL Data %d bytes, nil: %d",
                 args->data.data_len, args->nil);
    err = sasl_server_step(client->saslconn,
                           /* NB, distinction of NULL vs "" is *critical* in SASL */
                           args->nil ? NULL : args->data.data_val,
                           args->data.data_len,
                           &serverout,
                           &serveroutlen);
    if (err != SASL_OK &&
        err != SASL_CONTINUE) {
2902
        qemudLog(QEMUD_ERR, _("sasl step failed %d (%s)"),
2903 2904 2905
                 err, sasl_errdetail(client->saslconn));
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
2906
        goto authfail;
2907 2908 2909
    }

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2910 2911
        qemudLog(QEMUD_ERR, _("sasl step reply data too long %d"),
                 serveroutlen);
2912 2913
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
2914
        goto authfail;
2915 2916 2917 2918
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2919
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
2920
            remoteDispatchOOMError(rerr);
2921
            goto error;
2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933
        }
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

    REMOTE_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
    if (err == SASL_CONTINUE) {
        ret->complete = 0;
    } else {
2934
        if (remoteSASLCheckSSF(client, rerr) < 0)
2935
            goto error;
2936

2937
        /* Check username whitelist ACL */
2938
        if (remoteSASLCheckAccess(server, client, rerr) < 0)
2939
            goto error;
2940

2941 2942 2943 2944 2945
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

2946
    pthread_mutex_unlock(&client->lock);
2947
    return 0;
2948 2949 2950 2951 2952 2953

authfail:
    remoteDispatchAuthError(rerr);
error:
    pthread_mutex_unlock(&client->lock);
    return -1;
2954 2955 2956 2957 2958
}


#else /* HAVE_SASL */
static int
2959
remoteDispatchAuthSaslInit (struct qemud_server *server ATTRIBUTE_UNUSED,
2960 2961
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
2962
                            remote_error *rerr,
2963 2964 2965
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
J
Jim Meyering 已提交
2966
    qemudLog(QEMUD_ERR, "%s", _("client tried unsupported SASL init request"));
2967
    remoteDispatchAuthError(rerr);
2968 2969 2970 2971
    return -1;
}

static int
2972
remoteDispatchAuthSaslStart (struct qemud_server *server ATTRIBUTE_UNUSED,
2973 2974
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn ATTRIBUTE_UNUSED,
2975
                             remote_error *rerr,
2976 2977 2978
                             remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                             remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
J
Jim Meyering 已提交
2979
    qemudLog(QEMUD_ERR, "%s", _("client tried unsupported SASL start request"));
2980
    remoteDispatchAuthError(rerr);
2981 2982 2983 2984
    return -1;
}

static int
2985
remoteDispatchAuthSaslStep (struct qemud_server *server ATTRIBUTE_UNUSED,
2986 2987
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
2988
                            remote_error *rerr,
2989 2990 2991
                            remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
J
Jim Meyering 已提交
2992
    qemudLog(QEMUD_ERR, "%s", _("client tried unsupported SASL step request"));
2993
    remoteDispatchAuthError(rerr);
2994 2995 2996 2997 2998
    return -1;
}
#endif /* HAVE_SASL */


2999 3000
#if HAVE_POLKIT
static int
3001
remoteDispatchAuthPolkit (struct qemud_server *server,
3002
                          struct qemud_client *client,
3003
                          virConnectPtr conn ATTRIBUTE_UNUSED,
3004
                          remote_error *rerr,
3005 3006 3007 3008 3009
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
    pid_t callerPid;
    uid_t callerUid;
3010 3011 3012 3013 3014 3015
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
3016 3017 3018 3019 3020 3021 3022
    const char *action;

    pthread_mutex_lock(&server->lock);
    pthread_mutex_lock(&client->lock);
    pthread_mutex_unlock(&server->lock);

    action = client->readonly ?
3023 3024
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
3025 3026 3027

    REMOTE_DEBUG("Start PolicyKit auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_POLKIT) {
J
Jim Meyering 已提交
3028 3029
        qemudLog(QEMUD_ERR,
                 "%s", _("client tried invalid PolicyKit init request"));
3030
        goto authfail;
3031 3032 3033
    }

    if (qemudGetSocketIdentity(client->fd, &callerUid, &callerPid) < 0) {
J
Jim Meyering 已提交
3034
        qemudLog(QEMUD_ERR, "%s", _("cannot get peer socket identity"));
3035
        goto authfail;
3036 3037
    }

3038 3039 3040 3041 3042 3043 3044 3045
    qemudLog(QEMUD_INFO, _("Checking PID %d running as %d"),
             callerPid, callerUid);
    dbus_error_init(&err);
    if (!(pkcaller = polkit_caller_new_from_pid(server->sysbus,
                                                callerPid, &err))) {
        qemudLog(QEMUD_ERR, _("Failed to lookup policy kit caller: %s"),
                 err.message);
        dbus_error_free(&err);
3046
        goto authfail;
3047
    }
3048

3049 3050 3051 3052
    if (!(pkaction = polkit_action_new())) {
        qemudLog(QEMUD_ERR, _("Failed to create polkit action %s\n"),
                 strerror(errno));
        polkit_caller_unref(pkcaller);
3053
        goto authfail;
3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
        qemudLog(QEMUD_ERR, _("Failed to create polkit context %s\n"),
                 (pkerr ? polkit_error_get_error_message(pkerr)
                  : strerror(errno)));
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
3067
        goto authfail;
3068
    }
3069

3070
#if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
        qemudLog(QEMUD_ERR,
                 _("Policy kit failed to check authorization %d %s"),
                 polkit_error_get_error_code(pkerr),
                 polkit_error_get_error_message(pkerr));
3081
        goto authfail;
3082
    }
3083
#else
3084 3085 3086
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
3087
#endif
3088 3089 3090 3091 3092 3093 3094
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
        qemudLog(QEMUD_ERR,
                 _("Policy kit denied action %s from pid %d, uid %d,"
                   " result: %s\n"),
3095 3096
                 action, callerPid, callerUid,
                 polkit_result_to_string_representation(pkresult));
3097
        goto authfail;
3098
    }
3099 3100 3101 3102 3103 3104
    qemudLog(QEMUD_INFO,
             _("Policy allowed action %s from pid %d, uid %d, result %s"),
             action, callerPid, callerUid,
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;
3105

3106
    pthread_mutex_unlock(&client->lock);
3107
    return 0;
3108 3109 3110 3111 3112

authfail:
    remoteDispatchAuthError(rerr);
    pthread_mutex_unlock(&client->lock);
    return -1;
3113 3114 3115 3116 3117
}

#else /* HAVE_POLKIT */

static int
3118
remoteDispatchAuthPolkit (struct qemud_server *server ATTRIBUTE_UNUSED,
3119 3120
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
3121 3122 3123
                          remote_error *rerr,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
3124
{
J
Jim Meyering 已提交
3125 3126
    qemudLog(QEMUD_ERR,
             "%s", _("client tried unsupported PolicyKit init request"));
3127
    remoteDispatchAuthError(rerr);
3128 3129 3130 3131
    return -1;
}
#endif /* HAVE_POLKIT */

3132 3133 3134 3135 3136 3137 3138 3139

/***************************************************************
 *     STORAGE POOL APIS
 ***************************************************************/


static int
remoteDispatchListDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
3140 3141
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3142
                                       remote_error *rerr,
3143 3144 3145 3146 3147
                                       remote_list_defined_storage_pools_args *args,
                                       remote_list_defined_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
3148 3149 3150
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
3151 3152 3153
    }

    /* Allocate return buffer. */
3154
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
3155 3156
        remoteDispatchOOMError(rerr);
        return -1;
3157
    }
3158 3159

    ret->names.names_len =
3160
        virConnectListDefinedStoragePools (conn,
3161 3162 3163
                                           ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
3164
        remoteDispatchConnError(rerr, conn);
3165 3166
        return -1;
    }
3167 3168 3169 3170 3171 3172

    return 0;
}

static int
remoteDispatchListStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
3173 3174
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
3175
                                remote_error *rerr,
3176 3177 3178 3179 3180
                                remote_list_storage_pools_args *args,
                                remote_list_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
3181 3182 3183
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX"));
        return -1;
3184 3185 3186
    }

    /* Allocate return buffer. */
3187
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
3188 3189
        remoteDispatchOOMError(rerr);
        return -1;
3190
    }
3191 3192

    ret->names.names_len =
3193
        virConnectListStoragePools (conn,
3194
                                ret->names.names_val, args->maxnames);
3195 3196
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
3197
        remoteDispatchConnError(rerr, conn);
3198 3199
        return -1;
    }
3200 3201 3202 3203

    return 0;
}

3204 3205
static int
remoteDispatchFindStoragePoolSources (struct qemud_server *server ATTRIBUTE_UNUSED,
3206 3207
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
3208
                                      remote_error *rerr,
3209 3210 3211 3212
                                      remote_find_storage_pool_sources_args *args,
                                      remote_find_storage_pool_sources_ret *ret)
{
    ret->xml =
3213
        virConnectFindStoragePoolSources (conn,
3214 3215 3216
                                          args->type,
                                          args->srcSpec ? *args->srcSpec : NULL,
                                          args->flags);
3217
    if (ret->xml == NULL) {
3218
        remoteDispatchConnError(rerr, conn);
3219
        return -1;
3220
    }
3221 3222 3223 3224 3225

    return 0;
}


3226 3227
static int
remoteDispatchStoragePoolCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
3228 3229
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
3230
                                 remote_error *rerr,
3231 3232 3233 3234 3235
                                 remote_storage_pool_create_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

3236
    pool = get_nonnull_storage_pool (conn, args->pool);
3237
    if (pool == NULL) {
3238
        remoteDispatchConnError(rerr, conn);
3239
        return -1;
3240 3241 3242 3243
    }

    if (virStoragePoolCreate (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
3244
        remoteDispatchConnError(rerr, conn);
3245 3246 3247 3248 3249 3250 3251 3252
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
3253 3254
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
3255
                                    remote_error *rerr,
3256 3257 3258 3259 3260
                                    remote_storage_pool_create_xml_args *args,
                                    remote_storage_pool_create_xml_ret *ret)
{
    virStoragePoolPtr pool;

3261
    pool = virStoragePoolCreateXML (conn, args->xml, args->flags);
3262
    if (pool == NULL) {
3263
        remoteDispatchConnError(rerr, conn);
3264 3265
        return -1;
    }
3266 3267 3268 3269 3270 3271 3272 3273

    make_nonnull_storage_pool (&ret->pool, pool);
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
3274 3275
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
3276
                                    remote_error *rerr,
3277 3278 3279 3280 3281
                                    remote_storage_pool_define_xml_args *args,
                                    remote_storage_pool_define_xml_ret *ret)
{
    virStoragePoolPtr pool;

3282
    pool = virStoragePoolDefineXML (conn, args->xml, args->flags);
3283
    if (pool == NULL) {
3284
        remoteDispatchConnError(rerr, conn);
3285 3286
        return -1;
    }
3287 3288 3289 3290 3291 3292 3293 3294

    make_nonnull_storage_pool (&ret->pool, pool);
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolBuild (struct qemud_server *server ATTRIBUTE_UNUSED,
3295 3296
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
3297 3298 3299
                                remote_error *rerr,
                                remote_storage_pool_build_args *args,
                                void *ret ATTRIBUTE_UNUSED)
3300 3301 3302
{
    virStoragePoolPtr pool;

3303
    pool = get_nonnull_storage_pool (conn, args->pool);
3304
    if (pool == NULL) {
3305
        remoteDispatchConnError(rerr, conn);
3306
        return -1;
3307 3308 3309 3310
    }

    if (virStoragePoolBuild (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
3311
        remoteDispatchConnError(rerr, conn);
3312 3313 3314 3315 3316 3317 3318 3319 3320
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
3321 3322
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3323
                                  remote_error *rerr,
3324 3325 3326 3327 3328
                                  remote_storage_pool_destroy_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

3329
    pool = get_nonnull_storage_pool (conn, args->pool);
3330
    if (pool == NULL) {
3331
        remoteDispatchConnError(rerr, conn);
3332
        return -1;
3333 3334 3335 3336
    }

    if (virStoragePoolDestroy (pool) == -1) {
        virStoragePoolFree(pool);
3337
        remoteDispatchConnError(rerr, conn);
3338 3339 3340 3341 3342 3343 3344 3345
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
3346 3347
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
3348
                                 remote_error *rerr,
3349 3350 3351 3352 3353
                                 remote_storage_pool_delete_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

3354
    pool = get_nonnull_storage_pool (conn, args->pool);
3355
    if (pool == NULL) {
3356
        remoteDispatchConnError(rerr, conn);
3357
        return -1;
3358 3359 3360 3361
    }

    if (virStoragePoolDelete (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
3362
        remoteDispatchConnError(rerr, conn);
3363 3364 3365 3366 3367 3368 3369 3370
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolRefresh (struct qemud_server *server ATTRIBUTE_UNUSED,
3371 3372
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3373
                                  remote_error *rerr,
3374 3375 3376 3377 3378
                                  remote_storage_pool_refresh_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

3379
    pool = get_nonnull_storage_pool (conn, args->pool);
3380
    if (pool == NULL) {
3381
        remoteDispatchConnError(rerr, conn);
3382
        return -1;
3383 3384 3385 3386
    }

    if (virStoragePoolRefresh (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
3387
        remoteDispatchConnError(rerr, conn);
3388 3389 3390 3391 3392 3393 3394 3395
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
3396 3397
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3398
                                  remote_error *rerr,
3399 3400 3401 3402 3403 3404
                                  remote_storage_pool_get_info_args *args,
                                  remote_storage_pool_get_info_ret *ret)
{
    virStoragePoolPtr pool;
    virStoragePoolInfo info;

3405
    pool = get_nonnull_storage_pool (conn, args->pool);
3406
    if (pool == NULL) {
3407
        remoteDispatchConnError(rerr, conn);
3408
        return -1;
3409 3410 3411 3412
    }

    if (virStoragePoolGetInfo (pool, &info) == -1) {
        virStoragePoolFree(pool);
3413
        remoteDispatchConnError(rerr, conn);
3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
        return -1;
    }

    ret->state = info.state;
    ret->capacity = info.capacity;
    ret->allocation = info.allocation;
    ret->available = info.available;

    virStoragePoolFree(pool);

    return 0;
}

static int
remoteDispatchStoragePoolDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
3429 3430
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3431
                                  remote_error *rerr,
3432 3433 3434 3435 3436
                                  remote_storage_pool_dump_xml_args *args,
                                  remote_storage_pool_dump_xml_ret *ret)
{
    virStoragePoolPtr pool;

3437
    pool = get_nonnull_storage_pool (conn, args->pool);
3438
    if (pool == NULL) {
3439
        remoteDispatchConnError(rerr, conn);
3440
        return -1;
3441 3442 3443 3444 3445 3446
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStoragePoolGetXMLDesc (pool, args->flags);
    if (!ret->xml) {
        virStoragePoolFree(pool);
3447
        remoteDispatchConnError(rerr, conn);
3448 3449 3450 3451 3452 3453 3454 3455
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
3456 3457
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3458
                                       remote_error *rerr,
3459 3460 3461 3462 3463
                                       remote_storage_pool_get_autostart_args *args,
                                       remote_storage_pool_get_autostart_ret *ret)
{
    virStoragePoolPtr pool;

3464
    pool = get_nonnull_storage_pool (conn, args->pool);
3465
    if (pool == NULL) {
3466
        remoteDispatchConnError(rerr, conn);
3467
        return -1;
3468 3469 3470 3471
    }

    if (virStoragePoolGetAutostart (pool, &ret->autostart) == -1) {
        virStoragePoolFree(pool);
3472
        remoteDispatchConnError(rerr, conn);
3473 3474 3475 3476 3477 3478 3479 3480 3481
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
3482 3483
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3484
                                       remote_error *rerr,
3485 3486 3487 3488 3489
                                       remote_storage_pool_lookup_by_name_args *args,
                                       remote_storage_pool_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;

3490
    pool = virStoragePoolLookupByName (conn, args->name);
3491
    if (pool == NULL) {
3492
        remoteDispatchConnError(rerr, conn);
3493 3494
        return -1;
    }
3495 3496 3497 3498 3499 3500 3501 3502

    make_nonnull_storage_pool (&ret->pool, pool);
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
3503 3504
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3505
                                       remote_error *rerr,
3506 3507 3508 3509 3510
                                       remote_storage_pool_lookup_by_uuid_args *args,
                                       remote_storage_pool_lookup_by_uuid_ret *ret)
{
    virStoragePoolPtr pool;

3511
    pool = virStoragePoolLookupByUUID (conn, (unsigned char *) args->uuid);
3512
    if (pool == NULL) {
3513
        remoteDispatchConnError(rerr, conn);
3514 3515
        return -1;
    }
3516 3517 3518 3519 3520 3521 3522 3523

    make_nonnull_storage_pool (&ret->pool, pool);
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolLookupByVolume (struct qemud_server *server ATTRIBUTE_UNUSED,
3524 3525
                                         struct qemud_client *client ATTRIBUTE_UNUSED,
                                         virConnectPtr conn,
3526
                                         remote_error *rerr,
3527 3528 3529 3530 3531 3532
                                         remote_storage_pool_lookup_by_volume_args *args,
                                         remote_storage_pool_lookup_by_volume_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

3533
    vol = get_nonnull_storage_vol (conn, args->vol);
3534
    if (vol == NULL) {
3535
        remoteDispatchConnError(rerr, conn);
3536 3537
        return -1;
    }
3538 3539 3540

    pool = virStoragePoolLookupByVolume (vol);
    virStorageVolFree(vol);
3541
    if (pool == NULL) {
3542
        remoteDispatchConnError(rerr, conn);
3543 3544
        return -1;
    }
3545 3546 3547 3548 3549 3550 3551 3552

    make_nonnull_storage_pool (&ret->pool, pool);
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
3553 3554
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3555
                                       remote_error *rerr,
3556 3557 3558 3559 3560
                                       remote_storage_pool_set_autostart_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

3561
    pool = get_nonnull_storage_pool (conn, args->pool);
3562
    if (pool == NULL) {
3563
        remoteDispatchConnError(rerr, conn);
3564
        return -1;
3565 3566 3567 3568
    }

    if (virStoragePoolSetAutostart (pool, args->autostart) == -1) {
        virStoragePoolFree(pool);
3569
        remoteDispatchConnError(rerr, conn);
3570 3571 3572 3573 3574 3575 3576 3577
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
3578 3579
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3580
                                   remote_error *rerr,
3581 3582 3583 3584 3585
                                   remote_storage_pool_undefine_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

3586
    pool = get_nonnull_storage_pool (conn, args->pool);
3587
    if (pool == NULL) {
3588
        remoteDispatchConnError(rerr, conn);
3589
        return -1;
3590 3591 3592 3593
    }

    if (virStoragePoolUndefine (pool) == -1) {
        virStoragePoolFree(pool);
3594
        remoteDispatchConnError(rerr, conn);
3595 3596 3597 3598 3599 3600 3601 3602
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchNumOfStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
3603 3604
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
3605
                                 remote_error *rerr,
3606 3607 3608 3609
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_num_of_storage_pools_ret *ret)
{

3610
    ret->num = virConnectNumOfStoragePools (conn);
3611
    if (ret->num == -1) {
3612
        remoteDispatchConnError(rerr, conn);
3613 3614
        return -1;
    }
3615 3616 3617 3618 3619 3620

    return 0;
}

static int
remoteDispatchNumOfDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
3621 3622
                                        struct qemud_client *client ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
3623
                                        remote_error *rerr,
3624 3625 3626 3627
                                        void *args ATTRIBUTE_UNUSED,
                                        remote_num_of_defined_storage_pools_ret *ret)
{

3628
    ret->num = virConnectNumOfDefinedStoragePools (conn);
3629
    if (ret->num == -1) {
3630
        remoteDispatchConnError(rerr, conn);
3631 3632
        return -1;
    }
3633 3634 3635 3636 3637 3638

    return 0;
}

static int
remoteDispatchStoragePoolListVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
3639 3640
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
3641
                                      remote_error *rerr,
3642 3643 3644 3645 3646 3647
                                      remote_storage_pool_list_volumes_args *args,
                                      remote_storage_pool_list_volumes_ret *ret)
{
    virStoragePoolPtr pool;

    if (args->maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX) {
3648 3649 3650
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX"));
        return -1;
3651 3652
    }

3653
    pool = get_nonnull_storage_pool (conn, args->pool);
3654
    if (pool == NULL) {
3655
        remoteDispatchConnError(rerr, conn);
3656
        return -1;
3657 3658 3659
    }

    /* Allocate return buffer. */
3660 3661
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        virStoragePoolFree(pool);
3662 3663
        remoteDispatchOOMError(rerr);
        return -1;
3664
    }
3665 3666 3667 3668 3669

    ret->names.names_len =
        virStoragePoolListVolumes (pool,
                                   ret->names.names_val, args->maxnames);
    virStoragePoolFree(pool);
3670 3671
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
3672
        remoteDispatchConnError(rerr, conn);
3673 3674
        return -1;
    }
3675 3676 3677 3678 3679 3680 3681

    return 0;
}


static int
remoteDispatchStoragePoolNumOfVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
3682 3683
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3684
                                       remote_error *rerr,
3685 3686 3687 3688 3689
                                       remote_storage_pool_num_of_volumes_args *args,
                                       remote_storage_pool_num_of_volumes_ret *ret)
{
    virStoragePoolPtr pool;

3690
    pool = get_nonnull_storage_pool (conn, args->pool);
3691
    if (pool == NULL) {
3692
        remoteDispatchConnError(rerr, conn);
3693
        return -1;
3694 3695 3696 3697
    }

    ret->num = virStoragePoolNumOfVolumes (pool);
    virStoragePoolFree(pool);
3698
    if (ret->num == -1) {
3699
        remoteDispatchConnError(rerr, conn);
3700 3701
        return -1;
    }
3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714

    return 0;
}


/***************************************************************
 *     STORAGE VOL APIS
 ***************************************************************/



static int
remoteDispatchStorageVolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
3715 3716
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3717
                                   remote_error *rerr,
3718 3719 3720 3721 3722 3723
                                   remote_storage_vol_create_xml_args *args,
                                   remote_storage_vol_create_xml_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

3724
    pool = get_nonnull_storage_pool (conn, args->pool);
3725
    if (pool == NULL) {
3726
        remoteDispatchConnError(rerr, conn);
3727
        return -1;
3728 3729 3730 3731
    }

    vol = virStorageVolCreateXML (pool, args->xml, args->flags);
    virStoragePoolFree(pool);
3732
    if (vol == NULL) {
3733
        remoteDispatchConnError(rerr, conn);
3734 3735
        return -1;
    }
3736 3737 3738 3739 3740 3741 3742 3743 3744

    make_nonnull_storage_vol (&ret->vol, vol);
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
3745 3746
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
3747
                                remote_error *rerr,
3748 3749 3750 3751 3752
                                remote_storage_vol_delete_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virStorageVolPtr vol;

3753
    vol = get_nonnull_storage_vol (conn, args->vol);
3754
    if (vol == NULL) {
3755
        remoteDispatchConnError(rerr, conn);
3756
        return -1;
3757 3758 3759 3760
    }

    if (virStorageVolDelete (vol, args->flags) == -1) {
        virStorageVolFree(vol);
3761
        remoteDispatchConnError(rerr, conn);
3762 3763 3764 3765 3766 3767 3768 3769
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}

static int
remoteDispatchStorageVolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
3770 3771
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
3772
                                 remote_error *rerr,
3773 3774 3775 3776 3777 3778
                                 remote_storage_vol_get_info_args *args,
                                 remote_storage_vol_get_info_ret *ret)
{
    virStorageVolPtr vol;
    virStorageVolInfo info;

3779
    vol = get_nonnull_storage_vol (conn, args->vol);
3780
    if (vol == NULL) {
3781
        remoteDispatchConnError(rerr, conn);
3782
        return -1;
3783 3784 3785 3786
    }

    if (virStorageVolGetInfo (vol, &info) == -1) {
        virStorageVolFree(vol);
3787
        remoteDispatchConnError(rerr, conn);
3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801
        return -1;
    }

    ret->type = info.type;
    ret->capacity = info.capacity;
    ret->allocation = info.allocation;

    virStorageVolFree(vol);

    return 0;
}

static int
remoteDispatchStorageVolDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
3802 3803
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
3804
                                 remote_error *rerr,
3805 3806 3807 3808 3809
                                 remote_storage_vol_dump_xml_args *args,
                                 remote_storage_vol_dump_xml_ret *ret)
{
    virStorageVolPtr vol;

3810
    vol = get_nonnull_storage_vol (conn, args->vol);
3811
    if (vol == NULL) {
3812
        remoteDispatchConnError(rerr, conn);
3813
        return -1;
3814 3815 3816 3817 3818 3819
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStorageVolGetXMLDesc (vol, args->flags);
    if (!ret->xml) {
        virStorageVolFree(vol);
3820
        remoteDispatchConnError(rerr, conn);
3821 3822 3823 3824 3825 3826 3827 3828 3829
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolGetPath (struct qemud_server *server ATTRIBUTE_UNUSED,
3830 3831
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
3832
                                 remote_error *rerr,
3833 3834 3835 3836 3837
                                 remote_storage_vol_get_path_args *args,
                                 remote_storage_vol_get_path_ret *ret)
{
    virStorageVolPtr vol;

3838
    vol = get_nonnull_storage_vol (conn, args->vol);
3839
    if (vol == NULL) {
3840
        remoteDispatchConnError(rerr, conn);
3841
        return -1;
3842 3843 3844 3845 3846 3847
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virStorageVolGetPath (vol);
    if (!ret->name) {
        virStorageVolFree(vol);
3848
        remoteDispatchConnError(rerr, conn);
3849 3850 3851 3852 3853 3854 3855 3856 3857
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
3858 3859
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
3860
                                      remote_error *rerr,
3861 3862 3863 3864 3865 3866
                                      remote_storage_vol_lookup_by_name_args *args,
                                      remote_storage_vol_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

3867
    pool = get_nonnull_storage_pool (conn, args->pool);
3868
    if (pool == NULL) {
3869
        remoteDispatchConnError(rerr, conn);
3870
        return -1;
3871 3872 3873 3874
    }

    vol = virStorageVolLookupByName (pool, args->name);
    virStoragePoolFree(pool);
3875
    if (vol == NULL) {
3876
        remoteDispatchConnError(rerr, conn);
3877 3878
        return -1;
    }
3879 3880 3881 3882 3883 3884 3885 3886

    make_nonnull_storage_vol (&ret->vol, vol);
    virStorageVolFree(vol);
    return 0;
}

static int
remoteDispatchStorageVolLookupByKey (struct qemud_server *server ATTRIBUTE_UNUSED,
3887 3888
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
3889
                                     remote_error *rerr,
3890 3891 3892 3893 3894
                                     remote_storage_vol_lookup_by_key_args *args,
                                     remote_storage_vol_lookup_by_key_ret *ret)
{
    virStorageVolPtr vol;

3895
    vol = virStorageVolLookupByKey (conn, args->key);
3896
    if (vol == NULL) {
3897
        remoteDispatchConnError(rerr, conn);
3898 3899
        return -1;
    }
3900 3901 3902 3903 3904 3905 3906 3907 3908

    make_nonnull_storage_vol (&ret->vol, vol);
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolLookupByPath (struct qemud_server *server ATTRIBUTE_UNUSED,
3909 3910
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
3911
                                      remote_error *rerr,
3912 3913 3914 3915 3916
                                      remote_storage_vol_lookup_by_path_args *args,
                                      remote_storage_vol_lookup_by_path_ret *ret)
{
    virStorageVolPtr vol;

3917
    vol = virStorageVolLookupByPath (conn, args->path);
3918
    if (vol == NULL) {
3919
        remoteDispatchConnError(rerr, conn);
3920 3921
        return -1;
    }
3922 3923 3924 3925 3926 3927 3928

    make_nonnull_storage_vol (&ret->vol, vol);
    virStorageVolFree(vol);
    return 0;
}


3929 3930 3931 3932 3933 3934
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/

static int
remoteDispatchNodeNumOfDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
3935 3936
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
3937
                                remote_error *rerr,
3938 3939 3940 3941 3942
                                remote_node_num_of_devices_args *args,
                                remote_node_num_of_devices_ret *ret)
{
    CHECK_CONN(client);

3943
    ret->num = virNodeNumOfDevices (conn,
3944 3945
                                    args->cap ? *args->cap : NULL,
                                    args->flags);
3946
    if (ret->num == -1) {
3947
        remoteDispatchConnError(rerr, conn);
3948 3949
        return -1;
    }
3950 3951 3952 3953 3954 3955 3956

    return 0;
}


static int
remoteDispatchNodeListDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
3957 3958
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
3959
                               remote_error *rerr,
3960 3961 3962 3963 3964 3965
                               remote_node_list_devices_args *args,
                               remote_node_list_devices_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
3966 3967 3968
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
3969 3970 3971 3972
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
3973 3974
        remoteDispatchOOMError(rerr);
        return -1;
3975 3976 3977
    }

    ret->names.names_len =
3978
        virNodeListDevices (conn,
3979 3980 3981
                            args->cap ? *args->cap : NULL,
                            ret->names.names_val, args->maxnames, args->flags);
    if (ret->names.names_len == -1) {
3982
        remoteDispatchConnError(rerr, conn);
3983 3984 3985 3986 3987 3988 3989 3990 3991 3992
        VIR_FREE(ret->names.names_val);
        return -1;
    }

    return 0;
}


static int
remoteDispatchNodeDeviceLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
3993 3994
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
3995
                                      remote_error *rerr,
3996 3997 3998 3999 4000 4001 4002
                                      remote_node_device_lookup_by_name_args *args,
                                      remote_node_device_lookup_by_name_ret *ret)
{
    virNodeDevicePtr dev;

    CHECK_CONN(client);

4003
    dev = virNodeDeviceLookupByName (conn, args->name);
4004
    if (dev == NULL) {
4005
        remoteDispatchConnError(rerr, conn);
4006 4007
        return -1;
    }
4008 4009 4010 4011 4012 4013 4014 4015 4016

    make_nonnull_node_device (&ret->dev, dev);
    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4017 4018
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4019
                                 remote_error *rerr,
4020 4021 4022 4023 4024 4025
                                 remote_node_device_dump_xml_args *args,
                                 remote_node_device_dump_xml_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

4026
    dev = virNodeDeviceLookupByName(conn, args->name);
4027
    if (dev == NULL) {
4028 4029
        remoteDispatchFormatError(rerr, "%s", _("node_device not found"));
        return -1;
4030 4031 4032 4033 4034
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNodeDeviceGetXMLDesc (dev, args->flags);
    if (!ret->xml) {
4035
        remoteDispatchConnError(rerr, conn);
4036 4037 4038 4039 4040 4041 4042 4043 4044 4045
        virNodeDeviceFree(dev);
        return -1;
    }
    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceGetParent (struct qemud_server *server ATTRIBUTE_UNUSED,
4046 4047
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4048
                                   remote_error *rerr,
4049 4050 4051 4052 4053 4054 4055
                                   remote_node_device_get_parent_args *args,
                                   remote_node_device_get_parent_ret *ret)
{
    virNodeDevicePtr dev;
    const char *parent;
    CHECK_CONN(client);

4056
    dev = virNodeDeviceLookupByName(conn, args->name);
4057
    if (dev == NULL) {
4058 4059
        remoteDispatchFormatError(rerr, "%s", _("node_device not found"));
        return -1;
4060 4061 4062 4063 4064 4065 4066 4067 4068 4069
    }

    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
        if (VIR_ALLOC(parent_p) < 0) {
4070 4071
            remoteDispatchOOMError(rerr);
            return -1;
4072 4073 4074
        }
        *parent_p = strdup(parent);
        if (*parent_p == NULL) {
4075 4076
            remoteDispatchOOMError(rerr);
            return -1;
4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087
        }
        ret->parent = parent_p;
    }

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceNumOfCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
4088 4089
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4090
                                   remote_error *rerr,
4091 4092 4093 4094 4095 4096
                                   remote_node_device_num_of_caps_args *args,
                                   remote_node_device_num_of_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

4097
    dev = virNodeDeviceLookupByName(conn, args->name);
4098
    if (dev == NULL) {
4099 4100
        remoteDispatchFormatError(rerr, "%s", _("node_device not found"));
        return -1;
4101 4102 4103
    }

    ret->num = virNodeDeviceNumOfCaps(dev);
4104
    if (ret->num < 0) {
4105
        remoteDispatchConnError(rerr, conn);
4106 4107
        return -1;
    }
4108 4109 4110 4111 4112 4113 4114 4115

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceListCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
4116 4117
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4118
                                  remote_error *rerr,
4119 4120 4121 4122 4123 4124
                                  remote_node_device_list_caps_args *args,
                                  remote_node_device_list_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

4125
    dev = virNodeDeviceLookupByName(conn, args->name);
4126
    if (dev == NULL) {
4127 4128
        remoteDispatchFormatError(rerr, "%s", _("node_device not found"));
        return -1;
4129 4130 4131
    }

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
4132 4133 4134
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
4135 4136 4137 4138
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
4139 4140
        remoteDispatchOOMError(rerr);
        return -1;
4141 4142 4143 4144 4145 4146
    }

    ret->names.names_len =
        virNodeDeviceListCaps (dev, ret->names.names_val,
                               args->maxnames);
    if (ret->names.names_len == -1) {
4147
        remoteDispatchConnError(rerr, conn);
4148 4149 4150 4151 4152 4153 4154 4155
        VIR_FREE(ret->names.names_val);
        return -1;
    }

    return 0;
}


4156 4157 4158 4159 4160 4161
/**************************
 * Async Events
 **************************/
static int
remoteDispatchDomainEvent (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
4162
                           virConnectPtr conn ATTRIBUTE_UNUSED,
4163
                           remote_error *rerr ATTRIBUTE_UNUSED,
4164 4165 4166 4167 4168 4169 4170
                           void *args ATTRIBUTE_UNUSED,
                           remote_domain_event_ret *ret ATTRIBUTE_UNUSED)
{
    /* This call gets dispatched from a client call.
     * This does not make sense, as this should not be intiated
     * from the client side in generated code.
     */
4171 4172
    remoteDispatchFormatError(rerr, "%s", _("unexpected async event method call"));
    return -1;
4173 4174 4175 4176 4177 4178 4179
}

/***************************
 * Register / deregister events
 ***************************/
static int
remoteDispatchDomainEventsRegister (struct qemud_server *server ATTRIBUTE_UNUSED,
4180 4181
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
4182
                                    remote_error *rerr ATTRIBUTE_UNUSED,
4183 4184 4185 4186 4187 4188 4189
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

    /* Register event delivery callback */
    REMOTE_DEBUG("%s","Registering to relay remote events");
4190
    virConnectDomainEventRegister(conn, remoteRelayDomainEvent, client, NULL);
4191 4192 4193 4194 4195 4196 4197 4198

    if(ret)
        ret->cb_registered = 1;
    return 0;
}

static int
remoteDispatchDomainEventsDeregister (struct qemud_server *server ATTRIBUTE_UNUSED,
4199 4200
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4201
                                      remote_error *rerr ATTRIBUTE_UNUSED,
4202 4203 4204 4205 4206 4207 4208
                                      void *args ATTRIBUTE_UNUSED,
                                      remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

    /* Deregister event delivery callback */
    REMOTE_DEBUG("%s","Deregistering to relay remote events");
4209
    virConnectDomainEventDeregister(conn, remoteRelayDomainEvent);
4210 4211 4212 4213 4214 4215 4216 4217

    if(ret)
        ret->cb_registered = 0;
    return 0;
}

static void
remoteDispatchDomainEventSend (struct qemud_client *client,
4218 4219 4220
                               virDomainPtr dom,
                               int event,
                               int detail)
4221 4222 4223 4224 4225 4226
{
    remote_message_header rep;
    XDR xdr;
    int len;
    remote_domain_event_ret data;

4227
    if (!client)
4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241
        return;

    rep.prog = REMOTE_PROGRAM;
    rep.vers = REMOTE_PROTOCOL_VERSION;
    rep.proc = REMOTE_PROC_DOMAIN_EVENT;
    rep.direction = REMOTE_MESSAGE;
    rep.serial = 1;
    rep.status = REMOTE_OK;

    /* Serialise the return header and event. */
    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)) {
4242
        /*remoteDispatchError (client, NULL, "%s", _("xdr_int failed (1)"));*/
4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253
        xdr_destroy (&xdr);
        return;
    }

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

    /* build return data */
    make_nonnull_domain (&data.dom, dom);
4254 4255
    data.event = event;
    data.detail = detail;
4256 4257

    if (!xdr_remote_domain_event_ret(&xdr, &data)) {
4258
        /*remoteDispatchError (client, NULL, "%s", _("serialise return struct"));*/
4259 4260 4261 4262 4263 4264
        xdr_destroy (&xdr);
        return;
    }

    len = xdr_getpos (&xdr);
    if (xdr_setpos (&xdr, 0) == 0) {
4265
        /*remoteDispatchError (client, NULL, "%s", _("xdr_setpos failed"));*/
4266 4267 4268 4269 4270
        xdr_destroy (&xdr);
        return;
    }

    if (!xdr_int (&xdr, &len)) {
4271
        /*remoteDispatchError (client, NULL, "%s", _("xdr_int failed (2)"));*/
4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282
        xdr_destroy (&xdr);
        return;
    }

    xdr_destroy (&xdr);

    /* Send it. */
    client->mode = QEMUD_MODE_TX_PACKET;
    client->bufferLength = len;
    client->bufferOffset = 0;
}
4283

4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309
/*----- 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);
}

4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323
static virStoragePoolPtr
get_nonnull_storage_pool (virConnectPtr conn, remote_nonnull_storage_pool pool)
{
    return virGetStoragePool (conn, pool.name, BAD_CAST pool.uuid);
}

static virStorageVolPtr
get_nonnull_storage_vol (virConnectPtr conn, remote_nonnull_storage_vol vol)
{
    virStorageVolPtr ret;
    ret = virGetStorageVol (conn, vol.pool, vol.name, vol.key);
    return ret;
}

4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339
/* 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);
}

4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353
static void
make_nonnull_storage_pool (remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
{
    pool_dst->name = strdup (pool_src->name);
    memcpy (pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
}

static void
make_nonnull_storage_vol (remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
{
    vol_dst->pool = strdup (vol_src->pool);
    vol_dst->name = strdup (vol_src->name);
    vol_dst->key = strdup (vol_src->key);
}
4354 4355 4356 4357 4358 4359

static void
make_nonnull_node_device (remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
{
    dev_dst->name = strdup(dev_src->name);
}