remote.c 214.1 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007-2010 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Richard W.M. Jones <rjones@redhat.com>
 */

#include <config.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/poll.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <pwd.h>
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
43
#include <fnmatch.h>
44
#include <arpa/inet.h>
45
#include "virterror_internal.h"
46

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

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

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

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

64 65
static virDomainPtr get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network (virConnectPtr conn, remote_nonnull_network network);
66
static virInterfacePtr get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface);
67 68
static virStoragePoolPtr get_nonnull_storage_pool (virConnectPtr conn, remote_nonnull_storage_pool pool);
static virStorageVolPtr get_nonnull_storage_vol (virConnectPtr conn, remote_nonnull_storage_vol vol);
69
static virSecretPtr get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret);
70
static virNWFilterPtr get_nonnull_nwfilter (virConnectPtr conn, remote_nonnull_nwfilter nwfilter);
C
Chris Lalancette 已提交
71
static virDomainSnapshotPtr get_nonnull_domain_snapshot (virConnectPtr conn, remote_nonnull_domain_snapshot snapshot);
72 73
static void make_nonnull_domain (remote_nonnull_domain *dom_dst, virDomainPtr dom_src);
static void make_nonnull_network (remote_nonnull_network *net_dst, virNetworkPtr net_src);
D
Daniel Veillard 已提交
74
static void make_nonnull_interface (remote_nonnull_interface *interface_dst, virInterfacePtr interface_src);
75 76
static void make_nonnull_storage_pool (remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src);
static void make_nonnull_storage_vol (remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src);
77
static void make_nonnull_node_device (remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src);
78
static void make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src);
79
static void make_nonnull_nwfilter (remote_nonnull_nwfilter *net_dst, virNWFilterPtr nwfilter_src);
C
Chris Lalancette 已提交
80
static void make_nonnull_domain_snapshot (remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src);
81

82

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

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

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

    return &(dispatch_table[proc]);
}

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

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

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

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

120
    virMutexLock(&client->lock);
121

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

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

    virMutexUnlock(&client->lock);
133

134 135
    return 0;
}
136

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static int remoteRelayDomainEventReboot(virConnectPtr conn ATTRIBUTE_UNUSED,
                                        virDomainPtr dom,
                                        void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_reboot_msg data;

    if (!client)
        return -1;

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

    virMutexLock(&client->lock);

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

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

    virMutexUnlock(&client->lock);

    return 0;
}

164

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
static int remoteRelayDomainEventRTCChange(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           long long offset,
                                           void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_rtc_change_msg data;

    if (!client)
        return -1;

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

    virMutexLock(&client->lock);

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

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

    virMutexUnlock(&client->lock);

    return 0;
}


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

    if (!client)
        return -1;

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

    virMutexLock(&client->lock);

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

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

    virMutexUnlock(&client->lock);

    return 0;
}


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

    if (!client)
        return -1;

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

    virMutexLock(&client->lock);

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

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

    virMutexUnlock(&client->lock);

    return 0;
}


259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
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;
}


323
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
324
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
325
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
326
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
327
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
328
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
329
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
330 331 332 333
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

334 335 336
/*----- Functions. -----*/

static int
337
remoteDispatchOpen (struct qemud_server *server,
338
                    struct qemud_client *client,
339
                    virConnectPtr conn,
340
                    remote_message_header *hdr ATTRIBUTE_UNUSED,
341
                    remote_error *rerr,
342 343 344
                    struct remote_open_args *args, void *ret ATTRIBUTE_UNUSED)
{
    const char *name;
345
    int flags, rc;
346 347

    /* Already opened? */
348
    if (conn) {
349 350
        remoteDispatchFormatError (rerr, "%s", _("connection already open"));
        return -1;
351 352
    }

353 354 355
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
356

357 358 359 360 361 362 363 364 365 366 367 368 369
    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);

370
    if (client->conn == NULL)
371 372
        remoteDispatchConnError(rerr, NULL);

373
    rc = client->conn ? 0 : -1;
374
    virMutexUnlock(&client->lock);
375
    return rc;
376 377
}

378 379 380 381
#define CHECK_CONN(client)                                              \
    if (!client->conn) {                                                \
        remoteDispatchFormatError (rerr, "%s", _("connection not open")); \
        return -1;                                                      \
382 383 384
    }

static int
385
remoteDispatchClose (struct qemud_server *server ATTRIBUTE_UNUSED,
386 387
                     struct qemud_client *client ATTRIBUTE_UNUSED,
                     virConnectPtr conn ATTRIBUTE_UNUSED,
388
                     remote_message_header *hdr ATTRIBUTE_UNUSED,
389
                     remote_error *rerr ATTRIBUTE_UNUSED,
390 391
                     void *args ATTRIBUTE_UNUSED, void *ret ATTRIBUTE_UNUSED)
{
392 393 394
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
395

396
    client->closing = 1;
397

398
    virMutexUnlock(&client->lock);
399
    return 0;
400 401
}

402
static int
403
remoteDispatchSupportsFeature (struct qemud_server *server ATTRIBUTE_UNUSED,
404 405
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
406
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
407
                               remote_error *rerr,
408 409
                               remote_supports_feature_args *args, remote_supports_feature_ret *ret)
{
410
    ret->supported = virDrvSupportsFeature (conn, args->feature);
411 412

    if (ret->supported == -1) {
413
        remoteDispatchConnError(rerr, conn);
414 415
        return -1;
    }
416 417 418 419

    return 0;
}

420
static int
421
remoteDispatchGetType (struct qemud_server *server ATTRIBUTE_UNUSED,
422 423
                       struct qemud_client *client ATTRIBUTE_UNUSED,
                       virConnectPtr conn,
424
                       remote_message_header *hdr ATTRIBUTE_UNUSED,
425
                       remote_error *rerr,
426 427 428 429
                       void *args ATTRIBUTE_UNUSED, remote_get_type_ret *ret)
{
    const char *type;

430
    type = virConnectGetType (conn);
431
    if (type == NULL) {
432
        remoteDispatchConnError(rerr, conn);
433 434
        return -1;
    }
435 436 437 438 439 440

    /* We have to strdup because remoteDispatchClientRequest will
     * free this string after it's been serialised.
     */
    ret->type = strdup (type);
    if (!ret->type) {
441
        remoteDispatchOOMError(rerr);
442
        return -1;
443 444 445 446 447 448
    }

    return 0;
}

static int
449
remoteDispatchGetVersion (struct qemud_server *server ATTRIBUTE_UNUSED,
450 451
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
452
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
453
                          remote_error *rerr,
454 455 456 457 458
                          void *args ATTRIBUTE_UNUSED,
                          remote_get_version_ret *ret)
{
    unsigned long hvVer;

459 460
    if (virConnectGetVersion (conn, &hvVer) == -1) {
        remoteDispatchConnError(rerr, conn);
461
        return -1;
462
    }
463 464 465 466 467

    ret->hv_ver = hvVer;
    return 0;
}

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
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;
}

488
static int
489
remoteDispatchGetHostname (struct qemud_server *server ATTRIBUTE_UNUSED,
490 491
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
492
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
493
                           remote_error *rerr,
494 495 496 497 498
                           void *args ATTRIBUTE_UNUSED,
                           remote_get_hostname_ret *ret)
{
    char *hostname;

499
    hostname = virConnectGetHostname (conn);
500
    if (hostname == NULL) {
501
        remoteDispatchConnError(rerr, conn);
502 503
        return -1;
    }
504 505 506 507 508

    ret->hostname = hostname;
    return 0;
}

509 510
static int
remoteDispatchGetUri (struct qemud_server *server ATTRIBUTE_UNUSED,
511 512
                      struct qemud_client *client ATTRIBUTE_UNUSED,
                      virConnectPtr conn,
513
                      remote_message_header *hdr ATTRIBUTE_UNUSED,
514
                      remote_error *rerr,
515 516 517 518 519 520
                      void *args ATTRIBUTE_UNUSED,
                      remote_get_uri_ret *ret)
{
    char *uri;
    CHECK_CONN(client);

521
    uri = virConnectGetURI (conn);
522
    if (uri == NULL) {
523
        remoteDispatchConnError(rerr, conn);
524 525
        return -1;
    }
526 527 528 529 530

    ret->uri = uri;
    return 0;
}

531
static int
532
remoteDispatchGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
533 534
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
535
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
536
                           remote_error *rerr,
537 538 539 540 541 542
                           remote_get_max_vcpus_args *args,
                           remote_get_max_vcpus_ret *ret)
{
    char *type;

    type = args->type ? *args->type : NULL;
543
    ret->max_vcpus = virConnectGetMaxVcpus (conn, type);
544
    if (ret->max_vcpus == -1) {
545
        remoteDispatchConnError(rerr, conn);
546 547
        return -1;
    }
548 549 550 551 552

    return 0;
}

static int
553
remoteDispatchNodeGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
554 555
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
556
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
557
                           remote_error *rerr,
558 559 560 561 562
                           void *args ATTRIBUTE_UNUSED,
                           remote_node_get_info_ret *ret)
{
    virNodeInfo info;

563 564
    if (virNodeGetInfo (conn, &info) == -1) {
        remoteDispatchConnError(rerr, conn);
565
        return -1;
566
    }
567 568 569 570 571 572 573 574 575 576 577 578 579 580

    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
581
remoteDispatchGetCapabilities (struct qemud_server *server ATTRIBUTE_UNUSED,
582 583
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
584
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
585
                               remote_error *rerr,
586 587 588 589 590
                               void *args ATTRIBUTE_UNUSED,
                               remote_get_capabilities_ret *ret)
{
    char *caps;

591
    caps = virConnectGetCapabilities (conn);
592
    if (caps == NULL) {
593
        remoteDispatchConnError(rerr, conn);
594 595
        return -1;
    }
596 597 598 599 600

    ret->capabilities = caps;
    return 0;
}

601 602
static int
remoteDispatchNodeGetCellsFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
603 604
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
605
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
606
                                      remote_error *rerr,
607 608 609
                                      remote_node_get_cells_free_memory_args *args,
                                      remote_node_get_cells_free_memory_ret *ret)
{
D
Daniel P. Berrange 已提交
610
    int err;
611 612

    if (args->maxCells > REMOTE_NODE_MAX_CELLS) {
613 614 615
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxCells > REMOTE_NODE_MAX_CELLS"));
        return -1;
616 617 618
    }

    /* Allocate return buffer. */
619
    if (VIR_ALLOC_N(ret->freeMems.freeMems_val, args->maxCells) < 0) {
620 621
        remoteDispatchOOMError(rerr);
        return -1;
622
    }
623

D
Daniel P. Berrange 已提交
624 625 626 627 628
    err = virNodeGetCellsFreeMemory(conn,
                                    (unsigned long long *)ret->freeMems.freeMems_val,
                                    args->startCell,
                                    args->maxCells);
    if (err <= 0) {
629
        VIR_FREE(ret->freeMems.freeMems_val);
630
        remoteDispatchConnError(rerr, conn);
631
        return -1;
632
    }
D
Daniel P. Berrange 已提交
633
    ret->freeMems.freeMems_len = err;
634 635 636 637 638 639 640

    return 0;
}


static int
remoteDispatchNodeGetFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
641 642
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
643
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
644
                                 remote_error *rerr,
645 646 647 648 649
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_node_get_free_memory_ret *ret)
{
    unsigned long long freeMem;

650
    freeMem = virNodeGetFreeMemory(conn);
651
    if (freeMem == 0) {
652
        remoteDispatchConnError(rerr, conn);
653 654
        return -1;
    }
655 656 657 658 659
    ret->freeMem = freeMem;
    return 0;
}


660
static int
661
remoteDispatchDomainGetSchedulerType (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 671 672
                                      remote_domain_get_scheduler_type_args *args,
                                      remote_domain_get_scheduler_type_ret *ret)
{
    virDomainPtr dom;
    char *type;
    int nparams;

673
    dom = get_nonnull_domain (conn, args->dom);
674
    if (dom == NULL) {
675
        remoteDispatchConnError(rerr, conn);
676
        return -1;
677 678 679
    }

    type = virDomainGetSchedulerType (dom, &nparams);
680 681
    if (type == NULL) {
        virDomainFree(dom);
682
        remoteDispatchConnError(rerr, conn);
683 684
        return -1;
    }
685 686 687

    ret->type = type;
    ret->nparams = nparams;
688
    virDomainFree(dom);
689 690 691 692
    return 0;
}

static int
693
remoteDispatchDomainGetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
694 695
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
696
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
697
                                            remote_error *rerr,
698 699 700 701 702 703 704 705 706 707
                                            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) {
708 709
        remoteDispatchFormatError (rerr, "%s", _("nparams too large"));
        return -1;
710
    }
711
    if (VIR_ALLOC_N(params, nparams) < 0) {
712 713
        remoteDispatchOOMError(rerr);
        return -1;
714 715
    }

716
    dom = get_nonnull_domain (conn, args->dom);
717
    if (dom == NULL) {
718
        VIR_FREE(params);
719
        remoteDispatchConnError(rerr, conn);
720
        return -1;
721 722 723 724
    }

    r = virDomainGetSchedulerParameters (dom, params, &nparams);
    if (r == -1) {
725
        virDomainFree(dom);
726
        VIR_FREE(params);
727
        remoteDispatchConnError(rerr, conn);
728 729 730 731 732
        return -1;
    }

    /* Serialise the scheduler parameters. */
    ret->params.params_len = nparams;
733 734
    if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
        goto oom;
735 736 737 738

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

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
        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:
757
            remoteDispatchFormatError (rerr, "%s", _("unknown type"));
758
            goto cleanup;
759 760
        }
    }
761
    virDomainFree(dom);
762
    VIR_FREE(params);
763 764

    return 0;
765 766

oom:
767
    remoteDispatchOOMError(rerr);
768 769 770 771 772
cleanup:
    virDomainFree(dom);
    for (i = 0 ; i < nparams ; i++)
        VIR_FREE(ret->params.params_val[i].field);
    VIR_FREE(params);
773
    return -1;
774 775 776
}

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

    /* Deserialise parameters. */
    for (i = 0; i < nparams; ++i) {
C
Chris Lalancette 已提交
802 803 804 805 806
        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;
        }
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
        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;
        }
    }

824
    dom = get_nonnull_domain (conn, args->dom);
825
    if (dom == NULL) {
826
        VIR_FREE(params);
827
        remoteDispatchConnError(rerr, conn);
828
        return -1;
829 830 831
    }

    r = virDomainSetSchedulerParameters (dom, params, nparams);
832
    virDomainFree(dom);
833
    VIR_FREE(params);
834
    if (r == -1) {
835
        remoteDispatchConnError(rerr, conn);
836 837
        return -1;
    }
838 839 840 841

    return 0;
}

842
static int
843
remoteDispatchDomainBlockStats (struct qemud_server *server ATTRIBUTE_UNUSED,
844 845
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
846
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
847
                                remote_error *rerr,
848 849 850 851 852 853 854
                                remote_domain_block_stats_args *args,
                                remote_domain_block_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainBlockStats stats;

855
    dom = get_nonnull_domain (conn, args->dom);
856
    if (dom == NULL) {
857
        remoteDispatchConnError(rerr, conn);
858
        return -1;
859 860 861
    }
    path = args->path;

D
Daniel P. Berrange 已提交
862 863
    if (virDomainBlockStats (dom, path, &stats, sizeof stats) == -1) {
        virDomainFree (dom);
864
        remoteDispatchConnError(rerr, conn);
865
        return -1;
D
Daniel P. Berrange 已提交
866 867
    }
    virDomainFree (dom);
868 869 870 871 872 873 874 875 876 877 878

    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
879
remoteDispatchDomainInterfaceStats (struct qemud_server *server ATTRIBUTE_UNUSED,
880 881
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
882
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
883
                                    remote_error *rerr,
884 885 886 887 888 889 890
                                    remote_domain_interface_stats_args *args,
                                    remote_domain_interface_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainInterfaceStats stats;

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

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

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

917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
static int
remoteDispatchDomainMemoryStats (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_domain_memory_stats_args *args,
                                 remote_domain_memory_stats_ret *ret)
{
    virDomainPtr dom;
    struct _virDomainMemoryStat *stats;
    unsigned int nr_stats, i;

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

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

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

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

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

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

973 974
static int
remoteDispatchDomainBlockPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
975 976
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
977
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
978
                               remote_error *rerr,
979 980 981 982 983 984 985 986 987
                               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;

988
    dom = get_nonnull_domain (conn, args->dom);
989
    if (dom == NULL) {
990
        remoteDispatchConnError(rerr, conn);
991
        return -1;
992 993 994 995 996 997 998
    }
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
999
        virDomainFree (dom);
1000 1001 1002
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
        return -1;
1003 1004 1005
    }

    ret->buffer.buffer_len = size;
1006
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
1007
        virDomainFree (dom);
1008
        remoteDispatchOOMError(rerr);
1009
        return -1;
1010 1011 1012 1013 1014 1015
    }

    if (virDomainBlockPeek (dom, path, offset, size,
                            ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
1016
        remoteDispatchConnError(rerr, conn);
1017 1018 1019 1020 1021 1022 1023
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

R
Richard W.M. Jones 已提交
1024 1025
static int
remoteDispatchDomainMemoryPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
1026 1027
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
1028
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
1029
                                remote_error *rerr,
R
Richard W.M. Jones 已提交
1030 1031 1032 1033 1034 1035 1036 1037
                                remote_domain_memory_peek_args *args,
                                remote_domain_memory_peek_ret *ret)
{
    virDomainPtr dom;
    unsigned long long offset;
    size_t size;
    unsigned int flags;

1038
    dom = get_nonnull_domain (conn, args->dom);
R
Richard W.M. Jones 已提交
1039
    if (dom == NULL) {
1040
        remoteDispatchConnError(rerr, conn);
1041
        return -1;
R
Richard W.M. Jones 已提交
1042 1043 1044 1045 1046 1047
    }
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1048
        virDomainFree (dom);
1049 1050 1051
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
        return -1;
R
Richard W.M. Jones 已提交
1052 1053 1054 1055 1056
    }

    ret->buffer.buffer_len = size;
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
        virDomainFree (dom);
1057
        remoteDispatchOOMError(rerr);
1058
        return -1;
R
Richard W.M. Jones 已提交
1059 1060 1061 1062 1063 1064
    }

    if (virDomainMemoryPeek (dom, offset, size,
                             ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
1065
        remoteDispatchConnError(rerr, conn);
R
Richard W.M. Jones 已提交
1066 1067 1068 1069 1070 1071 1072
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

1073
static int
1074
remoteDispatchDomainAttachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
1075 1076
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1077
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1078
                                  remote_error *rerr,
1079 1080 1081 1082 1083
                                  remote_domain_attach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1084
    dom = get_nonnull_domain (conn, args->dom);
1085
    if (dom == NULL) {
1086
        remoteDispatchConnError(rerr, conn);
1087
        return -1;
1088 1089
    }

1090 1091
    if (virDomainAttachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
1092
        remoteDispatchConnError(rerr, conn);
1093
        return -1;
1094 1095
    }
    virDomainFree(dom);
1096 1097 1098
    return 0;
}

J
Jim Fehlig 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
static int
remoteDispatchDomainAttachDeviceFlags (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_attach_device_flags_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

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

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
static int
remoteDispatchDomainUpdateDeviceFlags (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_update_device_flags_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

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

1151
static int
1152
remoteDispatchDomainCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
1153 1154
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
1155
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
1156
                            remote_error *rerr,
1157 1158 1159 1160 1161
                            remote_domain_create_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1162
    dom = get_nonnull_domain (conn, args->dom);
1163
    if (dom == NULL) {
1164
        remoteDispatchConnError(rerr, conn);
1165
        return -1;
1166 1167
    }

1168 1169
    if (virDomainCreate (dom) == -1) {
        virDomainFree(dom);
1170
        remoteDispatchConnError(rerr, conn);
1171
        return -1;
1172 1173
    }
    virDomainFree(dom);
1174 1175 1176 1177
    return 0;
}

static int
1178
remoteDispatchDomainCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1179 1180
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1181
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1182 1183 1184
                               remote_error *rerr,
                               remote_domain_create_xml_args *args,
                               remote_domain_create_xml_ret *ret)
1185 1186 1187
{
    virDomainPtr dom;

1188
    dom = virDomainCreateXML (conn, args->xml_desc, args->flags);
1189
    if (dom == NULL) {
1190
        remoteDispatchConnError(rerr, conn);
1191 1192
        return -1;
    }
1193 1194

    make_nonnull_domain (&ret->dom, dom);
1195
    virDomainFree(dom);
1196 1197 1198 1199 1200

    return 0;
}

static int
1201
remoteDispatchDomainDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1202 1203
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1204
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1205
                               remote_error *rerr,
1206 1207 1208 1209 1210
                               remote_domain_define_xml_args *args,
                               remote_domain_define_xml_ret *ret)
{
    virDomainPtr dom;

1211
    dom = virDomainDefineXML (conn, args->xml);
1212
    if (dom == NULL) {
1213
        remoteDispatchConnError(rerr, conn);
1214 1215
        return -1;
    }
1216 1217

    make_nonnull_domain (&ret->dom, dom);
1218
    virDomainFree(dom);
1219 1220 1221 1222 1223

    return 0;
}

static int
1224
remoteDispatchDomainDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
1225 1226
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1227
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1228
                             remote_error *rerr,
1229 1230 1231 1232 1233
                             remote_domain_destroy_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1234
    dom = get_nonnull_domain (conn, args->dom);
1235
    if (dom == NULL) {
1236
        remoteDispatchConnError(rerr, conn);
1237
        return -1;
1238 1239
    }

1240 1241
    if (virDomainDestroy (dom) == -1) {
        virDomainFree(dom);
1242
        remoteDispatchConnError(rerr, conn);
1243
        return -1;
1244 1245
    }
    virDomainFree(dom);
1246 1247 1248 1249
    return 0;
}

static int
1250
remoteDispatchDomainDetachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
1251 1252
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1253
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1254
                                  remote_error *rerr,
1255 1256 1257 1258 1259
                                  remote_domain_detach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1260
    dom = get_nonnull_domain (conn, args->dom);
1261
    if (dom == NULL) {
1262
        remoteDispatchConnError(rerr, conn);
1263
        return -1;
1264 1265
    }

1266 1267
    if (virDomainDetachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
J
Jim Fehlig 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    virDomainFree(dom);
    return 0;
}

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

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

    if (virDomainDetachDeviceFlags (dom, args->xml, args->flags) == -1) {
        virDomainFree(dom);
1295
        remoteDispatchConnError(rerr, conn);
1296
        return -1;
1297
    }
1298

1299
    virDomainFree(dom);
1300 1301 1302 1303
    return 0;
}

static int
1304
remoteDispatchDomainDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1305 1306
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1307
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1308
                             remote_error *rerr,
1309 1310 1311 1312 1313
                             remote_domain_dump_xml_args *args,
                             remote_domain_dump_xml_ret *ret)
{
    virDomainPtr dom;

1314
    dom = get_nonnull_domain (conn, args->dom);
1315
    if (dom == NULL) {
1316
        remoteDispatchConnError(rerr, conn);
1317
        return -1;
1318 1319 1320 1321
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainGetXMLDesc (dom, args->flags);
1322
    if (!ret->xml) {
1323
        virDomainFree(dom);
1324
        remoteDispatchConnError(rerr, conn);
1325
        return -1;
1326 1327
    }
    virDomainFree(dom);
1328 1329 1330
    return 0;
}

1331 1332 1333 1334
static int
remoteDispatchDomainXmlFromNative (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1335
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
                                   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,
1356
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
                                 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;
}


1374
static int
1375
remoteDispatchDomainGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
1376 1377
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1378
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1379
                                  remote_error *rerr,
1380 1381 1382 1383 1384
                                  remote_domain_get_autostart_args *args,
                                  remote_domain_get_autostart_ret *ret)
{
    virDomainPtr dom;

1385
    dom = get_nonnull_domain (conn, args->dom);
1386
    if (dom == NULL) {
1387
        remoteDispatchConnError(rerr, conn);
1388
        return -1;
1389 1390
    }

1391 1392
    if (virDomainGetAutostart (dom, &ret->autostart) == -1) {
        virDomainFree(dom);
1393
        remoteDispatchConnError(rerr, conn);
1394
        return -1;
1395 1396
    }
    virDomainFree(dom);
1397 1398 1399 1400
    return 0;
}

static int
1401
remoteDispatchDomainGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
1402 1403
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1404
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1405
                             remote_error *rerr,
1406 1407 1408 1409 1410 1411
                             remote_domain_get_info_args *args,
                             remote_domain_get_info_ret *ret)
{
    virDomainPtr dom;
    virDomainInfo info;

1412
    dom = get_nonnull_domain (conn, args->dom);
1413
    if (dom == NULL) {
1414
        remoteDispatchConnError(rerr, conn);
1415
        return -1;
1416 1417
    }

1418 1419
    if (virDomainGetInfo (dom, &info) == -1) {
        virDomainFree(dom);
1420
        remoteDispatchConnError(rerr, conn);
1421
        return -1;
1422
    }
1423 1424 1425 1426 1427 1428 1429

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

1430 1431
    virDomainFree(dom);

1432 1433 1434 1435
    return 0;
}

static int
1436
remoteDispatchDomainGetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
1437 1438
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1439
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1440
                                  remote_error *rerr,
1441 1442 1443 1444 1445
                                  remote_domain_get_max_memory_args *args,
                                  remote_domain_get_max_memory_ret *ret)
{
    virDomainPtr dom;

1446
    dom = get_nonnull_domain (conn, args->dom);
1447
    if (dom == NULL) {
1448
        remoteDispatchConnError(rerr, conn);
1449
        return -1;
1450 1451 1452
    }

    ret->memory = virDomainGetMaxMemory (dom);
1453 1454
    if (ret->memory == 0) {
        virDomainFree(dom);
1455
        remoteDispatchConnError(rerr, conn);
1456 1457 1458
        return -1;
    }
    virDomainFree(dom);
1459 1460 1461 1462
    return 0;
}

static int
1463
remoteDispatchDomainGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1464 1465
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
1466
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
1467
                                 remote_error *rerr,
1468 1469 1470 1471 1472
                                 remote_domain_get_max_vcpus_args *args,
                                 remote_domain_get_max_vcpus_ret *ret)
{
    virDomainPtr dom;

1473
    dom = get_nonnull_domain (conn, args->dom);
1474
    if (dom == NULL) {
1475
        remoteDispatchConnError(rerr, conn);
1476
        return -1;
1477 1478 1479
    }

    ret->num = virDomainGetMaxVcpus (dom);
1480 1481
    if (ret->num == -1) {
        virDomainFree(dom);
1482
        remoteDispatchConnError(rerr, conn);
1483 1484 1485
        return -1;
    }
    virDomainFree(dom);
1486 1487 1488
    return 0;
}

1489 1490 1491 1492
static int
remoteDispatchDomainGetSecurityLabel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1493
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
                                     remote_error *rerr,
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
{
    virDomainPtr dom;
    virSecurityLabel seclabel;

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

    memset(&seclabel, 0, sizeof seclabel);
    if (virDomainGetSecurityLabel(dom, &seclabel) == -1) {
        virDomainFree(dom);
1510
        remoteDispatchConnError(rerr, conn);
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
        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,
1531
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1532 1533 1534 1535 1536 1537 1538 1539
                                   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) {
1540
        remoteDispatchConnError(rerr, conn);
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
        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;
}

1561
static int
1562
remoteDispatchDomainGetOsType (struct qemud_server *server ATTRIBUTE_UNUSED,
1563 1564
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1565
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1566
                               remote_error *rerr,
1567 1568 1569 1570 1571
                               remote_domain_get_os_type_args *args,
                               remote_domain_get_os_type_ret *ret)
{
    virDomainPtr dom;

1572
    dom = get_nonnull_domain (conn, args->dom);
1573
    if (dom == NULL) {
1574
        remoteDispatchConnError(rerr, conn);
1575
        return -1;
1576 1577 1578 1579
    }

    /* remoteDispatchClientRequest will free this */
    ret->type = virDomainGetOSType (dom);
1580
    if (ret->type == NULL) {
1581
        virDomainFree(dom);
1582
        remoteDispatchConnError(rerr, conn);
1583
        return -1;
1584 1585
    }
    virDomainFree(dom);
1586 1587 1588 1589
    return 0;
}

static int
1590
remoteDispatchDomainGetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1591 1592
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
1593
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
1594
                              remote_error *rerr,
1595 1596 1597
                              remote_domain_get_vcpus_args *args,
                              remote_domain_get_vcpus_ret *ret)
{
1598 1599 1600
    virDomainPtr dom = NULL;
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
1601 1602
    int info_len, i;

1603
    dom = get_nonnull_domain (conn, args->dom);
1604
    if (dom == NULL) {
1605
        remoteDispatchConnError(rerr, conn);
1606
        return -1;
1607 1608 1609
    }

    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1610
        virDomainFree(dom);
1611 1612
        remoteDispatchFormatError (rerr, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
        return -1;
1613 1614
    }

1615
    if (args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1616
        virDomainFree(dom);
1617 1618
        remoteDispatchFormatError (rerr, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
        return -1;
1619 1620 1621
    }

    /* Allocate buffers to take the results. */
1622 1623
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
        goto oom;
1624 1625
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
1626
        goto oom;
1627 1628 1629 1630

    info_len = virDomainGetVcpus (dom,
                                  info, args->maxinfo,
                                  cpumaps, args->maplen);
1631
    if (info_len == -1) {
1632 1633
        VIR_FREE(info);
        VIR_FREE(cpumaps);
1634
        virDomainFree(dom);
1635
        remoteDispatchConnError(rerr, conn);
1636 1637
        return -1;
    }
1638 1639 1640

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
1641 1642
    if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
        goto oom;
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654

    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.
     */
1655
    ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
1656 1657
    ret->cpumaps.cpumaps_val = (char *) cpumaps;

1658
    VIR_FREE(info);
1659
    virDomainFree(dom);
1660
    return 0;
1661 1662 1663 1664 1665

oom:
    VIR_FREE(info);
    VIR_FREE(cpumaps);
    virDomainFree(dom);
1666 1667
    remoteDispatchOOMError(rerr);
    return -1;
1668 1669
}

1670
static int
1671
remoteDispatchDomainMigratePrepare (struct qemud_server *server ATTRIBUTE_UNUSED,
1672 1673
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1674
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1675
                                    remote_error *rerr,
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
                                    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 ... */
1690
    if (VIR_ALLOC(uri_out) < 0) {
1691 1692
        remoteDispatchOOMError(rerr);
        return -1;
1693
    }
1694

1695
    r = virDomainMigratePrepare (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1696 1697
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1698
    if (r == -1) {
1699
        VIR_FREE(uri_out);
1700
        remoteDispatchConnError(rerr, conn);
D
Daniel P. Berrange 已提交
1701 1702
        return -1;
    }
1703 1704 1705 1706 1707 1708

    /* 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 已提交
1709 1710
    if (*uri_out == NULL) {
        ret->uri_out = NULL;
1711
        VIR_FREE(uri_out);
D
Daniel P. Berrange 已提交
1712 1713 1714
    } else {
        ret->uri_out = uri_out;
    }
1715 1716 1717 1718 1719

    return 0;
}

static int
1720
remoteDispatchDomainMigratePerform (struct qemud_server *server ATTRIBUTE_UNUSED,
1721 1722
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1723
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1724
                                    remote_error *rerr,
1725 1726 1727 1728 1729 1730 1731
                                    remote_domain_migrate_perform_args *args,
                                    void *ret ATTRIBUTE_UNUSED)
{
    int r;
    virDomainPtr dom;
    char *dname;

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

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

D
Daniel P. Berrange 已提交
1740 1741 1742 1743 1744
    r = virDomainMigratePerform (dom,
                                 args->cookie.cookie_val,
                                 args->cookie.cookie_len,
                                 args->uri,
                                 args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1745
    virDomainFree (dom);
1746
    if (r == -1) {
1747
        remoteDispatchConnError(rerr, conn);
1748 1749
        return -1;
    }
1750 1751 1752 1753 1754

    return 0;
}

static int
1755
remoteDispatchDomainMigrateFinish (struct qemud_server *server ATTRIBUTE_UNUSED,
1756 1757
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1758
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1759
                                   remote_error *rerr,
1760 1761 1762 1763 1764 1765
                                   remote_domain_migrate_finish_args *args,
                                   remote_domain_migrate_finish_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

1766
    ddom = virDomainMigrateFinish (conn, args->dname,
D
Daniel P. Berrange 已提交
1767 1768 1769 1770
                                   args->cookie.cookie_val,
                                   args->cookie.cookie_len,
                                   args->uri,
                                   args->flags);
1771
    if (ddom == NULL) {
1772
        remoteDispatchConnError(rerr, conn);
1773 1774
        return -1;
    }
1775 1776

    make_nonnull_domain (&ret->ddom, ddom);
D
Daniel P. Berrange 已提交
1777
    virDomainFree (ddom);
1778 1779 1780
    return 0;
}

D
Daniel Veillard 已提交
1781 1782
static int
remoteDispatchDomainMigratePrepare2 (struct qemud_server *server ATTRIBUTE_UNUSED,
1783 1784
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1785
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
1786
                                     remote_error *rerr,
D
Daniel Veillard 已提交
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
                                     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) {
1803 1804
        remoteDispatchOOMError(rerr);
        return -1;
D
Daniel Veillard 已提交
1805 1806
    }

1807
    r = virDomainMigratePrepare2 (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1808 1809 1810
                                  uri_in, uri_out,
                                  args->flags, dname, args->resource,
                                  args->dom_xml);
1811
    if (r == -1) {
1812
        remoteDispatchConnError(rerr, conn);
1813 1814
        return -1;
    }
D
Daniel Veillard 已提交
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827

    /* 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,
1828 1829
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1830
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1831
                                    remote_error *rerr,
D
Daniel Veillard 已提交
1832 1833 1834 1835 1836 1837
                                    remote_domain_migrate_finish2_args *args,
                                    remote_domain_migrate_finish2_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

1838
    ddom = virDomainMigrateFinish2 (conn, args->dname,
D
Daniel P. Berrange 已提交
1839 1840 1841 1842 1843
                                    args->cookie.cookie_val,
                                    args->cookie.cookie_len,
                                    args->uri,
                                    args->flags,
                                    args->retcode);
1844
    if (ddom == NULL) {
1845
        remoteDispatchConnError(rerr, conn);
1846 1847
        return -1;
    }
D
Daniel Veillard 已提交
1848 1849 1850 1851 1852 1853

    make_nonnull_domain (&ret->ddom, ddom);

    return 0;
}

C
Chris Lalancette 已提交
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
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;
    }

1876
    r = virDomainMigratePrepareTunnel(conn, stream->st,
C
Chris Lalancette 已提交
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
                                      args->flags, dname, args->resource,
                                      args->dom_xml);
    if (r == -1) {
        remoteFreeClientStream(client, stream);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

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

    return 0;
}

1895
static int
1896
remoteDispatchListDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
1897 1898
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1899
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1900
                                  remote_error *rerr,
1901 1902 1903 1904 1905
                                  remote_list_defined_domains_args *args,
                                  remote_list_defined_domains_ret *ret)
{

    if (args->maxnames > REMOTE_DOMAIN_NAME_LIST_MAX) {
1906 1907 1908
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_DOMAIN_NAME_LIST_MAX"));
        return -1;
1909 1910 1911
    }

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

    ret->names.names_len =
1918
        virConnectListDefinedDomains (conn,
1919
                                      ret->names.names_val, args->maxnames);
1920 1921
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
1922
        remoteDispatchConnError(rerr, conn);
1923 1924
        return -1;
    }
1925 1926 1927 1928 1929

    return 0;
}

static int
1930
remoteDispatchDomainLookupById (struct qemud_server *server ATTRIBUTE_UNUSED,
1931 1932
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
1933
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
1934
                                remote_error *rerr,
1935 1936 1937 1938 1939
                                remote_domain_lookup_by_id_args *args,
                                remote_domain_lookup_by_id_ret *ret)
{
    virDomainPtr dom;

1940
    dom = virDomainLookupByID (conn, args->id);
1941
    if (dom == NULL) {
1942
        remoteDispatchConnError(rerr, conn);
1943 1944
        return -1;
    }
1945 1946

    make_nonnull_domain (&ret->dom, dom);
1947
    virDomainFree(dom);
1948 1949 1950 1951
    return 0;
}

static int
1952
remoteDispatchDomainLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
1953 1954
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1955
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1956
                                  remote_error *rerr,
1957 1958 1959 1960 1961
                                  remote_domain_lookup_by_name_args *args,
                                  remote_domain_lookup_by_name_ret *ret)
{
    virDomainPtr dom;

1962
    dom = virDomainLookupByName (conn, args->name);
1963
    if (dom == NULL) {
1964
        remoteDispatchConnError(rerr, conn);
1965 1966
        return -1;
    }
1967 1968

    make_nonnull_domain (&ret->dom, dom);
1969
    virDomainFree(dom);
1970 1971 1972 1973
    return 0;
}

static int
1974
remoteDispatchDomainLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
1975 1976
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1977
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1978
                                  remote_error *rerr,
1979 1980 1981 1982 1983
                                  remote_domain_lookup_by_uuid_args *args,
                                  remote_domain_lookup_by_uuid_ret *ret)
{
    virDomainPtr dom;

1984
    dom = virDomainLookupByUUID (conn, (unsigned char *) args->uuid);
1985
    if (dom == NULL) {
1986
        remoteDispatchConnError(rerr, conn);
1987 1988
        return -1;
    }
1989 1990

    make_nonnull_domain (&ret->dom, dom);
1991
    virDomainFree(dom);
1992 1993 1994 1995
    return 0;
}

static int
1996
remoteDispatchNumOfDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
1997 1998
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1999
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2000
                                   remote_error *rerr,
2001 2002 2003 2004
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_num_of_defined_domains_ret *ret)
{

2005
    ret->num = virConnectNumOfDefinedDomains (conn);
2006
    if (ret->num == -1) {
2007
        remoteDispatchConnError(rerr, conn);
2008 2009
        return -1;
    }
2010 2011 2012 2013 2014

    return 0;
}

static int
2015
remoteDispatchDomainPinVcpu (struct qemud_server *server ATTRIBUTE_UNUSED,
2016 2017
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2018
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2019
                             remote_error *rerr,
2020 2021 2022 2023 2024 2025
                             remote_domain_pin_vcpu_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int rv;

2026
    dom = get_nonnull_domain (conn, args->dom);
2027
    if (dom == NULL) {
2028
        remoteDispatchConnError(rerr, conn);
2029
        return -1;
2030 2031 2032
    }

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
2033
        virDomainFree(dom);
2034 2035
        remoteDispatchFormatError (rerr, "%s", _("cpumap_len > REMOTE_CPUMAP_MAX"));
        return -1;
2036 2037 2038 2039 2040
    }

    rv = virDomainPinVcpu (dom, args->vcpu,
                           (unsigned char *) args->cpumap.cpumap_val,
                           args->cpumap.cpumap_len);
2041 2042
    if (rv == -1) {
        virDomainFree(dom);
2043
        remoteDispatchConnError(rerr, conn);
2044 2045 2046
        return -1;
    }
    virDomainFree(dom);
2047 2048 2049 2050
    return 0;
}

static int
2051
remoteDispatchDomainReboot (struct qemud_server *server ATTRIBUTE_UNUSED,
2052 2053
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2054
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2055
                            remote_error *rerr,
2056 2057 2058 2059 2060
                            remote_domain_reboot_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2061
    dom = get_nonnull_domain (conn, args->dom);
2062
    if (dom == NULL) {
2063
        remoteDispatchConnError(rerr, conn);
2064
        return -1;
2065 2066
    }

2067 2068
    if (virDomainReboot (dom, args->flags) == -1) {
        virDomainFree(dom);
2069
        remoteDispatchConnError(rerr, conn);
2070
        return -1;
2071 2072
    }
    virDomainFree(dom);
2073 2074 2075 2076
    return 0;
}

static int
2077
remoteDispatchDomainRestore (struct qemud_server *server ATTRIBUTE_UNUSED,
2078 2079
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2080
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2081
                             remote_error *rerr,
2082 2083 2084 2085
                             remote_domain_restore_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{

2086 2087
    if (virDomainRestore (conn, args->from) == -1) {
        remoteDispatchConnError(rerr, conn);
2088
        return -1;
2089
    }
2090 2091 2092 2093 2094

    return 0;
}

static int
2095
remoteDispatchDomainResume (struct qemud_server *server ATTRIBUTE_UNUSED,
2096 2097
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2098
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2099
                            remote_error *rerr,
2100 2101 2102 2103 2104
                            remote_domain_resume_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2105
    dom = get_nonnull_domain (conn, args->dom);
2106
    if (dom == NULL) {
2107
        remoteDispatchConnError(rerr, conn);
2108
        return -1;
2109 2110
    }

2111 2112
    if (virDomainResume (dom) == -1) {
        virDomainFree(dom);
2113
        remoteDispatchConnError(rerr, conn);
2114
        return -1;
2115 2116
    }
    virDomainFree(dom);
2117 2118 2119 2120
    return 0;
}

static int
2121
remoteDispatchDomainSave (struct qemud_server *server ATTRIBUTE_UNUSED,
2122 2123
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
2124
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
2125
                          remote_error *rerr,
2126 2127 2128 2129 2130
                          remote_domain_save_args *args,
                          void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2131
    dom = get_nonnull_domain (conn, args->dom);
2132
    if (dom == NULL) {
2133
        remoteDispatchConnError(rerr, conn);
2134
        return -1;
2135 2136
    }

2137 2138
    if (virDomainSave (dom, args->to) == -1) {
        virDomainFree(dom);
2139
        remoteDispatchConnError(rerr, conn);
2140
        return -1;
2141 2142
    }
    virDomainFree(dom);
2143 2144 2145 2146
    return 0;
}

static int
2147
remoteDispatchDomainCoreDump (struct qemud_server *server ATTRIBUTE_UNUSED,
2148 2149
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2150
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2151
                              remote_error *rerr,
2152 2153 2154 2155 2156
                              remote_domain_core_dump_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2157
    dom = get_nonnull_domain (conn, args->dom);
2158
    if (dom == NULL) {
2159
        remoteDispatchConnError(rerr, conn);
2160
        return -1;
2161 2162
    }

2163 2164
    if (virDomainCoreDump (dom, args->to, args->flags) == -1) {
        virDomainFree(dom);
2165
        remoteDispatchConnError(rerr, conn);
2166
        return -1;
2167 2168
    }
    virDomainFree(dom);
2169 2170 2171 2172
    return 0;
}

static int
2173
remoteDispatchDomainSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
2174 2175
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2176
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2177
                                  remote_error *rerr,
2178 2179 2180 2181 2182
                                  remote_domain_set_autostart_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2183
    dom = get_nonnull_domain (conn, args->dom);
2184
    if (dom == NULL) {
2185
        remoteDispatchConnError(rerr, conn);
2186
        return -1;
2187 2188
    }

2189 2190
    if (virDomainSetAutostart (dom, args->autostart) == -1) {
        virDomainFree(dom);
2191
        remoteDispatchConnError(rerr, conn);
2192
        return -1;
2193 2194
    }
    virDomainFree(dom);
2195 2196 2197 2198
    return 0;
}

static int
2199
remoteDispatchDomainSetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
2200 2201
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2202
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
2203
                                  remote_error *rerr,
2204 2205 2206 2207 2208
                                  remote_domain_set_max_memory_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2209
    dom = get_nonnull_domain (conn, args->dom);
2210
    if (dom == NULL) {
2211
        remoteDispatchConnError(rerr, conn);
2212
        return -1;
2213 2214
    }

2215 2216
    if (virDomainSetMaxMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
2217
        remoteDispatchConnError(rerr, conn);
2218
        return -1;
2219 2220
    }
    virDomainFree(dom);
2221 2222 2223 2224
    return 0;
}

static int
2225
remoteDispatchDomainSetMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
2226 2227
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
2228
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
2229
                               remote_error *rerr,
2230 2231 2232 2233 2234
                               remote_domain_set_memory_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2235
    dom = get_nonnull_domain (conn, args->dom);
2236
    if (dom == NULL) {
2237
        remoteDispatchConnError(rerr, conn);
2238
        return -1;
2239 2240
    }

2241 2242
    if (virDomainSetMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
2243
        remoteDispatchConnError(rerr, conn);
2244
        return -1;
2245 2246
    }
    virDomainFree(dom);
2247 2248 2249 2250
    return 0;
}

static int
2251
remoteDispatchDomainSetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
2252 2253
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2254
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2255
                              remote_error *rerr,
2256 2257 2258 2259 2260
                              remote_domain_set_vcpus_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2261
    dom = get_nonnull_domain (conn, args->dom);
2262
    if (dom == NULL) {
2263
        remoteDispatchConnError(rerr, conn);
2264
        return -1;
2265 2266
    }

2267 2268
    if (virDomainSetVcpus (dom, args->nvcpus) == -1) {
        virDomainFree(dom);
2269
        remoteDispatchConnError(rerr, conn);
2270
        return -1;
2271 2272
    }
    virDomainFree(dom);
2273 2274 2275 2276
    return 0;
}

static int
2277
remoteDispatchDomainShutdown (struct qemud_server *server ATTRIBUTE_UNUSED,
2278 2279
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2280
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2281
                              remote_error *rerr,
2282 2283 2284 2285 2286
                              remote_domain_shutdown_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2287
    dom = get_nonnull_domain (conn, args->dom);
2288
    if (dom == NULL) {
2289
        remoteDispatchConnError(rerr, conn);
2290
        return -1;
2291 2292
    }

2293 2294
    if (virDomainShutdown (dom) == -1) {
        virDomainFree(dom);
2295
        remoteDispatchConnError(rerr, conn);
2296
        return -1;
2297 2298
    }
    virDomainFree(dom);
2299 2300 2301 2302
    return 0;
}

static int
2303
remoteDispatchDomainSuspend (struct qemud_server *server ATTRIBUTE_UNUSED,
2304 2305
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2306
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2307
                             remote_error *rerr,
2308 2309 2310 2311 2312
                             remote_domain_suspend_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2313
    dom = get_nonnull_domain (conn, args->dom);
2314
    if (dom == NULL) {
2315
        remoteDispatchConnError(rerr, conn);
2316
        return -1;
2317 2318
    }

2319 2320
    if (virDomainSuspend (dom) == -1) {
        virDomainFree(dom);
2321
        remoteDispatchConnError(rerr, conn);
2322
        return -1;
2323 2324
    }
    virDomainFree(dom);
2325 2326 2327 2328
    return 0;
}

static int
2329
remoteDispatchDomainUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
2330 2331
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2332
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2333
                              remote_error *rerr,
2334 2335 2336 2337 2338
                              remote_domain_undefine_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2339
    dom = get_nonnull_domain (conn, args->dom);
2340
    if (dom == NULL) {
2341
        remoteDispatchConnError(rerr, conn);
2342
        return -1;
2343 2344
    }

2345 2346
    if (virDomainUndefine (dom) == -1) {
        virDomainFree(dom);
2347
        remoteDispatchConnError(rerr, conn);
2348
        return -1;
2349 2350
    }
    virDomainFree(dom);
2351 2352 2353 2354
    return 0;
}

static int
2355
remoteDispatchListDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2356 2357
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2358
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2359
                                   remote_error *rerr,
2360 2361 2362 2363 2364
                                   remote_list_defined_networks_args *args,
                                   remote_list_defined_networks_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
2365 2366 2367
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
2368 2369 2370
    }

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

    ret->names.names_len =
2377
        virConnectListDefinedNetworks (conn,
2378
                                       ret->names.names_val, args->maxnames);
2379 2380
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
2381
        remoteDispatchConnError(rerr, conn);
2382 2383
        return -1;
    }
2384 2385 2386 2387 2388

    return 0;
}

static int
2389
remoteDispatchListDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2390 2391
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
2392
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
2393
                           remote_error *rerr,
2394 2395 2396 2397 2398
                           remote_list_domains_args *args,
                           remote_list_domains_ret *ret)
{

    if (args->maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
2399 2400 2401
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxids > REMOTE_DOMAIN_ID_LIST_MAX"));
        return -1;
2402 2403 2404
    }

    /* Allocate return buffer. */
2405
    if (VIR_ALLOC_N(ret->ids.ids_val, args->maxids) < 0) {
2406 2407
        remoteDispatchOOMError(rerr);
        return -1;
2408
    }
2409

2410
    ret->ids.ids_len = virConnectListDomains (conn,
2411
                                              ret->ids.ids_val, args->maxids);
2412 2413
    if (ret->ids.ids_len == -1) {
        VIR_FREE(ret->ids.ids_val);
2414
        remoteDispatchConnError(rerr, conn);
2415 2416
        return -1;
    }
2417 2418 2419 2420

    return 0;
}

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
static int
remoteDispatchDomainManagedSave (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_domain_managed_save_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

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

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

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

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

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

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

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

2500
static int
2501
remoteDispatchListNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2502 2503
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2504
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2505
                            remote_error *rerr,
2506 2507 2508 2509 2510
                            remote_list_networks_args *args,
                            remote_list_networks_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
2511 2512 2513
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
2514 2515 2516
    }

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

    ret->names.names_len =
2523
        virConnectListNetworks (conn,
2524
                                ret->names.names_val, args->maxnames);
2525 2526
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_len);
2527
        remoteDispatchConnError(rerr, conn);
2528 2529
        return -1;
    }
2530 2531 2532 2533 2534

    return 0;
}

static int
2535
remoteDispatchNetworkCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
2536 2537
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2538
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2539
                             remote_error *rerr,
2540 2541 2542 2543 2544
                             remote_network_create_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2545
    net = get_nonnull_network (conn, args->net);
2546
    if (net == NULL) {
2547
        remoteDispatchConnError(rerr, conn);
2548
        return -1;
2549 2550
    }

2551 2552
    if (virNetworkCreate (net) == -1) {
        virNetworkFree(net);
2553
        remoteDispatchConnError(rerr, conn);
2554
        return -1;
2555 2556
    }
    virNetworkFree(net);
2557 2558 2559 2560
    return 0;
}

static int
2561
remoteDispatchNetworkCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2562 2563
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2564
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
2565
                                remote_error *rerr,
2566 2567 2568 2569 2570
                                remote_network_create_xml_args *args,
                                remote_network_create_xml_ret *ret)
{
    virNetworkPtr net;

2571
    net = virNetworkCreateXML (conn, args->xml);
2572
    if (net == NULL) {
2573
        remoteDispatchConnError(rerr, conn);
2574 2575
        return -1;
    }
2576 2577

    make_nonnull_network (&ret->net, net);
2578
    virNetworkFree(net);
2579 2580 2581 2582
    return 0;
}

static int
2583
remoteDispatchNetworkDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2584 2585
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2586
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
2587
                                remote_error *rerr,
2588 2589 2590 2591 2592
                                remote_network_define_xml_args *args,
                                remote_network_define_xml_ret *ret)
{
    virNetworkPtr net;

2593
    net = virNetworkDefineXML (conn, args->xml);
2594
    if (net == NULL) {
2595
        remoteDispatchConnError(rerr, conn);
2596 2597
        return -1;
    }
2598 2599

    make_nonnull_network (&ret->net, net);
2600
    virNetworkFree(net);
2601 2602 2603 2604
    return 0;
}

static int
2605
remoteDispatchNetworkDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
2606 2607
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2608
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2609
                              remote_error *rerr,
2610 2611 2612 2613 2614
                              remote_network_destroy_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2615
    net = get_nonnull_network (conn, args->net);
2616
    if (net == NULL) {
2617
        remoteDispatchConnError(rerr, conn);
2618
        return -1;
2619 2620
    }

2621 2622
    if (virNetworkDestroy (net) == -1) {
        virNetworkFree(net);
2623
        remoteDispatchConnError(rerr, conn);
2624
        return -1;
2625 2626
    }
    virNetworkFree(net);
2627 2628 2629 2630
    return 0;
}

static int
2631
remoteDispatchNetworkDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2632 2633
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2634
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2635
                              remote_error *rerr,
2636 2637 2638 2639 2640
                              remote_network_dump_xml_args *args,
                              remote_network_dump_xml_ret *ret)
{
    virNetworkPtr net;

2641
    net = get_nonnull_network (conn, args->net);
2642
    if (net == NULL) {
2643
        remoteDispatchConnError(rerr, conn);
2644
        return -1;
2645 2646 2647 2648
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNetworkGetXMLDesc (net, args->flags);
2649 2650
    if (!ret->xml) {
        virNetworkFree(net);
2651
        remoteDispatchConnError(rerr, conn);
2652 2653 2654
        return -1;
    }
    virNetworkFree(net);
2655 2656 2657 2658
    return 0;
}

static int
2659
remoteDispatchNetworkGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
2660 2661
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2662
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2663
                                   remote_error *rerr,
2664 2665 2666 2667 2668
                                   remote_network_get_autostart_args *args,
                                   remote_network_get_autostart_ret *ret)
{
    virNetworkPtr net;

2669
    net = get_nonnull_network (conn, args->net);
2670
    if (net == NULL) {
2671
        remoteDispatchConnError(rerr, conn);
2672
        return -1;
2673 2674
    }

2675 2676
    if (virNetworkGetAutostart (net, &ret->autostart) == -1) {
        virNetworkFree(net);
2677
        remoteDispatchConnError(rerr, conn);
2678
        return -1;
2679 2680
    }
    virNetworkFree(net);
2681 2682 2683 2684
    return 0;
}

static int
2685
remoteDispatchNetworkGetBridgeName (struct qemud_server *server ATTRIBUTE_UNUSED,
2686 2687
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
2688
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
2689
                                    remote_error *rerr,
2690 2691 2692 2693 2694
                                    remote_network_get_bridge_name_args *args,
                                    remote_network_get_bridge_name_ret *ret)
{
    virNetworkPtr net;

2695
    net = get_nonnull_network (conn, args->net);
2696
    if (net == NULL) {
2697
        remoteDispatchConnError(rerr, conn);
2698
        return -1;
2699 2700 2701 2702
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virNetworkGetBridgeName (net);
2703 2704
    if (!ret->name) {
        virNetworkFree(net);
2705
        remoteDispatchConnError(rerr, conn);
2706 2707 2708
        return -1;
    }
    virNetworkFree(net);
2709 2710 2711 2712
    return 0;
}

static int
2713
remoteDispatchNetworkLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
2714 2715
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2716
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2717
                                   remote_error *rerr,
2718 2719 2720 2721 2722
                                   remote_network_lookup_by_name_args *args,
                                   remote_network_lookup_by_name_ret *ret)
{
    virNetworkPtr net;

2723
    net = virNetworkLookupByName (conn, args->name);
2724
    if (net == NULL) {
2725
        remoteDispatchConnError(rerr, conn);
2726 2727
        return -1;
    }
2728 2729

    make_nonnull_network (&ret->net, net);
2730
    virNetworkFree(net);
2731 2732 2733 2734
    return 0;
}

static int
2735
remoteDispatchNetworkLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
2736 2737
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2738
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2739
                                   remote_error *rerr,
2740 2741 2742 2743 2744
                                   remote_network_lookup_by_uuid_args *args,
                                   remote_network_lookup_by_uuid_ret *ret)
{
    virNetworkPtr net;

2745
    net = virNetworkLookupByUUID (conn, (unsigned char *) args->uuid);
2746
    if (net == NULL) {
2747
        remoteDispatchConnError(rerr, conn);
2748 2749
        return -1;
    }
2750 2751

    make_nonnull_network (&ret->net, net);
2752
    virNetworkFree(net);
2753 2754 2755 2756
    return 0;
}

static int
2757
remoteDispatchNetworkSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
2758 2759
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2760
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2761
                                   remote_error *rerr,
2762 2763 2764 2765 2766
                                   remote_network_set_autostart_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2767
    net = get_nonnull_network (conn, args->net);
2768
    if (net == NULL) {
2769
        remoteDispatchConnError(rerr, conn);
2770
        return -1;
2771 2772
    }

2773 2774
    if (virNetworkSetAutostart (net, args->autostart) == -1) {
        virNetworkFree(net);
2775
        remoteDispatchConnError(rerr, conn);
2776
        return -1;
2777 2778
    }
    virNetworkFree(net);
2779 2780 2781 2782
    return 0;
}

static int
2783
remoteDispatchNetworkUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
2784 2785
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
2786
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
2787
                               remote_error *rerr,
2788 2789 2790 2791 2792
                               remote_network_undefine_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2793
    net = get_nonnull_network (conn, args->net);
2794
    if (net == NULL) {
2795
        remoteDispatchConnError(rerr, conn);
2796
        return -1;
2797 2798
    }

2799 2800
    if (virNetworkUndefine (net) == -1) {
        virNetworkFree(net);
2801
        remoteDispatchConnError(rerr, conn);
2802
        return -1;
2803 2804
    }
    virNetworkFree(net);
2805 2806 2807 2808
    return 0;
}

static int
2809
remoteDispatchNumOfDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2810 2811
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
2812
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
2813
                                    remote_error *rerr,
2814 2815 2816 2817
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_num_of_defined_networks_ret *ret)
{

2818
    ret->num = virConnectNumOfDefinedNetworks (conn);
2819
    if (ret->num == -1) {
2820
        remoteDispatchConnError(rerr, conn);
2821 2822
        return -1;
    }
2823 2824 2825 2826 2827

    return 0;
}

static int
2828
remoteDispatchNumOfDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2829 2830
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2831
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2832
                            remote_error *rerr,
2833 2834 2835 2836
                            void *args ATTRIBUTE_UNUSED,
                            remote_num_of_domains_ret *ret)
{

2837
    ret->num = virConnectNumOfDomains (conn);
2838
    if (ret->num == -1) {
2839
        remoteDispatchConnError(rerr, conn);
2840 2841
        return -1;
    }
2842 2843 2844 2845 2846

    return 0;
}

static int
2847
remoteDispatchNumOfNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2848 2849
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2850
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2851
                             remote_error *rerr,
2852 2853 2854 2855
                             void *args ATTRIBUTE_UNUSED,
                             remote_num_of_networks_ret *ret)
{

2856
    ret->num = virConnectNumOfNetworks (conn);
2857
    if (ret->num == -1) {
2858
        remoteDispatchConnError(rerr, conn);
2859 2860
        return -1;
    }
2861 2862 2863 2864

    return 0;
}

2865

D
Daniel Veillard 已提交
2866 2867 2868 2869 2870
/*-------------------------------------------------------------*/
static int
remoteDispatchNumOfInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
2871
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
                               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,
2890
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
                              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;
}

2920 2921 2922 2923
static int
remoteDispatchNumOfDefinedInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
2924
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942
                                      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,
2943
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
                                     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 已提交
2973 2974 2975 2976
static int
remoteDispatchInterfaceLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
2977
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
2978 2979 2980 2981
                                     remote_error *rerr,
                                     remote_interface_lookup_by_name_args *args,
                                     remote_interface_lookup_by_name_ret *ret)
{
2982
    virInterfacePtr iface;
D
Daniel Veillard 已提交
2983

2984 2985
    iface = virInterfaceLookupByName (conn, args->name);
    if (iface == NULL) {
D
Daniel Veillard 已提交
2986 2987 2988 2989
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

2990 2991
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
2992 2993 2994 2995 2996 2997 2998
    return 0;
}

static int
remoteDispatchInterfaceLookupByMacString (struct qemud_server *server ATTRIBUTE_UNUSED,
                                          struct qemud_client *client ATTRIBUTE_UNUSED,
                                          virConnectPtr conn,
2999
                                          remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3000 3001 3002 3003
                                          remote_error *rerr,
                                          remote_interface_lookup_by_mac_string_args *args,
                                          remote_interface_lookup_by_mac_string_ret *ret)
{
3004
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3005

3006 3007
    iface = virInterfaceLookupByMACString (conn, args->mac);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3008 3009 3010 3011
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3012 3013
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3014 3015 3016 3017 3018 3019 3020
    return 0;
}

static int
remoteDispatchInterfaceGetXmlDesc (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
3021
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3022 3023 3024 3025
                                   remote_error *rerr,
                                   remote_interface_get_xml_desc_args *args,
                                   remote_interface_get_xml_desc_ret *ret)
{
3026
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3027

3028 3029
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3030 3031 3032 3033 3034
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    /* remoteDispatchClientRequest will free this. */
3035
    ret->xml = virInterfaceGetXMLDesc (iface, args->flags);
D
Daniel Veillard 已提交
3036
    if (!ret->xml) {
3037
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3038 3039 3040
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3041
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3042 3043 3044 3045 3046 3047 3048
    return 0;
}

static int
remoteDispatchInterfaceDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3049
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
3050 3051 3052 3053
                                  remote_error *rerr,
                                  remote_interface_define_xml_args *args,
                                  remote_interface_define_xml_ret *ret)
{
3054
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3055

3056 3057
    iface = virInterfaceDefineXML (conn, args->xml, args->flags);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3058 3059 3060 3061
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3062 3063
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3064 3065 3066 3067 3068
    return 0;
}

static int
remoteDispatchInterfaceUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
3069 3070 3071 3072 3073 3074
                                 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 已提交
3075
{
3076
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3077

3078 3079
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3080 3081 3082 3083
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3084 3085
    if (virInterfaceUndefine (iface) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3086 3087 3088
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3089
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3090 3091 3092 3093 3094
    return 0;
}

static int
remoteDispatchInterfaceCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
3095 3096 3097 3098 3099 3100
                               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 已提交
3101
{
3102
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3103

3104 3105
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3106 3107 3108 3109
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3110 3111
    if (virInterfaceCreate (iface, args->flags) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3112 3113 3114
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3115
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3116 3117 3118 3119 3120
    return 0;
}

static int
remoteDispatchInterfaceDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
3121 3122 3123 3124 3125 3126
                                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 已提交
3127
{
3128
    virInterfacePtr iface;
D
Daniel Veillard 已提交
3129

3130 3131
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
3132 3133 3134 3135
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

3136 3137
    if (virInterfaceDestroy (iface, args->flags) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
3138 3139 3140
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
3141
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
3142 3143 3144 3145 3146
    return 0;
}

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

3147
static int
3148
remoteDispatchAuthList (struct qemud_server *server,
3149
                        struct qemud_client *client,
3150
                        virConnectPtr conn ATTRIBUTE_UNUSED,
3151
                        remote_message_header *hdr ATTRIBUTE_UNUSED,
3152
                        remote_error *rerr,
3153 3154 3155 3156
                        void *args ATTRIBUTE_UNUSED,
                        remote_auth_list_ret *ret)
{
    ret->types.types_len = 1;
3157
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
3158 3159
        remoteDispatchOOMError(rerr);
        return -1;
3160
    }
3161 3162 3163
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3164
    ret->types.types_val[0] = client->auth;
3165
    virMutexUnlock(&client->lock);
3166

3167 3168 3169 3170 3171 3172
    return 0;
}


#if HAVE_SASL
/*
3173
 * NB, keep in sync with similar method in src/remote/remote_driver.c
3174
 */
3175
static char *addrToString(remote_error *rerr,
3176 3177
                          struct sockaddr_storage *ss, socklen_t salen) {
    char host[NI_MAXHOST], port[NI_MAXSERV];
3178 3179
    char *addr;
    int err;
3180
    struct sockaddr *sa = (struct sockaddr *)ss;
3181

3182
    if ((err = getnameinfo(sa, salen,
3183 3184 3185
                           host, sizeof(host),
                           port, sizeof(port),
                           NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202
        char ip[INET6_ADDRSTRLEN];
        void *rawaddr;

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

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

3206
    if (virAsprintf(&addr, "%s;%s", host, port) == -1) {
3207
        virReportOOMError();
3208 3209 3210 3211 3212 3213 3214 3215 3216
        return NULL;
    }

    return addr;
}


/*
 * Initializes the SASL session in prepare for authentication
3217
 * and gives the client a list of allowed mechanisms to choose
3218 3219 3220 3221
 *
 * XXX callbacks for stuff like password verification ?
 */
static int
3222
remoteDispatchAuthSaslInit (struct qemud_server *server,
3223
                            struct qemud_client *client,
3224
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3225
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3226
                            remote_error *rerr,
3227 3228 3229 3230
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret)
{
    const char *mechlist = NULL;
3231
    sasl_security_properties_t secprops;
3232 3233 3234 3235 3236
    int err;
    struct sockaddr_storage sa;
    socklen_t salen;
    char *localAddr, *remoteAddr;

3237 3238 3239
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3240

3241 3242 3243
    REMOTE_DEBUG("Initialize SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn != NULL) {
3244
        VIR_ERROR0(_("client tried invalid SASL init request"));
3245
        goto authfail;
3246 3247 3248 3249 3250
    }

    /* Get local address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getsockname(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
3251
        char ebuf[1024];
3252
        remoteDispatchFormatError(rerr,
3253
                                  _("failed to get sock address: %s"),
3254
                                  virStrerror(errno, ebuf, sizeof ebuf));
3255
        goto error;
3256
    }
3257
    if ((localAddr = addrToString(rerr, &sa, salen)) == NULL) {
3258
        goto error;
3259 3260 3261 3262 3263
    }

    /* Get remote address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getpeername(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
3264
        char ebuf[1024];
3265
        remoteDispatchFormatError(rerr, _("failed to get peer address: %s"),
3266
                                  virStrerror(errno, ebuf, sizeof ebuf));
3267
        VIR_FREE(localAddr);
3268
        goto error;
3269
    }
3270
    if ((remoteAddr = addrToString(rerr, &sa, salen)) == NULL) {
3271
        VIR_FREE(localAddr);
3272
        goto error;
3273 3274 3275 3276 3277 3278 3279 3280 3281 3282
    }

    err = sasl_server_new("libvirt",
                          NULL, /* FQDN - just delegates to gethostname */
                          NULL, /* User realm */
                          localAddr,
                          remoteAddr,
                          NULL, /* XXX Callbacks */
                          SASL_SUCCESS_DATA,
                          &client->saslconn);
3283 3284
    VIR_FREE(localAddr);
    VIR_FREE(remoteAddr);
3285
    if (err != SASL_OK) {
3286 3287
        VIR_ERROR(_("sasl context setup failed %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3288
        client->saslconn = NULL;
3289
        goto authfail;
3290 3291
    }

3292 3293 3294 3295 3296 3297 3298
    /* 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 已提交
3299
            VIR_ERROR0(_("cannot get TLS cipher size"));
3300 3301
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
3302
            goto authfail;
3303 3304 3305 3306 3307
        }
        ssf *= 8; /* tls key size is bytes, sasl wants bits */

        err = sasl_setprop(client->saslconn, SASL_SSF_EXTERNAL, &ssf);
        if (err != SASL_OK) {
3308 3309
            VIR_ERROR(_("cannot set SASL external SSF %d (%s)"),
                      err, sasl_errstring(err, NULL, NULL));
3310 3311
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
3312
            goto authfail;
3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335
        }
    }

    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) {
3336 3337
        VIR_ERROR(_("cannot set SASL security props %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3338 3339
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3340
        goto authfail;
3341 3342
    }

3343 3344 3345 3346 3347 3348 3349 3350 3351
    err = sasl_listmech(client->saslconn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
3352 3353
        VIR_ERROR(_("cannot list SASL mechanisms %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3354 3355
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3356
        goto authfail;
3357 3358 3359 3360
    }
    REMOTE_DEBUG("Available mechanisms for client: '%s'", mechlist);
    ret->mechlist = strdup(mechlist);
    if (!ret->mechlist) {
3361
        VIR_ERROR0(_("cannot allocate mechlist"));
3362 3363
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3364
        goto authfail;
3365 3366
    }

3367
    virMutexUnlock(&client->lock);
3368
    return 0;
3369 3370 3371 3372

authfail:
    remoteDispatchAuthError(rerr);
error:
3373
    virMutexUnlock(&client->lock);
3374
    return -1;
3375 3376 3377
}


3378
/* We asked for an SSF layer, so sanity check that we actually
3379 3380 3381
 * got what we asked for */
static int
remoteSASLCheckSSF (struct qemud_client *client,
3382
                    remote_error *rerr) {
3383 3384 3385 3386 3387 3388 3389 3390 3391
    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) {
3392 3393
        VIR_ERROR(_("cannot query SASL ssf on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3394
        remoteDispatchAuthError(rerr);
3395 3396 3397 3398 3399 3400 3401
        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 */
3402
        VIR_ERROR(_("negotiated SSF %d was not strong enough"), ssf);
3403
        remoteDispatchAuthError(rerr);
3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }

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

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

3421 3422 3423
static int
remoteSASLCheckAccess (struct qemud_server *server,
                       struct qemud_client *client,
3424
                       remote_error *rerr) {
3425 3426 3427 3428 3429 3430
    const void *val;
    int err;
    char **wildcards;

    err = sasl_getprop(client->saslconn, SASL_USERNAME, &val);
    if (err != SASL_OK) {
3431 3432
        VIR_ERROR(_("cannot query SASL username on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3433
        remoteDispatchAuthError(rerr);
3434 3435 3436 3437 3438
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    if (val == NULL) {
3439
        VIR_ERROR0(_("no client username was found"));
3440
        remoteDispatchAuthError(rerr);
3441 3442 3443 3444 3445 3446 3447 3448
        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) {
3449
        VIR_ERROR0(_("out of memory copying username"));
3450
        remoteDispatchAuthError(rerr);
3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467
        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 */
3468
    VIR_ERROR(_("SASL client %s not allowed in whitelist"), client->saslUsername);
3469
    remoteDispatchAuthError(rerr);
3470 3471 3472 3473 3474 3475
    sasl_dispose(&client->saslconn);
    client->saslconn = NULL;
    return -1;
}


3476 3477 3478 3479
/*
 * This starts the SASL authentication negotiation.
 */
static int
3480 3481
remoteDispatchAuthSaslStart (struct qemud_server *server,
                             struct qemud_client *client,
3482
                             virConnectPtr conn ATTRIBUTE_UNUSED,
3483
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
3484
                             remote_error *rerr,
3485 3486 3487 3488 3489 3490 3491
                             remote_auth_sasl_start_args *args,
                             remote_auth_sasl_start_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

3492 3493 3494
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3495

3496 3497 3498
    REMOTE_DEBUG("Start SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
3499
        VIR_ERROR0(_("client tried invalid SASL start request"));
3500
        goto authfail;
3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513
    }

    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) {
3514 3515
        VIR_ERROR(_("sasl start failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3516 3517
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3518
        goto authfail;
3519 3520
    }
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3521
        VIR_ERROR(_("sasl start reply data too long %d"), serveroutlen);
3522 3523
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3524
        goto authfail;
3525 3526 3527 3528
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3529
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
3530
            remoteDispatchOOMError(rerr);
3531
            goto error;
3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543
        }
        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 {
3544
        if (remoteSASLCheckSSF(client, rerr) < 0)
3545
            goto error;
3546

3547
        /* Check username whitelist ACL */
3548
        if (remoteSASLCheckAccess(server, client, rerr) < 0)
3549
            goto error;
3550

3551 3552 3553 3554 3555
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

3556
    virMutexUnlock(&client->lock);
3557
    return 0;
3558 3559 3560 3561

authfail:
    remoteDispatchAuthError(rerr);
error:
3562
    virMutexUnlock(&client->lock);
3563
    return -1;
3564 3565 3566 3567
}


static int
3568 3569
remoteDispatchAuthSaslStep (struct qemud_server *server,
                            struct qemud_client *client,
3570
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3571
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3572
                            remote_error *rerr,
3573 3574 3575 3576 3577 3578 3579
                            remote_auth_sasl_step_args *args,
                            remote_auth_sasl_step_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

3580 3581 3582
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3583

3584 3585 3586
    REMOTE_DEBUG("Step SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
3587
        VIR_ERROR0(_("client tried invalid SASL start request"));
3588
        goto authfail;
3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600
    }

    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) {
3601 3602
        VIR_ERROR(_("sasl step failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3603 3604
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3605
        goto authfail;
3606 3607 3608
    }

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3609 3610
        VIR_ERROR(_("sasl step reply data too long %d"),
                  serveroutlen);
3611 3612
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3613
        goto authfail;
3614 3615 3616 3617
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3618
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
3619
            remoteDispatchOOMError(rerr);
3620
            goto error;
3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632
        }
        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 {
3633
        if (remoteSASLCheckSSF(client, rerr) < 0)
3634
            goto error;
3635

3636
        /* Check username whitelist ACL */
3637
        if (remoteSASLCheckAccess(server, client, rerr) < 0)
3638
            goto error;
3639

3640 3641 3642 3643 3644
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

3645
    virMutexUnlock(&client->lock);
3646
    return 0;
3647 3648 3649 3650

authfail:
    remoteDispatchAuthError(rerr);
error:
3651
    virMutexUnlock(&client->lock);
3652
    return -1;
3653 3654 3655 3656 3657
}


#else /* HAVE_SASL */
static int
3658
remoteDispatchAuthSaslInit (struct qemud_server *server ATTRIBUTE_UNUSED,
3659 3660
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3661
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3662
                            remote_error *rerr,
3663 3664 3665
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
3666
    VIR_ERROR0(_("client tried unsupported SASL init request"));
3667
    remoteDispatchAuthError(rerr);
3668 3669 3670 3671
    return -1;
}

static int
3672
remoteDispatchAuthSaslStart (struct qemud_server *server ATTRIBUTE_UNUSED,
3673 3674
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn ATTRIBUTE_UNUSED,
3675
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
3676
                             remote_error *rerr,
3677 3678 3679
                             remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                             remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
3680
    VIR_ERROR0(_("client tried unsupported SASL start request"));
3681
    remoteDispatchAuthError(rerr);
3682 3683 3684 3685
    return -1;
}

static int
3686
remoteDispatchAuthSaslStep (struct qemud_server *server ATTRIBUTE_UNUSED,
3687 3688
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3689
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3690
                            remote_error *rerr,
3691 3692 3693
                            remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
3694
    VIR_ERROR0(_("client tried unsupported SASL step request"));
3695
    remoteDispatchAuthError(rerr);
3696 3697 3698 3699 3700
    return -1;
}
#endif /* HAVE_SASL */


3701 3702 3703 3704 3705
#if HAVE_POLKIT1
static int
remoteDispatchAuthPolkit (struct qemud_server *server,
                          struct qemud_client *client,
                          virConnectPtr conn ATTRIBUTE_UNUSED,
3706
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749
                          remote_error *rerr,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
    pid_t callerPid;
    uid_t callerUid;
    const char *action;
    int status = -1;
    char pidbuf[50];
    int rv;

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

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

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

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

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

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

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

3753
    if (virRun(pkcheck, &status) < 0) {
3754
        VIR_ERROR(_("Cannot invoke %s"), PKCHECK_PATH);
3755
        goto authfail;
3756 3757
    }
    if (status != 0) {
3758
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d, result: %d"),
3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775
                  action, callerPid, callerUid, status);
        goto authfail;
    }
    VIR_INFO(_("Policy allowed action %s from pid %d, uid %d"),
             action, callerPid, callerUid);
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;

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

authfail:
    remoteDispatchAuthError(rerr);
    virMutexUnlock(&client->lock);
    return -1;
}
#elif HAVE_POLKIT0
3776
static int
3777
remoteDispatchAuthPolkit (struct qemud_server *server,
3778
                          struct qemud_client *client,
3779
                          virConnectPtr conn ATTRIBUTE_UNUSED,
3780
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
3781
                          remote_error *rerr,
3782 3783 3784 3785 3786
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
    pid_t callerPid;
    uid_t callerUid;
3787 3788 3789 3790 3791 3792
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
3793 3794
    const char *action;

3795 3796 3797
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3798 3799

    action = client->readonly ?
3800 3801
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
3802 3803 3804

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

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

3814
    VIR_INFO(_("Checking PID %d running as %d"), callerPid, callerUid);
3815 3816 3817
    dbus_error_init(&err);
    if (!(pkcaller = polkit_caller_new_from_pid(server->sysbus,
                                                callerPid, &err))) {
3818
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
3819
        dbus_error_free(&err);
3820
        goto authfail;
3821
    }
3822

3823
    if (!(pkaction = polkit_action_new())) {
3824
        char ebuf[1024];
3825
        VIR_ERROR(_("Failed to create polkit action %s"),
3826
                  virStrerror(errno, ebuf, sizeof ebuf));
3827
        polkit_caller_unref(pkcaller);
3828
        goto authfail;
3829 3830 3831 3832 3833
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
3834
        char ebuf[1024];
3835
        VIR_ERROR(_("Failed to create polkit context %s"),
3836
                  (pkerr ? polkit_error_get_error_message(pkerr)
3837
                   : virStrerror(errno, ebuf, sizeof ebuf)));
3838 3839 3840 3841 3842
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
3843
        goto authfail;
3844
    }
3845

3846
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
3847 3848 3849 3850 3851 3852
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
3853 3854 3855
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
3856
        goto authfail;
3857
    }
3858
# else
3859 3860 3861
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
3862
# endif
3863 3864 3865 3866
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
3867
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d, result: %s"),
3868 3869
                  action, callerPid, callerUid,
                  polkit_result_to_string_representation(pkresult));
3870
        goto authfail;
3871
    }
3872
    VIR_INFO(_("Policy allowed action %s from pid %d, uid %d, result %s"),
3873 3874 3875 3876
             action, callerPid, callerUid,
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;
3877

3878
    virMutexUnlock(&client->lock);
3879
    return 0;
3880 3881 3882

authfail:
    remoteDispatchAuthError(rerr);
3883
    virMutexUnlock(&client->lock);
3884
    return -1;
3885 3886
}

3887
#else /* !HAVE_POLKIT0 & !HAVE_POLKIT1*/
3888 3889

static int
3890
remoteDispatchAuthPolkit (struct qemud_server *server ATTRIBUTE_UNUSED,
3891
                          struct qemud_client *client ATTRIBUTE_UNUSED,
3892
                          virConnectPtr conn ATTRIBUTE_UNUSED,
3893
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
3894 3895 3896
                          remote_error *rerr,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
3897
{
3898
    VIR_ERROR0(_("client tried unsupported PolicyKit init request"));
3899
    remoteDispatchAuthError(rerr);
3900 3901
    return -1;
}
3902
#endif /* HAVE_POLKIT1 */
3903

3904 3905 3906 3907 3908 3909 3910 3911

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


static int
remoteDispatchListDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
3912 3913
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3914
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
3915
                                       remote_error *rerr,
3916 3917 3918 3919 3920
                                       remote_list_defined_storage_pools_args *args,
                                       remote_list_defined_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
3921 3922
        remoteDispatchFormatError (rerr, "%s",
                            _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
3923
        return -1;
3924 3925 3926
    }

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

    ret->names.names_len =
3933
        virConnectListDefinedStoragePools (conn,
3934 3935 3936
                                           ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
3937
        remoteDispatchConnError(rerr, conn);
3938 3939
        return -1;
    }
3940 3941 3942 3943 3944 3945

    return 0;
}

static int
remoteDispatchListStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
3946 3947
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
3948
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
3949
                                remote_error *rerr,
3950 3951 3952 3953 3954
                                remote_list_storage_pools_args *args,
                                remote_list_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
3955 3956 3957
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX"));
        return -1;
3958 3959 3960
    }

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

    ret->names.names_len =
3967
        virConnectListStoragePools (conn,
3968
                                ret->names.names_val, args->maxnames);
3969 3970
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
3971
        remoteDispatchConnError(rerr, conn);
3972 3973
        return -1;
    }
3974 3975 3976 3977

    return 0;
}

3978 3979
static int
remoteDispatchFindStoragePoolSources (struct qemud_server *server ATTRIBUTE_UNUSED,
3980 3981
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
3982
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
3983
                                      remote_error *rerr,
3984 3985 3986 3987
                                      remote_find_storage_pool_sources_args *args,
                                      remote_find_storage_pool_sources_ret *ret)
{
    ret->xml =
3988
        virConnectFindStoragePoolSources (conn,
3989 3990 3991
                                          args->type,
                                          args->srcSpec ? *args->srcSpec : NULL,
                                          args->flags);
3992
    if (ret->xml == NULL) {
3993
        remoteDispatchConnError(rerr, conn);
3994
        return -1;
3995
    }
3996 3997 3998 3999 4000

    return 0;
}


4001 4002
static int
remoteDispatchStoragePoolCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
4003 4004
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4005
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4006
                                 remote_error *rerr,
4007 4008 4009 4010 4011
                                 remote_storage_pool_create_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4012
    pool = get_nonnull_storage_pool (conn, args->pool);
4013
    if (pool == NULL) {
4014
        remoteDispatchConnError(rerr, conn);
4015
        return -1;
4016 4017 4018 4019
    }

    if (virStoragePoolCreate (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4020
        remoteDispatchConnError(rerr, conn);
4021 4022 4023 4024 4025 4026 4027 4028
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4029 4030
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
4031
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
4032
                                    remote_error *rerr,
4033 4034 4035 4036 4037
                                    remote_storage_pool_create_xml_args *args,
                                    remote_storage_pool_create_xml_ret *ret)
{
    virStoragePoolPtr pool;

4038
    pool = virStoragePoolCreateXML (conn, args->xml, args->flags);
4039
    if (pool == NULL) {
4040
        remoteDispatchConnError(rerr, conn);
4041 4042
        return -1;
    }
4043 4044 4045 4046 4047 4048 4049 4050

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

static int
remoteDispatchStoragePoolDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4051 4052
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
4053
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
4054
                                    remote_error *rerr,
4055 4056 4057 4058 4059
                                    remote_storage_pool_define_xml_args *args,
                                    remote_storage_pool_define_xml_ret *ret)
{
    virStoragePoolPtr pool;

4060
    pool = virStoragePoolDefineXML (conn, args->xml, args->flags);
4061
    if (pool == NULL) {
4062
        remoteDispatchConnError(rerr, conn);
4063 4064
        return -1;
    }
4065 4066 4067 4068 4069 4070 4071 4072

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

static int
remoteDispatchStoragePoolBuild (struct qemud_server *server ATTRIBUTE_UNUSED,
4073 4074
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4075
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4076 4077 4078
                                remote_error *rerr,
                                remote_storage_pool_build_args *args,
                                void *ret ATTRIBUTE_UNUSED)
4079 4080 4081
{
    virStoragePoolPtr pool;

4082
    pool = get_nonnull_storage_pool (conn, args->pool);
4083
    if (pool == NULL) {
4084
        remoteDispatchConnError(rerr, conn);
4085
        return -1;
4086 4087 4088 4089
    }

    if (virStoragePoolBuild (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4090
        remoteDispatchConnError(rerr, conn);
4091 4092 4093 4094 4095 4096 4097 4098 4099
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
4100 4101
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4102
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4103
                                  remote_error *rerr,
4104 4105 4106 4107 4108
                                  remote_storage_pool_destroy_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4109
    pool = get_nonnull_storage_pool (conn, args->pool);
4110
    if (pool == NULL) {
4111
        remoteDispatchConnError(rerr, conn);
4112
        return -1;
4113 4114 4115 4116
    }

    if (virStoragePoolDestroy (pool) == -1) {
        virStoragePoolFree(pool);
4117
        remoteDispatchConnError(rerr, conn);
4118 4119 4120 4121 4122 4123 4124 4125
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
4126 4127
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4128
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4129
                                 remote_error *rerr,
4130 4131 4132 4133 4134
                                 remote_storage_pool_delete_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4135
    pool = get_nonnull_storage_pool (conn, args->pool);
4136
    if (pool == NULL) {
4137
        remoteDispatchConnError(rerr, conn);
4138
        return -1;
4139 4140 4141 4142
    }

    if (virStoragePoolDelete (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4143
        remoteDispatchConnError(rerr, conn);
4144 4145 4146 4147 4148 4149 4150 4151
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolRefresh (struct qemud_server *server ATTRIBUTE_UNUSED,
4152 4153
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4154
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4155
                                  remote_error *rerr,
4156 4157 4158 4159 4160
                                  remote_storage_pool_refresh_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4161
    pool = get_nonnull_storage_pool (conn, args->pool);
4162
    if (pool == NULL) {
4163
        remoteDispatchConnError(rerr, conn);
4164
        return -1;
4165 4166 4167 4168
    }

    if (virStoragePoolRefresh (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
4169
        remoteDispatchConnError(rerr, conn);
4170 4171 4172 4173 4174 4175 4176 4177
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
4178 4179
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4180
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4181
                                  remote_error *rerr,
4182 4183 4184 4185 4186 4187
                                  remote_storage_pool_get_info_args *args,
                                  remote_storage_pool_get_info_ret *ret)
{
    virStoragePoolPtr pool;
    virStoragePoolInfo info;

4188
    pool = get_nonnull_storage_pool (conn, args->pool);
4189
    if (pool == NULL) {
4190
        remoteDispatchConnError(rerr, conn);
4191
        return -1;
4192 4193 4194 4195
    }

    if (virStoragePoolGetInfo (pool, &info) == -1) {
        virStoragePoolFree(pool);
4196
        remoteDispatchConnError(rerr, conn);
4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211
        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,
4212 4213
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4214
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4215
                                  remote_error *rerr,
4216 4217 4218 4219 4220
                                  remote_storage_pool_dump_xml_args *args,
                                  remote_storage_pool_dump_xml_ret *ret)
{
    virStoragePoolPtr pool;

4221
    pool = get_nonnull_storage_pool (conn, args->pool);
4222
    if (pool == NULL) {
4223
        remoteDispatchConnError(rerr, conn);
4224
        return -1;
4225 4226 4227 4228 4229 4230
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStoragePoolGetXMLDesc (pool, args->flags);
    if (!ret->xml) {
        virStoragePoolFree(pool);
4231
        remoteDispatchConnError(rerr, conn);
4232 4233 4234 4235 4236 4237 4238 4239
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
4240 4241
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4242
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4243
                                       remote_error *rerr,
4244 4245 4246 4247 4248
                                       remote_storage_pool_get_autostart_args *args,
                                       remote_storage_pool_get_autostart_ret *ret)
{
    virStoragePoolPtr pool;

4249
    pool = get_nonnull_storage_pool (conn, args->pool);
4250
    if (pool == NULL) {
4251
        remoteDispatchConnError(rerr, conn);
4252
        return -1;
4253 4254 4255 4256
    }

    if (virStoragePoolGetAutostart (pool, &ret->autostart) == -1) {
        virStoragePoolFree(pool);
4257
        remoteDispatchConnError(rerr, conn);
4258 4259 4260 4261 4262 4263 4264 4265 4266
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
4267 4268
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4269
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4270
                                       remote_error *rerr,
4271 4272 4273 4274 4275
                                       remote_storage_pool_lookup_by_name_args *args,
                                       remote_storage_pool_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;

4276
    pool = virStoragePoolLookupByName (conn, args->name);
4277
    if (pool == NULL) {
4278
        remoteDispatchConnError(rerr, conn);
4279 4280
        return -1;
    }
4281 4282 4283 4284 4285 4286 4287 4288

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

static int
remoteDispatchStoragePoolLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
4289 4290
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4291
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4292
                                       remote_error *rerr,
4293 4294 4295 4296 4297
                                       remote_storage_pool_lookup_by_uuid_args *args,
                                       remote_storage_pool_lookup_by_uuid_ret *ret)
{
    virStoragePoolPtr pool;

4298
    pool = virStoragePoolLookupByUUID (conn, (unsigned char *) args->uuid);
4299
    if (pool == NULL) {
4300
        remoteDispatchConnError(rerr, conn);
4301 4302
        return -1;
    }
4303 4304 4305 4306 4307 4308 4309 4310

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

static int
remoteDispatchStoragePoolLookupByVolume (struct qemud_server *server ATTRIBUTE_UNUSED,
4311 4312
                                         struct qemud_client *client ATTRIBUTE_UNUSED,
                                         virConnectPtr conn,
4313
                                         remote_message_header *hdr ATTRIBUTE_UNUSED,
4314
                                         remote_error *rerr,
4315 4316 4317 4318 4319 4320
                                         remote_storage_pool_lookup_by_volume_args *args,
                                         remote_storage_pool_lookup_by_volume_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

4321
    vol = get_nonnull_storage_vol (conn, args->vol);
4322
    if (vol == NULL) {
4323
        remoteDispatchConnError(rerr, conn);
4324 4325
        return -1;
    }
4326 4327 4328

    pool = virStoragePoolLookupByVolume (vol);
    virStorageVolFree(vol);
4329
    if (pool == NULL) {
4330
        remoteDispatchConnError(rerr, conn);
4331 4332
        return -1;
    }
4333 4334 4335 4336 4337 4338 4339 4340

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

static int
remoteDispatchStoragePoolSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
4341 4342
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4343
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4344
                                       remote_error *rerr,
4345 4346 4347 4348 4349
                                       remote_storage_pool_set_autostart_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4350
    pool = get_nonnull_storage_pool (conn, args->pool);
4351
    if (pool == NULL) {
4352
        remoteDispatchConnError(rerr, conn);
4353
        return -1;
4354 4355 4356 4357
    }

    if (virStoragePoolSetAutostart (pool, args->autostart) == -1) {
        virStoragePoolFree(pool);
4358
        remoteDispatchConnError(rerr, conn);
4359 4360 4361 4362 4363 4364 4365 4366
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
4367 4368
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4369
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4370
                                   remote_error *rerr,
4371 4372 4373 4374 4375
                                   remote_storage_pool_undefine_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4376
    pool = get_nonnull_storage_pool (conn, args->pool);
4377
    if (pool == NULL) {
4378
        remoteDispatchConnError(rerr, conn);
4379
        return -1;
4380 4381 4382 4383
    }

    if (virStoragePoolUndefine (pool) == -1) {
        virStoragePoolFree(pool);
4384
        remoteDispatchConnError(rerr, conn);
4385 4386 4387 4388 4389 4390 4391 4392
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchNumOfStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4393 4394
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4395
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4396
                                 remote_error *rerr,
4397 4398 4399 4400
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_num_of_storage_pools_ret *ret)
{

4401
    ret->num = virConnectNumOfStoragePools (conn);
4402
    if (ret->num == -1) {
4403
        remoteDispatchConnError(rerr, conn);
4404 4405
        return -1;
    }
4406 4407 4408 4409 4410 4411

    return 0;
}

static int
remoteDispatchNumOfDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4412 4413
                                        struct qemud_client *client ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
4414
                                        remote_message_header *hdr ATTRIBUTE_UNUSED,
4415
                                        remote_error *rerr,
4416 4417 4418 4419
                                        void *args ATTRIBUTE_UNUSED,
                                        remote_num_of_defined_storage_pools_ret *ret)
{

4420
    ret->num = virConnectNumOfDefinedStoragePools (conn);
4421
    if (ret->num == -1) {
4422
        remoteDispatchConnError(rerr, conn);
4423 4424
        return -1;
    }
4425 4426 4427 4428 4429 4430

    return 0;
}

static int
remoteDispatchStoragePoolListVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
4431 4432
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4433
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4434
                                      remote_error *rerr,
4435 4436 4437 4438 4439 4440
                                      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) {
4441 4442 4443
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX"));
        return -1;
4444 4445
    }

4446
    pool = get_nonnull_storage_pool (conn, args->pool);
4447
    if (pool == NULL) {
4448
        remoteDispatchConnError(rerr, conn);
4449
        return -1;
4450 4451 4452
    }

    /* Allocate return buffer. */
4453 4454
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        virStoragePoolFree(pool);
4455 4456
        remoteDispatchOOMError(rerr);
        return -1;
4457
    }
4458 4459 4460 4461 4462

    ret->names.names_len =
        virStoragePoolListVolumes (pool,
                                   ret->names.names_val, args->maxnames);
    virStoragePoolFree(pool);
4463 4464
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
4465
        remoteDispatchConnError(rerr, conn);
4466 4467
        return -1;
    }
4468 4469 4470 4471 4472 4473 4474

    return 0;
}


static int
remoteDispatchStoragePoolNumOfVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
4475 4476
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4477
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4478
                                       remote_error *rerr,
4479 4480 4481 4482 4483
                                       remote_storage_pool_num_of_volumes_args *args,
                                       remote_storage_pool_num_of_volumes_ret *ret)
{
    virStoragePoolPtr pool;

4484
    pool = get_nonnull_storage_pool (conn, args->pool);
4485
    if (pool == NULL) {
4486
        remoteDispatchConnError(rerr, conn);
4487
        return -1;
4488 4489 4490 4491
    }

    ret->num = virStoragePoolNumOfVolumes (pool);
    virStoragePoolFree(pool);
4492
    if (ret->num == -1) {
4493
        remoteDispatchConnError(rerr, conn);
4494 4495
        return -1;
    }
4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508

    return 0;
}


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



static int
remoteDispatchStorageVolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4509 4510
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4511
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4512
                                   remote_error *rerr,
4513 4514 4515 4516 4517 4518
                                   remote_storage_vol_create_xml_args *args,
                                   remote_storage_vol_create_xml_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

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

    vol = virStorageVolCreateXML (pool, args->xml, args->flags);
    virStoragePoolFree(pool);
4527
    if (vol == NULL) {
4528
        remoteDispatchConnError(rerr, conn);
4529 4530
        return -1;
    }
4531 4532 4533 4534 4535 4536

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

4537 4538 4539 4540
static int
remoteDispatchStorageVolCreateXmlFrom (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4541
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556
                                       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) {
4557
        virStoragePoolFree(pool);
4558 4559 4560 4561 4562 4563
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    newvol = virStorageVolCreateXMLFrom (pool, args->xml, clonevol,
                                         args->flags);
4564 4565
    virStorageVolFree(clonevol);
    virStoragePoolFree(pool);
4566 4567 4568 4569 4570 4571 4572 4573 4574
    if (newvol == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_storage_vol (&ret->vol, newvol);
    virStorageVolFree(newvol);
    return 0;
}
4575 4576 4577

static int
remoteDispatchStorageVolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
4578 4579
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4580
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4581
                                remote_error *rerr,
4582 4583 4584 4585 4586
                                remote_storage_vol_delete_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virStorageVolPtr vol;

4587
    vol = get_nonnull_storage_vol (conn, args->vol);
4588
    if (vol == NULL) {
4589
        remoteDispatchConnError(rerr, conn);
4590
        return -1;
4591 4592 4593 4594
    }

    if (virStorageVolDelete (vol, args->flags) == -1) {
        virStorageVolFree(vol);
4595
        remoteDispatchConnError(rerr, conn);
4596 4597 4598 4599 4600 4601
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}

4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633
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;
}

4634 4635
static int
remoteDispatchStorageVolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
4636 4637
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4638
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4639
                                 remote_error *rerr,
4640 4641 4642 4643 4644 4645
                                 remote_storage_vol_get_info_args *args,
                                 remote_storage_vol_get_info_ret *ret)
{
    virStorageVolPtr vol;
    virStorageVolInfo info;

4646
    vol = get_nonnull_storage_vol (conn, args->vol);
4647
    if (vol == NULL) {
4648
        remoteDispatchConnError(rerr, conn);
4649
        return -1;
4650 4651 4652 4653
    }

    if (virStorageVolGetInfo (vol, &info) == -1) {
        virStorageVolFree(vol);
4654
        remoteDispatchConnError(rerr, conn);
4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668
        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,
4669 4670
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4671
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4672
                                 remote_error *rerr,
4673 4674 4675 4676 4677
                                 remote_storage_vol_dump_xml_args *args,
                                 remote_storage_vol_dump_xml_ret *ret)
{
    virStorageVolPtr vol;

4678
    vol = get_nonnull_storage_vol (conn, args->vol);
4679
    if (vol == NULL) {
4680
        remoteDispatchConnError(rerr, conn);
4681
        return -1;
4682 4683 4684 4685 4686 4687
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStorageVolGetXMLDesc (vol, args->flags);
    if (!ret->xml) {
        virStorageVolFree(vol);
4688
        remoteDispatchConnError(rerr, conn);
4689 4690 4691 4692 4693 4694 4695 4696 4697
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolGetPath (struct qemud_server *server ATTRIBUTE_UNUSED,
4698 4699
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4700
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4701
                                 remote_error *rerr,
4702 4703 4704 4705 4706
                                 remote_storage_vol_get_path_args *args,
                                 remote_storage_vol_get_path_ret *ret)
{
    virStorageVolPtr vol;

4707
    vol = get_nonnull_storage_vol (conn, args->vol);
4708
    if (vol == NULL) {
4709
        remoteDispatchConnError(rerr, conn);
4710
        return -1;
4711 4712 4713 4714 4715 4716
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virStorageVolGetPath (vol);
    if (!ret->name) {
        virStorageVolFree(vol);
4717
        remoteDispatchConnError(rerr, conn);
4718 4719 4720 4721 4722 4723 4724 4725 4726
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
4727 4728
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4729
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4730
                                      remote_error *rerr,
4731 4732 4733 4734 4735 4736
                                      remote_storage_vol_lookup_by_name_args *args,
                                      remote_storage_vol_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

4737
    pool = get_nonnull_storage_pool (conn, args->pool);
4738
    if (pool == NULL) {
4739
        remoteDispatchConnError(rerr, conn);
4740
        return -1;
4741 4742 4743 4744
    }

    vol = virStorageVolLookupByName (pool, args->name);
    virStoragePoolFree(pool);
4745
    if (vol == NULL) {
4746
        remoteDispatchConnError(rerr, conn);
4747 4748
        return -1;
    }
4749 4750 4751 4752 4753 4754 4755 4756

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

static int
remoteDispatchStorageVolLookupByKey (struct qemud_server *server ATTRIBUTE_UNUSED,
4757 4758
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
4759
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
4760
                                     remote_error *rerr,
4761 4762 4763 4764 4765
                                     remote_storage_vol_lookup_by_key_args *args,
                                     remote_storage_vol_lookup_by_key_ret *ret)
{
    virStorageVolPtr vol;

4766
    vol = virStorageVolLookupByKey (conn, args->key);
4767
    if (vol == NULL) {
4768
        remoteDispatchConnError(rerr, conn);
4769 4770
        return -1;
    }
4771 4772 4773 4774 4775 4776 4777 4778 4779

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


static int
remoteDispatchStorageVolLookupByPath (struct qemud_server *server ATTRIBUTE_UNUSED,
4780 4781
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4782
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4783
                                      remote_error *rerr,
4784 4785 4786 4787 4788
                                      remote_storage_vol_lookup_by_path_args *args,
                                      remote_storage_vol_lookup_by_path_ret *ret)
{
    virStorageVolPtr vol;

4789
    vol = virStorageVolLookupByPath (conn, args->path);
4790
    if (vol == NULL) {
4791
        remoteDispatchConnError(rerr, conn);
4792 4793
        return -1;
    }
4794 4795 4796 4797 4798 4799 4800

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


4801 4802 4803 4804 4805 4806
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/

static int
remoteDispatchNodeNumOfDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
4807 4808
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4809
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4810
                                remote_error *rerr,
4811 4812 4813 4814 4815
                                remote_node_num_of_devices_args *args,
                                remote_node_num_of_devices_ret *ret)
{
    CHECK_CONN(client);

4816
    ret->num = virNodeNumOfDevices (conn,
4817 4818
                                    args->cap ? *args->cap : NULL,
                                    args->flags);
4819
    if (ret->num == -1) {
4820
        remoteDispatchConnError(rerr, conn);
4821 4822
        return -1;
    }
4823 4824 4825 4826 4827 4828 4829

    return 0;
}


static int
remoteDispatchNodeListDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
4830 4831
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
4832
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
4833
                               remote_error *rerr,
4834 4835 4836 4837 4838 4839
                               remote_node_list_devices_args *args,
                               remote_node_list_devices_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
4840 4841 4842
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
4843 4844 4845 4846
    }

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

    ret->names.names_len =
4852
        virNodeListDevices (conn,
4853 4854 4855
                            args->cap ? *args->cap : NULL,
                            ret->names.names_val, args->maxnames, args->flags);
    if (ret->names.names_len == -1) {
4856
        remoteDispatchConnError(rerr, conn);
4857 4858 4859 4860 4861 4862 4863 4864 4865 4866
        VIR_FREE(ret->names.names_val);
        return -1;
    }

    return 0;
}


static int
remoteDispatchNodeDeviceLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
4867 4868
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4869
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4870
                                      remote_error *rerr,
4871 4872 4873 4874 4875 4876 4877
                                      remote_node_device_lookup_by_name_args *args,
                                      remote_node_device_lookup_by_name_ret *ret)
{
    virNodeDevicePtr dev;

    CHECK_CONN(client);

4878
    dev = virNodeDeviceLookupByName (conn, args->name);
4879
    if (dev == NULL) {
4880
        remoteDispatchConnError(rerr, conn);
4881 4882
        return -1;
    }
4883 4884 4885 4886 4887 4888 4889 4890 4891

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


static int
remoteDispatchNodeDeviceDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4892 4893
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4894
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4895
                                 remote_error *rerr,
4896 4897 4898 4899 4900 4901
                                 remote_node_device_dump_xml_args *args,
                                 remote_node_device_dump_xml_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

4902
    dev = virNodeDeviceLookupByName(conn, args->name);
4903
    if (dev == NULL) {
4904
        remoteDispatchConnError(rerr, conn);
4905
        return -1;
4906 4907 4908 4909 4910
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNodeDeviceGetXMLDesc (dev, args->flags);
    if (!ret->xml) {
4911
        remoteDispatchConnError(rerr, conn);
4912 4913 4914 4915 4916 4917 4918 4919 4920 4921
        virNodeDeviceFree(dev);
        return -1;
    }
    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceGetParent (struct qemud_server *server ATTRIBUTE_UNUSED,
4922 4923
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4924
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4925
                                   remote_error *rerr,
4926 4927 4928 4929 4930 4931 4932
                                   remote_node_device_get_parent_args *args,
                                   remote_node_device_get_parent_ret *ret)
{
    virNodeDevicePtr dev;
    const char *parent;
    CHECK_CONN(client);

4933
    dev = virNodeDeviceLookupByName(conn, args->name);
4934
    if (dev == NULL) {
4935
        remoteDispatchConnError(rerr, conn);
4936
        return -1;
4937 4938 4939 4940 4941 4942 4943 4944 4945 4946
    }

    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
        if (VIR_ALLOC(parent_p) < 0) {
4947 4948
            remoteDispatchOOMError(rerr);
            return -1;
4949 4950 4951
        }
        *parent_p = strdup(parent);
        if (*parent_p == NULL) {
4952 4953
            remoteDispatchOOMError(rerr);
            return -1;
4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964
        }
        ret->parent = parent_p;
    }

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceNumOfCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
4965 4966
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4967
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4968
                                   remote_error *rerr,
4969 4970 4971 4972 4973 4974
                                   remote_node_device_num_of_caps_args *args,
                                   remote_node_device_num_of_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

4975
    dev = virNodeDeviceLookupByName(conn, args->name);
4976
    if (dev == NULL) {
4977
        remoteDispatchConnError(rerr, conn);
4978
        return -1;
4979 4980 4981
    }

    ret->num = virNodeDeviceNumOfCaps(dev);
4982
    if (ret->num < 0) {
4983
        remoteDispatchConnError(rerr, conn);
4984 4985
        return -1;
    }
4986 4987 4988 4989 4990 4991 4992 4993

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceListCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
4994 4995
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4996
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4997
                                  remote_error *rerr,
4998 4999 5000 5001 5002 5003
                                  remote_node_device_list_caps_args *args,
                                  remote_node_device_list_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

5004
    dev = virNodeDeviceLookupByName(conn, args->name);
5005
    if (dev == NULL) {
5006
        remoteDispatchConnError(rerr, conn);
5007
        return -1;
5008 5009 5010
    }

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
5011 5012 5013
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
5014 5015 5016 5017
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
5018 5019
        remoteDispatchOOMError(rerr);
        return -1;
5020 5021 5022 5023 5024 5025
    }

    ret->names.names_len =
        virNodeDeviceListCaps (dev, ret->names.names_val,
                               args->maxnames);
    if (ret->names.names_len == -1) {
5026
        remoteDispatchConnError(rerr, conn);
5027 5028 5029 5030 5031 5032 5033 5034
        VIR_FREE(ret->names.names_val);
        return -1;
    }

    return 0;
}


5035 5036 5037 5038
static int
remoteDispatchNodeDeviceDettach (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
5039
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
5040 5041 5042 5043 5044 5045 5046 5047 5048
                                 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) {
5049
        remoteDispatchConnError(rerr, conn);
5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065
        return -1;
    }

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

    return 0;
}


static int
remoteDispatchNodeDeviceReAttach (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
5066
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
5067 5068 5069 5070 5071 5072 5073 5074 5075
                                  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) {
5076
        remoteDispatchConnError(rerr, conn);
5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092
        return -1;
    }

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

    return 0;
}


static int
remoteDispatchNodeDeviceReset (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
5093
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
5094 5095 5096 5097 5098 5099 5100 5101 5102
                               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) {
5103
        remoteDispatchConnError(rerr, conn);
5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115
        return -1;
    }

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

    return 0;
}


5116 5117 5118 5119
static int
remoteDispatchNodeDeviceCreateXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
5120
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143
                                  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,
5144
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
5145 5146 5147 5148 5149 5150 5151 5152
                                remote_error *rerr,
                                remote_node_device_destroy_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virNodeDevicePtr dev;

    dev = virNodeDeviceLookupByName(conn, args->name);
    if (dev == NULL) {
5153
        remoteDispatchConnError(rerr, conn);
5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165
        return -1;
    }

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

    return 0;
}


5166 5167 5168 5169 5170 5171

/***************************
 * Register / deregister events
 ***************************/
static int
remoteDispatchDomainEventsRegister (struct qemud_server *server ATTRIBUTE_UNUSED,
5172 5173
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
5174
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
5175
                                    remote_error *rerr ATTRIBUTE_UNUSED,
5176 5177 5178 5179
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);
5180 5181 5182 5183 5184 5185
    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;
    }
5186

5187 5188 5189 5190 5191
    if ((callbackID = virConnectDomainEventRegisterAny(conn,
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
                                                       client, NULL)) < 0) {
5192 5193 5194
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
5195

5196
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;
5197

5198 5199 5200 5201 5202
    return 0;
}

static int
remoteDispatchDomainEventsDeregister (struct qemud_server *server ATTRIBUTE_UNUSED,
5203 5204
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
5205
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
5206
                                      remote_error *rerr ATTRIBUTE_UNUSED,
5207 5208 5209 5210 5211
                                      void *args ATTRIBUTE_UNUSED,
                                      remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

5212 5213
    if (client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] == -1) {
        remoteDispatchFormatError(rerr, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
5214 5215
        return -1;
    }
5216

5217 5218 5219 5220 5221
    if (virConnectDomainEventDeregisterAny(conn,
                                           client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
5222

5223
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;
5224 5225 5226 5227 5228
    return 0;
}

static void
remoteDispatchDomainEventSend (struct qemud_client *client,
5229 5230 5231
                               int procnr,
                               xdrproc_t proc,
                               void *data)
5232
{
5233
    struct qemud_client_message *msg = NULL;
5234
    XDR xdr;
5235
    unsigned int len;
5236

5237
    if (VIR_ALLOC(msg) < 0)
5238 5239
        return;

5240 5241
    msg->hdr.prog = REMOTE_PROGRAM;
    msg->hdr.vers = REMOTE_PROTOCOL_VERSION;
5242
    msg->hdr.proc = procnr;
5243
    msg->hdr.type = REMOTE_MESSAGE;
5244 5245
    msg->hdr.serial = 1;
    msg->hdr.status = REMOTE_OK;
5246

5247 5248
    if (remoteEncodeClientMessageHeader(msg) < 0)
        goto error;
5249

5250 5251
    /* Serialise the return header and event. */
    xdrmem_create (&xdr,
5252 5253
                   msg->buffer,
                   msg->bufferLength,
5254
                   XDR_ENCODE);
5255

5256 5257
    /* Skip over the header we just wrote */
    if (xdr_setpos (&xdr, msg->bufferOffset) == 0)
5258
        goto xdr_error;
5259

5260 5261
    if (!(proc)(&xdr, data)) {
        VIR_WARN("Failed to serialize domain event %d", procnr);
5262
        goto xdr_error;
5263
    }
5264

5265 5266
    /* Update length word to include payload*/
    len = msg->bufferOffset = xdr_getpos (&xdr);
5267 5268
    if (xdr_setpos (&xdr, 0) == 0)
        goto xdr_error;
5269

5270 5271
    if (!xdr_u_int (&xdr, &len))
        goto xdr_error;
5272 5273

    /* Send it. */
5274 5275 5276 5277
    msg->async = 1;
    msg->bufferLength = len;
    msg->bufferOffset = 0;
    qemudClientMessageQueuePush(&client->tx, msg);
5278
    qemudUpdateClientEvent(client);
5279 5280 5281 5282 5283 5284 5285 5286

    xdr_destroy (&xdr);
    return;

xdr_error:
    xdr_destroy(&xdr);
error:
    VIR_FREE(msg);
5287
}
5288

5289 5290 5291
static int
remoteDispatchNumOfSecrets (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client ATTRIBUTE_UNUSED,
5292 5293 5294
                            virConnectPtr conn,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *err,
5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309
                            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,
5310 5311 5312
                           virConnectPtr conn,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *err,
5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340
                           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,
5341 5342 5343
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
                               remote_error *err,
5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362
                               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,
5363 5364 5365
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394
                              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,
5395 5396 5397
                                virConnectPtr conn,
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                remote_error *err,
5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418
                                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
5419 5420
remoteDispatchSecretLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
5421 5422 5423
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *err,
5424 5425
                                  remote_secret_lookup_by_uuid_args *args,
                                  remote_secret_lookup_by_uuid_ret *ret)
5426 5427 5428
{
    virSecretPtr secret;

5429
    secret = virSecretLookupByUUID (conn, (unsigned char *)args->uuid);
5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442
    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,
5443 5444 5445
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469
                              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,
5470 5471 5472
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492
                              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;
}

5493 5494 5495
static int
remoteDispatchSecretLookupByUsage (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
5496 5497 5498
                                   virConnectPtr conn,
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                                   remote_error *err,
5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514
                                   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;
}

5515

5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717
static int remoteDispatchDomainIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                        struct qemud_client *client ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *hdr ATTRIBUTE_UNUSED,
                                        remote_error *err,
                                        remote_domain_is_active_args *args,
                                        remote_domain_is_active_ret *ret)
{
    virDomainPtr domain;

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

    ret->active = virDomainIsActive(domain);

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

    return 0;
}

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

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

    ret->persistent = virDomainIsPersistent(domain);

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

    return 0;
}

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

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

    ret->active = virInterfaceIsActive(iface);

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

    return 0;
}

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

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

    ret->active = virNetworkIsActive(network);

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

    return 0;
}

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

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

    ret->persistent = virNetworkIsPersistent(network);

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

    return 0;
}

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

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

    ret->active = virStoragePoolIsActive(pool);

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

    return 0;
}

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

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

    ret->persistent = virStoragePoolIsPersistent(pool);

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

    return 0;
}


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

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

    return 0;
}


5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739
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;
}


5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760
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;
5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804

    return 0;
}


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

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

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

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

    virDomainFree(dom);

5805 5806 5807 5808
    return 0;
}


5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837
static int
remoteDispatchDomainAbortJob (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *rerr,
                              remote_domain_abort_job_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

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

    virDomainFree(dom);

    return 0;
}


5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865
static int
remoteDispatchDomainMigrateSetMaxDowntime(struct qemud_server *server ATTRIBUTE_UNUSED,
                                          struct qemud_client *client ATTRIBUTE_UNUSED,
                                          virConnectPtr conn,
                                          remote_message_header *hdr ATTRIBUTE_UNUSED,
                                          remote_error *rerr,
                                          remote_domain_migrate_set_max_downtime_args *args,
                                          void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

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

    virDomainFree(dom);

    return 0;
}

C
Chris Lalancette 已提交
5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 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 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157
static int
remoteDispatchDomainSnapshotCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_snapshot_create_xml_args *args,
                                       remote_domain_snapshot_create_xml_ret *ret)
{
    virDomainSnapshotPtr snapshot;
    virDomainPtr domain;

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

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

    make_nonnull_domain_snapshot(&ret->snap, snapshot);

    virDomainSnapshotFree(snapshot);
    virDomainFree(domain);

    return 0;
}

static int
remoteDispatchDomainSnapshotDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
                                     remote_error *rerr,
                                     remote_domain_snapshot_dump_xml_args *args,
                                     remote_domain_snapshot_dump_xml_ret *ret)
{
    virDomainSnapshotPtr snapshot;

    snapshot = get_nonnull_domain_snapshot(conn, args->snap);
    if (snapshot == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainSnapshotGetXMLDesc(snapshot, args->flags);
    if (!ret->xml) {
        virDomainSnapshotFree(snapshot);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    virDomainSnapshotFree(snapshot);

    return 0;
}

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

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

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

    virDomainFree(domain);

    return 0;
}

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

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

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

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

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

    virDomainFree(domain);

    return 0;
}

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

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

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

    make_nonnull_domain_snapshot (&ret->snap, snapshot);

    virDomainSnapshotFree(snapshot);
    virDomainFree(domain);

    return 0;
}

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

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

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

    ret->result = result;

    virDomainFree(domain);

    return 0;
}

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

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

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

    make_nonnull_domain_snapshot(&ret->snap, snapshot);

    virDomainSnapshotFree(snapshot);
    virDomainFree(domain);

    return 0;
}

static int
remoteDispatchDomainRevertToSnapshot (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
                                      remote_error *rerr,
                                      remote_domain_revert_to_snapshot_args *args,
                                      void *ret ATTRIBUTE_UNUSED)
{
    virDomainSnapshotPtr snapshot;

    snapshot = get_nonnull_domain_snapshot(conn, args->snap);
    if (snapshot == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainRevertToSnapshot(snapshot, args->flags) == -1) {
        virDomainSnapshotFree(snapshot);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    virDomainSnapshotFree(snapshot);

    return 0;
}

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)
{
    virDomainSnapshotPtr snapshot;

    snapshot = get_nonnull_domain_snapshot(conn, args->snap);
    if (snapshot == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    if (virDomainSnapshotDelete(snapshot, args->flags) == -1) {
        virDomainSnapshotFree(snapshot);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    virDomainSnapshotFree(snapshot);

    return 0;
}

6158

6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230
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;
}


6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409

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

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

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

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

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

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


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

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

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


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

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

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

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

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

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

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

    return 0;
}


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

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

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


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

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

    return 0;
}


6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435
/*----- 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 已提交
6436
static virInterfacePtr
6437
get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
6438
{
6439
    return virGetInterface (conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
6440 6441
}

6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455
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;
}

6456 6457 6458
static virSecretPtr
get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret)
{
6459
    return virGetSecret (conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
6460 6461
}

6462 6463 6464 6465 6466 6467
static virNWFilterPtr
get_nonnull_nwfilter (virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
{
    return virGetNWFilter (conn, nwfilter.name, BAD_CAST nwfilter.uuid);
}

C
Chris Lalancette 已提交
6468 6469 6470 6471 6472 6473 6474 6475 6476 6477
static virDomainSnapshotPtr
get_nonnull_domain_snapshot (virConnectPtr conn, remote_nonnull_domain_snapshot snapshot)
{
    virDomainPtr domain;
    domain = get_nonnull_domain(conn, snapshot.domain);
    if (domain == NULL)
        return NULL;
    return virGetDomainSnapshot(domain, snapshot.name);
}

6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493
/* 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 已提交
6494 6495 6496 6497 6498 6499 6500 6501
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);
}

6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515
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);
}
6516 6517 6518 6519 6520 6521

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

static void
make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
{
6526
    memcpy (secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
6527 6528
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = strdup (secret_src->usageID);
6529
}
6530 6531 6532 6533 6534 6535 6536

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 已提交
6537 6538 6539 6540 6541 6542 6543

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