remote.c 195.7 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24
 *
 * Author: Richard W.M. Jones <rjones@redhat.com>
 */

#include <config.h>

25
#include "virerror.h"
26

27
#if WITH_POLKIT0
28 29
# include <polkit/polkit.h>
# include <polkit-dbus/polkit-dbus.h>
30 31
#endif

32
#include "remote.h"
33
#include "libvirtd.h"
34 35
#include "libvirt_internal.h"
#include "datatypes.h"
36
#include "viralloc.h"
37
#include "virlog.h"
C
Chris Lalancette 已提交
38
#include "stream.h"
39
#include "viruuid.h"
40
#include "vircommand.h"
41
#include "intprops.h"
42
#include "virnetserverservice.h"
J
Jim Fehlig 已提交
43
#include "virnetserver.h"
44
#include "virfile.h"
45
#include "virtypedparam.h"
46
#include "virdbus.h"
47
#include "virprocess.h"
48 49
#include "remote_protocol.h"
#include "qemu_protocol.h"
50
#include "lxc_protocol.h"
51
#include "virstring.h"
52
#include "object_event.h"
53 54
#include "domain_conf.h"
#include "network_conf.h"
55
#include "virprobe.h"
56
#include "viraccessapicheck.h"
57 58

#define VIR_FROM_THIS VIR_FROM_RPC
59

60
#if SIZEOF_LONG < 8
E
Eric Blake 已提交
61 62 63
# define HYPER_TO_TYPE(_type, _to, _from)                               \
    do {                                                                \
        if ((_from) != (_type)(_from)) {                                \
64 65 66
            virReportError(VIR_ERR_OVERFLOW,                            \
                           _("conversion from hyper to %s overflowed"), \
                           #_type);                                     \
E
Eric Blake 已提交
67 68 69
            goto cleanup;                                               \
        }                                                               \
        (_to) = (_from);                                                \
70 71 72 73 74 75 76 77 78
    } while (0)

# define HYPER_TO_LONG(_to, _from) HYPER_TO_TYPE(long, _to, _from)
# define HYPER_TO_ULONG(_to, _from) HYPER_TO_TYPE(unsigned long, _to, _from)
#else
# define HYPER_TO_LONG(_to, _from) (_to) = (_from)
# define HYPER_TO_ULONG(_to, _from) (_to) = (_from)
#endif

79 80 81 82
struct daemonClientEventCallback {
    virNetServerClientPtr client;
    int eventID;
    int callbackID;
83
    bool legacy;
84 85
};

86 87 88 89 90 91 92
static virDomainPtr get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network(virConnectPtr conn, remote_nonnull_network network);
static virInterfacePtr get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface);
static virStoragePoolPtr get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool);
static virStorageVolPtr get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol);
static virSecretPtr get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret);
static virNWFilterPtr get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter);
93
static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot);
94 95 96 97 98 99 100 101 102
static void make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src);
static void make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src);
static void make_nonnull_interface(remote_nonnull_interface *interface_dst, virInterfacePtr interface_src);
static void make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src);
static void make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src);
static void make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src);
static void make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src);
static void make_nonnull_nwfilter(remote_nonnull_nwfilter *net_dst, virNWFilterPtr nwfilter_src);
static void make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src);
103

104 105 106 107 108
static virTypedParameterPtr
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
                                 int limit,
                                 int *nparams);
109

110 111 112 113 114 115
static int
remoteSerializeDomainDiskErrors(virDomainDiskErrorPtr errors,
                                int nerrors,
                                remote_domain_disk_error **ret_errors_val,
                                u_int *ret_errors_len);

116 117
#include "remote_dispatch.h"
#include "qemu_dispatch.h"
118
#include "lxc_dispatch.h"
C
Chris Lalancette 已提交
119 120


121 122
/* Prototypes */
static void
123
remoteDispatchObjectEventSend(virNetServerClientPtr client,
124
                              virNetServerProgramPtr program,
125 126 127
                              int procnr,
                              xdrproc_t proc,
                              void *data);
128

129 130 131 132 133 134
static void
remoteEventCallbackFree(void *opaque)
{
    VIR_FREE(opaque);
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

static bool
remoteRelayDomainEventCheckACL(virNetServerClientPtr client,
                               virConnectPtr conn, virDomainPtr dom)
{
    virDomainDef def;
    virIdentityPtr identity = NULL;
    bool ret = false;

    /* For now, we just create a virDomainDef with enough contents to
     * satisfy what viraccessdriverpolkit.c references.  This is a bit
     * fragile, but I don't know of anything better.  */
    def.name = dom->name;
    memcpy(def.uuid, dom->uuid, VIR_UUID_BUFLEN);

    if (!(identity = virNetServerClientGetIdentity(client)))
        goto cleanup;
    if (virIdentitySetCurrent(identity) < 0)
        goto cleanup;
    ret = virConnectDomainEventRegisterAnyCheckACL(conn, &def);

cleanup:
    ignore_value(virIdentitySetCurrent(NULL));
    virObjectUnref(identity);
    return ret;
}


static bool
remoteRelayNetworkEventCheckACL(virNetServerClientPtr client,
                                virConnectPtr conn, virNetworkPtr net)
{
    virNetworkDef def;
    virIdentityPtr identity = NULL;
    bool ret = false;

    /* For now, we just create a virNetworkDef with enough contents to
     * satisfy what viraccessdriverpolkit.c references.  This is a bit
     * fragile, but I don't know of anything better.  */
    def.name = net->name;
    memcpy(def.uuid, net->uuid, VIR_UUID_BUFLEN);

    if (!(identity = virNetServerClientGetIdentity(client)))
        goto cleanup;
    if (virIdentitySetCurrent(identity) < 0)
        goto cleanup;
    ret = virConnectNetworkEventRegisterAnyCheckACL(conn, &def);

cleanup:
    ignore_value(virIdentitySetCurrent(NULL));
    virObjectUnref(identity);
    return ret;
}


static int
remoteRelayDomainEventLifecycle(virConnectPtr conn,
                                virDomainPtr dom,
                                int event,
                                int detail,
                                void *opaque)
196
{
197
    daemonClientEventCallbackPtr callback = opaque;
198
    remote_domain_event_lifecycle_msg data;
199

200 201
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
202 203
        return -1;

204 205
    VIR_DEBUG("Relaying domain lifecycle event %d %d, callback %d legacy %d",
              event, detail, callback->callbackID, callback->legacy);
206

207
    /* build return data */
208
    memset(&data, 0, sizeof(data));
209
    make_nonnull_domain(&data.dom, dom);
210 211
    data.event = event;
    data.detail = detail;
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
                                      (xdrproc_t)xdr_remote_domain_event_lifecycle_msg,
                                      &data);
    } else {
        remote_domain_event_callback_lifecycle_msg msg = { callback->callbackID,
                                                           data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_LIFECYCLE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_lifecycle_msg,
                                      &msg);
    }
227

228 229
    return 0;
}
230

231 232 233 234
static int
remoteRelayDomainEventReboot(virConnectPtr conn,
                             virDomainPtr dom,
                             void *opaque)
235
{
236
    daemonClientEventCallbackPtr callback = opaque;
237 238
    remote_domain_event_reboot_msg data;

239 240
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
241 242
        return -1;

243 244
    VIR_DEBUG("Relaying domain reboot event %s %d, callback %d legacy %d",
              dom->name, dom->id, callback->callbackID, callback->legacy);
245 246

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

250 251 252 253 254 255 256 257 258 259 260 261
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_REBOOT,
                                      (xdrproc_t)xdr_remote_domain_event_reboot_msg, &data);
    } else {
        remote_domain_event_callback_reboot_msg msg = { callback->callbackID,
                                                        data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_REBOOT,
                                      (xdrproc_t)xdr_remote_domain_event_callback_reboot_msg, &msg);
    }
262 263 264 265

    return 0;
}

266

267 268 269 270 271
static int
remoteRelayDomainEventRTCChange(virConnectPtr conn,
                                virDomainPtr dom,
                                long long offset,
                                void *opaque)
272
{
273
    daemonClientEventCallbackPtr callback = opaque;
274 275
    remote_domain_event_rtc_change_msg data;

276 277
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
278 279
        return -1;

280 281 282
    VIR_DEBUG("Relaying domain rtc change event %s %d %lld, callback %d legacy %d",
              dom->name, dom->id, offset,
              callback->callbackID, callback->legacy);
283 284

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

289 290 291 292 293 294 295 296 297 298 299 300
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_rtc_change_msg, &data);
    } else {
        remote_domain_event_callback_rtc_change_msg msg = { callback->callbackID,
                                                            data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_RTC_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_rtc_change_msg, &msg);
    }
301 302 303 304 305

    return 0;
}


306 307 308 309 310
static int
remoteRelayDomainEventWatchdog(virConnectPtr conn,
                               virDomainPtr dom,
                               int action,
                               void *opaque)
311
{
312
    daemonClientEventCallbackPtr callback = opaque;
313 314
    remote_domain_event_watchdog_msg data;

315 316
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
317 318
        return -1;

319 320
    VIR_DEBUG("Relaying domain watchdog event %s %d %d, callback %d",
              dom->name, dom->id, action, callback->callbackID);
321 322

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

327 328 329 330 331 332 333 334 335 336 337 338
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_WATCHDOG,
                                      (xdrproc_t)xdr_remote_domain_event_watchdog_msg, &data);
    } else {
        remote_domain_event_callback_watchdog_msg msg = { callback->callbackID,
                                                          data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_WATCHDOG,
                                      (xdrproc_t)xdr_remote_domain_event_callback_watchdog_msg, &msg);
    }
339 340 341 342 343

    return 0;
}


344 345 346 347 348 349 350
static int
remoteRelayDomainEventIOError(virConnectPtr conn,
                              virDomainPtr dom,
                              const char *srcPath,
                              const char *devAlias,
                              int action,
                              void *opaque)
351
{
352
    daemonClientEventCallbackPtr callback = opaque;
353 354
    remote_domain_event_io_error_msg data;

355 356
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
357 358
        return -1;

359 360 361
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d, callback %d",
              dom->name, dom->id, srcPath, devAlias, action,
              callback->callbackID);
362 363

    /* build return data */
364
    memset(&data, 0, sizeof(data));
365 366 367
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
368
    make_nonnull_domain(&data.dom, dom);
369 370
    data.action = action;

371 372 373 374 375 376 377 378 379 380 381 382
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_IO_ERROR,
                                      (xdrproc_t)xdr_remote_domain_event_io_error_msg, &data);
    } else {
        remote_domain_event_callback_io_error_msg msg = { callback->callbackID,
                                                          data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_IO_ERROR,
                                      (xdrproc_t)xdr_remote_domain_event_callback_io_error_msg, &msg);
    }
383 384

    return 0;
385
error:
E
Eric Blake 已提交
386 387
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
388
    return -1;
389 390 391
}


392 393 394 395 396 397 398 399
static int
remoteRelayDomainEventIOErrorReason(virConnectPtr conn,
                                    virDomainPtr dom,
                                    const char *srcPath,
                                    const char *devAlias,
                                    int action,
                                    const char *reason,
                                    void *opaque)
400
{
401
    daemonClientEventCallbackPtr callback = opaque;
402 403
    remote_domain_event_io_error_reason_msg data;

404 405
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
406 407
        return -1;

408 409 410
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s, callback %d",
              dom->name, dom->id, srcPath, devAlias, action, reason,
              callback->callbackID);
411 412

    /* build return data */
413
    memset(&data, 0, sizeof(data));
414 415 416 417
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0 ||
        VIR_STRDUP(data.reason, reason) < 0)
        goto error;
418
    data.action = action;
419 420

    make_nonnull_domain(&data.dom, dom);
421

422 423 424 425 426 427 428 429 430 431 432 433
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON,
                                      (xdrproc_t)xdr_remote_domain_event_io_error_reason_msg, &data);
    } else {
        remote_domain_event_callback_io_error_reason_msg msg = { callback->callbackID,
                                                                 data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_IO_ERROR_REASON,
                                      (xdrproc_t)xdr_remote_domain_event_callback_io_error_reason_msg, &msg);
    }
434 435

    return 0;
436

437
error:
E
Eric Blake 已提交
438 439 440
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
    VIR_FREE(data.reason);
441
    return -1;
442 443 444
}


445 446 447 448 449 450 451 452 453
static int
remoteRelayDomainEventGraphics(virConnectPtr conn,
                               virDomainPtr dom,
                               int phase,
                               virDomainEventGraphicsAddressPtr local,
                               virDomainEventGraphicsAddressPtr remote,
                               const char *authScheme,
                               virDomainEventGraphicsSubjectPtr subject,
                               void *opaque)
454
{
455
    daemonClientEventCallbackPtr callback = opaque;
456
    remote_domain_event_graphics_msg data;
457
    size_t i;
458

459 460
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
461 462
        return -1;

463 464
    VIR_DEBUG("Relaying domain graphics event %s %d %d - %d %s %s  - %d %s %s - %s, callback %d",
              dom->name, dom->id, phase,
465 466
              local->family, local->service, local->node,
              remote->family, remote->service, remote->node,
467
              authScheme, callback->callbackID);
468

469
    VIR_DEBUG("Subject %d", subject->nidentity);
470
    for (i = 0; i < subject->nidentity; i++) {
471
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
472 473 474
    }

    /* build return data */
475
    memset(&data, 0, sizeof(data));
476 477 478
    data.phase = phase;
    data.local.family = local->family;
    data.remote.family = remote->family;
479 480 481 482 483 484
    if (VIR_STRDUP(data.authScheme, authScheme) < 0 ||
        VIR_STRDUP(data.local.node, local->node) < 0 ||
        VIR_STRDUP(data.local.service, local->service) < 0 ||
        VIR_STRDUP(data.remote.node, remote->node) < 0 ||
        VIR_STRDUP(data.remote.service, remote->service) < 0)
        goto error;
485 486

    data.subject.subject_len = subject->nidentity;
487
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0)
488
        goto error;
489

490
    for (i = 0; i < data.subject.subject_len; i++) {
491 492 493
        if (VIR_STRDUP(data.subject.subject_val[i].type, subject->identities[i].type) < 0 ||
            VIR_STRDUP(data.subject.subject_val[i].name, subject->identities[i].name) < 0)
            goto error;
494
    }
495
    make_nonnull_domain(&data.dom, dom);
496

497 498 499 500 501 502 503 504 505 506 507 508
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
                                      (xdrproc_t)xdr_remote_domain_event_graphics_msg, &data);
    } else {
        remote_domain_event_callback_graphics_msg msg = { callback->callbackID,
                                                          data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_GRAPHICS,
                                      (xdrproc_t)xdr_remote_domain_event_callback_graphics_msg, &msg);
    }
509 510

    return 0;
511

512
error:
E
Eric Blake 已提交
513 514 515 516 517
    VIR_FREE(data.authScheme);
    VIR_FREE(data.local.node);
    VIR_FREE(data.local.service);
    VIR_FREE(data.remote.node);
    VIR_FREE(data.remote.service);
518
    if (data.subject.subject_val != NULL) {
519
        for (i = 0; i < data.subject.subject_len; i++) {
E
Eric Blake 已提交
520 521
            VIR_FREE(data.subject.subject_val[i].type);
            VIR_FREE(data.subject.subject_val[i].name);
522
        }
E
Eric Blake 已提交
523
        VIR_FREE(data.subject.subject_val);
524 525
    }
    return -1;
526 527
}

528 529 530 531 532 533 534
static int
remoteRelayDomainEventBlockJob(virConnectPtr conn,
                               virDomainPtr dom,
                               const char *path,
                               int type,
                               int status,
                               void *opaque)
535
{
536
    daemonClientEventCallbackPtr callback = opaque;
537 538
    remote_domain_event_block_job_msg data;

539 540
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
541 542
        return -1;

543 544
    VIR_DEBUG("Relaying domain block job event %s %d %s %i, %i, callback %d",
              dom->name, dom->id, path, type, status, callback->callbackID);
545 546

    /* build return data */
547
    memset(&data, 0, sizeof(data));
548 549
    if (VIR_STRDUP(data.path, path) < 0)
        goto error;
550 551
    data.type = type;
    data.status = status;
552
    make_nonnull_domain(&data.dom, dom);
553

554 555 556 557 558 559 560 561 562 563 564 565
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB,
                                      (xdrproc_t)xdr_remote_domain_event_block_job_msg, &data);
    } else {
        remote_domain_event_callback_block_job_msg msg = { callback->callbackID,
                                                           data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_BLOCK_JOB,
                                      (xdrproc_t)xdr_remote_domain_event_callback_block_job_msg, &msg);
    }
566 567

    return 0;
568
error:
E
Eric Blake 已提交
569
    VIR_FREE(data.path);
570
    return -1;
571 572
}

573

574 575 576 577
static int
remoteRelayDomainEventControlError(virConnectPtr conn,
                                   virDomainPtr dom,
                                   void *opaque)
578
{
579
    daemonClientEventCallbackPtr callback = opaque;
580 581
    remote_domain_event_control_error_msg data;

582 583
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
584 585
        return -1;

586 587
    VIR_DEBUG("Relaying domain control error %s %d, callback %d",
              dom->name, dom->id, callback->callbackID);
588 589

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

593 594 595 596 597 598 599 600 601 602 603 604
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CONTROL_ERROR,
                                      (xdrproc_t)xdr_remote_domain_event_control_error_msg, &data);
    } else {
        remote_domain_event_callback_control_error_msg msg = { callback->callbackID,
                                                               data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_CONTROL_ERROR,
                                      (xdrproc_t)xdr_remote_domain_event_callback_control_error_msg, &msg);
    }
605 606 607 608 609

    return 0;
}


610 611 612 613 614 615 616 617
static int
remoteRelayDomainEventDiskChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *oldSrcPath,
                                 const char *newSrcPath,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
618
{
619
    daemonClientEventCallbackPtr callback = opaque;
620 621 622
    remote_domain_event_disk_change_msg data;
    char **oldSrcPath_p = NULL, **newSrcPath_p = NULL;

623 624
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
625 626
        return -1;

627 628 629
    VIR_DEBUG("Relaying domain %s %d disk change %s %s %s %d, callback %d",
              dom->name, dom->id, oldSrcPath, newSrcPath, devAlias, reason,
              callback->callbackID);
630 631

    /* build return data */
632
    memset(&data, 0, sizeof(data));
633 634
    if (oldSrcPath &&
        ((VIR_ALLOC(oldSrcPath_p) < 0) ||
635
         VIR_STRDUP(*oldSrcPath_p, oldSrcPath) < 0))
636
        goto error;
637 638 639

    if (newSrcPath &&
        ((VIR_ALLOC(newSrcPath_p) < 0) ||
640
         VIR_STRDUP(*newSrcPath_p, newSrcPath) < 0))
641
        goto error;
642 643 644

    data.oldSrcPath = oldSrcPath_p;
    data.newSrcPath = newSrcPath_p;
645 646
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
647 648 649 650
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

651 652 653 654 655 656 657 658 659 660 661 662
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_DISK_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_disk_change_msg, &data);
    } else {
        remote_domain_event_callback_disk_change_msg msg = { callback->callbackID,
                                                             data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DISK_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_disk_change_msg, &msg);
    }
663 664 665

    return 0;

666
error:
M
Michal Privoznik 已提交
667 668
    VIR_FREE(oldSrcPath_p);
    VIR_FREE(newSrcPath_p);
669 670 671 672
    return -1;
}


673 674 675 676 677 678 679
static int
remoteRelayDomainEventTrayChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
{
680
    daemonClientEventCallbackPtr callback = opaque;
681 682
    remote_domain_event_tray_change_msg data;

683 684
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
685 686
        return -1;

687 688
    VIR_DEBUG("Relaying domain %s %d tray change devAlias: %s reason: %d, callback %d",
              dom->name, dom->id, devAlias, reason, callback->callbackID);
689 690

    /* build return data */
691
    memset(&data, 0, sizeof(data));
692

693
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
694 695 696 697 698
        return -1;
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

699 700 701 702 703 704 705 706 707 708 709 710
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_TRAY_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_tray_change_msg, &data);
    } else {
        remote_domain_event_callback_tray_change_msg msg = { callback->callbackID,
                                                             data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_TRAY_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_tray_change_msg, &msg);
    }
711 712 713 714

    return 0;
}

715 716 717
static int
remoteRelayDomainEventPMWakeup(virConnectPtr conn,
                               virDomainPtr dom,
E
Eric Blake 已提交
718
                               int reason,
719 720
                               void *opaque)
{
721
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
722 723
    remote_domain_event_pmwakeup_msg data;

724 725
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
726 727
        return -1;

728 729
    VIR_DEBUG("Relaying domain %s %d system pmwakeup, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
730 731

    /* build return data */
732
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
733 734
    make_nonnull_domain(&data.dom, dom);

735 736 737 738 739 740
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_PMWAKEUP,
                                      (xdrproc_t)xdr_remote_domain_event_pmwakeup_msg, &data);
    } else {
        remote_domain_event_callback_pmwakeup_msg msg = { callback->callbackID,
E
Eric Blake 已提交
741
                                                          reason, data };
742 743 744 745 746

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMWAKEUP,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmwakeup_msg, &msg);
    }
O
Osier Yang 已提交
747 748 749 750

    return 0;
}

751 752 753
static int
remoteRelayDomainEventPMSuspend(virConnectPtr conn,
                                virDomainPtr dom,
E
Eric Blake 已提交
754
                                int reason,
755 756
                                void *opaque)
{
757
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
758 759
    remote_domain_event_pmsuspend_msg data;

760 761
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
762 763
        return -1;

764 765
    VIR_DEBUG("Relaying domain %s %d system pmsuspend, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
766 767

    /* build return data */
768
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
769 770
    make_nonnull_domain(&data.dom, dom);

771 772 773 774 775 776
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND,
                                      (xdrproc_t)xdr_remote_domain_event_pmsuspend_msg, &data);
    } else {
        remote_domain_event_callback_pmsuspend_msg msg = { callback->callbackID,
E
Eric Blake 已提交
777
                                                           reason, data };
778 779 780 781 782

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_msg, &msg);
    }
O
Osier Yang 已提交
783 784 785 786

    return 0;
}

787
static int
788
remoteRelayDomainEventBalloonChange(virConnectPtr conn,
789 790 791 792
                                    virDomainPtr dom,
                                    unsigned long long actual,
                                    void *opaque)
{
793
    daemonClientEventCallbackPtr callback = opaque;
794 795
    remote_domain_event_balloon_change_msg data;

796 797
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
798 799
        return -1;

800 801
    VIR_DEBUG("Relaying domain balloon change event %s %d %lld, callback %d",
              dom->name, dom->id, actual, callback->callbackID);
802 803 804 805 806 807

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

808 809 810 811 812 813 814 815 816 817 818 819
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_balloon_change_msg, &data);
    } else {
        remote_domain_event_callback_balloon_change_msg msg = { callback->callbackID,
                                                                data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_BALLOON_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_balloon_change_msg, &msg);
    }
820 821 822 823 824

    return 0;
}


825 826 827
static int
remoteRelayDomainEventPMSuspendDisk(virConnectPtr conn,
                                    virDomainPtr dom,
E
Eric Blake 已提交
828
                                    int reason,
829 830
                                    void *opaque)
{
831
    daemonClientEventCallbackPtr callback = opaque;
832 833
    remote_domain_event_pmsuspend_disk_msg data;

834 835
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
836 837
        return -1;

838 839
    VIR_DEBUG("Relaying domain %s %d system pmsuspend-disk, callback %d",
              dom->name, dom->id, callback->callbackID);
840 841 842 843 844

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

845 846 847 848 849 850
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND_DISK,
                                      (xdrproc_t)xdr_remote_domain_event_pmsuspend_disk_msg, &data);
    } else {
        remote_domain_event_callback_pmsuspend_disk_msg msg = { callback->callbackID,
E
Eric Blake 已提交
851
                                                                reason, data };
852 853 854 855 856

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND_DISK,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_disk_msg, &msg);
    }
857 858 859 860

    return 0;
}

861
static int
862
remoteRelayDomainEventDeviceRemoved(virConnectPtr conn,
863 864 865 866
                                    virDomainPtr dom,
                                    const char *devAlias,
                                    void *opaque)
{
867
    daemonClientEventCallbackPtr callback = opaque;
868 869
    remote_domain_event_device_removed_msg data;

870 871
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
872 873
        return -1;

874 875
    VIR_DEBUG("Relaying domain device removed event %s %d %s, callback %d",
              dom->name, dom->id, devAlias, callback->callbackID);
876 877 878 879 880 881 882 883 884

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

    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
        return -1;

    make_nonnull_domain(&data.dom, dom);

885 886 887 888 889 890 891 892 893 894 895 896 897 898
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_DEVICE_REMOVED,
                                      (xdrproc_t)xdr_remote_domain_event_device_removed_msg,
                                      &data);
    } else {
        remote_domain_event_callback_device_removed_msg msg = { callback->callbackID,
                                                                data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DEVICE_REMOVED,
                                      (xdrproc_t)xdr_remote_domain_event_callback_device_removed_msg,
                                      &msg);
    }
899 900 901 902

    return 0;
}

903

904
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
905
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
906
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
907
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
908
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
909
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
910
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
911
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
912
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
913
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
914
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
915
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
916
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
917
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
918
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
919
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
920
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
921 922 923 924
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

925
static int
926
remoteRelayNetworkEventLifecycle(virConnectPtr conn,
927 928 929 930
                                 virNetworkPtr net,
                                 int event,
                                 int detail,
                                 void *opaque)
931
{
932
    daemonClientEventCallbackPtr callback = opaque;
933 934
    remote_network_event_lifecycle_msg data;

935 936
    if (callback->callbackID < 0 ||
        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
937 938
        return -1;

939 940
    VIR_DEBUG("Relaying network lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);
941 942 943 944

    /* build return data */
    memset(&data, 0, sizeof(data));
    make_nonnull_network(&data.net, net);
945
    data.callbackID = callback->callbackID;
946 947 948
    data.event = event;
    data.detail = detail;

949
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
950 951 952 953 954 955 956 957 958 959 960 961
                                  REMOTE_PROC_NETWORK_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_network_event_lifecycle_msg, &data);

    return 0;
}

static virConnectNetworkEventGenericCallback networkEventCallbacks[] = {
    VIR_NETWORK_EVENT_CALLBACK(remoteRelayNetworkEventLifecycle),
};

verify(ARRAY_CARDINALITY(networkEventCallbacks) == VIR_NETWORK_EVENT_ID_LAST);

962 963 964 965 966 967 968
/*
 * You must hold lock for at least the client
 * We don't free stuff here, merely disconnect the client's
 * network socket & resources.
 * We keep the libvirt connection open until any async
 * jobs have finished, then clean it up elsewhere
 */
969
void remoteClientFreeFunc(void *data)
970 971 972 973 974
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
975
        virIdentityPtr sysident = virIdentityGetSystem();
976
        size_t i;
977

978 979
        virIdentitySetCurrent(sysident);

980 981 982 983 984
        for (i = 0; i < priv->ndomainEventCallbacks; i++) {
            int callbackID = priv->domainEventCallbacks[i]->callbackID;
            if (callbackID < 0) {
                VIR_WARN("unexpected incomplete domain callback %zu", i);
                continue;
985
            }
986 987 988 989 990
            VIR_DEBUG("Deregistering remote domain event relay %d",
                      callbackID);
            priv->domainEventCallbacks[i]->callbackID = -1;
            if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
                VIR_WARN("unexpected domain event deregister failure");
991
        }
992
        VIR_FREE(priv->domainEventCallbacks);
993

994 995 996 997 998
        for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
            int callbackID = priv->networkEventCallbacks[i]->callbackID;
            if (callbackID < 0) {
                VIR_WARN("unexpected incomplete network callback %zu", i);
                continue;
999
            }
1000 1001 1002 1003 1004 1005
            VIR_DEBUG("Deregistering remote network event relay %d",
                      callbackID);
            priv->networkEventCallbacks[i]->callbackID = -1;
            if (virConnectNetworkEventDeregisterAny(priv->conn,
                                                    callbackID) < 0)
                VIR_WARN("unexpected network event deregister failure");
1006
        }
1007
        VIR_FREE(priv->networkEventCallbacks);
1008

1009
        virConnectClose(priv->conn);
1010 1011 1012

        virIdentitySetCurrent(NULL);
        virObjectUnref(sysident);
1013 1014 1015 1016 1017 1018
    }

    VIR_FREE(priv);
}


1019 1020 1021 1022 1023 1024 1025
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

1026

1027 1028
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
1029 1030 1031
{
    struct daemonClientPrivate *priv;

1032
    if (VIR_ALLOC(priv) < 0)
1033
        return NULL;
1034 1035 1036

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
1037
        virReportSystemError(errno, "%s", _("unable to init mutex"));
1038
        return NULL;
1039 1040
    }

1041
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
1042
    return priv;
1043 1044
}

1045 1046 1047
/*----- Functions. -----*/

static int
1048 1049 1050 1051 1052
remoteDispatchConnectOpen(virNetServerPtr server,
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
1053 1054
{
    const char *name;
1055
    unsigned int flags;
1056
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
1057
    int rv = -1;
1058

1059 1060 1061 1062
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
1063
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
1064 1065 1066
        goto cleanup;
    }

1067
    if (virNetServerKeepAliveRequired(server) && !priv->keepalive_supported) {
1068 1069
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("keepalive support is required to connect"));
1070 1071 1072
        goto cleanup;
    }

1073 1074 1075 1076 1077 1078
    name = args->name ? *args->name : NULL;

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

1082
    priv->conn =
1083
        flags & VIR_CONNECT_RO
1084 1085
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
1086

1087
    if (priv->conn == NULL)
1088 1089 1090
        goto cleanup;

    rv = 0;
1091

1092 1093
cleanup:
    if (rv < 0)
1094 1095
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
1096
    return rv;
1097 1098 1099 1100
}


static int
1101 1102 1103 1104
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
1105
{
1106
    virNetServerClientDelayedClose(client);
1107
    return 0;
1108 1109
}

1110

1111
static int
1112 1113
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1114
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1115
                                     virNetMessageErrorPtr rerr,
1116 1117
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
1118
{
1119
    virDomainPtr dom = NULL;
1120 1121
    char *type;
    int nparams;
1122
    int rv = -1;
1123 1124
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1125

1126
    if (!priv->conn) {
1127
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1128
        goto cleanup;
1129 1130
    }

1131
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1132
        goto cleanup;
1133

1134
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
1135
        goto cleanup;
1136 1137 1138

    ret->type = type;
    ret->nparams = nparams;
1139 1140 1141 1142
    rv = 0;

cleanup:
    if (rv < 0)
1143
        virNetMessageSaveError(rerr);
1144 1145 1146
    if (dom)
        virDomainFree(dom);
    return rv;
1147 1148
}

1149 1150
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
1151 1152 1153
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
1154
                               remote_typed_param **ret_params_val,
1155 1156
                               u_int *ret_params_len,
                               unsigned int flags)
1157
{
1158 1159
    size_t i;
    size_t j;
1160 1161 1162 1163
    int rv = -1;
    remote_typed_param *val;

    *ret_params_len = nparams;
1164
    if (VIR_ALLOC_N(val, nparams) < 0)
1165 1166
        goto cleanup;

1167
    for (i = 0, j = 0; i < nparams; ++i) {
1168 1169 1170 1171 1172
        /* virDomainGetCPUStats can return a sparse array; also, we
         * can't pass back strings to older clients.  */
        if (!params[i].type ||
            (!(flags & VIR_TYPED_PARAM_STRING_OKAY) &&
             params[i].type == VIR_TYPED_PARAM_STRING)) {
1173 1174 1175 1176
            --*ret_params_len;
            continue;
        }

1177
        /* remoteDispatchClientRequest will free this: */
1178
        if (VIR_STRDUP(val[j].field, params[i].field) < 0)
1179
            goto cleanup;
1180
        val[j].value.type = params[i].type;
1181
        switch (params[i].type) {
1182
        case VIR_TYPED_PARAM_INT:
1183
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
1184 1185
            break;
        case VIR_TYPED_PARAM_UINT:
1186
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
1187 1188
            break;
        case VIR_TYPED_PARAM_LLONG:
1189
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
1190 1191
            break;
        case VIR_TYPED_PARAM_ULLONG:
1192
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
1193 1194
            break;
        case VIR_TYPED_PARAM_DOUBLE:
1195
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
1196 1197
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
1198 1199 1200
            val[j].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
        case VIR_TYPED_PARAM_STRING:
1201
            if (VIR_STRDUP(val[j].value.remote_typed_param_value_u.s, params[i].value.s) < 0)
1202
                goto cleanup;
1203 1204
            break;
        default:
1205 1206
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
1207 1208
            goto cleanup;
        }
1209
        j++;
1210 1211 1212 1213 1214 1215 1216 1217
    }

    *ret_params_val = val;
    val = NULL;
    rv = 0;

cleanup:
    if (val) {
1218
        for (i = 0; i < nparams; i++) {
1219
            VIR_FREE(val[i].field);
1220
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
1221 1222
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
1223 1224 1225 1226 1227 1228 1229
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
1230 1231
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
1232 1233 1234
                                 int limit,
                                 int *nparams)
{
1235
    size_t i = 0;
1236 1237 1238 1239
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
1240
    if (limit && args_params_len > limit) {
1241
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1242 1243
        goto cleanup;
    }
1244
    if (VIR_ALLOC_N(params, args_params_len) < 0)
1245 1246 1247 1248 1249 1250 1251 1252
        goto cleanup;

    *nparams = args_params_len;

    /* Deserialise the result. */
    for (i = 0; i < args_params_len; ++i) {
        if (virStrcpyStatic(params[i].field,
                            args_params_val[i].field) == NULL) {
1253 1254 1255
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Parameter %s too big for destination"),
                           args_params_val[i].field);
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
            goto cleanup;
        }
        params[i].type = args_params_val[i].value.type;
        switch (params[i].type) {
        case VIR_TYPED_PARAM_INT:
            params[i].value.i =
                args_params_val[i].value.remote_typed_param_value_u.i;
            break;
        case VIR_TYPED_PARAM_UINT:
            params[i].value.ui =
                args_params_val[i].value.remote_typed_param_value_u.ui;
            break;
        case VIR_TYPED_PARAM_LLONG:
            params[i].value.l =
                args_params_val[i].value.remote_typed_param_value_u.l;
            break;
        case VIR_TYPED_PARAM_ULLONG:
            params[i].value.ul =
                args_params_val[i].value.remote_typed_param_value_u.ul;
            break;
        case VIR_TYPED_PARAM_DOUBLE:
            params[i].value.d =
                args_params_val[i].value.remote_typed_param_value_u.d;
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
            params[i].value.b =
                args_params_val[i].value.remote_typed_param_value_u.b;
            break;
1284
        case VIR_TYPED_PARAM_STRING:
1285 1286
            if (VIR_STRDUP(params[i].value.s,
                           args_params_val[i].value.remote_typed_param_value_u.s) < 0)
1287 1288
                goto cleanup;
            break;
1289
        default:
1290 1291
            virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                           params[i].type);
1292 1293 1294 1295 1296 1297 1298
            goto cleanup;
        }
    }

    rv = 0;

cleanup:
1299
    if (rv < 0) {
1300 1301
        virTypedParamsFree(params, i);
        params = NULL;
1302
    }
1303 1304 1305
    return params;
}

1306
static int
1307 1308
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
1309
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
1310
                                           virNetMessageErrorPtr rerr,
1311 1312
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
1313
{
1314
    virDomainPtr dom = NULL;
1315
    virTypedParameterPtr params = NULL;
1316
    int nparams = 0;
1317
    int rv = -1;
1318 1319
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1320

1321
    if (!priv->conn) {
1322
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1323
        goto cleanup;
1324 1325
    }

1326
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1327
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1328
        goto cleanup;
1329
    }
1330
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1331
        goto cleanup;
1332
    nparams = args->nparams;
1333

1334
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1335
        goto cleanup;
1336

1337
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1338
        goto cleanup;
1339

1340
    if (remoteSerializeTypedParameters(params, nparams,
1341
                                       &ret->params.params_val,
1342 1343
                                       &ret->params.params_len,
                                       0) < 0)
1344 1345 1346 1347 1348 1349
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
1350
        virNetMessageSaveError(rerr);
1351
    virTypedParamsFree(params, nparams);
1352 1353 1354 1355 1356
    if (dom)
        virDomainFree(dom);
    return rv;
}

1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
static int
remoteDispatchConnectListAllDomains(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_connect_list_all_domains_args *args,
                                    remote_connect_list_all_domains_ret *ret)
{
    virDomainPtr *doms = NULL;
    int ndomains = 0;
1367
    size_t i;
1368 1369 1370 1371
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1372
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1373 1374 1375 1376 1377 1378 1379 1380
        goto cleanup;
    }

    if ((ndomains = virConnectListAllDomains(priv->conn,
                                             args->need_results ? &doms : NULL,
                                             args->flags)) < 0)
        goto cleanup;

1381 1382 1383 1384 1385 1386 1387
    if (ndomains > REMOTE_DOMAIN_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domains '%d' for limit '%d'"),
                       ndomains, REMOTE_DOMAIN_LIST_MAX);
        goto cleanup;
    }

1388
    if (doms && ndomains) {
1389
        if (VIR_ALLOC_N(ret->domains.domains_val, ndomains) < 0)
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
            goto cleanup;

        ret->domains.domains_len = ndomains;

        for (i = 0; i < ndomains; i++)
            make_nonnull_domain(ret->domains.domains_val + i, doms[i]);
    } else {
        ret->domains.domains_len = 0;
        ret->domains.domains_val = NULL;
    }

    ret->ret = ndomains;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (doms) {
        for (i = 0; i < ndomains; i++)
            virDomainFree(doms[i]);
        VIR_FREE(doms);
    }
    return rv;
}

1416
static int
1417 1418
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1419
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1420
                                                virNetMessageErrorPtr rerr,
1421 1422 1423 1424 1425
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1426
    int nparams = 0;
1427
    int rv = -1;
1428 1429
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1430

1431
    if (!priv->conn) {
1432
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1433 1434 1435
        goto cleanup;
    }

1436
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1437
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1438 1439
        goto cleanup;
    }
1440
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1441
        goto cleanup;
1442
    nparams = args->nparams;
1443

1444
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1445 1446 1447 1448 1449 1450 1451
        goto cleanup;

    if (virDomainGetSchedulerParametersFlags(dom, params, &nparams,
                                             args->flags) < 0)
        goto cleanup;

    if (remoteSerializeTypedParameters(params, nparams,
1452
                                       &ret->params.params_val,
1453 1454
                                       &ret->params.params_len,
                                       args->flags) < 0)
1455
        goto cleanup;
1456

1457
    rv = 0;
1458 1459

cleanup:
1460
    if (rv < 0)
1461
        virNetMessageSaveError(rerr);
1462
    virTypedParamsFree(params, nparams);
1463 1464 1465
    if (dom)
        virDomainFree(dom);
    return rv;
1466 1467
}

1468
static int
1469 1470
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1471
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1472
                                virNetMessageErrorPtr rerr,
1473 1474
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1475
{
1476
    virDomainPtr dom = NULL;
1477
    struct _virDomainMemoryStat *stats = NULL;
1478 1479
    int nr_stats;
    size_t i;
1480
    int rv = -1;
1481 1482
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1483

1484
    if (!priv->conn) {
1485
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1486
        goto cleanup;
1487 1488
    }

1489
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1490 1491
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1492
        goto cleanup;
1493 1494
    }

1495
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1496
        goto cleanup;
1497 1498

    /* Allocate stats array for making dispatch call */
1499
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
1500
        goto cleanup;
1501

1502
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1503
    if (nr_stats < 0)
1504
        goto cleanup;
1505 1506

    /* Allocate return buffer */
1507
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
1508
        goto cleanup;
1509 1510 1511 1512 1513 1514 1515

    /* 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;
1516 1517 1518 1519
    rv = 0;

cleanup:
    if (rv < 0)
1520
        virNetMessageSaveError(rerr);
1521 1522
    if (dom)
        virDomainFree(dom);
1523
    VIR_FREE(stats);
1524
    return rv;
1525 1526
}

1527
static int
1528 1529
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1530
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1531
                              virNetMessageErrorPtr rerr,
1532 1533
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1534
{
1535
    virDomainPtr dom = NULL;
1536 1537 1538 1539
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1540
    int rv = -1;
1541 1542
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1543

1544
    if (!priv->conn) {
1545
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1546
        goto cleanup;
1547 1548
    }

1549
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1550
        goto cleanup;
1551 1552 1553 1554 1555 1556
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1557 1558
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1559
        goto cleanup;
1560 1561 1562
    }

    ret->buffer.buffer_len = size;
1563
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1564
        goto cleanup;
1565

1566
    if (virDomainBlockPeek(dom, path, offset, size,
1567
                           ret->buffer.buffer_val, flags) < 0)
1568
        goto cleanup;
1569

1570 1571 1572 1573
    rv = 0;

cleanup:
    if (rv < 0) {
1574
        virNetMessageSaveError(rerr);
1575 1576 1577 1578 1579
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
1580 1581
}

1582 1583 1584
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1585
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1586 1587 1588 1589 1590 1591 1592
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_block_stats_flags_args *args,
                                    remote_domain_block_stats_flags_ret *ret)
{
    virTypedParameterPtr params = NULL;
    virDomainPtr dom = NULL;
    const char *path = args->path;
1593
    int nparams = 0;
1594 1595 1596 1597 1598 1599
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1600
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1601 1602 1603 1604 1605 1606 1607
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;
    flags = args->flags;

1608
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1609
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1610 1611
        goto cleanup;
    }
1612
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1613
        goto cleanup;
1614
    nparams = args->nparams;
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629

    if (virDomainBlockStatsFlags(dom, path, params, &nparams, flags) < 0)
        goto cleanup;

    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

    /* Serialise the block stats. */
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
1630 1631
                                       &ret->params.params_len,
                                       args->flags) < 0)
1632 1633 1634 1635 1636 1637
        goto cleanup;

success:
    rv = 0;

cleanup:
1638
    if (rv < 0)
1639
        virNetMessageSaveError(rerr);
1640
    virTypedParamsFree(params, nparams);
A
ajia@redhat.com 已提交
1641 1642
    if (dom)
        virDomainFree(dom);
1643 1644 1645
    return rv;
}

R
Richard W.M. Jones 已提交
1646
static int
1647 1648
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1649
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1650
                               virNetMessageErrorPtr rerr,
1651 1652
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1653
{
1654
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1655 1656 1657
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1658
    int rv = -1;
1659 1660
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1661

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

1667
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1668
        goto cleanup;
R
Richard W.M. Jones 已提交
1669 1670 1671 1672 1673
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1674 1675
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1676
        goto cleanup;
R
Richard W.M. Jones 已提交
1677 1678 1679
    }

    ret->buffer.buffer_len = size;
1680
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1681
        goto cleanup;
R
Richard W.M. Jones 已提交
1682

1683
    if (virDomainMemoryPeek(dom, offset, size,
1684
                            ret->buffer.buffer_val, flags) < 0)
1685
        goto cleanup;
R
Richard W.M. Jones 已提交
1686

1687 1688 1689 1690
    rv = 0;

cleanup:
    if (rv < 0) {
1691
        virNetMessageSaveError(rerr);
1692 1693 1694 1695 1696
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
1697 1698
}

1699
static int
1700 1701
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1702
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1703
                                     virNetMessageErrorPtr rerr,
1704 1705
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1706
{
1707 1708
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1709
    int rv = -1;
1710 1711
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1712

1713
    if (!priv->conn) {
1714
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1715
        goto cleanup;
1716 1717
    }

1718
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1719 1720
        goto cleanup;

1721
    if (VIR_ALLOC(seclabel) < 0)
1722 1723 1724 1725 1726 1727
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
1728
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
1729
        goto cleanup;
1730 1731
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1732

1733 1734 1735 1736
    rv = 0;

cleanup:
    if (rv < 0)
1737
        virNetMessageSaveError(rerr);
1738 1739 1740
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
1741
    return rv;
1742 1743
}

M
Marcelo Cerri 已提交
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
static int
remoteDispatchDomainGetSecurityLabelList(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_domain_get_security_label_list_args *args,
                                         remote_domain_get_security_label_list_ret *ret)
{
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabels = NULL;
1754 1755
    int len, rv = -1;
    size_t i;
M
Marcelo Cerri 已提交
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if ((len = virDomainGetSecurityLabelList(dom, &seclabels)) < 0) {
        ret->ret = len;
        ret->labels.labels_len = 0;
        ret->labels.labels_val = NULL;
        goto done;
    }

1774
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
1775 1776 1777 1778 1779
        goto cleanup;

    for (i = 0; i < len; i++) {
        size_t label_len = strlen(seclabels[i].label) + 1;
        remote_domain_get_security_label_ret *cur = &ret->labels.labels_val[i];
1780
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0)
M
Marcelo Cerri 已提交
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803
            goto cleanup;
        if (virStrcpy(cur->label.label_val, seclabels[i].label, label_len) == NULL) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("failed to copy security label"));
            goto cleanup;
        }
        cur->label.label_len = label_len;
        cur->enforcing = seclabels[i].enforcing;
    }
    ret->labels.labels_len = ret->ret = len;

done:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabels);
    return rv;
}

1804
static int
1805 1806
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1807
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1808
                                   virNetMessageErrorPtr rerr,
1809
                                   remote_node_get_security_model_ret *ret)
1810
{
1811
    virSecurityModel secmodel;
1812
    int rv = -1;
1813 1814
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1815

1816
    if (!priv->conn) {
1817
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1818
        goto cleanup;
1819 1820
    }

1821
    memset(&secmodel, 0, sizeof(secmodel));
1822
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
1823 1824 1825
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
1826
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
1827 1828 1829 1830
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
1831
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
1832
        goto cleanup;
1833
    strcpy(ret->doi.doi_val, secmodel.doi);
1834

1835 1836 1837 1838
    rv = 0;

cleanup:
    if (rv < 0)
1839
        virNetMessageSaveError(rerr);
1840
    return rv;
1841 1842
}

1843
static int
1844 1845
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1846
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1847
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
1848 1849
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
1850 1851 1852 1853 1854
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
1855 1856
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1857

1858
    if (!priv->conn) {
1859
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1860 1861 1862
        goto cleanup;
    }

1863
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1864 1865 1866
        goto cleanup;

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
1867
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
1868 1869 1870 1871 1872
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
1873
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1874 1875 1876 1877 1878 1879
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->ncpumaps * args->maplen) < 0)
1880
        goto cleanup;
1881

E
Eric Blake 已提交
1882
    if ((num = virDomainGetVcpuPinInfo(dom,
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
                                       args->ncpumaps,
                                       cpumaps,
                                       args->maplen,
                                       args->flags)) < 0)
        goto cleanup;

    ret->num = num;
    /* 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.
     */
    ret->cpumaps.cpumaps_len = args->ncpumaps * args->maplen;
    ret->cpumaps.cpumaps_val = (char *) cpumaps;
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
    cpumaps = NULL;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;
}

static int
remoteDispatchDomainPinEmulator(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client,
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                virNetMessageErrorPtr rerr,
                                remote_domain_pin_emulator_args *args)
{
    int rv = -1;
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (virDomainPinEmulator(dom,
                             (unsigned char *) args->cpumap.cpumap_val,
                             args->cpumap.cpumap_len,
                             args->flags) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
remoteDispatchDomainGetEmulatorPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_emulator_pin_info_args *args,
                                       remote_domain_get_emulator_pin_info_ret *ret)
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int r;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    /* Allocate buffers to take the results */
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maplen) < 0)
1972
        goto cleanup;
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982

    if ((r = virDomainGetEmulatorPinInfo(dom,
                                         cpumaps,
                                         args->maplen,
                                         args->flags)) < 0)
        goto cleanup;

    ret->ret = r;
    ret->cpumaps.cpumaps_len = args->maplen;
    ret->cpumaps.cpumaps_val = (char *) cpumaps;
1983 1984 1985 1986 1987 1988
    cpumaps = NULL;

    rv = 0;

cleanup:
    if (rv < 0)
1989
        virNetMessageSaveError(rerr);
1990 1991 1992 1993 1994 1995
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;
}

1996
static int
1997 1998
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
1999
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
2000
                             virNetMessageErrorPtr rerr,
2001 2002
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
2003
{
2004
    virDomainPtr dom = NULL;
2005 2006
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
2007 2008
    int info_len;
    size_t i;
2009
    int rv = -1;
2010 2011
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2012

2013
    if (!priv->conn) {
2014
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2015
        goto cleanup;
2016 2017
    }

2018
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2019
        goto cleanup;
2020

2021
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
2022
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
2023
        goto cleanup;
2024
    }
2025

2026 2027
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
2028
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2029 2030 2031 2032 2033
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
2034
        goto cleanup;
2035 2036
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
2037
        goto cleanup;
2038 2039 2040 2041 2042 2043 2044 2045 2046

    if ((info_len = virDomainGetVcpus(dom,
                                      info, args->maxinfo,
                                      cpumaps, args->maplen)) < 0)
        goto cleanup;

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
    if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
2047
        goto cleanup;
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064

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

    rv = 0;
2065 2066

cleanup:
2067
    if (rv < 0) {
2068
        virNetMessageSaveError(rerr);
2069 2070 2071 2072
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
2073 2074 2075
    if (dom)
        virDomainFree(dom);
    return rv;
2076 2077 2078
}

static int
2079 2080
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2081
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2082
                                   virNetMessageErrorPtr rerr,
2083 2084
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
2085
{
2086 2087 2088 2089 2090
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2091
    int rv = -1;
2092 2093
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2094

2095
    if (!priv->conn) {
2096
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2097
        goto cleanup;
2098 2099
    }

2100 2101 2102 2103
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
2104
    if (VIR_ALLOC(uri_out) < 0)
2105
        goto cleanup;
2106

2107
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
2108 2109
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
2110
        goto cleanup;
2111

2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
    /* remoteDispatchClientRequest will free cookie, uri_out and
     * the string if there is one.
     */
    ret->cookie.cookie_len = cookielen;
    ret->cookie.cookie_val = cookie;
    if (*uri_out == NULL) {
        ret->uri_out = NULL;
    } else {
        ret->uri_out = uri_out;
        uri_out = NULL;
    }
2123

2124
    rv = 0;
2125

2126 2127
cleanup:
    if (rv < 0)
2128
        virNetMessageSaveError(rerr);
2129
    VIR_FREE(uri_out);
2130
    return rv;
2131 2132
}

2133
static int
2134 2135
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2136
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2137
                                    virNetMessageErrorPtr rerr,
2138 2139
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
2140
{
2141 2142 2143 2144 2145
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2146
    int rv = -1;
2147 2148
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2149

2150
    if (!priv->conn) {
2151
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2152
        goto cleanup;
2153 2154
    }

2155 2156
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
2157

2158
    /* Wacky world of XDR ... */
2159
    if (VIR_ALLOC(uri_out) < 0)
2160
        goto cleanup;
2161

2162
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
2163 2164 2165
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
2166
        goto cleanup;
2167

2168 2169 2170 2171 2172 2173
    /* 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;
2174

2175 2176 2177 2178
    rv = 0;

cleanup:
    if (rv < 0)
2179
        virNetMessageSaveError(rerr);
2180
    return rv;
2181 2182
}

C
Chris Lalancette 已提交
2183
static int
2184 2185
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
2186
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
2187 2188 2189
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
2190
{
2191
    virDomainPtr dom = NULL;
2192
    virTypedParameterPtr params = NULL;
2193
    int nparams = 0;
2194
    unsigned int flags;
2195
    int rv = -1;
2196 2197
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2198

2199
    if (!priv->conn) {
2200
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2201
        goto cleanup;
2202
    }
C
Chris Lalancette 已提交
2203

2204
    flags = args->flags;
C
Chris Lalancette 已提交
2205

2206
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2207
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2208 2209
        goto cleanup;
    }
2210
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2211
        goto cleanup;
2212
    nparams = args->nparams;
C
Chris Lalancette 已提交
2213

2214
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2215
        goto cleanup;
C
Chris Lalancette 已提交
2216

2217
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
2218
        goto cleanup;
C
Chris Lalancette 已提交
2219

2220 2221 2222 2223 2224 2225
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
2226 2227
    }

2228
    if (remoteSerializeTypedParameters(params, nparams,
2229
                                       &ret->params.params_val,
2230 2231
                                       &ret->params.params_len,
                                       args->flags) < 0)
2232
        goto cleanup;
2233

2234
success:
2235 2236 2237
    rv = 0;

cleanup:
2238
    if (rv < 0)
2239
        virNetMessageSaveError(rerr);
2240
    virTypedParamsFree(params, nparams);
2241 2242 2243
    if (dom)
        virDomainFree(dom);
    return rv;
2244 2245
}

2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
static int
remoteDispatchDomainGetNumaParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_domain_get_numa_parameters_args *args,
                                      remote_domain_get_numa_parameters_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
2256
    int nparams = 0;
2257 2258 2259 2260 2261 2262
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2263
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2264 2265 2266 2267 2268
        goto cleanup;
    }

    flags = args->flags;

2269
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
2270
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2271 2272
        goto cleanup;
    }
2273
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2274
        goto cleanup;
2275
    nparams = args->nparams;
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (virDomainGetNumaParameters(dom, params, &nparams, flags) < 0)
        goto cleanup;

    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       flags) < 0)
        goto cleanup;

success:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
2303
    virTypedParamsFree(params, nparams);
2304 2305 2306 2307 2308
    if (dom)
        virDomainFree(dom);
    return rv;
}

2309
static int
2310 2311
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
2312
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2313 2314 2315
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
2316
{
2317
    virDomainPtr dom = NULL;
2318
    virTypedParameterPtr params = NULL;
2319
    int nparams = 0;
2320
    unsigned int flags;
2321
    int rv = -1;
2322 2323
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2324

2325
    if (!priv->conn) {
2326
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2327
        goto cleanup;
2328 2329
    }

2330 2331
    flags = args->flags;

2332
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2333
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2334
        goto cleanup;
2335
    }
2336
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2337
        goto cleanup;
2338
    nparams = args->nparams;
2339

2340
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2341
        goto cleanup;
2342

2343
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2344
        goto cleanup;
2345

2346 2347 2348 2349 2350 2351 2352 2353
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2354
    if (remoteSerializeTypedParameters(params, nparams,
2355
                                       &ret->params.params_val,
2356 2357
                                       &ret->params.params_len,
                                       args->flags) < 0)
2358
        goto cleanup;
2359

2360 2361
success:
    rv = 0;
2362

2363
cleanup:
2364
    if (rv < 0)
2365
        virNetMessageSaveError(rerr);
2366
    virTypedParamsFree(params, nparams);
2367 2368 2369
    if (dom)
        virDomainFree(dom);
    return rv;
2370 2371
}

2372
static int
2373 2374
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2375
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2376 2377 2378
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2379
{
2380
    virNodeCPUStatsPtr params = NULL;
2381
    size_t i;
2382
    int cpuNum = args->cpuNum;
2383
    int nparams = 0;
2384 2385
    unsigned int flags;
    int rv = -1;
2386 2387
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2388

2389
    if (!priv->conn) {
2390
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2391 2392 2393 2394 2395
        goto cleanup;
    }

    flags = args->flags;

2396
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
2397
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2398 2399
        goto cleanup;
    }
2400
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2401
        goto cleanup;
2402
    nparams = args->nparams;
2403

2404
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
        goto cleanup;

    /* In this case, we need to send back the number of stats
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

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

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2422 2423
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2424 2425 2426 2427 2428 2429 2430

        ret->params.params_val[i].value = params[i].value;
    }

success:
    rv = 0;

2431 2432
cleanup:
    if (rv < 0) {
2433
        virNetMessageSaveError(rerr);
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444
        if (ret->params.params_val) {
            for (i = 0; i < nparams; i++)
                VIR_FREE(ret->params.params_val[i].field);
            VIR_FREE(ret->params.params_val);
        }
    }
    VIR_FREE(params);
    return rv;
}

static int
2445 2446
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2447
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2448 2449 2450
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2451
{
2452
    virNodeMemoryStatsPtr params = NULL;
2453
    size_t i;
2454
    int cellNum = args->cellNum;
2455
    int nparams = 0;
2456 2457
    unsigned int flags;
    int rv = -1;
2458 2459
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2460

2461
    if (!priv->conn) {
2462
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2463 2464 2465 2466 2467
        goto cleanup;
    }

    flags = args->flags;

2468
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
2469
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2470 2471
        goto cleanup;
    }
2472
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2473
        goto cleanup;
2474
    nparams = args->nparams;
2475

2476
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
        goto cleanup;

    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

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

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2494 2495
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2496 2497 2498 2499 2500 2501 2502

        ret->params.params_val[i].value = params[i].value;
    }

success:
    rv = 0;

2503 2504
cleanup:
    if (rv < 0) {
2505
        virNetMessageSaveError(rerr);
2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
        if (ret->params.params_val) {
            for (i = 0; i < nparams; i++)
                VIR_FREE(ret->params.params_val[i].field);
            VIR_FREE(ret->params.params_val);
        }
    }
    VIR_FREE(params);
    return rv;
}

2516 2517 2518
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2519
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_get_block_job_info_args *args,
                                    remote_domain_get_block_job_info_ret *ret)
{
    virDomainPtr dom = NULL;
    virDomainBlockJobInfo tmp;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2531
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    rv = virDomainGetBlockJobInfo(dom, args->path, &tmp, args->flags);
    if (rv <= 0)
        goto cleanup;

    ret->type = tmp.type;
    ret->bandwidth = tmp.bandwidth;
    ret->cur = tmp.cur;
    ret->end = tmp.end;
    ret->found = 1;
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567
static int
remoteDispatchDomainGetBlockIoTune(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                   virNetMessagePtr hdr ATTRIBUTE_UNUSED,
                                   virNetMessageErrorPtr rerr,
                                   remote_domain_get_block_io_tune_args *args,
                                   remote_domain_get_block_io_tune_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    virTypedParameterPtr params = NULL;
2568
    int nparams = 0;
2569 2570 2571 2572
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2573
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2574 2575 2576
        goto cleanup;
    }

2577
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
2578
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2579 2580 2581
        goto cleanup;
    }

2582
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2583
        goto cleanup;
2584
    nparams = args->nparams;
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (virDomainGetBlockIoTune(dom, args->disk ? *args->disk : NULL,
                                params, &nparams, args->flags) < 0)
        goto cleanup;

    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

    /* Serialise the block I/O tuning parameters. */
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       args->flags) < 0)
        goto cleanup;

success:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
2614
    virTypedParamsFree(params, nparams);
2615 2616 2617 2618
    if (dom)
        virDomainFree(dom);
    return rv;
}
2619

D
Daniel Veillard 已提交
2620 2621
/*-------------------------------------------------------------*/

2622
static int
2623
remoteDispatchAuthList(virNetServerPtr server,
2624
                       virNetServerClientPtr client,
2625
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2626
                       virNetMessageErrorPtr rerr,
2627
                       remote_auth_list_ret *ret)
2628
{
2629
    int rv = -1;
2630 2631
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2632
    gid_t callerGid;
2633
    pid_t callerPid;
2634
    unsigned long long timestamp;
2635 2636 2637 2638 2639 2640

    /* If the client is root then we want to bypass the
     * policykit auth to avoid root being denied if
     * some piece of polkit isn't present/running
     */
    if (auth == VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2641
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2642
                                              &callerPid, &timestamp) < 0) {
2643 2644 2645 2646
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2647 2648
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
2649
                            (long long) callerPid, (int) callerUid) < 0)
J
Jim Fehlig 已提交
2650 2651
                goto cleanup;
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
2652
            virNetServerClientSetAuth(client, 0);
2653
            virNetServerTrackCompletedAuth(server);
2654
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
2655
            VIR_FREE(ident);
2656 2657
        }
    }
2658

2659
    ret->types.types_len = 1;
2660
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
2661
        goto cleanup;
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675

    switch (auth) {
    case VIR_NET_SERVER_SERVICE_AUTH_NONE:
        ret->types.types_val[0] = REMOTE_AUTH_NONE;
        break;
    case VIR_NET_SERVER_SERVICE_AUTH_POLKIT:
        ret->types.types_val[0] = REMOTE_AUTH_POLKIT;
        break;
    case VIR_NET_SERVER_SERVICE_AUTH_SASL:
        ret->types.types_val[0] = REMOTE_AUTH_SASL;
        break;
    default:
        ret->types.types_val[0] = REMOTE_AUTH_NONE;
    }
2676

2677 2678 2679 2680
    rv = 0;

cleanup:
    if (rv < 0)
2681
        virNetMessageSaveError(rerr);
2682
    return rv;
2683 2684 2685
}


2686
#ifdef WITH_SASL
2687 2688
/*
 * Initializes the SASL session in prepare for authentication
2689
 * and gives the client a list of allowed mechanisms to choose
2690 2691
 */
static int
2692 2693
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2694
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2695
                           virNetMessageErrorPtr rerr,
2696
                           remote_auth_sasl_init_ret *ret)
2697
{
2698 2699 2700
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2701

2702
    virMutexLock(&priv->lock);
2703

2704 2705 2706
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2707
        VIR_ERROR(_("client tried invalid SASL init request"));
2708
        goto authfail;
2709 2710
    }

2711 2712 2713 2714 2715
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2716
        goto authfail;
2717

2718
# if WITH_GNUTLS
2719
    /* Inform SASL that we've got an external SSF layer from TLS */
2720 2721 2722 2723
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2724
            goto authfail;
2725 2726 2727 2728 2729

        ssf *= 8; /* key size is bytes, sasl wants bits */

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2730
            goto authfail;
2731
    }
2732
# endif
2733

2734
    if (virNetServerClientIsSecure(client))
2735
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2736 2737
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2738
        /* Plain TCP, better get an SSF layer */
2739 2740 2741 2742
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
2743

2744
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
2745
        goto authfail;
2746
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
2747

2748 2749
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
2750
    return 0;
2751 2752

authfail:
2753
    virResetLastError();
2754 2755
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2756
    virNetMessageSaveError(rerr);
2757 2758 2759
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2760
    virObjectUnref(sasl);
2761
    virMutexUnlock(&priv->lock);
2762
    return -1;
2763 2764
}

2765
/*
2766 2767
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
2768
static int
2769 2770
remoteSASLFinish(virNetServerPtr server,
                 virNetServerClientPtr client)
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785
{
    const char *identity;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    int ssf;

    /* TLS or UNIX domain sockets trivially OK */
    if (!virNetServerClientIsSecure(client)) {
        if ((ssf = virNetSASLSessionGetKeySize(priv->sasl)) < 0)
            goto error;

        VIR_DEBUG("negotiated an SSF of %d", ssf);
        if (ssf < 56) { /* 56 is good for Kerberos */
            VIR_ERROR(_("negotiated SSF %d was not strong enough"), ssf);
            return -2;
        }
2786
    }
2787 2788

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
2789
        return -2;
2790

2791 2792
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
2793

2794
    virNetServerClientSetAuth(client, 0);
2795
    virNetServerTrackCompletedAuth(server);
2796
    virNetServerClientSetSASLSession(client, priv->sasl);
2797

2798
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
2799

2800 2801 2802
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2803

2804
    virObjectUnref(priv->sasl);
2805
    priv->sasl = NULL;
2806

2807
    return 0;
2808

2809 2810 2811
error:
    return -1;
}
2812

2813 2814 2815 2816
/*
 * This starts the SASL authentication negotiation.
 */
static int
2817
remoteDispatchAuthSaslStart(virNetServerPtr server,
2818
                            virNetServerClientPtr client,
2819
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2820
                            virNetMessageErrorPtr rerr,
2821 2822
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2823 2824
{
    const char *serverout;
2825
    size_t serveroutlen;
2826
    int err;
2827 2828 2829
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2830
    const char *identity;
2831

2832
    virMutexLock(&priv->lock);
2833

2834 2835 2836
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
2837
        VIR_ERROR(_("client tried invalid SASL start request"));
2838
        goto authfail;
2839 2840
    }

2841 2842
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
2843 2844 2845 2846 2847 2848 2849 2850 2851
    err = virNetSASLSessionServerStart(priv->sasl,
                                       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 != VIR_NET_SASL_COMPLETE &&
        err != VIR_NET_SASL_CONTINUE)
2852
        goto authfail;
2853

2854
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2855
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
2856
        goto authfail;
2857 2858 2859 2860
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2861 2862
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2863 2864 2865 2866 2867 2868 2869
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2870
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2871
    if (err == VIR_NET_SASL_CONTINUE) {
2872 2873
        ret->complete = 0;
    } else {
2874
        /* Check username whitelist ACL */
2875
        if ((err = remoteSASLFinish(server, client)) < 0) {
2876 2877 2878 2879 2880
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2881

2882 2883 2884
        ret->complete = 1;
    }

2885
    virMutexUnlock(&priv->lock);
2886
    return 0;
2887 2888

authfail:
2889 2890 2891
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2892 2893 2894
    goto error;

authdeny:
2895
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2896 2897 2898
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2899 2900
    goto error;

2901
error:
2902
    virObjectUnref(priv->sasl);
2903 2904
    priv->sasl = NULL;
    virResetLastError();
2905 2906
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2907 2908 2909
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2910
    return -1;
2911 2912 2913 2914
}


static int
2915
remoteDispatchAuthSaslStep(virNetServerPtr server,
2916
                           virNetServerClientPtr client,
2917
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2918
                           virNetMessageErrorPtr rerr,
2919 2920
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
2921 2922
{
    const char *serverout;
2923
    size_t serveroutlen;
2924
    int err;
2925 2926 2927
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2928
    const char *identity;
2929

2930 2931 2932 2933 2934
    virMutexLock(&priv->lock);

    VIR_DEBUG("Step SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
2935
        VIR_ERROR(_("client tried invalid SASL start request"));
2936
        goto authfail;
2937 2938
    }

2939
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
2940
              args->data.data_len, args->nil);
2941 2942 2943 2944 2945 2946 2947 2948
    err = virNetSASLSessionServerStep(priv->sasl,
                                      /* NB, distinction of NULL vs "" is *critical* in SASL */
                                      args->nil ? NULL : args->data.data_val,
                                      args->data.data_len,
                                      &serverout,
                                      &serveroutlen);
    if (err != VIR_NET_SASL_COMPLETE &&
        err != VIR_NET_SASL_CONTINUE)
2949
        goto authfail;
2950 2951

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2952
        VIR_ERROR(_("sasl step reply data too long %d"),
2953
                  (int)serveroutlen);
2954
        goto authfail;
2955 2956 2957 2958
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2959 2960
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2961 2962 2963 2964 2965 2966 2967
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2968
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2969
    if (err == VIR_NET_SASL_CONTINUE) {
2970 2971
        ret->complete = 0;
    } else {
2972
        /* Check username whitelist ACL */
2973
        if ((err = remoteSASLFinish(server, client)) < 0) {
2974 2975 2976 2977 2978
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2979

2980 2981 2982
        ret->complete = 1;
    }

2983
    virMutexUnlock(&priv->lock);
2984
    return 0;
2985 2986

authfail:
2987 2988 2989
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2990 2991 2992
    goto error;

authdeny:
2993
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2994 2995 2996
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2997 2998
    goto error;

2999
error:
3000
    virObjectUnref(priv->sasl);
3001 3002
    priv->sasl = NULL;
    virResetLastError();
3003 3004
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3005 3006 3007
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3008 3009
    return -1;
}
3010 3011 3012 3013
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3014
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3015 3016 3017 3018
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3019 3020
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3021 3022 3023 3024 3025 3026
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
3027
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3028 3029 3030 3031 3032
                            virNetMessageErrorPtr rerr,
                            remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3033 3034
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3035 3036 3037 3038 3039 3040
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3041
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3042 3043 3044 3045 3046
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                           remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3047 3048
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3049 3050 3051 3052
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
3053 3054 3055



3056
#if WITH_POLKIT1
3057
static int
3058
remoteDispatchAuthPolkit(virNetServerPtr server,
3059
                         virNetServerClientPtr client,
3060
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3061
                         virNetMessageErrorPtr rerr,
3062
                         remote_auth_polkit_ret *ret)
3063
{
3064
    pid_t callerPid = -1;
3065
    gid_t callerGid = -1;
3066
    uid_t callerUid = -1;
3067
    unsigned long long timestamp;
3068 3069
    const char *action;
    int status = -1;
3070
    char *ident = NULL;
3071
    bool authdismissed = 0;
3072
    char *pkout = NULL;
3073 3074
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3075
    virCommandPtr cmd = NULL;
3076
# ifndef PKCHECK_SUPPORTS_UID
3077
    static bool polkitInsecureWarned;
3078
# endif
3079

3080 3081
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
3082 3083 3084
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

3085
    cmd = virCommandNewArgList(PKCHECK_PATH, "--action-id", action, NULL);
3086
    virCommandSetOutputBuffer(cmd, &pkout);
3087
    virCommandSetErrorBuffer(cmd, &pkout);
3088

3089 3090
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3091
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3092 3093 3094
        goto authfail;
    }

3095
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
3096
                                          &callerPid, &timestamp) < 0) {
3097 3098 3099
        goto authfail;
    }

3100 3101 3102 3103 3104 3105
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

3106 3107
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3108

3109
    virCommandAddArg(cmd, "--process");
3110

3111
# ifdef PKCHECK_SUPPORTS_UID
3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122
    virCommandAddArgFormat(cmd, "%lld,%llu,%lu",
                           (long long) callerPid,
                           timestamp,
                           (unsigned long) callerUid);
# else
    if (!polkitInsecureWarned) {
        VIR_WARN("No support for caller UID with pkcheck. "
                 "This deployment is known to be insecure.");
        polkitInsecureWarned = true;
    }
    virCommandAddArgFormat(cmd, "%lld,%llu", (long long) callerPid, timestamp);
3123
# endif
3124

3125
    virCommandAddArg(cmd, "--allow-user-interaction");
3126

3127
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
3128
                    (long long) callerPid, callerUid) < 0)
3129 3130
        goto authfail;

3131
    if (virCommandRun(cmd, &status) < 0)
3132
        goto authfail;
3133

3134
    authdismissed = (pkout && strstr(pkout, "dismissed=true"));
3135
    if (status != 0) {
3136 3137 3138
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d "
                    "with status %d"),
                  action, (long long) callerPid, callerUid, status);
3139
        goto authdeny;
3140
    }
3141 3142 3143
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3144 3145
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
3146 3147
    ret->complete = 1;

3148
    virNetServerClientSetAuth(client, 0);
3149
    virNetServerTrackCompletedAuth(server);
3150
    virMutexUnlock(&priv->lock);
3151
    virCommandFree(cmd);
E
Eric Blake 已提交
3152
    VIR_FREE(pkout);
3153
    VIR_FREE(ident);
3154

3155 3156
    return 0;

3157
error:
3158 3159
    virCommandFree(cmd);
    VIR_FREE(ident);
3160
    virResetLastError();
3161

3162
    if (authdismissed) {
3163 3164
        virReportError(VIR_ERR_AUTH_CANCELLED, "%s",
                       _("authentication cancelled by user"));
3165 3166
    } else if (pkout && *pkout) {
        virReportError(VIR_ERR_AUTH_FAILED, _("polkit: %s"), pkout);
3167
    } else {
3168
        virReportError(VIR_ERR_AUTH_FAILED, "%s", _("authentication failed"));
3169
    }
3170 3171

    VIR_FREE(pkout);
3172 3173 3174 3175
    virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return -1;

3176
authfail:
3177 3178 3179
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3180 3181 3182
    goto error;

authdeny:
3183 3184
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
3185
          client, REMOTE_AUTH_POLKIT, ident);
3186
    goto error;
3187
}
3188
#elif WITH_POLKIT0
3189
static int
3190
remoteDispatchAuthPolkit(virNetServerPtr server,
3191
                         virNetServerClientPtr client,
3192
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3193
                         virNetMessageErrorPtr rerr,
3194
                         remote_auth_polkit_ret *ret)
3195 3196
{
    pid_t callerPid;
3197
    gid_t callerGid;
3198
    uid_t callerUid;
3199 3200 3201 3202 3203 3204
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
3205
    const char *action;
3206
    char *ident = NULL;
3207 3208
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3209
    DBusConnection *sysbus;
J
Jim Fehlig 已提交
3210
    unsigned long long timestamp;
3211

J
Jim Fehlig 已提交
3212
    virMutexLock(&priv->lock);
3213

J
Jim Fehlig 已提交
3214
    action = virNetServerClientGetReadonly(client) ?
3215 3216
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
3217

3218
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
J
Jim Fehlig 已提交
3219
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3220
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3221
        goto authfail;
3222 3223
    }

3224
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
J
Jim Fehlig 已提交
3225
                                          &callerPid, &timestamp) < 0) {
3226
        VIR_ERROR(_("cannot get peer socket identity"));
3227
        goto authfail;
3228 3229
    }

3230
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
3231
                    (long long) callerPid, callerUid) < 0)
3232 3233
        goto authfail;

3234 3235 3236
    if (!(sysbus = virDBusGetSystemBus()))
        goto authfail;

3237 3238
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3239
    dbus_error_init(&err);
3240
    if (!(pkcaller = polkit_caller_new_from_pid(sysbus,
3241
                                                callerPid, &err))) {
3242
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
3243
        dbus_error_free(&err);
3244
        goto authfail;
3245
    }
3246

3247
    if (!(pkaction = polkit_action_new())) {
3248
        char ebuf[1024];
3249
        VIR_ERROR(_("Failed to create polkit action %s"),
3250
                  virStrerror(errno, ebuf, sizeof(ebuf)));
3251
        polkit_caller_unref(pkcaller);
3252
        goto authfail;
3253 3254 3255 3256 3257
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
3258
        char ebuf[1024];
3259
        VIR_ERROR(_("Failed to create polkit context %s"),
3260
                  (pkerr ? polkit_error_get_error_message(pkerr)
3261
                   : virStrerror(errno, ebuf, sizeof(ebuf))));
3262 3263 3264 3265 3266
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
3267
        goto authfail;
3268
    }
3269

3270
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
3271 3272 3273 3274 3275 3276
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
3277 3278 3279
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
3280
        goto authfail;
3281
    }
3282
# else
3283 3284 3285
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
3286
# endif
3287 3288 3289 3290
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
3291 3292
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d, result: %s"),
                  action, (long long) callerPid, callerUid,
3293
                  polkit_result_to_string_representation(pkresult));
3294
        goto authdeny;
3295
    }
3296 3297 3298
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3299 3300
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d, result %s",
             action, (long long) callerPid, callerUid,
3301 3302
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
3303

3304
    virNetServerClientSetAuth(client, 0);
3305
    virNetServerTrackCompletedAuth(server);
J
Jim Fehlig 已提交
3306
    virMutexUnlock(&priv->lock);
3307
    VIR_FREE(ident);
3308
    return 0;
3309

3310
error:
3311
    VIR_FREE(ident);
3312
    virResetLastError();
3313 3314
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3315
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3316
    virMutexUnlock(&priv->lock);
3317 3318
    return -1;

3319
authfail:
3320 3321 3322
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3323
    goto error;
3324

3325
authdeny:
3326 3327 3328
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3329
    goto error;
3330 3331
}

3332
#else /* !WITH_POLKIT0 & !HAVE_POLKIT1*/
3333 3334

static int
3335
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
3336
                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
3337
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3338
                         virNetMessageErrorPtr rerr,
3339
                         remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
3340
{
3341
    VIR_ERROR(_("client tried unsupported PolicyKit init request"));
3342 3343
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3344
    virNetMessageSaveError(rerr);
3345 3346
    return -1;
}
3347
#endif /* WITH_POLKIT1 */
3348 3349


3350 3351 3352
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3353

3354
static int
3355 3356
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3357
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3358
                                  virNetMessageErrorPtr rerr,
3359 3360
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3361
{
3362 3363
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3364
    int rv = -1;
3365 3366
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3367

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

3373
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3374 3375
        goto cleanup;

3376 3377 3378 3379 3380 3381 3382
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
3383
        if (VIR_ALLOC(parent_p) < 0)
3384
            goto cleanup;
3385
        if (VIR_STRDUP(*parent_p, parent) < 0) {
3386 3387 3388 3389 3390 3391
            VIR_FREE(parent_p);
            goto cleanup;
        }
        ret->parent = parent_p;
    }

3392 3393 3394 3395
    rv = 0;

cleanup:
    if (rv < 0)
3396
        virNetMessageSaveError(rerr);
3397 3398
    if (dev)
        virNodeDeviceFree(dev);
3399
    return rv;
3400 3401
}

3402 3403 3404 3405 3406

/***************************
 * Register / deregister events
 ***************************/
static int
3407
remoteDispatchConnectDomainEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
3408
                                         virNetServerClientPtr client,
3409 3410 3411
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                         remote_connect_domain_event_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
3412
{
3413
    int callbackID;
3414
    int rv = -1;
3415 3416
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3417 3418
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3419

3420
    if (!priv->conn) {
3421
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3422
        goto cleanup;
3423 3424
    }

3425 3426
    virMutexLock(&priv->lock);

3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437
    /* If we call register first, we could append a complete callback
     * to our array, but on OOM append failure, we'd have to then hope
     * deregister works to undo our register.  So instead we append an
     * incomplete callback to our array, then register, then fix up
     * our callback; but since VIR_APPEND_ELEMENT clears 'callback' on
     * success, we use 'ref' to save a copy of the pointer.  */
    if (VIR_ALLOC(callback) < 0)
        goto cleanup;
    callback->client = client;
    callback->eventID = VIR_DOMAIN_EVENT_ID_LIFECYCLE;
    callback->callbackID = -1;
3438
    callback->legacy = true;
3439 3440 3441 3442
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3443
        goto cleanup;
O
Osier Yang 已提交
3444

3445
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3446 3447 3448
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
3449 3450 3451 3452 3453
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3454
        goto cleanup;
3455
    }
O
Osier Yang 已提交
3456

3457
    ref->callbackID = callbackID;
3458

3459 3460 3461
    rv = 0;

cleanup:
3462
    VIR_FREE(callback);
3463
    if (rv < 0)
3464 3465
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3466
    return rv;
O
Osier Yang 已提交
3467 3468
}

3469
static int
3470
remoteDispatchConnectDomainEventDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
3471
                                           virNetServerClientPtr client,
3472 3473 3474
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                           remote_connect_domain_event_deregister_ret *ret ATTRIBUTE_UNUSED)
3475
{
3476
    int callbackID = -1;
3477
    int rv = -1;
3478
    size_t i;
3479 3480
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3481

3482
    if (!priv->conn) {
3483
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3484
        goto cleanup;
3485 3486
    }

3487 3488
    virMutexLock(&priv->lock);

3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->eventID == VIR_DOMAIN_EVENT_ID_LIFECYCLE) {
            callbackID = priv->domainEventCallbacks[i]->callbackID;
            break;
        }
    }

    if (callbackID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event %d not registered"),
                       VIR_DOMAIN_EVENT_ID_LIFECYCLE);
3500
        goto cleanup;
3501
    }
3502

3503
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3504
        goto cleanup;
3505

3506 3507
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3508

3509 3510 3511
    rv = 0;

cleanup:
3512
    if (rv < 0)
3513 3514
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3515 3516 3517 3518
    return rv;
}

static void
3519
remoteDispatchObjectEventSend(virNetServerClientPtr client,
3520
                              virNetServerProgramPtr program,
3521 3522 3523 3524
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3525
    virNetMessagePtr msg;
3526

3527
    if (!(msg = virNetMessageNew(false)))
3528
        goto cleanup;
3529

3530 3531 3532 3533 3534 3535
    msg->header.prog = virNetServerProgramGetID(program);
    msg->header.vers = virNetServerProgramGetVersion(program);
    msg->header.proc = procnr;
    msg->header.type = VIR_NET_MESSAGE;
    msg->header.serial = 1;
    msg->header.status = VIR_NET_OK;
3536

3537
    if (virNetMessageEncodeHeader(msg) < 0)
3538 3539
        goto cleanup;

3540 3541
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3542

3543 3544
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3545

3546
    xdr_free(proc, data);
3547 3548 3549
    return;

cleanup:
3550
    virNetMessageFree(msg);
3551
    xdr_free(proc, data);
3552 3553
}

3554
static int
3555 3556
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3557
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3558
                             virNetMessageErrorPtr rerr,
3559 3560
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3561
{
3562 3563 3564
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3565
    int rv = -1;
3566 3567
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3568

3569
    if (!priv->conn) {
3570
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3571
        goto cleanup;
3572 3573
    }

3574
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3575
        goto cleanup;
3576

3577
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3578
        goto cleanup;
3579

3580 3581 3582
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3583 3584 3585 3586
    rv = 0;

cleanup:
    if (rv < 0)
3587
        virNetMessageSaveError(rerr);
3588 3589
    if (secret)
        virSecretFree(secret);
3590
    return rv;
3591 3592
}

3593
static int
3594 3595
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3596
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3597
                             virNetMessageErrorPtr rerr,
3598 3599 3600 3601 3602
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3603 3604
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3605

3606
    if (!priv->conn) {
3607
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3608 3609 3610
        goto cleanup;
    }

3611
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3612 3613 3614 3615 3616 3617 3618 3619 3620
        goto cleanup;

    if (virDomainGetState(dom, &ret->state, &ret->reason, args->flags) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
3621
        virNetMessageSaveError(rerr);
3622 3623 3624 3625 3626
    if (dom)
        virDomainFree(dom);
    return rv;
}

3627 3628 3629 3630 3631 3632

/* Due to back-compat reasons, two RPC calls map to the same libvirt
 * API of virConnectDomainEventRegisterAny.  A client should only use
 * the new call if they have probed
 * VIR_DRV_SUPPORTS_FEATURE(VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK),
 * and must not mix the two styles.  */
3633
static int
3634
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3635
                                            virNetServerClientPtr client,
3636 3637 3638
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            remote_connect_domain_event_register_any_args *args)
3639 3640
{
    int callbackID;
3641
    int rv = -1;
3642 3643
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3644 3645
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3646

3647
    if (!priv->conn) {
3648
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3649
        goto cleanup;
3650 3651
    }

3652 3653
    virMutexLock(&priv->lock);

3654 3655 3656 3657
    /* We intentionally do not use VIR_DOMAIN_EVENT_ID_LAST here; any
     * new domain events added after this point should only use the
     * modern callback style of RPC.  */
    if (args->eventID > VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED ||
3658
        args->eventID < 0) {
3659 3660
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3661
        goto cleanup;
3662 3663
    }

3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674
    /* If we call register first, we could append a complete callback
     * to our array, but on OOM append failure, we'd have to then hope
     * deregister works to undo our register.  So instead we append an
     * incomplete callback to our array, then register, then fix up
     * our callback; but since VIR_APPEND_ELEMENT clears 'callback' on
     * success, we use 'ref' to save a copy of the pointer.  */
    if (VIR_ALLOC(callback) < 0)
        goto cleanup;
    callback->client = client;
    callback->eventID = args->eventID;
    callback->callbackID = -1;
3675
    callback->legacy = true;
3676 3677 3678 3679
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3680
        goto cleanup;
3681

3682
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3683 3684 3685
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3686 3687 3688 3689 3690
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3691
        goto cleanup;
3692
    }
3693

3694
    ref->callbackID = callbackID;
3695

3696 3697 3698
    rv = 0;

cleanup:
3699
    VIR_FREE(callback);
3700
    if (rv < 0)
3701 3702
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3703
    return rv;
3704 3705 3706
}


3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733
static int
remoteDispatchConnectDomainEventCallbackRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                    virNetServerClientPtr client,
                                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                    virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                                    remote_connect_domain_event_callback_register_any_args *args,
                                                    remote_connect_domain_event_callback_register_any_ret *ret)
{
    int callbackID;
    int rv = -1;
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

    if (args->dom &&
        !(dom = get_nonnull_domain(priv->conn, *args->dom)))
        goto cleanup;

3734
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST || args->eventID < 0) {
3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
        goto cleanup;
    }

    /* If we call register first, we could append a complete callback
     * to our array, but on OOM append failure, we'd have to then hope
     * deregister works to undo our register.  So instead we append an
     * incomplete callback to our array, then register, then fix up
     * our callback; but since VIR_APPEND_ELEMENT clears 'callback' on
     * success, we use 'ref' to save a copy of the pointer.  */
    if (VIR_ALLOC(callback) < 0)
        goto cleanup;
    callback->client = client;
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
        goto cleanup;

    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
                                                       dom,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
        goto cleanup;
    }

    ref->callbackID = callbackID;
    ret->callbackID = callbackID;

    rv = 0;

cleanup:
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    virMutexUnlock(&priv->lock);
    return rv;
}


3785
static int
3786
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3787
                                              virNetServerClientPtr client,
3788 3789 3790
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              remote_connect_domain_event_deregister_any_args *args)
3791 3792
{
    int callbackID = -1;
3793
    int rv = -1;
3794
    size_t i;
3795 3796
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3797

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

3803 3804
    virMutexLock(&priv->lock);

3805 3806 3807 3808
    /* We intentionally do not use VIR_DOMAIN_EVENT_ID_LAST here; any
     * new domain events added after this point should only use the
     * modern callback style of RPC.  */
    if (args->eventID > VIR_DOMAIN_EVENT_ID_DEVICE_REMOVED ||
3809
        args->eventID < 0) {
3810 3811
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3812
        goto cleanup;
3813 3814
    }

3815 3816 3817 3818 3819 3820
    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->eventID == args->eventID) {
            callbackID = priv->domainEventCallbacks[i]->callbackID;
            break;
        }
    }
3821
    if (callbackID < 0) {
3822 3823
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event %d not registered"), args->eventID);
3824
        goto cleanup;
3825 3826
    }

3827
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3828
        goto cleanup;
3829

3830 3831
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3832

3833 3834 3835 3836
    rv = 0;

cleanup:
    if (rv < 0)
3837 3838
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3839
    return rv;
3840 3841
}

3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888

static int
remoteDispatchConnectDomainEventCallbackDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                      virNetServerClientPtr client,
                                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                      virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                                      remote_connect_domain_event_callback_deregister_any_args *args)
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->callbackID == args->callbackID)
            break;
    }
    if (i == priv->ndomainEventCallbacks) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event callback %d not registered"),
                       args->callbackID);
        goto cleanup;
    }

    if (virConnectDomainEventDeregisterAny(priv->conn, args->callbackID) < 0)
        goto cleanup;

    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}


C
Chris Lalancette 已提交
3889
static int
3890 3891 3892 3893 3894 3895
qemuDispatchDomainMonitorCommand(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                 virNetMessageErrorPtr rerr,
                                 qemu_domain_monitor_command_args *args,
                                 qemu_domain_monitor_command_ret *ret)
C
Chris Lalancette 已提交
3896
{
3897
    virDomainPtr dom = NULL;
3898
    int rv = -1;
3899 3900
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
3901

3902
    if (!priv->conn) {
3903
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3904
        goto cleanup;
3905 3906
    }

3907
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3908
        goto cleanup;
C
Chris Lalancette 已提交
3909

3910
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
3911
                                    args->flags) < 0)
3912
        goto cleanup;
C
Chris Lalancette 已提交
3913

3914
    rv = 0;
C
Chris Lalancette 已提交
3915

3916 3917
cleanup:
    if (rv < 0)
3918
        virNetMessageSaveError(rerr);
3919 3920
    if (dom)
        virDomainFree(dom);
3921
    return rv;
C
Chris Lalancette 已提交
3922 3923
}

3924

3925
static int
3926 3927
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3928
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3929
                                  virNetMessageErrorPtr rerr,
3930 3931 3932 3933 3934 3935
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
3936
    char *xmlin;
3937 3938 3939
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3940 3941
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3942

3943
    if (!priv->conn) {
3944
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3945 3946 3947
        goto cleanup;
    }

3948
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3949 3950
        goto cleanup;

3951
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3952 3953
    dname = args->dname == NULL ? NULL : *args->dname;

3954
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969
                                       &cookieout, &cookieoutlen,
                                       args->flags, dname, args->resource)))
        goto cleanup;

    /* remoteDispatchClientRequest will free cookie and
     * the xml string if there is one.
     */
    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;
    ret->xml = xml;

    rv = 0;

cleanup:
    if (rv < 0)
3970
        virNetMessageSaveError(rerr);
3971 3972 3973 3974 3975 3976 3977
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3978 3979
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3980
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3981
                                    virNetMessageErrorPtr rerr,
3982 3983 3984 3985 3986 3987 3988 3989 3990
                                    remote_domain_migrate_prepare3_args *args,
                                    remote_domain_migrate_prepare3_ret *ret)
{
    char *cookieout = NULL;
    int cookieoutlen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
    int rv = -1;
3991 3992
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3993

3994
    if (!priv->conn) {
3995
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3996 3997 3998 3999 4000 4001 4002
        goto cleanup;
    }

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

    /* Wacky world of XDR ... */
4003
    if (VIR_ALLOC(uri_out) < 0)
4004 4005
        goto cleanup;

4006
    if (virDomainMigratePrepare3(priv->conn,
4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
        goto cleanup;

    /* remoteDispatchClientRequest will free cookie, uri_out and
     * the string if there is one.
     */
    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;
    ret->uri_out = *uri_out == NULL ? NULL : uri_out;

    rv = 0;

cleanup:
    if (rv < 0) {
4026
        virNetMessageSaveError(rerr);
4027 4028 4029 4030 4031
        VIR_FREE(uri_out);
    }
    return rv;
}

4032

4033
static int
4034 4035
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4036
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4037
                                    virNetMessageErrorPtr rerr,
4038 4039 4040 4041
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
4042
    char *xmlin;
4043
    char *dname;
4044 4045
    char *uri;
    char *dconnuri;
4046 4047 4048
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4049 4050
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4051

4052
    if (!priv->conn) {
4053
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4054 4055 4056
        goto cleanup;
    }

4057
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4058 4059
        goto cleanup;

4060
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4061
    dname = args->dname == NULL ? NULL : *args->dname;
4062 4063
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
4064

4065
    if (virDomainMigratePerform3(dom, xmlin,
4066 4067 4068
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
4069
                                 dconnuri, uri,
4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081
                                 args->flags, dname, args->resource) < 0)
        goto cleanup;

    /* remoteDispatchClientRequest will free cookie
     */
    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;

    rv = 0;

cleanup:
    if (rv < 0)
4082
        virNetMessageSaveError(rerr);
4083 4084 4085 4086 4087 4088 4089
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
4090 4091
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
4092
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
4093
                                   virNetMessageErrorPtr rerr,
4094 4095 4096 4097 4098 4099
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
4100 4101
    char *uri;
    char *dconnuri;
4102
    int rv = -1;
4103 4104
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4105

4106
    if (!priv->conn) {
4107
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4108 4109 4110
        goto cleanup;
    }

4111 4112 4113
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

4114
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
4115 4116 4117 4118 4119 4120
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
4121 4122
        goto cleanup;

4123
    make_nonnull_domain(&ret->dom, dom);
4124 4125 4126 4127 4128 4129 4130 4131 4132 4133

    /* remoteDispatchClientRequest will free cookie
     */
    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;

    rv = 0;

cleanup:
    if (rv < 0) {
4134
        virNetMessageSaveError(rerr);
4135 4136 4137 4138 4139 4140 4141 4142 4143
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
4144 4145
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4146
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4147 4148
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
4149 4150 4151
{
    virDomainPtr dom = NULL;
    int rv = -1;
4152 4153
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4154

4155
    if (!priv->conn) {
4156
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4157 4158 4159
        goto cleanup;
    }

4160
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172
        goto cleanup;

    if (virDomainMigrateConfirm3(dom,
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 args->flags, args->cancelled) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
4173
        virNetMessageSaveError(rerr);
4174 4175 4176 4177 4178 4179
    if (dom)
        virDomainFree(dom);
    return rv;
}


4180 4181 4182 4183 4184 4185
static int remoteDispatchConnectSupportsFeature(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client,
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                virNetMessageErrorPtr rerr,
                                                remote_connect_supports_feature_args *args,
                                                remote_connect_supports_feature_ret *ret)
4186 4187 4188 4189 4190 4191
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

4192 4193 4194 4195 4196 4197 4198 4199 4200 4201
    /* This feature is checked before opening the connection, thus we must
     * check it first.
     */
    if (args->feature == VIR_DRV_FEATURE_PROGRAM_KEEPALIVE) {
        if (virNetServerClientStartKeepAlive(client) < 0)
            goto cleanup;
        supported = 1;
        goto done;
    }

4202
    if (!priv->conn) {
4203
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4204 4205 4206 4207 4208
        goto cleanup;
    }

    switch (args->feature) {
    case VIR_DRV_FEATURE_FD_PASSING:
4209
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
4210 4211 4212 4213
        supported = 1;
        break;

    default:
4214
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
4215 4216 4217 4218
            goto cleanup;
        break;
    }

4219
done:
4220 4221 4222 4223 4224 4225 4226 4227 4228 4229
    ret->supported = supported;
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243
static int
remoteDispatchDomainOpenGraphics(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                 virNetMessagePtr msg,
                                 virNetMessageErrorPtr rerr,
                                 remote_domain_open_graphics_args *args)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    int fd = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4244
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if ((fd = virNetMessageDupFD(msg, 0)) < 0)
        goto cleanup;

    if (virDomainOpenGraphics(dom,
                              args->idx,
                              fd,
                              args->flags) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    VIR_FORCE_CLOSE(fd);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281
static int
remoteDispatchDomainGetInterfaceParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr,
                                           remote_domain_get_interface_parameters_args *args,
                                           remote_domain_get_interface_parameters_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    const char *device = args->device;
4282
    int nparams = 0;
4283 4284 4285 4286 4287 4288
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4289
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4290 4291 4292 4293 4294
        goto cleanup;
    }

    flags = args->flags;

4295
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
4296
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4297 4298
        goto cleanup;
    }
4299
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4300
        goto cleanup;
4301
    nparams = args->nparams;
4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (virDomainGetInterfaceParameters(dom, device, params, &nparams, flags) < 0)
        goto cleanup;

    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       flags) < 0)
        goto cleanup;

success:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
4329
    virTypedParamsFree(params, nparams);
4330 4331 4332 4333
    if (dom)
        virDomainFree(dom);
    return rv;
}
4334

4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350
static int
remoteDispatchDomainGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                virNetMessagePtr hdr ATTRIBUTE_UNUSED,
                                virNetMessageErrorPtr rerr,
                                remote_domain_get_cpu_stats_args *args,
                                remote_domain_get_cpu_stats_ret *ret)
{
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv;
    virTypedParameterPtr params = NULL;
    int rv = -1;
    int percpu_len = 0;

    priv = virNetServerClientGetPrivateData(client);
    if (!priv->conn) {
4351
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4352 4353 4354 4355
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
4356
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4357 4358 4359
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
4360
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
4361 4362 4363 4364
        goto cleanup;
    }

    if (args->nparams > 0 &&
4365
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388
        goto cleanup;

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    percpu_len = virDomainGetCPUStats(dom, params, args->nparams,
                                      args->start_cpu, args->ncpus,
                                      args->flags);
    if (percpu_len < 0)
        goto cleanup;
    /* If nparams == 0, the function returns a single value */
    if (args->nparams == 0)
        goto success;

    if (remoteSerializeTypedParameters(params, args->nparams * args->ncpus,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       args->flags) < 0)
        goto cleanup;

success:
    rv = 0;
    ret->nparams = percpu_len;
4389
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
4390
        size_t i;
4391 4392 4393 4394 4395 4396

        for (i = 0; i < percpu_len; i++) {
            if (params[i].type == VIR_TYPED_PARAM_STRING)
                ret->nparams--;
        }
    }
4397 4398 4399 4400

cleanup:
    if (rv < 0)
         virNetMessageSaveError(rerr);
4401
    virTypedParamsFree(params, args->ncpus * args->nparams);
4402 4403 4404 4405 4406
    if (dom)
        virDomainFree(dom);
    return rv;
}

4407 4408 4409 4410 4411 4412 4413
static int
remoteDispatchDomainGetDiskErrors(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client,
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                  virNetMessageErrorPtr rerr,
                                  remote_domain_get_disk_errors_args *args,
                                  remote_domain_get_disk_errors_ret *ret)
4414 4415 4416 4417
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
4418
    int len = 0;
4419 4420 4421 4422
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4423
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4424 4425 4426 4427 4428 4429 4430
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
4431 4432
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
4433 4434 4435 4436
        goto cleanup;
    }

    if (args->maxerrors &&
4437
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459
        goto cleanup;

    if ((len = virDomainGetDiskErrors(dom, errors,
                                      args->maxerrors,
                                      args->flags)) < 0)
        goto cleanup;

    ret->nerrors = len;
    if (errors &&
        remoteSerializeDomainDiskErrors(errors, len,
                                        &ret->errors.errors_val,
                                        &ret->errors.errors_len) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    if (errors) {
4460
        size_t i;
4461 4462 4463 4464 4465 4466 4467
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

4468 4469 4470 4471 4472 4473 4474 4475 4476 4477
static int
remoteDispatchDomainListAllSnapshots(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_domain_list_all_snapshots_args *args,
                                     remote_domain_list_all_snapshots_ret *ret)
{
    virDomainSnapshotPtr *snaps = NULL;
    int nsnaps = 0;
4478
    size_t i;
4479 4480 4481 4482 4483
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

    if (!priv->conn) {
4484
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if ((nsnaps = virDomainListAllSnapshots(dom,
                                            args->need_results ? &snaps : NULL,
                                            args->flags)) < 0)
        goto cleanup;

4496 4497 4498 4499 4500 4501 4502
    if (nsnaps > REMOTE_DOMAIN_SNAPSHOT_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domain snapshots '%d' for limit '%d'"),
                       nsnaps, REMOTE_DOMAIN_SNAPSHOT_LIST_MAX);
        goto cleanup;
    }

4503
    if (snaps && nsnaps) {
4504
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542
            goto cleanup;

        ret->snapshots.snapshots_len = nsnaps;

        for (i = 0; i < nsnaps; i++)
            make_nonnull_domain_snapshot(ret->snapshots.snapshots_val + i,
                                         snaps[i]);
    } else {
        ret->snapshots.snapshots_len = 0;
        ret->snapshots.snapshots_val = NULL;
    }

    ret->ret = nsnaps;
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    if (snaps) {
        for (i = 0; i < nsnaps; i++)
            virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }
    return rv;
}

static int
remoteDispatchDomainSnapshotListAllChildren(virNetServerPtr server ATTRIBUTE_UNUSED,
                                            virNetServerClientPtr client,
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr,
                                            remote_domain_snapshot_list_all_children_args *args,
                                            remote_domain_snapshot_list_all_children_ret *ret)
{
    virDomainSnapshotPtr *snaps = NULL;
    int nsnaps = 0;
4543
    size_t i;
4544 4545 4546 4547 4548 4549
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;
    virDomainSnapshotPtr snapshot = NULL;

    if (!priv->conn) {
4550
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->snapshot.dom)))
        goto cleanup;

    if (!(snapshot = get_nonnull_domain_snapshot(dom, args->snapshot)))
        goto cleanup;

    if ((nsnaps = virDomainSnapshotListAllChildren(snapshot,
                                                   args->need_results ? &snaps : NULL,
                                                   args->flags)) < 0)
        goto cleanup;

4565 4566 4567 4568 4569 4570 4571
    if (nsnaps > REMOTE_DOMAIN_SNAPSHOT_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domain snapshots '%d' for limit '%d'"),
                       nsnaps, REMOTE_DOMAIN_SNAPSHOT_LIST_MAX);
        goto cleanup;
    }

4572
    if (snaps && nsnaps) {
4573
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603
            goto cleanup;

        ret->snapshots.snapshots_len = nsnaps;

        for (i = 0; i < nsnaps; i++)
            make_nonnull_domain_snapshot(ret->snapshots.snapshots_val + i,
                                         snaps[i]);
    } else {
        ret->snapshots.snapshots_len = 0;
        ret->snapshots.snapshots_val = NULL;
    }

    ret->ret = nsnaps;
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (snapshot)
        virDomainSnapshotFree(snapshot);
    if (dom)
        virDomainFree(dom);
    if (snaps) {
        for (i = 0; i < nsnaps; i++)
            virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }
    return rv;
}

4604 4605 4606 4607 4608 4609 4610 4611 4612 4613
static int
remoteDispatchConnectListAllStoragePools(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_connect_list_all_storage_pools_args *args,
                                         remote_connect_list_all_storage_pools_ret *ret)
{
    virStoragePoolPtr *pools = NULL;
    int npools = 0;
4614
    size_t i;
4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((npools = virConnectListAllStoragePools(priv->conn,
                                                args->need_results ? &pools : NULL,
                                                args->flags)) < 0)
        goto cleanup;

4628 4629 4630 4631 4632 4633 4634
    if (npools > REMOTE_STORAGE_POOL_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many storage pools '%d' for limit '%d'"),
                       npools, REMOTE_STORAGE_POOL_LIST_MAX);
        goto cleanup;
    }

4635
    if (pools && npools) {
4636
        if (VIR_ALLOC_N(ret->pools.pools_val, npools) < 0)
4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662
            goto cleanup;

        ret->pools.pools_len = npools;

        for (i = 0; i < npools; i++)
            make_nonnull_storage_pool(ret->pools.pools_val + i, pools[i]);
    } else {
        ret->pools.pools_len = 0;
        ret->pools.pools_val = NULL;
    }

    ret->ret = npools;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (pools) {
        for (i = 0; i < npools; i++)
            virStoragePoolFree(pools[i]);
        VIR_FREE(pools);
    }
    return rv;
}

4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673
static int
remoteDispatchStoragePoolListAllVolumes(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_storage_pool_list_all_volumes_args *args,
                                        remote_storage_pool_list_all_volumes_ret *ret)
{
    virStorageVolPtr *vols = NULL;
    virStoragePoolPtr pool = NULL;
    int nvols = 0;
4674
    size_t i;
4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(pool = get_nonnull_storage_pool(priv->conn, args->pool)))
        goto cleanup;

    if ((nvols = virStoragePoolListAllVolumes(pool,
                                              args->need_results ? &vols : NULL,
                                              args->flags)) < 0)
        goto cleanup;

4691 4692 4693 4694 4695 4696 4697
    if (nvols > REMOTE_STORAGE_VOL_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many storage volumes '%d' for limit '%d'"),
                       nvols, REMOTE_STORAGE_VOL_LIST_MAX);
        goto cleanup;
    }

4698
    if (vols && nvols) {
4699
        if (VIR_ALLOC_N(ret->vols.vols_val, nvols) < 0)
4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722
            goto cleanup;

        ret->vols.vols_len = nvols;

        for (i = 0; i < nvols; i++)
            make_nonnull_storage_vol(ret->vols.vols_val + i, vols[i]);
    } else {
        ret->vols.vols_len = 0;
        ret->vols.vols_val = NULL;
    }

    ret->ret = nvols;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (vols) {
        for (i = 0; i < nvols; i++)
            virStorageVolFree(vols[i]);
        VIR_FREE(vols);
    }
4723 4724
    if (pool)
        virStoragePoolFree(pool);
4725 4726 4727
    return rv;
}

4728 4729 4730 4731 4732 4733 4734 4735 4736 4737
static int
remoteDispatchConnectListAllNetworks(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_connect_list_all_networks_args *args,
                                     remote_connect_list_all_networks_ret *ret)
{
    virNetworkPtr *nets = NULL;
    int nnets = 0;
4738
    size_t i;
4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((nnets = virConnectListAllNetworks(priv->conn,
                                           args->need_results ? &nets : NULL,
                                           args->flags)) < 0)
        goto cleanup;

4752 4753 4754 4755 4756 4757 4758
    if (nnets > REMOTE_NETWORK_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many networks '%d' for limit '%d'"),
                       nnets, REMOTE_NETWORK_LIST_MAX);
        goto cleanup;
    }

4759
    if (nets && nnets) {
4760
        if (VIR_ALLOC_N(ret->nets.nets_val, nnets) < 0)
4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786
            goto cleanup;

        ret->nets.nets_len = nnets;

        for (i = 0; i < nnets; i++)
            make_nonnull_network(ret->nets.nets_val + i, nets[i]);
    } else {
        ret->nets.nets_len = 0;
        ret->nets.nets_val = NULL;
    }

    ret->ret = nnets;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (nets) {
        for (i = 0; i < nnets; i++)
            virNetworkFree(nets[i]);
        VIR_FREE(nets);
    }
    return rv;
}

4787 4788 4789 4790 4791 4792 4793 4794 4795 4796
static int
remoteDispatchConnectListAllInterfaces(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_connect_list_all_interfaces_args *args,
                                       remote_connect_list_all_interfaces_ret *ret)
{
    virInterfacePtr *ifaces = NULL;
    int nifaces = 0;
4797
    size_t i;
4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((nifaces = virConnectListAllInterfaces(priv->conn,
                                               args->need_results ? &ifaces : NULL,
                                               args->flags)) < 0)
        goto cleanup;

4811 4812 4813 4814 4815 4816 4817
    if (nifaces > REMOTE_INTERFACE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many interfaces '%d' for limit '%d'"),
                       nifaces, REMOTE_INTERFACE_LIST_MAX);
        goto cleanup;
    }

4818
    if (ifaces && nifaces) {
4819
        if (VIR_ALLOC_N(ret->ifaces.ifaces_val, nifaces) < 0)
4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845
            goto cleanup;

        ret->ifaces.ifaces_len = nifaces;

        for (i = 0; i < nifaces; i++)
            make_nonnull_interface(ret->ifaces.ifaces_val + i, ifaces[i]);
    } else {
        ret->ifaces.ifaces_len = 0;
        ret->ifaces.ifaces_val = NULL;
    }

    ret->ret = nifaces;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (ifaces) {
        for (i = 0; i < nifaces; i++)
            virInterfaceFree(ifaces[i]);
        VIR_FREE(ifaces);
    }
    return rv;
}

4846 4847 4848 4849 4850 4851 4852 4853 4854 4855
static int
remoteDispatchConnectListAllNodeDevices(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_connect_list_all_node_devices_args *args,
                                        remote_connect_list_all_node_devices_ret *ret)
{
    virNodeDevicePtr *devices = NULL;
    int ndevices = 0;
4856
    size_t i;
4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((ndevices = virConnectListAllNodeDevices(priv->conn,
                                                 args->need_results ? &devices : NULL,
                                                 args->flags)) < 0)
        goto cleanup;

4870 4871 4872 4873 4874 4875 4876
    if (ndevices > REMOTE_NODE_DEVICE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many node devices '%d' for limit '%d'"),
                       ndevices, REMOTE_NODE_DEVICE_LIST_MAX);
        goto cleanup;
    }

4877
    if (devices && ndevices) {
4878
        if (VIR_ALLOC_N(ret->devices.devices_val, ndevices) < 0)
4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903
            goto cleanup;

        ret->devices.devices_len = ndevices;

        for (i = 0; i < ndevices; i++)
            make_nonnull_node_device(ret->devices.devices_val + i, devices[i]);
    } else {
        ret->devices.devices_len = 0;
        ret->devices.devices_val = NULL;
    }

    ret->ret = ndevices;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (devices) {
        for (i = 0; i < ndevices; i++)
            virNodeDeviceFree(devices[i]);
        VIR_FREE(devices);
    }
    return rv;
}
4904

4905 4906 4907 4908 4909 4910 4911 4912 4913 4914
static int
remoteDispatchConnectListAllNWFilters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_connect_list_all_nwfilters_args *args,
                                      remote_connect_list_all_nwfilters_ret *ret)
{
    virNWFilterPtr *filters = NULL;
    int nfilters = 0;
4915
    size_t i;
4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((nfilters = virConnectListAllNWFilters(priv->conn,
                                               args->need_results ? &filters : NULL,
                                               args->flags)) < 0)
        goto cleanup;

4929 4930 4931 4932 4933 4934 4935
    if (nfilters > REMOTE_NWFILTER_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many network filters '%d' for limit '%d'"),
                       nfilters, REMOTE_NWFILTER_LIST_MAX);
        goto cleanup;
    }

4936
    if (filters && nfilters) {
4937
        if (VIR_ALLOC_N(ret->filters.filters_val, nfilters) < 0)
4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963
            goto cleanup;

        ret->filters.filters_len = nfilters;

        for (i = 0; i < nfilters; i++)
            make_nonnull_nwfilter(ret->filters.filters_val + i, filters[i]);
    } else {
        ret->filters.filters_len = 0;
        ret->filters.filters_val = NULL;
    }

    ret->ret = nfilters;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (filters) {
        for (i = 0; i < nfilters; i++)
            virNWFilterFree(filters[i]);
        VIR_FREE(filters);
    }
    return rv;
}

4964 4965 4966 4967 4968 4969 4970 4971 4972 4973
static int
remoteDispatchConnectListAllSecrets(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_connect_list_all_secrets_args *args,
                                    remote_connect_list_all_secrets_ret *ret)
{
    virSecretPtr *secrets = NULL;
    int nsecrets = 0;
4974
    size_t i;
4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if ((nsecrets = virConnectListAllSecrets(priv->conn,
                                             args->need_results ? &secrets : NULL,
                                             args->flags)) < 0)
        goto cleanup;

4988 4989 4990 4991 4992 4993 4994
    if (nsecrets > REMOTE_SECRET_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many secrets '%d' for limit '%d'"),
                       nsecrets, REMOTE_SECRET_LIST_MAX);
        goto cleanup;
    }

4995
    if (secrets && nsecrets) {
4996
        if (VIR_ALLOC_N(ret->secrets.secrets_val, nsecrets) < 0)
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
            goto cleanup;

        ret->secrets.secrets_len = nsecrets;

        for (i = 0; i < nsecrets; i++)
            make_nonnull_secret(ret->secrets.secrets_val + i, secrets[i]);
    } else {
        ret->secrets.secrets_len = 0;
        ret->secrets.secrets_val = NULL;
    }

    ret->ret = nsecrets;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (secrets) {
        for (i = 0; i < nsecrets; i++)
            virSecretFree(secrets[i]);
        VIR_FREE(secrets);
    }
    return rv;
}

5023 5024 5025 5026 5027 5028 5029 5030 5031
static int
remoteDispatchNodeGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_node_get_memory_parameters_args *args,
                                      remote_node_get_memory_parameters_ret *ret)
{
    virTypedParameterPtr params = NULL;
5032
    int nparams = 0;
5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    flags = args->flags;

5045
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
5046 5047 5048
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
5049
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
5050
        goto cleanup;
5051
    nparams = args->nparams;
5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075

    if (virNodeGetMemoryParameters(priv->conn, params, &nparams, flags) < 0)
        goto cleanup;

    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       args->flags) < 0)
        goto cleanup;

success:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
5076
    virTypedParamsFree(params, nparams);
5077 5078 5079
    return rv;
}

5080 5081 5082 5083 5084 5085 5086 5087 5088
static int
remoteDispatchNodeGetCPUMap(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                            virNetMessageErrorPtr rerr,
                            remote_node_get_cpu_map_args *args,
                            remote_node_get_cpu_map_ret *ret)
{
    unsigned char *cpumap = NULL;
5089
    unsigned int online = 0;
5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102
    unsigned int flags;
    int cpunum;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    flags = args->flags;

5103 5104
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
5105 5106 5107 5108
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
5109
    if (args->need_map) {
5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126
        ret->cpumap.cpumap_len = VIR_CPU_MAPLEN(cpunum);
        ret->cpumap.cpumap_val = (char *) cpumap;
        cpumap = NULL;
    }

    ret->online = online;
    ret->ret = cpunum;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumap);
    return rv;
}

5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159
static int
lxcDispatchDomainOpenNamespace(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                               virNetMessageErrorPtr rerr,
                               lxc_domain_open_namespace_args *args)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *fdlist = NULL;
    int ret;
    virDomainPtr dom = NULL;
    size_t i;

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    ret = virDomainLxcOpenNamespace(dom,
                                    &fdlist,
                                    args->flags);
    if (ret < 0)
        goto cleanup;

    /* We shouldn't have received any from the client,
     * but in case they're playing games with us, prevent
     * a resource leak
     */
5160
    for (i = 0; i < msg->nfds; i++)
5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

    msg->fds = fdlist;
    msg->nfds = ret;

    rv = 1;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
5173 5174
    if (dom)
        virDomainFree(dom);
5175 5176 5177
    return rv;
}

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
static int
remoteDispatchDomainGetJobStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client,
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                virNetMessageErrorPtr rerr,
                                remote_domain_get_job_stats_args *args,
                                remote_domain_get_job_stats_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (virDomainGetJobStats(dom, &ret->type, &params,
                             &nparams, args->flags) < 0)
        goto cleanup;

5205 5206 5207 5208 5209 5210 5211
    if (nparams > REMOTE_DOMAIN_JOB_STATS_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many job stats '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_JOB_STATS_MAX);
        goto cleanup;
    }

5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       0) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virTypedParamsFree(params, nparams);
    if (dom)
        virDomainFree(dom);
    return rv;
}

5229
static int
5230 5231 5232 5233 5234 5235
remoteDispatchDomainMigrateBegin3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_migrate_begin3_params_args *args,
                                        remote_domain_migrate_begin3_params_ret *ret)
5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

5252 5253 5254 5255 5256 5257 5258
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

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
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    if (!(xml = virDomainMigrateBegin3Params(dom, params, nparams,
                                             &cookieout, &cookieoutlen,
                                             args->flags)))
        goto cleanup;

    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;
    ret->xml = xml;

    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

static int
5288 5289 5290 5291 5292 5293
remoteDispatchDomainMigratePrepare3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_prepare3_params_args *args,
                                          remote_domain_migrate_prepare3_params_ret *ret)
5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    char **uri_out;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

5309 5310 5311 5312 5313 5314 5315
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

5316 5317 5318 5319 5320 5321
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    /* Wacky world of XDR ... */
5322
    if (VIR_ALLOC(uri_out) < 0)
5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347
        goto cleanup;

    if (virDomainMigratePrepare3Params(priv->conn, params, nparams,
                                       args->cookie_in.cookie_in_val,
                                       args->cookie_in.cookie_in_len,
                                       &cookieout, &cookieoutlen,
                                       uri_out, args->flags) < 0)
        goto cleanup;

    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;
    ret->uri_out = !*uri_out ? NULL : uri_out;

    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(uri_out);
    }
    return rv;
}

static int
5348 5349 5350 5351 5352 5353
remoteDispatchDomainMigratePrepareTunnel3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client,
                                                virNetMessagePtr msg,
                                                virNetMessageErrorPtr rerr,
                                                remote_domain_migrate_prepare_tunnel3_params_args *args,
                                                remote_domain_migrate_prepare_tunnel3_params_ret *ret)
5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virStreamPtr st = NULL;
    daemonClientStreamPtr stream = NULL;

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

5370 5371 5372 5373 5374 5375 5376
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    if (!(st = virStreamNew(priv->conn, VIR_STREAM_NONBLOCK)) ||
        !(stream = daemonCreateClientStream(client, st, remoteProgram,
                                            &msg->header)))
        goto cleanup;

    if (virDomainMigratePrepareTunnel3Params(priv->conn, st, params, nparams,
                                             args->cookie_in.cookie_in_val,
                                             args->cookie_in.cookie_in_len,
                                             &cookieout, &cookieoutlen,
                                             args->flags) < 0)
        goto cleanup;

    if (daemonAddClientStream(client, stream, false) < 0)
        goto cleanup;

    ret->cookie_out.cookie_out_val = cookieout;
    ret->cookie_out.cookie_out_len = cookieoutlen;
    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
        if (stream) {
            virStreamAbort(st);
            daemonFreeClientStream(client, stream);
        } else {
            virStreamFree(st);
        }
    }
    return rv;
}


static int
5418 5419 5420 5421 5422 5423
remoteDispatchDomainMigratePerform3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_perform3_params_args *args,
                                          remote_domain_migrate_perform3_params_ret *ret)
5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    char *dconnuri;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

5440 5441 5442 5443 5444 5445 5446
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

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

    if (virDomainMigratePerform3Params(dom, dconnuri, params, nparams,
                                       args->cookie_in.cookie_in_val,
                                       args->cookie_in.cookie_in_len,
                                       &cookieout, &cookieoutlen,
                                       args->flags) < 0)
        goto cleanup;

    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;

    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
5480 5481 5482 5483 5484 5485
remoteDispatchDomainMigrateFinish3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_domain_migrate_finish3_params_args *args,
                                         remote_domain_migrate_finish3_params_ret *ret)
5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

5501 5502 5503 5504 5505 5506 5507
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    dom = virDomainMigrateFinish3Params(priv->conn, params, nparams,
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        args->flags, args->cancelled);
    if (!dom)
        goto cleanup;

    make_nonnull_domain(&ret->dom, dom);

    ret->cookie_out.cookie_out_len = cookieoutlen;
    ret->cookie_out.cookie_out_val = cookieout;

    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
5541 5542 5543 5544 5545
remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_confirm3_params_args *args)
5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

5559 5560 5561 5562 5563 5564 5565
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    if (virDomainMigrateConfirm3Params(dom, params, nparams,
                                       args->cookie_in.cookie_in_val,
                                       args->cookie_in.cookie_in_len,
                                       args->flags, args->cancelled) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643
static int
remoteDispatchConnectGetCPUModelNames(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_connect_get_cpu_model_names_args *args,
                                      remote_connect_get_cpu_model_names_ret *ret)
{
    int len, rv = -1;
    char **models = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    len = virConnectGetCPUModelNames(priv->conn, args->arch,
                                     args->need_results ? &models : NULL,
                                     args->flags);
    if (len < 0)
        goto cleanup;

    if (len > REMOTE_CONNECT_CPU_MODELS_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many CPU models '%d' for limit '%d'"),
                       len, REMOTE_CONNECT_CPU_MODELS_MAX);
        goto cleanup;
    }

    if (len && models) {
        ret->models.models_val = models;
        ret->models.models_len = len;
        models = NULL;
    } else {
        ret->models.models_val = NULL;
        ret->models.models_len = 0;
    }

    ret->ret = len;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virStringFreeList(models);
    return rv;
}


5644 5645 5646 5647 5648 5649 5650
static int
remoteDispatchDomainCreateXMLWithFiles(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_create_xml_with_files_args *args,
                                       remote_domain_create_xml_with_files_ret *ret)
5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693
{
    int rv = -1;
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *files = NULL;
    unsigned int nfiles = 0;
    size_t i;

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (VIR_ALLOC_N(files, msg->nfds) < 0)
        goto cleanup;
    for (i = 0; i < msg->nfds; i++) {
        if ((files[i] = virNetMessageDupFD(msg, i)) < 0)
            goto cleanup;
        nfiles++;
    }

    if ((dom = virDomainCreateXMLWithFiles(priv->conn, args->xml_desc,
                                           nfiles, files,
                                           args->flags)) == NULL)
        goto cleanup;

    make_nonnull_domain(&ret->dom, dom);
    rv = 0;

cleanup:
    for (i = 0; i < nfiles; i++) {
        VIR_FORCE_CLOSE(files[i]);
    }
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


5694 5695 5696 5697 5698 5699
static int remoteDispatchDomainCreateWithFiles(virNetServerPtr server ATTRIBUTE_UNUSED,
                                               virNetServerClientPtr client,
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr,
                                               remote_domain_create_with_files_args *args,
                                               remote_domain_create_with_files_ret *ret)
5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745
{
    int rv = -1;
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *files = NULL;
    unsigned int nfiles = 0;
    size_t i;

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (VIR_ALLOC_N(files, msg->nfds) < 0)
        goto cleanup;
    for (i = 0; i < msg->nfds; i++) {
        if ((files[i] = virNetMessageDupFD(msg, i)) < 0)
            goto cleanup;
        nfiles++;
    }

    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

    if (virDomainCreateWithFiles(dom,
                                 nfiles, files,
                                 args->flags) < 0)
        goto cleanup;

    make_nonnull_domain(&ret->dom, dom);
    rv = 0;

cleanup:
    for (i = 0; i < nfiles; i++) {
        VIR_FORCE_CLOSE(files[i]);
    }
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


5746 5747
static int
remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5748
                                             virNetServerClientPtr client,
5749 5750 5751
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                             remote_connect_network_event_register_any_args *args,
5752
                                             remote_connect_network_event_register_any_ret *ret)
5753 5754 5755
{
    int callbackID;
    int rv = -1;
5756 5757
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
5758 5759
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
5760
    virNetworkPtr net = NULL;
5761 5762 5763 5764 5765 5766 5767 5768

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

5769 5770 5771 5772
    if (args->net &&
        !(net = get_nonnull_network(priv->conn, *args->net)))
        goto cleanup;

5773 5774 5775
    if (args->eventID >= VIR_NETWORK_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network event ID %d"), args->eventID);
5776 5777 5778
        goto cleanup;
    }

5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793
    /* If we call register first, we could append a complete callback
     * to our array, but on OOM append failure, we'd have to then hope
     * deregister works to undo our register.  So instead we append an
     * incomplete callback to our array, then register, then fix up
     * our callback; but since VIR_APPEND_ELEMENT clears 'callback' on
     * success, we use 'ref' to save a copy of the pointer.  */
    if (VIR_ALLOC(callback) < 0)
        goto cleanup;
    callback->client = client;
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->networkEventCallbacks,
                           priv->nnetworkEventCallbacks,
                           callback) < 0)
5794 5795 5796
        goto cleanup;

    if ((callbackID = virConnectNetworkEventRegisterAny(priv->conn,
5797
                                                        net,
5798 5799
                                                        args->eventID,
                                                        networkEventCallbacks[args->eventID],
5800 5801 5802 5803 5804
                                                        ref,
                                                        remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->networkEventCallbacks,
                     priv->nnetworkEventCallbacks, 1);
        callback = ref;
5805
        goto cleanup;
5806
    }
5807

5808
    ref->callbackID = callbackID;
5809
    ret->callbackID = callbackID;
5810 5811 5812 5813

    rv = 0;

cleanup:
5814
    VIR_FREE(callback);
5815 5816
    if (rv < 0)
        virNetMessageSaveError(rerr);
5817 5818
    if (net)
        virNetworkFree(net);
5819 5820 5821 5822 5823 5824 5825
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5826
                                               virNetServerClientPtr client,
5827 5828
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
5829
                                               remote_connect_network_event_deregister_any_args *args)
5830 5831
{
    int rv = -1;
5832
    size_t i;
5833 5834 5835 5836 5837 5838 5839 5840 5841 5842
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

5843
    for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
5844
        if (priv->networkEventCallbacks[i]->callbackID == args->callbackID)
5845 5846
            break;
    }
5847
    if (i == priv->nnetworkEventCallbacks) {
5848
        virReportError(VIR_ERR_INTERNAL_ERROR,
5849 5850
                       _("network event callback %d not registered"),
                       args->callbackID);
5851 5852 5853
        goto cleanup;
    }

5854
    if (virConnectNetworkEventDeregisterAny(priv->conn, args->callbackID) < 0)
5855 5856
        goto cleanup;

5857 5858
    VIR_DELETE_ELEMENT(priv->networkEventCallbacks, i,
                       priv->nnetworkEventCallbacks);
5859 5860 5861 5862 5863 5864 5865 5866 5867 5868

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}

5869

5870 5871 5872 5873 5874 5875 5876 5877 5878
/*----- 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
5879
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
5880 5881
{
    virDomainPtr dom;
5882
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
5883 5884 5885 5886 5887 5888 5889 5890
    /* 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
5891
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
5892
{
5893
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
5894 5895
}

D
Daniel Veillard 已提交
5896
static virInterfacePtr
5897
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
5898
{
5899
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
5900 5901
}

5902
static virStoragePoolPtr
5903
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
5904
{
5905 5906
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
5907 5908 5909
}

static virStorageVolPtr
5910
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
5911 5912
{
    virStorageVolPtr ret;
5913 5914
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
5915 5916 5917
    return ret;
}

5918
static virSecretPtr
5919
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
5920
{
5921
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
5922 5923
}

5924
static virNWFilterPtr
5925
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
5926
{
5927
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
5928 5929
}

C
Chris Lalancette 已提交
5930
static virDomainSnapshotPtr
5931
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
5932
{
5933
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
5934 5935
}

5936 5937
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
5938
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
5939 5940
{
    dom_dst->id = dom_src->id;
5941
    ignore_value(VIR_STRDUP_QUIET(dom_dst->name, dom_src->name));
5942
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
5943 5944 5945
}

static void
5946
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
5947
{
5948
    ignore_value(VIR_STRDUP_QUIET(net_dst->name, net_src->name));
5949
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
5950 5951
}

D
Daniel Veillard 已提交
5952
static void
5953 5954
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
5955
{
5956 5957
    ignore_value(VIR_STRDUP_QUIET(interface_dst->name, interface_src->name));
    ignore_value(VIR_STRDUP_QUIET(interface_dst->mac, interface_src->mac));
D
Daniel Veillard 已提交
5958 5959
}

5960
static void
5961
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
5962
{
5963
    ignore_value(VIR_STRDUP_QUIET(pool_dst->name, pool_src->name));
5964
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
5965 5966 5967
}

static void
5968
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
5969
{
5970 5971 5972
    ignore_value(VIR_STRDUP_QUIET(vol_dst->pool, vol_src->pool));
    ignore_value(VIR_STRDUP_QUIET(vol_dst->name, vol_src->name));
    ignore_value(VIR_STRDUP_QUIET(vol_dst->key, vol_src->key));
5973
}
5974 5975

static void
5976
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
5977
{
5978
    ignore_value(VIR_STRDUP_QUIET(dev_dst->name, dev_src->name));
5979
}
5980 5981

static void
5982
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
5983
{
5984
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
5985
    secret_dst->usageType = secret_src->usageType;
5986
    ignore_value(VIR_STRDUP_QUIET(secret_dst->usageID, secret_src->usageID));
5987
}
5988 5989

static void
5990
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
5991
{
5992
    ignore_value(VIR_STRDUP_QUIET(nwfilter_dst->name, nwfilter_src->name));
5993
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
5994
}
C
Chris Lalancette 已提交
5995 5996

static void
5997
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
5998
{
5999
    ignore_value(VIR_STRDUP_QUIET(snapshot_dst->name, snapshot_src->name));
6000
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
6001
}
6002 6003 6004 6005 6006 6007 6008 6009

static int
remoteSerializeDomainDiskErrors(virDomainDiskErrorPtr errors,
                                int nerrors,
                                remote_domain_disk_error **ret_errors_val,
                                u_int *ret_errors_len)
{
    remote_domain_disk_error *val = NULL;
6010
    size_t i = 0;
6011

6012
    if (VIR_ALLOC_N(val, nerrors) < 0)
6013
        goto error;
6014 6015

    for (i = 0; i < nerrors; i++) {
6016 6017
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
6018 6019 6020 6021 6022 6023 6024 6025
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

6026
error:
6027
    if (val) {
6028
        size_t j;
6029 6030 6031 6032 6033 6034
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}