remote.c 230.1 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
#include "uuid.h"
61
#include "network.h"
C
Chris Lalancette 已提交
62
#include "libvirt/libvirt-qemu.h"
63

64
#define VIR_FROM_THIS VIR_FROM_REMOTE
65
#define REMOTE_DEBUG(fmt, ...) DEBUG(fmt, __VA_ARGS__)
66

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

85

86
#include "remote_dispatch_prototypes.h"
C
Chris Lalancette 已提交
87
#include "qemu_dispatch_prototypes.h"
88 89 90 91

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

C
Chris Lalancette 已提交
93 94 95 96
static const dispatch_data const qemu_dispatch_table[] = {
#include "qemu_dispatch_table.h"
};

97 98 99 100 101 102 103 104 105 106
const dispatch_data const *remoteGetDispatchData(int proc)
{
    if (proc >= ARRAY_CARDINALITY(dispatch_table) ||
        dispatch_table[proc].fn == NULL) {
        return NULL;
    }

    return &(dispatch_table[proc]);
}

C
Chris Lalancette 已提交
107 108 109 110 111 112 113 114 115 116
const dispatch_data const *qemuGetDispatchData(int proc)
{
    if (proc >= ARRAY_CARDINALITY(qemu_dispatch_table) ||
        qemu_dispatch_table[proc].fn == NULL) {
        return NULL;
    }

    return &(qemu_dispatch_table[proc]);
}

117 118 119
/* Prototypes */
static void
remoteDispatchDomainEventSend (struct qemud_client *client,
120 121 122
                               int procnr,
                               xdrproc_t proc,
                               void *data);
123

124 125 126 127 128
static int remoteRelayDomainEventLifecycle(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           int event,
                                           int detail,
                                           void *opaque)
129 130
{
    struct qemud_client *client = opaque;
131
    remote_domain_event_lifecycle_msg data;
132

133 134 135 136
    if (!client)
        return -1;

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

138
    virMutexLock(&client->lock);
139

140 141 142 143 144
    /* build return data */
    memset(&data, 0, sizeof data);
    make_nonnull_domain (&data.dom, dom);
    data.event = event;
    data.detail = detail;
145

146
    remoteDispatchDomainEventSend (client,
147 148
                                   REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
                                   (xdrproc_t)xdr_remote_domain_event_lifecycle_msg, &data);
149 150

    virMutexUnlock(&client->lock);
151

152 153
    return 0;
}
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
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;
}

182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
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;
}


213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
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;
}


243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
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;
}


277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
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;
}


314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
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;
}


378
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
379
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
380
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
381
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
382
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
383
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
384
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
385
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
386 387 388 389
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

390 391 392
/*----- Functions. -----*/

static int
393
remoteDispatchOpen (struct qemud_server *server,
394
                    struct qemud_client *client,
395
                    virConnectPtr conn,
396
                    remote_message_header *hdr ATTRIBUTE_UNUSED,
397
                    remote_error *rerr,
398 399 400
                    struct remote_open_args *args, void *ret ATTRIBUTE_UNUSED)
{
    const char *name;
401
    int flags, rc;
402 403

    /* Already opened? */
404
    if (conn) {
405 406
        remoteDispatchFormatError (rerr, "%s", _("connection already open"));
        return -1;
407 408
    }

409 410 411
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
412

413 414 415 416 417 418 419 420 421 422 423 424 425
    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);

426
    if (client->conn == NULL)
427 428
        remoteDispatchConnError(rerr, NULL);

429
    rc = client->conn ? 0 : -1;
430
    virMutexUnlock(&client->lock);
431
    return rc;
432 433
}

434 435 436 437
#define CHECK_CONN(client)                                              \
    if (!client->conn) {                                                \
        remoteDispatchFormatError (rerr, "%s", _("connection not open")); \
        return -1;                                                      \
438 439 440
    }

static int
441
remoteDispatchClose (struct qemud_server *server ATTRIBUTE_UNUSED,
442 443
                     struct qemud_client *client ATTRIBUTE_UNUSED,
                     virConnectPtr conn ATTRIBUTE_UNUSED,
444
                     remote_message_header *hdr ATTRIBUTE_UNUSED,
445
                     remote_error *rerr ATTRIBUTE_UNUSED,
446 447
                     void *args ATTRIBUTE_UNUSED, void *ret ATTRIBUTE_UNUSED)
{
448 449 450
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
451

452
    client->closing = 1;
453

454
    virMutexUnlock(&client->lock);
455
    return 0;
456 457
}

458
static int
459
remoteDispatchSupportsFeature (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
                               remote_supports_feature_args *args, remote_supports_feature_ret *ret)
{
466
    ret->supported = virDrvSupportsFeature (conn, args->feature);
467 468

    if (ret->supported == -1) {
469
        remoteDispatchConnError(rerr, conn);
470 471
        return -1;
    }
472 473 474 475

    return 0;
}

476
static int
477
remoteDispatchGetType (struct qemud_server *server ATTRIBUTE_UNUSED,
478 479
                       struct qemud_client *client ATTRIBUTE_UNUSED,
                       virConnectPtr conn,
480
                       remote_message_header *hdr ATTRIBUTE_UNUSED,
481
                       remote_error *rerr,
482 483 484 485
                       void *args ATTRIBUTE_UNUSED, remote_get_type_ret *ret)
{
    const char *type;

486
    type = virConnectGetType (conn);
487
    if (type == NULL) {
488
        remoteDispatchConnError(rerr, conn);
489 490
        return -1;
    }
491 492 493 494 495 496

    /* We have to strdup because remoteDispatchClientRequest will
     * free this string after it's been serialised.
     */
    ret->type = strdup (type);
    if (!ret->type) {
497
        remoteDispatchOOMError(rerr);
498
        return -1;
499 500 501 502 503 504
    }

    return 0;
}

static int
505
remoteDispatchGetVersion (struct qemud_server *server ATTRIBUTE_UNUSED,
506 507
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
508
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
509
                          remote_error *rerr,
510 511 512 513 514
                          void *args ATTRIBUTE_UNUSED,
                          remote_get_version_ret *ret)
{
    unsigned long hvVer;

515 516
    if (virConnectGetVersion (conn, &hvVer) == -1) {
        remoteDispatchConnError(rerr, conn);
517
        return -1;
518
    }
519 520 521 522 523

    ret->hv_ver = hvVer;
    return 0;
}

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
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;
}

544
static int
545
remoteDispatchGetHostname (struct qemud_server *server ATTRIBUTE_UNUSED,
546 547
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
548
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
549
                           remote_error *rerr,
550 551 552 553 554
                           void *args ATTRIBUTE_UNUSED,
                           remote_get_hostname_ret *ret)
{
    char *hostname;

555
    hostname = virConnectGetHostname (conn);
556
    if (hostname == NULL) {
557
        remoteDispatchConnError(rerr, conn);
558 559
        return -1;
    }
560 561 562 563 564

    ret->hostname = hostname;
    return 0;
}

565 566
static int
remoteDispatchGetUri (struct qemud_server *server ATTRIBUTE_UNUSED,
567 568
                      struct qemud_client *client ATTRIBUTE_UNUSED,
                      virConnectPtr conn,
569
                      remote_message_header *hdr ATTRIBUTE_UNUSED,
570
                      remote_error *rerr,
571 572 573 574 575 576
                      void *args ATTRIBUTE_UNUSED,
                      remote_get_uri_ret *ret)
{
    char *uri;
    CHECK_CONN(client);

577
    uri = virConnectGetURI (conn);
578
    if (uri == NULL) {
579
        remoteDispatchConnError(rerr, conn);
580 581
        return -1;
    }
582 583 584 585 586

    ret->uri = uri;
    return 0;
}

587
static int
588
remoteDispatchGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
589 590
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
591
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
592
                           remote_error *rerr,
593 594 595 596 597 598
                           remote_get_max_vcpus_args *args,
                           remote_get_max_vcpus_ret *ret)
{
    char *type;

    type = args->type ? *args->type : NULL;
599
    ret->max_vcpus = virConnectGetMaxVcpus (conn, type);
600
    if (ret->max_vcpus == -1) {
601
        remoteDispatchConnError(rerr, conn);
602 603
        return -1;
    }
604 605 606 607 608

    return 0;
}

static int
609
remoteDispatchNodeGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
610 611
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
612
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
613
                           remote_error *rerr,
614 615 616 617 618
                           void *args ATTRIBUTE_UNUSED,
                           remote_node_get_info_ret *ret)
{
    virNodeInfo info;

619 620
    if (virNodeGetInfo (conn, &info) == -1) {
        remoteDispatchConnError(rerr, conn);
621
        return -1;
622
    }
623 624 625 626 627 628 629 630 631 632 633 634 635 636

    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
637
remoteDispatchGetCapabilities (struct qemud_server *server ATTRIBUTE_UNUSED,
638 639
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
640
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
641
                               remote_error *rerr,
642 643 644 645 646
                               void *args ATTRIBUTE_UNUSED,
                               remote_get_capabilities_ret *ret)
{
    char *caps;

647
    caps = virConnectGetCapabilities (conn);
648
    if (caps == NULL) {
649
        remoteDispatchConnError(rerr, conn);
650 651
        return -1;
    }
652 653 654 655 656

    ret->capabilities = caps;
    return 0;
}

657 658
static int
remoteDispatchNodeGetCellsFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
659 660
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
661
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
662
                                      remote_error *rerr,
663 664 665
                                      remote_node_get_cells_free_memory_args *args,
                                      remote_node_get_cells_free_memory_ret *ret)
{
D
Daniel P. Berrange 已提交
666
    int err;
667 668

    if (args->maxCells > REMOTE_NODE_MAX_CELLS) {
669 670 671
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxCells > REMOTE_NODE_MAX_CELLS"));
        return -1;
672 673 674
    }

    /* Allocate return buffer. */
675
    if (VIR_ALLOC_N(ret->freeMems.freeMems_val, args->maxCells) < 0) {
676 677
        remoteDispatchOOMError(rerr);
        return -1;
678
    }
679

D
Daniel P. Berrange 已提交
680 681 682 683 684
    err = virNodeGetCellsFreeMemory(conn,
                                    (unsigned long long *)ret->freeMems.freeMems_val,
                                    args->startCell,
                                    args->maxCells);
    if (err <= 0) {
685
        VIR_FREE(ret->freeMems.freeMems_val);
686
        remoteDispatchConnError(rerr, conn);
687
        return -1;
688
    }
D
Daniel P. Berrange 已提交
689
    ret->freeMems.freeMems_len = err;
690 691 692 693 694 695 696

    return 0;
}


static int
remoteDispatchNodeGetFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
697 698
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
699
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
700
                                 remote_error *rerr,
701 702 703 704 705
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_node_get_free_memory_ret *ret)
{
    unsigned long long freeMem;

706
    freeMem = virNodeGetFreeMemory(conn);
707
    if (freeMem == 0) {
708
        remoteDispatchConnError(rerr, conn);
709 710
        return -1;
    }
711 712 713 714 715
    ret->freeMem = freeMem;
    return 0;
}


716
static int
717
remoteDispatchDomainGetSchedulerType (struct qemud_server *server ATTRIBUTE_UNUSED,
718 719
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
720
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
721
                                      remote_error *rerr,
722 723 724 725 726 727 728
                                      remote_domain_get_scheduler_type_args *args,
                                      remote_domain_get_scheduler_type_ret *ret)
{
    virDomainPtr dom;
    char *type;
    int nparams;

729
    dom = get_nonnull_domain (conn, args->dom);
730
    if (dom == NULL) {
731
        remoteDispatchConnError(rerr, conn);
732
        return -1;
733 734 735
    }

    type = virDomainGetSchedulerType (dom, &nparams);
736 737
    if (type == NULL) {
        virDomainFree(dom);
738
        remoteDispatchConnError(rerr, conn);
739 740
        return -1;
    }
741 742 743

    ret->type = type;
    ret->nparams = nparams;
744
    virDomainFree(dom);
745 746 747 748
    return 0;
}

static int
749
remoteDispatchDomainGetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
750 751
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
752
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
753
                                            remote_error *rerr,
754 755 756 757 758 759 760 761 762 763
                                            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) {
764 765
        remoteDispatchFormatError (rerr, "%s", _("nparams too large"));
        return -1;
766
    }
767
    if (VIR_ALLOC_N(params, nparams) < 0) {
768 769
        remoteDispatchOOMError(rerr);
        return -1;
770 771
    }

772
    dom = get_nonnull_domain (conn, args->dom);
773
    if (dom == NULL) {
774
        VIR_FREE(params);
775
        remoteDispatchConnError(rerr, conn);
776
        return -1;
777 778 779 780
    }

    r = virDomainGetSchedulerParameters (dom, params, &nparams);
    if (r == -1) {
781
        virDomainFree(dom);
782
        VIR_FREE(params);
783
        remoteDispatchConnError(rerr, conn);
784 785 786 787 788
        return -1;
    }

    /* Serialise the scheduler parameters. */
    ret->params.params_len = nparams;
789 790
    if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
        goto oom;
791 792 793 794

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

798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
        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:
813
            remoteDispatchFormatError (rerr, "%s", _("unknown type"));
814
            goto cleanup;
815 816
        }
    }
817
    virDomainFree(dom);
818
    VIR_FREE(params);
819 820

    return 0;
821 822

oom:
823
    remoteDispatchOOMError(rerr);
824 825 826 827 828
cleanup:
    virDomainFree(dom);
    for (i = 0 ; i < nparams ; i++)
        VIR_FREE(ret->params.params_val[i].field);
    VIR_FREE(params);
829
    return -1;
830 831 832
}

static int
833
remoteDispatchDomainSetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
834 835
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
836
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
837
                                            remote_error *rerr,
838 839 840 841 842 843 844 845 846 847
                                            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) {
848 849
        remoteDispatchFormatError (rerr, "%s", _("nparams too large"));
        return -1;
850
    }
851
    if (VIR_ALLOC_N(params, nparams) < 0) {
852 853
        remoteDispatchOOMError(rerr);
        return -1;
854 855 856 857
    }

    /* Deserialise parameters. */
    for (i = 0; i < nparams; ++i) {
C
Chris Lalancette 已提交
858 859 860 861 862
        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;
        }
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
        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;
        }
    }

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

    r = virDomainSetSchedulerParameters (dom, params, nparams);
888
    virDomainFree(dom);
889
    VIR_FREE(params);
890
    if (r == -1) {
891
        remoteDispatchConnError(rerr, conn);
892 893
        return -1;
    }
894 895 896 897

    return 0;
}

898
static int
899
remoteDispatchDomainBlockStats (struct qemud_server *server ATTRIBUTE_UNUSED,
900 901
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
902
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
903
                                remote_error *rerr,
904 905 906 907 908 909 910
                                remote_domain_block_stats_args *args,
                                remote_domain_block_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainBlockStats stats;

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

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

    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
935
remoteDispatchDomainInterfaceStats (struct qemud_server *server ATTRIBUTE_UNUSED,
936 937
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
938
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
939
                                    remote_error *rerr,
940 941 942 943 944 945 946
                                    remote_domain_interface_stats_args *args,
                                    remote_domain_interface_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainInterfaceStats stats;

947
    dom = get_nonnull_domain (conn, args->dom);
948
    if (dom == NULL) {
949
        remoteDispatchConnError(rerr, conn);
950
        return -1;
951 952 953
    }
    path = args->path;

D
Daniel P. Berrange 已提交
954 955
    if (virDomainInterfaceStats (dom, path, &stats, sizeof stats) == -1) {
        virDomainFree (dom);
956
        remoteDispatchConnError(rerr, conn);
957
        return -1;
D
Daniel P. Berrange 已提交
958 959
    }
    virDomainFree (dom);
960 961 962 963 964 965 966 967 968 969 970 971 972

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

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
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) {
1000
        virDomainFree (dom);
1001 1002
        remoteDispatchOOMError(rerr);
        return -1;
1003
    }
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

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

1030 1031
static int
remoteDispatchDomainBlockPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
1032 1033
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1034
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1035
                               remote_error *rerr,
1036 1037 1038 1039 1040 1041 1042 1043 1044
                               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;

1045
    dom = get_nonnull_domain (conn, args->dom);
1046
    if (dom == NULL) {
1047
        remoteDispatchConnError(rerr, conn);
1048
        return -1;
1049 1050 1051 1052 1053 1054 1055
    }
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1056
        virDomainFree (dom);
1057 1058 1059
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
        return -1;
1060 1061 1062
    }

    ret->buffer.buffer_len = size;
1063
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
1064
        virDomainFree (dom);
1065
        remoteDispatchOOMError(rerr);
1066
        return -1;
1067 1068 1069 1070 1071 1072
    }

    if (virDomainBlockPeek (dom, path, offset, size,
                            ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
1073
        remoteDispatchConnError(rerr, conn);
1074 1075 1076 1077 1078 1079 1080
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

R
Richard W.M. Jones 已提交
1081 1082
static int
remoteDispatchDomainMemoryPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
1083 1084
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
1085
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
1086
                                remote_error *rerr,
R
Richard W.M. Jones 已提交
1087 1088 1089 1090 1091 1092 1093 1094
                                remote_domain_memory_peek_args *args,
                                remote_domain_memory_peek_ret *ret)
{
    virDomainPtr dom;
    unsigned long long offset;
    size_t size;
    unsigned int flags;

1095
    dom = get_nonnull_domain (conn, args->dom);
R
Richard W.M. Jones 已提交
1096
    if (dom == NULL) {
1097
        remoteDispatchConnError(rerr, conn);
1098
        return -1;
R
Richard W.M. Jones 已提交
1099 1100 1101 1102 1103 1104
    }
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1105
        virDomainFree (dom);
1106 1107 1108
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
        return -1;
R
Richard W.M. Jones 已提交
1109 1110 1111 1112 1113
    }

    ret->buffer.buffer_len = size;
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
        virDomainFree (dom);
1114
        remoteDispatchOOMError(rerr);
1115
        return -1;
R
Richard W.M. Jones 已提交
1116 1117 1118 1119 1120 1121
    }

    if (virDomainMemoryPeek (dom, offset, size,
                             ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
1122
        remoteDispatchConnError(rerr, conn);
R
Richard W.M. Jones 已提交
1123 1124 1125 1126 1127 1128 1129
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

1130
static int
1131
remoteDispatchDomainAttachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
1132 1133
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1134
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1135
                                  remote_error *rerr,
1136 1137 1138 1139 1140
                                  remote_domain_attach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

1147 1148
    if (virDomainAttachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
1149
        remoteDispatchConnError(rerr, conn);
1150
        return -1;
1151 1152
    }
    virDomainFree(dom);
1153 1154 1155
    return 0;
}

J
Jim Fehlig 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
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;
}

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
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;
}

1208
static int
1209
remoteDispatchDomainCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
1210 1211
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
1212
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
1213
                            remote_error *rerr,
1214 1215 1216 1217 1218
                            remote_domain_create_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

1225
    if (virDomainCreate (dom) == -1) {
1226
        virDomainFree(dom);
1227
        remoteDispatchConnError(rerr, conn);
1228
        return -1;
1229 1230
    }
    virDomainFree(dom);
1231 1232 1233
    return 0;
}

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
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;
    }

1251
    if (virDomainCreateWithFlags (dom, args->flags) == -1) {
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

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

1262
static int
1263
remoteDispatchDomainCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1264 1265
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1266
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1267 1268 1269
                               remote_error *rerr,
                               remote_domain_create_xml_args *args,
                               remote_domain_create_xml_ret *ret)
1270 1271 1272
{
    virDomainPtr dom;

1273
    dom = virDomainCreateXML (conn, args->xml_desc, args->flags);
1274
    if (dom == NULL) {
1275
        remoteDispatchConnError(rerr, conn);
1276 1277
        return -1;
    }
1278 1279

    make_nonnull_domain (&ret->dom, dom);
1280
    virDomainFree(dom);
1281 1282 1283 1284 1285

    return 0;
}

static int
1286
remoteDispatchDomainDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1287 1288
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1289
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1290
                               remote_error *rerr,
1291 1292 1293 1294 1295
                               remote_domain_define_xml_args *args,
                               remote_domain_define_xml_ret *ret)
{
    virDomainPtr dom;

1296
    dom = virDomainDefineXML (conn, args->xml);
1297
    if (dom == NULL) {
1298
        remoteDispatchConnError(rerr, conn);
1299 1300
        return -1;
    }
1301 1302

    make_nonnull_domain (&ret->dom, dom);
1303
    virDomainFree(dom);
1304 1305 1306 1307 1308

    return 0;
}

static int
1309
remoteDispatchDomainDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
1310 1311
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1312
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1313
                             remote_error *rerr,
1314 1315 1316 1317 1318
                             remote_domain_destroy_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1319
    dom = get_nonnull_domain (conn, args->dom);
1320
    if (dom == NULL) {
1321
        remoteDispatchConnError(rerr, conn);
1322
        return -1;
1323 1324
    }

1325
    if (virDomainDestroy (dom) == -1) {
1326
        virDomainFree(dom);
1327
        remoteDispatchConnError(rerr, conn);
1328
        return -1;
1329 1330
    }
    virDomainFree(dom);
1331 1332 1333 1334
    return 0;
}

static int
1335
remoteDispatchDomainDetachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
1336 1337
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1338
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1339
                                  remote_error *rerr,
1340 1341 1342 1343 1344
                                  remote_domain_detach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1345
    dom = get_nonnull_domain (conn, args->dom);
1346
    if (dom == NULL) {
1347
        remoteDispatchConnError(rerr, conn);
1348
        return -1;
1349 1350
    }

1351 1352
    if (virDomainDetachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
J
Jim Fehlig 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
        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);
1380
        remoteDispatchConnError(rerr, conn);
1381
        return -1;
1382
    }
1383

1384
    virDomainFree(dom);
1385 1386 1387 1388
    return 0;
}

static int
1389
remoteDispatchDomainDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1390 1391
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1392
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1393
                             remote_error *rerr,
1394 1395 1396 1397 1398
                             remote_domain_dump_xml_args *args,
                             remote_domain_dump_xml_ret *ret)
{
    virDomainPtr dom;

1399
    dom = get_nonnull_domain (conn, args->dom);
1400
    if (dom == NULL) {
1401
        remoteDispatchConnError(rerr, conn);
1402
        return -1;
1403 1404 1405 1406
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainGetXMLDesc (dom, args->flags);
1407
    if (!ret->xml) {
1408
        virDomainFree(dom);
1409
        remoteDispatchConnError(rerr, conn);
1410
        return -1;
1411 1412
    }
    virDomainFree(dom);
1413 1414 1415
    return 0;
}

1416 1417 1418 1419
static int
remoteDispatchDomainXmlFromNative (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1420
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
                                   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,
1441
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
                                 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;
}


1459
static int
1460
remoteDispatchDomainGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
1461 1462
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1463
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1464
                                  remote_error *rerr,
1465 1466 1467 1468 1469
                                  remote_domain_get_autostart_args *args,
                                  remote_domain_get_autostart_ret *ret)
{
    virDomainPtr dom;

1470
    dom = get_nonnull_domain (conn, args->dom);
1471
    if (dom == NULL) {
1472
        remoteDispatchConnError(rerr, conn);
1473
        return -1;
1474 1475
    }

1476 1477
    if (virDomainGetAutostart (dom, &ret->autostart) == -1) {
        virDomainFree(dom);
1478
        remoteDispatchConnError(rerr, conn);
1479
        return -1;
1480 1481
    }
    virDomainFree(dom);
1482 1483 1484 1485
    return 0;
}

static int
1486
remoteDispatchDomainGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
1487 1488
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1489
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1490
                             remote_error *rerr,
1491 1492 1493 1494 1495 1496
                             remote_domain_get_info_args *args,
                             remote_domain_get_info_ret *ret)
{
    virDomainPtr dom;
    virDomainInfo info;

1497
    dom = get_nonnull_domain (conn, args->dom);
1498
    if (dom == NULL) {
1499
        remoteDispatchConnError(rerr, conn);
1500
        return -1;
1501 1502
    }

1503 1504
    if (virDomainGetInfo (dom, &info) == -1) {
        virDomainFree(dom);
1505
        remoteDispatchConnError(rerr, conn);
1506
        return -1;
1507
    }
1508 1509 1510 1511 1512 1513 1514

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

1515 1516
    virDomainFree(dom);

1517 1518 1519 1520
    return 0;
}

static int
1521
remoteDispatchDomainGetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
1522 1523
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1524
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1525
                                  remote_error *rerr,
1526 1527 1528 1529 1530
                                  remote_domain_get_max_memory_args *args,
                                  remote_domain_get_max_memory_ret *ret)
{
    virDomainPtr dom;

1531
    dom = get_nonnull_domain (conn, args->dom);
1532
    if (dom == NULL) {
1533
        remoteDispatchConnError(rerr, conn);
1534
        return -1;
1535 1536 1537
    }

    ret->memory = virDomainGetMaxMemory (dom);
1538 1539
    if (ret->memory == 0) {
        virDomainFree(dom);
1540
        remoteDispatchConnError(rerr, conn);
1541 1542 1543
        return -1;
    }
    virDomainFree(dom);
1544 1545 1546 1547
    return 0;
}

static int
1548
remoteDispatchDomainGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1549 1550
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
1551
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
1552
                                 remote_error *rerr,
1553 1554 1555 1556 1557
                                 remote_domain_get_max_vcpus_args *args,
                                 remote_domain_get_max_vcpus_ret *ret)
{
    virDomainPtr dom;

1558
    dom = get_nonnull_domain (conn, args->dom);
1559
    if (dom == NULL) {
1560
        remoteDispatchConnError(rerr, conn);
1561
        return -1;
1562 1563 1564
    }

    ret->num = virDomainGetMaxVcpus (dom);
1565 1566
    if (ret->num == -1) {
        virDomainFree(dom);
1567
        remoteDispatchConnError(rerr, conn);
1568 1569 1570
        return -1;
    }
    virDomainFree(dom);
1571 1572 1573
    return 0;
}

1574 1575 1576 1577
static int
remoteDispatchDomainGetSecurityLabel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1578
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
                                     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);
1595
        remoteDispatchConnError(rerr, conn);
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
        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,
1616
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1617 1618 1619 1620 1621 1622 1623 1624
                                   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) {
1625
        remoteDispatchConnError(rerr, conn);
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
        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;
}

1646
static int
1647
remoteDispatchDomainGetOsType (struct qemud_server *server ATTRIBUTE_UNUSED,
1648 1649
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1650
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1651
                               remote_error *rerr,
1652 1653 1654 1655 1656
                               remote_domain_get_os_type_args *args,
                               remote_domain_get_os_type_ret *ret)
{
    virDomainPtr dom;

1657
    dom = get_nonnull_domain (conn, args->dom);
1658
    if (dom == NULL) {
1659
        remoteDispatchConnError(rerr, conn);
1660
        return -1;
1661 1662 1663 1664
    }

    /* remoteDispatchClientRequest will free this */
    ret->type = virDomainGetOSType (dom);
1665
    if (ret->type == NULL) {
1666
        virDomainFree(dom);
1667
        remoteDispatchConnError(rerr, conn);
1668
        return -1;
1669 1670
    }
    virDomainFree(dom);
1671 1672 1673 1674
    return 0;
}

static int
1675
remoteDispatchDomainGetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1676 1677
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
1678
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
1679
                              remote_error *rerr,
1680 1681 1682
                              remote_domain_get_vcpus_args *args,
                              remote_domain_get_vcpus_ret *ret)
{
1683 1684 1685
    virDomainPtr dom = NULL;
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
1686 1687
    int info_len, i;

1688
    dom = get_nonnull_domain (conn, args->dom);
1689
    if (dom == NULL) {
1690
        remoteDispatchConnError(rerr, conn);
1691
        return -1;
1692 1693 1694
    }

    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1695
        virDomainFree(dom);
1696 1697
        remoteDispatchFormatError (rerr, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
        return -1;
1698 1699
    }

1700
    if (args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1701
        virDomainFree(dom);
1702 1703
        remoteDispatchFormatError (rerr, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
        return -1;
1704 1705 1706
    }

    /* Allocate buffers to take the results. */
1707 1708
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
        goto oom;
1709 1710
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
1711
        goto oom;
1712 1713 1714 1715

    info_len = virDomainGetVcpus (dom,
                                  info, args->maxinfo,
                                  cpumaps, args->maplen);
1716
    if (info_len == -1) {
1717 1718
        VIR_FREE(info);
        VIR_FREE(cpumaps);
1719
        virDomainFree(dom);
1720
        remoteDispatchConnError(rerr, conn);
1721 1722
        return -1;
    }
1723 1724 1725

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
1726 1727
    if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
        goto oom;
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739

    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.
     */
1740
    ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
1741 1742
    ret->cpumaps.cpumaps_val = (char *) cpumaps;

1743
    VIR_FREE(info);
1744
    virDomainFree(dom);
1745
    return 0;
1746 1747 1748 1749 1750

oom:
    VIR_FREE(info);
    VIR_FREE(cpumaps);
    virDomainFree(dom);
1751 1752
    remoteDispatchOOMError(rerr);
    return -1;
1753 1754
}

E
Eric Blake 已提交
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
static int
remoteDispatchDomainGetVcpusFlags (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_vcpus_flags_args *args,
                                   remote_domain_get_vcpus_flags_ret *ret)
{
    virDomainPtr dom;

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

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

1782
static int
1783
remoteDispatchDomainMigratePrepare (struct qemud_server *server ATTRIBUTE_UNUSED,
1784 1785
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1786
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1787
                                    remote_error *rerr,
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
                                    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 ... */
1802
    if (VIR_ALLOC(uri_out) < 0) {
1803 1804
        remoteDispatchOOMError(rerr);
        return -1;
1805
    }
1806

1807
    r = virDomainMigratePrepare (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1808 1809
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1810
    if (r == -1) {
1811
        VIR_FREE(uri_out);
1812
        remoteDispatchConnError(rerr, conn);
D
Daniel P. Berrange 已提交
1813 1814
        return -1;
    }
1815 1816 1817 1818 1819 1820

    /* 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 已提交
1821 1822
    if (*uri_out == NULL) {
        ret->uri_out = NULL;
1823
        VIR_FREE(uri_out);
D
Daniel P. Berrange 已提交
1824 1825 1826
    } else {
        ret->uri_out = uri_out;
    }
1827 1828 1829 1830 1831

    return 0;
}

static int
1832
remoteDispatchDomainMigratePerform (struct qemud_server *server ATTRIBUTE_UNUSED,
1833 1834
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1835
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1836
                                    remote_error *rerr,
1837 1838 1839 1840 1841
                                    remote_domain_migrate_perform_args *args,
                                    void *ret ATTRIBUTE_UNUSED)
{
    int r;
    virDomainPtr dom;
1842
    char *dname;
1843

1844
    dom = get_nonnull_domain (conn, args->dom);
1845
    if (dom == NULL) {
1846
        remoteDispatchConnError(rerr, conn);
1847
        return -1;
1848 1849 1850 1851
    }

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

D
Daniel P. Berrange 已提交
1852 1853 1854 1855 1856
    r = virDomainMigratePerform (dom,
                                 args->cookie.cookie_val,
                                 args->cookie.cookie_len,
                                 args->uri,
                                 args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1857
    virDomainFree (dom);
1858
    if (r == -1) {
1859
        remoteDispatchConnError(rerr, conn);
1860 1861
        return -1;
    }
1862 1863 1864 1865 1866

    return 0;
}

static int
1867
remoteDispatchDomainMigrateFinish (struct qemud_server *server ATTRIBUTE_UNUSED,
1868 1869
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1870
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1871
                                   remote_error *rerr,
1872 1873 1874 1875 1876 1877
                                   remote_domain_migrate_finish_args *args,
                                   remote_domain_migrate_finish_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

1878
    ddom = virDomainMigrateFinish (conn, args->dname,
D
Daniel P. Berrange 已提交
1879 1880 1881 1882
                                   args->cookie.cookie_val,
                                   args->cookie.cookie_len,
                                   args->uri,
                                   args->flags);
1883
    if (ddom == NULL) {
1884
        remoteDispatchConnError(rerr, conn);
1885 1886
        return -1;
    }
1887 1888

    make_nonnull_domain (&ret->ddom, ddom);
D
Daniel P. Berrange 已提交
1889
    virDomainFree (ddom);
1890 1891 1892
    return 0;
}

D
Daniel Veillard 已提交
1893 1894
static int
remoteDispatchDomainMigratePrepare2 (struct qemud_server *server ATTRIBUTE_UNUSED,
1895 1896
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1897
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
1898
                                     remote_error *rerr,
D
Daniel Veillard 已提交
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
                                     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) {
1915 1916
        remoteDispatchOOMError(rerr);
        return -1;
D
Daniel Veillard 已提交
1917 1918
    }

1919
    r = virDomainMigratePrepare2 (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1920 1921 1922
                                  uri_in, uri_out,
                                  args->flags, dname, args->resource,
                                  args->dom_xml);
1923
    if (r == -1) {
1924
        remoteDispatchConnError(rerr, conn);
1925 1926
        return -1;
    }
D
Daniel Veillard 已提交
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939

    /* 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,
1940 1941
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1942
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1943
                                    remote_error *rerr,
D
Daniel Veillard 已提交
1944 1945 1946 1947 1948 1949
                                    remote_domain_migrate_finish2_args *args,
                                    remote_domain_migrate_finish2_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

1950
    ddom = virDomainMigrateFinish2 (conn, args->dname,
D
Daniel P. Berrange 已提交
1951 1952 1953 1954 1955
                                    args->cookie.cookie_val,
                                    args->cookie.cookie_len,
                                    args->uri,
                                    args->flags,
                                    args->retcode);
1956
    if (ddom == NULL) {
1957
        remoteDispatchConnError(rerr, conn);
1958 1959
        return -1;
    }
D
Daniel Veillard 已提交
1960 1961

    make_nonnull_domain (&ret->ddom, ddom);
1962
    virDomainFree (ddom);
D
Daniel Veillard 已提交
1963 1964 1965 1966

    return 0;
}

C
Chris Lalancette 已提交
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
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;
    }

1989
    r = virDomainMigratePrepareTunnel(conn, stream->st,
C
Chris Lalancette 已提交
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
                                      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;
}

2008
static int
2009
remoteDispatchListDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2010 2011
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2012
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2013
                                  remote_error *rerr,
2014 2015 2016 2017 2018
                                  remote_list_defined_domains_args *args,
                                  remote_list_defined_domains_ret *ret)
{

    if (args->maxnames > REMOTE_DOMAIN_NAME_LIST_MAX) {
2019 2020 2021
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_DOMAIN_NAME_LIST_MAX"));
        return -1;
2022 2023 2024
    }

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

    ret->names.names_len =
2031
        virConnectListDefinedDomains (conn,
2032
                                      ret->names.names_val, args->maxnames);
2033 2034
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
2035
        remoteDispatchConnError(rerr, conn);
2036 2037
        return -1;
    }
2038 2039 2040 2041 2042

    return 0;
}

static int
2043
remoteDispatchDomainLookupById (struct qemud_server *server ATTRIBUTE_UNUSED,
2044 2045
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2046
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
2047
                                remote_error *rerr,
2048 2049 2050 2051 2052
                                remote_domain_lookup_by_id_args *args,
                                remote_domain_lookup_by_id_ret *ret)
{
    virDomainPtr dom;

2053
    dom = virDomainLookupByID (conn, args->id);
2054
    if (dom == NULL) {
2055
        remoteDispatchConnError(rerr, conn);
2056 2057
        return -1;
    }
2058 2059

    make_nonnull_domain (&ret->dom, dom);
2060
    virDomainFree(dom);
2061 2062 2063 2064
    return 0;
}

static int
2065
remoteDispatchDomainLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
2066 2067
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2068
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2069
                                  remote_error *rerr,
2070 2071 2072 2073 2074
                                  remote_domain_lookup_by_name_args *args,
                                  remote_domain_lookup_by_name_ret *ret)
{
    virDomainPtr dom;

2075
    dom = virDomainLookupByName (conn, args->name);
2076
    if (dom == NULL) {
2077
        remoteDispatchConnError(rerr, conn);
2078 2079
        return -1;
    }
2080 2081

    make_nonnull_domain (&ret->dom, dom);
2082
    virDomainFree(dom);
2083 2084 2085 2086
    return 0;
}

static int
2087
remoteDispatchDomainLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
2088 2089
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2090
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2091
                                  remote_error *rerr,
2092 2093 2094 2095 2096
                                  remote_domain_lookup_by_uuid_args *args,
                                  remote_domain_lookup_by_uuid_ret *ret)
{
    virDomainPtr dom;

2097
    dom = virDomainLookupByUUID (conn, (unsigned char *) args->uuid);
2098
    if (dom == NULL) {
2099
        remoteDispatchConnError(rerr, conn);
2100 2101
        return -1;
    }
2102 2103

    make_nonnull_domain (&ret->dom, dom);
2104
    virDomainFree(dom);
2105 2106 2107 2108
    return 0;
}

static int
2109
remoteDispatchNumOfDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2110 2111
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2112
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2113
                                   remote_error *rerr,
2114 2115 2116 2117
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_num_of_defined_domains_ret *ret)
{

2118
    ret->num = virConnectNumOfDefinedDomains (conn);
2119
    if (ret->num == -1) {
2120
        remoteDispatchConnError(rerr, conn);
2121 2122
        return -1;
    }
2123 2124 2125 2126 2127

    return 0;
}

static int
2128
remoteDispatchDomainPinVcpu (struct qemud_server *server ATTRIBUTE_UNUSED,
2129 2130
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2131
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2132
                             remote_error *rerr,
2133 2134 2135 2136 2137 2138
                             remote_domain_pin_vcpu_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int rv;

2139
    dom = get_nonnull_domain (conn, args->dom);
2140
    if (dom == NULL) {
2141
        remoteDispatchConnError(rerr, conn);
2142
        return -1;
2143 2144 2145
    }

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
2146
        virDomainFree(dom);
2147 2148
        remoteDispatchFormatError (rerr, "%s", _("cpumap_len > REMOTE_CPUMAP_MAX"));
        return -1;
2149 2150 2151 2152 2153
    }

    rv = virDomainPinVcpu (dom, args->vcpu,
                           (unsigned char *) args->cpumap.cpumap_val,
                           args->cpumap.cpumap_len);
2154 2155
    if (rv == -1) {
        virDomainFree(dom);
2156
        remoteDispatchConnError(rerr, conn);
2157 2158 2159
        return -1;
    }
    virDomainFree(dom);
2160 2161 2162 2163
    return 0;
}

static int
2164
remoteDispatchDomainReboot (struct qemud_server *server ATTRIBUTE_UNUSED,
2165 2166
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2167
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2168
                            remote_error *rerr,
2169 2170 2171 2172 2173
                            remote_domain_reboot_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2174
    dom = get_nonnull_domain (conn, args->dom);
2175
    if (dom == NULL) {
2176
        remoteDispatchConnError(rerr, conn);
2177
        return -1;
2178 2179
    }

2180 2181
    if (virDomainReboot (dom, args->flags) == -1) {
        virDomainFree(dom);
2182
        remoteDispatchConnError(rerr, conn);
2183
        return -1;
2184 2185
    }
    virDomainFree(dom);
2186 2187 2188 2189
    return 0;
}

static int
2190
remoteDispatchDomainRestore (struct qemud_server *server ATTRIBUTE_UNUSED,
2191 2192
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2193
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2194
                             remote_error *rerr,
2195 2196 2197
                             remote_domain_restore_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
2198

2199
    if (virDomainRestore (conn, args->from) == -1) {
2200
        remoteDispatchConnError(rerr, conn);
2201
        return -1;
2202
    }
2203 2204 2205 2206 2207

    return 0;
}

static int
2208
remoteDispatchDomainResume (struct qemud_server *server ATTRIBUTE_UNUSED,
2209 2210
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2211
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2212
                            remote_error *rerr,
2213 2214 2215 2216 2217
                            remote_domain_resume_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2218
    dom = get_nonnull_domain (conn, args->dom);
2219
    if (dom == NULL) {
2220
        remoteDispatchConnError(rerr, conn);
2221
        return -1;
2222 2223
    }

2224
    if (virDomainResume (dom) == -1) {
2225
        virDomainFree(dom);
2226
        remoteDispatchConnError(rerr, conn);
2227
        return -1;
2228 2229
    }
    virDomainFree(dom);
2230 2231 2232 2233
    return 0;
}

static int
2234
remoteDispatchDomainSave (struct qemud_server *server ATTRIBUTE_UNUSED,
2235 2236
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
2237
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
2238
                          remote_error *rerr,
2239 2240 2241 2242 2243
                          remote_domain_save_args *args,
                          void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2244
    dom = get_nonnull_domain (conn, args->dom);
2245
    if (dom == NULL) {
2246
        remoteDispatchConnError(rerr, conn);
2247
        return -1;
2248 2249
    }

2250
    if (virDomainSave (dom, args->to) == -1) {
2251
        virDomainFree(dom);
2252
        remoteDispatchConnError(rerr, conn);
2253
        return -1;
2254 2255
    }
    virDomainFree(dom);
2256 2257 2258 2259
    return 0;
}

static int
2260
remoteDispatchDomainCoreDump (struct qemud_server *server ATTRIBUTE_UNUSED,
2261 2262
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2263
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2264
                              remote_error *rerr,
2265 2266 2267 2268 2269
                              remote_domain_core_dump_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2270
    dom = get_nonnull_domain (conn, args->dom);
2271
    if (dom == NULL) {
2272
        remoteDispatchConnError(rerr, conn);
2273
        return -1;
2274 2275
    }

2276
    if (virDomainCoreDump (dom, args->to, args->flags) == -1) {
2277
        virDomainFree(dom);
2278
        remoteDispatchConnError(rerr, conn);
2279
        return -1;
2280 2281
    }
    virDomainFree(dom);
2282 2283 2284 2285
    return 0;
}

static int
2286
remoteDispatchDomainSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
2287 2288
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2289
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2290
                                  remote_error *rerr,
2291 2292 2293 2294 2295
                                  remote_domain_set_autostart_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2296
    dom = get_nonnull_domain (conn, args->dom);
2297
    if (dom == NULL) {
2298
        remoteDispatchConnError(rerr, conn);
2299
        return -1;
2300 2301
    }

2302 2303
    if (virDomainSetAutostart (dom, args->autostart) == -1) {
        virDomainFree(dom);
2304
        remoteDispatchConnError(rerr, conn);
2305
        return -1;
2306 2307
    }
    virDomainFree(dom);
2308 2309 2310 2311
    return 0;
}

static int
2312
remoteDispatchDomainSetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
2313 2314
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2315
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2316
                                  remote_error *rerr,
2317 2318 2319 2320 2321
                                  remote_domain_set_max_memory_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2322
    dom = get_nonnull_domain (conn, args->dom);
2323
    if (dom == NULL) {
2324
        remoteDispatchConnError(rerr, conn);
2325
        return -1;
2326 2327
    }

2328 2329
    if (virDomainSetMaxMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
2330
        remoteDispatchConnError(rerr, conn);
2331
        return -1;
2332 2333
    }
    virDomainFree(dom);
2334 2335 2336 2337
    return 0;
}

static int
2338
remoteDispatchDomainSetMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
2339 2340
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
2341
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
2342
                               remote_error *rerr,
2343 2344 2345 2346 2347
                               remote_domain_set_memory_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2348
    dom = get_nonnull_domain (conn, args->dom);
2349
    if (dom == NULL) {
2350
        remoteDispatchConnError(rerr, conn);
2351
        return -1;
2352 2353
    }

2354 2355
    if (virDomainSetMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
2356
        remoteDispatchConnError(rerr, conn);
2357
        return -1;
2358 2359
    }
    virDomainFree(dom);
2360 2361 2362
    return 0;
}

2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 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 2566 2567 2568 2569 2570 2571 2572
static int
remoteDispatchDomainSetMemoryParameters(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_set_memory_parameters_args
                                        * args, void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int i, r, nparams;
    virMemoryParameterPtr params;
    unsigned int flags;

    nparams = args->params.params_len;
    flags = args->flags;

    if (nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
        remoteDispatchFormatError(rerr, "%s", _("nparams too large"));
        return -1;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
        remoteDispatchOOMError(rerr);
        return -1;
    }

    /* Deserialise parameters. */
    for (i = 0; i < nparams; ++i) {
        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;
        }
        params[i].type = args->params.params_val[i].value.type;
        switch (params[i].type) {
            case VIR_DOMAIN_MEMORY_PARAM_INT:
                params[i].value.i =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.i;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_UINT:
                params[i].value.ui =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.ui;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_LLONG:
                params[i].value.l =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.l;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_ULLONG:
                params[i].value.ul =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.ul;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_DOUBLE:
                params[i].value.d =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.d;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_BOOLEAN:
                params[i].value.b =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.b;
                break;
        }
    }

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

    r = virDomainSetMemoryParameters(dom, params, nparams, flags);
    virDomainFree(dom);
    VIR_FREE(params);
    if (r == -1) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    return 0;
}

static int
remoteDispatchDomainGetMemoryParameters(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_memory_parameters_args
                                        * args,
                                        remote_domain_get_memory_parameters_ret
                                        * ret)
{
    virDomainPtr dom;
    virMemoryParameterPtr params;
    int i, r, nparams;
    unsigned int flags;

    nparams = args->nparams;
    flags = args->flags;

    if (nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
        remoteDispatchFormatError(rerr, "%s", _("nparams too large"));
        return -1;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
        remoteDispatchOOMError(rerr);
        return -1;
    }

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

    r = virDomainGetMemoryParameters(dom, params, &nparams, flags);
    if (r == -1) {
        virDomainFree(dom);
        VIR_FREE(params);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

    /* Serialise the memory parameters. */
    ret->params.params_len = nparams;
    if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
        goto oom;

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

        ret->params.params_val[i].value.type = params[i].type;
        switch (params[i].type) {
            case VIR_DOMAIN_MEMORY_PARAM_INT:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.i =
                    params[i].value.i;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_UINT:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.ui =
                    params[i].value.ui;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_LLONG:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.l =
                    params[i].value.l;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_ULLONG:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.ul =
                    params[i].value.ul;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_DOUBLE:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.d =
                    params[i].value.d;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_BOOLEAN:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.b =
                    params[i].value.b;
                break;
            default:
                remoteDispatchFormatError(rerr, "%s", _("unknown type"));
                goto cleanup;
        }
    }

  success:
    virDomainFree(dom);
    VIR_FREE(params);

    return 0;

  oom:
    remoteDispatchOOMError(rerr);
  cleanup:
    virDomainFree(dom);
    for (i = 0; i < nparams; i++)
        VIR_FREE(ret->params.params_val[i].field);
    VIR_FREE(params);
    return -1;
}

2573
static int
2574
remoteDispatchDomainSetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
2575 2576
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2577
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2578
                              remote_error *rerr,
2579 2580 2581 2582 2583
                              remote_domain_set_vcpus_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2584
    dom = get_nonnull_domain (conn, args->dom);
2585
    if (dom == NULL) {
2586
        remoteDispatchConnError(rerr, conn);
2587
        return -1;
2588 2589
    }

2590 2591
    if (virDomainSetVcpus (dom, args->nvcpus) == -1) {
        virDomainFree(dom);
2592
        remoteDispatchConnError(rerr, conn);
2593
        return -1;
2594 2595
    }
    virDomainFree(dom);
2596 2597 2598
    return 0;
}

E
Eric Blake 已提交
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
static int
remoteDispatchDomainSetVcpusFlags (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_set_vcpus_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 (virDomainSetVcpusFlags (dom, args->nvcpus, args->flags) == -1) {
        virDomainFree(dom);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    virDomainFree(dom);
    return 0;
}

2625
static int
2626
remoteDispatchDomainShutdown (struct qemud_server *server ATTRIBUTE_UNUSED,
2627 2628
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2629
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2630
                              remote_error *rerr,
2631 2632 2633 2634 2635
                              remote_domain_shutdown_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2636
    dom = get_nonnull_domain (conn, args->dom);
2637
    if (dom == NULL) {
2638
        remoteDispatchConnError(rerr, conn);
2639
        return -1;
2640 2641
    }

2642 2643
    if (virDomainShutdown (dom) == -1) {
        virDomainFree(dom);
2644
        remoteDispatchConnError(rerr, conn);
2645
        return -1;
2646 2647
    }
    virDomainFree(dom);
2648 2649 2650 2651
    return 0;
}

static int
2652
remoteDispatchDomainSuspend (struct qemud_server *server ATTRIBUTE_UNUSED,
2653 2654
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2655
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2656
                             remote_error *rerr,
2657 2658 2659 2660 2661
                             remote_domain_suspend_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2662
    dom = get_nonnull_domain (conn, args->dom);
2663
    if (dom == NULL) {
2664
        remoteDispatchConnError(rerr, conn);
2665
        return -1;
2666 2667
    }

2668
    if (virDomainSuspend (dom) == -1) {
2669
        virDomainFree(dom);
2670
        remoteDispatchConnError(rerr, conn);
2671
        return -1;
2672 2673
    }
    virDomainFree(dom);
2674 2675 2676 2677
    return 0;
}

static int
2678
remoteDispatchDomainUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
2679 2680
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2681
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2682
                              remote_error *rerr,
2683 2684 2685 2686 2687
                              remote_domain_undefine_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2688
    dom = get_nonnull_domain (conn, args->dom);
2689
    if (dom == NULL) {
2690
        remoteDispatchConnError(rerr, conn);
2691
        return -1;
2692 2693
    }

2694 2695
    if (virDomainUndefine (dom) == -1) {
        virDomainFree(dom);
2696
        remoteDispatchConnError(rerr, conn);
2697
        return -1;
2698 2699
    }
    virDomainFree(dom);
2700 2701 2702 2703
    return 0;
}

static int
2704
remoteDispatchListDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2705 2706
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2707
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2708
                                   remote_error *rerr,
2709 2710 2711 2712 2713
                                   remote_list_defined_networks_args *args,
                                   remote_list_defined_networks_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
2714 2715 2716
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
2717 2718 2719
    }

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

    ret->names.names_len =
2726
        virConnectListDefinedNetworks (conn,
2727
                                       ret->names.names_val, args->maxnames);
2728 2729
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
2730
        remoteDispatchConnError(rerr, conn);
2731 2732
        return -1;
    }
2733 2734 2735 2736 2737

    return 0;
}

static int
2738
remoteDispatchListDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2739 2740
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
2741
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
2742
                           remote_error *rerr,
2743 2744 2745 2746 2747
                           remote_list_domains_args *args,
                           remote_list_domains_ret *ret)
{

    if (args->maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
2748 2749 2750
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxids > REMOTE_DOMAIN_ID_LIST_MAX"));
        return -1;
2751 2752 2753
    }

    /* Allocate return buffer. */
2754
    if (VIR_ALLOC_N(ret->ids.ids_val, args->maxids) < 0) {
2755 2756
        remoteDispatchOOMError(rerr);
        return -1;
2757
    }
2758

2759
    ret->ids.ids_len = virConnectListDomains (conn,
2760
                                              ret->ids.ids_val, args->maxids);
2761 2762
    if (ret->ids.ids_len == -1) {
        VIR_FREE(ret->ids.ids_val);
2763
        remoteDispatchConnError(rerr, conn);
2764 2765
        return -1;
    }
2766 2767 2768 2769

    return 0;
}

2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
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;
    }

2787
    if (virDomainManagedSave (dom, args->flags) == -1) {
2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
        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;
}

2849
static int
2850
remoteDispatchListNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2851 2852
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2853
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2854
                            remote_error *rerr,
2855 2856 2857 2858 2859
                            remote_list_networks_args *args,
                            remote_list_networks_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
2860 2861 2862
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
2863 2864 2865
    }

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

    ret->names.names_len =
2872
        virConnectListNetworks (conn,
2873
                                ret->names.names_val, args->maxnames);
2874 2875
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_len);
2876
        remoteDispatchConnError(rerr, conn);
2877 2878
        return -1;
    }
2879 2880 2881 2882 2883

    return 0;
}

static int
2884
remoteDispatchNetworkCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
2885 2886
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2887
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2888
                             remote_error *rerr,
2889 2890 2891 2892 2893
                             remote_network_create_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2894
    net = get_nonnull_network (conn, args->net);
2895
    if (net == NULL) {
2896
        remoteDispatchConnError(rerr, conn);
2897
        return -1;
2898 2899
    }

2900 2901
    if (virNetworkCreate (net) == -1) {
        virNetworkFree(net);
2902
        remoteDispatchConnError(rerr, conn);
2903
        return -1;
2904 2905
    }
    virNetworkFree(net);
2906 2907 2908 2909
    return 0;
}

static int
2910
remoteDispatchNetworkCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2911 2912
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2913
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
2914
                                remote_error *rerr,
2915 2916 2917 2918 2919
                                remote_network_create_xml_args *args,
                                remote_network_create_xml_ret *ret)
{
    virNetworkPtr net;

2920
    net = virNetworkCreateXML (conn, args->xml);
2921
    if (net == NULL) {
2922
        remoteDispatchConnError(rerr, conn);
2923 2924
        return -1;
    }
2925 2926

    make_nonnull_network (&ret->net, net);
2927
    virNetworkFree(net);
2928 2929 2930 2931
    return 0;
}

static int
2932
remoteDispatchNetworkDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2933 2934
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2935
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
2936
                                remote_error *rerr,
2937 2938 2939 2940 2941
                                remote_network_define_xml_args *args,
                                remote_network_define_xml_ret *ret)
{
    virNetworkPtr net;

2942
    net = virNetworkDefineXML (conn, args->xml);
2943
    if (net == NULL) {
2944
        remoteDispatchConnError(rerr, conn);
2945 2946
        return -1;
    }
2947 2948

    make_nonnull_network (&ret->net, net);
2949
    virNetworkFree(net);
2950 2951 2952 2953
    return 0;
}

static int
2954
remoteDispatchNetworkDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
2955 2956
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2957
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2958
                              remote_error *rerr,
2959 2960 2961 2962 2963
                              remote_network_destroy_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2964
    net = get_nonnull_network (conn, args->net);
2965
    if (net == NULL) {
2966
        remoteDispatchConnError(rerr, conn);
2967
        return -1;
2968 2969
    }

2970 2971
    if (virNetworkDestroy (net) == -1) {
        virNetworkFree(net);
2972
        remoteDispatchConnError(rerr, conn);
2973
        return -1;
2974 2975
    }
    virNetworkFree(net);
2976 2977 2978 2979
    return 0;
}

static int
2980
remoteDispatchNetworkDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2981 2982
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2983
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2984
                              remote_error *rerr,
2985 2986 2987 2988 2989
                              remote_network_dump_xml_args *args,
                              remote_network_dump_xml_ret *ret)
{
    virNetworkPtr net;

2990
    net = get_nonnull_network (conn, args->net);
2991
    if (net == NULL) {
2992
        remoteDispatchConnError(rerr, conn);
2993
        return -1;
2994 2995 2996 2997
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNetworkGetXMLDesc (net, args->flags);
2998 2999
    if (!ret->xml) {
        virNetworkFree(net);
3000
        remoteDispatchConnError(rerr, conn);
3001 3002 3003
        return -1;
    }
    virNetworkFree(net);
3004 3005 3006 3007
    return 0;
}

static int
3008
remoteDispatchNetworkGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
3009 3010
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3011
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
3012
                                   remote_error *rerr,
3013 3014 3015 3016 3017
                                   remote_network_get_autostart_args *args,
                                   remote_network_get_autostart_ret *ret)
{
    virNetworkPtr net;

3018
    net = get_nonnull_network (conn, args->net);
3019
    if (net == NULL) {
3020
        remoteDispatchConnError(rerr, conn);
3021
        return -1;
3022 3023
    }

3024 3025
    if (virNetworkGetAutostart (net, &ret->autostart) == -1) {
        virNetworkFree(net);
3026
        remoteDispatchConnError(rerr, conn);
3027
        return -1;
3028 3029
    }
    virNetworkFree(net);
3030 3031 3032 3033
    return 0;
}

static int
3034
remoteDispatchNetworkGetBridgeName (struct qemud_server *server ATTRIBUTE_UNUSED,
3035 3036
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
3037
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
3038
                                    remote_error *rerr,
3039 3040 3041 3042 3043
                                    remote_network_get_bridge_name_args *args,
                                    remote_network_get_bridge_name_ret *ret)
{
    virNetworkPtr net;

3044
    net = get_nonnull_network (conn, args->net);
3045
    if (net == NULL) {
3046
        remoteDispatchConnError(rerr, conn);
3047
        return -1;
3048 3049 3050 3051
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virNetworkGetBridgeName (net);
3052 3053
    if (!ret->name) {
        virNetworkFree(net);
3054
        remoteDispatchConnError(rerr, conn);
3055 3056 3057
        return -1;
    }
    virNetworkFree(net);
3058 3059 3060 3061
    return 0;
}

static int
3062
remoteDispatchNetworkLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
3063 3064
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3065
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
3066
                                   remote_error *rerr,
3067 3068 3069 3070 3071
                                   remote_network_lookup_by_name_args *args,
                                   remote_network_lookup_by_name_ret *ret)
{
    virNetworkPtr net;

3072
    net = virNetworkLookupByName (conn, args->name);
3073
    if (net == NULL) {
3074
        remoteDispatchConnError(rerr, conn);
3075 3076
        return -1;
    }
3077 3078

    make_nonnull_network (&ret->net, net);
3079
    virNetworkFree(net);
3080 3081 3082 3083
    return 0;
}

static int
3084
remoteDispatchNetworkLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
3085 3086
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3087
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
3088
                                   remote_error *rerr,
3089 3090 3091 3092 3093
                                   remote_network_lookup_by_uuid_args *args,
                                   remote_network_lookup_by_uuid_ret *ret)
{
    virNetworkPtr net;

3094
    net = virNetworkLookupByUUID (conn, (unsigned char *) args->uuid);
3095
    if (net == NULL) {
3096
        remoteDispatchConnError(rerr, conn);
3097 3098
        return -1;
    }
3099 3100

    make_nonnull_network (&ret->net, net);
3101
    virNetworkFree(net);
3102 3103 3104 3105
    return 0;
}

static int
3106
remoteDispatchNetworkSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
3107 3108
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3109
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
3110
                                   remote_error *rerr,
3111 3112 3113 3114 3115
                                   remote_network_set_autostart_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

3116
    net = get_nonnull_network (conn, args->net);
3117
    if (net == NULL) {
3118
        remoteDispatchConnError(rerr, conn);
3119
        return -1;
3120 3121
    }

3122 3123
    if (virNetworkSetAutostart (net, args->autostart) == -1) {
        virNetworkFree(net);
3124
        remoteDispatchConnError(rerr, conn);
3125
        return -1;
3126 3127
    }
    virNetworkFree(net);
3128 3129 3130 3131
    return 0;
}

static int
3132
remoteDispatchNetworkUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
3133 3134
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
3135
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
3136
                               remote_error *rerr,
3137 3138 3139 3140 3141
                               remote_network_undefine_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

3142
    net = get_nonnull_network (conn, args->net);
3143
    if (net == NULL) {
3144
        remoteDispatchConnError(rerr, conn);
3145
        return -1;
3146 3147
    }

3148 3149
    if (virNetworkUndefine (net) == -1) {
        virNetworkFree(net);
3150
        remoteDispatchConnError(rerr, conn);
3151
        return -1;
3152 3153
    }
    virNetworkFree(net);
3154 3155 3156 3157
    return 0;
}

static int
3158
remoteDispatchNumOfDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
3159 3160
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
3161
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
3162
                                    remote_error *rerr,
3163 3164 3165 3166
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_num_of_defined_networks_ret *ret)
{

3167
    ret->num = virConnectNumOfDefinedNetworks (conn);
3168
    if (ret->num == -1) {
3169
        remoteDispatchConnError(rerr, conn);
3170 3171
        return -1;
    }
3172 3173 3174 3175 3176

    return 0;
}

static int
3177
remoteDispatchNumOfDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
3178 3179
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
3180
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3181
                            remote_error *rerr,
3182 3183 3184 3185
                            void *args ATTRIBUTE_UNUSED,
                            remote_num_of_domains_ret *ret)
{

3186
    ret->num = virConnectNumOfDomains (conn);
3187
    if (ret->num == -1) {
3188
        remoteDispatchConnError(rerr, conn);
3189 3190
        return -1;
    }
3191 3192 3193 3194 3195

    return 0;
}

static int
3196
remoteDispatchNumOfNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
3197 3198
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
3199
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
3200
                             remote_error *rerr,
3201 3202 3203 3204
                             void *args ATTRIBUTE_UNUSED,
                             remote_num_of_networks_ret *ret)
{

3205
    ret->num = virConnectNumOfNetworks (conn);
3206
    if (ret->num == -1) {
3207
        remoteDispatchConnError(rerr, conn);
3208 3209
        return -1;
    }
3210 3211 3212 3213

    return 0;
}

3214

D
Daniel Veillard 已提交
3215 3216 3217 3218 3219
/*-------------------------------------------------------------*/
static int
remoteDispatchNumOfInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
3220
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238
                               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,
3239
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268
                              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;
}

3269 3270 3271 3272
static int
remoteDispatchNumOfDefinedInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
3273
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291
                                      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,
3292
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321
                                     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 已提交
3322 3323 3324 3325
static int
remoteDispatchInterfaceLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
3326
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3327 3328 3329 3330
                                     remote_error *rerr,
                                     remote_interface_lookup_by_name_args *args,
                                     remote_interface_lookup_by_name_ret *ret)
{
3331
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3332

3333 3334
    iface = virInterfaceLookupByName (conn, args->name);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3335 3336 3337 3338
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3339 3340
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3341 3342 3343 3344 3345 3346 3347
    return 0;
}

static int
remoteDispatchInterfaceLookupByMacString (struct qemud_server *server ATTRIBUTE_UNUSED,
                                          struct qemud_client *client ATTRIBUTE_UNUSED,
                                          virConnectPtr conn,
3348
                                          remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3349 3350 3351 3352
                                          remote_error *rerr,
                                          remote_interface_lookup_by_mac_string_args *args,
                                          remote_interface_lookup_by_mac_string_ret *ret)
{
3353
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3354

3355 3356
    iface = virInterfaceLookupByMACString (conn, args->mac);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3357 3358 3359 3360
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3361 3362
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3363 3364 3365 3366 3367 3368 3369
    return 0;
}

static int
remoteDispatchInterfaceGetXmlDesc (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3370
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3371 3372 3373 3374
                                   remote_error *rerr,
                                   remote_interface_get_xml_desc_args *args,
                                   remote_interface_get_xml_desc_ret *ret)
{
3375
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3376

3377 3378
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3379 3380 3381 3382 3383
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    /* remoteDispatchClientRequest will free this. */
3384
    ret->xml = virInterfaceGetXMLDesc (iface, args->flags);
D
Daniel Veillard 已提交
3385
    if (!ret->xml) {
3386
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3387 3388 3389
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3390
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3391 3392 3393 3394 3395 3396 3397
    return 0;
}

static int
remoteDispatchInterfaceDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3398
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3399 3400 3401 3402
                                  remote_error *rerr,
                                  remote_interface_define_xml_args *args,
                                  remote_interface_define_xml_ret *ret)
{
3403
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3404

3405 3406
    iface = virInterfaceDefineXML (conn, args->xml, args->flags);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3407 3408 3409 3410
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3411 3412
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3413 3414 3415 3416 3417
    return 0;
}

static int
remoteDispatchInterfaceUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
3418 3419 3420 3421 3422 3423
                                 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 已提交
3424
{
3425
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3426

3427 3428
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3429 3430 3431 3432
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3433 3434
    if (virInterfaceUndefine (iface) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3435 3436 3437
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3438
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3439 3440 3441 3442 3443
    return 0;
}

static int
remoteDispatchInterfaceCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
3444 3445 3446 3447 3448 3449
                               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 已提交
3450
{
3451
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3452

3453 3454
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3455 3456 3457 3458
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3459 3460
    if (virInterfaceCreate (iface, args->flags) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3461 3462 3463
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3464
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3465 3466 3467 3468 3469
    return 0;
}

static int
remoteDispatchInterfaceDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
3470 3471 3472 3473 3474 3475
                                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 已提交
3476
{
3477
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3478

3479 3480
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3481 3482 3483 3484
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3485 3486
    if (virInterfaceDestroy (iface, args->flags) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3487 3488 3489
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3490
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3491 3492 3493 3494 3495
    return 0;
}

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

3496
static int
3497
remoteDispatchAuthList (struct qemud_server *server,
3498
                        struct qemud_client *client,
3499
                        virConnectPtr conn ATTRIBUTE_UNUSED,
3500
                        remote_message_header *hdr ATTRIBUTE_UNUSED,
3501
                        remote_error *rerr,
3502 3503 3504 3505
                        void *args ATTRIBUTE_UNUSED,
                        remote_auth_list_ret *ret)
{
    ret->types.types_len = 1;
3506
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
3507 3508
        remoteDispatchOOMError(rerr);
        return -1;
3509
    }
3510 3511 3512
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3513
    ret->types.types_val[0] = client->auth;
3514
    virMutexUnlock(&client->lock);
3515

3516 3517 3518 3519 3520 3521 3522
    return 0;
}


#if HAVE_SASL
/*
 * Initializes the SASL session in prepare for authentication
3523
 * and gives the client a list of allowed mechanisms to choose
3524 3525 3526 3527
 *
 * XXX callbacks for stuff like password verification ?
 */
static int
3528
remoteDispatchAuthSaslInit (struct qemud_server *server,
3529
                            struct qemud_client *client,
3530
                            virConnectPtr conn,
3531
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3532
                            remote_error *rerr,
3533 3534 3535 3536
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret)
{
    const char *mechlist = NULL;
3537
    sasl_security_properties_t secprops;
3538
    int err;
3539
    virSocketAddr sa;
3540 3541
    char *localAddr, *remoteAddr;

3542 3543 3544
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3545

3546 3547 3548
    REMOTE_DEBUG("Initialize SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn != NULL) {
3549
        VIR_ERROR0(_("client tried invalid SASL init request"));
3550
        goto authfail;
3551 3552 3553
    }

    /* Get local address in form  IPADDR:PORT */
3554 3555
    sa.len = sizeof(sa.data.stor);
    if (getsockname(client->fd, &sa.data.sa, &sa.len) < 0) {
3556
        char ebuf[1024];
3557
        remoteDispatchFormatError(rerr,
3558
                                  _("failed to get sock address: %s"),
3559
                                  virStrerror(errno, ebuf, sizeof ebuf));
3560
        goto error;
3561
    }
3562 3563
    if ((localAddr = virSocketFormatAddrFull(&sa, true, ";")) == NULL) {
        remoteDispatchConnError(rerr, conn);
3564
        goto error;
3565 3566 3567
    }

    /* Get remote address in form  IPADDR:PORT */
3568 3569
    sa.len = sizeof(sa.data.stor);
    if (getpeername(client->fd, &sa.data.sa, &sa.len) < 0) {
3570
        char ebuf[1024];
3571
        remoteDispatchFormatError(rerr, _("failed to get peer address: %s"),
3572
                                  virStrerror(errno, ebuf, sizeof ebuf));
3573
        VIR_FREE(localAddr);
3574
        goto error;
3575
    }
3576
    if ((remoteAddr = virSocketFormatAddrFull(&sa, true, ";")) == NULL) {
3577
        VIR_FREE(localAddr);
3578
        remoteDispatchConnError(rerr, conn);
3579
        goto error;
3580 3581 3582 3583 3584 3585 3586 3587 3588 3589
    }

    err = sasl_server_new("libvirt",
                          NULL, /* FQDN - just delegates to gethostname */
                          NULL, /* User realm */
                          localAddr,
                          remoteAddr,
                          NULL, /* XXX Callbacks */
                          SASL_SUCCESS_DATA,
                          &client->saslconn);
3590 3591
    VIR_FREE(localAddr);
    VIR_FREE(remoteAddr);
3592
    if (err != SASL_OK) {
3593 3594
        VIR_ERROR(_("sasl context setup failed %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3595
        client->saslconn = NULL;
3596
        goto authfail;
3597 3598
    }

3599 3600 3601 3602 3603 3604 3605
    /* 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 已提交
3606
            VIR_ERROR0(_("cannot get TLS cipher size"));
3607 3608
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
3609
            goto authfail;
3610 3611 3612 3613 3614
        }
        ssf *= 8; /* tls key size is bytes, sasl wants bits */

        err = sasl_setprop(client->saslconn, SASL_SSF_EXTERNAL, &ssf);
        if (err != SASL_OK) {
3615 3616
            VIR_ERROR(_("cannot set SASL external SSF %d (%s)"),
                      err, sasl_errstring(err, NULL, NULL));
3617 3618
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
3619
            goto authfail;
3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642
        }
    }

    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) {
3643 3644
        VIR_ERROR(_("cannot set SASL security props %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3645 3646
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3647
        goto authfail;
3648 3649
    }

3650 3651 3652 3653 3654 3655 3656 3657 3658
    err = sasl_listmech(client->saslconn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
3659 3660
        VIR_ERROR(_("cannot list SASL mechanisms %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3661 3662
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3663
        goto authfail;
3664 3665 3666 3667
    }
    REMOTE_DEBUG("Available mechanisms for client: '%s'", mechlist);
    ret->mechlist = strdup(mechlist);
    if (!ret->mechlist) {
3668
        VIR_ERROR0(_("cannot allocate mechlist"));
3669 3670
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3671
        goto authfail;
3672 3673
    }

3674
    virMutexUnlock(&client->lock);
3675
    return 0;
3676 3677 3678 3679

authfail:
    remoteDispatchAuthError(rerr);
error:
3680
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
3681
    virMutexUnlock(&client->lock);
3682
    return -1;
3683 3684 3685
}


3686
/* We asked for an SSF layer, so sanity check that we actually
3687 3688 3689
 * got what we asked for
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
3690 3691
static int
remoteSASLCheckSSF (struct qemud_client *client,
3692
                    remote_error *rerr) {
3693 3694 3695 3696 3697 3698 3699 3700 3701
    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) {
3702 3703
        VIR_ERROR(_("cannot query SASL ssf on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3704
        remoteDispatchAuthError(rerr);
3705 3706 3707 3708 3709 3710 3711
        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 */
3712
        VIR_ERROR(_("negotiated SSF %d was not strong enough"), ssf);
3713
        remoteDispatchAuthError(rerr);
3714 3715
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3716
        return -2;
3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730
    }

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

3731 3732 3733
/*
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
3734 3735 3736
static int
remoteSASLCheckAccess (struct qemud_server *server,
                       struct qemud_client *client,
3737
                       remote_error *rerr) {
3738 3739 3740 3741 3742 3743
    const void *val;
    int err;
    char **wildcards;

    err = sasl_getprop(client->saslconn, SASL_USERNAME, &val);
    if (err != SASL_OK) {
3744 3745
        VIR_ERROR(_("cannot query SASL username on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3746
        remoteDispatchAuthError(rerr);
3747 3748 3749 3750 3751
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    if (val == NULL) {
3752
        VIR_ERROR0(_("no client username was found"));
3753
        remoteDispatchAuthError(rerr);
3754 3755 3756 3757 3758 3759 3760 3761
        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) {
3762
        VIR_ERROR0(_("out of memory copying username"));
3763
        remoteDispatchAuthError(rerr);
3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780
        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 */
3781
    VIR_ERROR(_("SASL client %s not allowed in whitelist"), client->saslUsername);
3782
    remoteDispatchAuthError(rerr);
3783 3784
    sasl_dispose(&client->saslconn);
    client->saslconn = NULL;
3785
    return -2;
3786 3787 3788
}


3789 3790 3791 3792
/*
 * This starts the SASL authentication negotiation.
 */
static int
3793 3794
remoteDispatchAuthSaslStart (struct qemud_server *server,
                             struct qemud_client *client,
3795
                             virConnectPtr conn ATTRIBUTE_UNUSED,
3796
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
3797
                             remote_error *rerr,
3798 3799 3800 3801 3802 3803 3804
                             remote_auth_sasl_start_args *args,
                             remote_auth_sasl_start_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

3805 3806 3807
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3808

3809 3810 3811
    REMOTE_DEBUG("Start SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
3812
        VIR_ERROR0(_("client tried invalid SASL start request"));
3813
        goto authfail;
3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826
    }

    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) {
3827 3828
        VIR_ERROR(_("sasl start failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3829 3830
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3831
        goto authfail;
3832 3833
    }
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3834
        VIR_ERROR(_("sasl start reply data too long %d"), serveroutlen);
3835 3836
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3837
        goto authfail;
3838 3839 3840 3841
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3842
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
3843
            remoteDispatchOOMError(rerr);
3844
            goto error;
3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856
        }
        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 {
3857
        /* Check username whitelist ACL */
3858 3859 3860 3861 3862 3863 3864
        if ((err = remoteSASLCheckAccess(server, client, rerr)) < 0 ||
            (err = remoteSASLCheckSSF(client, rerr)) < 0) {
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3865

3866
        REMOTE_DEBUG("Authentication successful %d", client->fd);
3867 3868
        PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
              client->fd, REMOTE_AUTH_SASL, client->saslUsername);
3869 3870 3871 3872
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

3873
    virMutexUnlock(&client->lock);
3874
    return 0;
3875 3876

authfail:
3877
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
3878
    remoteDispatchAuthError(rerr);
3879 3880 3881
    goto error;

authdeny:
3882 3883
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_SASL, client->saslUsername);
3884 3885
    goto error;

3886
error:
3887
    virMutexUnlock(&client->lock);
3888
    return -1;
3889 3890 3891 3892
}


static int
3893 3894
remoteDispatchAuthSaslStep (struct qemud_server *server,
                            struct qemud_client *client,
3895
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3896
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3897
                            remote_error *rerr,
3898 3899 3900 3901 3902 3903 3904
                            remote_auth_sasl_step_args *args,
                            remote_auth_sasl_step_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

3905 3906 3907
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3908

3909 3910 3911
    REMOTE_DEBUG("Step SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
3912
        VIR_ERROR0(_("client tried invalid SASL start request"));
3913
        goto authfail;
3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925
    }

    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) {
3926 3927
        VIR_ERROR(_("sasl step failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3928 3929
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3930
        goto authfail;
3931 3932 3933
    }

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3934 3935
        VIR_ERROR(_("sasl step reply data too long %d"),
                  serveroutlen);
3936 3937
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3938
        goto authfail;
3939 3940 3941 3942
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3943
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
3944
            remoteDispatchOOMError(rerr);
3945
            goto error;
3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957
        }
        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 {
3958
        /* Check username whitelist ACL */
3959 3960 3961 3962 3963 3964 3965
        if ((err = remoteSASLCheckAccess(server, client, rerr)) < 0 ||
            (err = remoteSASLCheckSSF(client, rerr)) < 0) {
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3966

3967
        REMOTE_DEBUG("Authentication successful %d", client->fd);
3968 3969
        PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
              client->fd, REMOTE_AUTH_SASL, client->saslUsername);
3970 3971 3972 3973
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

3974
    virMutexUnlock(&client->lock);
3975
    return 0;
3976 3977

authfail:
3978
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
3979
    remoteDispatchAuthError(rerr);
3980 3981 3982
    goto error;

authdeny:
3983 3984
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_SASL, client->saslUsername);
3985 3986
    goto error;

3987
error:
3988
    virMutexUnlock(&client->lock);
3989
    return -1;
3990 3991 3992 3993 3994
}


#else /* HAVE_SASL */
static int
3995
remoteDispatchAuthSaslInit (struct qemud_server *server ATTRIBUTE_UNUSED,
3996 3997
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3998
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3999
                            remote_error *rerr,
4000 4001 4002
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
4003
    VIR_ERROR0(_("client tried unsupported SASL init request"));
4004
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
4005
    remoteDispatchAuthError(rerr);
4006 4007 4008 4009
    return -1;
}

static int
4010
remoteDispatchAuthSaslStart (struct qemud_server *server ATTRIBUTE_UNUSED,
4011 4012
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn ATTRIBUTE_UNUSED,
4013
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
4014
                             remote_error *rerr,
4015 4016 4017
                             remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                             remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
4018
    VIR_ERROR0(_("client tried unsupported SASL start request"));
4019
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
4020
    remoteDispatchAuthError(rerr);
4021 4022 4023 4024
    return -1;
}

static int
4025
remoteDispatchAuthSaslStep (struct qemud_server *server ATTRIBUTE_UNUSED,
4026 4027
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
4028
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
4029
                            remote_error *rerr,
4030 4031 4032
                            remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
4033
    VIR_ERROR0(_("client tried unsupported SASL step request"));
4034
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
4035
    remoteDispatchAuthError(rerr);
4036 4037 4038 4039 4040
    return -1;
}
#endif /* HAVE_SASL */


4041 4042 4043 4044 4045
#if HAVE_POLKIT1
static int
remoteDispatchAuthPolkit (struct qemud_server *server,
                          struct qemud_client *client,
                          virConnectPtr conn ATTRIBUTE_UNUSED,
4046
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
4047 4048 4049 4050
                          remote_error *rerr,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
4051 4052
    pid_t callerPid = -1;
    uid_t callerUid = -1;
4053 4054 4055
    const char *action;
    int status = -1;
    char pidbuf[50];
4056
    char ident[100];
4057 4058
    int rv;

4059 4060
    memset(ident, 0, sizeof ident);

4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092
    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);
4093
        goto authfail;
4094 4095
    }

4096 4097 4098 4099 4100 4101
    rv = snprintf(ident, sizeof ident, "pid:%d,uid:%d", callerPid, callerUid);
    if (rv < 0 || rv >= sizeof ident) {
        VIR_ERROR(_("Caller identity was too large %d:%d"), callerPid, callerUid);
        goto authfail;
    }

4102
    if (virRun(pkcheck, &status) < 0) {
4103
        VIR_ERROR(_("Cannot invoke %s"), PKCHECK_PATH);
4104
        goto authfail;
4105 4106
    }
    if (status != 0) {
4107
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d, result: %d"),
4108
                  action, callerPid, callerUid, status);
4109
        goto authdeny;
4110
    }
4111
    PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
M
Matthias Bolte 已提交
4112
          client->fd, REMOTE_AUTH_POLKIT, (char *)ident);
4113 4114 4115 4116 4117 4118 4119 4120 4121
    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:
4122
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_POLKIT);
4123 4124 4125
    goto error;

authdeny:
4126
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
M
Matthias Bolte 已提交
4127
          client->fd, REMOTE_AUTH_POLKIT, (char *)ident);
4128 4129 4130
    goto error;

error:
4131 4132 4133 4134 4135
    remoteDispatchAuthError(rerr);
    virMutexUnlock(&client->lock);
    return -1;
}
#elif HAVE_POLKIT0
4136
static int
4137
remoteDispatchAuthPolkit (struct qemud_server *server,
4138
                          struct qemud_client *client,
4139
                          virConnectPtr conn ATTRIBUTE_UNUSED,
4140
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
4141
                          remote_error *rerr,
4142 4143 4144 4145 4146
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
    pid_t callerPid;
    uid_t callerUid;
4147 4148 4149 4150 4151 4152
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
4153
    const char *action;
4154
    char ident[100];
J
Jim Fehlig 已提交
4155
    int rv;
4156 4157

    memset(ident, 0, sizeof ident);
4158

4159 4160 4161
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
4162 4163

    action = client->readonly ?
4164 4165
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
4166 4167 4168

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

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

4178 4179 4180 4181 4182 4183
    rv = snprintf(ident, sizeof ident, "pid:%d,uid:%d", callerPid, callerUid);
    if (rv < 0 || rv >= sizeof ident) {
        VIR_ERROR(_("Caller identity was too large %d:%d"), callerPid, callerUid);
        goto authfail;
    }

4184
    VIR_INFO(_("Checking PID %d running as %d"), callerPid, callerUid);
4185 4186 4187
    dbus_error_init(&err);
    if (!(pkcaller = polkit_caller_new_from_pid(server->sysbus,
                                                callerPid, &err))) {
4188
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
4189
        dbus_error_free(&err);
4190
        goto authfail;
4191
    }
4192

4193
    if (!(pkaction = polkit_action_new())) {
4194
        char ebuf[1024];
4195
        VIR_ERROR(_("Failed to create polkit action %s"),
4196
                  virStrerror(errno, ebuf, sizeof ebuf));
4197
        polkit_caller_unref(pkcaller);
4198
        goto authfail;
4199 4200 4201 4202 4203
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
4204
        char ebuf[1024];
4205
        VIR_ERROR(_("Failed to create polkit context %s"),
4206
                  (pkerr ? polkit_error_get_error_message(pkerr)
4207
                   : virStrerror(errno, ebuf, sizeof ebuf)));
4208 4209 4210 4211 4212
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
4213
        goto authfail;
4214
    }
4215

4216
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
4217 4218 4219 4220 4221 4222
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
4223 4224 4225
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
4226
        goto authfail;
4227
    }
4228
# else
4229 4230 4231
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
4232
# endif
4233 4234 4235 4236
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
4237
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d, result: %s"),
4238 4239
                  action, callerPid, callerUid,
                  polkit_result_to_string_representation(pkresult));
4240
        goto authdeny;
4241
    }
4242 4243
    PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_POLKIT, ident);
4244
    VIR_INFO(_("Policy allowed action %s from pid %d, uid %d, result %s"),
4245 4246 4247 4248
             action, callerPid, callerUid,
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;
4249

4250
    virMutexUnlock(&client->lock);
4251
    return 0;
4252 4253

authfail:
4254
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_POLKIT);
4255 4256 4257
    goto error;

authdeny:
4258 4259
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_POLKIT, ident);
4260 4261 4262
    goto error;

error:
4263
    remoteDispatchAuthError(rerr);
4264
    virMutexUnlock(&client->lock);
4265
    return -1;
4266 4267
}

4268
#else /* !HAVE_POLKIT0 & !HAVE_POLKIT1*/
4269 4270

static int
4271
remoteDispatchAuthPolkit (struct qemud_server *server ATTRIBUTE_UNUSED,
4272
                          struct qemud_client *client ATTRIBUTE_UNUSED,
4273
                          virConnectPtr conn ATTRIBUTE_UNUSED,
4274
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
4275 4276 4277
                          remote_error *rerr,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
4278
{
4279
    VIR_ERROR0(_("client tried unsupported PolicyKit init request"));
4280
    remoteDispatchAuthError(rerr);
4281 4282
    return -1;
}
4283
#endif /* HAVE_POLKIT1 */
4284

4285 4286 4287 4288 4289 4290 4291 4292

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


static int
remoteDispatchListDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4293 4294
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4295
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4296
                                       remote_error *rerr,
4297 4298 4299 4300 4301
                                       remote_list_defined_storage_pools_args *args,
                                       remote_list_defined_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
4302 4303
        remoteDispatchFormatError (rerr, "%s",
                            _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
4304
        return -1;
4305 4306 4307
    }

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

    ret->names.names_len =
4314
        virConnectListDefinedStoragePools (conn,
4315 4316 4317
                                           ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
4318
        remoteDispatchConnError(rerr, conn);
4319 4320
        return -1;
    }
4321 4322 4323 4324 4325 4326

    return 0;
}

static int
remoteDispatchListStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4327 4328
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4329
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4330
                                remote_error *rerr,
4331 4332 4333 4334 4335
                                remote_list_storage_pools_args *args,
                                remote_list_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
4336 4337 4338
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX"));
        return -1;
4339 4340 4341
    }

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

    ret->names.names_len =
4348
        virConnectListStoragePools (conn,
4349
                                ret->names.names_val, args->maxnames);
4350 4351
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
4352
        remoteDispatchConnError(rerr, conn);
4353 4354
        return -1;
    }
4355 4356 4357 4358

    return 0;
}

4359 4360
static int
remoteDispatchFindStoragePoolSources (struct qemud_server *server ATTRIBUTE_UNUSED,
4361 4362
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4363
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4364
                                      remote_error *rerr,
4365 4366 4367 4368
                                      remote_find_storage_pool_sources_args *args,
                                      remote_find_storage_pool_sources_ret *ret)
{
    ret->xml =
4369
        virConnectFindStoragePoolSources (conn,
4370 4371 4372
                                          args->type,
                                          args->srcSpec ? *args->srcSpec : NULL,
                                          args->flags);
4373
    if (ret->xml == NULL) {
4374
        remoteDispatchConnError(rerr, conn);
4375
        return -1;
4376
    }
4377 4378 4379 4380 4381

    return 0;
}


4382 4383
static int
remoteDispatchStoragePoolCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
4384 4385
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4386
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4387
                                 remote_error *rerr,
4388 4389 4390 4391 4392
                                 remote_storage_pool_create_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4393
    pool = get_nonnull_storage_pool (conn, args->pool);
4394
    if (pool == NULL) {
4395
        remoteDispatchConnError(rerr, conn);
4396
        return -1;
4397 4398 4399 4400
    }

    if (virStoragePoolCreate (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4401
        remoteDispatchConnError(rerr, conn);
4402 4403 4404 4405 4406 4407 4408 4409
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4410 4411
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
4412
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
4413
                                    remote_error *rerr,
4414 4415 4416 4417 4418
                                    remote_storage_pool_create_xml_args *args,
                                    remote_storage_pool_create_xml_ret *ret)
{
    virStoragePoolPtr pool;

4419
    pool = virStoragePoolCreateXML (conn, args->xml, args->flags);
4420
    if (pool == NULL) {
4421
        remoteDispatchConnError(rerr, conn);
4422 4423
        return -1;
    }
4424 4425 4426 4427 4428 4429 4430 4431

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

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

4441
    pool = virStoragePoolDefineXML (conn, args->xml, args->flags);
4442
    if (pool == NULL) {
4443
        remoteDispatchConnError(rerr, conn);
4444 4445
        return -1;
    }
4446 4447 4448 4449 4450 4451 4452 4453

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

static int
remoteDispatchStoragePoolBuild (struct qemud_server *server ATTRIBUTE_UNUSED,
4454 4455
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4456
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4457 4458 4459
                                remote_error *rerr,
                                remote_storage_pool_build_args *args,
                                void *ret ATTRIBUTE_UNUSED)
4460 4461 4462
{
    virStoragePoolPtr pool;

4463
    pool = get_nonnull_storage_pool (conn, args->pool);
4464
    if (pool == NULL) {
4465
        remoteDispatchConnError(rerr, conn);
4466
        return -1;
4467 4468 4469 4470
    }

    if (virStoragePoolBuild (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4471
        remoteDispatchConnError(rerr, conn);
4472 4473 4474 4475 4476 4477 4478 4479 4480
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
4481 4482
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4483
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4484
                                  remote_error *rerr,
4485 4486 4487 4488 4489
                                  remote_storage_pool_destroy_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4490
    pool = get_nonnull_storage_pool (conn, args->pool);
4491
    if (pool == NULL) {
4492
        remoteDispatchConnError(rerr, conn);
4493
        return -1;
4494 4495 4496 4497
    }

    if (virStoragePoolDestroy (pool) == -1) {
        virStoragePoolFree(pool);
4498
        remoteDispatchConnError(rerr, conn);
4499 4500 4501 4502 4503 4504 4505 4506
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
4507 4508
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4509
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4510
                                 remote_error *rerr,
4511 4512 4513 4514 4515
                                 remote_storage_pool_delete_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4516
    pool = get_nonnull_storage_pool (conn, args->pool);
4517
    if (pool == NULL) {
4518
        remoteDispatchConnError(rerr, conn);
4519
        return -1;
4520 4521 4522 4523
    }

    if (virStoragePoolDelete (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4524
        remoteDispatchConnError(rerr, conn);
4525 4526 4527 4528 4529 4530 4531 4532
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolRefresh (struct qemud_server *server ATTRIBUTE_UNUSED,
4533 4534
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4535
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4536
                                  remote_error *rerr,
4537 4538 4539 4540 4541
                                  remote_storage_pool_refresh_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4542
    pool = get_nonnull_storage_pool (conn, args->pool);
4543
    if (pool == NULL) {
4544
        remoteDispatchConnError(rerr, conn);
4545
        return -1;
4546 4547 4548 4549
    }

    if (virStoragePoolRefresh (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4550
        remoteDispatchConnError(rerr, conn);
4551 4552 4553 4554 4555 4556 4557 4558
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
4559 4560
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4561
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4562
                                  remote_error *rerr,
4563 4564 4565 4566 4567 4568
                                  remote_storage_pool_get_info_args *args,
                                  remote_storage_pool_get_info_ret *ret)
{
    virStoragePoolPtr pool;
    virStoragePoolInfo info;

4569
    pool = get_nonnull_storage_pool (conn, args->pool);
4570
    if (pool == NULL) {
4571
        remoteDispatchConnError(rerr, conn);
4572
        return -1;
4573 4574 4575 4576
    }

    if (virStoragePoolGetInfo (pool, &info) == -1) {
        virStoragePoolFree(pool);
4577
        remoteDispatchConnError(rerr, conn);
4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592
        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,
4593 4594
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4595
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4596
                                  remote_error *rerr,
4597 4598 4599 4600 4601
                                  remote_storage_pool_dump_xml_args *args,
                                  remote_storage_pool_dump_xml_ret *ret)
{
    virStoragePoolPtr pool;

4602
    pool = get_nonnull_storage_pool (conn, args->pool);
4603
    if (pool == NULL) {
4604
        remoteDispatchConnError(rerr, conn);
4605
        return -1;
4606 4607 4608 4609 4610 4611
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStoragePoolGetXMLDesc (pool, args->flags);
    if (!ret->xml) {
        virStoragePoolFree(pool);
4612
        remoteDispatchConnError(rerr, conn);
4613 4614 4615 4616 4617 4618 4619 4620
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
4621 4622
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4623
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4624
                                       remote_error *rerr,
4625 4626 4627 4628 4629
                                       remote_storage_pool_get_autostart_args *args,
                                       remote_storage_pool_get_autostart_ret *ret)
{
    virStoragePoolPtr pool;

4630
    pool = get_nonnull_storage_pool (conn, args->pool);
4631
    if (pool == NULL) {
4632
        remoteDispatchConnError(rerr, conn);
4633
        return -1;
4634 4635 4636 4637
    }

    if (virStoragePoolGetAutostart (pool, &ret->autostart) == -1) {
        virStoragePoolFree(pool);
4638
        remoteDispatchConnError(rerr, conn);
4639 4640 4641 4642 4643 4644 4645 4646 4647
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
4648 4649
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4650
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4651
                                       remote_error *rerr,
4652 4653 4654 4655 4656
                                       remote_storage_pool_lookup_by_name_args *args,
                                       remote_storage_pool_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;

4657
    pool = virStoragePoolLookupByName (conn, args->name);
4658
    if (pool == NULL) {
4659
        remoteDispatchConnError(rerr, conn);
4660 4661
        return -1;
    }
4662 4663 4664 4665 4666 4667 4668 4669

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

static int
remoteDispatchStoragePoolLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
4670 4671
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4672
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4673
                                       remote_error *rerr,
4674 4675 4676 4677 4678
                                       remote_storage_pool_lookup_by_uuid_args *args,
                                       remote_storage_pool_lookup_by_uuid_ret *ret)
{
    virStoragePoolPtr pool;

4679
    pool = virStoragePoolLookupByUUID (conn, (unsigned char *) args->uuid);
4680
    if (pool == NULL) {
4681
        remoteDispatchConnError(rerr, conn);
4682 4683
        return -1;
    }
4684 4685 4686 4687 4688 4689 4690 4691

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

static int
remoteDispatchStoragePoolLookupByVolume (struct qemud_server *server ATTRIBUTE_UNUSED,
4692 4693
                                         struct qemud_client *client ATTRIBUTE_UNUSED,
                                         virConnectPtr conn,
4694
                                         remote_message_header *hdr ATTRIBUTE_UNUSED,
4695
                                         remote_error *rerr,
4696 4697 4698 4699 4700 4701
                                         remote_storage_pool_lookup_by_volume_args *args,
                                         remote_storage_pool_lookup_by_volume_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

4702
    vol = get_nonnull_storage_vol (conn, args->vol);
4703
    if (vol == NULL) {
4704
        remoteDispatchConnError(rerr, conn);
4705 4706
        return -1;
    }
4707 4708 4709

    pool = virStoragePoolLookupByVolume (vol);
    virStorageVolFree(vol);
4710
    if (pool == NULL) {
4711
        remoteDispatchConnError(rerr, conn);
4712 4713
        return -1;
    }
4714 4715 4716 4717 4718 4719 4720 4721

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

static int
remoteDispatchStoragePoolSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
4722 4723
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4724
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4725
                                       remote_error *rerr,
4726 4727 4728 4729 4730
                                       remote_storage_pool_set_autostart_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4731
    pool = get_nonnull_storage_pool (conn, args->pool);
4732
    if (pool == NULL) {
4733
        remoteDispatchConnError(rerr, conn);
4734
        return -1;
4735 4736 4737 4738
    }

    if (virStoragePoolSetAutostart (pool, args->autostart) == -1) {
        virStoragePoolFree(pool);
4739
        remoteDispatchConnError(rerr, conn);
4740 4741 4742 4743 4744 4745 4746 4747
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
4748 4749
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4750
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4751
                                   remote_error *rerr,
4752 4753 4754 4755 4756
                                   remote_storage_pool_undefine_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4757
    pool = get_nonnull_storage_pool (conn, args->pool);
4758
    if (pool == NULL) {
4759
        remoteDispatchConnError(rerr, conn);
4760
        return -1;
4761 4762 4763 4764
    }

    if (virStoragePoolUndefine (pool) == -1) {
        virStoragePoolFree(pool);
4765
        remoteDispatchConnError(rerr, conn);
4766 4767 4768 4769 4770 4771 4772 4773
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchNumOfStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4774 4775
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4776
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4777
                                 remote_error *rerr,
4778 4779 4780 4781
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_num_of_storage_pools_ret *ret)
{

4782
    ret->num = virConnectNumOfStoragePools (conn);
4783
    if (ret->num == -1) {
4784
        remoteDispatchConnError(rerr, conn);
4785 4786
        return -1;
    }
4787 4788 4789 4790 4791 4792

    return 0;
}

static int
remoteDispatchNumOfDefinedStoragePools (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
                                        void *args ATTRIBUTE_UNUSED,
                                        remote_num_of_defined_storage_pools_ret *ret)
{

4801
    ret->num = virConnectNumOfDefinedStoragePools (conn);
4802
    if (ret->num == -1) {
4803
        remoteDispatchConnError(rerr, conn);
4804 4805
        return -1;
    }
4806 4807 4808 4809 4810 4811

    return 0;
}

static int
remoteDispatchStoragePoolListVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
4812 4813
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4814
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4815
                                      remote_error *rerr,
4816 4817 4818 4819 4820 4821
                                      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) {
4822 4823 4824
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX"));
        return -1;
4825 4826
    }

4827
    pool = get_nonnull_storage_pool (conn, args->pool);
4828
    if (pool == NULL) {
4829
        remoteDispatchConnError(rerr, conn);
4830
        return -1;
4831 4832 4833
    }

    /* Allocate return buffer. */
4834 4835
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        virStoragePoolFree(pool);
4836 4837
        remoteDispatchOOMError(rerr);
        return -1;
4838
    }
4839 4840 4841 4842 4843

    ret->names.names_len =
        virStoragePoolListVolumes (pool,
                                   ret->names.names_val, args->maxnames);
    virStoragePoolFree(pool);
4844 4845
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
4846
        remoteDispatchConnError(rerr, conn);
4847 4848
        return -1;
    }
4849 4850 4851 4852 4853 4854 4855

    return 0;
}


static int
remoteDispatchStoragePoolNumOfVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
4856 4857
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4858
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4859
                                       remote_error *rerr,
4860 4861 4862 4863 4864
                                       remote_storage_pool_num_of_volumes_args *args,
                                       remote_storage_pool_num_of_volumes_ret *ret)
{
    virStoragePoolPtr pool;

4865
    pool = get_nonnull_storage_pool (conn, args->pool);
4866
    if (pool == NULL) {
4867
        remoteDispatchConnError(rerr, conn);
4868
        return -1;
4869 4870 4871 4872
    }

    ret->num = virStoragePoolNumOfVolumes (pool);
    virStoragePoolFree(pool);
4873
    if (ret->num == -1) {
4874
        remoteDispatchConnError(rerr, conn);
4875 4876
        return -1;
    }
4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889

    return 0;
}


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



static int
remoteDispatchStorageVolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4890 4891
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4892
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4893
                                   remote_error *rerr,
4894 4895 4896 4897 4898 4899
                                   remote_storage_vol_create_xml_args *args,
                                   remote_storage_vol_create_xml_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

4900
    pool = get_nonnull_storage_pool (conn, args->pool);
4901
    if (pool == NULL) {
4902
        remoteDispatchConnError(rerr, conn);
4903
        return -1;
4904 4905 4906 4907
    }

    vol = virStorageVolCreateXML (pool, args->xml, args->flags);
    virStoragePoolFree(pool);
4908
    if (vol == NULL) {
4909
        remoteDispatchConnError(rerr, conn);
4910 4911
        return -1;
    }
4912 4913 4914 4915 4916 4917

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

4918 4919 4920 4921
static int
remoteDispatchStorageVolCreateXmlFrom (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4922
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937
                                       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) {
4938
        virStoragePoolFree(pool);
4939 4940 4941 4942 4943 4944
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    newvol = virStorageVolCreateXMLFrom (pool, args->xml, clonevol,
                                         args->flags);
4945 4946
    virStorageVolFree(clonevol);
    virStoragePoolFree(pool);
4947 4948 4949 4950 4951 4952 4953 4954 4955
    if (newvol == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_storage_vol (&ret->vol, newvol);
    virStorageVolFree(newvol);
    return 0;
}
4956 4957 4958

static int
remoteDispatchStorageVolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
4959 4960
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4961
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4962
                                remote_error *rerr,
4963 4964 4965 4966 4967
                                remote_storage_vol_delete_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virStorageVolPtr vol;

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

    if (virStorageVolDelete (vol, args->flags) == -1) {
        virStorageVolFree(vol);
4976
        remoteDispatchConnError(rerr, conn);
4977 4978 4979 4980 4981 4982
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}

4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014
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;
}

5015 5016
static int
remoteDispatchStorageVolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
5017 5018
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5019
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5020
                                 remote_error *rerr,
5021 5022 5023 5024 5025 5026
                                 remote_storage_vol_get_info_args *args,
                                 remote_storage_vol_get_info_ret *ret)
{
    virStorageVolPtr vol;
    virStorageVolInfo info;

5027
    vol = get_nonnull_storage_vol (conn, args->vol);
5028
    if (vol == NULL) {
5029
        remoteDispatchConnError(rerr, conn);
5030
        return -1;
5031 5032 5033 5034
    }

    if (virStorageVolGetInfo (vol, &info) == -1) {
        virStorageVolFree(vol);
5035
        remoteDispatchConnError(rerr, conn);
5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049
        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,
5050 5051
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5052
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5053
                                 remote_error *rerr,
5054 5055 5056 5057 5058
                                 remote_storage_vol_dump_xml_args *args,
                                 remote_storage_vol_dump_xml_ret *ret)
{
    virStorageVolPtr vol;

5059
    vol = get_nonnull_storage_vol (conn, args->vol);
5060
    if (vol == NULL) {
5061
        remoteDispatchConnError(rerr, conn);
5062
        return -1;
5063 5064 5065 5066 5067 5068
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStorageVolGetXMLDesc (vol, args->flags);
    if (!ret->xml) {
        virStorageVolFree(vol);
5069
        remoteDispatchConnError(rerr, conn);
5070 5071 5072 5073 5074 5075 5076 5077 5078
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolGetPath (struct qemud_server *server ATTRIBUTE_UNUSED,
5079 5080
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5081
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5082
                                 remote_error *rerr,
5083 5084 5085 5086 5087
                                 remote_storage_vol_get_path_args *args,
                                 remote_storage_vol_get_path_ret *ret)
{
    virStorageVolPtr vol;

5088
    vol = get_nonnull_storage_vol (conn, args->vol);
5089
    if (vol == NULL) {
5090
        remoteDispatchConnError(rerr, conn);
5091
        return -1;
5092 5093 5094 5095 5096 5097
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virStorageVolGetPath (vol);
    if (!ret->name) {
        virStorageVolFree(vol);
5098
        remoteDispatchConnError(rerr, conn);
5099 5100 5101 5102 5103 5104 5105 5106 5107
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
5108 5109
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
5110
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
5111
                                      remote_error *rerr,
5112 5113 5114 5115 5116 5117
                                      remote_storage_vol_lookup_by_name_args *args,
                                      remote_storage_vol_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

5118
    pool = get_nonnull_storage_pool (conn, args->pool);
5119
    if (pool == NULL) {
5120
        remoteDispatchConnError(rerr, conn);
5121
        return -1;
5122 5123 5124 5125
    }

    vol = virStorageVolLookupByName (pool, args->name);
    virStoragePoolFree(pool);
5126
    if (vol == NULL) {
5127
        remoteDispatchConnError(rerr, conn);
5128 5129
        return -1;
    }
5130 5131 5132 5133 5134 5135 5136 5137

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

static int
remoteDispatchStorageVolLookupByKey (struct qemud_server *server ATTRIBUTE_UNUSED,
5138 5139
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
5140
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
5141
                                     remote_error *rerr,
5142 5143 5144 5145 5146
                                     remote_storage_vol_lookup_by_key_args *args,
                                     remote_storage_vol_lookup_by_key_ret *ret)
{
    virStorageVolPtr vol;

5147
    vol = virStorageVolLookupByKey (conn, args->key);
5148
    if (vol == NULL) {
5149
        remoteDispatchConnError(rerr, conn);
5150 5151
        return -1;
    }
5152 5153 5154 5155 5156 5157 5158 5159 5160

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


static int
remoteDispatchStorageVolLookupByPath (struct qemud_server *server ATTRIBUTE_UNUSED,
5161 5162
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
5163
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
5164
                                      remote_error *rerr,
5165 5166 5167 5168 5169
                                      remote_storage_vol_lookup_by_path_args *args,
                                      remote_storage_vol_lookup_by_path_ret *ret)
{
    virStorageVolPtr vol;

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

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


5182 5183 5184 5185 5186 5187
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/

static int
remoteDispatchNodeNumOfDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
5188 5189
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
5190
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
5191
                                remote_error *rerr,
5192 5193 5194 5195 5196
                                remote_node_num_of_devices_args *args,
                                remote_node_num_of_devices_ret *ret)
{
    CHECK_CONN(client);

5197
    ret->num = virNodeNumOfDevices (conn,
5198 5199
                                    args->cap ? *args->cap : NULL,
                                    args->flags);
5200
    if (ret->num == -1) {
5201
        remoteDispatchConnError(rerr, conn);
5202 5203
        return -1;
    }
5204 5205 5206 5207 5208 5209 5210

    return 0;
}


static int
remoteDispatchNodeListDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
5211 5212
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
5213
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
5214
                               remote_error *rerr,
5215 5216 5217 5218 5219 5220
                               remote_node_list_devices_args *args,
                               remote_node_list_devices_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
5221 5222 5223
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
5224 5225 5226 5227
    }

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

    ret->names.names_len =
5233
        virNodeListDevices (conn,
5234 5235 5236
                            args->cap ? *args->cap : NULL,
                            ret->names.names_val, args->maxnames, args->flags);
    if (ret->names.names_len == -1) {
5237
        remoteDispatchConnError(rerr, conn);
5238 5239 5240 5241 5242 5243 5244 5245 5246 5247
        VIR_FREE(ret->names.names_val);
        return -1;
    }

    return 0;
}


static int
remoteDispatchNodeDeviceLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
5248 5249
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
5250
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
5251
                                      remote_error *rerr,
5252 5253 5254 5255 5256 5257 5258
                                      remote_node_device_lookup_by_name_args *args,
                                      remote_node_device_lookup_by_name_ret *ret)
{
    virNodeDevicePtr dev;

    CHECK_CONN(client);

5259
    dev = virNodeDeviceLookupByName (conn, args->name);
5260
    if (dev == NULL) {
5261
        remoteDispatchConnError(rerr, conn);
5262 5263
        return -1;
    }
5264 5265 5266 5267 5268 5269 5270 5271 5272

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


static int
remoteDispatchNodeDeviceDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
5273 5274
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5275
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5276
                                 remote_error *rerr,
5277 5278 5279 5280 5281 5282
                                 remote_node_device_dump_xml_args *args,
                                 remote_node_device_dump_xml_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

5283
    dev = virNodeDeviceLookupByName(conn, args->name);
5284
    if (dev == NULL) {
5285
        remoteDispatchConnError(rerr, conn);
5286
        return -1;
5287 5288 5289 5290 5291
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNodeDeviceGetXMLDesc (dev, args->flags);
    if (!ret->xml) {
5292
        remoteDispatchConnError(rerr, conn);
5293 5294 5295 5296 5297 5298 5299 5300 5301 5302
        virNodeDeviceFree(dev);
        return -1;
    }
    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceGetParent (struct qemud_server *server ATTRIBUTE_UNUSED,
5303 5304
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
5305
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
5306
                                   remote_error *rerr,
5307 5308 5309 5310 5311 5312 5313
                                   remote_node_device_get_parent_args *args,
                                   remote_node_device_get_parent_ret *ret)
{
    virNodeDevicePtr dev;
    const char *parent;
    CHECK_CONN(client);

5314
    dev = virNodeDeviceLookupByName(conn, args->name);
5315
    if (dev == NULL) {
5316
        remoteDispatchConnError(rerr, conn);
5317
        return -1;
5318 5319 5320 5321 5322 5323 5324 5325 5326 5327
    }

    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
        if (VIR_ALLOC(parent_p) < 0) {
5328
            virNodeDeviceFree(dev);
5329 5330
            remoteDispatchOOMError(rerr);
            return -1;
5331 5332 5333
        }
        *parent_p = strdup(parent);
        if (*parent_p == NULL) {
5334
            virNodeDeviceFree(dev);
5335 5336
            remoteDispatchOOMError(rerr);
            return -1;
5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347
        }
        ret->parent = parent_p;
    }

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceNumOfCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
5348 5349
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
5350
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
5351
                                   remote_error *rerr,
5352 5353 5354 5355 5356 5357
                                   remote_node_device_num_of_caps_args *args,
                                   remote_node_device_num_of_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

5358
    dev = virNodeDeviceLookupByName(conn, args->name);
5359
    if (dev == NULL) {
5360
        remoteDispatchConnError(rerr, conn);
5361
        return -1;
5362 5363 5364
    }

    ret->num = virNodeDeviceNumOfCaps(dev);
5365
    if (ret->num < 0) {
5366
        virNodeDeviceFree(dev);
5367
        remoteDispatchConnError(rerr, conn);
5368 5369
        return -1;
    }
5370 5371 5372 5373 5374 5375 5376 5377

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceListCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
5378 5379
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
5380
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
5381
                                  remote_error *rerr,
5382 5383 5384 5385 5386 5387
                                  remote_node_device_list_caps_args *args,
                                  remote_node_device_list_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

5388
    dev = virNodeDeviceLookupByName(conn, args->name);
5389
    if (dev == NULL) {
5390
        remoteDispatchConnError(rerr, conn);
5391
        return -1;
5392 5393 5394
    }

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
5395
        virNodeDeviceFree(dev);
5396 5397 5398
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
5399 5400 5401 5402
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
5403
        virNodeDeviceFree(dev);
5404 5405
        remoteDispatchOOMError(rerr);
        return -1;
5406 5407 5408 5409 5410 5411
    }

    ret->names.names_len =
        virNodeDeviceListCaps (dev, ret->names.names_val,
                               args->maxnames);
    if (ret->names.names_len == -1) {
5412
        virNodeDeviceFree(dev);
5413
        remoteDispatchConnError(rerr, conn);
5414 5415 5416 5417
        VIR_FREE(ret->names.names_val);
        return -1;
    }

5418
    virNodeDeviceFree(dev);
5419 5420 5421 5422
    return 0;
}


5423 5424 5425 5426
static int
remoteDispatchNodeDeviceDettach (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5427
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5428 5429 5430 5431 5432 5433 5434 5435 5436
                                 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) {
5437
        remoteDispatchConnError(rerr, conn);
5438 5439 5440 5441
        return -1;
    }

    if (virNodeDeviceDettach(dev) == -1) {
5442
        virNodeDeviceFree(dev);
5443 5444 5445 5446
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

5447
    virNodeDeviceFree(dev);
5448 5449 5450 5451 5452 5453 5454 5455
    return 0;
}


static int
remoteDispatchNodeDeviceReAttach (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
5456
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
5457 5458 5459 5460 5461 5462 5463 5464 5465
                                  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) {
5466
        remoteDispatchConnError(rerr, conn);
5467 5468 5469 5470
        return -1;
    }

    if (virNodeDeviceReAttach(dev) == -1) {
5471
        virNodeDeviceFree(dev);
5472 5473 5474 5475
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

5476
    virNodeDeviceFree(dev);
5477 5478 5479 5480 5481 5482 5483 5484
    return 0;
}


static int
remoteDispatchNodeDeviceReset (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
5485
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
5486 5487 5488 5489 5490 5491 5492 5493 5494
                               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) {
5495
        remoteDispatchConnError(rerr, conn);
5496 5497 5498 5499
        return -1;
    }

    if (virNodeDeviceReset(dev) == -1) {
5500
        virNodeDeviceFree(dev);
5501 5502 5503 5504
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

5505
    virNodeDeviceFree(dev);
5506 5507 5508 5509
    return 0;
}


5510 5511 5512 5513
static int
remoteDispatchNodeDeviceCreateXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
5514
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537
                                  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,
5538
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
5539 5540 5541 5542 5543 5544 5545 5546
                                remote_error *rerr,
                                remote_node_device_destroy_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virNodeDevicePtr dev;

    dev = virNodeDeviceLookupByName(conn, args->name);
    if (dev == NULL) {
5547
        remoteDispatchConnError(rerr, conn);
5548 5549 5550 5551
        return -1;
    }

    if (virNodeDeviceDestroy(dev) == -1) {
5552
        virNodeDeviceFree(dev);
5553 5554 5555 5556
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

5557
    virNodeDeviceFree(dev);
5558 5559 5560 5561
    return 0;
}


5562 5563 5564 5565 5566 5567

/***************************
 * Register / deregister events
 ***************************/
static int
remoteDispatchDomainEventsRegister (struct qemud_server *server ATTRIBUTE_UNUSED,
5568 5569
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
5570
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
5571
                                    remote_error *rerr ATTRIBUTE_UNUSED,
5572 5573 5574 5575
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);
5576 5577 5578 5579 5580 5581
    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;
    }
5582

5583 5584 5585 5586 5587
    if ((callbackID = virConnectDomainEventRegisterAny(conn,
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
                                                       client, NULL)) < 0) {
5588 5589 5590
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
5591

5592
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;
5593

5594 5595 5596 5597 5598
    return 0;
}

static int
remoteDispatchDomainEventsDeregister (struct qemud_server *server ATTRIBUTE_UNUSED,
5599 5600
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
5601
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
5602
                                      remote_error *rerr ATTRIBUTE_UNUSED,
5603 5604 5605 5606 5607
                                      void *args ATTRIBUTE_UNUSED,
                                      remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

5608 5609
    if (client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] == -1) {
        remoteDispatchFormatError(rerr, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
5610 5611
        return -1;
    }
5612

5613 5614 5615 5616 5617
    if (virConnectDomainEventDeregisterAny(conn,
                                           client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
5618

5619
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;
5620 5621 5622 5623 5624
    return 0;
}

static void
remoteDispatchDomainEventSend (struct qemud_client *client,
5625 5626 5627
                               int procnr,
                               xdrproc_t proc,
                               void *data)
5628
{
5629
    struct qemud_client_message *msg = NULL;
5630
    XDR xdr;
5631
    unsigned int len;
5632

5633
    if (VIR_ALLOC(msg) < 0)
5634 5635
        return;

5636 5637
    msg->hdr.prog = REMOTE_PROGRAM;
    msg->hdr.vers = REMOTE_PROTOCOL_VERSION;
5638
    msg->hdr.proc = procnr;
5639
    msg->hdr.type = REMOTE_MESSAGE;
5640 5641
    msg->hdr.serial = 1;
    msg->hdr.status = REMOTE_OK;
5642

5643 5644
    if (remoteEncodeClientMessageHeader(msg) < 0)
        goto error;
5645

5646 5647
    /* Serialise the return header and event. */
    xdrmem_create (&xdr,
5648 5649
                   msg->buffer,
                   msg->bufferLength,
5650
                   XDR_ENCODE);
5651

5652 5653
    /* Skip over the header we just wrote */
    if (xdr_setpos (&xdr, msg->bufferOffset) == 0)
5654
        goto xdr_error;
5655

5656 5657
    if (!(proc)(&xdr, data)) {
        VIR_WARN("Failed to serialize domain event %d", procnr);
5658
        goto xdr_error;
5659
    }
5660

5661 5662
    /* Update length word to include payload*/
    len = msg->bufferOffset = xdr_getpos (&xdr);
5663 5664
    if (xdr_setpos (&xdr, 0) == 0)
        goto xdr_error;
5665

5666 5667
    if (!xdr_u_int (&xdr, &len))
        goto xdr_error;
5668 5669

    /* Send it. */
5670 5671 5672 5673
    msg->async = 1;
    msg->bufferLength = len;
    msg->bufferOffset = 0;
    qemudClientMessageQueuePush(&client->tx, msg);
5674
    qemudUpdateClientEvent(client);
5675 5676 5677 5678 5679 5680 5681 5682

    xdr_destroy (&xdr);
    return;

xdr_error:
    xdr_destroy(&xdr);
error:
    VIR_FREE(msg);
5683
}
5684

5685 5686 5687
static int
remoteDispatchNumOfSecrets (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client ATTRIBUTE_UNUSED,
5688 5689 5690
                            virConnectPtr conn,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *err,
5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705
                            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,
5706 5707 5708
                           virConnectPtr conn,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *err,
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
                           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,
5737 5738 5739
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
                               remote_error *err,
5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758
                               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,
5759 5760 5761
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790
                              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,
5791 5792 5793
                                virConnectPtr conn,
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                remote_error *err,
5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814
                                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
5815 5816
remoteDispatchSecretLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
5817 5818 5819
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *err,
5820 5821
                                  remote_secret_lookup_by_uuid_args *args,
                                  remote_secret_lookup_by_uuid_ret *ret)
5822 5823 5824
{
    virSecretPtr secret;

5825
    secret = virSecretLookupByUUID (conn, (unsigned char *)args->uuid);
5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838
    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,
5839 5840 5841
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865
                              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,
5866 5867 5868
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888
                              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;
}

5889 5890 5891
static int
remoteDispatchSecretLookupByUsage (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
5892 5893 5894
                                   virConnectPtr conn,
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                                   remote_error *err,
5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910
                                   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;
}

5911

5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930
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) {
5931
        virDomainFree(domain);
5932 5933 5934 5935
        remoteDispatchConnError(err, conn);
        return -1;
    }

5936
    virDomainFree(domain);
5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958
    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) {
5959
        virDomainFree(domain);
5960 5961 5962 5963
        remoteDispatchConnError(err, conn);
        return -1;
    }

5964
    virDomainFree(domain);
5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986
    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) {
5987
        virInterfaceFree(iface);
5988 5989 5990 5991
        remoteDispatchConnError(err, conn);
        return -1;
    }

5992
    virInterfaceFree(iface);
5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014
    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) {
6015
        virNetworkFree(network);
6016 6017 6018 6019
        remoteDispatchConnError(err, conn);
        return -1;
    }

6020
    virNetworkFree(network);
6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042
    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) {
6043
        virNetworkFree(network);
6044 6045 6046 6047
        remoteDispatchConnError(err, conn);
        return -1;
    }

6048
    virNetworkFree(network);
6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070
    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) {
6071
        virStoragePoolFree(pool);
6072 6073 6074 6075
        remoteDispatchConnError(err, conn);
        return -1;
    }

6076
    virStoragePoolFree(pool);
6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098
    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) {
6099
        virStoragePoolFree(pool);
6100 6101 6102 6103
        remoteDispatchConnError(err, conn);
        return -1;
    }

6104
    virStoragePoolFree(pool);
6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127
    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;
}


6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149
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;
}


6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170
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;
6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214

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

6215 6216 6217 6218
    return 0;
}


6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247
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;
}


6248 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
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 已提交
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
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)
{
6318 6319 6320
    virDomainPtr domain = NULL;
    virDomainSnapshotPtr snapshot = NULL;
    int rc = -1;
C
Chris Lalancette 已提交
6321

6322 6323 6324 6325 6326 6327 6328
    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 已提交
6329 6330 6331

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainSnapshotGetXMLDesc(snapshot, args->flags);
6332 6333 6334 6335 6336 6337 6338
    if (!ret->xml)
        goto cleanup;

    rc = 0;

cleanup:
    if (snapshot)
C
Chris Lalancette 已提交
6339
        virDomainSnapshotFree(snapshot);
6340 6341 6342
    if (domain)
        virDomainFree(domain);
    if (rc < 0)
C
Chris Lalancette 已提交
6343 6344
        remoteDispatchConnError(rerr, conn);

6345
    return rc;
C
Chris Lalancette 已提交
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 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
}

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)
{
6530 6531 6532
    virDomainPtr domain = NULL;
    virDomainSnapshotPtr snapshot = NULL;
    int rc = -1;
C
Chris Lalancette 已提交
6533

6534 6535 6536 6537 6538 6539 6540
    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 已提交
6541

6542 6543 6544 6545 6546 6547 6548
    if (virDomainRevertToSnapshot(snapshot, args->flags) == -1)
        goto cleanup;

    rc = 0;

cleanup:
    if (snapshot)
C
Chris Lalancette 已提交
6549
        virDomainSnapshotFree(snapshot);
6550 6551 6552
    if (domain)
        virDomainFree(domain);
    if (rc < 0)
C
Chris Lalancette 已提交
6553 6554
        remoteDispatchConnError(rerr, conn);

6555
    return rc;
C
Chris Lalancette 已提交
6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566
}

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)
{
6567 6568 6569
    virDomainPtr domain = NULL;
    virDomainSnapshotPtr snapshot = NULL;
    int rc = -1;
C
Chris Lalancette 已提交
6570

6571 6572 6573 6574 6575 6576 6577
    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 已提交
6578

6579 6580 6581 6582 6583 6584 6585
    if (virDomainSnapshotDelete(snapshot, args->flags) == -1)
        goto cleanup;

    rc = 0;

cleanup:
    if (snapshot)
C
Chris Lalancette 已提交
6586
        virDomainSnapshotFree(snapshot);
6587 6588 6589
    if (domain)
        virDomainFree(domain);
    if (rc < 0)
C
Chris Lalancette 已提交
6590 6591
        remoteDispatchConnError(rerr, conn);

6592
    return rc;
C
Chris Lalancette 已提交
6593 6594
}

6595

6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667
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;
}


6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846

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


6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879
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;
}

C
Chris Lalancette 已提交
6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908
static int
qemuDispatchMonitorCommand (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *rerr,
                            qemu_monitor_command_args *args,
                            qemu_monitor_command_ret *ret)
{
    virDomainPtr domain;

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

    if (virDomainQemuMonitorCommand(domain, args->cmd, &ret->result,
                                    args->flags) == -1) {
        virDomainFree(domain);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    virDomainFree(domain);

    return 0;
}

6909

6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935
/*----- 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 已提交
6936
static virInterfacePtr
6937
get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
6938
{
6939
    return virGetInterface (conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
6940 6941
}

6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955
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;
}

6956 6957 6958
static virSecretPtr
get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret)
{
6959
    return virGetSecret (conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
6960 6961
}

6962 6963 6964 6965 6966 6967
static virNWFilterPtr
get_nonnull_nwfilter (virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
{
    return virGetNWFilter (conn, nwfilter.name, BAD_CAST nwfilter.uuid);
}

C
Chris Lalancette 已提交
6968
static virDomainSnapshotPtr
6969
get_nonnull_domain_snapshot (virDomainPtr domain, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
6970 6971 6972 6973
{
    return virGetDomainSnapshot(domain, snapshot.name);
}

6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989
/* 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 已提交
6990 6991 6992 6993 6994 6995 6996 6997
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);
}

6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011
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);
}
7012 7013 7014 7015 7016 7017

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

static void
make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
{
7022
    memcpy (secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
7023 7024
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = strdup (secret_src->usageID);
7025
}
7026 7027 7028 7029 7030 7031 7032

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 已提交
7033 7034 7035 7036 7037 7038 7039

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