remote.c 223.4 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
static virNodeDevicePtr get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev);
95 96 97 98 99 100 101 102 103
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);
104

105 106 107 108 109 110
static int
remoteSerializeDomainDiskErrors(virDomainDiskErrorPtr errors,
                                int nerrors,
                                remote_domain_disk_error **ret_errors_val,
                                u_int *ret_errors_len);

111 112
#include "remote_dispatch.h"
#include "qemu_dispatch.h"
113
#include "lxc_dispatch.h"
C
Chris Lalancette 已提交
114 115


116 117
/* Prototypes */
static void
118
remoteDispatchObjectEventSend(virNetServerClientPtr client,
119
                              virNetServerProgramPtr program,
120 121 122
                              int procnr,
                              xdrproc_t proc,
                              void *data);
123

124 125 126 127 128 129
static void
remoteEventCallbackFree(void *opaque)
{
    VIR_FREE(opaque);
}

130 131 132 133 134 135 136 137 138 139 140 141

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.  */
142
    memset(&def, 0, sizeof(def));
143 144 145 146 147 148 149 150 151
    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);

152
 cleanup:
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    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);

179
 cleanup:
180 181 182 183 184
    ignore_value(virIdentitySetCurrent(NULL));
    virObjectUnref(identity);
    return ret;
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
static bool
remoteRelayStoragePoolEventCheckACL(virNetServerClientPtr client,
                                    virConnectPtr conn,
                                    virStoragePoolPtr pool)
{
    virStoragePoolDef def;
    virIdentityPtr identity = NULL;
    bool ret = false;

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

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

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

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
static bool
remoteRelayNodeDeviceEventCheckACL(virNetServerClientPtr client,
                                   virConnectPtr conn,
                                   virNodeDevicePtr dev)
{
    virNodeDeviceDef def;
    virIdentityPtr identity = NULL;
    bool ret = false;

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

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

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

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
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);

258
 cleanup:
259 260 261 262 263 264
    ignore_value(virIdentitySetCurrent(NULL));
    virObjectUnref(identity);
    return ret;
}


265 266 267 268 269 270
static int
remoteRelayDomainEventLifecycle(virConnectPtr conn,
                                virDomainPtr dom,
                                int event,
                                int detail,
                                void *opaque)
271
{
272
    daemonClientEventCallbackPtr callback = opaque;
273
    remote_domain_event_lifecycle_msg data;
274

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

279 280
    VIR_DEBUG("Relaying domain lifecycle event %d %d, callback %d legacy %d",
              event, detail, callback->callbackID, callback->legacy);
281

282
    /* build return data */
283
    memset(&data, 0, sizeof(data));
284
    make_nonnull_domain(&data.dom, dom);
285 286
    data.event = event;
    data.detail = detail;
287

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

303 304
    return 0;
}
305

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

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

318 319
    VIR_DEBUG("Relaying domain reboot event %s %d, callback %d legacy %d",
              dom->name, dom->id, callback->callbackID, callback->legacy);
320 321

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

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

    return 0;
}

341

342 343 344 345 346
static int
remoteRelayDomainEventRTCChange(virConnectPtr conn,
                                virDomainPtr dom,
                                long long offset,
                                void *opaque)
347
{
348
    daemonClientEventCallbackPtr callback = opaque;
349 350
    remote_domain_event_rtc_change_msg data;

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

355 356 357
    VIR_DEBUG("Relaying domain rtc change event %s %d %lld, callback %d legacy %d",
              dom->name, dom->id, offset,
              callback->callbackID, callback->legacy);
358 359

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

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

    return 0;
}


381 382 383 384 385
static int
remoteRelayDomainEventWatchdog(virConnectPtr conn,
                               virDomainPtr dom,
                               int action,
                               void *opaque)
386
{
387
    daemonClientEventCallbackPtr callback = opaque;
388 389
    remote_domain_event_watchdog_msg data;

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

394 395
    VIR_DEBUG("Relaying domain watchdog event %s %d %d, callback %d",
              dom->name, dom->id, action, callback->callbackID);
396 397

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

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

    return 0;
}


419 420 421 422 423 424 425
static int
remoteRelayDomainEventIOError(virConnectPtr conn,
                              virDomainPtr dom,
                              const char *srcPath,
                              const char *devAlias,
                              int action,
                              void *opaque)
426
{
427
    daemonClientEventCallbackPtr callback = opaque;
428 429
    remote_domain_event_io_error_msg data;

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

434 435 436
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d, callback %d",
              dom->name, dom->id, srcPath, devAlias, action,
              callback->callbackID);
437 438

    /* build return data */
439
    memset(&data, 0, sizeof(data));
440 441 442
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
443
    make_nonnull_domain(&data.dom, dom);
444 445
    data.action = action;

446 447 448 449 450 451 452 453 454 455 456 457
    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);
    }
458 459

    return 0;
460
 error:
E
Eric Blake 已提交
461 462
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
463
    return -1;
464 465 466
}


467 468 469 470 471 472 473 474
static int
remoteRelayDomainEventIOErrorReason(virConnectPtr conn,
                                    virDomainPtr dom,
                                    const char *srcPath,
                                    const char *devAlias,
                                    int action,
                                    const char *reason,
                                    void *opaque)
475
{
476
    daemonClientEventCallbackPtr callback = opaque;
477 478
    remote_domain_event_io_error_reason_msg data;

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

483 484 485
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s, callback %d",
              dom->name, dom->id, srcPath, devAlias, action, reason,
              callback->callbackID);
486 487

    /* build return data */
488
    memset(&data, 0, sizeof(data));
489 490 491 492
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0 ||
        VIR_STRDUP(data.reason, reason) < 0)
        goto error;
493
    data.action = action;
494 495

    make_nonnull_domain(&data.dom, dom);
496

497 498 499 500 501 502 503 504 505 506 507 508
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_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);
    }
509 510

    return 0;
511

512
 error:
E
Eric Blake 已提交
513 514 515
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
    VIR_FREE(data.reason);
516
    return -1;
517 518 519
}


520 521 522 523 524 525 526 527 528
static int
remoteRelayDomainEventGraphics(virConnectPtr conn,
                               virDomainPtr dom,
                               int phase,
                               virDomainEventGraphicsAddressPtr local,
                               virDomainEventGraphicsAddressPtr remote,
                               const char *authScheme,
                               virDomainEventGraphicsSubjectPtr subject,
                               void *opaque)
529
{
530
    daemonClientEventCallbackPtr callback = opaque;
531
    remote_domain_event_graphics_msg data;
532
    size_t i;
533

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

538 539
    VIR_DEBUG("Relaying domain graphics event %s %d %d - %d %s %s  - %d %s %s - %s, callback %d",
              dom->name, dom->id, phase,
540 541
              local->family, local->service, local->node,
              remote->family, remote->service, remote->node,
542
              authScheme, callback->callbackID);
543

544
    VIR_DEBUG("Subject %d", subject->nidentity);
545
    for (i = 0; i < subject->nidentity; i++)
546
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
547 548

    /* build return data */
549
    memset(&data, 0, sizeof(data));
550 551 552
    data.phase = phase;
    data.local.family = local->family;
    data.remote.family = remote->family;
553 554 555 556 557 558
    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;
559 560

    data.subject.subject_len = subject->nidentity;
561
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0)
562
        goto error;
563

564
    for (i = 0; i < data.subject.subject_len; i++) {
565 566 567
        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;
568
    }
569
    make_nonnull_domain(&data.dom, dom);
570

571 572 573 574 575 576 577 578 579 580 581 582
    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);
    }
583 584

    return 0;
585

586
 error:
E
Eric Blake 已提交
587 588 589 590 591
    VIR_FREE(data.authScheme);
    VIR_FREE(data.local.node);
    VIR_FREE(data.local.service);
    VIR_FREE(data.remote.node);
    VIR_FREE(data.remote.service);
592
    if (data.subject.subject_val != NULL) {
593
        for (i = 0; i < data.subject.subject_len; i++) {
E
Eric Blake 已提交
594 595
            VIR_FREE(data.subject.subject_val[i].type);
            VIR_FREE(data.subject.subject_val[i].name);
596
        }
E
Eric Blake 已提交
597
        VIR_FREE(data.subject.subject_val);
598 599
    }
    return -1;
600 601
}

602 603 604 605 606 607 608
static int
remoteRelayDomainEventBlockJob(virConnectPtr conn,
                               virDomainPtr dom,
                               const char *path,
                               int type,
                               int status,
                               void *opaque)
609
{
610
    daemonClientEventCallbackPtr callback = opaque;
611 612
    remote_domain_event_block_job_msg data;

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

617 618
    VIR_DEBUG("Relaying domain block job event %s %d %s %i, %i, callback %d",
              dom->name, dom->id, path, type, status, callback->callbackID);
619 620

    /* build return data */
621
    memset(&data, 0, sizeof(data));
622 623
    if (VIR_STRDUP(data.path, path) < 0)
        goto error;
624 625
    data.type = type;
    data.status = status;
626
    make_nonnull_domain(&data.dom, dom);
627

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

    return 0;
642
 error:
E
Eric Blake 已提交
643
    VIR_FREE(data.path);
644
    return -1;
645 646
}

647

648 649 650 651
static int
remoteRelayDomainEventControlError(virConnectPtr conn,
                                   virDomainPtr dom,
                                   void *opaque)
652
{
653
    daemonClientEventCallbackPtr callback = opaque;
654 655
    remote_domain_event_control_error_msg data;

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

660 661
    VIR_DEBUG("Relaying domain control error %s %d, callback %d",
              dom->name, dom->id, callback->callbackID);
662 663

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

667 668 669 670 671 672 673 674 675 676 677 678
    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);
    }
679 680 681 682 683

    return 0;
}


684 685 686 687 688 689 690 691
static int
remoteRelayDomainEventDiskChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *oldSrcPath,
                                 const char *newSrcPath,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
692
{
693
    daemonClientEventCallbackPtr callback = opaque;
694 695 696
    remote_domain_event_disk_change_msg data;
    char **oldSrcPath_p = NULL, **newSrcPath_p = NULL;

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

701 702 703
    VIR_DEBUG("Relaying domain %s %d disk change %s %s %s %d, callback %d",
              dom->name, dom->id, oldSrcPath, newSrcPath, devAlias, reason,
              callback->callbackID);
704 705

    /* build return data */
706
    memset(&data, 0, sizeof(data));
707 708
    if (oldSrcPath &&
        ((VIR_ALLOC(oldSrcPath_p) < 0) ||
709
         VIR_STRDUP(*oldSrcPath_p, oldSrcPath) < 0))
710
        goto error;
711 712 713

    if (newSrcPath &&
        ((VIR_ALLOC(newSrcPath_p) < 0) ||
714
         VIR_STRDUP(*newSrcPath_p, newSrcPath) < 0))
715
        goto error;
716 717 718

    data.oldSrcPath = oldSrcPath_p;
    data.newSrcPath = newSrcPath_p;
719 720
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
721 722 723 724
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

725 726 727 728 729 730 731 732 733 734 735 736
    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);
    }
737 738 739

    return 0;

740
 error:
M
Michal Privoznik 已提交
741 742
    VIR_FREE(oldSrcPath_p);
    VIR_FREE(newSrcPath_p);
743 744 745 746
    return -1;
}


747 748 749 750 751 752 753
static int
remoteRelayDomainEventTrayChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
{
754
    daemonClientEventCallbackPtr callback = opaque;
755 756
    remote_domain_event_tray_change_msg data;

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

761 762
    VIR_DEBUG("Relaying domain %s %d tray change devAlias: %s reason: %d, callback %d",
              dom->name, dom->id, devAlias, reason, callback->callbackID);
763 764

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

767
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
768 769 770 771 772
        return -1;
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

773 774 775 776 777 778 779 780 781 782 783 784
    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);
    }
785 786 787 788

    return 0;
}

789 790 791
static int
remoteRelayDomainEventPMWakeup(virConnectPtr conn,
                               virDomainPtr dom,
E
Eric Blake 已提交
792
                               int reason,
793 794
                               void *opaque)
{
795
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
796 797
    remote_domain_event_pmwakeup_msg data;

798 799
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
800 801
        return -1;

802 803
    VIR_DEBUG("Relaying domain %s %d system pmwakeup, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
804 805

    /* build return data */
806
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
807 808
    make_nonnull_domain(&data.dom, dom);

809 810 811 812 813 814
    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 已提交
815
                                                          reason, data };
816 817 818 819 820

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMWAKEUP,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmwakeup_msg, &msg);
    }
O
Osier Yang 已提交
821 822 823 824

    return 0;
}

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

834 835
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
836 837
        return -1;

838 839
    VIR_DEBUG("Relaying domain %s %d system pmsuspend, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
840 841

    /* build return data */
842
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
843 844
    make_nonnull_domain(&data.dom, dom);

845 846 847 848 849 850
    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 已提交
851
                                                           reason, data };
852 853 854 855 856

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_msg, &msg);
    }
O
Osier Yang 已提交
857 858 859 860

    return 0;
}

861
static int
862
remoteRelayDomainEventBalloonChange(virConnectPtr conn,
863 864 865 866
                                    virDomainPtr dom,
                                    unsigned long long actual,
                                    void *opaque)
{
867
    daemonClientEventCallbackPtr callback = opaque;
868 869
    remote_domain_event_balloon_change_msg data;

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

874 875
    VIR_DEBUG("Relaying domain balloon change event %s %d %lld, callback %d",
              dom->name, dom->id, actual, callback->callbackID);
876 877 878 879 880 881

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

882 883 884 885 886 887 888 889 890 891 892 893
    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);
    }
894 895 896 897 898

    return 0;
}


899 900 901
static int
remoteRelayDomainEventPMSuspendDisk(virConnectPtr conn,
                                    virDomainPtr dom,
E
Eric Blake 已提交
902
                                    int reason,
903 904
                                    void *opaque)
{
905
    daemonClientEventCallbackPtr callback = opaque;
906 907
    remote_domain_event_pmsuspend_disk_msg data;

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

912 913
    VIR_DEBUG("Relaying domain %s %d system pmsuspend-disk, callback %d",
              dom->name, dom->id, callback->callbackID);
914 915 916 917 918

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

919 920 921 922 923 924
    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 已提交
925
                                                                reason, data };
926 927 928 929 930

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND_DISK,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_disk_msg, &msg);
    }
931 932 933 934

    return 0;
}

935
static int
936
remoteRelayDomainEventDeviceRemoved(virConnectPtr conn,
937 938 939 940
                                    virDomainPtr dom,
                                    const char *devAlias,
                                    void *opaque)
{
941
    daemonClientEventCallbackPtr callback = opaque;
942 943
    remote_domain_event_device_removed_msg data;

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

948 949
    VIR_DEBUG("Relaying domain device removed event %s %d %s, callback %d",
              dom->name, dom->id, devAlias, callback->callbackID);
950 951 952 953 954 955 956 957 958

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

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

    make_nonnull_domain(&data.dom, dom);

959 960 961 962 963 964 965 966 967 968 969 970 971 972
    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);
    }
973 974 975 976

    return 0;
}

977

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
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;
}


1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
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;

1030 1031
    VIR_DEBUG("Relaying domain tunable event %s %d, callback %d, params %p %d",
              dom->name, dom->id, callback->callbackID, params, nparams);
1032 1033 1034 1035 1036 1037

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

1038 1039 1040 1041
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &data.params.params_val,
                                &data.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0)
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
        return -1;

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

    return 0;
}


1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
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;
}


1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
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 已提交
1111
    data.callbackID = callback->callbackID;
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121

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

    return 0;
}


1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
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;
}
1153 1154


J
Jiri Denemark 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
static int
remoteRelayDomainEventJobCompleted(virConnectPtr conn,
                                   virDomainPtr dom,
                                   virTypedParameterPtr params,
                                   int nparams,
                                   void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_job_completed_msg data;

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

    VIR_DEBUG("Relaying domain migration completed event %s %d, "
              "callback %d, params %p %d",
              dom->name, dom->id, callback->callbackID, params, nparams);

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

    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &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_JOB_COMPLETED,
                                  (xdrproc_t)xdr_remote_domain_event_callback_job_completed_msg,
                                  &data);
    return 0;
}


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 1219 1220 1221 1222 1223 1224 1225 1226
static int
remoteRelayDomainEventDeviceRemovalFailed(virConnectPtr conn,
                                          virDomainPtr dom,
                                          const char *devAlias,
                                          void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_device_removal_failed_msg data;

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

    VIR_DEBUG("Relaying domain device removal failed 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);
    data.callbackID = callback->callbackID;

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DEVICE_REMOVAL_FAILED,
                                  (xdrproc_t)xdr_remote_domain_event_callback_device_removal_failed_msg,
                                  &data);

    return 0;
}



1227
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
1228
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
1229
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
1230
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
1231
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
1232
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
1233
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
1234
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
1235
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
1236
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
1237
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
1238
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
1239
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
1240
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
1241
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
1242
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
1243
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
1244
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob2),
1245
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTunable),
1246
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventAgentLifecycle),
1247
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceAdded),
1248
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventMigrationIteration),
J
Jiri Denemark 已提交
1249
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventJobCompleted),
1250
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemovalFailed),
1251 1252 1253 1254
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

1255
static int
1256
remoteRelayNetworkEventLifecycle(virConnectPtr conn,
1257 1258 1259 1260
                                 virNetworkPtr net,
                                 int event,
                                 int detail,
                                 void *opaque)
1261
{
1262
    daemonClientEventCallbackPtr callback = opaque;
1263 1264
    remote_network_event_lifecycle_msg data;

1265 1266
    if (callback->callbackID < 0 ||
        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
1267 1268
        return -1;

1269 1270
    VIR_DEBUG("Relaying network lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);
1271 1272 1273 1274

    /* build return data */
    memset(&data, 0, sizeof(data));
    make_nonnull_network(&data.net, net);
1275
    data.callbackID = callback->callbackID;
1276 1277 1278
    data.event = event;
    data.detail = detail;

1279
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
                                  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);

1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
static int
remoteRelayStoragePoolEventLifecycle(virConnectPtr conn,
                                     virStoragePoolPtr pool,
                                     int event,
                                     int detail,
                                     void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_storage_pool_event_lifecycle_msg data;

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

    VIR_DEBUG("Relaying storage pool lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);

    /* build return data */
    memset(&data, 0, sizeof(data));
    make_nonnull_storage_pool(&data.pool, pool);
    data.callbackID = callback->callbackID;
    data.event = event;
    data.detail = detail;

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_STORAGE_POOL_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_storage_pool_event_lifecycle_msg,
                                  &data);

    return 0;
}

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
static int
remoteRelayStoragePoolEventRefresh(virConnectPtr conn,
                                   virStoragePoolPtr pool,
                                   void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_storage_pool_event_refresh_msg data;

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

    VIR_DEBUG("Relaying storage pool refresh event callback %d",
              callback->callbackID);

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

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_STORAGE_POOL_EVENT_REFRESH,
                                  (xdrproc_t)xdr_remote_storage_pool_event_refresh_msg,
                                  &data);

    return 0;
}

1352 1353
static virConnectStoragePoolEventGenericCallback storageEventCallbacks[] = {
    VIR_STORAGE_POOL_EVENT_CALLBACK(remoteRelayStoragePoolEventLifecycle),
1354
    VIR_STORAGE_POOL_EVENT_CALLBACK(remoteRelayStoragePoolEventRefresh),
1355 1356 1357 1358
};

verify(ARRAY_CARDINALITY(storageEventCallbacks) == VIR_STORAGE_POOL_EVENT_ID_LAST);

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
static int
remoteRelayNodeDeviceEventLifecycle(virConnectPtr conn,
                                    virNodeDevicePtr dev,
                                    int event,
                                    int detail,
                                    void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_node_device_event_lifecycle_msg data;

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

    VIR_DEBUG("Relaying node device lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);

    /* build return data */
    memset(&data, 0, sizeof(data));
    make_nonnull_node_device(&data.dev, dev);
    data.callbackID = callback->callbackID;
    data.event = event;
    data.detail = detail;

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_NODE_DEVICE_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_node_device_event_lifecycle_msg,
                                  &data);

    return 0;
}

1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
static int
remoteRelayNodeDeviceEventUpdate(virConnectPtr conn,
                                 virNodeDevicePtr dev,
                                 void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_node_device_event_update_msg data;

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

    VIR_DEBUG("Relaying node device update event callback %d",
              callback->callbackID);

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

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_NODE_DEVICE_EVENT_UPDATE,
                                  (xdrproc_t)xdr_remote_node_device_event_update_msg,
                                  &data);

    return 0;
}

1419 1420
static virConnectNodeDeviceEventGenericCallback nodeDeviceEventCallbacks[] = {
    VIR_NODE_DEVICE_EVENT_CALLBACK(remoteRelayNodeDeviceEventLifecycle),
1421
    VIR_NODE_DEVICE_EVENT_CALLBACK(remoteRelayNodeDeviceEventUpdate),
1422 1423 1424 1425
};

verify(ARRAY_CARDINALITY(nodeDeviceEventCallbacks) == VIR_NODE_DEVICE_EVENT_ID_LAST);

1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
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;

1467
 error:
1468 1469 1470 1471
    VIR_FREE(data.event);
    VIR_FREE(details_p);
}

1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
static
void remoteRelayConnectionClosedEvent(virConnectPtr conn ATTRIBUTE_UNUSED, int reason, void *opaque)
{
    virNetServerClientPtr client = opaque;

    VIR_DEBUG("Relaying connection closed event, reason %d", reason);

    remote_connect_event_connection_closed_msg msg = { reason };
    remoteDispatchObjectEventSend(client, remoteProgram,
                                  REMOTE_PROC_CONNECT_EVENT_CONNECTION_CLOSED,
                                  (xdrproc_t)xdr_remote_connect_event_connection_closed_msg,
                                  &msg);
}

1486 1487 1488 1489 1490 1491 1492
/*
 * 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
 */
1493
void remoteClientFreeFunc(void *data)
1494 1495 1496 1497 1498
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
1499
        virIdentityPtr sysident = virIdentityGetSystem();
1500
        size_t i;
1501

1502 1503
        virIdentitySetCurrent(sysident);

1504 1505 1506 1507 1508
        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;
1509
            }
1510 1511 1512 1513 1514
            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");
1515
        }
1516
        VIR_FREE(priv->domainEventCallbacks);
1517

1518 1519 1520 1521 1522
        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;
1523
            }
1524 1525 1526 1527 1528 1529
            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");
1530
        }
1531
        VIR_FREE(priv->networkEventCallbacks);
1532

1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
        for (i = 0; i < priv->nstorageEventCallbacks; i++) {
            int callbackID = priv->storageEventCallbacks[i]->callbackID;
            if (callbackID < 0) {
                VIR_WARN("unexpected incomplete storage pool callback %zu", i);
                continue;
            }
            VIR_DEBUG("Deregistering remote storage pool event relay %d",
                      callbackID);
            priv->storageEventCallbacks[i]->callbackID = -1;
            if (virConnectStoragePoolEventDeregisterAny(priv->conn,
                                                        callbackID) < 0)
                VIR_WARN("unexpected storage pool event deregister failure");
        }
        VIR_FREE(priv->storageEventCallbacks);

1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
        for (i = 0; i < priv->nnodeDeviceEventCallbacks; i++) {
            int callbackID = priv->nodeDeviceEventCallbacks[i]->callbackID;
            if (callbackID < 0) {
                VIR_WARN("unexpected incomplete node device callback %zu", i);
                continue;
            }
            VIR_DEBUG("Deregistering remote node device event relay %d",
                      callbackID);
            priv->nodeDeviceEventCallbacks[i]->callbackID = -1;
            if (virConnectNodeDeviceEventDeregisterAny(priv->conn,
                                                       callbackID) < 0)
                VIR_WARN("unexpected node device event deregister failure");
        }
        VIR_FREE(priv->nodeDeviceEventCallbacks);

1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
        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);

1578 1579 1580 1581 1582 1583
        if (priv->closeRegistered) {
            if (virConnectUnregisterCloseCallback(priv->conn,
                                                  remoteRelayConnectionClosedEvent) < 0)
                VIR_WARN("unexpected close callback event deregister failure");
        }

1584
        virConnectClose(priv->conn);
1585 1586 1587

        virIdentitySetCurrent(NULL);
        virObjectUnref(sysident);
1588 1589 1590 1591 1592 1593
    }

    VIR_FREE(priv);
}


1594 1595 1596 1597 1598 1599 1600
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

1601

1602 1603
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
1604 1605 1606
{
    struct daemonClientPrivate *priv;

1607
    if (VIR_ALLOC(priv) < 0)
1608
        return NULL;
1609 1610 1611

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
1612
        virReportSystemError(errno, "%s", _("unable to init mutex"));
1613
        return NULL;
1614 1615
    }

1616
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
1617
    return priv;
1618 1619
}

1620 1621 1622
/*----- Functions. -----*/

static int
1623
remoteDispatchConnectOpen(virNetServerPtr server ATTRIBUTE_UNUSED,
1624 1625 1626 1627
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
1628 1629
{
    const char *name;
1630
    unsigned int flags;
1631
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
1632
    int rv = -1;
1633

1634 1635 1636 1637
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
1638
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
1639 1640 1641
        goto cleanup;
    }

1642 1643 1644 1645 1646 1647
    name = args->name ? *args->name : NULL;

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

1651
    priv->conn =
1652
        flags & VIR_CONNECT_RO
1653 1654
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
1655

1656
    if (priv->conn == NULL)
1657 1658 1659
        goto cleanup;

    rv = 0;
1660

1661
 cleanup:
1662
    if (rv < 0)
1663 1664
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
1665
    return rv;
1666 1667 1668 1669
}


static int
1670 1671 1672 1673
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
1674
{
1675
    virNetServerClientDelayedClose(client);
1676
    return 0;
1677 1678
}

1679

1680
static int
1681 1682
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1683
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1684
                                     virNetMessageErrorPtr rerr,
1685 1686
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
1687
{
1688
    virDomainPtr dom = NULL;
1689 1690
    char *type;
    int nparams;
1691
    int rv = -1;
1692 1693
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1694

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

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

1703
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
1704
        goto cleanup;
1705 1706 1707

    ret->type = type;
    ret->nparams = nparams;
1708 1709
    rv = 0;

1710
 cleanup:
1711
    if (rv < 0)
1712
        virNetMessageSaveError(rerr);
1713
    virObjectUnref(dom);
1714
    return rv;
1715 1716 1717
}

static int
1718 1719
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
1720
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
1721
                                           virNetMessageErrorPtr rerr,
1722 1723
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
1724
{
1725
    virDomainPtr dom = NULL;
1726
    virTypedParameterPtr params = NULL;
1727
    int nparams = 0;
1728
    int rv = -1;
1729 1730
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1731

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

1737
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1738
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1739
        goto cleanup;
1740
    }
1741
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1742
        goto cleanup;
1743
    nparams = args->nparams;
1744

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

1748
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1749
        goto cleanup;
1750

1751 1752 1753 1754
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                0) < 0)
1755 1756 1757 1758
        goto cleanup;

    rv = 0;

1759
 cleanup:
1760
    if (rv < 0)
1761
        virNetMessageSaveError(rerr);
1762
    virTypedParamsFree(params, nparams);
1763
    virObjectUnref(dom);
1764 1765 1766 1767
    return rv;
}

static int
1768 1769
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1770
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1771
                                                virNetMessageErrorPtr rerr,
1772 1773 1774 1775 1776
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1777
    int nparams = 0;
1778
    int rv = -1;
1779 1780
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1781

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

1787
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1788
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1789 1790
        goto cleanup;
    }
1791
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1792
        goto cleanup;
1793
    nparams = args->nparams;
1794

1795
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1796 1797 1798 1799 1800 1801
        goto cleanup;

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

1802 1803 1804 1805
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
1806
        goto cleanup;
1807

1808
    rv = 0;
1809

1810
 cleanup:
1811
    if (rv < 0)
1812
        virNetMessageSaveError(rerr);
1813
    virTypedParamsFree(params, nparams);
1814
    virObjectUnref(dom);
1815
    return rv;
1816 1817
}

1818
static int
1819 1820
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1821
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1822
                                virNetMessageErrorPtr rerr,
1823 1824
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1825
{
1826
    virDomainPtr dom = NULL;
1827
    virDomainMemoryStatPtr stats = NULL;
1828 1829
    int nr_stats;
    size_t i;
1830
    int rv = -1;
1831 1832
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1833

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

1839
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1840 1841
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1842
        goto cleanup;
1843 1844
    }

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

    /* Allocate stats array for making dispatch call */
1849
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
1850
        goto cleanup;
1851

1852
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1853
    if (nr_stats < 0)
1854
        goto cleanup;
1855 1856

    /* Allocate return buffer */
1857
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
1858
        goto cleanup;
1859 1860 1861 1862 1863 1864 1865

    /* 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;
1866 1867
    rv = 0;

1868
 cleanup:
1869
    if (rv < 0)
1870
        virNetMessageSaveError(rerr);
1871
    virObjectUnref(dom);
1872
    VIR_FREE(stats);
1873
    return rv;
1874 1875
}

1876
static int
1877 1878
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1879
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1880
                              virNetMessageErrorPtr rerr,
1881 1882
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1883
{
1884
    virDomainPtr dom = NULL;
1885 1886 1887 1888
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1889
    int rv = -1;
1890 1891
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1892

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

1898
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1899
        goto cleanup;
1900 1901 1902 1903 1904 1905
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1906 1907
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1908
        goto cleanup;
1909 1910 1911
    }

    ret->buffer.buffer_len = size;
1912
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1913
        goto cleanup;
1914

1915
    if (virDomainBlockPeek(dom, path, offset, size,
1916
                           ret->buffer.buffer_val, flags) < 0)
1917
        goto cleanup;
1918

1919 1920
    rv = 0;

1921
 cleanup:
1922
    if (rv < 0) {
1923
        virNetMessageSaveError(rerr);
1924 1925
        VIR_FREE(ret->buffer.buffer_val);
    }
1926
    virObjectUnref(dom);
1927
    return rv;
1928 1929
}

1930 1931 1932
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1933
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1934 1935 1936 1937 1938 1939 1940
                                    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;
1941
    int nparams = 0;
1942 1943 1944 1945 1946 1947
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1948
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1949 1950 1951 1952 1953 1954 1955
        goto cleanup;
    }

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

1956
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1957
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1958 1959
        goto cleanup;
    }
1960
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1961
        goto cleanup;
1962
    nparams = args->nparams;
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974

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

1975 1976 1977 1978 1979
    /* Serialize the block stats. */
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
1980 1981
        goto cleanup;

1982
 success:
1983 1984
    rv = 0;

1985
 cleanup:
1986
    if (rv < 0)
1987
        virNetMessageSaveError(rerr);
1988
    virTypedParamsFree(params, nparams);
1989
    virObjectUnref(dom);
1990 1991 1992
    return rv;
}

R
Richard W.M. Jones 已提交
1993
static int
1994 1995
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1996
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1997
                               virNetMessageErrorPtr rerr,
1998 1999
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
2000
{
2001
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
2002 2003 2004
    unsigned long long offset;
    size_t size;
    unsigned int flags;
2005
    int rv = -1;
2006 2007
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
2008

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

2014
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2015
        goto cleanup;
R
Richard W.M. Jones 已提交
2016 2017 2018 2019 2020
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
2021 2022
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
2023
        goto cleanup;
R
Richard W.M. Jones 已提交
2024 2025 2026
    }

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

2030
    if (virDomainMemoryPeek(dom, offset, size,
2031
                            ret->buffer.buffer_val, flags) < 0)
2032
        goto cleanup;
R
Richard W.M. Jones 已提交
2033

2034 2035
    rv = 0;

2036
 cleanup:
2037
    if (rv < 0) {
2038
        virNetMessageSaveError(rerr);
2039 2040
        VIR_FREE(ret->buffer.buffer_val);
    }
2041
    virObjectUnref(dom);
2042
    return rv;
R
Richard W.M. Jones 已提交
2043 2044
}

2045
static int
2046 2047
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
2048
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
2049
                                     virNetMessageErrorPtr rerr,
2050 2051
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
2052
{
2053 2054
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
2055
    int rv = -1;
2056 2057
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2058

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

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

2067
    if (VIR_ALLOC(seclabel) < 0)
2068 2069 2070 2071 2072 2073
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
2074
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
2075
        goto cleanup;
2076 2077
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
2078

2079 2080
    rv = 0;

2081
 cleanup:
2082
    if (rv < 0)
2083
        virNetMessageSaveError(rerr);
2084
    virObjectUnref(dom);
2085
    VIR_FREE(seclabel);
2086
    return rv;
2087 2088
}

M
Marcelo Cerri 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
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;
2099 2100
    int len, rv = -1;
    size_t i;
M
Marcelo Cerri 已提交
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
    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;
    }

2119
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
2120 2121 2122 2123 2124
        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];
2125
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0)
M
Marcelo Cerri 已提交
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
            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;

2137
 done:
M
Marcelo Cerri 已提交
2138 2139
    rv = 0;

2140
 cleanup:
M
Marcelo Cerri 已提交
2141 2142
    if (rv < 0)
        virNetMessageSaveError(rerr);
2143
    virObjectUnref(dom);
M
Marcelo Cerri 已提交
2144 2145 2146 2147
    VIR_FREE(seclabels);
    return rv;
}

2148
static int
2149 2150
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2151
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2152
                                   virNetMessageErrorPtr rerr,
2153
                                   remote_node_get_security_model_ret *ret)
2154
{
2155
    virSecurityModel secmodel;
2156
    int rv = -1;
2157 2158
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2159

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

2165
    memset(&secmodel, 0, sizeof(secmodel));
2166
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
2167 2168 2169
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
2170
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
2171 2172 2173 2174
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
2175
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
2176
        goto cleanup;
2177
    strcpy(ret->doi.doi_val, secmodel.doi);
2178

2179 2180
    rv = 0;

2181
 cleanup:
2182
    if (rv < 0)
2183
        virNetMessageSaveError(rerr);
2184
    return rv;
2185 2186
}

2187
static int
2188 2189
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2190
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2191
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
2192 2193
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
2194 2195 2196 2197 2198
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
2199 2200
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2201

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

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

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
2211
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
2212 2213 2214 2215 2216
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
2217
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2218 2219 2220 2221 2222 2223
        goto cleanup;
    }

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

E
Eric Blake 已提交
2226
    if ((num = virDomainGetVcpuPinInfo(dom,
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
                                       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;
2240 2241 2242 2243
    cpumaps = NULL;

    rv = 0;

2244
 cleanup:
2245 2246 2247
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumaps);
2248
    virObjectUnref(dom);
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
    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;

2280
 cleanup:
2281 2282
    if (rv < 0)
        virNetMessageSaveError(rerr);
2283
    virObjectUnref(dom);
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
    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)
2314
        goto cleanup;
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324

    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;
2325 2326 2327 2328
    cpumaps = NULL;

    rv = 0;

2329
 cleanup:
2330
    if (rv < 0)
2331
        virNetMessageSaveError(rerr);
2332
    VIR_FREE(cpumaps);
2333
    virObjectUnref(dom);
2334 2335 2336
    return rv;
}

2337
static int
2338 2339
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
2340
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
2341
                             virNetMessageErrorPtr rerr,
2342 2343
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
2344
{
2345
    virDomainPtr dom = NULL;
2346 2347
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
2348 2349
    int info_len;
    size_t i;
2350
    int rv = -1;
2351 2352
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2353

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

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

2362
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
2363
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
2364
        goto cleanup;
2365
    }
2366

2367 2368
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
2369
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2370 2371 2372 2373 2374
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
2375
        goto cleanup;
2376 2377
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
2378
        goto cleanup;
2379 2380 2381 2382 2383 2384 2385 2386 2387

    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)
2388
        goto cleanup;
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405

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

2407
 cleanup:
2408
    if (rv < 0) {
2409
        virNetMessageSaveError(rerr);
2410 2411 2412 2413
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
2414
    virObjectUnref(dom);
2415
    return rv;
2416 2417
}

2418
static int
2419 2420 2421 2422 2423 2424
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)
2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441
{
    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;

2442
    if ((ninfo = virDomainGetIOThreadInfo(dom, &info, args->flags)) < 0)
2443 2444
        goto cleanup;

2445
    if (ninfo > REMOTE_IOTHREAD_INFO_MAX) {
2446 2447
        virReportError(VIR_ERR_RPC,
                       _("Too many IOThreads in info: %d for limit %d"),
2448
                       ninfo, REMOTE_IOTHREAD_INFO_MAX);
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
        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++)
2484
            virDomainIOThreadInfoFree(info[i]);
2485 2486 2487 2488 2489
    VIR_FREE(info);

    return rv;
}

2490
static int
2491 2492
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2493
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2494
                                   virNetMessageErrorPtr rerr,
2495 2496
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
2497
{
2498 2499 2500 2501 2502
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2503
    int rv = -1;
2504 2505
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2506

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

2512 2513 2514 2515
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
2516
    if (VIR_ALLOC(uri_out) < 0)
2517
        goto cleanup;
2518

2519
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
2520 2521
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
2522
        goto cleanup;
2523

2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534
    /* 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;
    }
2535

2536
    rv = 0;
2537

2538
 cleanup:
2539
    if (rv < 0)
2540
        virNetMessageSaveError(rerr);
2541
    VIR_FREE(uri_out);
2542
    return rv;
2543 2544
}

2545
static int
2546 2547
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2548
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2549
                                    virNetMessageErrorPtr rerr,
2550 2551
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
2552
{
2553 2554 2555 2556 2557
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2558
    int rv = -1;
2559 2560
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2561

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

2567 2568
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
2569

2570
    /* Wacky world of XDR ... */
2571
    if (VIR_ALLOC(uri_out) < 0)
2572
        goto cleanup;
2573

2574
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
2575 2576 2577
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
2578
        goto cleanup;
2579

2580 2581 2582 2583 2584 2585
    /* 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;
2586

2587 2588
    rv = 0;

2589
 cleanup:
2590
    if (rv < 0) {
2591
        virNetMessageSaveError(rerr);
2592 2593
        VIR_FREE(uri_out);
    }
2594
    return rv;
2595 2596
}

C
Chris Lalancette 已提交
2597
static int
2598 2599
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
2600
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
2601 2602 2603
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
2604
{
2605
    virDomainPtr dom = NULL;
2606
    virTypedParameterPtr params = NULL;
2607
    int nparams = 0;
2608
    unsigned int flags;
2609
    int rv = -1;
2610 2611
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2612

2613
    if (!priv->conn) {
2614
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2615
        goto cleanup;
2616
    }
C
Chris Lalancette 已提交
2617

2618
    flags = args->flags;
C
Chris Lalancette 已提交
2619

2620
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2621
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2622 2623
        goto cleanup;
    }
2624
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2625
        goto cleanup;
2626
    nparams = args->nparams;
C
Chris Lalancette 已提交
2627

2628
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2629
        goto cleanup;
C
Chris Lalancette 已提交
2630

2631
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
2632
        goto cleanup;
C
Chris Lalancette 已提交
2633

2634 2635 2636 2637 2638 2639
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
2640 2641
    }

2642 2643 2644 2645
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
2646
        goto cleanup;
2647

2648
 success:
2649 2650
    rv = 0;

2651
 cleanup:
2652
    if (rv < 0)
2653
        virNetMessageSaveError(rerr);
2654
    virTypedParamsFree(params, nparams);
2655
    virObjectUnref(dom);
2656
    return rv;
2657 2658
}

2659 2660 2661 2662 2663 2664 2665 2666 2667 2668
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;
2669
    int nparams = 0;
2670 2671 2672 2673 2674 2675
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2676
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2677 2678 2679 2680 2681
        goto cleanup;
    }

    flags = args->flags;

2682
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
2683
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2684 2685
        goto cleanup;
    }
2686
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2687
        goto cleanup;
2688
    nparams = args->nparams;
2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703

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

2704 2705 2706 2707
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                flags) < 0)
2708 2709
        goto cleanup;

2710
 success:
2711 2712
    rv = 0;

2713
 cleanup:
2714 2715
    if (rv < 0)
        virNetMessageSaveError(rerr);
2716
    virTypedParamsFree(params, nparams);
2717
    virObjectUnref(dom);
2718 2719 2720
    return rv;
}

2721
static int
2722 2723
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
2724
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2725 2726 2727
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
2728
{
2729
    virDomainPtr dom = NULL;
2730
    virTypedParameterPtr params = NULL;
2731
    int nparams = 0;
2732
    unsigned int flags;
2733
    int rv = -1;
2734 2735
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2736

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

2742 2743
    flags = args->flags;

2744
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2745
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2746
        goto cleanup;
2747
    }
2748
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2749
        goto cleanup;
2750
    nparams = args->nparams;
2751

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

2755
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2756
        goto cleanup;
2757

2758 2759 2760 2761 2762 2763 2764 2765
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2766 2767 2768 2769
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
2770
        goto cleanup;
2771

2772
 success:
2773
    rv = 0;
2774

2775
 cleanup:
2776
    if (rv < 0)
2777
        virNetMessageSaveError(rerr);
2778
    virTypedParamsFree(params, nparams);
2779
    virObjectUnref(dom);
2780
    return rv;
2781 2782
}

2783
static int
2784 2785
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2786
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2787 2788 2789
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2790
{
2791
    virNodeCPUStatsPtr params = NULL;
2792
    size_t i;
2793
    int cpuNum = args->cpuNum;
2794
    int nparams = 0;
2795 2796
    unsigned int flags;
    int rv = -1;
2797 2798
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2799

2800
    if (!priv->conn) {
2801
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2802 2803 2804 2805 2806
        goto cleanup;
    }

    flags = args->flags;

2807
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
2808
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2809 2810
        goto cleanup;
    }
2811
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2812
        goto cleanup;
2813
    nparams = args->nparams;
2814

2815
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828
        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)
2829
        goto cleanup;
2830 2831 2832

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2833 2834
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2835 2836 2837 2838

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

2839
 success:
2840 2841
    rv = 0;

2842
 cleanup:
2843
    if (rv < 0) {
2844
        virNetMessageSaveError(rerr);
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855
        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
2856 2857
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2858
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2859 2860 2861
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2862
{
2863
    virNodeMemoryStatsPtr params = NULL;
2864
    size_t i;
2865
    int cellNum = args->cellNum;
2866
    int nparams = 0;
2867 2868
    unsigned int flags;
    int rv = -1;
2869 2870
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2871

2872
    if (!priv->conn) {
2873
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2874 2875 2876 2877 2878
        goto cleanup;
    }

    flags = args->flags;

2879
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
2880
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2881 2882
        goto cleanup;
    }
2883
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2884
        goto cleanup;
2885
    nparams = args->nparams;
2886

2887
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900
        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)
2901
        goto cleanup;
2902 2903 2904

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2905 2906
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2907 2908 2909 2910

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

2911
 success:
2912 2913
    rv = 0;

2914
 cleanup:
2915
    if (rv < 0) {
2916
        virNetMessageSaveError(rerr);
2917 2918 2919 2920 2921 2922 2923 2924 2925 2926
        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;
}

2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949
static int
remoteDispatchDomainGetPerfEvents(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                  virNetMessageErrorPtr rerr,
                                  remote_domain_get_perf_events_args *args,
                                  remote_domain_get_perf_events_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;

2950
    if (virDomainGetPerfEvents(dom, &params, &nparams, args->flags) < 0)
2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973
        goto cleanup;

    if (nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }

    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                0) < 0)
        goto cleanup;

    rv = 0;

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

2974 2975 2976
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2977
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
                                    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) {
2989
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006
        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;

3007
 cleanup:
3008 3009
    if (rv < 0)
        virNetMessageSaveError(rerr);
3010
    virObjectUnref(dom);
3011 3012 3013
    return rv;
}

3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024
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;
3025
    int nparams = 0;
3026 3027 3028 3029
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

3034
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
3035
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3036 3037 3038
        goto cleanup;
    }

3039
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3040
        goto cleanup;
3041
    nparams = args->nparams;
3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057

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

3058 3059 3060 3061 3062
    /* Serialize the block I/O tuning parameters. */
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
3063 3064
        goto cleanup;

3065
 success:
3066 3067
    rv = 0;

3068
 cleanup:
3069 3070
    if (rv < 0)
        virNetMessageSaveError(rerr);
3071
    virTypedParamsFree(params, nparams);
3072
    virObjectUnref(dom);
3073 3074
    return rv;
}
3075

D
Daniel Veillard 已提交
3076 3077
/*-------------------------------------------------------------*/

3078
static int
3079
remoteDispatchAuthList(virNetServerPtr server,
3080
                       virNetServerClientPtr client,
3081
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
3082
                       virNetMessageErrorPtr rerr,
3083
                       remote_auth_list_ret *ret)
3084
{
3085
    int rv = -1;
3086 3087
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
3088
    gid_t callerGid;
3089
    pid_t callerPid;
3090
    unsigned long long timestamp;
3091 3092 3093 3094 3095 3096

    /* 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) {
3097
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
3098
                                              &callerPid, &timestamp) < 0) {
3099 3100 3101 3102
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
3103 3104
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
3105
                            (long long) callerPid, (int) callerUid) < 0)
J
Jim Fehlig 已提交
3106 3107
                goto cleanup;
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
3108
            virNetServerClientSetAuth(client, 0);
3109
            virNetServerTrackCompletedAuth(server);
3110
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
3111
            VIR_FREE(ident);
3112 3113
        }
    }
3114

3115
    ret->types.types_len = 1;
3116
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
3117
        goto cleanup;
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131

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

3133 3134
    rv = 0;

3135
 cleanup:
3136
    if (rv < 0)
3137
        virNetMessageSaveError(rerr);
3138
    return rv;
3139 3140 3141
}


3142
#ifdef WITH_SASL
3143 3144
/*
 * Initializes the SASL session in prepare for authentication
3145
 * and gives the client a list of allowed mechanisms to choose
3146 3147
 */
static int
3148 3149
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
3150
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3151
                           virNetMessageErrorPtr rerr,
3152
                           remote_auth_sasl_init_ret *ret)
3153
{
3154 3155 3156
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3157

3158
    virMutexLock(&priv->lock);
3159

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

3167 3168
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
3169 3170
                                      virNetServerClientLocalAddrStringSASL(client),
                                      virNetServerClientRemoteAddrStringSASL(client));
3171
    if (!sasl)
3172
        goto authfail;
3173

3174
# if WITH_GNUTLS
3175
    /* Inform SASL that we've got an external SSF layer from TLS */
3176 3177 3178 3179
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
3180
            goto authfail;
3181 3182 3183 3184 3185

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
3186
            goto authfail;
3187
    }
3188
# endif
3189

3190
    if (virNetServerClientIsSecure(client))
3191
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
3192 3193
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
3194
        /* Plain TCP, better get an SSF layer */
3195 3196 3197 3198
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
3199

3200
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
3201
        goto authfail;
3202
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
3203

3204 3205
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
3206
    return 0;
3207

3208
 authfail:
3209
    virResetLastError();
3210 3211
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3212
    virNetMessageSaveError(rerr);
3213 3214 3215
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3216
    virObjectUnref(sasl);
3217
    virMutexUnlock(&priv->lock);
3218
    return -1;
3219 3220
}

3221
/*
3222 3223
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
3224
static int
3225 3226
remoteSASLFinish(virNetServerPtr server,
                 virNetServerClientPtr client)
3227
{
3228
    virIdentityPtr clnt_identity = NULL;
3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242
    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;
        }
3243
    }
3244 3245

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
3246
        return -2;
3247

3248 3249
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
3250

3251 3252 3253
    if (!(clnt_identity = virNetServerClientGetIdentity(client)))
        goto error;

3254
    virNetServerClientSetAuth(client, 0);
3255
    virNetServerTrackCompletedAuth(server);
3256
    virNetServerClientSetSASLSession(client, priv->sasl);
3257
    virIdentitySetSASLUserName(clnt_identity, identity);
3258

3259
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
3260

3261 3262 3263
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3264

3265
    virObjectUnref(clnt_identity);
3266
    virObjectUnref(priv->sasl);
3267
    priv->sasl = NULL;
3268

3269
    return 0;
3270

3271
 error:
3272 3273
    return -1;
}
3274

3275 3276 3277 3278
/*
 * This starts the SASL authentication negotiation.
 */
static int
3279
remoteDispatchAuthSaslStart(virNetServerPtr server,
3280
                            virNetServerClientPtr client,
3281
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3282
                            virNetMessageErrorPtr rerr,
3283 3284
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
3285 3286
{
    const char *serverout;
3287
    size_t serveroutlen;
3288
    int err;
3289 3290 3291
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3292
    const char *identity;
3293

3294
    virMutexLock(&priv->lock);
3295

3296 3297 3298
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
3299
        VIR_ERROR(_("client tried invalid SASL start request"));
3300
        goto authfail;
3301 3302
    }

3303 3304
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
3305 3306 3307 3308 3309 3310 3311 3312 3313
    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)
3314
        goto authfail;
3315

3316
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3317
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
3318
        goto authfail;
3319 3320 3321 3322
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3323 3324
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3325 3326 3327 3328 3329 3330 3331
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3332
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3333
    if (err == VIR_NET_SASL_CONTINUE) {
3334 3335
        ret->complete = 0;
    } else {
3336
        /* Check username whitelist ACL */
3337
        if ((err = remoteSASLFinish(server, client)) < 0) {
3338 3339 3340 3341 3342
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3343

3344 3345 3346
        ret->complete = 1;
    }

3347
    virMutexUnlock(&priv->lock);
3348
    return 0;
3349

3350
 authfail:
3351 3352 3353
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3354 3355
    goto error;

3356
 authdeny:
3357
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3358 3359 3360
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3361 3362
    goto error;

3363
 error:
3364
    virObjectUnref(priv->sasl);
3365 3366
    priv->sasl = NULL;
    virResetLastError();
3367 3368
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3369 3370 3371
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3372
    return -1;
3373 3374 3375 3376
}


static int
3377
remoteDispatchAuthSaslStep(virNetServerPtr server,
3378
                           virNetServerClientPtr client,
3379
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3380
                           virNetMessageErrorPtr rerr,
3381 3382
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
3383 3384
{
    const char *serverout;
3385
    size_t serveroutlen;
3386
    int err;
3387 3388 3389
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3390
    const char *identity;
3391

3392 3393 3394 3395 3396
    virMutexLock(&priv->lock);

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

3401
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
3402
              args->data.data_len, args->nil);
3403 3404 3405 3406 3407 3408 3409 3410
    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)
3411
        goto authfail;
3412 3413

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3414
        VIR_ERROR(_("sasl step reply data too long %d"),
3415
                  (int)serveroutlen);
3416
        goto authfail;
3417 3418 3419 3420
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3421 3422
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3423 3424 3425 3426 3427 3428 3429
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3430
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3431
    if (err == VIR_NET_SASL_CONTINUE) {
3432 3433
        ret->complete = 0;
    } else {
3434
        /* Check username whitelist ACL */
3435
        if ((err = remoteSASLFinish(server, client)) < 0) {
3436 3437 3438 3439 3440
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3441

3442 3443 3444
        ret->complete = 1;
    }

3445
    virMutexUnlock(&priv->lock);
3446
    return 0;
3447

3448
 authfail:
3449 3450 3451
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3452 3453
    goto error;

3454
 authdeny:
3455
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3456 3457 3458
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3459 3460
    goto error;

3461
 error:
3462
    virObjectUnref(priv->sasl);
3463 3464
    priv->sasl = NULL;
    virResetLastError();
3465 3466
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3467 3468 3469
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3470 3471
    return -1;
}
3472 3473 3474 3475
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3476
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3477 3478 3479 3480
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3481 3482
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3483 3484 3485 3486 3487 3488
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
3489
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3490 3491 3492 3493 3494
                            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");
3495 3496
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3497 3498 3499 3500 3501 3502
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3503
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3504 3505 3506 3507 3508
                           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");
3509 3510
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3511 3512 3513 3514
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
3515 3516 3517



3518
static int
3519
remoteDispatchAuthPolkit(virNetServerPtr server,
3520
                         virNetServerClientPtr client,
3521
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3522
                         virNetMessageErrorPtr rerr,
3523
                         remote_auth_polkit_ret *ret)
3524
{
3525
    pid_t callerPid = -1;
3526
    gid_t callerGid = -1;
3527
    uid_t callerUid = -1;
3528
    unsigned long long timestamp;
3529
    const char *action;
3530
    char *ident = NULL;
3531 3532
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3533
    int rv;
3534

3535 3536
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
3537 3538 3539
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

3540 3541
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3542
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3543 3544 3545
        goto authfail;
    }

3546
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
3547
                                          &callerPid, &timestamp) < 0) {
3548 3549 3550
        goto authfail;
    }

3551 3552 3553 3554 3555 3556
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

3557 3558
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3559

3560 3561 3562 3563 3564 3565 3566
    rv = virPolkitCheckAuth(action,
                            callerPid,
                            timestamp,
                            callerUid,
                            NULL,
                            true);
    if (rv == -1)
3567
        goto authfail;
3568
    else if (rv == -2)
3569
        goto authdeny;
3570

3571 3572 3573
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3574 3575
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
3576 3577
    ret->complete = 1;

3578
    virNetServerClientSetAuth(client, 0);
3579
    virNetServerTrackCompletedAuth(server);
3580
    virMutexUnlock(&priv->lock);
3581

3582
    return 0;
3583

3584
 error:
3585
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3586
    virMutexUnlock(&priv->lock);
3587 3588
    return -1;

3589
 authfail:
3590 3591 3592
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3593
    goto error;
3594

3595
 authdeny:
3596 3597 3598
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3599
    goto error;
3600 3601
}

3602

3603 3604 3605
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3606

3607
static int
3608 3609
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3610
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3611
                                  virNetMessageErrorPtr rerr,
3612 3613
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3614
{
3615 3616
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3617
    int rv = -1;
3618 3619
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3620

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

3626
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3627 3628
        goto cleanup;

3629 3630 3631 3632 3633 3634 3635
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
3636
        if (VIR_ALLOC(parent_p) < 0)
3637
            goto cleanup;
3638
        if (VIR_STRDUP(*parent_p, parent) < 0) {
3639 3640 3641 3642 3643 3644
            VIR_FREE(parent_p);
            goto cleanup;
        }
        ret->parent = parent_p;
    }

3645 3646
    rv = 0;

3647
 cleanup:
3648
    if (rv < 0)
3649
        virNetMessageSaveError(rerr);
3650
    virObjectUnref(dev);
3651
    return rv;
3652 3653
}

3654
static int
3655
remoteDispatchConnectRegisterCloseCallback(virNetServerPtr server ATTRIBUTE_UNUSED,
3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672
                                           virNetServerClientPtr client,
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    virMutexLock(&priv->lock);

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

    if (virConnectRegisterCloseCallback(priv->conn,
                                        remoteRelayConnectionClosedEvent,
3673
                                        client, NULL) < 0)
3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686
        goto cleanup;

    priv->closeRegistered = true;
    rv = 0;

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

static int
3687
remoteDispatchConnectUnregisterCloseCallback(virNetServerPtr server ATTRIBUTE_UNUSED,
3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715
                                             virNetServerClientPtr client,
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    virMutexLock(&priv->lock);

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

    if (virConnectUnregisterCloseCallback(priv->conn,
                                          remoteRelayConnectionClosedEvent) < 0)
        goto cleanup;

    priv->closeRegistered = false;
    rv = 0;

 cleanup:
    virMutexUnlock(&priv->lock);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}
3716 3717 3718 3719 3720

/***************************
 * Register / deregister events
 ***************************/
static int
3721
remoteDispatchConnectDomainEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
3722
                                         virNetServerClientPtr client,
3723 3724 3725
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                         remote_connect_domain_event_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
3726
{
3727
    int callbackID;
3728
    int rv = -1;
3729 3730
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3731 3732
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3733

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

3739 3740
    virMutexLock(&priv->lock);

3741 3742 3743 3744
    /* 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
3745 3746 3747 3748
     * our callback; or you can use VIR_APPEND_ELEMENT_COPY to avoid
     * clearing 'callback' and having to juggle the pointer
     * between 'ref' and 'callback'.
     */
3749 3750 3751 3752 3753
    if (VIR_ALLOC(callback) < 0)
        goto cleanup;
    callback->client = client;
    callback->eventID = VIR_DOMAIN_EVENT_ID_LIFECYCLE;
    callback->callbackID = -1;
3754
    callback->legacy = true;
3755 3756 3757 3758
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3759
        goto cleanup;
O
Osier Yang 已提交
3760

3761
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3762 3763 3764
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
3765 3766 3767 3768 3769
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3770
        goto cleanup;
3771
    }
O
Osier Yang 已提交
3772

3773
    ref->callbackID = callbackID;
3774

3775 3776
    rv = 0;

3777
 cleanup:
3778
    VIR_FREE(callback);
3779
    if (rv < 0)
3780 3781
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3782
    return rv;
O
Osier Yang 已提交
3783 3784
}

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

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

3803 3804
    virMutexLock(&priv->lock);

3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815
    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);
3816
        goto cleanup;
3817
    }
3818

3819
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3820
        goto cleanup;
3821

3822 3823
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3824

3825 3826
    rv = 0;

3827
 cleanup:
3828
    if (rv < 0)
3829 3830
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3831 3832 3833 3834
    return rv;
}

static void
3835
remoteDispatchObjectEventSend(virNetServerClientPtr client,
3836
                              virNetServerProgramPtr program,
3837 3838 3839 3840
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3841
    virNetMessagePtr msg;
3842

3843
    if (!(msg = virNetMessageNew(false)))
3844
        goto cleanup;
3845

3846 3847 3848 3849 3850 3851
    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;
3852

3853
    if (virNetMessageEncodeHeader(msg) < 0)
3854 3855
        goto cleanup;

3856 3857
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3858

3859 3860
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3861

3862
    xdr_free(proc, data);
3863 3864
    return;

3865
 cleanup:
3866
    virNetMessageFree(msg);
3867
    xdr_free(proc, data);
3868 3869
}

3870
static int
3871 3872
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3873
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3874
                             virNetMessageErrorPtr rerr,
3875 3876
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3877
{
3878 3879 3880
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3881
    int rv = -1;
3882 3883
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3884

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

3890
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3891
        goto cleanup;
3892

3893
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3894
        goto cleanup;
3895

3896 3897 3898
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3899 3900
    rv = 0;

3901
 cleanup:
3902
    if (rv < 0)
3903
        virNetMessageSaveError(rerr);
3904
    virObjectUnref(secret);
3905
    return rv;
3906 3907
}

3908
static int
3909 3910
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3911
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3912
                             virNetMessageErrorPtr rerr,
3913 3914 3915 3916 3917
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3918 3919
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3920

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

3926
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3927 3928 3929 3930 3931 3932 3933
        goto cleanup;

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

    rv = 0;

3934
 cleanup:
3935
    if (rv < 0)
3936
        virNetMessageSaveError(rerr);
3937
    virObjectUnref(dom);
3938 3939 3940
    return rv;
}

3941 3942 3943 3944 3945 3946

/* 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.  */
3947
static int
3948
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3949
                                            virNetServerClientPtr client,
3950 3951 3952
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            remote_connect_domain_event_register_any_args *args)
3953 3954
{
    int callbackID;
3955
    int rv = -1;
3956 3957
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3958 3959
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3960

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

3966 3967
    virMutexLock(&priv->lock);

3968 3969 3970 3971
    /* 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 ||
3972
        args->eventID < 0) {
3973 3974
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3975
        goto cleanup;
3976 3977
    }

3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988
    /* 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;
3989
    callback->legacy = true;
3990 3991 3992 3993
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3994
        goto cleanup;
3995

3996
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3997 3998 3999
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
4000 4001 4002 4003 4004
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
4005
        goto cleanup;
4006
    }
4007

4008
    ref->callbackID = callbackID;
4009

4010 4011
    rv = 0;

4012
 cleanup:
4013
    VIR_FREE(callback);
4014
    if (rv < 0)
4015 4016
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
4017
    return rv;
4018 4019 4020
}


4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047
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;

4048
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST || args->eventID < 0) {
4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087
        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;

4088
 cleanup:
4089 4090 4091
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
4092
    virObjectUnref(dom);
4093 4094 4095 4096 4097
    virMutexUnlock(&priv->lock);
    return rv;
}


4098
static int
4099
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
4100
                                              virNetServerClientPtr client,
4101 4102 4103
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              remote_connect_domain_event_deregister_any_args *args)
4104 4105
{
    int callbackID = -1;
4106
    int rv = -1;
4107
    size_t i;
4108 4109
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4110

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

4116 4117
    virMutexLock(&priv->lock);

4118 4119 4120 4121
    /* 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 ||
4122
        args->eventID < 0) {
4123 4124
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
4125
        goto cleanup;
4126 4127
    }

4128 4129 4130 4131 4132 4133
    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->eventID == args->eventID) {
            callbackID = priv->domainEventCallbacks[i]->callbackID;
            break;
        }
    }
4134
    if (callbackID < 0) {
4135 4136
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event %d not registered"), args->eventID);
4137
        goto cleanup;
4138 4139
    }

4140
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
4141
        goto cleanup;
4142

4143 4144
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
4145

4146 4147
    rv = 0;

4148
 cleanup:
4149
    if (rv < 0)
4150 4151
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
4152
    return rv;
4153 4154
}

4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193

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;

4194
 cleanup:
4195 4196 4197 4198 4199 4200 4201
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}


C
Chris Lalancette 已提交
4202
static int
4203 4204 4205 4206 4207 4208
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 已提交
4209
{
4210
    virDomainPtr dom = NULL;
4211
    int rv = -1;
4212 4213
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
4214

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

4220
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4221
        goto cleanup;
C
Chris Lalancette 已提交
4222

4223
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
4224
                                    args->flags) < 0)
4225
        goto cleanup;
C
Chris Lalancette 已提交
4226

4227
    rv = 0;
C
Chris Lalancette 已提交
4228

4229
 cleanup:
4230
    if (rv < 0)
4231
        virNetMessageSaveError(rerr);
4232
    virObjectUnref(dom);
4233
    return rv;
C
Chris Lalancette 已提交
4234 4235
}

4236

4237
static int
4238 4239
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
4240
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
4241
                                  virNetMessageErrorPtr rerr,
4242 4243 4244 4245 4246 4247
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
4248
    char *xmlin;
4249 4250 4251
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4252 4253
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4254

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

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

4263
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4264 4265
    dname = args->dname == NULL ? NULL : *args->dname;

4266
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279
                                       &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;

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


static int
4289 4290
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4291
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4292
                                    virNetMessageErrorPtr rerr,
4293 4294 4295 4296 4297 4298 4299 4300 4301
                                    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;
4302 4303
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4304

4305
    if (!priv->conn) {
4306
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4307 4308 4309 4310 4311 4312 4313
        goto cleanup;
    }

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

    /* Wacky world of XDR ... */
4314
    if (VIR_ALLOC(uri_out) < 0)
4315 4316
        goto cleanup;

4317
    if (virDomainMigratePrepare3(priv->conn,
4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334
                                 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;

4335
 cleanup:
4336
    if (rv < 0) {
4337
        virNetMessageSaveError(rerr);
4338 4339 4340 4341 4342
        VIR_FREE(uri_out);
    }
    return rv;
}

4343

4344
static int
4345 4346
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4347
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4348
                                    virNetMessageErrorPtr rerr,
4349 4350 4351 4352
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
4353
    char *xmlin;
4354
    char *dname;
4355 4356
    char *uri;
    char *dconnuri;
4357 4358 4359
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4360 4361
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4362

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

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

4371
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4372
    dname = args->dname == NULL ? NULL : *args->dname;
4373 4374
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
4375

4376
    if (virDomainMigratePerform3(dom, xmlin,
4377 4378 4379
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
4380
                                 dconnuri, uri,
4381 4382 4383 4384 4385 4386 4387 4388 4389 4390
                                 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;

4391
 cleanup:
4392
    if (rv < 0)
4393
        virNetMessageSaveError(rerr);
4394
    virObjectUnref(dom);
4395 4396 4397 4398 4399
    return rv;
}


static int
4400 4401
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
4402
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
4403
                                   virNetMessageErrorPtr rerr,
4404 4405 4406 4407 4408 4409
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
4410 4411
    char *uri;
    char *dconnuri;
4412
    int rv = -1;
4413 4414
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4415

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

4421 4422 4423
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

4424
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
4425 4426 4427 4428 4429 4430
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
4431 4432
        goto cleanup;

4433
    make_nonnull_domain(&ret->dom, dom);
4434 4435 4436 4437 4438 4439 4440 4441

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

    rv = 0;

4442
 cleanup:
4443
    if (rv < 0) {
4444
        virNetMessageSaveError(rerr);
4445 4446
        VIR_FREE(cookieout);
    }
4447
    virObjectUnref(dom);
4448 4449 4450 4451 4452
    return rv;
}


static int
4453 4454
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4455
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4456 4457
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
4458 4459 4460
{
    virDomainPtr dom = NULL;
    int rv = -1;
4461 4462
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4463

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

4469
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4470 4471 4472 4473 4474 4475 4476 4477 4478 4479
        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;

4480
 cleanup:
4481
    if (rv < 0)
4482
        virNetMessageSaveError(rerr);
4483
    virObjectUnref(dom);
4484 4485 4486 4487
    return rv;
}


4488 4489 4490 4491 4492 4493
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)
4494 4495 4496 4497 4498 4499
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

4500 4501 4502 4503 4504 4505 4506 4507 4508 4509
    /* 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;
    }

4510
    if (!priv->conn) {
4511
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4512 4513 4514 4515 4516
        goto cleanup;
    }

    switch (args->feature) {
    case VIR_DRV_FEATURE_FD_PASSING:
4517
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
4518
    case VIR_DRV_FEATURE_REMOTE_CLOSE_CALLBACK:
4519 4520 4521 4522
        supported = 1;
        break;

    default:
4523
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
4524 4525 4526 4527
            goto cleanup;
        break;
    }

4528
 done:
4529 4530 4531
    ret->supported = supported;
    rv = 0;

4532
 cleanup:
4533 4534 4535 4536 4537 4538
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552
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) {
4553
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570
        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;

4571
 cleanup:
4572 4573 4574
    VIR_FORCE_CLOSE(fd);
    if (rv < 0)
        virNetMessageSaveError(rerr);
4575
    virObjectUnref(dom);
4576 4577 4578
    return rv;
}

4579

4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600
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;

4601 4602 4603
    if ((fd = virDomainOpenGraphicsFD(dom,
                                      args->idx,
                                      args->flags)) < 0)
4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614
        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);
4615
    if (rv < 0)
4616 4617
        virNetMessageSaveError(rerr);

4618
    virObjectUnref(dom);
4619 4620
    return rv;
}
4621 4622


4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633
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;
4634
    int nparams = 0;
4635 4636 4637 4638 4639 4640
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4641
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4642 4643 4644 4645 4646
        goto cleanup;
    }

    flags = args->flags;

4647
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
4648
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4649 4650
        goto cleanup;
    }
4651
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4652
        goto cleanup;
4653
    nparams = args->nparams;
4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668

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

4669 4670 4671 4672
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                flags) < 0)
4673 4674
        goto cleanup;

4675
 success:
4676 4677
    rv = 0;

4678
 cleanup:
4679 4680
    if (rv < 0)
        virNetMessageSaveError(rerr);
4681
    virTypedParamsFree(params, nparams);
4682
    virObjectUnref(dom);
4683 4684
    return rv;
}
4685

4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701
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) {
4702
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4703 4704 4705 4706
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
4707
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4708 4709 4710
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
4711
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
4712 4713 4714 4715
        goto cleanup;
    }

    if (args->nparams > 0 &&
4716
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730
        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;

4731 4732 4733 4734
    if (virTypedParamsSerialize(params, args->nparams * args->ncpus,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
4735 4736
        goto cleanup;

4737
 success:
4738 4739
    rv = 0;
    ret->nparams = percpu_len;
4740
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
4741
        size_t i;
4742 4743 4744 4745 4746 4747

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

4749
 cleanup:
4750 4751
    if (rv < 0)
         virNetMessageSaveError(rerr);
4752
    virTypedParamsFree(params, args->ncpus * args->nparams);
4753
    virObjectUnref(dom);
4754 4755 4756
    return rv;
}

4757 4758 4759 4760 4761 4762 4763
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)
4764 4765 4766 4767
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
4768
    int len = 0;
4769 4770 4771 4772
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4773
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4774 4775 4776 4777 4778 4779 4780
        goto cleanup;
    }

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
4781 4782
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
4783 4784 4785 4786
        goto cleanup;
    }

    if (args->maxerrors &&
4787
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803
        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;

4804
 cleanup:
4805 4806
    if (rv < 0)
        virNetMessageSaveError(rerr);
4807
    virObjectUnref(dom);
4808
    if (errors && len > 0) {
4809
        size_t i;
4810 4811 4812 4813 4814 4815 4816
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

4817

4818 4819 4820 4821 4822 4823 4824 4825 4826
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;
4827
    int nparams = 0;
4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839
    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;

4840
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
4841 4842 4843
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
4844
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4845
        goto cleanup;
4846
    nparams = args->nparams;
4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858

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

4859 4860 4861 4862
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
4863 4864
        goto cleanup;

4865
 success:
4866 4867
    rv = 0;

4868
 cleanup:
4869 4870
    if (rv < 0)
        virNetMessageSaveError(rerr);
4871
    virTypedParamsFree(params, nparams);
4872 4873 4874
    return rv;
}

4875 4876 4877 4878 4879 4880 4881 4882 4883
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;
4884
    unsigned int online = 0;
4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897
    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;

4898 4899
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
4900 4901 4902 4903
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
4904
    if (args->need_map) {
4905 4906 4907 4908 4909 4910 4911 4912 4913 4914
        ret->cpumap.cpumap_len = VIR_CPU_MAPLEN(cpunum);
        ret->cpumap.cpumap_val = (char *) cpumap;
        cpumap = NULL;
    }

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

    rv = 0;

4915
 cleanup:
4916 4917 4918 4919 4920 4921
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumap);
    return rv;
}

4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954
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
     */
4955
    for (i = 0; i < msg->nfds; i++)
4956 4957 4958 4959 4960 4961 4962 4963 4964
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

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

    rv = 1;

4965
 cleanup:
4966 4967
    if (rv < 0)
        virNetMessageSaveError(rerr);
4968
    virObjectUnref(dom);
4969 4970 4971
    return rv;
}

4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998
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;

4999 5000 5001 5002 5003 5004 5005
    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;
    }

5006 5007 5008 5009
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                0) < 0)
5010 5011 5012 5013
        goto cleanup;

    rv = 0;

5014
 cleanup:
5015 5016 5017
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virTypedParamsFree(params, nparams);
5018
    virObjectUnref(dom);
5019 5020 5021
    return rv;
}

5022
static int
5023 5024 5025 5026 5027 5028
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)
5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044
{
    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;
    }

5045 5046 5047 5048 5049 5050 5051
    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;
    }

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

5055 5056 5057
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070
        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;

5071
 cleanup:
5072 5073 5074
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5075
    virObjectUnref(dom);
5076 5077 5078 5079
    return rv;
}

static int
5080 5081 5082 5083 5084 5085
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)
5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100
{
    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;
    }

5101 5102 5103 5104 5105 5106 5107
    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;
    }

5108 5109 5110
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5111 5112 5113
        goto cleanup;

    /* Wacky world of XDR ... */
5114
    if (VIR_ALLOC(uri_out) < 0)
5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129
        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;

5130
 cleanup:
5131 5132 5133 5134 5135 5136 5137 5138 5139
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(uri_out);
    }
    return rv;
}

static int
5140 5141 5142 5143 5144 5145
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)
5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161
{
    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;
    }

5162 5163 5164 5165 5166 5167 5168
    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;
    }

5169 5170 5171
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192
        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;

5193
 cleanup:
5194 5195 5196 5197 5198 5199 5200 5201
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
        if (stream) {
            virStreamAbort(st);
            daemonFreeClientStream(client, stream);
        } else {
5202
            virObjectUnref(st);
5203 5204 5205 5206 5207 5208 5209
        }
    }
    return rv;
}


static int
5210 5211 5212 5213 5214 5215
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)
5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231
{
    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;
    }

5232 5233 5234 5235 5236 5237 5238
    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;
    }

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

5242 5243 5244
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260
        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;

5261
 cleanup:
5262 5263 5264
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5265
    virObjectUnref(dom);
5266 5267 5268 5269 5270
    return rv;
}


static int
5271 5272 5273 5274 5275 5276
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)
5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291
{
    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;
    }

5292 5293 5294 5295 5296 5297 5298
    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;
    }

5299 5300 5301
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318
        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;

5319
 cleanup:
5320 5321 5322 5323 5324
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
    }
5325
    virObjectUnref(dom);
5326 5327 5328 5329 5330
    return rv;
}


static int
5331 5332 5333 5334 5335
remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_confirm3_params_args *args)
5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348
{
    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;
    }

5349 5350 5351 5352 5353 5354 5355
    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;
    }

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

5359 5360 5361
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5362 5363 5364 5365 5366 5367 5368 5369 5370 5371
        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;

5372
 cleanup:
5373 5374 5375
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5376
    virObjectUnref(dom);
5377 5378 5379 5380
    return rv;
}


5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424
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;

5425
 cleanup:
5426 5427
    if (rv < 0)
        virNetMessageSaveError(rerr);
5428
    virStringListFree(models);
5429 5430 5431 5432
    return rv;
}


5433 5434 5435 5436 5437 5438 5439
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)
5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469
{
    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;

5470
 cleanup:
5471
    for (i = 0; i < nfiles; i++)
5472 5473 5474 5475
        VIR_FORCE_CLOSE(files[i]);
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5476
    virObjectUnref(dom);
5477 5478 5479 5480
    return rv;
}


5481 5482 5483 5484 5485 5486
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)
5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519
{
    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;

5520
 cleanup:
5521
    for (i = 0; i < nfiles; i++)
5522 5523 5524 5525
        VIR_FORCE_CLOSE(files[i]);
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5526
    virObjectUnref(dom);
5527 5528 5529 5530
    return rv;
}


5531 5532
static int
remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5533
                                             virNetServerClientPtr client,
5534 5535 5536
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                             remote_connect_network_event_register_any_args *args,
5537
                                             remote_connect_network_event_register_any_ret *ret)
5538 5539 5540
{
    int callbackID;
    int rv = -1;
5541 5542
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
5543 5544
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
5545
    virNetworkPtr net = NULL;
5546 5547 5548 5549 5550 5551 5552 5553

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

    virMutexLock(&priv->lock);

5554 5555 5556 5557
    if (args->net &&
        !(net = get_nonnull_network(priv->conn, *args->net)))
        goto cleanup;

5558 5559 5560
    if (args->eventID >= VIR_NETWORK_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network event ID %d"), args->eventID);
5561 5562 5563
        goto cleanup;
    }

5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578
    /* 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)
5579 5580 5581
        goto cleanup;

    if ((callbackID = virConnectNetworkEventRegisterAny(priv->conn,
5582
                                                        net,
5583 5584
                                                        args->eventID,
                                                        networkEventCallbacks[args->eventID],
5585 5586 5587 5588 5589
                                                        ref,
                                                        remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->networkEventCallbacks,
                     priv->nnetworkEventCallbacks, 1);
        callback = ref;
5590
        goto cleanup;
5591
    }
5592

5593
    ref->callbackID = callbackID;
5594
    ret->callbackID = callbackID;
5595 5596 5597

    rv = 0;

5598
 cleanup:
5599
    VIR_FREE(callback);
5600 5601
    if (rv < 0)
        virNetMessageSaveError(rerr);
5602
    virObjectUnref(net);
5603 5604 5605 5606 5607 5608 5609
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5610
                                               virNetServerClientPtr client,
5611 5612
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
5613
                                               remote_connect_network_event_deregister_any_args *args)
5614 5615
{
    int rv = -1;
5616
    size_t i;
5617 5618 5619 5620 5621 5622 5623 5624 5625 5626
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    virMutexLock(&priv->lock);

5627
    for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
5628
        if (priv->networkEventCallbacks[i]->callbackID == args->callbackID)
5629 5630
            break;
    }
5631
    if (i == priv->nnetworkEventCallbacks) {
5632
        virReportError(VIR_ERR_INTERNAL_ERROR,
5633 5634
                       _("network event callback %d not registered"),
                       args->callbackID);
5635 5636 5637
        goto cleanup;
    }

5638
    if (virConnectNetworkEventDeregisterAny(priv->conn, args->callbackID) < 0)
5639 5640
        goto cleanup;

5641 5642
    VIR_DELETE_ELEMENT(priv->networkEventCallbacks, i,
                       priv->nnetworkEventCallbacks);
5643 5644 5645

    rv = 0;

5646
 cleanup:
5647 5648 5649 5650 5651 5652
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}

5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773
static int
remoteDispatchConnectStoragePoolEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                 virNetServerClientPtr client,
                                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                 virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                                 remote_connect_storage_pool_event_register_any_args *args,
                                                 remote_connect_storage_pool_event_register_any_ret *ret)
{
    int callbackID;
    int rv = -1;
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virStoragePoolPtr  pool = NULL;

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

    virMutexLock(&priv->lock);

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

    if (args->eventID >= VIR_STORAGE_POOL_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported storage pool 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->storageEventCallbacks,
                           priv->nstorageEventCallbacks,
                           callback) < 0)
        goto cleanup;

    if ((callbackID = virConnectStoragePoolEventRegisterAny(priv->conn,
                                                            pool,
                                                            args->eventID,
                                                            storageEventCallbacks[args->eventID],
                                                            ref,
                                                            remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->storageEventCallbacks,
                     priv->nstorageEventCallbacks, 1);
        callback = ref;
        goto cleanup;
    }

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

    rv = 0;

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

static int
remoteDispatchConnectStoragePoolEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                               virNetServerClientPtr client,
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                               remote_connect_storage_pool_event_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->nstorageEventCallbacks; i++) {
        if (priv->storageEventCallbacks[i]->callbackID == args->callbackID)
            break;
    }
    if (i == priv->nstorageEventCallbacks) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("storage pool event callback %d not registered"),
                       args->callbackID);
        goto cleanup;
    }

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

    VIR_DELETE_ELEMENT(priv->storageEventCallbacks, i,
                       priv->nstorageEventCallbacks);

    rv = 0;

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

5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893
static int
remoteDispatchConnectNodeDeviceEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client,
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                                remote_connect_node_device_event_register_any_args *args,
                                                remote_connect_node_device_event_register_any_ret *ret)
{
    int callbackID;
    int rv = -1;
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virNodeDevicePtr  dev = NULL;

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

    virMutexLock(&priv->lock);

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

    if (args->eventID >= VIR_NODE_DEVICE_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported node device 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->nodeDeviceEventCallbacks,
                           priv->nnodeDeviceEventCallbacks,
                           callback) < 0)
        goto cleanup;

    if ((callbackID = virConnectNodeDeviceEventRegisterAny(priv->conn,
                                                           dev,
                                                           args->eventID,
                                                           nodeDeviceEventCallbacks[args->eventID],
                                                           ref,
                                                           remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->nodeDeviceEventCallbacks,
                     priv->nnodeDeviceEventCallbacks, 1);
        callback = ref;
        goto cleanup;
    }

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

    rv = 0;

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

static int
remoteDispatchConnectNodeDeviceEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                  virNetServerClientPtr client,
                                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                  virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                                  remote_connect_node_device_event_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->nnodeDeviceEventCallbacks; i++) {
        if (priv->nodeDeviceEventCallbacks[i]->callbackID == args->callbackID)
            break;
    }
    if (i == priv->nnodeDeviceEventCallbacks) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("node device event callback %d not registered"),
                       args->callbackID);
        goto cleanup;
    }

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

    VIR_DELETE_ELEMENT(priv->nodeDeviceEventCallbacks, i,
                       priv->nnodeDeviceEventCallbacks);

    rv = 0;

 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}
5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956

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;

5957
 cleanup:
5958 5959 5960
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5961
    virObjectUnref(dom);
5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005
    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;

6006
 cleanup:
6007 6008 6009 6010 6011 6012
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}

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
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);
6046
    virObjectUnref(dom);
6047 6048
    return rv;
}
6049

M
Michal Privoznik 已提交
6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097

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;
6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112
}

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

6113
    if (VIR_STRDUP(lease_dst->iface, lease_src->iface) < 0 ||
J
Ján Tomko 已提交
6114
        VIR_STRDUP(lease_dst->ipaddr, lease_src->ipaddr) < 0)
6115 6116
        goto error;

J
Ján Tomko 已提交
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
    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;
6142 6143 6144 6145

    return 0;

 error:
J
Ján Tomko 已提交
6146 6147 6148 6149 6150 6151 6152 6153
    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);
6154 6155 6156 6157 6158
    VIR_FREE(mac_tmp);
    VIR_FREE(iaid_tmp);
    VIR_FREE(hostname_tmp);
    VIR_FREE(clientid_tmp);
    VIR_FREE(lease_dst->ipaddr);
6159
    VIR_FREE(lease_dst->iface);
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
    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,
6188
                                           args->mac ? *args->mac : NULL,
6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222
                                           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);
6223
    if (leases && nleases > 0)
6224 6225
        for (i = 0; i < nleases; i++)
            virNetworkDHCPLeaseFree(leases[i]);
6226
    VIR_FREE(leases);
6227
    virObjectUnref(net);
6228
    return rv;
M
Michal Privoznik 已提交
6229 6230 6231
}


6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277
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"),
C
Cole Robinson 已提交
6278
                       nrecords, REMOTE_CONNECT_GET_ALL_DOMAIN_STATS_MAX);
6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292
        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);

6293 6294 6295 6296 6297
            if (virTypedParamsSerialize(retStats[i]->params,
                                        retStats[i]->nparams,
                                        (virTypedParameterRemotePtr *) &dst->params.params_val,
                                        &dst->params.params_len,
                                        VIR_TYPED_PARAM_STRING_OKAY) < 0)
6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311
                goto cleanup;
        }
    } else {
        ret->retStats.retStats_len = 0;
        ret->retStats.retStats_val = NULL;
    }

    rv = 0;

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

    virDomainStatsRecordListFree(retStats);
6312
    virObjectListFree(doms);
6313 6314 6315 6316 6317

    return rv;
}


M
Michal Privoznik 已提交
6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354
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;
}


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 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457
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);
        }
    }
6458
    virObjectUnref(dom);
6459 6460 6461 6462 6463 6464 6465 6466 6467
    if (ninfo >= 0)
        for (i = 0; i < ninfo; i++)
            virDomainFSInfoFree(info[i]);
    VIR_FREE(info);

    return rv;
}


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

6494 6495 6496
        if (iface->hwaddr &&
            (VIR_ALLOC(iface_ret->hwaddr) < 0 ||
             VIR_STRDUP(*iface_ret->hwaddr, iface->hwaddr) < 0))
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
            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);
6532 6533 6534 6535
            if (iface_ret->hwaddr) {
                VIR_FREE(*iface_ret->hwaddr);
                VIR_FREE(iface_ret->hwaddr);
            }
6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 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 6593 6594 6595 6596
            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;
}


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 6631 6632 6633 6634
static int
remoteDispatchStorageVolGetInfoFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_storage_vol_get_info_flags_args *args,
                                     remote_storage_vol_get_info_flags_ret *ret)
{
    int rv = -1;
    virStorageVolPtr vol = NULL;
    virStorageVolInfo tmp;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    if (!(vol = get_nonnull_storage_vol(priv->conn, args->vol)))
        goto cleanup;

    if (virStorageVolGetInfoFlags(vol, &tmp, args->flags) < 0)
        goto cleanup;

    ret->type = tmp.type;
    ret->capacity = tmp.capacity;
    ret->allocation = tmp.allocation;
    rv = 0;

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


6635 6636 6637 6638 6639 6640 6641 6642 6643
/*----- 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
6644
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
6645 6646
{
    virDomainPtr dom;
6647
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
6648 6649 6650 6651 6652 6653 6654 6655
    /* 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
6656
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
6657
{
6658
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
6659 6660
}

D
Daniel Veillard 已提交
6661
static virInterfacePtr
6662
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
6663
{
6664
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
6665 6666
}

6667
static virStoragePoolPtr
6668
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
6669
{
6670 6671
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
6672 6673 6674
}

static virStorageVolPtr
6675
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
6676 6677
{
    virStorageVolPtr ret;
6678 6679
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
6680 6681 6682
    return ret;
}

6683
static virSecretPtr
6684
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
6685
{
6686
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
6687 6688
}

6689
static virNWFilterPtr
6690
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
6691
{
6692
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
6693 6694
}

C
Chris Lalancette 已提交
6695
static virDomainSnapshotPtr
6696
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
6697
{
6698
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
6699 6700
}

6701 6702 6703 6704 6705 6706
static virNodeDevicePtr
get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev)
{
    return virGetNodeDevice(conn, dev.name);
}

6707 6708
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
6709
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
6710 6711
{
    dom_dst->id = dom_src->id;
6712
    ignore_value(VIR_STRDUP_QUIET(dom_dst->name, dom_src->name));
6713
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
6714 6715 6716
}

static void
6717
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
6718
{
6719
    ignore_value(VIR_STRDUP_QUIET(net_dst->name, net_src->name));
6720
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
6721 6722
}

D
Daniel Veillard 已提交
6723
static void
6724 6725
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
6726
{
6727 6728
    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 已提交
6729 6730
}

6731
static void
6732
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
6733
{
6734
    ignore_value(VIR_STRDUP_QUIET(pool_dst->name, pool_src->name));
6735
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
6736 6737 6738
}

static void
6739
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
6740
{
6741 6742 6743
    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));
6744
}
6745 6746

static void
6747
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
6748
{
6749
    ignore_value(VIR_STRDUP_QUIET(dev_dst->name, dev_src->name));
6750
}
6751 6752

static void
6753
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
6754
{
6755
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
6756
    secret_dst->usageType = secret_src->usageType;
6757
    ignore_value(VIR_STRDUP_QUIET(secret_dst->usageID, secret_src->usageID));
6758
}
6759 6760

static void
6761
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
6762
{
6763
    ignore_value(VIR_STRDUP_QUIET(nwfilter_dst->name, nwfilter_src->name));
6764
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
6765
}
C
Chris Lalancette 已提交
6766 6767

static void
6768
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
6769
{
6770
    ignore_value(VIR_STRDUP_QUIET(snapshot_dst->name, snapshot_src->name));
6771
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
6772
}
6773 6774 6775 6776 6777 6778 6779 6780

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;
6781
    size_t i = 0;
6782

6783
    if (VIR_ALLOC_N(val, nerrors) < 0)
6784
        goto error;
6785 6786

    for (i = 0; i < nerrors; i++) {
6787 6788
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
6789 6790 6791 6792 6793 6794 6795 6796
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

6797
 error:
6798
    if (val) {
6799
        size_t j;
6800 6801 6802 6803 6804 6805
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}