remote.c 217.8 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007-2010 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
 *
 * 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 <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>
43
#include <fnmatch.h>
44
#include <arpa/inet.h>
45
#include "virterror_internal.h"
46

47
#if HAVE_POLKIT0
48 49
# include <polkit/polkit.h>
# include <polkit-dbus/polkit-dbus.h>
50 51
#endif

52 53 54
#include "remote.h"
#include "dispatch.h"

55 56
#include "libvirt_internal.h"
#include "datatypes.h"
57
#include "memory.h"
58
#include "util.h"
C
Chris Lalancette 已提交
59
#include "stream.h"
60

61
#define VIR_FROM_THIS VIR_FROM_REMOTE
62
#define REMOTE_DEBUG(fmt, ...) DEBUG(fmt, __VA_ARGS__)
63

64 65
static virDomainPtr get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network (virConnectPtr conn, remote_nonnull_network network);
66
static virInterfacePtr get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface);
67 68
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);
69
static virSecretPtr get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret);
70
static virNWFilterPtr get_nonnull_nwfilter (virConnectPtr conn, remote_nonnull_nwfilter nwfilter);
71
static virDomainSnapshotPtr get_nonnull_domain_snapshot (virDomainPtr domain, remote_nonnull_domain_snapshot snapshot);
72 73
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);
D
Daniel Veillard 已提交
74
static void make_nonnull_interface (remote_nonnull_interface *interface_dst, virInterfacePtr interface_src);
75 76
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);
77
static void make_nonnull_node_device (remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src);
78
static void make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src);
79
static void make_nonnull_nwfilter (remote_nonnull_nwfilter *net_dst, virNWFilterPtr nwfilter_src);
C
Chris Lalancette 已提交
80
static void make_nonnull_domain_snapshot (remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src);
81

82

83
#include "remote_dispatch_prototypes.h"
84 85 86 87

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

89 90 91 92 93 94 95 96 97 98
const dispatch_data const *remoteGetDispatchData(int proc)
{
    if (proc >= ARRAY_CARDINALITY(dispatch_table) ||
        dispatch_table[proc].fn == NULL) {
        return NULL;
    }

    return &(dispatch_table[proc]);
}

99 100 101
/* Prototypes */
static void
remoteDispatchDomainEventSend (struct qemud_client *client,
102 103 104
                               int procnr,
                               xdrproc_t proc,
                               void *data);
105

106 107 108 109 110
static int remoteRelayDomainEventLifecycle(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           int event,
                                           int detail,
                                           void *opaque)
111 112
{
    struct qemud_client *client = opaque;
113
    remote_domain_event_lifecycle_msg data;
114

115 116 117 118
    if (!client)
        return -1;

    REMOTE_DEBUG("Relaying domain lifecycle event %d %d", event, detail);
119

120
    virMutexLock(&client->lock);
121

122 123 124 125 126
    /* build return data */
    memset(&data, 0, sizeof data);
    make_nonnull_domain (&data.dom, dom);
    data.event = event;
    data.detail = detail;
127

128
    remoteDispatchDomainEventSend (client,
129 130
                                   REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
                                   (xdrproc_t)xdr_remote_domain_event_lifecycle_msg, &data);
131 132

    virMutexUnlock(&client->lock);
133

134 135
    return 0;
}
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
static int remoteRelayDomainEventReboot(virConnectPtr conn ATTRIBUTE_UNUSED,
                                        virDomainPtr dom,
                                        void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_reboot_msg data;

    if (!client)
        return -1;

    REMOTE_DEBUG("Relaying domain reboot event %s %d", dom->name, dom->id);

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
    make_nonnull_domain (&data.dom, dom);

    remoteDispatchDomainEventSend (client,
                                   REMOTE_PROC_DOMAIN_EVENT_REBOOT,
                                   (xdrproc_t)xdr_remote_domain_event_reboot_msg, &data);

    virMutexUnlock(&client->lock);

    return 0;
}

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
static int remoteRelayDomainEventRTCChange(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           long long offset,
                                           void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_rtc_change_msg data;

    if (!client)
        return -1;

    REMOTE_DEBUG("Relaying domain rtc change event %s %d %lld", dom->name, dom->id, offset);

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
    make_nonnull_domain (&data.dom, dom);
    data.offset = offset;

    remoteDispatchDomainEventSend (client,
                                   REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
                                   (xdrproc_t)xdr_remote_domain_event_rtc_change_msg, &data);

    virMutexUnlock(&client->lock);

    return 0;
}


195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
static int remoteRelayDomainEventWatchdog(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          int action,
                                          void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_watchdog_msg data;

    if (!client)
        return -1;

    REMOTE_DEBUG("Relaying domain watchdog event %s %d %d", dom->name, dom->id, action);

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
    make_nonnull_domain (&data.dom, dom);
    data.action = action;

    remoteDispatchDomainEventSend (client,
                                   REMOTE_PROC_DOMAIN_EVENT_WATCHDOG,
                                   (xdrproc_t)xdr_remote_domain_event_watchdog_msg, &data);

    virMutexUnlock(&client->lock);

    return 0;
}


225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
static int remoteRelayDomainEventIOError(virConnectPtr conn ATTRIBUTE_UNUSED,
                                         virDomainPtr dom,
                                         const char *srcPath,
                                         const char *devAlias,
                                         int action,
                                         void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_io_error_msg data;

    if (!client)
        return -1;

    REMOTE_DEBUG("Relaying domain io error %s %d %s %s %d", dom->name, dom->id, srcPath, devAlias, action);

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
    make_nonnull_domain (&data.dom, dom);
    data.srcPath = (char*)srcPath;
    data.devAlias = (char*)devAlias;
    data.action = action;

    remoteDispatchDomainEventSend (client,
                                   REMOTE_PROC_DOMAIN_EVENT_IO_ERROR,
                                   (xdrproc_t)xdr_remote_domain_event_io_error_msg, &data);

    virMutexUnlock(&client->lock);

    return 0;
}


259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
static int remoteRelayDomainEventIOErrorReason(virConnectPtr conn ATTRIBUTE_UNUSED,
                                               virDomainPtr dom,
                                               const char *srcPath,
                                               const char *devAlias,
                                               int action,
                                               const char *reason,
                                               void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_io_error_reason_msg data;

    if (!client)
        return -1;

    REMOTE_DEBUG("Relaying domain io error %s %d %s %s %d %s",
                 dom->name, dom->id, srcPath, devAlias, action, reason);

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
    make_nonnull_domain (&data.dom, dom);
    data.srcPath = (char*)srcPath;
    data.devAlias = (char*)devAlias;
    data.action = action;
    data.reason = (char*)reason;

    remoteDispatchDomainEventSend (client,
                                   REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON,
                                   (xdrproc_t)xdr_remote_domain_event_io_error_reason_msg, &data);

    virMutexUnlock(&client->lock);

    return 0;
}


296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
static int remoteRelayDomainEventGraphics(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          int phase,
                                          virDomainEventGraphicsAddressPtr local,
                                          virDomainEventGraphicsAddressPtr remote,
                                          const char *authScheme,
                                          virDomainEventGraphicsSubjectPtr subject,
                                          void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_graphics_msg data;
    int i;

    if (!client)
        return -1;

    REMOTE_DEBUG("Relaying domain graphics event %s %d %d - %d %s %s  - %d %s %s - %s", dom->name, dom->id, phase,
                 local->family, local->service, local->node,
                 remote->family, remote->service, remote->node,
                 authScheme);

    REMOTE_DEBUG("Subject %d", subject->nidentity);
    for (i = 0 ; i < subject->nidentity ; i++) {
        REMOTE_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
    }

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
    make_nonnull_domain (&data.dom, dom);
    data.phase = phase;
    data.authScheme = (char*)authScheme;

    data.local.family = local->family;
    data.local.node = (char *)local->node;
    data.local.service = (char *)local->service;

    data.remote.family = remote->family;
    data.remote.node = (char*)remote->node;
    data.remote.service = (char*)remote->service;

    data.subject.subject_len = subject->nidentity;
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0) {
        VIR_WARN0("cannot allocate memory for graphics event subject");
        return -1;
    }
    for (i = 0 ; i < data.subject.subject_len ; i++) {
        data.subject.subject_val[i].type = (char*)subject->identities[i].type;
        data.subject.subject_val[i].name = (char*)subject->identities[i].name;
    }

    remoteDispatchDomainEventSend (client,
                                   REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
                                   (xdrproc_t)xdr_remote_domain_event_graphics_msg, &data);

    VIR_FREE(data.subject.subject_val);

    virMutexUnlock(&client->lock);

    return 0;
}


360
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
361
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
362
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
363
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
364
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
365
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
366
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
367
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
368 369 370 371
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

372 373 374
/*----- Functions. -----*/

static int
375
remoteDispatchOpen (struct qemud_server *server,
376
                    struct qemud_client *client,
377
                    virConnectPtr conn,
378
                    remote_message_header *hdr ATTRIBUTE_UNUSED,
379
                    remote_error *rerr,
380 381 382
                    struct remote_open_args *args, void *ret ATTRIBUTE_UNUSED)
{
    const char *name;
383
    int flags, rc;
384 385

    /* Already opened? */
386
    if (conn) {
387 388
        remoteDispatchFormatError (rerr, "%s", _("connection already open"));
        return -1;
389 390
    }

391 392 393
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
394

395 396 397 398 399 400 401 402 403 404 405 406 407
    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);

408
    if (client->conn == NULL)
409 410
        remoteDispatchConnError(rerr, NULL);

411
    rc = client->conn ? 0 : -1;
412
    virMutexUnlock(&client->lock);
413
    return rc;
414 415
}

416 417 418 419
#define CHECK_CONN(client)                                              \
    if (!client->conn) {                                                \
        remoteDispatchFormatError (rerr, "%s", _("connection not open")); \
        return -1;                                                      \
420 421 422
    }

static int
423
remoteDispatchClose (struct qemud_server *server ATTRIBUTE_UNUSED,
424 425
                     struct qemud_client *client ATTRIBUTE_UNUSED,
                     virConnectPtr conn ATTRIBUTE_UNUSED,
426
                     remote_message_header *hdr ATTRIBUTE_UNUSED,
427
                     remote_error *rerr ATTRIBUTE_UNUSED,
428 429
                     void *args ATTRIBUTE_UNUSED, void *ret ATTRIBUTE_UNUSED)
{
430 431 432
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
433

434
    client->closing = 1;
435

436
    virMutexUnlock(&client->lock);
437
    return 0;
438 439
}

440
static int
441
remoteDispatchSupportsFeature (struct qemud_server *server ATTRIBUTE_UNUSED,
442 443
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
444
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
445
                               remote_error *rerr,
446 447
                               remote_supports_feature_args *args, remote_supports_feature_ret *ret)
{
448
    ret->supported = virDrvSupportsFeature (conn, args->feature);
449 450

    if (ret->supported == -1) {
451
        remoteDispatchConnError(rerr, conn);
452 453
        return -1;
    }
454 455 456 457

    return 0;
}

458
static int
459
remoteDispatchGetType (struct qemud_server *server ATTRIBUTE_UNUSED,
460 461
                       struct qemud_client *client ATTRIBUTE_UNUSED,
                       virConnectPtr conn,
462
                       remote_message_header *hdr ATTRIBUTE_UNUSED,
463
                       remote_error *rerr,
464 465 466 467
                       void *args ATTRIBUTE_UNUSED, remote_get_type_ret *ret)
{
    const char *type;

468
    type = virConnectGetType (conn);
469
    if (type == NULL) {
470
        remoteDispatchConnError(rerr, conn);
471 472
        return -1;
    }
473 474 475 476 477 478

    /* We have to strdup because remoteDispatchClientRequest will
     * free this string after it's been serialised.
     */
    ret->type = strdup (type);
    if (!ret->type) {
479
        remoteDispatchOOMError(rerr);
480
        return -1;
481 482 483 484 485 486
    }

    return 0;
}

static int
487
remoteDispatchGetVersion (struct qemud_server *server ATTRIBUTE_UNUSED,
488 489
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
490
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
491
                          remote_error *rerr,
492 493 494 495 496
                          void *args ATTRIBUTE_UNUSED,
                          remote_get_version_ret *ret)
{
    unsigned long hvVer;

497 498
    if (virConnectGetVersion (conn, &hvVer) == -1) {
        remoteDispatchConnError(rerr, conn);
499
        return -1;
500
    }
501 502 503 504 505

    ret->hv_ver = hvVer;
    return 0;
}

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
static int
remoteDispatchGetLibVersion (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                             remote_error *rerr,
                             void *args ATTRIBUTE_UNUSED,
                             remote_get_lib_version_ret *ret)
{
    unsigned long libVer;

    if (virConnectGetLibVersion (conn, &libVer) == -1) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    ret->lib_ver = libVer;
    return 0;
}

526
static int
527
remoteDispatchGetHostname (struct qemud_server *server ATTRIBUTE_UNUSED,
528 529
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
530
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
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_message_header *hdr ATTRIBUTE_UNUSED,
552
                      remote_error *rerr,
553 554 555 556 557 558
                      void *args ATTRIBUTE_UNUSED,
                      remote_get_uri_ret *ret)
{
    char *uri;
    CHECK_CONN(client);

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

    ret->uri = uri;
    return 0;
}

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

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

    return 0;
}

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

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

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

629
    caps = virConnectGetCapabilities (conn);
630
    if (caps == NULL) {
631
        remoteDispatchConnError(rerr, conn);
632 633
        return -1;
    }
634 635 636 637 638

    ret->capabilities = caps;
    return 0;
}

639 640
static int
remoteDispatchNodeGetCellsFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
641 642
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
643
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
644
                                      remote_error *rerr,
645 646 647
                                      remote_node_get_cells_free_memory_args *args,
                                      remote_node_get_cells_free_memory_ret *ret)
{
D
Daniel P. Berrange 已提交
648
    int err;
649 650

    if (args->maxCells > REMOTE_NODE_MAX_CELLS) {
651 652 653
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxCells > REMOTE_NODE_MAX_CELLS"));
        return -1;
654 655 656
    }

    /* Allocate return buffer. */
657
    if (VIR_ALLOC_N(ret->freeMems.freeMems_val, args->maxCells) < 0) {
658 659
        remoteDispatchOOMError(rerr);
        return -1;
660
    }
661

D
Daniel P. Berrange 已提交
662 663 664 665 666
    err = virNodeGetCellsFreeMemory(conn,
                                    (unsigned long long *)ret->freeMems.freeMems_val,
                                    args->startCell,
                                    args->maxCells);
    if (err <= 0) {
667
        VIR_FREE(ret->freeMems.freeMems_val);
668
        remoteDispatchConnError(rerr, conn);
669
        return -1;
670
    }
D
Daniel P. Berrange 已提交
671
    ret->freeMems.freeMems_len = err;
672 673 674 675 676 677 678

    return 0;
}


static int
remoteDispatchNodeGetFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
679 680
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
681
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
682
                                 remote_error *rerr,
683 684 685 686 687
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_node_get_free_memory_ret *ret)
{
    unsigned long long freeMem;

688
    freeMem = virNodeGetFreeMemory(conn);
689
    if (freeMem == 0) {
690
        remoteDispatchConnError(rerr, conn);
691 692
        return -1;
    }
693 694 695 696 697
    ret->freeMem = freeMem;
    return 0;
}


698
static int
699
remoteDispatchDomainGetSchedulerType (struct qemud_server *server ATTRIBUTE_UNUSED,
700 701
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
702
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
703
                                      remote_error *rerr,
704 705 706 707 708 709 710
                                      remote_domain_get_scheduler_type_args *args,
                                      remote_domain_get_scheduler_type_ret *ret)
{
    virDomainPtr dom;
    char *type;
    int nparams;

711
    dom = get_nonnull_domain (conn, args->dom);
712
    if (dom == NULL) {
713
        remoteDispatchConnError(rerr, conn);
714
        return -1;
715 716 717
    }

    type = virDomainGetSchedulerType (dom, &nparams);
718 719
    if (type == NULL) {
        virDomainFree(dom);
720
        remoteDispatchConnError(rerr, conn);
721 722
        return -1;
    }
723 724 725

    ret->type = type;
    ret->nparams = nparams;
726
    virDomainFree(dom);
727 728 729 730
    return 0;
}

static int
731
remoteDispatchDomainGetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
732 733
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
734
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
735
                                            remote_error *rerr,
736 737 738 739 740 741 742 743 744 745
                                            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) {
746 747
        remoteDispatchFormatError (rerr, "%s", _("nparams too large"));
        return -1;
748
    }
749
    if (VIR_ALLOC_N(params, nparams) < 0) {
750 751
        remoteDispatchOOMError(rerr);
        return -1;
752 753
    }

754
    dom = get_nonnull_domain (conn, args->dom);
755
    if (dom == NULL) {
756
        VIR_FREE(params);
757
        remoteDispatchConnError(rerr, conn);
758
        return -1;
759 760 761 762
    }

    r = virDomainGetSchedulerParameters (dom, params, &nparams);
    if (r == -1) {
763
        virDomainFree(dom);
764
        VIR_FREE(params);
765
        remoteDispatchConnError(rerr, conn);
766 767 768 769 770
        return -1;
    }

    /* Serialise the scheduler parameters. */
    ret->params.params_len = nparams;
771 772
    if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
        goto oom;
773 774 775 776

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

780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
        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:
795
            remoteDispatchFormatError (rerr, "%s", _("unknown type"));
796
            goto cleanup;
797 798
        }
    }
799
    virDomainFree(dom);
800
    VIR_FREE(params);
801 802

    return 0;
803 804

oom:
805
    remoteDispatchOOMError(rerr);
806 807 808 809 810
cleanup:
    virDomainFree(dom);
    for (i = 0 ; i < nparams ; i++)
        VIR_FREE(ret->params.params_val[i].field);
    VIR_FREE(params);
811
    return -1;
812 813 814
}

static int
815
remoteDispatchDomainSetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
816 817
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
818
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
819
                                            remote_error *rerr,
820 821 822 823 824 825 826 827 828 829
                                            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) {
830 831
        remoteDispatchFormatError (rerr, "%s", _("nparams too large"));
        return -1;
832
    }
833
    if (VIR_ALLOC_N(params, nparams) < 0) {
834 835
        remoteDispatchOOMError(rerr);
        return -1;
836 837 838 839
    }

    /* Deserialise parameters. */
    for (i = 0; i < nparams; ++i) {
C
Chris Lalancette 已提交
840 841 842 843 844
        if (virStrcpyStatic(params[i].field, args->params.params_val[i].field) == NULL) {
            remoteDispatchFormatError(rerr, _("Field %s too big for destination"),
                                      args->params.params_val[i].field);
            return -1;
        }
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
        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;
        }
    }

862
    dom = get_nonnull_domain (conn, args->dom);
863
    if (dom == NULL) {
864
        VIR_FREE(params);
865
        remoteDispatchConnError(rerr, conn);
866
        return -1;
867 868 869
    }

    r = virDomainSetSchedulerParameters (dom, params, nparams);
870
    virDomainFree(dom);
871
    VIR_FREE(params);
872
    if (r == -1) {
873
        remoteDispatchConnError(rerr, conn);
874 875
        return -1;
    }
876 877 878 879

    return 0;
}

880
static int
881
remoteDispatchDomainBlockStats (struct qemud_server *server ATTRIBUTE_UNUSED,
882 883
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
884
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
885
                                remote_error *rerr,
886 887 888 889 890 891 892
                                remote_domain_block_stats_args *args,
                                remote_domain_block_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainBlockStats stats;

893
    dom = get_nonnull_domain (conn, args->dom);
894
    if (dom == NULL) {
895
        remoteDispatchConnError(rerr, conn);
896
        return -1;
897 898 899
    }
    path = args->path;

D
Daniel P. Berrange 已提交
900 901
    if (virDomainBlockStats (dom, path, &stats, sizeof stats) == -1) {
        virDomainFree (dom);
902
        remoteDispatchConnError(rerr, conn);
903
        return -1;
D
Daniel P. Berrange 已提交
904 905
    }
    virDomainFree (dom);
906 907 908 909 910 911 912 913 914 915 916

    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
917
remoteDispatchDomainInterfaceStats (struct qemud_server *server ATTRIBUTE_UNUSED,
918 919
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
920
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
921
                                    remote_error *rerr,
922 923 924 925 926 927 928
                                    remote_domain_interface_stats_args *args,
                                    remote_domain_interface_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainInterfaceStats stats;

929
    dom = get_nonnull_domain (conn, args->dom);
930
    if (dom == NULL) {
931
        remoteDispatchConnError(rerr, conn);
932
        return -1;
933 934 935
    }
    path = args->path;

D
Daniel P. Berrange 已提交
936 937
    if (virDomainInterfaceStats (dom, path, &stats, sizeof stats) == -1) {
        virDomainFree (dom);
938
        remoteDispatchConnError(rerr, conn);
939
        return -1;
D
Daniel P. Berrange 已提交
940 941
    }
    virDomainFree (dom);
942 943 944 945 946 947 948 949 950 951 952 953 954

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

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
static int
remoteDispatchDomainMemoryStats (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_domain_memory_stats_args *args,
                                 remote_domain_memory_stats_ret *ret)
{
    virDomainPtr dom;
    struct _virDomainMemoryStat *stats;
    unsigned int nr_stats, i;

    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
        remoteDispatchFormatError (rerr, "%s",
                               _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
        return -1;
    }

    dom = get_nonnull_domain (conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    /* Allocate stats array for making dispatch call */
    if (VIR_ALLOC_N(stats, args->maxStats) < 0) {
        remoteDispatchOOMError(rerr);
        return -1;
     }

    nr_stats = virDomainMemoryStats (dom, stats, args->maxStats, 0);
    virDomainFree (dom);
    if (nr_stats == -1) {
        VIR_FREE(stats);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    /* Allocate return buffer */
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0) {
        VIR_FREE(stats);
        remoteDispatchOOMError(rerr);
        return -1;
    }

    /* Copy the stats into the xdr return structure */
    for (i = 0; i < nr_stats; i++) {
        ret->stats.stats_val[i].tag = stats[i].tag;
        ret->stats.stats_val[i].val = stats[i].val;
    }
    ret->stats.stats_len = nr_stats;
    VIR_FREE(stats);
    return 0;
}

1011 1012
static int
remoteDispatchDomainBlockPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
1013 1014
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1015
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1016
                               remote_error *rerr,
1017 1018 1019 1020 1021 1022 1023 1024 1025
                               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;

1026
    dom = get_nonnull_domain (conn, args->dom);
1027
    if (dom == NULL) {
1028
        remoteDispatchConnError(rerr, conn);
1029
        return -1;
1030 1031 1032 1033 1034 1035 1036
    }
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1037
        virDomainFree (dom);
1038 1039 1040
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
        return -1;
1041 1042 1043
    }

    ret->buffer.buffer_len = size;
1044
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
1045
        virDomainFree (dom);
1046
        remoteDispatchOOMError(rerr);
1047
        return -1;
1048 1049 1050 1051 1052 1053
    }

    if (virDomainBlockPeek (dom, path, offset, size,
                            ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
1054
        remoteDispatchConnError(rerr, conn);
1055 1056 1057 1058 1059 1060 1061
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

R
Richard W.M. Jones 已提交
1062 1063
static int
remoteDispatchDomainMemoryPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
1064 1065
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
1066
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
1067
                                remote_error *rerr,
R
Richard W.M. Jones 已提交
1068 1069 1070 1071 1072 1073 1074 1075
                                remote_domain_memory_peek_args *args,
                                remote_domain_memory_peek_ret *ret)
{
    virDomainPtr dom;
    unsigned long long offset;
    size_t size;
    unsigned int flags;

1076
    dom = get_nonnull_domain (conn, args->dom);
R
Richard W.M. Jones 已提交
1077
    if (dom == NULL) {
1078
        remoteDispatchConnError(rerr, conn);
1079
        return -1;
R
Richard W.M. Jones 已提交
1080 1081 1082 1083 1084 1085
    }
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1086
        virDomainFree (dom);
1087 1088 1089
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
        return -1;
R
Richard W.M. Jones 已提交
1090 1091 1092 1093 1094
    }

    ret->buffer.buffer_len = size;
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
        virDomainFree (dom);
1095
        remoteDispatchOOMError(rerr);
1096
        return -1;
R
Richard W.M. Jones 已提交
1097 1098 1099 1100 1101 1102
    }

    if (virDomainMemoryPeek (dom, offset, size,
                             ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
1103
        remoteDispatchConnError(rerr, conn);
R
Richard W.M. Jones 已提交
1104 1105 1106 1107 1108 1109 1110
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

1111
static int
1112
remoteDispatchDomainAttachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
1113 1114
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1115
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1116
                                  remote_error *rerr,
1117 1118 1119 1120 1121
                                  remote_domain_attach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1122
    dom = get_nonnull_domain (conn, args->dom);
1123
    if (dom == NULL) {
1124
        remoteDispatchConnError(rerr, conn);
1125
        return -1;
1126 1127
    }

1128 1129
    if (virDomainAttachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
1130
        remoteDispatchConnError(rerr, conn);
1131
        return -1;
1132 1133
    }
    virDomainFree(dom);
1134 1135 1136
    return 0;
}

J
Jim Fehlig 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
static int
remoteDispatchDomainAttachDeviceFlags (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_attach_device_flags_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

    dom = get_nonnull_domain (conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainAttachDeviceFlags (dom, args->xml, args->flags) == -1) {
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    virDomainFree(dom);
    return 0;
}

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
static int
remoteDispatchDomainUpdateDeviceFlags (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_update_device_flags_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

    dom = get_nonnull_domain (conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainUpdateDeviceFlags (dom, args->xml, args->flags) == -1) {
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    virDomainFree(dom);
    return 0;
}

1189
static int
1190
remoteDispatchDomainCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
1191 1192
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
1193
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
1194
                            remote_error *rerr,
1195 1196 1197 1198 1199
                            remote_domain_create_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1200
    dom = get_nonnull_domain (conn, args->dom);
1201
    if (dom == NULL) {
1202
        remoteDispatchConnError(rerr, conn);
1203
        return -1;
1204 1205
    }

1206 1207
    if (virDomainCreate (dom) == -1) {
        virDomainFree(dom);
1208
        remoteDispatchConnError(rerr, conn);
1209
        return -1;
1210 1211
    }
    virDomainFree(dom);
1212 1213 1214
    return 0;
}

1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
static int
remoteDispatchDomainCreateWithFlags (struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
                                     remote_error *rerr,
                                     remote_domain_create_with_flags_args *args,
                                     remote_domain_create_with_flags_ret *ret)
{
    virDomainPtr dom;

    dom = get_nonnull_domain (conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainCreateWithFlags (dom, args->flags) == -1) {
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_domain (&ret->dom, dom);
    virDomainFree(dom);
    return 0;
}

1243
static int
1244
remoteDispatchDomainCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1245 1246
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1247
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1248 1249 1250
                               remote_error *rerr,
                               remote_domain_create_xml_args *args,
                               remote_domain_create_xml_ret *ret)
1251 1252 1253
{
    virDomainPtr dom;

1254
    dom = virDomainCreateXML (conn, args->xml_desc, args->flags);
1255
    if (dom == NULL) {
1256
        remoteDispatchConnError(rerr, conn);
1257 1258
        return -1;
    }
1259 1260

    make_nonnull_domain (&ret->dom, dom);
1261
    virDomainFree(dom);
1262 1263 1264 1265 1266

    return 0;
}

static int
1267
remoteDispatchDomainDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1268 1269
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1270
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1271
                               remote_error *rerr,
1272 1273 1274 1275 1276
                               remote_domain_define_xml_args *args,
                               remote_domain_define_xml_ret *ret)
{
    virDomainPtr dom;

1277
    dom = virDomainDefineXML (conn, args->xml);
1278
    if (dom == NULL) {
1279
        remoteDispatchConnError(rerr, conn);
1280 1281
        return -1;
    }
1282 1283

    make_nonnull_domain (&ret->dom, dom);
1284
    virDomainFree(dom);
1285 1286 1287 1288 1289

    return 0;
}

static int
1290
remoteDispatchDomainDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
1291 1292
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1293
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1294
                             remote_error *rerr,
1295 1296 1297 1298 1299
                             remote_domain_destroy_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1300
    dom = get_nonnull_domain (conn, args->dom);
1301
    if (dom == NULL) {
1302
        remoteDispatchConnError(rerr, conn);
1303
        return -1;
1304 1305
    }

1306 1307
    if (virDomainDestroy (dom) == -1) {
        virDomainFree(dom);
1308
        remoteDispatchConnError(rerr, conn);
1309
        return -1;
1310 1311
    }
    virDomainFree(dom);
1312 1313 1314 1315
    return 0;
}

static int
1316
remoteDispatchDomainDetachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
1317 1318
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1319
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1320
                                  remote_error *rerr,
1321 1322 1323 1324 1325
                                  remote_domain_detach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1326
    dom = get_nonnull_domain (conn, args->dom);
1327
    if (dom == NULL) {
1328
        remoteDispatchConnError(rerr, conn);
1329
        return -1;
1330 1331
    }

1332 1333
    if (virDomainDetachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
J
Jim Fehlig 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    virDomainFree(dom);
    return 0;
}

static int
remoteDispatchDomainDetachDeviceFlags (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_detach_device_flags_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

    dom = get_nonnull_domain (conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainDetachDeviceFlags (dom, args->xml, args->flags) == -1) {
        virDomainFree(dom);
1361
        remoteDispatchConnError(rerr, conn);
1362
        return -1;
1363
    }
1364

1365
    virDomainFree(dom);
1366 1367 1368 1369
    return 0;
}

static int
1370
remoteDispatchDomainDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1371 1372
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1373
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1374
                             remote_error *rerr,
1375 1376 1377 1378 1379
                             remote_domain_dump_xml_args *args,
                             remote_domain_dump_xml_ret *ret)
{
    virDomainPtr dom;

1380
    dom = get_nonnull_domain (conn, args->dom);
1381
    if (dom == NULL) {
1382
        remoteDispatchConnError(rerr, conn);
1383
        return -1;
1384 1385 1386 1387
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainGetXMLDesc (dom, args->flags);
1388
    if (!ret->xml) {
1389
        virDomainFree(dom);
1390
        remoteDispatchConnError(rerr, conn);
1391
        return -1;
1392 1393
    }
    virDomainFree(dom);
1394 1395 1396
    return 0;
}

1397 1398 1399 1400
static int
remoteDispatchDomainXmlFromNative (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1401
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
                                   remote_error *rerr,
                                   remote_domain_xml_from_native_args *args,
                                   remote_domain_xml_from_native_ret *ret)
{
    /* remoteDispatchClientRequest will free this. */
    ret->domainXml = virConnectDomainXMLFromNative (conn,
                                                    args->nativeFormat,
                                                    args->nativeConfig,
                                                    args->flags);
    if (!ret->domainXml) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    return 0;
}

static int
remoteDispatchDomainXmlToNative (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
1422
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
                                 remote_error *rerr,
                                 remote_domain_xml_to_native_args *args,
                                 remote_domain_xml_to_native_ret *ret)
{
    /* remoteDispatchClientRequest will free this. */
    ret->nativeConfig = virConnectDomainXMLToNative (conn,
                                                     args->nativeFormat,
                                                     args->domainXml,
                                                     args->flags);
    if (!ret->nativeConfig) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    return 0;
}


1440
static int
1441
remoteDispatchDomainGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
1442 1443
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1444
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1445
                                  remote_error *rerr,
1446 1447 1448 1449 1450
                                  remote_domain_get_autostart_args *args,
                                  remote_domain_get_autostart_ret *ret)
{
    virDomainPtr dom;

1451
    dom = get_nonnull_domain (conn, args->dom);
1452
    if (dom == NULL) {
1453
        remoteDispatchConnError(rerr, conn);
1454
        return -1;
1455 1456
    }

1457 1458
    if (virDomainGetAutostart (dom, &ret->autostart) == -1) {
        virDomainFree(dom);
1459
        remoteDispatchConnError(rerr, conn);
1460
        return -1;
1461 1462
    }
    virDomainFree(dom);
1463 1464 1465 1466
    return 0;
}

static int
1467
remoteDispatchDomainGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
1468 1469
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1470
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1471
                             remote_error *rerr,
1472 1473 1474 1475 1476 1477
                             remote_domain_get_info_args *args,
                             remote_domain_get_info_ret *ret)
{
    virDomainPtr dom;
    virDomainInfo info;

1478
    dom = get_nonnull_domain (conn, args->dom);
1479
    if (dom == NULL) {
1480
        remoteDispatchConnError(rerr, conn);
1481
        return -1;
1482 1483
    }

1484 1485
    if (virDomainGetInfo (dom, &info) == -1) {
        virDomainFree(dom);
1486
        remoteDispatchConnError(rerr, conn);
1487
        return -1;
1488
    }
1489 1490 1491 1492 1493 1494 1495

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

1496 1497
    virDomainFree(dom);

1498 1499 1500 1501
    return 0;
}

static int
1502
remoteDispatchDomainGetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
1503 1504
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1505
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1506
                                  remote_error *rerr,
1507 1508 1509 1510 1511
                                  remote_domain_get_max_memory_args *args,
                                  remote_domain_get_max_memory_ret *ret)
{
    virDomainPtr dom;

1512
    dom = get_nonnull_domain (conn, args->dom);
1513
    if (dom == NULL) {
1514
        remoteDispatchConnError(rerr, conn);
1515
        return -1;
1516 1517 1518
    }

    ret->memory = virDomainGetMaxMemory (dom);
1519 1520
    if (ret->memory == 0) {
        virDomainFree(dom);
1521
        remoteDispatchConnError(rerr, conn);
1522 1523 1524
        return -1;
    }
    virDomainFree(dom);
1525 1526 1527 1528
    return 0;
}

static int
1529
remoteDispatchDomainGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1530 1531
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
1532
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
1533
                                 remote_error *rerr,
1534 1535 1536 1537 1538
                                 remote_domain_get_max_vcpus_args *args,
                                 remote_domain_get_max_vcpus_ret *ret)
{
    virDomainPtr dom;

1539
    dom = get_nonnull_domain (conn, args->dom);
1540
    if (dom == NULL) {
1541
        remoteDispatchConnError(rerr, conn);
1542
        return -1;
1543 1544 1545
    }

    ret->num = virDomainGetMaxVcpus (dom);
1546 1547
    if (ret->num == -1) {
        virDomainFree(dom);
1548
        remoteDispatchConnError(rerr, conn);
1549 1550 1551
        return -1;
    }
    virDomainFree(dom);
1552 1553 1554
    return 0;
}

1555 1556 1557 1558
static int
remoteDispatchDomainGetSecurityLabel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1559
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
                                     remote_error *rerr,
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
{
    virDomainPtr dom;
    virSecurityLabel seclabel;

    dom = get_nonnull_domain(conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    memset(&seclabel, 0, sizeof seclabel);
    if (virDomainGetSecurityLabel(dom, &seclabel) == -1) {
        virDomainFree(dom);
1576
        remoteDispatchConnError(rerr, conn);
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
        return -1;
    }

    ret->label.label_len = strlen(seclabel.label) + 1;
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0) {
        virDomainFree(dom);
        remoteDispatchOOMError(rerr);
        return -1;
    }
    strcpy(ret->label.label_val, seclabel.label);
    ret->enforcing = seclabel.enforcing;
    virDomainFree(dom);

    return 0;
}

static int
remoteDispatchNodeGetSecurityModel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1597
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1598 1599 1600 1601 1602 1603 1604 1605
                                   remote_error *rerr,
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_node_get_security_model_ret *ret)
{
    virSecurityModel secmodel;

    memset(&secmodel, 0, sizeof secmodel);
    if (virNodeGetSecurityModel(conn, &secmodel) == -1) {
1606
        remoteDispatchConnError(rerr, conn);
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
        return -1;
    }

    ret->model.model_len = strlen(secmodel.model) + 1;
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0) {
        remoteDispatchOOMError(rerr);
        return -1;
    }
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0) {
        remoteDispatchOOMError(rerr);
        return -1;
    }
    strcpy(ret->doi.doi_val, secmodel.doi);

    return 0;
}

1627
static int
1628
remoteDispatchDomainGetOsType (struct qemud_server *server ATTRIBUTE_UNUSED,
1629 1630
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1631
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1632
                               remote_error *rerr,
1633 1634 1635 1636 1637
                               remote_domain_get_os_type_args *args,
                               remote_domain_get_os_type_ret *ret)
{
    virDomainPtr dom;

1638
    dom = get_nonnull_domain (conn, args->dom);
1639
    if (dom == NULL) {
1640
        remoteDispatchConnError(rerr, conn);
1641
        return -1;
1642 1643 1644 1645
    }

    /* remoteDispatchClientRequest will free this */
    ret->type = virDomainGetOSType (dom);
1646
    if (ret->type == NULL) {
1647
        virDomainFree(dom);
1648
        remoteDispatchConnError(rerr, conn);
1649
        return -1;
1650 1651
    }
    virDomainFree(dom);
1652 1653 1654 1655
    return 0;
}

static int
1656
remoteDispatchDomainGetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1657 1658
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
1659
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
1660
                              remote_error *rerr,
1661 1662 1663
                              remote_domain_get_vcpus_args *args,
                              remote_domain_get_vcpus_ret *ret)
{
1664 1665 1666
    virDomainPtr dom = NULL;
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
1667 1668
    int info_len, i;

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

    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1676
        virDomainFree(dom);
1677 1678
        remoteDispatchFormatError (rerr, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
        return -1;
1679 1680
    }

1681
    if (args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1682
        virDomainFree(dom);
1683 1684
        remoteDispatchFormatError (rerr, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
        return -1;
1685 1686 1687
    }

    /* Allocate buffers to take the results. */
1688 1689
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
        goto oom;
1690 1691
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
1692
        goto oom;
1693 1694 1695 1696

    info_len = virDomainGetVcpus (dom,
                                  info, args->maxinfo,
                                  cpumaps, args->maplen);
1697
    if (info_len == -1) {
1698 1699
        VIR_FREE(info);
        VIR_FREE(cpumaps);
1700
        virDomainFree(dom);
1701
        remoteDispatchConnError(rerr, conn);
1702 1703
        return -1;
    }
1704 1705 1706

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
1707 1708
    if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
        goto oom;
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720

    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.
     */
1721
    ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
1722 1723
    ret->cpumaps.cpumaps_val = (char *) cpumaps;

1724
    VIR_FREE(info);
1725
    virDomainFree(dom);
1726
    return 0;
1727 1728 1729 1730 1731

oom:
    VIR_FREE(info);
    VIR_FREE(cpumaps);
    virDomainFree(dom);
1732 1733
    remoteDispatchOOMError(rerr);
    return -1;
1734 1735
}

1736
static int
1737
remoteDispatchDomainMigratePrepare (struct qemud_server *server ATTRIBUTE_UNUSED,
1738 1739
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1740
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1741
                                    remote_error *rerr,
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
                                    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 ... */
1756
    if (VIR_ALLOC(uri_out) < 0) {
1757 1758
        remoteDispatchOOMError(rerr);
        return -1;
1759
    }
1760

1761
    r = virDomainMigratePrepare (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1762 1763
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1764
    if (r == -1) {
1765
        VIR_FREE(uri_out);
1766
        remoteDispatchConnError(rerr, conn);
D
Daniel P. Berrange 已提交
1767 1768
        return -1;
    }
1769 1770 1771 1772 1773 1774

    /* 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 已提交
1775 1776
    if (*uri_out == NULL) {
        ret->uri_out = NULL;
1777
        VIR_FREE(uri_out);
D
Daniel P. Berrange 已提交
1778 1779 1780
    } else {
        ret->uri_out = uri_out;
    }
1781 1782 1783 1784 1785

    return 0;
}

static int
1786
remoteDispatchDomainMigratePerform (struct qemud_server *server ATTRIBUTE_UNUSED,
1787 1788
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1789
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1790
                                    remote_error *rerr,
1791 1792 1793 1794 1795 1796 1797
                                    remote_domain_migrate_perform_args *args,
                                    void *ret ATTRIBUTE_UNUSED)
{
    int r;
    virDomainPtr dom;
    char *dname;

1798
    dom = get_nonnull_domain (conn, args->dom);
1799
    if (dom == NULL) {
1800
        remoteDispatchConnError(rerr, conn);
1801
        return -1;
1802 1803 1804 1805
    }

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

D
Daniel P. Berrange 已提交
1806 1807 1808 1809 1810
    r = virDomainMigratePerform (dom,
                                 args->cookie.cookie_val,
                                 args->cookie.cookie_len,
                                 args->uri,
                                 args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1811
    virDomainFree (dom);
1812
    if (r == -1) {
1813
        remoteDispatchConnError(rerr, conn);
1814 1815
        return -1;
    }
1816 1817 1818 1819 1820

    return 0;
}

static int
1821
remoteDispatchDomainMigrateFinish (struct qemud_server *server ATTRIBUTE_UNUSED,
1822 1823
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1824
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1825
                                   remote_error *rerr,
1826 1827 1828 1829 1830 1831
                                   remote_domain_migrate_finish_args *args,
                                   remote_domain_migrate_finish_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

1832
    ddom = virDomainMigrateFinish (conn, args->dname,
D
Daniel P. Berrange 已提交
1833 1834 1835 1836
                                   args->cookie.cookie_val,
                                   args->cookie.cookie_len,
                                   args->uri,
                                   args->flags);
1837
    if (ddom == NULL) {
1838
        remoteDispatchConnError(rerr, conn);
1839 1840
        return -1;
    }
1841 1842

    make_nonnull_domain (&ret->ddom, ddom);
D
Daniel P. Berrange 已提交
1843
    virDomainFree (ddom);
1844 1845 1846
    return 0;
}

D
Daniel Veillard 已提交
1847 1848
static int
remoteDispatchDomainMigratePrepare2 (struct qemud_server *server ATTRIBUTE_UNUSED,
1849 1850
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1851
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
1852
                                     remote_error *rerr,
D
Daniel Veillard 已提交
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
                                     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) {
1869 1870
        remoteDispatchOOMError(rerr);
        return -1;
D
Daniel Veillard 已提交
1871 1872
    }

1873
    r = virDomainMigratePrepare2 (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1874 1875 1876
                                  uri_in, uri_out,
                                  args->flags, dname, args->resource,
                                  args->dom_xml);
1877
    if (r == -1) {
1878
        remoteDispatchConnError(rerr, conn);
1879 1880
        return -1;
    }
D
Daniel Veillard 已提交
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893

    /* 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,
1894 1895
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1896
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1897
                                    remote_error *rerr,
D
Daniel Veillard 已提交
1898 1899 1900 1901 1902 1903
                                    remote_domain_migrate_finish2_args *args,
                                    remote_domain_migrate_finish2_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

1904
    ddom = virDomainMigrateFinish2 (conn, args->dname,
D
Daniel P. Berrange 已提交
1905 1906 1907 1908 1909
                                    args->cookie.cookie_val,
                                    args->cookie.cookie_len,
                                    args->uri,
                                    args->flags,
                                    args->retcode);
1910
    if (ddom == NULL) {
1911
        remoteDispatchConnError(rerr, conn);
1912 1913
        return -1;
    }
D
Daniel Veillard 已提交
1914 1915 1916 1917 1918 1919

    make_nonnull_domain (&ret->ddom, ddom);

    return 0;
}

C
Chris Lalancette 已提交
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
static int
remoteDispatchDomainMigratePrepareTunnel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                         struct qemud_client *client,
                                         virConnectPtr conn,
                                         remote_message_header *hdr,
                                         remote_error *rerr,
                                         remote_domain_migrate_prepare_tunnel_args *args,
                                         void *ret ATTRIBUTE_UNUSED)
{
    int r;
    char *dname;
    struct qemud_client_stream *stream;
    CHECK_CONN (client);

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

    stream = remoteCreateClientStream(conn, hdr);
    if (!stream) {
        remoteDispatchOOMError(rerr);
        return -1;
    }

1942
    r = virDomainMigratePrepareTunnel(conn, stream->st,
C
Chris Lalancette 已提交
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
                                      args->flags, dname, args->resource,
                                      args->dom_xml);
    if (r == -1) {
        remoteFreeClientStream(client, stream);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (remoteAddClientStream(client, stream, 0) < 0) {
        remoteDispatchConnError(rerr, conn);
        virStreamAbort(stream->st);
        remoteFreeClientStream(client, stream);
        return -1;
    }

    return 0;
}

1961
static int
1962
remoteDispatchListDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
1963 1964
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1965
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1966
                                  remote_error *rerr,
1967 1968 1969 1970 1971
                                  remote_list_defined_domains_args *args,
                                  remote_list_defined_domains_ret *ret)
{

    if (args->maxnames > REMOTE_DOMAIN_NAME_LIST_MAX) {
1972 1973 1974
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_DOMAIN_NAME_LIST_MAX"));
        return -1;
1975 1976 1977
    }

    /* Allocate return buffer. */
1978
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
1979 1980
        remoteDispatchOOMError(rerr);
        return -1;
1981
    }
1982 1983

    ret->names.names_len =
1984
        virConnectListDefinedDomains (conn,
1985
                                      ret->names.names_val, args->maxnames);
1986 1987
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
1988
        remoteDispatchConnError(rerr, conn);
1989 1990
        return -1;
    }
1991 1992 1993 1994 1995

    return 0;
}

static int
1996
remoteDispatchDomainLookupById (struct qemud_server *server ATTRIBUTE_UNUSED,
1997 1998
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
1999
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
2000
                                remote_error *rerr,
2001 2002 2003 2004 2005
                                remote_domain_lookup_by_id_args *args,
                                remote_domain_lookup_by_id_ret *ret)
{
    virDomainPtr dom;

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

    make_nonnull_domain (&ret->dom, dom);
2013
    virDomainFree(dom);
2014 2015 2016 2017
    return 0;
}

static int
2018
remoteDispatchDomainLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
2019 2020
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2021
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2022
                                  remote_error *rerr,
2023 2024 2025 2026 2027
                                  remote_domain_lookup_by_name_args *args,
                                  remote_domain_lookup_by_name_ret *ret)
{
    virDomainPtr dom;

2028
    dom = virDomainLookupByName (conn, args->name);
2029
    if (dom == NULL) {
2030
        remoteDispatchConnError(rerr, conn);
2031 2032
        return -1;
    }
2033 2034

    make_nonnull_domain (&ret->dom, dom);
2035
    virDomainFree(dom);
2036 2037 2038 2039
    return 0;
}

static int
2040
remoteDispatchDomainLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
2041 2042
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2043
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2044
                                  remote_error *rerr,
2045 2046 2047 2048 2049
                                  remote_domain_lookup_by_uuid_args *args,
                                  remote_domain_lookup_by_uuid_ret *ret)
{
    virDomainPtr dom;

2050
    dom = virDomainLookupByUUID (conn, (unsigned char *) args->uuid);
2051
    if (dom == NULL) {
2052
        remoteDispatchConnError(rerr, conn);
2053 2054
        return -1;
    }
2055 2056

    make_nonnull_domain (&ret->dom, dom);
2057
    virDomainFree(dom);
2058 2059 2060 2061
    return 0;
}

static int
2062
remoteDispatchNumOfDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2063 2064
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2065
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2066
                                   remote_error *rerr,
2067 2068 2069 2070
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_num_of_defined_domains_ret *ret)
{

2071
    ret->num = virConnectNumOfDefinedDomains (conn);
2072
    if (ret->num == -1) {
2073
        remoteDispatchConnError(rerr, conn);
2074 2075
        return -1;
    }
2076 2077 2078 2079 2080

    return 0;
}

static int
2081
remoteDispatchDomainPinVcpu (struct qemud_server *server ATTRIBUTE_UNUSED,
2082 2083
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2084
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2085
                             remote_error *rerr,
2086 2087 2088 2089 2090 2091
                             remote_domain_pin_vcpu_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int rv;

2092
    dom = get_nonnull_domain (conn, args->dom);
2093
    if (dom == NULL) {
2094
        remoteDispatchConnError(rerr, conn);
2095
        return -1;
2096 2097 2098
    }

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
2099
        virDomainFree(dom);
2100 2101
        remoteDispatchFormatError (rerr, "%s", _("cpumap_len > REMOTE_CPUMAP_MAX"));
        return -1;
2102 2103 2104 2105 2106
    }

    rv = virDomainPinVcpu (dom, args->vcpu,
                           (unsigned char *) args->cpumap.cpumap_val,
                           args->cpumap.cpumap_len);
2107 2108
    if (rv == -1) {
        virDomainFree(dom);
2109
        remoteDispatchConnError(rerr, conn);
2110 2111 2112
        return -1;
    }
    virDomainFree(dom);
2113 2114 2115 2116
    return 0;
}

static int
2117
remoteDispatchDomainReboot (struct qemud_server *server ATTRIBUTE_UNUSED,
2118 2119
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2120
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2121
                            remote_error *rerr,
2122 2123 2124 2125 2126
                            remote_domain_reboot_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2127
    dom = get_nonnull_domain (conn, args->dom);
2128
    if (dom == NULL) {
2129
        remoteDispatchConnError(rerr, conn);
2130
        return -1;
2131 2132
    }

2133 2134
    if (virDomainReboot (dom, args->flags) == -1) {
        virDomainFree(dom);
2135
        remoteDispatchConnError(rerr, conn);
2136
        return -1;
2137 2138
    }
    virDomainFree(dom);
2139 2140 2141 2142
    return 0;
}

static int
2143
remoteDispatchDomainRestore (struct qemud_server *server ATTRIBUTE_UNUSED,
2144 2145
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2146
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2147
                             remote_error *rerr,
2148 2149 2150 2151
                             remote_domain_restore_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{

2152 2153
    if (virDomainRestore (conn, args->from) == -1) {
        remoteDispatchConnError(rerr, conn);
2154
        return -1;
2155
    }
2156 2157 2158 2159 2160

    return 0;
}

static int
2161
remoteDispatchDomainResume (struct qemud_server *server ATTRIBUTE_UNUSED,
2162 2163
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2164
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2165
                            remote_error *rerr,
2166 2167 2168 2169 2170
                            remote_domain_resume_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2171
    dom = get_nonnull_domain (conn, args->dom);
2172
    if (dom == NULL) {
2173
        remoteDispatchConnError(rerr, conn);
2174
        return -1;
2175 2176
    }

2177 2178
    if (virDomainResume (dom) == -1) {
        virDomainFree(dom);
2179
        remoteDispatchConnError(rerr, conn);
2180
        return -1;
2181 2182
    }
    virDomainFree(dom);
2183 2184 2185 2186
    return 0;
}

static int
2187
remoteDispatchDomainSave (struct qemud_server *server ATTRIBUTE_UNUSED,
2188 2189
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
2190
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
2191
                          remote_error *rerr,
2192 2193 2194 2195 2196
                          remote_domain_save_args *args,
                          void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2197
    dom = get_nonnull_domain (conn, args->dom);
2198
    if (dom == NULL) {
2199
        remoteDispatchConnError(rerr, conn);
2200
        return -1;
2201 2202
    }

2203 2204
    if (virDomainSave (dom, args->to) == -1) {
        virDomainFree(dom);
2205
        remoteDispatchConnError(rerr, conn);
2206
        return -1;
2207 2208
    }
    virDomainFree(dom);
2209 2210 2211 2212
    return 0;
}

static int
2213
remoteDispatchDomainCoreDump (struct qemud_server *server ATTRIBUTE_UNUSED,
2214 2215
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2216
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2217
                              remote_error *rerr,
2218 2219 2220 2221 2222
                              remote_domain_core_dump_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2223
    dom = get_nonnull_domain (conn, args->dom);
2224
    if (dom == NULL) {
2225
        remoteDispatchConnError(rerr, conn);
2226
        return -1;
2227 2228
    }

2229 2230
    if (virDomainCoreDump (dom, args->to, args->flags) == -1) {
        virDomainFree(dom);
2231
        remoteDispatchConnError(rerr, conn);
2232
        return -1;
2233 2234
    }
    virDomainFree(dom);
2235 2236 2237 2238
    return 0;
}

static int
2239
remoteDispatchDomainSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
2240 2241
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2242
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2243
                                  remote_error *rerr,
2244 2245 2246 2247 2248
                                  remote_domain_set_autostart_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2249
    dom = get_nonnull_domain (conn, args->dom);
2250
    if (dom == NULL) {
2251
        remoteDispatchConnError(rerr, conn);
2252
        return -1;
2253 2254
    }

2255 2256
    if (virDomainSetAutostart (dom, args->autostart) == -1) {
        virDomainFree(dom);
2257
        remoteDispatchConnError(rerr, conn);
2258
        return -1;
2259 2260
    }
    virDomainFree(dom);
2261 2262 2263 2264
    return 0;
}

static int
2265
remoteDispatchDomainSetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
2266 2267
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2268
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2269
                                  remote_error *rerr,
2270 2271 2272 2273 2274
                                  remote_domain_set_max_memory_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2275
    dom = get_nonnull_domain (conn, args->dom);
2276
    if (dom == NULL) {
2277
        remoteDispatchConnError(rerr, conn);
2278
        return -1;
2279 2280
    }

2281 2282
    if (virDomainSetMaxMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
2283
        remoteDispatchConnError(rerr, conn);
2284
        return -1;
2285 2286
    }
    virDomainFree(dom);
2287 2288 2289 2290
    return 0;
}

static int
2291
remoteDispatchDomainSetMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
2292 2293
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
2294
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
2295
                               remote_error *rerr,
2296 2297 2298 2299 2300
                               remote_domain_set_memory_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2301
    dom = get_nonnull_domain (conn, args->dom);
2302
    if (dom == NULL) {
2303
        remoteDispatchConnError(rerr, conn);
2304
        return -1;
2305 2306
    }

2307 2308
    if (virDomainSetMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
2309
        remoteDispatchConnError(rerr, conn);
2310
        return -1;
2311 2312
    }
    virDomainFree(dom);
2313 2314 2315 2316
    return 0;
}

static int
2317
remoteDispatchDomainSetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
2318 2319
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2320
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2321
                              remote_error *rerr,
2322 2323 2324 2325 2326
                              remote_domain_set_vcpus_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2327
    dom = get_nonnull_domain (conn, args->dom);
2328
    if (dom == NULL) {
2329
        remoteDispatchConnError(rerr, conn);
2330
        return -1;
2331 2332
    }

2333 2334
    if (virDomainSetVcpus (dom, args->nvcpus) == -1) {
        virDomainFree(dom);
2335
        remoteDispatchConnError(rerr, conn);
2336
        return -1;
2337 2338
    }
    virDomainFree(dom);
2339 2340 2341 2342
    return 0;
}

static int
2343
remoteDispatchDomainShutdown (struct qemud_server *server ATTRIBUTE_UNUSED,
2344 2345
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2346
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2347
                              remote_error *rerr,
2348 2349 2350 2351 2352
                              remote_domain_shutdown_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2353
    dom = get_nonnull_domain (conn, args->dom);
2354
    if (dom == NULL) {
2355
        remoteDispatchConnError(rerr, conn);
2356
        return -1;
2357 2358
    }

2359 2360
    if (virDomainShutdown (dom) == -1) {
        virDomainFree(dom);
2361
        remoteDispatchConnError(rerr, conn);
2362
        return -1;
2363 2364
    }
    virDomainFree(dom);
2365 2366 2367 2368
    return 0;
}

static int
2369
remoteDispatchDomainSuspend (struct qemud_server *server ATTRIBUTE_UNUSED,
2370 2371
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2372
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2373
                             remote_error *rerr,
2374 2375 2376 2377 2378
                             remote_domain_suspend_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2379
    dom = get_nonnull_domain (conn, args->dom);
2380
    if (dom == NULL) {
2381
        remoteDispatchConnError(rerr, conn);
2382
        return -1;
2383 2384
    }

2385 2386
    if (virDomainSuspend (dom) == -1) {
        virDomainFree(dom);
2387
        remoteDispatchConnError(rerr, conn);
2388
        return -1;
2389 2390
    }
    virDomainFree(dom);
2391 2392 2393 2394
    return 0;
}

static int
2395
remoteDispatchDomainUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
2396 2397
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2398
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2399
                              remote_error *rerr,
2400 2401 2402 2403 2404
                              remote_domain_undefine_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2405
    dom = get_nonnull_domain (conn, args->dom);
2406
    if (dom == NULL) {
2407
        remoteDispatchConnError(rerr, conn);
2408
        return -1;
2409 2410
    }

2411 2412
    if (virDomainUndefine (dom) == -1) {
        virDomainFree(dom);
2413
        remoteDispatchConnError(rerr, conn);
2414
        return -1;
2415 2416
    }
    virDomainFree(dom);
2417 2418 2419 2420
    return 0;
}

static int
2421
remoteDispatchListDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2422 2423
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2424
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2425
                                   remote_error *rerr,
2426 2427 2428 2429 2430
                                   remote_list_defined_networks_args *args,
                                   remote_list_defined_networks_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
2431 2432 2433
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
2434 2435 2436
    }

    /* Allocate return buffer. */
2437
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
2438 2439
        remoteDispatchOOMError(rerr);
        return -1;
2440
    }
2441 2442

    ret->names.names_len =
2443
        virConnectListDefinedNetworks (conn,
2444
                                       ret->names.names_val, args->maxnames);
2445 2446
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
2447
        remoteDispatchConnError(rerr, conn);
2448 2449
        return -1;
    }
2450 2451 2452 2453 2454

    return 0;
}

static int
2455
remoteDispatchListDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2456 2457
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
2458
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
2459
                           remote_error *rerr,
2460 2461 2462 2463 2464
                           remote_list_domains_args *args,
                           remote_list_domains_ret *ret)
{

    if (args->maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
2465 2466 2467
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxids > REMOTE_DOMAIN_ID_LIST_MAX"));
        return -1;
2468 2469 2470
    }

    /* Allocate return buffer. */
2471
    if (VIR_ALLOC_N(ret->ids.ids_val, args->maxids) < 0) {
2472 2473
        remoteDispatchOOMError(rerr);
        return -1;
2474
    }
2475

2476
    ret->ids.ids_len = virConnectListDomains (conn,
2477
                                              ret->ids.ids_val, args->maxids);
2478 2479
    if (ret->ids.ids_len == -1) {
        VIR_FREE(ret->ids.ids_val);
2480
        remoteDispatchConnError(rerr, conn);
2481 2482
        return -1;
    }
2483 2484 2485 2486

    return 0;
}

2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
static int
remoteDispatchDomainManagedSave (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_domain_managed_save_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

    dom = get_nonnull_domain (conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainManagedSave (dom, args->flags) == -1) {
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    virDomainFree(dom);
    return 0;
}

static int
remoteDispatchDomainHasManagedSaveImage (struct qemud_server *server ATTRIBUTE_UNUSED,
                                         struct qemud_client *client ATTRIBUTE_UNUSED,
                                         virConnectPtr conn,
                                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                                         remote_error *rerr,
                                         remote_domain_has_managed_save_image_args *args,
                                         remote_domain_has_managed_save_image_ret *ret)
{
    virDomainPtr dom;

    dom = get_nonnull_domain (conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    ret->ret = virDomainHasManagedSaveImage (dom, args->flags);
    if (ret->ret == -1) {
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    virDomainFree(dom);
    return 0;
}

static int
remoteDispatchDomainManagedSaveRemove (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_managed_save_remove_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

    dom = get_nonnull_domain (conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainManagedSaveRemove (dom, args->flags) == -1) {
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    virDomainFree(dom);
    return 0;
}

2566
static int
2567
remoteDispatchListNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2568 2569
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2570
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2571
                            remote_error *rerr,
2572 2573 2574 2575 2576
                            remote_list_networks_args *args,
                            remote_list_networks_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
2577 2578 2579
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
2580 2581 2582
    }

    /* Allocate return buffer. */
2583
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
2584 2585
        remoteDispatchOOMError(rerr);
        return -1;
2586
    }
2587 2588

    ret->names.names_len =
2589
        virConnectListNetworks (conn,
2590
                                ret->names.names_val, args->maxnames);
2591 2592
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_len);
2593
        remoteDispatchConnError(rerr, conn);
2594 2595
        return -1;
    }
2596 2597 2598 2599 2600

    return 0;
}

static int
2601
remoteDispatchNetworkCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
2602 2603
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2604
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2605
                             remote_error *rerr,
2606 2607 2608 2609 2610
                             remote_network_create_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2611
    net = get_nonnull_network (conn, args->net);
2612
    if (net == NULL) {
2613
        remoteDispatchConnError(rerr, conn);
2614
        return -1;
2615 2616
    }

2617 2618
    if (virNetworkCreate (net) == -1) {
        virNetworkFree(net);
2619
        remoteDispatchConnError(rerr, conn);
2620
        return -1;
2621 2622
    }
    virNetworkFree(net);
2623 2624 2625 2626
    return 0;
}

static int
2627
remoteDispatchNetworkCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2628 2629
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2630
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
2631
                                remote_error *rerr,
2632 2633 2634 2635 2636
                                remote_network_create_xml_args *args,
                                remote_network_create_xml_ret *ret)
{
    virNetworkPtr net;

2637
    net = virNetworkCreateXML (conn, args->xml);
2638
    if (net == NULL) {
2639
        remoteDispatchConnError(rerr, conn);
2640 2641
        return -1;
    }
2642 2643

    make_nonnull_network (&ret->net, net);
2644
    virNetworkFree(net);
2645 2646 2647 2648
    return 0;
}

static int
2649
remoteDispatchNetworkDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2650 2651
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2652
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
2653
                                remote_error *rerr,
2654 2655 2656 2657 2658
                                remote_network_define_xml_args *args,
                                remote_network_define_xml_ret *ret)
{
    virNetworkPtr net;

2659
    net = virNetworkDefineXML (conn, args->xml);
2660
    if (net == NULL) {
2661
        remoteDispatchConnError(rerr, conn);
2662 2663
        return -1;
    }
2664 2665

    make_nonnull_network (&ret->net, net);
2666
    virNetworkFree(net);
2667 2668 2669 2670
    return 0;
}

static int
2671
remoteDispatchNetworkDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
2672 2673
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2674
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2675
                              remote_error *rerr,
2676 2677 2678 2679 2680
                              remote_network_destroy_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2681
    net = get_nonnull_network (conn, args->net);
2682
    if (net == NULL) {
2683
        remoteDispatchConnError(rerr, conn);
2684
        return -1;
2685 2686
    }

2687 2688
    if (virNetworkDestroy (net) == -1) {
        virNetworkFree(net);
2689
        remoteDispatchConnError(rerr, conn);
2690
        return -1;
2691 2692
    }
    virNetworkFree(net);
2693 2694 2695 2696
    return 0;
}

static int
2697
remoteDispatchNetworkDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2698 2699
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2700
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2701
                              remote_error *rerr,
2702 2703 2704 2705 2706
                              remote_network_dump_xml_args *args,
                              remote_network_dump_xml_ret *ret)
{
    virNetworkPtr net;

2707
    net = get_nonnull_network (conn, args->net);
2708
    if (net == NULL) {
2709
        remoteDispatchConnError(rerr, conn);
2710
        return -1;
2711 2712 2713 2714
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNetworkGetXMLDesc (net, args->flags);
2715 2716
    if (!ret->xml) {
        virNetworkFree(net);
2717
        remoteDispatchConnError(rerr, conn);
2718 2719 2720
        return -1;
    }
    virNetworkFree(net);
2721 2722 2723 2724
    return 0;
}

static int
2725
remoteDispatchNetworkGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
2726 2727
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2728
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2729
                                   remote_error *rerr,
2730 2731 2732 2733 2734
                                   remote_network_get_autostart_args *args,
                                   remote_network_get_autostart_ret *ret)
{
    virNetworkPtr net;

2735
    net = get_nonnull_network (conn, args->net);
2736
    if (net == NULL) {
2737
        remoteDispatchConnError(rerr, conn);
2738
        return -1;
2739 2740
    }

2741 2742
    if (virNetworkGetAutostart (net, &ret->autostart) == -1) {
        virNetworkFree(net);
2743
        remoteDispatchConnError(rerr, conn);
2744
        return -1;
2745 2746
    }
    virNetworkFree(net);
2747 2748 2749 2750
    return 0;
}

static int
2751
remoteDispatchNetworkGetBridgeName (struct qemud_server *server ATTRIBUTE_UNUSED,
2752 2753
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
2754
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
2755
                                    remote_error *rerr,
2756 2757 2758 2759 2760
                                    remote_network_get_bridge_name_args *args,
                                    remote_network_get_bridge_name_ret *ret)
{
    virNetworkPtr net;

2761
    net = get_nonnull_network (conn, args->net);
2762
    if (net == NULL) {
2763
        remoteDispatchConnError(rerr, conn);
2764
        return -1;
2765 2766 2767 2768
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virNetworkGetBridgeName (net);
2769 2770
    if (!ret->name) {
        virNetworkFree(net);
2771
        remoteDispatchConnError(rerr, conn);
2772 2773 2774
        return -1;
    }
    virNetworkFree(net);
2775 2776 2777 2778
    return 0;
}

static int
2779
remoteDispatchNetworkLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
2780 2781
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2782
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2783
                                   remote_error *rerr,
2784 2785 2786 2787 2788
                                   remote_network_lookup_by_name_args *args,
                                   remote_network_lookup_by_name_ret *ret)
{
    virNetworkPtr net;

2789
    net = virNetworkLookupByName (conn, args->name);
2790
    if (net == NULL) {
2791
        remoteDispatchConnError(rerr, conn);
2792 2793
        return -1;
    }
2794 2795

    make_nonnull_network (&ret->net, net);
2796
    virNetworkFree(net);
2797 2798 2799 2800
    return 0;
}

static int
2801
remoteDispatchNetworkLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
2802 2803
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2804
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2805
                                   remote_error *rerr,
2806 2807 2808 2809 2810
                                   remote_network_lookup_by_uuid_args *args,
                                   remote_network_lookup_by_uuid_ret *ret)
{
    virNetworkPtr net;

2811
    net = virNetworkLookupByUUID (conn, (unsigned char *) args->uuid);
2812
    if (net == NULL) {
2813
        remoteDispatchConnError(rerr, conn);
2814 2815
        return -1;
    }
2816 2817

    make_nonnull_network (&ret->net, net);
2818
    virNetworkFree(net);
2819 2820 2821 2822
    return 0;
}

static int
2823
remoteDispatchNetworkSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
2824 2825
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2826
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2827
                                   remote_error *rerr,
2828 2829 2830 2831 2832
                                   remote_network_set_autostart_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2833
    net = get_nonnull_network (conn, args->net);
2834
    if (net == NULL) {
2835
        remoteDispatchConnError(rerr, conn);
2836
        return -1;
2837 2838
    }

2839 2840
    if (virNetworkSetAutostart (net, args->autostart) == -1) {
        virNetworkFree(net);
2841
        remoteDispatchConnError(rerr, conn);
2842
        return -1;
2843 2844
    }
    virNetworkFree(net);
2845 2846 2847 2848
    return 0;
}

static int
2849
remoteDispatchNetworkUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
2850 2851
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
2852
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
2853
                               remote_error *rerr,
2854 2855 2856 2857 2858
                               remote_network_undefine_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2859
    net = get_nonnull_network (conn, args->net);
2860
    if (net == NULL) {
2861
        remoteDispatchConnError(rerr, conn);
2862
        return -1;
2863 2864
    }

2865 2866
    if (virNetworkUndefine (net) == -1) {
        virNetworkFree(net);
2867
        remoteDispatchConnError(rerr, conn);
2868
        return -1;
2869 2870
    }
    virNetworkFree(net);
2871 2872 2873 2874
    return 0;
}

static int
2875
remoteDispatchNumOfDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2876 2877
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
2878
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
2879
                                    remote_error *rerr,
2880 2881 2882 2883
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_num_of_defined_networks_ret *ret)
{

2884
    ret->num = virConnectNumOfDefinedNetworks (conn);
2885
    if (ret->num == -1) {
2886
        remoteDispatchConnError(rerr, conn);
2887 2888
        return -1;
    }
2889 2890 2891 2892 2893

    return 0;
}

static int
2894
remoteDispatchNumOfDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2895 2896
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2897
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2898
                            remote_error *rerr,
2899 2900 2901 2902
                            void *args ATTRIBUTE_UNUSED,
                            remote_num_of_domains_ret *ret)
{

2903
    ret->num = virConnectNumOfDomains (conn);
2904
    if (ret->num == -1) {
2905
        remoteDispatchConnError(rerr, conn);
2906 2907
        return -1;
    }
2908 2909 2910 2911 2912

    return 0;
}

static int
2913
remoteDispatchNumOfNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2914 2915
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2916
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2917
                             remote_error *rerr,
2918 2919 2920 2921
                             void *args ATTRIBUTE_UNUSED,
                             remote_num_of_networks_ret *ret)
{

2922
    ret->num = virConnectNumOfNetworks (conn);
2923
    if (ret->num == -1) {
2924
        remoteDispatchConnError(rerr, conn);
2925 2926
        return -1;
    }
2927 2928 2929 2930

    return 0;
}

2931

D
Daniel Veillard 已提交
2932 2933 2934 2935 2936
/*-------------------------------------------------------------*/
static int
remoteDispatchNumOfInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
2937
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955
                               remote_error *rerr,
                               void *args ATTRIBUTE_UNUSED,
                               remote_num_of_interfaces_ret *ret)
{

    ret->num = virConnectNumOfInterfaces (conn);
    if (ret->num == -1) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    return 0;
}

static int
remoteDispatchListInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2956
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985
                              remote_error *rerr,
                              remote_list_interfaces_args *args,
                              remote_list_interfaces_ret *ret)
{

    if (args->maxnames > REMOTE_INTERFACE_NAME_LIST_MAX) {
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_INTERFACE_NAME_LIST_MAX"));
        return -1;
    }

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

    ret->names.names_len =
        virConnectListInterfaces (conn,
                                  ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_len);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    return 0;
}

2986 2987 2988 2989
static int
remoteDispatchNumOfDefinedInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
2990
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008
                                      remote_error *rerr,
                                      void *args ATTRIBUTE_UNUSED,
                                      remote_num_of_defined_interfaces_ret *ret)
{

    ret->num = virConnectNumOfDefinedInterfaces (conn);
    if (ret->num == -1) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    return 0;
}

static int
remoteDispatchListDefinedInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
3009
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
                                     remote_error *rerr,
                                     remote_list_defined_interfaces_args *args,
                                     remote_list_defined_interfaces_ret *ret)
{

    if (args->maxnames > REMOTE_DEFINED_INTERFACE_NAME_LIST_MAX) {
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_DEFINED_INTERFACE_NAME_LIST_MAX"));
        return -1;
    }

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

    ret->names.names_len =
        virConnectListDefinedInterfaces (conn,
                                         ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_len);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    return 0;
}

D
Daniel Veillard 已提交
3039 3040 3041 3042
static int
remoteDispatchInterfaceLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
3043
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3044 3045 3046 3047
                                     remote_error *rerr,
                                     remote_interface_lookup_by_name_args *args,
                                     remote_interface_lookup_by_name_ret *ret)
{
3048
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3049

3050 3051
    iface = virInterfaceLookupByName (conn, args->name);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3052 3053 3054 3055
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3056 3057
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3058 3059 3060 3061 3062 3063 3064
    return 0;
}

static int
remoteDispatchInterfaceLookupByMacString (struct qemud_server *server ATTRIBUTE_UNUSED,
                                          struct qemud_client *client ATTRIBUTE_UNUSED,
                                          virConnectPtr conn,
3065
                                          remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3066 3067 3068 3069
                                          remote_error *rerr,
                                          remote_interface_lookup_by_mac_string_args *args,
                                          remote_interface_lookup_by_mac_string_ret *ret)
{
3070
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3071

3072 3073
    iface = virInterfaceLookupByMACString (conn, args->mac);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3074 3075 3076 3077
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3078 3079
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3080 3081 3082 3083 3084 3085 3086
    return 0;
}

static int
remoteDispatchInterfaceGetXmlDesc (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3087
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3088 3089 3090 3091
                                   remote_error *rerr,
                                   remote_interface_get_xml_desc_args *args,
                                   remote_interface_get_xml_desc_ret *ret)
{
3092
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3093

3094 3095
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3096 3097 3098 3099 3100
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    /* remoteDispatchClientRequest will free this. */
3101
    ret->xml = virInterfaceGetXMLDesc (iface, args->flags);
D
Daniel Veillard 已提交
3102
    if (!ret->xml) {
3103
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3104 3105 3106
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3107
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3108 3109 3110 3111 3112 3113 3114
    return 0;
}

static int
remoteDispatchInterfaceDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3115
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3116 3117 3118 3119
                                  remote_error *rerr,
                                  remote_interface_define_xml_args *args,
                                  remote_interface_define_xml_ret *ret)
{
3120
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3121

3122 3123
    iface = virInterfaceDefineXML (conn, args->xml, args->flags);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3124 3125 3126 3127
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3128 3129
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3130 3131 3132 3133 3134
    return 0;
}

static int
remoteDispatchInterfaceUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
3135 3136 3137 3138 3139 3140
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_interface_undefine_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
D
Daniel Veillard 已提交
3141
{
3142
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3143

3144 3145
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3146 3147 3148 3149
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3150 3151
    if (virInterfaceUndefine (iface) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3152 3153 3154
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3155
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3156 3157 3158 3159 3160
    return 0;
}

static int
remoteDispatchInterfaceCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
3161 3162 3163 3164 3165 3166
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
                               remote_error *rerr,
                               remote_interface_create_args *args,
                               void *ret ATTRIBUTE_UNUSED)
D
Daniel Veillard 已提交
3167
{
3168
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3169

3170 3171
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3172 3173 3174 3175
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3176 3177
    if (virInterfaceCreate (iface, args->flags) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3178 3179 3180
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3181
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3182 3183 3184 3185 3186
    return 0;
}

static int
remoteDispatchInterfaceDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
3187 3188 3189 3190 3191 3192
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                remote_error *rerr,
                                remote_interface_destroy_args *args,
                                void *ret ATTRIBUTE_UNUSED)
D
Daniel Veillard 已提交
3193
{
3194
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3195

3196 3197
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3198 3199 3200 3201
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3202 3203
    if (virInterfaceDestroy (iface, args->flags) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3204 3205 3206
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3207
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3208 3209 3210 3211 3212
    return 0;
}

/*-------------------------------------------------------------*/

3213
static int
3214
remoteDispatchAuthList (struct qemud_server *server,
3215
                        struct qemud_client *client,
3216
                        virConnectPtr conn ATTRIBUTE_UNUSED,
3217
                        remote_message_header *hdr ATTRIBUTE_UNUSED,
3218
                        remote_error *rerr,
3219 3220 3221 3222
                        void *args ATTRIBUTE_UNUSED,
                        remote_auth_list_ret *ret)
{
    ret->types.types_len = 1;
3223
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
3224 3225
        remoteDispatchOOMError(rerr);
        return -1;
3226
    }
3227 3228 3229
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3230
    ret->types.types_val[0] = client->auth;
3231
    virMutexUnlock(&client->lock);
3232

3233 3234 3235 3236 3237 3238
    return 0;
}


#if HAVE_SASL
/*
3239
 * NB, keep in sync with similar method in src/remote/remote_driver.c
3240
 */
3241
static char *addrToString(remote_error *rerr,
3242 3243
                          struct sockaddr_storage *ss, socklen_t salen) {
    char host[NI_MAXHOST], port[NI_MAXSERV];
3244 3245
    char *addr;
    int err;
3246
    struct sockaddr *sa = (struct sockaddr *)ss;
3247

3248
    if ((err = getnameinfo(sa, salen,
3249 3250 3251
                           host, sizeof(host),
                           port, sizeof(port),
                           NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268
        char ip[INET6_ADDRSTRLEN];
        void *rawaddr;

        if (sa->sa_family == AF_INET)
            rawaddr = &((struct sockaddr_in *)sa)->sin_addr;
        else
            rawaddr = &((struct sockaddr_in6 *)sa)->sin6_addr;

        if (inet_ntop(sa->sa_family, rawaddr, ip, sizeof ip)) {
            remoteDispatchFormatError(rerr,
                                      _("Cannot resolve address %s: %s"),
                                      ip, gai_strerror(err));
        } else {
            remoteDispatchFormatError(rerr,
                                      _("Cannot resolve address: %s"),
                                      gai_strerror(err));
        }
3269 3270 3271
        return NULL;
    }

3272
    if (virAsprintf(&addr, "%s;%s", host, port) == -1) {
3273
        virReportOOMError();
3274 3275 3276 3277 3278 3279 3280 3281 3282
        return NULL;
    }

    return addr;
}


/*
 * Initializes the SASL session in prepare for authentication
3283
 * and gives the client a list of allowed mechanisms to choose
3284 3285 3286 3287
 *
 * XXX callbacks for stuff like password verification ?
 */
static int
3288
remoteDispatchAuthSaslInit (struct qemud_server *server,
3289
                            struct qemud_client *client,
3290
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3291
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3292
                            remote_error *rerr,
3293 3294 3295 3296
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret)
{
    const char *mechlist = NULL;
3297
    sasl_security_properties_t secprops;
3298 3299 3300 3301 3302
    int err;
    struct sockaddr_storage sa;
    socklen_t salen;
    char *localAddr, *remoteAddr;

3303 3304 3305
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3306

3307 3308 3309
    REMOTE_DEBUG("Initialize SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn != NULL) {
3310
        VIR_ERROR0(_("client tried invalid SASL init request"));
3311
        goto authfail;
3312 3313 3314 3315 3316
    }

    /* Get local address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getsockname(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
3317
        char ebuf[1024];
3318
        remoteDispatchFormatError(rerr,
3319
                                  _("failed to get sock address: %s"),
3320
                                  virStrerror(errno, ebuf, sizeof ebuf));
3321
        goto error;
3322
    }
3323
    if ((localAddr = addrToString(rerr, &sa, salen)) == NULL) {
3324
        goto error;
3325 3326 3327 3328 3329
    }

    /* Get remote address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getpeername(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
3330
        char ebuf[1024];
3331
        remoteDispatchFormatError(rerr, _("failed to get peer address: %s"),
3332
                                  virStrerror(errno, ebuf, sizeof ebuf));
3333
        VIR_FREE(localAddr);
3334
        goto error;
3335
    }
3336
    if ((remoteAddr = addrToString(rerr, &sa, salen)) == NULL) {
3337
        VIR_FREE(localAddr);
3338
        goto error;
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348
    }

    err = sasl_server_new("libvirt",
                          NULL, /* FQDN - just delegates to gethostname */
                          NULL, /* User realm */
                          localAddr,
                          remoteAddr,
                          NULL, /* XXX Callbacks */
                          SASL_SUCCESS_DATA,
                          &client->saslconn);
3349 3350
    VIR_FREE(localAddr);
    VIR_FREE(remoteAddr);
3351
    if (err != SASL_OK) {
3352 3353
        VIR_ERROR(_("sasl context setup failed %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3354
        client->saslconn = NULL;
3355
        goto authfail;
3356 3357
    }

3358 3359 3360 3361 3362 3363 3364
    /* 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))) {
D
Daniel Veillard 已提交
3365
            VIR_ERROR0(_("cannot get TLS cipher size"));
3366 3367
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
3368
            goto authfail;
3369 3370 3371 3372 3373
        }
        ssf *= 8; /* tls key size is bytes, sasl wants bits */

        err = sasl_setprop(client->saslconn, SASL_SSF_EXTERNAL, &ssf);
        if (err != SASL_OK) {
3374 3375
            VIR_ERROR(_("cannot set SASL external SSF %d (%s)"),
                      err, sasl_errstring(err, NULL, NULL));
3376 3377
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
3378
            goto authfail;
3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401
        }
    }

    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) {
3402 3403
        VIR_ERROR(_("cannot set SASL security props %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3404 3405
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3406
        goto authfail;
3407 3408
    }

3409 3410 3411 3412 3413 3414 3415 3416 3417
    err = sasl_listmech(client->saslconn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
3418 3419
        VIR_ERROR(_("cannot list SASL mechanisms %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3420 3421
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3422
        goto authfail;
3423 3424 3425 3426
    }
    REMOTE_DEBUG("Available mechanisms for client: '%s'", mechlist);
    ret->mechlist = strdup(mechlist);
    if (!ret->mechlist) {
3427
        VIR_ERROR0(_("cannot allocate mechlist"));
3428 3429
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3430
        goto authfail;
3431 3432
    }

3433
    virMutexUnlock(&client->lock);
3434
    return 0;
3435 3436 3437 3438

authfail:
    remoteDispatchAuthError(rerr);
error:
3439
    virMutexUnlock(&client->lock);
3440
    return -1;
3441 3442 3443
}


3444
/* We asked for an SSF layer, so sanity check that we actually
3445 3446 3447
 * got what we asked for */
static int
remoteSASLCheckSSF (struct qemud_client *client,
3448
                    remote_error *rerr) {
3449 3450 3451 3452 3453 3454 3455 3456 3457
    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) {
3458 3459
        VIR_ERROR(_("cannot query SASL ssf on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3460
        remoteDispatchAuthError(rerr);
3461 3462 3463 3464 3465 3466 3467
        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 */
3468
        VIR_ERROR(_("negotiated SSF %d was not strong enough"), ssf);
3469
        remoteDispatchAuthError(rerr);
3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486
        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;
}

3487 3488 3489
static int
remoteSASLCheckAccess (struct qemud_server *server,
                       struct qemud_client *client,
3490
                       remote_error *rerr) {
3491 3492 3493 3494 3495 3496
    const void *val;
    int err;
    char **wildcards;

    err = sasl_getprop(client->saslconn, SASL_USERNAME, &val);
    if (err != SASL_OK) {
3497 3498
        VIR_ERROR(_("cannot query SASL username on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3499
        remoteDispatchAuthError(rerr);
3500 3501 3502 3503 3504
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    if (val == NULL) {
3505
        VIR_ERROR0(_("no client username was found"));
3506
        remoteDispatchAuthError(rerr);
3507 3508 3509 3510 3511 3512 3513 3514
        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) {
3515
        VIR_ERROR0(_("out of memory copying username"));
3516
        remoteDispatchAuthError(rerr);
3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
        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 */
3534
    VIR_ERROR(_("SASL client %s not allowed in whitelist"), client->saslUsername);
3535
    remoteDispatchAuthError(rerr);
3536 3537 3538 3539 3540 3541
    sasl_dispose(&client->saslconn);
    client->saslconn = NULL;
    return -1;
}


3542 3543 3544 3545
/*
 * This starts the SASL authentication negotiation.
 */
static int
3546 3547
remoteDispatchAuthSaslStart (struct qemud_server *server,
                             struct qemud_client *client,
3548
                             virConnectPtr conn ATTRIBUTE_UNUSED,
3549
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
3550
                             remote_error *rerr,
3551 3552 3553 3554 3555 3556 3557
                             remote_auth_sasl_start_args *args,
                             remote_auth_sasl_start_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

3558 3559 3560
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3561

3562 3563 3564
    REMOTE_DEBUG("Start SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
3565
        VIR_ERROR0(_("client tried invalid SASL start request"));
3566
        goto authfail;
3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579
    }

    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) {
3580 3581
        VIR_ERROR(_("sasl start failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3582 3583
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3584
        goto authfail;
3585 3586
    }
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3587
        VIR_ERROR(_("sasl start reply data too long %d"), serveroutlen);
3588 3589
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3590
        goto authfail;
3591 3592 3593 3594
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3595
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
3596
            remoteDispatchOOMError(rerr);
3597
            goto error;
3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609
        }
        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 {
3610
        if (remoteSASLCheckSSF(client, rerr) < 0)
3611
            goto error;
3612

3613
        /* Check username whitelist ACL */
3614
        if (remoteSASLCheckAccess(server, client, rerr) < 0)
3615
            goto error;
3616

3617 3618 3619 3620 3621
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

3622
    virMutexUnlock(&client->lock);
3623
    return 0;
3624 3625 3626 3627

authfail:
    remoteDispatchAuthError(rerr);
error:
3628
    virMutexUnlock(&client->lock);
3629
    return -1;
3630 3631 3632 3633
}


static int
3634 3635
remoteDispatchAuthSaslStep (struct qemud_server *server,
                            struct qemud_client *client,
3636
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3637
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3638
                            remote_error *rerr,
3639 3640 3641 3642 3643 3644 3645
                            remote_auth_sasl_step_args *args,
                            remote_auth_sasl_step_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

3646 3647 3648
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3649

3650 3651 3652
    REMOTE_DEBUG("Step SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
3653
        VIR_ERROR0(_("client tried invalid SASL start request"));
3654
        goto authfail;
3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666
    }

    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) {
3667 3668
        VIR_ERROR(_("sasl step failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3669 3670
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3671
        goto authfail;
3672 3673 3674
    }

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3675 3676
        VIR_ERROR(_("sasl step reply data too long %d"),
                  serveroutlen);
3677 3678
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3679
        goto authfail;
3680 3681 3682 3683
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3684
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
3685
            remoteDispatchOOMError(rerr);
3686
            goto error;
3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698
        }
        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 {
3699
        if (remoteSASLCheckSSF(client, rerr) < 0)
3700
            goto error;
3701

3702
        /* Check username whitelist ACL */
3703
        if (remoteSASLCheckAccess(server, client, rerr) < 0)
3704
            goto error;
3705

3706 3707 3708 3709 3710
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

3711
    virMutexUnlock(&client->lock);
3712
    return 0;
3713 3714 3715 3716

authfail:
    remoteDispatchAuthError(rerr);
error:
3717
    virMutexUnlock(&client->lock);
3718
    return -1;
3719 3720 3721 3722 3723
}


#else /* HAVE_SASL */
static int
3724
remoteDispatchAuthSaslInit (struct qemud_server *server ATTRIBUTE_UNUSED,
3725 3726
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3727
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3728
                            remote_error *rerr,
3729 3730 3731
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
3732
    VIR_ERROR0(_("client tried unsupported SASL init request"));
3733
    remoteDispatchAuthError(rerr);
3734 3735 3736 3737
    return -1;
}

static int
3738
remoteDispatchAuthSaslStart (struct qemud_server *server ATTRIBUTE_UNUSED,
3739 3740
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn ATTRIBUTE_UNUSED,
3741
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
3742
                             remote_error *rerr,
3743 3744 3745
                             remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                             remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
3746
    VIR_ERROR0(_("client tried unsupported SASL start request"));
3747
    remoteDispatchAuthError(rerr);
3748 3749 3750 3751
    return -1;
}

static int
3752
remoteDispatchAuthSaslStep (struct qemud_server *server ATTRIBUTE_UNUSED,
3753 3754
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3755
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3756
                            remote_error *rerr,
3757 3758 3759
                            remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
3760
    VIR_ERROR0(_("client tried unsupported SASL step request"));
3761
    remoteDispatchAuthError(rerr);
3762 3763 3764 3765 3766
    return -1;
}
#endif /* HAVE_SASL */


3767 3768 3769 3770 3771
#if HAVE_POLKIT1
static int
remoteDispatchAuthPolkit (struct qemud_server *server,
                          struct qemud_client *client,
                          virConnectPtr conn ATTRIBUTE_UNUSED,
3772
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815
                          remote_error *rerr,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
    pid_t callerPid;
    uid_t callerUid;
    const char *action;
    int status = -1;
    char pidbuf[50];
    int rv;

    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);

    action = client->readonly ?
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

    const char * const pkcheck [] = {
      PKCHECK_PATH,
      "--action-id", action,
      "--process", pidbuf,
      "--allow-user-interaction",
      NULL
    };

    REMOTE_DEBUG("Start PolicyKit auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_POLKIT) {
        VIR_ERROR0(_("client tried invalid PolicyKit init request"));
        goto authfail;
    }

    if (qemudGetSocketIdentity(client->fd, &callerUid, &callerPid) < 0) {
        VIR_ERROR0(_("cannot get peer socket identity"));
        goto authfail;
    }

    VIR_INFO(_("Checking PID %d running as %d"), callerPid, callerUid);

    rv = snprintf(pidbuf, sizeof pidbuf, "%d", callerPid);
    if (rv < 0 || rv >= sizeof pidbuf) {
        VIR_ERROR(_("Caller PID was too large %d"), callerPid);
3816
        goto authfail;
3817 3818
    }

3819
    if (virRun(pkcheck, &status) < 0) {
3820
        VIR_ERROR(_("Cannot invoke %s"), PKCHECK_PATH);
3821
        goto authfail;
3822 3823
    }
    if (status != 0) {
3824
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d, result: %d"),
3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841
                  action, callerPid, callerUid, status);
        goto authfail;
    }
    VIR_INFO(_("Policy allowed action %s from pid %d, uid %d"),
             action, callerPid, callerUid);
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;

    virMutexUnlock(&client->lock);
    return 0;

authfail:
    remoteDispatchAuthError(rerr);
    virMutexUnlock(&client->lock);
    return -1;
}
#elif HAVE_POLKIT0
3842
static int
3843
remoteDispatchAuthPolkit (struct qemud_server *server,
3844
                          struct qemud_client *client,
3845
                          virConnectPtr conn ATTRIBUTE_UNUSED,
3846
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
3847
                          remote_error *rerr,
3848 3849 3850 3851 3852
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
    pid_t callerPid;
    uid_t callerUid;
3853 3854 3855 3856 3857 3858
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
3859 3860
    const char *action;

3861 3862 3863
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3864 3865

    action = client->readonly ?
3866 3867
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
3868 3869 3870

    REMOTE_DEBUG("Start PolicyKit auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_POLKIT) {
3871
        VIR_ERROR0(_("client tried invalid PolicyKit init request"));
3872
        goto authfail;
3873 3874 3875
    }

    if (qemudGetSocketIdentity(client->fd, &callerUid, &callerPid) < 0) {
3876
        VIR_ERROR0(_("cannot get peer socket identity"));
3877
        goto authfail;
3878 3879
    }

3880
    VIR_INFO(_("Checking PID %d running as %d"), callerPid, callerUid);
3881 3882 3883
    dbus_error_init(&err);
    if (!(pkcaller = polkit_caller_new_from_pid(server->sysbus,
                                                callerPid, &err))) {
3884
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
3885
        dbus_error_free(&err);
3886
        goto authfail;
3887
    }
3888

3889
    if (!(pkaction = polkit_action_new())) {
3890
        char ebuf[1024];
3891
        VIR_ERROR(_("Failed to create polkit action %s"),
3892
                  virStrerror(errno, ebuf, sizeof ebuf));
3893
        polkit_caller_unref(pkcaller);
3894
        goto authfail;
3895 3896 3897 3898 3899
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
3900
        char ebuf[1024];
3901
        VIR_ERROR(_("Failed to create polkit context %s"),
3902
                  (pkerr ? polkit_error_get_error_message(pkerr)
3903
                   : virStrerror(errno, ebuf, sizeof ebuf)));
3904 3905 3906 3907 3908
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
3909
        goto authfail;
3910
    }
3911

3912
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
3913 3914 3915 3916 3917 3918
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
3919 3920 3921
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
3922
        goto authfail;
3923
    }
3924
# else
3925 3926 3927
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
3928
# endif
3929 3930 3931 3932
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
3933
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d, result: %s"),
3934 3935
                  action, callerPid, callerUid,
                  polkit_result_to_string_representation(pkresult));
3936
        goto authfail;
3937
    }
3938
    VIR_INFO(_("Policy allowed action %s from pid %d, uid %d, result %s"),
3939 3940 3941 3942
             action, callerPid, callerUid,
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;
3943

3944
    virMutexUnlock(&client->lock);
3945
    return 0;
3946 3947 3948

authfail:
    remoteDispatchAuthError(rerr);
3949
    virMutexUnlock(&client->lock);
3950
    return -1;
3951 3952
}

3953
#else /* !HAVE_POLKIT0 & !HAVE_POLKIT1*/
3954 3955

static int
3956
remoteDispatchAuthPolkit (struct qemud_server *server ATTRIBUTE_UNUSED,
3957
                          struct qemud_client *client ATTRIBUTE_UNUSED,
3958
                          virConnectPtr conn ATTRIBUTE_UNUSED,
3959
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
3960 3961 3962
                          remote_error *rerr,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
3963
{
3964
    VIR_ERROR0(_("client tried unsupported PolicyKit init request"));
3965
    remoteDispatchAuthError(rerr);
3966 3967
    return -1;
}
3968
#endif /* HAVE_POLKIT1 */
3969

3970 3971 3972 3973 3974 3975 3976 3977

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


static int
remoteDispatchListDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
3978 3979
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3980
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
3981
                                       remote_error *rerr,
3982 3983 3984 3985 3986
                                       remote_list_defined_storage_pools_args *args,
                                       remote_list_defined_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
3987 3988
        remoteDispatchFormatError (rerr, "%s",
                            _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
3989
        return -1;
3990 3991 3992
    }

    /* Allocate return buffer. */
3993
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
3994 3995
        remoteDispatchOOMError(rerr);
        return -1;
3996
    }
3997 3998

    ret->names.names_len =
3999
        virConnectListDefinedStoragePools (conn,
4000 4001 4002
                                           ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
4003
        remoteDispatchConnError(rerr, conn);
4004 4005
        return -1;
    }
4006 4007 4008 4009 4010 4011

    return 0;
}

static int
remoteDispatchListStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4012 4013
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4014
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4015
                                remote_error *rerr,
4016 4017 4018 4019 4020
                                remote_list_storage_pools_args *args,
                                remote_list_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
4021 4022 4023
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX"));
        return -1;
4024 4025 4026
    }

    /* Allocate return buffer. */
4027
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
4028 4029
        remoteDispatchOOMError(rerr);
        return -1;
4030
    }
4031 4032

    ret->names.names_len =
4033
        virConnectListStoragePools (conn,
4034
                                ret->names.names_val, args->maxnames);
4035 4036
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
4037
        remoteDispatchConnError(rerr, conn);
4038 4039
        return -1;
    }
4040 4041 4042 4043

    return 0;
}

4044 4045
static int
remoteDispatchFindStoragePoolSources (struct qemud_server *server ATTRIBUTE_UNUSED,
4046 4047
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4048
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4049
                                      remote_error *rerr,
4050 4051 4052 4053
                                      remote_find_storage_pool_sources_args *args,
                                      remote_find_storage_pool_sources_ret *ret)
{
    ret->xml =
4054
        virConnectFindStoragePoolSources (conn,
4055 4056 4057
                                          args->type,
                                          args->srcSpec ? *args->srcSpec : NULL,
                                          args->flags);
4058
    if (ret->xml == NULL) {
4059
        remoteDispatchConnError(rerr, conn);
4060
        return -1;
4061
    }
4062 4063 4064 4065 4066

    return 0;
}


4067 4068
static int
remoteDispatchStoragePoolCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
4069 4070
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4071
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4072
                                 remote_error *rerr,
4073 4074 4075 4076 4077
                                 remote_storage_pool_create_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4078
    pool = get_nonnull_storage_pool (conn, args->pool);
4079
    if (pool == NULL) {
4080
        remoteDispatchConnError(rerr, conn);
4081
        return -1;
4082 4083 4084 4085
    }

    if (virStoragePoolCreate (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4086
        remoteDispatchConnError(rerr, conn);
4087 4088 4089 4090 4091 4092 4093 4094
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4095 4096
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
4097
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
4098
                                    remote_error *rerr,
4099 4100 4101 4102 4103
                                    remote_storage_pool_create_xml_args *args,
                                    remote_storage_pool_create_xml_ret *ret)
{
    virStoragePoolPtr pool;

4104
    pool = virStoragePoolCreateXML (conn, args->xml, args->flags);
4105
    if (pool == NULL) {
4106
        remoteDispatchConnError(rerr, conn);
4107 4108
        return -1;
    }
4109 4110 4111 4112 4113 4114 4115 4116

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

static int
remoteDispatchStoragePoolDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4117 4118
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
4119
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
4120
                                    remote_error *rerr,
4121 4122 4123 4124 4125
                                    remote_storage_pool_define_xml_args *args,
                                    remote_storage_pool_define_xml_ret *ret)
{
    virStoragePoolPtr pool;

4126
    pool = virStoragePoolDefineXML (conn, args->xml, args->flags);
4127
    if (pool == NULL) {
4128
        remoteDispatchConnError(rerr, conn);
4129 4130
        return -1;
    }
4131 4132 4133 4134 4135 4136 4137 4138

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

static int
remoteDispatchStoragePoolBuild (struct qemud_server *server ATTRIBUTE_UNUSED,
4139 4140
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4141
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4142 4143 4144
                                remote_error *rerr,
                                remote_storage_pool_build_args *args,
                                void *ret ATTRIBUTE_UNUSED)
4145 4146 4147
{
    virStoragePoolPtr pool;

4148
    pool = get_nonnull_storage_pool (conn, args->pool);
4149
    if (pool == NULL) {
4150
        remoteDispatchConnError(rerr, conn);
4151
        return -1;
4152 4153 4154 4155
    }

    if (virStoragePoolBuild (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4156
        remoteDispatchConnError(rerr, conn);
4157 4158 4159 4160 4161 4162 4163 4164 4165
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
4166 4167
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4168
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4169
                                  remote_error *rerr,
4170 4171 4172 4173 4174
                                  remote_storage_pool_destroy_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4175
    pool = get_nonnull_storage_pool (conn, args->pool);
4176
    if (pool == NULL) {
4177
        remoteDispatchConnError(rerr, conn);
4178
        return -1;
4179 4180 4181 4182
    }

    if (virStoragePoolDestroy (pool) == -1) {
        virStoragePoolFree(pool);
4183
        remoteDispatchConnError(rerr, conn);
4184 4185 4186 4187 4188 4189 4190 4191
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
4192 4193
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4194
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4195
                                 remote_error *rerr,
4196 4197 4198 4199 4200
                                 remote_storage_pool_delete_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4201
    pool = get_nonnull_storage_pool (conn, args->pool);
4202
    if (pool == NULL) {
4203
        remoteDispatchConnError(rerr, conn);
4204
        return -1;
4205 4206 4207 4208
    }

    if (virStoragePoolDelete (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4209
        remoteDispatchConnError(rerr, conn);
4210 4211 4212 4213 4214 4215 4216 4217
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolRefresh (struct qemud_server *server ATTRIBUTE_UNUSED,
4218 4219
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4220
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4221
                                  remote_error *rerr,
4222 4223 4224 4225 4226
                                  remote_storage_pool_refresh_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4227
    pool = get_nonnull_storage_pool (conn, args->pool);
4228
    if (pool == NULL) {
4229
        remoteDispatchConnError(rerr, conn);
4230
        return -1;
4231 4232 4233 4234
    }

    if (virStoragePoolRefresh (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4235
        remoteDispatchConnError(rerr, conn);
4236 4237 4238 4239 4240 4241 4242 4243
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
4244 4245
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4246
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4247
                                  remote_error *rerr,
4248 4249 4250 4251 4252 4253
                                  remote_storage_pool_get_info_args *args,
                                  remote_storage_pool_get_info_ret *ret)
{
    virStoragePoolPtr pool;
    virStoragePoolInfo info;

4254
    pool = get_nonnull_storage_pool (conn, args->pool);
4255
    if (pool == NULL) {
4256
        remoteDispatchConnError(rerr, conn);
4257
        return -1;
4258 4259 4260 4261
    }

    if (virStoragePoolGetInfo (pool, &info) == -1) {
        virStoragePoolFree(pool);
4262
        remoteDispatchConnError(rerr, conn);
4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277
        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,
4278 4279
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4280
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4281
                                  remote_error *rerr,
4282 4283 4284 4285 4286
                                  remote_storage_pool_dump_xml_args *args,
                                  remote_storage_pool_dump_xml_ret *ret)
{
    virStoragePoolPtr pool;

4287
    pool = get_nonnull_storage_pool (conn, args->pool);
4288
    if (pool == NULL) {
4289
        remoteDispatchConnError(rerr, conn);
4290
        return -1;
4291 4292 4293 4294 4295 4296
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStoragePoolGetXMLDesc (pool, args->flags);
    if (!ret->xml) {
        virStoragePoolFree(pool);
4297
        remoteDispatchConnError(rerr, conn);
4298 4299 4300 4301 4302 4303 4304 4305
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
4306 4307
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4308
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4309
                                       remote_error *rerr,
4310 4311 4312 4313 4314
                                       remote_storage_pool_get_autostart_args *args,
                                       remote_storage_pool_get_autostart_ret *ret)
{
    virStoragePoolPtr pool;

4315
    pool = get_nonnull_storage_pool (conn, args->pool);
4316
    if (pool == NULL) {
4317
        remoteDispatchConnError(rerr, conn);
4318
        return -1;
4319 4320 4321 4322
    }

    if (virStoragePoolGetAutostart (pool, &ret->autostart) == -1) {
        virStoragePoolFree(pool);
4323
        remoteDispatchConnError(rerr, conn);
4324 4325 4326 4327 4328 4329 4330 4331 4332
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
4333 4334
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4335
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4336
                                       remote_error *rerr,
4337 4338 4339 4340 4341
                                       remote_storage_pool_lookup_by_name_args *args,
                                       remote_storage_pool_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;

4342
    pool = virStoragePoolLookupByName (conn, args->name);
4343
    if (pool == NULL) {
4344
        remoteDispatchConnError(rerr, conn);
4345 4346
        return -1;
    }
4347 4348 4349 4350 4351 4352 4353 4354

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

static int
remoteDispatchStoragePoolLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
4355 4356
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4357
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4358
                                       remote_error *rerr,
4359 4360 4361 4362 4363
                                       remote_storage_pool_lookup_by_uuid_args *args,
                                       remote_storage_pool_lookup_by_uuid_ret *ret)
{
    virStoragePoolPtr pool;

4364
    pool = virStoragePoolLookupByUUID (conn, (unsigned char *) args->uuid);
4365
    if (pool == NULL) {
4366
        remoteDispatchConnError(rerr, conn);
4367 4368
        return -1;
    }
4369 4370 4371 4372 4373 4374 4375 4376

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

static int
remoteDispatchStoragePoolLookupByVolume (struct qemud_server *server ATTRIBUTE_UNUSED,
4377 4378
                                         struct qemud_client *client ATTRIBUTE_UNUSED,
                                         virConnectPtr conn,
4379
                                         remote_message_header *hdr ATTRIBUTE_UNUSED,
4380
                                         remote_error *rerr,
4381 4382 4383 4384 4385 4386
                                         remote_storage_pool_lookup_by_volume_args *args,
                                         remote_storage_pool_lookup_by_volume_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

4387
    vol = get_nonnull_storage_vol (conn, args->vol);
4388
    if (vol == NULL) {
4389
        remoteDispatchConnError(rerr, conn);
4390 4391
        return -1;
    }
4392 4393 4394

    pool = virStoragePoolLookupByVolume (vol);
    virStorageVolFree(vol);
4395
    if (pool == NULL) {
4396
        remoteDispatchConnError(rerr, conn);
4397 4398
        return -1;
    }
4399 4400 4401 4402 4403 4404 4405 4406

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

static int
remoteDispatchStoragePoolSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
4407 4408
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4409
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4410
                                       remote_error *rerr,
4411 4412 4413 4414 4415
                                       remote_storage_pool_set_autostart_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4416
    pool = get_nonnull_storage_pool (conn, args->pool);
4417
    if (pool == NULL) {
4418
        remoteDispatchConnError(rerr, conn);
4419
        return -1;
4420 4421 4422 4423
    }

    if (virStoragePoolSetAutostart (pool, args->autostart) == -1) {
        virStoragePoolFree(pool);
4424
        remoteDispatchConnError(rerr, conn);
4425 4426 4427 4428 4429 4430 4431 4432
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
4433 4434
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4435
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4436
                                   remote_error *rerr,
4437 4438 4439 4440 4441
                                   remote_storage_pool_undefine_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4442
    pool = get_nonnull_storage_pool (conn, args->pool);
4443
    if (pool == NULL) {
4444
        remoteDispatchConnError(rerr, conn);
4445
        return -1;
4446 4447 4448 4449
    }

    if (virStoragePoolUndefine (pool) == -1) {
        virStoragePoolFree(pool);
4450
        remoteDispatchConnError(rerr, conn);
4451 4452 4453 4454 4455 4456 4457 4458
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchNumOfStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4459 4460
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4461
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4462
                                 remote_error *rerr,
4463 4464 4465 4466
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_num_of_storage_pools_ret *ret)
{

4467
    ret->num = virConnectNumOfStoragePools (conn);
4468
    if (ret->num == -1) {
4469
        remoteDispatchConnError(rerr, conn);
4470 4471
        return -1;
    }
4472 4473 4474 4475 4476 4477

    return 0;
}

static int
remoteDispatchNumOfDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4478 4479
                                        struct qemud_client *client ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
4480
                                        remote_message_header *hdr ATTRIBUTE_UNUSED,
4481
                                        remote_error *rerr,
4482 4483 4484 4485
                                        void *args ATTRIBUTE_UNUSED,
                                        remote_num_of_defined_storage_pools_ret *ret)
{

4486
    ret->num = virConnectNumOfDefinedStoragePools (conn);
4487
    if (ret->num == -1) {
4488
        remoteDispatchConnError(rerr, conn);
4489 4490
        return -1;
    }
4491 4492 4493 4494 4495 4496

    return 0;
}

static int
remoteDispatchStoragePoolListVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
4497 4498
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4499
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4500
                                      remote_error *rerr,
4501 4502 4503 4504 4505 4506
                                      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) {
4507 4508 4509
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX"));
        return -1;
4510 4511
    }

4512
    pool = get_nonnull_storage_pool (conn, args->pool);
4513
    if (pool == NULL) {
4514
        remoteDispatchConnError(rerr, conn);
4515
        return -1;
4516 4517 4518
    }

    /* Allocate return buffer. */
4519 4520
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        virStoragePoolFree(pool);
4521 4522
        remoteDispatchOOMError(rerr);
        return -1;
4523
    }
4524 4525 4526 4527 4528

    ret->names.names_len =
        virStoragePoolListVolumes (pool,
                                   ret->names.names_val, args->maxnames);
    virStoragePoolFree(pool);
4529 4530
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
4531
        remoteDispatchConnError(rerr, conn);
4532 4533
        return -1;
    }
4534 4535 4536 4537 4538 4539 4540

    return 0;
}


static int
remoteDispatchStoragePoolNumOfVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
4541 4542
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4543
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4544
                                       remote_error *rerr,
4545 4546 4547 4548 4549
                                       remote_storage_pool_num_of_volumes_args *args,
                                       remote_storage_pool_num_of_volumes_ret *ret)
{
    virStoragePoolPtr pool;

4550
    pool = get_nonnull_storage_pool (conn, args->pool);
4551
    if (pool == NULL) {
4552
        remoteDispatchConnError(rerr, conn);
4553
        return -1;
4554 4555 4556 4557
    }

    ret->num = virStoragePoolNumOfVolumes (pool);
    virStoragePoolFree(pool);
4558
    if (ret->num == -1) {
4559
        remoteDispatchConnError(rerr, conn);
4560 4561
        return -1;
    }
4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574

    return 0;
}


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



static int
remoteDispatchStorageVolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4575 4576
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4577
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4578
                                   remote_error *rerr,
4579 4580 4581 4582 4583 4584
                                   remote_storage_vol_create_xml_args *args,
                                   remote_storage_vol_create_xml_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

4585
    pool = get_nonnull_storage_pool (conn, args->pool);
4586
    if (pool == NULL) {
4587
        remoteDispatchConnError(rerr, conn);
4588
        return -1;
4589 4590 4591 4592
    }

    vol = virStorageVolCreateXML (pool, args->xml, args->flags);
    virStoragePoolFree(pool);
4593
    if (vol == NULL) {
4594
        remoteDispatchConnError(rerr, conn);
4595 4596
        return -1;
    }
4597 4598 4599 4600 4601 4602

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

4603 4604 4605 4606
static int
remoteDispatchStorageVolCreateXmlFrom (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4607
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622
                                       remote_error *rerr,
                                       remote_storage_vol_create_xml_from_args *args,
                                       remote_storage_vol_create_xml_from_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr clonevol, newvol;

    pool = get_nonnull_storage_pool (conn, args->pool);
    if (pool == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    clonevol = get_nonnull_storage_vol (conn, args->clonevol);
    if (clonevol == NULL) {
4623
        virStoragePoolFree(pool);
4624 4625 4626 4627 4628 4629
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    newvol = virStorageVolCreateXMLFrom (pool, args->xml, clonevol,
                                         args->flags);
4630 4631
    virStorageVolFree(clonevol);
    virStoragePoolFree(pool);
4632 4633 4634 4635 4636 4637 4638 4639 4640
    if (newvol == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_storage_vol (&ret->vol, newvol);
    virStorageVolFree(newvol);
    return 0;
}
4641 4642 4643

static int
remoteDispatchStorageVolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
4644 4645
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4646
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4647
                                remote_error *rerr,
4648 4649 4650 4651 4652
                                remote_storage_vol_delete_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virStorageVolPtr vol;

4653
    vol = get_nonnull_storage_vol (conn, args->vol);
4654
    if (vol == NULL) {
4655
        remoteDispatchConnError(rerr, conn);
4656
        return -1;
4657 4658 4659 4660
    }

    if (virStorageVolDelete (vol, args->flags) == -1) {
        virStorageVolFree(vol);
4661
        remoteDispatchConnError(rerr, conn);
4662 4663 4664 4665 4666 4667
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}

4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699
static int
remoteDispatchStorageVolWipe(struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                             remote_error *rerr,
                             remote_storage_vol_wipe_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    int retval = -1;
    virStorageVolPtr vol;

    vol = get_nonnull_storage_vol(conn, args->vol);
    if (vol == NULL) {
        remoteDispatchConnError(rerr, conn);
        goto out;
    }

    if (virStorageVolWipe(vol, args->flags) == -1) {
        remoteDispatchConnError(rerr, conn);
        goto out;
    }

    retval = 0;

out:
    if (vol != NULL) {
        virStorageVolFree(vol);
    }
    return retval;
}

4700 4701
static int
remoteDispatchStorageVolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
4702 4703
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4704
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4705
                                 remote_error *rerr,
4706 4707 4708 4709 4710 4711
                                 remote_storage_vol_get_info_args *args,
                                 remote_storage_vol_get_info_ret *ret)
{
    virStorageVolPtr vol;
    virStorageVolInfo info;

4712
    vol = get_nonnull_storage_vol (conn, args->vol);
4713
    if (vol == NULL) {
4714
        remoteDispatchConnError(rerr, conn);
4715
        return -1;
4716 4717 4718 4719
    }

    if (virStorageVolGetInfo (vol, &info) == -1) {
        virStorageVolFree(vol);
4720
        remoteDispatchConnError(rerr, conn);
4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734
        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,
4735 4736
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4737
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4738
                                 remote_error *rerr,
4739 4740 4741 4742 4743
                                 remote_storage_vol_dump_xml_args *args,
                                 remote_storage_vol_dump_xml_ret *ret)
{
    virStorageVolPtr vol;

4744
    vol = get_nonnull_storage_vol (conn, args->vol);
4745
    if (vol == NULL) {
4746
        remoteDispatchConnError(rerr, conn);
4747
        return -1;
4748 4749 4750 4751 4752 4753
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStorageVolGetXMLDesc (vol, args->flags);
    if (!ret->xml) {
        virStorageVolFree(vol);
4754
        remoteDispatchConnError(rerr, conn);
4755 4756 4757 4758 4759 4760 4761 4762 4763
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolGetPath (struct qemud_server *server ATTRIBUTE_UNUSED,
4764 4765
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4766
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4767
                                 remote_error *rerr,
4768 4769 4770 4771 4772
                                 remote_storage_vol_get_path_args *args,
                                 remote_storage_vol_get_path_ret *ret)
{
    virStorageVolPtr vol;

4773
    vol = get_nonnull_storage_vol (conn, args->vol);
4774
    if (vol == NULL) {
4775
        remoteDispatchConnError(rerr, conn);
4776
        return -1;
4777 4778 4779 4780 4781 4782
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virStorageVolGetPath (vol);
    if (!ret->name) {
        virStorageVolFree(vol);
4783
        remoteDispatchConnError(rerr, conn);
4784 4785 4786 4787 4788 4789 4790 4791 4792
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
4793 4794
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4795
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4796
                                      remote_error *rerr,
4797 4798 4799 4800 4801 4802
                                      remote_storage_vol_lookup_by_name_args *args,
                                      remote_storage_vol_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

4803
    pool = get_nonnull_storage_pool (conn, args->pool);
4804
    if (pool == NULL) {
4805
        remoteDispatchConnError(rerr, conn);
4806
        return -1;
4807 4808 4809 4810
    }

    vol = virStorageVolLookupByName (pool, args->name);
    virStoragePoolFree(pool);
4811
    if (vol == NULL) {
4812
        remoteDispatchConnError(rerr, conn);
4813 4814
        return -1;
    }
4815 4816 4817 4818 4819 4820 4821 4822

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

static int
remoteDispatchStorageVolLookupByKey (struct qemud_server *server ATTRIBUTE_UNUSED,
4823 4824
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
4825
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
4826
                                     remote_error *rerr,
4827 4828 4829 4830 4831
                                     remote_storage_vol_lookup_by_key_args *args,
                                     remote_storage_vol_lookup_by_key_ret *ret)
{
    virStorageVolPtr vol;

4832
    vol = virStorageVolLookupByKey (conn, args->key);
4833
    if (vol == NULL) {
4834
        remoteDispatchConnError(rerr, conn);
4835 4836
        return -1;
    }
4837 4838 4839 4840 4841 4842 4843 4844 4845

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


static int
remoteDispatchStorageVolLookupByPath (struct qemud_server *server ATTRIBUTE_UNUSED,
4846 4847
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4848
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4849
                                      remote_error *rerr,
4850 4851 4852 4853 4854
                                      remote_storage_vol_lookup_by_path_args *args,
                                      remote_storage_vol_lookup_by_path_ret *ret)
{
    virStorageVolPtr vol;

4855
    vol = virStorageVolLookupByPath (conn, args->path);
4856
    if (vol == NULL) {
4857
        remoteDispatchConnError(rerr, conn);
4858 4859
        return -1;
    }
4860 4861 4862 4863 4864 4865 4866

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


4867 4868 4869 4870 4871 4872
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/

static int
remoteDispatchNodeNumOfDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
4873 4874
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4875
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4876
                                remote_error *rerr,
4877 4878 4879 4880 4881
                                remote_node_num_of_devices_args *args,
                                remote_node_num_of_devices_ret *ret)
{
    CHECK_CONN(client);

4882
    ret->num = virNodeNumOfDevices (conn,
4883 4884
                                    args->cap ? *args->cap : NULL,
                                    args->flags);
4885
    if (ret->num == -1) {
4886
        remoteDispatchConnError(rerr, conn);
4887 4888
        return -1;
    }
4889 4890 4891 4892 4893 4894 4895

    return 0;
}


static int
remoteDispatchNodeListDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
4896 4897
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
4898
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
4899
                               remote_error *rerr,
4900 4901 4902 4903 4904 4905
                               remote_node_list_devices_args *args,
                               remote_node_list_devices_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
4906 4907 4908
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
4909 4910 4911 4912
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
4913 4914
        remoteDispatchOOMError(rerr);
        return -1;
4915 4916 4917
    }

    ret->names.names_len =
4918
        virNodeListDevices (conn,
4919 4920 4921
                            args->cap ? *args->cap : NULL,
                            ret->names.names_val, args->maxnames, args->flags);
    if (ret->names.names_len == -1) {
4922
        remoteDispatchConnError(rerr, conn);
4923 4924 4925 4926 4927 4928 4929 4930 4931 4932
        VIR_FREE(ret->names.names_val);
        return -1;
    }

    return 0;
}


static int
remoteDispatchNodeDeviceLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
4933 4934
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4935
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4936
                                      remote_error *rerr,
4937 4938 4939 4940 4941 4942 4943
                                      remote_node_device_lookup_by_name_args *args,
                                      remote_node_device_lookup_by_name_ret *ret)
{
    virNodeDevicePtr dev;

    CHECK_CONN(client);

4944
    dev = virNodeDeviceLookupByName (conn, args->name);
4945
    if (dev == NULL) {
4946
        remoteDispatchConnError(rerr, conn);
4947 4948
        return -1;
    }
4949 4950 4951 4952 4953 4954 4955 4956 4957

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


static int
remoteDispatchNodeDeviceDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4958 4959
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4960
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4961
                                 remote_error *rerr,
4962 4963 4964 4965 4966 4967
                                 remote_node_device_dump_xml_args *args,
                                 remote_node_device_dump_xml_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

4968
    dev = virNodeDeviceLookupByName(conn, args->name);
4969
    if (dev == NULL) {
4970
        remoteDispatchConnError(rerr, conn);
4971
        return -1;
4972 4973 4974 4975 4976
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNodeDeviceGetXMLDesc (dev, args->flags);
    if (!ret->xml) {
4977
        remoteDispatchConnError(rerr, conn);
4978 4979 4980 4981 4982 4983 4984 4985 4986 4987
        virNodeDeviceFree(dev);
        return -1;
    }
    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceGetParent (struct qemud_server *server ATTRIBUTE_UNUSED,
4988 4989
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4990
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4991
                                   remote_error *rerr,
4992 4993 4994 4995 4996 4997 4998
                                   remote_node_device_get_parent_args *args,
                                   remote_node_device_get_parent_ret *ret)
{
    virNodeDevicePtr dev;
    const char *parent;
    CHECK_CONN(client);

4999
    dev = virNodeDeviceLookupByName(conn, args->name);
5000
    if (dev == NULL) {
5001
        remoteDispatchConnError(rerr, conn);
5002
        return -1;
5003 5004 5005 5006 5007 5008 5009 5010 5011 5012
    }

    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
        if (VIR_ALLOC(parent_p) < 0) {
5013 5014
            remoteDispatchOOMError(rerr);
            return -1;
5015 5016 5017
        }
        *parent_p = strdup(parent);
        if (*parent_p == NULL) {
5018 5019
            remoteDispatchOOMError(rerr);
            return -1;
5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030
        }
        ret->parent = parent_p;
    }

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceNumOfCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
5031 5032
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
5033
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
5034
                                   remote_error *rerr,
5035 5036 5037 5038 5039 5040
                                   remote_node_device_num_of_caps_args *args,
                                   remote_node_device_num_of_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

5041
    dev = virNodeDeviceLookupByName(conn, args->name);
5042
    if (dev == NULL) {
5043
        remoteDispatchConnError(rerr, conn);
5044
        return -1;
5045 5046 5047
    }

    ret->num = virNodeDeviceNumOfCaps(dev);
5048
    if (ret->num < 0) {
5049
        remoteDispatchConnError(rerr, conn);
5050 5051
        return -1;
    }
5052 5053 5054 5055 5056 5057 5058 5059

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceListCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
5060 5061
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
5062
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
5063
                                  remote_error *rerr,
5064 5065 5066 5067 5068 5069
                                  remote_node_device_list_caps_args *args,
                                  remote_node_device_list_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

5070
    dev = virNodeDeviceLookupByName(conn, args->name);
5071
    if (dev == NULL) {
5072
        remoteDispatchConnError(rerr, conn);
5073
        return -1;
5074 5075 5076
    }

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
5077 5078 5079
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
5080 5081 5082 5083
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
5084 5085
        remoteDispatchOOMError(rerr);
        return -1;
5086 5087 5088 5089 5090 5091
    }

    ret->names.names_len =
        virNodeDeviceListCaps (dev, ret->names.names_val,
                               args->maxnames);
    if (ret->names.names_len == -1) {
5092
        remoteDispatchConnError(rerr, conn);
5093 5094 5095 5096 5097 5098 5099 5100
        VIR_FREE(ret->names.names_val);
        return -1;
    }

    return 0;
}


5101 5102 5103 5104
static int
remoteDispatchNodeDeviceDettach (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5105
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5106 5107 5108 5109 5110 5111 5112 5113 5114
                                 remote_error *rerr,
                                 remote_node_device_dettach_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

    dev = virNodeDeviceLookupByName(conn, args->name);
    if (dev == NULL) {
5115
        remoteDispatchConnError(rerr, conn);
5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131
        return -1;
    }

    if (virNodeDeviceDettach(dev) == -1) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    return 0;
}


static int
remoteDispatchNodeDeviceReAttach (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
5132
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
5133 5134 5135 5136 5137 5138 5139 5140 5141
                                  remote_error *rerr,
                                  remote_node_device_re_attach_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

    dev = virNodeDeviceLookupByName(conn, args->name);
    if (dev == NULL) {
5142
        remoteDispatchConnError(rerr, conn);
5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158
        return -1;
    }

    if (virNodeDeviceReAttach(dev) == -1) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    return 0;
}


static int
remoteDispatchNodeDeviceReset (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
5159
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
5160 5161 5162 5163 5164 5165 5166 5167 5168
                               remote_error *rerr,
                               remote_node_device_reset_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

    dev = virNodeDeviceLookupByName(conn, args->name);
    if (dev == NULL) {
5169
        remoteDispatchConnError(rerr, conn);
5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181
        return -1;
    }

    if (virNodeDeviceReset(dev) == -1) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    return 0;
}


5182 5183 5184 5185
static int
remoteDispatchNodeDeviceCreateXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
5186
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209
                                  remote_error *rerr,
                                  remote_node_device_create_xml_args *args,
                                  remote_node_device_create_xml_ret *ret)
{
    virNodeDevicePtr dev;

    dev = virNodeDeviceCreateXML (conn, args->xml_desc, args->flags);
    if (dev == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_node_device (&ret->dev, dev);
    virNodeDeviceFree(dev);

    return 0;
}


static int
remoteDispatchNodeDeviceDestroy(struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
5210
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
5211 5212 5213 5214 5215 5216 5217 5218
                                remote_error *rerr,
                                remote_node_device_destroy_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virNodeDevicePtr dev;

    dev = virNodeDeviceLookupByName(conn, args->name);
    if (dev == NULL) {
5219
        remoteDispatchConnError(rerr, conn);
5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231
        return -1;
    }

    if (virNodeDeviceDestroy(dev) == -1) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    return 0;
}


5232 5233 5234 5235 5236 5237

/***************************
 * Register / deregister events
 ***************************/
static int
remoteDispatchDomainEventsRegister (struct qemud_server *server ATTRIBUTE_UNUSED,
5238 5239
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
5240
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
5241
                                    remote_error *rerr ATTRIBUTE_UNUSED,
5242 5243 5244 5245
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);
5246 5247 5248 5249 5250 5251
    int callbackID;

    if (client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] != -1) {
        remoteDispatchFormatError(rerr, _("domain event %d already registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
        return -1;
    }
5252

5253 5254 5255 5256 5257
    if ((callbackID = virConnectDomainEventRegisterAny(conn,
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
                                                       client, NULL)) < 0) {
5258 5259 5260
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
5261

5262
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;
5263

5264 5265 5266 5267 5268
    return 0;
}

static int
remoteDispatchDomainEventsDeregister (struct qemud_server *server ATTRIBUTE_UNUSED,
5269 5270
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
5271
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
5272
                                      remote_error *rerr ATTRIBUTE_UNUSED,
5273 5274 5275 5276 5277
                                      void *args ATTRIBUTE_UNUSED,
                                      remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

5278 5279
    if (client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] == -1) {
        remoteDispatchFormatError(rerr, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
5280 5281
        return -1;
    }
5282

5283 5284 5285 5286 5287
    if (virConnectDomainEventDeregisterAny(conn,
                                           client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
5288

5289
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;
5290 5291 5292 5293 5294
    return 0;
}

static void
remoteDispatchDomainEventSend (struct qemud_client *client,
5295 5296 5297
                               int procnr,
                               xdrproc_t proc,
                               void *data)
5298
{
5299
    struct qemud_client_message *msg = NULL;
5300
    XDR xdr;
5301
    unsigned int len;
5302

5303
    if (VIR_ALLOC(msg) < 0)
5304 5305
        return;

5306 5307
    msg->hdr.prog = REMOTE_PROGRAM;
    msg->hdr.vers = REMOTE_PROTOCOL_VERSION;
5308
    msg->hdr.proc = procnr;
5309
    msg->hdr.type = REMOTE_MESSAGE;
5310 5311
    msg->hdr.serial = 1;
    msg->hdr.status = REMOTE_OK;
5312

5313 5314
    if (remoteEncodeClientMessageHeader(msg) < 0)
        goto error;
5315

5316 5317
    /* Serialise the return header and event. */
    xdrmem_create (&xdr,
5318 5319
                   msg->buffer,
                   msg->bufferLength,
5320
                   XDR_ENCODE);
5321

5322 5323
    /* Skip over the header we just wrote */
    if (xdr_setpos (&xdr, msg->bufferOffset) == 0)
5324
        goto xdr_error;
5325

5326 5327
    if (!(proc)(&xdr, data)) {
        VIR_WARN("Failed to serialize domain event %d", procnr);
5328
        goto xdr_error;
5329
    }
5330

5331 5332
    /* Update length word to include payload*/
    len = msg->bufferOffset = xdr_getpos (&xdr);
5333 5334
    if (xdr_setpos (&xdr, 0) == 0)
        goto xdr_error;
5335

5336 5337
    if (!xdr_u_int (&xdr, &len))
        goto xdr_error;
5338 5339

    /* Send it. */
5340 5341 5342 5343
    msg->async = 1;
    msg->bufferLength = len;
    msg->bufferOffset = 0;
    qemudClientMessageQueuePush(&client->tx, msg);
5344
    qemudUpdateClientEvent(client);
5345 5346 5347 5348 5349 5350 5351 5352

    xdr_destroy (&xdr);
    return;

xdr_error:
    xdr_destroy(&xdr);
error:
    VIR_FREE(msg);
5353
}
5354

5355 5356 5357
static int
remoteDispatchNumOfSecrets (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client ATTRIBUTE_UNUSED,
5358 5359 5360
                            virConnectPtr conn,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *err,
5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375
                            void *args ATTRIBUTE_UNUSED,
                            remote_num_of_secrets_ret *ret)
{
    ret->num = virConnectNumOfSecrets (conn);
    if (ret->num == -1) {
        remoteDispatchConnError (err, conn);
        return -1;
    }

    return 0;
}

static int
remoteDispatchListSecrets (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
5376 5377 5378
                           virConnectPtr conn,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *err,
5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406
                           remote_list_secrets_args *args,
                           remote_list_secrets_ret *ret)
{
    if (args->maxuuids > REMOTE_SECRET_UUID_LIST_MAX) {
        remoteDispatchFormatError (err, "%s",
                                   _("maxuuids > REMOTE_SECRET_UUID_LIST_MAX"));
        return -1;
    }

    if (VIR_ALLOC_N (ret->uuids.uuids_val, args->maxuuids) < 0) {
        remoteDispatchOOMError (err);
        return -1;
    }

    ret->uuids.uuids_len = virConnectListSecrets (conn, ret->uuids.uuids_val,
                                                  args->maxuuids);
    if (ret->uuids.uuids_len == -1) {
        VIR_FREE (ret->uuids.uuids_val);
        remoteDispatchConnError (err, conn);
        return -1;
    }

    return 0;
}

static int
remoteDispatchSecretDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
5407 5408 5409
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
                               remote_error *err,
5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428
                               remote_secret_define_xml_args *args,
                               remote_secret_define_xml_ret *ret)
{
    virSecretPtr secret;

    secret = virSecretDefineXML (conn, args->xml, args->flags);
    if (secret == NULL) {
        remoteDispatchConnError (err, conn);
        return -1;
    }

    make_nonnull_secret (&ret->secret, secret);
    virSecretFree (secret);
    return 0;
}

static int
remoteDispatchSecretGetValue (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
5429 5430 5431
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460
                              remote_secret_get_value_args *args,
                              remote_secret_get_value_ret *ret)
{
    virSecretPtr secret;
    size_t value_size;
    unsigned char *value;

    secret = get_nonnull_secret (conn, args->secret);
    if (secret == NULL) {
        remoteDispatchConnError (err, conn);
        return -1;
    }

    value = virSecretGetValue (secret, &value_size, args->flags);
    if (value == NULL) {
        remoteDispatchConnError (err, conn);
        virSecretFree(secret);
        return -1;
    }

    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;
    virSecretFree(secret);
    return 0;
}

static int
remoteDispatchSecretGetXmlDesc (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client ATTRIBUTE_UNUSED,
5461 5462 5463
                                virConnectPtr conn,
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                remote_error *err,
5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484
                                remote_secret_get_xml_desc_args *args,
                                remote_secret_get_xml_desc_ret *ret)
{
    virSecretPtr secret;

    secret = get_nonnull_secret (conn, args->secret);
    if (secret == NULL) {
        remoteDispatchConnError (err, conn);
        return -1;
    }
    ret->xml = virSecretGetXMLDesc (secret, args->flags);
    if (ret->xml == NULL) {
        remoteDispatchConnError (err, conn);
        virSecretFree(secret);
        return -1;
    }
    virSecretFree(secret);
    return 0;
}

static int
5485 5486
remoteDispatchSecretLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
5487 5488 5489
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *err,
5490 5491
                                  remote_secret_lookup_by_uuid_args *args,
                                  remote_secret_lookup_by_uuid_ret *ret)
5492 5493 5494
{
    virSecretPtr secret;

5495
    secret = virSecretLookupByUUID (conn, (unsigned char *)args->uuid);
5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508
    if (secret == NULL) {
        remoteDispatchConnError (err, conn);
        return -1;
    }

    make_nonnull_secret (&ret->secret, secret);
    virSecretFree (secret);
    return 0;
}

static int
remoteDispatchSecretSetValue (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
5509 5510 5511
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535
                              remote_secret_set_value_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virSecretPtr secret;

    secret = get_nonnull_secret (conn, args->secret);
    if (secret == NULL) {
        remoteDispatchConnError (err, conn);
        return -1;
    }
    if (virSecretSetValue (secret, (const unsigned char *)args->value.value_val,
                           args->value.value_len, args->flags) < 0) {
        remoteDispatchConnError (err, conn);
        virSecretFree(secret);
        return -1;
    }

    virSecretFree(secret);
    return 0;
}

static int
remoteDispatchSecretUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
5536 5537 5538
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558
                              remote_secret_undefine_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virSecretPtr secret;

    secret = get_nonnull_secret (conn, args->secret);
    if (secret == NULL) {
        remoteDispatchConnError (err, conn);
        return -1;
    }
    if (virSecretUndefine (secret) < 0) {
        remoteDispatchConnError (err, conn);
        virSecretFree(secret);
        return -1;
    }

    virSecretFree(secret);
    return 0;
}

5559 5560 5561
static int
remoteDispatchSecretLookupByUsage (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
5562 5563 5564
                                   virConnectPtr conn,
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                                   remote_error *err,
5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580
                                   remote_secret_lookup_by_usage_args *args,
                                   remote_secret_lookup_by_usage_ret *ret)
{
    virSecretPtr secret;

    secret = virSecretLookupByUsage (conn, args->usageType, args->usageID);
    if (secret == NULL) {
        remoteDispatchConnError (err, conn);
        return -1;
    }

    make_nonnull_secret (&ret->secret, secret);
    virSecretFree (secret);
    return 0;
}

5581

5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783
static int remoteDispatchDomainIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                        struct qemud_client *client ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *hdr ATTRIBUTE_UNUSED,
                                        remote_error *err,
                                        remote_domain_is_active_args *args,
                                        remote_domain_is_active_ret *ret)
{
    virDomainPtr domain;

    domain = get_nonnull_domain(conn, args->dom);
    if (domain == NULL) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    ret->active = virDomainIsActive(domain);

    if (ret->active < 0) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    return 0;
}

static int remoteDispatchDomainIsPersistent(struct qemud_server *server ATTRIBUTE_UNUSED,
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                                            remote_error *err,
                                            remote_domain_is_persistent_args *args,
                                            remote_domain_is_persistent_ret *ret)
{
    virDomainPtr domain;

    domain = get_nonnull_domain(conn, args->dom);
    if (domain == NULL) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    ret->persistent = virDomainIsPersistent(domain);

    if (ret->persistent < 0) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    return 0;
}

static int remoteDispatchInterfaceIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                           struct qemud_client *client ATTRIBUTE_UNUSED,
                                           virConnectPtr conn,
                                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                                           remote_error *err,
                                           remote_interface_is_active_args *args,
                                           remote_interface_is_active_ret *ret)
{
    virInterfacePtr iface;

    iface = get_nonnull_interface(conn, args->iface);
    if (iface == NULL) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    ret->active = virInterfaceIsActive(iface);

    if (ret->active < 0) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    return 0;
}

static int remoteDispatchNetworkIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                         struct qemud_client *client ATTRIBUTE_UNUSED,
                                         virConnectPtr conn,
                                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                                         remote_error *err,
                                         remote_network_is_active_args *args,
                                         remote_network_is_active_ret *ret)
{
    virNetworkPtr network;

    network = get_nonnull_network(conn, args->net);
    if (network == NULL) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    ret->active = virNetworkIsActive(network);

    if (ret->active < 0) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    return 0;
}

static int remoteDispatchNetworkIsPersistent(struct qemud_server *server ATTRIBUTE_UNUSED,
                                             struct qemud_client *client ATTRIBUTE_UNUSED,
                                             virConnectPtr conn,
                                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                                             remote_error *err,
                                             remote_network_is_persistent_args *args,
                                             remote_network_is_persistent_ret *ret)
{
    virNetworkPtr network;

    network = get_nonnull_network(conn, args->net);
    if (network == NULL) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    ret->persistent = virNetworkIsPersistent(network);

    if (ret->persistent < 0) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    return 0;
}

static int remoteDispatchStoragePoolIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                             struct qemud_client *client ATTRIBUTE_UNUSED,
                                             virConnectPtr conn,
                                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                                             remote_error *err,
                                             remote_storage_pool_is_active_args *args,
                                             remote_storage_pool_is_active_ret *ret)
{
    virStoragePoolPtr pool;

    pool = get_nonnull_storage_pool(conn, args->pool);
    if (pool == NULL) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    ret->active = virStoragePoolIsActive(pool);

    if (ret->active < 0) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    return 0;
}

static int remoteDispatchStoragePoolIsPersistent(struct qemud_server *server ATTRIBUTE_UNUSED,
                                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                                 virConnectPtr conn,
                                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                                 remote_error *err,
                                                 remote_storage_pool_is_persistent_args *args,
                                                 remote_storage_pool_is_persistent_ret *ret)
{
    virStoragePoolPtr pool;

    pool = get_nonnull_storage_pool(conn, args->pool);
    if (pool == NULL) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    ret->persistent = virStoragePoolIsPersistent(pool);

    if (ret->persistent < 0) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    return 0;
}


static int remoteDispatchIsSecure(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *err,
                                  void *args ATTRIBUTE_UNUSED,
                                  remote_is_secure_ret *ret)
{
    ret->secure = virConnectIsSecure(conn);

    if (ret->secure < 0) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    return 0;
}


5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805
static int
remoteDispatchCpuCompare(struct qemud_server *server ATTRIBUTE_UNUSED,
                         struct qemud_client *client ATTRIBUTE_UNUSED,
                         virConnectPtr conn,
                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                         remote_error *err,
                         remote_cpu_compare_args *args,
                         remote_cpu_compare_ret *ret)
{
    int result;

    result = virConnectCompareCPU(conn, args->xml, args->flags);
    if (result == VIR_CPU_COMPARE_ERROR) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    ret->result = result;
    return 0;
}


5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826
static int
remoteDispatchCpuBaseline(struct qemud_server *server ATTRIBUTE_UNUSED,
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
                          remote_error *err,
                          remote_cpu_baseline_args *args,
                          remote_cpu_baseline_ret *ret)
{
    char *cpu;

    cpu = virConnectBaselineCPU(conn,
                                (const char **) args->xmlCPUs.xmlCPUs_val,
                                args->xmlCPUs.xmlCPUs_len,
                                args->flags);
    if (cpu == NULL) {
        remoteDispatchConnError(err, conn);
        return -1;
    }

    ret->cpu = cpu;
5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870

    return 0;
}


static int
remoteDispatchDomainGetJobInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                remote_error *rerr,
                                remote_domain_get_job_info_args *args,
                                remote_domain_get_job_info_ret *ret)
{
    virDomainPtr dom;
    virDomainJobInfo info;

    dom = get_nonnull_domain (conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainGetJobInfo (dom, &info) == -1) {
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    ret->type = info.type;
    ret->timeElapsed = info.timeElapsed;
    ret->timeRemaining = info.timeRemaining;
    ret->dataTotal = info.dataTotal;
    ret->dataProcessed = info.dataProcessed;
    ret->dataRemaining = info.dataRemaining;
    ret->memTotal = info.memTotal;
    ret->memProcessed = info.memProcessed;
    ret->memRemaining = info.memRemaining;
    ret->fileTotal = info.fileTotal;
    ret->fileProcessed = info.fileProcessed;
    ret->fileRemaining = info.fileRemaining;

    virDomainFree(dom);

5871 5872 5873 5874
    return 0;
}


5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903
static int
remoteDispatchDomainAbortJob (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *rerr,
                              remote_domain_abort_job_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

    dom = get_nonnull_domain (conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainAbortJob (dom) == -1) {
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    virDomainFree(dom);

    return 0;
}


5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931
static int
remoteDispatchDomainMigrateSetMaxDowntime(struct qemud_server *server ATTRIBUTE_UNUSED,
                                          struct qemud_client *client ATTRIBUTE_UNUSED,
                                          virConnectPtr conn,
                                          remote_message_header *hdr ATTRIBUTE_UNUSED,
                                          remote_error *rerr,
                                          remote_domain_migrate_set_max_downtime_args *args,
                                          void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

    dom = get_nonnull_domain(conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainMigrateSetMaxDowntime(dom, args->downtime, args->flags) == -1) {
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    virDomainFree(dom);

    return 0;
}

C
Chris Lalancette 已提交
5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973
static int
remoteDispatchDomainSnapshotCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_snapshot_create_xml_args *args,
                                       remote_domain_snapshot_create_xml_ret *ret)
{
    virDomainSnapshotPtr snapshot;
    virDomainPtr domain;

    domain = get_nonnull_domain(conn, args->domain);
    if (domain == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    snapshot = virDomainSnapshotCreateXML(domain, args->xml_desc, args->flags);
    if (snapshot == NULL) {
        virDomainFree(domain);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_domain_snapshot(&ret->snap, snapshot);

    virDomainSnapshotFree(snapshot);
    virDomainFree(domain);

    return 0;
}

static int
remoteDispatchDomainSnapshotDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
                                     remote_error *rerr,
                                     remote_domain_snapshot_dump_xml_args *args,
                                     remote_domain_snapshot_dump_xml_ret *ret)
{
5974 5975 5976
    virDomainPtr domain = NULL;
    virDomainSnapshotPtr snapshot = NULL;
    int rc = -1;
C
Chris Lalancette 已提交
5977

5978 5979 5980 5981 5982 5983 5984
    domain = get_nonnull_domain(conn, args->snap.domain);
    if (domain == NULL)
        goto cleanup;

    snapshot = get_nonnull_domain_snapshot(domain, args->snap);
    if (snapshot == NULL)
        goto cleanup;
C
Chris Lalancette 已提交
5985 5986 5987

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainSnapshotGetXMLDesc(snapshot, args->flags);
5988 5989 5990 5991 5992 5993 5994
    if (!ret->xml)
        goto cleanup;

    rc = 0;

cleanup:
    if (snapshot)
C
Chris Lalancette 已提交
5995
        virDomainSnapshotFree(snapshot);
5996 5997 5998
    if (domain)
        virDomainFree(domain);
    if (rc < 0)
C
Chris Lalancette 已提交
5999 6000
        remoteDispatchConnError(rerr, conn);

6001
    return rc;
C
Chris Lalancette 已提交
6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185
}

static int
remoteDispatchDomainSnapshotNum (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_domain_snapshot_num_args *args,
                                 remote_domain_snapshot_num_ret *ret)
{
    virDomainPtr domain;

    domain = get_nonnull_domain(conn, args->domain);
    if (domain == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    ret->num = virDomainSnapshotNum(domain, args->flags);
    if (ret->num == -1) {
        virDomainFree(domain);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    virDomainFree(domain);

    return 0;
}

static int
remoteDispatchDomainSnapshotListNames (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_snapshot_list_names_args *args,
                                       remote_domain_snapshot_list_names_ret *ret)
{
    virDomainPtr domain;

    if (args->nameslen > REMOTE_DOMAIN_SNAPSHOT_LIST_NAMES_MAX) {
        remoteDispatchFormatError (rerr, "%s",
                                   _("nameslen > REMOTE_DOMAIN_SNAPSHOT_LIST_NAMES_MAX"));
        return -1;
    }

    domain = get_nonnull_domain(conn, args->domain);
    if (domain == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->nameslen) < 0) {
        virDomainFree(domain);
        remoteDispatchOOMError(rerr);
        return -1;
    }

    ret->names.names_len = virDomainSnapshotListNames(domain,
                                                      ret->names.names_val,
                                                      args->nameslen,
                                                      args->flags);
    if (ret->names.names_len == -1) {
        virDomainFree(domain);
        VIR_FREE(ret->names.names_val);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    virDomainFree(domain);

    return 0;
}

static int
remoteDispatchDomainSnapshotLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                          struct qemud_client *client ATTRIBUTE_UNUSED,
                                          virConnectPtr conn,
                                          remote_message_header *hdr ATTRIBUTE_UNUSED,
                                          remote_error *rerr,
                                          remote_domain_snapshot_lookup_by_name_args *args,
                                          remote_domain_snapshot_lookup_by_name_ret *ret)
{
    virDomainSnapshotPtr snapshot;
    virDomainPtr domain;

    domain = get_nonnull_domain(conn, args->domain);
    if (domain == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    snapshot = virDomainSnapshotLookupByName(domain, args->name, args->flags);
    if (snapshot == NULL) {
        virDomainFree(domain);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_domain_snapshot (&ret->snap, snapshot);

    virDomainSnapshotFree(snapshot);
    virDomainFree(domain);

    return 0;
}

static int
remoteDispatchDomainHasCurrentSnapshot(struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_has_current_snapshot_args *args,
                                       remote_domain_has_current_snapshot_ret *ret)
{
    virDomainPtr domain;
    int result;

    domain = get_nonnull_domain(conn, args->domain);
    if (domain == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    result = virDomainHasCurrentSnapshot(domain, args->flags);
    if (result < 0) {
        virDomainFree(domain);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    ret->result = result;

    virDomainFree(domain);

    return 0;
}

static int
remoteDispatchDomainSnapshotCurrent(struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
                                    remote_error *rerr,
                                    remote_domain_snapshot_current_args *args,
                                    remote_domain_snapshot_current_ret *ret)
{
    virDomainSnapshotPtr snapshot;
    virDomainPtr domain;

    domain = get_nonnull_domain(conn, args->domain);
    if (domain == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    snapshot = virDomainSnapshotCurrent(domain, args->flags);
    if (snapshot == NULL) {
        virDomainFree(domain);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_domain_snapshot(&ret->snap, snapshot);

    virDomainSnapshotFree(snapshot);
    virDomainFree(domain);

    return 0;
}

static int
remoteDispatchDomainRevertToSnapshot (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
                                      remote_error *rerr,
                                      remote_domain_revert_to_snapshot_args *args,
                                      void *ret ATTRIBUTE_UNUSED)
{
6186 6187 6188
    virDomainPtr domain = NULL;
    virDomainSnapshotPtr snapshot = NULL;
    int rc = -1;
C
Chris Lalancette 已提交
6189

6190 6191 6192 6193 6194 6195 6196
    domain = get_nonnull_domain(conn, args->snap.domain);
    if (domain == NULL)
        goto cleanup;

    snapshot = get_nonnull_domain_snapshot(domain, args->snap);
    if (snapshot == NULL)
        goto cleanup;
C
Chris Lalancette 已提交
6197

6198 6199 6200 6201 6202 6203 6204
    if (virDomainRevertToSnapshot(snapshot, args->flags) == -1)
        goto cleanup;

    rc = 0;

cleanup:
    if (snapshot)
C
Chris Lalancette 已提交
6205
        virDomainSnapshotFree(snapshot);
6206 6207 6208
    if (domain)
        virDomainFree(domain);
    if (rc < 0)
C
Chris Lalancette 已提交
6209 6210
        remoteDispatchConnError(rerr, conn);

6211
    return rc;
C
Chris Lalancette 已提交
6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222
}

static int
remoteDispatchDomainSnapshotDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
                                    remote_error *rerr,
                                    remote_domain_snapshot_delete_args *args,
                                    void *ret ATTRIBUTE_UNUSED)
{
6223 6224 6225
    virDomainPtr domain = NULL;
    virDomainSnapshotPtr snapshot = NULL;
    int rc = -1;
C
Chris Lalancette 已提交
6226

6227 6228 6229 6230 6231 6232 6233
    domain = get_nonnull_domain(conn, args->snap.domain);
    if (domain == NULL)
        goto cleanup;

    snapshot = get_nonnull_domain_snapshot(domain, args->snap);
    if (snapshot == NULL)
        goto cleanup;
C
Chris Lalancette 已提交
6234

6235 6236 6237 6238 6239 6240 6241
    if (virDomainSnapshotDelete(snapshot, args->flags) == -1)
        goto cleanup;

    rc = 0;

cleanup:
    if (snapshot)
C
Chris Lalancette 已提交
6242
        virDomainSnapshotFree(snapshot);
6243 6244 6245
    if (domain)
        virDomainFree(domain);
    if (rc < 0)
C
Chris Lalancette 已提交
6246 6247
        remoteDispatchConnError(rerr, conn);

6248
    return rc;
C
Chris Lalancette 已提交
6249 6250
}

6251

6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323
static int
remoteDispatchDomainEventsRegisterAny (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr ATTRIBUTE_UNUSED,
                                       remote_domain_events_register_any_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);
    int callbackID;

    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
        remoteDispatchFormatError(rerr, _("unsupported event ID %d"), args->eventID);
        return -1;
    }

    if (client->domainEventCallbackID[args->eventID] != -1)  {
        remoteDispatchFormatError(rerr, _("domain event %d already registered"), args->eventID);
        return -1;
    }

    if ((callbackID = virConnectDomainEventRegisterAny(conn,
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
                                                       client, NULL)) < 0) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    client->domainEventCallbackID[args->eventID] = callbackID;

    return 0;
}


static int
remoteDispatchDomainEventsDeregisterAny (struct qemud_server *server ATTRIBUTE_UNUSED,
                                         struct qemud_client *client ATTRIBUTE_UNUSED,
                                         virConnectPtr conn,
                                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                                         remote_error *rerr ATTRIBUTE_UNUSED,
                                         remote_domain_events_deregister_any_args *args,
                                         void *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);
    int callbackID = -1;

    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
        remoteDispatchFormatError(rerr, _("unsupported event ID %d"), args->eventID);
        return -1;
    }

    callbackID = client->domainEventCallbackID[args->eventID];
    if (callbackID < 0) {
        remoteDispatchFormatError(rerr, _("domain event %d not registered"), args->eventID);
        return -1;
    }

    if (virConnectDomainEventDeregisterAny(conn, callbackID) < 0) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    client->domainEventCallbackID[args->eventID] = -1;
    return 0;
}


6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502

static int
remoteDispatchNwfilterLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
                                    remote_error *rerr,
                                    remote_nwfilter_lookup_by_name_args *args,
                                    remote_nwfilter_lookup_by_name_ret *ret)
{
    virNWFilterPtr nwfilter;

    nwfilter = virNWFilterLookupByName (conn, args->name);
    if (nwfilter == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_nwfilter (&ret->nwfilter, nwfilter);
    virNWFilterFree(nwfilter);
    return 0;
}

static int
remoteDispatchNwfilterLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
                                    remote_error *rerr,
                                    remote_nwfilter_lookup_by_uuid_args *args,
                                    remote_nwfilter_lookup_by_uuid_ret *ret)
{
    virNWFilterPtr nwfilter;

    nwfilter = virNWFilterLookupByUUID (conn, (unsigned char *) args->uuid);
    if (nwfilter == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_nwfilter (&ret->nwfilter, nwfilter);
    virNWFilterFree(nwfilter);
    return 0;
}


static int
remoteDispatchNwfilterDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_nwfilter_define_xml_args *args,
                                 remote_nwfilter_define_xml_ret *ret)
{
    virNWFilterPtr nwfilter;

    nwfilter = virNWFilterDefineXML (conn, args->xml);
    if (nwfilter == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_nwfilter (&ret->nwfilter, nwfilter);
    virNWFilterFree(nwfilter);
    return 0;
}


static int
remoteDispatchNwfilterUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                remote_error *rerr,
                                remote_nwfilter_undefine_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virNWFilterPtr nwfilter;

    nwfilter = get_nonnull_nwfilter (conn, args->nwfilter);
    if (nwfilter == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virNWFilterUndefine (nwfilter) == -1) {
        virNWFilterFree(nwfilter);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    virNWFilterFree(nwfilter);
    return 0;
}

static int
remoteDispatchListNwfilters (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                             remote_error *rerr,
                             remote_list_nwfilters_args *args,
                             remote_list_nwfilters_ret *ret)
{

    if (args->maxnames > REMOTE_NWFILTER_NAME_LIST_MAX) {
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NWFILTER_NAME_LIST_MAX"));
        return -1;
    }

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

    ret->names.names_len =
        virConnectListNWFilters (conn,
                                 ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_len);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    return 0;
}


static int
remoteDispatchNwfilterGetXmlDesc (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *rerr,
                              remote_nwfilter_get_xml_desc_args *args,
                              remote_nwfilter_get_xml_desc_ret *ret)
{
    virNWFilterPtr nwfilter;

    nwfilter = get_nonnull_nwfilter (conn, args->nwfilter);
    if (nwfilter == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNWFilterGetXMLDesc (nwfilter, args->flags);
    if (!ret->xml) {
        virNWFilterFree(nwfilter);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    virNWFilterFree(nwfilter);
    return 0;
}


static int
remoteDispatchNumOfNwfilters (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *rerr,
                              void *args ATTRIBUTE_UNUSED,
                              remote_num_of_nwfilters_ret *ret)
{

    ret->num = virConnectNumOfNWFilters (conn);
    if (ret->num == -1) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    return 0;
}


6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536
static int
remoteDispatchDomainGetBlockInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_domain_get_block_info_args *args,
                                  remote_domain_get_block_info_ret *ret)
{
    virDomainPtr dom;
    virDomainBlockInfo info;

    dom = get_nonnull_domain (conn, args->dom);
    if (dom == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainGetBlockInfo (dom, args->path, &info, args->flags) == -1) {
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

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

    virDomainFree(dom);

    return 0;
}


6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562
/*----- 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);
}

D
Daniel Veillard 已提交
6563
static virInterfacePtr
6564
get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
6565
{
6566
    return virGetInterface (conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
6567 6568
}

6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582
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;
}

6583 6584 6585
static virSecretPtr
get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret)
{
6586
    return virGetSecret (conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
6587 6588
}

6589 6590 6591 6592 6593 6594
static virNWFilterPtr
get_nonnull_nwfilter (virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
{
    return virGetNWFilter (conn, nwfilter.name, BAD_CAST nwfilter.uuid);
}

C
Chris Lalancette 已提交
6595
static virDomainSnapshotPtr
6596
get_nonnull_domain_snapshot (virDomainPtr domain, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
6597 6598 6599 6600
{
    return virGetDomainSnapshot(domain, snapshot.name);
}

6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616
/* 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);
}

D
Daniel Veillard 已提交
6617 6618 6619 6620 6621 6622 6623 6624
static void
make_nonnull_interface (remote_nonnull_interface *interface_dst,
                        virInterfacePtr interface_src)
{
    interface_dst->name = strdup (interface_src->name);
    interface_dst->mac = strdup (interface_src->mac);
}

6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638
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);
}
6639 6640 6641 6642 6643 6644

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

static void
make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
{
6649
    memcpy (secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
6650 6651
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = strdup (secret_src->usageID);
6652
}
6653 6654 6655 6656 6657 6658 6659

static void
make_nonnull_nwfilter (remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
{
    nwfilter_dst->name = strdup (nwfilter_src->name);
    memcpy (nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
}
C
Chris Lalancette 已提交
6660 6661 6662 6663 6664 6665 6666

static void
make_nonnull_domain_snapshot (remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
{
    snapshot_dst->name = strdup(snapshot_src->name);
    make_nonnull_domain(&snapshot_dst->domain, snapshot_src->domain);
}