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

#include <config.h>

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

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

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

55 56
#include "libvirt_internal.h"
#include "datatypes.h"
57
#include "memory.h"
58
#include "util.h"
C
Chris Lalancette 已提交
59
#include "stream.h"
60
#include "uuid.h"
61
#include "network.h"
C
Chris Lalancette 已提交
62
#include "libvirt/libvirt-qemu.h"
63
#include "command.h"
64

65
#define VIR_FROM_THIS VIR_FROM_REMOTE
66

67 68 69 70
#define virNetError(code, ...)                                    \
    virReportErrorHelper(VIR_FROM_THIS, code, __FILE__,           \
                         __FUNCTION__, __LINE__, __VA_ARGS__)

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

89

90
#include "remote_dispatch_prototypes.h"
C
Chris Lalancette 已提交
91
#include "qemu_dispatch_prototypes.h"
92 93 94 95

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

C
Chris Lalancette 已提交
97 98 99 100
static const dispatch_data const qemu_dispatch_table[] = {
#include "qemu_dispatch_table.h"
};

101 102 103 104 105 106 107 108 109 110
const dispatch_data const *remoteGetDispatchData(int proc)
{
    if (proc >= ARRAY_CARDINALITY(dispatch_table) ||
        dispatch_table[proc].fn == NULL) {
        return NULL;
    }

    return &(dispatch_table[proc]);
}

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

    return &(qemu_dispatch_table[proc]);
}

121 122
/* Prototypes */
static void
123 124 125 126
remoteDispatchDomainEventSend(struct qemud_client *client,
                              int procnr,
                              xdrproc_t proc,
                              void *data);
127

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

137 138 139
    if (!client)
        return -1;

140
    VIR_DEBUG("Relaying domain lifecycle event %d %d", event, detail);
141

142
    virMutexLock(&client->lock);
143

144 145
    /* build return data */
    memset(&data, 0, sizeof data);
146
    make_nonnull_domain(&data.dom, dom);
147 148
    data.event = event;
    data.detail = detail;
149

150 151 152
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_domain_event_lifecycle_msg, &data);
153 154

    virMutexUnlock(&client->lock);
155

156 157
    return 0;
}
158

159 160 161 162 163 164 165 166 167 168
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;

169
    VIR_DEBUG("Relaying domain reboot event %s %d", dom->name, dom->id);
170 171 172 173 174

    virMutexLock(&client->lock);

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

177 178 179
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_REBOOT,
                                  (xdrproc_t)xdr_remote_domain_event_reboot_msg, &data);
180 181 182 183 184 185

    virMutexUnlock(&client->lock);

    return 0;
}

186

187 188 189 190 191 192 193 194 195 196 197
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;

198
    VIR_DEBUG("Relaying domain rtc change event %s %d %lld", dom->name, dom->id, offset);
199 200 201 202 203

    virMutexLock(&client->lock);

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

207 208 209
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_rtc_change_msg, &data);
210 211 212 213 214 215 216

    virMutexUnlock(&client->lock);

    return 0;
}


217 218 219 220 221 222 223 224 225 226 227
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;

228
    VIR_DEBUG("Relaying domain watchdog event %s %d %d", dom->name, dom->id, action);
229 230 231 232 233

    virMutexLock(&client->lock);

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

237 238 239
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_WATCHDOG,
                                  (xdrproc_t)xdr_remote_domain_event_watchdog_msg, &data);
240 241 242 243 244 245 246

    virMutexUnlock(&client->lock);

    return 0;
}


247 248 249 250 251 252 253 254 255 256 257 258 259
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;

260
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d", dom->name, dom->id, srcPath, devAlias, action);
261 262 263 264 265

    virMutexLock(&client->lock);

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

271 272 273
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_msg, &data);
274 275 276 277 278 279 280

    virMutexUnlock(&client->lock);

    return 0;
}


281 282 283 284 285 286 287 288 289 290 291 292 293 294
static int remoteRelayDomainEventIOErrorReason(virConnectPtr conn ATTRIBUTE_UNUSED,
                                               virDomainPtr dom,
                                               const char *srcPath,
                                               const char *devAlias,
                                               int action,
                                               const char *reason,
                                               void *opaque)
{
    struct qemud_client *client = opaque;
    remote_domain_event_io_error_reason_msg data;

    if (!client)
        return -1;

295 296
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s",
              dom->name, dom->id, srcPath, devAlias, action, reason);
297 298 299 300 301

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
302
    make_nonnull_domain(&data.dom, dom);
303 304 305 306 307
    data.srcPath = (char*)srcPath;
    data.devAlias = (char*)devAlias;
    data.action = action;
    data.reason = (char*)reason;

308 309 310
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_reason_msg, &data);
311 312 313 314 315 316 317

    virMutexUnlock(&client->lock);

    return 0;
}


318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
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;

334 335 336 337
    VIR_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);
338

339
    VIR_DEBUG("Subject %d", subject->nidentity);
340
    for (i = 0 ; i < subject->nidentity ; i++) {
341
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
342 343 344 345 346 347
    }

    virMutexLock(&client->lock);

    /* build return data */
    memset(&data, 0, sizeof data);
348
    make_nonnull_domain(&data.dom, dom);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    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;
    }

370 371 372
    remoteDispatchDomainEventSend(client,
                                  REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
                                  (xdrproc_t)xdr_remote_domain_event_graphics_msg, &data);
373 374 375 376 377 378 379 380 381

    VIR_FREE(data.subject.subject_val);

    virMutexUnlock(&client->lock);

    return 0;
}


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

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

394 395 396
/*----- Functions. -----*/

static int
397 398 399 400 401 402
remoteDispatchOpen(struct qemud_server *server,
                   struct qemud_client *client,
                   virConnectPtr conn,
                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                   remote_error *rerr,
                   struct remote_open_args *args, void *ret ATTRIBUTE_UNUSED)
403 404
{
    const char *name;
405 406
    int flags;
    int rv = -1;
407

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

412 413 414 415 416
    if (conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
        goto cleanup;
    }

417 418 419 420 421 422 423 424 425 426
    name = args->name ? *args->name : NULL;

    /* If this connection arrived on a readonly socket, force
     * the connection to be readonly.
     */
    flags = args->flags;
    if (client->readonly) flags |= VIR_CONNECT_RO;

    client->conn =
        flags & VIR_CONNECT_RO
427 428
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
429

430
    if (client->conn == NULL)
431 432 433
        goto cleanup;

    rv = 0;
434

435 436 437
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
438
    virMutexUnlock(&client->lock);
439
    return rv;
440 441 442 443
}


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

455
    client->closing = 1;
456

457
    virMutexUnlock(&client->lock);
458
    return 0;
459 460
}

461
static int
462 463 464 465 466 467
remoteDispatchSupportsFeature(struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *rerr,
                              remote_supports_feature_args *args, remote_supports_feature_ret *ret)
468
{
469
    int rv = -1;
470 471

    if (!conn) {
472 473
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
474 475
    }

476
    if ((ret->supported = virDrvSupportsFeature(conn, args->feature)) < 0)
477
        goto cleanup;
478

479 480 481 482 483 484
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
485 486
}

487
static int
488 489 490 491 492 493
remoteDispatchGetType(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_type_ret *ret)
494 495
{
    const char *type;
496
    int rv = -1;
497

498
    if (!conn) {
499 500
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
501 502
    }

503
    if (!(type = virConnectGetType(conn)))
504
        goto cleanup;
505 506 507 508

    /* We have to strdup because remoteDispatchClientRequest will
     * free this string after it's been serialised.
     */
509
    if (!(ret->type = strdup(type))) {
510 511
        virReportOOMError();
        goto cleanup;
512 513
    }

514 515 516 517 518 519
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
520 521 522
}

static int
523 524 525 526 527 528 529
remoteDispatchGetVersion(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_version_ret *ret)
530 531
{
    unsigned long hvVer;
532
    int rv = -1;
533

534
    if (!conn) {
535 536
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
537 538
    }

539
    if (virConnectGetVersion(conn, &hvVer) < 0)
540
        goto cleanup;
541 542

    ret->hv_ver = hvVer;
543 544 545 546 547 548
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
549 550
}

551
static int
552 553 554 555 556 557 558
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)
559 560
{
    unsigned long libVer;
561
    int rv = -1;
562

563
    if (!conn) {
564 565
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
566 567
    }

568
    if (virConnectGetLibVersion(conn, &libVer) < 0)
569
        goto cleanup;
570 571

    ret->lib_ver = libVer;
572 573 574 575 576 577
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
578 579
}

580
static int
581 582 583 584 585 586 587
remoteDispatchGetHostname(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_hostname_ret *ret)
588 589
{
    char *hostname;
590
    int rv = -1;
591

592
    if (!conn) {
593 594
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
595 596
    }

597
    if (!(hostname = virConnectGetHostname(conn)))
598
        goto cleanup;
599 600

    ret->hostname = hostname;
601 602 603 604 605 606
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
607 608
}

609
static int
610 611 612 613 614 615 616
remoteDispatchGetUri(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_uri_ret *ret)
617 618
{
    char *uri;
619
    int rv = -1;
620 621

    if (!conn) {
622 623
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
624
    }
625

626
    if (!(uri = virConnectGetURI(conn)))
627
        goto cleanup;
628 629

    ret->uri = uri;
630 631 632 633 634 635
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
636 637
}

638
static int
639 640 641 642 643 644 645
remoteDispatchGetSysinfo(struct qemud_server *server ATTRIBUTE_UNUSED,
                         struct qemud_client *client ATTRIBUTE_UNUSED,
                         virConnectPtr conn,
                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                         remote_error *rerr,
                         remote_get_sysinfo_args *args,
                         remote_get_sysinfo_ret *ret)
646 647 648
{
    unsigned int flags;
    char *sysinfo;
649
    int rv = -1;
650

651
    if (!conn) {
652 653
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
654 655
    }

656
    flags = args->flags;
657
    if (!(sysinfo = virConnectGetSysinfo(conn, flags)))
658
        goto cleanup;
659 660

    ret->sysinfo = sysinfo;
661 662 663 664 665 666
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
667 668
}

669
static int
670 671 672 673 674 675 676
remoteDispatchGetMaxVcpus(struct qemud_server *server ATTRIBUTE_UNUSED,
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
                          remote_error *rerr,
                          remote_get_max_vcpus_args *args,
                          remote_get_max_vcpus_ret *ret)
677 678
{
    char *type;
679
    int rv = -1;
680

681
    if (!conn) {
682 683
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
684 685
    }

686
    type = args->type ? *args->type : NULL;
687
    if ((ret->max_vcpus = virConnectGetMaxVcpus(conn, type)) < 0)
688
        goto cleanup;
689

690 691 692 693 694 695
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
696 697 698
}

static int
699 700 701 702 703 704 705
remoteDispatchNodeGetInfo(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_node_get_info_ret *ret)
706 707
{
    virNodeInfo info;
708
    int rv = -1;
709

710
    if (!conn) {
711 712
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
713 714
    }

715
    if (virNodeGetInfo(conn, &info) < 0)
716
        goto cleanup;
717

718
    memcpy(ret->model, info.model, sizeof ret->model);
719 720 721 722 723 724 725 726
    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;

727 728 729 730 731 732
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
733 734 735
}

static int
736 737 738 739 740 741 742
remoteDispatchGetCapabilities(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_capabilities_ret *ret)
743 744
{
    char *caps;
745
    int rv = -1;
746

747
    if (!conn) {
748 749
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
750 751
    }

752
    if (!(caps = virConnectGetCapabilities(conn)))
753
        goto cleanup;
754 755

    ret->capabilities = caps;
756 757 758 759 760 761
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
762 763
}

764
static int
765 766 767 768 769 770 771
remoteDispatchNodeGetCellsFreeMemory(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
                                     remote_error *rerr,
                                     remote_node_get_cells_free_memory_args *args,
                                     remote_node_get_cells_free_memory_ret *ret)
772
{
773
    int len;
774
    int rv = -1;
775

776
    if (!conn) {
777 778
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
779 780
    }

781
    if (args->maxCells > REMOTE_NODE_MAX_CELLS) {
782 783 784
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxCells > REMOTE_NODE_MAX_CELLS"));
        goto cleanup;
785 786 787
    }

    /* Allocate return buffer. */
788
    if (VIR_ALLOC_N(ret->freeMems.freeMems_val, args->maxCells) < 0) {
789 790
        virReportOOMError();
        goto cleanup;
791
    }
792

793
    len = virNodeGetCellsFreeMemory(conn,
D
Daniel P. Berrange 已提交
794 795 796
                                    (unsigned long long *)ret->freeMems.freeMems_val,
                                    args->startCell,
                                    args->maxCells);
797
    if (len <= 0)
798
        goto cleanup;
799
    ret->freeMems.freeMems_len = len;
800

801 802 803 804 805 806 807 808
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->freeMems.freeMems_val);
    }
    return rv;
809 810 811 812
}


static int
813 814 815 816 817 818 819
remoteDispatchNodeGetFreeMemory(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_node_get_free_memory_ret *ret)
820 821
{
    unsigned long long freeMem;
822
    int rv = -1;
823

824
    if (!conn) {
825 826
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
827 828
    }

829
    if ((freeMem = virNodeGetFreeMemory(conn)) == 0)
830
        goto cleanup;
831
    ret->freeMem = freeMem;
832 833 834 835 836 837
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
838 839 840
}


841
static int
842 843 844 845 846 847 848
remoteDispatchDomainGetSchedulerType(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_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
849
{
850
    virDomainPtr dom = NULL;
851 852
    char *type;
    int nparams;
853
    int rv = -1;
854

855
    if (!conn) {
856 857
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
858 859
    }

860
    if (!(dom = get_nonnull_domain(conn, args->dom)))
861
        goto cleanup;
862

863
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
864
        goto cleanup;
865 866 867

    ret->type = type;
    ret->nparams = nparams;
868 869 870 871 872 873 874 875
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
876 877 878
}

static int
879 880 881 882 883 884 885
remoteDispatchDomainGetSchedulerParameters(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_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
886
{
887 888
    virDomainPtr dom = NULL;
    virSchedParameterPtr params = NULL;
889
    int i, nparams;
890
    int rv = -1;
891

892
    if (!conn) {
893 894
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
895 896
    }

897 898 899
    nparams = args->nparams;

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
900 901
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
902
    }
903 904
    if (VIR_ALLOC_N(params, nparams) < 0)
        goto no_memory;
905

906
    if (!(dom = get_nonnull_domain(conn, args->dom)))
907
        goto cleanup;
908

909
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
910
        goto cleanup;
911 912 913

    /* Serialise the scheduler parameters. */
    ret->params.params_len = nparams;
914
    if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
915
        goto no_memory;
916 917

    for (i = 0; i < nparams; ++i) {
918
        /* remoteDispatchClientRequest will free this: */
919
        ret->params.params_val[i].field = strdup(params[i].field);
920
        if (ret->params.params_val[i].field == NULL)
921
            goto no_memory;
922

923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
        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:
938
            virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("unknown type"));
939
            goto cleanup;
940 941 942
        }
    }

943
    rv = 0;
944 945

cleanup:
946 947 948 949 950 951 952
    if (rv < 0) {
        remoteDispatchError(rerr);
        for (i = 0 ; i < nparams ; i++)
            VIR_FREE(ret->params.params_val[i].field);
    }
    if (dom)
        virDomainFree(dom);
953
    VIR_FREE(params);
954 955 956 957 958
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
959 960 961
}

static int
962 963 964 965 966 967 968
remoteDispatchDomainSetSchedulerParameters(struct qemud_server *server ATTRIBUTE_UNUSED,
                                           struct qemud_client *client ATTRIBUTE_UNUSED,
                                           virConnectPtr conn,
                                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                                           remote_error *rerr,
                                           remote_domain_set_scheduler_parameters_args *args,
                                           void *ret ATTRIBUTE_UNUSED)
969
{
970 971
    virDomainPtr dom = NULL;
    virSchedParameterPtr params = NULL;
972
    int i, nparams;
973
    int rv = -1;
974

975
    if (!conn) {
976 977
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
978 979
    }

980 981 982
    nparams = args->params.params_len;

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
983 984
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
985
    }
986
    if (VIR_ALLOC_N(params, nparams) < 0) {
987 988
        virReportOOMError();
        goto cleanup;
989 990 991 992
    }

    /* Deserialise parameters. */
    for (i = 0; i < nparams; ++i) {
C
Chris Lalancette 已提交
993
        if (virStrcpyStatic(params[i].field, args->params.params_val[i].field) == NULL) {
994
            virNetError(VIR_ERR_INTERNAL_ERROR, _("Field %s too big for destination"),
C
Chris Lalancette 已提交
995
                                      args->params.params_val[i].field);
996
            goto cleanup;
C
Chris Lalancette 已提交
997
        }
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
        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;
        }
    }

1015
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1016
        goto cleanup;
1017

1018
    if (virDomainSetSchedulerParameters(dom, params, nparams) < 0)
1019
        goto cleanup;
1020

1021 1022 1023 1024 1025 1026 1027 1028 1029
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    VIR_FREE(params);
    return rv;
1030 1031
}

1032
static int
1033 1034 1035 1036 1037 1038 1039
remoteDispatchDomainBlockStats(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_block_stats_args *args,
                               remote_domain_block_stats_ret *ret)
1040
{
1041
    virDomainPtr dom = NULL;
1042 1043
    char *path;
    struct _virDomainBlockStats stats;
1044
    int rv = -1;
1045

1046
    if (!conn) {
1047 1048
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1049 1050
    }

1051
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1052
        goto cleanup;
1053 1054
    path = args->path;

1055
    if (virDomainBlockStats(dom, path, &stats, sizeof stats) < 0)
1056
        goto cleanup;
1057 1058 1059 1060 1061 1062 1063

    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;

1064 1065 1066 1067 1068 1069 1070 1071
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1072 1073 1074
}

static int
1075 1076 1077 1078 1079 1080 1081
remoteDispatchDomainInterfaceStats(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_interface_stats_args *args,
                                   remote_domain_interface_stats_ret *ret)
1082
{
1083
    virDomainPtr dom = NULL;
1084 1085
    char *path;
    struct _virDomainInterfaceStats stats;
1086
    int rv = -1;
1087

1088
    if (!conn) {
1089 1090
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1091 1092
    }

1093
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1094
        goto cleanup;
1095 1096
    path = args->path;

1097
    if (virDomainInterfaceStats(dom, path, &stats, sizeof stats) < 0)
1098
        goto cleanup;
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108

    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;

1109 1110 1111 1112 1113 1114 1115 1116
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1117 1118
}

1119
static int
1120 1121 1122 1123 1124 1125 1126
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)
1127
{
1128
    virDomainPtr dom = NULL;
1129
    struct _virDomainMemoryStat *stats;
1130
    int nr_stats, i;
1131
    int rv = -1;
1132

1133
    if (!conn) {
1134 1135
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1136 1137
    }

1138
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1139 1140 1141
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
        goto cleanup;
1142 1143
    }

1144
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1145
        goto cleanup;
1146 1147 1148

    /* Allocate stats array for making dispatch call */
    if (VIR_ALLOC_N(stats, args->maxStats) < 0) {
1149 1150
        virReportOOMError();
        goto cleanup;
1151
    }
1152

1153
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, 0);
1154
    if (nr_stats < 0)
1155
        goto cleanup;
1156 1157 1158

    /* Allocate return buffer */
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0) {
1159 1160
        virReportOOMError();
        goto cleanup;
1161 1162 1163 1164 1165 1166 1167 1168
    }

    /* 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;
1169 1170 1171 1172 1173 1174 1175
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
1176
    VIR_FREE(stats);
1177
    return rv;
1178 1179
}

1180
static int
1181 1182 1183 1184 1185 1186 1187
remoteDispatchDomainBlockPeek(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_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1188
{
1189
    virDomainPtr dom = NULL;
1190 1191 1192 1193
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1194
    int rv = -1;
1195

1196
    if (!conn) {
1197 1198
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1199 1200
    }

1201
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1202
        goto cleanup;
1203 1204 1205 1206 1207 1208
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1209 1210 1211
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("size > maximum buffer size"));
        goto cleanup;
1212 1213 1214
    }

    ret->buffer.buffer_len = size;
1215
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
1216 1217
        virReportOOMError();
        goto cleanup;
1218 1219
    }

1220
    if (virDomainBlockPeek(dom, path, offset, size,
1221
                           ret->buffer.buffer_val, flags) < 0)
1222
        goto cleanup;
1223

1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
1234 1235
}

R
Richard W.M. Jones 已提交
1236
static int
1237 1238 1239 1240 1241 1242 1243
remoteDispatchDomainMemoryPeek(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_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1244
{
1245
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1246 1247 1248
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1249
    int rv = -1;
R
Richard W.M. Jones 已提交
1250

1251
    if (!conn) {
1252 1253
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1254 1255
    }

1256
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1257
        goto cleanup;
R
Richard W.M. Jones 已提交
1258 1259 1260 1261 1262
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1263 1264 1265
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("size > maximum buffer size"));
        goto cleanup;
R
Richard W.M. Jones 已提交
1266 1267 1268
    }

    ret->buffer.buffer_len = size;
1269
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
1270 1271
        virReportOOMError();
        goto cleanup;
R
Richard W.M. Jones 已提交
1272 1273
    }

1274
    if (virDomainMemoryPeek(dom, offset, size,
1275
                            ret->buffer.buffer_val, flags) < 0)
1276 1277 1278
        goto cleanup;
    if (dom)
        virDomainFree(dom);
R
Richard W.M. Jones 已提交
1279

1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
1290 1291
}

1292
static int
1293 1294 1295 1296 1297 1298 1299
remoteDispatchDomainAttachDevice(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_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
1300
{
1301 1302
    virDomainPtr dom = NULL;
    int rv = -1;
1303

1304
    if (!conn) {
1305 1306
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1307 1308
    }

1309
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1310
        goto cleanup;
1311

1312
    if (virDomainAttachDevice(dom, args->xml) < 0)
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1323 1324
}

J
Jim Fehlig 已提交
1325
static int
1326 1327 1328 1329 1330 1331 1332
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)
J
Jim Fehlig 已提交
1333
{
1334 1335
    virDomainPtr dom = NULL;
    int rv = -1;
J
Jim Fehlig 已提交
1336

1337
    if (!conn) {
1338 1339
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1340 1341
    }

1342
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1343
        goto cleanup;
J
Jim Fehlig 已提交
1344

1345
    if (virDomainAttachDeviceFlags(dom, args->xml, args->flags) < 0)
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
J
Jim Fehlig 已提交
1356 1357
}

1358
static int
1359 1360 1361 1362 1363 1364 1365
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)
1366
{
1367 1368
    virDomainPtr dom = NULL;
    int rv = -1;
1369

1370
    if (!conn) {
1371 1372
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1373 1374
    }

1375
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1376
        goto cleanup;
1377

1378
    if (virDomainUpdateDeviceFlags(dom, args->xml, args->flags) < 0)
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1389 1390
}

1391
static int
1392 1393 1394 1395 1396 1397 1398
remoteDispatchDomainCreate(struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *rerr,
                           remote_domain_create_args *args,
                           void *ret ATTRIBUTE_UNUSED)
1399
{
1400 1401
    virDomainPtr dom = NULL;
    int rv = -1;
1402

1403
    if (!conn) {
1404 1405
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1406 1407
    }

1408
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1409
        goto cleanup;
1410

1411
    if (virDomainCreate(dom) < 0)
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1422 1423
}

1424
static int
1425 1426 1427 1428 1429 1430 1431
remoteDispatchDomainCreateWithFlags(struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
                                    remote_error *rerr,
                                    remote_domain_create_with_flags_args *args,
                                    remote_domain_create_with_flags_ret *ret)
1432
{
1433 1434
    int rv = -1;
    virDomainPtr dom = NULL;
1435

1436
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1437
        goto cleanup;
1438

1439
    if (virDomainCreateWithFlags(dom, args->flags) < 0)
1440
        goto cleanup;
1441

1442
    make_nonnull_domain(&ret->dom, dom);
1443 1444 1445 1446 1447 1448 1449 1450 1451

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1452 1453
}

1454
static int
1455 1456 1457 1458 1459 1460 1461
remoteDispatchDomainCreateXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *rerr,
                              remote_domain_create_xml_args *args,
                              remote_domain_create_xml_ret *ret)
1462
{
1463 1464
    virDomainPtr dom = NULL;
    int rv = -1;
1465

1466
    if (!conn) {
1467 1468
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1469 1470
    }

1471
    if (!(dom = virDomainCreateXML(conn, args->xml_desc, args->flags)))
1472
        goto cleanup;
1473

1474
    make_nonnull_domain(&ret->dom, dom);
1475

1476 1477 1478 1479 1480 1481 1482 1483
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1484 1485 1486
}

static int
1487 1488 1489 1490 1491 1492 1493
remoteDispatchDomainDefineXml(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_define_xml_args *args,
                              remote_domain_define_xml_ret *ret)
1494
{
1495 1496
    virDomainPtr dom = NULL;
    int rv = -1;
1497

1498
    if (!conn) {
1499 1500
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1501 1502
    }

1503
    if (!(dom = virDomainDefineXML(conn, args->xml)))
1504
        goto cleanup;
1505

1506
    make_nonnull_domain(&ret->dom, dom);
1507

1508 1509 1510 1511 1512 1513 1514 1515
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1516 1517 1518
}

static int
1519 1520 1521 1522 1523 1524 1525
remoteDispatchDomainDestroy(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_destroy_args *args,
                            void *ret ATTRIBUTE_UNUSED)
1526
{
1527 1528
    virDomainPtr dom = NULL;
    int rv = -1;
1529

1530
    if (!conn) {
1531 1532
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1533 1534
    }

1535
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1536
        goto cleanup;
1537

1538
    if (virDomainDestroy(dom) < 0)
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1549 1550 1551
}

static int
1552 1553 1554 1555 1556 1557 1558
remoteDispatchDomainDetachDevice(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_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
1559
{
1560 1561
    virDomainPtr dom = NULL;
    int rv = -1;
1562

1563
    if (!conn) {
1564 1565
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1566 1567
    }

1568
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1569
        goto cleanup;
1570

1571
    if (virDomainDetachDevice(dom, args->xml) < 0)
1572
        goto cleanup;
J
Jim Fehlig 已提交
1573

1574 1575 1576 1577 1578 1579 1580 1581
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
J
Jim Fehlig 已提交
1582 1583 1584
}

static int
1585 1586 1587 1588 1589 1590 1591
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)
J
Jim Fehlig 已提交
1592
{
1593 1594
    virDomainPtr dom = NULL;
    int rv = -1;
J
Jim Fehlig 已提交
1595

1596
    if (!conn) {
1597 1598
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1599 1600
    }

1601
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1602
        goto cleanup;
J
Jim Fehlig 已提交
1603

1604
    if (virDomainDetachDeviceFlags(dom, args->xml, args->flags) < 0)
1605
        goto cleanup;
1606

1607 1608 1609 1610 1611 1612 1613 1614
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1615 1616 1617
}

static int
1618 1619 1620 1621 1622 1623 1624
remoteDispatchDomainDumpXml(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_dump_xml_args *args,
                            remote_domain_dump_xml_ret *ret)
1625
{
1626 1627
    virDomainPtr dom = NULL;
    int rv = -1;
1628

1629
    if (!conn) {
1630 1631
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1632 1633
    }

1634
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1635
        goto cleanup;
1636 1637

    /* remoteDispatchClientRequest will free this. */
1638
    if (!(ret->xml = virDomainGetXMLDesc(dom, args->flags)))
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1649 1650
}

1651
static int
1652 1653 1654 1655 1656 1657 1658
remoteDispatchDomainXmlFromNative(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_xml_from_native_args *args,
                                  remote_domain_xml_from_native_ret *ret)
1659
{
1660 1661
    int rv = -1;

1662
    if (!conn) {
1663 1664
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1665 1666
    }

1667
    /* remoteDispatchClientRequest will free this. */
1668 1669 1670 1671
    if (!(ret->domainXml = virConnectDomainXMLFromNative(conn,
                                                         args->nativeFormat,
                                                         args->nativeConfig,
                                                         args->flags)))
1672
        goto cleanup;
1673

1674 1675 1676 1677 1678 1679
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
1680 1681 1682
}

static int
1683 1684 1685 1686 1687 1688 1689
remoteDispatchDomainXmlToNative(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_xml_to_native_args *args,
                                remote_domain_xml_to_native_ret *ret)
1690
{
1691 1692
    int rv = -1;

1693
    if (!conn) {
1694 1695
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1696 1697
    }

1698
    /* remoteDispatchClientRequest will free this. */
1699 1700 1701 1702
    if (!(ret->nativeConfig = virConnectDomainXMLToNative(conn,
                                                          args->nativeFormat,
                                                          args->domainXml,
                                                          args->flags)))
1703
        goto cleanup;
1704

1705 1706 1707 1708 1709 1710
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
1711 1712 1713
}


1714
static int
1715 1716 1717 1718 1719 1720 1721
remoteDispatchDomainGetAutostart(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_autostart_args *args,
                                 remote_domain_get_autostart_ret *ret)
1722
{
1723 1724
    virDomainPtr dom = NULL;
    int rv = -1;
1725

1726
    if (!conn) {
1727 1728
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1729 1730
    }

1731
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1732
        goto cleanup;
1733

1734
    if (virDomainGetAutostart(dom, &ret->autostart) < 0)
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1745 1746 1747
}

static int
1748 1749 1750 1751 1752 1753 1754
remoteDispatchDomainGetInfo(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_info_args *args,
                            remote_domain_get_info_ret *ret)
1755
{
1756
    virDomainPtr dom = NULL;
1757
    virDomainInfo info;
1758
    int rv = -1;
1759

1760
    if (!conn) {
1761 1762
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1763 1764
    }

1765
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1766
        goto cleanup;
1767

1768
    if (virDomainGetInfo(dom, &info) < 0)
1769
        goto cleanup;
1770 1771 1772 1773 1774 1775 1776

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

1777
    rv = 0;
1778

1779 1780 1781 1782 1783 1784
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1785 1786 1787
}

static int
1788 1789 1790 1791 1792 1793 1794
remoteDispatchDomainGetMaxMemory(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_max_memory_args *args,
                                 remote_domain_get_max_memory_ret *ret)
1795
{
1796 1797
    virDomainPtr dom = NULL;
    int rv = -1;
1798

1799
    if (!conn) {
1800 1801
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1802 1803
    }

1804
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1805
        goto cleanup;
1806

1807
    if ((ret->memory = virDomainGetMaxMemory(dom)) == 0)
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1818 1819 1820
}

static int
1821 1822 1823 1824 1825 1826 1827
remoteDispatchDomainGetMaxVcpus(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_max_vcpus_args *args,
                                remote_domain_get_max_vcpus_ret *ret)
1828
{
1829 1830
    virDomainPtr dom = NULL;
    int rv = -1;
1831

1832
    if (!conn) {
1833 1834
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1835 1836
    }

1837
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1838
        goto cleanup;
1839

1840
    if ((ret->num = virDomainGetMaxVcpus(dom)) < 0)
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1851 1852
}

1853 1854 1855 1856
static int
remoteDispatchDomainGetSecurityLabel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1857
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
1858 1859 1860 1861
                                     remote_error *rerr,
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
{
1862 1863 1864
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
    int rv = -1;
1865

1866
    if (!conn) {
1867 1868
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1869 1870
    }

1871
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1872
        goto cleanup;
1873

1874
    if (VIR_ALLOC(seclabel) < 0) {
1875 1876
        virReportOOMError();
        goto cleanup;
1877 1878
    }

1879
    if (virDomainGetSecurityLabel(dom, seclabel) < 0)
1880
        goto cleanup;
1881

1882
    ret->label.label_len = strlen(seclabel->label) + 1;
1883
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0) {
1884 1885
        virReportOOMError();
        goto cleanup;
1886
    }
1887 1888
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1889

1890 1891 1892 1893 1894 1895 1896 1897 1898
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
    return rv;
1899 1900 1901 1902 1903 1904
}

static int
remoteDispatchNodeGetSecurityModel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1905
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1906 1907 1908 1909 1910
                                   remote_error *rerr,
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_node_get_security_model_ret *ret)
{
    virSecurityModel secmodel;
1911
    int rv = -1;
1912

1913
    if (!conn) {
1914 1915
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1916 1917
    }

1918
    memset(&secmodel, 0, sizeof secmodel);
1919
    if (virNodeGetSecurityModel(conn, &secmodel) < 0)
1920
        goto cleanup;
1921 1922 1923

    ret->model.model_len = strlen(secmodel.model) + 1;
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0) {
1924 1925
        virReportOOMError();
        goto cleanup;
1926 1927 1928 1929 1930
    }
    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) {
1931 1932
        virReportOOMError();
        goto cleanup;
1933 1934 1935
    }
    strcpy(ret->doi.doi_val, secmodel.doi);

1936 1937 1938 1939 1940 1941
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
1942 1943
}

1944
static int
1945 1946 1947 1948 1949 1950 1951
remoteDispatchDomainGetOsType(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_os_type_args *args,
                              remote_domain_get_os_type_ret *ret)
1952
{
1953 1954
    virDomainPtr dom = NULL;
    int rv = -1;
1955

1956
    if (!conn) {
1957 1958
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1959 1960
    }

1961
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1962
        goto cleanup;
1963 1964

    /* remoteDispatchClientRequest will free this */
1965
    if (!(ret->type = virDomainGetOSType(dom)))
1966
        goto cleanup;
1967

1968 1969 1970 1971 1972 1973 1974 1975
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
1976 1977 1978
}

static int
1979 1980 1981 1982 1983 1984 1985
remoteDispatchDomainGetVcpus(struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                             remote_error *rerr,
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
1986
{
1987 1988 1989
    virDomainPtr dom = NULL;
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
1990
    int info_len, i;
1991
    int rv = -1;
1992

1993
    if (!conn) {
1994 1995
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1996 1997
    }

1998
    if (!(dom = get_nonnull_domain(conn, args->dom)))
1999
        goto cleanup;
2000 2001

    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
2002 2003
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
        goto cleanup;
2004 2005
    }

2006
    if (args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
2007 2008
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
        goto cleanup;
2009 2010 2011
    }

    /* Allocate buffers to take the results. */
2012
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
2013
        goto no_memory;
2014 2015
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
2016
        goto no_memory;
2017

2018 2019 2020
    if ((info_len = virDomainGetVcpus(dom,
                                      info, args->maxinfo,
                                      cpumaps, args->maplen)) < 0)
2021
        goto cleanup;
2022 2023 2024

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
2025
    if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
2026
        goto no_memory;
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038

    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.
     */
2039
    ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
2040
    ret->cpumaps.cpumaps_val = (char *) cpumaps;
2041
    cpumaps = NULL;
2042

2043
    rv = 0;
2044

2045 2046 2047 2048 2049
cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->info.info_val);
    }
2050
    VIR_FREE(cpumaps);
2051 2052 2053 2054 2055 2056 2057 2058
    VIR_FREE(info);
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
2059 2060
}

E
Eric Blake 已提交
2061
static int
2062 2063 2064 2065 2066 2067 2068
remoteDispatchDomainGetVcpusFlags(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_domain_get_vcpus_flags_args *args,
                                  remote_domain_get_vcpus_flags_ret *ret)
E
Eric Blake 已提交
2069
{
2070 2071
    virDomainPtr dom = NULL;
    int rv = -1;
E
Eric Blake 已提交
2072

2073
    if (!conn) {
2074 2075
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2076 2077
    }

2078
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2079
        goto cleanup;
E
Eric Blake 已提交
2080

2081
    if ((ret->num = virDomainGetVcpusFlags(dom, args->flags)) < 0)
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
E
Eric Blake 已提交
2092 2093
}

2094
static int
2095 2096 2097 2098 2099 2100 2101
remoteDispatchDomainMigratePrepare(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_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
2102 2103 2104 2105 2106 2107
{
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2108
    int rv = -1;
2109

2110
    if (!conn) {
2111 2112
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2113 2114
    }

2115 2116 2117 2118
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
2119
    if (VIR_ALLOC(uri_out) < 0) {
2120 2121
        virReportOOMError();
        goto cleanup;
2122
    }
2123

2124
    if (virDomainMigratePrepare(conn, &cookie, &cookielen,
2125
                                uri_in, uri_out,
2126
                                args->flags, dname, args->resource) < 0)
2127
        goto cleanup;
2128 2129 2130 2131 2132 2133

    /* 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 已提交
2134 2135 2136 2137
    if (*uri_out == NULL) {
        ret->uri_out = NULL;
    } else {
        ret->uri_out = uri_out;
2138
        uri_out = NULL;
D
Daniel P. Berrange 已提交
2139
    }
2140

2141 2142 2143 2144 2145 2146 2147
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    VIR_FREE(uri_out);
    return rv;
2148 2149 2150
}

static int
2151 2152 2153 2154 2155 2156 2157
remoteDispatchDomainMigratePerform(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_perform_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
2158
{
2159
    virDomainPtr dom = NULL;
2160
    char *dname;
2161
    int rv = -1;
2162

2163
    if (!conn) {
2164 2165
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2166 2167
    }

2168
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2169
        goto cleanup;
2170 2171 2172

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

2173
    if (virDomainMigratePerform(dom,
2174 2175 2176
                                args->cookie.cookie_val,
                                args->cookie.cookie_len,
                                args->uri,
2177
                                args->flags, dname, args->resource) < 0)
2178
        goto cleanup;
2179

2180 2181 2182 2183 2184 2185 2186 2187
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2188 2189 2190
}

static int
2191 2192 2193 2194 2195 2196 2197
remoteDispatchDomainMigrateFinish(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_finish_args *args,
                                  remote_domain_migrate_finish_ret *ret)
2198
{
2199
    virDomainPtr dom = NULL;
2200
    int rv = -1;
2201 2202

    if (!conn) {
2203 2204
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2205
    }
2206

2207 2208 2209 2210 2211
    if (!(dom = virDomainMigrateFinish(conn, args->dname,
                                       args->cookie.cookie_val,
                                       args->cookie.cookie_len,
                                       args->uri,
                                       args->flags)))
2212
        goto cleanup;
2213

2214
    make_nonnull_domain(&ret->ddom, dom);
2215 2216 2217 2218 2219
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
2220 2221
    if (dom)
        virDomainFree(dom);
2222
    return rv;
2223 2224
}

D
Daniel Veillard 已提交
2225
static int
2226 2227 2228 2229 2230 2231 2232
remoteDispatchDomainMigratePrepare2(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_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
D
Daniel Veillard 已提交
2233 2234 2235 2236 2237 2238
{
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2239
    int rv = -1;
2240 2241

    if (!conn) {
2242 2243
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2244
    }
D
Daniel Veillard 已提交
2245 2246 2247 2248 2249 2250

    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) {
2251 2252
        virReportOOMError();
        goto cleanup;
D
Daniel Veillard 已提交
2253 2254
    }

2255
    if (virDomainMigratePrepare2(conn, &cookie, &cookielen,
2256 2257
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
2258
                                 args->dom_xml) < 0)
2259
        goto cleanup;
D
Daniel Veillard 已提交
2260 2261 2262 2263 2264 2265 2266 2267

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

2268 2269 2270 2271 2272 2273
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
D
Daniel Veillard 已提交
2274 2275 2276
}

static int
2277 2278 2279 2280 2281 2282 2283
remoteDispatchDomainMigrateFinish2(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_finish2_args *args,
                                   remote_domain_migrate_finish2_ret *ret)
D
Daniel Veillard 已提交
2284
{
2285
    virDomainPtr dom = NULL;
2286
    int rv = -1;
2287 2288

    if (!conn) {
2289 2290
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2291
    }
D
Daniel Veillard 已提交
2292

2293 2294 2295 2296 2297 2298
    if (!(dom = virDomainMigrateFinish2(conn, args->dname,
                                        args->cookie.cookie_val,
                                        args->cookie.cookie_len,
                                        args->uri,
                                        args->flags,
                                        args->retcode)))
2299
        goto cleanup;
D
Daniel Veillard 已提交
2300

2301
    make_nonnull_domain(&ret->ddom, dom);
D
Daniel Veillard 已提交
2302

2303 2304 2305 2306 2307
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
2308 2309
    if (dom)
        virDomainFree(dom);
2310
    return rv;
D
Daniel Veillard 已提交
2311 2312
}

C
Chris Lalancette 已提交
2313 2314 2315 2316 2317 2318 2319 2320 2321 2322
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)
{
    char *dname;
2323 2324
    struct qemud_client_stream *stream = NULL;
    int rv = -1;
2325 2326

    if (!conn) {
2327 2328
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2329
    }
C
Chris Lalancette 已提交
2330 2331 2332

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

2333
    if (!(stream = remoteCreateClientStream(conn, hdr))) {
2334 2335
        virReportOOMError();
        goto cleanup;
C
Chris Lalancette 已提交
2336 2337
    }

2338
    if (virDomainMigratePrepareTunnel(conn, stream->st,
C
Chris Lalancette 已提交
2339
                                      args->flags, dname, args->resource,
2340
                                      args->dom_xml) < 0)
2341
        goto cleanup;
C
Chris Lalancette 已提交
2342

2343
    if (remoteAddClientStream(client, stream, 0) < 0)
2344
        goto cleanup;
C
Chris Lalancette 已提交
2345

2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        if (stream) {
            virStreamAbort(stream->st);
            remoteFreeClientStream(client, stream);
        }
    }
    return rv;
C
Chris Lalancette 已提交
2357 2358
}

2359
static int
2360 2361 2362 2363 2364 2365 2366
remoteDispatchListDefinedDomains(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_defined_domains_args *args,
                                 remote_list_defined_domains_ret *ret)
2367
{
2368
    int rv = -1;
2369
    int len;
2370

2371
    if (!conn) {
2372 2373
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2374
    }
2375 2376

    if (args->maxnames > REMOTE_DOMAIN_NAME_LIST_MAX) {
2377 2378 2379
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxnames > REMOTE_DOMAIN_NAME_LIST_MAX"));
        goto cleanup;
2380 2381 2382
    }

    /* Allocate return buffer. */
2383
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
2384 2385
        virReportOOMError();
        goto cleanup;
2386
    }
2387

2388 2389
    len = virConnectListDefinedDomains(conn,
                                       ret->names.names_val, args->maxnames);
2390
    if (len < 0)
2391
        goto cleanup;
2392
    ret->names.names_len = len;
2393

2394 2395 2396 2397 2398 2399 2400 2401
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->names.names_val);
    }
    return rv;
2402 2403 2404
}

static int
2405 2406 2407 2408 2409 2410 2411
remoteDispatchDomainLookupById(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_lookup_by_id_args *args,
                               remote_domain_lookup_by_id_ret *ret)
2412
{
2413 2414 2415
    virDomainPtr dom = NULL;
    int rv = -1;

2416
    if (!conn) {
2417 2418
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2419 2420
    }

2421
    if (!(dom = virDomainLookupByID(conn, args->id)))
2422
        goto cleanup;
2423

2424
    make_nonnull_domain(&ret->dom, dom);
2425 2426 2427 2428 2429 2430 2431 2432 2433

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2434 2435 2436
}

static int
2437 2438 2439 2440 2441 2442 2443
remoteDispatchDomainLookupByName(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_lookup_by_name_args *args,
                                 remote_domain_lookup_by_name_ret *ret)
2444
{
2445 2446
    virDomainPtr dom = NULL;
    int rv = -1;
2447

2448
    if (!conn) {
2449 2450
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2451 2452
    }

2453
    if (!(dom = virDomainLookupByName(conn, args->name)))
2454
        goto cleanup;
2455

2456
    make_nonnull_domain(&ret->dom, dom);
2457 2458 2459 2460 2461 2462 2463 2464 2465

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2466 2467 2468
}

static int
2469 2470 2471 2472 2473 2474 2475
remoteDispatchDomainLookupByUuid(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_lookup_by_uuid_args *args,
                                 remote_domain_lookup_by_uuid_ret *ret)
2476
{
2477 2478
    virDomainPtr dom = NULL;
    int rv = -1;
2479

2480
    if (!conn) {
2481 2482
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2483 2484
    }

2485
    if (!(dom = virDomainLookupByUUID(conn, (unsigned char *) args->uuid)))
2486
        goto cleanup;
2487

2488
    make_nonnull_domain(&ret->dom, dom);
2489 2490 2491 2492 2493 2494 2495 2496 2497

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2498 2499 2500
}

static int
2501 2502 2503 2504 2505 2506 2507
remoteDispatchNumOfDefinedDomains(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_defined_domains_ret *ret)
2508
{
2509 2510
    int rv = -1;

2511
    if (!conn) {
2512 2513
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2514
    }
2515

2516
    if ((ret->num = virConnectNumOfDefinedDomains(conn)) < 0)
2517
        goto cleanup;
2518

2519 2520 2521 2522 2523 2524
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
2525 2526 2527
}

static int
2528 2529 2530 2531 2532 2533 2534
remoteDispatchDomainPinVcpu(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_pin_vcpu_args *args,
                            void *ret ATTRIBUTE_UNUSED)
2535
{
2536 2537
    virDomainPtr dom = NULL;
    int rv = -1;
2538

2539
    if (!conn) {
2540 2541
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2542 2543
    }

2544
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2545
        goto cleanup;
2546 2547

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
2548 2549
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("cpumap_len > REMOTE_CPUMAP_MAX"));
        goto cleanup;
2550 2551
    }

2552 2553 2554
    if (virDomainPinVcpu(dom, args->vcpu,
                         (unsigned char *) args->cpumap.cpumap_val,
                         args->cpumap.cpumap_len) < 0)
2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2565 2566 2567
}

static int
2568 2569 2570 2571 2572 2573 2574
remoteDispatchDomainReboot(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_reboot_args *args,
                           void *ret ATTRIBUTE_UNUSED)
2575
{
2576 2577
    virDomainPtr dom = NULL;
    int rv = -1;
2578

2579
    if (!conn) {
2580 2581
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2582 2583
    }

2584
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2585
        goto cleanup;
2586

2587
    if (virDomainReboot(dom, args->flags) < 0)
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2598 2599 2600
}

static int
2601 2602 2603 2604 2605 2606 2607
remoteDispatchDomainRestore(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_restore_args *args,
                            void *ret ATTRIBUTE_UNUSED)
2608
{
2609 2610
    int rv = -1;

2611
    if (!conn) {
2612 2613
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2614
    }
2615

2616
    if (virDomainRestore(conn, args->from) < 0)
2617
        goto cleanup;
2618

2619 2620 2621 2622 2623 2624
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
2625 2626 2627
}

static int
2628 2629 2630 2631 2632 2633 2634
remoteDispatchDomainResume(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_resume_args *args,
                           void *ret ATTRIBUTE_UNUSED)
2635
{
2636 2637
    virDomainPtr dom = NULL;
    int rv = -1;
2638

2639
    if (!conn) {
2640 2641
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2642 2643
    }

2644
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2645
        goto cleanup;
2646

2647
    if (virDomainResume(dom) < 0)
2648 2649 2650 2651 2652 2653 2654 2655 2656 2657
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2658 2659 2660
}

static int
2661 2662 2663 2664 2665 2666 2667
remoteDispatchDomainSave(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_save_args *args,
                         void *ret ATTRIBUTE_UNUSED)
2668
{
2669 2670
    virDomainPtr dom = NULL;
    int rv = -1;
2671

2672
    if (!conn) {
2673 2674
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2675 2676
    }

2677
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2678
        goto cleanup;
2679

2680
    if (virDomainSave(dom, args->to) < 0)
2681 2682 2683 2684 2685 2686 2687 2688 2689 2690
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2691 2692 2693
}

static int
2694 2695 2696 2697 2698 2699 2700
remoteDispatchDomainCoreDump(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_core_dump_args *args,
                             void *ret ATTRIBUTE_UNUSED)
2701
{
2702 2703
    virDomainPtr dom = NULL;
    int rv = -1;
2704

2705
    if (!conn) {
2706 2707
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2708 2709
    }

2710
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2711
        goto cleanup;
2712

2713
    if (virDomainCoreDump(dom, args->to, args->flags) < 0)
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2724 2725 2726
}

static int
2727 2728 2729 2730 2731 2732 2733
remoteDispatchDomainSetAutostart(struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_domain_set_autostart_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
2734
{
2735 2736
    virDomainPtr dom = NULL;
    int rv = -1;
2737

2738
    if (!conn) {
2739 2740
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2741 2742
    }

2743
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2744
        goto cleanup;
2745

2746
    if (virDomainSetAutostart(dom, args->autostart) < 0)
2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2757 2758 2759
}

static int
2760 2761 2762 2763 2764 2765 2766
remoteDispatchDomainSetMaxMemory(struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_domain_set_max_memory_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
2767
{
2768 2769
    virDomainPtr dom = NULL;
    int rv = -1;
2770

2771
    if (!conn) {
2772 2773
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2774 2775
    }

2776
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2777
        goto cleanup;
2778

2779
    if (virDomainSetMaxMemory(dom, args->memory) < 0)
2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2790 2791 2792
}

static int
2793 2794 2795 2796 2797 2798 2799
remoteDispatchDomainSetMemory(struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *rerr,
                              remote_domain_set_memory_args *args,
                              void *ret ATTRIBUTE_UNUSED)
2800
{
2801 2802
    virDomainPtr dom = NULL;
    int rv = -1;
2803

2804
    if (!conn) {
2805 2806
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2807 2808
    }

2809
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2810
        goto cleanup;
2811

2812
    if (virDomainSetMemory(dom, args->memory) < 0)
2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2823 2824
}

2825
static int
2826 2827 2828 2829 2830 2831 2832
remoteDispatchDomainSetMemoryFlags(struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                                   remote_error *rerr,
                                   remote_domain_set_memory_flags_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
2833
{
2834 2835
    virDomainPtr dom = NULL;
    int rv = -1;
2836

2837
    if (!conn) {
2838 2839
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2840 2841
    }

2842
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2843
        goto cleanup;
2844

2845
    if (virDomainSetMemoryFlags(dom, args->memory, args->flags) < 0)
2846 2847 2848 2849 2850 2851 2852 2853 2854 2855
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
2856 2857
}

2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869
static int
remoteDispatchDomainSetMemoryParameters(struct qemud_server *server
                                        ATTRIBUTE_UNUSED,
                                        struct qemud_client *client
                                        ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *
                                        hdr ATTRIBUTE_UNUSED,
                                        remote_error * rerr,
                                        remote_domain_set_memory_parameters_args
                                        * args, void *ret ATTRIBUTE_UNUSED)
{
2870 2871
    virDomainPtr dom = NULL;
    virMemoryParameterPtr params = NULL;
2872
    int i, nparams;
2873
    unsigned int flags;
2874
    int rv = -1;
2875

2876
    if (!conn) {
2877 2878
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2879 2880
    }

2881 2882 2883 2884
    nparams = args->params.params_len;
    flags = args->flags;

    if (nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2885 2886
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
2887 2888
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
2889 2890
        virReportOOMError();
        goto cleanup;
2891 2892 2893 2894 2895 2896
    }

    /* Deserialise parameters. */
    for (i = 0; i < nparams; ++i) {
        if (virStrcpyStatic
            (params[i].field, args->params.params_val[i].field) == NULL) {
2897 2898 2899 2900
            virNetError(VIR_ERR_INTERNAL_ERROR,
                        _("Field %s too big for destination"),
                        args->params.params_val[i].field);
            goto cleanup;
2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936
        }
        params[i].type = args->params.params_val[i].value.type;
        switch (params[i].type) {
            case VIR_DOMAIN_MEMORY_PARAM_INT:
                params[i].value.i =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.i;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_UINT:
                params[i].value.ui =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.ui;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_LLONG:
                params[i].value.l =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.l;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_ULLONG:
                params[i].value.ul =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.ul;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_DOUBLE:
                params[i].value.d =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.d;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_BOOLEAN:
                params[i].value.b =
                    args->params.params_val[i].value.
                    remote_memory_param_value_u.b;
                break;
        }
    }

2937
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2938
        goto cleanup;
2939

2940
    if (virDomainSetMemoryParameters(dom, params, nparams, flags) < 0)
2941
        goto cleanup;
2942

2943 2944 2945 2946 2947 2948 2949 2950 2951
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    VIR_FREE(params);
    if (dom)
        virDomainFree(dom);
    return rv;
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967
}

static int
remoteDispatchDomainGetMemoryParameters(struct qemud_server *server
                                        ATTRIBUTE_UNUSED,
                                        struct qemud_client *client
                                        ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *
                                        hdr ATTRIBUTE_UNUSED,
                                        remote_error * rerr,
                                        remote_domain_get_memory_parameters_args
                                        * args,
                                        remote_domain_get_memory_parameters_ret
                                        * ret)
{
2968 2969
    virDomainPtr dom = NULL;
    virMemoryParameterPtr params = NULL;
2970
    int i, nparams;
2971
    unsigned int flags;
2972
    int rv = -1;
2973

2974
    if (!conn) {
2975 2976
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2977 2978
    }

2979 2980 2981 2982
    nparams = args->nparams;
    flags = args->flags;

    if (nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2983 2984
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
2985 2986
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
2987 2988
        virReportOOMError();
        goto cleanup;
2989 2990
    }

2991
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2992
        goto cleanup;
2993

2994
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
2995
        goto cleanup;
2996

2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

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

    for (i = 0; i < nparams; ++i) {
3011
        /* remoteDispatchClientRequest will free this: */
3012 3013
        ret->params.params_val[i].field = strdup(params[i].field);
        if (ret->params.params_val[i].field == NULL)
3014
            goto no_memory;
3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046

        ret->params.params_val[i].value.type = params[i].type;
        switch (params[i].type) {
            case VIR_DOMAIN_MEMORY_PARAM_INT:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.i =
                    params[i].value.i;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_UINT:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.ui =
                    params[i].value.ui;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_LLONG:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.l =
                    params[i].value.l;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_ULLONG:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.ul =
                    params[i].value.ul;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_DOUBLE:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.d =
                    params[i].value.d;
                break;
            case VIR_DOMAIN_MEMORY_PARAM_BOOLEAN:
                ret->params.params_val[i].
                    value.remote_memory_param_value_u.b =
                    params[i].value.b;
3047 3048
                break;
            default:
3049
                virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("unknown type"));
3050 3051 3052 3053
                goto cleanup;
        }
    }

3054 3055
success:
    rv = 0;
3056

3057 3058 3059 3060 3061 3062 3063 3064 3065
cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        for (i = 0; i < nparams; i++)
            VIR_FREE(ret->params.params_val[i].field);
        VIR_FREE(ret->params.params_val);
    }
    if (dom)
        virDomainFree(dom);
3066
    VIR_FREE(params);
3067 3068 3069 3070 3071
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085
}

static int
remoteDispatchDomainSetBlkioParameters(struct qemud_server *server
                                        ATTRIBUTE_UNUSED,
                                        struct qemud_client *client
                                        ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *
                                        hdr ATTRIBUTE_UNUSED,
                                        remote_error * rerr,
                                        remote_domain_set_blkio_parameters_args
                                        * args, void *ret ATTRIBUTE_UNUSED)
{
3086 3087
    virDomainPtr dom = NULL;
    virBlkioParameterPtr params = NULL;
3088
    int i, nparams;
3089
    unsigned int flags;
3090
    int rv = -1;
3091

3092
    if (!conn) {
3093 3094
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3095 3096
    }

3097 3098 3099 3100
    nparams = args->params.params_len;
    flags = args->flags;

    if (nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
3101 3102
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
3103 3104
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
3105 3106
        virReportOOMError();
        goto cleanup;
3107 3108 3109 3110 3111 3112
    }

    /* Deserialise parameters. */
    for (i = 0; i < nparams; ++i) {
        if (virStrcpyStatic
            (params[i].field, args->params.params_val[i].field) == NULL) {
3113 3114 3115 3116
            virNetError(VIR_ERR_INTERNAL_ERROR,
                        _("Field %s too big for destination"),
                        args->params.params_val[i].field);
            goto cleanup;
3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152
        }
        params[i].type = args->params.params_val[i].value.type;
        switch (params[i].type) {
            case VIR_DOMAIN_BLKIO_PARAM_INT:
                params[i].value.i =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.i;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_UINT:
                params[i].value.ui =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.ui;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_LLONG:
                params[i].value.l =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.l;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_ULLONG:
                params[i].value.ul =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.ul;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_DOUBLE:
                params[i].value.d =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.d;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_BOOLEAN:
                params[i].value.b =
                    args->params.params_val[i].value.
                    remote_blkio_param_value_u.b;
                break;
        }
    }

3153
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3154
        goto cleanup;
3155

3156
    if (virDomainSetBlkioParameters(dom, params, nparams, flags) < 0)
3157
        goto cleanup;
3158

3159 3160 3161 3162 3163 3164 3165 3166 3167
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    VIR_FREE(params);
    if (dom)
        virDomainFree(dom);
    return rv;
3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
}

static int
remoteDispatchDomainGetBlkioParameters(struct qemud_server *server
                                        ATTRIBUTE_UNUSED,
                                        struct qemud_client *client
                                        ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *
                                        hdr ATTRIBUTE_UNUSED,
                                        remote_error * rerr,
                                        remote_domain_get_blkio_parameters_args
                                        * args,
                                        remote_domain_get_blkio_parameters_ret
                                        * ret)
{
3184 3185
    virDomainPtr dom = NULL;
    virBlkioParameterPtr params = NULL;
3186
    int i, nparams;
3187
    unsigned int flags;
3188
    int rv = -1;
3189

3190
    if (!conn) {
3191 3192
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3193 3194
    }

3195 3196 3197 3198
    nparams = args->nparams;
    flags = args->flags;

    if (nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
3199 3200
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
3201 3202
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
3203 3204
        virReportOOMError();
        goto cleanup;
3205 3206
    }

3207
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3208
        goto cleanup;
3209

3210
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
3211
        goto cleanup;
3212

3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

    /* Serialise the blkio parameters. */
    ret->params.params_len = nparams;
    if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
3224
        goto no_memory;
3225 3226 3227 3228 3229

    for (i = 0; i < nparams; ++i) {
        // remoteDispatchClientRequest will free this:
        ret->params.params_val[i].field = strdup(params[i].field);
        if (ret->params.params_val[i].field == NULL)
3230
            goto no_memory;
3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262

        ret->params.params_val[i].value.type = params[i].type;
        switch (params[i].type) {
            case VIR_DOMAIN_BLKIO_PARAM_INT:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.i =
                    params[i].value.i;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_UINT:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.ui =
                    params[i].value.ui;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_LLONG:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.l =
                    params[i].value.l;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_ULLONG:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.ul =
                    params[i].value.ul;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_DOUBLE:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.d =
                    params[i].value.d;
                break;
            case VIR_DOMAIN_BLKIO_PARAM_BOOLEAN:
                ret->params.params_val[i].
                    value.remote_blkio_param_value_u.b =
                    params[i].value.b;
3263 3264
                break;
            default:
3265
                virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("unknown type"));
3266 3267 3268 3269
                goto cleanup;
        }
    }

3270 3271
success:
    rv = 0;
3272

3273 3274 3275 3276 3277 3278 3279
cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        for (i = 0; i < nparams; i++)
            VIR_FREE(ret->params.params_val[i].field);
        VIR_FREE(ret->params.params_val);
    }
3280
    VIR_FREE(params);
3281 3282 3283 3284 3285 3286 3287
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
3288 3289
}

3290
static int
3291 3292 3293 3294 3295 3296 3297
remoteDispatchDomainSetVcpus(struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                             remote_error *rerr,
                             remote_domain_set_vcpus_args *args,
                             void *ret ATTRIBUTE_UNUSED)
3298
{
3299 3300
    virDomainPtr dom = NULL;
    int rv = -1;
3301

3302
    if (!conn) {
3303 3304
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3305 3306
    }

3307
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3308
        goto cleanup;
3309

3310
    if (virDomainSetVcpus(dom, args->nvcpus) < 0)
3311 3312 3313 3314 3315 3316 3317 3318 3319 3320
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
3321 3322
}

E
Eric Blake 已提交
3323
static int
3324 3325 3326 3327 3328 3329 3330
remoteDispatchDomainSetVcpusFlags(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_domain_set_vcpus_flags_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
E
Eric Blake 已提交
3331
{
3332 3333
    virDomainPtr dom = NULL;
    int rv = -1;
E
Eric Blake 已提交
3334

3335
    if (!conn) {
3336 3337
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3338 3339
    }

3340
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3341
        goto cleanup;
E
Eric Blake 已提交
3342

3343
    if (virDomainSetVcpusFlags(dom, args->nvcpus, args->flags) < 0)
3344 3345 3346 3347 3348 3349 3350 3351 3352 3353
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
E
Eric Blake 已提交
3354 3355
}

3356
static int
3357 3358 3359 3360 3361 3362 3363
remoteDispatchDomainShutdown(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_shutdown_args *args,
                             void *ret ATTRIBUTE_UNUSED)
3364
{
3365 3366
    virDomainPtr dom = NULL;
    int rv = -1;
3367

3368
    if (!conn) {
3369 3370
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3371 3372
    }

3373
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3374
        goto cleanup;
3375

3376
    if (virDomainShutdown(dom) < 0)
3377 3378 3379 3380 3381 3382 3383 3384 3385 3386
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
3387 3388 3389
}

static int
3390 3391 3392 3393 3394 3395 3396
remoteDispatchDomainSuspend(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_suspend_args *args,
                            void *ret ATTRIBUTE_UNUSED)
3397
{
3398 3399
    virDomainPtr dom = NULL;
    int rv = -1;
3400

3401
    if (!conn) {
3402 3403
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3404 3405
    }

3406
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3407
        goto cleanup;
3408

3409
    if (virDomainSuspend(dom) < 0)
3410 3411 3412 3413 3414 3415 3416 3417 3418 3419
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
3420 3421 3422
}

static int
3423 3424 3425 3426 3427 3428 3429
remoteDispatchDomainUndefine(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_undefine_args *args,
                             void *ret ATTRIBUTE_UNUSED)
3430
{
3431 3432
    virDomainPtr dom = NULL;
    int rv = -1;
3433

3434
    if (!conn) {
3435 3436
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3437 3438
    }

3439
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3440
        goto cleanup;
3441

3442
    if (virDomainUndefine(dom) < 0)
3443 3444 3445 3446 3447 3448 3449 3450 3451 3452
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
3453 3454 3455
}

static int
3456 3457 3458 3459 3460 3461 3462
remoteDispatchListDefinedNetworks(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_defined_networks_args *args,
                                  remote_list_defined_networks_ret *ret)
3463
{
3464
    int rv = -1;
3465
    int len;
3466

3467
    if (!conn) {
3468 3469
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3470
    }
3471 3472

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
3473 3474 3475
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        goto cleanup;
3476 3477 3478
    }

    /* Allocate return buffer. */
3479
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
3480 3481
        virReportOOMError();
        goto cleanup;
3482
    }
3483

3484
    len = virConnectListDefinedNetworks(conn,
3485
                                      ret->names.names_val, args->maxnames);
3486
    if (len < 0)
3487
        goto cleanup;
3488
    ret->names.names_len = len;
3489

3490 3491 3492 3493 3494 3495 3496 3497
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->names.names_val);
    }
    return rv;
3498 3499 3500
}

static int
3501 3502 3503 3504 3505 3506 3507
remoteDispatchListDomains(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_domains_args *args,
                          remote_list_domains_ret *ret)
3508
{
3509
    int rv = -1;
3510
    int len;
3511

3512
    if (!conn) {
3513 3514
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3515
    }
3516 3517

    if (args->maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
3518 3519 3520
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxids > REMOTE_DOMAIN_ID_LIST_MAX"));
        goto cleanup;
3521 3522 3523
    }

    /* Allocate return buffer. */
3524
    if (VIR_ALLOC_N(ret->ids.ids_val, args->maxids) < 0) {
3525 3526
        virReportOOMError();
        goto cleanup;
3527
    }
3528

3529 3530
    len = virConnectListDomains(conn,
                                ret->ids.ids_val, args->maxids);
3531
    if (len < 0)
3532
        goto cleanup;
3533
    ret->ids.ids_len = len;
3534

3535 3536 3537 3538 3539 3540 3541 3542
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->ids.ids_val);
    }
    return rv;
3543 3544
}

3545
static int
3546 3547 3548 3549 3550 3551 3552
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)
3553
{
3554 3555
    virDomainPtr dom = NULL;
    int rv = -1;
3556

3557
    if (!conn) {
3558 3559
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3560 3561
    }

3562
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3563
        goto cleanup;
3564

3565
    if (virDomainManagedSave(dom, args->flags) < 0)
3566 3567 3568 3569 3570 3571 3572 3573 3574 3575
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
3576 3577 3578
}

static int
3579 3580 3581 3582 3583 3584 3585
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)
3586
{
3587 3588
    virDomainPtr dom = NULL;
    int rv = -1;
3589

3590
    if (!conn) {
3591 3592
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3593 3594
    }

3595
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3596
        goto cleanup;
3597

3598
    if ((ret->ret = virDomainHasManagedSaveImage(dom, args->flags)) < 0)
3599 3600 3601 3602 3603 3604 3605 3606 3607 3608
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
3609 3610 3611
}

static int
3612 3613 3614 3615 3616 3617 3618
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)
3619
{
3620 3621
    virDomainPtr dom = NULL;
    int rv = -1;
3622

3623
    if (!conn) {
3624 3625
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3626 3627
    }

3628
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3629
        goto cleanup;
3630

3631
    if (virDomainManagedSaveRemove(dom, args->flags) < 0)
3632 3633 3634 3635 3636 3637 3638 3639 3640 3641
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
3642 3643
}

3644
static int
3645 3646 3647 3648 3649 3650 3651
remoteDispatchListNetworks(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_networks_args *args,
                           remote_list_networks_ret *ret)
3652
{
3653
    int rv = -1;
3654
    int len;
3655

3656
    if (!conn) {
3657 3658
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3659
    }
3660 3661

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
3662 3663 3664
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        goto cleanup;
3665 3666 3667
    }

    /* Allocate return buffer. */
3668
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
3669 3670
        virReportOOMError();
        goto cleanup;
3671
    }
3672

3673 3674
    len = virConnectListNetworks(conn,
                                 ret->names.names_val, args->maxnames);
3675
    if (len < 0)
3676
        goto cleanup;
3677
    ret->names.names_len = len;
3678

3679 3680 3681 3682 3683
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
3684
        VIR_FREE(ret->names.names_val);
3685 3686
    }
    return rv;
3687 3688 3689
}

static int
3690 3691 3692 3693 3694 3695 3696
remoteDispatchNetworkCreate(struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *rerr,
                            remote_network_create_args *args,
                            void *ret ATTRIBUTE_UNUSED)
3697
{
3698 3699
    virNetworkPtr net = NULL;
    int rv = -1;
3700

3701
    if (!conn) {
3702 3703
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3704 3705
    }

3706
    if (!(net = get_nonnull_network(conn, args->net)))
3707
        goto cleanup;
3708

3709
    if (virNetworkCreate(net) < 0)
3710 3711 3712 3713 3714 3715 3716 3717 3718 3719
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (net)
        virNetworkFree(net);
    return rv;
3720 3721 3722
}

static int
3723 3724 3725 3726 3727 3728 3729
remoteDispatchNetworkCreateXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
                               remote_error *rerr,
                               remote_network_create_xml_args *args,
                               remote_network_create_xml_ret *ret)
3730
{
3731 3732
    virNetworkPtr net = NULL;
    int rv = -1;
3733

3734
    if (!conn) {
3735 3736
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3737 3738
    }

3739
    if (!(net = virNetworkCreateXML(conn, args->xml)))
3740
        goto cleanup;
3741

3742
    make_nonnull_network(&ret->net, net);
3743 3744 3745 3746 3747 3748 3749 3750 3751

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (net)
        virNetworkFree(net);
    return rv;
3752 3753 3754
}

static int
3755 3756 3757 3758 3759 3760 3761
remoteDispatchNetworkDefineXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
                               remote_error *rerr,
                               remote_network_define_xml_args *args,
                               remote_network_define_xml_ret *ret)
3762
{
3763 3764
    virNetworkPtr net = NULL;
    int rv = -1;
3765

3766
    if (!conn) {
3767 3768
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3769 3770
    }

3771
    if (!(net = virNetworkDefineXML(conn, args->xml)))
3772
        goto cleanup;
3773

3774
    make_nonnull_network(&ret->net, net);
3775 3776 3777 3778 3779 3780 3781 3782 3783

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (net)
        virNetworkFree(net);
    return rv;
3784 3785 3786
}

static int
3787 3788 3789 3790 3791 3792 3793
remoteDispatchNetworkDestroy(struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                             remote_error *rerr,
                             remote_network_destroy_args *args,
                             void *ret ATTRIBUTE_UNUSED)
3794
{
3795 3796
    virNetworkPtr net = NULL;
    int rv = -1;
3797

3798
    if (!conn) {
3799 3800
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3801 3802
    }

3803
    if (!(net = get_nonnull_network(conn, args->net)))
3804
        goto cleanup;
3805

3806
    if (virNetworkDestroy(net) < 0)
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (net)
        virNetworkFree(net);
    return rv;
3817 3818 3819
}

static int
3820 3821 3822 3823 3824 3825 3826
remoteDispatchNetworkDumpXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                             remote_error *rerr,
                             remote_network_dump_xml_args *args,
                             remote_network_dump_xml_ret *ret)
3827
{
3828 3829
    virNetworkPtr net = NULL;
    int rv = -1;
3830

3831
    if (!conn) {
3832 3833
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3834 3835
    }

3836
    if (!(net = get_nonnull_network(conn, args->net)))
3837
        goto cleanup;
3838 3839

    /* remoteDispatchClientRequest will free this. */
3840
    if (!(ret->xml = virNetworkGetXMLDesc(net, args->flags)))
3841 3842 3843 3844 3845 3846 3847 3848 3849 3850
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (net)
        virNetworkFree(net);
    return rv;
3851 3852 3853
}

static int
3854 3855 3856 3857 3858 3859 3860
remoteDispatchNetworkGetAutostart(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_network_get_autostart_args *args,
                                  remote_network_get_autostart_ret *ret)
3861
{
3862 3863
    virNetworkPtr net = NULL;
    int rv = -1;
3864

3865
    if (!conn) {
3866 3867
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3868 3869
    }

3870
    if (!(net = get_nonnull_network(conn, args->net)))
3871
        goto cleanup;
3872

3873
    if (virNetworkGetAutostart(net, &ret->autostart) < 0)
3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (net)
        virNetworkFree(net);
    return rv;
3884 3885 3886
}

static int
3887 3888 3889 3890 3891 3892 3893
remoteDispatchNetworkGetBridgeName(struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                                   remote_error *rerr,
                                   remote_network_get_bridge_name_args *args,
                                   remote_network_get_bridge_name_ret *ret)
3894
{
3895 3896
    virNetworkPtr net = NULL;
    int rv = -1;
3897

3898
    if (!conn) {
3899 3900
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3901 3902
    }

3903
    if (!(net = get_nonnull_network(conn, args->net)))
3904
        goto cleanup;
3905 3906

    /* remoteDispatchClientRequest will free this. */
3907
    if (!(ret->name = virNetworkGetBridgeName(net)))
3908 3909 3910 3911 3912 3913 3914 3915 3916 3917
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (net)
        virNetworkFree(net);
    return rv;
3918 3919 3920
}

static int
3921 3922 3923 3924 3925 3926 3927
remoteDispatchNetworkLookupByName(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_network_lookup_by_name_args *args,
                                  remote_network_lookup_by_name_ret *ret)
3928
{
3929 3930
    virNetworkPtr net = NULL;
    int rv = -1;
3931

3932
    if (!conn) {
3933 3934
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3935 3936
    }

3937
    if (!(net = virNetworkLookupByName(conn, args->name)))
3938
        goto cleanup;
3939

3940
    make_nonnull_network(&ret->net, net);
3941 3942 3943 3944 3945 3946 3947 3948 3949

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (net)
        virNetworkFree(net);
    return rv;
3950 3951 3952
}

static int
3953 3954 3955 3956 3957 3958 3959
remoteDispatchNetworkLookupByUuid(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_network_lookup_by_uuid_args *args,
                                  remote_network_lookup_by_uuid_ret *ret)
3960
{
3961 3962
    virNetworkPtr net = NULL;
    int rv = -1;
3963

3964
    if (!conn) {
3965 3966
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3967 3968
    }

3969
    if (!(net = virNetworkLookupByUUID(conn, (unsigned char *) args->uuid)))
3970
        goto cleanup;
3971

3972
    make_nonnull_network(&ret->net, net);
3973 3974 3975 3976 3977 3978 3979 3980 3981

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (net)
        virNetworkFree(net);
    return rv;
3982 3983 3984
}

static int
3985 3986 3987 3988 3989 3990 3991
remoteDispatchNetworkSetAutostart(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_network_set_autostart_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
3992
{
3993 3994
    virNetworkPtr net = NULL;
    int rv = -1;
3995

3996
    if (!conn) {
3997 3998
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3999 4000
    }

4001
    if (!(net = get_nonnull_network(conn, args->net)))
4002
        goto cleanup;
4003

4004
    if (virNetworkSetAutostart(net, args->autostart) < 0)
4005 4006 4007 4008 4009 4010 4011 4012 4013 4014
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (net)
        virNetworkFree(net);
    return rv;
4015 4016 4017
}

static int
4018 4019 4020 4021 4022 4023 4024
remoteDispatchNetworkUndefine(struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *rerr,
                              remote_network_undefine_args *args,
                              void *ret ATTRIBUTE_UNUSED)
4025
{
4026 4027
    virNetworkPtr net = NULL;
    int rv = -1;
4028

4029
    if (!conn) {
4030 4031
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4032 4033
    }

4034
    if (!(net = get_nonnull_network(conn, args->net)))
4035
        goto cleanup;
4036

4037
    if (virNetworkUndefine(net) < 0)
4038 4039 4040 4041 4042 4043 4044 4045 4046 4047
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (net)
        virNetworkFree(net);
    return rv;
4048 4049 4050
}

static int
4051 4052 4053 4054 4055 4056 4057
remoteDispatchNumOfDefinedNetworks(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_defined_networks_ret *ret)
4058
{
4059
    int rv = -1;
4060
    int len;
4061

4062
    if (!conn) {
4063 4064
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4065
    }
4066

4067
    len = virConnectNumOfDefinedNetworks(conn);
4068
    if (len < 0)
4069
        goto cleanup;
4070
    ret->num = len;
4071

4072 4073 4074 4075 4076 4077
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
4078 4079 4080
}

static int
4081 4082 4083 4084 4085 4086 4087
remoteDispatchNumOfDomains(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_domains_ret *ret)
4088
{
4089 4090
    int rv = -1;

4091
    if (!conn) {
4092 4093
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4094
    }
4095

4096
    if ((ret->num = virConnectNumOfDomains(conn)) < 0)
4097
        goto cleanup;
4098

4099 4100 4101 4102 4103 4104
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
4105 4106 4107
}

static int
4108 4109 4110 4111 4112 4113 4114
remoteDispatchNumOfNetworks(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_networks_ret *ret)
4115
{
4116 4117
    int rv = -1;

4118
    if (!conn) {
4119 4120
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4121
    }
4122

4123
    if ((ret->num = virConnectNumOfNetworks(conn)) < 0)
4124
        goto cleanup;
4125

4126 4127 4128 4129 4130 4131
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
4132 4133
}

4134

D
Daniel Veillard 已提交
4135 4136
/*-------------------------------------------------------------*/
static int
4137 4138 4139 4140 4141 4142 4143
remoteDispatchNumOfInterfaces(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_interfaces_ret *ret)
D
Daniel Veillard 已提交
4144
{
4145 4146
    int rv = -1;

4147
    if (!conn) {
4148 4149
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4150
    }
D
Daniel Veillard 已提交
4151

4152
    if ((ret->num = virConnectNumOfInterfaces(conn)) < 0)
4153
        goto cleanup;
D
Daniel Veillard 已提交
4154

4155 4156 4157 4158 4159 4160
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
D
Daniel Veillard 已提交
4161 4162 4163
}

static int
4164 4165 4166 4167 4168 4169 4170
remoteDispatchListInterfaces(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_interfaces_args *args,
                             remote_list_interfaces_ret *ret)
D
Daniel Veillard 已提交
4171
{
4172
    int rv = -1;
4173
    int len;
4174

4175
    if (!conn) {
4176 4177
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4178
    }
D
Daniel Veillard 已提交
4179 4180

    if (args->maxnames > REMOTE_INTERFACE_NAME_LIST_MAX) {
4181 4182 4183
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxnames > REMOTE_INTERFACE_NAME_LIST_MAX"));
        goto cleanup;
D
Daniel Veillard 已提交
4184 4185 4186 4187
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
4188 4189
        virReportOOMError();
        goto cleanup;
D
Daniel Veillard 已提交
4190 4191
    }

4192 4193
    len = virConnectListInterfaces(conn,
                                   ret->names.names_val, args->maxnames);
4194
    if (len < 0)
4195
        goto cleanup;
4196
    ret->names.names_len = len;
D
Daniel Veillard 已提交
4197

4198 4199 4200 4201 4202
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
4203
        VIR_FREE(ret->names.names_val);
4204 4205
    }
    return rv;
D
Daniel Veillard 已提交
4206 4207
}

4208
static int
4209 4210 4211 4212 4213 4214 4215
remoteDispatchNumOfDefinedInterfaces(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_defined_interfaces_ret *ret)
4216
{
4217
    int rv = -1;
4218
    int len;
4219

4220
    if (!conn) {
4221 4222
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4223
    }
4224

4225
    len = virConnectNumOfDefinedInterfaces(conn);
4226
    if (len < 0)
4227
        goto cleanup;
4228
    ret->num = len;
4229

4230 4231 4232 4233 4234 4235
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
4236 4237 4238
}

static int
4239 4240 4241 4242 4243 4244 4245
remoteDispatchListDefinedInterfaces(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_defined_interfaces_args *args,
                                    remote_list_defined_interfaces_ret *ret)
4246
{
4247
    int rv = -1;
4248
    int len;
4249

4250
    if (!conn) {
4251 4252
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4253
    }
4254 4255

    if (args->maxnames > REMOTE_DEFINED_INTERFACE_NAME_LIST_MAX) {
4256 4257 4258
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxnames > REMOTE_DEFINED_INTERFACE_NAME_LIST_MAX"));
        goto cleanup;
4259 4260 4261 4262
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
4263 4264
        virReportOOMError();
        goto cleanup;
4265 4266
    }

4267 4268
    len = virConnectListDefinedInterfaces(conn,
                                          ret->names.names_val, args->maxnames);
4269
    if (len < 0)
4270
        goto cleanup;
4271
    ret->names.names_len = len;
4272

4273 4274 4275 4276 4277
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
4278
        VIR_FREE(ret->names.names_val);
4279 4280
    }
    return rv;
4281 4282
}

D
Daniel Veillard 已提交
4283
static int
4284 4285 4286 4287 4288 4289 4290
remoteDispatchInterfaceLookupByName(struct qemud_server *server ATTRIBUTE_UNUSED,
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
                                    remote_error *rerr,
                                    remote_interface_lookup_by_name_args *args,
                                    remote_interface_lookup_by_name_ret *ret)
D
Daniel Veillard 已提交
4291
{
4292 4293
    virInterfacePtr iface = NULL;
    int rv = -1;
D
Daniel Veillard 已提交
4294

4295
    if (!conn) {
4296 4297
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4298 4299
    }

4300
    if (!(iface = virInterfaceLookupByName(conn, args->name)))
4301
        goto cleanup;
D
Daniel Veillard 已提交
4302

4303
    make_nonnull_interface(&ret->iface, iface);
4304 4305 4306 4307 4308 4309 4310 4311 4312

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (iface)
        virInterfaceFree(iface);
    return rv;
D
Daniel Veillard 已提交
4313 4314 4315
}

static int
4316 4317 4318 4319 4320 4321 4322
remoteDispatchInterfaceLookupByMacString(struct qemud_server *server ATTRIBUTE_UNUSED,
                                         struct qemud_client *client ATTRIBUTE_UNUSED,
                                         virConnectPtr conn,
                                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                                         remote_error *rerr,
                                         remote_interface_lookup_by_mac_string_args *args,
                                         remote_interface_lookup_by_mac_string_ret *ret)
D
Daniel Veillard 已提交
4323
{
4324 4325
    virInterfacePtr iface = NULL;
    int rv = -1;
D
Daniel Veillard 已提交
4326

4327
    if (!conn) {
4328 4329
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4330 4331
    }

4332
    if (!(iface = virInterfaceLookupByMACString(conn, args->mac)))
4333
        goto cleanup;
D
Daniel Veillard 已提交
4334

4335
    make_nonnull_interface(&ret->iface, iface);
4336 4337 4338 4339 4340 4341 4342 4343 4344

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (iface)
        virInterfaceFree(iface);
    return rv;
D
Daniel Veillard 已提交
4345
}
4346 4347 4348 4349 4350 4351 4352 4353 4354

static int
remoteDispatchInterfaceGetXmlDesc(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_interface_get_xml_desc_args *args,
                                  remote_interface_get_xml_desc_ret *ret)
D
Daniel Veillard 已提交
4355
{
4356 4357
    virInterfacePtr iface = NULL;
    int rv = -1;
D
Daniel Veillard 已提交
4358

4359
    if (!conn) {
4360 4361
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4362 4363
    }

4364
    if (!(iface = get_nonnull_interface(conn, args->iface)))
4365
        goto cleanup;
D
Daniel Veillard 已提交
4366 4367

    /* remoteDispatchClientRequest will free this. */
4368
    if (!(ret->xml = virInterfaceGetXMLDesc(iface, args->flags)))
4369 4370 4371 4372 4373 4374 4375 4376 4377 4378
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (iface)
        virInterfaceFree(iface);
    return rv;
D
Daniel Veillard 已提交
4379 4380 4381
}

static int
4382 4383 4384 4385 4386 4387 4388
remoteDispatchInterfaceDefineXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_interface_define_xml_args *args,
                                 remote_interface_define_xml_ret *ret)
D
Daniel Veillard 已提交
4389
{
4390 4391
    virInterfacePtr iface = NULL;
    int rv = -1;
D
Daniel Veillard 已提交
4392

4393
    if (!conn) {
4394 4395
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4396 4397
    }

4398
    if (!(iface = virInterfaceDefineXML(conn, args->xml, args->flags)))
4399
        goto cleanup;
D
Daniel Veillard 已提交
4400

4401
    make_nonnull_interface(&ret->iface, iface);
4402 4403 4404 4405 4406 4407 4408 4409 4410

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (iface)
        virInterfaceFree(iface);
    return rv;
D
Daniel Veillard 已提交
4411 4412 4413
}

static int
4414 4415 4416 4417 4418 4419 4420
remoteDispatchInterfaceUndefine(struct qemud_server *server ATTRIBUTE_UNUSED,
                                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 已提交
4421
{
4422 4423
    virInterfacePtr iface = NULL;
    int rv = -1;
D
Daniel Veillard 已提交
4424

4425
    if (!conn) {
4426 4427
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4428 4429
    }

4430
    if (!(iface = get_nonnull_interface(conn, args->iface)))
4431
        goto cleanup;
D
Daniel Veillard 已提交
4432

4433
    if (virInterfaceUndefine(iface) < 0)
4434 4435 4436 4437 4438 4439 4440 4441 4442 4443
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (iface)
        virInterfaceFree(iface);
    return rv;
D
Daniel Veillard 已提交
4444 4445 4446
}

static int
4447 4448 4449 4450 4451 4452 4453
remoteDispatchInterfaceCreate(struct qemud_server *server ATTRIBUTE_UNUSED,
                              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 已提交
4454
{
4455 4456
    virInterfacePtr iface = NULL;
    int rv = -1;
D
Daniel Veillard 已提交
4457

4458
    if (!conn) {
4459 4460
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4461 4462
    }

4463
    if (!(iface = get_nonnull_interface(conn, args->iface)))
4464
        goto cleanup;
D
Daniel Veillard 已提交
4465

4466
    if (virInterfaceCreate(iface, args->flags) < 0)
4467 4468 4469 4470 4471 4472 4473 4474 4475 4476
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (iface)
        virInterfaceFree(iface);
    return rv;
D
Daniel Veillard 已提交
4477 4478 4479
}

static int
4480 4481 4482 4483 4484 4485 4486
remoteDispatchInterfaceDestroy(struct qemud_server *server ATTRIBUTE_UNUSED,
                               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 已提交
4487
{
4488 4489
    virInterfacePtr iface = NULL;
    int rv = -1;
D
Daniel Veillard 已提交
4490

4491
    if (!conn) {
4492 4493
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
4494 4495
    }

4496
    if (!(iface = get_nonnull_interface(conn, args->iface)))
4497
        goto cleanup;
D
Daniel Veillard 已提交
4498

4499
    if (virInterfaceDestroy(iface, args->flags) < 0)
4500 4501 4502 4503 4504 4505 4506 4507 4508 4509
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (iface)
        virInterfaceFree(iface);
    return rv;
D
Daniel Veillard 已提交
4510 4511 4512 4513
}

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

4514
static int
4515 4516 4517 4518 4519 4520 4521
remoteDispatchAuthList(struct qemud_server *server,
                       struct qemud_client *client,
                       virConnectPtr conn ATTRIBUTE_UNUSED,
                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                       remote_error *rerr,
                       void *args ATTRIBUTE_UNUSED,
                       remote_auth_list_ret *ret)
4522
{
4523 4524
    int rv = -1;

4525
    ret->types.types_len = 1;
4526
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
4527 4528
        virReportOOMError();
        goto cleanup;
4529
    }
4530 4531 4532
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
4533
    ret->types.types_val[0] = client->auth;
4534
    virMutexUnlock(&client->lock);
4535

4536 4537 4538 4539 4540 4541
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
4542 4543 4544 4545 4546 4547
}


#if HAVE_SASL
/*
 * Initializes the SASL session in prepare for authentication
4548
 * and gives the client a list of allowed mechanisms to choose
4549 4550 4551 4552
 *
 * XXX callbacks for stuff like password verification ?
 */
static int
4553 4554
remoteDispatchAuthSaslInit(struct qemud_server *server,
                           struct qemud_client *client,
4555
                           virConnectPtr conn ATTRIBUTE_UNUSED,
4556 4557 4558 4559
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *rerr,
                           void *args ATTRIBUTE_UNUSED,
                           remote_auth_sasl_init_ret *ret)
4560 4561
{
    const char *mechlist = NULL;
4562
    sasl_security_properties_t secprops;
4563
    int err;
4564
    virSocketAddr sa;
4565 4566
    char *localAddr, *remoteAddr;

4567 4568 4569
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
4570

4571
    VIR_DEBUG("Initialize SASL auth %d", client->fd);
4572 4573
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn != NULL) {
4574
        VIR_ERROR0(_("client tried invalid SASL init request"));
4575
        goto authfail;
4576 4577 4578
    }

    /* Get local address in form  IPADDR:PORT */
4579 4580
    sa.len = sizeof(sa.data.stor);
    if (getsockname(client->fd, &sa.data.sa, &sa.len) < 0) {
4581
        char ebuf[1024];
4582 4583 4584
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    _("failed to get sock address: %s"),
                    virStrerror(errno, ebuf, sizeof ebuf));
4585
        goto error;
4586
    }
4587
    if ((localAddr = virSocketFormatAddrFull(&sa, true, ";")) == NULL)
4588
        goto error;
4589 4590

    /* Get remote address in form  IPADDR:PORT */
4591 4592
    sa.len = sizeof(sa.data.stor);
    if (getpeername(client->fd, &sa.data.sa, &sa.len) < 0) {
4593
        char ebuf[1024];
4594 4595
        virNetError(VIR_ERR_INTERNAL_ERROR, _("failed to get peer address: %s"),
                    virStrerror(errno, ebuf, sizeof ebuf));
4596
        VIR_FREE(localAddr);
4597
        goto error;
4598
    }
4599
    if ((remoteAddr = virSocketFormatAddrFull(&sa, true, ";")) == NULL) {
4600
        VIR_FREE(localAddr);
4601
        goto error;
4602 4603 4604 4605 4606 4607 4608 4609 4610 4611
    }

    err = sasl_server_new("libvirt",
                          NULL, /* FQDN - just delegates to gethostname */
                          NULL, /* User realm */
                          localAddr,
                          remoteAddr,
                          NULL, /* XXX Callbacks */
                          SASL_SUCCESS_DATA,
                          &client->saslconn);
4612 4613
    VIR_FREE(localAddr);
    VIR_FREE(remoteAddr);
4614
    if (err != SASL_OK) {
4615 4616
        VIR_ERROR(_("sasl context setup failed %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
4617
        client->saslconn = NULL;
4618
        goto authfail;
4619 4620
    }

4621 4622 4623 4624 4625 4626 4627
    /* 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 已提交
4628
            VIR_ERROR0(_("cannot get TLS cipher size"));
4629 4630
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
4631
            goto authfail;
4632 4633 4634 4635 4636
        }
        ssf *= 8; /* tls key size is bytes, sasl wants bits */

        err = sasl_setprop(client->saslconn, SASL_SSF_EXTERNAL, &ssf);
        if (err != SASL_OK) {
4637 4638
            VIR_ERROR(_("cannot set SASL external SSF %d (%s)"),
                      err, sasl_errstring(err, NULL, NULL));
4639 4640
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
4641
            goto authfail;
4642 4643 4644
        }
    }

4645
    memset(&secprops, 0, sizeof secprops);
4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664
    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) {
4665 4666
        VIR_ERROR(_("cannot set SASL security props %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
4667 4668
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4669
        goto authfail;
4670 4671
    }

4672 4673 4674 4675 4676 4677 4678 4679 4680
    err = sasl_listmech(client->saslconn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
4681 4682
        VIR_ERROR(_("cannot list SASL mechanisms %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
4683 4684
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4685
        goto authfail;
4686
    }
4687
    VIR_DEBUG("Available mechanisms for client: '%s'", mechlist);
4688 4689
    ret->mechlist = strdup(mechlist);
    if (!ret->mechlist) {
4690
        VIR_ERROR0(_("cannot allocate mechlist"));
4691 4692
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4693
        goto authfail;
4694 4695
    }

4696
    virMutexUnlock(&client->lock);
4697
    return 0;
4698 4699 4700 4701

authfail:
    remoteDispatchAuthError(rerr);
error:
4702
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
4703
    virMutexUnlock(&client->lock);
4704
    return -1;
4705 4706 4707
}


4708
/* We asked for an SSF layer, so sanity check that we actually
4709 4710 4711
 * got what we asked for
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
4712
static int
4713 4714
remoteSASLCheckSSF(struct qemud_client *client,
                   remote_error *rerr) {
4715 4716 4717 4718 4719 4720 4721 4722 4723
    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) {
4724 4725
        VIR_ERROR(_("cannot query SASL ssf on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
4726
        remoteDispatchAuthError(rerr);
4727 4728 4729 4730 4731
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    ssf = *(const int *)val;
4732
    VIR_DEBUG("negotiated an SSF of %d", ssf);
4733
    if (ssf < 56) { /* 56 is good for Kerberos */
4734
        VIR_ERROR(_("negotiated SSF %d was not strong enough"), ssf);
4735
        remoteDispatchAuthError(rerr);
4736 4737
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4738
        return -2;
4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752
    }

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

4753 4754 4755
/*
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
4756
static int
4757 4758 4759
remoteSASLCheckAccess(struct qemud_server *server,
                      struct qemud_client *client,
                      remote_error *rerr) {
4760 4761 4762 4763 4764 4765
    const void *val;
    int err;
    char **wildcards;

    err = sasl_getprop(client->saslconn, SASL_USERNAME, &val);
    if (err != SASL_OK) {
4766 4767
        VIR_ERROR(_("cannot query SASL username on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
4768
        remoteDispatchAuthError(rerr);
4769 4770 4771 4772 4773
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    if (val == NULL) {
4774
        VIR_ERROR0(_("no client username was found"));
4775
        remoteDispatchAuthError(rerr);
4776 4777 4778 4779
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
4780
    VIR_DEBUG("SASL client username %s", (const char *)val);
4781 4782 4783

    client->saslUsername = strdup((const char*)val);
    if (client->saslUsername == NULL) {
4784
        VIR_ERROR0(_("out of memory copying username"));
4785
        remoteDispatchAuthError(rerr);
4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796
        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) {
4797
        if (fnmatch(*wildcards, client->saslUsername, 0) == 0)
4798 4799 4800 4801 4802
            return 0; /* Allowed */
        wildcards++;
    }

    /* Denied */
4803
    VIR_ERROR(_("SASL client %s not allowed in whitelist"), client->saslUsername);
4804
    remoteDispatchAuthError(rerr);
4805 4806
    sasl_dispose(&client->saslconn);
    client->saslconn = NULL;
4807
    return -2;
4808 4809 4810
}


4811 4812 4813 4814
/*
 * This starts the SASL authentication negotiation.
 */
static int
4815 4816 4817 4818 4819 4820 4821
remoteDispatchAuthSaslStart(struct qemud_server *server,
                            struct qemud_client *client,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *rerr,
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
4822 4823 4824 4825 4826
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

4827 4828 4829
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
4830

4831
    VIR_DEBUG("Start SASL auth %d", client->fd);
4832 4833
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
4834
        VIR_ERROR0(_("client tried invalid SASL start request"));
4835
        goto authfail;
4836 4837
    }

4838 4839
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
4840 4841 4842 4843 4844 4845 4846 4847 4848
    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) {
4849 4850
        VIR_ERROR(_("sasl start failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
4851 4852
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4853
        goto authfail;
4854 4855
    }
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
4856
        VIR_ERROR(_("sasl start reply data too long %d"), serveroutlen);
4857 4858
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4859
        goto authfail;
4860 4861 4862 4863
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
4864
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
4865 4866
            virReportOOMError();
            remoteDispatchError(rerr);
4867
            goto error;
4868 4869 4870 4871 4872 4873 4874 4875
        }
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

4876
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
4877 4878 4879
    if (err == SASL_CONTINUE) {
        ret->complete = 0;
    } else {
4880
        /* Check username whitelist ACL */
4881 4882 4883 4884 4885 4886 4887
        if ((err = remoteSASLCheckAccess(server, client, rerr)) < 0 ||
            (err = remoteSASLCheckSSF(client, rerr)) < 0) {
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
4888

4889
        VIR_DEBUG("Authentication successful %d", client->fd);
4890 4891
        PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
              client->fd, REMOTE_AUTH_SASL, client->saslUsername);
4892 4893 4894 4895
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

4896
    virMutexUnlock(&client->lock);
4897
    return 0;
4898 4899

authfail:
4900
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
4901
    remoteDispatchAuthError(rerr);
4902 4903 4904
    goto error;

authdeny:
4905 4906
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_SASL, client->saslUsername);
4907 4908
    goto error;

4909
error:
4910
    virMutexUnlock(&client->lock);
4911
    return -1;
4912 4913 4914 4915
}


static int
4916 4917 4918 4919 4920 4921 4922
remoteDispatchAuthSaslStep(struct qemud_server *server,
                           struct qemud_client *client,
                           virConnectPtr conn ATTRIBUTE_UNUSED,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *rerr,
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
4923 4924 4925 4926 4927
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

4928 4929 4930
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
4931

4932
    VIR_DEBUG("Step SASL auth %d", client->fd);
4933 4934
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
4935
        VIR_ERROR0(_("client tried invalid SASL start request"));
4936
        goto authfail;
4937 4938
    }

4939 4940
    VIR_DEBUG("Using SASL Data %d bytes, nil: %d",
              args->data.data_len, args->nil);
4941 4942 4943 4944 4945 4946 4947 4948
    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) {
4949 4950
        VIR_ERROR(_("sasl step failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
4951 4952
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4953
        goto authfail;
4954 4955 4956
    }

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
4957 4958
        VIR_ERROR(_("sasl step reply data too long %d"),
                  serveroutlen);
4959 4960
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
4961
        goto authfail;
4962 4963 4964 4965
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
4966
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
4967 4968
            virReportOOMError();
            remoteDispatchError(rerr);
4969
            goto error;
4970 4971 4972 4973 4974 4975 4976 4977
        }
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

4978
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
4979 4980 4981
    if (err == SASL_CONTINUE) {
        ret->complete = 0;
    } else {
4982
        /* Check username whitelist ACL */
4983 4984 4985 4986 4987 4988 4989
        if ((err = remoteSASLCheckAccess(server, client, rerr)) < 0 ||
            (err = remoteSASLCheckSSF(client, rerr)) < 0) {
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
4990

4991
        VIR_DEBUG("Authentication successful %d", client->fd);
4992 4993
        PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
              client->fd, REMOTE_AUTH_SASL, client->saslUsername);
4994 4995 4996 4997
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

4998
    virMutexUnlock(&client->lock);
4999
    return 0;
5000 5001

authfail:
5002
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
5003
    remoteDispatchAuthError(rerr);
5004 5005 5006
    goto error;

authdeny:
5007 5008
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_SASL, client->saslUsername);
5009 5010
    goto error;

5011
error:
5012
    virMutexUnlock(&client->lock);
5013
    return -1;
5014 5015 5016 5017 5018
}


#else /* HAVE_SASL */
static int
5019 5020 5021 5022 5023 5024 5025
remoteDispatchAuthSaslInit(struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn ATTRIBUTE_UNUSED,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *rerr,
                           void *args ATTRIBUTE_UNUSED,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
5026
{
5027
    VIR_ERROR0(_("client tried unsupported SASL init request"));
5028
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
5029
    remoteDispatchAuthError(rerr);
5030 5031 5032 5033
    return -1;
}

static int
5034 5035 5036 5037 5038 5039 5040
remoteDispatchAuthSaslStart(struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *rerr,
                            remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
5041
{
5042
    VIR_ERROR0(_("client tried unsupported SASL start request"));
5043
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
5044
    remoteDispatchAuthError(rerr);
5045 5046 5047 5048
    return -1;
}

static int
5049 5050 5051 5052 5053 5054 5055
remoteDispatchAuthSaslStep(struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn ATTRIBUTE_UNUSED,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *rerr,
                           remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                           remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
5056
{
5057
    VIR_ERROR0(_("client tried unsupported SASL step request"));
5058
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_SASL);
5059
    remoteDispatchAuthError(rerr);
5060 5061 5062 5063 5064
    return -1;
}
#endif /* HAVE_SASL */


5065 5066
#if HAVE_POLKIT1
static int
5067 5068 5069 5070 5071 5072 5073
remoteDispatchAuthPolkit(struct qemud_server *server,
                         struct qemud_client *client,
                         virConnectPtr conn ATTRIBUTE_UNUSED,
                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                         remote_error *rerr,
                         void *args ATTRIBUTE_UNUSED,
                         remote_auth_polkit_ret *ret)
5074
{
5075 5076
    pid_t callerPid = -1;
    uid_t callerUid = -1;
5077 5078 5079
    const char *action;
    int status = -1;
    char pidbuf[50];
5080
    char ident[100];
5081 5082
    int rv;

5083 5084
    memset(ident, 0, sizeof ident);

5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100
    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
    };

5101
    VIR_DEBUG("Start PolicyKit auth %d", client->fd);
5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116
    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);
5117
        goto authfail;
5118 5119
    }

5120 5121 5122 5123 5124 5125
    rv = snprintf(ident, sizeof ident, "pid:%d,uid:%d", callerPid, callerUid);
    if (rv < 0 || rv >= sizeof ident) {
        VIR_ERROR(_("Caller identity was too large %d:%d"), callerPid, callerUid);
        goto authfail;
    }

5126
    if (virRun(pkcheck, &status) < 0) {
5127
        VIR_ERROR(_("Cannot invoke %s"), PKCHECK_PATH);
5128
        goto authfail;
5129 5130
    }
    if (status != 0) {
5131 5132 5133 5134
        char *tmp = virCommandTranslateStatus(status);
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d: %s"),
                  action, callerPid, callerUid, NULLSTR(tmp));
        VIR_FREE(tmp);
5135
        goto authdeny;
5136
    }
5137
    PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
M
Matthias Bolte 已提交
5138
          client->fd, REMOTE_AUTH_POLKIT, (char *)ident);
5139 5140 5141 5142 5143 5144 5145 5146 5147
    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:
5148
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_POLKIT);
5149 5150 5151
    goto error;

authdeny:
5152
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
M
Matthias Bolte 已提交
5153
          client->fd, REMOTE_AUTH_POLKIT, (char *)ident);
5154 5155 5156
    goto error;

error:
5157 5158 5159 5160 5161
    remoteDispatchAuthError(rerr);
    virMutexUnlock(&client->lock);
    return -1;
}
#elif HAVE_POLKIT0
5162
static int
5163 5164 5165 5166 5167 5168 5169
remoteDispatchAuthPolkit(struct qemud_server *server,
                         struct qemud_client *client,
                         virConnectPtr conn ATTRIBUTE_UNUSED,
                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                         remote_error *rerr,
                         void *args ATTRIBUTE_UNUSED,
                         remote_auth_polkit_ret *ret)
5170 5171 5172
{
    pid_t callerPid;
    uid_t callerUid;
5173 5174 5175 5176 5177 5178
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
5179
    const char *action;
5180
    char ident[100];
J
Jim Fehlig 已提交
5181
    int rv;
5182 5183

    memset(ident, 0, sizeof ident);
5184

5185 5186 5187
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
5188 5189

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

5193
    VIR_DEBUG("Start PolicyKit auth %d", client->fd);
5194
    if (client->auth != REMOTE_AUTH_POLKIT) {
5195
        VIR_ERROR0(_("client tried invalid PolicyKit init request"));
5196
        goto authfail;
5197 5198 5199
    }

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

5204 5205 5206 5207 5208 5209
    rv = snprintf(ident, sizeof ident, "pid:%d,uid:%d", callerPid, callerUid);
    if (rv < 0 || rv >= sizeof ident) {
        VIR_ERROR(_("Caller identity was too large %d:%d"), callerPid, callerUid);
        goto authfail;
    }

5210
    VIR_INFO(_("Checking PID %d running as %d"), callerPid, callerUid);
5211 5212 5213
    dbus_error_init(&err);
    if (!(pkcaller = polkit_caller_new_from_pid(server->sysbus,
                                                callerPid, &err))) {
5214
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
5215
        dbus_error_free(&err);
5216
        goto authfail;
5217
    }
5218

5219
    if (!(pkaction = polkit_action_new())) {
5220
        char ebuf[1024];
5221
        VIR_ERROR(_("Failed to create polkit action %s"),
5222
                  virStrerror(errno, ebuf, sizeof ebuf));
5223
        polkit_caller_unref(pkcaller);
5224
        goto authfail;
5225 5226 5227 5228 5229
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
5230
        char ebuf[1024];
5231
        VIR_ERROR(_("Failed to create polkit context %s"),
5232
                  (pkerr ? polkit_error_get_error_message(pkerr)
5233
                   : virStrerror(errno, ebuf, sizeof ebuf)));
5234 5235 5236 5237 5238
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
5239
        goto authfail;
5240
    }
5241

5242
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
5243 5244 5245 5246 5247 5248
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
5249 5250 5251
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
5252
        goto authfail;
5253
    }
5254
# else
5255 5256 5257
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
5258
# endif
5259 5260 5261 5262
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
5263
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d, result: %s"),
5264 5265
                  action, callerPid, callerUid,
                  polkit_result_to_string_representation(pkresult));
5266
        goto authdeny;
5267
    }
5268 5269
    PROBE(CLIENT_AUTH_ALLOW, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_POLKIT, ident);
5270
    VIR_INFO(_("Policy allowed action %s from pid %d, uid %d, result %s"),
5271 5272 5273 5274
             action, callerPid, callerUid,
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;
5275

5276
    virMutexUnlock(&client->lock);
5277
    return 0;
5278 5279

authfail:
5280
    PROBE(CLIENT_AUTH_FAIL, "fd=%d, auth=%d", client->fd, REMOTE_AUTH_POLKIT);
5281 5282 5283
    goto error;

authdeny:
5284 5285
    PROBE(CLIENT_AUTH_DENY, "fd=%d, auth=%d, username=%s",
          client->fd, REMOTE_AUTH_POLKIT, ident);
5286 5287 5288
    goto error;

error:
5289
    remoteDispatchAuthError(rerr);
5290
    virMutexUnlock(&client->lock);
5291
    return -1;
5292 5293
}

5294
#else /* !HAVE_POLKIT0 & !HAVE_POLKIT1*/
5295 5296

static int
5297 5298 5299 5300 5301 5302 5303
remoteDispatchAuthPolkit(struct qemud_server *server ATTRIBUTE_UNUSED,
                         struct qemud_client *client ATTRIBUTE_UNUSED,
                         virConnectPtr conn ATTRIBUTE_UNUSED,
                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                         remote_error *rerr,
                         void *args ATTRIBUTE_UNUSED,
                         remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
5304
{
5305
    VIR_ERROR0(_("client tried unsupported PolicyKit init request"));
5306
    remoteDispatchAuthError(rerr);
5307 5308
    return -1;
}
5309
#endif /* HAVE_POLKIT1 */
5310

5311 5312 5313 5314 5315 5316 5317

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


static int
5318 5319 5320 5321 5322 5323 5324
remoteDispatchListDefinedStoragePools(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_defined_storage_pools_args *args,
                                      remote_list_defined_storage_pools_ret *ret)
5325
{
5326
    int rv = -1;
5327
    int len;
5328

5329
    if (!conn) {
5330 5331
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5332
    }
5333 5334

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
5335 5336 5337
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        goto cleanup;
5338 5339 5340
    }

    /* Allocate return buffer. */
5341
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
5342 5343
        virReportOOMError();
        goto cleanup;
5344
    }
5345

5346 5347
    len = virConnectListDefinedStoragePools(conn,
                                            ret->names.names_val, args->maxnames);
5348
    if (len < 0)
5349
        goto cleanup;
5350
    ret->names.names_len = len;
5351

5352 5353 5354 5355 5356 5357 5358 5359
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->names.names_val);
    }
    return rv;
5360 5361 5362
}

static int
5363 5364 5365 5366 5367 5368 5369
remoteDispatchListStoragePools(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_storage_pools_args *args,
                               remote_list_storage_pools_ret *ret)
5370
{
5371
    int rv = -1;
5372
    int len;
5373

5374
    if (!conn) {
5375 5376
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5377
    }
5378 5379

    if (args->maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
5380 5381 5382
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX"));
        goto cleanup;
5383 5384 5385
    }

    /* Allocate return buffer. */
5386
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
5387 5388
        virReportOOMError();
        goto cleanup;
5389
    }
5390

5391 5392
    len = virConnectListStoragePools(conn,
                                     ret->names.names_val, args->maxnames);
5393
    if (len < 0)
5394
        goto cleanup;
5395
    ret->names.names_len = len;
5396

5397 5398 5399 5400 5401 5402 5403 5404
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->names.names_val);
    }
    return rv;
5405 5406
}

5407
static int
5408 5409 5410 5411 5412 5413 5414
remoteDispatchFindStoragePoolSources(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
                                     remote_error *rerr,
                                     remote_find_storage_pool_sources_args *args,
                                     remote_find_storage_pool_sources_ret *ret)
5415
{
5416 5417
    int rv = -1;

5418
    if (!conn) {
5419 5420
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5421 5422
    }

5423 5424 5425 5426 5427
    if (!(ret->xml =
          virConnectFindStoragePoolSources(conn,
                                           args->type,
                                           args->srcSpec ? *args->srcSpec : NULL,
                                           args->flags)))
5428
        goto cleanup;
5429

5430 5431 5432 5433 5434 5435
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
5436 5437 5438
}


5439
static int
5440 5441 5442 5443 5444 5445 5446
remoteDispatchStoragePoolCreate(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_pool_create_args *args,
                                void *ret ATTRIBUTE_UNUSED)
5447
{
5448 5449
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5450

5451
    if (!conn) {
5452 5453
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5454 5455
    }

5456
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
5457
        goto cleanup;
5458

5459
    if (virStoragePoolCreate(pool, args->flags) < 0)
5460 5461 5462 5463 5464 5465 5466 5467 5468 5469
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5470 5471 5472
}

static int
5473 5474 5475 5476 5477 5478 5479
remoteDispatchStoragePoolCreateXml(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_pool_create_xml_args *args,
                                   remote_storage_pool_create_xml_ret *ret)
5480
{
5481 5482
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5483

5484
    if (!conn) {
5485 5486
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5487 5488
    }

5489
    if (!(pool = virStoragePoolCreateXML(conn, args->xml, args->flags)))
5490
        goto cleanup;
5491

5492
    make_nonnull_storage_pool(&ret->pool, pool);
5493 5494 5495 5496 5497 5498 5499 5500 5501

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5502 5503 5504
}

static int
5505 5506 5507 5508 5509 5510 5511
remoteDispatchStoragePoolDefineXml(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_pool_define_xml_args *args,
                                   remote_storage_pool_define_xml_ret *ret)
5512
{
5513 5514
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5515

5516
    if (!conn) {
5517 5518
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5519 5520
    }

5521
    if (!(pool = virStoragePoolDefineXML(conn, args->xml, args->flags)))
5522
        goto cleanup;
5523

5524
    make_nonnull_storage_pool(&ret->pool, pool);
5525 5526 5527 5528 5529 5530 5531 5532 5533

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5534 5535 5536
}

static int
5537 5538 5539 5540 5541 5542 5543
remoteDispatchStoragePoolBuild(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_pool_build_args *args,
                               void *ret ATTRIBUTE_UNUSED)
5544
{
5545 5546
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5547

5548
    if (!conn) {
5549 5550
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5551 5552
    }

5553
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
5554
        goto cleanup;
5555

5556
    if (virStoragePoolBuild(pool, args->flags) < 0)
5557 5558 5559 5560 5561 5562 5563 5564 5565 5566
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5567 5568 5569 5570
}


static int
5571 5572 5573 5574 5575 5576 5577
remoteDispatchStoragePoolDestroy(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_pool_destroy_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
5578
{
5579 5580
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5581

5582
    if (!conn) {
5583 5584
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5585 5586
    }

5587
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
5588
        goto cleanup;
5589

5590
    if (virStoragePoolDestroy(pool) < 0)
5591 5592 5593 5594 5595 5596 5597 5598 5599 5600
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5601 5602 5603
}

static int
5604 5605 5606 5607 5608 5609 5610
remoteDispatchStoragePoolDelete(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_pool_delete_args *args,
                                void *ret ATTRIBUTE_UNUSED)
5611
{
5612 5613
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5614

5615
    if (!conn) {
5616 5617
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5618 5619
    }

5620
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
5621
        goto cleanup;
5622

5623
    if (virStoragePoolDelete(pool, args->flags) < 0)
5624 5625 5626 5627 5628 5629 5630 5631 5632 5633
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5634 5635 5636
}

static int
5637 5638 5639 5640 5641 5642 5643
remoteDispatchStoragePoolRefresh(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_pool_refresh_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
5644
{
5645 5646
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5647

5648
    if (!conn) {
5649 5650
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5651 5652
    }

5653
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
5654
        goto cleanup;
5655

5656
    if (virStoragePoolRefresh(pool, args->flags) < 0)
5657 5658 5659 5660 5661 5662 5663 5664 5665 5666
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5667 5668 5669
}

static int
5670 5671 5672 5673 5674 5675 5676
remoteDispatchStoragePoolGetInfo(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_pool_get_info_args *args,
                                 remote_storage_pool_get_info_ret *ret)
5677
{
5678
    virStoragePoolPtr pool = NULL;
5679
    virStoragePoolInfo info;
5680
    int rv = -1;
5681

5682
    if (!conn) {
5683 5684
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5685 5686
    }

5687
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
5688
        goto cleanup;
5689

5690
    if (virStoragePoolGetInfo(pool, &info) < 0)
5691
        goto cleanup;
5692 5693 5694 5695 5696 5697

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

5698
    rv = 0;
5699

5700 5701 5702 5703 5704 5705
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5706 5707 5708
}

static int
5709 5710 5711 5712 5713 5714 5715
remoteDispatchStoragePoolDumpXml(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_pool_dump_xml_args *args,
                                 remote_storage_pool_dump_xml_ret *ret)
5716
{
5717 5718
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5719

5720
    if (!conn) {
5721 5722
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5723 5724
    }

5725
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
5726
        goto cleanup;
5727 5728

    /* remoteDispatchClientRequest will free this. */
5729
    if (!(ret->xml = virStoragePoolGetXMLDesc(pool, args->flags)))
5730 5731 5732 5733 5734 5735 5736 5737 5738 5739
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5740 5741 5742
}

static int
5743 5744 5745 5746 5747 5748 5749
remoteDispatchStoragePoolGetAutostart(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_pool_get_autostart_args *args,
                                      remote_storage_pool_get_autostart_ret *ret)
5750
{
5751 5752
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5753

5754
    if (!conn) {
5755 5756
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5757 5758
    }

5759
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
5760
        goto cleanup;
5761

5762
    if (virStoragePoolGetAutostart(pool, &ret->autostart) < 0)
5763 5764 5765 5766 5767 5768 5769 5770 5771 5772
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5773 5774 5775 5776
}


static int
5777 5778 5779 5780 5781 5782 5783
remoteDispatchStoragePoolLookupByName(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_pool_lookup_by_name_args *args,
                                      remote_storage_pool_lookup_by_name_ret *ret)
5784
{
5785 5786
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5787

5788
    if (!conn) {
5789 5790
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5791 5792
    }

5793
    if (!(pool = virStoragePoolLookupByName(conn, args->name)))
5794
        goto cleanup;
5795

5796
    make_nonnull_storage_pool(&ret->pool, pool);
5797 5798 5799 5800 5801 5802 5803 5804 5805

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5806 5807 5808
}

static int
5809 5810 5811 5812 5813 5814 5815
remoteDispatchStoragePoolLookupByUuid(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_pool_lookup_by_uuid_args *args,
                                      remote_storage_pool_lookup_by_uuid_ret *ret)
5816
{
5817 5818
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5819

5820
    if (!conn) {
5821 5822
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5823 5824
    }

5825
    if (!(pool = virStoragePoolLookupByUUID(conn, (unsigned char *) args->uuid)))
5826
        goto cleanup;
5827

5828
    make_nonnull_storage_pool(&ret->pool, pool);
5829 5830 5831 5832 5833 5834 5835 5836 5837

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5838 5839 5840
}

static int
5841 5842 5843 5844 5845 5846 5847
remoteDispatchStoragePoolLookupByVolume(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_pool_lookup_by_volume_args *args,
                                        remote_storage_pool_lookup_by_volume_ret *ret)
5848
{
5849 5850 5851
    virStoragePoolPtr pool = NULL;
    virStorageVolPtr vol = NULL;
    int rv = -1;
5852

5853
    if (!conn) {
5854 5855
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5856 5857
    }

5858
    if (!(vol = get_nonnull_storage_vol(conn, args->vol)))
5859
        goto cleanup;
5860

5861
    if (!(pool = virStoragePoolLookupByVolume(vol)))
5862
        goto cleanup;
5863

5864
    make_nonnull_storage_pool(&ret->pool, pool);
5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (vol)
        virStorageVolFree(vol);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5876 5877 5878
}

static int
5879 5880 5881 5882 5883 5884 5885
remoteDispatchStoragePoolSetAutostart(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_pool_set_autostart_args *args,
                                      void *ret ATTRIBUTE_UNUSED)
5886
{
5887 5888
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5889

5890
    if (!conn) {
5891 5892
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5893 5894
    }

5895
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
5896
        goto cleanup;
5897

5898
    if (virStoragePoolSetAutostart(pool, args->autostart) < 0)
5899 5900 5901 5902 5903 5904 5905 5906 5907 5908
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5909 5910 5911
}

static int
5912 5913 5914 5915 5916 5917 5918
remoteDispatchStoragePoolUndefine(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_pool_undefine_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
5919
{
5920 5921
    virStoragePoolPtr pool = NULL;
    int rv = -1;
5922

5923
    if (!conn) {
5924 5925
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5926 5927
    }

5928
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
5929
        goto cleanup;
5930

5931
    if (virStoragePoolUndefine(pool) < 0)
5932 5933 5934 5935 5936 5937 5938 5939 5940 5941
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
5942 5943 5944
}

static int
5945 5946 5947 5948 5949 5950 5951
remoteDispatchNumOfStoragePools(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_storage_pools_ret *ret)
5952
{
5953 5954
    int rv = -1;

5955
    if (!conn) {
5956 5957
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5958
    }
5959

5960
    if ((ret->num = virConnectNumOfStoragePools(conn)) < 0)
5961
        goto cleanup;
5962

5963 5964 5965 5966 5967 5968
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
5969 5970 5971
}

static int
5972 5973 5974 5975 5976 5977 5978
remoteDispatchNumOfDefinedStoragePools(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_defined_storage_pools_ret *ret)
5979
{
5980 5981
    int rv = -1;

5982
    if (!conn) {
5983 5984
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
5985
    }
5986

5987
    if ((ret->num = virConnectNumOfDefinedStoragePools(conn)) < 0)
5988
        goto cleanup;
5989

5990 5991 5992 5993 5994 5995
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
5996 5997 5998
}

static int
5999 6000 6001 6002 6003 6004 6005
remoteDispatchStoragePoolListVolumes(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_pool_list_volumes_args *args,
                                     remote_storage_pool_list_volumes_ret *ret)
6006
{
6007 6008
    virStoragePoolPtr pool = NULL;
    int rv = -1;
6009
    int len;
6010

6011
    if (!conn) {
6012 6013
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6014 6015
    }

6016
    if (args->maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX) {
6017 6018 6019
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX"));
        goto cleanup;
6020 6021
    }

6022
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
6023
        goto cleanup;
6024 6025

    /* Allocate return buffer. */
6026
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
6027 6028
        virReportOOMError();
        goto cleanup;
6029
    }
6030

6031 6032
    len = virStoragePoolListVolumes(pool,
                                    ret->names.names_val, args->maxnames);
6033
    if (len < 0)
6034
        goto cleanup;
6035
    ret->names.names_len = len;
6036 6037 6038 6039 6040 6041

    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
6042 6043
        VIR_FREE(ret->names.names_val);
    }
6044 6045 6046
    if (pool)
        virStoragePoolFree(pool);
    return rv;
6047 6048 6049 6050
}


static int
6051 6052 6053 6054 6055 6056 6057
remoteDispatchStoragePoolNumOfVolumes(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_pool_num_of_volumes_args *args,
                                      remote_storage_pool_num_of_volumes_ret *ret)
6058
{
6059 6060
    virStoragePoolPtr pool = NULL;
    int rv = -1;
6061

6062
    if (!conn) {
6063 6064
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6065 6066
    }

6067
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
6068
        goto cleanup;
6069

6070
    if ((ret->num = virStoragePoolNumOfVolumes(pool)) < 0)
6071
        goto cleanup;
6072

6073 6074 6075 6076 6077 6078 6079 6080
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
6081 6082 6083 6084 6085 6086 6087 6088 6089 6090
}


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



static int
6091 6092 6093 6094 6095 6096 6097
remoteDispatchStorageVolCreateXml(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_create_xml_args *args,
                                  remote_storage_vol_create_xml_ret *ret)
6098
{
6099 6100 6101
    virStoragePoolPtr pool = NULL;
    virStorageVolPtr vol = NULL;
    int rv = -1;
6102

6103
    if (!conn) {
6104 6105
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6106 6107
    }

6108
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
6109
        goto cleanup;
6110

6111
    if (!(vol = virStorageVolCreateXML(pool, args->xml, args->flags)))
6112
        goto cleanup;
6113

6114
    make_nonnull_storage_vol(&ret->vol, vol);
6115 6116 6117 6118 6119 6120 6121 6122 6123 6124
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    if (vol)
        virStorageVolFree(vol);
    return rv;
6125 6126
}

6127
static int
6128 6129 6130 6131 6132 6133 6134
remoteDispatchStorageVolCreateXmlFrom(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_create_xml_from_args *args,
                                      remote_storage_vol_create_xml_from_ret *ret)
6135
{
6136 6137 6138 6139
    virStoragePoolPtr pool = NULL;
    virStorageVolPtr clonevol = NULL;
    virStorageVolPtr newvol = NULL;
    int rv = -1;
6140

6141
    if (!conn) {
6142 6143
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6144 6145
    }

6146
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
6147
        goto cleanup;
6148

6149
    if (!(clonevol = get_nonnull_storage_vol(conn, args->clonevol)))
6150
        goto cleanup;
6151

6152 6153
    if (!(newvol = virStorageVolCreateXMLFrom(pool, args->xml, clonevol,
                                              args->flags)))
6154
        goto cleanup;
6155

6156
    make_nonnull_storage_vol(&ret->vol, newvol);
6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (newvol)
        virStorageVolFree(newvol);
    if (clonevol)
        virStorageVolFree(clonevol);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
6169
}
6170 6171

static int
6172 6173 6174 6175 6176 6177 6178
remoteDispatchStorageVolDelete(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_delete_args *args,
                               void *ret ATTRIBUTE_UNUSED)
6179
{
6180 6181
    virStorageVolPtr vol = NULL;
    int rv = -1;
6182

6183
    if (!conn) {
6184 6185
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6186 6187
    }

6188
    if (!(vol = get_nonnull_storage_vol(conn, args->vol)))
6189
        goto cleanup;
6190

6191
    if (virStorageVolDelete(vol, args->flags) < 0)
6192 6193 6194 6195 6196 6197 6198 6199 6200 6201
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (vol)
        virStorageVolFree(vol);
    return rv;
6202 6203
}

6204 6205 6206 6207 6208 6209 6210 6211 6212
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)
{
6213 6214
    virStorageVolPtr vol = NULL;
    int rv = -1;
6215

6216
    if (!conn) {
6217 6218
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6219 6220
    }

6221
    if (!(vol = get_nonnull_storage_vol(conn, args->vol)))
6222
        goto cleanup;
6223

6224
    if (virStorageVolWipe(vol, args->flags) < 0)
6225
        goto cleanup;
6226

6227
    rv = 0;
6228

6229 6230 6231 6232
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (vol)
6233
        virStorageVolFree(vol);
6234
    return rv;
6235 6236
}

6237
static int
6238 6239 6240 6241 6242 6243 6244
remoteDispatchStorageVolGetInfo(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_get_info_args *args,
                                remote_storage_vol_get_info_ret *ret)
6245
{
6246
    virStorageVolPtr vol = NULL;
6247
    virStorageVolInfo info;
6248
    int rv = -1;
6249

6250
    if (!conn) {
6251 6252
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6253 6254
    }

6255
    if (!(vol = get_nonnull_storage_vol(conn, args->vol)))
6256
        goto cleanup;
6257

6258
    if (virStorageVolGetInfo(vol, &info) < 0)
6259
        goto cleanup;
6260 6261 6262 6263 6264

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

6265
    rv = 0;
6266

6267 6268 6269 6270 6271 6272
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (vol)
        virStorageVolFree(vol);
    return rv;
6273 6274 6275
}

static int
6276 6277 6278 6279 6280 6281 6282
remoteDispatchStorageVolDumpXml(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_dump_xml_args *args,
                                remote_storage_vol_dump_xml_ret *ret)
6283
{
6284 6285
    virStorageVolPtr vol = NULL;
    int rv = -1;
6286

6287
    if (!conn) {
6288 6289
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6290 6291
    }

6292
    if (!(vol = get_nonnull_storage_vol(conn, args->vol)))
6293
        goto cleanup;
6294 6295

    /* remoteDispatchClientRequest will free this. */
6296
    if (!(ret->xml = virStorageVolGetXMLDesc(vol, args->flags)))
6297 6298 6299 6300 6301 6302 6303 6304 6305 6306
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (vol)
        virStorageVolFree(vol);
    return rv;
6307 6308 6309 6310
}


static int
6311 6312 6313 6314 6315 6316 6317
remoteDispatchStorageVolGetPath(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_get_path_args *args,
                                remote_storage_vol_get_path_ret *ret)
6318
{
6319 6320
    virStorageVolPtr vol = NULL;
    int rv = -1;
6321

6322
    if (!conn) {
6323 6324
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6325 6326
    }

6327
    if (!(vol = get_nonnull_storage_vol(conn, args->vol)))
6328
        goto cleanup;
6329 6330

    /* remoteDispatchClientRequest will free this. */
6331
    if (!(ret->name = virStorageVolGetPath(vol)))
6332 6333 6334 6335 6336 6337 6338 6339 6340 6341
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (vol)
        virStorageVolFree(vol);
    return rv;
6342 6343 6344 6345
}


static int
6346 6347 6348 6349 6350 6351 6352
remoteDispatchStorageVolLookupByName(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_lookup_by_name_args *args,
                                     remote_storage_vol_lookup_by_name_ret *ret)
6353
{
6354 6355 6356
    virStoragePoolPtr pool = NULL;
    virStorageVolPtr vol = NULL;
    int rv = -1;
6357

6358
    if (!conn) {
6359 6360
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6361 6362
    }

6363
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
6364
        goto cleanup;
6365

6366
    if (!(vol = virStorageVolLookupByName(pool, args->name)))
6367
        goto cleanup;
6368

6369
    make_nonnull_storage_vol(&ret->vol, vol);
6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    if (vol)
        virStorageVolFree(vol);
    return rv;
6381 6382 6383
}

static int
6384 6385 6386 6387 6388 6389 6390
remoteDispatchStorageVolLookupByKey(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_lookup_by_key_args *args,
                                    remote_storage_vol_lookup_by_key_ret *ret)
6391
{
6392 6393
    virStorageVolPtr vol = NULL;
    int rv = -1;
6394

6395
    if (!conn) {
6396 6397
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6398 6399
    }

6400
    if (!(vol = virStorageVolLookupByKey(conn, args->key)))
6401
        goto cleanup;
6402

6403
    make_nonnull_storage_vol(&ret->vol, vol);
6404 6405 6406 6407 6408 6409 6410 6411 6412

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (vol)
        virStorageVolFree(vol);
    return rv;
6413 6414 6415 6416
}


static int
6417 6418 6419 6420 6421 6422 6423
remoteDispatchStorageVolLookupByPath(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_lookup_by_path_args *args,
                                     remote_storage_vol_lookup_by_path_ret *ret)
6424
{
6425 6426
    virStorageVolPtr vol = NULL;
    int rv = -1;
6427

6428
    if (!conn) {
6429 6430
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6431 6432
    }

6433
    if (!(vol = virStorageVolLookupByPath(conn, args->path)))
6434
        goto cleanup;
6435

6436
    make_nonnull_storage_vol(&ret->vol, vol);
6437 6438 6439 6440 6441 6442 6443 6444 6445

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (vol)
        virStorageVolFree(vol);
    return rv;
6446 6447 6448
}


6449 6450 6451 6452 6453
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/

static int
6454 6455 6456 6457 6458 6459 6460
remoteDispatchNodeNumOfDevices(struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
                               remote_error *rerr,
                               remote_node_num_of_devices_args *args,
                               remote_node_num_of_devices_ret *ret)
6461
{
6462 6463
    int rv = -1;

6464
    if (!conn) {
6465 6466
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6467
    }
6468

6469 6470 6471
    if ((ret->num = virNodeNumOfDevices(conn,
                                        args->cap ? *args->cap : NULL,
                                        args->flags)) < 0)
6472
        goto cleanup;
6473

6474 6475 6476 6477 6478 6479
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
6480 6481 6482 6483
}


static int
6484 6485 6486 6487 6488 6489 6490
remoteDispatchNodeListDevices(struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *rerr,
                              remote_node_list_devices_args *args,
                              remote_node_list_devices_ret *ret)
6491
{
6492
    int rv = -1;
6493
    int len;
6494

6495
    if (!conn) {
6496 6497
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6498
    }
6499 6500

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
6501 6502 6503
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        goto cleanup;
6504 6505 6506 6507
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
6508 6509
        virReportOOMError();
        goto cleanup;
6510 6511
    }

6512 6513 6514
    len = virNodeListDevices(conn,
                             args->cap ? *args->cap : NULL,
                             ret->names.names_val, args->maxnames, args->flags);
6515
    if (len < 0)
6516
        goto cleanup;
6517
    ret->names.names_len = len;
6518

6519 6520 6521 6522 6523 6524 6525 6526
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->names.names_val);
    }
    return rv;
6527 6528 6529 6530
}


static int
6531 6532 6533 6534 6535 6536 6537
remoteDispatchNodeDeviceLookupByName(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
                                     remote_error *rerr,
                                     remote_node_device_lookup_by_name_args *args,
                                     remote_node_device_lookup_by_name_ret *ret)
6538
{
6539 6540
    virNodeDevicePtr dev = NULL;
    int rv = -1;
6541

6542
    if (!conn) {
6543 6544
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6545
    }
6546

6547
    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))
6548
        goto cleanup;
6549

6550
    make_nonnull_node_device(&ret->dev, dev);
6551 6552 6553 6554 6555 6556 6557 6558 6559

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dev)
        virNodeDeviceFree(dev);
    return rv;
6560 6561 6562 6563
}


static int
6564 6565 6566 6567 6568 6569 6570
remoteDispatchNodeDeviceDumpXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                remote_error *rerr,
                                remote_node_device_dump_xml_args *args,
                                remote_node_device_dump_xml_ret *ret)
6571
{
6572 6573
    virNodeDevicePtr dev = NULL;
    int rv = -1;
6574 6575

    if (!conn) {
6576 6577
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6578
    }
6579

6580
    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))
6581
        goto cleanup;
6582 6583

    /* remoteDispatchClientRequest will free this. */
6584
    if (!(ret->xml = virNodeDeviceGetXMLDesc(dev, args->flags)))
6585 6586 6587 6588 6589 6590 6591 6592 6593 6594
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dev)
        virNodeDeviceFree(dev);
    return rv;
6595 6596 6597 6598
}


static int
6599 6600 6601 6602 6603 6604 6605
remoteDispatchNodeDeviceGetParent(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
6606
{
6607 6608 6609
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
    int rv = -1;
6610 6611

    if (!conn) {
6612 6613
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6614
    }
6615

6616
    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))
6617
        goto cleanup;
6618 6619 6620 6621 6622 6623 6624 6625 6626

    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
        if (VIR_ALLOC(parent_p) < 0) {
6627 6628
            virReportOOMError();
            goto cleanup;
6629
        }
6630 6631 6632 6633
        if (!(*parent_p = strdup(parent))) {
            VIR_FREE(parent_p);
            virReportOOMError();
            goto cleanup;
6634 6635 6636 6637
        }
        ret->parent = parent_p;
    }

6638 6639 6640 6641 6642 6643 6644 6645
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dev)
        virNodeDeviceFree(dev);
    return rv;
6646 6647 6648 6649
}


static int
6650 6651 6652 6653 6654 6655 6656
remoteDispatchNodeDeviceNumOfCaps(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *rerr,
                                  remote_node_device_num_of_caps_args *args,
                                  remote_node_device_num_of_caps_ret *ret)
6657
{
6658 6659
    virNodeDevicePtr dev = NULL;
    int rv = -1;
6660 6661

    if (!conn) {
6662 6663
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6664
    }
6665

6666
    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))
6667
        goto cleanup;
6668

6669
    if ((ret->num = virNodeDeviceNumOfCaps(dev)) < 0)
6670
        goto cleanup;
6671

6672 6673 6674 6675 6676 6677 6678 6679
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dev)
        virNodeDeviceFree(dev);
    return rv;
6680 6681 6682 6683
}


static int
6684 6685 6686 6687 6688 6689 6690
remoteDispatchNodeDeviceListCaps(struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_node_device_list_caps_args *args,
                                 remote_node_device_list_caps_ret *ret)
6691
{
6692 6693
    virNodeDevicePtr dev = NULL;
    int rv = -1;
6694
    int len;
6695 6696

    if (!conn) {
6697 6698
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6699
    }
6700

6701
    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))
6702
        goto cleanup;
6703 6704

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
6705 6706 6707
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        goto cleanup;
6708 6709 6710 6711
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
6712 6713
        virReportOOMError();
        goto cleanup;
6714 6715
    }

6716 6717
    len = virNodeDeviceListCaps(dev, ret->names.names_val,
                                args->maxnames);
6718
    if (len < 0)
6719
        goto cleanup;
6720
    ret->names.names_len = len;
6721

6722 6723 6724 6725 6726 6727 6728 6729 6730 6731
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->names.names_val);
    }
    if (dev)
        virNodeDeviceFree(dev);
    return rv;
6732 6733 6734
}


6735
static int
6736 6737 6738 6739 6740 6741 6742
remoteDispatchNodeDeviceDettach(struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                remote_error *rerr,
                                remote_node_device_dettach_args *args,
                                void *ret ATTRIBUTE_UNUSED)
6743
{
6744 6745
    virNodeDevicePtr dev = NULL;
    int rv = -1;
6746 6747

    if (!conn) {
6748 6749
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6750
    }
6751

6752
    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))
6753
        goto cleanup;
6754

6755
    if (virNodeDeviceDettach(dev) < 0)
6756
        goto cleanup;
6757

6758 6759 6760 6761 6762 6763 6764 6765
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dev)
        virNodeDeviceFree(dev);
    return rv;
6766 6767 6768 6769
}


static int
6770 6771 6772 6773 6774 6775 6776
remoteDispatchNodeDeviceReAttach(struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_node_device_re_attach_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
6777
{
6778 6779
    virNodeDevicePtr dev = NULL;
    int rv = -1;
6780 6781

    if (!conn) {
6782 6783
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6784
    }
6785

6786
    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))
6787
        goto cleanup;
6788

6789
    if (virNodeDeviceReAttach(dev) < 0)
6790
        goto cleanup;
6791

6792 6793 6794 6795 6796 6797 6798 6799
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dev)
        virNodeDeviceFree(dev);
    return rv;
6800 6801 6802 6803
}


static int
6804 6805 6806 6807 6808 6809 6810
remoteDispatchNodeDeviceReset(struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *rerr,
                              remote_node_device_reset_args *args,
                              void *ret ATTRIBUTE_UNUSED)
6811
{
6812 6813
    virNodeDevicePtr dev = NULL;
    int rv = -1;
6814 6815

    if (!conn) {
6816 6817
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6818
    }
6819

6820
    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))
6821
        goto cleanup;
6822

6823
    if (virNodeDeviceReset(dev) < 0)
6824
        goto cleanup;
6825

6826 6827 6828 6829 6830 6831 6832 6833
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dev)
        virNodeDeviceFree(dev);
    return rv;
6834 6835 6836
}


6837 6838 6839 6840
static int
remoteDispatchNodeDeviceCreateXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
6841
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
6842 6843 6844 6845
                                  remote_error *rerr,
                                  remote_node_device_create_xml_args *args,
                                  remote_node_device_create_xml_ret *ret)
{
6846 6847
    virNodeDevicePtr dev = NULL;
    int rv = -1;
6848

6849
    if (!conn) {
6850 6851
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6852 6853
    }

6854
    if (!(dev = virNodeDeviceCreateXML(conn, args->xml_desc, args->flags)))
6855
        goto cleanup;
6856

6857
    make_nonnull_node_device(&ret->dev, dev);
6858

6859 6860 6861 6862 6863 6864 6865 6866
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dev)
        virNodeDeviceFree(dev);
    return rv;
6867 6868 6869 6870 6871 6872 6873
}


static int
remoteDispatchNodeDeviceDestroy(struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
6874
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
6875 6876 6877 6878
                                remote_error *rerr,
                                remote_node_device_destroy_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
6879 6880
    virNodeDevicePtr dev = NULL;
    int rv = -1;
6881

6882
    if (!conn) {
6883 6884
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6885 6886
    }

6887
    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))
6888
        goto cleanup;
6889

6890
    if (virNodeDeviceDestroy(dev) < 0)
6891
        goto cleanup;
6892

6893 6894 6895 6896 6897 6898 6899 6900
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dev)
        virNodeDeviceFree(dev);
    return rv;
6901
}
6902

6903 6904 6905 6906 6907 6908 6909 6910 6911
static int remoteDispatchStorageVolUpload(struct qemud_server *server ATTRIBUTE_UNUSED,
                                          struct qemud_client *client,
                                          virConnectPtr conn,
                                          remote_message_header *hdr,
                                          remote_error *rerr,
                                          remote_storage_vol_upload_args *args,
                                          void *ret ATTRIBUTE_UNUSED)
{
    struct qemud_client_stream *stream = NULL;
6912 6913
    virStorageVolPtr vol = NULL;
    int rv = -1;
6914

6915
    if (!conn) {
6916 6917
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6918 6919
    }

6920
    if (!(vol = get_nonnull_storage_vol(conn, args->vol)))
6921 6922
        goto cleanup;

6923
    if (!(stream = remoteCreateClientStream(conn, hdr)))
6924 6925 6926 6927
        goto cleanup;

    if (virStorageVolUpload(vol, stream->st,
                            args->offset, args->length,
6928
                            args->flags) < 0)
6929 6930
        goto cleanup;

6931
    if (remoteAddClientStream(client, stream, 0) < 0)
6932 6933 6934 6935 6936
        goto cleanup;

    rv = 0;

cleanup:
6937 6938
    if (rv < 0)
        remoteDispatchError(rerr);
6939 6940
    if (vol)
        virStorageVolFree(vol);
6941 6942
    if (stream && rv != 0) {
        virStreamAbort(stream->st);
6943
        remoteFreeClientStream(client, stream);
6944
    }
6945 6946
    return rv;
}
6947

6948 6949 6950 6951 6952 6953 6954 6955 6956
static int remoteDispatchStorageVolDownload(struct qemud_server *server ATTRIBUTE_UNUSED,
                                            struct qemud_client *client,
                                            virConnectPtr conn,
                                            remote_message_header *hdr,
                                            remote_error *rerr,
                                            remote_storage_vol_download_args *args,
                                            void *ret ATTRIBUTE_UNUSED)
{
    struct qemud_client_stream *stream = NULL;
6957 6958
    virStorageVolPtr vol = NULL;
    int rv = -1;
6959

6960
    if (!conn) {
6961 6962
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
6963 6964
    }

6965
    if (!(vol = get_nonnull_storage_vol(conn, args->vol)))
6966 6967
        goto cleanup;

6968
    if (!(stream = remoteCreateClientStream(conn, hdr)))
6969 6970 6971 6972
        goto cleanup;

    if (virStorageVolDownload(vol, stream->st,
                              args->offset, args->length,
6973
                              args->flags) < 0)
6974 6975
        goto cleanup;

6976
    if (remoteAddClientStream(client, stream, 1) < 0)
6977 6978 6979 6980 6981
        goto cleanup;

    rv = 0;

cleanup:
6982 6983
    if (rv < 0)
        remoteDispatchError(rerr);
6984 6985
    if (vol)
        virStorageVolFree(vol);
6986 6987
    if (stream && rv != 0) {
        virStreamAbort(stream->st);
6988
        remoteFreeClientStream(client, stream);
6989
    }
6990 6991
    return rv;
}
6992

6993 6994 6995 6996 6997

/***************************
 * Register / deregister events
 ***************************/
static int
6998 6999 7000 7001 7002 7003 7004
remoteDispatchDomainEventsRegister(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,
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
7005
{
7006
    int callbackID;
7007
    int rv = -1;
7008

7009
    if (!conn) {
7010 7011
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7012 7013
    }

7014
    if (client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] != -1) {
7015 7016
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
        goto cleanup;
7017
    }
7018

7019 7020 7021 7022
    if ((callbackID = virConnectDomainEventRegisterAny(conn,
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
7023
                                                       client, NULL)) < 0)
7024
        goto cleanup;
7025

7026
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;
7027

7028 7029 7030 7031 7032 7033
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
7034 7035 7036
}

static int
7037 7038 7039 7040 7041 7042 7043
remoteDispatchDomainEventsDeregister(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,
                                     void *args ATTRIBUTE_UNUSED,
                                     remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
7044
{
7045 7046
    int rv = -1;

7047
    if (!conn) {
7048 7049
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7050
    }
7051

7052
    if (client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] < 0) {
7053 7054
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
        goto cleanup;
7055
    }
7056

7057
    if (virConnectDomainEventDeregisterAny(conn,
7058
                                           client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0)
7059
        goto cleanup;
7060

7061
    client->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;
7062 7063 7064 7065 7066 7067
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
7068 7069 7070
}

static void
7071 7072 7073 7074
remoteDispatchDomainEventSend(struct qemud_client *client,
                              int procnr,
                              xdrproc_t proc,
                              void *data)
7075
{
7076
    struct qemud_client_message *msg = NULL;
7077
    XDR xdr;
7078
    unsigned int len;
7079

7080
    if (VIR_ALLOC(msg) < 0)
7081 7082
        return;

7083 7084
    msg->hdr.prog = REMOTE_PROGRAM;
    msg->hdr.vers = REMOTE_PROTOCOL_VERSION;
7085
    msg->hdr.proc = procnr;
7086
    msg->hdr.type = REMOTE_MESSAGE;
7087 7088
    msg->hdr.serial = 1;
    msg->hdr.status = REMOTE_OK;
7089

7090
    if (remoteEncodeClientMessageHeader(msg) < 0)
7091
        goto cleanup;
7092

7093
    /* Serialise the return header and event. */
7094 7095 7096 7097
    xdrmem_create(&xdr,
                  msg->buffer,
                  msg->bufferLength,
                  XDR_ENCODE);
7098

7099
    /* Skip over the header we just wrote */
7100
    if (xdr_setpos(&xdr, msg->bufferOffset) == 0)
7101
        goto xdr_cleanup;
7102

7103 7104
    if (!(proc)(&xdr, data)) {
        VIR_WARN("Failed to serialize domain event %d", procnr);
7105
        goto xdr_cleanup;
7106
    }
7107

7108
    /* Update length word to include payload*/
7109 7110
    len = msg->bufferOffset = xdr_getpos(&xdr);
    if (xdr_setpos(&xdr, 0) == 0)
7111
        goto xdr_cleanup;
7112

7113
    if (!xdr_u_int(&xdr, &len))
7114
        goto xdr_cleanup;
7115 7116

    /* Send it. */
7117 7118 7119
    msg->async = 1;
    msg->bufferLength = len;
    msg->bufferOffset = 0;
7120 7121

    VIR_DEBUG("Queue event %d %d", procnr, msg->bufferLength);
7122
    qemudClientMessageQueuePush(&client->tx, msg);
7123
    qemudUpdateClientEvent(client);
7124

7125
    xdr_destroy(&xdr);
7126 7127
    return;

7128
xdr_cleanup:
7129
    xdr_destroy(&xdr);
7130
cleanup:
7131
    VIR_FREE(msg);
7132
}
7133

7134
static int
7135 7136 7137 7138
remoteDispatchNumOfSecrets(struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
7139
                           remote_error *rerr,
7140 7141
                           void *args ATTRIBUTE_UNUSED,
                           remote_num_of_secrets_ret *ret)
7142
{
7143 7144
    int rv = -1;

7145
    if (!conn) {
7146 7147
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7148 7149
    }

7150
    if ((ret->num = virConnectNumOfSecrets(conn)) < 0)
7151
        goto cleanup;
7152

7153 7154 7155 7156 7157 7158
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
7159 7160 7161
}

static int
7162 7163 7164 7165
remoteDispatchListSecrets(struct qemud_server *server ATTRIBUTE_UNUSED,
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
7166
                          remote_error *rerr,
7167 7168
                          remote_list_secrets_args *args,
                          remote_list_secrets_ret *ret)
7169
{
7170
    int rv = -1;
7171
    int len;
7172

7173
    if (!conn) {
7174 7175
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7176 7177
    }

7178
    if (args->maxuuids > REMOTE_SECRET_UUID_LIST_MAX) {
7179 7180 7181
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("maxuuids > REMOTE_SECRET_UUID_LIST_MAX"));
        goto cleanup;
7182 7183
    }

7184
    if (VIR_ALLOC_N(ret->uuids.uuids_val, args->maxuuids) < 0) {
7185 7186
        virReportOOMError();
        goto cleanup;
7187 7188
    }

7189 7190
    len = virConnectListSecrets(conn, ret->uuids.uuids_val,
                                args->maxuuids);
7191
    if (len < 0)
7192
        goto cleanup;
7193
    ret->uuids.uuids_len = len;
7194

7195 7196 7197 7198 7199 7200 7201 7202
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->uuids.uuids_val);
    }
    return rv;
7203 7204 7205
}

static int
7206 7207 7208 7209
remoteDispatchSecretDefineXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
7210
                              remote_error *rerr,
7211 7212
                              remote_secret_define_xml_args *args,
                              remote_secret_define_xml_ret *ret)
7213
{
7214 7215
    virSecretPtr secret = NULL;
    int rv = -1;
7216

7217
    if (!conn) {
7218 7219
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7220 7221
    }

7222
    if (!(secret = virSecretDefineXML(conn, args->xml, args->flags)))
7223
        goto cleanup;
7224

7225
    make_nonnull_secret(&ret->secret, secret);
7226 7227 7228 7229 7230 7231 7232 7233
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (secret)
        virSecretFree(secret);
    return rv;
7234 7235 7236
}

static int
7237 7238 7239 7240
remoteDispatchSecretGetValue(struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
7241
                             remote_error *rerr,
7242 7243
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
7244
{
7245
    virSecretPtr secret = NULL;
7246 7247
    size_t value_size;
    unsigned char *value;
7248
    int rv = -1;
7249

7250
    if (!conn) {
7251 7252
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7253 7254
    }

7255
    if (!(secret = get_nonnull_secret(conn, args->secret)))
7256
        goto cleanup;
7257

7258
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
7259
        goto cleanup;
7260 7261 7262

    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;
7263 7264 7265 7266 7267 7268 7269 7270 7271

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (secret)
        virSecretFree(secret);
    return rv;
7272 7273 7274
}

static int
7275 7276 7277 7278
remoteDispatchSecretGetXmlDesc(struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
7279
                               remote_error *rerr,
7280 7281
                               remote_secret_get_xml_desc_args *args,
                               remote_secret_get_xml_desc_ret *ret)
7282
{
7283 7284
    virSecretPtr secret = NULL;
    int rv = -1;
7285

7286
    if (!conn) {
7287 7288
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7289 7290
    }

7291
    if (!(secret = get_nonnull_secret(conn, args->secret)))
7292
        goto cleanup;
7293
    if (!(ret->xml = virSecretGetXMLDesc(secret, args->flags)))
7294 7295 7296 7297 7298 7299 7300 7301 7302 7303
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (secret)
        virSecretFree(secret);
    return rv;
7304 7305 7306
}

static int
7307 7308 7309 7310
remoteDispatchSecretLookupByUuid(struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
7311
                                 remote_error *rerr,
7312 7313
                                 remote_secret_lookup_by_uuid_args *args,
                                 remote_secret_lookup_by_uuid_ret *ret)
7314
{
7315 7316
    virSecretPtr secret = NULL;
    int rv = -1;
7317

7318
    if (!conn) {
7319 7320
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7321 7322
    }

7323
    if (!(secret = virSecretLookupByUUID(conn, (unsigned char *)args->uuid)))
7324
        goto cleanup;
7325

7326
    make_nonnull_secret(&ret->secret, secret);
7327 7328 7329 7330 7331 7332 7333 7334 7335

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (secret)
        virSecretFree(secret);
    return rv;
7336 7337 7338
}

static int
7339 7340 7341 7342
remoteDispatchSecretSetValue(struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
7343
                             remote_error *rerr,
7344 7345
                             remote_secret_set_value_args *args,
                             void *ret ATTRIBUTE_UNUSED)
7346
{
7347 7348
    virSecretPtr secret = NULL;
    int rv = -1;
7349

7350
    if (!conn) {
7351 7352
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7353 7354
    }

7355
    if (!(secret = get_nonnull_secret(conn, args->secret)))
7356
        goto cleanup;
7357
    if (virSecretSetValue(secret, (const unsigned char *)args->value.value_val,
7358
                          args->value.value_len, args->flags) < 0)
7359
        goto cleanup;
7360

7361 7362 7363 7364 7365 7366 7367 7368
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (secret)
        virSecretFree(secret);
    return rv;
7369 7370 7371
}

static int
7372 7373 7374 7375
remoteDispatchSecretUndefine(struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
7376
                             remote_error *rerr,
7377 7378
                             remote_secret_undefine_args *args,
                             void *ret ATTRIBUTE_UNUSED)
7379
{
7380 7381
    virSecretPtr secret = NULL;
    int rv = -1;
7382

7383
    if (!conn) {
7384 7385
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7386 7387
    }

7388
    if (!(secret = get_nonnull_secret(conn, args->secret)))
7389
        goto cleanup;
7390
    if (virSecretUndefine(secret) < 0)
7391
        goto cleanup;
7392

7393 7394 7395 7396 7397 7398 7399 7400
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (secret)
        virSecretFree(secret);
    return rv;
7401 7402
}

7403
static int
7404 7405 7406 7407
remoteDispatchSecretLookupByUsage(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
7408
                                  remote_error *rerr,
7409 7410
                                  remote_secret_lookup_by_usage_args *args,
                                  remote_secret_lookup_by_usage_ret *ret)
7411
{
7412 7413
    virSecretPtr secret = NULL;
    int rv = -1;
7414

7415
    if (!conn) {
7416 7417
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7418 7419
    }

7420
    if (!(secret = virSecretLookupByUsage(conn, args->usageType, args->usageID)))
7421
        goto cleanup;
7422

7423
    make_nonnull_secret(&ret->secret, secret);
7424 7425 7426 7427 7428 7429 7430 7431 7432

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (secret)
        virSecretFree(secret);
    return rv;
7433 7434
}

7435

7436 7437 7438 7439
static int remoteDispatchDomainIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                        struct qemud_client *client ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *hdr ATTRIBUTE_UNUSED,
7440
                                        remote_error *rerr,
7441 7442 7443
                                        remote_domain_is_active_args *args,
                                        remote_domain_is_active_ret *ret)
{
7444
    virDomainPtr dom = NULL;
7445
    int rv = -1;
7446

7447
    if (!conn) {
7448 7449
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7450 7451
    }

7452
    if (!(dom = get_nonnull_domain(conn, args->dom)))
7453
        goto cleanup;
7454

7455
    if ((ret->active = virDomainIsActive(dom)) < 0)
7456
        goto cleanup;
7457

7458 7459 7460 7461 7462
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
7463 7464
    if (dom)
        virDomainFree(dom);
7465
    return rv;
7466 7467 7468 7469 7470 7471
}

static int remoteDispatchDomainIsPersistent(struct qemud_server *server ATTRIBUTE_UNUSED,
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
7472
                                            remote_error *rerr,
7473 7474 7475
                                            remote_domain_is_persistent_args *args,
                                            remote_domain_is_persistent_ret *ret)
{
7476
    virDomainPtr dom = NULL;
7477
    int rv = -1;
7478

7479
    if (!conn) {
7480 7481
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7482 7483
    }

7484
    if (!(dom = get_nonnull_domain(conn, args->dom)))
7485
        goto cleanup;
7486

7487
    if ((ret->persistent = virDomainIsPersistent(dom)) < 0)
7488
        goto cleanup;
7489

7490 7491 7492 7493 7494
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
7495 7496
    if (dom)
        virDomainFree(dom);
7497
    return rv;
7498 7499
}

O
Osier Yang 已提交
7500 7501 7502 7503
static int remoteDispatchDomainIsUpdated(struct qemud_server *server ATTRIBUTE_UNUSED,
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
7504
                                            remote_error *rerr,
O
Osier Yang 已提交
7505 7506 7507
                                            remote_domain_is_updated_args *args,
                                            remote_domain_is_updated_ret *ret)
{
7508
    virDomainPtr dom = NULL;
7509
    int rv = -1;
O
Osier Yang 已提交
7510

7511
    if (!conn) {
7512 7513
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7514 7515
    }

7516
    if (!(dom = get_nonnull_domain(conn, args->dom)))
7517
        goto cleanup;
O
Osier Yang 已提交
7518

7519
    if ((ret->updated = virDomainIsUpdated(dom)) < 0)
7520
        goto cleanup;
O
Osier Yang 已提交
7521

7522 7523 7524 7525 7526
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
7527 7528
    if (dom)
        virDomainFree(dom);
7529
    return rv;
O
Osier Yang 已提交
7530 7531
}

7532 7533 7534 7535
static int remoteDispatchInterfaceIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                           struct qemud_client *client ATTRIBUTE_UNUSED,
                                           virConnectPtr conn,
                                           remote_message_header *hdr ATTRIBUTE_UNUSED,
7536
                                           remote_error *rerr,
7537 7538 7539
                                           remote_interface_is_active_args *args,
                                           remote_interface_is_active_ret *ret)
{
7540 7541
    virInterfacePtr iface = NULL;
    int rv = -1;
7542

7543
    if (!conn) {
7544 7545
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7546 7547
    }

7548
    if (!(iface = get_nonnull_interface(conn, args->iface)))
7549
        goto cleanup;
7550

7551
    if ((ret->active = virInterfaceIsActive(iface)) < 0)
7552
        goto cleanup;
7553

7554 7555 7556 7557 7558 7559 7560 7561
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (iface)
        virInterfaceFree(iface);
    return rv;
7562 7563 7564 7565 7566 7567
}

static int remoteDispatchNetworkIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                         struct qemud_client *client ATTRIBUTE_UNUSED,
                                         virConnectPtr conn,
                                         remote_message_header *hdr ATTRIBUTE_UNUSED,
7568
                                         remote_error *rerr,
7569 7570 7571
                                         remote_network_is_active_args *args,
                                         remote_network_is_active_ret *ret)
{
7572
    virNetworkPtr net = NULL;
7573
    int rv = -1;
7574

7575
    if (!conn) {
7576 7577
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7578 7579
    }

7580
    if (!(net = get_nonnull_network(conn, args->net)))
7581
        goto cleanup;
7582

7583
    if ((ret->active = virNetworkIsActive(net)) < 0)
7584
        goto cleanup;
7585

7586 7587 7588 7589 7590
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
7591 7592
    if (net)
        virNetworkFree(net);
7593
    return rv;
7594 7595 7596 7597 7598 7599
}

static int remoteDispatchNetworkIsPersistent(struct qemud_server *server ATTRIBUTE_UNUSED,
                                             struct qemud_client *client ATTRIBUTE_UNUSED,
                                             virConnectPtr conn,
                                             remote_message_header *hdr ATTRIBUTE_UNUSED,
7600
                                             remote_error *rerr,
7601 7602 7603
                                             remote_network_is_persistent_args *args,
                                             remote_network_is_persistent_ret *ret)
{
7604
    virNetworkPtr net = NULL;
7605
    int rv = -1;
7606

7607
    if (!conn) {
7608 7609
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7610 7611
    }

7612
    if (!(net = get_nonnull_network(conn, args->net)))
7613
        goto cleanup;
7614

7615
    if ((ret->persistent = virNetworkIsPersistent(net)) < 0)
7616
        goto cleanup;
7617

7618 7619 7620 7621 7622
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
7623 7624
    if (net)
        virNetworkFree(net);
7625
    return rv;
7626 7627 7628 7629 7630 7631
}

static int remoteDispatchStoragePoolIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                             struct qemud_client *client ATTRIBUTE_UNUSED,
                                             virConnectPtr conn,
                                             remote_message_header *hdr ATTRIBUTE_UNUSED,
7632
                                             remote_error *rerr,
7633 7634 7635
                                             remote_storage_pool_is_active_args *args,
                                             remote_storage_pool_is_active_ret *ret)
{
7636 7637
    virStoragePoolPtr pool = NULL;
    int rv = -1;
7638

7639
    if (!conn) {
7640 7641
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7642 7643
    }

7644
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
7645
        goto cleanup;
7646

7647
    if ((ret->active = virStoragePoolIsActive(pool)) < 0)
7648
        goto cleanup;
7649

7650 7651 7652 7653 7654 7655 7656 7657
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
7658 7659 7660 7661 7662 7663
}

static int remoteDispatchStoragePoolIsPersistent(struct qemud_server *server ATTRIBUTE_UNUSED,
                                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                                 virConnectPtr conn,
                                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
7664
                                                 remote_error *rerr,
7665 7666 7667
                                                 remote_storage_pool_is_persistent_args *args,
                                                 remote_storage_pool_is_persistent_ret *ret)
{
7668 7669
    virStoragePoolPtr pool = NULL;
    int rv = -1;
7670

7671
    if (!conn) {
7672 7673
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7674 7675
    }

7676
    if (!(pool = get_nonnull_storage_pool(conn, args->pool)))
7677
        goto cleanup;
7678

7679
    if ((ret->persistent = virStoragePoolIsPersistent(pool)) < 0)
7680
        goto cleanup;
7681

7682 7683 7684 7685 7686 7687 7688 7689
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (pool)
        virStoragePoolFree(pool);
    return rv;
7690 7691 7692 7693 7694 7695 7696
}


static int remoteDispatchIsSecure(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
7697
                                  remote_error *rerr,
7698 7699 7700
                                  void *args ATTRIBUTE_UNUSED,
                                  remote_is_secure_ret *ret)
{
7701 7702
    int rv = -1;

7703
    if (!conn) {
7704 7705
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7706 7707
    }

7708
    if ((ret->secure = virConnectIsSecure(conn)) < 0)
7709
        goto cleanup;
7710

7711 7712 7713 7714 7715 7716
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
7717 7718 7719
}


7720 7721 7722 7723 7724
static int
remoteDispatchCpuCompare(struct qemud_server *server ATTRIBUTE_UNUSED,
                         struct qemud_client *client ATTRIBUTE_UNUSED,
                         virConnectPtr conn,
                         remote_message_header *hdr ATTRIBUTE_UNUSED,
7725
                         remote_error *rerr,
7726 7727 7728 7729
                         remote_cpu_compare_args *args,
                         remote_cpu_compare_ret *ret)
{
    int result;
7730
    int rv = -1;
7731

7732
    if (!conn) {
7733 7734
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7735 7736
    }

7737
    if ((result = virConnectCompareCPU(conn, args->xml, args->flags)) == VIR_CPU_COMPARE_ERROR)
7738
        goto cleanup;
7739 7740

    ret->result = result;
7741 7742 7743 7744 7745 7746
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
7747 7748 7749
}


7750 7751 7752 7753 7754
static int
remoteDispatchCpuBaseline(struct qemud_server *server ATTRIBUTE_UNUSED,
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
7755
                          remote_error *rerr,
7756 7757 7758 7759
                          remote_cpu_baseline_args *args,
                          remote_cpu_baseline_ret *ret)
{
    char *cpu;
7760
    int rv = -1;
7761

7762
    if (!conn) {
7763 7764
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7765 7766
    }

7767 7768 7769 7770
    if (!(cpu = virConnectBaselineCPU(conn,
                                      (const char **) args->xmlCPUs.xmlCPUs_val,
                                      args->xmlCPUs.xmlCPUs_len,
                                      args->flags)))
7771
        goto cleanup;
7772 7773

    ret->cpu = cpu;
7774

7775 7776 7777 7778 7779 7780
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
7781 7782 7783 7784
}


static int
7785 7786 7787 7788 7789 7790 7791
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)
7792
{
7793
    virDomainPtr dom = NULL;
7794
    virDomainJobInfo info;
7795
    int rv = -1;
7796

7797
    if (!conn) {
7798 7799
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7800 7801
    }

7802
    if (!(dom = get_nonnull_domain(conn, args->dom)))
7803
        goto cleanup;
7804

7805
    if (virDomainGetJobInfo(dom, &info) < 0)
7806
        goto cleanup;
7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820

    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;

7821
    rv = 0;
7822

7823 7824 7825 7826 7827 7828
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
7829 7830 7831
}


7832
static int
7833 7834 7835 7836 7837 7838 7839
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)
7840
{
7841 7842
    virDomainPtr dom = NULL;
    int rv = -1;
7843

7844
    if (!conn) {
7845 7846
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7847 7848
    }

7849
    if (!(dom = get_nonnull_domain(conn, args->dom)))
7850
        goto cleanup;
7851

7852
    if (virDomainAbortJob(dom) < 0)
7853
        goto cleanup;
7854

7855
    rv = 0;
7856

7857 7858 7859 7860 7861 7862
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
7863 7864 7865
}


7866 7867 7868 7869 7870 7871 7872 7873 7874
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)
{
7875 7876
    virDomainPtr dom = NULL;
    int rv = -1;
7877

7878
    if (!conn) {
7879 7880
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7881 7882
    }

7883
    if (!(dom = get_nonnull_domain(conn, args->dom)))
7884
        goto cleanup;
7885

7886
    if (virDomainMigrateSetMaxDowntime(dom, args->downtime, args->flags) < 0)
7887
        goto cleanup;
7888

7889
    rv = 0;
7890

7891 7892 7893 7894 7895 7896
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907
}

static int
remoteDispatchDomainMigrateSetMaxSpeed(struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_migrate_set_max_speed_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
7908 7909
    virDomainPtr dom = NULL;
    int rv = -1;
7910

7911
    if (!conn) {
7912 7913
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7914 7915
    }

7916
    if (!(dom = get_nonnull_domain(conn, args->dom)))
7917
        goto cleanup;
7918

7919
    if (virDomainMigrateSetMaxSpeed(dom, args->bandwidth, args->flags) < 0)
7920
        goto cleanup;
7921

7922
    rv = 0;
7923

7924 7925 7926 7927 7928 7929
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
7930 7931
}

C
Chris Lalancette 已提交
7932
static int
7933 7934 7935 7936 7937 7938 7939
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)
C
Chris Lalancette 已提交
7940
{
7941
    virDomainSnapshotPtr snapshot = NULL;
7942
    virDomainPtr dom = NULL;
7943
    int rv = -1;
C
Chris Lalancette 已提交
7944

7945
    if (!conn) {
7946 7947
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7948 7949
    }

7950
    if (!(dom = get_nonnull_domain(conn, args->domain)))
7951
        goto cleanup;
C
Chris Lalancette 已提交
7952

7953
    if (!(snapshot = virDomainSnapshotCreateXML(dom, args->xml_desc, args->flags)))
7954
        goto cleanup;
C
Chris Lalancette 已提交
7955 7956 7957

    make_nonnull_domain_snapshot(&ret->snap, snapshot);

7958
    rv = 0;
C
Chris Lalancette 已提交
7959

7960 7961 7962 7963 7964
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (snapshot)
        virDomainSnapshotFree(snapshot);
7965 7966
    if (dom)
        virDomainFree(dom);
7967
    return rv;
C
Chris Lalancette 已提交
7968 7969 7970
}

static int
7971 7972 7973 7974 7975 7976 7977
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)
C
Chris Lalancette 已提交
7978
{
7979
    virDomainPtr dom = NULL;
7980
    virDomainSnapshotPtr snapshot = NULL;
7981
    int rv = -1;
C
Chris Lalancette 已提交
7982

7983
    if (!conn) {
7984 7985
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
7986 7987
    }

7988
    if (!(dom = get_nonnull_domain(conn, args->snap.domain)))
7989 7990
        goto cleanup;

7991
    if (!(snapshot = get_nonnull_domain_snapshot(dom, args->snap)))
7992
        goto cleanup;
C
Chris Lalancette 已提交
7993 7994

    /* remoteDispatchClientRequest will free this. */
7995
    if (!(ret->xml = virDomainSnapshotGetXMLDesc(snapshot, args->flags)))
7996 7997
        goto cleanup;

7998
    rv = 0;
7999 8000

cleanup:
8001 8002
    if (rv < 0)
        remoteDispatchError(rerr);
8003
    if (snapshot)
C
Chris Lalancette 已提交
8004
        virDomainSnapshotFree(snapshot);
8005 8006
    if (dom)
        virDomainFree(dom);
8007
    return rv;
C
Chris Lalancette 已提交
8008 8009 8010
}

static int
8011 8012 8013 8014 8015 8016 8017
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)
C
Chris Lalancette 已提交
8018
{
8019
    virDomainPtr dom = NULL;
8020
    int rv = -1;
C
Chris Lalancette 已提交
8021

8022
    if (!conn) {
8023 8024
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8025 8026
    }

8027
    if (!(dom = get_nonnull_domain(conn, args->domain)))
8028
        goto cleanup;
C
Chris Lalancette 已提交
8029

8030
    if ((ret->num = virDomainSnapshotNum(dom, args->flags)) < 0)
8031
        goto cleanup;
C
Chris Lalancette 已提交
8032

8033
    rv = 0;
C
Chris Lalancette 已提交
8034

8035 8036 8037
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
8038 8039
    if (dom)
        virDomainFree(dom);
8040
    return rv;
C
Chris Lalancette 已提交
8041 8042 8043
}

static int
8044 8045 8046 8047 8048 8049 8050
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)
C
Chris Lalancette 已提交
8051
{
8052
    virDomainPtr dom = NULL;
8053
    int rv = -1;
8054
    int len;
C
Chris Lalancette 已提交
8055

8056
    if (!conn) {
8057 8058
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8059 8060
    }

C
Chris Lalancette 已提交
8061
    if (args->nameslen > REMOTE_DOMAIN_SNAPSHOT_LIST_NAMES_MAX) {
8062 8063 8064
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("nameslen > REMOTE_DOMAIN_SNAPSHOT_LIST_NAMES_MAX"));
        goto cleanup;
C
Chris Lalancette 已提交
8065 8066
    }

8067
    if (!(dom = get_nonnull_domain(conn, args->domain)))
8068
        goto cleanup;
C
Chris Lalancette 已提交
8069 8070 8071

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->nameslen) < 0) {
8072 8073
        virReportOOMError();
        goto cleanup;
C
Chris Lalancette 已提交
8074 8075
    }

8076
    len = virDomainSnapshotListNames(dom,
8077 8078 8079
                                     ret->names.names_val,
                                     args->nameslen,
                                     args->flags);
8080
    if (len < 0)
8081
        goto cleanup;
8082
    ret->names.names_len = len;
C
Chris Lalancette 已提交
8083

8084
    rv = 0;
C
Chris Lalancette 已提交
8085

8086 8087 8088 8089 8090
cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
        VIR_FREE(ret->names.names_val);
    }
8091 8092
    if (dom)
        virDomainFree(dom);
8093
    return rv;
C
Chris Lalancette 已提交
8094 8095 8096
}

static int
8097 8098 8099 8100 8101 8102 8103
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)
C
Chris Lalancette 已提交
8104
{
8105
    virDomainSnapshotPtr snapshot = NULL;
8106
    virDomainPtr dom = NULL;
8107
    int rv = -1;
C
Chris Lalancette 已提交
8108

8109
    if (!conn) {
8110 8111
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8112 8113
    }

8114
    if (!(dom = get_nonnull_domain(conn, args->domain)))
8115
        goto cleanup;
C
Chris Lalancette 已提交
8116

8117
    if (!(snapshot = virDomainSnapshotLookupByName(dom, args->name, args->flags)))
8118
        goto cleanup;
C
Chris Lalancette 已提交
8119

8120
    make_nonnull_domain_snapshot(&ret->snap, snapshot);
C
Chris Lalancette 已提交
8121

8122
    rv = 0;
C
Chris Lalancette 已提交
8123

8124 8125 8126 8127 8128
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (snapshot)
        virDomainSnapshotFree(snapshot);
8129 8130
    if (dom)
        virDomainFree(dom);
8131
    return rv;
C
Chris Lalancette 已提交
8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142
}

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)
{
8143
    virDomainPtr dom = NULL;
C
Chris Lalancette 已提交
8144
    int result;
8145
    int rv = -1;
C
Chris Lalancette 已提交
8146

8147
    if (!conn) {
8148 8149
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8150 8151
    }

8152
    if (!(dom = get_nonnull_domain(conn, args->domain)))
8153
        goto cleanup;
C
Chris Lalancette 已提交
8154

8155
    result = virDomainHasCurrentSnapshot(dom, args->flags);
8156
    if (result < 0)
8157
        goto cleanup;
C
Chris Lalancette 已提交
8158 8159 8160

    ret->result = result;

8161
    rv = 0;
C
Chris Lalancette 已提交
8162

8163 8164 8165
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
8166 8167
    if (dom)
        virDomainFree(dom);
8168
    return rv;
C
Chris Lalancette 已提交
8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179
}

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)
{
8180
    virDomainSnapshotPtr snapshot = NULL;
8181
    virDomainPtr dom = NULL;
8182
    int rv = -1;
C
Chris Lalancette 已提交
8183

8184
    if (!conn) {
8185 8186
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8187 8188
    }

8189
    if (!(dom = get_nonnull_domain(conn, args->domain)))
8190
        goto cleanup;
C
Chris Lalancette 已提交
8191

8192
    if (!(snapshot = virDomainSnapshotCurrent(dom, args->flags)))
8193
        goto cleanup;
C
Chris Lalancette 已提交
8194 8195 8196

    make_nonnull_domain_snapshot(&ret->snap, snapshot);

8197
    rv = 0;
C
Chris Lalancette 已提交
8198

8199 8200 8201 8202 8203
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (snapshot)
        virDomainSnapshotFree(snapshot);
8204 8205
    if (dom)
        virDomainFree(dom);
8206
    return rv;
C
Chris Lalancette 已提交
8207 8208 8209
}

static int
8210 8211 8212 8213 8214 8215 8216
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)
C
Chris Lalancette 已提交
8217
{
8218
    virDomainPtr dom = NULL;
8219
    virDomainSnapshotPtr snapshot = NULL;
8220
    int rv = -1;
C
Chris Lalancette 已提交
8221

8222
    if (!conn) {
8223 8224
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8225 8226
    }

8227
    if (!(dom = get_nonnull_domain(conn, args->snap.domain)))
8228 8229
        goto cleanup;

8230
    if (!(snapshot = get_nonnull_domain_snapshot(dom, args->snap)))
8231
        goto cleanup;
C
Chris Lalancette 已提交
8232

8233
    if (virDomainRevertToSnapshot(snapshot, args->flags) < 0)
8234 8235
        goto cleanup;

8236
    rv = 0;
8237 8238

cleanup:
8239 8240
    if (rv < 0)
        remoteDispatchError(rerr);
8241
    if (snapshot)
C
Chris Lalancette 已提交
8242
        virDomainSnapshotFree(snapshot);
8243 8244
    if (dom)
        virDomainFree(dom);
8245
    return rv;
C
Chris Lalancette 已提交
8246 8247 8248
}

static int
8249 8250 8251 8252 8253 8254 8255
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)
C
Chris Lalancette 已提交
8256
{
8257
    virDomainPtr dom = NULL;
8258
    virDomainSnapshotPtr snapshot = NULL;
8259
    int rv = -1;
C
Chris Lalancette 已提交
8260

8261
    if (!conn) {
8262 8263
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8264 8265
    }

8266
    if (!(dom = get_nonnull_domain(conn, args->snap.domain)))
8267 8268
        goto cleanup;

8269
    if (!(snapshot = get_nonnull_domain_snapshot(dom, args->snap)))
8270
        goto cleanup;
C
Chris Lalancette 已提交
8271

8272
    if (virDomainSnapshotDelete(snapshot, args->flags) < 0)
8273 8274
        goto cleanup;

8275
    rv = 0;
8276 8277

cleanup:
8278 8279
    if (rv < 0)
        remoteDispatchError(rerr);
8280
    if (snapshot)
C
Chris Lalancette 已提交
8281
        virDomainSnapshotFree(snapshot);
8282 8283
    if (dom)
        virDomainFree(dom);
8284
    return rv;
C
Chris Lalancette 已提交
8285 8286
}

8287

8288
static int
8289 8290 8291 8292 8293 8294 8295
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)
8296 8297
{
    int callbackID;
8298
    int rv = -1;
8299

8300
    if (!conn) {
8301 8302
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8303 8304
    }

8305 8306
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
8307 8308
        virNetError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
        goto cleanup;
8309 8310 8311
    }

    if (client->domainEventCallbackID[args->eventID] != -1)  {
8312 8313
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), args->eventID);
        goto cleanup;
8314 8315 8316 8317 8318 8319
    }

    if ((callbackID = virConnectDomainEventRegisterAny(conn,
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
8320
                                                       client, NULL)) < 0)
8321
        goto cleanup;
8322 8323 8324

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

8325 8326 8327 8328 8329 8330
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
8331 8332 8333 8334
}


static int
8335 8336 8337 8338 8339 8340 8341
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)
8342 8343
{
    int callbackID = -1;
8344
    int rv = -1;
8345

8346
    if (!conn) {
8347 8348
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8349 8350
    }

8351 8352
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
8353 8354
        virNetError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
        goto cleanup;
8355 8356
    }

8357
    if ((callbackID = client->domainEventCallbackID[args->eventID]) < 0) {
8358 8359
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), args->eventID);
        goto cleanup;
8360 8361
    }

8362
    if (virConnectDomainEventDeregisterAny(conn, callbackID) < 0)
8363
        goto cleanup;
8364 8365

    client->domainEventCallbackID[args->eventID] = -1;
8366 8367 8368 8369 8370 8371
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
8372 8373 8374
}


8375 8376

static int
8377 8378 8379 8380 8381 8382 8383
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)
8384
{
8385 8386
    virNWFilterPtr nwfilter = NULL;
    int rv = -1;
8387

8388
    if (!conn) {
8389 8390
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8391 8392
    }

8393
    if (!(nwfilter = virNWFilterLookupByName(conn, args->name)))
8394
        goto cleanup;
8395

8396
    make_nonnull_nwfilter(&ret->nwfilter, nwfilter);
8397 8398 8399 8400 8401 8402 8403 8404 8405

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (nwfilter)
        virNWFilterFree(nwfilter);
    return rv;
8406 8407 8408
}

static int
8409 8410 8411 8412 8413 8414 8415
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)
8416
{
8417 8418
    virNWFilterPtr nwfilter = NULL;
    int rv = -1;
8419

8420
    if (!conn) {
8421 8422
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8423 8424
    }

8425
    if (!(nwfilter = virNWFilterLookupByUUID(conn, (unsigned char *) args->uuid)))
8426
        goto cleanup;
8427

8428
    make_nonnull_nwfilter(&ret->nwfilter, nwfilter);
8429 8430 8431 8432 8433 8434 8435 8436 8437

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (nwfilter)
        virNWFilterFree(nwfilter);
    return rv;
8438 8439 8440 8441
}


static int
8442 8443 8444 8445 8446 8447 8448
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)
8449
{
8450 8451
    virNWFilterPtr nwfilter = NULL;
    int rv = -1;
8452

8453
    if (!conn) {
8454 8455
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8456 8457
    }

8458
    if (!(nwfilter = virNWFilterDefineXML(conn, args->xml)))
8459
        goto cleanup;
8460

8461
    make_nonnull_nwfilter(&ret->nwfilter, nwfilter);
8462 8463 8464 8465 8466 8467 8468 8469 8470

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (nwfilter)
        virNWFilterFree(nwfilter);
    return rv;
8471 8472 8473 8474
}


static int
8475 8476 8477 8478 8479 8480 8481
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)
8482
{
8483 8484
    virNWFilterPtr nwfilter = NULL;
    int rv = -1;
8485

8486
    if (!conn) {
8487 8488
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8489 8490
    }

8491
    if (!(nwfilter = get_nonnull_nwfilter(conn, args->nwfilter)))
8492
        goto cleanup;
8493

8494
    if (virNWFilterUndefine(nwfilter) < 0)
8495 8496 8497 8498 8499 8500 8501 8502 8503 8504
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (nwfilter)
        virNWFilterFree(nwfilter);
    return rv;
8505 8506 8507
}

static int
8508 8509 8510 8511 8512 8513 8514
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)
8515
{
8516
    int rv = -1;
8517
    int len;
8518

8519
    if (!conn) {
8520 8521
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8522
    }
8523 8524

    if (args->maxnames > REMOTE_NWFILTER_NAME_LIST_MAX) {
8525 8526 8527
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("maxnames > REMOTE_NWFILTER_NAME_LIST_MAX"));
        goto cleanup;
8528 8529 8530 8531
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
8532 8533
        virReportOOMError();
        goto cleanup;
8534 8535
    }

8536 8537
    len = virConnectListNWFilters(conn,
                                  ret->names.names_val, args->maxnames);
8538
    if (len < 0)
8539
        goto cleanup;
8540
    ret->names.names_len = len;
8541

8542 8543 8544 8545 8546
    rv = 0;

cleanup:
    if (rv < 0) {
        remoteDispatchError(rerr);
8547
        VIR_FREE(ret->names.names_val);
8548 8549
    }
    return rv;
8550 8551 8552 8553
}


static int
8554 8555 8556 8557 8558 8559 8560
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)
8561
{
8562 8563
    virNWFilterPtr nwfilter = NULL;
    int rv = -1;
8564

8565
    if (!conn) {
8566 8567
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8568 8569
    }

8570
    if (!(nwfilter = get_nonnull_nwfilter(conn, args->nwfilter)))
8571
        goto cleanup;
8572 8573

    /* remoteDispatchClientRequest will free this. */
8574
    if (!(ret->xml = virNWFilterGetXMLDesc(nwfilter, args->flags)))
8575 8576 8577 8578 8579 8580 8581 8582 8583 8584
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (nwfilter)
        virNWFilterFree(nwfilter);
    return rv;
8585 8586 8587 8588
}


static int
8589 8590 8591 8592 8593 8594 8595
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)
8596
{
8597 8598
    int rv = -1;

8599
    if (!conn) {
8600 8601
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8602
    }
8603

8604
    if ((ret->num = virConnectNumOfNWFilters(conn)) < 0)
8605
        goto cleanup;
8606

8607 8608 8609 8610 8611 8612
    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    return rv;
8613 8614 8615
}


8616
static int
8617 8618 8619 8620 8621 8622 8623
remoteDispatchDomainGetBlockInfo(struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_domain_get_block_info_args *args,
                                 remote_domain_get_block_info_ret *ret)
8624
{
8625
    virDomainPtr dom = NULL;
8626
    virDomainBlockInfo info;
8627
    int rv = -1;
8628

8629
    if (!conn) {
8630 8631
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8632 8633
    }

8634
    if (!(dom = get_nonnull_domain(conn, args->dom)))
8635
        goto cleanup;
8636

8637
    if (virDomainGetBlockInfo(dom, args->path, &info, args->flags) < 0)
8638
        goto cleanup;
8639 8640 8641 8642 8643

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

8644
    rv = 0;
8645

8646 8647 8648 8649 8650 8651
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
8652 8653
}

C
Chris Lalancette 已提交
8654
static int
8655 8656 8657 8658 8659 8660 8661
qemuDispatchMonitorCommand(struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *rerr,
                           qemu_monitor_command_args *args,
                           qemu_monitor_command_ret *ret)
C
Chris Lalancette 已提交
8662
{
8663
    virDomainPtr dom = NULL;
8664
    int rv = -1;
C
Chris Lalancette 已提交
8665

8666
    if (!conn) {
8667 8668
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8669 8670
    }

8671
    if (!(dom = get_nonnull_domain(conn, args->domain)))
8672
        goto cleanup;
C
Chris Lalancette 已提交
8673

8674
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
8675
                                    args->flags) < 0)
8676
        goto cleanup;
C
Chris Lalancette 已提交
8677

8678
    rv = 0;
C
Chris Lalancette 已提交
8679

8680 8681 8682
cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
8683 8684
    if (dom)
        virDomainFree(dom);
8685
    return rv;
C
Chris Lalancette 已提交
8686 8687
}

8688

8689 8690 8691 8692 8693 8694 8695 8696 8697
static int
remoteDispatchDomainOpenConsole(struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client,
                                virConnectPtr conn,
                                remote_message_header *hdr,
                                remote_error *rerr,
                                remote_domain_open_console_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
8698 8699 8700
    struct qemud_client_stream *stream = NULL;
    virDomainPtr dom = NULL;
    int rv = -1;
8701

8702
    if (!conn) {
8703 8704
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
8705
    }
8706

8707
    if (!(dom = get_nonnull_domain(conn, args->domain)))
8708
        goto cleanup;
8709

8710
    if (!(stream = remoteCreateClientStream(conn, hdr))) {
8711 8712
        virReportOOMError();
        goto cleanup;
8713 8714
    }

8715
    if (virDomainOpenConsole(dom,
8716 8717
                             args->devname ? *args->devname : NULL,
                             stream->st,
8718
                             args->flags) < 0)
8719
        goto cleanup;
8720

8721
    if (remoteAddClientStream(client, stream, 1) < 0)
8722 8723 8724 8725 8726 8727 8728 8729
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        remoteDispatchError(rerr);
    if (stream && rv < 0) {
8730 8731 8732
        virStreamAbort(stream->st);
        remoteFreeClientStream(client, stream);
    }
8733 8734 8735
    if (dom)
        virDomainFree(dom);
    return rv;
8736 8737 8738
}


8739 8740 8741 8742 8743 8744 8745 8746 8747
/*----- 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
8748
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
8749 8750
{
    virDomainPtr dom;
8751
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
8752 8753 8754 8755 8756 8757 8758 8759
    /* 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
8760
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
8761
{
8762
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
8763 8764
}

D
Daniel Veillard 已提交
8765
static virInterfacePtr
8766
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
8767
{
8768
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
8769 8770
}

8771
static virStoragePoolPtr
8772
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
8773
{
8774
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid);
8775 8776 8777
}

static virStorageVolPtr
8778
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
8779 8780
{
    virStorageVolPtr ret;
8781
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key);
8782 8783 8784
    return ret;
}

8785
static virSecretPtr
8786
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
8787
{
8788
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
8789 8790
}

8791
static virNWFilterPtr
8792
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
8793
{
8794
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
8795 8796
}

C
Chris Lalancette 已提交
8797
static virDomainSnapshotPtr
8798
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
8799
{
8800
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
8801 8802
}

8803 8804
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
8805
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
8806 8807
{
    dom_dst->id = dom_src->id;
8808 8809
    dom_dst->name = strdup(dom_src->name);
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
8810 8811 8812
}

static void
8813
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
8814
{
8815 8816
    net_dst->name = strdup(net_src->name);
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
8817 8818
}

D
Daniel Veillard 已提交
8819
static void
8820 8821
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
8822
{
8823 8824
    interface_dst->name = strdup(interface_src->name);
    interface_dst->mac = strdup(interface_src->mac);
D
Daniel Veillard 已提交
8825 8826
}

8827
static void
8828
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
8829
{
8830 8831
    pool_dst->name = strdup(pool_src->name);
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
8832 8833 8834
}

static void
8835
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
8836
{
8837 8838 8839
    vol_dst->pool = strdup(vol_src->pool);
    vol_dst->name = strdup(vol_src->name);
    vol_dst->key = strdup(vol_src->key);
8840
}
8841 8842

static void
8843
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
8844 8845 8846
{
    dev_dst->name = strdup(dev_src->name);
}
8847 8848

static void
8849
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
8850
{
8851
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
8852
    secret_dst->usageType = secret_src->usageType;
8853
    secret_dst->usageID = strdup(secret_src->usageID);
8854
}
8855 8856

static void
8857
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
8858
{
8859 8860
    nwfilter_dst->name = strdup(nwfilter_src->name);
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
8861
}
C
Chris Lalancette 已提交
8862 8863

static void
8864
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
8865 8866 8867 8868
{
    snapshot_dst->name = strdup(snapshot_src->name);
    make_nonnull_domain(&snapshot_dst->domain, snapshot_src->domain);
}