remote_daemon_dispatch.c 240.8 KB
Newer Older
1
/*
2
 * remote_daemon_dispatch.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007-2018 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
 */

#include <config.h>

23
#include "virerror.h"
24

25 26
#include "remote_daemon_dispatch.h"
#include "remote_daemon.h"
27 28
#include "libvirt_internal.h"
#include "datatypes.h"
29
#include "viralloc.h"
30
#include "virlog.h"
31
#include "remote_daemon_stream.h"
32
#include "viruuid.h"
33
#include "vircommand.h"
34
#include "virnetserverservice.h"
J
Jim Fehlig 已提交
35
#include "virnetserver.h"
36
#include "virfile.h"
37
#include "virtypedparam.h"
38
#include "virdbus.h"
39
#include "virprocess.h"
40 41
#include "remote_protocol.h"
#include "qemu_protocol.h"
42
#include "lxc_protocol.h"
43
#include "virstring.h"
44
#include "object_event.h"
45 46
#include "domain_conf.h"
#include "network_conf.h"
47
#include "virprobe.h"
48
#include "viraccessapicheck.h"
49
#include "viraccessapicheckqemu.h"
50
#include "virpolkit.h"
51
#include "virthreadjob.h"
52
#include "configmake.h"
53
#include "access/viraccessapicheck.h"
54 55

#define VIR_FROM_THIS VIR_FROM_RPC
56

57 58
VIR_LOG_INIT("daemon.remote");

59
#if SIZEOF_LONG < 8
60 61 62 63
# define HYPER_TO_TYPE(_type, _to, _from) \
    do { \
        if ((_from) != (_type)(_from)) { \
            virReportError(VIR_ERR_OVERFLOW, \
64
                           _("conversion from hyper to %s overflowed"), \
65 66 67 68
                           #_type); \
            goto cleanup; \
        } \
        (_to) = (_from); \
69 70 71 72 73 74 75 76 77
    } 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

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

86 87
static virDomainPtr get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network(virConnectPtr conn, remote_nonnull_network network);
88
static virNetworkPortPtr get_nonnull_network_port(virConnectPtr conn, remote_nonnull_network_port port);
89 90 91 92 93
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);
94
static virNWFilterBindingPtr get_nonnull_nwfilter_binding(virConnectPtr conn, remote_nonnull_nwfilter_binding binding);
95
static virDomainCheckpointPtr get_nonnull_domain_checkpoint(virDomainPtr dom, remote_nonnull_domain_checkpoint checkpoint);
96
static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot);
97
static virNodeDevicePtr get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev);
98 99 100 101 102 103 104 105 106 107 108 109
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_network_port(remote_nonnull_network_port *port_dst, virNetworkPortPtr port_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_nwfilter_binding(remote_nonnull_nwfilter_binding *binding_dst, virNWFilterBindingPtr binding_src);
static void make_nonnull_domain_checkpoint(remote_nonnull_domain_checkpoint *checkpoint_dst, virDomainCheckpointPtr checkpoint_src);
static void make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src);
110

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

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
static virConnectPtr
remoteGetHypervisorConn(virNetServerClientPtr client);
static virConnectPtr
remoteGetInterfaceConn(virNetServerClientPtr client);
static virConnectPtr
remoteGetNetworkConn(virNetServerClientPtr client);
static virConnectPtr
remoteGetNodeDevConn(virNetServerClientPtr client);
static virConnectPtr
remoteGetNWFilterConn(virNetServerClientPtr client);
static virConnectPtr
remoteGetSecretConn(virNetServerClientPtr client);
static virConnectPtr
remoteGetStorageConn(virNetServerClientPtr client);


133
#include "remote_daemon_dispatch_stubs.h"
134 135
#include "qemu_daemon_dispatch_stubs.h"
#include "lxc_daemon_dispatch_stubs.h"
C
Chris Lalancette 已提交
136 137


138 139
/* Prototypes */
static void
140
remoteDispatchObjectEventSend(virNetServerClientPtr client,
141
                              virNetServerProgramPtr program,
142 143 144
                              int procnr,
                              xdrproc_t proc,
                              void *data);
145

146 147 148
static void
remoteEventCallbackFree(void *opaque)
{
149 150 151
    daemonClientEventCallbackPtr callback = opaque;
    if (!callback)
        return;
152
    virObjectUnref(callback->program);
153 154
    virObjectUnref(callback->client);
    VIR_FREE(callback);
155 156
}

157 158 159 160 161 162

static bool
remoteRelayDomainEventCheckACL(virNetServerClientPtr client,
                               virConnectPtr conn, virDomainPtr dom)
{
    virDomainDef def;
163
    g_autoptr(virIdentity) identity = NULL;
164 165 166 167 168
    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.  */
169
    memset(&def, 0, sizeof(def));
170 171 172 173 174 175 176 177 178
    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);

179
 cleanup:
180 181 182 183 184 185 186 187 188 189
    ignore_value(virIdentitySetCurrent(NULL));
    return ret;
}


static bool
remoteRelayNetworkEventCheckACL(virNetServerClientPtr client,
                                virConnectPtr conn, virNetworkPtr net)
{
    virNetworkDef def;
190
    g_autoptr(virIdentity) identity = NULL;
191 192 193 194 195 196 197 198 199 200 201 202 203 204
    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);

205
 cleanup:
206 207 208 209
    ignore_value(virIdentitySetCurrent(NULL));
    return ret;
}

210 211 212 213 214 215
static bool
remoteRelayStoragePoolEventCheckACL(virNetServerClientPtr client,
                                    virConnectPtr conn,
                                    virStoragePoolPtr pool)
{
    virStoragePoolDef def;
216
    g_autoptr(virIdentity) identity = NULL;
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    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));
    return ret;
}
235

236 237 238 239 240 241
static bool
remoteRelayNodeDeviceEventCheckACL(virNetServerClientPtr client,
                                   virConnectPtr conn,
                                   virNodeDevicePtr dev)
{
    virNodeDeviceDef def;
242
    g_autoptr(virIdentity) identity = NULL;
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    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));
    return ret;
}

261 262 263 264 265 266
static bool
remoteRelaySecretEventCheckACL(virNetServerClientPtr client,
                               virConnectPtr conn,
                               virSecretPtr secret)
{
    virSecretDef def;
267
    g_autoptr(virIdentity) identity = NULL;
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    bool ret = false;

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

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

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

288 289 290 291 292
static bool
remoteRelayDomainQemuMonitorEventCheckACL(virNetServerClientPtr client,
                                          virConnectPtr conn, virDomainPtr dom)
{
    virDomainDef def;
293
    g_autoptr(virIdentity) identity = NULL;
294 295 296 297 298 299 300 301 302 303 304 305 306 307
    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);

308
 cleanup:
309 310 311 312 313
    ignore_value(virIdentitySetCurrent(NULL));
    return ret;
}


314 315 316 317 318 319
static int
remoteRelayDomainEventLifecycle(virConnectPtr conn,
                                virDomainPtr dom,
                                int event,
                                int detail,
                                void *opaque)
320
{
321
    daemonClientEventCallbackPtr callback = opaque;
322
    remote_domain_event_lifecycle_msg data;
323

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

328 329
    VIR_DEBUG("Relaying domain lifecycle event %d %d, callback %d legacy %d",
              event, detail, callback->callbackID, callback->legacy);
330

331
    /* build return data */
332
    memset(&data, 0, sizeof(data));
333
    make_nonnull_domain(&data.dom, dom);
334 335
    data.event = event;
    data.detail = detail;
336

337
    if (callback->legacy) {
338
        remoteDispatchObjectEventSend(callback->client, callback->program,
339 340 341 342 343 344 345
                                      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 };

346
        remoteDispatchObjectEventSend(callback->client, callback->program,
347 348 349 350
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_LIFECYCLE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_lifecycle_msg,
                                      &msg);
    }
351

352 353
    return 0;
}
354

355 356 357 358
static int
remoteRelayDomainEventReboot(virConnectPtr conn,
                             virDomainPtr dom,
                             void *opaque)
359
{
360
    daemonClientEventCallbackPtr callback = opaque;
361 362
    remote_domain_event_reboot_msg data;

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

367 368
    VIR_DEBUG("Relaying domain reboot event %s %d, callback %d legacy %d",
              dom->name, dom->id, callback->callbackID, callback->legacy);
369 370

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

374
    if (callback->legacy) {
375
        remoteDispatchObjectEventSend(callback->client, callback->program,
376 377 378 379 380 381
                                      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 };

382
        remoteDispatchObjectEventSend(callback->client, callback->program,
383 384 385
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_REBOOT,
                                      (xdrproc_t)xdr_remote_domain_event_callback_reboot_msg, &msg);
    }
386 387 388 389

    return 0;
}

390

391 392 393 394 395
static int
remoteRelayDomainEventRTCChange(virConnectPtr conn,
                                virDomainPtr dom,
                                long long offset,
                                void *opaque)
396
{
397
    daemonClientEventCallbackPtr callback = opaque;
398 399
    remote_domain_event_rtc_change_msg data;

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

404 405 406
    VIR_DEBUG("Relaying domain rtc change event %s %d %lld, callback %d legacy %d",
              dom->name, dom->id, offset,
              callback->callbackID, callback->legacy);
407 408

    /* build return data */
409
    memset(&data, 0, sizeof(data));
410
    make_nonnull_domain(&data.dom, dom);
411 412
    data.offset = offset;

413
    if (callback->legacy) {
414
        remoteDispatchObjectEventSend(callback->client, callback->program,
415 416 417 418 419 420
                                      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 };

421
        remoteDispatchObjectEventSend(callback->client, callback->program,
422 423 424
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_RTC_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_rtc_change_msg, &msg);
    }
425 426 427 428 429

    return 0;
}


430 431 432 433 434
static int
remoteRelayDomainEventWatchdog(virConnectPtr conn,
                               virDomainPtr dom,
                               int action,
                               void *opaque)
435
{
436
    daemonClientEventCallbackPtr callback = opaque;
437 438
    remote_domain_event_watchdog_msg data;

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

443 444
    VIR_DEBUG("Relaying domain watchdog event %s %d %d, callback %d",
              dom->name, dom->id, action, callback->callbackID);
445 446

    /* build return data */
447
    memset(&data, 0, sizeof(data));
448
    make_nonnull_domain(&data.dom, dom);
449 450
    data.action = action;

451
    if (callback->legacy) {
452
        remoteDispatchObjectEventSend(callback->client, callback->program,
453 454 455 456 457 458
                                      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 };

459
        remoteDispatchObjectEventSend(callback->client, callback->program,
460 461 462
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_WATCHDOG,
                                      (xdrproc_t)xdr_remote_domain_event_callback_watchdog_msg, &msg);
    }
463 464 465 466 467

    return 0;
}


468 469 470 471 472 473 474
static int
remoteRelayDomainEventIOError(virConnectPtr conn,
                              virDomainPtr dom,
                              const char *srcPath,
                              const char *devAlias,
                              int action,
                              void *opaque)
475
{
476
    daemonClientEventCallbackPtr callback = opaque;
477 478
    remote_domain_event_io_error_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, callback %d",
              dom->name, dom->id, srcPath, devAlias, action,
              callback->callbackID);
486 487

    /* build return data */
488
    memset(&data, 0, sizeof(data));
489 490
    data.srcPath = g_strdup(srcPath);
    data.devAlias = g_strdup(devAlias);
491
    make_nonnull_domain(&data.dom, dom);
492 493
    data.action = action;

494
    if (callback->legacy) {
495
        remoteDispatchObjectEventSend(callback->client, callback->program,
496 497 498 499 500 501
                                      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 };

502
        remoteDispatchObjectEventSend(callback->client, callback->program,
503 504 505
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_IO_ERROR,
                                      (xdrproc_t)xdr_remote_domain_event_callback_io_error_msg, &msg);
    }
506 507 508 509 510

    return 0;
}


511 512 513 514 515 516 517 518
static int
remoteRelayDomainEventIOErrorReason(virConnectPtr conn,
                                    virDomainPtr dom,
                                    const char *srcPath,
                                    const char *devAlias,
                                    int action,
                                    const char *reason,
                                    void *opaque)
519
{
520
    daemonClientEventCallbackPtr callback = opaque;
521 522
    remote_domain_event_io_error_reason_msg data;

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

527 528 529
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s, callback %d",
              dom->name, dom->id, srcPath, devAlias, action, reason,
              callback->callbackID);
530 531

    /* build return data */
532
    memset(&data, 0, sizeof(data));
533 534 535
    data.srcPath = g_strdup(srcPath);
    data.devAlias = g_strdup(devAlias);
    data.reason = g_strdup(reason);
536
    data.action = action;
537
    make_nonnull_domain(&data.dom, dom);
538

539
    if (callback->legacy) {
540
        remoteDispatchObjectEventSend(callback->client, callback->program,
541 542 543 544 545 546
                                      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 };

547
        remoteDispatchObjectEventSend(callback->client, callback->program,
548 549 550
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_IO_ERROR_REASON,
                                      (xdrproc_t)xdr_remote_domain_event_callback_io_error_reason_msg, &msg);
    }
551 552 553 554 555

    return 0;
}


556 557 558 559 560 561 562 563 564
static int
remoteRelayDomainEventGraphics(virConnectPtr conn,
                               virDomainPtr dom,
                               int phase,
                               virDomainEventGraphicsAddressPtr local,
                               virDomainEventGraphicsAddressPtr remote,
                               const char *authScheme,
                               virDomainEventGraphicsSubjectPtr subject,
                               void *opaque)
565
{
566
    daemonClientEventCallbackPtr callback = opaque;
567
    remote_domain_event_graphics_msg data;
568
    size_t i;
569

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

574 575
    VIR_DEBUG("Relaying domain graphics event %s %d %d - %d %s %s  - %d %s %s - %s, callback %d",
              dom->name, dom->id, phase,
576 577
              local->family, local->service, local->node,
              remote->family, remote->service, remote->node,
578
              authScheme, callback->callbackID);
579

580
    VIR_DEBUG("Subject %d", subject->nidentity);
581
    for (i = 0; i < subject->nidentity; i++)
582
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
583 584

    /* build return data */
585
    memset(&data, 0, sizeof(data));
586 587 588
    data.phase = phase;
    data.local.family = local->family;
    data.remote.family = remote->family;
589 590 591 592 593 594 595 596 597
    data.authScheme = g_strdup(authScheme);

    data.local.node = g_strdup(local->node);

    data.local.service = g_strdup(local->service);

    data.remote.node = g_strdup(remote->node);

    data.remote.service = g_strdup(remote->service);
598 599

    data.subject.subject_len = subject->nidentity;
600 601
    data.subject.subject_val = g_new0(remote_domain_event_graphics_identity,
                                      data.subject.subject_len);
602

603
    for (i = 0; i < data.subject.subject_len; i++) {
604 605
        data.subject.subject_val[i].type = g_strdup(subject->identities[i].type);
        data.subject.subject_val[i].name = g_strdup(subject->identities[i].name);
606
    }
607
    make_nonnull_domain(&data.dom, dom);
608

609
    if (callback->legacy) {
610
        remoteDispatchObjectEventSend(callback->client, callback->program,
611 612 613 614 615 616
                                      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 };

617
        remoteDispatchObjectEventSend(callback->client, callback->program,
618 619 620
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_GRAPHICS,
                                      (xdrproc_t)xdr_remote_domain_event_callback_graphics_msg, &msg);
    }
621 622 623 624

    return 0;
}

625 626 627 628 629 630 631
static int
remoteRelayDomainEventBlockJob(virConnectPtr conn,
                               virDomainPtr dom,
                               const char *path,
                               int type,
                               int status,
                               void *opaque)
632
{
633
    daemonClientEventCallbackPtr callback = opaque;
634 635
    remote_domain_event_block_job_msg data;

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

640 641
    VIR_DEBUG("Relaying domain block job event %s %d %s %i, %i, callback %d",
              dom->name, dom->id, path, type, status, callback->callbackID);
642 643

    /* build return data */
644
    memset(&data, 0, sizeof(data));
645
    data.path = g_strdup(path);
646 647
    data.type = type;
    data.status = status;
648
    make_nonnull_domain(&data.dom, dom);
649

650
    if (callback->legacy) {
651
        remoteDispatchObjectEventSend(callback->client, callback->program,
652 653 654 655 656 657
                                      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 };

658
        remoteDispatchObjectEventSend(callback->client, callback->program,
659 660 661
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_BLOCK_JOB,
                                      (xdrproc_t)xdr_remote_domain_event_callback_block_job_msg, &msg);
    }
662 663 664 665

    return 0;
}

666

667 668 669 670
static int
remoteRelayDomainEventControlError(virConnectPtr conn,
                                   virDomainPtr dom,
                                   void *opaque)
671
{
672
    daemonClientEventCallbackPtr callback = opaque;
673 674
    remote_domain_event_control_error_msg data;

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

679 680
    VIR_DEBUG("Relaying domain control error %s %d, callback %d",
              dom->name, dom->id, callback->callbackID);
681 682

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

686
    if (callback->legacy) {
687
        remoteDispatchObjectEventSend(callback->client, callback->program,
688 689 690 691 692 693
                                      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 };

694
        remoteDispatchObjectEventSend(callback->client, callback->program,
695 696 697
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_CONTROL_ERROR,
                                      (xdrproc_t)xdr_remote_domain_event_callback_control_error_msg, &msg);
    }
698 699 700 701 702

    return 0;
}


703 704 705 706 707 708 709 710
static int
remoteRelayDomainEventDiskChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *oldSrcPath,
                                 const char *newSrcPath,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
711
{
712
    daemonClientEventCallbackPtr callback = opaque;
713 714
    remote_domain_event_disk_change_msg data;

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

719 720 721
    VIR_DEBUG("Relaying domain %s %d disk change %s %s %s %d, callback %d",
              dom->name, dom->id, oldSrcPath, newSrcPath, devAlias, reason,
              callback->callbackID);
722 723

    /* build return data */
724
    memset(&data, 0, sizeof(data));
725
    if (oldSrcPath) {
726
        data.oldSrcPath = g_new0(remote_nonnull_string, 1);
727 728
        *(data.oldSrcPath) = g_strdup(oldSrcPath);
    }
729

730
    if (newSrcPath) {
731
        data.newSrcPath = g_new0(remote_nonnull_string, 1);
732 733
        *(data.newSrcPath) = g_strdup(newSrcPath);
    }
734

735
    data.devAlias = g_strdup(devAlias);
736
    data.reason = reason;
737
    make_nonnull_domain(&data.dom, dom);
738

739
    if (callback->legacy) {
740
        remoteDispatchObjectEventSend(callback->client, callback->program,
741 742 743 744 745 746
                                      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 };

747
        remoteDispatchObjectEventSend(callback->client, callback->program,
748 749 750
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DISK_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_disk_change_msg, &msg);
    }
751 752 753 754 755

    return 0;
}


756 757 758 759 760 761 762
static int
remoteRelayDomainEventTrayChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
{
763
    daemonClientEventCallbackPtr callback = opaque;
764 765
    remote_domain_event_tray_change_msg data;

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

770 771
    VIR_DEBUG("Relaying domain %s %d tray change devAlias: %s reason: %d, callback %d",
              dom->name, dom->id, devAlias, reason, callback->callbackID);
772 773

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

776
    data.devAlias = g_strdup(devAlias);
777
    data.reason = reason;
778
    make_nonnull_domain(&data.dom, dom);
779

780
    if (callback->legacy) {
781
        remoteDispatchObjectEventSend(callback->client, callback->program,
782 783 784 785 786 787
                                      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 };

788
        remoteDispatchObjectEventSend(callback->client, callback->program,
789 790 791
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_TRAY_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_tray_change_msg, &msg);
    }
792 793 794 795

    return 0;
}

796 797 798
static int
remoteRelayDomainEventPMWakeup(virConnectPtr conn,
                               virDomainPtr dom,
E
Eric Blake 已提交
799
                               int reason,
800 801
                               void *opaque)
{
802
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
803 804
    remote_domain_event_pmwakeup_msg data;

805 806
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
807 808
        return -1;

809 810
    VIR_DEBUG("Relaying domain %s %d system pmwakeup, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
811 812

    /* build return data */
813
    memset(&data, 0, sizeof(data));
814
    make_nonnull_domain(&data.dom, dom);
O
Osier Yang 已提交
815

816
    if (callback->legacy) {
817
        remoteDispatchObjectEventSend(callback->client, callback->program,
818 819 820 821
                                      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 已提交
822
                                                          reason, data };
823

824
        remoteDispatchObjectEventSend(callback->client, callback->program,
825 826 827
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMWAKEUP,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmwakeup_msg, &msg);
    }
O
Osier Yang 已提交
828 829 830 831

    return 0;
}

832 833 834
static int
remoteRelayDomainEventPMSuspend(virConnectPtr conn,
                                virDomainPtr dom,
E
Eric Blake 已提交
835
                                int reason,
836 837
                                void *opaque)
{
838
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
839 840
    remote_domain_event_pmsuspend_msg data;

841 842
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
843 844
        return -1;

845 846
    VIR_DEBUG("Relaying domain %s %d system pmsuspend, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
847 848

    /* build return data */
849
    memset(&data, 0, sizeof(data));
850
    make_nonnull_domain(&data.dom, dom);
O
Osier Yang 已提交
851

852
    if (callback->legacy) {
853
        remoteDispatchObjectEventSend(callback->client, callback->program,
854 855 856 857
                                      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 已提交
858
                                                           reason, data };
859

860
        remoteDispatchObjectEventSend(callback->client, callback->program,
861 862 863
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_msg, &msg);
    }
O
Osier Yang 已提交
864 865 866 867

    return 0;
}

868
static int
869
remoteRelayDomainEventBalloonChange(virConnectPtr conn,
870 871 872 873
                                    virDomainPtr dom,
                                    unsigned long long actual,
                                    void *opaque)
{
874
    daemonClientEventCallbackPtr callback = opaque;
875 876
    remote_domain_event_balloon_change_msg data;

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

881 882
    VIR_DEBUG("Relaying domain balloon change event %s %d %lld, callback %d",
              dom->name, dom->id, actual, callback->callbackID);
883 884 885

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

889
    if (callback->legacy) {
890
        remoteDispatchObjectEventSend(callback->client, callback->program,
891 892 893 894 895 896
                                      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 };

897
        remoteDispatchObjectEventSend(callback->client, callback->program,
898 899 900
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_BALLOON_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_balloon_change_msg, &msg);
    }
901 902 903 904 905

    return 0;
}


906 907 908
static int
remoteRelayDomainEventPMSuspendDisk(virConnectPtr conn,
                                    virDomainPtr dom,
E
Eric Blake 已提交
909
                                    int reason,
910 911
                                    void *opaque)
{
912
    daemonClientEventCallbackPtr callback = opaque;
913 914
    remote_domain_event_pmsuspend_disk_msg data;

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

919 920
    VIR_DEBUG("Relaying domain %s %d system pmsuspend-disk, callback %d",
              dom->name, dom->id, callback->callbackID);
921 922 923

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

926
    if (callback->legacy) {
927
        remoteDispatchObjectEventSend(callback->client, callback->program,
928 929 930 931
                                      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 已提交
932
                                                                reason, data };
933

934
        remoteDispatchObjectEventSend(callback->client, callback->program,
935 936 937
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND_DISK,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_disk_msg, &msg);
    }
938 939 940 941

    return 0;
}

942
static int
943
remoteRelayDomainEventDeviceRemoved(virConnectPtr conn,
944 945 946 947
                                    virDomainPtr dom,
                                    const char *devAlias,
                                    void *opaque)
{
948
    daemonClientEventCallbackPtr callback = opaque;
949 950
    remote_domain_event_device_removed_msg data;

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

955 956
    VIR_DEBUG("Relaying domain device removed event %s %d %s, callback %d",
              dom->name, dom->id, devAlias, callback->callbackID);
957 958 959 960

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

961
    data.devAlias = g_strdup(devAlias);
962

963
    make_nonnull_domain(&data.dom, dom);
964

965
    if (callback->legacy) {
966
        remoteDispatchObjectEventSend(callback->client, callback->program,
967 968 969 970 971 972 973
                                      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 };

974
        remoteDispatchObjectEventSend(callback->client, callback->program,
975 976 977 978
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DEVICE_REMOVED,
                                      (xdrproc_t)xdr_remote_domain_event_callback_device_removed_msg,
                                      &msg);
    }
979 980 981 982

    return 0;
}

983

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
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;
1005
    data.dst = g_strdup(dst);
1006 1007
    data.type = type;
    data.status = status;
1008
    make_nonnull_domain(&data.dom, dom);
1009

1010
    remoteDispatchObjectEventSend(callback->client, callback->program,
1011 1012 1013 1014 1015 1016 1017
                                  REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB_2,
                                  (xdrproc_t)xdr_remote_domain_event_block_job_2_msg, &data);

    return 0;
}


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

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

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

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

    data.callbackID = callback->callbackID;
    make_nonnull_domain(&data.dom, dom);

1048

1049
    remoteDispatchObjectEventSend(callback->client, callback->program,
1050 1051 1052 1053 1054 1055 1056 1057
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_TUNABLE,
                                  (xdrproc_t)xdr_remote_domain_event_callback_tunable_msg,
                                  &data);

    return 0;
}


1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
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;
1079
    make_nonnull_domain(&data.dom, dom);
1080 1081 1082
    data.state = state;
    data.reason = reason;

1083
    remoteDispatchObjectEventSend(callback->client, callback->program,
1084 1085 1086 1087 1088 1089 1090 1091
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_AGENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_domain_event_callback_agent_lifecycle_msg,
                                  &data);

    return 0;
}


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

1111
    data.devAlias = g_strdup(devAlias);
1112
    make_nonnull_domain(&data.dom, dom);
E
Eric Blake 已提交
1113
    data.callbackID = callback->callbackID;
1114

1115
    remoteDispatchObjectEventSend(callback->client, callback->program,
1116 1117 1118 1119 1120 1121 1122 1123
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DEVICE_ADDED,
                                  (xdrproc_t)xdr_remote_domain_event_callback_device_added_msg,
                                  &data);

    return 0;
}


1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
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;
1144
    make_nonnull_domain(&data.dom, dom);
1145 1146 1147

    data.iteration = iteration;

1148
    remoteDispatchObjectEventSend(callback->client, callback->program,
1149 1150 1151 1152 1153 1154
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_MIGRATION_ITERATION,
                                  (xdrproc_t)xdr_remote_domain_event_callback_migration_iteration_msg,
                                  &data);

    return 0;
}
1155 1156


J
Jiri Denemark 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
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));

    if (virTypedParamsSerialize(params, nparams,
1179
                                REMOTE_DOMAIN_JOB_STATS_MAX,
J
Jiri Denemark 已提交
1180 1181
                                (virTypedParameterRemotePtr *) &data.params.params_val,
                                &data.params.params_len,
1182 1183 1184 1185 1186
                                VIR_TYPED_PARAM_STRING_OKAY) < 0)
        return -1;

    data.callbackID = callback->callbackID;
    make_nonnull_domain(&data.dom, dom);
J
Jiri Denemark 已提交
1187

1188
    remoteDispatchObjectEventSend(callback->client, callback->program,
J
Jiri Denemark 已提交
1189 1190 1191 1192 1193 1194 1195
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_JOB_COMPLETED,
                                  (xdrproc_t)xdr_remote_domain_event_callback_job_completed_msg,
                                  &data);
    return 0;
}


1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
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));

1215
    data.devAlias = g_strdup(devAlias);
1216

1217
    make_nonnull_domain(&data.dom, dom);
1218 1219
    data.callbackID = callback->callbackID;

1220
    remoteDispatchObjectEventSend(callback->client, callback->program,
1221 1222 1223 1224 1225 1226 1227 1228
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DEVICE_REMOVAL_FAILED,
                                  (xdrproc_t)xdr_remote_domain_event_callback_device_removal_failed_msg,
                                  &data);

    return 0;
}


1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
static int
remoteRelayDomainEventMetadataChange(virConnectPtr conn,
                                     virDomainPtr dom,
                                     int type,
                                     const char *nsuri,
                                     void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_metadata_change_msg data;

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

    VIR_DEBUG("Relaying domain metadata change %s %d %d %s, callback %d",
              dom->name, dom->id, type, NULLSTR(nsuri), callback->callbackID);

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

    data.type = type;
    if (nsuri) {
1251
        data.nsuri = g_new0(remote_nonnull_string, 1);
1252
        *(data.nsuri) = g_strdup(nsuri);
1253 1254
    }

1255
    make_nonnull_domain(&data.dom, dom);
1256 1257
    data.callbackID = callback->callbackID;

1258
    remoteDispatchObjectEventSend(callback->client, callback->program,
1259 1260 1261 1262 1263 1264 1265 1266
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_METADATA_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_callback_metadata_change_msg,
                                  &data);

    return 0;
}


1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
static int
remoteRelayDomainEventBlockThreshold(virConnectPtr conn,
                                     virDomainPtr dom,
                                     const char *dev,
                                     const char *path,
                                     unsigned long long threshold,
                                     unsigned long long excess,
                                     void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_block_threshold_msg data;

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

    VIR_DEBUG("Relaying domain block threshold event %s %d %s %s %llu %llu, callback %d",
              dom->name, dom->id, dev, NULLSTR(path), threshold, excess, callback->callbackID);

    /* build return data */
    memset(&data, 0, sizeof(data));
    data.callbackID = callback->callbackID;
1289
    data.dev = g_strdup(dev);
1290
    if (path) {
1291
        data.path = g_new0(remote_nonnull_string, 1);
1292
        *(data.path) = g_strdup(path);
1293 1294 1295
    }
    data.threshold = threshold;
    data.excess = excess;
1296
    make_nonnull_domain(&data.dom, dom);
1297

1298
    remoteDispatchObjectEventSend(callback->client, callback->program,
1299 1300 1301 1302 1303 1304
                                  REMOTE_PROC_DOMAIN_EVENT_BLOCK_THRESHOLD,
                                  (xdrproc_t)xdr_remote_domain_event_block_threshold_msg, &data);

    return 0;
}

1305

1306
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
1307
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
1308
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
1309
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
1310
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
1311
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
1312
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
1313
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
1314
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
1315
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
1316
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
1317
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
1318
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
1319
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
1320
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
1321
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
1322
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
1323
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob2),
1324
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTunable),
1325
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventAgentLifecycle),
1326
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceAdded),
1327
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventMigrationIteration),
J
Jiri Denemark 已提交
1328
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventJobCompleted),
1329
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemovalFailed),
1330
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventMetadataChange),
1331
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockThreshold),
1332 1333
};

1334
G_STATIC_ASSERT(G_N_ELEMENTS(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);
1335

1336
static int
1337
remoteRelayNetworkEventLifecycle(virConnectPtr conn,
1338 1339 1340 1341
                                 virNetworkPtr net,
                                 int event,
                                 int detail,
                                 void *opaque)
1342
{
1343
    daemonClientEventCallbackPtr callback = opaque;
1344 1345
    remote_network_event_lifecycle_msg data;

1346 1347
    if (callback->callbackID < 0 ||
        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
1348 1349
        return -1;

1350 1351
    VIR_DEBUG("Relaying network lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);
1352 1353 1354

    /* build return data */
    memset(&data, 0, sizeof(data));
1355
    make_nonnull_network(&data.net, net);
1356
    data.callbackID = callback->callbackID;
1357 1358 1359
    data.event = event;
    data.detail = detail;

1360
    remoteDispatchObjectEventSend(callback->client, callback->program,
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
                                  REMOTE_PROC_NETWORK_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_network_event_lifecycle_msg, &data);

    return 0;
}

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

1371
G_STATIC_ASSERT(G_N_ELEMENTS(networkEventCallbacks) == VIR_NETWORK_EVENT_ID_LAST);
1372

1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
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));
1392
    make_nonnull_storage_pool(&data.pool, pool);
1393 1394 1395 1396
    data.callbackID = callback->callbackID;
    data.event = event;
    data.detail = detail;

1397
    remoteDispatchObjectEventSend(callback->client, callback->program,
1398 1399 1400 1401 1402 1403 1404
                                  REMOTE_PROC_STORAGE_POOL_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_storage_pool_event_lifecycle_msg,
                                  &data);

    return 0;
}

1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
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));
1422
    make_nonnull_storage_pool(&data.pool, pool);
1423 1424
    data.callbackID = callback->callbackID;

1425
    remoteDispatchObjectEventSend(callback->client, callback->program,
1426 1427 1428 1429 1430 1431 1432
                                  REMOTE_PROC_STORAGE_POOL_EVENT_REFRESH,
                                  (xdrproc_t)xdr_remote_storage_pool_event_refresh_msg,
                                  &data);

    return 0;
}

1433 1434
static virConnectStoragePoolEventGenericCallback storageEventCallbacks[] = {
    VIR_STORAGE_POOL_EVENT_CALLBACK(remoteRelayStoragePoolEventLifecycle),
1435
    VIR_STORAGE_POOL_EVENT_CALLBACK(remoteRelayStoragePoolEventRefresh),
1436 1437
};

1438
G_STATIC_ASSERT(G_N_ELEMENTS(storageEventCallbacks) == VIR_STORAGE_POOL_EVENT_ID_LAST);
1439

1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
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));
1459
    make_nonnull_node_device(&data.dev, dev);
1460 1461 1462 1463
    data.callbackID = callback->callbackID;
    data.event = event;
    data.detail = detail;

1464
    remoteDispatchObjectEventSend(callback->client, callback->program,
1465 1466 1467 1468 1469 1470 1471
                                  REMOTE_PROC_NODE_DEVICE_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_node_device_event_lifecycle_msg,
                                  &data);

    return 0;
}

1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
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));
1489
    make_nonnull_node_device(&data.dev, dev);
1490 1491
    data.callbackID = callback->callbackID;

1492
    remoteDispatchObjectEventSend(callback->client, callback->program,
1493 1494 1495 1496 1497 1498 1499
                                  REMOTE_PROC_NODE_DEVICE_EVENT_UPDATE,
                                  (xdrproc_t)xdr_remote_node_device_event_update_msg,
                                  &data);

    return 0;
}

1500 1501
static virConnectNodeDeviceEventGenericCallback nodeDeviceEventCallbacks[] = {
    VIR_NODE_DEVICE_EVENT_CALLBACK(remoteRelayNodeDeviceEventLifecycle),
1502
    VIR_NODE_DEVICE_EVENT_CALLBACK(remoteRelayNodeDeviceEventUpdate),
1503 1504
};

1505
G_STATIC_ASSERT(G_N_ELEMENTS(nodeDeviceEventCallbacks) == VIR_NODE_DEVICE_EVENT_ID_LAST);
1506

1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
static int
remoteRelaySecretEventLifecycle(virConnectPtr conn,
                                virSecretPtr secret,
                                int event,
                                int detail,
                                void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_secret_event_lifecycle_msg data;

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

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

    /* build return data */
    memset(&data, 0, sizeof(data));
1526
    make_nonnull_secret(&data.secret, secret);
1527 1528 1529 1530
    data.callbackID = callback->callbackID;
    data.event = event;
    data.detail = detail;

1531
    remoteDispatchObjectEventSend(callback->client, callback->program,
1532 1533 1534 1535 1536 1537 1538
                                  REMOTE_PROC_SECRET_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_secret_event_lifecycle_msg,
                                  &data);

    return 0;
}

1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
static int
remoteRelaySecretEventValueChanged(virConnectPtr conn,
                                   virSecretPtr secret,
                                   void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_secret_event_value_changed_msg data;

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

    VIR_DEBUG("Relaying node secret value changed callback %d",
              callback->callbackID);

    /* build return data */
    memset(&data, 0, sizeof(data));
1556
    make_nonnull_secret(&data.secret, secret);
1557 1558
    data.callbackID = callback->callbackID;

1559
    remoteDispatchObjectEventSend(callback->client, callback->program,
1560 1561 1562 1563 1564 1565 1566
                                  REMOTE_PROC_SECRET_EVENT_VALUE_CHANGED,
                                  (xdrproc_t)xdr_remote_secret_event_value_changed_msg,
                                  &data);

    return 0;
}

1567 1568
static virConnectSecretEventGenericCallback secretEventCallbacks[] = {
    VIR_SECRET_EVENT_CALLBACK(remoteRelaySecretEventLifecycle),
1569
    VIR_SECRET_EVENT_CALLBACK(remoteRelaySecretEventValueChanged),
1570 1571
};

1572
G_STATIC_ASSERT(G_N_ELEMENTS(secretEventCallbacks) == VIR_SECRET_EVENT_ID_LAST);
1573

1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
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;

    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;
1597
    data.event = g_strdup(event);
1598 1599
    data.seconds = seconds;
    data.micros = micros;
1600 1601 1602 1603 1604
    if (details) {
        if (VIR_ALLOC(data.details) < 0)
            goto error;
        *(data.details) = g_strdup(details);
    }
1605
    make_nonnull_domain(&data.dom, dom);
1606

1607
    remoteDispatchObjectEventSend(callback->client, callback->program,
1608 1609 1610 1611 1612
                                  QEMU_PROC_DOMAIN_MONITOR_EVENT,
                                  (xdrproc_t)xdr_qemu_domain_monitor_event_msg,
                                  &data);
    return;

1613
 error:
1614
    xdr_free((xdrproc_t)xdr_qemu_domain_monitor_event_msg,
1615
             (char *) &data);
1616
    return;
1617 1618
}

1619
static
J
Ján Tomko 已提交
1620
void remoteRelayConnectionClosedEvent(virConnectPtr conn G_GNUC_UNUSED, int reason, void *opaque)
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
{
    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);
}

1633 1634 1635
#define DEREG_CB(conn, eventCallbacks, neventCallbacks, deregFcn, name) \
    do { \
        size_t i; \
1636 1637 1638 1639 1640
        if (neventCallbacks && !conn) { \
            VIR_WARN("Have %zu %s event callbacks but no connection", \
                     neventCallbacks, name); \
            break; \
        } \
1641 1642 1643
        for (i = 0; i < neventCallbacks; i++) { \
            int callbackID = eventCallbacks[i]->callbackID; \
            if (callbackID < 0) { \
1644
                VIR_WARN("unexpected incomplete %s callback %zu", name, i); \
1645 1646 1647 1648 1649 1650 1651 1652 1653
                continue; \
            } \
            VIR_DEBUG("Deregistering remote %s event relay %d", \
                      name, callbackID); \
            eventCallbacks[i]->callbackID = -1; \
            if (deregFcn(conn, callbackID) < 0) \
                VIR_WARN("unexpected %s event deregister failure", name); \
        } \
        VIR_FREE(eventCallbacks); \
1654
        neventCallbacks = 0; \
1655 1656
    } while (0);

1657 1658 1659 1660

static void
remoteClientFreePrivateCallbacks(struct daemonClientPrivate *priv)
{
1661
    g_autoptr(virIdentity) sysident = virIdentityGetSystem();
1662 1663 1664 1665 1666
    virIdentitySetCurrent(sysident);

    DEREG_CB(priv->conn, priv->domainEventCallbacks,
             priv->ndomainEventCallbacks,
             virConnectDomainEventDeregisterAny, "domain");
1667
    DEREG_CB(priv->networkConn, priv->networkEventCallbacks,
1668 1669
             priv->nnetworkEventCallbacks,
             virConnectNetworkEventDeregisterAny, "network");
1670
    DEREG_CB(priv->storageConn, priv->storageEventCallbacks,
1671 1672
             priv->nstorageEventCallbacks,
             virConnectStoragePoolEventDeregisterAny, "storage");
1673
    DEREG_CB(priv->nodedevConn, priv->nodeDeviceEventCallbacks,
1674 1675
             priv->nnodeDeviceEventCallbacks,
             virConnectNodeDeviceEventDeregisterAny, "node device");
1676
    DEREG_CB(priv->secretConn, priv->secretEventCallbacks,
1677 1678 1679 1680 1681 1682
             priv->nsecretEventCallbacks,
             virConnectSecretEventDeregisterAny, "secret");
    DEREG_CB(priv->conn, priv->qemuEventCallbacks,
             priv->nqemuEventCallbacks,
             virConnectDomainQemuMonitorEventDeregister, "qemu monitor");

1683
    if (priv->closeRegistered && priv->conn) {
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
        if (virConnectUnregisterCloseCallback(priv->conn,
                                              remoteRelayConnectionClosedEvent) < 0)
            VIR_WARN("unexpected close callback event deregister failure");
    }

    virIdentitySetCurrent(NULL);
}
#undef DEREG_CB


1694 1695 1696 1697 1698 1699 1700
/*
 * 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
 */
1701
void remoteClientFree(void *data)
1702 1703 1704
{
    struct daemonClientPrivate *priv = data;

1705
    if (priv->conn)
1706
        virConnectClose(priv->conn);
1707 1708
    if (priv->interfaceConn)
        virConnectClose(priv->interfaceConn);
1709 1710
    if (priv->networkConn)
        virConnectClose(priv->networkConn);
1711 1712
    if (priv->nodedevConn)
        virConnectClose(priv->nodedevConn);
1713 1714
    if (priv->nwfilterConn)
        virConnectClose(priv->nwfilterConn);
1715 1716
    if (priv->secretConn)
        virConnectClose(priv->secretConn);
1717 1718
    if (priv->storageConn)
        virConnectClose(priv->storageConn);
1719

1720 1721 1722 1723
    VIR_FREE(priv);
}


1724 1725 1726 1727 1728
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
1729

1730
    remoteClientFreePrivateCallbacks(priv);
1731 1732
}

1733

1734 1735 1736
static int
remoteOpenConn(const char *uri,
               bool readonly,
1737
               bool preserveIdentity,
1738 1739
               virConnectPtr *conn)
{
1740 1741 1742 1743 1744 1745 1746
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    int ret = -1;

    VIR_DEBUG("Getting secondary uri=%s readonly=%d preserveIdent=%d conn=%p",
              NULLSTR(uri), readonly, preserveIdentity, conn);

1747 1748 1749 1750 1751 1752 1753 1754
    if (*conn)
        return 0;

    if (!uri) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        return -1;
    }

1755
    if (preserveIdentity) {
1756
        g_autoptr(virIdentity) ident = NULL;
1757 1758 1759 1760 1761 1762 1763 1764

        if (!(ident = virIdentityGetCurrent()))
            return -1;

        if (virIdentityGetParameters(ident, &params, &nparams) < 0)
            goto error;
    }

1765 1766 1767 1768 1769 1770
    VIR_DEBUG("Opening driver %s", uri);
    if (readonly)
        *conn = virConnectOpenReadOnly(uri);
    else
        *conn = virConnectOpen(uri);
    if (!*conn)
1771
        goto error;
1772 1773
    VIR_DEBUG("Opened driver %p", *conn);

1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
    if (preserveIdentity) {
        if (virConnectSetIdentity(*conn, params, nparams, 0) < 0)
            goto error;

        VIR_DEBUG("Forwarded current identity to secondary driver");
    }

    ret = 0;
 cleanup:
    virTypedParamsFree(params, nparams);
    return ret;

 error:
    if (*conn) {
        virConnectClose(*conn);
        *conn = NULL;
    }
    goto cleanup;
1792 1793 1794
}


1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
static virConnectPtr
remoteGetHypervisorConn(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    return priv->conn;
}


static virConnectPtr
remoteGetInterfaceConn(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

1816 1817
    if (remoteOpenConn(priv->interfaceURI,
                       priv->readonly,
1818
                       true,
1819
                       &priv->interfaceConn) < 0)
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
        return NULL;

    return priv->interfaceConn;
}


static virConnectPtr
remoteGetNetworkConn(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

1832 1833
    if (remoteOpenConn(priv->networkURI,
                       priv->readonly,
1834
                       true,
1835
                       &priv->networkConn) < 0)
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
        return NULL;

    return priv->networkConn;
}


static virConnectPtr
remoteGetNodeDevConn(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

1848 1849
    if (remoteOpenConn(priv->nodedevURI,
                       priv->readonly,
1850
                       true,
1851
                       &priv->nodedevConn) < 0)
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
        return NULL;

    return priv->nodedevConn;
}


static virConnectPtr
remoteGetNWFilterConn(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

1864 1865
    if (remoteOpenConn(priv->nwfilterURI,
                       priv->readonly,
1866
                       true,
1867
                       &priv->nwfilterConn) < 0)
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
        return NULL;

    return priv->nwfilterConn;
}


static virConnectPtr
remoteGetSecretConn(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

1880 1881
    if (remoteOpenConn(priv->secretURI,
                       priv->readonly,
1882
                       true,
1883
                       &priv->secretConn) < 0)
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
        return NULL;

    return priv->secretConn;
}


static virConnectPtr
remoteGetStorageConn(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

1896 1897
    if (remoteOpenConn(priv->storageURI,
                       priv->readonly,
1898
                       true,
1899
                       &priv->storageConn) < 0)
1900 1901 1902 1903 1904 1905
        return NULL;

    return priv->storageConn;
}


1906
void *remoteClientNew(virNetServerClientPtr client,
J
Ján Tomko 已提交
1907
                      void *opaque G_GNUC_UNUSED)
1908 1909 1910
{
    struct daemonClientPrivate *priv;

1911
    if (VIR_ALLOC(priv) < 0)
1912
        return NULL;
1913 1914 1915

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
1916
        virReportSystemError(errno, "%s", _("unable to init mutex"));
1917
        return NULL;
1918 1919
    }

1920
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
1921
    return priv;
1922 1923
}

1924 1925
/*----- Functions. -----*/

1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
#ifdef VIRTPROXYD
/*
 * When running in virtproxyd regular auto-probing of drivers
 * does not work as we don't have any drivers present (except
 * stateless ones inside libvirt.so). All the interesting
 * drivers are in separate daemons. Thus when we get a NULL
 * URI we need to simulate probing that virConnectOpen would
 * previously do. We use the existance of the UNIX domain
 * socket as our hook for probing.
 *
 * This assumes no stale sockets left over from a now dead
 * daemon, but that's reasonable since libvirtd unlinks
 * sockets it creates on shutdown, or uses systemd activation
 *
 * We only try to probe for primary hypervisor drivers,
 * not the secondary drivers.
 */
static int
remoteDispatchProbeURI(bool readonly,
                       char **probeduri)
{
    *probeduri = NULL;
    VIR_DEBUG("Probing for driver daemon sockets");

    /*
     * If running root, either the daemon is running and the socket
     * exists, or we're using socket activation so the socket exists
     * too.
     *
     * If running non-root, chances are that the daemon won't be
     * running, nor any socket activation is used. We need to
     * be able to auto-spawn the daemon. We thus just check to
     * see what daemons are installed. This is not a big deal as
     * only QEMU & VBox run as non-root, anyway.
     */
    if (geteuid() != 0) {
        /* Order these the same as virDriverLoadModule
         * calls in daemonInitialize */
        const char *drivers[] = {
# ifdef WITH_QEMU
            "qemu",
# endif
# ifdef WITH_VBOX
            "vbox",
# endif
        };
1972
        ssize_t i;
1973

1974
        for (i = 0; i < (ssize_t) G_N_ELEMENTS(drivers) && !*probeduri; i++) {
1975 1976
            g_autofree char *daemonname = NULL;
            g_autofree char *daemonpath = NULL;
1977

1978
            daemonname = g_strdup_printf("virt%sd", drivers[i]);
1979 1980 1981 1982 1983 1984 1985 1986 1987

            if (!(daemonpath = virFileFindResource(daemonname, "src", SBINDIR)))
                return -1;

            if (!virFileExists(daemonpath)) {
                VIR_DEBUG("Missing daemon %s for driver %s", daemonpath, drivers[i]);
                continue;
            }

1988
            *probeduri = g_strdup_printf("%s:///session", drivers[i]);
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015

            VIR_DEBUG("Probed URI %s via daemon %s", *probeduri, daemonpath);
            return 0;
        }
    } else {
        /* Order these the same as virDriverLoadModule
         * calls in daemonInitialize */
        const char *drivers[] = {
# ifdef WITH_LIBXL
            "libxl",
# endif
# ifdef WITH_QEMU
            "qemu",
# endif
# ifdef WITH_LXC
            "lxc",
# endif
# ifdef WITH_VBOX
            "vbox",
# endif
# ifdef WITH_BHYVE
            "bhyve",
# endif
# ifdef WITH_VZ
            "vz",
# endif
        };
2016
        ssize_t i;
2017

2018
        for (i = 0; i < (ssize_t) G_N_ELEMENTS(drivers) && !*probeduri; i++) {
2019
            g_autofree char *sockname = NULL;
2020

2021 2022
            sockname = g_strdup_printf("%s/libvirt/virt%sd-%s", RUNSTATEDIR,
                                       drivers[i], readonly ? "sock-ro" : "sock");
2023 2024 2025 2026 2027 2028

            if (!virFileExists(sockname)) {
                VIR_DEBUG("Missing sock %s for driver %s", sockname, drivers[i]);
                continue;
            }

2029
            *probeduri = g_strdup_printf("%s:///system", drivers[i]);
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045

            VIR_DEBUG("Probed URI %s via sock %s", *probeduri, sockname);
            return 0;
        }
    }

    /* Even if we didn't probe any socket, we won't
     * return error. Just let virConnectOpen's normal
     * logic run which will likely return an error anyway
     */
    VIR_DEBUG("No driver sock exists");
    return 0;
}
#endif /* VIRTPROXYD */


2046
static int
J
Ján Tomko 已提交
2047
remoteDispatchConnectOpen(virNetServerPtr server G_GNUC_UNUSED,
2048
                          virNetServerClientPtr client,
J
Ján Tomko 已提交
2049
                          virNetMessagePtr msg G_GNUC_UNUSED,
2050 2051
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
2052 2053
{
    const char *name;
2054
#ifdef VIRTPROXYD
2055
    g_autofree char *probeduri = NULL;
2056
#endif
2057
    unsigned int flags;
2058
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
2059
    int rv = -1;
2060 2061 2062
#ifdef MODULE_NAME
    const char *type = NULL;
#endif /* !MODULE_NAME */
2063
    bool preserveIdentity = false;
2064

2065 2066 2067 2068
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
2069
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
2070 2071 2072
        goto cleanup;
    }

2073 2074 2075 2076 2077 2078
    name = args->name ? *args->name : NULL;

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

2082 2083
    priv->readonly = flags & VIR_CONNECT_RO;

2084 2085 2086 2087 2088 2089 2090
#ifdef VIRTPROXYD
    if (!name || STREQ(name, "")) {
        if (remoteDispatchProbeURI(priv->readonly, &probeduri) < 0)
            goto cleanup;

        name = probeduri;
    }
2091 2092 2093

    preserveIdentity = true;
#endif /* VIRTPROXYD */
2094

2095
    VIR_DEBUG("Opening driver %s", name);
2096 2097 2098 2099 2100
    if (remoteOpenConn(name,
                       priv->readonly,
                       preserveIdentity,
                       &priv->conn) < 0)
        goto cleanup;
2101
    VIR_DEBUG("Opened %p", priv->conn);
2102

2103 2104 2105 2106 2107 2108
#ifdef MODULE_NAME
    /*
     * For per-driver daemons, we must setup connection URIs
     * for sub-drivers.
     */
    if (!(type = virConnectGetType(priv->conn)))
2109 2110
        goto cleanup;

2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
    VIR_DEBUG("Primary driver type is '%s'", type);
    if (STREQ(type, "QEMU") ||
        STREQ(type, "LIBXL") ||
        STREQ(type, "LXC") ||
        STREQ(type, "VBOX") ||
        STREQ(type, "bhyve") ||
        STREQ(type, "vz") ||
        STREQ(type, "Parallels")) {
        VIR_DEBUG("Hypervisor driver found, setting URIs for secondary drivers");
        if (getuid() == 0) {
            priv->interfaceURI = "interface:///system";
            priv->networkURI = "network:///system";
            priv->nodedevURI = "nodedev:///system";
            priv->nwfilterURI = "nwfilter:///system";
            priv->secretURI = "secret:///system";
            priv->storageURI = "storage:///system";
        } else {
            priv->interfaceURI = "interface:///session";
            priv->networkURI = "network:///session";
            priv->nodedevURI = "nodedev:///session";
            /* No nwfilterURI as this is a root-only driver */
            priv->secretURI = "secret:///session";
            priv->storageURI = "storage:///session";
        }
    } else if (STREQ(type, "interface")) {
        VIR_DEBUG("Interface driver found");
        priv->interfaceConn = virObjectRef(priv->conn);
    } else if (STREQ(type, "network")) {
        VIR_DEBUG("Network driver found");
        priv->networkConn = virObjectRef(priv->conn);
    } else if (STREQ(type, "nodedev")) {
        VIR_DEBUG("Nodedev driver found");
        priv->nodedevConn = virObjectRef(priv->conn);
    } else if (STREQ(type, "nwfilter")) {
        VIR_DEBUG("NWFilter driver found");
        priv->nwfilterConn = virObjectRef(priv->conn);
    } else if (STREQ(type, "secret")) {
        VIR_DEBUG("Secret driver found");
        priv->secretConn = virObjectRef(priv->conn);
    } else if (STREQ(type, "storage")) {
        VIR_DEBUG("Storage driver found");
        priv->storageConn = virObjectRef(priv->conn);

        /* Co-open the secret driver, as apps using the storage driver may well
         * need access to secrets for storage auth
         */
        if (getuid() == 0)
            priv->secretURI = "secret:///system";
        else
            priv->secretURI = "secret:///session";
    } else {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unexpected driver type '%s' opened"), type);
        goto cleanup;
    }
#else /* !MODULE_NAME */
    /*
     * For libvirtd/virtproxyd one connection handles
     * all drivers
     */
    VIR_DEBUG("Pointing secondary drivers to primary");
2172
    priv->interfaceConn = virObjectRef(priv->conn);
2173
    priv->networkConn = virObjectRef(priv->conn);
2174
    priv->nodedevConn = virObjectRef(priv->conn);
2175
    priv->nwfilterConn = virObjectRef(priv->conn);
2176
    priv->secretConn = virObjectRef(priv->conn);
2177
    priv->storageConn = virObjectRef(priv->conn);
2178
#endif /* !MODULE_NAME */
2179

2180 2181 2182 2183 2184
    /* force update the @readonly attribute which was inherited from the
     * virNetServerService object - this is important for sockets that are RW
     * by default, but do accept RO flags, e.g. TCP
     */
    virNetServerClientSetReadonly(client, (flags & VIR_CONNECT_RO));
2185
    rv = 0;
2186

2187
 cleanup:
2188
    if (rv < 0) {
2189
        virNetMessageSaveError(rerr);
2190 2191 2192 2193 2194
        if (priv->conn) {
            virObjectUnref(priv->conn);
            priv->conn = NULL;
        }
    }
2195
    virMutexUnlock(&priv->lock);
2196
    return rv;
2197 2198 2199 2200
}


static int
J
Ján Tomko 已提交
2201
remoteDispatchConnectClose(virNetServerPtr server G_GNUC_UNUSED,
2202
                           virNetServerClientPtr client,
J
Ján Tomko 已提交
2203 2204
                           virNetMessagePtr msg G_GNUC_UNUSED,
                           virNetMessageErrorPtr rerr G_GNUC_UNUSED)
2205
{
2206
    virNetServerClientDelayedClose(client);
2207
    return 0;
2208 2209
}

2210

2211
static int
J
Ján Tomko 已提交
2212
remoteDispatchConnectSetIdentity(virNetServerPtr server G_GNUC_UNUSED,
2213
                                 virNetServerClientPtr client,
J
Ján Tomko 已提交
2214
                                 virNetMessagePtr msg G_GNUC_UNUSED,
2215 2216 2217 2218 2219 2220 2221
                                 virNetMessageErrorPtr rerr,
                                 remote_connect_set_identity_args *args)
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    int rv = -1;
    virConnectPtr conn = remoteGetHypervisorConn(client);
2222
    g_autoptr(virIdentity) ident = NULL;
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
    if (!conn)
        goto cleanup;

    VIR_DEBUG("Received forwarded identity");
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  REMOTE_CONNECT_IDENTITY_PARAMS_MAX,
                                  &params,
                                  &nparams) < 0)
        goto cleanup;

    VIR_TYPED_PARAMS_DEBUG(params, nparams);

    if (virConnectSetIdentityEnsureACL(conn) < 0)
        goto cleanup;

    if (!(ident = virIdentityNew()))
        goto cleanup;

    if (virIdentitySetParameters(ident, params, nparams) < 0)
        goto cleanup;

    virNetServerClientSetIdentity(client, ident);

    rv = 0;

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



2258
static int
J
Ján Tomko 已提交
2259
remoteDispatchDomainGetSchedulerType(virNetServerPtr server G_GNUC_UNUSED,
2260
                                     virNetServerClientPtr client,
J
Ján Tomko 已提交
2261
                                     virNetMessagePtr msg G_GNUC_UNUSED,
2262
                                     virNetMessageErrorPtr rerr,
2263 2264
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
2265
{
2266
    virDomainPtr dom = NULL;
2267 2268
    char *type;
    int nparams;
2269
    int rv = -1;
2270
    virConnectPtr conn = remoteGetHypervisorConn(client);
2271

2272
    if (!conn)
2273
        goto cleanup;
2274

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

2278
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
2279
        goto cleanup;
2280 2281 2282

    ret->type = type;
    ret->nparams = nparams;
2283 2284
    rv = 0;

2285
 cleanup:
2286
    if (rv < 0)
2287
        virNetMessageSaveError(rerr);
2288
    virObjectUnref(dom);
2289
    return rv;
2290 2291 2292
}

static int
J
Ján Tomko 已提交
2293
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server G_GNUC_UNUSED,
2294
                                           virNetServerClientPtr client,
J
Ján Tomko 已提交
2295
                                           virNetMessagePtr msg G_GNUC_UNUSED,
2296
                                           virNetMessageErrorPtr rerr,
2297 2298
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
2299
{
2300
    virDomainPtr dom = NULL;
2301
    virTypedParameterPtr params = NULL;
2302
    int nparams = 0;
2303
    int rv = -1;
2304
    virConnectPtr conn = remoteGetHypervisorConn(client);
2305

2306
    if (!conn)
2307
        goto cleanup;
2308

2309
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
2310
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2311
        goto cleanup;
2312
    }
2313
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2314
        goto cleanup;
2315
    nparams = args->nparams;
2316

2317
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2318
        goto cleanup;
2319

2320
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
2321
        goto cleanup;
2322

2323
    if (virTypedParamsSerialize(params, nparams,
2324
                                REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX,
2325 2326 2327
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                0) < 0)
2328 2329 2330 2331
        goto cleanup;

    rv = 0;

2332
 cleanup:
2333
    if (rv < 0)
2334
        virNetMessageSaveError(rerr);
2335
    virTypedParamsFree(params, nparams);
2336
    virObjectUnref(dom);
2337 2338 2339 2340
    return rv;
}

static int
J
Ján Tomko 已提交
2341
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server G_GNUC_UNUSED,
2342
                                                virNetServerClientPtr client,
J
Ján Tomko 已提交
2343
                                                virNetMessagePtr msg G_GNUC_UNUSED,
2344
                                                virNetMessageErrorPtr rerr,
2345 2346 2347 2348 2349
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
2350
    int nparams = 0;
2351
    int rv = -1;
2352
    virConnectPtr conn = remoteGetHypervisorConn(client);
2353

2354
    if (!conn)
2355 2356
        goto cleanup;

2357
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
2358
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2359 2360
        goto cleanup;
    }
2361
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2362
        goto cleanup;
2363
    nparams = args->nparams;
2364

2365
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2366 2367 2368 2369 2370 2371
        goto cleanup;

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

2372
    if (virTypedParamsSerialize(params, nparams,
2373
                                REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX,
2374 2375 2376
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
2377
        goto cleanup;
2378

2379
    rv = 0;
2380

2381
 cleanup:
2382
    if (rv < 0)
2383
        virNetMessageSaveError(rerr);
2384
    virTypedParamsFree(params, nparams);
2385
    virObjectUnref(dom);
2386
    return rv;
2387 2388
}

2389
static int
J
Ján Tomko 已提交
2390
remoteDispatchDomainMemoryStats(virNetServerPtr server G_GNUC_UNUSED,
2391
                                virNetServerClientPtr client,
J
Ján Tomko 已提交
2392
                                virNetMessagePtr msg G_GNUC_UNUSED,
2393
                                virNetMessageErrorPtr rerr,
2394 2395
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
2396
{
2397
    virDomainPtr dom = NULL;
2398
    virDomainMemoryStatPtr stats = NULL;
2399 2400
    int nr_stats;
    size_t i;
2401
    int rv = -1;
2402
    virConnectPtr conn = remoteGetHypervisorConn(client);
2403

2404
    if (!conn)
2405
        goto cleanup;
2406

2407
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
2408 2409
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
2410
        goto cleanup;
2411 2412
    }

2413
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2414
        goto cleanup;
2415 2416

    /* Allocate stats array for making dispatch call */
2417
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
2418
        goto cleanup;
2419

2420
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
2421
    if (nr_stats < 0)
2422
        goto cleanup;
2423 2424

    /* Allocate return buffer */
2425
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
2426
        goto cleanup;
2427 2428 2429 2430 2431 2432 2433

    /* 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;
2434 2435
    rv = 0;

2436
 cleanup:
2437
    if (rv < 0)
2438
        virNetMessageSaveError(rerr);
2439
    virObjectUnref(dom);
2440
    VIR_FREE(stats);
2441
    return rv;
2442 2443
}

2444
static int
J
Ján Tomko 已提交
2445
remoteDispatchDomainBlockPeek(virNetServerPtr server G_GNUC_UNUSED,
2446
                              virNetServerClientPtr client,
J
Ján Tomko 已提交
2447
                              virNetMessagePtr msg G_GNUC_UNUSED,
2448
                              virNetMessageErrorPtr rerr,
2449 2450
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
2451
{
2452
    virDomainPtr dom = NULL;
2453 2454 2455 2456
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
2457
    int rv = -1;
2458
    virConnectPtr conn = remoteGetHypervisorConn(client);
2459

2460
    if (!conn)
2461
        goto cleanup;
2462

2463
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2464
        goto cleanup;
2465 2466 2467 2468 2469 2470
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
2471 2472
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
2473
        goto cleanup;
2474 2475 2476
    }

    ret->buffer.buffer_len = size;
2477
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
2478
        goto cleanup;
2479

2480
    if (virDomainBlockPeek(dom, path, offset, size,
2481
                           ret->buffer.buffer_val, flags) < 0)
2482
        goto cleanup;
2483

2484 2485
    rv = 0;

2486
 cleanup:
2487
    if (rv < 0) {
2488
        virNetMessageSaveError(rerr);
2489 2490
        VIR_FREE(ret->buffer.buffer_val);
    }
2491
    virObjectUnref(dom);
2492
    return rv;
2493 2494
}

2495
static int
J
Ján Tomko 已提交
2496
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server G_GNUC_UNUSED,
2497
                                    virNetServerClientPtr client,
J
Ján Tomko 已提交
2498
                                    virNetMessagePtr msg G_GNUC_UNUSED,
2499 2500 2501 2502 2503 2504 2505
                                    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;
2506
    int nparams = 0;
2507 2508
    unsigned int flags;
    int rv = -1;
2509
    virConnectPtr conn = remoteGetHypervisorConn(client);
2510

2511
    if (!conn)
2512 2513
        goto cleanup;

2514
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2515 2516 2517
        goto cleanup;
    flags = args->flags;

2518
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
2519
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2520 2521
        goto cleanup;
    }
2522
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2523
        goto cleanup;
2524
    nparams = args->nparams;
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536

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

2537 2538
    /* Serialize the block stats. */
    if (virTypedParamsSerialize(params, nparams,
2539
                                REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX,
2540 2541 2542
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
2543 2544
        goto cleanup;

2545
 success:
2546 2547
    rv = 0;

2548
 cleanup:
2549
    if (rv < 0)
2550
        virNetMessageSaveError(rerr);
2551
    virTypedParamsFree(params, nparams);
2552
    virObjectUnref(dom);
2553 2554 2555
    return rv;
}

R
Richard W.M. Jones 已提交
2556
static int
J
Ján Tomko 已提交
2557
remoteDispatchDomainMemoryPeek(virNetServerPtr server G_GNUC_UNUSED,
2558
                               virNetServerClientPtr client,
J
Ján Tomko 已提交
2559
                               virNetMessagePtr msg G_GNUC_UNUSED,
2560
                               virNetMessageErrorPtr rerr,
2561 2562
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
2563
{
2564
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
2565 2566 2567
    unsigned long long offset;
    size_t size;
    unsigned int flags;
2568
    int rv = -1;
2569
    virConnectPtr conn = remoteGetHypervisorConn(client);
R
Richard W.M. Jones 已提交
2570

2571
    if (!conn)
2572
        goto cleanup;
2573

2574
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2575
        goto cleanup;
R
Richard W.M. Jones 已提交
2576 2577 2578 2579 2580
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
2581 2582
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
2583
        goto cleanup;
R
Richard W.M. Jones 已提交
2584 2585 2586
    }

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

2590
    if (virDomainMemoryPeek(dom, offset, size,
2591
                            ret->buffer.buffer_val, flags) < 0)
2592
        goto cleanup;
R
Richard W.M. Jones 已提交
2593

2594 2595
    rv = 0;

2596
 cleanup:
2597
    if (rv < 0) {
2598
        virNetMessageSaveError(rerr);
2599 2600
        VIR_FREE(ret->buffer.buffer_val);
    }
2601
    virObjectUnref(dom);
2602
    return rv;
R
Richard W.M. Jones 已提交
2603 2604
}

2605
static int
J
Ján Tomko 已提交
2606
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server G_GNUC_UNUSED,
2607
                                     virNetServerClientPtr client,
J
Ján Tomko 已提交
2608
                                     virNetMessagePtr msg G_GNUC_UNUSED,
2609
                                     virNetMessageErrorPtr rerr,
2610 2611
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
2612
{
2613 2614
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
2615
    int rv = -1;
2616
    virConnectPtr conn = remoteGetHypervisorConn(client);
2617

2618
    if (!conn)
2619
        goto cleanup;
2620

2621
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2622 2623
        goto cleanup;

2624
    if (VIR_ALLOC(seclabel) < 0)
2625 2626 2627 2628 2629 2630
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
2631
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
2632
        goto cleanup;
2633 2634
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
2635

2636 2637
    rv = 0;

2638
 cleanup:
2639
    if (rv < 0)
2640
        virNetMessageSaveError(rerr);
2641
    virObjectUnref(dom);
2642
    VIR_FREE(seclabel);
2643
    return rv;
2644 2645
}

M
Marcelo Cerri 已提交
2646
static int
J
Ján Tomko 已提交
2647
remoteDispatchDomainGetSecurityLabelList(virNetServerPtr server G_GNUC_UNUSED,
2648
                                         virNetServerClientPtr client,
J
Ján Tomko 已提交
2649
                                         virNetMessagePtr msg G_GNUC_UNUSED,
M
Marcelo Cerri 已提交
2650 2651 2652 2653 2654 2655
                                         virNetMessageErrorPtr rerr,
                                         remote_domain_get_security_label_list_args *args,
                                         remote_domain_get_security_label_list_ret *ret)
{
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabels = NULL;
2656 2657
    int len, rv = -1;
    size_t i;
2658
    virConnectPtr conn = remoteGetHypervisorConn(client);
M
Marcelo Cerri 已提交
2659

2660
    if (!conn)
M
Marcelo Cerri 已提交
2661 2662
        goto cleanup;

2663
    if (!(dom = get_nonnull_domain(conn, args->dom)))
M
Marcelo Cerri 已提交
2664 2665 2666 2667 2668 2669 2670 2671 2672
        goto cleanup;

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

2673
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
2674 2675 2676 2677 2678
        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];
2679
        cur->label.label_val = g_strdup(seclabels[i].label);
M
Marcelo Cerri 已提交
2680 2681 2682 2683 2684
        cur->label.label_len = label_len;
        cur->enforcing = seclabels[i].enforcing;
    }
    ret->labels.labels_len = ret->ret = len;

2685
 done:
M
Marcelo Cerri 已提交
2686 2687
    rv = 0;

2688
 cleanup:
M
Marcelo Cerri 已提交
2689 2690
    if (rv < 0)
        virNetMessageSaveError(rerr);
2691
    virObjectUnref(dom);
M
Marcelo Cerri 已提交
2692 2693 2694 2695
    VIR_FREE(seclabels);
    return rv;
}

2696
static int
J
Ján Tomko 已提交
2697
remoteDispatchNodeGetSecurityModel(virNetServerPtr server G_GNUC_UNUSED,
2698
                                   virNetServerClientPtr client,
J
Ján Tomko 已提交
2699
                                   virNetMessagePtr msg G_GNUC_UNUSED,
2700
                                   virNetMessageErrorPtr rerr,
2701
                                   remote_node_get_security_model_ret *ret)
2702
{
2703
    virSecurityModel secmodel;
2704
    int rv = -1;
2705
    virConnectPtr conn = remoteGetHypervisorConn(client);
2706

2707
    if (!conn)
2708
        goto cleanup;
2709

2710
    memset(&secmodel, 0, sizeof(secmodel));
2711
    if (virNodeGetSecurityModel(conn, &secmodel) < 0)
2712 2713 2714
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
2715
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
2716 2717 2718 2719
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
2720
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
2721
        goto cleanup;
2722
    strcpy(ret->doi.doi_val, secmodel.doi);
2723

2724 2725
    rv = 0;

2726
 cleanup:
2727
    if (rv < 0)
2728
        virNetMessageSaveError(rerr);
2729
    return rv;
2730 2731
}

2732
static int
J
Ján Tomko 已提交
2733
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server G_GNUC_UNUSED,
2734
                                   virNetServerClientPtr client,
J
Ján Tomko 已提交
2735
                                   virNetMessagePtr msg G_GNUC_UNUSED,
2736
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
2737 2738
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
2739 2740 2741 2742 2743
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
2744
    virConnectPtr conn = remoteGetHypervisorConn(client);
2745

2746
    if (!conn)
2747 2748
        goto cleanup;

2749
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2750 2751 2752
        goto cleanup;

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
2753
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
2754 2755 2756
        goto cleanup;
    }

2757
    if (VIR_INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
2758
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
2759
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2760 2761 2762 2763 2764 2765
        goto cleanup;
    }

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

E
Eric Blake 已提交
2768
    if ((num = virDomainGetVcpuPinInfo(dom,
2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
                                       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;
2782 2783 2784 2785
    cpumaps = NULL;

    rv = 0;

2786
 cleanup:
2787 2788 2789
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumaps);
2790
    virObjectUnref(dom);
2791 2792 2793 2794
    return rv;
}

static int
J
Ján Tomko 已提交
2795
remoteDispatchDomainPinEmulator(virNetServerPtr server G_GNUC_UNUSED,
2796
                                virNetServerClientPtr client,
J
Ján Tomko 已提交
2797
                                virNetMessagePtr msg G_GNUC_UNUSED,
2798 2799 2800 2801 2802
                                virNetMessageErrorPtr rerr,
                                remote_domain_pin_emulator_args *args)
{
    int rv = -1;
    virDomainPtr dom = NULL;
2803
    virConnectPtr conn = remoteGetHypervisorConn(client);
2804

2805
    if (!conn)
2806 2807
        goto cleanup;

2808
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
        goto cleanup;

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

    rv = 0;

2819
 cleanup:
2820 2821
    if (rv < 0)
        virNetMessageSaveError(rerr);
2822
    virObjectUnref(dom);
2823 2824 2825 2826 2827
    return rv;
}


static int
J
Ján Tomko 已提交
2828
remoteDispatchDomainGetEmulatorPinInfo(virNetServerPtr server G_GNUC_UNUSED,
2829
                                       virNetServerClientPtr client,
J
Ján Tomko 已提交
2830
                                       virNetMessagePtr msg G_GNUC_UNUSED,
2831 2832 2833 2834 2835 2836 2837 2838
                                       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;
2839
    virConnectPtr conn = remoteGetHypervisorConn(client);
2840

2841
    if (!conn)
2842 2843
        goto cleanup;

2844
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2845 2846 2847 2848 2849
        goto cleanup;

    /* Allocate buffers to take the results */
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maplen) < 0)
2850
        goto cleanup;
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860

    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;
2861 2862 2863 2864
    cpumaps = NULL;

    rv = 0;

2865
 cleanup:
2866
    if (rv < 0)
2867
        virNetMessageSaveError(rerr);
2868
    VIR_FREE(cpumaps);
2869
    virObjectUnref(dom);
2870 2871 2872
    return rv;
}

2873
static int
J
Ján Tomko 已提交
2874
remoteDispatchDomainGetVcpus(virNetServerPtr server G_GNUC_UNUSED,
2875
                             virNetServerClientPtr client,
J
Ján Tomko 已提交
2876
                             virNetMessagePtr msg G_GNUC_UNUSED,
2877
                             virNetMessageErrorPtr rerr,
2878 2879
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
2880
{
2881
    virDomainPtr dom = NULL;
2882 2883
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
2884 2885
    int info_len;
    size_t i;
2886
    int rv = -1;
2887
    virConnectPtr conn = remoteGetHypervisorConn(client);
2888

2889
    if (!conn)
2890
        goto cleanup;
2891

2892
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2893
        goto cleanup;
2894

2895
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
2896
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
2897
        goto cleanup;
2898
    }
2899

2900
    if (VIR_INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
2901
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
2902
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2903 2904 2905 2906 2907
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
2908
        goto cleanup;
2909 2910
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
2911
        goto cleanup;
2912 2913 2914 2915 2916 2917 2918 2919 2920

    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)
2921
        goto cleanup;
2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938

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

2940
 cleanup:
2941
    if (rv < 0) {
2942
        virNetMessageSaveError(rerr);
2943 2944 2945 2946
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
2947
    virObjectUnref(dom);
2948
    return rv;
2949 2950
}

2951
static int
J
Ján Tomko 已提交
2952
remoteDispatchDomainGetIOThreadInfo(virNetServerPtr server G_GNUC_UNUSED,
2953
                                    virNetServerClientPtr client,
J
Ján Tomko 已提交
2954
                                    virNetMessagePtr msg G_GNUC_UNUSED,
2955 2956 2957
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_get_iothread_info_args *args,
                                    remote_domain_get_iothread_info_ret *ret)
2958 2959 2960 2961 2962 2963 2964
{
    int rv = -1;
    size_t i;
    virDomainIOThreadInfoPtr *info = NULL;
    virDomainPtr dom = NULL;
    remote_domain_iothread_info *dst;
    int ninfo = 0;
2965
    virConnectPtr conn = remoteGetHypervisorConn(client);
2966

2967
    if (!conn)
2968 2969
        goto cleanup;

2970
    if (!(dom = get_nonnull_domain(conn, args->dom)))
2971 2972
        goto cleanup;

2973
    if ((ninfo = virDomainGetIOThreadInfo(dom, &info, args->flags)) < 0)
2974 2975
        goto cleanup;

2976
    if (ninfo > REMOTE_IOTHREAD_INFO_MAX) {
2977 2978
        virReportError(VIR_ERR_RPC,
                       _("Too many IOThreads in info: %d for limit %d"),
2979
                       ninfo, REMOTE_IOTHREAD_INFO_MAX);
2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014
        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++)
3015
            virDomainIOThreadInfoFree(info[i]);
3016 3017 3018 3019 3020
    VIR_FREE(info);

    return rv;
}

3021
static int
J
Ján Tomko 已提交
3022
remoteDispatchDomainMigratePrepare(virNetServerPtr server G_GNUC_UNUSED,
3023
                                   virNetServerClientPtr client,
J
Ján Tomko 已提交
3024
                                   virNetMessagePtr msg G_GNUC_UNUSED,
3025
                                   virNetMessageErrorPtr rerr,
3026 3027
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
3028
{
3029 3030 3031 3032 3033
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
3034
    int rv = -1;
3035
    virConnectPtr conn = remoteGetHypervisorConn(client);
3036

3037
    if (!conn)
3038
        goto cleanup;
3039

3040 3041 3042 3043
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
3044
    if (VIR_ALLOC(uri_out) < 0)
3045
        goto cleanup;
3046

3047
    if (virDomainMigratePrepare(conn, &cookie, &cookielen,
3048 3049
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
3050
        goto cleanup;
3051

3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062
    /* 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;
    }
3063

3064
    rv = 0;
3065

3066
 cleanup:
3067
    if (rv < 0)
3068
        virNetMessageSaveError(rerr);
3069
    VIR_FREE(uri_out);
3070
    return rv;
3071 3072
}

3073
static int
J
Ján Tomko 已提交
3074
remoteDispatchDomainMigratePrepare2(virNetServerPtr server G_GNUC_UNUSED,
3075
                                    virNetServerClientPtr client,
J
Ján Tomko 已提交
3076
                                    virNetMessagePtr msg G_GNUC_UNUSED,
3077
                                    virNetMessageErrorPtr rerr,
3078 3079
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
3080
{
3081 3082 3083 3084 3085
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
3086
    int rv = -1;
3087
    virConnectPtr conn = remoteGetHypervisorConn(client);
3088

3089
    if (!conn)
3090
        goto cleanup;
3091

3092 3093
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
3094

3095
    /* Wacky world of XDR ... */
3096
    if (VIR_ALLOC(uri_out) < 0)
3097
        goto cleanup;
3098

3099
    if (virDomainMigratePrepare2(conn, &cookie, &cookielen,
3100 3101 3102
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
3103
        goto cleanup;
3104

3105 3106 3107 3108 3109 3110
    /* 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;
3111

3112 3113
    rv = 0;

3114
 cleanup:
3115
    if (rv < 0) {
3116
        virNetMessageSaveError(rerr);
3117 3118
        VIR_FREE(uri_out);
    }
3119
    return rv;
3120 3121
}

C
Chris Lalancette 已提交
3122
static int
J
Ján Tomko 已提交
3123
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server G_GNUC_UNUSED,
3124
                                        virNetServerClientPtr client,
J
Ján Tomko 已提交
3125
                                        virNetMessagePtr msg G_GNUC_UNUSED,
3126 3127 3128
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
3129
{
3130
    virDomainPtr dom = NULL;
3131
    virTypedParameterPtr params = NULL;
3132
    int nparams = 0;
3133
    unsigned int flags;
3134
    int rv = -1;
3135
    virConnectPtr conn = remoteGetHypervisorConn(client);
3136

3137
    if (!conn)
3138
        goto cleanup;
C
Chris Lalancette 已提交
3139

3140
    flags = args->flags;
C
Chris Lalancette 已提交
3141

3142
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
3143
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3144 3145
        goto cleanup;
    }
3146
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3147
        goto cleanup;
3148
    nparams = args->nparams;
C
Chris Lalancette 已提交
3149

3150
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3151
        goto cleanup;
C
Chris Lalancette 已提交
3152

3153
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
3154
        goto cleanup;
C
Chris Lalancette 已提交
3155

3156 3157 3158 3159 3160 3161
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
3162 3163
    }

3164
    if (virTypedParamsSerialize(params, nparams,
3165
                                REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX,
3166 3167 3168
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
3169
        goto cleanup;
3170

3171
 success:
3172 3173
    rv = 0;

3174
 cleanup:
3175
    if (rv < 0)
3176
        virNetMessageSaveError(rerr);
3177
    virTypedParamsFree(params, nparams);
3178
    virObjectUnref(dom);
3179
    return rv;
3180 3181
}

3182
static int
J
Ján Tomko 已提交
3183
remoteDispatchDomainGetNumaParameters(virNetServerPtr server G_GNUC_UNUSED,
3184
                                      virNetServerClientPtr client,
J
Ján Tomko 已提交
3185
                                      virNetMessagePtr msg G_GNUC_UNUSED,
3186 3187 3188 3189 3190 3191
                                      virNetMessageErrorPtr rerr,
                                      remote_domain_get_numa_parameters_args *args,
                                      remote_domain_get_numa_parameters_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
3192
    int nparams = 0;
3193 3194
    unsigned int flags;
    int rv = -1;
3195
    virConnectPtr conn = remoteGetHypervisorConn(client);
3196

3197
    if (!conn)
3198 3199 3200 3201
        goto cleanup;

    flags = args->flags;

3202
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
3203
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3204 3205
        goto cleanup;
    }
3206
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3207
        goto cleanup;
3208
    nparams = args->nparams;
3209

3210
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223
        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;
    }

3224
    if (virTypedParamsSerialize(params, nparams,
3225
                                REMOTE_DOMAIN_NUMA_PARAMETERS_MAX,
3226 3227 3228
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                flags) < 0)
3229 3230
        goto cleanup;

3231
 success:
3232 3233
    rv = 0;

3234
 cleanup:
3235 3236
    if (rv < 0)
        virNetMessageSaveError(rerr);
3237
    virTypedParamsFree(params, nparams);
3238
    virObjectUnref(dom);
3239 3240 3241
    return rv;
}

3242
static int
J
Ján Tomko 已提交
3243
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server G_GNUC_UNUSED,
3244
                                       virNetServerClientPtr client,
J
Ján Tomko 已提交
3245
                                       virNetMessagePtr msg G_GNUC_UNUSED,
3246 3247 3248
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
3249
{
3250
    virDomainPtr dom = NULL;
3251
    virTypedParameterPtr params = NULL;
3252
    int nparams = 0;
3253
    unsigned int flags;
3254
    int rv = -1;
3255
    virConnectPtr conn = remoteGetHypervisorConn(client);
3256

3257
    if (!conn)
3258
        goto cleanup;
3259

3260 3261
    flags = args->flags;

3262
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
3263
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3264
        goto cleanup;
3265
    }
3266
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3267
        goto cleanup;
3268
    nparams = args->nparams;
3269

3270
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3271
        goto cleanup;
3272

3273
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
3274
        goto cleanup;
3275

3276 3277 3278 3279 3280 3281 3282 3283
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

3284
    if (virTypedParamsSerialize(params, nparams,
3285
                                REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX,
3286 3287 3288
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
3289
        goto cleanup;
3290

3291
 success:
3292
    rv = 0;
3293

3294
 cleanup:
3295
    if (rv < 0)
3296
        virNetMessageSaveError(rerr);
3297
    virTypedParamsFree(params, nparams);
3298
    virObjectUnref(dom);
3299
    return rv;
3300 3301
}

3302
static int
J
Ján Tomko 已提交
3303
remoteDispatchNodeGetCPUStats(virNetServerPtr server G_GNUC_UNUSED,
3304
                              virNetServerClientPtr client,
J
Ján Tomko 已提交
3305
                              virNetMessagePtr msg G_GNUC_UNUSED,
3306 3307 3308
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
3309
{
3310
    virNodeCPUStatsPtr params = NULL;
3311
    size_t i;
3312
    int cpuNum = args->cpuNum;
3313
    int nparams = 0;
3314 3315
    unsigned int flags;
    int rv = -1;
3316
    virConnectPtr conn = remoteGetHypervisorConn(client);
3317

3318
    if (!conn)
3319 3320 3321 3322
        goto cleanup;

    flags = args->flags;

3323
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
3324
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3325 3326
        goto cleanup;
    }
3327
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3328
        goto cleanup;
3329
    nparams = args->nparams;
3330

3331
    if (virNodeGetCPUStats(conn, cpuNum, params, &nparams, flags) < 0)
3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344
        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)
3345
        goto cleanup;
3346 3347 3348

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
3349
        ret->params.params_val[i].field = g_strdup(params[i].field);
3350 3351 3352 3353

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

3354
 success:
3355 3356
    rv = 0;

3357
 cleanup:
3358
    if (rv < 0) {
3359
        virNetMessageSaveError(rerr);
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370
        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
J
Ján Tomko 已提交
3371
remoteDispatchNodeGetMemoryStats(virNetServerPtr server G_GNUC_UNUSED,
3372
                                 virNetServerClientPtr client,
J
Ján Tomko 已提交
3373
                                 virNetMessagePtr msg G_GNUC_UNUSED,
3374 3375 3376
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
3377
{
3378
    virNodeMemoryStatsPtr params = NULL;
3379
    size_t i;
3380
    int cellNum = args->cellNum;
3381
    int nparams = 0;
3382 3383
    unsigned int flags;
    int rv = -1;
3384
    virConnectPtr conn = remoteGetHypervisorConn(client);
3385

3386
    if (!conn)
3387 3388 3389 3390
        goto cleanup;

    flags = args->flags;

3391
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
3392
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3393 3394
        goto cleanup;
    }
3395
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3396
        goto cleanup;
3397
    nparams = args->nparams;
3398

3399
    if (virNodeGetMemoryStats(conn, cellNum, params, &nparams, flags) < 0)
3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412
        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)
3413
        goto cleanup;
3414 3415 3416

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
3417
        ret->params.params_val[i].field = g_strdup(params[i].field);
3418 3419 3420 3421

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

3422
 success:
3423 3424
    rv = 0;

3425
 cleanup:
3426
    if (rv < 0) {
3427
        virNetMessageSaveError(rerr);
3428 3429 3430 3431 3432 3433 3434 3435 3436 3437
        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;
}

3438
static int
J
Ján Tomko 已提交
3439
remoteDispatchDomainGetLaunchSecurityInfo(virNetServerPtr server G_GNUC_UNUSED,
3440
                                          virNetServerClientPtr client,
J
Ján Tomko 已提交
3441
                                          virNetMessagePtr msg G_GNUC_UNUSED,
3442 3443 3444 3445 3446 3447 3448 3449
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_get_launch_security_info_args *args,
                                          remote_domain_get_launch_security_info_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    int rv = -1;
3450
    virConnectPtr conn = remoteGetHypervisorConn(client);
3451

3452
    if (!conn)
3453 3454
        goto cleanup;

3455
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3456 3457 3458 3459 3460 3461
        goto cleanup;

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

    if (virTypedParamsSerialize(params, nparams,
3462
                                REMOTE_DOMAIN_LAUNCH_SECURITY_INFO_PARAMS_MAX,
3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
        goto cleanup;

    rv = 0;

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

3478
static int
J
Ján Tomko 已提交
3479
remoteDispatchDomainGetPerfEvents(virNetServerPtr server G_GNUC_UNUSED,
3480
                                  virNetServerClientPtr client,
J
Ján Tomko 已提交
3481
                                  virNetMessagePtr msg G_GNUC_UNUSED,
3482 3483 3484 3485 3486 3487 3488 3489
                                  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;
3490
    virConnectPtr conn = remoteGetHypervisorConn(client);
3491

3492
    if (!conn)
3493 3494
        goto cleanup;

3495
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3496 3497
        goto cleanup;

3498
    if (virDomainGetPerfEvents(dom, &params, &nparams, args->flags) < 0)
3499 3500 3501
        goto cleanup;

    if (virTypedParamsSerialize(params, nparams,
3502
                                REMOTE_DOMAIN_PERF_EVENTS_MAX,
3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517
                                (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;
}

3518
static int
J
Ján Tomko 已提交
3519
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server G_GNUC_UNUSED,
3520
                                    virNetServerClientPtr client,
J
Ján Tomko 已提交
3521
                                    virNetMessagePtr msg G_GNUC_UNUSED,
3522 3523 3524 3525 3526 3527 3528
                                    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;
3529
    virConnectPtr conn = remoteGetHypervisorConn(client);
3530

3531
    if (!conn)
3532 3533
        goto cleanup;

3534
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547
        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;

3548
 cleanup:
3549 3550
    if (rv < 0)
        virNetMessageSaveError(rerr);
3551
    virObjectUnref(dom);
3552 3553 3554
    return rv;
}

3555
static int
J
Ján Tomko 已提交
3556
remoteDispatchDomainGetBlockIoTune(virNetServerPtr server G_GNUC_UNUSED,
3557
                                   virNetServerClientPtr client,
J
Ján Tomko 已提交
3558
                                   virNetMessagePtr hdr G_GNUC_UNUSED,
3559 3560 3561 3562 3563 3564 3565
                                   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;
3566
    int nparams = 0;
3567
    virConnectPtr conn = remoteGetHypervisorConn(client);
3568

3569
    if (!conn)
3570 3571
        goto cleanup;

3572
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
3573
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3574 3575 3576
        goto cleanup;
    }

3577
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3578
        goto cleanup;
3579
    nparams = args->nparams;
3580

3581
    if (!(dom = get_nonnull_domain(conn, args->dom)))
3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595
        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;
    }

3596 3597
    /* Serialize the block I/O tuning parameters. */
    if (virTypedParamsSerialize(params, nparams,
3598
                                REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX,
3599 3600 3601
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
3602 3603
        goto cleanup;

3604
 success:
3605 3606
    rv = 0;

3607
 cleanup:
3608 3609
    if (rv < 0)
        virNetMessageSaveError(rerr);
3610
    virTypedParamsFree(params, nparams);
3611
    virObjectUnref(dom);
3612 3613
    return rv;
}
3614

D
Daniel Veillard 已提交
3615 3616
/*-------------------------------------------------------------*/

3617
static int
3618
remoteDispatchAuthList(virNetServerPtr server,
3619
                       virNetServerClientPtr client,
J
Ján Tomko 已提交
3620
                       virNetMessagePtr msg G_GNUC_UNUSED,
3621
                       virNetMessageErrorPtr rerr,
3622
                       remote_auth_list_ret *ret)
3623
{
3624
    int rv = -1;
3625 3626
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
3627
    gid_t callerGid;
3628
    pid_t callerPid;
3629
    unsigned long long timestamp;
3630 3631 3632 3633 3634 3635

    /* 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) {
3636
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
3637
                                              &callerPid, &timestamp) < 0) {
3638 3639 3640 3641
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
3642
            char *ident;
3643 3644
            ident = g_strdup_printf("pid:%lld,uid:%d", (long long)callerPid,
                                    (int)callerUid);
J
Jim Fehlig 已提交
3645
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
3646
            virNetServerSetClientAuthenticated(server, client);
3647
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
3648
            VIR_FREE(ident);
3649 3650
        }
    }
3651

3652
    ret->types.types_len = 1;
3653
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
3654
        goto cleanup;
3655

3656
    switch ((virNetServerServiceAuthMethods) auth) {
3657 3658 3659 3660 3661 3662 3663 3664 3665 3666
    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;
    }
3667

3668 3669
    rv = 0;

3670
 cleanup:
3671
    if (rv < 0)
3672
        virNetMessageSaveError(rerr);
3673
    return rv;
3674 3675 3676
}


3677
#ifdef WITH_SASL
3678 3679
/*
 * Initializes the SASL session in prepare for authentication
3680
 * and gives the client a list of allowed mechanisms to choose
3681 3682
 */
static int
J
Ján Tomko 已提交
3683
remoteDispatchAuthSaslInit(virNetServerPtr server G_GNUC_UNUSED,
3684
                           virNetServerClientPtr client,
J
Ján Tomko 已提交
3685
                           virNetMessagePtr msg G_GNUC_UNUSED,
3686
                           virNetMessageErrorPtr rerr,
3687
                           remote_auth_sasl_init_ret *ret)
3688
{
3689 3690 3691
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3692

3693
    virMutexLock(&priv->lock);
3694

3695 3696 3697
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
3698
        VIR_ERROR(_("client tried invalid SASL init request"));
3699
        goto authfail;
3700 3701
    }

3702 3703
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
3704 3705
                                      virNetServerClientLocalAddrStringSASL(client),
                                      virNetServerClientRemoteAddrStringSASL(client));
3706
    if (!sasl)
3707
        goto authfail;
3708

3709
    /* Inform SASL that we've got an external SSF layer from TLS */
3710 3711 3712 3713
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
3714
            goto authfail;
3715 3716 3717 3718 3719

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
3720
            goto authfail;
3721 3722
    }

3723
    if (virNetServerClientIsSecure(client))
3724
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
3725 3726
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
3727
        /* Plain TCP, better get an SSF layer */
3728 3729 3730 3731
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
3732

3733
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
3734
        goto authfail;
3735
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
3736

3737 3738
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
3739
    return 0;
3740

3741
 authfail:
3742
    virResetLastError();
3743 3744
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3745
    virNetMessageSaveError(rerr);
3746 3747 3748
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3749
    virObjectUnref(sasl);
3750
    virMutexUnlock(&priv->lock);
3751
    return -1;
3752 3753
}

3754
/*
3755 3756
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
3757
static int
3758 3759
remoteSASLFinish(virNetServerPtr server,
                 virNetServerClientPtr client)
3760
{
3761
    g_autoptr(virIdentity) clnt_identity = NULL;
3762 3763 3764 3765 3766 3767 3768
    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)
3769
            return -1;
3770 3771 3772 3773 3774 3775

        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;
        }
3776
    }
3777 3778

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
3779
        return -2;
3780

3781 3782
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
3783

3784
    if (!(clnt_identity = virNetServerClientGetIdentity(client)))
3785
        return -1;
3786

3787
    virNetServerSetClientAuthenticated(server, client);
3788
    virNetServerClientSetSASLSession(client, priv->sasl);
3789
    virIdentitySetSASLUserName(clnt_identity, identity);
3790

3791
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
3792

3793 3794 3795
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3796

3797
    virObjectUnref(priv->sasl);
3798
    priv->sasl = NULL;
3799

3800 3801
    return 0;
}
3802

3803 3804 3805 3806
/*
 * This starts the SASL authentication negotiation.
 */
static int
3807
remoteDispatchAuthSaslStart(virNetServerPtr server,
3808
                            virNetServerClientPtr client,
J
Ján Tomko 已提交
3809
                            virNetMessagePtr msg G_GNUC_UNUSED,
3810
                            virNetMessageErrorPtr rerr,
3811 3812
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
3813 3814
{
    const char *serverout;
3815
    size_t serveroutlen;
3816
    int err;
3817 3818
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3819
    const char *identity;
3820

3821
    virMutexLock(&priv->lock);
3822

3823 3824 3825
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
3826
        VIR_ERROR(_("client tried invalid SASL start request"));
3827
        goto authfail;
3828 3829
    }

3830 3831
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
3832 3833 3834 3835 3836 3837 3838 3839 3840
    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)
3841
        goto authfail;
3842

3843
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3844
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
3845
        goto authfail;
3846 3847 3848 3849
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3850 3851
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3852 3853 3854 3855 3856 3857 3858
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3859
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3860
    if (err == VIR_NET_SASL_CONTINUE) {
3861 3862
        ret->complete = 0;
    } else {
3863
        /* Check username whitelist ACL */
3864
        if ((err = remoteSASLFinish(server, client)) < 0) {
3865 3866 3867 3868 3869
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3870

3871 3872 3873
        ret->complete = 1;
    }

3874
    virMutexUnlock(&priv->lock);
3875
    return 0;
3876

3877
 authfail:
3878 3879 3880
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3881 3882
    goto error;

3883
 authdeny:
3884
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3885 3886 3887
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3888 3889
    goto error;

3890
 error:
3891
    virObjectUnref(priv->sasl);
3892 3893
    priv->sasl = NULL;
    virResetLastError();
3894 3895
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3896
    virNetMessageSaveError(rerr);
3897
    virMutexUnlock(&priv->lock);
3898
    return -1;
3899 3900 3901 3902
}


static int
3903
remoteDispatchAuthSaslStep(virNetServerPtr server,
3904
                           virNetServerClientPtr client,
J
Ján Tomko 已提交
3905
                           virNetMessagePtr msg G_GNUC_UNUSED,
3906
                           virNetMessageErrorPtr rerr,
3907 3908
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
3909 3910
{
    const char *serverout;
3911
    size_t serveroutlen;
3912
    int err;
3913 3914
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3915
    const char *identity;
3916

3917 3918 3919 3920 3921
    virMutexLock(&priv->lock);

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

3926
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
3927
              args->data.data_len, args->nil);
3928 3929 3930 3931 3932 3933 3934 3935
    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)
3936
        goto authfail;
3937 3938

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3939
        VIR_ERROR(_("sasl step reply data too long %d"),
3940
                  (int)serveroutlen);
3941
        goto authfail;
3942 3943 3944 3945
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3946 3947
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3948 3949 3950 3951 3952 3953 3954
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3955
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3956
    if (err == VIR_NET_SASL_CONTINUE) {
3957 3958
        ret->complete = 0;
    } else {
3959
        /* Check username whitelist ACL */
3960
        if ((err = remoteSASLFinish(server, client)) < 0) {
3961 3962 3963 3964 3965
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3966

3967 3968 3969
        ret->complete = 1;
    }

3970
    virMutexUnlock(&priv->lock);
3971
    return 0;
3972

3973
 authfail:
3974 3975 3976
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3977 3978
    goto error;

3979
 authdeny:
3980
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3981 3982 3983
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3984 3985
    goto error;

3986
 error:
3987
    virObjectUnref(priv->sasl);
3988 3989
    priv->sasl = NULL;
    virResetLastError();
3990 3991
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3992
    virNetMessageSaveError(rerr);
3993
    virMutexUnlock(&priv->lock);
3994 3995
    return -1;
}
3996 3997
#else
static int
J
Ján Tomko 已提交
3998 3999 4000
remoteDispatchAuthSaslInit(virNetServerPtr server G_GNUC_UNUSED,
                           virNetServerClientPtr client G_GNUC_UNUSED,
                           virNetMessagePtr msg G_GNUC_UNUSED,
4001
                           virNetMessageErrorPtr rerr,
J
Ján Tomko 已提交
4002
                           remote_auth_sasl_init_ret *ret G_GNUC_UNUSED)
4003 4004
{
    VIR_WARN("Client tried unsupported SASL auth");
4005 4006
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
4007 4008 4009 4010
    virNetMessageSaveError(rerr);
    return -1;
}
static int
J
Ján Tomko 已提交
4011 4012 4013
remoteDispatchAuthSaslStart(virNetServerPtr server G_GNUC_UNUSED,
                            virNetServerClientPtr client G_GNUC_UNUSED,
                            virNetMessagePtr msg G_GNUC_UNUSED,
4014
                            virNetMessageErrorPtr rerr,
J
Ján Tomko 已提交
4015 4016
                            remote_auth_sasl_start_args *args G_GNUC_UNUSED,
                            remote_auth_sasl_start_ret *ret G_GNUC_UNUSED)
4017 4018
{
    VIR_WARN("Client tried unsupported SASL auth");
4019 4020
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
4021 4022 4023 4024
    virNetMessageSaveError(rerr);
    return -1;
}
static int
J
Ján Tomko 已提交
4025 4026 4027
remoteDispatchAuthSaslStep(virNetServerPtr server G_GNUC_UNUSED,
                           virNetServerClientPtr client G_GNUC_UNUSED,
                           virNetMessagePtr msg G_GNUC_UNUSED,
4028
                           virNetMessageErrorPtr rerr,
J
Ján Tomko 已提交
4029 4030
                           remote_auth_sasl_step_args *args G_GNUC_UNUSED,
                           remote_auth_sasl_step_ret *ret G_GNUC_UNUSED)
4031 4032
{
    VIR_WARN("Client tried unsupported SASL auth");
4033 4034
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
4035 4036 4037 4038
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
4039 4040 4041



4042
static int
4043
remoteDispatchAuthPolkit(virNetServerPtr server,
4044
                         virNetServerClientPtr client,
J
Ján Tomko 已提交
4045
                         virNetMessagePtr msg G_GNUC_UNUSED,
4046
                         virNetMessageErrorPtr rerr,
4047
                         remote_auth_polkit_ret *ret)
4048
{
4049
    pid_t callerPid = -1;
4050
    gid_t callerGid = -1;
4051
    uid_t callerUid = -1;
4052
    unsigned long long timestamp;
4053
    const char *action;
4054
    char *ident = NULL;
4055 4056
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4057
    int rv;
4058

4059 4060
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
4061 4062 4063
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

4064 4065
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
4066
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
4067 4068 4069
        goto authfail;
    }

4070
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
4071
                                          &callerPid, &timestamp) < 0) {
4072 4073 4074
        goto authfail;
    }

4075 4076 4077 4078 4079 4080
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

4081 4082
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
4083

4084 4085 4086 4087 4088 4089 4090
    rv = virPolkitCheckAuth(action,
                            callerPid,
                            timestamp,
                            callerUid,
                            NULL,
                            true);
    if (rv == -1)
4091
        goto authfail;
4092
    else if (rv == -2)
4093
        goto authdeny;
4094

4095 4096 4097
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
4098 4099
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
4100 4101
    ret->complete = 1;

4102
    virNetServerSetClientAuthenticated(server, client);
4103
    virMutexUnlock(&priv->lock);
4104

4105
    return 0;
4106

4107
 error:
4108
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
4109
    virMutexUnlock(&priv->lock);
4110 4111
    return -1;

4112
 authfail:
4113 4114 4115
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
4116
    goto error;
4117

4118
 authdeny:
4119 4120 4121
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
4122
    goto error;
4123 4124
}

4125

4126
static int
J
Ján Tomko 已提交
4127
remoteDispatchNodeDeviceGetParent(virNetServerPtr server G_GNUC_UNUSED,
4128
                                  virNetServerClientPtr client,
J
Ján Tomko 已提交
4129
                                  virNetMessagePtr msg G_GNUC_UNUSED,
4130
                                  virNetMessageErrorPtr rerr,
4131 4132
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
4133
{
4134 4135
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
4136
    int rv = -1;
4137
    virConnectPtr conn = remoteGetNodeDevConn(client);
4138

4139
    if (!conn)
4140
        goto cleanup;
4141

4142
    if (!(dev = virNodeDeviceLookupByName(conn, args->name)))
4143 4144
        goto cleanup;

4145 4146 4147
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
4148
        ret->parentName = NULL;
4149 4150
    } else {
        /* remoteDispatchClientRequest will free this. */
4151
        if (VIR_ALLOC(ret->parentName) < 0)
4152
            goto cleanup;
4153
        *(ret->parentName) = g_strdup(parent);
4154 4155
    }

4156 4157
    rv = 0;

4158
 cleanup:
4159
    if (rv < 0)
4160
        virNetMessageSaveError(rerr);
4161
    virObjectUnref(dev);
4162
    return rv;
4163 4164
}

4165
static int
J
Ján Tomko 已提交
4166
remoteDispatchConnectRegisterCloseCallback(virNetServerPtr server G_GNUC_UNUSED,
4167
                                           virNetServerClientPtr client,
J
Ján Tomko 已提交
4168
                                           virNetMessagePtr msg G_GNUC_UNUSED,
4169 4170 4171 4172 4173
                                           virNetMessageErrorPtr rerr)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4174
    virConnectPtr conn = remoteGetHypervisorConn(client);
4175 4176 4177

    virMutexLock(&priv->lock);

4178
    if (!conn)
4179 4180
        goto cleanup;

4181
    if (virConnectRegisterCloseCallback(conn,
4182
                                        remoteRelayConnectionClosedEvent,
4183
                                        client, NULL) < 0)
4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196
        goto cleanup;

    priv->closeRegistered = true;
    rv = 0;

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

static int
J
Ján Tomko 已提交
4197
remoteDispatchConnectUnregisterCloseCallback(virNetServerPtr server G_GNUC_UNUSED,
4198
                                             virNetServerClientPtr client,
J
Ján Tomko 已提交
4199
                                             virNetMessagePtr msg G_GNUC_UNUSED,
4200 4201 4202 4203 4204
                                             virNetMessageErrorPtr rerr)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4205
    virConnectPtr conn = remoteGetHypervisorConn(client);
4206 4207 4208

    virMutexLock(&priv->lock);

4209
    if (!conn)
4210 4211
        goto cleanup;

4212
    if (virConnectUnregisterCloseCallback(conn,
4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224
                                          remoteRelayConnectionClosedEvent) < 0)
        goto cleanup;

    priv->closeRegistered = false;
    rv = 0;

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

static int
J
Ján Tomko 已提交
4227
remoteDispatchConnectDomainEventRegister(virNetServerPtr server G_GNUC_UNUSED,
4228
                                         virNetServerClientPtr client,
J
Ján Tomko 已提交
4229 4230 4231
                                         virNetMessagePtr msg G_GNUC_UNUSED,
                                         virNetMessageErrorPtr rerr G_GNUC_UNUSED,
                                         remote_connect_domain_event_register_ret *ret G_GNUC_UNUSED)
O
Osier Yang 已提交
4232
{
4233
    int callbackID;
4234
    int rv = -1;
4235 4236
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
4237 4238
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4239
    virConnectPtr conn = remoteGetHypervisorConn(client);
O
Osier Yang 已提交
4240

4241 4242
    virMutexLock(&priv->lock);

4243
    if (!conn)
4244
        goto cleanup;
4245

4246 4247 4248 4249
    /* 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
4250 4251 4252 4253
     * our callback; or you can use VIR_APPEND_ELEMENT_COPY to avoid
     * clearing 'callback' and having to juggle the pointer
     * between 'ref' and 'callback'.
     */
4254 4255
    if (VIR_ALLOC(callback) < 0)
        goto cleanup;
4256
    callback->client = virObjectRef(client);
4257
    callback->program = virObjectRef(remoteProgram);
4258 4259
    callback->eventID = VIR_DOMAIN_EVENT_ID_LIFECYCLE;
    callback->callbackID = -1;
4260
    callback->legacy = true;
4261 4262 4263 4264
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
4265
        goto cleanup;
O
Osier Yang 已提交
4266

4267
    if ((callbackID = virConnectDomainEventRegisterAny(conn,
4268 4269 4270
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
4271 4272 4273 4274 4275
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
4276
        goto cleanup;
4277
    }
O
Osier Yang 已提交
4278

4279
    ref->callbackID = callbackID;
4280

4281 4282
    rv = 0;

4283
 cleanup:
4284
    virMutexUnlock(&priv->lock);
4285
    remoteEventCallbackFree(callback);
4286
    if (rv < 0)
4287
        virNetMessageSaveError(rerr);
4288
    return rv;
O
Osier Yang 已提交
4289 4290
}

4291
static int
J
Ján Tomko 已提交
4292
remoteDispatchConnectDomainEventDeregister(virNetServerPtr server G_GNUC_UNUSED,
4293
                                           virNetServerClientPtr client,
J
Ján Tomko 已提交
4294 4295 4296
                                           virNetMessagePtr msg G_GNUC_UNUSED,
                                           virNetMessageErrorPtr rerr G_GNUC_UNUSED,
                                           remote_connect_domain_event_deregister_ret *ret G_GNUC_UNUSED)
4297
{
4298
    int callbackID = -1;
4299
    int rv = -1;
4300
    size_t i;
4301 4302
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4303
    virConnectPtr conn = remoteGetHypervisorConn(client);
4304

4305 4306
    virMutexLock(&priv->lock);

4307
    if (!conn)
4308
        goto cleanup;
4309

4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320
    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);
4321
        goto cleanup;
4322
    }
4323

4324
    if (virConnectDomainEventDeregisterAny(conn, callbackID) < 0)
4325
        goto cleanup;
4326

4327 4328
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
4329

4330 4331
    rv = 0;

4332
 cleanup:
4333
    virMutexUnlock(&priv->lock);
4334
    if (rv < 0)
4335
        virNetMessageSaveError(rerr);
4336 4337 4338 4339
    return rv;
}

static void
4340
remoteDispatchObjectEventSend(virNetServerClientPtr client,
4341
                              virNetServerProgramPtr program,
4342 4343 4344 4345
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
4346
    virNetMessagePtr msg;
4347

4348
    if (!(msg = virNetMessageNew(false)))
4349
        goto cleanup;
4350

4351 4352 4353 4354 4355 4356
    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;
4357

4358
    if (virNetMessageEncodeHeader(msg) < 0)
4359 4360
        goto cleanup;

4361 4362
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
4363

4364
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
4365 4366
    if (virNetServerClientSendMessage(client, msg) < 0)
        goto cleanup;
4367

4368
    xdr_free(proc, data);
4369 4370
    return;

4371
 cleanup:
4372
    virNetMessageFree(msg);
4373
    xdr_free(proc, data);
4374 4375
}

4376
static int
J
Ján Tomko 已提交
4377
remoteDispatchSecretGetValue(virNetServerPtr server G_GNUC_UNUSED,
4378
                             virNetServerClientPtr client,
J
Ján Tomko 已提交
4379
                             virNetMessagePtr msg G_GNUC_UNUSED,
4380
                             virNetMessageErrorPtr rerr,
4381 4382
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
4383
{
4384 4385 4386
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
4387
    int rv = -1;
4388
    virConnectPtr conn = remoteGetSecretConn(client);
4389

4390
    if (!conn)
4391
        goto cleanup;
4392

4393
    if (!(secret = get_nonnull_secret(conn, args->secret)))
4394
        goto cleanup;
4395

4396
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
4397
        goto cleanup;
4398

4399 4400 4401
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

4402 4403
    rv = 0;

4404
 cleanup:
4405
    if (rv < 0)
4406
        virNetMessageSaveError(rerr);
4407
    virObjectUnref(secret);
4408
    return rv;
4409 4410
}

4411
static int
J
Ján Tomko 已提交
4412
remoteDispatchDomainGetState(virNetServerPtr server G_GNUC_UNUSED,
4413
                             virNetServerClientPtr client,
J
Ján Tomko 已提交
4414
                             virNetMessagePtr msg G_GNUC_UNUSED,
4415
                             virNetMessageErrorPtr rerr,
4416 4417 4418 4419 4420
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
4421
    virConnectPtr conn = remoteGetHypervisorConn(client);
4422

4423
    if (!conn)
4424 4425
        goto cleanup;

4426
    if (!(dom = get_nonnull_domain(conn, args->dom)))
4427 4428 4429 4430 4431 4432 4433
        goto cleanup;

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

    rv = 0;

4434
 cleanup:
4435
    if (rv < 0)
4436
        virNetMessageSaveError(rerr);
4437
    virObjectUnref(dom);
4438 4439 4440
    return rv;
}

4441 4442 4443 4444 4445 4446

/* 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.  */
4447
static int
J
Ján Tomko 已提交
4448
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED,
4449
                                            virNetServerClientPtr client,
J
Ján Tomko 已提交
4450 4451
                                            virNetMessagePtr msg G_GNUC_UNUSED,
                                            virNetMessageErrorPtr rerr G_GNUC_UNUSED,
4452
                                            remote_connect_domain_event_register_any_args *args)
4453 4454
{
    int callbackID;
4455
    int rv = -1;
4456 4457
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
4458 4459
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4460
    virConnectPtr conn = remoteGetHypervisorConn(client);
4461

4462 4463
    virMutexLock(&priv->lock);

4464
    if (!conn)
4465
        goto cleanup;
4466

4467 4468 4469 4470
    /* 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 ||
4471
        args->eventID < 0) {
4472 4473
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
4474
        goto cleanup;
4475 4476
    }

4477 4478 4479 4480 4481 4482 4483 4484
    /* 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;
4485
    callback->client = virObjectRef(client);
4486
    callback->program = virObjectRef(remoteProgram);
4487 4488
    callback->eventID = args->eventID;
    callback->callbackID = -1;
4489
    callback->legacy = true;
4490 4491 4492 4493
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
4494
        goto cleanup;
4495

4496
    if ((callbackID = virConnectDomainEventRegisterAny(conn,
4497 4498 4499
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
4500 4501 4502 4503 4504
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
4505
        goto cleanup;
4506
    }
4507

4508
    ref->callbackID = callbackID;
4509

4510 4511
    rv = 0;

4512
 cleanup:
4513
    virMutexUnlock(&priv->lock);
4514
    remoteEventCallbackFree(callback);
4515
    if (rv < 0)
4516
        virNetMessageSaveError(rerr);
4517
    return rv;
4518 4519 4520
}


4521
static int
J
Ján Tomko 已提交
4522
remoteDispatchConnectDomainEventCallbackRegisterAny(virNetServerPtr server G_GNUC_UNUSED,
4523
                                                    virNetServerClientPtr client,
J
Ján Tomko 已提交
4524 4525
                                                    virNetMessagePtr msg G_GNUC_UNUSED,
                                                    virNetMessageErrorPtr rerr G_GNUC_UNUSED,
4526 4527 4528 4529 4530 4531 4532 4533 4534 4535
                                                    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;
4536
    virConnectPtr conn = remoteGetHypervisorConn(client);
4537

4538 4539
    virMutexLock(&priv->lock);

4540
    if (!conn)
4541 4542 4543
        goto cleanup;

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

4547
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST || args->eventID < 0) {
4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560
        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;
4561
    callback->client = virObjectRef(client);
4562
    callback->program = virObjectRef(remoteProgram);
4563 4564 4565 4566 4567 4568 4569 4570
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
        goto cleanup;

4571
    if ((callbackID = virConnectDomainEventRegisterAny(conn,
4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587
                                                       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;

4588
 cleanup:
4589
    virMutexUnlock(&priv->lock);
4590
    remoteEventCallbackFree(callback);
4591 4592
    if (rv < 0)
        virNetMessageSaveError(rerr);
4593
    virObjectUnref(dom);
4594 4595 4596 4597
    return rv;
}


4598
static int
J
Ján Tomko 已提交
4599
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server G_GNUC_UNUSED,
4600
                                              virNetServerClientPtr client,
J
Ján Tomko 已提交
4601 4602
                                              virNetMessagePtr msg G_GNUC_UNUSED,
                                              virNetMessageErrorPtr rerr G_GNUC_UNUSED,
4603
                                              remote_connect_domain_event_deregister_any_args *args)
4604 4605
{
    int callbackID = -1;
4606
    int rv = -1;
4607
    size_t i;
4608 4609
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4610
    virConnectPtr conn = remoteGetHypervisorConn(client);
4611

4612 4613
    virMutexLock(&priv->lock);

4614
    if (!conn)
4615
        goto cleanup;
4616

4617 4618 4619 4620
    /* 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 ||
4621
        args->eventID < 0) {
4622 4623
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
4624
        goto cleanup;
4625 4626
    }

4627 4628 4629 4630 4631 4632
    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->eventID == args->eventID) {
            callbackID = priv->domainEventCallbacks[i]->callbackID;
            break;
        }
    }
4633
    if (callbackID < 0) {
4634 4635
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event %d not registered"), args->eventID);
4636
        goto cleanup;
4637 4638
    }

4639
    if (virConnectDomainEventDeregisterAny(conn, callbackID) < 0)
4640
        goto cleanup;
4641

4642 4643
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
4644

4645 4646
    rv = 0;

4647
 cleanup:
4648
    virMutexUnlock(&priv->lock);
4649
    if (rv < 0)
4650
        virNetMessageSaveError(rerr);
4651
    return rv;
4652 4653
}

4654 4655

static int
J
Ján Tomko 已提交
4656
remoteDispatchConnectDomainEventCallbackDeregisterAny(virNetServerPtr server G_GNUC_UNUSED,
4657
                                                      virNetServerClientPtr client,
J
Ján Tomko 已提交
4658 4659
                                                      virNetMessagePtr msg G_GNUC_UNUSED,
                                                      virNetMessageErrorPtr rerr G_GNUC_UNUSED,
4660 4661 4662 4663 4664 4665
                                                      remote_connect_domain_event_callback_deregister_any_args *args)
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4666
    virConnectPtr conn = remoteGetHypervisorConn(client);
4667

4668 4669
    virMutexLock(&priv->lock);

4670
    if (!conn)
4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683
        goto cleanup;

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

4684
    if (virConnectDomainEventDeregisterAny(conn, args->callbackID) < 0)
4685 4686 4687 4688 4689 4690 4691
        goto cleanup;

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

    rv = 0;

4692
 cleanup:
4693
    virMutexUnlock(&priv->lock);
4694 4695 4696 4697 4698 4699
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


C
Chris Lalancette 已提交
4700
static int
J
Ján Tomko 已提交
4701
qemuDispatchDomainMonitorCommand(virNetServerPtr server G_GNUC_UNUSED,
4702
                                 virNetServerClientPtr client,
J
Ján Tomko 已提交
4703
                                 virNetMessagePtr msg G_GNUC_UNUSED,
4704 4705 4706
                                 virNetMessageErrorPtr rerr,
                                 qemu_domain_monitor_command_args *args,
                                 qemu_domain_monitor_command_ret *ret)
C
Chris Lalancette 已提交
4707
{
4708
    virDomainPtr dom = NULL;
4709
    int rv = -1;
4710
    virConnectPtr conn = remoteGetHypervisorConn(client);
C
Chris Lalancette 已提交
4711

4712
    if (!conn)
4713
        goto cleanup;
4714

4715
    if (!(dom = get_nonnull_domain(conn, args->dom)))
4716
        goto cleanup;
C
Chris Lalancette 已提交
4717

4718
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
4719
                                    args->flags) < 0)
4720
        goto cleanup;
C
Chris Lalancette 已提交
4721

4722
    rv = 0;
C
Chris Lalancette 已提交
4723

4724
 cleanup:
4725
    if (rv < 0)
4726
        virNetMessageSaveError(rerr);
4727
    virObjectUnref(dom);
4728
    return rv;
C
Chris Lalancette 已提交
4729 4730
}

4731

4732
static int
J
Ján Tomko 已提交
4733
remoteDispatchDomainMigrateBegin3(virNetServerPtr server G_GNUC_UNUSED,
4734
                                  virNetServerClientPtr client,
J
Ján Tomko 已提交
4735
                                  virNetMessagePtr msg G_GNUC_UNUSED,
4736
                                  virNetMessageErrorPtr rerr,
4737 4738 4739 4740 4741 4742
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
4743
    char *xmlin;
4744 4745 4746
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4747
    virConnectPtr conn = remoteGetHypervisorConn(client);
4748

4749
    if (!conn)
4750 4751
        goto cleanup;

4752
    if (!(dom = get_nonnull_domain(conn, args->dom)))
4753 4754
        goto cleanup;

4755
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4756 4757
    dname = args->dname == NULL ? NULL : *args->dname;

4758
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771
                                       &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;

4772
 cleanup:
4773
    if (rv < 0)
4774
        virNetMessageSaveError(rerr);
4775
    virObjectUnref(dom);
4776 4777 4778 4779 4780
    return rv;
}


static int
J
Ján Tomko 已提交
4781
remoteDispatchDomainMigratePrepare3(virNetServerPtr server G_GNUC_UNUSED,
4782
                                    virNetServerClientPtr client,
J
Ján Tomko 已提交
4783
                                    virNetMessagePtr msg G_GNUC_UNUSED,
4784
                                    virNetMessageErrorPtr rerr,
4785 4786 4787 4788 4789 4790 4791 4792 4793
                                    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;
4794
    virConnectPtr conn = remoteGetHypervisorConn(client);
4795

4796
    if (!conn)
4797 4798 4799 4800 4801 4802
        goto cleanup;

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

    /* Wacky world of XDR ... */
4803
    if (VIR_ALLOC(uri_out) < 0)
4804 4805
        goto cleanup;

4806
    if (virDomainMigratePrepare3(conn,
4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823
                                 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;

4824
 cleanup:
4825
    if (rv < 0) {
4826
        virNetMessageSaveError(rerr);
4827 4828 4829 4830 4831
        VIR_FREE(uri_out);
    }
    return rv;
}

4832

4833
static int
J
Ján Tomko 已提交
4834
remoteDispatchDomainMigratePerform3(virNetServerPtr server G_GNUC_UNUSED,
4835
                                    virNetServerClientPtr client,
J
Ján Tomko 已提交
4836
                                    virNetMessagePtr msg G_GNUC_UNUSED,
4837
                                    virNetMessageErrorPtr rerr,
4838 4839 4840 4841
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
4842
    char *xmlin;
4843
    char *dname;
4844 4845
    char *uri;
    char *dconnuri;
4846 4847 4848
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4849
    virConnectPtr conn = remoteGetHypervisorConn(client);
4850

4851
    if (!conn)
4852 4853
        goto cleanup;

4854
    if (!(dom = get_nonnull_domain(conn, args->dom)))
4855 4856
        goto cleanup;

4857
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4858
    dname = args->dname == NULL ? NULL : *args->dname;
4859 4860
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
4861

4862
    if (virDomainMigratePerform3(dom, xmlin,
4863 4864 4865
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
4866
                                 dconnuri, uri,
4867 4868 4869 4870 4871 4872 4873 4874 4875 4876
                                 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;

4877
 cleanup:
4878
    if (rv < 0)
4879
        virNetMessageSaveError(rerr);
4880
    virObjectUnref(dom);
4881 4882 4883 4884 4885
    return rv;
}


static int
J
Ján Tomko 已提交
4886
remoteDispatchDomainMigrateFinish3(virNetServerPtr server G_GNUC_UNUSED,
4887
                                   virNetServerClientPtr client,
J
Ján Tomko 已提交
4888
                                   virNetMessagePtr msg G_GNUC_UNUSED,
4889
                                   virNetMessageErrorPtr rerr,
4890 4891 4892 4893 4894 4895
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
4896 4897
    char *uri;
    char *dconnuri;
4898
    int rv = -1;
4899
    virConnectPtr conn = remoteGetHypervisorConn(client);
4900

4901
    if (!conn)
4902 4903
        goto cleanup;

4904 4905 4906
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

4907
    if (!(dom = virDomainMigrateFinish3(conn, args->dname,
4908 4909 4910 4911 4912 4913
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
4914 4915
        goto cleanup;

4916
    make_nonnull_domain(&ret->dom, dom);
4917 4918 4919 4920 4921 4922 4923 4924

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

    rv = 0;

4925
 cleanup:
4926
    if (rv < 0) {
4927
        virNetMessageSaveError(rerr);
4928 4929
        VIR_FREE(cookieout);
    }
4930
    virObjectUnref(dom);
4931 4932 4933 4934 4935
    return rv;
}


static int
J
Ján Tomko 已提交
4936
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server G_GNUC_UNUSED,
4937
                                    virNetServerClientPtr client,
J
Ján Tomko 已提交
4938
                                    virNetMessagePtr msg G_GNUC_UNUSED,
4939 4940
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
4941 4942 4943
{
    virDomainPtr dom = NULL;
    int rv = -1;
4944
    virConnectPtr conn = remoteGetHypervisorConn(client);
4945

4946
    if (!conn)
4947 4948
        goto cleanup;

4949
    if (!(dom = get_nonnull_domain(conn, args->dom)))
4950 4951 4952 4953 4954 4955 4956 4957 4958 4959
        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;

4960
 cleanup:
4961
    if (rv < 0)
4962
        virNetMessageSaveError(rerr);
4963
    virObjectUnref(dom);
4964 4965 4966 4967
    return rv;
}


J
Ján Tomko 已提交
4968
static int remoteDispatchConnectSupportsFeature(virNetServerPtr server G_GNUC_UNUSED,
4969
                                                virNetServerClientPtr client,
J
Ján Tomko 已提交
4970
                                                virNetMessagePtr msg G_GNUC_UNUSED,
4971 4972 4973
                                                virNetMessageErrorPtr rerr,
                                                remote_connect_supports_feature_args *args,
                                                remote_connect_supports_feature_ret *ret)
4974 4975
{
    int rv = -1;
4976
    int supported = -1;
4977
    virConnectPtr conn = NULL;
4978

4979 4980 4981 4982 4983 4984 4985 4986 4987 4988
    /* 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;
    }

4989 4990 4991
    conn = remoteGetHypervisorConn(client);

    if (!conn)
4992 4993
        goto cleanup;

4994
    switch ((virDrvFeature) args->feature) {
4995
    case VIR_DRV_FEATURE_FD_PASSING:
4996
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
4997
    case VIR_DRV_FEATURE_REMOTE_CLOSE_CALLBACK:
4998 4999
        supported = 1;
        break;
5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010
    case VIR_DRV_FEATURE_MIGRATION_V1:
    case VIR_DRV_FEATURE_REMOTE:
    case VIR_DRV_FEATURE_MIGRATION_V2:
    case VIR_DRV_FEATURE_MIGRATION_P2P:
    case VIR_DRV_FEATURE_MIGRATION_DIRECT:
    case VIR_DRV_FEATURE_MIGRATION_V3:
    case VIR_DRV_FEATURE_MIGRATE_CHANGE_PROTECTION:
    case VIR_DRV_FEATURE_TYPED_PARAM_STRING:
    case VIR_DRV_FEATURE_XML_MIGRATABLE:
    case VIR_DRV_FEATURE_MIGRATION_OFFLINE:
    case VIR_DRV_FEATURE_MIGRATION_PARAMS:
5011
    default:
5012
        if ((supported = virConnectSupportsFeature(conn, args->feature)) < 0)
5013 5014
            goto cleanup;
        break;
5015 5016 5017
    case VIR_DRV_FEATURE_PROGRAM_KEEPALIVE:
        /* should not be possible! */
        goto cleanup;
5018 5019
    }

5020
 done:
5021 5022 5023
    ret->supported = supported;
    rv = 0;

5024
 cleanup:
5025 5026 5027 5028 5029 5030
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


5031
static int
J
Ján Tomko 已提交
5032
remoteDispatchDomainOpenGraphics(virNetServerPtr server G_GNUC_UNUSED,
5033
                                 virNetServerClientPtr client,
5034 5035 5036 5037 5038 5039 5040
                                 virNetMessagePtr msg,
                                 virNetMessageErrorPtr rerr,
                                 remote_domain_open_graphics_args *args)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    int fd = -1;
5041
    virConnectPtr conn = remoteGetHypervisorConn(client);
5042

5043
    if (!conn)
5044 5045
        goto cleanup;

5046
    if (!(dom = get_nonnull_domain(conn, args->dom)))
5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059
        goto cleanup;

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

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

    rv = 0;

5060
 cleanup:
5061 5062 5063
    VIR_FORCE_CLOSE(fd);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5064
    virObjectUnref(dom);
5065 5066 5067
    return rv;
}

5068

5069
static int
J
Ján Tomko 已提交
5070
remoteDispatchDomainOpenGraphicsFd(virNetServerPtr server G_GNUC_UNUSED,
5071
                                   virNetServerClientPtr client,
5072 5073 5074 5075 5076 5077 5078
                                   virNetMessagePtr msg,
                                   virNetMessageErrorPtr rerr,
                                   remote_domain_open_graphics_fd_args *args)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    int fd = -1;
5079
    virConnectPtr conn = remoteGetHypervisorConn(client);
5080

5081
    if (!conn)
5082 5083
        goto cleanup;

5084
    if (!(dom = get_nonnull_domain(conn, args->dom)))
5085 5086
        goto cleanup;

5087 5088 5089
    if ((fd = virDomainOpenGraphicsFD(dom,
                                      args->idx,
                                      args->flags)) < 0)
5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100
        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);
5101
    if (rv < 0)
5102 5103
        virNetMessageSaveError(rerr);

5104
    virObjectUnref(dom);
5105 5106
    return rv;
}
5107 5108


5109
static int
J
Ján Tomko 已提交
5110
remoteDispatchDomainGetInterfaceParameters(virNetServerPtr server G_GNUC_UNUSED,
5111
                                           virNetServerClientPtr client,
J
Ján Tomko 已提交
5112
                                           virNetMessagePtr msg G_GNUC_UNUSED,
5113 5114 5115 5116 5117 5118 5119
                                           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;
5120
    int nparams = 0;
5121 5122
    unsigned int flags;
    int rv = -1;
5123
    virConnectPtr conn = remoteGetHypervisorConn(client);
5124

5125
    if (!conn)
5126 5127 5128 5129
        goto cleanup;

    flags = args->flags;

5130
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
5131
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
5132 5133
        goto cleanup;
    }
5134
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
5135
        goto cleanup;
5136
    nparams = args->nparams;
5137

5138
    if (!(dom = get_nonnull_domain(conn, args->dom)))
5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151
        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;
    }

5152
    if (virTypedParamsSerialize(params, nparams,
5153
                                REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX,
5154 5155 5156
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                flags) < 0)
5157 5158
        goto cleanup;

5159
 success:
5160 5161
    rv = 0;

5162
 cleanup:
5163 5164
    if (rv < 0)
        virNetMessageSaveError(rerr);
5165
    virTypedParamsFree(params, nparams);
5166
    virObjectUnref(dom);
5167 5168
    return rv;
}
5169

5170
static int
J
Ján Tomko 已提交
5171
remoteDispatchDomainGetCPUStats(virNetServerPtr server G_GNUC_UNUSED,
5172
                                virNetServerClientPtr client,
J
Ján Tomko 已提交
5173
                                virNetMessagePtr hdr G_GNUC_UNUSED,
5174 5175 5176 5177 5178 5179 5180 5181
                                virNetMessageErrorPtr rerr,
                                remote_domain_get_cpu_stats_args *args,
                                remote_domain_get_cpu_stats_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int rv = -1;
    int percpu_len = 0;
5182
    virConnectPtr conn = remoteGetHypervisorConn(client);
5183

5184
    if (!conn)
5185 5186 5187
        goto cleanup;

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
5188
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
5189 5190 5191
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
5192
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
5193 5194 5195 5196
        goto cleanup;
    }

    if (args->nparams > 0 &&
5197
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
5198 5199
        goto cleanup;

5200
    if (!(dom = get_nonnull_domain(conn, args->dom)))
5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211
        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;

5212
    if (virTypedParamsSerialize(params, args->nparams * args->ncpus,
5213
                                REMOTE_DOMAIN_GET_CPU_STATS_MAX,
5214 5215 5216
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
5217 5218
        goto cleanup;

5219
 success:
5220 5221
    rv = 0;
    ret->nparams = percpu_len;
5222
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
5223
        size_t i;
5224 5225 5226 5227 5228 5229

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

5231
 cleanup:
5232 5233
    if (rv < 0)
         virNetMessageSaveError(rerr);
5234
    virTypedParamsFree(params, args->ncpus * args->nparams);
5235
    virObjectUnref(dom);
5236 5237 5238
    return rv;
}

5239
static int
J
Ján Tomko 已提交
5240
remoteDispatchDomainGetDiskErrors(virNetServerPtr server G_GNUC_UNUSED,
5241
                                  virNetServerClientPtr client,
J
Ján Tomko 已提交
5242
                                  virNetMessagePtr msg G_GNUC_UNUSED,
5243 5244 5245
                                  virNetMessageErrorPtr rerr,
                                  remote_domain_get_disk_errors_args *args,
                                  remote_domain_get_disk_errors_ret *ret)
5246 5247 5248 5249
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
5250
    int len = 0;
5251
    virConnectPtr conn = remoteGetHypervisorConn(client);
5252

5253
    if (!conn)
5254 5255
        goto cleanup;

5256
    if (!(dom = get_nonnull_domain(conn, args->dom)))
5257 5258 5259
        goto cleanup;

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
5260 5261
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
5262 5263 5264 5265
        goto cleanup;
    }

    if (args->maxerrors &&
5266
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282
        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;

5283
 cleanup:
5284 5285
    if (rv < 0)
        virNetMessageSaveError(rerr);
5286
    virObjectUnref(dom);
5287
    if (errors && len > 0) {
5288
        size_t i;
5289 5290 5291 5292 5293 5294 5295
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

5296

5297
static int
J
Ján Tomko 已提交
5298
remoteDispatchNodeGetSevInfo(virNetServerPtr server G_GNUC_UNUSED,
5299
                             virNetServerClientPtr client,
J
Ján Tomko 已提交
5300
                             virNetMessagePtr msg G_GNUC_UNUSED,
5301 5302 5303 5304 5305 5306 5307
                             virNetMessageErrorPtr rerr,
                             remote_node_get_sev_info_args *args,
                             remote_node_get_sev_info_ret *ret)
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    int rv = -1;
5308
    virConnectPtr conn = remoteGetHypervisorConn(client);
5309

5310
    if (!conn)
5311 5312
        goto cleanup;

5313
    if (virNodeGetSEVInfo(conn, &params, &nparams, args->flags) < 0)
5314 5315 5316
        goto cleanup;

    if (virTypedParamsSerialize(params, nparams,
5317
                                REMOTE_NODE_SEV_INFO_MAX,
5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
        goto cleanup;

    rv = 0;

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


5333
static int
J
Ján Tomko 已提交
5334
remoteDispatchNodeGetMemoryParameters(virNetServerPtr server G_GNUC_UNUSED,
5335
                                      virNetServerClientPtr client,
J
Ján Tomko 已提交
5336
                                      virNetMessagePtr msg G_GNUC_UNUSED,
5337 5338 5339 5340 5341
                                      virNetMessageErrorPtr rerr,
                                      remote_node_get_memory_parameters_args *args,
                                      remote_node_get_memory_parameters_ret *ret)
{
    virTypedParameterPtr params = NULL;
5342
    int nparams = 0;
5343 5344
    unsigned int flags;
    int rv = -1;
5345
    virConnectPtr conn = remoteGetHypervisorConn(client);
5346

5347
    if (!conn)
5348 5349 5350 5351
        goto cleanup;

    flags = args->flags;

5352
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
5353 5354 5355
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
5356
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
5357
        goto cleanup;
5358
    nparams = args->nparams;
5359

5360
    if (virNodeGetMemoryParameters(conn, params, &nparams, flags) < 0)
5361 5362 5363 5364 5365 5366 5367 5368 5369 5370
        goto cleanup;

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

5371
    if (virTypedParamsSerialize(params, nparams,
5372
                                REMOTE_NODE_MEMORY_PARAMETERS_MAX,
5373 5374 5375
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
5376 5377
        goto cleanup;

5378
 success:
5379 5380
    rv = 0;

5381
 cleanup:
5382 5383
    if (rv < 0)
        virNetMessageSaveError(rerr);
5384
    virTypedParamsFree(params, nparams);
5385 5386 5387
    return rv;
}

5388
static int
J
Ján Tomko 已提交
5389
remoteDispatchNodeGetCPUMap(virNetServerPtr server G_GNUC_UNUSED,
5390
                            virNetServerClientPtr client,
J
Ján Tomko 已提交
5391
                            virNetMessagePtr msg G_GNUC_UNUSED,
5392 5393 5394 5395 5396
                            virNetMessageErrorPtr rerr,
                            remote_node_get_cpu_map_args *args,
                            remote_node_get_cpu_map_ret *ret)
{
    unsigned char *cpumap = NULL;
5397
    unsigned int online = 0;
5398 5399 5400
    unsigned int flags;
    int cpunum;
    int rv = -1;
5401
    virConnectPtr conn = remoteGetHypervisorConn(client);
5402

5403
    if (!conn)
5404 5405 5406 5407
        goto cleanup;

    flags = args->flags;

5408
    cpunum = virNodeGetCPUMap(conn, args->need_map ? &cpumap : NULL,
5409
                              args->need_online ? &online : NULL, flags);
5410 5411 5412 5413
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
5414
    if (args->need_map) {
5415 5416 5417 5418 5419 5420 5421 5422 5423 5424
        ret->cpumap.cpumap_len = VIR_CPU_MAPLEN(cpunum);
        ret->cpumap.cpumap_val = (char *) cpumap;
        cpumap = NULL;
    }

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

    rv = 0;

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

5432
static int
J
Ján Tomko 已提交
5433
lxcDispatchDomainOpenNamespace(virNetServerPtr server G_GNUC_UNUSED,
5434
                               virNetServerClientPtr client,
J
Ján Tomko 已提交
5435
                               virNetMessagePtr msg G_GNUC_UNUSED,
5436 5437 5438 5439 5440 5441 5442 5443
                               virNetMessageErrorPtr rerr,
                               lxc_domain_open_namespace_args *args)
{
    int rv = -1;
    int *fdlist = NULL;
    int ret;
    virDomainPtr dom = NULL;
    size_t i;
5444
    virConnectPtr conn = remoteGetHypervisorConn(client);
5445

5446
    if (!conn)
5447 5448
        goto cleanup;

5449
    if (!(dom = get_nonnull_domain(conn, args->dom)))
5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461
        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
     */
5462
    for (i = 0; i < msg->nfds; i++)
5463 5464 5465 5466 5467 5468 5469 5470 5471
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

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

    rv = 1;

5472
 cleanup:
5473 5474
    if (rv < 0)
        virNetMessageSaveError(rerr);
5475
    virObjectUnref(dom);
5476 5477 5478
    return rv;
}

5479
static int
J
Ján Tomko 已提交
5480
remoteDispatchDomainGetJobStats(virNetServerPtr server G_GNUC_UNUSED,
5481
                                virNetServerClientPtr client,
J
Ján Tomko 已提交
5482
                                virNetMessagePtr msg G_GNUC_UNUSED,
5483 5484 5485 5486 5487 5488 5489 5490
                                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;
5491
    virConnectPtr conn = remoteGetHypervisorConn(client);
5492

5493
    if (!conn)
5494 5495
        goto cleanup;

5496
    if (!(dom = get_nonnull_domain(conn, args->dom)))
5497 5498 5499 5500 5501 5502
        goto cleanup;

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

5503
    if (virTypedParamsSerialize(params, nparams,
5504
                                REMOTE_DOMAIN_JOB_STATS_MAX,
5505 5506 5507
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                0) < 0)
5508 5509 5510 5511
        goto cleanup;

    rv = 0;

5512
 cleanup:
5513 5514 5515
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virTypedParamsFree(params, nparams);
5516
    virObjectUnref(dom);
5517 5518 5519
    return rv;
}

5520
static int
J
Ján Tomko 已提交
5521
remoteDispatchDomainMigrateBegin3Params(virNetServerPtr server G_GNUC_UNUSED,
5522
                                        virNetServerClientPtr client,
J
Ján Tomko 已提交
5523
                                        virNetMessagePtr msg G_GNUC_UNUSED,
5524 5525 5526
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_migrate_begin3_params_args *args,
                                        remote_domain_migrate_begin3_params_ret *ret)
5527 5528 5529 5530 5531 5532 5533 5534
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
5535
    virConnectPtr conn = remoteGetHypervisorConn(client);
5536

5537
    if (!conn)
5538 5539
        goto cleanup;

5540 5541 5542 5543 5544 5545 5546
    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;
    }

5547
    if (!(dom = get_nonnull_domain(conn, args->dom)))
5548 5549
        goto cleanup;

5550 5551 5552
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565
        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;

5566
 cleanup:
5567 5568 5569
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5570
    virObjectUnref(dom);
5571 5572 5573 5574
    return rv;
}

static int
J
Ján Tomko 已提交
5575
remoteDispatchDomainMigratePrepare3Params(virNetServerPtr server G_GNUC_UNUSED,
5576
                                          virNetServerClientPtr client,
J
Ján Tomko 已提交
5577
                                          virNetMessagePtr msg G_GNUC_UNUSED,
5578 5579 5580
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_prepare3_params_args *args,
                                          remote_domain_migrate_prepare3_params_ret *ret)
5581 5582 5583 5584 5585 5586 5587
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    char **uri_out;
    int rv = -1;
5588
    virConnectPtr conn = remoteGetHypervisorConn(client);
5589

5590
    if (!conn)
5591 5592
        goto cleanup;

5593 5594 5595 5596 5597 5598 5599
    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;
    }

5600 5601 5602
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5603 5604 5605
        goto cleanup;

    /* Wacky world of XDR ... */
5606
    if (VIR_ALLOC(uri_out) < 0)
5607 5608
        goto cleanup;

5609
    if (virDomainMigratePrepare3Params(conn, params, nparams,
5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621
                                       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;

5622
 cleanup:
5623 5624 5625 5626 5627 5628 5629 5630 5631
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(uri_out);
    }
    return rv;
}

static int
J
Ján Tomko 已提交
5632
remoteDispatchDomainMigratePrepareTunnel3Params(virNetServerPtr server G_GNUC_UNUSED,
5633 5634 5635 5636 5637
                                                virNetServerClientPtr client,
                                                virNetMessagePtr msg,
                                                virNetMessageErrorPtr rerr,
                                                remote_domain_migrate_prepare_tunnel3_params_args *args,
                                                remote_domain_migrate_prepare_tunnel3_params_ret *ret)
5638 5639 5640 5641 5642 5643 5644 5645
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
    virStreamPtr st = NULL;
    daemonClientStreamPtr stream = NULL;
5646
    virConnectPtr conn = remoteGetHypervisorConn(client);
5647

5648
    if (!conn)
5649 5650
        goto cleanup;

5651 5652 5653 5654 5655 5656 5657
    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;
    }

5658 5659 5660
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5661 5662
        goto cleanup;

5663
    if (!(st = virStreamNew(conn, VIR_STREAM_NONBLOCK)) ||
5664
        !(stream = daemonCreateClientStream(client, st, remoteProgram,
5665
                                            &msg->header, false)))
5666 5667
        goto cleanup;

5668
    if (virDomainMigratePrepareTunnel3Params(conn, st, params, nparams,
5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681
                                             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;

5682
 cleanup:
5683 5684 5685 5686 5687 5688 5689 5690
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
        if (stream) {
            virStreamAbort(st);
            daemonFreeClientStream(client, stream);
        } else {
5691
            virObjectUnref(st);
5692 5693 5694 5695 5696 5697 5698
        }
    }
    return rv;
}


static int
J
Ján Tomko 已提交
5699
remoteDispatchDomainMigratePerform3Params(virNetServerPtr server G_GNUC_UNUSED,
5700
                                          virNetServerClientPtr client,
J
Ján Tomko 已提交
5701
                                          virNetMessagePtr msg G_GNUC_UNUSED,
5702 5703 5704
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_perform3_params_args *args,
                                          remote_domain_migrate_perform3_params_ret *ret)
5705 5706 5707 5708 5709 5710 5711 5712
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    char *dconnuri;
    int rv = -1;
5713
    virConnectPtr conn = remoteGetHypervisorConn(client);
5714

5715
    if (!conn)
5716 5717
        goto cleanup;

5718 5719 5720 5721 5722 5723 5724
    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;
    }

5725
    if (!(dom = get_nonnull_domain(conn, args->dom)))
5726 5727
        goto cleanup;

5728 5729 5730
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746
        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;

5747
 cleanup:
5748 5749 5750
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5751
    virObjectUnref(dom);
5752 5753 5754 5755 5756
    return rv;
}


static int
J
Ján Tomko 已提交
5757
remoteDispatchDomainMigrateFinish3Params(virNetServerPtr server G_GNUC_UNUSED,
5758
                                         virNetServerClientPtr client,
J
Ján Tomko 已提交
5759
                                         virNetMessagePtr msg G_GNUC_UNUSED,
5760 5761 5762
                                         virNetMessageErrorPtr rerr,
                                         remote_domain_migrate_finish3_params_args *args,
                                         remote_domain_migrate_finish3_params_ret *ret)
5763 5764 5765 5766 5767 5768 5769
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
5770
    virConnectPtr conn = remoteGetHypervisorConn(client);
5771

5772
    if (!conn)
5773 5774
        goto cleanup;

5775 5776 5777 5778 5779 5780 5781
    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;
    }

5782 5783 5784
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5785 5786
        goto cleanup;

5787
    dom = virDomainMigrateFinish3Params(conn, params, nparams,
5788 5789 5790 5791 5792 5793 5794
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        args->flags, args->cancelled);
    if (!dom)
        goto cleanup;

5795
    make_nonnull_domain(&ret->dom, dom);
5796 5797 5798 5799 5800 5801

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

    rv = 0;

5802
 cleanup:
5803 5804 5805 5806 5807
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
    }
5808
    virObjectUnref(dom);
5809 5810 5811 5812 5813
    return rv;
}


static int
J
Ján Tomko 已提交
5814
remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server G_GNUC_UNUSED,
5815
                                          virNetServerClientPtr client,
J
Ján Tomko 已提交
5816
                                          virNetMessagePtr msg G_GNUC_UNUSED,
5817 5818
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_confirm3_params_args *args)
5819 5820 5821 5822 5823
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    int rv = -1;
5824
    virConnectPtr conn = remoteGetHypervisorConn(client);
5825

5826
    if (!conn)
5827 5828
        goto cleanup;

5829 5830 5831 5832 5833 5834 5835
    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;
    }

5836
    if (!(dom = get_nonnull_domain(conn, args->dom)))
5837 5838
        goto cleanup;

5839 5840 5841
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5842 5843 5844 5845 5846 5847 5848 5849 5850 5851
        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;

5852
 cleanup:
5853 5854 5855
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5856
    virObjectUnref(dom);
5857 5858 5859 5860
    return rv;
}


5861
static int
J
Ján Tomko 已提交
5862
remoteDispatchConnectGetCPUModelNames(virNetServerPtr server G_GNUC_UNUSED,
5863
                                      virNetServerClientPtr client,
J
Ján Tomko 已提交
5864
                                      virNetMessagePtr msg G_GNUC_UNUSED,
5865 5866 5867 5868 5869 5870
                                      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;
5871
    virConnectPtr conn = remoteGetHypervisorConn(client);
5872

5873
    if (!conn)
5874 5875
        goto cleanup;

5876
    len = virConnectGetCPUModelNames(conn, args->arch,
5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901
                                     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;

5902
 cleanup:
5903 5904
    if (rv < 0)
        virNetMessageSaveError(rerr);
5905
    virStringListFree(models);
5906 5907 5908 5909
    return rv;
}


5910
static int
J
Ján Tomko 已提交
5911
remoteDispatchDomainCreateXMLWithFiles(virNetServerPtr server G_GNUC_UNUSED,
5912
                                       virNetServerClientPtr client,
J
Ján Tomko 已提交
5913
                                       virNetMessagePtr msg G_GNUC_UNUSED,
5914 5915 5916
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_create_xml_with_files_args *args,
                                       remote_domain_create_xml_with_files_ret *ret)
5917 5918 5919 5920 5921 5922
{
    int rv = -1;
    virDomainPtr dom = NULL;
    int *files = NULL;
    unsigned int nfiles = 0;
    size_t i;
5923
    virConnectPtr conn = remoteGetHypervisorConn(client);
5924

5925
    if (!conn)
5926 5927 5928 5929 5930 5931 5932 5933 5934 5935
        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++;
    }

5936
    if ((dom = virDomainCreateXMLWithFiles(conn, args->xml_desc,
5937 5938 5939 5940
                                           nfiles, files,
                                           args->flags)) == NULL)
        goto cleanup;

5941
    make_nonnull_domain(&ret->dom, dom);
5942

5943 5944
    rv = 0;

5945
 cleanup:
5946
    for (i = 0; i < nfiles; i++)
5947 5948 5949 5950
        VIR_FORCE_CLOSE(files[i]);
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5951
    virObjectUnref(dom);
5952 5953 5954 5955
    return rv;
}


J
Ján Tomko 已提交
5956
static int remoteDispatchDomainCreateWithFiles(virNetServerPtr server G_GNUC_UNUSED,
5957
                                               virNetServerClientPtr client,
J
Ján Tomko 已提交
5958
                                               virNetMessagePtr msg G_GNUC_UNUSED,
5959 5960 5961
                                               virNetMessageErrorPtr rerr,
                                               remote_domain_create_with_files_args *args,
                                               remote_domain_create_with_files_ret *ret)
5962 5963 5964 5965 5966 5967
{
    int rv = -1;
    virDomainPtr dom = NULL;
    int *files = NULL;
    unsigned int nfiles = 0;
    size_t i;
5968
    virConnectPtr conn = remoteGetHypervisorConn(client);
5969

5970
    if (!conn)
5971 5972 5973 5974 5975 5976 5977 5978 5979 5980
        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++;
    }

5981
    if (!(dom = get_nonnull_domain(conn, args->dom)))
5982 5983 5984 5985 5986 5987 5988
        goto cleanup;

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

5989
    make_nonnull_domain(&ret->dom, dom);
5990

5991 5992
    rv = 0;

5993
 cleanup:
5994
    for (i = 0; i < nfiles; i++)
5995 5996 5997 5998
        VIR_FORCE_CLOSE(files[i]);
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5999
    virObjectUnref(dom);
6000 6001 6002 6003
    return rv;
}


6004
static int
J
Ján Tomko 已提交
6005
remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED,
6006
                                             virNetServerClientPtr client,
J
Ján Tomko 已提交
6007 6008
                                             virNetMessagePtr msg G_GNUC_UNUSED,
                                             virNetMessageErrorPtr rerr G_GNUC_UNUSED,
6009
                                             remote_connect_network_event_register_any_args *args,
6010
                                             remote_connect_network_event_register_any_ret *ret)
6011 6012 6013
{
    int callbackID;
    int rv = -1;
6014 6015
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
6016
    virNetworkPtr net = NULL;
6017 6018
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
6019
    virConnectPtr conn = remoteGetNetworkConn(client);
6020

6021 6022
    virMutexLock(&priv->lock);

6023
    if (!conn)
6024 6025
        goto cleanup;

6026
    if (args->net &&
6027
        !(net = get_nonnull_network(conn, *args->net)))
6028 6029
        goto cleanup;

6030 6031 6032
    if (args->eventID >= VIR_NETWORK_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network event ID %d"), args->eventID);
6033 6034 6035
        goto cleanup;
    }

6036 6037 6038 6039 6040 6041 6042 6043
    /* 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;
6044
    callback->client = virObjectRef(client);
6045
    callback->program = virObjectRef(remoteProgram);
6046 6047 6048 6049 6050 6051
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->networkEventCallbacks,
                           priv->nnetworkEventCallbacks,
                           callback) < 0)
6052 6053
        goto cleanup;

6054
    if ((callbackID = virConnectNetworkEventRegisterAny(conn,
6055
                                                        net,
6056 6057
                                                        args->eventID,
                                                        networkEventCallbacks[args->eventID],
6058 6059 6060 6061 6062
                                                        ref,
                                                        remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->networkEventCallbacks,
                     priv->nnetworkEventCallbacks, 1);
        callback = ref;
6063
        goto cleanup;
6064
    }
6065

6066
    ref->callbackID = callbackID;
6067
    ret->callbackID = callbackID;
6068 6069 6070

    rv = 0;

6071
 cleanup:
6072
    virMutexUnlock(&priv->lock);
6073
    remoteEventCallbackFree(callback);
6074 6075
    if (rv < 0)
        virNetMessageSaveError(rerr);
6076
    virObjectUnref(net);
6077 6078 6079 6080 6081
    return rv;
}


static int
J
Ján Tomko 已提交
6082
remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server G_GNUC_UNUSED,
6083
                                               virNetServerClientPtr client,
J
Ján Tomko 已提交
6084 6085
                                               virNetMessagePtr msg G_GNUC_UNUSED,
                                               virNetMessageErrorPtr rerr G_GNUC_UNUSED,
6086
                                               remote_connect_network_event_deregister_any_args *args)
6087 6088
{
    int rv = -1;
6089
    size_t i;
6090 6091
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
6092
    virConnectPtr conn = remoteGetNetworkConn(client);
6093

6094 6095
    virMutexLock(&priv->lock);

6096
    if (!conn)
6097 6098
        goto cleanup;

6099
    for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
6100
        if (priv->networkEventCallbacks[i]->callbackID == args->callbackID)
6101 6102
            break;
    }
6103
    if (i == priv->nnetworkEventCallbacks) {
6104
        virReportError(VIR_ERR_INTERNAL_ERROR,
6105 6106
                       _("network event callback %d not registered"),
                       args->callbackID);
6107 6108 6109
        goto cleanup;
    }

6110
    if (virConnectNetworkEventDeregisterAny(conn, args->callbackID) < 0)
6111 6112
        goto cleanup;

6113 6114
    VIR_DELETE_ELEMENT(priv->networkEventCallbacks, i,
                       priv->nnetworkEventCallbacks);
6115 6116 6117

    rv = 0;

6118
 cleanup:
6119
    virMutexUnlock(&priv->lock);
6120 6121 6122 6123 6124
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}

6125
static int
J
Ján Tomko 已提交
6126
remoteDispatchConnectStoragePoolEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED,
6127
                                                 virNetServerClientPtr client,
J
Ján Tomko 已提交
6128 6129
                                                 virNetMessagePtr msg G_GNUC_UNUSED,
                                                 virNetMessageErrorPtr rerr G_GNUC_UNUSED,
6130 6131 6132 6133 6134 6135 6136 6137 6138 6139
                                                 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;
6140
    virConnectPtr conn = remoteGetStorageConn(client);
6141

6142 6143
    virMutexLock(&priv->lock);

6144
    if (!conn)
6145 6146 6147
        goto cleanup;

    if (args->pool &&
6148
        !(pool = get_nonnull_storage_pool(conn, *args->pool)))
6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164
        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;
6165
    callback->client = virObjectRef(client);
6166
    callback->program = virObjectRef(remoteProgram);
6167 6168 6169 6170 6171 6172 6173 6174
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->storageEventCallbacks,
                           priv->nstorageEventCallbacks,
                           callback) < 0)
        goto cleanup;

6175
    if ((callbackID = virConnectStoragePoolEventRegisterAny(conn,
6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192
                                                            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:
6193
    virMutexUnlock(&priv->lock);
6194
    remoteEventCallbackFree(callback);
6195 6196 6197 6198 6199 6200 6201
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virObjectUnref(pool);
    return rv;
}

static int
J
Ján Tomko 已提交
6202
remoteDispatchConnectStoragePoolEventDeregisterAny(virNetServerPtr server G_GNUC_UNUSED,
6203
                                               virNetServerClientPtr client,
J
Ján Tomko 已提交
6204 6205
                                               virNetMessagePtr msg G_GNUC_UNUSED,
                                               virNetMessageErrorPtr rerr G_GNUC_UNUSED,
6206 6207 6208 6209 6210 6211
                                               remote_connect_storage_pool_event_deregister_any_args *args)
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
6212
    virConnectPtr conn = remoteGetStorageConn(client);
6213

6214 6215
    virMutexLock(&priv->lock);

6216
    if (!conn)
6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229
        goto cleanup;

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

6230
    if (virConnectStoragePoolEventDeregisterAny(conn, args->callbackID) < 0)
6231 6232 6233 6234 6235 6236 6237 6238
        goto cleanup;

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

    rv = 0;

 cleanup:
6239
    virMutexUnlock(&priv->lock);
6240 6241 6242 6243 6244
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}

6245
static int
J
Ján Tomko 已提交
6246
remoteDispatchConnectNodeDeviceEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED,
6247
                                                virNetServerClientPtr client,
J
Ján Tomko 已提交
6248 6249
                                                virNetMessagePtr msg G_GNUC_UNUSED,
                                                virNetMessageErrorPtr rerr G_GNUC_UNUSED,
6250 6251 6252 6253 6254 6255 6256 6257 6258 6259
                                                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;
6260
    virConnectPtr conn = remoteGetNodeDevConn(client);
6261

6262 6263
    virMutexLock(&priv->lock);

6264
    if (!conn)
6265 6266 6267
        goto cleanup;

    if (args->dev &&
6268
        !(dev = get_nonnull_node_device(conn, *args->dev)))
6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284
        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;
6285
    callback->client = virObjectRef(client);
6286
    callback->program = virObjectRef(remoteProgram);
6287 6288 6289 6290 6291 6292 6293 6294
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->nodeDeviceEventCallbacks,
                           priv->nnodeDeviceEventCallbacks,
                           callback) < 0)
        goto cleanup;

6295
    if ((callbackID = virConnectNodeDeviceEventRegisterAny(conn,
6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312
                                                           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:
6313
    virMutexUnlock(&priv->lock);
6314
    remoteEventCallbackFree(callback);
6315 6316 6317 6318 6319 6320 6321
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virObjectUnref(dev);
    return rv;
}

static int
J
Ján Tomko 已提交
6322
remoteDispatchConnectNodeDeviceEventDeregisterAny(virNetServerPtr server G_GNUC_UNUSED,
6323
                                                  virNetServerClientPtr client,
J
Ján Tomko 已提交
6324 6325
                                                  virNetMessagePtr msg G_GNUC_UNUSED,
                                                  virNetMessageErrorPtr rerr G_GNUC_UNUSED,
6326 6327 6328 6329 6330 6331
                                                  remote_connect_node_device_event_deregister_any_args *args)
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
6332
    virConnectPtr conn = remoteGetNodeDevConn(client);
6333

6334 6335
    virMutexLock(&priv->lock);

6336
    if (!conn)
6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349
        goto cleanup;

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

6350
    if (virConnectNodeDeviceEventDeregisterAny(conn, args->callbackID) < 0)
6351 6352 6353 6354 6355 6356 6357 6358
        goto cleanup;

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

    rv = 0;

 cleanup:
6359
    virMutexUnlock(&priv->lock);
6360 6361 6362 6363
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}
6364

6365
static int
J
Ján Tomko 已提交
6366
remoteDispatchConnectSecretEventRegisterAny(virNetServerPtr server G_GNUC_UNUSED,
6367
                                            virNetServerClientPtr client,
J
Ján Tomko 已提交
6368 6369
                                            virNetMessagePtr msg G_GNUC_UNUSED,
                                            virNetMessageErrorPtr rerr G_GNUC_UNUSED,
6370 6371 6372 6373 6374 6375 6376 6377 6378 6379
                                            remote_connect_secret_event_register_any_args *args,
                                            remote_connect_secret_event_register_any_ret *ret)
{
    int callbackID;
    int rv = -1;
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virSecretPtr secret = NULL;
6380
    virConnectPtr conn = remoteGetSecretConn(client);
6381

6382 6383
    virMutexLock(&priv->lock);

6384
    if (!conn)
6385 6386 6387
        goto cleanup;

    if (args->secret &&
6388
        !(secret = get_nonnull_secret(conn, *args->secret)))
6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404
        goto cleanup;

    if (args->eventID >= VIR_SECRET_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported secret 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;
6405
    callback->client = virObjectRef(client);
6406
    callback->program = virObjectRef(remoteProgram);
6407 6408 6409 6410 6411 6412 6413 6414
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->secretEventCallbacks,
                           priv->nsecretEventCallbacks,
                           callback) < 0)
        goto cleanup;

6415
    if ((callbackID = virConnectSecretEventRegisterAny(conn,
6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432
                                                       secret,
                                                       args->eventID,
                                                       secretEventCallbacks[args->eventID],
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->secretEventCallbacks,
                     priv->nsecretEventCallbacks, 1);
        callback = ref;
        goto cleanup;
    }

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

    rv = 0;

 cleanup:
6433
    virMutexUnlock(&priv->lock);
6434
    remoteEventCallbackFree(callback);
6435 6436 6437 6438 6439 6440 6441
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virObjectUnref(secret);
    return rv;
}

static int
J
Ján Tomko 已提交
6442
remoteDispatchConnectSecretEventDeregisterAny(virNetServerPtr server G_GNUC_UNUSED,
6443
                                                  virNetServerClientPtr client,
J
Ján Tomko 已提交
6444 6445
                                                  virNetMessagePtr msg G_GNUC_UNUSED,
                                                  virNetMessageErrorPtr rerr G_GNUC_UNUSED,
6446 6447 6448 6449 6450 6451
                                                  remote_connect_secret_event_deregister_any_args *args)
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
6452
    virConnectPtr conn = remoteGetSecretConn(client);
6453

6454 6455
    virMutexLock(&priv->lock);

6456
    if (!conn)
6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469
        goto cleanup;

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

6470
    if (virConnectSecretEventDeregisterAny(conn, args->callbackID) < 0)
6471 6472 6473 6474 6475 6476 6477 6478
        goto cleanup;

    VIR_DELETE_ELEMENT(priv->secretEventCallbacks, i,
                       priv->nsecretEventCallbacks);

    rv = 0;

 cleanup:
6479
    virMutexUnlock(&priv->lock);
6480 6481 6482 6483 6484
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}

6485
static int
J
Ján Tomko 已提交
6486
qemuDispatchConnectDomainMonitorEventRegister(virNetServerPtr server G_GNUC_UNUSED,
6487
                                              virNetServerClientPtr client,
J
Ján Tomko 已提交
6488 6489
                                              virNetMessagePtr msg G_GNUC_UNUSED,
                                              virNetMessageErrorPtr rerr G_GNUC_UNUSED,
6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500
                                              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;
6501
    virConnectPtr conn = remoteGetHypervisorConn(client);
6502

6503 6504
    virMutexLock(&priv->lock);

6505
    if (!conn)
6506 6507 6508
        goto cleanup;

    if (args->dom &&
6509
        !(dom = get_nonnull_domain(conn, *args->dom)))
6510 6511 6512 6513 6514 6515 6516 6517 6518 6519
        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;
6520
    callback->client = virObjectRef(client);
6521
    callback->program = virObjectRef(qemuProgram);
6522
    callback->eventID = -1;
6523 6524 6525 6526 6527 6528 6529
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->qemuEventCallbacks,
                           priv->nqemuEventCallbacks,
                           callback) < 0)
        goto cleanup;

6530
    if ((callbackID = virConnectDomainQemuMonitorEventRegister(conn,
6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547
                                                               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;

6548
 cleanup:
6549
    virMutexUnlock(&priv->lock);
6550
    remoteEventCallbackFree(callback);
6551 6552
    if (rv < 0)
        virNetMessageSaveError(rerr);
6553
    virObjectUnref(dom);
6554 6555 6556 6557 6558
    return rv;
}


static int
J
Ján Tomko 已提交
6559
qemuDispatchConnectDomainMonitorEventDeregister(virNetServerPtr server G_GNUC_UNUSED,
6560
                                                virNetServerClientPtr client,
J
Ján Tomko 已提交
6561 6562
                                                virNetMessagePtr msg G_GNUC_UNUSED,
                                                virNetMessageErrorPtr rerr G_GNUC_UNUSED,
6563 6564 6565 6566 6567 6568
                                                qemu_connect_domain_monitor_event_deregister_args *args)
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
6569
    virConnectPtr conn = remoteGetHypervisorConn(client);
6570

6571 6572
    virMutexLock(&priv->lock);

6573
    if (!conn)
6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586
        goto cleanup;

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

6587
    if (virConnectDomainQemuMonitorEventDeregister(conn,
6588 6589 6590 6591 6592 6593 6594 6595
                                                   args->callbackID) < 0)
        goto cleanup;

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

    rv = 0;

6596
 cleanup:
6597
    virMutexUnlock(&priv->lock);
6598 6599 6600 6601 6602
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}

6603
static int
J
Ján Tomko 已提交
6604
remoteDispatchDomainGetTime(virNetServerPtr server G_GNUC_UNUSED,
6605
                            virNetServerClientPtr client,
J
Ján Tomko 已提交
6606
                            virNetMessagePtr msg G_GNUC_UNUSED,
6607 6608 6609 6610 6611 6612 6613 6614
                            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;
6615
    virConnectPtr conn = remoteGetHypervisorConn(client);
6616

6617
    if (!conn)
6618 6619
        goto cleanup;

6620
    if (!(dom = get_nonnull_domain(conn, args->dom)))
6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632
        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);
6633
    virObjectUnref(dom);
6634 6635
    return rv;
}
6636

M
Michal Privoznik 已提交
6637 6638

static int
J
Ján Tomko 已提交
6639
remoteDispatchNodeGetFreePages(virNetServerPtr server G_GNUC_UNUSED,
M
Michal Privoznik 已提交
6640
                               virNetServerClientPtr client,
J
Ján Tomko 已提交
6641
                               virNetMessagePtr msg G_GNUC_UNUSED,
M
Michal Privoznik 已提交
6642 6643 6644 6645 6646 6647
                               virNetMessageErrorPtr rerr,
                               remote_node_get_free_pages_args *args,
                               remote_node_get_free_pages_ret *ret)
{
    int rv = -1;
    int len;
6648
    virConnectPtr conn = remoteGetHypervisorConn(client);
M
Michal Privoznik 已提交
6649

6650
    if (!conn)
M
Michal Privoznik 已提交
6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663
        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;

6664
    if ((len = virNodeGetFreePages(conn,
M
Michal Privoznik 已提交
6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681
                                   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;
6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696
}

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

6697 6698
    lease_dst->iface = g_strdup(lease_src->iface);
    lease_dst->ipaddr = g_strdup(lease_src->ipaddr);
6699

J
Ján Tomko 已提交
6700
    if (lease_src->mac) {
6701
        if (VIR_ALLOC(mac_tmp) < 0)
J
Ján Tomko 已提交
6702
            goto error;
6703
        *mac_tmp = g_strdup(lease_src->mac);
J
Ján Tomko 已提交
6704 6705
    }
    if (lease_src->iaid) {
6706
        if (VIR_ALLOC(iaid_tmp) < 0)
J
Ján Tomko 已提交
6707
            goto error;
6708
        *iaid_tmp = g_strdup(lease_src->iaid);
J
Ján Tomko 已提交
6709 6710
    }
    if (lease_src->hostname) {
6711
        if (VIR_ALLOC(hostname_tmp) < 0)
J
Ján Tomko 已提交
6712
            goto error;
6713
        *hostname_tmp = g_strdup(lease_src->hostname);
J
Ján Tomko 已提交
6714 6715
    }
    if (lease_src->clientid) {
6716
        if (VIR_ALLOC(clientid_tmp) < 0)
J
Ján Tomko 已提交
6717
            goto error;
6718
        *clientid_tmp = g_strdup(lease_src->clientid);
J
Ján Tomko 已提交
6719 6720 6721 6722 6723 6724
    }

    lease_dst->mac = mac_tmp;
    lease_dst->iaid = iaid_tmp;
    lease_dst->hostname = hostname_tmp;
    lease_dst->clientid = clientid_tmp;
6725 6726 6727 6728

    return 0;

 error:
J
Ján Tomko 已提交
6729 6730 6731 6732 6733 6734 6735 6736
    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);
6737 6738 6739 6740 6741
    VIR_FREE(mac_tmp);
    VIR_FREE(iaid_tmp);
    VIR_FREE(hostname_tmp);
    VIR_FREE(clientid_tmp);
    VIR_FREE(lease_dst->ipaddr);
6742
    VIR_FREE(lease_dst->iface);
6743 6744 6745 6746 6747
    return -1;
}


static int
J
Ján Tomko 已提交
6748
remoteDispatchNetworkGetDHCPLeases(virNetServerPtr server G_GNUC_UNUSED,
6749
                                   virNetServerClientPtr client,
J
Ján Tomko 已提交
6750
                                   virNetMessagePtr msg G_GNUC_UNUSED,
6751 6752 6753 6754 6755 6756 6757 6758 6759
                                   virNetMessageErrorPtr rerr,
                                   remote_network_get_dhcp_leases_args *args,
                                   remote_network_get_dhcp_leases_ret *ret)
{
    int rv = -1;
    size_t i;
    virNetworkDHCPLeasePtr *leases = NULL;
    virNetworkPtr net = NULL;
    int nleases = 0;
6760
    virConnectPtr conn = remoteGetNetworkConn(client);
6761

6762
    if (!conn)
6763 6764
        goto cleanup;

6765
    if (!(net = get_nonnull_network(conn, args->net)))
6766 6767 6768
        goto cleanup;

    if ((nleases = virNetworkGetDHCPLeases(net,
6769
                                           args->mac ? *args->mac : NULL,
6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803
                                           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);
6804
    if (leases && nleases > 0)
6805 6806
        for (i = 0; i < nleases; i++)
            virNetworkDHCPLeaseFree(leases[i]);
6807
    VIR_FREE(leases);
6808
    virObjectUnref(net);
6809
    return rv;
M
Michal Privoznik 已提交
6810 6811 6812
}


6813
static int
J
Ján Tomko 已提交
6814
remoteDispatchConnectGetAllDomainStats(virNetServerPtr server G_GNUC_UNUSED,
6815
                                       virNetServerClientPtr client,
J
Ján Tomko 已提交
6816
                                       virNetMessagePtr msg G_GNUC_UNUSED,
6817 6818 6819 6820 6821 6822 6823 6824 6825
                                       virNetMessageErrorPtr rerr,
                                       remote_connect_get_all_domain_stats_args *args,
                                       remote_connect_get_all_domain_stats_ret *ret)
{
    int rv = -1;
    size_t i;
    virDomainStatsRecordPtr *retStats = NULL;
    int nrecords = 0;
    virDomainPtr *doms = NULL;
6826
    virConnectPtr conn = remoteGetHypervisorConn(client);
6827

6828
    if (!conn)
6829 6830 6831 6832 6833 6834 6835
        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++) {
6836
            if (!(doms[i] = get_nonnull_domain(conn, args->doms.doms_val[i])))
6837 6838 6839 6840 6841 6842 6843 6844 6845
                goto cleanup;
        }

        if ((nrecords = virDomainListGetStats(doms,
                                              args->stats,
                                              &retStats,
                                              args->flags)) < 0)
            goto cleanup;
    } else {
6846
        if ((nrecords = virConnectGetAllDomainStats(conn,
6847 6848 6849 6850 6851 6852 6853
                                                    args->stats,
                                                    &retStats,
                                                    args->flags)) < 0)
            goto cleanup;
    }

    if (nrecords) {
6854 6855 6856 6857 6858 6859 6860 6861
        if (nrecords > REMOTE_DOMAIN_LIST_MAX) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Number of domain stats records is %d, "
                             "which exceeds max limit: %d"),
                           nrecords, REMOTE_DOMAIN_LIST_MAX);
            goto cleanup;
        }

6862 6863 6864 6865 6866 6867 6868 6869
        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;

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

6872 6873
            if (virTypedParamsSerialize(retStats[i]->params,
                                        retStats[i]->nparams,
6874
                                        REMOTE_CONNECT_GET_ALL_DOMAIN_STATS_MAX,
6875 6876 6877
                                        (virTypedParameterRemotePtr *) &dst->params.params_val,
                                        &dst->params.params_len,
                                        VIR_TYPED_PARAM_STRING_OKAY) < 0)
6878 6879 6880 6881 6882 6883 6884 6885 6886 6887
                goto cleanup;
        }
    } else {
        ret->retStats.retStats_len = 0;
        ret->retStats.retStats_val = NULL;
    }

    rv = 0;

 cleanup:
6888
    if (rv < 0) {
6889
        virNetMessageSaveError(rerr);
6890 6891 6892
        xdr_free((xdrproc_t)xdr_remote_connect_get_all_domain_stats_ret,
                 (char *) ret);
    }
6893 6894

    virDomainStatsRecordListFree(retStats);
6895
    virObjectListFree(doms);
6896 6897 6898 6899 6900

    return rv;
}


M
Michal Privoznik 已提交
6901
static int
J
Ján Tomko 已提交
6902
remoteDispatchNodeAllocPages(virNetServerPtr server G_GNUC_UNUSED,
M
Michal Privoznik 已提交
6903
                             virNetServerClientPtr client,
J
Ján Tomko 已提交
6904
                             virNetMessagePtr msg G_GNUC_UNUSED,
M
Michal Privoznik 已提交
6905 6906 6907 6908 6909 6910
                             virNetMessageErrorPtr rerr,
                             remote_node_alloc_pages_args *args,
                             remote_node_alloc_pages_ret *ret)
{
    int rv = -1;
    int len;
6911
    virConnectPtr conn = remoteGetHypervisorConn(client);
M
Michal Privoznik 已提交
6912

6913
    if (!conn)
M
Michal Privoznik 已提交
6914 6915
        goto cleanup;

6916
    if ((len = virNodeAllocPages(conn,
M
Michal Privoznik 已提交
6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934
                                 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;
}


6935
static int
J
Ján Tomko 已提交
6936
remoteDispatchDomainGetFSInfo(virNetServerPtr server G_GNUC_UNUSED,
6937
                              virNetServerClientPtr client,
J
Ján Tomko 已提交
6938
                              virNetMessagePtr msg G_GNUC_UNUSED,
6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949
                              virNetMessageErrorPtr rerr,
                              remote_domain_get_fsinfo_args *args,
                              remote_domain_get_fsinfo_ret *ret)
{
    int rv = -1;
    size_t i, j;
    virDomainFSInfoPtr *info = NULL;
    virDomainPtr dom = NULL;
    remote_domain_fsinfo *dst;
    int ninfo = 0;
    size_t ndisk;
6950
    virConnectPtr conn = remoteGetHypervisorConn(client);
6951

6952
    if (!conn)
6953 6954
        goto cleanup;

6955
    if (!(dom = get_nonnull_domain(conn, args->dom)))
6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975
        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];
6976
            dst->mountpoint = g_strdup(info[i]->mountpoint);
6977

6978
            dst->name = g_strdup(info[i]->name);
6979

6980
            dst->fstype = g_strdup(info[i]->fstype);
6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993

            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;

6994 6995
                for (j = 0; j < ndisk; j++)
                    dst->dev_aliases.dev_aliases_val[j] = g_strdup(info[i]->devAlias[j]);
6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029

                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);
        }
    }
7030
    virObjectUnref(dom);
7031 7032 7033
    if (ninfo >= 0)
        for (i = 0; i < ninfo; i++)
            virDomainFSInfoFree(info[i]);
7034
    g_free(info);
7035 7036 7037 7038 7039

    return rv;
}


7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062
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]);

7063
        iface_ret->name = g_strdup(iface->name);
7064

7065 7066 7067 7068 7069
        if (iface->hwaddr) {
            if (VIR_ALLOC(iface_ret->hwaddr) < 0)
                goto cleanup;
            *iface_ret->hwaddr = g_strdup(iface->hwaddr);
        }
7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088

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

7089
            ip_addr_ret->addr = g_strdup(ip_addr->addr);
7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102

            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);
7103 7104 7105 7106
            if (iface_ret->hwaddr) {
                VIR_FREE(*iface_ret->hwaddr);
                VIR_FREE(iface_ret->hwaddr);
            }
7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120
            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
J
Ján Tomko 已提交
7121
remoteDispatchDomainInterfaceAddresses(virNetServerPtr server G_GNUC_UNUSED,
7122
                                       virNetServerClientPtr client,
J
Ján Tomko 已提交
7123
                                       virNetMessagePtr msg G_GNUC_UNUSED,
7124 7125 7126 7127 7128 7129 7130 7131 7132
                                       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;
7133
    virConnectPtr conn = remoteGetHypervisorConn(client);
7134

7135
    if (!conn)
7136 7137
        goto cleanup;

7138
    if (!(dom = get_nonnull_domain(conn, args->dom)))
7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164
        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;
}


7165
static int
J
Ján Tomko 已提交
7166
remoteDispatchNetworkPortGetParameters(virNetServerPtr server G_GNUC_UNUSED,
7167
                                       virNetServerClientPtr client,
J
Ján Tomko 已提交
7168
                                       virNetMessagePtr msg G_GNUC_UNUSED,
7169 7170 7171 7172 7173 7174 7175 7176
                                       virNetMessageErrorPtr rerr,
                                       remote_network_port_get_parameters_args *args,
                                       remote_network_port_get_parameters_ret *ret)
{
    int rv = -1;
    virNetworkPortPtr port = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
7177
    virConnectPtr conn = remoteGetNetworkConn(client);
7178

7179
    if (!conn)
7180 7181
        goto cleanup;

7182
    if (!(port = get_nonnull_network_port(conn, args->port)))
7183 7184 7185 7186 7187 7188
        goto cleanup;

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

    if (virTypedParamsSerialize(params, nparams,
7189
                                REMOTE_NETWORK_PORT_PARAMETERS_MAX,
7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
        goto cleanup;

    rv = 0;

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


7206 7207 7208 7209 7210 7211 7212 7213 7214
/*----- 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
7215
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
7216
{
7217
    return virGetDomain(conn, domain.name, BAD_CAST domain.uuid, domain.id);
7218 7219 7220
}

static virNetworkPtr
7221
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
7222
{
7223
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
7224 7225
}

7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238
static virNetworkPortPtr
get_nonnull_network_port(virConnectPtr conn, remote_nonnull_network_port port)
{
    virNetworkPortPtr ret;
    virNetworkPtr net;
    net = virGetNetwork(conn, port.net.name, BAD_CAST port.net.uuid);
    if (!net)
        return NULL;
    ret = virGetNetworkPort(net, BAD_CAST port.uuid);
    virObjectUnref(net);
    return ret;
}

D
Daniel Veillard 已提交
7239
static virInterfacePtr
7240
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
7241
{
7242
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
7243 7244
}

7245
static virStoragePoolPtr
7246
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
7247
{
7248 7249
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
7250 7251 7252
}

static virStorageVolPtr
7253
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
7254
{
M
Michal Privoznik 已提交
7255
    return virGetStorageVol(conn, vol.pool, vol.name, vol.key, NULL, NULL);
7256 7257
}

7258
static virSecretPtr
7259
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
7260
{
7261
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
7262 7263
}

7264
static virNWFilterPtr
7265
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
7266
{
7267
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
7268 7269
}

7270 7271 7272 7273 7274 7275
static virNWFilterBindingPtr
get_nonnull_nwfilter_binding(virConnectPtr conn, remote_nonnull_nwfilter_binding binding)
{
    return virGetNWFilterBinding(conn, binding.portdev, binding.filtername);
}

7276 7277 7278 7279 7280 7281
static virDomainCheckpointPtr
get_nonnull_domain_checkpoint(virDomainPtr dom, remote_nonnull_domain_checkpoint checkpoint)
{
    return virGetDomainCheckpoint(dom, checkpoint.name);
}

C
Chris Lalancette 已提交
7282
static virDomainSnapshotPtr
7283
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
7284
{
7285
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
7286 7287
}

7288 7289 7290 7291 7292 7293
static virNodeDevicePtr
get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev)
{
    return virGetNodeDevice(conn, dev.name);
}

7294
static void
7295
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
7296 7297
{
    dom_dst->id = dom_src->id;
7298
    dom_dst->name = g_strdup(dom_src->name);
7299
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
7300 7301
}

7302
static void
7303
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
7304
{
7305
    net_dst->name = g_strdup(net_src->name);
7306
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
7307 7308
}

7309
static void
7310 7311
make_nonnull_network_port(remote_nonnull_network_port *port_dst, virNetworkPortPtr port_src)
{
7312
    port_dst->net.name = g_strdup(port_src->net->name);
7313 7314 7315 7316
    memcpy(port_dst->net.uuid, port_src->net->uuid, VIR_UUID_BUFLEN);
    memcpy(port_dst->uuid, port_src->uuid, VIR_UUID_BUFLEN);
}

7317
static void
7318 7319
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
7320
{
7321 7322
    interface_dst->name = g_strdup(interface_src->name);
    interface_dst->mac = g_strdup(interface_src->mac);
D
Daniel Veillard 已提交
7323 7324
}

7325
static void
7326
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
7327
{
7328
    pool_dst->name = g_strdup(pool_src->name);
7329
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
7330 7331
}

7332
static void
7333
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
7334
{
7335 7336 7337
    vol_dst->pool = g_strdup(vol_src->pool);
    vol_dst->name = g_strdup(vol_src->name);
    vol_dst->key = g_strdup(vol_src->key);
7338
}
7339

7340
static void
7341
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
7342
{
7343
    dev_dst->name = g_strdup(dev_src->name);
7344
}
7345

7346
static void
7347
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
7348
{
7349
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
7350
    secret_dst->usageType = secret_src->usageType;
7351
    secret_dst->usageID = g_strdup(secret_src->usageID);
7352
}
7353

7354
static void
7355
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
7356
{
7357
    nwfilter_dst->name = g_strdup(nwfilter_src->name);
7358
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
7359
}
C
Chris Lalancette 已提交
7360

7361
static void
7362 7363
make_nonnull_nwfilter_binding(remote_nonnull_nwfilter_binding *binding_dst, virNWFilterBindingPtr binding_src)
{
7364 7365
    binding_dst->portdev = g_strdup(binding_src->portdev);
    binding_dst->filtername = g_strdup(binding_src->filtername);
7366 7367
}

7368
static void
7369 7370
make_nonnull_domain_checkpoint(remote_nonnull_domain_checkpoint *checkpoint_dst, virDomainCheckpointPtr checkpoint_src)
{
7371
    checkpoint_dst->name = g_strdup(checkpoint_src->name);
7372
    make_nonnull_domain(&checkpoint_dst->dom, checkpoint_src->domain);
7373 7374
}

7375
static void
7376
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
7377
{
7378
    snapshot_dst->name = g_strdup(snapshot_src->name);
7379
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
7380
}
7381 7382 7383 7384 7385 7386 7387 7388

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;
7389
    size_t i = 0;
7390

7391
    if (VIR_ALLOC_N(val, nerrors) < 0)
7392
        goto error;
7393 7394

    for (i = 0; i < nerrors; i++) {
7395
        val[i].disk = g_strdup(errors[i].disk);
7396 7397 7398 7399 7400 7401 7402 7403
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

7404
 error:
7405
    if (val) {
7406
        size_t j;
7407 7408 7409 7410 7411 7412
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}
7413 7414

static int
J
Ján Tomko 已提交
7415
remoteDispatchDomainGetGuestInfo(virNetServerPtr server G_GNUC_UNUSED,
7416
                                 virNetServerClientPtr client,
J
Ján Tomko 已提交
7417
                                 virNetMessagePtr msg G_GNUC_UNUSED,
7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437
                                 virNetMessageErrorPtr rerr,
                                 remote_domain_get_guest_info_args *args,
                                 remote_domain_get_guest_info_ret *ret)
{
    int rv = -1;
    virConnectPtr conn = remoteGetHypervisorConn(client);
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;

    if (!conn)
        goto cleanup;

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

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

    if (virTypedParamsSerialize(params, nparams,
7438
                                REMOTE_DOMAIN_GUEST_INFO_PARAMS_MAX,
7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0)
        goto cleanup;

    rv = 0;

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

    return rv;
}