remote.c 242.5 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007-2011 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
#include "command.h"
64

65
#define VIR_FROM_THIS VIR_FROM_REMOTE
66
#define REMOTE_DEBUG(fmt, ...) VIR_DEBUG(fmt, __VA_ARGS__)
67

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

86

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

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

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

98 99 100 101 102 103 104 105 106 107
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 已提交
108 109 110 111 112 113 114 115 116 117
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]);
}

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

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

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

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

139
    virMutexLock(&client->lock);
140

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

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

    virMutexUnlock(&client->lock);
152

153 154
    return 0;
}
155

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

183

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


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


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


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


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


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

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

391 392 393
/*----- Functions. -----*/

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

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

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

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

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

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

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

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

453
    client->closing = 1;
454

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

459
static int
460
remoteDispatchSupportsFeature (struct qemud_server *server ATTRIBUTE_UNUSED,
461 462
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
463
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
464
                               remote_error *rerr,
465 466
                               remote_supports_feature_args *args, remote_supports_feature_ret *ret)
{
467
    ret->supported = virDrvSupportsFeature (conn, args->feature);
468 469

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

    return 0;
}

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

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

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

    return 0;
}

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

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

    ret->hv_ver = hvVer;
    return 0;
}

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

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

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

    ret->hostname = hostname;
    return 0;
}

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

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

    ret->uri = uri;
    return 0;
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
static int
remoteDispatchGetSysinfo (struct qemud_server *server ATTRIBUTE_UNUSED,
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
                          remote_error *rerr,
                          remote_get_sysinfo_args *args,
                          remote_get_sysinfo_ret *ret)
{
    unsigned int flags;
    char *sysinfo;

    flags = args->flags;
    sysinfo = virConnectGetSysinfo (conn, flags);
    if (sysinfo == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    ret->sysinfo = sysinfo;
    return 0;
}

611
static int
612
remoteDispatchGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
613 614
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
615
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
616
                           remote_error *rerr,
617 618 619 620 621 622
                           remote_get_max_vcpus_args *args,
                           remote_get_max_vcpus_ret *ret)
{
    char *type;

    type = args->type ? *args->type : NULL;
623
    ret->max_vcpus = virConnectGetMaxVcpus (conn, type);
624
    if (ret->max_vcpus == -1) {
625
        remoteDispatchConnError(rerr, conn);
626 627
        return -1;
    }
628 629 630 631 632

    return 0;
}

static int
633
remoteDispatchNodeGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
634 635
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
636
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
637
                           remote_error *rerr,
638 639 640 641 642
                           void *args ATTRIBUTE_UNUSED,
                           remote_node_get_info_ret *ret)
{
    virNodeInfo info;

643 644
    if (virNodeGetInfo (conn, &info) == -1) {
        remoteDispatchConnError(rerr, conn);
645
        return -1;
646
    }
647 648 649 650 651 652 653 654 655 656 657 658 659 660

    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
661
remoteDispatchGetCapabilities (struct qemud_server *server ATTRIBUTE_UNUSED,
662 663
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
664
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
665
                               remote_error *rerr,
666 667 668 669 670
                               void *args ATTRIBUTE_UNUSED,
                               remote_get_capabilities_ret *ret)
{
    char *caps;

671
    caps = virConnectGetCapabilities (conn);
672
    if (caps == NULL) {
673
        remoteDispatchConnError(rerr, conn);
674 675
        return -1;
    }
676 677 678 679 680

    ret->capabilities = caps;
    return 0;
}

681 682
static int
remoteDispatchNodeGetCellsFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
683 684
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
685
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
686
                                      remote_error *rerr,
687 688 689
                                      remote_node_get_cells_free_memory_args *args,
                                      remote_node_get_cells_free_memory_ret *ret)
{
D
Daniel P. Berrange 已提交
690
    int err;
691 692

    if (args->maxCells > REMOTE_NODE_MAX_CELLS) {
693 694 695
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxCells > REMOTE_NODE_MAX_CELLS"));
        return -1;
696 697 698
    }

    /* Allocate return buffer. */
699
    if (VIR_ALLOC_N(ret->freeMems.freeMems_val, args->maxCells) < 0) {
700 701
        remoteDispatchOOMError(rerr);
        return -1;
702
    }
703

D
Daniel P. Berrange 已提交
704 705 706 707 708
    err = virNodeGetCellsFreeMemory(conn,
                                    (unsigned long long *)ret->freeMems.freeMems_val,
                                    args->startCell,
                                    args->maxCells);
    if (err <= 0) {
709
        VIR_FREE(ret->freeMems.freeMems_val);
710
        remoteDispatchConnError(rerr, conn);
711
        return -1;
712
    }
D
Daniel P. Berrange 已提交
713
    ret->freeMems.freeMems_len = err;
714 715 716 717 718 719 720

    return 0;
}


static int
remoteDispatchNodeGetFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
721 722
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
723
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
724
                                 remote_error *rerr,
725 726 727 728 729
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_node_get_free_memory_ret *ret)
{
    unsigned long long freeMem;

730
    freeMem = virNodeGetFreeMemory(conn);
731
    if (freeMem == 0) {
732
        remoteDispatchConnError(rerr, conn);
733 734
        return -1;
    }
735 736 737 738 739
    ret->freeMem = freeMem;
    return 0;
}


740
static int
741
remoteDispatchDomainGetSchedulerType (struct qemud_server *server ATTRIBUTE_UNUSED,
742 743
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
744
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
745
                                      remote_error *rerr,
746 747 748 749 750 751 752
                                      remote_domain_get_scheduler_type_args *args,
                                      remote_domain_get_scheduler_type_ret *ret)
{
    virDomainPtr dom;
    char *type;
    int nparams;

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

    type = virDomainGetSchedulerType (dom, &nparams);
760
    if (type == NULL) {
761
        remoteDispatchConnError(rerr, conn);
762
        virDomainFree(dom);
763 764
        return -1;
    }
765 766 767

    ret->type = type;
    ret->nparams = nparams;
768
    virDomainFree(dom);
769 770 771 772
    return 0;
}

static int
773
remoteDispatchDomainGetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
774 775
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
776
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
777
                                            remote_error *rerr,
778 779 780 781 782 783 784 785 786 787
                                            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) {
788 789
        remoteDispatchFormatError (rerr, "%s", _("nparams too large"));
        return -1;
790
    }
791
    if (VIR_ALLOC_N(params, nparams) < 0) {
792 793
        remoteDispatchOOMError(rerr);
        return -1;
794 795
    }

796
    dom = get_nonnull_domain (conn, args->dom);
797
    if (dom == NULL) {
798
        VIR_FREE(params);
799
        remoteDispatchConnError(rerr, conn);
800
        return -1;
801 802 803 804
    }

    r = virDomainGetSchedulerParameters (dom, params, &nparams);
    if (r == -1) {
805
        remoteDispatchConnError(rerr, conn);
806
        virDomainFree(dom);
807
        VIR_FREE(params);
808 809 810 811 812
        return -1;
    }

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

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

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
        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:
837
            remoteDispatchFormatError (rerr, "%s", _("unknown type"));
838
            goto cleanup;
839 840
        }
    }
841
    virDomainFree(dom);
842
    VIR_FREE(params);
843 844

    return 0;
845 846

oom:
847
    remoteDispatchOOMError(rerr);
848 849 850 851 852
cleanup:
    virDomainFree(dom);
    for (i = 0 ; i < nparams ; i++)
        VIR_FREE(ret->params.params_val[i].field);
    VIR_FREE(params);
853
    return -1;
854 855 856
}

static int
857
remoteDispatchDomainSetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
858 859
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
860
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
861
                                            remote_error *rerr,
862 863 864 865 866 867 868 869 870 871
                                            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) {
872 873
        remoteDispatchFormatError (rerr, "%s", _("nparams too large"));
        return -1;
874
    }
875
    if (VIR_ALLOC_N(params, nparams) < 0) {
876 877
        remoteDispatchOOMError(rerr);
        return -1;
878 879 880 881
    }

    /* Deserialise parameters. */
    for (i = 0; i < nparams; ++i) {
C
Chris Lalancette 已提交
882 883 884 885 886
        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;
        }
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
        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;
        }
    }

904
    dom = get_nonnull_domain (conn, args->dom);
905
    if (dom == NULL) {
906
        VIR_FREE(params);
907
        remoteDispatchConnError(rerr, conn);
908
        return -1;
909 910 911
    }

    r = virDomainSetSchedulerParameters (dom, params, nparams);
912
    VIR_FREE(params);
913
    if (r == -1) {
914
        remoteDispatchConnError(rerr, conn);
915
        virDomainFree(dom);
916 917
        return -1;
    }
918
    virDomainFree(dom);
919 920 921 922

    return 0;
}

923
static int
924
remoteDispatchDomainBlockStats (struct qemud_server *server ATTRIBUTE_UNUSED,
925 926
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
927
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
928
                                remote_error *rerr,
929 930 931 932 933 934 935
                                remote_domain_block_stats_args *args,
                                remote_domain_block_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainBlockStats stats;

936
    dom = get_nonnull_domain (conn, args->dom);
937
    if (dom == NULL) {
938
        remoteDispatchConnError(rerr, conn);
939
        return -1;
940 941 942
    }
    path = args->path;

D
Daniel P. Berrange 已提交
943
    if (virDomainBlockStats (dom, path, &stats, sizeof stats) == -1) {
944
        remoteDispatchConnError(rerr, conn);
945
        virDomainFree (dom);
946
        return -1;
D
Daniel P. Berrange 已提交
947 948
    }
    virDomainFree (dom);
949 950 951 952 953 954 955 956 957 958 959

    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
960
remoteDispatchDomainInterfaceStats (struct qemud_server *server ATTRIBUTE_UNUSED,
961 962
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
963
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
964
                                    remote_error *rerr,
965 966 967 968 969 970 971
                                    remote_domain_interface_stats_args *args,
                                    remote_domain_interface_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainInterfaceStats stats;

972
    dom = get_nonnull_domain (conn, args->dom);
973
    if (dom == NULL) {
974
        remoteDispatchConnError(rerr, conn);
975
        return -1;
976 977 978
    }
    path = args->path;

D
Daniel P. Berrange 已提交
979
    if (virDomainInterfaceStats (dom, path, &stats, sizeof stats) == -1) {
980
        remoteDispatchConnError(rerr, conn);
981
        virDomainFree (dom);
982
        return -1;
D
Daniel P. Berrange 已提交
983 984
    }
    virDomainFree (dom);
985 986 987 988 989 990 991 992 993 994 995 996 997

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

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

    nr_stats = virDomainMemoryStats (dom, stats, args->maxStats, 0);
    if (nr_stats == -1) {
        VIR_FREE(stats);
        remoteDispatchConnError(rerr, conn);
1034
        virDomainFree (dom);
1035 1036
        return -1;
    }
1037
    virDomainFree (dom);
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055

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

1056 1057
static int
remoteDispatchDomainBlockPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
1058 1059
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1060
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1061
                               remote_error *rerr,
1062 1063 1064 1065 1066 1067 1068 1069 1070
                               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;

1071
    dom = get_nonnull_domain (conn, args->dom);
1072
    if (dom == NULL) {
1073
        remoteDispatchConnError(rerr, conn);
1074
        return -1;
1075 1076 1077 1078 1079 1080 1081
    }
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1082
        virDomainFree (dom);
1083 1084 1085
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
        return -1;
1086 1087 1088
    }

    ret->buffer.buffer_len = size;
1089
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
1090
        virDomainFree (dom);
1091
        remoteDispatchOOMError(rerr);
1092
        return -1;
1093 1094 1095 1096 1097
    }

    if (virDomainBlockPeek (dom, path, offset, size,
                            ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
1098
        remoteDispatchConnError(rerr, conn);
1099
        virDomainFree (dom);
1100 1101 1102 1103 1104 1105 1106
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

R
Richard W.M. Jones 已提交
1107 1108
static int
remoteDispatchDomainMemoryPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
1109 1110
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
1111
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
1112
                                remote_error *rerr,
R
Richard W.M. Jones 已提交
1113 1114 1115 1116 1117 1118 1119 1120
                                remote_domain_memory_peek_args *args,
                                remote_domain_memory_peek_ret *ret)
{
    virDomainPtr dom;
    unsigned long long offset;
    size_t size;
    unsigned int flags;

1121
    dom = get_nonnull_domain (conn, args->dom);
R
Richard W.M. Jones 已提交
1122
    if (dom == NULL) {
1123
        remoteDispatchConnError(rerr, conn);
1124
        return -1;
R
Richard W.M. Jones 已提交
1125 1126 1127 1128 1129 1130
    }
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1131
        virDomainFree (dom);
1132 1133 1134
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
        return -1;
R
Richard W.M. Jones 已提交
1135 1136 1137 1138 1139
    }

    ret->buffer.buffer_len = size;
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
        virDomainFree (dom);
1140
        remoteDispatchOOMError(rerr);
1141
        return -1;
R
Richard W.M. Jones 已提交
1142 1143 1144 1145 1146
    }

    if (virDomainMemoryPeek (dom, offset, size,
                             ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
1147
        remoteDispatchConnError(rerr, conn);
1148
        virDomainFree (dom);
R
Richard W.M. Jones 已提交
1149 1150 1151 1152 1153 1154 1155
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

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

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

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

J
Jim Fehlig 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
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) {
        remoteDispatchConnError(rerr, conn);
1201
        virDomainFree(dom);
J
Jim Fehlig 已提交
1202 1203 1204 1205 1206 1207
        return -1;
    }
    virDomainFree(dom);
    return 0;
}

1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
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) {
        remoteDispatchConnError(rerr, conn);
1227
        virDomainFree(dom);
1228 1229 1230 1231 1232 1233
        return -1;
    }
    virDomainFree(dom);
    return 0;
}

1234
static int
1235
remoteDispatchDomainCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
1236 1237
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
1238
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
1239
                            remote_error *rerr,
1240 1241 1242 1243 1244
                            remote_domain_create_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

1251
    if (virDomainCreate (dom) == -1) {
1252
        remoteDispatchConnError(rerr, conn);
1253
        virDomainFree(dom);
1254
        return -1;
1255 1256
    }
    virDomainFree(dom);
1257 1258 1259
    return 0;
}

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
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;
    }

1277
    if (virDomainCreateWithFlags (dom, args->flags) == -1) {
1278
        remoteDispatchConnError(rerr, conn);
1279
        virDomainFree(dom);
1280 1281 1282 1283 1284 1285 1286 1287
        return -1;
    }

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

1288
static int
1289
remoteDispatchDomainCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1290 1291
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1292
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1293 1294 1295
                               remote_error *rerr,
                               remote_domain_create_xml_args *args,
                               remote_domain_create_xml_ret *ret)
1296 1297 1298
{
    virDomainPtr dom;

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

    make_nonnull_domain (&ret->dom, dom);
1306
    virDomainFree(dom);
1307 1308 1309 1310 1311

    return 0;
}

static int
1312
remoteDispatchDomainDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1313 1314
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1315
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1316
                               remote_error *rerr,
1317 1318 1319 1320 1321
                               remote_domain_define_xml_args *args,
                               remote_domain_define_xml_ret *ret)
{
    virDomainPtr dom;

1322
    dom = virDomainDefineXML (conn, args->xml);
1323
    if (dom == NULL) {
1324
        remoteDispatchConnError(rerr, conn);
1325 1326
        return -1;
    }
1327 1328

    make_nonnull_domain (&ret->dom, dom);
1329
    virDomainFree(dom);
1330 1331 1332 1333 1334

    return 0;
}

static int
1335
remoteDispatchDomainDestroy (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_destroy_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
    if (virDomainDestroy (dom) == -1) {
1352
        remoteDispatchConnError(rerr, conn);
1353
        virDomainFree(dom);
1354
        return -1;
1355 1356
    }
    virDomainFree(dom);
1357 1358 1359 1360
    return 0;
}

static int
1361
remoteDispatchDomainDetachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
1362 1363
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1364
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1365
                                  remote_error *rerr,
1366 1367 1368 1369 1370
                                  remote_domain_detach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1371
    dom = get_nonnull_domain (conn, args->dom);
1372
    if (dom == NULL) {
1373
        remoteDispatchConnError(rerr, conn);
1374
        return -1;
1375 1376
    }

1377
    if (virDomainDetachDevice (dom, args->xml) == -1) {
J
Jim Fehlig 已提交
1378
        remoteDispatchConnError(rerr, conn);
1379
        virDomainFree(dom);
J
Jim Fehlig 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
        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) {
1405
        remoteDispatchConnError(rerr, conn);
1406
        virDomainFree(dom);
1407
        return -1;
1408
    }
1409

1410
    virDomainFree(dom);
1411 1412 1413 1414
    return 0;
}

static int
1415
remoteDispatchDomainDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1416 1417
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1418
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1419
                             remote_error *rerr,
1420 1421 1422 1423 1424
                             remote_domain_dump_xml_args *args,
                             remote_domain_dump_xml_ret *ret)
{
    virDomainPtr dom;

1425
    dom = get_nonnull_domain (conn, args->dom);
1426
    if (dom == NULL) {
1427
        remoteDispatchConnError(rerr, conn);
1428
        return -1;
1429 1430 1431 1432
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainGetXMLDesc (dom, args->flags);
1433
    if (!ret->xml) {
1434
        remoteDispatchConnError(rerr, conn);
1435
        virDomainFree(dom);
1436
        return -1;
1437 1438
    }
    virDomainFree(dom);
1439 1440 1441
    return 0;
}

1442 1443 1444 1445
static int
remoteDispatchDomainXmlFromNative (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1446
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
                                   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,
1467
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
                                 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;
}


1485
static int
1486
remoteDispatchDomainGetAutostart (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
                                  remote_domain_get_autostart_args *args,
                                  remote_domain_get_autostart_ret *ret)
{
    virDomainPtr dom;

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

1502
    if (virDomainGetAutostart (dom, &ret->autostart) == -1) {
1503
        remoteDispatchConnError(rerr, conn);
1504
        virDomainFree(dom);
1505
        return -1;
1506 1507
    }
    virDomainFree(dom);
1508 1509 1510 1511
    return 0;
}

static int
1512
remoteDispatchDomainGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
1513 1514
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1515
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1516
                             remote_error *rerr,
1517 1518 1519 1520 1521 1522
                             remote_domain_get_info_args *args,
                             remote_domain_get_info_ret *ret)
{
    virDomainPtr dom;
    virDomainInfo info;

1523
    dom = get_nonnull_domain (conn, args->dom);
1524
    if (dom == NULL) {
1525
        remoteDispatchConnError(rerr, conn);
1526
        return -1;
1527 1528
    }

1529
    if (virDomainGetInfo (dom, &info) == -1) {
1530
        remoteDispatchConnError(rerr, conn);
1531
        virDomainFree(dom);
1532
        return -1;
1533
    }
1534 1535 1536 1537 1538 1539 1540

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

1541 1542
    virDomainFree(dom);

1543 1544 1545 1546
    return 0;
}

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

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

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

static int
1574
remoteDispatchDomainGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1575 1576
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
1577
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
1578
                                 remote_error *rerr,
1579 1580 1581 1582 1583
                                 remote_domain_get_max_vcpus_args *args,
                                 remote_domain_get_max_vcpus_ret *ret)
{
    virDomainPtr dom;

1584
    dom = get_nonnull_domain (conn, args->dom);
1585
    if (dom == NULL) {
1586
        remoteDispatchConnError(rerr, conn);
1587
        return -1;
1588 1589 1590
    }

    ret->num = virDomainGetMaxVcpus (dom);
1591
    if (ret->num == -1) {
1592
        remoteDispatchConnError(rerr, conn);
1593
        virDomainFree(dom);
1594 1595 1596
        return -1;
    }
    virDomainFree(dom);
1597 1598 1599
    return 0;
}

1600 1601 1602 1603
static int
remoteDispatchDomainGetSecurityLabel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1604
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
                                     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) {
1620
        remoteDispatchConnError(rerr, conn);
1621
        virDomainFree(dom);
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
        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,
1642
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1643 1644 1645 1646 1647 1648 1649 1650
                                   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) {
1651
        remoteDispatchConnError(rerr, conn);
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
        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;
}

1672
static int
1673
remoteDispatchDomainGetOsType (struct qemud_server *server ATTRIBUTE_UNUSED,
1674 1675
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1676
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1677
                               remote_error *rerr,
1678 1679 1680 1681 1682
                               remote_domain_get_os_type_args *args,
                               remote_domain_get_os_type_ret *ret)
{
    virDomainPtr dom;

1683
    dom = get_nonnull_domain (conn, args->dom);
1684
    if (dom == NULL) {
1685
        remoteDispatchConnError(rerr, conn);
1686
        return -1;
1687 1688 1689 1690
    }

    /* remoteDispatchClientRequest will free this */
    ret->type = virDomainGetOSType (dom);
1691
    if (ret->type == NULL) {
1692
        remoteDispatchConnError(rerr, conn);
1693
        virDomainFree(dom);
1694
        return -1;
1695 1696
    }
    virDomainFree(dom);
1697 1698 1699 1700
    return 0;
}

static int
1701
remoteDispatchDomainGetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1702 1703
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
1704
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
1705
                              remote_error *rerr,
1706 1707 1708
                              remote_domain_get_vcpus_args *args,
                              remote_domain_get_vcpus_ret *ret)
{
1709 1710 1711
    virDomainPtr dom = NULL;
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
1712 1713
    int info_len, i;

1714
    dom = get_nonnull_domain (conn, args->dom);
1715
    if (dom == NULL) {
1716
        remoteDispatchConnError(rerr, conn);
1717
        return -1;
1718 1719 1720
    }

    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1721
        virDomainFree(dom);
1722 1723
        remoteDispatchFormatError (rerr, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
        return -1;
1724 1725
    }

1726
    if (args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1727
        virDomainFree(dom);
1728 1729
        remoteDispatchFormatError (rerr, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
        return -1;
1730 1731 1732
    }

    /* Allocate buffers to take the results. */
1733 1734
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
        goto oom;
1735 1736
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
1737
        goto oom;
1738 1739 1740 1741

    info_len = virDomainGetVcpus (dom,
                                  info, args->maxinfo,
                                  cpumaps, args->maplen);
1742
    if (info_len == -1) {
1743
        remoteDispatchConnError(rerr, conn);
1744 1745
        VIR_FREE(info);
        VIR_FREE(cpumaps);
1746 1747 1748
        virDomainFree(dom);
        return -1;
    }
1749 1750 1751

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
1752 1753
    if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
        goto oom;
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765

    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.
     */
1766
    ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
1767 1768
    ret->cpumaps.cpumaps_val = (char *) cpumaps;

1769
    VIR_FREE(info);
1770
    virDomainFree(dom);
1771
    return 0;
1772 1773 1774 1775 1776

oom:
    VIR_FREE(info);
    VIR_FREE(cpumaps);
    virDomainFree(dom);
1777 1778
    remoteDispatchOOMError(rerr);
    return -1;
1779 1780
}

E
Eric Blake 已提交
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
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) {
        remoteDispatchConnError(rerr, conn);
1801
        virDomainFree(dom);
E
Eric Blake 已提交
1802 1803 1804 1805 1806 1807
        return -1;
    }
    virDomainFree(dom);
    return 0;
}

1808
static int
1809
remoteDispatchDomainMigratePrepare (struct qemud_server *server ATTRIBUTE_UNUSED,
1810 1811
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1812
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1813
                                    remote_error *rerr,
1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
                                    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 ... */
1828
    if (VIR_ALLOC(uri_out) < 0) {
1829 1830
        remoteDispatchOOMError(rerr);
        return -1;
1831
    }
1832

1833
    r = virDomainMigratePrepare (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1834 1835
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1836
    if (r == -1) {
1837
        VIR_FREE(uri_out);
1838
        remoteDispatchConnError(rerr, conn);
D
Daniel P. Berrange 已提交
1839 1840
        return -1;
    }
1841 1842 1843 1844 1845 1846

    /* 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 已提交
1847 1848
    if (*uri_out == NULL) {
        ret->uri_out = NULL;
1849
        VIR_FREE(uri_out);
D
Daniel P. Berrange 已提交
1850 1851 1852
    } else {
        ret->uri_out = uri_out;
    }
1853 1854 1855 1856 1857

    return 0;
}

static int
1858
remoteDispatchDomainMigratePerform (struct qemud_server *server ATTRIBUTE_UNUSED,
1859 1860
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1861
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1862
                                    remote_error *rerr,
1863 1864 1865 1866 1867
                                    remote_domain_migrate_perform_args *args,
                                    void *ret ATTRIBUTE_UNUSED)
{
    int r;
    virDomainPtr dom;
1868
    char *dname;
1869

1870
    dom = get_nonnull_domain (conn, args->dom);
1871
    if (dom == NULL) {
1872
        remoteDispatchConnError(rerr, conn);
1873
        return -1;
1874 1875 1876 1877
    }

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

D
Daniel P. Berrange 已提交
1878 1879 1880 1881 1882
    r = virDomainMigratePerform (dom,
                                 args->cookie.cookie_val,
                                 args->cookie.cookie_len,
                                 args->uri,
                                 args->flags, dname, args->resource);
1883
    if (r == -1) {
1884
        remoteDispatchConnError(rerr, conn);
1885
        virDomainFree (dom);
1886 1887
        return -1;
    }
1888
    virDomainFree (dom);
1889 1890 1891 1892 1893

    return 0;
}

static int
1894
remoteDispatchDomainMigrateFinish (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,
1899 1900 1901 1902 1903 1904
                                   remote_domain_migrate_finish_args *args,
                                   remote_domain_migrate_finish_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

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

    make_nonnull_domain (&ret->ddom, ddom);
D
Daniel P. Berrange 已提交
1916
    virDomainFree (ddom);
1917 1918 1919
    return 0;
}

D
Daniel Veillard 已提交
1920 1921
static int
remoteDispatchDomainMigratePrepare2 (struct qemud_server *server ATTRIBUTE_UNUSED,
1922 1923
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1924
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
1925
                                     remote_error *rerr,
D
Daniel Veillard 已提交
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
                                     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) {
1942 1943
        remoteDispatchOOMError(rerr);
        return -1;
D
Daniel Veillard 已提交
1944 1945
    }

1946
    r = virDomainMigratePrepare2 (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1947 1948 1949
                                  uri_in, uri_out,
                                  args->flags, dname, args->resource,
                                  args->dom_xml);
1950
    if (r == -1) {
1951
        remoteDispatchConnError(rerr, conn);
1952 1953
        return -1;
    }
D
Daniel Veillard 已提交
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966

    /* 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,
1967 1968
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1969
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1970
                                    remote_error *rerr,
D
Daniel Veillard 已提交
1971 1972 1973 1974 1975 1976
                                    remote_domain_migrate_finish2_args *args,
                                    remote_domain_migrate_finish2_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

1977
    ddom = virDomainMigrateFinish2 (conn, args->dname,
D
Daniel P. Berrange 已提交
1978 1979 1980 1981 1982
                                    args->cookie.cookie_val,
                                    args->cookie.cookie_len,
                                    args->uri,
                                    args->flags,
                                    args->retcode);
1983
    if (ddom == NULL) {
1984
        remoteDispatchConnError(rerr, conn);
1985 1986
        return -1;
    }
D
Daniel Veillard 已提交
1987 1988

    make_nonnull_domain (&ret->ddom, ddom);
1989
    virDomainFree (ddom);
D
Daniel Veillard 已提交
1990 1991 1992 1993

    return 0;
}

C
Chris Lalancette 已提交
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
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;
    }

2016
    r = virDomainMigratePrepareTunnel(conn, stream->st,
C
Chris Lalancette 已提交
2017 2018 2019 2020
                                      args->flags, dname, args->resource,
                                      args->dom_xml);
    if (r == -1) {
        remoteDispatchConnError(rerr, conn);
2021
        remoteFreeClientStream(client, stream);
C
Chris Lalancette 已提交
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
        return -1;
    }

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

    return 0;
}

2035
static int
2036
remoteDispatchListDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2037 2038
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2039
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2040
                                  remote_error *rerr,
2041 2042 2043 2044 2045
                                  remote_list_defined_domains_args *args,
                                  remote_list_defined_domains_ret *ret)
{

    if (args->maxnames > REMOTE_DOMAIN_NAME_LIST_MAX) {
2046 2047 2048
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_DOMAIN_NAME_LIST_MAX"));
        return -1;
2049 2050 2051
    }

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

    ret->names.names_len =
2058
        virConnectListDefinedDomains (conn,
2059
                                      ret->names.names_val, args->maxnames);
2060 2061
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
2062
        remoteDispatchConnError(rerr, conn);
2063 2064
        return -1;
    }
2065 2066 2067 2068 2069

    return 0;
}

static int
2070
remoteDispatchDomainLookupById (struct qemud_server *server ATTRIBUTE_UNUSED,
2071 2072
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2073
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
2074
                                remote_error *rerr,
2075 2076 2077 2078 2079
                                remote_domain_lookup_by_id_args *args,
                                remote_domain_lookup_by_id_ret *ret)
{
    virDomainPtr dom;

2080
    dom = virDomainLookupByID (conn, args->id);
2081
    if (dom == NULL) {
2082
        remoteDispatchConnError(rerr, conn);
2083 2084
        return -1;
    }
2085 2086

    make_nonnull_domain (&ret->dom, dom);
2087
    virDomainFree(dom);
2088 2089 2090 2091
    return 0;
}

static int
2092
remoteDispatchDomainLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
2093 2094
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2095
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2096
                                  remote_error *rerr,
2097 2098 2099 2100 2101
                                  remote_domain_lookup_by_name_args *args,
                                  remote_domain_lookup_by_name_ret *ret)
{
    virDomainPtr dom;

2102
    dom = virDomainLookupByName (conn, args->name);
2103
    if (dom == NULL) {
2104
        remoteDispatchConnError(rerr, conn);
2105 2106
        return -1;
    }
2107 2108

    make_nonnull_domain (&ret->dom, dom);
2109
    virDomainFree(dom);
2110 2111 2112 2113
    return 0;
}

static int
2114
remoteDispatchDomainLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
2115 2116
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2117
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2118
                                  remote_error *rerr,
2119 2120 2121 2122 2123
                                  remote_domain_lookup_by_uuid_args *args,
                                  remote_domain_lookup_by_uuid_ret *ret)
{
    virDomainPtr dom;

2124
    dom = virDomainLookupByUUID (conn, (unsigned char *) args->uuid);
2125
    if (dom == NULL) {
2126
        remoteDispatchConnError(rerr, conn);
2127 2128
        return -1;
    }
2129 2130

    make_nonnull_domain (&ret->dom, dom);
2131
    virDomainFree(dom);
2132 2133 2134 2135
    return 0;
}

static int
2136
remoteDispatchNumOfDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2137 2138
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2139
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2140
                                   remote_error *rerr,
2141 2142 2143 2144
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_num_of_defined_domains_ret *ret)
{

2145
    ret->num = virConnectNumOfDefinedDomains (conn);
2146
    if (ret->num == -1) {
2147
        remoteDispatchConnError(rerr, conn);
2148 2149
        return -1;
    }
2150 2151 2152 2153 2154

    return 0;
}

static int
2155
remoteDispatchDomainPinVcpu (struct qemud_server *server ATTRIBUTE_UNUSED,
2156 2157
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2158
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2159
                             remote_error *rerr,
2160 2161 2162 2163 2164 2165
                             remote_domain_pin_vcpu_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int rv;

2166
    dom = get_nonnull_domain (conn, args->dom);
2167
    if (dom == NULL) {
2168
        remoteDispatchConnError(rerr, conn);
2169
        return -1;
2170 2171 2172
    }

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
2173
        virDomainFree(dom);
2174 2175
        remoteDispatchFormatError (rerr, "%s", _("cpumap_len > REMOTE_CPUMAP_MAX"));
        return -1;
2176 2177 2178 2179 2180
    }

    rv = virDomainPinVcpu (dom, args->vcpu,
                           (unsigned char *) args->cpumap.cpumap_val,
                           args->cpumap.cpumap_len);
2181
    if (rv == -1) {
2182
        remoteDispatchConnError(rerr, conn);
2183
        virDomainFree(dom);
2184 2185 2186
        return -1;
    }
    virDomainFree(dom);
2187 2188 2189 2190
    return 0;
}

static int
2191
remoteDispatchDomainReboot (struct qemud_server *server ATTRIBUTE_UNUSED,
2192 2193
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2194
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2195
                            remote_error *rerr,
2196 2197 2198 2199 2200
                            remote_domain_reboot_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2201
    dom = get_nonnull_domain (conn, args->dom);
2202
    if (dom == NULL) {
2203
        remoteDispatchConnError(rerr, conn);
2204
        return -1;
2205 2206
    }

2207
    if (virDomainReboot (dom, args->flags) == -1) {
2208
        remoteDispatchConnError(rerr, conn);
2209
        virDomainFree(dom);
2210
        return -1;
2211 2212
    }
    virDomainFree(dom);
2213 2214 2215 2216
    return 0;
}

static int
2217
remoteDispatchDomainRestore (struct qemud_server *server ATTRIBUTE_UNUSED,
2218 2219
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2220
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2221
                             remote_error *rerr,
2222 2223 2224
                             remote_domain_restore_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
2225

2226
    if (virDomainRestore (conn, args->from) == -1) {
2227
        remoteDispatchConnError(rerr, conn);
2228
        return -1;
2229
    }
2230 2231 2232 2233 2234

    return 0;
}

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

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

2251
    if (virDomainResume (dom) == -1) {
2252
        remoteDispatchConnError(rerr, conn);
2253
        virDomainFree(dom);
2254
        return -1;
2255 2256
    }
    virDomainFree(dom);
2257 2258 2259 2260
    return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

static int
2365
remoteDispatchDomainSetMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
2366 2367
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
2368
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
2369
                               remote_error *rerr,
2370 2371 2372 2373 2374
                               remote_domain_set_memory_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2375
    dom = get_nonnull_domain (conn, args->dom);
2376
    if (dom == NULL) {
2377
        remoteDispatchConnError(rerr, conn);
2378
        return -1;
2379 2380
    }

2381
    if (virDomainSetMemory (dom, args->memory) == -1) {
2382
        remoteDispatchConnError(rerr, conn);
2383
        virDomainFree(dom);
2384
        return -1;
2385 2386
    }
    virDomainFree(dom);
2387 2388 2389
    return 0;
}

2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
static int
remoteDispatchDomainSetMemoryFlags (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_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 (virDomainSetMemoryFlags (dom, args->memory, args->flags) == -1) {
        remoteDispatchConnError(rerr, conn);
2409
        virDomainFree(dom);
2410 2411 2412 2413 2414 2415
        return -1;
    }
   virDomainFree(dom);
   return 0;
}

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
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);
    VIR_FREE(params);
    if (r == -1) {
        remoteDispatchConnError(rerr, conn);
2501
        virDomainFree(dom);
2502 2503
        return -1;
    }
2504
    virDomainFree(dom);
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

    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) {
2549
        remoteDispatchConnError(rerr, conn);
2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567
        virDomainFree(dom);
        VIR_FREE(params);
        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) {
2568
        /* remoteDispatchClientRequest will free this: */
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603
        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;
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
                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;
}

static int
remoteDispatchDomainSetBlkioParameters(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_blkio_parameters_args
                                        * args, void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int i, r, nparams;
    virBlkioParameterPtr params;
    unsigned int flags;

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

    if (nparams > REMOTE_DOMAIN_BLKIO_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_BLKIO_PARAM_INT:
                params[i].value.i =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.i;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_UINT:
                params[i].value.ui =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.ui;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_LLONG:
                params[i].value.l =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.l;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_ULLONG:
                params[i].value.ul =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.ul;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_DOUBLE:
                params[i].value.d =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.d;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_BOOLEAN:
                params[i].value.b =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.b;
                break;
        }
    }

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

    r = virDomainSetBlkioParameters(dom, params, nparams, flags);
    VIR_FREE(params);
    if (r == -1) {
        remoteDispatchConnError(rerr, conn);
2712
        virDomainFree(dom);
2713 2714
        return -1;
    }
2715
    virDomainFree(dom);
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759

    return 0;
}

static int
remoteDispatchDomainGetBlkioParameters(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_blkio_parameters_args
                                        * args,
                                        remote_domain_get_blkio_parameters_ret
                                        * ret)
{
    virDomainPtr dom;
    virBlkioParameterPtr params;
    int i, r, nparams;
    unsigned int flags;

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

    if (nparams > REMOTE_DOMAIN_BLKIO_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 = virDomainGetBlkioParameters(dom, params, &nparams, flags);
    if (r == -1) {
2760
        remoteDispatchConnError(rerr, conn);
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 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
        virDomainFree(dom);
        VIR_FREE(params);
        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 blkio 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_BLKIO_PARAM_INT:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.i =
                    params[i].value.i;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_UINT:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.ui =
                    params[i].value.ui;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_LLONG:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.l =
                    params[i].value.l;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_ULLONG:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.ul =
                    params[i].value.ul;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_DOUBLE:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.d =
                    params[i].value.d;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_BOOLEAN:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.b =
                    params[i].value.b;
2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837
                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;
}

2838
static int
2839
remoteDispatchDomainSetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
2840 2841
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2842
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2843
                              remote_error *rerr,
2844 2845 2846 2847 2848
                              remote_domain_set_vcpus_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2849
    dom = get_nonnull_domain (conn, args->dom);
2850
    if (dom == NULL) {
2851
        remoteDispatchConnError(rerr, conn);
2852
        return -1;
2853 2854
    }

2855
    if (virDomainSetVcpus (dom, args->nvcpus) == -1) {
2856
        remoteDispatchConnError(rerr, conn);
2857
        virDomainFree(dom);
2858
        return -1;
2859 2860
    }
    virDomainFree(dom);
2861 2862 2863
    return 0;
}

E
Eric Blake 已提交
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882
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) {
        remoteDispatchConnError(rerr, conn);
2883
        virDomainFree(dom);
E
Eric Blake 已提交
2884 2885 2886 2887 2888 2889
        return -1;
    }
    virDomainFree(dom);
    return 0;
}

2890
static int
2891
remoteDispatchDomainShutdown (struct qemud_server *server ATTRIBUTE_UNUSED,
2892 2893
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2894
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2895
                              remote_error *rerr,
2896 2897 2898 2899 2900
                              remote_domain_shutdown_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2901
    dom = get_nonnull_domain (conn, args->dom);
2902
    if (dom == NULL) {
2903
        remoteDispatchConnError(rerr, conn);
2904
        return -1;
2905 2906
    }

2907
    if (virDomainShutdown (dom) == -1) {
2908
        remoteDispatchConnError(rerr, conn);
2909
        virDomainFree(dom);
2910
        return -1;
2911 2912
    }
    virDomainFree(dom);
2913 2914 2915 2916
    return 0;
}

static int
2917
remoteDispatchDomainSuspend (struct qemud_server *server ATTRIBUTE_UNUSED,
2918 2919
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2920
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2921
                             remote_error *rerr,
2922 2923 2924 2925 2926
                             remote_domain_suspend_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2927
    dom = get_nonnull_domain (conn, args->dom);
2928
    if (dom == NULL) {
2929
        remoteDispatchConnError(rerr, conn);
2930
        return -1;
2931 2932
    }

2933
    if (virDomainSuspend (dom) == -1) {
2934
        remoteDispatchConnError(rerr, conn);
2935
        virDomainFree(dom);
2936
        return -1;
2937 2938
    }
    virDomainFree(dom);
2939 2940 2941 2942
    return 0;
}

static int
2943
remoteDispatchDomainUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
2944 2945
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2946
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2947
                              remote_error *rerr,
2948 2949 2950 2951 2952
                              remote_domain_undefine_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2953
    dom = get_nonnull_domain (conn, args->dom);
2954
    if (dom == NULL) {
2955
        remoteDispatchConnError(rerr, conn);
2956
        return -1;
2957 2958
    }

2959
    if (virDomainUndefine (dom) == -1) {
2960
        remoteDispatchConnError(rerr, conn);
2961
        virDomainFree(dom);
2962
        return -1;
2963 2964
    }
    virDomainFree(dom);
2965 2966 2967 2968
    return 0;
}

static int
2969
remoteDispatchListDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2970 2971
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2972
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2973
                                   remote_error *rerr,
2974 2975 2976 2977 2978
                                   remote_list_defined_networks_args *args,
                                   remote_list_defined_networks_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
2979 2980 2981
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
2982 2983 2984
    }

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

    ret->names.names_len =
2991
        virConnectListDefinedNetworks (conn,
2992
                                       ret->names.names_val, args->maxnames);
2993 2994
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
2995
        remoteDispatchConnError(rerr, conn);
2996 2997
        return -1;
    }
2998 2999 3000 3001 3002

    return 0;
}

static int
3003
remoteDispatchListDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
3004 3005
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
3006
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
3007
                           remote_error *rerr,
3008 3009 3010 3011 3012
                           remote_list_domains_args *args,
                           remote_list_domains_ret *ret)
{

    if (args->maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
3013 3014 3015
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxids > REMOTE_DOMAIN_ID_LIST_MAX"));
        return -1;
3016 3017 3018
    }

    /* Allocate return buffer. */
3019
    if (VIR_ALLOC_N(ret->ids.ids_val, args->maxids) < 0) {
3020 3021
        remoteDispatchOOMError(rerr);
        return -1;
3022
    }
3023

3024
    ret->ids.ids_len = virConnectListDomains (conn,
3025
                                              ret->ids.ids_val, args->maxids);
3026 3027
    if (ret->ids.ids_len == -1) {
        VIR_FREE(ret->ids.ids_val);
3028
        remoteDispatchConnError(rerr, conn);
3029 3030
        return -1;
    }
3031 3032 3033 3034

    return 0;
}

3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051
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;
    }

3052
    if (virDomainManagedSave (dom, args->flags) == -1) {
3053
        remoteDispatchConnError(rerr, conn);
3054
        virDomainFree(dom);
3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
        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) {
        remoteDispatchConnError(rerr, conn);
3081
        virDomainFree(dom);
3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106
        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) {
        remoteDispatchConnError(rerr, conn);
3107
        virDomainFree(dom);
3108 3109 3110 3111 3112 3113
        return -1;
    }
    virDomainFree(dom);
    return 0;
}

3114
static int
3115
remoteDispatchListNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
3116 3117
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
3118
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3119
                            remote_error *rerr,
3120 3121 3122 3123 3124
                            remote_list_networks_args *args,
                            remote_list_networks_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
3125 3126 3127
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
3128 3129 3130
    }

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

    ret->names.names_len =
3137
        virConnectListNetworks (conn,
3138
                                ret->names.names_val, args->maxnames);
3139 3140
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_len);
3141
        remoteDispatchConnError(rerr, conn);
3142 3143
        return -1;
    }
3144 3145 3146 3147 3148

    return 0;
}

static int
3149
remoteDispatchNetworkCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
3150 3151
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
3152
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
3153
                             remote_error *rerr,
3154 3155 3156 3157 3158
                             remote_network_create_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

3159
    net = get_nonnull_network (conn, args->net);
3160
    if (net == NULL) {
3161
        remoteDispatchConnError(rerr, conn);
3162
        return -1;
3163 3164
    }

3165
    if (virNetworkCreate (net) == -1) {
3166
        remoteDispatchConnError(rerr, conn);
3167
        virNetworkFree(net);
3168
        return -1;
3169 3170
    }
    virNetworkFree(net);
3171 3172 3173 3174
    return 0;
}

static int
3175
remoteDispatchNetworkCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
3176 3177
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
3178
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
3179
                                remote_error *rerr,
3180 3181 3182 3183 3184
                                remote_network_create_xml_args *args,
                                remote_network_create_xml_ret *ret)
{
    virNetworkPtr net;

3185
    net = virNetworkCreateXML (conn, args->xml);
3186
    if (net == NULL) {
3187
        remoteDispatchConnError(rerr, conn);
3188 3189
        return -1;
    }
3190 3191

    make_nonnull_network (&ret->net, net);
3192
    virNetworkFree(net);
3193 3194 3195 3196
    return 0;
}

static int
3197
remoteDispatchNetworkDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
3198 3199
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
3200
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
3201
                                remote_error *rerr,
3202 3203 3204 3205 3206
                                remote_network_define_xml_args *args,
                                remote_network_define_xml_ret *ret)
{
    virNetworkPtr net;

3207
    net = virNetworkDefineXML (conn, args->xml);
3208
    if (net == NULL) {
3209
        remoteDispatchConnError(rerr, conn);
3210 3211
        return -1;
    }
3212 3213

    make_nonnull_network (&ret->net, net);
3214
    virNetworkFree(net);
3215 3216 3217 3218
    return 0;
}

static int
3219
remoteDispatchNetworkDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
3220 3221
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
3222
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
3223
                              remote_error *rerr,
3224 3225 3226 3227 3228
                              remote_network_destroy_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

3229
    net = get_nonnull_network (conn, args->net);
3230
    if (net == NULL) {
3231
        remoteDispatchConnError(rerr, conn);
3232
        return -1;
3233 3234
    }

3235
    if (virNetworkDestroy (net) == -1) {
3236
        remoteDispatchConnError(rerr, conn);
3237
        virNetworkFree(net);
3238
        return -1;
3239 3240
    }
    virNetworkFree(net);
3241 3242 3243 3244
    return 0;
}

static int
3245
remoteDispatchNetworkDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
3246 3247
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
3248
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
3249
                              remote_error *rerr,
3250 3251 3252 3253 3254
                              remote_network_dump_xml_args *args,
                              remote_network_dump_xml_ret *ret)
{
    virNetworkPtr net;

3255
    net = get_nonnull_network (conn, args->net);
3256
    if (net == NULL) {
3257
        remoteDispatchConnError(rerr, conn);
3258
        return -1;
3259 3260 3261 3262
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNetworkGetXMLDesc (net, args->flags);
3263
    if (!ret->xml) {
3264
        remoteDispatchConnError(rerr, conn);
3265
        virNetworkFree(net);
3266 3267 3268
        return -1;
    }
    virNetworkFree(net);
3269 3270 3271 3272
    return 0;
}

static int
3273
remoteDispatchNetworkGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
3274 3275
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3276
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
3277
                                   remote_error *rerr,
3278 3279 3280 3281 3282
                                   remote_network_get_autostart_args *args,
                                   remote_network_get_autostart_ret *ret)
{
    virNetworkPtr net;

3283
    net = get_nonnull_network (conn, args->net);
3284
    if (net == NULL) {
3285
        remoteDispatchConnError(rerr, conn);
3286
        return -1;
3287 3288
    }

3289
    if (virNetworkGetAutostart (net, &ret->autostart) == -1) {
3290
        remoteDispatchConnError(rerr, conn);
3291
        virNetworkFree(net);
3292
        return -1;
3293 3294
    }
    virNetworkFree(net);
3295 3296 3297 3298
    return 0;
}

static int
3299
remoteDispatchNetworkGetBridgeName (struct qemud_server *server ATTRIBUTE_UNUSED,
3300 3301
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
3302
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
3303
                                    remote_error *rerr,
3304 3305 3306 3307 3308
                                    remote_network_get_bridge_name_args *args,
                                    remote_network_get_bridge_name_ret *ret)
{
    virNetworkPtr net;

3309
    net = get_nonnull_network (conn, args->net);
3310
    if (net == NULL) {
3311
        remoteDispatchConnError(rerr, conn);
3312
        return -1;
3313 3314 3315 3316
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virNetworkGetBridgeName (net);
3317
    if (!ret->name) {
3318
        remoteDispatchConnError(rerr, conn);
3319
        virNetworkFree(net);
3320 3321 3322
        return -1;
    }
    virNetworkFree(net);
3323 3324 3325 3326
    return 0;
}

static int
3327
remoteDispatchNetworkLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
3328 3329
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3330
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
3331
                                   remote_error *rerr,
3332 3333 3334 3335 3336
                                   remote_network_lookup_by_name_args *args,
                                   remote_network_lookup_by_name_ret *ret)
{
    virNetworkPtr net;

3337
    net = virNetworkLookupByName (conn, args->name);
3338
    if (net == NULL) {
3339
        remoteDispatchConnError(rerr, conn);
3340 3341
        return -1;
    }
3342 3343

    make_nonnull_network (&ret->net, net);
3344
    virNetworkFree(net);
3345 3346 3347 3348
    return 0;
}

static int
3349
remoteDispatchNetworkLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
3350 3351
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3352
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
3353
                                   remote_error *rerr,
3354 3355 3356 3357 3358
                                   remote_network_lookup_by_uuid_args *args,
                                   remote_network_lookup_by_uuid_ret *ret)
{
    virNetworkPtr net;

3359
    net = virNetworkLookupByUUID (conn, (unsigned char *) args->uuid);
3360
    if (net == NULL) {
3361
        remoteDispatchConnError(rerr, conn);
3362 3363
        return -1;
    }
3364 3365

    make_nonnull_network (&ret->net, net);
3366
    virNetworkFree(net);
3367 3368 3369 3370
    return 0;
}

static int
3371
remoteDispatchNetworkSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
3372 3373
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3374
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
3375
                                   remote_error *rerr,
3376 3377 3378 3379 3380
                                   remote_network_set_autostart_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

3381
    net = get_nonnull_network (conn, args->net);
3382
    if (net == NULL) {
3383
        remoteDispatchConnError(rerr, conn);
3384
        return -1;
3385 3386
    }

3387
    if (virNetworkSetAutostart (net, args->autostart) == -1) {
3388
        remoteDispatchConnError(rerr, conn);
3389
        virNetworkFree(net);
3390
        return -1;
3391 3392
    }
    virNetworkFree(net);
3393 3394 3395 3396
    return 0;
}

static int
3397
remoteDispatchNetworkUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
3398 3399
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
3400
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
3401
                               remote_error *rerr,
3402 3403 3404 3405 3406
                               remote_network_undefine_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

3407
    net = get_nonnull_network (conn, args->net);
3408
    if (net == NULL) {
3409
        remoteDispatchConnError(rerr, conn);
3410
        return -1;
3411 3412
    }

3413
    if (virNetworkUndefine (net) == -1) {
3414
        remoteDispatchConnError(rerr, conn);
3415
        virNetworkFree(net);
3416
        return -1;
3417 3418
    }
    virNetworkFree(net);
3419 3420 3421 3422
    return 0;
}

static int
3423
remoteDispatchNumOfDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
3424 3425
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
3426
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
3427
                                    remote_error *rerr,
3428 3429 3430 3431
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_num_of_defined_networks_ret *ret)
{

3432
    ret->num = virConnectNumOfDefinedNetworks (conn);
3433
    if (ret->num == -1) {
3434
        remoteDispatchConnError(rerr, conn);
3435 3436
        return -1;
    }
3437 3438 3439 3440 3441

    return 0;
}

static int
3442
remoteDispatchNumOfDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
3443 3444
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
3445
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3446
                            remote_error *rerr,
3447 3448 3449 3450
                            void *args ATTRIBUTE_UNUSED,
                            remote_num_of_domains_ret *ret)
{

3451
    ret->num = virConnectNumOfDomains (conn);
3452
    if (ret->num == -1) {
3453
        remoteDispatchConnError(rerr, conn);
3454 3455
        return -1;
    }
3456 3457 3458 3459 3460

    return 0;
}

static int
3461
remoteDispatchNumOfNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
3462 3463
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
3464
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
3465
                             remote_error *rerr,
3466 3467 3468 3469
                             void *args ATTRIBUTE_UNUSED,
                             remote_num_of_networks_ret *ret)
{

3470
    ret->num = virConnectNumOfNetworks (conn);
3471
    if (ret->num == -1) {
3472
        remoteDispatchConnError(rerr, conn);
3473 3474
        return -1;
    }
3475 3476 3477 3478

    return 0;
}

3479

D
Daniel Veillard 已提交
3480 3481 3482 3483 3484
/*-------------------------------------------------------------*/
static int
remoteDispatchNumOfInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
3485
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503
                               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,
3504
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533
                              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;
}

3534 3535 3536 3537
static int
remoteDispatchNumOfDefinedInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
3538
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556
                                      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,
3557
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586
                                     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 已提交
3587 3588 3589 3590
static int
remoteDispatchInterfaceLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
3591
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3592 3593 3594 3595
                                     remote_error *rerr,
                                     remote_interface_lookup_by_name_args *args,
                                     remote_interface_lookup_by_name_ret *ret)
{
3596
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3597

3598 3599
    iface = virInterfaceLookupByName (conn, args->name);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3600 3601 3602 3603
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3604 3605
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3606 3607 3608 3609 3610 3611 3612
    return 0;
}

static int
remoteDispatchInterfaceLookupByMacString (struct qemud_server *server ATTRIBUTE_UNUSED,
                                          struct qemud_client *client ATTRIBUTE_UNUSED,
                                          virConnectPtr conn,
3613
                                          remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3614 3615 3616 3617
                                          remote_error *rerr,
                                          remote_interface_lookup_by_mac_string_args *args,
                                          remote_interface_lookup_by_mac_string_ret *ret)
{
3618
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3619

3620 3621
    iface = virInterfaceLookupByMACString (conn, args->mac);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3622 3623 3624 3625
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3626 3627
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3628 3629 3630 3631 3632 3633 3634
    return 0;
}

static int
remoteDispatchInterfaceGetXmlDesc (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3635
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3636 3637 3638 3639
                                   remote_error *rerr,
                                   remote_interface_get_xml_desc_args *args,
                                   remote_interface_get_xml_desc_ret *ret)
{
3640
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3641

3642 3643
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3644 3645 3646 3647 3648
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    /* remoteDispatchClientRequest will free this. */
3649
    ret->xml = virInterfaceGetXMLDesc (iface, args->flags);
D
Daniel Veillard 已提交
3650 3651
    if (!ret->xml) {
        remoteDispatchConnError(rerr, conn);
3652
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3653 3654
        return -1;
    }
3655
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3656 3657 3658 3659 3660 3661 3662
    return 0;
}

static int
remoteDispatchInterfaceDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3663
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3664 3665 3666 3667
                                  remote_error *rerr,
                                  remote_interface_define_xml_args *args,
                                  remote_interface_define_xml_ret *ret)
{
3668
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3669

3670 3671
    iface = virInterfaceDefineXML (conn, args->xml, args->flags);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3672 3673 3674 3675
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3676 3677
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3678 3679 3680 3681 3682
    return 0;
}

static int
remoteDispatchInterfaceUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
3683 3684 3685 3686 3687 3688
                                 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 已提交
3689
{
3690
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3691

3692 3693
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3694 3695 3696 3697
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3698
    if (virInterfaceUndefine (iface) == -1) {
D
Daniel Veillard 已提交
3699
        remoteDispatchConnError(rerr, conn);
3700
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3701 3702
        return -1;
    }
3703
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3704 3705 3706 3707 3708
    return 0;
}

static int
remoteDispatchInterfaceCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
3709 3710 3711 3712 3713 3714
                               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 已提交
3715
{
3716
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3717

3718 3719
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3720 3721 3722 3723
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3724
    if (virInterfaceCreate (iface, args->flags) == -1) {
D
Daniel Veillard 已提交
3725
        remoteDispatchConnError(rerr, conn);
3726
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3727 3728
        return -1;
    }
3729
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3730 3731 3732 3733 3734
    return 0;
}

static int
remoteDispatchInterfaceDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
3735 3736 3737 3738 3739 3740
                                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 已提交
3741
{
3742
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3743

3744 3745
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3746 3747 3748 3749
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3750
    if (virInterfaceDestroy (iface, args->flags) == -1) {
D
Daniel Veillard 已提交
3751
        remoteDispatchConnError(rerr, conn);
3752
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3753 3754
        return -1;
    }
3755
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3756 3757 3758 3759 3760
    return 0;
}

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

3761
static int
3762
remoteDispatchAuthList (struct qemud_server *server,
3763
                        struct qemud_client *client,
3764
                        virConnectPtr conn ATTRIBUTE_UNUSED,
3765
                        remote_message_header *hdr ATTRIBUTE_UNUSED,
3766
                        remote_error *rerr,
3767 3768 3769 3770
                        void *args ATTRIBUTE_UNUSED,
                        remote_auth_list_ret *ret)
{
    ret->types.types_len = 1;
3771
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
3772 3773
        remoteDispatchOOMError(rerr);
        return -1;
3774
    }
3775 3776 3777
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3778
    ret->types.types_val[0] = client->auth;
3779
    virMutexUnlock(&client->lock);
3780

3781 3782 3783 3784 3785 3786 3787
    return 0;
}


#if HAVE_SASL
/*
 * Initializes the SASL session in prepare for authentication
3788
 * and gives the client a list of allowed mechanisms to choose
3789 3790 3791 3792
 *
 * XXX callbacks for stuff like password verification ?
 */
static int
3793
remoteDispatchAuthSaslInit (struct qemud_server *server,
3794
                            struct qemud_client *client,
3795
                            virConnectPtr conn,
3796
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3797
                            remote_error *rerr,
3798 3799 3800 3801
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret)
{
    const char *mechlist = NULL;
3802
    sasl_security_properties_t secprops;
3803
    int err;
3804
    virSocketAddr sa;
3805 3806
    char *localAddr, *remoteAddr;

3807 3808 3809
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3810

3811 3812 3813
    REMOTE_DEBUG("Initialize SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn != NULL) {
3814
        VIR_ERROR0(_("client tried invalid SASL init request"));
3815
        goto authfail;
3816 3817 3818
    }

    /* Get local address in form  IPADDR:PORT */
3819 3820
    sa.len = sizeof(sa.data.stor);
    if (getsockname(client->fd, &sa.data.sa, &sa.len) < 0) {
3821
        char ebuf[1024];
3822
        remoteDispatchFormatError(rerr,
3823
                                  _("failed to get sock address: %s"),
3824
                                  virStrerror(errno, ebuf, sizeof ebuf));
3825
        goto error;
3826
    }
3827 3828
    if ((localAddr = virSocketFormatAddrFull(&sa, true, ";")) == NULL) {
        remoteDispatchConnError(rerr, conn);
3829
        goto error;
3830 3831 3832
    }

    /* Get remote address in form  IPADDR:PORT */
3833 3834
    sa.len = sizeof(sa.data.stor);
    if (getpeername(client->fd, &sa.data.sa, &sa.len) < 0) {
3835
        char ebuf[1024];
3836
        remoteDispatchFormatError(rerr, _("failed to get peer address: %s"),
3837
                                  virStrerror(errno, ebuf, sizeof ebuf));
3838
        VIR_FREE(localAddr);
3839
        goto error;
3840
    }
3841
    if ((remoteAddr = virSocketFormatAddrFull(&sa, true, ";")) == NULL) {
3842
        VIR_FREE(localAddr);
3843
        remoteDispatchConnError(rerr, conn);
3844
        goto error;
3845 3846 3847 3848 3849 3850 3851 3852 3853 3854
    }

    err = sasl_server_new("libvirt",
                          NULL, /* FQDN - just delegates to gethostname */
                          NULL, /* User realm */
                          localAddr,
                          remoteAddr,
                          NULL, /* XXX Callbacks */
                          SASL_SUCCESS_DATA,
                          &client->saslconn);
3855 3856
    VIR_FREE(localAddr);
    VIR_FREE(remoteAddr);
3857
    if (err != SASL_OK) {
3858 3859
        VIR_ERROR(_("sasl context setup failed %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3860
        client->saslconn = NULL;
3861
        goto authfail;
3862 3863
    }

3864 3865 3866 3867 3868 3869 3870
    /* 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 已提交
3871
            VIR_ERROR0(_("cannot get TLS cipher size"));
3872 3873
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
3874
            goto authfail;
3875 3876 3877 3878 3879
        }
        ssf *= 8; /* tls key size is bytes, sasl wants bits */

        err = sasl_setprop(client->saslconn, SASL_SSF_EXTERNAL, &ssf);
        if (err != SASL_OK) {
3880 3881
            VIR_ERROR(_("cannot set SASL external SSF %d (%s)"),
                      err, sasl_errstring(err, NULL, NULL));
3882 3883
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
3884
            goto authfail;
3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907
        }
    }

    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) {
3908 3909
        VIR_ERROR(_("cannot set SASL security props %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3910 3911
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3912
        goto authfail;
3913 3914
    }

3915 3916 3917 3918 3919 3920 3921 3922 3923
    err = sasl_listmech(client->saslconn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
3924 3925
        VIR_ERROR(_("cannot list SASL mechanisms %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3926 3927
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3928
        goto authfail;
3929 3930 3931 3932
    }
    REMOTE_DEBUG("Available mechanisms for client: '%s'", mechlist);
    ret->mechlist = strdup(mechlist);
    if (!ret->mechlist) {
3933
        VIR_ERROR0(_("cannot allocate mechlist"));
3934 3935
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3936
        goto authfail;
3937 3938
    }

3939
    virMutexUnlock(&client->lock);
3940
    return 0;
3941 3942 3943 3944

authfail:
    remoteDispatchAuthError(rerr);
error:
3945
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
3946
    virMutexUnlock(&client->lock);
3947
    return -1;
3948 3949 3950
}


3951
/* We asked for an SSF layer, so sanity check that we actually
3952 3953 3954
 * got what we asked for
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
3955 3956
static int
remoteSASLCheckSSF (struct qemud_client *client,
3957
                    remote_error *rerr) {
3958 3959 3960 3961 3962 3963 3964 3965 3966
    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) {
3967 3968
        VIR_ERROR(_("cannot query SASL ssf on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3969
        remoteDispatchAuthError(rerr);
3970 3971 3972 3973 3974 3975 3976
        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 */
3977
        VIR_ERROR(_("negotiated SSF %d was not strong enough"), ssf);
3978
        remoteDispatchAuthError(rerr);
3979 3980
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3981
        return -2;
3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995
    }

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

3996 3997 3998
/*
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
3999 4000 4001
static int
remoteSASLCheckAccess (struct qemud_server *server,
                       struct qemud_client *client,
4002
                       remote_error *rerr) {
4003 4004 4005 4006 4007 4008
    const void *val;
    int err;
    char **wildcards;

    err = sasl_getprop(client->saslconn, SASL_USERNAME, &val);
    if (err != SASL_OK) {
4009 4010
        VIR_ERROR(_("cannot query SASL username on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
4011
        remoteDispatchAuthError(rerr);
4012 4013 4014 4015 4016
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    if (val == NULL) {
4017
        VIR_ERROR0(_("no client username was found"));
4018
        remoteDispatchAuthError(rerr);
4019 4020 4021 4022 4023 4024 4025 4026
        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) {
4027
        VIR_ERROR0(_("out of memory copying username"));
4028
        remoteDispatchAuthError(rerr);
4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045
        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 */
4046
    VIR_ERROR(_("SASL client %s not allowed in whitelist"), client->saslUsername);
4047
    remoteDispatchAuthError(rerr);
4048 4049
    sasl_dispose(&client->saslconn);
    client->saslconn = NULL;
4050
    return -2;
4051 4052 4053
}


4054 4055 4056 4057
/*
 * This starts the SASL authentication negotiation.
 */
static int
4058 4059
remoteDispatchAuthSaslStart (struct qemud_server *server,
                             struct qemud_client *client,
4060
                             virConnectPtr conn ATTRIBUTE_UNUSED,
4061
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
4062
                             remote_error *rerr,
4063 4064 4065 4066 4067 4068 4069
                             remote_auth_sasl_start_args *args,
                             remote_auth_sasl_start_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

4070 4071 4072
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
4073

4074 4075 4076
    REMOTE_DEBUG("Start SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
4077
        VIR_ERROR0(_("client tried invalid SASL start request"));
4078
        goto authfail;
4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091
    }

    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) {
4092 4093
        VIR_ERROR(_("sasl start failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
4094 4095
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4096
        goto authfail;
4097 4098
    }
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
4099
        VIR_ERROR(_("sasl start reply data too long %d"), serveroutlen);
4100 4101
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4102
        goto authfail;
4103 4104 4105 4106
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
4107
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
4108
            remoteDispatchOOMError(rerr);
4109
            goto error;
4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121
        }
        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 {
4122
        /* Check username whitelist ACL */
4123 4124 4125 4126 4127 4128 4129
        if ((err = remoteSASLCheckAccess(server, client, rerr)) < 0 ||
            (err = remoteSASLCheckSSF(client, rerr)) < 0) {
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
4130

4131
        REMOTE_DEBUG("Authentication successful %d", client->fd);
4132 4133
        PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
              client->fd, REMOTE_AUTH_SASL, client->saslUsername);
4134 4135 4136 4137
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

4138
    virMutexUnlock(&client->lock);
4139
    return 0;
4140 4141

authfail:
4142
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
4143
    remoteDispatchAuthError(rerr);
4144 4145 4146
    goto error;

authdeny:
4147 4148
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_SASL, client->saslUsername);
4149 4150
    goto error;

4151
error:
4152
    virMutexUnlock(&client->lock);
4153
    return -1;
4154 4155 4156 4157
}


static int
4158 4159
remoteDispatchAuthSaslStep (struct qemud_server *server,
                            struct qemud_client *client,
4160
                            virConnectPtr conn ATTRIBUTE_UNUSED,
4161
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
4162
                            remote_error *rerr,
4163 4164 4165 4166 4167 4168 4169
                            remote_auth_sasl_step_args *args,
                            remote_auth_sasl_step_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

4170 4171 4172
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
4173

4174 4175 4176
    REMOTE_DEBUG("Step SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
4177
        VIR_ERROR0(_("client tried invalid SASL start request"));
4178
        goto authfail;
4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190
    }

    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) {
4191 4192
        VIR_ERROR(_("sasl step failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
4193 4194
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4195
        goto authfail;
4196 4197 4198
    }

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
4199 4200
        VIR_ERROR(_("sasl step reply data too long %d"),
                  serveroutlen);
4201 4202
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4203
        goto authfail;
4204 4205 4206 4207
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
4208
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
4209
            remoteDispatchOOMError(rerr);
4210
            goto error;
4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222
        }
        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 {
4223
        /* Check username whitelist ACL */
4224 4225 4226 4227 4228 4229 4230
        if ((err = remoteSASLCheckAccess(server, client, rerr)) < 0 ||
            (err = remoteSASLCheckSSF(client, rerr)) < 0) {
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
4231

4232
        REMOTE_DEBUG("Authentication successful %d", client->fd);
4233 4234
        PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
              client->fd, REMOTE_AUTH_SASL, client->saslUsername);
4235 4236 4237 4238
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

4239
    virMutexUnlock(&client->lock);
4240
    return 0;
4241 4242

authfail:
4243
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
4244
    remoteDispatchAuthError(rerr);
4245 4246 4247
    goto error;

authdeny:
4248 4249
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_SASL, client->saslUsername);
4250 4251
    goto error;

4252
error:
4253
    virMutexUnlock(&client->lock);
4254
    return -1;
4255 4256 4257 4258 4259
}


#else /* HAVE_SASL */
static int
4260
remoteDispatchAuthSaslInit (struct qemud_server *server ATTRIBUTE_UNUSED,
4261 4262
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
4263
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
4264
                            remote_error *rerr,
4265 4266 4267
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
4268
    VIR_ERROR0(_("client tried unsupported SASL init request"));
4269
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
4270
    remoteDispatchAuthError(rerr);
4271 4272 4273 4274
    return -1;
}

static int
4275
remoteDispatchAuthSaslStart (struct qemud_server *server ATTRIBUTE_UNUSED,
4276 4277
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn ATTRIBUTE_UNUSED,
4278
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
4279
                             remote_error *rerr,
4280 4281 4282
                             remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                             remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
4283
    VIR_ERROR0(_("client tried unsupported SASL start request"));
4284
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
4285
    remoteDispatchAuthError(rerr);
4286 4287 4288 4289
    return -1;
}

static int
4290
remoteDispatchAuthSaslStep (struct qemud_server *server ATTRIBUTE_UNUSED,
4291 4292
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
4293
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
4294
                            remote_error *rerr,
4295 4296 4297
                            remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
4298
    VIR_ERROR0(_("client tried unsupported SASL step request"));
4299
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
4300
    remoteDispatchAuthError(rerr);
4301 4302 4303 4304 4305
    return -1;
}
#endif /* HAVE_SASL */


4306 4307 4308 4309 4310
#if HAVE_POLKIT1
static int
remoteDispatchAuthPolkit (struct qemud_server *server,
                          struct qemud_client *client,
                          virConnectPtr conn ATTRIBUTE_UNUSED,
4311
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
4312 4313 4314 4315
                          remote_error *rerr,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
4316 4317
    pid_t callerPid = -1;
    uid_t callerUid = -1;
4318 4319 4320
    const char *action;
    int status = -1;
    char pidbuf[50];
4321
    char ident[100];
4322 4323
    int rv;

4324 4325
    memset(ident, 0, sizeof ident);

4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357
    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);
4358
        goto authfail;
4359 4360
    }

4361 4362 4363 4364 4365 4366
    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;
    }

4367
    if (virRun(pkcheck, &status) < 0) {
4368
        VIR_ERROR(_("Cannot invoke %s"), PKCHECK_PATH);
4369
        goto authfail;
4370 4371
    }
    if (status != 0) {
4372 4373 4374 4375
        char *tmp = virCommandTranslateStatus(status);
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d: %s"),
                  action, callerPid, callerUid, NULLSTR(tmp));
        VIR_FREE(tmp);
4376
        goto authdeny;
4377
    }
4378
    PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
M
Matthias Bolte 已提交
4379
          client->fd, REMOTE_AUTH_POLKIT, (char *)ident);
4380 4381 4382 4383 4384 4385 4386 4387 4388
    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:
4389
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_POLKIT);
4390 4391 4392
    goto error;

authdeny:
4393
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
M
Matthias Bolte 已提交
4394
          client->fd, REMOTE_AUTH_POLKIT, (char *)ident);
4395 4396 4397
    goto error;

error:
4398 4399 4400 4401 4402
    remoteDispatchAuthError(rerr);
    virMutexUnlock(&client->lock);
    return -1;
}
#elif HAVE_POLKIT0
4403
static int
4404
remoteDispatchAuthPolkit (struct qemud_server *server,
4405
                          struct qemud_client *client,
4406
                          virConnectPtr conn ATTRIBUTE_UNUSED,
4407
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
4408
                          remote_error *rerr,
4409 4410 4411 4412 4413
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
    pid_t callerPid;
    uid_t callerUid;
4414 4415 4416 4417 4418 4419
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
4420
    const char *action;
4421
    char ident[100];
J
Jim Fehlig 已提交
4422
    int rv;
4423 4424

    memset(ident, 0, sizeof ident);
4425

4426 4427 4428
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
4429 4430

    action = client->readonly ?
4431 4432
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
4433 4434 4435

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

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

4445 4446 4447 4448 4449 4450
    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;
    }

4451
    VIR_INFO(_("Checking PID %d running as %d"), callerPid, callerUid);
4452 4453 4454
    dbus_error_init(&err);
    if (!(pkcaller = polkit_caller_new_from_pid(server->sysbus,
                                                callerPid, &err))) {
4455
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
4456
        dbus_error_free(&err);
4457
        goto authfail;
4458
    }
4459

4460
    if (!(pkaction = polkit_action_new())) {
4461
        char ebuf[1024];
4462
        VIR_ERROR(_("Failed to create polkit action %s"),
4463
                  virStrerror(errno, ebuf, sizeof ebuf));
4464
        polkit_caller_unref(pkcaller);
4465
        goto authfail;
4466 4467 4468 4469 4470
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
4471
        char ebuf[1024];
4472
        VIR_ERROR(_("Failed to create polkit context %s"),
4473
                  (pkerr ? polkit_error_get_error_message(pkerr)
4474
                   : virStrerror(errno, ebuf, sizeof ebuf)));
4475 4476 4477 4478 4479
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
4480
        goto authfail;
4481
    }
4482

4483
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
4484 4485 4486 4487 4488 4489
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
4490 4491 4492
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
4493
        goto authfail;
4494
    }
4495
# else
4496 4497 4498
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
4499
# endif
4500 4501 4502 4503
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
4504
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d, result: %s"),
4505 4506
                  action, callerPid, callerUid,
                  polkit_result_to_string_representation(pkresult));
4507
        goto authdeny;
4508
    }
4509 4510
    PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_POLKIT, ident);
4511
    VIR_INFO(_("Policy allowed action %s from pid %d, uid %d, result %s"),
4512 4513 4514 4515
             action, callerPid, callerUid,
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;
4516

4517
    virMutexUnlock(&client->lock);
4518
    return 0;
4519 4520

authfail:
4521
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_POLKIT);
4522 4523 4524
    goto error;

authdeny:
4525 4526
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_POLKIT, ident);
4527 4528 4529
    goto error;

error:
4530
    remoteDispatchAuthError(rerr);
4531
    virMutexUnlock(&client->lock);
4532
    return -1;
4533 4534
}

4535
#else /* !HAVE_POLKIT0 & !HAVE_POLKIT1*/
4536 4537

static int
4538
remoteDispatchAuthPolkit (struct qemud_server *server ATTRIBUTE_UNUSED,
4539
                          struct qemud_client *client ATTRIBUTE_UNUSED,
4540
                          virConnectPtr conn ATTRIBUTE_UNUSED,
4541
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
4542 4543 4544
                          remote_error *rerr,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
4545
{
4546
    VIR_ERROR0(_("client tried unsupported PolicyKit init request"));
4547
    remoteDispatchAuthError(rerr);
4548 4549
    return -1;
}
4550
#endif /* HAVE_POLKIT1 */
4551

4552 4553 4554 4555 4556 4557 4558 4559

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


static int
remoteDispatchListDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4560 4561
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4562
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4563
                                       remote_error *rerr,
4564 4565 4566 4567 4568
                                       remote_list_defined_storage_pools_args *args,
                                       remote_list_defined_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
4569 4570
        remoteDispatchFormatError (rerr, "%s",
                            _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
4571
        return -1;
4572 4573 4574
    }

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

    ret->names.names_len =
4581
        virConnectListDefinedStoragePools (conn,
4582 4583 4584
                                           ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
4585
        remoteDispatchConnError(rerr, conn);
4586 4587
        return -1;
    }
4588 4589 4590 4591 4592 4593

    return 0;
}

static int
remoteDispatchListStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4594 4595
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4596
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4597
                                remote_error *rerr,
4598 4599 4600 4601 4602
                                remote_list_storage_pools_args *args,
                                remote_list_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
4603 4604 4605
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX"));
        return -1;
4606 4607 4608
    }

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

    ret->names.names_len =
4615
        virConnectListStoragePools (conn,
4616
                                ret->names.names_val, args->maxnames);
4617 4618
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
4619
        remoteDispatchConnError(rerr, conn);
4620 4621
        return -1;
    }
4622 4623 4624 4625

    return 0;
}

4626 4627
static int
remoteDispatchFindStoragePoolSources (struct qemud_server *server ATTRIBUTE_UNUSED,
4628 4629
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4630
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4631
                                      remote_error *rerr,
4632 4633 4634 4635
                                      remote_find_storage_pool_sources_args *args,
                                      remote_find_storage_pool_sources_ret *ret)
{
    ret->xml =
4636
        virConnectFindStoragePoolSources (conn,
4637 4638 4639
                                          args->type,
                                          args->srcSpec ? *args->srcSpec : NULL,
                                          args->flags);
4640
    if (ret->xml == NULL) {
4641
        remoteDispatchConnError(rerr, conn);
4642
        return -1;
4643
    }
4644 4645 4646 4647 4648

    return 0;
}


4649 4650
static int
remoteDispatchStoragePoolCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
4651 4652
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4653
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4654
                                 remote_error *rerr,
4655 4656 4657 4658 4659
                                 remote_storage_pool_create_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4660
    pool = get_nonnull_storage_pool (conn, args->pool);
4661
    if (pool == NULL) {
4662
        remoteDispatchConnError(rerr, conn);
4663
        return -1;
4664 4665 4666
    }

    if (virStoragePoolCreate (pool, args->flags) == -1) {
4667
        remoteDispatchConnError(rerr, conn);
4668
        virStoragePoolFree(pool);
4669 4670 4671 4672 4673 4674 4675 4676
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4677 4678
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
4679
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
4680
                                    remote_error *rerr,
4681 4682 4683 4684 4685
                                    remote_storage_pool_create_xml_args *args,
                                    remote_storage_pool_create_xml_ret *ret)
{
    virStoragePoolPtr pool;

4686
    pool = virStoragePoolCreateXML (conn, args->xml, args->flags);
4687
    if (pool == NULL) {
4688
        remoteDispatchConnError(rerr, conn);
4689 4690
        return -1;
    }
4691 4692 4693 4694 4695 4696 4697 4698

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

static int
remoteDispatchStoragePoolDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4699 4700
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
4701
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
4702
                                    remote_error *rerr,
4703 4704 4705 4706 4707
                                    remote_storage_pool_define_xml_args *args,
                                    remote_storage_pool_define_xml_ret *ret)
{
    virStoragePoolPtr pool;

4708
    pool = virStoragePoolDefineXML (conn, args->xml, args->flags);
4709
    if (pool == NULL) {
4710
        remoteDispatchConnError(rerr, conn);
4711 4712
        return -1;
    }
4713 4714 4715 4716 4717 4718 4719 4720

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

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

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

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


static int
remoteDispatchStoragePoolDestroy (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_destroy_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
    }

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

static int
remoteDispatchStoragePoolDelete (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 4782
                                 remote_storage_pool_delete_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4783
    pool = get_nonnull_storage_pool (conn, args->pool);
4784
    if (pool == NULL) {
4785
        remoteDispatchConnError(rerr, conn);
4786
        return -1;
4787 4788 4789
    }

    if (virStoragePoolDelete (pool, args->flags) == -1) {
4790
        remoteDispatchConnError(rerr, conn);
4791
        virStoragePoolFree(pool);
4792 4793 4794 4795 4796 4797 4798 4799
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolRefresh (struct qemud_server *server ATTRIBUTE_UNUSED,
4800 4801
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4802
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4803
                                  remote_error *rerr,
4804 4805 4806 4807 4808
                                  remote_storage_pool_refresh_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4809
    pool = get_nonnull_storage_pool (conn, args->pool);
4810
    if (pool == NULL) {
4811
        remoteDispatchConnError(rerr, conn);
4812
        return -1;
4813 4814 4815
    }

    if (virStoragePoolRefresh (pool, args->flags) == -1) {
4816
        remoteDispatchConnError(rerr, conn);
4817
        virStoragePoolFree(pool);
4818 4819 4820 4821 4822 4823 4824 4825
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
4826 4827
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4828
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4829
                                  remote_error *rerr,
4830 4831 4832 4833 4834 4835
                                  remote_storage_pool_get_info_args *args,
                                  remote_storage_pool_get_info_ret *ret)
{
    virStoragePoolPtr pool;
    virStoragePoolInfo info;

4836
    pool = get_nonnull_storage_pool (conn, args->pool);
4837
    if (pool == NULL) {
4838
        remoteDispatchConnError(rerr, conn);
4839
        return -1;
4840 4841 4842
    }

    if (virStoragePoolGetInfo (pool, &info) == -1) {
4843
        remoteDispatchConnError(rerr, conn);
4844
        virStoragePoolFree(pool);
4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859
        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,
4860 4861
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4862
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4863
                                  remote_error *rerr,
4864 4865 4866 4867 4868
                                  remote_storage_pool_dump_xml_args *args,
                                  remote_storage_pool_dump_xml_ret *ret)
{
    virStoragePoolPtr pool;

4869
    pool = get_nonnull_storage_pool (conn, args->pool);
4870
    if (pool == NULL) {
4871
        remoteDispatchConnError(rerr, conn);
4872
        return -1;
4873 4874 4875 4876 4877
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStoragePoolGetXMLDesc (pool, args->flags);
    if (!ret->xml) {
4878
        remoteDispatchConnError(rerr, conn);
4879
        virStoragePoolFree(pool);
4880 4881 4882 4883 4884 4885 4886 4887
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
4888 4889
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4890
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4891
                                       remote_error *rerr,
4892 4893 4894 4895 4896
                                       remote_storage_pool_get_autostart_args *args,
                                       remote_storage_pool_get_autostart_ret *ret)
{
    virStoragePoolPtr pool;

4897
    pool = get_nonnull_storage_pool (conn, args->pool);
4898
    if (pool == NULL) {
4899
        remoteDispatchConnError(rerr, conn);
4900
        return -1;
4901 4902 4903
    }

    if (virStoragePoolGetAutostart (pool, &ret->autostart) == -1) {
4904
        remoteDispatchConnError(rerr, conn);
4905
        virStoragePoolFree(pool);
4906 4907 4908 4909 4910 4911 4912 4913 4914
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
4915 4916
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4917
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4918
                                       remote_error *rerr,
4919 4920 4921 4922 4923
                                       remote_storage_pool_lookup_by_name_args *args,
                                       remote_storage_pool_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;

4924
    pool = virStoragePoolLookupByName (conn, args->name);
4925
    if (pool == NULL) {
4926
        remoteDispatchConnError(rerr, conn);
4927 4928
        return -1;
    }
4929 4930 4931 4932 4933 4934 4935 4936

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

static int
remoteDispatchStoragePoolLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
4937 4938
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4939
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4940
                                       remote_error *rerr,
4941 4942 4943 4944 4945
                                       remote_storage_pool_lookup_by_uuid_args *args,
                                       remote_storage_pool_lookup_by_uuid_ret *ret)
{
    virStoragePoolPtr pool;

4946
    pool = virStoragePoolLookupByUUID (conn, (unsigned char *) args->uuid);
4947
    if (pool == NULL) {
4948
        remoteDispatchConnError(rerr, conn);
4949 4950
        return -1;
    }
4951 4952 4953 4954 4955 4956 4957 4958

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

static int
remoteDispatchStoragePoolLookupByVolume (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 4968
                                         remote_storage_pool_lookup_by_volume_args *args,
                                         remote_storage_pool_lookup_by_volume_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

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

    pool = virStoragePoolLookupByVolume (vol);
4976
    if (pool == NULL) {
4977
        remoteDispatchConnError(rerr, conn);
4978
        virStorageVolFree(vol);
4979 4980
        return -1;
    }
4981
    virStorageVolFree(vol);
4982 4983 4984 4985 4986 4987 4988 4989

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

static int
remoteDispatchStoragePoolSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
4990 4991
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4992
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4993
                                       remote_error *rerr,
4994 4995 4996 4997 4998
                                       remote_storage_pool_set_autostart_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4999
    pool = get_nonnull_storage_pool (conn, args->pool);
5000
    if (pool == NULL) {
5001
        remoteDispatchConnError(rerr, conn);
5002
        return -1;
5003 5004 5005
    }

    if (virStoragePoolSetAutostart (pool, args->autostart) == -1) {
5006
        remoteDispatchConnError(rerr, conn);
5007
        virStoragePoolFree(pool);
5008 5009 5010 5011 5012 5013 5014 5015
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
5016 5017
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
5018
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
5019
                                   remote_error *rerr,
5020 5021 5022 5023 5024
                                   remote_storage_pool_undefine_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

5025
    pool = get_nonnull_storage_pool (conn, args->pool);
5026
    if (pool == NULL) {
5027
        remoteDispatchConnError(rerr, conn);
5028
        return -1;
5029 5030 5031
    }

    if (virStoragePoolUndefine (pool) == -1) {
5032
        remoteDispatchConnError(rerr, conn);
5033
        virStoragePoolFree(pool);
5034 5035 5036 5037 5038 5039 5040 5041
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchNumOfStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
5042 5043
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5044
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5045
                                 remote_error *rerr,
5046 5047 5048 5049
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_num_of_storage_pools_ret *ret)
{

5050
    ret->num = virConnectNumOfStoragePools (conn);
5051
    if (ret->num == -1) {
5052
        remoteDispatchConnError(rerr, conn);
5053 5054
        return -1;
    }
5055 5056 5057 5058 5059 5060

    return 0;
}

static int
remoteDispatchNumOfDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
5061 5062
                                        struct qemud_client *client ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
5063
                                        remote_message_header *hdr ATTRIBUTE_UNUSED,
5064
                                        remote_error *rerr,
5065 5066 5067 5068
                                        void *args ATTRIBUTE_UNUSED,
                                        remote_num_of_defined_storage_pools_ret *ret)
{

5069
    ret->num = virConnectNumOfDefinedStoragePools (conn);
5070
    if (ret->num == -1) {
5071
        remoteDispatchConnError(rerr, conn);
5072 5073
        return -1;
    }
5074 5075 5076 5077 5078 5079

    return 0;
}

static int
remoteDispatchStoragePoolListVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
5080 5081
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
5082
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
5083
                                      remote_error *rerr,
5084 5085 5086 5087 5088 5089
                                      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) {
5090 5091 5092
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX"));
        return -1;
5093 5094
    }

5095
    pool = get_nonnull_storage_pool (conn, args->pool);
5096
    if (pool == NULL) {
5097
        remoteDispatchConnError(rerr, conn);
5098
        return -1;
5099 5100 5101
    }

    /* Allocate return buffer. */
5102 5103
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        virStoragePoolFree(pool);
5104 5105
        remoteDispatchOOMError(rerr);
        return -1;
5106
    }
5107 5108 5109 5110 5111

    ret->names.names_len =
        virStoragePoolListVolumes (pool,
                                   ret->names.names_val, args->maxnames);
    virStoragePoolFree(pool);
5112 5113
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
5114
        remoteDispatchConnError(rerr, conn);
5115 5116
        return -1;
    }
5117 5118 5119 5120 5121 5122 5123

    return 0;
}


static int
remoteDispatchStoragePoolNumOfVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
5124 5125
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
5126
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
5127
                                       remote_error *rerr,
5128 5129 5130 5131 5132
                                       remote_storage_pool_num_of_volumes_args *args,
                                       remote_storage_pool_num_of_volumes_ret *ret)
{
    virStoragePoolPtr pool;

5133
    pool = get_nonnull_storage_pool (conn, args->pool);
5134
    if (pool == NULL) {
5135
        remoteDispatchConnError(rerr, conn);
5136
        return -1;
5137 5138 5139 5140
    }

    ret->num = virStoragePoolNumOfVolumes (pool);
    virStoragePoolFree(pool);
5141
    if (ret->num == -1) {
5142
        remoteDispatchConnError(rerr, conn);
5143 5144
        return -1;
    }
5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157

    return 0;
}


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



static int
remoteDispatchStorageVolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
5158 5159
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
5160
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
5161
                                   remote_error *rerr,
5162 5163 5164 5165 5166 5167
                                   remote_storage_vol_create_xml_args *args,
                                   remote_storage_vol_create_xml_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

5168
    pool = get_nonnull_storage_pool (conn, args->pool);
5169
    if (pool == NULL) {
5170
        remoteDispatchConnError(rerr, conn);
5171
        return -1;
5172 5173 5174
    }

    vol = virStorageVolCreateXML (pool, args->xml, args->flags);
5175
    if (vol == NULL) {
5176
        remoteDispatchConnError(rerr, conn);
5177
        virStoragePoolFree(pool);
5178 5179
        return -1;
    }
5180
    virStoragePoolFree(pool);
5181 5182 5183 5184 5185 5186

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

5187 5188 5189 5190
static int
remoteDispatchStorageVolCreateXmlFrom (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
5191
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207
                                       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) {
        remoteDispatchConnError(rerr, conn);
5208
        virStoragePoolFree(pool);
5209 5210 5211 5212 5213 5214 5215
        return -1;
    }

    newvol = virStorageVolCreateXMLFrom (pool, args->xml, clonevol,
                                         args->flags);
    if (newvol == NULL) {
        remoteDispatchConnError(rerr, conn);
5216 5217
        virStorageVolFree(clonevol);
        virStoragePoolFree(pool);
5218 5219
        return -1;
    }
5220 5221
    virStorageVolFree(clonevol);
    virStoragePoolFree(pool);
5222 5223 5224 5225 5226

    make_nonnull_storage_vol (&ret->vol, newvol);
    virStorageVolFree(newvol);
    return 0;
}
5227 5228 5229

static int
remoteDispatchStorageVolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
5230 5231
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
5232
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
5233
                                remote_error *rerr,
5234 5235 5236 5237 5238
                                remote_storage_vol_delete_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virStorageVolPtr vol;

5239
    vol = get_nonnull_storage_vol (conn, args->vol);
5240
    if (vol == NULL) {
5241
        remoteDispatchConnError(rerr, conn);
5242
        return -1;
5243 5244 5245
    }

    if (virStorageVolDelete (vol, args->flags) == -1) {
5246
        remoteDispatchConnError(rerr, conn);
5247
        virStorageVolFree(vol);
5248 5249 5250 5251 5252 5253
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}

5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285
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;
}

5286 5287
static int
remoteDispatchStorageVolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
5288 5289
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5290
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5291
                                 remote_error *rerr,
5292 5293 5294 5295 5296 5297
                                 remote_storage_vol_get_info_args *args,
                                 remote_storage_vol_get_info_ret *ret)
{
    virStorageVolPtr vol;
    virStorageVolInfo info;

5298
    vol = get_nonnull_storage_vol (conn, args->vol);
5299
    if (vol == NULL) {
5300
        remoteDispatchConnError(rerr, conn);
5301
        return -1;
5302 5303 5304
    }

    if (virStorageVolGetInfo (vol, &info) == -1) {
5305
        remoteDispatchConnError(rerr, conn);
5306
        virStorageVolFree(vol);
5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320
        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,
5321 5322
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5323
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5324
                                 remote_error *rerr,
5325 5326 5327 5328 5329
                                 remote_storage_vol_dump_xml_args *args,
                                 remote_storage_vol_dump_xml_ret *ret)
{
    virStorageVolPtr vol;

5330
    vol = get_nonnull_storage_vol (conn, args->vol);
5331
    if (vol == NULL) {
5332
        remoteDispatchConnError(rerr, conn);
5333
        return -1;
5334 5335 5336 5337 5338
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStorageVolGetXMLDesc (vol, args->flags);
    if (!ret->xml) {
5339
        remoteDispatchConnError(rerr, conn);
5340
        virStorageVolFree(vol);
5341 5342 5343 5344 5345 5346 5347 5348 5349
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolGetPath (struct qemud_server *server ATTRIBUTE_UNUSED,
5350 5351
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5352
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5353
                                 remote_error *rerr,
5354 5355 5356 5357 5358
                                 remote_storage_vol_get_path_args *args,
                                 remote_storage_vol_get_path_ret *ret)
{
    virStorageVolPtr vol;

5359
    vol = get_nonnull_storage_vol (conn, args->vol);
5360
    if (vol == NULL) {
5361
        remoteDispatchConnError(rerr, conn);
5362
        return -1;
5363 5364 5365 5366 5367
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virStorageVolGetPath (vol);
    if (!ret->name) {
5368
        remoteDispatchConnError(rerr, conn);
5369
        virStorageVolFree(vol);
5370 5371 5372 5373 5374 5375 5376 5377 5378
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
5379 5380
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
5381
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
5382
                                      remote_error *rerr,
5383 5384 5385 5386 5387 5388
                                      remote_storage_vol_lookup_by_name_args *args,
                                      remote_storage_vol_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

5389
    pool = get_nonnull_storage_pool (conn, args->pool);
5390
    if (pool == NULL) {
5391
        remoteDispatchConnError(rerr, conn);
5392
        return -1;
5393 5394 5395
    }

    vol = virStorageVolLookupByName (pool, args->name);
5396
    if (vol == NULL) {
5397
        remoteDispatchConnError(rerr, conn);
5398
        virStoragePoolFree(pool);
5399 5400
        return -1;
    }
5401
    virStoragePoolFree(pool);
5402 5403 5404 5405 5406 5407 5408 5409

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

static int
remoteDispatchStorageVolLookupByKey (struct qemud_server *server ATTRIBUTE_UNUSED,
5410 5411
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
5412
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
5413
                                     remote_error *rerr,
5414 5415 5416 5417 5418
                                     remote_storage_vol_lookup_by_key_args *args,
                                     remote_storage_vol_lookup_by_key_ret *ret)
{
    virStorageVolPtr vol;

5419
    vol = virStorageVolLookupByKey (conn, args->key);
5420
    if (vol == NULL) {
5421
        remoteDispatchConnError(rerr, conn);
5422 5423
        return -1;
    }
5424 5425 5426 5427 5428 5429 5430 5431 5432

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


static int
remoteDispatchStorageVolLookupByPath (struct qemud_server *server ATTRIBUTE_UNUSED,
5433 5434
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
5435
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
5436
                                      remote_error *rerr,
5437 5438 5439 5440 5441
                                      remote_storage_vol_lookup_by_path_args *args,
                                      remote_storage_vol_lookup_by_path_ret *ret)
{
    virStorageVolPtr vol;

5442
    vol = virStorageVolLookupByPath (conn, args->path);
5443
    if (vol == NULL) {
5444
        remoteDispatchConnError(rerr, conn);
5445 5446
        return -1;
    }
5447 5448 5449 5450 5451 5452 5453

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


5454 5455 5456 5457 5458 5459
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/

static int
remoteDispatchNodeNumOfDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
5460 5461
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
5462
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
5463
                                remote_error *rerr,
5464 5465 5466 5467 5468
                                remote_node_num_of_devices_args *args,
                                remote_node_num_of_devices_ret *ret)
{
    CHECK_CONN(client);

5469
    ret->num = virNodeNumOfDevices (conn,
5470 5471
                                    args->cap ? *args->cap : NULL,
                                    args->flags);
5472
    if (ret->num == -1) {
5473
        remoteDispatchConnError(rerr, conn);
5474 5475
        return -1;
    }
5476 5477 5478 5479 5480 5481 5482

    return 0;
}


static int
remoteDispatchNodeListDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
5483 5484
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
5485
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
5486
                               remote_error *rerr,
5487 5488 5489 5490 5491 5492
                               remote_node_list_devices_args *args,
                               remote_node_list_devices_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
5493 5494 5495
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
5496 5497 5498 5499
    }

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

    ret->names.names_len =
5505
        virNodeListDevices (conn,
5506 5507 5508
                            args->cap ? *args->cap : NULL,
                            ret->names.names_val, args->maxnames, args->flags);
    if (ret->names.names_len == -1) {
5509
        remoteDispatchConnError(rerr, conn);
5510 5511 5512 5513 5514 5515 5516 5517 5518 5519
        VIR_FREE(ret->names.names_val);
        return -1;
    }

    return 0;
}


static int
remoteDispatchNodeDeviceLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
5520 5521
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
5522
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
5523
                                      remote_error *rerr,
5524 5525 5526 5527 5528 5529 5530
                                      remote_node_device_lookup_by_name_args *args,
                                      remote_node_device_lookup_by_name_ret *ret)
{
    virNodeDevicePtr dev;

    CHECK_CONN(client);

5531
    dev = virNodeDeviceLookupByName (conn, args->name);
5532
    if (dev == NULL) {
5533
        remoteDispatchConnError(rerr, conn);
5534 5535
        return -1;
    }
5536 5537 5538 5539 5540 5541 5542 5543 5544

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


static int
remoteDispatchNodeDeviceDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
5545 5546
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5547
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5548
                                 remote_error *rerr,
5549 5550 5551 5552 5553 5554
                                 remote_node_device_dump_xml_args *args,
                                 remote_node_device_dump_xml_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

5555
    dev = virNodeDeviceLookupByName(conn, args->name);
5556
    if (dev == NULL) {
5557
        remoteDispatchConnError(rerr, conn);
5558
        return -1;
5559 5560 5561 5562 5563
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNodeDeviceGetXMLDesc (dev, args->flags);
    if (!ret->xml) {
5564
        remoteDispatchConnError(rerr, conn);
5565 5566 5567 5568 5569 5570 5571 5572 5573 5574
        virNodeDeviceFree(dev);
        return -1;
    }
    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceGetParent (struct qemud_server *server ATTRIBUTE_UNUSED,
5575 5576
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
5577
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
5578
                                   remote_error *rerr,
5579 5580 5581 5582 5583 5584 5585
                                   remote_node_device_get_parent_args *args,
                                   remote_node_device_get_parent_ret *ret)
{
    virNodeDevicePtr dev;
    const char *parent;
    CHECK_CONN(client);

5586
    dev = virNodeDeviceLookupByName(conn, args->name);
5587
    if (dev == NULL) {
5588
        remoteDispatchConnError(rerr, conn);
5589
        return -1;
5590 5591 5592 5593 5594 5595 5596 5597 5598 5599
    }

    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
        if (VIR_ALLOC(parent_p) < 0) {
5600
            virNodeDeviceFree(dev);
5601 5602
            remoteDispatchOOMError(rerr);
            return -1;
5603 5604 5605
        }
        *parent_p = strdup(parent);
        if (*parent_p == NULL) {
5606
            virNodeDeviceFree(dev);
5607 5608
            remoteDispatchOOMError(rerr);
            return -1;
5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619
        }
        ret->parent = parent_p;
    }

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceNumOfCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
5620 5621
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
5622
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
5623
                                   remote_error *rerr,
5624 5625 5626 5627 5628 5629
                                   remote_node_device_num_of_caps_args *args,
                                   remote_node_device_num_of_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

5630
    dev = virNodeDeviceLookupByName(conn, args->name);
5631
    if (dev == NULL) {
5632
        remoteDispatchConnError(rerr, conn);
5633
        return -1;
5634 5635 5636
    }

    ret->num = virNodeDeviceNumOfCaps(dev);
5637
    if (ret->num < 0) {
5638
        remoteDispatchConnError(rerr, conn);
5639
        virNodeDeviceFree(dev);
5640 5641
        return -1;
    }
5642 5643 5644 5645 5646 5647 5648 5649

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceListCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
5650 5651
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
5652
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
5653
                                  remote_error *rerr,
5654 5655 5656 5657 5658 5659
                                  remote_node_device_list_caps_args *args,
                                  remote_node_device_list_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

5660
    dev = virNodeDeviceLookupByName(conn, args->name);
5661
    if (dev == NULL) {
5662
        remoteDispatchConnError(rerr, conn);
5663
        return -1;
5664 5665 5666
    }

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
5667
        virNodeDeviceFree(dev);
5668 5669 5670
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
5671 5672 5673 5674
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
5675
        virNodeDeviceFree(dev);
5676 5677
        remoteDispatchOOMError(rerr);
        return -1;
5678 5679 5680 5681 5682 5683
    }

    ret->names.names_len =
        virNodeDeviceListCaps (dev, ret->names.names_val,
                               args->maxnames);
    if (ret->names.names_len == -1) {
5684
        remoteDispatchConnError(rerr, conn);
5685
        virNodeDeviceFree(dev);
5686 5687 5688 5689
        VIR_FREE(ret->names.names_val);
        return -1;
    }

5690
    virNodeDeviceFree(dev);
5691 5692 5693 5694
    return 0;
}


5695 5696 5697 5698
static int
remoteDispatchNodeDeviceDettach (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5699
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5700 5701 5702 5703 5704 5705 5706 5707 5708
                                 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) {
5709
        remoteDispatchConnError(rerr, conn);
5710 5711 5712 5713 5714
        return -1;
    }

    if (virNodeDeviceDettach(dev) == -1) {
        remoteDispatchConnError(rerr, conn);
5715
        virNodeDeviceFree(dev);
5716 5717 5718
        return -1;
    }

5719
    virNodeDeviceFree(dev);
5720 5721 5722 5723 5724 5725 5726 5727
    return 0;
}


static int
remoteDispatchNodeDeviceReAttach (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
5728
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
5729 5730 5731 5732 5733 5734 5735 5736 5737
                                  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) {
5738
        remoteDispatchConnError(rerr, conn);
5739 5740 5741 5742 5743
        return -1;
    }

    if (virNodeDeviceReAttach(dev) == -1) {
        remoteDispatchConnError(rerr, conn);
5744
        virNodeDeviceFree(dev);
5745 5746 5747
        return -1;
    }

5748
    virNodeDeviceFree(dev);
5749 5750 5751 5752 5753 5754 5755 5756
    return 0;
}


static int
remoteDispatchNodeDeviceReset (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
5757
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
5758 5759 5760 5761 5762 5763 5764 5765 5766
                               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) {
5767
        remoteDispatchConnError(rerr, conn);
5768 5769 5770 5771 5772
        return -1;
    }

    if (virNodeDeviceReset(dev) == -1) {
        remoteDispatchConnError(rerr, conn);
5773
        virNodeDeviceFree(dev);
5774 5775 5776
        return -1;
    }

5777
    virNodeDeviceFree(dev);
5778 5779 5780 5781
    return 0;
}


5782 5783 5784 5785
static int
remoteDispatchNodeDeviceCreateXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
5786
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809
                                  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,
5810
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
5811 5812 5813 5814 5815 5816 5817 5818
                                remote_error *rerr,
                                remote_node_device_destroy_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virNodeDevicePtr dev;

    dev = virNodeDeviceLookupByName(conn, args->name);
    if (dev == NULL) {
5819
        remoteDispatchConnError(rerr, conn);
5820 5821 5822 5823 5824
        return -1;
    }

    if (virNodeDeviceDestroy(dev) == -1) {
        remoteDispatchConnError(rerr, conn);
5825
        virNodeDeviceFree(dev);
5826 5827 5828
        return -1;
    }

5829
    virNodeDeviceFree(dev);
5830 5831 5832 5833
    return 0;
}


5834 5835 5836 5837 5838 5839

/***************************
 * Register / deregister events
 ***************************/
static int
remoteDispatchDomainEventsRegister (struct qemud_server *server ATTRIBUTE_UNUSED,
5840 5841
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
5842
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
5843
                                    remote_error *rerr ATTRIBUTE_UNUSED,
5844 5845 5846 5847
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);
5848 5849 5850 5851 5852 5853
    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;
    }
5854

5855 5856 5857 5858 5859
    if ((callbackID = virConnectDomainEventRegisterAny(conn,
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
                                                       client, NULL)) < 0) {
5860 5861 5862
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
5863

5864
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;
5865

5866 5867 5868 5869 5870
    return 0;
}

static int
remoteDispatchDomainEventsDeregister (struct qemud_server *server ATTRIBUTE_UNUSED,
5871 5872
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
5873
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
5874
                                      remote_error *rerr ATTRIBUTE_UNUSED,
5875 5876 5877 5878 5879
                                      void *args ATTRIBUTE_UNUSED,
                                      remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

5880 5881
    if (client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] == -1) {
        remoteDispatchFormatError(rerr, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
5882 5883
        return -1;
    }
5884

5885 5886 5887 5888 5889
    if (virConnectDomainEventDeregisterAny(conn,
                                           client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
5890

5891
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;
5892 5893 5894 5895 5896
    return 0;
}

static void
remoteDispatchDomainEventSend (struct qemud_client *client,
5897 5898 5899
                               int procnr,
                               xdrproc_t proc,
                               void *data)
5900
{
5901
    struct qemud_client_message *msg = NULL;
5902
    XDR xdr;
5903
    unsigned int len;
5904

5905
    if (VIR_ALLOC(msg) < 0)
5906 5907
        return;

5908 5909
    msg->hdr.prog = REMOTE_PROGRAM;
    msg->hdr.vers = REMOTE_PROTOCOL_VERSION;
5910
    msg->hdr.proc = procnr;
5911
    msg->hdr.type = REMOTE_MESSAGE;
5912 5913
    msg->hdr.serial = 1;
    msg->hdr.status = REMOTE_OK;
5914

5915 5916
    if (remoteEncodeClientMessageHeader(msg) < 0)
        goto error;
5917

5918 5919
    /* Serialise the return header and event. */
    xdrmem_create (&xdr,
5920 5921
                   msg->buffer,
                   msg->bufferLength,
5922
                   XDR_ENCODE);
5923

5924 5925
    /* Skip over the header we just wrote */
    if (xdr_setpos (&xdr, msg->bufferOffset) == 0)
5926
        goto xdr_error;
5927

5928 5929
    if (!(proc)(&xdr, data)) {
        VIR_WARN("Failed to serialize domain event %d", procnr);
5930
        goto xdr_error;
5931
    }
5932

5933 5934
    /* Update length word to include payload*/
    len = msg->bufferOffset = xdr_getpos (&xdr);
5935 5936
    if (xdr_setpos (&xdr, 0) == 0)
        goto xdr_error;
5937

5938 5939
    if (!xdr_u_int (&xdr, &len))
        goto xdr_error;
5940 5941

    /* Send it. */
5942 5943 5944
    msg->async = 1;
    msg->bufferLength = len;
    msg->bufferOffset = 0;
5945 5946

    VIR_DEBUG("Queue event %d %d", procnr, msg->bufferLength);
5947
    qemudClientMessageQueuePush(&client->tx, msg);
5948
    qemudUpdateClientEvent(client);
5949 5950 5951 5952 5953 5954 5955 5956

    xdr_destroy (&xdr);
    return;

xdr_error:
    xdr_destroy(&xdr);
error:
    VIR_FREE(msg);
5957
}
5958

5959 5960 5961
static int
remoteDispatchNumOfSecrets (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client ATTRIBUTE_UNUSED,
5962 5963 5964
                            virConnectPtr conn,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *err,
5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979
                            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,
5980 5981 5982
                           virConnectPtr conn,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *err,
5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010
                           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,
6011 6012 6013
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
                               remote_error *err,
6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032
                               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,
6033 6034 6035
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064
                              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,
6065 6066 6067
                                virConnectPtr conn,
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                remote_error *err,
6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088
                                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
6089 6090
remoteDispatchSecretLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
6091 6092 6093
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *err,
6094 6095
                                  remote_secret_lookup_by_uuid_args *args,
                                  remote_secret_lookup_by_uuid_ret *ret)
6096 6097 6098
{
    virSecretPtr secret;

6099
    secret = virSecretLookupByUUID (conn, (unsigned char *)args->uuid);
6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112
    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,
6113 6114 6115
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139
                              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,
6140 6141 6142
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162
                              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;
}

6163 6164 6165
static int
remoteDispatchSecretLookupByUsage (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
6166 6167 6168
                                   virConnectPtr conn,
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                                   remote_error *err,
6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184
                                   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;
}

6185

6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205
static int remoteDispatchDomainIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                        struct qemud_client *client ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *hdr ATTRIBUTE_UNUSED,
                                        remote_error *err,
                                        remote_domain_is_active_args *args,
                                        remote_domain_is_active_ret *ret)
{
    virDomainPtr domain;

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

    ret->active = virDomainIsActive(domain);

    if (ret->active < 0) {
        remoteDispatchConnError(err, conn);
6206
        virDomainFree(domain);
6207 6208 6209
        return -1;
    }

6210
    virDomainFree(domain);
6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233
    return 0;
}

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

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

    ret->persistent = virDomainIsPersistent(domain);

    if (ret->persistent < 0) {
        remoteDispatchConnError(err, conn);
6234
        virDomainFree(domain);
6235 6236 6237
        return -1;
    }

6238
    virDomainFree(domain);
6239 6240 6241
    return 0;
}

O
Osier Yang 已提交
6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261
static int remoteDispatchDomainIsUpdated(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_updated_args *args,
                                            remote_domain_is_updated_ret *ret)
{
    virDomainPtr domain;

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

    ret->updated = virDomainIsUpdated(domain);

    if (ret->updated < 0) {
        remoteDispatchConnError(err, conn);
6262
        virDomainFree(domain);
O
Osier Yang 已提交
6263 6264 6265 6266 6267 6268 6269
        return -1;
    }

    virDomainFree(domain);
    return 0;
}

6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289
static int remoteDispatchInterfaceIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                           struct qemud_client *client ATTRIBUTE_UNUSED,
                                           virConnectPtr conn,
                                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                                           remote_error *err,
                                           remote_interface_is_active_args *args,
                                           remote_interface_is_active_ret *ret)
{
    virInterfacePtr iface;

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

    ret->active = virInterfaceIsActive(iface);

    if (ret->active < 0) {
        remoteDispatchConnError(err, conn);
6290
        virInterfaceFree(iface);
6291 6292 6293
        return -1;
    }

6294
    virInterfaceFree(iface);
6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317
    return 0;
}

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

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

    ret->active = virNetworkIsActive(network);

    if (ret->active < 0) {
        remoteDispatchConnError(err, conn);
6318
        virNetworkFree(network);
6319 6320 6321
        return -1;
    }

6322
    virNetworkFree(network);
6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345
    return 0;
}

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

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

    ret->persistent = virNetworkIsPersistent(network);

    if (ret->persistent < 0) {
        remoteDispatchConnError(err, conn);
6346
        virNetworkFree(network);
6347 6348 6349
        return -1;
    }

6350
    virNetworkFree(network);
6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373
    return 0;
}

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

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

    ret->active = virStoragePoolIsActive(pool);

    if (ret->active < 0) {
        remoteDispatchConnError(err, conn);
6374
        virStoragePoolFree(pool);
6375 6376 6377
        return -1;
    }

6378
    virStoragePoolFree(pool);
6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401
    return 0;
}

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

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

    ret->persistent = virStoragePoolIsPersistent(pool);

    if (ret->persistent < 0) {
        remoteDispatchConnError(err, conn);
6402
        virStoragePoolFree(pool);
6403 6404 6405
        return -1;
    }

6406
    virStoragePoolFree(pool);
6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429
    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;
}


6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451
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;
}


6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472
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;
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

    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) {
        remoteDispatchConnError(rerr, conn);
6498
        virDomainFree(dom);
6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516
        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);

6517 6518 6519 6520
    return 0;
}


6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539
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) {
        remoteDispatchConnError(rerr, conn);
6540
        virDomainFree(dom);
6541 6542 6543 6544 6545 6546 6547 6548 6549
        return -1;
    }

    virDomainFree(dom);

    return 0;
}


6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568
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) {
        remoteDispatchConnError(rerr, conn);
6569
        virDomainFree(dom);
6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596
        return -1;
    }

    virDomainFree(dom);

    return 0;
}

static int
remoteDispatchDomainMigrateSetMaxSpeed(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_speed_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

    if (virDomainMigrateSetMaxSpeed(dom, args->bandwidth, args->flags) == -1) {
        remoteDispatchConnError(rerr, conn);
6597
        virDomainFree(dom);
6598 6599 6600 6601 6602 6603 6604 6605
        return -1;
    }

    virDomainFree(dom);

    return 0;
}

C
Chris Lalancette 已提交
6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626
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) {
        remoteDispatchConnError(rerr, conn);
6627
        virDomainFree(domain);
C
Chris Lalancette 已提交
6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647
        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)
{
6648 6649 6650
    virDomainPtr domain = NULL;
    virDomainSnapshotPtr snapshot = NULL;
    int rc = -1;
C
Chris Lalancette 已提交
6651

6652 6653 6654 6655 6656 6657 6658
    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 已提交
6659 6660 6661

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainSnapshotGetXMLDesc(snapshot, args->flags);
6662 6663 6664 6665 6666 6667
    if (!ret->xml)
        goto cleanup;

    rc = 0;

cleanup:
6668 6669
    if (rc < 0)
        remoteDispatchConnError(rerr, conn);
6670
    if (snapshot)
C
Chris Lalancette 已提交
6671
        virDomainSnapshotFree(snapshot);
6672 6673
    if (domain)
        virDomainFree(domain);
C
Chris Lalancette 已提交
6674

6675
    return rc;
C
Chris Lalancette 已提交
6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697
}

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) {
        remoteDispatchConnError(rerr, conn);
6698
        virDomainFree(domain);
C
Chris Lalancette 已提交
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
        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) {
6742
        remoteDispatchConnError(rerr, conn);
C
Chris Lalancette 已提交
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
        virDomainFree(domain);
        VIR_FREE(ret->names.names_val);
        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) {
        remoteDispatchConnError(rerr, conn);
6774
        virDomainFree(domain);
C
Chris Lalancette 已提交
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
        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) {
        remoteDispatchConnError(rerr, conn);
6807
        virDomainFree(domain);
C
Chris Lalancette 已提交
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
        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) {
        remoteDispatchConnError(rerr, conn);
6839
        virDomainFree(domain);
C
Chris Lalancette 已提交
6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859
        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)
{
6860 6861 6862
    virDomainPtr domain = NULL;
    virDomainSnapshotPtr snapshot = NULL;
    int rc = -1;
C
Chris Lalancette 已提交
6863

6864 6865 6866 6867 6868 6869 6870
    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 已提交
6871

6872 6873 6874 6875 6876 6877
    if (virDomainRevertToSnapshot(snapshot, args->flags) == -1)
        goto cleanup;

    rc = 0;

cleanup:
6878 6879
    if (rc < 0)
        remoteDispatchConnError(rerr, conn);
6880
    if (snapshot)
C
Chris Lalancette 已提交
6881
        virDomainSnapshotFree(snapshot);
6882 6883
    if (domain)
        virDomainFree(domain);
C
Chris Lalancette 已提交
6884

6885
    return rc;
C
Chris Lalancette 已提交
6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896
}

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)
{
6897 6898 6899
    virDomainPtr domain = NULL;
    virDomainSnapshotPtr snapshot = NULL;
    int rc = -1;
C
Chris Lalancette 已提交
6900

6901 6902 6903 6904 6905 6906 6907
    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 已提交
6908

6909 6910 6911 6912 6913 6914
    if (virDomainSnapshotDelete(snapshot, args->flags) == -1)
        goto cleanup;

    rc = 0;

cleanup:
6915 6916
    if (rc < 0)
        remoteDispatchConnError(rerr, conn);
6917
    if (snapshot)
C
Chris Lalancette 已提交
6918
        virDomainSnapshotFree(snapshot);
6919 6920
    if (domain)
        virDomainFree(domain);
C
Chris Lalancette 已提交
6921

6922
    return rc;
C
Chris Lalancette 已提交
6923 6924
}

6925

6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997
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;
}


6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085

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) {
        remoteDispatchConnError(rerr, conn);
7086
        virNWFilterFree(nwfilter);
7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148
        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) {
        remoteDispatchConnError(rerr, conn);
7149
        virNWFilterFree(nwfilter);
7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176
        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;
}


7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196
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) {
        remoteDispatchConnError(rerr, conn);
7197
        virDomainFree(dom);
7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209
        return -1;
    }

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

    virDomainFree(dom);

    return 0;
}

C
Chris Lalancette 已提交
7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229
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) {
        remoteDispatchConnError(rerr, conn);
7230
        virDomainFree(domain);
C
Chris Lalancette 已提交
7231 7232 7233 7234 7235 7236 7237 7238
        return -1;
    }

    virDomainFree(domain);

    return 0;
}

7239

7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272
static int
remoteDispatchDomainOpenConsole(struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
                                virConnectPtr conn,
                                remote_message_header *hdr,
                                remote_error *rerr,
                                remote_domain_open_console_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    int r;
    struct qemud_client_stream *stream;
    virDomainPtr dom;

    CHECK_CONN (client);

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

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

    r = virDomainOpenConsole(dom,
                             args->devname ? *args->devname : NULL,
                             stream->st,
                             args->flags);
    if (r == -1) {
7273
        remoteDispatchConnError(rerr, conn);
7274 7275 7276 7277 7278 7279 7280
        virDomainFree(dom);
        remoteFreeClientStream(client, stream);
        return -1;
    }

    if (remoteAddClientStream(client, stream, 1) < 0) {
        remoteDispatchConnError(rerr, conn);
7281
        virDomainFree(dom);
7282 7283 7284 7285 7286 7287 7288 7289 7290 7291
        virStreamAbort(stream->st);
        remoteFreeClientStream(client, stream);
        return -1;
    }

    virDomainFree(dom);
    return 0;
}


7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317
/*----- 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 已提交
7318
static virInterfacePtr
7319
get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
7320
{
7321
    return virGetInterface (conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
7322 7323
}

7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337
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;
}

7338 7339 7340
static virSecretPtr
get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret)
{
7341
    return virGetSecret (conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
7342 7343
}

7344 7345 7346 7347 7348 7349
static virNWFilterPtr
get_nonnull_nwfilter (virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
{
    return virGetNWFilter (conn, nwfilter.name, BAD_CAST nwfilter.uuid);
}

C
Chris Lalancette 已提交
7350
static virDomainSnapshotPtr
7351
get_nonnull_domain_snapshot (virDomainPtr domain, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
7352 7353 7354 7355
{
    return virGetDomainSnapshot(domain, snapshot.name);
}

7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371
/* 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 已提交
7372 7373 7374 7375 7376 7377 7378 7379
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);
}

7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393
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);
}
7394 7395 7396 7397 7398 7399

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

static void
make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
{
7404
    memcpy (secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
7405 7406
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = strdup (secret_src->usageID);
7407
}
7408 7409 7410 7411 7412 7413 7414

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 已提交
7415 7416 7417 7418 7419 7420 7421

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