remote.c 178.2 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007, 2008, 2009 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 "virterror_internal.h"
45

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

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

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

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

63 64
static virDomainPtr get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network (virConnectPtr conn, remote_nonnull_network network);
65
static virInterfacePtr get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface);
66 67
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);
68
static virSecretPtr get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret);
69 70
static void make_nonnull_domain (remote_nonnull_domain *dom_dst, virDomainPtr dom_src);
static void make_nonnull_network (remote_nonnull_network *net_dst, virNetworkPtr net_src);
D
Daniel Veillard 已提交
71
static void make_nonnull_interface (remote_nonnull_interface *interface_dst, virInterfacePtr interface_src);
72 73
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);
74
static void make_nonnull_node_device (remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src);
75
static void make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src);
76

77

78
#include "remote_dispatch_prototypes.h"
79 80 81 82

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

84 85 86 87 88 89 90 91 92 93
const dispatch_data const *remoteGetDispatchData(int proc)
{
    if (proc >= ARRAY_CARDINALITY(dispatch_table) ||
        dispatch_table[proc].fn == NULL) {
        return NULL;
    }

    return &(dispatch_table[proc]);
}

94 95 96
/* Prototypes */
static void
remoteDispatchDomainEventSend (struct qemud_client *client,
97
                               remote_domain_event_msg *data);
98

99
int remoteRelayDomainEvent (virConnectPtr conn ATTRIBUTE_UNUSED,
100 101 102 103
                            virDomainPtr dom,
                            int event,
                            int detail,
                            void *opaque)
104 105
{
    struct qemud_client *client = opaque;
106
    REMOTE_DEBUG("Relaying domain event %d %d", event, detail);
107

108
    if (client) {
109 110
        remote_domain_event_msg data;

111 112
        virMutexLock(&client->lock);

113 114 115 116 117
        /* build return data */
        memset(&data, 0, sizeof data);
        make_nonnull_domain (&data.dom, dom);
        data.event = event;
        data.detail = detail;
118

119
        remoteDispatchDomainEventSend (client, &data);
120 121

        virMutexUnlock(&client->lock);
122 123 124
    }
    return 0;
}
125 126


127 128 129
/*----- Functions. -----*/

static int
130
remoteDispatchOpen (struct qemud_server *server,
131
                    struct qemud_client *client,
132
                    virConnectPtr conn,
133
                    remote_message_header *hdr ATTRIBUTE_UNUSED,
134
                    remote_error *rerr,
135 136 137
                    struct remote_open_args *args, void *ret ATTRIBUTE_UNUSED)
{
    const char *name;
138
    int flags, rc;
139 140

    /* Already opened? */
141
    if (conn) {
142 143
        remoteDispatchFormatError (rerr, "%s", _("connection already open"));
        return -1;
144 145
    }

146 147 148
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
149

150 151 152 153 154 155 156 157 158 159 160 161 162
    name = args->name ? *args->name : NULL;

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

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

163
    if (client->conn == NULL)
164 165
        remoteDispatchConnError(rerr, NULL);

166
    rc = client->conn ? 0 : -1;
167
    virMutexUnlock(&client->lock);
168
    return rc;
169 170
}

171 172 173 174
#define CHECK_CONN(client)                                              \
    if (!client->conn) {                                                \
        remoteDispatchFormatError (rerr, "%s", _("connection not open")); \
        return -1;                                                      \
175 176 177
    }

static int
178
remoteDispatchClose (struct qemud_server *server ATTRIBUTE_UNUSED,
179 180
                     struct qemud_client *client ATTRIBUTE_UNUSED,
                     virConnectPtr conn ATTRIBUTE_UNUSED,
181
                     remote_message_header *hdr ATTRIBUTE_UNUSED,
182
                     remote_error *rerr ATTRIBUTE_UNUSED,
183 184
                     void *args ATTRIBUTE_UNUSED, void *ret ATTRIBUTE_UNUSED)
{
185 186 187
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
188

189
    client->closing = 1;
190

191
    virMutexUnlock(&client->lock);
192
    return 0;
193 194
}

195
static int
196
remoteDispatchSupportsFeature (struct qemud_server *server ATTRIBUTE_UNUSED,
197 198
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
199
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
200
                               remote_error *rerr,
201 202
                               remote_supports_feature_args *args, remote_supports_feature_ret *ret)
{
203
    ret->supported = virDrvSupportsFeature (conn, args->feature);
204 205

    if (ret->supported == -1) {
206
        remoteDispatchConnError(rerr, conn);
207 208
        return -1;
    }
209 210 211 212

    return 0;
}

213
static int
214
remoteDispatchGetType (struct qemud_server *server ATTRIBUTE_UNUSED,
215 216
                       struct qemud_client *client ATTRIBUTE_UNUSED,
                       virConnectPtr conn,
217
                       remote_message_header *hdr ATTRIBUTE_UNUSED,
218
                       remote_error *rerr,
219 220 221 222
                       void *args ATTRIBUTE_UNUSED, remote_get_type_ret *ret)
{
    const char *type;

223
    type = virConnectGetType (conn);
224
    if (type == NULL) {
225
        remoteDispatchConnError(rerr, conn);
226 227
        return -1;
    }
228 229 230 231 232 233

    /* We have to strdup because remoteDispatchClientRequest will
     * free this string after it's been serialised.
     */
    ret->type = strdup (type);
    if (!ret->type) {
234
        remoteDispatchOOMError(rerr);
235
        return -1;
236 237 238 239 240 241
    }

    return 0;
}

static int
242
remoteDispatchGetVersion (struct qemud_server *server ATTRIBUTE_UNUSED,
243 244
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
245
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
246
                          remote_error *rerr,
247 248 249 250 251
                          void *args ATTRIBUTE_UNUSED,
                          remote_get_version_ret *ret)
{
    unsigned long hvVer;

252 253
    if (virConnectGetVersion (conn, &hvVer) == -1) {
        remoteDispatchConnError(rerr, conn);
254
        return -1;
255
    }
256 257 258 259 260

    ret->hv_ver = hvVer;
    return 0;
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
static int
remoteDispatchGetLibVersion (struct qemud_server *server ATTRIBUTE_UNUSED,
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
                             remote_error *rerr,
                             void *args ATTRIBUTE_UNUSED,
                             remote_get_lib_version_ret *ret)
{
    unsigned long libVer;

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

    ret->lib_ver = libVer;
    return 0;
}

281
static int
282
remoteDispatchGetHostname (struct qemud_server *server ATTRIBUTE_UNUSED,
283 284
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
285
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
286
                           remote_error *rerr,
287 288 289 290 291
                           void *args ATTRIBUTE_UNUSED,
                           remote_get_hostname_ret *ret)
{
    char *hostname;

292
    hostname = virConnectGetHostname (conn);
293
    if (hostname == NULL) {
294
        remoteDispatchConnError(rerr, conn);
295 296
        return -1;
    }
297 298 299 300 301

    ret->hostname = hostname;
    return 0;
}

302 303
static int
remoteDispatchGetUri (struct qemud_server *server ATTRIBUTE_UNUSED,
304 305
                      struct qemud_client *client ATTRIBUTE_UNUSED,
                      virConnectPtr conn,
306
                      remote_message_header *hdr ATTRIBUTE_UNUSED,
307
                      remote_error *rerr,
308 309 310 311 312 313
                      void *args ATTRIBUTE_UNUSED,
                      remote_get_uri_ret *ret)
{
    char *uri;
    CHECK_CONN(client);

314
    uri = virConnectGetURI (conn);
315
    if (uri == NULL) {
316
        remoteDispatchConnError(rerr, conn);
317 318
        return -1;
    }
319 320 321 322 323

    ret->uri = uri;
    return 0;
}

324
static int
325
remoteDispatchGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
326 327
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
328
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
329
                           remote_error *rerr,
330 331 332 333 334 335
                           remote_get_max_vcpus_args *args,
                           remote_get_max_vcpus_ret *ret)
{
    char *type;

    type = args->type ? *args->type : NULL;
336
    ret->max_vcpus = virConnectGetMaxVcpus (conn, type);
337
    if (ret->max_vcpus == -1) {
338
        remoteDispatchConnError(rerr, conn);
339 340
        return -1;
    }
341 342 343 344 345

    return 0;
}

static int
346
remoteDispatchNodeGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
347 348
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
349
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
350
                           remote_error *rerr,
351 352 353 354 355
                           void *args ATTRIBUTE_UNUSED,
                           remote_node_get_info_ret *ret)
{
    virNodeInfo info;

356 357
    if (virNodeGetInfo (conn, &info) == -1) {
        remoteDispatchConnError(rerr, conn);
358
        return -1;
359
    }
360 361 362 363 364 365 366 367 368 369 370 371 372 373

    memcpy (ret->model, info.model, sizeof ret->model);
    ret->memory = info.memory;
    ret->cpus = info.cpus;
    ret->mhz = info.mhz;
    ret->nodes = info.nodes;
    ret->sockets = info.sockets;
    ret->cores = info.cores;
    ret->threads = info.threads;

    return 0;
}

static int
374
remoteDispatchGetCapabilities (struct qemud_server *server ATTRIBUTE_UNUSED,
375 376
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
377
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
378
                               remote_error *rerr,
379 380 381 382 383
                               void *args ATTRIBUTE_UNUSED,
                               remote_get_capabilities_ret *ret)
{
    char *caps;

384
    caps = virConnectGetCapabilities (conn);
385
    if (caps == NULL) {
386
        remoteDispatchConnError(rerr, conn);
387 388
        return -1;
    }
389 390 391 392 393

    ret->capabilities = caps;
    return 0;
}

394 395
static int
remoteDispatchNodeGetCellsFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
396 397
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
398
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
399
                                      remote_error *rerr,
400 401 402
                                      remote_node_get_cells_free_memory_args *args,
                                      remote_node_get_cells_free_memory_ret *ret)
{
D
Daniel P. Berrange 已提交
403
    int err;
404 405

    if (args->maxCells > REMOTE_NODE_MAX_CELLS) {
406 407 408
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxCells > REMOTE_NODE_MAX_CELLS"));
        return -1;
409 410 411
    }

    /* Allocate return buffer. */
412
    if (VIR_ALLOC_N(ret->freeMems.freeMems_val, args->maxCells) < 0) {
413 414
        remoteDispatchOOMError(rerr);
        return -1;
415
    }
416

D
Daniel P. Berrange 已提交
417 418 419 420 421
    err = virNodeGetCellsFreeMemory(conn,
                                    (unsigned long long *)ret->freeMems.freeMems_val,
                                    args->startCell,
                                    args->maxCells);
    if (err <= 0) {
422
        VIR_FREE(ret->freeMems.freeMems_val);
423
        remoteDispatchConnError(rerr, conn);
424
        return -1;
425
    }
D
Daniel P. Berrange 已提交
426
    ret->freeMems.freeMems_len = err;
427 428 429 430 431 432 433

    return 0;
}


static int
remoteDispatchNodeGetFreeMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
434 435
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
436
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
437
                                 remote_error *rerr,
438 439 440 441 442
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_node_get_free_memory_ret *ret)
{
    unsigned long long freeMem;

443
    freeMem = virNodeGetFreeMemory(conn);
444
    if (freeMem == 0) {
445
        remoteDispatchConnError(rerr, conn);
446 447
        return -1;
    }
448 449 450 451 452
    ret->freeMem = freeMem;
    return 0;
}


453
static int
454
remoteDispatchDomainGetSchedulerType (struct qemud_server *server ATTRIBUTE_UNUSED,
455 456
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
457
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
458
                                      remote_error *rerr,
459 460 461 462 463 464 465
                                      remote_domain_get_scheduler_type_args *args,
                                      remote_domain_get_scheduler_type_ret *ret)
{
    virDomainPtr dom;
    char *type;
    int nparams;

466
    dom = get_nonnull_domain (conn, args->dom);
467
    if (dom == NULL) {
468
        remoteDispatchConnError(rerr, conn);
469
        return -1;
470 471 472
    }

    type = virDomainGetSchedulerType (dom, &nparams);
473 474
    if (type == NULL) {
        virDomainFree(dom);
475
        remoteDispatchConnError(rerr, conn);
476 477
        return -1;
    }
478 479 480

    ret->type = type;
    ret->nparams = nparams;
481
    virDomainFree(dom);
482 483 484 485
    return 0;
}

static int
486
remoteDispatchDomainGetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
487 488
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
489
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
490
                                            remote_error *rerr,
491 492 493 494 495 496 497 498 499 500
                                            remote_domain_get_scheduler_parameters_args *args,
                                            remote_domain_get_scheduler_parameters_ret *ret)
{
    virDomainPtr dom;
    virSchedParameterPtr params;
    int i, r, nparams;

    nparams = args->nparams;

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
501 502
        remoteDispatchFormatError (rerr, "%s", _("nparams too large"));
        return -1;
503
    }
504
    if (VIR_ALLOC_N(params, nparams) < 0) {
505 506
        remoteDispatchOOMError(rerr);
        return -1;
507 508
    }

509
    dom = get_nonnull_domain (conn, args->dom);
510
    if (dom == NULL) {
511
        VIR_FREE(params);
512
        remoteDispatchConnError(rerr, conn);
513
        return -1;
514 515 516 517
    }

    r = virDomainGetSchedulerParameters (dom, params, &nparams);
    if (r == -1) {
518
        virDomainFree(dom);
519
        VIR_FREE(params);
520
        remoteDispatchConnError(rerr, conn);
521 522 523 524 525
        return -1;
    }

    /* Serialise the scheduler parameters. */
    ret->params.params_len = nparams;
526 527
    if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
        goto oom;
528 529 530 531

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

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
        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:
550
            remoteDispatchFormatError (rerr, "%s", _("unknown type"));
551
            goto cleanup;
552 553
        }
    }
554
    virDomainFree(dom);
555
    VIR_FREE(params);
556 557

    return 0;
558 559

oom:
560
    remoteDispatchOOMError(rerr);
561 562 563 564 565
cleanup:
    virDomainFree(dom);
    for (i = 0 ; i < nparams ; i++)
        VIR_FREE(ret->params.params_val[i].field);
    VIR_FREE(params);
566
    return -1;
567 568 569
}

static int
570
remoteDispatchDomainSetSchedulerParameters (struct qemud_server *server ATTRIBUTE_UNUSED,
571 572
                                            struct qemud_client *client ATTRIBUTE_UNUSED,
                                            virConnectPtr conn,
573
                                            remote_message_header *hdr ATTRIBUTE_UNUSED,
574
                                            remote_error *rerr,
575 576 577 578 579 580 581 582 583 584
                                            remote_domain_set_scheduler_parameters_args *args,
                                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int i, r, nparams;
    virSchedParameterPtr params;

    nparams = args->params.params_len;

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
585 586
        remoteDispatchFormatError (rerr, "%s", _("nparams too large"));
        return -1;
587
    }
588
    if (VIR_ALLOC_N(params, nparams) < 0) {
589 590
        remoteDispatchOOMError(rerr);
        return -1;
591 592 593 594
    }

    /* Deserialise parameters. */
    for (i = 0; i < nparams; ++i) {
C
Chris Lalancette 已提交
595 596 597 598 599
        if (virStrcpyStatic(params[i].field, args->params.params_val[i].field) == NULL) {
            remoteDispatchFormatError(rerr, _("Field %s too big for destination"),
                                      args->params.params_val[i].field);
            return -1;
        }
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
        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;
        }
    }

617
    dom = get_nonnull_domain (conn, args->dom);
618
    if (dom == NULL) {
619
        VIR_FREE(params);
620
        remoteDispatchConnError(rerr, conn);
621
        return -1;
622 623 624
    }

    r = virDomainSetSchedulerParameters (dom, params, nparams);
625
    virDomainFree(dom);
626
    VIR_FREE(params);
627
    if (r == -1) {
628
        remoteDispatchConnError(rerr, conn);
629 630
        return -1;
    }
631 632 633 634

    return 0;
}

635
static int
636
remoteDispatchDomainBlockStats (struct qemud_server *server ATTRIBUTE_UNUSED,
637 638
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
639
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
640
                                remote_error *rerr,
641 642 643 644 645 646 647
                                remote_domain_block_stats_args *args,
                                remote_domain_block_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainBlockStats stats;

648
    dom = get_nonnull_domain (conn, args->dom);
649
    if (dom == NULL) {
650
        remoteDispatchConnError(rerr, conn);
651
        return -1;
652 653 654
    }
    path = args->path;

D
Daniel P. Berrange 已提交
655 656
    if (virDomainBlockStats (dom, path, &stats, sizeof stats) == -1) {
        virDomainFree (dom);
657
        remoteDispatchConnError(rerr, conn);
658
        return -1;
D
Daniel P. Berrange 已提交
659 660
    }
    virDomainFree (dom);
661 662 663 664 665 666 667 668 669 670 671

    ret->rd_req = stats.rd_req;
    ret->rd_bytes = stats.rd_bytes;
    ret->wr_req = stats.wr_req;
    ret->wr_bytes = stats.wr_bytes;
    ret->errs = stats.errs;

    return 0;
}

static int
672
remoteDispatchDomainInterfaceStats (struct qemud_server *server ATTRIBUTE_UNUSED,
673 674
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
675
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
676
                                    remote_error *rerr,
677 678 679 680 681 682 683
                                    remote_domain_interface_stats_args *args,
                                    remote_domain_interface_stats_ret *ret)
{
    virDomainPtr dom;
    char *path;
    struct _virDomainInterfaceStats stats;

684
    dom = get_nonnull_domain (conn, args->dom);
685
    if (dom == NULL) {
686
        remoteDispatchConnError(rerr, conn);
687
        return -1;
688 689 690
    }
    path = args->path;

D
Daniel P. Berrange 已提交
691 692
    if (virDomainInterfaceStats (dom, path, &stats, sizeof stats) == -1) {
        virDomainFree (dom);
693
        remoteDispatchConnError(rerr, conn);
694
        return -1;
D
Daniel P. Berrange 已提交
695 696
    }
    virDomainFree (dom);
697 698 699 700 701 702 703 704 705 706 707 708 709

    ret->rx_bytes = stats.rx_bytes;
    ret->rx_packets = stats.rx_packets;
    ret->rx_errs = stats.rx_errs;
    ret->rx_drop = stats.rx_drop;
    ret->tx_bytes = stats.tx_bytes;
    ret->tx_packets = stats.tx_packets;
    ret->tx_errs = stats.tx_errs;
    ret->tx_drop = stats.tx_drop;

    return 0;
}

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
static int
remoteDispatchDomainMemoryStats (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
                                 remote_error *rerr,
                                 remote_domain_memory_stats_args *args,
                                 remote_domain_memory_stats_ret *ret)
{
    virDomainPtr dom;
    struct _virDomainMemoryStat *stats;
    unsigned int nr_stats, i;

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

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

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

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

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

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

766 767
static int
remoteDispatchDomainBlockPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
768 769
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
770
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
771
                               remote_error *rerr,
772 773 774 775 776 777 778 779 780
                               remote_domain_block_peek_args *args,
                               remote_domain_block_peek_ret *ret)
{
    virDomainPtr dom;
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;

781
    dom = get_nonnull_domain (conn, args->dom);
782
    if (dom == NULL) {
783
        remoteDispatchConnError(rerr, conn);
784
        return -1;
785 786 787 788 789 790 791
    }
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
792
        virDomainFree (dom);
793 794 795
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
        return -1;
796 797 798
    }

    ret->buffer.buffer_len = size;
799
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
800
        virDomainFree (dom);
801
        remoteDispatchOOMError(rerr);
802
        return -1;
803 804 805 806 807 808
    }

    if (virDomainBlockPeek (dom, path, offset, size,
                            ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
809
        remoteDispatchConnError(rerr, conn);
810 811 812 813 814 815 816
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

R
Richard W.M. Jones 已提交
817 818
static int
remoteDispatchDomainMemoryPeek (struct qemud_server *server ATTRIBUTE_UNUSED,
819 820
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
821
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
822
                                remote_error *rerr,
R
Richard W.M. Jones 已提交
823 824 825 826 827 828 829 830
                                remote_domain_memory_peek_args *args,
                                remote_domain_memory_peek_ret *ret)
{
    virDomainPtr dom;
    unsigned long long offset;
    size_t size;
    unsigned int flags;

831
    dom = get_nonnull_domain (conn, args->dom);
R
Richard W.M. Jones 已提交
832
    if (dom == NULL) {
833
        remoteDispatchConnError(rerr, conn);
834
        return -1;
R
Richard W.M. Jones 已提交
835 836 837 838 839 840
    }
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
841
        virDomainFree (dom);
842 843 844
        remoteDispatchFormatError (rerr,
                                   "%s", _("size > maximum buffer size"));
        return -1;
R
Richard W.M. Jones 已提交
845 846 847 848 849
    }

    ret->buffer.buffer_len = size;
    if (VIR_ALLOC_N (ret->buffer.buffer_val, size) < 0) {
        virDomainFree (dom);
850
        remoteDispatchOOMError(rerr);
851
        return -1;
R
Richard W.M. Jones 已提交
852 853 854 855 856 857
    }

    if (virDomainMemoryPeek (dom, offset, size,
                             ret->buffer.buffer_val, flags) == -1) {
        /* free (ret->buffer.buffer_val); - caller frees */
        virDomainFree (dom);
858
        remoteDispatchConnError(rerr, conn);
R
Richard W.M. Jones 已提交
859 860 861 862 863 864 865
        return -1;
    }
    virDomainFree (dom);

    return 0;
}

866
static int
867
remoteDispatchDomainAttachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
868 869
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
870
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
871
                                  remote_error *rerr,
872 873 874 875 876
                                  remote_domain_attach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

877
    dom = get_nonnull_domain (conn, args->dom);
878
    if (dom == NULL) {
879
        remoteDispatchConnError(rerr, conn);
880
        return -1;
881 882
    }

883 884
    if (virDomainAttachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
885
        remoteDispatchConnError(rerr, conn);
886
        return -1;
887 888
    }
    virDomainFree(dom);
889 890 891
    return 0;
}

J
Jim Fehlig 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
static int
remoteDispatchDomainAttachDeviceFlags (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
                                       remote_error *rerr,
                                       remote_domain_attach_device_flags_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

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

918
static int
919
remoteDispatchDomainCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
920 921
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
922
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
923
                            remote_error *rerr,
924 925 926 927 928
                            remote_domain_create_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

935 936
    if (virDomainCreate (dom) == -1) {
        virDomainFree(dom);
937
        remoteDispatchConnError(rerr, conn);
938
        return -1;
939 940
    }
    virDomainFree(dom);
941 942 943 944
    return 0;
}

static int
945
remoteDispatchDomainCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
946 947
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
948
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
949 950 951
                               remote_error *rerr,
                               remote_domain_create_xml_args *args,
                               remote_domain_create_xml_ret *ret)
952 953 954
{
    virDomainPtr dom;

955
    dom = virDomainCreateXML (conn, args->xml_desc, args->flags);
956
    if (dom == NULL) {
957
        remoteDispatchConnError(rerr, conn);
958 959
        return -1;
    }
960 961

    make_nonnull_domain (&ret->dom, dom);
962
    virDomainFree(dom);
963 964 965 966 967

    return 0;
}

static int
968
remoteDispatchDomainDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
969 970
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
971
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
972
                               remote_error *rerr,
973 974 975 976 977
                               remote_domain_define_xml_args *args,
                               remote_domain_define_xml_ret *ret)
{
    virDomainPtr dom;

978
    dom = virDomainDefineXML (conn, args->xml);
979
    if (dom == NULL) {
980
        remoteDispatchConnError(rerr, conn);
981 982
        return -1;
    }
983 984

    make_nonnull_domain (&ret->dom, dom);
985
    virDomainFree(dom);
986 987 988 989 990

    return 0;
}

static int
991
remoteDispatchDomainDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
992 993
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
994
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
995
                             remote_error *rerr,
996 997 998 999 1000
                             remote_domain_destroy_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1001
    dom = get_nonnull_domain (conn, args->dom);
1002
    if (dom == NULL) {
1003
        remoteDispatchConnError(rerr, conn);
1004
        return -1;
1005 1006
    }

1007 1008
    if (virDomainDestroy (dom) == -1) {
        virDomainFree(dom);
1009
        remoteDispatchConnError(rerr, conn);
1010
        return -1;
1011 1012
    }
    virDomainFree(dom);
1013 1014 1015 1016
    return 0;
}

static int
1017
remoteDispatchDomainDetachDevice (struct qemud_server *server ATTRIBUTE_UNUSED,
1018 1019
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1020
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1021
                                  remote_error *rerr,
1022 1023 1024 1025 1026
                                  remote_domain_detach_device_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1027
    dom = get_nonnull_domain (conn, args->dom);
1028
    if (dom == NULL) {
1029
        remoteDispatchConnError(rerr, conn);
1030
        return -1;
1031 1032
    }

1033 1034
    if (virDomainDetachDevice (dom, args->xml) == -1) {
        virDomainFree(dom);
J
Jim Fehlig 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    virDomainFree(dom);
    return 0;
}

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

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

    if (virDomainDetachDeviceFlags (dom, args->xml, args->flags) == -1) {
        virDomainFree(dom);
1062
        remoteDispatchConnError(rerr, conn);
1063
        return -1;
1064
    }
1065

1066
    virDomainFree(dom);
1067 1068 1069 1070
    return 0;
}

static int
1071
remoteDispatchDomainDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
1072 1073
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1074
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1075
                             remote_error *rerr,
1076 1077 1078 1079 1080
                             remote_domain_dump_xml_args *args,
                             remote_domain_dump_xml_ret *ret)
{
    virDomainPtr dom;

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

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virDomainGetXMLDesc (dom, args->flags);
1089
    if (!ret->xml) {
1090
        virDomainFree(dom);
1091
        remoteDispatchConnError(rerr, conn);
1092
        return -1;
1093 1094
    }
    virDomainFree(dom);
1095 1096 1097
    return 0;
}

1098 1099 1100 1101
static int
remoteDispatchDomainXmlFromNative (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1102
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
                                   remote_error *rerr,
                                   remote_domain_xml_from_native_args *args,
                                   remote_domain_xml_from_native_ret *ret)
{
    /* remoteDispatchClientRequest will free this. */
    ret->domainXml = virConnectDomainXMLFromNative (conn,
                                                    args->nativeFormat,
                                                    args->nativeConfig,
                                                    args->flags);
    if (!ret->domainXml) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    return 0;
}

static int
remoteDispatchDomainXmlToNative (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
1123
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
                                 remote_error *rerr,
                                 remote_domain_xml_to_native_args *args,
                                 remote_domain_xml_to_native_ret *ret)
{
    /* remoteDispatchClientRequest will free this. */
    ret->nativeConfig = virConnectDomainXMLToNative (conn,
                                                     args->nativeFormat,
                                                     args->domainXml,
                                                     args->flags);
    if (!ret->nativeConfig) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
    return 0;
}


1141
static int
1142
remoteDispatchDomainGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
1143 1144
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1145
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1146
                                  remote_error *rerr,
1147 1148 1149 1150 1151
                                  remote_domain_get_autostart_args *args,
                                  remote_domain_get_autostart_ret *ret)
{
    virDomainPtr dom;

1152
    dom = get_nonnull_domain (conn, args->dom);
1153
    if (dom == NULL) {
1154
        remoteDispatchConnError(rerr, conn);
1155
        return -1;
1156 1157
    }

1158 1159
    if (virDomainGetAutostart (dom, &ret->autostart) == -1) {
        virDomainFree(dom);
1160
        remoteDispatchConnError(rerr, conn);
1161
        return -1;
1162 1163
    }
    virDomainFree(dom);
1164 1165 1166 1167
    return 0;
}

static int
1168
remoteDispatchDomainGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
1169 1170
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1171
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1172
                             remote_error *rerr,
1173 1174 1175 1176 1177 1178
                             remote_domain_get_info_args *args,
                             remote_domain_get_info_ret *ret)
{
    virDomainPtr dom;
    virDomainInfo info;

1179
    dom = get_nonnull_domain (conn, args->dom);
1180
    if (dom == NULL) {
1181
        remoteDispatchConnError(rerr, conn);
1182
        return -1;
1183 1184
    }

1185 1186
    if (virDomainGetInfo (dom, &info) == -1) {
        virDomainFree(dom);
1187
        remoteDispatchConnError(rerr, conn);
1188
        return -1;
1189
    }
1190 1191 1192 1193 1194 1195 1196

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

1197 1198
    virDomainFree(dom);

1199 1200 1201 1202
    return 0;
}

static int
1203
remoteDispatchDomainGetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
1204 1205
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1206
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1207
                                  remote_error *rerr,
1208 1209 1210 1211 1212
                                  remote_domain_get_max_memory_args *args,
                                  remote_domain_get_max_memory_ret *ret)
{
    virDomainPtr dom;

1213
    dom = get_nonnull_domain (conn, args->dom);
1214
    if (dom == NULL) {
1215
        remoteDispatchConnError(rerr, conn);
1216
        return -1;
1217 1218 1219
    }

    ret->memory = virDomainGetMaxMemory (dom);
1220 1221
    if (ret->memory == 0) {
        virDomainFree(dom);
1222
        remoteDispatchConnError(rerr, conn);
1223 1224 1225
        return -1;
    }
    virDomainFree(dom);
1226 1227 1228 1229
    return 0;
}

static int
1230
remoteDispatchDomainGetMaxVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1231 1232
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
1233
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
1234
                                 remote_error *rerr,
1235 1236 1237 1238 1239
                                 remote_domain_get_max_vcpus_args *args,
                                 remote_domain_get_max_vcpus_ret *ret)
{
    virDomainPtr dom;

1240
    dom = get_nonnull_domain (conn, args->dom);
1241
    if (dom == NULL) {
1242
        remoteDispatchConnError(rerr, conn);
1243
        return -1;
1244 1245 1246
    }

    ret->num = virDomainGetMaxVcpus (dom);
1247 1248
    if (ret->num == -1) {
        virDomainFree(dom);
1249
        remoteDispatchConnError(rerr, conn);
1250 1251 1252
        return -1;
    }
    virDomainFree(dom);
1253 1254 1255
    return 0;
}

1256 1257 1258 1259
static int
remoteDispatchDomainGetSecurityLabel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1260
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
                                     remote_error *rerr,
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
{
    virDomainPtr dom;
    virSecurityLabel seclabel;

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

    memset(&seclabel, 0, sizeof seclabel);
    if (virDomainGetSecurityLabel(dom, &seclabel) == -1) {
        virDomainFree(dom);
1277
        remoteDispatchConnError(rerr, conn);
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
        return -1;
    }

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

    return 0;
}

static int
remoteDispatchNodeGetSecurityModel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1298
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1299 1300 1301 1302 1303 1304 1305 1306
                                   remote_error *rerr,
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_node_get_security_model_ret *ret)
{
    virSecurityModel secmodel;

    memset(&secmodel, 0, sizeof secmodel);
    if (virNodeGetSecurityModel(conn, &secmodel) == -1) {
1307
        remoteDispatchConnError(rerr, conn);
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
        return -1;
    }

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

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

    return 0;
}

1328
static int
1329
remoteDispatchDomainGetOsType (struct qemud_server *server ATTRIBUTE_UNUSED,
1330 1331
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1332
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1333
                               remote_error *rerr,
1334 1335 1336 1337 1338
                               remote_domain_get_os_type_args *args,
                               remote_domain_get_os_type_ret *ret)
{
    virDomainPtr dom;

1339
    dom = get_nonnull_domain (conn, args->dom);
1340
    if (dom == NULL) {
1341
        remoteDispatchConnError(rerr, conn);
1342
        return -1;
1343 1344 1345 1346
    }

    /* remoteDispatchClientRequest will free this */
    ret->type = virDomainGetOSType (dom);
1347
    if (ret->type == NULL) {
1348
        virDomainFree(dom);
1349
        remoteDispatchConnError(rerr, conn);
1350
        return -1;
1351 1352
    }
    virDomainFree(dom);
1353 1354 1355 1356
    return 0;
}

static int
1357
remoteDispatchDomainGetVcpus (struct qemud_server *server ATTRIBUTE_UNUSED,
1358 1359
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
1360
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
1361
                              remote_error *rerr,
1362 1363 1364
                              remote_domain_get_vcpus_args *args,
                              remote_domain_get_vcpus_ret *ret)
{
1365 1366 1367
    virDomainPtr dom = NULL;
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
1368 1369
    int info_len, i;

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

    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1377
        virDomainFree(dom);
1378 1379
        remoteDispatchFormatError (rerr, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
        return -1;
1380 1381
    }

1382
    if (args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1383
        virDomainFree(dom);
1384 1385
        remoteDispatchFormatError (rerr, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
        return -1;
1386 1387 1388
    }

    /* Allocate buffers to take the results. */
1389 1390
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
        goto oom;
1391 1392
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
1393
        goto oom;
1394 1395 1396 1397

    info_len = virDomainGetVcpus (dom,
                                  info, args->maxinfo,
                                  cpumaps, args->maplen);
1398
    if (info_len == -1) {
1399 1400
        VIR_FREE(info);
        VIR_FREE(cpumaps);
1401
        virDomainFree(dom);
1402
        remoteDispatchConnError(rerr, conn);
1403 1404
        return -1;
    }
1405 1406 1407

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
1408 1409
    if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
        goto oom;
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421

    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.
     */
1422
    ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
1423 1424
    ret->cpumaps.cpumaps_val = (char *) cpumaps;

1425
    VIR_FREE(info);
1426
    virDomainFree(dom);
1427
    return 0;
1428 1429 1430 1431 1432

oom:
    VIR_FREE(info);
    VIR_FREE(cpumaps);
    virDomainFree(dom);
1433 1434
    remoteDispatchOOMError(rerr);
    return -1;
1435 1436
}

1437
static int
1438
remoteDispatchDomainMigratePrepare (struct qemud_server *server ATTRIBUTE_UNUSED,
1439 1440
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1441
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1442
                                    remote_error *rerr,
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
                                    remote_domain_migrate_prepare_args *args,
                                    remote_domain_migrate_prepare_ret *ret)
{
    int r;
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;

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

    /* Wacky world of XDR ... */
1457
    if (VIR_ALLOC(uri_out) < 0) {
1458 1459
        remoteDispatchOOMError(rerr);
        return -1;
1460
    }
1461

1462
    r = virDomainMigratePrepare (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1463 1464
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1465
    if (r == -1) {
1466
        VIR_FREE(uri_out);
1467
        remoteDispatchConnError(rerr, conn);
D
Daniel P. Berrange 已提交
1468 1469
        return -1;
    }
1470 1471 1472 1473 1474 1475

    /* 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 已提交
1476 1477
    if (*uri_out == NULL) {
        ret->uri_out = NULL;
1478
        VIR_FREE(uri_out);
D
Daniel P. Berrange 已提交
1479 1480 1481
    } else {
        ret->uri_out = uri_out;
    }
1482 1483 1484 1485 1486

    return 0;
}

static int
1487
remoteDispatchDomainMigratePerform (struct qemud_server *server ATTRIBUTE_UNUSED,
1488 1489
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1490
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1491
                                    remote_error *rerr,
1492 1493 1494 1495 1496 1497 1498
                                    remote_domain_migrate_perform_args *args,
                                    void *ret ATTRIBUTE_UNUSED)
{
    int r;
    virDomainPtr dom;
    char *dname;

1499
    dom = get_nonnull_domain (conn, args->dom);
1500
    if (dom == NULL) {
1501
        remoteDispatchConnError(rerr, conn);
1502
        return -1;
1503 1504 1505 1506
    }

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

D
Daniel P. Berrange 已提交
1507 1508 1509 1510 1511
    r = virDomainMigratePerform (dom,
                                 args->cookie.cookie_val,
                                 args->cookie.cookie_len,
                                 args->uri,
                                 args->flags, dname, args->resource);
D
Daniel P. Berrange 已提交
1512
    virDomainFree (dom);
1513
    if (r == -1) {
1514
        remoteDispatchConnError(rerr, conn);
1515 1516
        return -1;
    }
1517 1518 1519 1520 1521

    return 0;
}

static int
1522
remoteDispatchDomainMigrateFinish (struct qemud_server *server ATTRIBUTE_UNUSED,
1523 1524
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1525
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1526
                                   remote_error *rerr,
1527 1528 1529 1530 1531 1532
                                   remote_domain_migrate_finish_args *args,
                                   remote_domain_migrate_finish_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

1533
    ddom = virDomainMigrateFinish (conn, args->dname,
D
Daniel P. Berrange 已提交
1534 1535 1536 1537
                                   args->cookie.cookie_val,
                                   args->cookie.cookie_len,
                                   args->uri,
                                   args->flags);
1538
    if (ddom == NULL) {
1539
        remoteDispatchConnError(rerr, conn);
1540 1541
        return -1;
    }
1542 1543

    make_nonnull_domain (&ret->ddom, ddom);
D
Daniel P. Berrange 已提交
1544
    virDomainFree (ddom);
1545 1546 1547
    return 0;
}

D
Daniel Veillard 已提交
1548 1549
static int
remoteDispatchDomainMigratePrepare2 (struct qemud_server *server ATTRIBUTE_UNUSED,
1550 1551
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
1552
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
1553
                                     remote_error *rerr,
D
Daniel Veillard 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
                                     remote_domain_migrate_prepare2_args *args,
                                     remote_domain_migrate_prepare2_ret *ret)
{
    int r;
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
    CHECK_CONN (client);

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

    /* Wacky world of XDR ... */
    if (VIR_ALLOC(uri_out) < 0) {
1570 1571
        remoteDispatchOOMError(rerr);
        return -1;
D
Daniel Veillard 已提交
1572 1573
    }

1574
    r = virDomainMigratePrepare2 (conn, &cookie, &cookielen,
D
Daniel P. Berrange 已提交
1575 1576 1577
                                  uri_in, uri_out,
                                  args->flags, dname, args->resource,
                                  args->dom_xml);
1578
    if (r == -1) {
1579
        remoteDispatchConnError(rerr, conn);
1580 1581
        return -1;
    }
D
Daniel Veillard 已提交
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594

    /* remoteDispatchClientRequest will free cookie, uri_out and
     * the string if there is one.
     */
    ret->cookie.cookie_len = cookielen;
    ret->cookie.cookie_val = cookie;
    ret->uri_out = *uri_out == NULL ? NULL : uri_out;

    return 0;
}

static int
remoteDispatchDomainMigrateFinish2 (struct qemud_server *server ATTRIBUTE_UNUSED,
1595 1596
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
1597
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
1598
                                    remote_error *rerr,
D
Daniel Veillard 已提交
1599 1600 1601 1602 1603 1604
                                    remote_domain_migrate_finish2_args *args,
                                    remote_domain_migrate_finish2_ret *ret)
{
    virDomainPtr ddom;
    CHECK_CONN (client);

1605
    ddom = virDomainMigrateFinish2 (conn, args->dname,
D
Daniel P. Berrange 已提交
1606 1607 1608 1609 1610
                                    args->cookie.cookie_val,
                                    args->cookie.cookie_len,
                                    args->uri,
                                    args->flags,
                                    args->retcode);
1611
    if (ddom == NULL) {
1612
        remoteDispatchConnError(rerr, conn);
1613 1614
        return -1;
    }
D
Daniel Veillard 已提交
1615 1616 1617 1618 1619 1620

    make_nonnull_domain (&ret->ddom, ddom);

    return 0;
}

C
Chris Lalancette 已提交
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
static int
remoteDispatchDomainMigratePrepareTunnel(struct qemud_server *server ATTRIBUTE_UNUSED,
                                         struct qemud_client *client,
                                         virConnectPtr conn,
                                         remote_message_header *hdr,
                                         remote_error *rerr,
                                         remote_domain_migrate_prepare_tunnel_args *args,
                                         void *ret ATTRIBUTE_UNUSED)
{
    int r;
    char *dname;
    struct qemud_client_stream *stream;
    CHECK_CONN (client);

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

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

1643
    r = virDomainMigratePrepareTunnel(conn, stream->st,
C
Chris Lalancette 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
                                      args->flags, dname, args->resource,
                                      args->dom_xml);
    if (r == -1) {
        remoteFreeClientStream(client, stream);
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

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

    return 0;
}

1662
static int
1663
remoteDispatchListDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
1664 1665
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1666
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1667
                                  remote_error *rerr,
1668 1669 1670 1671 1672
                                  remote_list_defined_domains_args *args,
                                  remote_list_defined_domains_ret *ret)
{

    if (args->maxnames > REMOTE_DOMAIN_NAME_LIST_MAX) {
1673 1674 1675
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_DOMAIN_NAME_LIST_MAX"));
        return -1;
1676 1677 1678
    }

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

    ret->names.names_len =
1685
        virConnectListDefinedDomains (conn,
1686
                                      ret->names.names_val, args->maxnames);
1687 1688
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
1689
        remoteDispatchConnError(rerr, conn);
1690 1691
        return -1;
    }
1692 1693 1694 1695 1696

    return 0;
}

static int
1697
remoteDispatchDomainLookupById (struct qemud_server *server ATTRIBUTE_UNUSED,
1698 1699
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
1700
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
1701
                                remote_error *rerr,
1702 1703 1704 1705 1706
                                remote_domain_lookup_by_id_args *args,
                                remote_domain_lookup_by_id_ret *ret)
{
    virDomainPtr dom;

1707
    dom = virDomainLookupByID (conn, args->id);
1708
    if (dom == NULL) {
1709
        remoteDispatchConnError(rerr, conn);
1710 1711
        return -1;
    }
1712 1713

    make_nonnull_domain (&ret->dom, dom);
1714
    virDomainFree(dom);
1715 1716 1717 1718
    return 0;
}

static int
1719
remoteDispatchDomainLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
1720 1721
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1722
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1723
                                  remote_error *rerr,
1724 1725 1726 1727 1728
                                  remote_domain_lookup_by_name_args *args,
                                  remote_domain_lookup_by_name_ret *ret)
{
    virDomainPtr dom;

1729
    dom = virDomainLookupByName (conn, args->name);
1730
    if (dom == NULL) {
1731
        remoteDispatchConnError(rerr, conn);
1732 1733
        return -1;
    }
1734 1735

    make_nonnull_domain (&ret->dom, dom);
1736
    virDomainFree(dom);
1737 1738 1739 1740
    return 0;
}

static int
1741
remoteDispatchDomainLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
1742 1743
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1744
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1745
                                  remote_error *rerr,
1746 1747 1748 1749 1750
                                  remote_domain_lookup_by_uuid_args *args,
                                  remote_domain_lookup_by_uuid_ret *ret)
{
    virDomainPtr dom;

1751
    dom = virDomainLookupByUUID (conn, (unsigned char *) args->uuid);
1752
    if (dom == NULL) {
1753
        remoteDispatchConnError(rerr, conn);
1754 1755
        return -1;
    }
1756 1757

    make_nonnull_domain (&ret->dom, dom);
1758
    virDomainFree(dom);
1759 1760 1761 1762
    return 0;
}

static int
1763
remoteDispatchNumOfDefinedDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
1764 1765
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
1766
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
1767
                                   remote_error *rerr,
1768 1769 1770 1771
                                   void *args ATTRIBUTE_UNUSED,
                                   remote_num_of_defined_domains_ret *ret)
{

1772
    ret->num = virConnectNumOfDefinedDomains (conn);
1773
    if (ret->num == -1) {
1774
        remoteDispatchConnError(rerr, conn);
1775 1776
        return -1;
    }
1777 1778 1779 1780 1781

    return 0;
}

static int
1782
remoteDispatchDomainPinVcpu (struct qemud_server *server ATTRIBUTE_UNUSED,
1783 1784
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1785
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1786
                             remote_error *rerr,
1787 1788 1789 1790 1791 1792
                             remote_domain_pin_vcpu_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;
    int rv;

1793
    dom = get_nonnull_domain (conn, args->dom);
1794
    if (dom == NULL) {
1795
        remoteDispatchConnError(rerr, conn);
1796
        return -1;
1797 1798 1799
    }

    if (args->cpumap.cpumap_len > REMOTE_CPUMAP_MAX) {
1800
        virDomainFree(dom);
1801 1802
        remoteDispatchFormatError (rerr, "%s", _("cpumap_len > REMOTE_CPUMAP_MAX"));
        return -1;
1803 1804 1805 1806 1807
    }

    rv = virDomainPinVcpu (dom, args->vcpu,
                           (unsigned char *) args->cpumap.cpumap_val,
                           args->cpumap.cpumap_len);
1808 1809
    if (rv == -1) {
        virDomainFree(dom);
1810
        remoteDispatchConnError(rerr, conn);
1811 1812 1813
        return -1;
    }
    virDomainFree(dom);
1814 1815 1816 1817
    return 0;
}

static int
1818
remoteDispatchDomainReboot (struct qemud_server *server ATTRIBUTE_UNUSED,
1819 1820
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
1821
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
1822
                            remote_error *rerr,
1823 1824 1825 1826 1827
                            remote_domain_reboot_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1828
    dom = get_nonnull_domain (conn, args->dom);
1829
    if (dom == NULL) {
1830
        remoteDispatchConnError(rerr, conn);
1831
        return -1;
1832 1833
    }

1834 1835
    if (virDomainReboot (dom, args->flags) == -1) {
        virDomainFree(dom);
1836
        remoteDispatchConnError(rerr, conn);
1837
        return -1;
1838 1839
    }
    virDomainFree(dom);
1840 1841 1842 1843
    return 0;
}

static int
1844
remoteDispatchDomainRestore (struct qemud_server *server ATTRIBUTE_UNUSED,
1845 1846
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
1847
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
1848
                             remote_error *rerr,
1849 1850 1851 1852
                             remote_domain_restore_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{

1853 1854
    if (virDomainRestore (conn, args->from) == -1) {
        remoteDispatchConnError(rerr, conn);
1855
        return -1;
1856
    }
1857 1858 1859 1860 1861

    return 0;
}

static int
1862
remoteDispatchDomainResume (struct qemud_server *server ATTRIBUTE_UNUSED,
1863 1864
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
1865
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
1866
                            remote_error *rerr,
1867 1868 1869 1870 1871
                            remote_domain_resume_args *args,
                            void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

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

1878 1879
    if (virDomainResume (dom) == -1) {
        virDomainFree(dom);
1880
        remoteDispatchConnError(rerr, conn);
1881
        return -1;
1882 1883
    }
    virDomainFree(dom);
1884 1885 1886 1887
    return 0;
}

static int
1888
remoteDispatchDomainSave (struct qemud_server *server ATTRIBUTE_UNUSED,
1889 1890
                          struct qemud_client *client ATTRIBUTE_UNUSED,
                          virConnectPtr conn,
1891
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
1892
                          remote_error *rerr,
1893 1894 1895 1896 1897
                          remote_domain_save_args *args,
                          void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1898
    dom = get_nonnull_domain (conn, args->dom);
1899
    if (dom == NULL) {
1900
        remoteDispatchConnError(rerr, conn);
1901
        return -1;
1902 1903
    }

1904 1905
    if (virDomainSave (dom, args->to) == -1) {
        virDomainFree(dom);
1906
        remoteDispatchConnError(rerr, conn);
1907
        return -1;
1908 1909
    }
    virDomainFree(dom);
1910 1911 1912 1913
    return 0;
}

static int
1914
remoteDispatchDomainCoreDump (struct qemud_server *server ATTRIBUTE_UNUSED,
1915 1916
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
1917
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
1918
                              remote_error *rerr,
1919 1920 1921 1922 1923
                              remote_domain_core_dump_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1924
    dom = get_nonnull_domain (conn, args->dom);
1925
    if (dom == NULL) {
1926
        remoteDispatchConnError(rerr, conn);
1927
        return -1;
1928 1929
    }

1930 1931
    if (virDomainCoreDump (dom, args->to, args->flags) == -1) {
        virDomainFree(dom);
1932
        remoteDispatchConnError(rerr, conn);
1933
        return -1;
1934 1935
    }
    virDomainFree(dom);
1936 1937 1938 1939
    return 0;
}

static int
1940
remoteDispatchDomainSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
1941 1942
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1943
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1944
                                  remote_error *rerr,
1945 1946 1947 1948 1949
                                  remote_domain_set_autostart_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1950
    dom = get_nonnull_domain (conn, args->dom);
1951
    if (dom == NULL) {
1952
        remoteDispatchConnError(rerr, conn);
1953
        return -1;
1954 1955
    }

1956 1957
    if (virDomainSetAutostart (dom, args->autostart) == -1) {
        virDomainFree(dom);
1958
        remoteDispatchConnError(rerr, conn);
1959
        return -1;
1960 1961
    }
    virDomainFree(dom);
1962 1963 1964 1965
    return 0;
}

static int
1966
remoteDispatchDomainSetMaxMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
1967 1968
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
1969
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
1970
                                  remote_error *rerr,
1971 1972 1973 1974 1975
                                  remote_domain_set_max_memory_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

1976
    dom = get_nonnull_domain (conn, args->dom);
1977
    if (dom == NULL) {
1978
        remoteDispatchConnError(rerr, conn);
1979
        return -1;
1980 1981
    }

1982 1983
    if (virDomainSetMaxMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
1984
        remoteDispatchConnError(rerr, conn);
1985
        return -1;
1986 1987
    }
    virDomainFree(dom);
1988 1989 1990 1991
    return 0;
}

static int
1992
remoteDispatchDomainSetMemory (struct qemud_server *server ATTRIBUTE_UNUSED,
1993 1994
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
1995
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
1996
                               remote_error *rerr,
1997 1998 1999 2000 2001
                               remote_domain_set_memory_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virDomainPtr dom;

2002
    dom = get_nonnull_domain (conn, args->dom);
2003
    if (dom == NULL) {
2004
        remoteDispatchConnError(rerr, conn);
2005
        return -1;
2006 2007
    }

2008 2009
    if (virDomainSetMemory (dom, args->memory) == -1) {
        virDomainFree(dom);
2010
        remoteDispatchConnError(rerr, conn);
2011
        return -1;
2012 2013
    }
    virDomainFree(dom);
2014 2015 2016 2017
    return 0;
}

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

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

2034 2035
    if (virDomainSetVcpus (dom, args->nvcpus) == -1) {
        virDomainFree(dom);
2036
        remoteDispatchConnError(rerr, conn);
2037
        return -1;
2038 2039
    }
    virDomainFree(dom);
2040 2041 2042 2043
    return 0;
}

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

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

2060 2061
    if (virDomainShutdown (dom) == -1) {
        virDomainFree(dom);
2062
        remoteDispatchConnError(rerr, conn);
2063
        return -1;
2064 2065
    }
    virDomainFree(dom);
2066 2067 2068 2069
    return 0;
}

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

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

2086 2087
    if (virDomainSuspend (dom) == -1) {
        virDomainFree(dom);
2088
        remoteDispatchConnError(rerr, conn);
2089
        return -1;
2090 2091
    }
    virDomainFree(dom);
2092 2093 2094 2095
    return 0;
}

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

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

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

static int
2122
remoteDispatchListDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2123 2124
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2125
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2126
                                   remote_error *rerr,
2127 2128 2129 2130 2131
                                   remote_list_defined_networks_args *args,
                                   remote_list_defined_networks_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
2132 2133 2134
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
2135 2136 2137
    }

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

    ret->names.names_len =
2144
        virConnectListDefinedNetworks (conn,
2145
                                       ret->names.names_val, args->maxnames);
2146 2147
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
2148
        remoteDispatchConnError(rerr, conn);
2149 2150
        return -1;
    }
2151 2152 2153 2154 2155

    return 0;
}

static int
2156
remoteDispatchListDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2157 2158
                           struct qemud_client *client ATTRIBUTE_UNUSED,
                           virConnectPtr conn,
2159
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
2160
                           remote_error *rerr,
2161 2162 2163 2164 2165
                           remote_list_domains_args *args,
                           remote_list_domains_ret *ret)
{

    if (args->maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
2166 2167 2168
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxids > REMOTE_DOMAIN_ID_LIST_MAX"));
        return -1;
2169 2170 2171
    }

    /* Allocate return buffer. */
2172
    if (VIR_ALLOC_N(ret->ids.ids_val, args->maxids) < 0) {
2173 2174
        remoteDispatchOOMError(rerr);
        return -1;
2175
    }
2176

2177
    ret->ids.ids_len = virConnectListDomains (conn,
2178
                                              ret->ids.ids_val, args->maxids);
2179 2180
    if (ret->ids.ids_len == -1) {
        VIR_FREE(ret->ids.ids_val);
2181
        remoteDispatchConnError(rerr, conn);
2182 2183
        return -1;
    }
2184 2185 2186 2187 2188

    return 0;
}

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

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
2199 2200 2201
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
        return -1;
2202 2203 2204
    }

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

    ret->names.names_len =
2211
        virConnectListNetworks (conn,
2212
                                ret->names.names_val, args->maxnames);
2213 2214
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_len);
2215
        remoteDispatchConnError(rerr, conn);
2216 2217
        return -1;
    }
2218 2219 2220 2221 2222

    return 0;
}

static int
2223
remoteDispatchNetworkCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
2224 2225
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2226
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2227
                             remote_error *rerr,
2228 2229 2230 2231 2232
                             remote_network_create_args *args,
                             void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2233
    net = get_nonnull_network (conn, args->net);
2234
    if (net == NULL) {
2235
        remoteDispatchConnError(rerr, conn);
2236
        return -1;
2237 2238
    }

2239 2240
    if (virNetworkCreate (net) == -1) {
        virNetworkFree(net);
2241
        remoteDispatchConnError(rerr, conn);
2242
        return -1;
2243 2244
    }
    virNetworkFree(net);
2245 2246 2247 2248
    return 0;
}

static int
2249
remoteDispatchNetworkCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2250 2251
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2252
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
2253
                                remote_error *rerr,
2254 2255 2256 2257 2258
                                remote_network_create_xml_args *args,
                                remote_network_create_xml_ret *ret)
{
    virNetworkPtr net;

2259
    net = virNetworkCreateXML (conn, args->xml);
2260
    if (net == NULL) {
2261
        remoteDispatchConnError(rerr, conn);
2262 2263
        return -1;
    }
2264 2265

    make_nonnull_network (&ret->net, net);
2266
    virNetworkFree(net);
2267 2268 2269 2270
    return 0;
}

static int
2271
remoteDispatchNetworkDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2272 2273
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
2274
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
2275
                                remote_error *rerr,
2276 2277 2278 2279 2280
                                remote_network_define_xml_args *args,
                                remote_network_define_xml_ret *ret)
{
    virNetworkPtr net;

2281
    net = virNetworkDefineXML (conn, args->xml);
2282
    if (net == NULL) {
2283
        remoteDispatchConnError(rerr, conn);
2284 2285
        return -1;
    }
2286 2287

    make_nonnull_network (&ret->net, net);
2288
    virNetworkFree(net);
2289 2290 2291 2292
    return 0;
}

static int
2293
remoteDispatchNetworkDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
2294 2295
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2296
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2297
                              remote_error *rerr,
2298 2299 2300 2301 2302
                              remote_network_destroy_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2303
    net = get_nonnull_network (conn, args->net);
2304
    if (net == NULL) {
2305
        remoteDispatchConnError(rerr, conn);
2306
        return -1;
2307 2308
    }

2309 2310
    if (virNetworkDestroy (net) == -1) {
        virNetworkFree(net);
2311
        remoteDispatchConnError(rerr, conn);
2312
        return -1;
2313 2314
    }
    virNetworkFree(net);
2315 2316 2317 2318
    return 0;
}

static int
2319
remoteDispatchNetworkDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
2320 2321
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2322
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
2323
                              remote_error *rerr,
2324 2325 2326 2327 2328
                              remote_network_dump_xml_args *args,
                              remote_network_dump_xml_ret *ret)
{
    virNetworkPtr net;

2329
    net = get_nonnull_network (conn, args->net);
2330
    if (net == NULL) {
2331
        remoteDispatchConnError(rerr, conn);
2332
        return -1;
2333 2334 2335 2336
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNetworkGetXMLDesc (net, args->flags);
2337 2338
    if (!ret->xml) {
        virNetworkFree(net);
2339
        remoteDispatchConnError(rerr, conn);
2340 2341 2342
        return -1;
    }
    virNetworkFree(net);
2343 2344 2345 2346
    return 0;
}

static int
2347
remoteDispatchNetworkGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
2348 2349
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2350
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2351
                                   remote_error *rerr,
2352 2353 2354 2355 2356
                                   remote_network_get_autostart_args *args,
                                   remote_network_get_autostart_ret *ret)
{
    virNetworkPtr net;

2357
    net = get_nonnull_network (conn, args->net);
2358
    if (net == NULL) {
2359
        remoteDispatchConnError(rerr, conn);
2360
        return -1;
2361 2362
    }

2363 2364
    if (virNetworkGetAutostart (net, &ret->autostart) == -1) {
        virNetworkFree(net);
2365
        remoteDispatchConnError(rerr, conn);
2366
        return -1;
2367 2368
    }
    virNetworkFree(net);
2369 2370 2371 2372
    return 0;
}

static int
2373
remoteDispatchNetworkGetBridgeName (struct qemud_server *server ATTRIBUTE_UNUSED,
2374 2375
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
2376
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
2377
                                    remote_error *rerr,
2378 2379 2380 2381 2382
                                    remote_network_get_bridge_name_args *args,
                                    remote_network_get_bridge_name_ret *ret)
{
    virNetworkPtr net;

2383
    net = get_nonnull_network (conn, args->net);
2384
    if (net == NULL) {
2385
        remoteDispatchConnError(rerr, conn);
2386
        return -1;
2387 2388 2389 2390
    }

    /* remoteDispatchClientRequest will free this. */
    ret->name = virNetworkGetBridgeName (net);
2391 2392
    if (!ret->name) {
        virNetworkFree(net);
2393
        remoteDispatchConnError(rerr, conn);
2394 2395 2396
        return -1;
    }
    virNetworkFree(net);
2397 2398 2399 2400
    return 0;
}

static int
2401
remoteDispatchNetworkLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
2402 2403
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2404
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2405
                                   remote_error *rerr,
2406 2407 2408 2409 2410
                                   remote_network_lookup_by_name_args *args,
                                   remote_network_lookup_by_name_ret *ret)
{
    virNetworkPtr net;

2411
    net = virNetworkLookupByName (conn, args->name);
2412
    if (net == NULL) {
2413
        remoteDispatchConnError(rerr, conn);
2414 2415
        return -1;
    }
2416 2417

    make_nonnull_network (&ret->net, net);
2418
    virNetworkFree(net);
2419 2420 2421 2422
    return 0;
}

static int
2423
remoteDispatchNetworkLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
2424 2425
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2426
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2427
                                   remote_error *rerr,
2428 2429 2430 2431 2432
                                   remote_network_lookup_by_uuid_args *args,
                                   remote_network_lookup_by_uuid_ret *ret)
{
    virNetworkPtr net;

2433
    net = virNetworkLookupByUUID (conn, (unsigned char *) args->uuid);
2434
    if (net == NULL) {
2435
        remoteDispatchConnError(rerr, conn);
2436 2437
        return -1;
    }
2438 2439

    make_nonnull_network (&ret->net, net);
2440
    virNetworkFree(net);
2441 2442 2443 2444
    return 0;
}

static int
2445
remoteDispatchNetworkSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
2446 2447
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2448
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
2449
                                   remote_error *rerr,
2450 2451 2452 2453 2454
                                   remote_network_set_autostart_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2455
    net = get_nonnull_network (conn, args->net);
2456
    if (net == NULL) {
2457
        remoteDispatchConnError(rerr, conn);
2458
        return -1;
2459 2460
    }

2461 2462
    if (virNetworkSetAutostart (net, args->autostart) == -1) {
        virNetworkFree(net);
2463
        remoteDispatchConnError(rerr, conn);
2464
        return -1;
2465 2466
    }
    virNetworkFree(net);
2467 2468 2469 2470
    return 0;
}

static int
2471
remoteDispatchNetworkUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
2472 2473
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
2474
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
2475
                               remote_error *rerr,
2476 2477 2478 2479 2480
                               remote_network_undefine_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virNetworkPtr net;

2481
    net = get_nonnull_network (conn, args->net);
2482
    if (net == NULL) {
2483
        remoteDispatchConnError(rerr, conn);
2484
        return -1;
2485 2486
    }

2487 2488
    if (virNetworkUndefine (net) == -1) {
        virNetworkFree(net);
2489
        remoteDispatchConnError(rerr, conn);
2490
        return -1;
2491 2492
    }
    virNetworkFree(net);
2493 2494 2495 2496
    return 0;
}

static int
2497
remoteDispatchNumOfDefinedNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2498 2499
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
2500
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
2501
                                    remote_error *rerr,
2502 2503 2504 2505
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_num_of_defined_networks_ret *ret)
{

2506
    ret->num = virConnectNumOfDefinedNetworks (conn);
2507
    if (ret->num == -1) {
2508
        remoteDispatchConnError(rerr, conn);
2509 2510
        return -1;
    }
2511 2512 2513 2514 2515

    return 0;
}

static int
2516
remoteDispatchNumOfDomains (struct qemud_server *server ATTRIBUTE_UNUSED,
2517 2518
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn,
2519
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2520
                            remote_error *rerr,
2521 2522 2523 2524
                            void *args ATTRIBUTE_UNUSED,
                            remote_num_of_domains_ret *ret)
{

2525
    ret->num = virConnectNumOfDomains (conn);
2526
    if (ret->num == -1) {
2527
        remoteDispatchConnError(rerr, conn);
2528 2529
        return -1;
    }
2530 2531 2532 2533 2534

    return 0;
}

static int
2535
remoteDispatchNumOfNetworks (struct qemud_server *server ATTRIBUTE_UNUSED,
2536 2537
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn,
2538
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
2539
                             remote_error *rerr,
2540 2541 2542 2543
                             void *args ATTRIBUTE_UNUSED,
                             remote_num_of_networks_ret *ret)
{

2544
    ret->num = virConnectNumOfNetworks (conn);
2545
    if (ret->num == -1) {
2546
        remoteDispatchConnError(rerr, conn);
2547 2548
        return -1;
    }
2549 2550 2551 2552

    return 0;
}

2553

D
Daniel Veillard 已提交
2554 2555 2556 2557 2558
/*-------------------------------------------------------------*/
static int
remoteDispatchNumOfInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
2559
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
                               remote_error *rerr,
                               void *args ATTRIBUTE_UNUSED,
                               remote_num_of_interfaces_ret *ret)
{

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

    return 0;
}

static int
remoteDispatchListInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
                              virConnectPtr conn,
2578
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
                              remote_error *rerr,
                              remote_list_interfaces_args *args,
                              remote_list_interfaces_ret *ret)
{

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

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

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

    return 0;
}

2608 2609 2610 2611
static int
remoteDispatchNumOfDefinedInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
2612
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
                                      remote_error *rerr,
                                      void *args ATTRIBUTE_UNUSED,
                                      remote_num_of_defined_interfaces_ret *ret)
{

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

    return 0;
}

static int
remoteDispatchListDefinedInterfaces (struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
2631
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660
                                     remote_error *rerr,
                                     remote_list_defined_interfaces_args *args,
                                     remote_list_defined_interfaces_ret *ret)
{

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

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

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

    return 0;
}

D
Daniel Veillard 已提交
2661 2662 2663 2664
static int
remoteDispatchInterfaceLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
2665
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
2666 2667 2668 2669
                                     remote_error *rerr,
                                     remote_interface_lookup_by_name_args *args,
                                     remote_interface_lookup_by_name_ret *ret)
{
2670
    virInterfacePtr iface;
D
Daniel Veillard 已提交
2671

2672 2673
    iface = virInterfaceLookupByName (conn, args->name);
    if (iface == NULL) {
D
Daniel Veillard 已提交
2674 2675 2676 2677
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

2678 2679
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
2680 2681 2682 2683 2684 2685 2686
    return 0;
}

static int
remoteDispatchInterfaceLookupByMacString (struct qemud_server *server ATTRIBUTE_UNUSED,
                                          struct qemud_client *client ATTRIBUTE_UNUSED,
                                          virConnectPtr conn,
2687
                                          remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
2688 2689 2690 2691
                                          remote_error *rerr,
                                          remote_interface_lookup_by_mac_string_args *args,
                                          remote_interface_lookup_by_mac_string_ret *ret)
{
2692
    virInterfacePtr iface;
D
Daniel Veillard 已提交
2693

2694 2695
    iface = virInterfaceLookupByMACString (conn, args->mac);
    if (iface == NULL) {
D
Daniel Veillard 已提交
2696 2697 2698 2699
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

2700 2701
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
2702 2703 2704 2705 2706 2707 2708
    return 0;
}

static int
remoteDispatchInterfaceGetXmlDesc (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
2709
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
2710 2711 2712 2713
                                   remote_error *rerr,
                                   remote_interface_get_xml_desc_args *args,
                                   remote_interface_get_xml_desc_ret *ret)
{
2714
    virInterfacePtr iface;
D
Daniel Veillard 已提交
2715

2716 2717
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
2718 2719 2720 2721 2722
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    /* remoteDispatchClientRequest will free this. */
2723
    ret->xml = virInterfaceGetXMLDesc (iface, args->flags);
D
Daniel Veillard 已提交
2724
    if (!ret->xml) {
2725
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
2726 2727 2728
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
2729
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
2730 2731 2732 2733 2734 2735 2736
    return 0;
}

static int
remoteDispatchInterfaceDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
2737
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
D
Daniel Veillard 已提交
2738 2739 2740 2741
                                  remote_error *rerr,
                                  remote_interface_define_xml_args *args,
                                  remote_interface_define_xml_ret *ret)
{
2742
    virInterfacePtr iface;
D
Daniel Veillard 已提交
2743

2744 2745
    iface = virInterfaceDefineXML (conn, args->xml, args->flags);
    if (iface == NULL) {
D
Daniel Veillard 已提交
2746 2747 2748 2749
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

2750 2751
    make_nonnull_interface (&ret->iface, iface);
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
2752 2753 2754 2755 2756
    return 0;
}

static int
remoteDispatchInterfaceUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
2757 2758 2759 2760 2761 2762
                                 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 已提交
2763
{
2764
    virInterfacePtr iface;
D
Daniel Veillard 已提交
2765

2766 2767
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
2768 2769 2770 2771
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

2772 2773
    if (virInterfaceUndefine (iface) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
2774 2775 2776
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
2777
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
2778 2779 2780 2781 2782
    return 0;
}

static int
remoteDispatchInterfaceCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
2783 2784 2785 2786 2787 2788
                               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 已提交
2789
{
2790
    virInterfacePtr iface;
D
Daniel Veillard 已提交
2791

2792 2793
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
2794 2795 2796 2797
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

2798 2799
    if (virInterfaceCreate (iface, args->flags) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
2800 2801 2802
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
2803
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
2804 2805 2806 2807 2808
    return 0;
}

static int
remoteDispatchInterfaceDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
2809 2810 2811 2812 2813 2814
                                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 已提交
2815
{
2816
    virInterfacePtr iface;
D
Daniel Veillard 已提交
2817

2818 2819
    iface = get_nonnull_interface (conn, args->iface);
    if (iface == NULL) {
D
Daniel Veillard 已提交
2820 2821 2822 2823
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

2824 2825
    if (virInterfaceDestroy (iface, args->flags) == -1) {
        virInterfaceFree(iface);
D
Daniel Veillard 已提交
2826 2827 2828
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
2829
    virInterfaceFree(iface);
D
Daniel Veillard 已提交
2830 2831 2832 2833 2834
    return 0;
}

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

2835
static int
2836
remoteDispatchAuthList (struct qemud_server *server,
2837
                        struct qemud_client *client,
2838
                        virConnectPtr conn ATTRIBUTE_UNUSED,
2839
                        remote_message_header *hdr ATTRIBUTE_UNUSED,
2840
                        remote_error *rerr,
2841 2842 2843 2844
                        void *args ATTRIBUTE_UNUSED,
                        remote_auth_list_ret *ret)
{
    ret->types.types_len = 1;
2845
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
2846 2847
        remoteDispatchOOMError(rerr);
        return -1;
2848
    }
2849 2850 2851
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
2852
    ret->types.types_val[0] = client->auth;
2853
    virMutexUnlock(&client->lock);
2854

2855 2856 2857 2858 2859 2860 2861 2862
    return 0;
}


#if HAVE_SASL
/*
 * NB, keep in sync with similar method in src/remote_internal.c
 */
2863
static char *addrToString(remote_error *rerr,
2864 2865 2866 2867 2868 2869 2870 2871 2872
                          struct sockaddr_storage *sa, socklen_t salen) {
    char host[1024], port[20];
    char *addr;
    int err;

    if ((err = getnameinfo((struct sockaddr *)sa, salen,
                           host, sizeof(host),
                           port, sizeof(port),
                           NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
2873 2874 2875
        remoteDispatchFormatError(rerr,
                                  _("Cannot resolve address %d: %s"),
                                  err, gai_strerror(err));
2876 2877 2878
        return NULL;
    }

2879
    if (virAsprintf(&addr, "%s;%s", host, port) == -1) {
2880
        virReportOOMError();
2881 2882 2883 2884 2885 2886 2887 2888 2889
        return NULL;
    }

    return addr;
}


/*
 * Initializes the SASL session in prepare for authentication
2890
 * and gives the client a list of allowed mechanisms to choose
2891 2892 2893 2894
 *
 * XXX callbacks for stuff like password verification ?
 */
static int
2895
remoteDispatchAuthSaslInit (struct qemud_server *server,
2896
                            struct qemud_client *client,
2897
                            virConnectPtr conn ATTRIBUTE_UNUSED,
2898
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
2899
                            remote_error *rerr,
2900 2901 2902 2903
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret)
{
    const char *mechlist = NULL;
2904
    sasl_security_properties_t secprops;
2905 2906 2907 2908 2909
    int err;
    struct sockaddr_storage sa;
    socklen_t salen;
    char *localAddr, *remoteAddr;

2910 2911 2912
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
2913

2914 2915 2916
    REMOTE_DEBUG("Initialize SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn != NULL) {
2917
        VIR_ERROR0(_("client tried invalid SASL init request"));
2918
        goto authfail;
2919 2920 2921 2922 2923
    }

    /* Get local address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getsockname(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
2924
        char ebuf[1024];
2925
        remoteDispatchFormatError(rerr,
2926
                                  _("failed to get sock address: %s"),
2927
                                  virStrerror(errno, ebuf, sizeof ebuf));
2928
        goto error;
2929
    }
2930
    if ((localAddr = addrToString(rerr, &sa, salen)) == NULL) {
2931
        goto error;
2932 2933 2934 2935 2936
    }

    /* Get remote address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getpeername(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
2937
        char ebuf[1024];
2938
        remoteDispatchFormatError(rerr, _("failed to get peer address: %s"),
2939
                                  virStrerror(errno, ebuf, sizeof ebuf));
2940
        VIR_FREE(localAddr);
2941
        goto error;
2942
    }
2943
    if ((remoteAddr = addrToString(rerr, &sa, salen)) == NULL) {
2944
        VIR_FREE(localAddr);
2945
        goto error;
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955
    }

    err = sasl_server_new("libvirt",
                          NULL, /* FQDN - just delegates to gethostname */
                          NULL, /* User realm */
                          localAddr,
                          remoteAddr,
                          NULL, /* XXX Callbacks */
                          SASL_SUCCESS_DATA,
                          &client->saslconn);
2956 2957
    VIR_FREE(localAddr);
    VIR_FREE(remoteAddr);
2958
    if (err != SASL_OK) {
2959 2960
        VIR_ERROR(_("sasl context setup failed %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
2961
        client->saslconn = NULL;
2962
        goto authfail;
2963 2964
    }

2965 2966 2967 2968 2969 2970 2971
    /* 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 已提交
2972
            VIR_ERROR0(_("cannot get TLS cipher size"));
2973 2974
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
2975
            goto authfail;
2976 2977 2978 2979 2980
        }
        ssf *= 8; /* tls key size is bytes, sasl wants bits */

        err = sasl_setprop(client->saslconn, SASL_SSF_EXTERNAL, &ssf);
        if (err != SASL_OK) {
2981 2982
            VIR_ERROR(_("cannot set SASL external SSF %d (%s)"),
                      err, sasl_errstring(err, NULL, NULL));
2983 2984
            sasl_dispose(&client->saslconn);
            client->saslconn = NULL;
2985
            goto authfail;
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008
        }
    }

    memset (&secprops, 0, sizeof secprops);
    if (client->type == QEMUD_SOCK_TYPE_TLS ||
        client->type == QEMUD_SOCK_TYPE_UNIX) {
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
        secprops.min_ssf = 0;
        secprops.max_ssf = 0;
        secprops.maxbufsize = 8192;
        secprops.security_flags = 0;
    } else {
        /* Plain TCP, better get an SSF layer */
        secprops.min_ssf = 56; /* Good enough to require kerberos */
        secprops.max_ssf = 100000; /* Arbitrary big number */
        secprops.maxbufsize = 8192;
        /* Forbid any anonymous or trivially crackable auth */
        secprops.security_flags =
            SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT;
    }

    err = sasl_setprop(client->saslconn, SASL_SEC_PROPS, &secprops);
    if (err != SASL_OK) {
3009 3010
        VIR_ERROR(_("cannot set SASL security props %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3011 3012
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3013
        goto authfail;
3014 3015
    }

3016 3017 3018 3019 3020 3021 3022 3023 3024
    err = sasl_listmech(client->saslconn,
                        NULL, /* Don't need to set user */
                        "", /* Prefix */
                        ",", /* Separator */
                        "", /* Suffix */
                        &mechlist,
                        NULL,
                        NULL);
    if (err != SASL_OK) {
3025 3026
        VIR_ERROR(_("cannot list SASL mechanisms %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3027 3028
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3029
        goto authfail;
3030 3031 3032 3033
    }
    REMOTE_DEBUG("Available mechanisms for client: '%s'", mechlist);
    ret->mechlist = strdup(mechlist);
    if (!ret->mechlist) {
3034
        VIR_ERROR0(_("cannot allocate mechlist"));
3035 3036
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3037
        goto authfail;
3038 3039
    }

3040
    virMutexUnlock(&client->lock);
3041
    return 0;
3042 3043 3044 3045

authfail:
    remoteDispatchAuthError(rerr);
error:
3046
    virMutexUnlock(&client->lock);
3047
    return -1;
3048 3049 3050
}


3051
/* We asked for an SSF layer, so sanity check that we actually
3052 3053 3054
 * got what we asked for */
static int
remoteSASLCheckSSF (struct qemud_client *client,
3055
                    remote_error *rerr) {
3056 3057 3058 3059 3060 3061 3062 3063 3064
    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) {
3065 3066
        VIR_ERROR(_("cannot query SASL ssf on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3067
        remoteDispatchAuthError(rerr);
3068 3069 3070 3071 3072 3073 3074
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    ssf = *(const int *)val;
    REMOTE_DEBUG("negotiated an SSF of %d", ssf);
    if (ssf < 56) { /* 56 is good for Kerberos */
3075
        VIR_ERROR(_("negotiated SSF %d was not strong enough"), ssf);
3076
        remoteDispatchAuthError(rerr);
3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }

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

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

3094 3095 3096
static int
remoteSASLCheckAccess (struct qemud_server *server,
                       struct qemud_client *client,
3097
                       remote_error *rerr) {
3098 3099 3100 3101 3102 3103
    const void *val;
    int err;
    char **wildcards;

    err = sasl_getprop(client->saslconn, SASL_USERNAME, &val);
    if (err != SASL_OK) {
3104 3105
        VIR_ERROR(_("cannot query SASL username on connection %d (%s)"),
                  err, sasl_errstring(err, NULL, NULL));
3106
        remoteDispatchAuthError(rerr);
3107 3108 3109 3110 3111
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    if (val == NULL) {
3112
        VIR_ERROR0(_("no client username was found"));
3113
        remoteDispatchAuthError(rerr);
3114 3115 3116 3117 3118 3119 3120 3121
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }
    REMOTE_DEBUG("SASL client username %s", (const char *)val);

    client->saslUsername = strdup((const char*)val);
    if (client->saslUsername == NULL) {
3122
        VIR_ERROR0(_("out of memory copying username"));
3123
        remoteDispatchAuthError(rerr);
3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
        return -1;
    }

    /* If the list is not set, allow any DN. */
    wildcards = server->saslUsernameWhitelist;
    if (!wildcards)
        return 0; /* No ACL, allow all */

    while (*wildcards) {
        if (fnmatch (*wildcards, client->saslUsername, 0) == 0)
            return 0; /* Allowed */
        wildcards++;
    }

    /* Denied */
3141
    VIR_ERROR(_("SASL client %s not allowed in whitelist"), client->saslUsername);
3142
    remoteDispatchAuthError(rerr);
3143 3144 3145 3146 3147 3148
    sasl_dispose(&client->saslconn);
    client->saslconn = NULL;
    return -1;
}


3149 3150 3151 3152
/*
 * This starts the SASL authentication negotiation.
 */
static int
3153 3154
remoteDispatchAuthSaslStart (struct qemud_server *server,
                             struct qemud_client *client,
3155
                             virConnectPtr conn ATTRIBUTE_UNUSED,
3156
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
3157
                             remote_error *rerr,
3158 3159 3160 3161 3162 3163 3164
                             remote_auth_sasl_start_args *args,
                             remote_auth_sasl_start_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

3165 3166 3167
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3168

3169 3170 3171
    REMOTE_DEBUG("Start SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
3172
        VIR_ERROR0(_("client tried invalid SASL start request"));
3173
        goto authfail;
3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186
    }

    REMOTE_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
                 args->mech, args->data.data_len, args->nil);
    err = sasl_server_start(client->saslconn,
                            args->mech,
                            /* NB, distinction of NULL vs "" is *critical* in SASL */
                            args->nil ? NULL : args->data.data_val,
                            args->data.data_len,
                            &serverout,
                            &serveroutlen);
    if (err != SASL_OK &&
        err != SASL_CONTINUE) {
3187 3188
        VIR_ERROR(_("sasl start failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3189 3190
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3191
        goto authfail;
3192 3193
    }
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3194
        VIR_ERROR(_("sasl start reply data too long %d"), serveroutlen);
3195 3196
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3197
        goto authfail;
3198 3199 3200 3201
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3202
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
3203
            remoteDispatchOOMError(rerr);
3204
            goto error;
3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216
        }
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

    REMOTE_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
    if (err == SASL_CONTINUE) {
        ret->complete = 0;
    } else {
3217
        if (remoteSASLCheckSSF(client, rerr) < 0)
3218
            goto error;
3219

3220
        /* Check username whitelist ACL */
3221
        if (remoteSASLCheckAccess(server, client, rerr) < 0)
3222
            goto error;
3223

3224 3225 3226 3227 3228
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

3229
    virMutexUnlock(&client->lock);
3230
    return 0;
3231 3232 3233 3234

authfail:
    remoteDispatchAuthError(rerr);
error:
3235
    virMutexUnlock(&client->lock);
3236
    return -1;
3237 3238 3239 3240
}


static int
3241 3242
remoteDispatchAuthSaslStep (struct qemud_server *server,
                            struct qemud_client *client,
3243
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3244
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3245
                            remote_error *rerr,
3246 3247 3248 3249 3250 3251 3252
                            remote_auth_sasl_step_args *args,
                            remote_auth_sasl_step_ret *ret)
{
    const char *serverout;
    unsigned int serveroutlen;
    int err;

3253 3254 3255
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3256

3257 3258 3259
    REMOTE_DEBUG("Step SASL auth %d", client->fd);
    if (client->auth != REMOTE_AUTH_SASL ||
        client->saslconn == NULL) {
3260
        VIR_ERROR0(_("client tried invalid SASL start request"));
3261
        goto authfail;
3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273
    }

    REMOTE_DEBUG("Using SASL Data %d bytes, nil: %d",
                 args->data.data_len, args->nil);
    err = sasl_server_step(client->saslconn,
                           /* NB, distinction of NULL vs "" is *critical* in SASL */
                           args->nil ? NULL : args->data.data_val,
                           args->data.data_len,
                           &serverout,
                           &serveroutlen);
    if (err != SASL_OK &&
        err != SASL_CONTINUE) {
3274 3275
        VIR_ERROR(_("sasl step failed %d (%s)"),
                  err, sasl_errdetail(client->saslconn));
3276 3277
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3278
        goto authfail;
3279 3280 3281
    }

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3282 3283
        VIR_ERROR(_("sasl step reply data too long %d"),
                  serveroutlen);
3284 3285
        sasl_dispose(&client->saslconn);
        client->saslconn = NULL;
3286
        goto authfail;
3287 3288 3289 3290
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3291
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
3292
            remoteDispatchOOMError(rerr);
3293
            goto error;
3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305
        }
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

    REMOTE_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
    if (err == SASL_CONTINUE) {
        ret->complete = 0;
    } else {
3306
        if (remoteSASLCheckSSF(client, rerr) < 0)
3307
            goto error;
3308

3309
        /* Check username whitelist ACL */
3310
        if (remoteSASLCheckAccess(server, client, rerr) < 0)
3311
            goto error;
3312

3313 3314 3315 3316 3317
        REMOTE_DEBUG("Authentication successful %d", client->fd);
        ret->complete = 1;
        client->auth = REMOTE_AUTH_NONE;
    }

3318
    virMutexUnlock(&client->lock);
3319
    return 0;
3320 3321 3322 3323

authfail:
    remoteDispatchAuthError(rerr);
error:
3324
    virMutexUnlock(&client->lock);
3325
    return -1;
3326 3327 3328 3329 3330
}


#else /* HAVE_SASL */
static int
3331
remoteDispatchAuthSaslInit (struct qemud_server *server ATTRIBUTE_UNUSED,
3332 3333
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3334
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3335
                            remote_error *rerr,
3336 3337 3338
                            void *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
3339
    VIR_ERROR0(_("client tried unsupported SASL init request"));
3340
    remoteDispatchAuthError(rerr);
3341 3342 3343 3344
    return -1;
}

static int
3345
remoteDispatchAuthSaslStart (struct qemud_server *server ATTRIBUTE_UNUSED,
3346 3347
                             struct qemud_client *client ATTRIBUTE_UNUSED,
                             virConnectPtr conn ATTRIBUTE_UNUSED,
3348
                             remote_message_header *hdr ATTRIBUTE_UNUSED,
3349
                             remote_error *rerr,
3350 3351 3352
                             remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                             remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
3353
    VIR_ERROR0(_("client tried unsupported SASL start request"));
3354
    remoteDispatchAuthError(rerr);
3355 3356 3357 3358
    return -1;
}

static int
3359
remoteDispatchAuthSaslStep (struct qemud_server *server ATTRIBUTE_UNUSED,
3360 3361
                            struct qemud_client *client ATTRIBUTE_UNUSED,
                            virConnectPtr conn ATTRIBUTE_UNUSED,
3362
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
3363
                            remote_error *rerr,
3364 3365 3366
                            remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
3367
    VIR_ERROR0(_("client tried unsupported SASL step request"));
3368
    remoteDispatchAuthError(rerr);
3369 3370 3371 3372 3373
    return -1;
}
#endif /* HAVE_SASL */


3374 3375 3376 3377 3378
#if HAVE_POLKIT1
static int
remoteDispatchAuthPolkit (struct qemud_server *server,
                          struct qemud_client *client,
                          virConnectPtr conn ATTRIBUTE_UNUSED,
3379
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422
                          remote_error *rerr,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
    pid_t callerPid;
    uid_t callerUid;
    const char *action;
    int status = -1;
    char pidbuf[50];
    int rv;

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

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

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

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

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

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

    rv = snprintf(pidbuf, sizeof pidbuf, "%d", callerPid);
    if (rv < 0 || rv >= sizeof pidbuf) {
        VIR_ERROR(_("Caller PID was too large %d"), callerPid);
3423
        goto authfail;
3424 3425 3426 3427
    }

    if (virRun(NULL, pkcheck, &status) < 0) {
        VIR_ERROR(_("Cannot invoke %s"), PKCHECK_PATH);
3428
        goto authfail;
3429 3430
    }
    if (status != 0) {
3431
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d, result: %d"),
3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
                  action, callerPid, callerUid, status);
        goto authfail;
    }
    VIR_INFO(_("Policy allowed action %s from pid %d, uid %d"),
             action, callerPid, callerUid);
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;

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

authfail:
    remoteDispatchAuthError(rerr);
    virMutexUnlock(&client->lock);
    return -1;
}
#elif HAVE_POLKIT0
3449
static int
3450
remoteDispatchAuthPolkit (struct qemud_server *server,
3451
                          struct qemud_client *client,
3452
                          virConnectPtr conn ATTRIBUTE_UNUSED,
3453
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
3454
                          remote_error *rerr,
3455 3456 3457 3458 3459
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret)
{
    pid_t callerPid;
    uid_t callerUid;
3460 3461 3462 3463 3464 3465
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
3466 3467
    const char *action;

3468 3469 3470
    virMutexLock(&server->lock);
    virMutexLock(&client->lock);
    virMutexUnlock(&server->lock);
3471 3472

    action = client->readonly ?
3473 3474
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
3475 3476 3477

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

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

3487
    VIR_INFO(_("Checking PID %d running as %d"), callerPid, callerUid);
3488 3489 3490
    dbus_error_init(&err);
    if (!(pkcaller = polkit_caller_new_from_pid(server->sysbus,
                                                callerPid, &err))) {
3491
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
3492
        dbus_error_free(&err);
3493
        goto authfail;
3494
    }
3495

3496
    if (!(pkaction = polkit_action_new())) {
3497
        char ebuf[1024];
3498
        VIR_ERROR(_("Failed to create polkit action %s"),
3499
                  virStrerror(errno, ebuf, sizeof ebuf));
3500
        polkit_caller_unref(pkcaller);
3501
        goto authfail;
3502 3503 3504 3505 3506
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
3507
        char ebuf[1024];
3508
        VIR_ERROR(_("Failed to create polkit context %s"),
3509
                  (pkerr ? polkit_error_get_error_message(pkerr)
3510
                   : virStrerror(errno, ebuf, sizeof ebuf)));
3511 3512 3513 3514 3515
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
3516
        goto authfail;
3517
    }
3518

3519
#if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
3520 3521 3522 3523 3524 3525
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
3526 3527 3528
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
3529
        goto authfail;
3530
    }
3531
#else
3532 3533 3534
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
3535
#endif
3536 3537 3538 3539
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
3540
        VIR_ERROR(_("Policy kit denied action %s from pid %d, uid %d, result: %s"),
3541 3542
                  action, callerPid, callerUid,
                  polkit_result_to_string_representation(pkresult));
3543
        goto authfail;
3544
    }
3545
    VIR_INFO(_("Policy allowed action %s from pid %d, uid %d, result %s"),
3546 3547 3548 3549
             action, callerPid, callerUid,
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
    client->auth = REMOTE_AUTH_NONE;
3550

3551
    virMutexUnlock(&client->lock);
3552
    return 0;
3553 3554 3555

authfail:
    remoteDispatchAuthError(rerr);
3556
    virMutexUnlock(&client->lock);
3557
    return -1;
3558 3559
}

3560
#else /* !HAVE_POLKIT0 & !HAVE_POLKIT1*/
3561 3562

static int
3563
remoteDispatchAuthPolkit (struct qemud_server *server ATTRIBUTE_UNUSED,
3564
                          struct qemud_client *client ATTRIBUTE_UNUSED,
3565
                          virConnectPtr conn ATTRIBUTE_UNUSED,
3566
                          remote_message_header *hdr ATTRIBUTE_UNUSED,
3567 3568 3569
                          remote_error *rerr,
                          void *args ATTRIBUTE_UNUSED,
                          remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
3570
{
3571
    VIR_ERROR0(_("client tried unsupported PolicyKit init request"));
3572
    remoteDispatchAuthError(rerr);
3573 3574
    return -1;
}
3575
#endif /* HAVE_POLKIT1 */
3576

3577 3578 3579 3580 3581 3582 3583 3584

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


static int
remoteDispatchListDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
3585 3586
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3587
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
3588
                                       remote_error *rerr,
3589 3590 3591 3592 3593
                                       remote_list_defined_storage_pools_args *args,
                                       remote_list_defined_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
3594 3595
        remoteDispatchFormatError (rerr, "%s",
                            _("maxnames > REMOTE_NETWORK_NAME_LIST_MAX"));
3596
        return -1;
3597 3598 3599
    }

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

    ret->names.names_len =
3606
        virConnectListDefinedStoragePools (conn,
3607 3608 3609
                                           ret->names.names_val, args->maxnames);
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
3610
        remoteDispatchConnError(rerr, conn);
3611 3612
        return -1;
    }
3613 3614 3615 3616 3617 3618

    return 0;
}

static int
remoteDispatchListStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
3619 3620
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
3621
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
3622
                                remote_error *rerr,
3623 3624 3625 3626 3627
                                remote_list_storage_pools_args *args,
                                remote_list_storage_pools_ret *ret)
{

    if (args->maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
3628 3629 3630
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX"));
        return -1;
3631 3632 3633
    }

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

    ret->names.names_len =
3640
        virConnectListStoragePools (conn,
3641
                                ret->names.names_val, args->maxnames);
3642 3643
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
3644
        remoteDispatchConnError(rerr, conn);
3645 3646
        return -1;
    }
3647 3648 3649 3650

    return 0;
}

3651 3652
static int
remoteDispatchFindStoragePoolSources (struct qemud_server *server ATTRIBUTE_UNUSED,
3653 3654
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
3655
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
3656
                                      remote_error *rerr,
3657 3658 3659 3660
                                      remote_find_storage_pool_sources_args *args,
                                      remote_find_storage_pool_sources_ret *ret)
{
    ret->xml =
3661
        virConnectFindStoragePoolSources (conn,
3662 3663 3664
                                          args->type,
                                          args->srcSpec ? *args->srcSpec : NULL,
                                          args->flags);
3665
    if (ret->xml == NULL) {
3666
        remoteDispatchConnError(rerr, conn);
3667
        return -1;
3668
    }
3669 3670 3671 3672 3673

    return 0;
}


3674 3675
static int
remoteDispatchStoragePoolCreate (struct qemud_server *server ATTRIBUTE_UNUSED,
3676 3677
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
3678
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
3679
                                 remote_error *rerr,
3680 3681 3682 3683 3684
                                 remote_storage_pool_create_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

3685
    pool = get_nonnull_storage_pool (conn, args->pool);
3686
    if (pool == NULL) {
3687
        remoteDispatchConnError(rerr, conn);
3688
        return -1;
3689 3690 3691 3692
    }

    if (virStoragePoolCreate (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
3693
        remoteDispatchConnError(rerr, conn);
3694 3695 3696 3697 3698 3699 3700 3701
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
3702 3703
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
3704
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
3705
                                    remote_error *rerr,
3706 3707 3708 3709 3710
                                    remote_storage_pool_create_xml_args *args,
                                    remote_storage_pool_create_xml_ret *ret)
{
    virStoragePoolPtr pool;

3711
    pool = virStoragePoolCreateXML (conn, args->xml, args->flags);
3712
    if (pool == NULL) {
3713
        remoteDispatchConnError(rerr, conn);
3714 3715
        return -1;
    }
3716 3717 3718 3719 3720 3721 3722 3723

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

static int
remoteDispatchStoragePoolDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
3724 3725
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
3726
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
3727
                                    remote_error *rerr,
3728 3729 3730 3731 3732
                                    remote_storage_pool_define_xml_args *args,
                                    remote_storage_pool_define_xml_ret *ret)
{
    virStoragePoolPtr pool;

3733
    pool = virStoragePoolDefineXML (conn, args->xml, args->flags);
3734
    if (pool == NULL) {
3735
        remoteDispatchConnError(rerr, conn);
3736 3737
        return -1;
    }
3738 3739 3740 3741 3742 3743 3744 3745

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

static int
remoteDispatchStoragePoolBuild (struct qemud_server *server ATTRIBUTE_UNUSED,
3746 3747
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
3748
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
3749 3750 3751
                                remote_error *rerr,
                                remote_storage_pool_build_args *args,
                                void *ret ATTRIBUTE_UNUSED)
3752 3753 3754
{
    virStoragePoolPtr pool;

3755
    pool = get_nonnull_storage_pool (conn, args->pool);
3756
    if (pool == NULL) {
3757
        remoteDispatchConnError(rerr, conn);
3758
        return -1;
3759 3760 3761 3762
    }

    if (virStoragePoolBuild (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
3763
        remoteDispatchConnError(rerr, conn);
3764 3765 3766 3767 3768 3769 3770 3771 3772
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolDestroy (struct qemud_server *server ATTRIBUTE_UNUSED,
3773 3774
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3775
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
3776
                                  remote_error *rerr,
3777 3778 3779 3780 3781
                                  remote_storage_pool_destroy_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

3782
    pool = get_nonnull_storage_pool (conn, args->pool);
3783
    if (pool == NULL) {
3784
        remoteDispatchConnError(rerr, conn);
3785
        return -1;
3786 3787 3788 3789
    }

    if (virStoragePoolDestroy (pool) == -1) {
        virStoragePoolFree(pool);
3790
        remoteDispatchConnError(rerr, conn);
3791 3792 3793 3794 3795 3796 3797 3798
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
3799 3800
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
3801
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
3802
                                 remote_error *rerr,
3803 3804 3805 3806 3807
                                 remote_storage_pool_delete_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

3808
    pool = get_nonnull_storage_pool (conn, args->pool);
3809
    if (pool == NULL) {
3810
        remoteDispatchConnError(rerr, conn);
3811
        return -1;
3812 3813 3814 3815
    }

    if (virStoragePoolDelete (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
3816
        remoteDispatchConnError(rerr, conn);
3817 3818 3819 3820 3821 3822 3823 3824
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolRefresh (struct qemud_server *server ATTRIBUTE_UNUSED,
3825 3826
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3827
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
3828
                                  remote_error *rerr,
3829 3830 3831 3832 3833
                                  remote_storage_pool_refresh_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

3834
    pool = get_nonnull_storage_pool (conn, args->pool);
3835
    if (pool == NULL) {
3836
        remoteDispatchConnError(rerr, conn);
3837
        return -1;
3838 3839 3840 3841
    }

    if (virStoragePoolRefresh (pool, args->flags) == -1) {
        virStoragePoolFree(pool);
3842
        remoteDispatchConnError(rerr, conn);
3843 3844 3845 3846 3847 3848 3849 3850
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
3851 3852
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3853
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
3854
                                  remote_error *rerr,
3855 3856 3857 3858 3859 3860
                                  remote_storage_pool_get_info_args *args,
                                  remote_storage_pool_get_info_ret *ret)
{
    virStoragePoolPtr pool;
    virStoragePoolInfo info;

3861
    pool = get_nonnull_storage_pool (conn, args->pool);
3862
    if (pool == NULL) {
3863
        remoteDispatchConnError(rerr, conn);
3864
        return -1;
3865 3866 3867 3868
    }

    if (virStoragePoolGetInfo (pool, &info) == -1) {
        virStoragePoolFree(pool);
3869
        remoteDispatchConnError(rerr, conn);
3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884
        return -1;
    }

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

    virStoragePoolFree(pool);

    return 0;
}

static int
remoteDispatchStoragePoolDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
3885 3886
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
3887
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
3888
                                  remote_error *rerr,
3889 3890 3891 3892 3893
                                  remote_storage_pool_dump_xml_args *args,
                                  remote_storage_pool_dump_xml_ret *ret)
{
    virStoragePoolPtr pool;

3894
    pool = get_nonnull_storage_pool (conn, args->pool);
3895
    if (pool == NULL) {
3896
        remoteDispatchConnError(rerr, conn);
3897
        return -1;
3898 3899 3900 3901 3902 3903
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStoragePoolGetXMLDesc (pool, args->flags);
    if (!ret->xml) {
        virStoragePoolFree(pool);
3904
        remoteDispatchConnError(rerr, conn);
3905 3906 3907 3908 3909 3910 3911 3912
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolGetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
3913 3914
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3915
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
3916
                                       remote_error *rerr,
3917 3918 3919 3920 3921
                                       remote_storage_pool_get_autostart_args *args,
                                       remote_storage_pool_get_autostart_ret *ret)
{
    virStoragePoolPtr pool;

3922
    pool = get_nonnull_storage_pool (conn, args->pool);
3923
    if (pool == NULL) {
3924
        remoteDispatchConnError(rerr, conn);
3925
        return -1;
3926 3927 3928 3929
    }

    if (virStoragePoolGetAutostart (pool, &ret->autostart) == -1) {
        virStoragePoolFree(pool);
3930
        remoteDispatchConnError(rerr, conn);
3931 3932 3933 3934 3935 3936 3937 3938 3939
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}


static int
remoteDispatchStoragePoolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
3940 3941
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3942
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
3943
                                       remote_error *rerr,
3944 3945 3946 3947 3948
                                       remote_storage_pool_lookup_by_name_args *args,
                                       remote_storage_pool_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;

3949
    pool = virStoragePoolLookupByName (conn, args->name);
3950
    if (pool == NULL) {
3951
        remoteDispatchConnError(rerr, conn);
3952 3953
        return -1;
    }
3954 3955 3956 3957 3958 3959 3960 3961

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

static int
remoteDispatchStoragePoolLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
3962 3963
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
3964
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
3965
                                       remote_error *rerr,
3966 3967 3968 3969 3970
                                       remote_storage_pool_lookup_by_uuid_args *args,
                                       remote_storage_pool_lookup_by_uuid_ret *ret)
{
    virStoragePoolPtr pool;

3971
    pool = virStoragePoolLookupByUUID (conn, (unsigned char *) args->uuid);
3972
    if (pool == NULL) {
3973
        remoteDispatchConnError(rerr, conn);
3974 3975
        return -1;
    }
3976 3977 3978 3979 3980 3981 3982 3983

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

static int
remoteDispatchStoragePoolLookupByVolume (struct qemud_server *server ATTRIBUTE_UNUSED,
3984 3985
                                         struct qemud_client *client ATTRIBUTE_UNUSED,
                                         virConnectPtr conn,
3986
                                         remote_message_header *hdr ATTRIBUTE_UNUSED,
3987
                                         remote_error *rerr,
3988 3989 3990 3991 3992 3993
                                         remote_storage_pool_lookup_by_volume_args *args,
                                         remote_storage_pool_lookup_by_volume_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

3994
    vol = get_nonnull_storage_vol (conn, args->vol);
3995
    if (vol == NULL) {
3996
        remoteDispatchConnError(rerr, conn);
3997 3998
        return -1;
    }
3999 4000 4001

    pool = virStoragePoolLookupByVolume (vol);
    virStorageVolFree(vol);
4002
    if (pool == NULL) {
4003
        remoteDispatchConnError(rerr, conn);
4004 4005
        return -1;
    }
4006 4007 4008 4009 4010 4011 4012 4013

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

static int
remoteDispatchStoragePoolSetAutostart (struct qemud_server *server ATTRIBUTE_UNUSED,
4014 4015
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4016
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4017
                                       remote_error *rerr,
4018 4019 4020 4021 4022
                                       remote_storage_pool_set_autostart_args *args,
                                       void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4023
    pool = get_nonnull_storage_pool (conn, args->pool);
4024
    if (pool == NULL) {
4025
        remoteDispatchConnError(rerr, conn);
4026
        return -1;
4027 4028 4029 4030
    }

    if (virStoragePoolSetAutostart (pool, args->autostart) == -1) {
        virStoragePoolFree(pool);
4031
        remoteDispatchConnError(rerr, conn);
4032 4033 4034 4035 4036 4037 4038 4039
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchStoragePoolUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
4040 4041
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4042
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4043
                                   remote_error *rerr,
4044 4045 4046 4047 4048
                                   remote_storage_pool_undefine_args *args,
                                   void *ret ATTRIBUTE_UNUSED)
{
    virStoragePoolPtr pool;

4049
    pool = get_nonnull_storage_pool (conn, args->pool);
4050
    if (pool == NULL) {
4051
        remoteDispatchConnError(rerr, conn);
4052
        return -1;
4053 4054 4055 4056
    }

    if (virStoragePoolUndefine (pool) == -1) {
        virStoragePoolFree(pool);
4057
        remoteDispatchConnError(rerr, conn);
4058 4059 4060 4061 4062 4063 4064 4065
        return -1;
    }
    virStoragePoolFree(pool);
    return 0;
}

static int
remoteDispatchNumOfStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4066 4067
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4068
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4069
                                 remote_error *rerr,
4070 4071 4072 4073
                                 void *args ATTRIBUTE_UNUSED,
                                 remote_num_of_storage_pools_ret *ret)
{

4074
    ret->num = virConnectNumOfStoragePools (conn);
4075
    if (ret->num == -1) {
4076
        remoteDispatchConnError(rerr, conn);
4077 4078
        return -1;
    }
4079 4080 4081 4082 4083 4084

    return 0;
}

static int
remoteDispatchNumOfDefinedStoragePools (struct qemud_server *server ATTRIBUTE_UNUSED,
4085 4086
                                        struct qemud_client *client ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
4087
                                        remote_message_header *hdr ATTRIBUTE_UNUSED,
4088
                                        remote_error *rerr,
4089 4090 4091 4092
                                        void *args ATTRIBUTE_UNUSED,
                                        remote_num_of_defined_storage_pools_ret *ret)
{

4093
    ret->num = virConnectNumOfDefinedStoragePools (conn);
4094
    if (ret->num == -1) {
4095
        remoteDispatchConnError(rerr, conn);
4096 4097
        return -1;
    }
4098 4099 4100 4101 4102 4103

    return 0;
}

static int
remoteDispatchStoragePoolListVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
4104 4105
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4106
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4107
                                      remote_error *rerr,
4108 4109 4110 4111 4112 4113
                                      remote_storage_pool_list_volumes_args *args,
                                      remote_storage_pool_list_volumes_ret *ret)
{
    virStoragePoolPtr pool;

    if (args->maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX) {
4114 4115 4116
        remoteDispatchFormatError (rerr,
                                   "%s", _("maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX"));
        return -1;
4117 4118
    }

4119
    pool = get_nonnull_storage_pool (conn, args->pool);
4120
    if (pool == NULL) {
4121
        remoteDispatchConnError(rerr, conn);
4122
        return -1;
4123 4124 4125
    }

    /* Allocate return buffer. */
4126 4127
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
        virStoragePoolFree(pool);
4128 4129
        remoteDispatchOOMError(rerr);
        return -1;
4130
    }
4131 4132 4133 4134 4135

    ret->names.names_len =
        virStoragePoolListVolumes (pool,
                                   ret->names.names_val, args->maxnames);
    virStoragePoolFree(pool);
4136 4137
    if (ret->names.names_len == -1) {
        VIR_FREE(ret->names.names_val);
4138
        remoteDispatchConnError(rerr, conn);
4139 4140
        return -1;
    }
4141 4142 4143 4144 4145 4146 4147

    return 0;
}


static int
remoteDispatchStoragePoolNumOfVolumes (struct qemud_server *server ATTRIBUTE_UNUSED,
4148 4149
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4150
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4151
                                       remote_error *rerr,
4152 4153 4154 4155 4156
                                       remote_storage_pool_num_of_volumes_args *args,
                                       remote_storage_pool_num_of_volumes_ret *ret)
{
    virStoragePoolPtr pool;

4157
    pool = get_nonnull_storage_pool (conn, args->pool);
4158
    if (pool == NULL) {
4159
        remoteDispatchConnError(rerr, conn);
4160
        return -1;
4161 4162 4163 4164
    }

    ret->num = virStoragePoolNumOfVolumes (pool);
    virStoragePoolFree(pool);
4165
    if (ret->num == -1) {
4166
        remoteDispatchConnError(rerr, conn);
4167 4168
        return -1;
    }
4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181

    return 0;
}


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



static int
remoteDispatchStorageVolCreateXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4182 4183
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4184
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4185
                                   remote_error *rerr,
4186 4187 4188 4189 4190 4191
                                   remote_storage_vol_create_xml_args *args,
                                   remote_storage_vol_create_xml_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

4192
    pool = get_nonnull_storage_pool (conn, args->pool);
4193
    if (pool == NULL) {
4194
        remoteDispatchConnError(rerr, conn);
4195
        return -1;
4196 4197 4198 4199
    }

    vol = virStorageVolCreateXML (pool, args->xml, args->flags);
    virStoragePoolFree(pool);
4200
    if (vol == NULL) {
4201
        remoteDispatchConnError(rerr, conn);
4202 4203
        return -1;
    }
4204 4205 4206 4207 4208 4209

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

4210 4211 4212 4213
static int
remoteDispatchStorageVolCreateXmlFrom (struct qemud_server *server ATTRIBUTE_UNUSED,
                                       struct qemud_client *client ATTRIBUTE_UNUSED,
                                       virConnectPtr conn,
4214
                                       remote_message_header *hdr ATTRIBUTE_UNUSED,
4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229
                                       remote_error *rerr,
                                       remote_storage_vol_create_xml_from_args *args,
                                       remote_storage_vol_create_xml_from_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr clonevol, newvol;

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

    clonevol = get_nonnull_storage_vol (conn, args->clonevol);
    if (clonevol == NULL) {
4230
        virStoragePoolFree(pool);
4231 4232 4233 4234 4235 4236
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    newvol = virStorageVolCreateXMLFrom (pool, args->xml, clonevol,
                                         args->flags);
4237 4238
    virStorageVolFree(clonevol);
    virStoragePoolFree(pool);
4239 4240 4241 4242 4243 4244 4245 4246 4247
    if (newvol == NULL) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }

    make_nonnull_storage_vol (&ret->vol, newvol);
    virStorageVolFree(newvol);
    return 0;
}
4248 4249 4250

static int
remoteDispatchStorageVolDelete (struct qemud_server *server ATTRIBUTE_UNUSED,
4251 4252
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4253
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4254
                                remote_error *rerr,
4255 4256 4257 4258 4259
                                remote_storage_vol_delete_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virStorageVolPtr vol;

4260
    vol = get_nonnull_storage_vol (conn, args->vol);
4261
    if (vol == NULL) {
4262
        remoteDispatchConnError(rerr, conn);
4263
        return -1;
4264 4265 4266 4267
    }

    if (virStorageVolDelete (vol, args->flags) == -1) {
        virStorageVolFree(vol);
4268
        remoteDispatchConnError(rerr, conn);
4269 4270 4271 4272 4273 4274 4275 4276
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}

static int
remoteDispatchStorageVolGetInfo (struct qemud_server *server ATTRIBUTE_UNUSED,
4277 4278
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4279
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4280
                                 remote_error *rerr,
4281 4282 4283 4284 4285 4286
                                 remote_storage_vol_get_info_args *args,
                                 remote_storage_vol_get_info_ret *ret)
{
    virStorageVolPtr vol;
    virStorageVolInfo info;

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

    if (virStorageVolGetInfo (vol, &info) == -1) {
        virStorageVolFree(vol);
4295
        remoteDispatchConnError(rerr, conn);
4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309
        return -1;
    }

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

    virStorageVolFree(vol);

    return 0;
}

static int
remoteDispatchStorageVolDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4310 4311
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4312
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4313
                                 remote_error *rerr,
4314 4315 4316 4317 4318
                                 remote_storage_vol_dump_xml_args *args,
                                 remote_storage_vol_dump_xml_ret *ret)
{
    virStorageVolPtr vol;

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

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virStorageVolGetXMLDesc (vol, args->flags);
    if (!ret->xml) {
        virStorageVolFree(vol);
4329
        remoteDispatchConnError(rerr, conn);
4330 4331 4332 4333 4334 4335 4336 4337 4338
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolGetPath (struct qemud_server *server ATTRIBUTE_UNUSED,
4339 4340
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4341
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4342
                                 remote_error *rerr,
4343 4344 4345 4346 4347
                                 remote_storage_vol_get_path_args *args,
                                 remote_storage_vol_get_path_ret *ret)
{
    virStorageVolPtr vol;

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

    /* remoteDispatchClientRequest will free this. */
    ret->name = virStorageVolGetPath (vol);
    if (!ret->name) {
        virStorageVolFree(vol);
4358
        remoteDispatchConnError(rerr, conn);
4359 4360 4361 4362 4363 4364 4365 4366 4367
        return -1;
    }
    virStorageVolFree(vol);
    return 0;
}


static int
remoteDispatchStorageVolLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
4368 4369
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4370
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4371
                                      remote_error *rerr,
4372 4373 4374 4375 4376 4377
                                      remote_storage_vol_lookup_by_name_args *args,
                                      remote_storage_vol_lookup_by_name_ret *ret)
{
    virStoragePoolPtr pool;
    virStorageVolPtr vol;

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

    vol = virStorageVolLookupByName (pool, args->name);
    virStoragePoolFree(pool);
4386
    if (vol == NULL) {
4387
        remoteDispatchConnError(rerr, conn);
4388 4389
        return -1;
    }
4390 4391 4392 4393 4394 4395 4396 4397

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

static int
remoteDispatchStorageVolLookupByKey (struct qemud_server *server ATTRIBUTE_UNUSED,
4398 4399
                                     struct qemud_client *client ATTRIBUTE_UNUSED,
                                     virConnectPtr conn,
4400
                                     remote_message_header *hdr ATTRIBUTE_UNUSED,
4401
                                     remote_error *rerr,
4402 4403 4404 4405 4406
                                     remote_storage_vol_lookup_by_key_args *args,
                                     remote_storage_vol_lookup_by_key_ret *ret)
{
    virStorageVolPtr vol;

4407
    vol = virStorageVolLookupByKey (conn, args->key);
4408
    if (vol == NULL) {
4409
        remoteDispatchConnError(rerr, conn);
4410 4411
        return -1;
    }
4412 4413 4414 4415 4416 4417 4418 4419 4420

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


static int
remoteDispatchStorageVolLookupByPath (struct qemud_server *server ATTRIBUTE_UNUSED,
4421 4422
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4423
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4424
                                      remote_error *rerr,
4425 4426 4427 4428 4429
                                      remote_storage_vol_lookup_by_path_args *args,
                                      remote_storage_vol_lookup_by_path_ret *ret)
{
    virStorageVolPtr vol;

4430
    vol = virStorageVolLookupByPath (conn, args->path);
4431
    if (vol == NULL) {
4432
        remoteDispatchConnError(rerr, conn);
4433 4434
        return -1;
    }
4435 4436 4437 4438 4439 4440 4441

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


4442 4443 4444 4445 4446 4447
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/

static int
remoteDispatchNodeNumOfDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
4448 4449
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4450
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4451
                                remote_error *rerr,
4452 4453 4454 4455 4456
                                remote_node_num_of_devices_args *args,
                                remote_node_num_of_devices_ret *ret)
{
    CHECK_CONN(client);

4457
    ret->num = virNodeNumOfDevices (conn,
4458 4459
                                    args->cap ? *args->cap : NULL,
                                    args->flags);
4460
    if (ret->num == -1) {
4461
        remoteDispatchConnError(rerr, conn);
4462 4463
        return -1;
    }
4464 4465 4466 4467 4468 4469 4470

    return 0;
}


static int
remoteDispatchNodeListDevices (struct qemud_server *server ATTRIBUTE_UNUSED,
4471 4472
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
4473
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
4474
                               remote_error *rerr,
4475 4476 4477 4478 4479 4480
                               remote_node_list_devices_args *args,
                               remote_node_list_devices_ret *ret)
{
    CHECK_CONN(client);

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
4481 4482 4483
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
4484 4485 4486 4487
    }

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

    ret->names.names_len =
4493
        virNodeListDevices (conn,
4494 4495 4496
                            args->cap ? *args->cap : NULL,
                            ret->names.names_val, args->maxnames, args->flags);
    if (ret->names.names_len == -1) {
4497
        remoteDispatchConnError(rerr, conn);
4498 4499 4500 4501 4502 4503 4504 4505 4506 4507
        VIR_FREE(ret->names.names_val);
        return -1;
    }

    return 0;
}


static int
remoteDispatchNodeDeviceLookupByName (struct qemud_server *server ATTRIBUTE_UNUSED,
4508 4509
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4510
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4511
                                      remote_error *rerr,
4512 4513 4514 4515 4516 4517 4518
                                      remote_node_device_lookup_by_name_args *args,
                                      remote_node_device_lookup_by_name_ret *ret)
{
    virNodeDevicePtr dev;

    CHECK_CONN(client);

4519
    dev = virNodeDeviceLookupByName (conn, args->name);
4520
    if (dev == NULL) {
4521
        remoteDispatchConnError(rerr, conn);
4522 4523
        return -1;
    }
4524 4525 4526 4527 4528 4529 4530 4531 4532

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


static int
remoteDispatchNodeDeviceDumpXml (struct qemud_server *server ATTRIBUTE_UNUSED,
4533 4534
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4535
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4536
                                 remote_error *rerr,
4537 4538 4539 4540 4541 4542
                                 remote_node_device_dump_xml_args *args,
                                 remote_node_device_dump_xml_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

4543
    dev = virNodeDeviceLookupByName(conn, args->name);
4544
    if (dev == NULL) {
4545
        remoteDispatchConnError(rerr, conn);
4546
        return -1;
4547 4548 4549 4550 4551
    }

    /* remoteDispatchClientRequest will free this. */
    ret->xml = virNodeDeviceGetXMLDesc (dev, args->flags);
    if (!ret->xml) {
4552
        remoteDispatchConnError(rerr, conn);
4553 4554 4555 4556 4557 4558 4559 4560 4561 4562
        virNodeDeviceFree(dev);
        return -1;
    }
    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceGetParent (struct qemud_server *server ATTRIBUTE_UNUSED,
4563 4564
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4565
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4566
                                   remote_error *rerr,
4567 4568 4569 4570 4571 4572 4573
                                   remote_node_device_get_parent_args *args,
                                   remote_node_device_get_parent_ret *ret)
{
    virNodeDevicePtr dev;
    const char *parent;
    CHECK_CONN(client);

4574
    dev = virNodeDeviceLookupByName(conn, args->name);
4575
    if (dev == NULL) {
4576
        remoteDispatchConnError(rerr, conn);
4577
        return -1;
4578 4579 4580 4581 4582 4583 4584 4585 4586 4587
    }

    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
        if (VIR_ALLOC(parent_p) < 0) {
4588 4589
            remoteDispatchOOMError(rerr);
            return -1;
4590 4591 4592
        }
        *parent_p = strdup(parent);
        if (*parent_p == NULL) {
4593 4594
            remoteDispatchOOMError(rerr);
            return -1;
4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605
        }
        ret->parent = parent_p;
    }

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceNumOfCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
4606 4607
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
                                   virConnectPtr conn,
4608
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
4609
                                   remote_error *rerr,
4610 4611 4612 4613 4614 4615
                                   remote_node_device_num_of_caps_args *args,
                                   remote_node_device_num_of_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

4616
    dev = virNodeDeviceLookupByName(conn, args->name);
4617
    if (dev == NULL) {
4618
        remoteDispatchConnError(rerr, conn);
4619
        return -1;
4620 4621 4622
    }

    ret->num = virNodeDeviceNumOfCaps(dev);
4623
    if (ret->num < 0) {
4624
        remoteDispatchConnError(rerr, conn);
4625 4626
        return -1;
    }
4627 4628 4629 4630 4631 4632 4633 4634

    virNodeDeviceFree(dev);
    return 0;
}


static int
remoteDispatchNodeDeviceListCaps (struct qemud_server *server ATTRIBUTE_UNUSED,
4635 4636
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4637
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4638
                                  remote_error *rerr,
4639 4640 4641 4642 4643 4644
                                  remote_node_device_list_caps_args *args,
                                  remote_node_device_list_caps_ret *ret)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

4645
    dev = virNodeDeviceLookupByName(conn, args->name);
4646
    if (dev == NULL) {
4647
        remoteDispatchConnError(rerr, conn);
4648
        return -1;
4649 4650 4651
    }

    if (args->maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
4652 4653 4654
        remoteDispatchFormatError(rerr,
                                  "%s", _("maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX"));
        return -1;
4655 4656 4657 4658
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
4659 4660
        remoteDispatchOOMError(rerr);
        return -1;
4661 4662 4663 4664 4665 4666
    }

    ret->names.names_len =
        virNodeDeviceListCaps (dev, ret->names.names_val,
                               args->maxnames);
    if (ret->names.names_len == -1) {
4667
        remoteDispatchConnError(rerr, conn);
4668 4669 4670 4671 4672 4673 4674 4675
        VIR_FREE(ret->names.names_val);
        return -1;
    }

    return 0;
}


4676 4677 4678 4679
static int
remoteDispatchNodeDeviceDettach (struct qemud_server *server ATTRIBUTE_UNUSED,
                                 struct qemud_client *client ATTRIBUTE_UNUSED,
                                 virConnectPtr conn,
4680
                                 remote_message_header *hdr ATTRIBUTE_UNUSED,
4681 4682 4683 4684 4685 4686 4687 4688 4689
                                 remote_error *rerr,
                                 remote_node_device_dettach_args *args,
                                 void *ret ATTRIBUTE_UNUSED)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

    dev = virNodeDeviceLookupByName(conn, args->name);
    if (dev == NULL) {
4690
        remoteDispatchConnError(rerr, conn);
4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706
        return -1;
    }

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

    return 0;
}


static int
remoteDispatchNodeDeviceReAttach (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4707
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4708 4709 4710 4711 4712 4713 4714 4715 4716
                                  remote_error *rerr,
                                  remote_node_device_re_attach_args *args,
                                  void *ret ATTRIBUTE_UNUSED)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

    dev = virNodeDeviceLookupByName(conn, args->name);
    if (dev == NULL) {
4717
        remoteDispatchConnError(rerr, conn);
4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733
        return -1;
    }

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

    return 0;
}


static int
remoteDispatchNodeDeviceReset (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
                               virConnectPtr conn,
4734
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
4735 4736 4737 4738 4739 4740 4741 4742 4743
                               remote_error *rerr,
                               remote_node_device_reset_args *args,
                               void *ret ATTRIBUTE_UNUSED)
{
    virNodeDevicePtr dev;
    CHECK_CONN(client);

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

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

    return 0;
}


4757 4758 4759 4760
static int
remoteDispatchNodeDeviceCreateXml(struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
                                  virConnectPtr conn,
4761
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784
                                  remote_error *rerr,
                                  remote_node_device_create_xml_args *args,
                                  remote_node_device_create_xml_ret *ret)
{
    virNodeDevicePtr dev;

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

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

    return 0;
}


static int
remoteDispatchNodeDeviceDestroy(struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client ATTRIBUTE_UNUSED,
                                virConnectPtr conn,
4785
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
4786 4787 4788 4789 4790 4791 4792 4793
                                remote_error *rerr,
                                remote_node_device_destroy_args *args,
                                void *ret ATTRIBUTE_UNUSED)
{
    virNodeDevicePtr dev;

    dev = virNodeDeviceLookupByName(conn, args->name);
    if (dev == NULL) {
4794
        remoteDispatchConnError(rerr, conn);
4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806
        return -1;
    }

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

    return 0;
}


4807 4808 4809 4810 4811 4812

/***************************
 * Register / deregister events
 ***************************/
static int
remoteDispatchDomainEventsRegister (struct qemud_server *server ATTRIBUTE_UNUSED,
4813 4814
                                    struct qemud_client *client ATTRIBUTE_UNUSED,
                                    virConnectPtr conn,
4815
                                    remote_message_header *hdr ATTRIBUTE_UNUSED,
4816
                                    remote_error *rerr ATTRIBUTE_UNUSED,
4817 4818 4819 4820 4821
                                    void *args ATTRIBUTE_UNUSED,
                                    remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

4822 4823 4824 4825 4826 4827
    if (virConnectDomainEventRegister(conn,
                                      remoteRelayDomainEvent,
                                      client, NULL) < 0) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
4828

4829
    if (ret)
4830
        ret->cb_registered = 1;
4831 4832

    client->domain_events_registered = 1;
4833 4834 4835 4836 4837
    return 0;
}

static int
remoteDispatchDomainEventsDeregister (struct qemud_server *server ATTRIBUTE_UNUSED,
4838 4839
                                      struct qemud_client *client ATTRIBUTE_UNUSED,
                                      virConnectPtr conn,
4840
                                      remote_message_header *hdr ATTRIBUTE_UNUSED,
4841
                                      remote_error *rerr ATTRIBUTE_UNUSED,
4842 4843 4844 4845 4846
                                      void *args ATTRIBUTE_UNUSED,
                                      remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
{
    CHECK_CONN(client);

4847 4848 4849 4850
    if (virConnectDomainEventDeregister(conn, remoteRelayDomainEvent) < 0) {
        remoteDispatchConnError(rerr, conn);
        return -1;
    }
4851

4852
    if (ret)
4853
        ret->cb_registered = 0;
4854 4855

    client->domain_events_registered = 0;
4856 4857 4858 4859 4860
    return 0;
}

static void
remoteDispatchDomainEventSend (struct qemud_client *client,
4861
                               remote_domain_event_msg *data)
4862
{
4863
    struct qemud_client_message *msg = NULL;
4864
    XDR xdr;
4865
    unsigned int len;
4866

4867
    if (VIR_ALLOC(msg) < 0)
4868 4869
        return;

4870 4871 4872
    msg->hdr.prog = REMOTE_PROGRAM;
    msg->hdr.vers = REMOTE_PROTOCOL_VERSION;
    msg->hdr.proc = REMOTE_PROC_DOMAIN_EVENT;
4873
    msg->hdr.type = REMOTE_MESSAGE;
4874 4875
    msg->hdr.serial = 1;
    msg->hdr.status = REMOTE_OK;
4876

4877 4878
    if (remoteEncodeClientMessageHeader(msg) < 0)
        goto error;
4879

4880 4881
    /* Serialise the return header and event. */
    xdrmem_create (&xdr,
4882 4883
                   msg->buffer,
                   msg->bufferLength,
4884
                   XDR_ENCODE);
4885

4886 4887
    /* Skip over the header we just wrote */
    if (xdr_setpos (&xdr, msg->bufferOffset) == 0)
4888
        goto xdr_error;
4889

4890 4891
    if (!xdr_remote_domain_event_msg(&xdr, data))
        goto xdr_error;
4892

4893 4894
    /* Update length word to include payload*/
    len = msg->bufferOffset = xdr_getpos (&xdr);
4895 4896
    if (xdr_setpos (&xdr, 0) == 0)
        goto xdr_error;
4897

4898 4899
    if (!xdr_u_int (&xdr, &len))
        goto xdr_error;
4900 4901

    /* Send it. */
4902 4903 4904 4905
    msg->async = 1;
    msg->bufferLength = len;
    msg->bufferOffset = 0;
    qemudClientMessageQueuePush(&client->tx, msg);
4906
    qemudUpdateClientEvent(client);
4907 4908 4909 4910 4911 4912 4913 4914

    xdr_destroy (&xdr);
    return;

xdr_error:
    xdr_destroy(&xdr);
error:
    VIR_FREE(msg);
4915
}
4916

4917 4918 4919
static int
remoteDispatchNumOfSecrets (struct qemud_server *server ATTRIBUTE_UNUSED,
                            struct qemud_client *client ATTRIBUTE_UNUSED,
4920 4921 4922
                            virConnectPtr conn,
                            remote_message_header *hdr ATTRIBUTE_UNUSED,
                            remote_error *err,
4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937
                            void *args ATTRIBUTE_UNUSED,
                            remote_num_of_secrets_ret *ret)
{
    ret->num = virConnectNumOfSecrets (conn);
    if (ret->num == -1) {
        remoteDispatchConnError (err, conn);
        return -1;
    }

    return 0;
}

static int
remoteDispatchListSecrets (struct qemud_server *server ATTRIBUTE_UNUSED,
                           struct qemud_client *client ATTRIBUTE_UNUSED,
4938 4939 4940
                           virConnectPtr conn,
                           remote_message_header *hdr ATTRIBUTE_UNUSED,
                           remote_error *err,
4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968
                           remote_list_secrets_args *args,
                           remote_list_secrets_ret *ret)
{
    if (args->maxuuids > REMOTE_SECRET_UUID_LIST_MAX) {
        remoteDispatchFormatError (err, "%s",
                                   _("maxuuids > REMOTE_SECRET_UUID_LIST_MAX"));
        return -1;
    }

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

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

    return 0;
}

static int
remoteDispatchSecretDefineXml (struct qemud_server *server ATTRIBUTE_UNUSED,
                               struct qemud_client *client ATTRIBUTE_UNUSED,
4969 4970 4971
                               virConnectPtr conn,
                               remote_message_header *hdr ATTRIBUTE_UNUSED,
                               remote_error *err,
4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990
                               remote_secret_define_xml_args *args,
                               remote_secret_define_xml_ret *ret)
{
    virSecretPtr secret;

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

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

static int
remoteDispatchSecretGetValue (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
4991 4992 4993
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022
                              remote_secret_get_value_args *args,
                              remote_secret_get_value_ret *ret)
{
    virSecretPtr secret;
    size_t value_size;
    unsigned char *value;

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

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

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

static int
remoteDispatchSecretGetXmlDesc (struct qemud_server *server ATTRIBUTE_UNUSED,
                                struct qemud_client *client ATTRIBUTE_UNUSED,
5023 5024 5025
                                virConnectPtr conn,
                                remote_message_header *hdr ATTRIBUTE_UNUSED,
                                remote_error *err,
5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046
                                remote_secret_get_xml_desc_args *args,
                                remote_secret_get_xml_desc_ret *ret)
{
    virSecretPtr secret;

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

static int
5047 5048
remoteDispatchSecretLookupByUuid (struct qemud_server *server ATTRIBUTE_UNUSED,
                                  struct qemud_client *client ATTRIBUTE_UNUSED,
5049 5050 5051
                                  virConnectPtr conn,
                                  remote_message_header *hdr ATTRIBUTE_UNUSED,
                                  remote_error *err,
5052 5053
                                  remote_secret_lookup_by_uuid_args *args,
                                  remote_secret_lookup_by_uuid_ret *ret)
5054 5055 5056
{
    virSecretPtr secret;

5057
    secret = virSecretLookupByUUID (conn, (unsigned char *)args->uuid);
5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070
    if (secret == NULL) {
        remoteDispatchConnError (err, conn);
        return -1;
    }

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

static int
remoteDispatchSecretSetValue (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
5071 5072 5073
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097
                              remote_secret_set_value_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virSecretPtr secret;

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

    virSecretFree(secret);
    return 0;
}

static int
remoteDispatchSecretUndefine (struct qemud_server *server ATTRIBUTE_UNUSED,
                              struct qemud_client *client ATTRIBUTE_UNUSED,
5098 5099 5100
                              virConnectPtr conn,
                              remote_message_header *hdr ATTRIBUTE_UNUSED,
                              remote_error *err,
5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120
                              remote_secret_undefine_args *args,
                              void *ret ATTRIBUTE_UNUSED)
{
    virSecretPtr secret;

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

    virSecretFree(secret);
    return 0;
}

5121 5122 5123
static int
remoteDispatchSecretLookupByUsage (struct qemud_server *server ATTRIBUTE_UNUSED,
                                   struct qemud_client *client ATTRIBUTE_UNUSED,
5124 5125 5126
                                   virConnectPtr conn,
                                   remote_message_header *hdr ATTRIBUTE_UNUSED,
                                   remote_error *err,
5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142
                                   remote_secret_lookup_by_usage_args *args,
                                   remote_secret_lookup_by_usage_ret *ret)
{
    virSecretPtr secret;

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

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

5143

5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345
static int remoteDispatchDomainIsActive(struct qemud_server *server ATTRIBUTE_UNUSED,
                                        struct qemud_client *client ATTRIBUTE_UNUSED,
                                        virConnectPtr conn,
                                        remote_message_header *hdr ATTRIBUTE_UNUSED,
                                        remote_error *err,
                                        remote_domain_is_active_args *args,
                                        remote_domain_is_active_ret *ret)
{
    virDomainPtr domain;

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

    ret->active = virDomainIsActive(domain);

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

    return 0;
}

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

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

    ret->persistent = virDomainIsPersistent(domain);

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

    return 0;
}

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

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

    ret->active = virInterfaceIsActive(iface);

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

    return 0;
}

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

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

    ret->active = virNetworkIsActive(network);

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

    return 0;
}

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

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

    ret->persistent = virNetworkIsPersistent(network);

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

    return 0;
}

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

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

    ret->active = virStoragePoolIsActive(pool);

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

    return 0;
}

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

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

    ret->persistent = virStoragePoolIsPersistent(pool);

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

    return 0;
}


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

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

    return 0;
}


5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367
static int
remoteDispatchCpuCompare(struct qemud_server *server ATTRIBUTE_UNUSED,
                         struct qemud_client *client ATTRIBUTE_UNUSED,
                         virConnectPtr conn,
                         remote_message_header *hdr ATTRIBUTE_UNUSED,
                         remote_error *err,
                         remote_cpu_compare_args *args,
                         remote_cpu_compare_ret *ret)
{
    int result;

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

    ret->result = result;
    return 0;
}


5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393
/*----- Helpers. -----*/

/* get_nonnull_domain and get_nonnull_network turn an on-wire
 * (name, uuid) pair into virDomainPtr or virNetworkPtr object.
 * virDomainPtr or virNetworkPtr cannot be NULL.
 *
 * NB. If these return NULL then the caller must return an error.
 */
static virDomainPtr
get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain)
{
    virDomainPtr dom;
    dom = virGetDomain (conn, domain.name, BAD_CAST domain.uuid);
    /* Should we believe the domain.id sent by the client?  Maybe
     * this should be a check rather than an assignment? XXX
     */
    if (dom) dom->id = domain.id;
    return dom;
}

static virNetworkPtr
get_nonnull_network (virConnectPtr conn, remote_nonnull_network network)
{
    return virGetNetwork (conn, network.name, BAD_CAST network.uuid);
}

D
Daniel Veillard 已提交
5394
static virInterfacePtr
5395
get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
5396
{
5397
    return virGetInterface (conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
5398 5399
}

5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413
static virStoragePoolPtr
get_nonnull_storage_pool (virConnectPtr conn, remote_nonnull_storage_pool pool)
{
    return virGetStoragePool (conn, pool.name, BAD_CAST pool.uuid);
}

static virStorageVolPtr
get_nonnull_storage_vol (virConnectPtr conn, remote_nonnull_storage_vol vol)
{
    virStorageVolPtr ret;
    ret = virGetStorageVol (conn, vol.pool, vol.name, vol.key);
    return ret;
}

5414 5415 5416
static virSecretPtr
get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret)
{
5417
    return virGetSecret (conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
5418 5419
}

5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
make_nonnull_domain (remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
{
    dom_dst->id = dom_src->id;
    dom_dst->name = strdup (dom_src->name);
    memcpy (dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
}

static void
make_nonnull_network (remote_nonnull_network *net_dst, virNetworkPtr net_src)
{
    net_dst->name = strdup (net_src->name);
    memcpy (net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
}

D
Daniel Veillard 已提交
5436 5437 5438 5439 5440 5441 5442 5443
static void
make_nonnull_interface (remote_nonnull_interface *interface_dst,
                        virInterfacePtr interface_src)
{
    interface_dst->name = strdup (interface_src->name);
    interface_dst->mac = strdup (interface_src->mac);
}

5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457
static void
make_nonnull_storage_pool (remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
{
    pool_dst->name = strdup (pool_src->name);
    memcpy (pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
}

static void
make_nonnull_storage_vol (remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
{
    vol_dst->pool = strdup (vol_src->pool);
    vol_dst->name = strdup (vol_src->name);
    vol_dst->key = strdup (vol_src->key);
}
5458 5459 5460 5461 5462 5463

static void
make_nonnull_node_device (remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
{
    dev_dst->name = strdup(dev_src->name);
}
5464 5465 5466 5467

static void
make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
{
5468
    memcpy (secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
5469 5470
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = strdup (secret_src->usageID);
5471
}