remote.c 223.0 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007-2015 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
#include "remote.h"
28
#include "libvirtd.h"
29 30
#include "libvirt_internal.h"
#include "datatypes.h"
31
#include "viralloc.h"
32
#include "virlog.h"
C
Chris Lalancette 已提交
33
#include "stream.h"
34
#include "viruuid.h"
35
#include "vircommand.h"
36
#include "intprops.h"
37
#include "virnetserverservice.h"
J
Jim Fehlig 已提交
38
#include "virnetserver.h"
39
#include "virfile.h"
40
#include "virtypedparam.h"
41
#include "virdbus.h"
42
#include "virprocess.h"
43 44
#include "remote_protocol.h"
#include "qemu_protocol.h"
45
#include "lxc_protocol.h"
46
#include "virstring.h"
47
#include "object_event.h"
48 49
#include "domain_conf.h"
#include "network_conf.h"
50
#include "virprobe.h"
51
#include "viraccessapicheck.h"
52
#include "viraccessapicheckqemu.h"
53
#include "virpolkit.h"
54
#include "virthreadjob.h"
55 56

#define VIR_FROM_THIS VIR_FROM_RPC
57

58 59
VIR_LOG_INIT("daemon.remote");

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 116
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
                               remote_typed_param **ret_params_val,
                               u_int *ret_params_len,
                               unsigned int flags);

117 118 119 120 121 122
static int
remoteSerializeDomainDiskErrors(virDomainDiskErrorPtr errors,
                                int nerrors,
                                remote_domain_disk_error **ret_errors_val,
                                u_int *ret_errors_len);

123 124
#include "remote_dispatch.h"
#include "qemu_dispatch.h"
125
#include "lxc_dispatch.h"
C
Chris Lalancette 已提交
126 127


128 129
/* Prototypes */
static void
130
remoteDispatchObjectEventSend(virNetServerClientPtr client,
131
                              virNetServerProgramPtr program,
132 133 134
                              int procnr,
                              xdrproc_t proc,
                              void *data);
135

136 137 138 139 140 141
static void
remoteEventCallbackFree(void *opaque)
{
    VIR_FREE(opaque);
}

142 143 144 145 146 147 148 149 150 151 152 153

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.  */
154
    memset(&def, 0, sizeof(def));
155 156 157 158 159 160 161 162 163
    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);

164
 cleanup:
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
    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);

191
 cleanup:
192 193 194 195 196 197
    ignore_value(virIdentitySetCurrent(NULL));
    virObjectUnref(identity);
    return ret;
}


198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static bool
remoteRelayDomainQemuMonitorEventCheckACL(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 = virConnectDomainQemuMonitorEventRegisterCheckACL(conn, &def);

218
 cleanup:
219 220 221 222 223 224
    ignore_value(virIdentitySetCurrent(NULL));
    virObjectUnref(identity);
    return ret;
}


225 226 227 228 229 230
static int
remoteRelayDomainEventLifecycle(virConnectPtr conn,
                                virDomainPtr dom,
                                int event,
                                int detail,
                                void *opaque)
231
{
232
    daemonClientEventCallbackPtr callback = opaque;
233
    remote_domain_event_lifecycle_msg data;
234

235 236
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
237 238
        return -1;

239 240
    VIR_DEBUG("Relaying domain lifecycle event %d %d, callback %d legacy %d",
              event, detail, callback->callbackID, callback->legacy);
241

242
    /* build return data */
243
    memset(&data, 0, sizeof(data));
244
    make_nonnull_domain(&data.dom, dom);
245 246
    data.event = event;
    data.detail = detail;
247

248 249 250 251 252 253 254 255 256 257 258 259 260 261
    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);
    }
262

263 264
    return 0;
}
265

266 267 268 269
static int
remoteRelayDomainEventReboot(virConnectPtr conn,
                             virDomainPtr dom,
                             void *opaque)
270
{
271
    daemonClientEventCallbackPtr callback = opaque;
272 273
    remote_domain_event_reboot_msg data;

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

278 279
    VIR_DEBUG("Relaying domain reboot event %s %d, callback %d legacy %d",
              dom->name, dom->id, callback->callbackID, callback->legacy);
280 281

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

285 286 287 288 289 290 291 292 293 294 295 296
    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);
    }
297 298 299 300

    return 0;
}

301

302 303 304 305 306
static int
remoteRelayDomainEventRTCChange(virConnectPtr conn,
                                virDomainPtr dom,
                                long long offset,
                                void *opaque)
307
{
308
    daemonClientEventCallbackPtr callback = opaque;
309 310
    remote_domain_event_rtc_change_msg data;

311 312
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
313 314
        return -1;

315 316 317
    VIR_DEBUG("Relaying domain rtc change event %s %d %lld, callback %d legacy %d",
              dom->name, dom->id, offset,
              callback->callbackID, callback->legacy);
318 319

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

324 325 326 327 328 329 330 331 332 333 334 335
    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);
    }
336 337 338 339 340

    return 0;
}


341 342 343 344 345
static int
remoteRelayDomainEventWatchdog(virConnectPtr conn,
                               virDomainPtr dom,
                               int action,
                               void *opaque)
346
{
347
    daemonClientEventCallbackPtr callback = opaque;
348 349
    remote_domain_event_watchdog_msg data;

350 351
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
352 353
        return -1;

354 355
    VIR_DEBUG("Relaying domain watchdog event %s %d %d, callback %d",
              dom->name, dom->id, action, callback->callbackID);
356 357

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

362 363 364 365 366 367 368 369 370 371 372 373
    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);
    }
374 375 376 377 378

    return 0;
}


379 380 381 382 383 384 385
static int
remoteRelayDomainEventIOError(virConnectPtr conn,
                              virDomainPtr dom,
                              const char *srcPath,
                              const char *devAlias,
                              int action,
                              void *opaque)
386
{
387
    daemonClientEventCallbackPtr callback = opaque;
388 389
    remote_domain_event_io_error_msg data;

390 391
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
392 393
        return -1;

394 395 396
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d, callback %d",
              dom->name, dom->id, srcPath, devAlias, action,
              callback->callbackID);
397 398

    /* build return data */
399
    memset(&data, 0, sizeof(data));
400 401 402
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
403
    make_nonnull_domain(&data.dom, dom);
404 405
    data.action = action;

406 407 408 409 410 411 412 413 414 415 416 417
    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);
    }
418 419

    return 0;
420
 error:
E
Eric Blake 已提交
421 422
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
423
    return -1;
424 425 426
}


427 428 429 430 431 432 433 434
static int
remoteRelayDomainEventIOErrorReason(virConnectPtr conn,
                                    virDomainPtr dom,
                                    const char *srcPath,
                                    const char *devAlias,
                                    int action,
                                    const char *reason,
                                    void *opaque)
435
{
436
    daemonClientEventCallbackPtr callback = opaque;
437 438
    remote_domain_event_io_error_reason_msg data;

439 440
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
441 442
        return -1;

443 444 445
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s, callback %d",
              dom->name, dom->id, srcPath, devAlias, action, reason,
              callback->callbackID);
446 447

    /* build return data */
448
    memset(&data, 0, sizeof(data));
449 450 451 452
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0 ||
        VIR_STRDUP(data.reason, reason) < 0)
        goto error;
453
    data.action = action;
454 455

    make_nonnull_domain(&data.dom, dom);
456

457 458 459 460 461 462 463 464 465 466 467 468
    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);
    }
469 470

    return 0;
471

472
 error:
E
Eric Blake 已提交
473 474 475
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
    VIR_FREE(data.reason);
476
    return -1;
477 478 479
}


480 481 482 483 484 485 486 487 488
static int
remoteRelayDomainEventGraphics(virConnectPtr conn,
                               virDomainPtr dom,
                               int phase,
                               virDomainEventGraphicsAddressPtr local,
                               virDomainEventGraphicsAddressPtr remote,
                               const char *authScheme,
                               virDomainEventGraphicsSubjectPtr subject,
                               void *opaque)
489
{
490
    daemonClientEventCallbackPtr callback = opaque;
491
    remote_domain_event_graphics_msg data;
492
    size_t i;
493

494 495
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
496 497
        return -1;

498 499
    VIR_DEBUG("Relaying domain graphics event %s %d %d - %d %s %s  - %d %s %s - %s, callback %d",
              dom->name, dom->id, phase,
500 501
              local->family, local->service, local->node,
              remote->family, remote->service, remote->node,
502
              authScheme, callback->callbackID);
503

504
    VIR_DEBUG("Subject %d", subject->nidentity);
505
    for (i = 0; i < subject->nidentity; i++)
506
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
507 508

    /* build return data */
509
    memset(&data, 0, sizeof(data));
510 511 512
    data.phase = phase;
    data.local.family = local->family;
    data.remote.family = remote->family;
513 514 515 516 517 518
    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;
519 520

    data.subject.subject_len = subject->nidentity;
521
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0)
522
        goto error;
523

524
    for (i = 0; i < data.subject.subject_len; i++) {
525 526 527
        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;
528
    }
529
    make_nonnull_domain(&data.dom, dom);
530

531 532 533 534 535 536 537 538 539 540 541 542
    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);
    }
543 544

    return 0;
545

546
 error:
E
Eric Blake 已提交
547 548 549 550 551
    VIR_FREE(data.authScheme);
    VIR_FREE(data.local.node);
    VIR_FREE(data.local.service);
    VIR_FREE(data.remote.node);
    VIR_FREE(data.remote.service);
552
    if (data.subject.subject_val != NULL) {
553
        for (i = 0; i < data.subject.subject_len; i++) {
E
Eric Blake 已提交
554 555
            VIR_FREE(data.subject.subject_val[i].type);
            VIR_FREE(data.subject.subject_val[i].name);
556
        }
E
Eric Blake 已提交
557
        VIR_FREE(data.subject.subject_val);
558 559
    }
    return -1;
560 561
}

562 563 564 565 566 567 568
static int
remoteRelayDomainEventBlockJob(virConnectPtr conn,
                               virDomainPtr dom,
                               const char *path,
                               int type,
                               int status,
                               void *opaque)
569
{
570
    daemonClientEventCallbackPtr callback = opaque;
571 572
    remote_domain_event_block_job_msg data;

573 574
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
575 576
        return -1;

577 578
    VIR_DEBUG("Relaying domain block job event %s %d %s %i, %i, callback %d",
              dom->name, dom->id, path, type, status, callback->callbackID);
579 580

    /* build return data */
581
    memset(&data, 0, sizeof(data));
582 583
    if (VIR_STRDUP(data.path, path) < 0)
        goto error;
584 585
    data.type = type;
    data.status = status;
586
    make_nonnull_domain(&data.dom, dom);
587

588 589 590 591 592 593 594 595 596 597 598 599
    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);
    }
600 601

    return 0;
602
 error:
E
Eric Blake 已提交
603
    VIR_FREE(data.path);
604
    return -1;
605 606
}

607

608 609 610 611
static int
remoteRelayDomainEventControlError(virConnectPtr conn,
                                   virDomainPtr dom,
                                   void *opaque)
612
{
613
    daemonClientEventCallbackPtr callback = opaque;
614 615
    remote_domain_event_control_error_msg data;

616 617
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
618 619
        return -1;

620 621
    VIR_DEBUG("Relaying domain control error %s %d, callback %d",
              dom->name, dom->id, callback->callbackID);
622 623

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

627 628 629 630 631 632 633 634 635 636 637 638
    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);
    }
639 640 641 642 643

    return 0;
}


644 645 646 647 648 649 650 651
static int
remoteRelayDomainEventDiskChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *oldSrcPath,
                                 const char *newSrcPath,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
652
{
653
    daemonClientEventCallbackPtr callback = opaque;
654 655 656
    remote_domain_event_disk_change_msg data;
    char **oldSrcPath_p = NULL, **newSrcPath_p = NULL;

657 658
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
659 660
        return -1;

661 662 663
    VIR_DEBUG("Relaying domain %s %d disk change %s %s %s %d, callback %d",
              dom->name, dom->id, oldSrcPath, newSrcPath, devAlias, reason,
              callback->callbackID);
664 665

    /* build return data */
666
    memset(&data, 0, sizeof(data));
667 668
    if (oldSrcPath &&
        ((VIR_ALLOC(oldSrcPath_p) < 0) ||
669
         VIR_STRDUP(*oldSrcPath_p, oldSrcPath) < 0))
670
        goto error;
671 672 673

    if (newSrcPath &&
        ((VIR_ALLOC(newSrcPath_p) < 0) ||
674
         VIR_STRDUP(*newSrcPath_p, newSrcPath) < 0))
675
        goto error;
676 677 678

    data.oldSrcPath = oldSrcPath_p;
    data.newSrcPath = newSrcPath_p;
679 680
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
681 682 683 684
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

685 686 687 688 689 690 691 692 693 694 695 696
    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);
    }
697 698 699

    return 0;

700
 error:
M
Michal Privoznik 已提交
701 702
    VIR_FREE(oldSrcPath_p);
    VIR_FREE(newSrcPath_p);
703 704 705 706
    return -1;
}


707 708 709 710 711 712 713
static int
remoteRelayDomainEventTrayChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
{
714
    daemonClientEventCallbackPtr callback = opaque;
715 716
    remote_domain_event_tray_change_msg data;

717 718
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
719 720
        return -1;

721 722
    VIR_DEBUG("Relaying domain %s %d tray change devAlias: %s reason: %d, callback %d",
              dom->name, dom->id, devAlias, reason, callback->callbackID);
723 724

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

727
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
728 729 730 731 732
        return -1;
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

733 734 735 736 737 738 739 740 741 742 743 744
    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);
    }
745 746 747 748

    return 0;
}

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

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

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

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

769 770 771 772 773 774
    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 已提交
775
                                                          reason, data };
776 777 778 779 780

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMWAKEUP,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmwakeup_msg, &msg);
    }
O
Osier Yang 已提交
781 782 783 784

    return 0;
}

785 786 787
static int
remoteRelayDomainEventPMSuspend(virConnectPtr conn,
                                virDomainPtr dom,
E
Eric Blake 已提交
788
                                int reason,
789 790
                                void *opaque)
{
791
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
792 793
    remote_domain_event_pmsuspend_msg data;

794 795
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
796 797
        return -1;

798 799
    VIR_DEBUG("Relaying domain %s %d system pmsuspend, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
800 801

    /* build return data */
802
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
803 804
    make_nonnull_domain(&data.dom, dom);

805 806 807 808 809 810
    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 已提交
811
                                                           reason, data };
812 813 814 815 816

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_msg, &msg);
    }
O
Osier Yang 已提交
817 818 819 820

    return 0;
}

821
static int
822
remoteRelayDomainEventBalloonChange(virConnectPtr conn,
823 824 825 826
                                    virDomainPtr dom,
                                    unsigned long long actual,
                                    void *opaque)
{
827
    daemonClientEventCallbackPtr callback = opaque;
828 829
    remote_domain_event_balloon_change_msg data;

830 831
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
832 833
        return -1;

834 835
    VIR_DEBUG("Relaying domain balloon change event %s %d %lld, callback %d",
              dom->name, dom->id, actual, callback->callbackID);
836 837 838 839 840 841

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

842 843 844 845 846 847 848 849 850 851 852 853
    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);
    }
854 855 856 857 858

    return 0;
}


859 860 861
static int
remoteRelayDomainEventPMSuspendDisk(virConnectPtr conn,
                                    virDomainPtr dom,
E
Eric Blake 已提交
862
                                    int reason,
863 864
                                    void *opaque)
{
865
    daemonClientEventCallbackPtr callback = opaque;
866 867
    remote_domain_event_pmsuspend_disk_msg data;

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

872 873
    VIR_DEBUG("Relaying domain %s %d system pmsuspend-disk, callback %d",
              dom->name, dom->id, callback->callbackID);
874 875 876 877 878

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

879 880 881 882 883 884
    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 已提交
885
                                                                reason, data };
886 887 888 889 890

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND_DISK,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_disk_msg, &msg);
    }
891 892 893 894

    return 0;
}

895
static int
896
remoteRelayDomainEventDeviceRemoved(virConnectPtr conn,
897 898 899 900
                                    virDomainPtr dom,
                                    const char *devAlias,
                                    void *opaque)
{
901
    daemonClientEventCallbackPtr callback = opaque;
902 903
    remote_domain_event_device_removed_msg data;

904 905
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
906 907
        return -1;

908 909
    VIR_DEBUG("Relaying domain device removed event %s %d %s, callback %d",
              dom->name, dom->id, devAlias, callback->callbackID);
910 911 912 913 914 915 916 917 918

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

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

    make_nonnull_domain(&data.dom, dom);

919 920 921 922 923 924 925 926 927 928 929 930 931 932
    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);
    }
933 934 935 936

    return 0;
}

937

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
static int
remoteRelayDomainEventBlockJob2(virConnectPtr conn,
                                virDomainPtr dom,
                                const char *dst,
                                int type,
                                int status,
                                void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_block_job_2_msg data;

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

    VIR_DEBUG("Relaying domain block job 2 event %s %d %s %i, %i, callback %d",
              dom->name, dom->id, dst, type, status, callback->callbackID);

    /* build return data */
    memset(&data, 0, sizeof(data));
    data.callbackID = callback->callbackID;
    if (VIR_STRDUP(data.dst, dst) < 0)
        goto error;
    data.type = type;
    data.status = status;
    make_nonnull_domain(&data.dom, dom);

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB_2,
                                  (xdrproc_t)xdr_remote_domain_event_block_job_2_msg, &data);

    return 0;
 error:
    VIR_FREE(data.dst);
    return -1;
}


976 977 978 979 980 981 982 983 984 985 986 987 988 989
static int
remoteRelayDomainEventTunable(virConnectPtr conn,
                              virDomainPtr dom,
                              virTypedParameterPtr params,
                              int nparams,
                              void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_tunable_msg data;

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

990 991
    VIR_DEBUG("Relaying domain tunable event %s %d, callback %d, params %p %d",
              dom->name, dom->id, callback->callbackID, params, nparams);
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

    /* build return data */
    memset(&data, 0, sizeof(data));
    data.callbackID = callback->callbackID;
    make_nonnull_domain(&data.dom, dom);

    if (remoteSerializeTypedParameters(params, nparams,
                                       &data.params.params_val,
                                       &data.params.params_len,
                                       VIR_TYPED_PARAM_STRING_OKAY) < 0)
        return -1;

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_TUNABLE,
                                  (xdrproc_t)xdr_remote_domain_event_callback_tunable_msg,
                                  &data);

    return 0;
}


1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
static int
remoteRelayDomainEventAgentLifecycle(virConnectPtr conn,
                                     virDomainPtr dom,
                                     int state,
                                     int reason,
                                     void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_agent_lifecycle_msg data;

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

    VIR_DEBUG("Relaying domain agent lifecycle event %s %d, callback %d, "
              " state %d, reason %d",
              dom->name, dom->id, callback->callbackID, state, reason);

    /* build return data */
    memset(&data, 0, sizeof(data));
    data.callbackID = callback->callbackID;
    make_nonnull_domain(&data.dom, dom);

    data.state = state;
    data.reason = reason;

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_AGENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_domain_event_callback_agent_lifecycle_msg,
                                  &data);

    return 0;
}


1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
static int
remoteRelayDomainEventDeviceAdded(virConnectPtr conn,
                                  virDomainPtr dom,
                                  const char *devAlias,
                                  void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_device_added_msg data;

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

    VIR_DEBUG("Relaying domain device added event %s %d %s, callback %d",
              dom->name, dom->id, devAlias, callback->callbackID);

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

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

    make_nonnull_domain(&data.dom, dom);
E
Eric Blake 已提交
1071
    data.callbackID = callback->callbackID;
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DEVICE_ADDED,
                                  (xdrproc_t)xdr_remote_domain_event_callback_device_added_msg,
                                  &data);

    return 0;
}


1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
static int
remoteRelayDomainEventMigrationIteration(virConnectPtr conn,
                                         virDomainPtr dom,
                                         int iteration,
                                         void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_migration_iteration_msg data;

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

    VIR_DEBUG("Relaying domain migration pass event %s %d, "
              "callback %d, iteration %d",
              dom->name, dom->id, callback->callbackID, iteration);

    /* build return data */
    memset(&data, 0, sizeof(data));
    data.callbackID = callback->callbackID;
    make_nonnull_domain(&data.dom, dom);

    data.iteration = iteration;

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_MIGRATION_ITERATION,
                                  (xdrproc_t)xdr_remote_domain_event_callback_migration_iteration_msg,
                                  &data);

    return 0;
}
1113 1114


1115
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
1116
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
1117
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
1118
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
1119
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
1120
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
1121
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
1122
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
1123
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
1124
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
1125
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
1126
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
1127
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
1128
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
1129
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
1130
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
1131
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
1132
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob2),
1133
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTunable),
1134
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventAgentLifecycle),
1135
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceAdded),
1136
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventMigrationIteration),
1137 1138 1139 1140
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

1141
static int
1142
remoteRelayNetworkEventLifecycle(virConnectPtr conn,
1143 1144 1145 1146
                                 virNetworkPtr net,
                                 int event,
                                 int detail,
                                 void *opaque)
1147
{
1148
    daemonClientEventCallbackPtr callback = opaque;
1149 1150
    remote_network_event_lifecycle_msg data;

1151 1152
    if (callback->callbackID < 0 ||
        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
1153 1154
        return -1;

1155 1156
    VIR_DEBUG("Relaying network lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);
1157 1158 1159 1160

    /* build return data */
    memset(&data, 0, sizeof(data));
    make_nonnull_network(&data.net, net);
1161
    data.callbackID = callback->callbackID;
1162 1163 1164
    data.event = event;
    data.detail = detail;

1165
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
                                  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);

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
static void
remoteRelayDomainQemuMonitorEvent(virConnectPtr conn,
                                  virDomainPtr dom,
                                  const char *event,
                                  long long seconds,
                                  unsigned int micros,
                                  const char *details,
                                  void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    qemu_domain_monitor_event_msg data;
    char **details_p = NULL;

    if (callback->callbackID < 0 ||
        !remoteRelayDomainQemuMonitorEventCheckACL(callback->client, conn,
                                                   dom))
        return;

    VIR_DEBUG("Relaying qemu monitor event %s %s, callback %d",
              event, details, callback->callbackID);

    /* build return data */
    memset(&data, 0, sizeof(data));
    data.callbackID = callback->callbackID;
    if (VIR_STRDUP(data.event, event) < 0)
        goto error;
    data.seconds = seconds;
    data.micros = micros;
    if (details &&
        ((VIR_ALLOC(details_p) < 0) ||
         VIR_STRDUP(*details_p, details) < 0))
        goto error;
    data.details = details_p;
    make_nonnull_domain(&data.dom, dom);

    remoteDispatchObjectEventSend(callback->client, qemuProgram,
                                  QEMU_PROC_DOMAIN_MONITOR_EVENT,
                                  (xdrproc_t)xdr_qemu_domain_monitor_event_msg,
                                  &data);
    return;

1219
 error:
1220 1221 1222 1223
    VIR_FREE(data.event);
    VIR_FREE(details_p);
}

1224 1225 1226 1227 1228 1229 1230
/*
 * 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
 */
1231
void remoteClientFreeFunc(void *data)
1232 1233 1234 1235 1236
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
1237
        virIdentityPtr sysident = virIdentityGetSystem();
1238
        size_t i;
1239

1240 1241
        virIdentitySetCurrent(sysident);

1242 1243 1244 1245 1246
        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;
1247
            }
1248 1249 1250 1251 1252
            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");
1253
        }
1254
        VIR_FREE(priv->domainEventCallbacks);
1255

1256 1257 1258 1259 1260
        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;
1261
            }
1262 1263 1264 1265 1266 1267
            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");
1268
        }
1269
        VIR_FREE(priv->networkEventCallbacks);
1270

1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
        for (i = 0; i < priv->nqemuEventCallbacks; i++) {
            int callbackID = priv->qemuEventCallbacks[i]->callbackID;
            if (callbackID < 0) {
                VIR_WARN("unexpected incomplete qemu monitor callback %zu", i);
                continue;
            }
            VIR_DEBUG("Deregistering remote qemu monitor event relay %d",
                      callbackID);
            priv->qemuEventCallbacks[i]->callbackID = -1;
            if (virConnectDomainQemuMonitorEventDeregister(priv->conn,
                                                           callbackID) < 0)
                VIR_WARN("unexpected qemu monitor event deregister failure");
        }
        VIR_FREE(priv->qemuEventCallbacks);

1286
        virConnectClose(priv->conn);
1287 1288 1289

        virIdentitySetCurrent(NULL);
        virObjectUnref(sysident);
1290 1291 1292 1293 1294 1295
    }

    VIR_FREE(priv);
}


1296 1297 1298 1299 1300 1301 1302
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

1303

1304 1305
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
1306 1307 1308
{
    struct daemonClientPrivate *priv;

1309
    if (VIR_ALLOC(priv) < 0)
1310
        return NULL;
1311 1312 1313

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
1314
        virReportSystemError(errno, "%s", _("unable to init mutex"));
1315
        return NULL;
1316 1317
    }

1318
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
1319
    return priv;
1320 1321
}

1322 1323 1324
/*----- Functions. -----*/

static int
1325
remoteDispatchConnectOpen(virNetServerPtr server ATTRIBUTE_UNUSED,
1326 1327 1328 1329
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
1330 1331
{
    const char *name;
1332
    unsigned int flags;
1333
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
1334
    int rv = -1;
1335

1336 1337 1338 1339
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
1340
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
1341 1342 1343
        goto cleanup;
    }

1344 1345 1346 1347 1348 1349
    name = args->name ? *args->name : NULL;

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

1353
    priv->conn =
1354
        flags & VIR_CONNECT_RO
1355 1356
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
1357

1358
    if (priv->conn == NULL)
1359 1360 1361
        goto cleanup;

    rv = 0;
1362

1363
 cleanup:
1364
    if (rv < 0)
1365 1366
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
1367
    return rv;
1368 1369 1370 1371
}


static int
1372 1373 1374 1375
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
1376
{
1377
    virNetServerClientDelayedClose(client);
1378
    return 0;
1379 1380
}

1381

1382
static int
1383 1384
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1385
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1386
                                     virNetMessageErrorPtr rerr,
1387 1388
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
1389
{
1390
    virDomainPtr dom = NULL;
1391 1392
    char *type;
    int nparams;
1393
    int rv = -1;
1394 1395
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1396

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

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

1405
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
1406
        goto cleanup;
1407 1408 1409

    ret->type = type;
    ret->nparams = nparams;
1410 1411
    rv = 0;

1412
 cleanup:
1413
    if (rv < 0)
1414
        virNetMessageSaveError(rerr);
1415
    virObjectUnref(dom);
1416
    return rv;
1417 1418
}

1419 1420
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
1421 1422 1423
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
1424
                               remote_typed_param **ret_params_val,
1425 1426
                               u_int *ret_params_len,
                               unsigned int flags)
1427
{
1428 1429
    size_t i;
    size_t j;
1430 1431 1432 1433
    int rv = -1;
    remote_typed_param *val;

    *ret_params_len = nparams;
1434
    if (VIR_ALLOC_N(val, nparams) < 0)
1435 1436
        goto cleanup;

1437
    for (i = 0, j = 0; i < nparams; ++i) {
1438 1439 1440 1441 1442
        /* 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)) {
1443 1444 1445 1446
            --*ret_params_len;
            continue;
        }

1447
        /* remoteDispatchClientRequest will free this: */
1448
        if (VIR_STRDUP(val[j].field, params[i].field) < 0)
1449
            goto cleanup;
1450
        val[j].value.type = params[i].type;
1451
        switch (params[i].type) {
1452
        case VIR_TYPED_PARAM_INT:
1453
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
1454 1455
            break;
        case VIR_TYPED_PARAM_UINT:
1456
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
1457 1458
            break;
        case VIR_TYPED_PARAM_LLONG:
1459
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
1460 1461
            break;
        case VIR_TYPED_PARAM_ULLONG:
1462
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
1463 1464
            break;
        case VIR_TYPED_PARAM_DOUBLE:
1465
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
1466 1467
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
1468 1469 1470
            val[j].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
        case VIR_TYPED_PARAM_STRING:
1471
            if (VIR_STRDUP(val[j].value.remote_typed_param_value_u.s, params[i].value.s) < 0)
1472
                goto cleanup;
1473 1474
            break;
        default:
1475 1476
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
1477 1478
            goto cleanup;
        }
1479
        j++;
1480 1481 1482 1483 1484 1485
    }

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

1486
 cleanup:
1487
    if (val) {
1488
        for (i = 0; i < nparams; i++) {
1489
            VIR_FREE(val[i].field);
1490
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
1491 1492
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
1493 1494 1495 1496 1497 1498 1499
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
1500 1501
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
1502 1503 1504
                                 int limit,
                                 int *nparams)
{
1505
    size_t i = 0;
1506 1507 1508 1509
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
1510
    if (limit && args_params_len > limit) {
1511
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1512 1513
        goto cleanup;
    }
1514
    if (VIR_ALLOC_N(params, args_params_len) < 0)
1515 1516 1517 1518 1519 1520 1521 1522
        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) {
1523 1524 1525
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Parameter %s too big for destination"),
                           args_params_val[i].field);
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
            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;
1554
        case VIR_TYPED_PARAM_STRING:
1555 1556
            if (VIR_STRDUP(params[i].value.s,
                           args_params_val[i].value.remote_typed_param_value_u.s) < 0)
1557 1558
                goto cleanup;
            break;
1559
        default:
1560 1561
            virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                           params[i].type);
1562 1563 1564 1565 1566 1567
            goto cleanup;
        }
    }

    rv = 0;

1568
 cleanup:
1569
    if (rv < 0) {
1570 1571
        virTypedParamsFree(params, i);
        params = NULL;
1572
    }
1573 1574 1575
    return params;
}

1576
static int
1577 1578
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
1579
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
1580
                                           virNetMessageErrorPtr rerr,
1581 1582
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
1583
{
1584
    virDomainPtr dom = NULL;
1585
    virTypedParameterPtr params = NULL;
1586
    int nparams = 0;
1587
    int rv = -1;
1588 1589
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1590

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

1596
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1597
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1598
        goto cleanup;
1599
    }
1600
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1601
        goto cleanup;
1602
    nparams = args->nparams;
1603

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

1607
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1608
        goto cleanup;
1609

1610
    if (remoteSerializeTypedParameters(params, nparams,
1611
                                       &ret->params.params_val,
1612 1613
                                       &ret->params.params_len,
                                       0) < 0)
1614 1615 1616 1617
        goto cleanup;

    rv = 0;

1618
 cleanup:
1619
    if (rv < 0)
1620
        virNetMessageSaveError(rerr);
1621
    virTypedParamsFree(params, nparams);
1622
    virObjectUnref(dom);
1623 1624 1625
    return rv;
}

1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
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;
1636
    size_t i;
1637 1638 1639 1640
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1641
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1642 1643 1644 1645 1646 1647 1648 1649
        goto cleanup;
    }

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

1650 1651 1652 1653 1654 1655 1656
    if (ndomains > REMOTE_DOMAIN_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domains '%d' for limit '%d'"),
                       ndomains, REMOTE_DOMAIN_LIST_MAX);
        goto cleanup;
    }

1657
    if (doms && ndomains) {
1658
        if (VIR_ALLOC_N(ret->domains.domains_val, ndomains) < 0)
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
            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;

1674
 cleanup:
1675 1676
    if (rv < 0)
        virNetMessageSaveError(rerr);
1677
    if (doms && ndomains > 0)
1678
        for (i = 0; i < ndomains; i++)
1679
            virObjectUnref(doms[i]);
1680
    VIR_FREE(doms);
1681 1682 1683
    return rv;
}

1684
static int
1685 1686
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1687
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1688
                                                virNetMessageErrorPtr rerr,
1689 1690 1691 1692 1693
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1694
    int nparams = 0;
1695
    int rv = -1;
1696 1697
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1698

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

1704
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1705
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1706 1707
        goto cleanup;
    }
1708
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1709
        goto cleanup;
1710
    nparams = args->nparams;
1711

1712
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1713 1714 1715 1716 1717 1718 1719
        goto cleanup;

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

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

1725
    rv = 0;
1726

1727
 cleanup:
1728
    if (rv < 0)
1729
        virNetMessageSaveError(rerr);
1730
    virTypedParamsFree(params, nparams);
1731
    virObjectUnref(dom);
1732
    return rv;
1733 1734
}

1735
static int
1736 1737
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1738
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1739
                                virNetMessageErrorPtr rerr,
1740 1741
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1742
{
1743
    virDomainPtr dom = NULL;
1744
    virDomainMemoryStatPtr stats = NULL;
1745 1746
    int nr_stats;
    size_t i;
1747
    int rv = -1;
1748 1749
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1750

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

1756
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1757 1758
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1759
        goto cleanup;
1760 1761
    }

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

    /* Allocate stats array for making dispatch call */
1766
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
1767
        goto cleanup;
1768

1769
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1770
    if (nr_stats < 0)
1771
        goto cleanup;
1772 1773

    /* Allocate return buffer */
1774
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
1775
        goto cleanup;
1776 1777 1778 1779 1780 1781 1782

    /* 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;
1783 1784
    rv = 0;

1785
 cleanup:
1786
    if (rv < 0)
1787
        virNetMessageSaveError(rerr);
1788
    virObjectUnref(dom);
1789
    VIR_FREE(stats);
1790
    return rv;
1791 1792
}

1793
static int
1794 1795
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1796
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1797
                              virNetMessageErrorPtr rerr,
1798 1799
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1800
{
1801
    virDomainPtr dom = NULL;
1802 1803 1804 1805
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1806
    int rv = -1;
1807 1808
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1809

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

1815
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1816
        goto cleanup;
1817 1818 1819 1820 1821 1822
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1823 1824
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1825
        goto cleanup;
1826 1827 1828
    }

    ret->buffer.buffer_len = size;
1829
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1830
        goto cleanup;
1831

1832
    if (virDomainBlockPeek(dom, path, offset, size,
1833
                           ret->buffer.buffer_val, flags) < 0)
1834
        goto cleanup;
1835

1836 1837
    rv = 0;

1838
 cleanup:
1839
    if (rv < 0) {
1840
        virNetMessageSaveError(rerr);
1841 1842
        VIR_FREE(ret->buffer.buffer_val);
    }
1843
    virObjectUnref(dom);
1844
    return rv;
1845 1846
}

1847 1848 1849
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1850
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1851 1852 1853 1854 1855 1856 1857
                                    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;
1858
    int nparams = 0;
1859 1860 1861 1862 1863 1864
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1865
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1866 1867 1868 1869 1870 1871 1872
        goto cleanup;
    }

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

1873
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1874
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1875 1876
        goto cleanup;
    }
1877
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1878
        goto cleanup;
1879
    nparams = args->nparams;
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894

    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,
1895 1896
                                       &ret->params.params_len,
                                       args->flags) < 0)
1897 1898
        goto cleanup;

1899
 success:
1900 1901
    rv = 0;

1902
 cleanup:
1903
    if (rv < 0)
1904
        virNetMessageSaveError(rerr);
1905
    virTypedParamsFree(params, nparams);
1906
    virObjectUnref(dom);
1907 1908 1909
    return rv;
}

R
Richard W.M. Jones 已提交
1910
static int
1911 1912
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1913
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1914
                               virNetMessageErrorPtr rerr,
1915 1916
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1917
{
1918
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1919 1920 1921
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1922
    int rv = -1;
1923 1924
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1925

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

1931
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1932
        goto cleanup;
R
Richard W.M. Jones 已提交
1933 1934 1935 1936 1937
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1938 1939
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1940
        goto cleanup;
R
Richard W.M. Jones 已提交
1941 1942 1943
    }

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

1947
    if (virDomainMemoryPeek(dom, offset, size,
1948
                            ret->buffer.buffer_val, flags) < 0)
1949
        goto cleanup;
R
Richard W.M. Jones 已提交
1950

1951 1952
    rv = 0;

1953
 cleanup:
1954
    if (rv < 0) {
1955
        virNetMessageSaveError(rerr);
1956 1957
        VIR_FREE(ret->buffer.buffer_val);
    }
1958
    virObjectUnref(dom);
1959
    return rv;
R
Richard W.M. Jones 已提交
1960 1961
}

1962
static int
1963 1964
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1965
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1966
                                     virNetMessageErrorPtr rerr,
1967 1968
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1969
{
1970 1971
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1972
    int rv = -1;
1973 1974
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1975

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

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

1984
    if (VIR_ALLOC(seclabel) < 0)
1985 1986 1987 1988 1989 1990
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
1991
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
1992
        goto cleanup;
1993 1994
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1995

1996 1997
    rv = 0;

1998
 cleanup:
1999
    if (rv < 0)
2000
        virNetMessageSaveError(rerr);
2001
    virObjectUnref(dom);
2002
    VIR_FREE(seclabel);
2003
    return rv;
2004 2005
}

M
Marcelo Cerri 已提交
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
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;
2016 2017
    int len, rv = -1;
    size_t i;
M
Marcelo Cerri 已提交
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
    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;
    }

2036
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
2037 2038 2039 2040 2041
        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];
2042
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0)
M
Marcelo Cerri 已提交
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
            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;

2054
 done:
M
Marcelo Cerri 已提交
2055 2056
    rv = 0;

2057
 cleanup:
M
Marcelo Cerri 已提交
2058 2059
    if (rv < 0)
        virNetMessageSaveError(rerr);
2060
    virObjectUnref(dom);
M
Marcelo Cerri 已提交
2061 2062 2063 2064
    VIR_FREE(seclabels);
    return rv;
}

2065
static int
2066 2067
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2068
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2069
                                   virNetMessageErrorPtr rerr,
2070
                                   remote_node_get_security_model_ret *ret)
2071
{
2072
    virSecurityModel secmodel;
2073
    int rv = -1;
2074 2075
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2076

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

2082
    memset(&secmodel, 0, sizeof(secmodel));
2083
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
2084 2085 2086
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
2087
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
2088 2089 2090 2091
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
2092
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
2093
        goto cleanup;
2094
    strcpy(ret->doi.doi_val, secmodel.doi);
2095

2096 2097
    rv = 0;

2098
 cleanup:
2099
    if (rv < 0)
2100
        virNetMessageSaveError(rerr);
2101
    return rv;
2102 2103
}

2104
static int
2105 2106
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2107
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2108
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
2109 2110
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
2111 2112 2113 2114 2115
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
2116 2117
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2118

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

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

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
2128
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
2129 2130 2131 2132 2133
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
2134
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2135 2136 2137 2138 2139 2140
        goto cleanup;
    }

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

E
Eric Blake 已提交
2143
    if ((num = virDomainGetVcpuPinInfo(dom,
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
                                       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;
2157 2158 2159 2160
    cpumaps = NULL;

    rv = 0;

2161
 cleanup:
2162 2163 2164
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumaps);
2165
    virObjectUnref(dom);
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
    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;

2197
 cleanup:
2198 2199
    if (rv < 0)
        virNetMessageSaveError(rerr);
2200
    virObjectUnref(dom);
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
    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)
2231
        goto cleanup;
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241

    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;
2242 2243 2244 2245
    cpumaps = NULL;

    rv = 0;

2246
 cleanup:
2247
    if (rv < 0)
2248
        virNetMessageSaveError(rerr);
2249
    VIR_FREE(cpumaps);
2250
    virObjectUnref(dom);
2251 2252 2253
    return rv;
}

2254
static int
2255 2256
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
2257
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
2258
                             virNetMessageErrorPtr rerr,
2259 2260
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
2261
{
2262
    virDomainPtr dom = NULL;
2263 2264
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
2265 2266
    int info_len;
    size_t i;
2267
    int rv = -1;
2268 2269
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2270

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

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

2279
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
2280
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
2281
        goto cleanup;
2282
    }
2283

2284 2285
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
2286
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2287 2288 2289 2290 2291
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
2292
        goto cleanup;
2293 2294
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
2295
        goto cleanup;
2296 2297 2298 2299 2300 2301 2302 2303 2304

    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)
2305
        goto cleanup;
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322

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

2324
 cleanup:
2325
    if (rv < 0) {
2326
        virNetMessageSaveError(rerr);
2327 2328 2329 2330
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
2331
    virObjectUnref(dom);
2332
    return rv;
2333 2334
}

2335
static int
2336 2337 2338 2339 2340 2341
remoteDispatchDomainGetIOThreadInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_get_iothread_info_args *args,
                                    remote_domain_get_iothread_info_ret *ret)
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainIOThreadInfoPtr *info = NULL;
    virDomainPtr dom = NULL;
    remote_domain_iothread_info *dst;
    int ninfo = 0;

    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;

2359
    if ((ninfo = virDomainGetIOThreadInfo(dom, &info, args->flags)) < 0)
2360 2361
        goto cleanup;

2362
    if (ninfo > REMOTE_IOTHREAD_INFO_MAX) {
2363 2364
        virReportError(VIR_ERR_RPC,
                       _("Too many IOThreads in info: %d for limit %d"),
2365
                       ninfo, REMOTE_IOTHREAD_INFO_MAX);
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
        goto cleanup;
    }

    if (ninfo) {
        if (VIR_ALLOC_N(ret->info.info_val, ninfo) < 0)
            goto cleanup;

        ret->info.info_len = ninfo;

        for (i = 0; i < ninfo; i++) {
            dst = &ret->info.info_val[i];
            dst->iothread_id = info[i]->iothread_id;

            /* No need to allocate/copy the cpumap if we make the reasonable
             * assumption that unsigned char and char are the same size.
             */
            dst->cpumap.cpumap_len = info[i]->cpumaplen;
            dst->cpumap.cpumap_val = (char *)info[i]->cpumap;
            info[i]->cpumap = NULL;
        }
    } else {
        ret->info.info_len = 0;
        ret->info.info_val = NULL;
    }

    ret->ret = ninfo;

    rv = 0;

 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virObjectUnref(dom);
    if (ninfo >= 0)
        for (i = 0; i < ninfo; i++)
2401
            virDomainIOThreadInfoFree(info[i]);
2402 2403 2404 2405 2406
    VIR_FREE(info);

    return rv;
}

2407
static int
2408 2409
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2410
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2411
                                   virNetMessageErrorPtr rerr,
2412 2413
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
2414
{
2415 2416 2417 2418 2419
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2420
    int rv = -1;
2421 2422
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2423

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

2429 2430 2431 2432
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
2433
    if (VIR_ALLOC(uri_out) < 0)
2434
        goto cleanup;
2435

2436
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
2437 2438
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
2439
        goto cleanup;
2440

2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
    /* 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;
    }
2452

2453
    rv = 0;
2454

2455
 cleanup:
2456
    if (rv < 0)
2457
        virNetMessageSaveError(rerr);
2458
    VIR_FREE(uri_out);
2459
    return rv;
2460 2461
}

2462
static int
2463 2464
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2465
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2466
                                    virNetMessageErrorPtr rerr,
2467 2468
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
2469
{
2470 2471 2472 2473 2474
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2475
    int rv = -1;
2476 2477
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2478

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

2484 2485
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
2486

2487
    /* Wacky world of XDR ... */
2488
    if (VIR_ALLOC(uri_out) < 0)
2489
        goto cleanup;
2490

2491
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
2492 2493 2494
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
2495
        goto cleanup;
2496

2497 2498 2499 2500 2501 2502
    /* 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;
2503

2504 2505
    rv = 0;

2506
 cleanup:
2507
    if (rv < 0) {
2508
        virNetMessageSaveError(rerr);
2509 2510
        VIR_FREE(uri_out);
    }
2511
    return rv;
2512 2513
}

C
Chris Lalancette 已提交
2514
static int
2515 2516
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
2517
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
2518 2519 2520
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
2521
{
2522
    virDomainPtr dom = NULL;
2523
    virTypedParameterPtr params = NULL;
2524
    int nparams = 0;
2525
    unsigned int flags;
2526
    int rv = -1;
2527 2528
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2529

2530
    if (!priv->conn) {
2531
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2532
        goto cleanup;
2533
    }
C
Chris Lalancette 已提交
2534

2535
    flags = args->flags;
C
Chris Lalancette 已提交
2536

2537
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2538
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2539 2540
        goto cleanup;
    }
2541
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2542
        goto cleanup;
2543
    nparams = args->nparams;
C
Chris Lalancette 已提交
2544

2545
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2546
        goto cleanup;
C
Chris Lalancette 已提交
2547

2548
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
2549
        goto cleanup;
C
Chris Lalancette 已提交
2550

2551 2552 2553 2554 2555 2556
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
2557 2558
    }

2559
    if (remoteSerializeTypedParameters(params, nparams,
2560
                                       &ret->params.params_val,
2561 2562
                                       &ret->params.params_len,
                                       args->flags) < 0)
2563
        goto cleanup;
2564

2565
 success:
2566 2567
    rv = 0;

2568
 cleanup:
2569
    if (rv < 0)
2570
        virNetMessageSaveError(rerr);
2571
    virTypedParamsFree(params, nparams);
2572
    virObjectUnref(dom);
2573
    return rv;
2574 2575
}

2576 2577 2578 2579 2580 2581 2582 2583 2584 2585
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;
2586
    int nparams = 0;
2587 2588 2589 2590 2591 2592
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2593
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2594 2595 2596 2597 2598
        goto cleanup;
    }

    flags = args->flags;

2599
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
2600
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2601 2602
        goto cleanup;
    }
2603
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2604
        goto cleanup;
2605
    nparams = args->nparams;
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626

    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;

2627
 success:
2628 2629
    rv = 0;

2630
 cleanup:
2631 2632
    if (rv < 0)
        virNetMessageSaveError(rerr);
2633
    virTypedParamsFree(params, nparams);
2634
    virObjectUnref(dom);
2635 2636 2637
    return rv;
}

2638
static int
2639 2640
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
2641
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2642 2643 2644
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
2645
{
2646
    virDomainPtr dom = NULL;
2647
    virTypedParameterPtr params = NULL;
2648
    int nparams = 0;
2649
    unsigned int flags;
2650
    int rv = -1;
2651 2652
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2653

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

2659 2660
    flags = args->flags;

2661
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2662
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2663
        goto cleanup;
2664
    }
2665
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2666
        goto cleanup;
2667
    nparams = args->nparams;
2668

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

2672
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2673
        goto cleanup;
2674

2675 2676 2677 2678 2679 2680 2681 2682
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2683
    if (remoteSerializeTypedParameters(params, nparams,
2684
                                       &ret->params.params_val,
2685 2686
                                       &ret->params.params_len,
                                       args->flags) < 0)
2687
        goto cleanup;
2688

2689
 success:
2690
    rv = 0;
2691

2692
 cleanup:
2693
    if (rv < 0)
2694
        virNetMessageSaveError(rerr);
2695
    virTypedParamsFree(params, nparams);
2696
    virObjectUnref(dom);
2697
    return rv;
2698 2699
}

2700
static int
2701 2702
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2703
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2704 2705 2706
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2707
{
2708
    virNodeCPUStatsPtr params = NULL;
2709
    size_t i;
2710
    int cpuNum = args->cpuNum;
2711
    int nparams = 0;
2712 2713
    unsigned int flags;
    int rv = -1;
2714 2715
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2716

2717
    if (!priv->conn) {
2718
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2719 2720 2721 2722 2723
        goto cleanup;
    }

    flags = args->flags;

2724
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
2725
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2726 2727
        goto cleanup;
    }
2728
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2729
        goto cleanup;
2730
    nparams = args->nparams;
2731

2732
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745
        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)
2746
        goto cleanup;
2747 2748 2749

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2750 2751
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2752 2753 2754 2755

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

2756
 success:
2757 2758
    rv = 0;

2759
 cleanup:
2760
    if (rv < 0) {
2761
        virNetMessageSaveError(rerr);
2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772
        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
2773 2774
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2775
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2776 2777 2778
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2779
{
2780
    virNodeMemoryStatsPtr params = NULL;
2781
    size_t i;
2782
    int cellNum = args->cellNum;
2783
    int nparams = 0;
2784 2785
    unsigned int flags;
    int rv = -1;
2786 2787
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2788

2789
    if (!priv->conn) {
2790
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2791 2792 2793 2794 2795
        goto cleanup;
    }

    flags = args->flags;

2796
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
2797
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2798 2799
        goto cleanup;
    }
2800
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2801
        goto cleanup;
2802
    nparams = args->nparams;
2803

2804
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
        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)
2818
        goto cleanup;
2819 2820 2821

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2822 2823
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2824 2825 2826 2827

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

2828
 success:
2829 2830
    rv = 0;

2831
 cleanup:
2832
    if (rv < 0) {
2833
        virNetMessageSaveError(rerr);
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843
        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;
}

2844 2845 2846
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2847
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858
                                    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) {
2859
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876
        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;

2877
 cleanup:
2878 2879
    if (rv < 0)
        virNetMessageSaveError(rerr);
2880
    virObjectUnref(dom);
2881 2882 2883
    return rv;
}

2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894
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;
2895
    int nparams = 0;
2896 2897 2898 2899
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

2904
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
2905
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2906 2907 2908
        goto cleanup;
    }

2909
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2910
        goto cleanup;
2911
    nparams = args->nparams;
2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934

    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;

2935
 success:
2936 2937
    rv = 0;

2938
 cleanup:
2939 2940
    if (rv < 0)
        virNetMessageSaveError(rerr);
2941
    virTypedParamsFree(params, nparams);
2942
    virObjectUnref(dom);
2943 2944
    return rv;
}
2945

D
Daniel Veillard 已提交
2946 2947
/*-------------------------------------------------------------*/

2948
static int
2949
remoteDispatchAuthList(virNetServerPtr server,
2950
                       virNetServerClientPtr client,
2951
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2952
                       virNetMessageErrorPtr rerr,
2953
                       remote_auth_list_ret *ret)
2954
{
2955
    int rv = -1;
2956 2957
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2958
    gid_t callerGid;
2959
    pid_t callerPid;
2960
    unsigned long long timestamp;
2961 2962 2963 2964 2965 2966

    /* 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) {
2967
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2968
                                              &callerPid, &timestamp) < 0) {
2969 2970 2971 2972
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2973 2974
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
2975
                            (long long) callerPid, (int) callerUid) < 0)
J
Jim Fehlig 已提交
2976 2977
                goto cleanup;
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
2978
            virNetServerClientSetAuth(client, 0);
2979
            virNetServerTrackCompletedAuth(server);
2980
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
2981
            VIR_FREE(ident);
2982 2983
        }
    }
2984

2985
    ret->types.types_len = 1;
2986
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
2987
        goto cleanup;
2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001

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

3003 3004
    rv = 0;

3005
 cleanup:
3006
    if (rv < 0)
3007
        virNetMessageSaveError(rerr);
3008
    return rv;
3009 3010 3011
}


3012
#ifdef WITH_SASL
3013 3014
/*
 * Initializes the SASL session in prepare for authentication
3015
 * and gives the client a list of allowed mechanisms to choose
3016 3017
 */
static int
3018 3019
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
3020
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3021
                           virNetMessageErrorPtr rerr,
3022
                           remote_auth_sasl_init_ret *ret)
3023
{
3024 3025 3026
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3027

3028
    virMutexLock(&priv->lock);
3029

3030 3031 3032
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
3033
        VIR_ERROR(_("client tried invalid SASL init request"));
3034
        goto authfail;
3035 3036
    }

3037 3038 3039 3040 3041
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
3042
        goto authfail;
3043

3044
# if WITH_GNUTLS
3045
    /* Inform SASL that we've got an external SSF layer from TLS */
3046 3047 3048 3049
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
3050
            goto authfail;
3051 3052 3053 3054 3055

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
3056
            goto authfail;
3057
    }
3058
# endif
3059

3060
    if (virNetServerClientIsSecure(client))
3061
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
3062 3063
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
3064
        /* Plain TCP, better get an SSF layer */
3065 3066 3067 3068
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
3069

3070
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
3071
        goto authfail;
3072
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
3073

3074 3075
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
3076
    return 0;
3077

3078
 authfail:
3079
    virResetLastError();
3080 3081
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3082
    virNetMessageSaveError(rerr);
3083 3084 3085
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3086
    virObjectUnref(sasl);
3087
    virMutexUnlock(&priv->lock);
3088
    return -1;
3089 3090
}

3091
/*
3092 3093
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
3094
static int
3095 3096
remoteSASLFinish(virNetServerPtr server,
                 virNetServerClientPtr client)
3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111
{
    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;
        }
3112
    }
3113 3114

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
3115
        return -2;
3116

3117 3118
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
3119

3120
    virNetServerClientSetAuth(client, 0);
3121
    virNetServerTrackCompletedAuth(server);
3122
    virNetServerClientSetSASLSession(client, priv->sasl);
3123

3124
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
3125

3126 3127 3128
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3129

3130
    virObjectUnref(priv->sasl);
3131
    priv->sasl = NULL;
3132

3133
    return 0;
3134

3135
 error:
3136 3137
    return -1;
}
3138

3139 3140 3141 3142
/*
 * This starts the SASL authentication negotiation.
 */
static int
3143
remoteDispatchAuthSaslStart(virNetServerPtr server,
3144
                            virNetServerClientPtr client,
3145
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3146
                            virNetMessageErrorPtr rerr,
3147 3148
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
3149 3150
{
    const char *serverout;
3151
    size_t serveroutlen;
3152
    int err;
3153 3154 3155
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3156
    const char *identity;
3157

3158
    virMutexLock(&priv->lock);
3159

3160 3161 3162
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
3163
        VIR_ERROR(_("client tried invalid SASL start request"));
3164
        goto authfail;
3165 3166
    }

3167 3168
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
3169 3170 3171 3172 3173 3174 3175 3176 3177
    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)
3178
        goto authfail;
3179

3180
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3181
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
3182
        goto authfail;
3183 3184 3185 3186
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3187 3188
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3189 3190 3191 3192 3193 3194 3195
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3196
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3197
    if (err == VIR_NET_SASL_CONTINUE) {
3198 3199
        ret->complete = 0;
    } else {
3200
        /* Check username whitelist ACL */
3201
        if ((err = remoteSASLFinish(server, client)) < 0) {
3202 3203 3204 3205 3206
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3207

3208 3209 3210
        ret->complete = 1;
    }

3211
    virMutexUnlock(&priv->lock);
3212
    return 0;
3213

3214
 authfail:
3215 3216 3217
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3218 3219
    goto error;

3220
 authdeny:
3221
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3222 3223 3224
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3225 3226
    goto error;

3227
 error:
3228
    virObjectUnref(priv->sasl);
3229 3230
    priv->sasl = NULL;
    virResetLastError();
3231 3232
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3233 3234 3235
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3236
    return -1;
3237 3238 3239 3240
}


static int
3241
remoteDispatchAuthSaslStep(virNetServerPtr server,
3242
                           virNetServerClientPtr client,
3243
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3244
                           virNetMessageErrorPtr rerr,
3245 3246
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
3247 3248
{
    const char *serverout;
3249
    size_t serveroutlen;
3250
    int err;
3251 3252 3253
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3254
    const char *identity;
3255

3256 3257 3258 3259 3260
    virMutexLock(&priv->lock);

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

3265
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
3266
              args->data.data_len, args->nil);
3267 3268 3269 3270 3271 3272 3273 3274
    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)
3275
        goto authfail;
3276 3277

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3278
        VIR_ERROR(_("sasl step reply data too long %d"),
3279
                  (int)serveroutlen);
3280
        goto authfail;
3281 3282 3283 3284
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3285 3286
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3287 3288 3289 3290 3291 3292 3293
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3294
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3295
    if (err == VIR_NET_SASL_CONTINUE) {
3296 3297
        ret->complete = 0;
    } else {
3298
        /* Check username whitelist ACL */
3299
        if ((err = remoteSASLFinish(server, client)) < 0) {
3300 3301 3302 3303 3304
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3305

3306 3307 3308
        ret->complete = 1;
    }

3309
    virMutexUnlock(&priv->lock);
3310
    return 0;
3311

3312
 authfail:
3313 3314 3315
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3316 3317
    goto error;

3318
 authdeny:
3319
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3320 3321 3322
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3323 3324
    goto error;

3325
 error:
3326
    virObjectUnref(priv->sasl);
3327 3328
    priv->sasl = NULL;
    virResetLastError();
3329 3330
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3331 3332 3333
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3334 3335
    return -1;
}
3336 3337 3338 3339
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3340
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3341 3342 3343 3344
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3345 3346
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3347 3348 3349 3350 3351 3352
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
3353
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3354 3355 3356 3357 3358
                            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");
3359 3360
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3361 3362 3363 3364 3365 3366
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3367
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3368 3369 3370 3371 3372
                           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");
3373 3374
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3375 3376 3377 3378
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
3379 3380 3381



3382
static int
3383
remoteDispatchAuthPolkit(virNetServerPtr server,
3384
                         virNetServerClientPtr client,
3385
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3386
                         virNetMessageErrorPtr rerr,
3387
                         remote_auth_polkit_ret *ret)
3388
{
3389
    pid_t callerPid = -1;
3390
    gid_t callerGid = -1;
3391
    uid_t callerUid = -1;
3392
    unsigned long long timestamp;
3393
    const char *action;
3394
    char *ident = NULL;
3395 3396
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3397
    int rv;
3398

3399 3400
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
3401 3402 3403
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

3404 3405
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3406
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3407 3408 3409
        goto authfail;
    }

3410
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
3411
                                          &callerPid, &timestamp) < 0) {
3412 3413 3414
        goto authfail;
    }

3415 3416 3417 3418 3419 3420
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

3421 3422
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3423

3424 3425 3426 3427 3428 3429 3430
    rv = virPolkitCheckAuth(action,
                            callerPid,
                            timestamp,
                            callerUid,
                            NULL,
                            true);
    if (rv == -1)
3431
        goto authfail;
3432
    else if (rv == -2)
3433
        goto authdeny;
3434

3435 3436 3437
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3438 3439
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
3440 3441
    ret->complete = 1;

3442
    virNetServerClientSetAuth(client, 0);
3443
    virNetServerTrackCompletedAuth(server);
3444
    virMutexUnlock(&priv->lock);
3445

3446
    return 0;
3447

3448
 error:
3449
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3450
    virMutexUnlock(&priv->lock);
3451 3452
    return -1;

3453
 authfail:
3454 3455 3456
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3457
    goto error;
3458

3459
 authdeny:
3460 3461 3462
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3463
    goto error;
3464 3465
}

3466

3467 3468 3469
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3470

3471
static int
3472 3473
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3474
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3475
                                  virNetMessageErrorPtr rerr,
3476 3477
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3478
{
3479 3480
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3481
    int rv = -1;
3482 3483
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3484

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

3490
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3491 3492
        goto cleanup;

3493 3494 3495 3496 3497 3498 3499
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
3500
        if (VIR_ALLOC(parent_p) < 0)
3501
            goto cleanup;
3502
        if (VIR_STRDUP(*parent_p, parent) < 0) {
3503 3504 3505 3506 3507 3508
            VIR_FREE(parent_p);
            goto cleanup;
        }
        ret->parent = parent_p;
    }

3509 3510
    rv = 0;

3511
 cleanup:
3512
    if (rv < 0)
3513
        virNetMessageSaveError(rerr);
3514
    virObjectUnref(dev);
3515
    return rv;
3516 3517
}

3518 3519 3520 3521 3522

/***************************
 * Register / deregister events
 ***************************/
static int
3523
remoteDispatchConnectDomainEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
3524
                                         virNetServerClientPtr client,
3525 3526 3527
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                         remote_connect_domain_event_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
3528
{
3529
    int callbackID;
3530
    int rv = -1;
3531 3532
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3533 3534
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3535

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

3541 3542
    virMutexLock(&priv->lock);

3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553
    /* 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;
3554
    callback->legacy = true;
3555 3556 3557 3558
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3559
        goto cleanup;
O
Osier Yang 已提交
3560

3561
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3562 3563 3564
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
3565 3566 3567 3568 3569
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3570
        goto cleanup;
3571
    }
O
Osier Yang 已提交
3572

3573
    ref->callbackID = callbackID;
3574

3575 3576
    rv = 0;

3577
 cleanup:
3578
    VIR_FREE(callback);
3579
    if (rv < 0)
3580 3581
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3582
    return rv;
O
Osier Yang 已提交
3583 3584
}

3585
static int
3586
remoteDispatchConnectDomainEventDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
3587
                                           virNetServerClientPtr client,
3588 3589 3590
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                           remote_connect_domain_event_deregister_ret *ret ATTRIBUTE_UNUSED)
3591
{
3592
    int callbackID = -1;
3593
    int rv = -1;
3594
    size_t i;
3595 3596
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3597

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

3603 3604
    virMutexLock(&priv->lock);

3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615
    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);
3616
        goto cleanup;
3617
    }
3618

3619
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3620
        goto cleanup;
3621

3622 3623
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3624

3625 3626
    rv = 0;

3627
 cleanup:
3628
    if (rv < 0)
3629 3630
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3631 3632 3633 3634
    return rv;
}

static void
3635
remoteDispatchObjectEventSend(virNetServerClientPtr client,
3636
                              virNetServerProgramPtr program,
3637 3638 3639 3640
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3641
    virNetMessagePtr msg;
3642

3643
    if (!(msg = virNetMessageNew(false)))
3644
        goto cleanup;
3645

3646 3647 3648 3649 3650 3651
    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;
3652

3653
    if (virNetMessageEncodeHeader(msg) < 0)
3654 3655
        goto cleanup;

3656 3657
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3658

3659 3660
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3661

3662
    xdr_free(proc, data);
3663 3664
    return;

3665
 cleanup:
3666
    virNetMessageFree(msg);
3667
    xdr_free(proc, data);
3668 3669
}

3670
static int
3671 3672
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3673
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3674
                             virNetMessageErrorPtr rerr,
3675 3676
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3677
{
3678 3679 3680
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3681
    int rv = -1;
3682 3683
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3684

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

3690
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3691
        goto cleanup;
3692

3693
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3694
        goto cleanup;
3695

3696 3697 3698
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3699 3700
    rv = 0;

3701
 cleanup:
3702
    if (rv < 0)
3703
        virNetMessageSaveError(rerr);
3704
    virObjectUnref(secret);
3705
    return rv;
3706 3707
}

3708
static int
3709 3710
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3711
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3712
                             virNetMessageErrorPtr rerr,
3713 3714 3715 3716 3717
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3718 3719
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3720

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

3726
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3727 3728 3729 3730 3731 3732 3733
        goto cleanup;

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

    rv = 0;

3734
 cleanup:
3735
    if (rv < 0)
3736
        virNetMessageSaveError(rerr);
3737
    virObjectUnref(dom);
3738 3739 3740
    return rv;
}

3741 3742 3743 3744 3745 3746

/* 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.  */
3747
static int
3748
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3749
                                            virNetServerClientPtr client,
3750 3751 3752
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            remote_connect_domain_event_register_any_args *args)
3753 3754
{
    int callbackID;
3755
    int rv = -1;
3756 3757
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3758 3759
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3760

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

3766 3767
    virMutexLock(&priv->lock);

3768 3769 3770 3771
    /* 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 ||
3772
        args->eventID < 0) {
3773 3774
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3775
        goto cleanup;
3776 3777
    }

3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788
    /* 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;
3789
    callback->legacy = true;
3790 3791 3792 3793
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3794
        goto cleanup;
3795

3796
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3797 3798 3799
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3800 3801 3802 3803 3804
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3805
        goto cleanup;
3806
    }
3807

3808
    ref->callbackID = callbackID;
3809

3810 3811
    rv = 0;

3812
 cleanup:
3813
    VIR_FREE(callback);
3814
    if (rv < 0)
3815 3816
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3817
    return rv;
3818 3819 3820
}


3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847
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;

3848
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST || args->eventID < 0) {
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
        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;

3888
 cleanup:
3889 3890 3891
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
3892
    virObjectUnref(dom);
3893 3894 3895 3896 3897
    virMutexUnlock(&priv->lock);
    return rv;
}


3898
static int
3899
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3900
                                              virNetServerClientPtr client,
3901 3902 3903
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              remote_connect_domain_event_deregister_any_args *args)
3904 3905
{
    int callbackID = -1;
3906
    int rv = -1;
3907
    size_t i;
3908 3909
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3910

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

3916 3917
    virMutexLock(&priv->lock);

3918 3919 3920 3921
    /* 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 ||
3922
        args->eventID < 0) {
3923 3924
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3925
        goto cleanup;
3926 3927
    }

3928 3929 3930 3931 3932 3933
    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->eventID == args->eventID) {
            callbackID = priv->domainEventCallbacks[i]->callbackID;
            break;
        }
    }
3934
    if (callbackID < 0) {
3935 3936
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event %d not registered"), args->eventID);
3937
        goto cleanup;
3938 3939
    }

3940
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3941
        goto cleanup;
3942

3943 3944
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3945

3946 3947
    rv = 0;

3948
 cleanup:
3949
    if (rv < 0)
3950 3951
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3952
    return rv;
3953 3954
}

3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993

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;

3994
 cleanup:
3995 3996 3997 3998 3999 4000 4001
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}


C
Chris Lalancette 已提交
4002
static int
4003 4004 4005 4006 4007 4008
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 已提交
4009
{
4010
    virDomainPtr dom = NULL;
4011
    int rv = -1;
4012 4013
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
4014

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

4020
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4021
        goto cleanup;
C
Chris Lalancette 已提交
4022

4023
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
4024
                                    args->flags) < 0)
4025
        goto cleanup;
C
Chris Lalancette 已提交
4026

4027
    rv = 0;
C
Chris Lalancette 已提交
4028

4029
 cleanup:
4030
    if (rv < 0)
4031
        virNetMessageSaveError(rerr);
4032
    virObjectUnref(dom);
4033
    return rv;
C
Chris Lalancette 已提交
4034 4035
}

4036

4037
static int
4038 4039
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
4040
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
4041
                                  virNetMessageErrorPtr rerr,
4042 4043 4044 4045 4046 4047
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
4048
    char *xmlin;
4049 4050 4051
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4052 4053
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4054

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

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

4063
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4064 4065
    dname = args->dname == NULL ? NULL : *args->dname;

4066
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079
                                       &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;

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


static int
4089 4090
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4091
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4092
                                    virNetMessageErrorPtr rerr,
4093 4094 4095 4096 4097 4098 4099 4100 4101
                                    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;
4102 4103
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4104

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

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

    /* Wacky world of XDR ... */
4114
    if (VIR_ALLOC(uri_out) < 0)
4115 4116
        goto cleanup;

4117
    if (virDomainMigratePrepare3(priv->conn,
4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134
                                 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;

4135
 cleanup:
4136
    if (rv < 0) {
4137
        virNetMessageSaveError(rerr);
4138 4139 4140 4141 4142
        VIR_FREE(uri_out);
    }
    return rv;
}

4143

4144
static int
4145 4146
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4147
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4148
                                    virNetMessageErrorPtr rerr,
4149 4150 4151 4152
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
4153
    char *xmlin;
4154
    char *dname;
4155 4156
    char *uri;
    char *dconnuri;
4157 4158 4159
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4160 4161
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4162

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

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

4171
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4172
    dname = args->dname == NULL ? NULL : *args->dname;
4173 4174
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
4175

4176
    if (virDomainMigratePerform3(dom, xmlin,
4177 4178 4179
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
4180
                                 dconnuri, uri,
4181 4182 4183 4184 4185 4186 4187 4188 4189 4190
                                 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;

4191
 cleanup:
4192
    if (rv < 0)
4193
        virNetMessageSaveError(rerr);
4194
    virObjectUnref(dom);
4195 4196 4197 4198 4199
    return rv;
}


static int
4200 4201
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
4202
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
4203
                                   virNetMessageErrorPtr rerr,
4204 4205 4206 4207 4208 4209
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
4210 4211
    char *uri;
    char *dconnuri;
4212
    int rv = -1;
4213 4214
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4215

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

4221 4222 4223
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

4224
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
4225 4226 4227 4228 4229 4230
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
4231 4232
        goto cleanup;

4233
    make_nonnull_domain(&ret->dom, dom);
4234 4235 4236 4237 4238 4239 4240 4241

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

    rv = 0;

4242
 cleanup:
4243
    if (rv < 0) {
4244
        virNetMessageSaveError(rerr);
4245 4246
        VIR_FREE(cookieout);
    }
4247
    virObjectUnref(dom);
4248 4249 4250 4251 4252
    return rv;
}


static int
4253 4254
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4255
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4256 4257
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
4258 4259 4260
{
    virDomainPtr dom = NULL;
    int rv = -1;
4261 4262
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4263

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

4269
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4270 4271 4272 4273 4274 4275 4276 4277 4278 4279
        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;

4280
 cleanup:
4281
    if (rv < 0)
4282
        virNetMessageSaveError(rerr);
4283
    virObjectUnref(dom);
4284 4285 4286 4287
    return rv;
}


4288 4289 4290 4291 4292 4293
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)
4294 4295 4296 4297 4298 4299
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

4300 4301 4302 4303 4304 4305 4306 4307 4308 4309
    /* 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;
    }

4310
    if (!priv->conn) {
4311
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4312 4313 4314 4315 4316
        goto cleanup;
    }

    switch (args->feature) {
    case VIR_DRV_FEATURE_FD_PASSING:
4317
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
4318 4319 4320 4321
        supported = 1;
        break;

    default:
4322
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
4323 4324 4325 4326
            goto cleanup;
        break;
    }

4327
 done:
4328 4329 4330
    ret->supported = supported;
    rv = 0;

4331
 cleanup:
4332 4333 4334 4335 4336 4337
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351
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) {
4352
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369
        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;

4370
 cleanup:
4371 4372 4373
    VIR_FORCE_CLOSE(fd);
    if (rv < 0)
        virNetMessageSaveError(rerr);
4374
    virObjectUnref(dom);
4375 4376 4377
    return rv;
}

4378

4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399
static int
remoteDispatchDomainOpenGraphicsFd(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                   virNetMessagePtr msg,
                                   virNetMessageErrorPtr rerr,
                                   remote_domain_open_graphics_fd_args *args)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    int fd = -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;

4400 4401 4402
    if ((fd = virDomainOpenGraphicsFD(dom,
                                      args->idx,
                                      args->flags)) < 0)
4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413
        goto cleanup;

    if (virNetMessageAddFD(msg, fd) < 0)
        goto cleanup;

    /* return 1 here to let virNetServerProgramDispatchCall know
     * we are passing a FD */
    rv = 1;

 cleanup:
    VIR_FORCE_CLOSE(fd);
4414
    if (rv < 0)
4415 4416
        virNetMessageSaveError(rerr);

4417
    virObjectUnref(dom);
4418 4419
    return rv;
}
4420 4421


4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432
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;
4433
    int nparams = 0;
4434 4435 4436 4437 4438 4439
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4440
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4441 4442 4443 4444 4445
        goto cleanup;
    }

    flags = args->flags;

4446
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
4447
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4448 4449
        goto cleanup;
    }
4450
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4451
        goto cleanup;
4452
    nparams = args->nparams;
4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473

    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;

4474
 success:
4475 4476
    rv = 0;

4477
 cleanup:
4478 4479
    if (rv < 0)
        virNetMessageSaveError(rerr);
4480
    virTypedParamsFree(params, nparams);
4481
    virObjectUnref(dom);
4482 4483
    return rv;
}
4484

4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500
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) {
4501
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4502 4503 4504 4505
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
4506
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4507 4508 4509
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
4510
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
4511 4512 4513 4514
        goto cleanup;
    }

    if (args->nparams > 0 &&
4515
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535
        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;

4536
 success:
4537 4538
    rv = 0;
    ret->nparams = percpu_len;
4539
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
4540
        size_t i;
4541 4542 4543 4544 4545 4546

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

4548
 cleanup:
4549 4550
    if (rv < 0)
         virNetMessageSaveError(rerr);
4551
    virTypedParamsFree(params, args->ncpus * args->nparams);
4552
    virObjectUnref(dom);
4553 4554 4555
    return rv;
}

4556 4557 4558 4559 4560 4561 4562
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)
4563 4564 4565 4566
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
4567
    int len = 0;
4568 4569 4570 4571
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4572
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4573 4574 4575 4576 4577 4578 4579
        goto cleanup;
    }

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
4580 4581
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
4582 4583 4584 4585
        goto cleanup;
    }

    if (args->maxerrors &&
4586
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602
        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;

4603
 cleanup:
4604 4605
    if (rv < 0)
        virNetMessageSaveError(rerr);
4606
    virObjectUnref(dom);
4607
    if (errors && len > 0) {
4608
        size_t i;
4609 4610 4611 4612 4613 4614 4615
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

4616 4617 4618 4619 4620 4621 4622 4623 4624 4625
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;
4626
    size_t i;
4627 4628 4629 4630 4631
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

    if (!priv->conn) {
4632
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643
        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;

4644 4645 4646 4647 4648 4649 4650
    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;
    }

4651
    if (snaps && nsnaps) {
4652
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667
            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;

4668
 cleanup:
4669 4670
    if (rv < 0)
        virNetMessageSaveError(rerr);
4671
    virObjectUnref(dom);
4672
    if (snaps && nsnaps > 0)
4673
        for (i = 0; i < nsnaps; i++)
4674
            virObjectUnref(snaps[i]);
4675
    VIR_FREE(snaps);
4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688
    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;
4689
    size_t i;
4690 4691 4692 4693 4694 4695
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;
    virDomainSnapshotPtr snapshot = NULL;

    if (!priv->conn) {
4696
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710
        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;

4711 4712 4713 4714 4715 4716 4717
    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;
    }

4718
    if (snaps && nsnaps) {
4719
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734
            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;

4735
 cleanup:
4736 4737
    if (rv < 0)
        virNetMessageSaveError(rerr);
4738
    virObjectUnref(snapshot);
4739
    virObjectUnref(dom);
4740
    if (snaps && nsnaps > 0)
4741
        for (i = 0; i < nsnaps; i++)
4742
            virObjectUnref(snaps[i]);
4743
    VIR_FREE(snaps);
4744 4745 4746
    return rv;
}

4747 4748 4749 4750 4751 4752 4753 4754 4755 4756
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;
4757
    size_t i;
4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770
    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;

4771 4772 4773 4774 4775 4776 4777
    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;
    }

4778
    if (pools && npools) {
4779
        if (VIR_ALLOC_N(ret->pools.pools_val, npools) < 0)
4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794
            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;

4795
 cleanup:
4796 4797
    if (rv < 0)
        virNetMessageSaveError(rerr);
4798
    if (pools && npools > 0)
4799
        for (i = 0; i < npools; i++)
4800
            virObjectUnref(pools[i]);
4801
    VIR_FREE(pools);
4802 4803 4804
    return rv;
}

4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815
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;
4816
    size_t i;
4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832
    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;

4833 4834 4835 4836 4837 4838 4839
    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;
    }

4840
    if (vols && nvols) {
4841
        if (VIR_ALLOC_N(ret->vols.vols_val, nvols) < 0)
4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856
            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;

4857
 cleanup:
4858 4859
    if (rv < 0)
        virNetMessageSaveError(rerr);
4860
    if (vols && nvols > 0)
4861
        for (i = 0; i < nvols; i++)
4862
            virObjectUnref(vols[i]);
4863
    VIR_FREE(vols);
4864
    virObjectUnref(pool);
4865 4866 4867
    return rv;
}

4868 4869 4870 4871 4872 4873 4874 4875 4876 4877
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;
4878
    size_t i;
4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891
    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;

4892 4893 4894 4895 4896 4897 4898
    if (nnets > REMOTE_NETWORK_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many networks '%d' for limit '%d'"),
                       nnets, REMOTE_NETWORK_LIST_MAX);
        goto cleanup;
    }

4899
    if (nets && nnets) {
4900
        if (VIR_ALLOC_N(ret->nets.nets_val, nnets) < 0)
4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915
            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;

4916
 cleanup:
4917 4918
    if (rv < 0)
        virNetMessageSaveError(rerr);
4919
    if (nets && nnets > 0)
4920
        for (i = 0; i < nnets; i++)
4921
            virObjectUnref(nets[i]);
4922
    VIR_FREE(nets);
4923 4924 4925
    return rv;
}

4926 4927 4928 4929 4930 4931 4932 4933 4934 4935
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;
4936
    size_t i;
4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949
    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;

4950 4951 4952 4953 4954 4955 4956
    if (nifaces > REMOTE_INTERFACE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many interfaces '%d' for limit '%d'"),
                       nifaces, REMOTE_INTERFACE_LIST_MAX);
        goto cleanup;
    }

4957
    if (ifaces && nifaces) {
4958
        if (VIR_ALLOC_N(ret->ifaces.ifaces_val, nifaces) < 0)
4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973
            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;

4974
 cleanup:
4975 4976
    if (rv < 0)
        virNetMessageSaveError(rerr);
4977
    if (ifaces && nifaces > 0)
4978
        for (i = 0; i < nifaces; i++)
4979
            virObjectUnref(ifaces[i]);
4980
    VIR_FREE(ifaces);
4981 4982 4983
    return rv;
}

4984 4985 4986 4987 4988 4989 4990 4991 4992 4993
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;
4994
    size_t i;
4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007
    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;

5008 5009 5010 5011 5012 5013 5014
    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;
    }

5015
    if (devices && ndevices) {
5016
        if (VIR_ALLOC_N(ret->devices.devices_val, ndevices) < 0)
5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031
            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;

5032
 cleanup:
5033 5034
    if (rv < 0)
        virNetMessageSaveError(rerr);
5035
    if (devices && ndevices > 0)
5036
        for (i = 0; i < ndevices; i++)
5037
            virObjectUnref(devices[i]);
5038
    VIR_FREE(devices);
5039 5040
    return rv;
}
5041

5042 5043 5044 5045 5046 5047 5048 5049 5050 5051
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;
5052
    size_t i;
5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065
    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;

5066 5067 5068 5069 5070 5071 5072
    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;
    }

5073
    if (filters && nfilters) {
5074
        if (VIR_ALLOC_N(ret->filters.filters_val, nfilters) < 0)
5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089
            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;

5090
 cleanup:
5091 5092
    if (rv < 0)
        virNetMessageSaveError(rerr);
5093
    if (filters && nfilters > 0)
5094
        for (i = 0; i < nfilters; i++)
5095
            virObjectUnref(filters[i]);
5096
    VIR_FREE(filters);
5097 5098 5099
    return rv;
}

5100 5101 5102 5103 5104 5105 5106 5107 5108 5109
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;
5110
    size_t i;
5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123
    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;

5124 5125 5126 5127 5128 5129 5130
    if (nsecrets > REMOTE_SECRET_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many secrets '%d' for limit '%d'"),
                       nsecrets, REMOTE_SECRET_LIST_MAX);
        goto cleanup;
    }

5131
    if (secrets && nsecrets) {
5132
        if (VIR_ALLOC_N(ret->secrets.secrets_val, nsecrets) < 0)
5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147
            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;

5148
 cleanup:
5149 5150
    if (rv < 0)
        virNetMessageSaveError(rerr);
5151
    if (secrets && nsecrets > 0)
5152
        for (i = 0; i < nsecrets; i++)
5153
            virObjectUnref(secrets[i]);
5154
    VIR_FREE(secrets);
5155 5156 5157
    return rv;
}

5158 5159 5160 5161 5162 5163 5164 5165 5166
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;
5167
    int nparams = 0;
5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179
    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;

5180
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
5181 5182 5183
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
5184
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
5185
        goto cleanup;
5186
    nparams = args->nparams;
5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204

    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;

5205
 success:
5206 5207
    rv = 0;

5208
 cleanup:
5209 5210
    if (rv < 0)
        virNetMessageSaveError(rerr);
5211
    virTypedParamsFree(params, nparams);
5212 5213 5214
    return rv;
}

5215 5216 5217 5218 5219 5220 5221 5222 5223
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;
5224
    unsigned int online = 0;
5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237
    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;

5238 5239
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
5240 5241 5242 5243
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
5244
    if (args->need_map) {
5245 5246 5247 5248 5249 5250 5251 5252 5253 5254
        ret->cpumap.cpumap_len = VIR_CPU_MAPLEN(cpunum);
        ret->cpumap.cpumap_val = (char *) cpumap;
        cpumap = NULL;
    }

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

    rv = 0;

5255
 cleanup:
5256 5257 5258 5259 5260 5261
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumap);
    return rv;
}

5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294
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
     */
5295
    for (i = 0; i < msg->nfds; i++)
5296 5297 5298 5299 5300 5301 5302 5303 5304
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

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

    rv = 1;

5305
 cleanup:
5306 5307
    if (rv < 0)
        virNetMessageSaveError(rerr);
5308
    virObjectUnref(dom);
5309 5310 5311
    return rv;
}

5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338
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;

5339 5340 5341 5342 5343 5344 5345
    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;
    }

5346 5347 5348 5349 5350 5351 5352 5353
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       0) < 0)
        goto cleanup;

    rv = 0;

5354
 cleanup:
5355 5356 5357
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virTypedParamsFree(params, nparams);
5358
    virObjectUnref(dom);
5359 5360 5361
    return rv;
}

5362
static int
5363 5364 5365 5366 5367 5368
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)
5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384
{
    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;
    }

5385 5386 5387 5388 5389 5390 5391
    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;
    }

5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410
    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;

5411
 cleanup:
5412 5413 5414
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5415
    virObjectUnref(dom);
5416 5417 5418 5419
    return rv;
}

static int
5420 5421 5422 5423 5424 5425
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)
5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440
{
    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;
    }

5441 5442 5443 5444 5445 5446 5447
    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;
    }

5448 5449 5450 5451 5452 5453
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    /* Wacky world of XDR ... */
5454
    if (VIR_ALLOC(uri_out) < 0)
5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469
        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;

5470
 cleanup:
5471 5472 5473 5474 5475 5476 5477 5478 5479
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(uri_out);
    }
    return rv;
}

static int
5480 5481 5482 5483 5484 5485
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)
5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501
{
    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;
    }

5502 5503 5504 5505 5506 5507 5508
    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;
    }

5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532
    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;

5533
 cleanup:
5534 5535 5536 5537 5538 5539 5540 5541
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
        if (stream) {
            virStreamAbort(st);
            daemonFreeClientStream(client, stream);
        } else {
5542
            virObjectUnref(st);
5543 5544 5545 5546 5547 5548 5549
        }
    }
    return rv;
}


static int
5550 5551 5552 5553 5554 5555
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)
5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571
{
    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;
    }

5572 5573 5574 5575 5576 5577 5578
    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;
    }

5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600
    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;

5601
 cleanup:
5602 5603 5604
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5605
    virObjectUnref(dom);
5606 5607 5608 5609 5610
    return rv;
}


static int
5611 5612 5613 5614 5615 5616
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)
5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631
{
    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;
    }

5632 5633 5634 5635 5636 5637 5638
    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;
    }

5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658
    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;

5659
 cleanup:
5660 5661 5662 5663 5664
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
    }
5665
    virObjectUnref(dom);
5666 5667 5668 5669 5670
    return rv;
}


static int
5671 5672 5673 5674 5675
remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_confirm3_params_args *args)
5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688
{
    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;
    }

5689 5690 5691 5692 5693 5694 5695
    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;
    }

5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711
    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;

5712
 cleanup:
5713 5714 5715
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5716
    virObjectUnref(dom);
5717 5718 5719 5720
    return rv;
}


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 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764
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;

5765
 cleanup:
5766 5767 5768 5769 5770 5771 5772
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virStringFreeList(models);
    return rv;
}


5773 5774 5775 5776 5777 5778 5779
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)
5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809
{
    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;

5810
 cleanup:
5811
    for (i = 0; i < nfiles; i++)
5812 5813 5814 5815
        VIR_FORCE_CLOSE(files[i]);
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5816
    virObjectUnref(dom);
5817 5818 5819 5820
    return rv;
}


5821 5822 5823 5824 5825 5826
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)
5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859
{
    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;

5860
 cleanup:
5861
    for (i = 0; i < nfiles; i++)
5862 5863 5864 5865
        VIR_FORCE_CLOSE(files[i]);
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5866
    virObjectUnref(dom);
5867 5868 5869 5870
    return rv;
}


5871 5872
static int
remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5873
                                             virNetServerClientPtr client,
5874 5875 5876
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                             remote_connect_network_event_register_any_args *args,
5877
                                             remote_connect_network_event_register_any_ret *ret)
5878 5879 5880
{
    int callbackID;
    int rv = -1;
5881 5882
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
5883 5884
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
5885
    virNetworkPtr net = NULL;
5886 5887 5888 5889 5890 5891 5892 5893

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

    virMutexLock(&priv->lock);

5894 5895 5896 5897
    if (args->net &&
        !(net = get_nonnull_network(priv->conn, *args->net)))
        goto cleanup;

5898 5899 5900
    if (args->eventID >= VIR_NETWORK_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network event ID %d"), args->eventID);
5901 5902 5903
        goto cleanup;
    }

5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918
    /* 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)
5919 5920 5921
        goto cleanup;

    if ((callbackID = virConnectNetworkEventRegisterAny(priv->conn,
5922
                                                        net,
5923 5924
                                                        args->eventID,
                                                        networkEventCallbacks[args->eventID],
5925 5926 5927 5928 5929
                                                        ref,
                                                        remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->networkEventCallbacks,
                     priv->nnetworkEventCallbacks, 1);
        callback = ref;
5930
        goto cleanup;
5931
    }
5932

5933
    ref->callbackID = callbackID;
5934
    ret->callbackID = callbackID;
5935 5936 5937

    rv = 0;

5938
 cleanup:
5939
    VIR_FREE(callback);
5940 5941
    if (rv < 0)
        virNetMessageSaveError(rerr);
5942
    virObjectUnref(net);
5943 5944 5945 5946 5947 5948 5949
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5950
                                               virNetServerClientPtr client,
5951 5952
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
5953
                                               remote_connect_network_event_deregister_any_args *args)
5954 5955
{
    int rv = -1;
5956
    size_t i;
5957 5958 5959 5960 5961 5962 5963 5964 5965 5966
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    virMutexLock(&priv->lock);

5967
    for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
5968
        if (priv->networkEventCallbacks[i]->callbackID == args->callbackID)
5969 5970
            break;
    }
5971
    if (i == priv->nnetworkEventCallbacks) {
5972
        virReportError(VIR_ERR_INTERNAL_ERROR,
5973 5974
                       _("network event callback %d not registered"),
                       args->callbackID);
5975 5976 5977
        goto cleanup;
    }

5978
    if (virConnectNetworkEventDeregisterAny(priv->conn, args->callbackID) < 0)
5979 5980
        goto cleanup;

5981 5982
    VIR_DELETE_ELEMENT(priv->networkEventCallbacks, i,
                       priv->nnetworkEventCallbacks);
5983 5984 5985

    rv = 0;

5986
 cleanup:
5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
qemuDispatchConnectDomainMonitorEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                              virNetServerClientPtr client,
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              qemu_connect_domain_monitor_event_register_args *args,
                                              qemu_connect_domain_monitor_event_register_ret *ret)
{
    int callbackID;
    int rv = -1;
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;
    const char *event = args->event ? *args->event : 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;

    /* 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->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->qemuEventCallbacks,
                           priv->nqemuEventCallbacks,
                           callback) < 0)
        goto cleanup;

    if ((callbackID = virConnectDomainQemuMonitorEventRegister(priv->conn,
                                                               dom,
                                                               event,
                                                               remoteRelayDomainQemuMonitorEvent,
                                                               ref,
                                                               remoteEventCallbackFree,
                                                               args->flags)) < 0) {
        VIR_SHRINK_N(priv->qemuEventCallbacks,
                     priv->nqemuEventCallbacks, 1);
        callback = ref;
        goto cleanup;
    }

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

    rv = 0;

6056
 cleanup:
6057 6058 6059
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
6060
    virObjectUnref(dom);
6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
qemuDispatchConnectDomainMonitorEventDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client,
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                                qemu_connect_domain_monitor_event_deregister_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->nqemuEventCallbacks; i++) {
        if (priv->qemuEventCallbacks[i]->callbackID == args->callbackID)
            break;
    }
    if (i == priv->nqemuEventCallbacks) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("qemu monitor event callback %d not registered"),
                       args->callbackID);
        goto cleanup;
    }

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

    VIR_DELETE_ELEMENT(priv->qemuEventCallbacks, i,
                       priv->nqemuEventCallbacks);

    rv = 0;

6105
 cleanup:
6106 6107 6108 6109 6110 6111
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}

6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144
static int
remoteDispatchDomainGetTime(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client,
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                            virNetMessageErrorPtr rerr,
                            remote_domain_get_time_args *args,
                            remote_domain_get_time_ret *ret)
{
    int rv = -1;
    virDomainPtr dom = NULL;
    long long seconds;
    unsigned int nseconds;
    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 (virDomainGetTime(dom, &seconds, &nseconds, args->flags) < 0)
        goto cleanup;

    ret->seconds = seconds;
    ret->nseconds = nseconds;
    rv = 0;

 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
6145
    virObjectUnref(dom);
6146 6147
    return rv;
}
6148

M
Michal Privoznik 已提交
6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196

static int
remoteDispatchNodeGetFreePages(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client,
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                               virNetMessageErrorPtr rerr,
                               remote_node_get_free_pages_args *args,
                               remote_node_get_free_pages_ret *ret)
{
    int rv = -1;
    int len;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    if (args->pages.pages_len * args->cellCount > REMOTE_NODE_MAX_CELLS) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("the result won't fit into REMOTE_NODE_MAX_CELLS"));
        goto cleanup;
    }

    /* Allocate return buffer. */
    if (VIR_ALLOC_N(ret->counts.counts_val,
                    args->pages.pages_len * args->cellCount) < 0)
        goto cleanup;

    if ((len = virNodeGetFreePages(priv->conn,
                                   args->pages.pages_len,
                                   args->pages.pages_val,
                                   args->startCell,
                                   args->cellCount,
                                   (unsigned long long *) ret->counts.counts_val,
                                   args->flags)) <= 0)
        goto cleanup;

    ret->counts.counts_len = len;
    rv = 0;

 cleanup:
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(ret->counts.counts_val);
    }
    return rv;
6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211
}

/* Copy contents of virNetworkDHCPLeasePtr to remote_network_dhcp_lease */
static int
remoteSerializeDHCPLease(remote_network_dhcp_lease *lease_dst, virNetworkDHCPLeasePtr lease_src)
{
    char **mac_tmp = NULL;
    char **iaid_tmp = NULL;
    char **hostname_tmp = NULL;
    char **clientid_tmp = NULL;

    lease_dst->expirytime = lease_src->expirytime;
    lease_dst->type = lease_src->type;
    lease_dst->prefix = lease_src->prefix;

6212
    if (VIR_STRDUP(lease_dst->iface, lease_src->iface) < 0 ||
J
Ján Tomko 已提交
6213
        VIR_STRDUP(lease_dst->ipaddr, lease_src->ipaddr) < 0)
6214 6215
        goto error;

J
Ján Tomko 已提交
6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240
    if (lease_src->mac) {
        if (VIR_ALLOC(mac_tmp) < 0 ||
            VIR_STRDUP(*mac_tmp, lease_src->mac) < 0)
            goto error;
    }
    if (lease_src->iaid) {
        if (VIR_ALLOC(iaid_tmp) < 0 ||
            VIR_STRDUP(*iaid_tmp, lease_src->iaid) < 0)
            goto error;
    }
    if (lease_src->hostname) {
        if (VIR_ALLOC(hostname_tmp) < 0 ||
            VIR_STRDUP(*hostname_tmp, lease_src->hostname) < 0)
            goto error;
    }
    if (lease_src->clientid) {
        if (VIR_ALLOC(clientid_tmp) < 0 ||
            VIR_STRDUP(*clientid_tmp, lease_src->clientid) < 0)
            goto error;
    }

    lease_dst->mac = mac_tmp;
    lease_dst->iaid = iaid_tmp;
    lease_dst->hostname = hostname_tmp;
    lease_dst->clientid = clientid_tmp;
6241 6242 6243 6244

    return 0;

 error:
J
Ján Tomko 已提交
6245 6246 6247 6248 6249 6250 6251 6252
    if (mac_tmp)
        VIR_FREE(*mac_tmp);
    if (iaid_tmp)
        VIR_FREE(*iaid_tmp);
    if (hostname_tmp)
        VIR_FREE(*hostname_tmp);
    if (clientid_tmp)
        VIR_FREE(*clientid_tmp);
6253 6254 6255 6256 6257
    VIR_FREE(mac_tmp);
    VIR_FREE(iaid_tmp);
    VIR_FREE(hostname_tmp);
    VIR_FREE(clientid_tmp);
    VIR_FREE(lease_dst->ipaddr);
6258
    VIR_FREE(lease_dst->iface);
6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286
    return -1;
}


static int
remoteDispatchNetworkGetDHCPLeases(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client,
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                   virNetMessageErrorPtr rerr,
                                   remote_network_get_dhcp_leases_args *args,
                                   remote_network_get_dhcp_leases_ret *ret)
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virNetworkDHCPLeasePtr *leases = NULL;
    virNetworkPtr net = NULL;
    int nleases = 0;

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

    if (!(net = get_nonnull_network(priv->conn, args->net)))
        goto cleanup;

    if ((nleases = virNetworkGetDHCPLeases(net,
6287
                                           args->mac ? *args->mac : NULL,
6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321
                                           args->need_results ? &leases : NULL,
                                           args->flags)) < 0)
        goto cleanup;

    if (nleases > REMOTE_NETWORK_DHCP_LEASES_MAX) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Number of leases is %d, which exceeds max limit: %d"),
                       nleases, REMOTE_NETWORK_DHCP_LEASES_MAX);
        goto cleanup;
    }

    if (leases && nleases) {
        if (VIR_ALLOC_N(ret->leases.leases_val, nleases) < 0)
            goto cleanup;

        ret->leases.leases_len = nleases;

        for (i = 0; i < nleases; i++) {
            if (remoteSerializeDHCPLease(ret->leases.leases_val + i, leases[i]) < 0)
                goto cleanup;
        }

    } else {
        ret->leases.leases_len = 0;
        ret->leases.leases_val = NULL;
    }

    ret->ret = nleases;

    rv = 0;

 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
6322
    if (leases && nleases > 0)
6323 6324
        for (i = 0; i < nleases; i++)
            virNetworkDHCPLeaseFree(leases[i]);
6325
    VIR_FREE(leases);
6326
    virObjectUnref(net);
6327
    return rv;
M
Michal Privoznik 已提交
6328 6329 6330
}


6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410
static int
remoteDispatchConnectGetAllDomainStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_connect_get_all_domain_stats_args *args,
                                       remote_connect_get_all_domain_stats_ret *ret)
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainStatsRecordPtr *retStats = NULL;
    int nrecords = 0;
    virDomainPtr *doms = NULL;

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

    if (args->doms.doms_len) {
        if (VIR_ALLOC_N(doms, args->doms.doms_len + 1) < 0)
            goto cleanup;

        for (i = 0; i < args->doms.doms_len; i++) {
            if (!(doms[i] = get_nonnull_domain(priv->conn, args->doms.doms_val[i])))
                goto cleanup;
        }

        if ((nrecords = virDomainListGetStats(doms,
                                              args->stats,
                                              &retStats,
                                              args->flags)) < 0)
            goto cleanup;
    } else {
        if ((nrecords = virConnectGetAllDomainStats(priv->conn,
                                                    args->stats,
                                                    &retStats,
                                                    args->flags)) < 0)
            goto cleanup;
    }

    if (nrecords > REMOTE_CONNECT_GET_ALL_DOMAIN_STATS_MAX) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Number of domain stats records is %d, "
                         "which exceeds max limit: %d"),
                       nrecords, REMOTE_DOMAIN_LIST_MAX);
        goto cleanup;
    }

    if (nrecords) {
        if (VIR_ALLOC_N(ret->retStats.retStats_val, nrecords) < 0)
            goto cleanup;

        ret->retStats.retStats_len = nrecords;

        for (i = 0; i < nrecords; i++) {
            remote_domain_stats_record *dst = ret->retStats.retStats_val + i;

            make_nonnull_domain(&dst->dom, retStats[i]->dom);

            if (remoteSerializeTypedParameters(retStats[i]->params,
                                               retStats[i]->nparams,
                                               &dst->params.params_val,
                                               &dst->params.params_len,
                                               VIR_TYPED_PARAM_STRING_OKAY) < 0)
                goto cleanup;
        }
    } else {
        ret->retStats.retStats_len = 0;
        ret->retStats.retStats_val = NULL;
    }

    rv = 0;

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

    virDomainStatsRecordListFree(retStats);
6411
    virObjectListFree(doms);
6412 6413 6414 6415 6416

    return rv;
}


M
Michal Privoznik 已提交
6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453
static int
remoteDispatchNodeAllocPages(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client,
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                             virNetMessageErrorPtr rerr,
                             remote_node_alloc_pages_args *args,
                             remote_node_alloc_pages_ret *ret)
{
    int rv = -1;
    int len;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    if ((len = virNodeAllocPages(priv->conn,
                                 args->pageSizes.pageSizes_len,
                                 args->pageSizes.pageSizes_val,
                                 (unsigned long long *) args->pageCounts.pageCounts_val,
                                 args->startCell,
                                 args->cellCount,
                                 args->flags)) < 0)
        goto cleanup;

    ret->ret = len;
    rv = 0;

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


6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556
static int
remoteDispatchDomainGetFSInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client,
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                              virNetMessageErrorPtr rerr,
                              remote_domain_get_fsinfo_args *args,
                              remote_domain_get_fsinfo_ret *ret)
{
    int rv = -1;
    size_t i, j;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainFSInfoPtr *info = NULL;
    virDomainPtr dom = NULL;
    remote_domain_fsinfo *dst;
    int ninfo = 0;
    size_t ndisk;

    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 ((ninfo = virDomainGetFSInfo(dom, &info, args->flags)) < 0)
        goto cleanup;

    if (ninfo > REMOTE_DOMAIN_FSINFO_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many mountpoints in fsinfo: %d for limit %d"),
                       ninfo, REMOTE_DOMAIN_FSINFO_MAX);
        goto cleanup;
    }

    if (ninfo) {
        if (VIR_ALLOC_N(ret->info.info_val, ninfo) < 0)
            goto cleanup;

        ret->info.info_len = ninfo;

        for (i = 0; i < ninfo; i++) {
            dst = &ret->info.info_val[i];
            if (VIR_STRDUP(dst->mountpoint, info[i]->mountpoint) < 0)
                goto cleanup;

            if (VIR_STRDUP(dst->name, info[i]->name) < 0)
                goto cleanup;

            if (VIR_STRDUP(dst->fstype, info[i]->fstype) < 0)
                goto cleanup;

            ndisk = info[i]->ndevAlias;
            if (ndisk > REMOTE_DOMAIN_FSINFO_DISKS_MAX) {
                virReportError(VIR_ERR_RPC,
                               _("Too many disks in fsinfo: %zd for limit %d"),
                               ndisk, REMOTE_DOMAIN_FSINFO_DISKS_MAX);
                goto cleanup;
            }

            if (ndisk > 0) {
                if (VIR_ALLOC_N(dst->dev_aliases.dev_aliases_val, ndisk) < 0)
                    goto cleanup;

                for (j = 0; j < ndisk; j++) {
                    if (VIR_STRDUP(dst->dev_aliases.dev_aliases_val[j],
                                   info[i]->devAlias[j]) < 0)
                        goto cleanup;
                }

                dst->dev_aliases.dev_aliases_len = ndisk;
            } else {
                dst->dev_aliases.dev_aliases_val = NULL;
                dst->dev_aliases.dev_aliases_len = 0;
            }
        }

    } else {
        ret->info.info_len = 0;
        ret->info.info_val = NULL;
    }

    ret->ret = ninfo;

    rv = 0;

 cleanup:
    if (rv < 0) {
        virNetMessageSaveError(rerr);

        if (ret->info.info_val && ninfo > 0) {
            for (i = 0; i < ninfo; i++) {
                dst = &ret->info.info_val[i];
                VIR_FREE(dst->mountpoint);
                if (dst->dev_aliases.dev_aliases_val) {
                    for (j = 0; j < dst->dev_aliases.dev_aliases_len; j++)
                        VIR_FREE(dst->dev_aliases.dev_aliases_val[j]);
                    VIR_FREE(dst->dev_aliases.dev_aliases_val);
                }
            }
            VIR_FREE(ret->info.info_val);
        }
    }
6557
    virObjectUnref(dom);
6558 6559 6560 6561 6562 6563 6564 6565 6566
    if (ninfo >= 0)
        for (i = 0; i < ninfo; i++)
            virDomainFSInfoFree(info[i]);
    VIR_FREE(info);

    return rv;
}


6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592
static int
remoteSerializeDomainInterface(virDomainInterfacePtr *ifaces,
                               unsigned int ifaces_count,
                               remote_domain_interface_addresses_ret *ret)
{
    size_t i, j;

    if (ifaces_count > REMOTE_DOMAIN_INTERFACE_MAX) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Number of interfaces, %d exceeds the max limit: %d"),
                       ifaces_count, REMOTE_DOMAIN_INTERFACE_MAX);
        return -1;
    }

    if (VIR_ALLOC_N(ret->ifaces.ifaces_val, ifaces_count) < 0)
        return -1;

    ret->ifaces.ifaces_len = ifaces_count;

    for (i = 0; i < ifaces_count; i++) {
        virDomainInterfacePtr iface = ifaces[i];
        remote_domain_interface *iface_ret = &(ret->ifaces.ifaces_val[i]);

        if ((VIR_STRDUP(iface_ret->name, iface->name)) < 0)
            goto cleanup;

6593 6594 6595
        if (iface->hwaddr &&
            (VIR_ALLOC(iface_ret->hwaddr) < 0 ||
             VIR_STRDUP(*iface_ret->hwaddr, iface->hwaddr) < 0))
6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630
            goto cleanup;

        if (iface->naddrs > REMOTE_DOMAIN_IP_ADDR_MAX) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Number of interfaces, %d exceeds the max limit: %d"),
                           iface->naddrs, REMOTE_DOMAIN_IP_ADDR_MAX);
            goto cleanup;
        }

        if (VIR_ALLOC_N(iface_ret->addrs.addrs_val,
                        iface->naddrs) < 0)
            goto cleanup;

        iface_ret->addrs.addrs_len = iface->naddrs;

        for (j = 0; j < iface->naddrs; j++) {
            virDomainIPAddressPtr ip_addr = &(iface->addrs[j]);
            remote_domain_ip_addr *ip_addr_ret =
                &(iface_ret->addrs.addrs_val[j]);

            if (VIR_STRDUP(ip_addr_ret->addr, ip_addr->addr) < 0)
                goto cleanup;

            ip_addr_ret->prefix = ip_addr->prefix;
            ip_addr_ret->type = ip_addr->type;
        }
    }

    return 0;

 cleanup:
    if (ret->ifaces.ifaces_val) {
        for (i = 0; i < ifaces_count; i++) {
            remote_domain_interface *iface_ret = &(ret->ifaces.ifaces_val[i]);
            VIR_FREE(iface_ret->name);
6631 6632 6633 6634
            if (iface_ret->hwaddr) {
                VIR_FREE(*iface_ret->hwaddr);
                VIR_FREE(iface_ret->hwaddr);
            }
6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695
            for (j = 0; j < iface_ret->addrs.addrs_len; j++) {
                remote_domain_ip_addr *ip_addr =
                    &(iface_ret->addrs.addrs_val[j]);
                VIR_FREE(ip_addr->addr);
            }
        }
        VIR_FREE(ret->ifaces.ifaces_val);
    }

    return -1;
}


static int
remoteDispatchDomainInterfaceAddresses(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_interface_addresses_args *args,
                                       remote_domain_interface_addresses_ret *ret)
{
    size_t i;
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainInterfacePtr *ifaces = NULL;
    int ifaces_count = 0;
    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 ((ifaces_count = virDomainInterfaceAddresses(dom, &ifaces, args->source, args->flags)) < 0)
        goto cleanup;

    if (remoteSerializeDomainInterface(ifaces, ifaces_count, ret) < 0)
        goto cleanup;

    rv = 0;

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

    virObjectUnref(dom);

    if (ifaces && ifaces_count > 0) {
        for (i = 0; i < ifaces_count; i++)
            virDomainInterfaceFree(ifaces[i]);
    }
    VIR_FREE(ifaces);

    return rv;
}


6696 6697 6698 6699 6700 6701 6702 6703 6704
/*----- 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
6705
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
6706 6707
{
    virDomainPtr dom;
6708
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
6709 6710 6711 6712 6713 6714 6715 6716
    /* 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
6717
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
6718
{
6719
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
6720 6721
}

D
Daniel Veillard 已提交
6722
static virInterfacePtr
6723
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
6724
{
6725
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
6726 6727
}

6728
static virStoragePoolPtr
6729
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
6730
{
6731 6732
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
6733 6734 6735
}

static virStorageVolPtr
6736
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
6737 6738
{
    virStorageVolPtr ret;
6739 6740
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
6741 6742 6743
    return ret;
}

6744
static virSecretPtr
6745
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
6746
{
6747
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
6748 6749
}

6750
static virNWFilterPtr
6751
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
6752
{
6753
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
6754 6755
}

C
Chris Lalancette 已提交
6756
static virDomainSnapshotPtr
6757
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
6758
{
6759
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
6760 6761
}

6762 6763
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
6764
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
6765 6766
{
    dom_dst->id = dom_src->id;
6767
    ignore_value(VIR_STRDUP_QUIET(dom_dst->name, dom_src->name));
6768
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
6769 6770 6771
}

static void
6772
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
6773
{
6774
    ignore_value(VIR_STRDUP_QUIET(net_dst->name, net_src->name));
6775
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
6776 6777
}

D
Daniel Veillard 已提交
6778
static void
6779 6780
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
6781
{
6782 6783
    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 已提交
6784 6785
}

6786
static void
6787
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
6788
{
6789
    ignore_value(VIR_STRDUP_QUIET(pool_dst->name, pool_src->name));
6790
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
6791 6792 6793
}

static void
6794
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
6795
{
6796 6797 6798
    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));
6799
}
6800 6801

static void
6802
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
6803
{
6804
    ignore_value(VIR_STRDUP_QUIET(dev_dst->name, dev_src->name));
6805
}
6806 6807

static void
6808
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
6809
{
6810
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
6811
    secret_dst->usageType = secret_src->usageType;
6812
    ignore_value(VIR_STRDUP_QUIET(secret_dst->usageID, secret_src->usageID));
6813
}
6814 6815

static void
6816
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
6817
{
6818
    ignore_value(VIR_STRDUP_QUIET(nwfilter_dst->name, nwfilter_src->name));
6819
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
6820
}
C
Chris Lalancette 已提交
6821 6822

static void
6823
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
6824
{
6825
    ignore_value(VIR_STRDUP_QUIET(snapshot_dst->name, snapshot_src->name));
6826
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
6827
}
6828 6829 6830 6831 6832 6833 6834 6835

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;
6836
    size_t i = 0;
6837

6838
    if (VIR_ALLOC_N(val, nerrors) < 0)
6839
        goto error;
6840 6841

    for (i = 0; i < nerrors; i++) {
6842 6843
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
6844 6845 6846 6847 6848 6849 6850 6851
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

6852
 error:
6853
    if (val) {
6854
        size_t j;
6855 6856 6857 6858 6859 6860
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}