remote_daemon_dispatch.c 244.2 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 "intprops.h"
35
#include "virnetserverservice.h"
J
Jim Fehlig 已提交
36
#include "virnetserver.h"
37
#include "virfile.h"
38
#include "virtypedparam.h"
39
#include "virdbus.h"
40
#include "virprocess.h"
41 42
#include "remote_protocol.h"
#include "qemu_protocol.h"
43
#include "lxc_protocol.h"
44
#include "virstring.h"
45
#include "object_event.h"
46 47
#include "domain_conf.h"
#include "network_conf.h"
48
#include "virprobe.h"
49
#include "viraccessapicheck.h"
50
#include "viraccessapicheckqemu.h"
51
#include "virpolkit.h"
52
#include "virthreadjob.h"
53 54

#define VIR_FROM_THIS VIR_FROM_RPC
55

56 57
VIR_LOG_INIT("daemon.remote");

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

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

84 85
static virDomainPtr get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network(virConnectPtr conn, remote_nonnull_network network);
86
static virNetworkPortPtr get_nonnull_network_port(virConnectPtr conn, remote_nonnull_network_port port);
87 88 89 90 91
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);
92
static virNWFilterBindingPtr get_nonnull_nwfilter_binding(virConnectPtr conn, remote_nonnull_nwfilter_binding binding);
93
static virDomainCheckpointPtr get_nonnull_domain_checkpoint(virDomainPtr dom, remote_nonnull_domain_checkpoint checkpoint);
94
static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot);
95
static virNodeDevicePtr get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev);
96 97
static int make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src) ATTRIBUTE_RETURN_CHECK;
static int make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src) ATTRIBUTE_RETURN_CHECK;
98
static int make_nonnull_network_port(remote_nonnull_network_port *port_dst, virNetworkPortPtr port_src) ATTRIBUTE_RETURN_CHECK;
99 100 101 102 103 104 105
static int make_nonnull_interface(remote_nonnull_interface *interface_dst, virInterfacePtr interface_src) ATTRIBUTE_RETURN_CHECK;
static int make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src) ATTRIBUTE_RETURN_CHECK;
static int make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src) ATTRIBUTE_RETURN_CHECK;
static int make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src) ATTRIBUTE_RETURN_CHECK;
static int make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src) ATTRIBUTE_RETURN_CHECK;
static int make_nonnull_nwfilter(remote_nonnull_nwfilter *net_dst, virNWFilterPtr nwfilter_src) ATTRIBUTE_RETURN_CHECK;
static int make_nonnull_nwfilter_binding(remote_nonnull_nwfilter_binding *binding_dst, virNWFilterBindingPtr binding_src) ATTRIBUTE_RETURN_CHECK;
106
static int make_nonnull_domain_checkpoint(remote_nonnull_domain_checkpoint *checkpoint_dst, virDomainCheckpointPtr checkpoint_src) ATTRIBUTE_RETURN_CHECK;
107
static int make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src) ATTRIBUTE_RETURN_CHECK;
108

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

115 116 117
#include "remote_daemon_dispatch_stubs.h"
#include "remote_daemon_dispatch_qemu_stubs.h"
#include "remote_daemon_dispatch_lxc_stubs.h"
C
Chris Lalancette 已提交
118 119


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

128 129 130
static void
remoteEventCallbackFree(void *opaque)
{
131 132 133 134 135
    daemonClientEventCallbackPtr callback = opaque;
    if (!callback)
        return;
    virObjectUnref(callback->client);
    VIR_FREE(callback);
136 137
}

138 139 140 141 142 143 144 145 146 147 148 149

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

    /* For now, we just create a virDomainDef with enough contents to
     * satisfy what viraccessdriverpolkit.c references.  This is a bit
     * fragile, but I don't know of anything better.  */
150
    memset(&def, 0, sizeof(def));
151 152 153 154 155 156 157 158 159
    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);

160
 cleanup:
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    ignore_value(virIdentitySetCurrent(NULL));
    virObjectUnref(identity);
    return ret;
}


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

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

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

187
 cleanup:
188 189 190 191 192
    ignore_value(virIdentitySetCurrent(NULL));
    virObjectUnref(identity);
    return ret;
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
static bool
remoteRelayStoragePoolEventCheckACL(virNetServerClientPtr client,
                                    virConnectPtr conn,
                                    virStoragePoolPtr pool)
{
    virStoragePoolDef def;
    virIdentityPtr identity = NULL;
    bool ret = false;

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

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

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

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
static bool
remoteRelayNodeDeviceEventCheckACL(virNetServerClientPtr client,
                                   virConnectPtr conn,
                                   virNodeDevicePtr dev)
{
    virNodeDeviceDef def;
    virIdentityPtr identity = NULL;
    bool ret = false;

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

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

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

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
static bool
remoteRelaySecretEventCheckACL(virNetServerClientPtr client,
                               virConnectPtr conn,
                               virSecretPtr secret)
{
    virSecretDef def;
    virIdentityPtr identity = NULL;
    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));
    virObjectUnref(identity);
    return ret;
}

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
static bool
remoteRelayDomainQemuMonitorEventCheckACL(virNetServerClientPtr client,
                                          virConnectPtr conn, virDomainPtr dom)
{
    virDomainDef def;
    virIdentityPtr identity = NULL;
    bool ret = false;

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

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

294
 cleanup:
295 296 297 298 299 300
    ignore_value(virIdentitySetCurrent(NULL));
    virObjectUnref(identity);
    return ret;
}


301 302 303 304 305 306
static int
remoteRelayDomainEventLifecycle(virConnectPtr conn,
                                virDomainPtr dom,
                                int event,
                                int detail,
                                void *opaque)
307
{
308
    daemonClientEventCallbackPtr callback = opaque;
309
    remote_domain_event_lifecycle_msg data;
310

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

315 316
    VIR_DEBUG("Relaying domain lifecycle event %d %d, callback %d legacy %d",
              event, detail, callback->callbackID, callback->legacy);
317

318
    /* build return data */
319
    memset(&data, 0, sizeof(data));
320 321
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
322 323
    data.event = event;
    data.detail = detail;
324

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

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

340
    return 0;
341 342 343

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_lifecycle_msg,
344
             (char *) &data);
345
    return -1;
346
}
347

348 349 350 351
static int
remoteRelayDomainEventReboot(virConnectPtr conn,
                             virDomainPtr dom,
                             void *opaque)
352
{
353
    daemonClientEventCallbackPtr callback = opaque;
354 355
    remote_domain_event_reboot_msg data;

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

360 361
    VIR_DEBUG("Relaying domain reboot event %s %d, callback %d legacy %d",
              dom->name, dom->id, callback->callbackID, callback->legacy);
362 363

    /* build return data */
364
    memset(&data, 0, sizeof(data));
365 366
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
367

368 369 370 371 372 373 374 375 376 377 378 379
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_REBOOT,
                                      (xdrproc_t)xdr_remote_domain_event_reboot_msg, &data);
    } else {
        remote_domain_event_callback_reboot_msg msg = { callback->callbackID,
                                                        data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_REBOOT,
                                      (xdrproc_t)xdr_remote_domain_event_callback_reboot_msg, &msg);
    }
380 381

    return 0;
382 383 384

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_reboot_msg,
385
             (char *) &data);
386
    return -1;
387 388
}

389

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

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

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

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

413 414 415 416 417 418 419 420 421 422 423 424
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_rtc_change_msg, &data);
    } else {
        remote_domain_event_callback_rtc_change_msg msg = { callback->callbackID,
                                                            data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_RTC_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_rtc_change_msg, &msg);
    }
425 426

    return 0;
427 428 429

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_rtc_change_msg,
430
             (char *) &data);
431
    return -1;
432 433 434
}


435 436 437 438 439
static int
remoteRelayDomainEventWatchdog(virConnectPtr conn,
                               virDomainPtr dom,
                               int action,
                               void *opaque)
440
{
441
    daemonClientEventCallbackPtr callback = opaque;
442 443
    remote_domain_event_watchdog_msg data;

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

448 449
    VIR_DEBUG("Relaying domain watchdog event %s %d %d, callback %d",
              dom->name, dom->id, action, callback->callbackID);
450 451

    /* build return data */
452
    memset(&data, 0, sizeof(data));
453 454
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
455 456
    data.action = action;

457 458 459 460 461 462 463 464 465 466 467 468
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_WATCHDOG,
                                      (xdrproc_t)xdr_remote_domain_event_watchdog_msg, &data);
    } else {
        remote_domain_event_callback_watchdog_msg msg = { callback->callbackID,
                                                          data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_WATCHDOG,
                                      (xdrproc_t)xdr_remote_domain_event_callback_watchdog_msg, &msg);
    }
469 470

    return 0;
471 472 473

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_watchdog_msg,
474
             (char *) &data);
475
    return -1;
476 477 478
}


479 480 481 482 483 484 485
static int
remoteRelayDomainEventIOError(virConnectPtr conn,
                              virDomainPtr dom,
                              const char *srcPath,
                              const char *devAlias,
                              int action,
                              void *opaque)
486
{
487
    daemonClientEventCallbackPtr callback = opaque;
488 489
    remote_domain_event_io_error_msg data;

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

494 495 496
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d, callback %d",
              dom->name, dom->id, srcPath, devAlias, action,
              callback->callbackID);
497 498

    /* build return data */
499
    memset(&data, 0, sizeof(data));
500 501 502
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
503 504
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
505 506
    data.action = action;

507 508 509 510 511 512 513 514 515 516 517 518
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_IO_ERROR,
                                      (xdrproc_t)xdr_remote_domain_event_io_error_msg, &data);
    } else {
        remote_domain_event_callback_io_error_msg msg = { callback->callbackID,
                                                          data };

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

    return 0;
521
 error:
522
    xdr_free((xdrproc_t)xdr_remote_domain_event_io_error_msg,
523
             (char *) &data);
524
    return -1;
525 526 527
}


528 529 530 531 532 533 534 535
static int
remoteRelayDomainEventIOErrorReason(virConnectPtr conn,
                                    virDomainPtr dom,
                                    const char *srcPath,
                                    const char *devAlias,
                                    int action,
                                    const char *reason,
                                    void *opaque)
536
{
537
    daemonClientEventCallbackPtr callback = opaque;
538 539
    remote_domain_event_io_error_reason_msg data;

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

544 545 546
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s, callback %d",
              dom->name, dom->id, srcPath, devAlias, action, reason,
              callback->callbackID);
547 548

    /* build return data */
549
    memset(&data, 0, sizeof(data));
550 551 552 553
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0 ||
        VIR_STRDUP(data.reason, reason) < 0)
        goto error;
554
    data.action = action;
555

556 557
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
558

559 560 561 562 563 564 565 566 567 568 569 570
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON,
                                      (xdrproc_t)xdr_remote_domain_event_io_error_reason_msg, &data);
    } else {
        remote_domain_event_callback_io_error_reason_msg msg = { callback->callbackID,
                                                                 data };

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

    return 0;
573

574
 error:
575
    xdr_free((xdrproc_t)xdr_remote_domain_event_io_error_reason_msg,
576
             (char *) &data);
577
    return -1;
578 579 580
}


581 582 583 584 585 586 587 588 589
static int
remoteRelayDomainEventGraphics(virConnectPtr conn,
                               virDomainPtr dom,
                               int phase,
                               virDomainEventGraphicsAddressPtr local,
                               virDomainEventGraphicsAddressPtr remote,
                               const char *authScheme,
                               virDomainEventGraphicsSubjectPtr subject,
                               void *opaque)
590
{
591
    daemonClientEventCallbackPtr callback = opaque;
592
    remote_domain_event_graphics_msg data;
593
    size_t i;
594

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

599 600
    VIR_DEBUG("Relaying domain graphics event %s %d %d - %d %s %s  - %d %s %s - %s, callback %d",
              dom->name, dom->id, phase,
601 602
              local->family, local->service, local->node,
              remote->family, remote->service, remote->node,
603
              authScheme, callback->callbackID);
604

605
    VIR_DEBUG("Subject %d", subject->nidentity);
606
    for (i = 0; i < subject->nidentity; i++)
607
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
608 609

    /* build return data */
610
    memset(&data, 0, sizeof(data));
611 612 613
    data.phase = phase;
    data.local.family = local->family;
    data.remote.family = remote->family;
614 615 616 617 618 619
    if (VIR_STRDUP(data.authScheme, authScheme) < 0 ||
        VIR_STRDUP(data.local.node, local->node) < 0 ||
        VIR_STRDUP(data.local.service, local->service) < 0 ||
        VIR_STRDUP(data.remote.node, remote->node) < 0 ||
        VIR_STRDUP(data.remote.service, remote->service) < 0)
        goto error;
620 621

    data.subject.subject_len = subject->nidentity;
622
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0)
623
        goto error;
624

625
    for (i = 0; i < data.subject.subject_len; i++) {
626 627 628
        if (VIR_STRDUP(data.subject.subject_val[i].type, subject->identities[i].type) < 0 ||
            VIR_STRDUP(data.subject.subject_val[i].name, subject->identities[i].name) < 0)
            goto error;
629
    }
630 631
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
632

633 634 635 636 637 638 639 640 641 642 643 644
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
                                      (xdrproc_t)xdr_remote_domain_event_graphics_msg, &data);
    } else {
        remote_domain_event_callback_graphics_msg msg = { callback->callbackID,
                                                          data };

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

    return 0;
647

648
 error:
649
    xdr_free((xdrproc_t)xdr_remote_domain_event_graphics_msg,
650
             (char *) &data);
651
    return -1;
652 653
}

654 655 656 657 658 659 660
static int
remoteRelayDomainEventBlockJob(virConnectPtr conn,
                               virDomainPtr dom,
                               const char *path,
                               int type,
                               int status,
                               void *opaque)
661
{
662
    daemonClientEventCallbackPtr callback = opaque;
663 664
    remote_domain_event_block_job_msg data;

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

669 670
    VIR_DEBUG("Relaying domain block job event %s %d %s %i, %i, callback %d",
              dom->name, dom->id, path, type, status, callback->callbackID);
671 672

    /* build return data */
673
    memset(&data, 0, sizeof(data));
674
    if (VIR_STRDUP(data.path, path) < 0)
675
        return -1;
676 677
    data.type = type;
    data.status = status;
678 679
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
680

681 682 683 684 685 686 687 688 689 690 691 692
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB,
                                      (xdrproc_t)xdr_remote_domain_event_block_job_msg, &data);
    } else {
        remote_domain_event_callback_block_job_msg msg = { callback->callbackID,
                                                           data };

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

    return 0;
695 696 697

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_block_job_msg,
698
             (char *) &data);
699
    return -1;
700 701
}

702

703 704 705 706
static int
remoteRelayDomainEventControlError(virConnectPtr conn,
                                   virDomainPtr dom,
                                   void *opaque)
707
{
708
    daemonClientEventCallbackPtr callback = opaque;
709 710
    remote_domain_event_control_error_msg data;

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

715 716
    VIR_DEBUG("Relaying domain control error %s %d, callback %d",
              dom->name, dom->id, callback->callbackID);
717 718

    /* build return data */
719
    memset(&data, 0, sizeof(data));
720 721
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
722

723 724 725 726 727 728 729 730 731 732 733 734
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CONTROL_ERROR,
                                      (xdrproc_t)xdr_remote_domain_event_control_error_msg, &data);
    } else {
        remote_domain_event_callback_control_error_msg msg = { callback->callbackID,
                                                               data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_CONTROL_ERROR,
                                      (xdrproc_t)xdr_remote_domain_event_callback_control_error_msg, &msg);
    }
735 736

    return 0;
737 738 739

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_control_error_msg,
740
             (char *) &data);
741
    return -1;
742 743 744
}


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

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

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

    /* build return data */
766
    memset(&data, 0, sizeof(data));
767
    if (oldSrcPath &&
J
John Ferlan 已提交
768 769
        ((VIR_ALLOC(data.oldSrcPath) < 0) ||
         VIR_STRDUP(*(data.oldSrcPath), oldSrcPath) < 0))
770
        goto error;
771 772

    if (newSrcPath &&
J
John Ferlan 已提交
773 774
        ((VIR_ALLOC(data.newSrcPath) < 0) ||
         VIR_STRDUP(*(data.newSrcPath), newSrcPath) < 0))
775
        goto error;
776

777 778
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
779 780
    data.reason = reason;

781 782
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
783

784 785 786 787 788 789 790 791 792 793 794 795
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_DISK_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_disk_change_msg, &data);
    } else {
        remote_domain_event_callback_disk_change_msg msg = { callback->callbackID,
                                                             data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DISK_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_disk_change_msg, &msg);
    }
796 797 798

    return 0;

799
 error:
800
    xdr_free((xdrproc_t)xdr_remote_domain_event_disk_change_msg,
801
             (char *) &data);
802 803 804 805
    return -1;
}


806 807 808 809 810 811 812
static int
remoteRelayDomainEventTrayChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
{
813
    daemonClientEventCallbackPtr callback = opaque;
814 815
    remote_domain_event_tray_change_msg data;

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

820 821
    VIR_DEBUG("Relaying domain %s %d tray change devAlias: %s reason: %d, callback %d",
              dom->name, dom->id, devAlias, reason, callback->callbackID);
822 823

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

826
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
827 828 829
        return -1;
    data.reason = reason;

830 831
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
832

833 834 835 836 837 838 839 840 841 842 843 844
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_TRAY_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_tray_change_msg, &data);
    } else {
        remote_domain_event_callback_tray_change_msg msg = { callback->callbackID,
                                                             data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_TRAY_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_tray_change_msg, &msg);
    }
845 846

    return 0;
847 848 849

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_tray_change_msg,
850
             (char *) &data);
851
    return -1;
852 853
}

854 855 856
static int
remoteRelayDomainEventPMWakeup(virConnectPtr conn,
                               virDomainPtr dom,
E
Eric Blake 已提交
857
                               int reason,
858 859
                               void *opaque)
{
860
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
861 862
    remote_domain_event_pmwakeup_msg data;

863 864
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
865 866
        return -1;

867 868
    VIR_DEBUG("Relaying domain %s %d system pmwakeup, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
869 870

    /* build return data */
871
    memset(&data, 0, sizeof(data));
872 873
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
O
Osier Yang 已提交
874

875 876 877 878 879 880
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_PMWAKEUP,
                                      (xdrproc_t)xdr_remote_domain_event_pmwakeup_msg, &data);
    } else {
        remote_domain_event_callback_pmwakeup_msg msg = { callback->callbackID,
E
Eric Blake 已提交
881
                                                          reason, data };
882 883 884 885 886

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMWAKEUP,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmwakeup_msg, &msg);
    }
O
Osier Yang 已提交
887 888

    return 0;
889 890 891

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_pmwakeup_msg,
892
             (char *) &data);
893
    return -1;
O
Osier Yang 已提交
894 895
}

896 897 898
static int
remoteRelayDomainEventPMSuspend(virConnectPtr conn,
                                virDomainPtr dom,
E
Eric Blake 已提交
899
                                int reason,
900 901
                                void *opaque)
{
902
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
903 904
    remote_domain_event_pmsuspend_msg data;

905 906
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
907 908
        return -1;

909 910
    VIR_DEBUG("Relaying domain %s %d system pmsuspend, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
911 912

    /* build return data */
913
    memset(&data, 0, sizeof(data));
914 915
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
O
Osier Yang 已提交
916

917 918 919 920 921 922
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND,
                                      (xdrproc_t)xdr_remote_domain_event_pmsuspend_msg, &data);
    } else {
        remote_domain_event_callback_pmsuspend_msg msg = { callback->callbackID,
E
Eric Blake 已提交
923
                                                           reason, data };
924 925 926 927 928

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_msg, &msg);
    }
O
Osier Yang 已提交
929 930

    return 0;
931 932 933

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_pmsuspend_msg,
934
             (char *) &data);
935
    return -1;
O
Osier Yang 已提交
936 937
}

938
static int
939
remoteRelayDomainEventBalloonChange(virConnectPtr conn,
940 941 942 943
                                    virDomainPtr dom,
                                    unsigned long long actual,
                                    void *opaque)
{
944
    daemonClientEventCallbackPtr callback = opaque;
945 946
    remote_domain_event_balloon_change_msg data;

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

951 952
    VIR_DEBUG("Relaying domain balloon change event %s %d %lld, callback %d",
              dom->name, dom->id, actual, callback->callbackID);
953 954 955

    /* build return data */
    memset(&data, 0, sizeof(data));
956 957
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
958 959
    data.actual = actual;

960 961 962 963 964 965 966 967 968 969 970 971
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_balloon_change_msg, &data);
    } else {
        remote_domain_event_callback_balloon_change_msg msg = { callback->callbackID,
                                                                data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_BALLOON_CHANGE,
                                      (xdrproc_t)xdr_remote_domain_event_callback_balloon_change_msg, &msg);
    }
972 973

    return 0;
974 975 976

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_balloon_change_msg,
977
             (char *) &data);
978
    return -1;
979 980 981
}


982 983 984
static int
remoteRelayDomainEventPMSuspendDisk(virConnectPtr conn,
                                    virDomainPtr dom,
E
Eric Blake 已提交
985
                                    int reason,
986 987
                                    void *opaque)
{
988
    daemonClientEventCallbackPtr callback = opaque;
989 990
    remote_domain_event_pmsuspend_disk_msg data;

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

995 996
    VIR_DEBUG("Relaying domain %s %d system pmsuspend-disk, callback %d",
              dom->name, dom->id, callback->callbackID);
997 998 999

    /* build return data */
    memset(&data, 0, sizeof(data));
1000 1001
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
1002

1003 1004 1005 1006 1007 1008
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND_DISK,
                                      (xdrproc_t)xdr_remote_domain_event_pmsuspend_disk_msg, &data);
    } else {
        remote_domain_event_callback_pmsuspend_disk_msg msg = { callback->callbackID,
E
Eric Blake 已提交
1009
                                                                reason, data };
1010 1011 1012 1013 1014

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND_DISK,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_disk_msg, &msg);
    }
1015 1016

    return 0;
1017 1018 1019

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_pmsuspend_disk_msg,
1020
             (char *) &data);
1021
    return -1;
1022 1023
}

1024
static int
1025
remoteRelayDomainEventDeviceRemoved(virConnectPtr conn,
1026 1027 1028 1029
                                    virDomainPtr dom,
                                    const char *devAlias,
                                    void *opaque)
{
1030
    daemonClientEventCallbackPtr callback = opaque;
1031 1032
    remote_domain_event_device_removed_msg data;

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

1037 1038
    VIR_DEBUG("Relaying domain device removed event %s %d %s, callback %d",
              dom->name, dom->id, devAlias, callback->callbackID);
1039 1040 1041 1042 1043 1044 1045

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

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

1046 1047
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
1048

1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
    if (callback->legacy) {
        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_DEVICE_REMOVED,
                                      (xdrproc_t)xdr_remote_domain_event_device_removed_msg,
                                      &data);
    } else {
        remote_domain_event_callback_device_removed_msg msg = { callback->callbackID,
                                                                data };

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DEVICE_REMOVED,
                                      (xdrproc_t)xdr_remote_domain_event_callback_device_removed_msg,
                                      &msg);
    }
1063 1064

    return 0;
1065 1066 1067

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_device_removed_msg,
1068
             (char *) &data);
1069
    return -1;
1070 1071
}

1072

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
static int
remoteRelayDomainEventBlockJob2(virConnectPtr conn,
                                virDomainPtr dom,
                                const char *dst,
                                int type,
                                int status,
                                void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_block_job_2_msg data;

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

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

    /* build return data */
    memset(&data, 0, sizeof(data));
    data.callbackID = callback->callbackID;
    if (VIR_STRDUP(data.dst, dst) < 0)
1095
        return -1;
1096 1097
    data.type = type;
    data.status = status;
1098 1099
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
1100 1101 1102 1103 1104 1105

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

    return 0;
1106 1107 1108

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_block_job_2_msg,
1109
             (char *) &data);
1110
    return -1;
1111 1112 1113
}


1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
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;

1128 1129
    VIR_DEBUG("Relaying domain tunable event %s %d, callback %d, params %p %d",
              dom->name, dom->id, callback->callbackID, params, nparams);
1130 1131 1132 1133

    /* build return data */
    memset(&data, 0, sizeof(data));
    data.callbackID = callback->callbackID;
1134 1135
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
1136

1137 1138 1139
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &data.params.params_val,
                                &data.params.params_len,
1140 1141
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
        VIR_FREE(data.dom.name);
1142
        return -1;
1143
    }
1144 1145 1146 1147 1148 1149 1150

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

    return 0;
1151 1152 1153

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_callback_tunable_msg,
1154
             (char *) &data);
1155
    return -1;
1156 1157 1158
}


1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
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;
1180 1181
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

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

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

    return 0;
1192 1193 1194

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_callback_agent_lifecycle_msg,
1195
             (char *) &data);
1196
    return -1;
1197 1198 1199
}


1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
static int
remoteRelayDomainEventDeviceAdded(virConnectPtr conn,
                                  virDomainPtr dom,
                                  const char *devAlias,
                                  void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_device_added_msg data;

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

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

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

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

1222 1223
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
E
Eric Blake 已提交
1224
    data.callbackID = callback->callbackID;
1225 1226 1227 1228 1229 1230 1231

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

    return 0;
1232 1233 1234

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_callback_device_added_msg,
1235
             (char *) &data);
1236
    return -1;
1237 1238 1239
}


1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
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;
1260 1261
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
1262 1263 1264 1265 1266 1267 1268 1269 1270

    data.iteration = iteration;

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

    return 0;
1271 1272 1273

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_callback_migration_iteration_msg,
1274
             (char *) &data);
1275
    return -1;
1276
}
1277 1278


J
Jiri Denemark 已提交
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
static int
remoteRelayDomainEventJobCompleted(virConnectPtr conn,
                                   virDomainPtr dom,
                                   virTypedParameterPtr params,
                                   int nparams,
                                   void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_job_completed_msg data;

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

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

    /* build return data */
    memset(&data, 0, sizeof(data));
    data.callbackID = callback->callbackID;
1300 1301
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
J
Jiri Denemark 已提交
1302 1303 1304 1305

    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &data.params.params_val,
                                &data.params.params_len,
1306 1307
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
        VIR_FREE(data.dom.name);
J
Jiri Denemark 已提交
1308
        return -1;
1309
    }
J
Jiri Denemark 已提交
1310 1311 1312 1313 1314 1315

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_JOB_COMPLETED,
                                  (xdrproc_t)xdr_remote_domain_event_callback_job_completed_msg,
                                  &data);
    return 0;
1316 1317 1318

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_callback_job_completed_msg,
1319
             (char *) &data);
1320
    return -1;
J
Jiri Denemark 已提交
1321 1322 1323
}


1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
static int
remoteRelayDomainEventDeviceRemovalFailed(virConnectPtr conn,
                                          virDomainPtr dom,
                                          const char *devAlias,
                                          void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_device_removal_failed_msg data;

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

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

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

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

1346 1347
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
1348 1349 1350 1351 1352 1353 1354 1355
    data.callbackID = callback->callbackID;

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

    return 0;
1356 1357 1358

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_callback_device_removal_failed_msg,
1359
             (char *) &data);
1360
    return -1;
1361 1362 1363
}


1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
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) {
1386 1387 1388
        if (VIR_ALLOC(data.nsuri) < 0 ||
            VIR_STRDUP(*(data.nsuri), nsuri) < 0)
            goto error;
1389 1390
    }

1391 1392
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
1393 1394 1395 1396 1397 1398 1399 1400
    data.callbackID = callback->callbackID;

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_CALLBACK_METADATA_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_callback_metadata_change_msg,
                                  &data);

    return 0;
1401 1402 1403

 error:
    xdr_free((xdrproc_t)xdr_remote_domain_event_callback_metadata_change_msg,
1404
             (char *) &data);
1405
    return -1;
1406 1407 1408
}


1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
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;
    if (VIR_STRDUP(data.dev, dev) < 0)
        goto error;
    if (path) {
1434 1435
        if (VIR_ALLOC(data.path) < 0 ||
            VIR_STRDUP(*(data.path), path) < 0)
1436 1437 1438 1439
            goto error;
    }
    data.threshold = threshold;
    data.excess = excess;
1440 1441
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
1442 1443 1444 1445 1446 1447

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_BLOCK_THRESHOLD,
                                  (xdrproc_t)xdr_remote_domain_event_block_threshold_msg, &data);

    return 0;
1448

1449
 error:
1450
    xdr_free((xdrproc_t)xdr_remote_domain_event_block_threshold_msg,
1451
             (char *) &data);
1452 1453 1454
    return -1;
}

1455

1456
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
1457
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
1458
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
1459
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
1460
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
1461
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
1462
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
1463
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
1464
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
1465
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
1466
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
1467
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
1468
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
1469
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
1470
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
1471
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
1472
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
1473
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob2),
1474
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTunable),
1475
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventAgentLifecycle),
1476
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceAdded),
1477
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventMigrationIteration),
J
Jiri Denemark 已提交
1478
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventJobCompleted),
1479
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemovalFailed),
1480
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventMetadataChange),
1481
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockThreshold),
1482 1483 1484 1485
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

1486
static int
1487
remoteRelayNetworkEventLifecycle(virConnectPtr conn,
1488 1489 1490 1491
                                 virNetworkPtr net,
                                 int event,
                                 int detail,
                                 void *opaque)
1492
{
1493
    daemonClientEventCallbackPtr callback = opaque;
1494 1495
    remote_network_event_lifecycle_msg data;

1496 1497
    if (callback->callbackID < 0 ||
        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
1498 1499
        return -1;

1500 1501
    VIR_DEBUG("Relaying network lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);
1502 1503 1504

    /* build return data */
    memset(&data, 0, sizeof(data));
1505 1506
    if (make_nonnull_network(&data.net, net) < 0)
        goto error;
1507
    data.callbackID = callback->callbackID;
1508 1509 1510
    data.event = event;
    data.detail = detail;

1511
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
1512 1513 1514 1515
                                  REMOTE_PROC_NETWORK_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_network_event_lifecycle_msg, &data);

    return 0;
1516 1517 1518

 error:
    xdr_free((xdrproc_t)xdr_remote_network_event_lifecycle_msg,
1519
             (char *) &data);
1520
    return -1;
1521 1522 1523 1524 1525 1526 1527 1528
}

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

verify(ARRAY_CARDINALITY(networkEventCallbacks) == VIR_NETWORK_EVENT_ID_LAST);

1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
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));
1548 1549
    if (make_nonnull_storage_pool(&data.pool, pool) < 0)
        goto error;
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
    data.callbackID = callback->callbackID;
    data.event = event;
    data.detail = detail;

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

    return 0;
1560 1561 1562

 error:
    xdr_free((xdrproc_t)xdr_remote_storage_pool_event_lifecycle_msg,
1563
             (char *) &data);
1564
    return -1;
1565 1566
}

1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
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));
1584 1585
    if (make_nonnull_storage_pool(&data.pool, pool) < 0)
        goto error;
1586 1587 1588 1589 1590 1591 1592 1593
    data.callbackID = callback->callbackID;

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

    return 0;
1594 1595 1596

 error:
    xdr_free((xdrproc_t)xdr_remote_storage_pool_event_refresh_msg,
1597
             (char *) &data);
1598
    return -1;
1599 1600
}

1601 1602
static virConnectStoragePoolEventGenericCallback storageEventCallbacks[] = {
    VIR_STORAGE_POOL_EVENT_CALLBACK(remoteRelayStoragePoolEventLifecycle),
1603
    VIR_STORAGE_POOL_EVENT_CALLBACK(remoteRelayStoragePoolEventRefresh),
1604 1605 1606 1607
};

verify(ARRAY_CARDINALITY(storageEventCallbacks) == VIR_STORAGE_POOL_EVENT_ID_LAST);

1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
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));
1627 1628
    if (make_nonnull_node_device(&data.dev, dev) < 0)
        goto error;
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638
    data.callbackID = callback->callbackID;
    data.event = event;
    data.detail = detail;

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

    return 0;
1639 1640 1641

 error:
    xdr_free((xdrproc_t)xdr_remote_node_device_event_lifecycle_msg,
1642
             (char *) &data);
1643
    return -1;
1644 1645
}

1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
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));
1663 1664
    if (make_nonnull_node_device(&data.dev, dev) < 0)
        goto error;
1665 1666 1667 1668 1669 1670 1671 1672
    data.callbackID = callback->callbackID;

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

    return 0;
1673 1674 1675

 error:
    xdr_free((xdrproc_t)xdr_remote_node_device_event_update_msg,
1676
             (char *) &data);
1677
    return -1;
1678 1679
}

1680 1681
static virConnectNodeDeviceEventGenericCallback nodeDeviceEventCallbacks[] = {
    VIR_NODE_DEVICE_EVENT_CALLBACK(remoteRelayNodeDeviceEventLifecycle),
1682
    VIR_NODE_DEVICE_EVENT_CALLBACK(remoteRelayNodeDeviceEventUpdate),
1683 1684 1685 1686
};

verify(ARRAY_CARDINALITY(nodeDeviceEventCallbacks) == VIR_NODE_DEVICE_EVENT_ID_LAST);

1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
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));
1706 1707
    if (make_nonnull_secret(&data.secret, secret) < 0)
        goto error;
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
    data.callbackID = callback->callbackID;
    data.event = event;
    data.detail = detail;

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_SECRET_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_secret_event_lifecycle_msg,
                                  &data);

    return 0;
1718 1719 1720

 error:
    xdr_free((xdrproc_t)xdr_remote_secret_event_lifecycle_msg,
1721
             (char *) &data);
1722
    return -1;
1723 1724
}

1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
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));
1742 1743
    if (make_nonnull_secret(&data.secret, secret) < 0)
        goto error;
1744 1745 1746 1747 1748 1749 1750 1751
    data.callbackID = callback->callbackID;

    remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                  REMOTE_PROC_SECRET_EVENT_VALUE_CHANGED,
                                  (xdrproc_t)xdr_remote_secret_event_value_changed_msg,
                                  &data);

    return 0;
1752 1753 1754

 error:
    xdr_free((xdrproc_t)xdr_remote_secret_event_value_changed_msg,
1755
             (char *) &data);
1756
    return -1;
1757 1758
}

1759 1760
static virConnectSecretEventGenericCallback secretEventCallbacks[] = {
    VIR_SECRET_EVENT_CALLBACK(remoteRelaySecretEventLifecycle),
1761
    VIR_SECRET_EVENT_CALLBACK(remoteRelaySecretEventValueChanged),
1762 1763 1764 1765
};

verify(ARRAY_CARDINALITY(secretEventCallbacks) == VIR_SECRET_EVENT_ID_LAST);

1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
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;
    if (VIR_STRDUP(data.event, event) < 0)
        goto error;
    data.seconds = seconds;
    data.micros = micros;
    if (details &&
J
John Ferlan 已提交
1794 1795
        ((VIR_ALLOC(data.details) < 0) ||
         VIR_STRDUP(*(data.details), details) < 0))
1796
        goto error;
1797 1798
    if (make_nonnull_domain(&data.dom, dom) < 0)
        goto error;
1799 1800 1801 1802 1803 1804 1805

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

1806
 error:
1807
    xdr_free((xdrproc_t)xdr_qemu_domain_monitor_event_msg,
1808
             (char *) &data);
1809
    return;
1810 1811
}

1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
static
void remoteRelayConnectionClosedEvent(virConnectPtr conn ATTRIBUTE_UNUSED, int reason, void *opaque)
{
    virNetServerClientPtr client = opaque;

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

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

1826 1827 1828
#define DEREG_CB(conn, eventCallbacks, neventCallbacks, deregFcn, name) \
    do { \
        size_t i; \
1829 1830 1831 1832 1833
        if (neventCallbacks && !conn) { \
            VIR_WARN("Have %zu %s event callbacks but no connection", \
                     neventCallbacks, name); \
            break; \
        } \
1834 1835 1836
        for (i = 0; i < neventCallbacks; i++) { \
            int callbackID = eventCallbacks[i]->callbackID; \
            if (callbackID < 0) { \
1837
                VIR_WARN("unexpected incomplete %s callback %zu", name, i); \
1838 1839 1840 1841 1842 1843 1844 1845 1846
                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); \
1847
        neventCallbacks = 0; \
1848 1849
    } while (0);

1850 1851 1852 1853 1854 1855 1856 1857 1858 1859

static void
remoteClientFreePrivateCallbacks(struct daemonClientPrivate *priv)
{
    virIdentityPtr sysident = virIdentityGetSystem();
    virIdentitySetCurrent(sysident);

    DEREG_CB(priv->conn, priv->domainEventCallbacks,
             priv->ndomainEventCallbacks,
             virConnectDomainEventDeregisterAny, "domain");
1860
    DEREG_CB(priv->networkConn, priv->networkEventCallbacks,
1861 1862
             priv->nnetworkEventCallbacks,
             virConnectNetworkEventDeregisterAny, "network");
1863
    DEREG_CB(priv->storageConn, priv->storageEventCallbacks,
1864 1865
             priv->nstorageEventCallbacks,
             virConnectStoragePoolEventDeregisterAny, "storage");
1866
    DEREG_CB(priv->nodedevConn, priv->nodeDeviceEventCallbacks,
1867 1868
             priv->nnodeDeviceEventCallbacks,
             virConnectNodeDeviceEventDeregisterAny, "node device");
1869
    DEREG_CB(priv->secretConn, priv->secretEventCallbacks,
1870 1871 1872 1873 1874 1875
             priv->nsecretEventCallbacks,
             virConnectSecretEventDeregisterAny, "secret");
    DEREG_CB(priv->conn, priv->qemuEventCallbacks,
             priv->nqemuEventCallbacks,
             virConnectDomainQemuMonitorEventDeregister, "qemu monitor");

1876
    if (priv->closeRegistered && priv->conn) {
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
        if (virConnectUnregisterCloseCallback(priv->conn,
                                              remoteRelayConnectionClosedEvent) < 0)
            VIR_WARN("unexpected close callback event deregister failure");
    }

    virIdentitySetCurrent(NULL);
    virObjectUnref(sysident);
}
#undef DEREG_CB


1888 1889 1890 1891 1892 1893 1894
/*
 * 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
 */
1895
void remoteClientFree(void *data)
1896 1897 1898
{
    struct daemonClientPrivate *priv = data;

1899
    if (priv->conn)
1900
        virConnectClose(priv->conn);
1901 1902
    if (priv->interfaceConn)
        virConnectClose(priv->interfaceConn);
1903 1904
    if (priv->networkConn)
        virConnectClose(priv->networkConn);
1905 1906
    if (priv->nodedevConn)
        virConnectClose(priv->nodedevConn);
1907 1908
    if (priv->nwfilterConn)
        virConnectClose(priv->nwfilterConn);
1909 1910
    if (priv->secretConn)
        virConnectClose(priv->secretConn);
1911 1912
    if (priv->storageConn)
        virConnectClose(priv->storageConn);
1913

1914 1915 1916 1917
    VIR_FREE(priv);
}


1918 1919 1920 1921 1922
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
1923

1924
    remoteClientFreePrivateCallbacks(priv);
1925 1926
}

1927

1928 1929
void *remoteClientNew(virNetServerClientPtr client,
                      void *opaque ATTRIBUTE_UNUSED)
1930 1931 1932
{
    struct daemonClientPrivate *priv;

1933
    if (VIR_ALLOC(priv) < 0)
1934
        return NULL;
1935 1936 1937

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
1938
        virReportSystemError(errno, "%s", _("unable to init mutex"));
1939
        return NULL;
1940 1941
    }

1942
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
1943
    return priv;
1944 1945
}

1946 1947 1948
/*----- Functions. -----*/

static int
1949
remoteDispatchConnectOpen(virNetServerPtr server ATTRIBUTE_UNUSED,
1950 1951 1952 1953
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
1954 1955
{
    const char *name;
1956
    unsigned int flags;
1957
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
1958
    int rv = -1;
1959

1960 1961 1962 1963
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
1964
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
1965 1966 1967
        goto cleanup;
    }

1968 1969 1970 1971 1972 1973
    name = args->name ? *args->name : NULL;

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

1977
    priv->conn =
1978
        flags & VIR_CONNECT_RO
1979 1980
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
1981

1982
    if (priv->conn == NULL)
1983 1984
        goto cleanup;

1985
    priv->interfaceConn = virObjectRef(priv->conn);
1986
    priv->networkConn = virObjectRef(priv->conn);
1987
    priv->nodedevConn = virObjectRef(priv->conn);
1988
    priv->nwfilterConn = virObjectRef(priv->conn);
1989
    priv->secretConn = virObjectRef(priv->conn);
1990
    priv->storageConn = virObjectRef(priv->conn);
1991

1992 1993 1994 1995 1996
    /* 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));
1997
    rv = 0;
1998

1999
 cleanup:
2000
    if (rv < 0)
2001 2002
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2003
    return rv;
2004 2005 2006 2007
}


static int
2008
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
2009
                           virNetServerClientPtr client,
2010 2011
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
2012
{
2013
    virNetServerClientDelayedClose(client);
2014
    return 0;
2015 2016
}

2017

2018
static int
2019
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
2020
                                     virNetServerClientPtr client,
2021
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
2022
                                     virNetMessageErrorPtr rerr,
2023 2024
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
2025
{
2026
    virDomainPtr dom = NULL;
2027 2028
    char *type;
    int nparams;
2029
    int rv = -1;
2030 2031
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2032

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

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

2041
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
2042
        goto cleanup;
2043 2044 2045

    ret->type = type;
    ret->nparams = nparams;
2046 2047
    rv = 0;

2048
 cleanup:
2049
    if (rv < 0)
2050
        virNetMessageSaveError(rerr);
2051
    virObjectUnref(dom);
2052
    return rv;
2053 2054 2055
}

static int
2056
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
2057
                                           virNetServerClientPtr client,
2058
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2059
                                           virNetMessageErrorPtr rerr,
2060 2061
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
2062
{
2063
    virDomainPtr dom = NULL;
2064
    virTypedParameterPtr params = NULL;
2065
    int nparams = 0;
2066
    int rv = -1;
2067 2068
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2069

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

2075
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
2076
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2077
        goto cleanup;
2078
    }
2079
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2080
        goto cleanup;
2081
    nparams = args->nparams;
2082

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

2086
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
2087
        goto cleanup;
2088

2089 2090 2091 2092
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                0) < 0)
2093 2094 2095 2096
        goto cleanup;

    rv = 0;

2097
 cleanup:
2098
    if (rv < 0)
2099
        virNetMessageSaveError(rerr);
2100
    virTypedParamsFree(params, nparams);
2101
    virObjectUnref(dom);
2102 2103 2104 2105
    return rv;
}

static int
2106
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
2107
                                                virNetServerClientPtr client,
2108
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
2109
                                                virNetMessageErrorPtr rerr,
2110 2111 2112 2113 2114
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
2115
    int nparams = 0;
2116
    int rv = -1;
2117 2118
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2119

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

2125
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
2126
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2127 2128
        goto cleanup;
    }
2129
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2130
        goto cleanup;
2131
    nparams = args->nparams;
2132

2133
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2134 2135 2136 2137 2138 2139
        goto cleanup;

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

2140 2141 2142 2143
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
2144
        goto cleanup;
2145

2146
    rv = 0;
2147

2148
 cleanup:
2149
    if (rv < 0)
2150
        virNetMessageSaveError(rerr);
2151
    virTypedParamsFree(params, nparams);
2152
    virObjectUnref(dom);
2153
    return rv;
2154 2155
}

2156
static int
2157
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
2158
                                virNetServerClientPtr client,
2159
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
2160
                                virNetMessageErrorPtr rerr,
2161 2162
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
2163
{
2164
    virDomainPtr dom = NULL;
2165
    virDomainMemoryStatPtr stats = NULL;
2166 2167
    int nr_stats;
    size_t i;
2168
    int rv = -1;
2169 2170
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2171

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

2177
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
2178 2179
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
2180
        goto cleanup;
2181 2182
    }

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

    /* Allocate stats array for making dispatch call */
2187
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
2188
        goto cleanup;
2189

2190
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
2191
    if (nr_stats < 0)
2192
        goto cleanup;
2193 2194

    /* Allocate return buffer */
2195
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
2196
        goto cleanup;
2197 2198 2199 2200 2201 2202 2203

    /* 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;
2204 2205
    rv = 0;

2206
 cleanup:
2207
    if (rv < 0)
2208
        virNetMessageSaveError(rerr);
2209
    virObjectUnref(dom);
2210
    VIR_FREE(stats);
2211
    return rv;
2212 2213
}

2214
static int
2215
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
2216
                              virNetServerClientPtr client,
2217
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2218
                              virNetMessageErrorPtr rerr,
2219 2220
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
2221
{
2222
    virDomainPtr dom = NULL;
2223 2224 2225 2226
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
2227
    int rv = -1;
2228 2229
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2230

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

2236
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2237
        goto cleanup;
2238 2239 2240 2241 2242 2243
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
2244 2245
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
2246
        goto cleanup;
2247 2248 2249
    }

    ret->buffer.buffer_len = size;
2250
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
2251
        goto cleanup;
2252

2253
    if (virDomainBlockPeek(dom, path, offset, size,
2254
                           ret->buffer.buffer_val, flags) < 0)
2255
        goto cleanup;
2256

2257 2258
    rv = 0;

2259
 cleanup:
2260
    if (rv < 0) {
2261
        virNetMessageSaveError(rerr);
2262 2263
        VIR_FREE(ret->buffer.buffer_val);
    }
2264
    virObjectUnref(dom);
2265
    return rv;
2266 2267
}

2268 2269
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
2270
                                    virNetServerClientPtr client,
2271
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2272 2273 2274 2275 2276 2277 2278
                                    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;
2279
    int nparams = 0;
2280 2281 2282 2283 2284 2285
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2286
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2287 2288 2289 2290 2291 2292 2293
        goto cleanup;
    }

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

2294
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
2295
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2296 2297
        goto cleanup;
    }
2298
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2299
        goto cleanup;
2300
    nparams = args->nparams;
2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312

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

2313 2314 2315 2316 2317
    /* Serialize the block stats. */
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
2318 2319
        goto cleanup;

2320
 success:
2321 2322
    rv = 0;

2323
 cleanup:
2324
    if (rv < 0)
2325
        virNetMessageSaveError(rerr);
2326
    virTypedParamsFree(params, nparams);
2327
    virObjectUnref(dom);
2328 2329 2330
    return rv;
}

R
Richard W.M. Jones 已提交
2331
static int
2332
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
2333
                               virNetServerClientPtr client,
2334
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
2335
                               virNetMessageErrorPtr rerr,
2336 2337
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
2338
{
2339
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
2340 2341 2342
    unsigned long long offset;
    size_t size;
    unsigned int flags;
2343
    int rv = -1;
2344 2345
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
2346

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

2352
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2353
        goto cleanup;
R
Richard W.M. Jones 已提交
2354 2355 2356 2357 2358
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
2359 2360
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
2361
        goto cleanup;
R
Richard W.M. Jones 已提交
2362 2363 2364
    }

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

2368
    if (virDomainMemoryPeek(dom, offset, size,
2369
                            ret->buffer.buffer_val, flags) < 0)
2370
        goto cleanup;
R
Richard W.M. Jones 已提交
2371

2372 2373
    rv = 0;

2374
 cleanup:
2375
    if (rv < 0) {
2376
        virNetMessageSaveError(rerr);
2377 2378
        VIR_FREE(ret->buffer.buffer_val);
    }
2379
    virObjectUnref(dom);
2380
    return rv;
R
Richard W.M. Jones 已提交
2381 2382
}

2383
static int
2384
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
2385
                                     virNetServerClientPtr client,
2386
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
2387
                                     virNetMessageErrorPtr rerr,
2388 2389
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
2390
{
2391 2392
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
2393
    int rv = -1;
2394 2395
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2396

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

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

2405
    if (VIR_ALLOC(seclabel) < 0)
2406 2407 2408 2409 2410 2411
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
2412
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
2413
        goto cleanup;
2414 2415
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
2416

2417 2418
    rv = 0;

2419
 cleanup:
2420
    if (rv < 0)
2421
        virNetMessageSaveError(rerr);
2422
    virObjectUnref(dom);
2423
    VIR_FREE(seclabel);
2424
    return rv;
2425 2426
}

M
Marcelo Cerri 已提交
2427 2428
static int
remoteDispatchDomainGetSecurityLabelList(virNetServerPtr server ATTRIBUTE_UNUSED,
2429
                                         virNetServerClientPtr client,
M
Marcelo Cerri 已提交
2430 2431 2432 2433 2434 2435 2436
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_domain_get_security_label_list_args *args,
                                         remote_domain_get_security_label_list_ret *ret)
{
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabels = NULL;
2437 2438
    int len, rv = -1;
    size_t i;
M
Marcelo Cerri 已提交
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

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

2457
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
2458 2459 2460 2461 2462
        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];
2463
        if (VIR_STRDUP(cur->label.label_val, seclabels[i].label) < 0) {
M
Marcelo Cerri 已提交
2464 2465 2466 2467 2468 2469 2470 2471 2472
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("failed to copy security label"));
            goto cleanup;
        }
        cur->label.label_len = label_len;
        cur->enforcing = seclabels[i].enforcing;
    }
    ret->labels.labels_len = ret->ret = len;

2473
 done:
M
Marcelo Cerri 已提交
2474 2475
    rv = 0;

2476
 cleanup:
M
Marcelo Cerri 已提交
2477 2478
    if (rv < 0)
        virNetMessageSaveError(rerr);
2479
    virObjectUnref(dom);
M
Marcelo Cerri 已提交
2480 2481 2482 2483
    VIR_FREE(seclabels);
    return rv;
}

2484
static int
2485
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
2486
                                   virNetServerClientPtr client,
2487
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2488
                                   virNetMessageErrorPtr rerr,
2489
                                   remote_node_get_security_model_ret *ret)
2490
{
2491
    virSecurityModel secmodel;
2492
    int rv = -1;
2493 2494
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2495

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

2501
    memset(&secmodel, 0, sizeof(secmodel));
2502
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
2503 2504 2505
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
2506
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
2507 2508 2509 2510
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
2511
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
2512
        goto cleanup;
2513
    strcpy(ret->doi.doi_val, secmodel.doi);
2514

2515 2516
    rv = 0;

2517
 cleanup:
2518
    if (rv < 0)
2519
        virNetMessageSaveError(rerr);
2520
    return rv;
2521 2522
}

2523
static int
2524
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
2525
                                   virNetServerClientPtr client,
2526
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2527
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
2528 2529
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
2530 2531 2532 2533 2534
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
2535 2536
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2537

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

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

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
2547
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
2548 2549 2550 2551 2552
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
2553
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2554 2555 2556 2557 2558 2559
        goto cleanup;
    }

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

E
Eric Blake 已提交
2562
    if ((num = virDomainGetVcpuPinInfo(dom,
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
                                       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;
2576 2577 2578 2579
    cpumaps = NULL;

    rv = 0;

2580
 cleanup:
2581 2582 2583
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumaps);
2584
    virObjectUnref(dom);
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615
    return rv;
}

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

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

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

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

    rv = 0;

2616
 cleanup:
2617 2618
    if (rv < 0)
        virNetMessageSaveError(rerr);
2619
    virObjectUnref(dom);
2620 2621 2622 2623 2624 2625
    return rv;
}


static int
remoteDispatchDomainGetEmulatorPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
2626
                                       virNetServerClientPtr client,
2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_emulator_pin_info_args *args,
                                       remote_domain_get_emulator_pin_info_ret *ret)
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int r;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    /* Allocate buffers to take the results */
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maplen) < 0)
2650
        goto cleanup;
2651 2652 2653 2654 2655 2656 2657 2658 2659 2660

    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;
2661 2662 2663 2664
    cpumaps = NULL;

    rv = 0;

2665
 cleanup:
2666
    if (rv < 0)
2667
        virNetMessageSaveError(rerr);
2668
    VIR_FREE(cpumaps);
2669
    virObjectUnref(dom);
2670 2671 2672
    return rv;
}

2673
static int
2674
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
2675
                             virNetServerClientPtr client,
2676
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
2677
                             virNetMessageErrorPtr rerr,
2678 2679
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
2680
{
2681
    virDomainPtr dom = NULL;
2682 2683
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
2684 2685
    int info_len;
    size_t i;
2686
    int rv = -1;
2687 2688
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2689

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

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

2698
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
2699
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
2700
        goto cleanup;
2701
    }
2702

2703 2704
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
2705
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2706 2707 2708 2709 2710
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
2711
        goto cleanup;
2712 2713
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
2714
        goto cleanup;
2715 2716 2717 2718 2719 2720 2721 2722 2723

    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)
2724
        goto cleanup;
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741

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

2743
 cleanup:
2744
    if (rv < 0) {
2745
        virNetMessageSaveError(rerr);
2746 2747 2748 2749
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
2750
    virObjectUnref(dom);
2751
    return rv;
2752 2753
}

2754
static int
2755 2756 2757 2758 2759 2760
remoteDispatchDomainGetIOThreadInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_get_iothread_info_args *args,
                                    remote_domain_get_iothread_info_ret *ret)
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainIOThreadInfoPtr *info = NULL;
    virDomainPtr dom = NULL;
    remote_domain_iothread_info *dst;
    int ninfo = 0;

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

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

2778
    if ((ninfo = virDomainGetIOThreadInfo(dom, &info, args->flags)) < 0)
2779 2780
        goto cleanup;

2781
    if (ninfo > REMOTE_IOTHREAD_INFO_MAX) {
2782 2783
        virReportError(VIR_ERR_RPC,
                       _("Too many IOThreads in info: %d for limit %d"),
2784
                       ninfo, REMOTE_IOTHREAD_INFO_MAX);
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819
        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++)
2820
            virDomainIOThreadInfoFree(info[i]);
2821 2822 2823 2824 2825
    VIR_FREE(info);

    return rv;
}

2826
static int
2827
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
2828
                                   virNetServerClientPtr client,
2829
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2830
                                   virNetMessageErrorPtr rerr,
2831 2832
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
2833
{
2834 2835 2836 2837 2838
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2839
    int rv = -1;
2840 2841
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2842

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

2848 2849 2850 2851
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
2852
    if (VIR_ALLOC(uri_out) < 0)
2853
        goto cleanup;
2854

2855
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
2856 2857
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
2858
        goto cleanup;
2859

2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870
    /* 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;
    }
2871

2872
    rv = 0;
2873

2874
 cleanup:
2875
    if (rv < 0)
2876
        virNetMessageSaveError(rerr);
2877
    VIR_FREE(uri_out);
2878
    return rv;
2879 2880
}

2881
static int
2882
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
2883
                                    virNetServerClientPtr client,
2884
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2885
                                    virNetMessageErrorPtr rerr,
2886 2887
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
2888
{
2889 2890 2891 2892 2893
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2894
    int rv = -1;
2895 2896
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2897

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

2903 2904
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
2905

2906
    /* Wacky world of XDR ... */
2907
    if (VIR_ALLOC(uri_out) < 0)
2908
        goto cleanup;
2909

2910
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
2911 2912 2913
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
2914
        goto cleanup;
2915

2916 2917 2918 2919 2920 2921
    /* 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;
2922

2923 2924
    rv = 0;

2925
 cleanup:
2926
    if (rv < 0) {
2927
        virNetMessageSaveError(rerr);
2928 2929
        VIR_FREE(uri_out);
    }
2930
    return rv;
2931 2932
}

C
Chris Lalancette 已提交
2933
static int
2934
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
2935
                                        virNetServerClientPtr client,
2936
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
2937 2938 2939
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
2940
{
2941
    virDomainPtr dom = NULL;
2942
    virTypedParameterPtr params = NULL;
2943
    int nparams = 0;
2944
    unsigned int flags;
2945
    int rv = -1;
2946 2947
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2948

2949
    if (!priv->conn) {
2950
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2951
        goto cleanup;
2952
    }
C
Chris Lalancette 已提交
2953

2954
    flags = args->flags;
C
Chris Lalancette 已提交
2955

2956
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2957
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2958 2959
        goto cleanup;
    }
2960
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2961
        goto cleanup;
2962
    nparams = args->nparams;
C
Chris Lalancette 已提交
2963

2964
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2965
        goto cleanup;
C
Chris Lalancette 已提交
2966

2967
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
2968
        goto cleanup;
C
Chris Lalancette 已提交
2969

2970 2971 2972 2973 2974 2975
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
2976 2977
    }

2978 2979 2980 2981
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
2982
        goto cleanup;
2983

2984
 success:
2985 2986
    rv = 0;

2987
 cleanup:
2988
    if (rv < 0)
2989
        virNetMessageSaveError(rerr);
2990
    virTypedParamsFree(params, nparams);
2991
    virObjectUnref(dom);
2992
    return rv;
2993 2994
}

2995 2996
static int
remoteDispatchDomainGetNumaParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
2997
                                      virNetServerClientPtr client,
2998 2999 3000 3001 3002 3003 3004
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_domain_get_numa_parameters_args *args,
                                      remote_domain_get_numa_parameters_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
3005
    int nparams = 0;
3006 3007 3008 3009 3010 3011
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
3012
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3013 3014 3015 3016 3017
        goto cleanup;
    }

    flags = args->flags;

3018
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
3019
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3020 3021
        goto cleanup;
    }
3022
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3023
        goto cleanup;
3024
    nparams = args->nparams;
3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039

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

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

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

3040 3041 3042 3043
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                flags) < 0)
3044 3045
        goto cleanup;

3046
 success:
3047 3048
    rv = 0;

3049
 cleanup:
3050 3051
    if (rv < 0)
        virNetMessageSaveError(rerr);
3052
    virTypedParamsFree(params, nparams);
3053
    virObjectUnref(dom);
3054 3055 3056
    return rv;
}

3057
static int
3058
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
3059
                                       virNetServerClientPtr client,
3060
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
3061 3062 3063
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
3064
{
3065
    virDomainPtr dom = NULL;
3066
    virTypedParameterPtr params = NULL;
3067
    int nparams = 0;
3068
    unsigned int flags;
3069
    int rv = -1;
3070 3071
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3072

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

3078 3079
    flags = args->flags;

3080
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
3081
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3082
        goto cleanup;
3083
    }
3084
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3085
        goto cleanup;
3086
    nparams = args->nparams;
3087

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

3091
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
3092
        goto cleanup;
3093

3094 3095 3096 3097 3098 3099 3100 3101
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

3102 3103 3104 3105
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
3106
        goto cleanup;
3107

3108
 success:
3109
    rv = 0;
3110

3111
 cleanup:
3112
    if (rv < 0)
3113
        virNetMessageSaveError(rerr);
3114
    virTypedParamsFree(params, nparams);
3115
    virObjectUnref(dom);
3116
    return rv;
3117 3118
}

3119
static int
3120
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
3121
                              virNetServerClientPtr client,
3122
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
3123 3124 3125
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
3126
{
3127
    virNodeCPUStatsPtr params = NULL;
3128
    size_t i;
3129
    int cpuNum = args->cpuNum;
3130
    int nparams = 0;
3131 3132
    unsigned int flags;
    int rv = -1;
3133 3134
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3135

3136
    if (!priv->conn) {
3137
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3138 3139 3140 3141 3142
        goto cleanup;
    }

    flags = args->flags;

3143
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
3144
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3145 3146
        goto cleanup;
    }
3147
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3148
        goto cleanup;
3149
    nparams = args->nparams;
3150

3151
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164
        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)
3165
        goto cleanup;
3166 3167 3168

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
3169 3170
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
3171 3172 3173 3174

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

3175
 success:
3176 3177
    rv = 0;

3178
 cleanup:
3179
    if (rv < 0) {
3180
        virNetMessageSaveError(rerr);
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191
        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
3192
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
3193
                                 virNetServerClientPtr client,
3194
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
3195 3196 3197
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
3198
{
3199
    virNodeMemoryStatsPtr params = NULL;
3200
    size_t i;
3201
    int cellNum = args->cellNum;
3202
    int nparams = 0;
3203 3204
    unsigned int flags;
    int rv = -1;
3205 3206
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3207

3208
    if (!priv->conn) {
3209
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3210 3211 3212 3213 3214
        goto cleanup;
    }

    flags = args->flags;

3215
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
3216
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3217 3218
        goto cleanup;
    }
3219
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3220
        goto cleanup;
3221
    nparams = args->nparams;
3222

3223
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236
        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)
3237
        goto cleanup;
3238 3239 3240

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
3241 3242
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
3243 3244 3245 3246

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

3247
 success:
3248 3249
    rv = 0;

3250
 cleanup:
3251
    if (rv < 0) {
3252
        virNetMessageSaveError(rerr);
3253 3254 3255 3256 3257 3258 3259 3260 3261 3262
        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;
}

3263 3264
static int
remoteDispatchDomainGetLaunchSecurityInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
3265
                                          virNetServerClientPtr client,
3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          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;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

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

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

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

    rv = 0;

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

3310 3311
static int
remoteDispatchDomainGetPerfEvents(virNetServerPtr server ATTRIBUTE_UNUSED,
3312
                                  virNetServerClientPtr client,
3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                  virNetMessageErrorPtr rerr,
                                  remote_domain_get_perf_events_args *args,
                                  remote_domain_get_perf_events_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

3333
    if (virDomainGetPerfEvents(dom, &params, &nparams, args->flags) < 0)
3334 3335
        goto cleanup;

3336
    if (nparams > REMOTE_DOMAIN_PERF_EVENTS_MAX) {
3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }

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

    rv = 0;

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

3357 3358
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
3359
                                    virNetServerClientPtr client,
3360
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_get_block_job_info_args *args,
                                    remote_domain_get_block_job_info_ret *ret)
{
    virDomainPtr dom = NULL;
    virDomainBlockJobInfo tmp;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
3372
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
        goto cleanup;
    }

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

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

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

3390
 cleanup:
3391 3392
    if (rv < 0)
        virNetMessageSaveError(rerr);
3393
    virObjectUnref(dom);
3394 3395 3396
    return rv;
}

3397 3398
static int
remoteDispatchDomainGetBlockIoTune(virNetServerPtr server ATTRIBUTE_UNUSED,
3399
                                   virNetServerClientPtr client,
3400 3401 3402 3403 3404 3405 3406 3407
                                   virNetMessagePtr hdr ATTRIBUTE_UNUSED,
                                   virNetMessageErrorPtr rerr,
                                   remote_domain_get_block_io_tune_args *args,
                                   remote_domain_get_block_io_tune_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    virTypedParameterPtr params = NULL;
3408
    int nparams = 0;
3409 3410 3411 3412
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

3417
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
3418
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3419 3420 3421
        goto cleanup;
    }

3422
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3423
        goto cleanup;
3424
    nparams = args->nparams;
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440

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

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

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

3441 3442 3443 3444 3445
    /* Serialize the block I/O tuning parameters. */
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
3446 3447
        goto cleanup;

3448
 success:
3449 3450
    rv = 0;

3451
 cleanup:
3452 3453
    if (rv < 0)
        virNetMessageSaveError(rerr);
3454
    virTypedParamsFree(params, nparams);
3455
    virObjectUnref(dom);
3456 3457
    return rv;
}
3458

D
Daniel Veillard 已提交
3459 3460
/*-------------------------------------------------------------*/

3461
static int
3462
remoteDispatchAuthList(virNetServerPtr server,
3463
                       virNetServerClientPtr client,
3464
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
3465
                       virNetMessageErrorPtr rerr,
3466
                       remote_auth_list_ret *ret)
3467
{
3468
    int rv = -1;
3469 3470
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
3471
    gid_t callerGid;
3472
    pid_t callerPid;
3473
    unsigned long long timestamp;
3474 3475 3476 3477 3478 3479

    /* 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) {
3480
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
3481
                                              &callerPid, &timestamp) < 0) {
3482 3483 3484 3485
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
3486 3487
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
3488
                            (long long) callerPid, (int) callerUid) < 0)
J
Jim Fehlig 已提交
3489 3490
                goto cleanup;
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
3491
            virNetServerSetClientAuthenticated(server, client);
3492
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
3493
            VIR_FREE(ident);
3494 3495
        }
    }
3496

3497
    ret->types.types_len = 1;
3498
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
3499
        goto cleanup;
3500

3501
    switch ((virNetServerServiceAuthMethods) auth) {
3502 3503 3504 3505 3506 3507 3508 3509 3510 3511
    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;
    }
3512

3513 3514
    rv = 0;

3515
 cleanup:
3516
    if (rv < 0)
3517
        virNetMessageSaveError(rerr);
3518
    return rv;
3519 3520 3521
}


3522
#ifdef WITH_SASL
3523 3524
/*
 * Initializes the SASL session in prepare for authentication
3525
 * and gives the client a list of allowed mechanisms to choose
3526 3527
 */
static int
3528 3529
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
3530
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3531
                           virNetMessageErrorPtr rerr,
3532
                           remote_auth_sasl_init_ret *ret)
3533
{
3534 3535 3536
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3537

3538
    virMutexLock(&priv->lock);
3539

3540 3541 3542
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
3543
        VIR_ERROR(_("client tried invalid SASL init request"));
3544
        goto authfail;
3545 3546
    }

3547 3548
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
3549 3550
                                      virNetServerClientLocalAddrStringSASL(client),
                                      virNetServerClientRemoteAddrStringSASL(client));
3551
    if (!sasl)
3552
        goto authfail;
3553

3554
    /* Inform SASL that we've got an external SSF layer from TLS */
3555 3556 3557 3558
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
3559
            goto authfail;
3560 3561 3562 3563 3564

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
3565
            goto authfail;
3566 3567
    }

3568
    if (virNetServerClientIsSecure(client))
3569
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
3570 3571
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
3572
        /* Plain TCP, better get an SSF layer */
3573 3574 3575 3576
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
3577

3578
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
3579
        goto authfail;
3580
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
3581

3582 3583
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
3584
    return 0;
3585

3586
 authfail:
3587
    virResetLastError();
3588 3589
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3590
    virNetMessageSaveError(rerr);
3591 3592 3593
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3594
    virObjectUnref(sasl);
3595
    virMutexUnlock(&priv->lock);
3596
    return -1;
3597 3598
}

3599
/*
3600 3601
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
3602
static int
3603 3604
remoteSASLFinish(virNetServerPtr server,
                 virNetServerClientPtr client)
3605
{
3606
    virIdentityPtr clnt_identity = NULL;
3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620
    const char *identity;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    int ssf;

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

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

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
3624
        return -2;
3625

3626 3627
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
3628

3629 3630 3631
    if (!(clnt_identity = virNetServerClientGetIdentity(client)))
        goto error;

3632
    virNetServerSetClientAuthenticated(server, client);
3633
    virNetServerClientSetSASLSession(client, priv->sasl);
3634
    virIdentitySetSASLUserName(clnt_identity, identity);
3635

3636
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
3637

3638 3639 3640
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3641

3642
    virObjectUnref(clnt_identity);
3643
    virObjectUnref(priv->sasl);
3644
    priv->sasl = NULL;
3645

3646
    return 0;
3647

3648
 error:
3649 3650
    return -1;
}
3651

3652 3653 3654 3655
/*
 * This starts the SASL authentication negotiation.
 */
static int
3656
remoteDispatchAuthSaslStart(virNetServerPtr server,
3657
                            virNetServerClientPtr client,
3658
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3659
                            virNetMessageErrorPtr rerr,
3660 3661
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
3662 3663
{
    const char *serverout;
3664
    size_t serveroutlen;
3665
    int err;
3666 3667
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3668
    const char *identity;
3669

3670
    virMutexLock(&priv->lock);
3671

3672 3673 3674
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
3675
        VIR_ERROR(_("client tried invalid SASL start request"));
3676
        goto authfail;
3677 3678
    }

3679 3680
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
3681 3682 3683 3684 3685 3686 3687 3688 3689
    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)
3690
        goto authfail;
3691

3692
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3693
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
3694
        goto authfail;
3695 3696 3697 3698
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3699 3700
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3701 3702 3703 3704 3705 3706 3707
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3708
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3709
    if (err == VIR_NET_SASL_CONTINUE) {
3710 3711
        ret->complete = 0;
    } else {
3712
        /* Check username whitelist ACL */
3713
        if ((err = remoteSASLFinish(server, client)) < 0) {
3714 3715 3716 3717 3718
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3719

3720 3721 3722
        ret->complete = 1;
    }

3723
    virMutexUnlock(&priv->lock);
3724
    return 0;
3725

3726
 authfail:
3727 3728 3729
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3730 3731
    goto error;

3732
 authdeny:
3733
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3734 3735 3736
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3737 3738
    goto error;

3739
 error:
3740
    virObjectUnref(priv->sasl);
3741 3742
    priv->sasl = NULL;
    virResetLastError();
3743 3744
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3745
    virNetMessageSaveError(rerr);
3746
    virMutexUnlock(&priv->lock);
3747
    return -1;
3748 3749 3750 3751
}


static int
3752
remoteDispatchAuthSaslStep(virNetServerPtr server,
3753
                           virNetServerClientPtr client,
3754
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3755
                           virNetMessageErrorPtr rerr,
3756 3757
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
3758 3759
{
    const char *serverout;
3760
    size_t serveroutlen;
3761
    int err;
3762 3763
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3764
    const char *identity;
3765

3766 3767 3768 3769 3770
    virMutexLock(&priv->lock);

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

3775
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
3776
              args->data.data_len, args->nil);
3777 3778 3779 3780 3781 3782 3783 3784
    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)
3785
        goto authfail;
3786 3787

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3788
        VIR_ERROR(_("sasl step reply data too long %d"),
3789
                  (int)serveroutlen);
3790
        goto authfail;
3791 3792 3793 3794
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3795 3796
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3797 3798 3799 3800 3801 3802 3803
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3804
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3805
    if (err == VIR_NET_SASL_CONTINUE) {
3806 3807
        ret->complete = 0;
    } else {
3808
        /* Check username whitelist ACL */
3809
        if ((err = remoteSASLFinish(server, client)) < 0) {
3810 3811 3812 3813 3814
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3815

3816 3817 3818
        ret->complete = 1;
    }

3819
    virMutexUnlock(&priv->lock);
3820
    return 0;
3821

3822
 authfail:
3823 3824 3825
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3826 3827
    goto error;

3828
 authdeny:
3829
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3830 3831 3832
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3833 3834
    goto error;

3835
 error:
3836
    virObjectUnref(priv->sasl);
3837 3838
    priv->sasl = NULL;
    virResetLastError();
3839 3840
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3841
    virNetMessageSaveError(rerr);
3842
    virMutexUnlock(&priv->lock);
3843 3844
    return -1;
}
3845 3846 3847 3848
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3849
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3850 3851 3852 3853
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3854 3855
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3856 3857 3858 3859 3860 3861
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
3862
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3863 3864 3865 3866 3867
                            virNetMessageErrorPtr rerr,
                            remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3868 3869
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3870 3871 3872 3873 3874 3875
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3876
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3877 3878 3879 3880 3881
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                           remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3882 3883
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3884 3885 3886 3887
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
3888 3889 3890



3891
static int
3892
remoteDispatchAuthPolkit(virNetServerPtr server,
3893
                         virNetServerClientPtr client,
3894
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3895
                         virNetMessageErrorPtr rerr,
3896
                         remote_auth_polkit_ret *ret)
3897
{
3898
    pid_t callerPid = -1;
3899
    gid_t callerGid = -1;
3900
    uid_t callerUid = -1;
3901
    unsigned long long timestamp;
3902
    const char *action;
3903
    char *ident = NULL;
3904 3905
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3906
    int rv;
3907

3908 3909
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
3910 3911 3912
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

3913 3914
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3915
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3916 3917 3918
        goto authfail;
    }

3919
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
3920
                                          &callerPid, &timestamp) < 0) {
3921 3922 3923
        goto authfail;
    }

3924 3925 3926 3927 3928 3929
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

3930 3931
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3932

3933 3934 3935 3936 3937 3938 3939
    rv = virPolkitCheckAuth(action,
                            callerPid,
                            timestamp,
                            callerUid,
                            NULL,
                            true);
    if (rv == -1)
3940
        goto authfail;
3941
    else if (rv == -2)
3942
        goto authdeny;
3943

3944 3945 3946
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3947 3948
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
3949 3950
    ret->complete = 1;

3951
    virNetServerSetClientAuthenticated(server, client);
3952
    virMutexUnlock(&priv->lock);
3953

3954
    return 0;
3955

3956
 error:
3957
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3958
    virMutexUnlock(&priv->lock);
3959 3960
    return -1;

3961
 authfail:
3962 3963 3964
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3965
    goto error;
3966

3967
 authdeny:
3968 3969 3970
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3971
    goto error;
3972 3973
}

3974

3975
static int
3976
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
3977
                                  virNetServerClientPtr client,
3978
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3979
                                  virNetMessageErrorPtr rerr,
3980 3981
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3982
{
3983 3984
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3985
    int rv = -1;
3986 3987
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3988

3989
    if (!priv->nodedevConn) {
3990
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3991
        goto cleanup;
3992 3993
    }

3994
    if (!(dev = virNodeDeviceLookupByName(priv->nodedevConn, args->name)))
3995 3996
        goto cleanup;

3997 3998 3999
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
4000
        ret->parentName = NULL;
4001 4002
    } else {
        /* remoteDispatchClientRequest will free this. */
4003 4004
        if (VIR_ALLOC(ret->parentName) < 0 ||
            VIR_STRDUP(*(ret->parentName), parent) < 0)
4005 4006 4007
            goto cleanup;
    }

4008 4009
    rv = 0;

4010
 cleanup:
4011
    if (rv < 0)
4012
        virNetMessageSaveError(rerr);
4013
    virObjectUnref(dev);
4014
    return rv;
4015 4016
}

4017
static int
4018
remoteDispatchConnectRegisterCloseCallback(virNetServerPtr server ATTRIBUTE_UNUSED,
4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035
                                           virNetServerClientPtr client,
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    virMutexLock(&priv->lock);

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

    if (virConnectRegisterCloseCallback(priv->conn,
                                        remoteRelayConnectionClosedEvent,
4036
                                        client, NULL) < 0)
4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049
        goto cleanup;

    priv->closeRegistered = true;
    rv = 0;

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

static int
4050
remoteDispatchConnectUnregisterCloseCallback(virNetServerPtr server ATTRIBUTE_UNUSED,
4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078
                                             virNetServerClientPtr client,
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    virMutexLock(&priv->lock);

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

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

    priv->closeRegistered = false;
    rv = 0;

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

static int
4081
remoteDispatchConnectDomainEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
4082
                                         virNetServerClientPtr client,
4083 4084 4085
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                         remote_connect_domain_event_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
4086
{
4087
    int callbackID;
4088
    int rv = -1;
4089 4090
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
4091 4092
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
4093

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

4099 4100
    virMutexLock(&priv->lock);

4101 4102 4103 4104
    /* 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
4105 4106 4107 4108
     * our callback; or you can use VIR_APPEND_ELEMENT_COPY to avoid
     * clearing 'callback' and having to juggle the pointer
     * between 'ref' and 'callback'.
     */
4109 4110
    if (VIR_ALLOC(callback) < 0)
        goto cleanup;
4111
    callback->client = virObjectRef(client);
4112 4113
    callback->eventID = VIR_DOMAIN_EVENT_ID_LIFECYCLE;
    callback->callbackID = -1;
4114
    callback->legacy = true;
4115 4116 4117 4118
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
4119
        goto cleanup;
O
Osier Yang 已提交
4120

4121
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
4122 4123 4124
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
4125 4126 4127 4128 4129
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
4130
        goto cleanup;
4131
    }
O
Osier Yang 已提交
4132

4133
    ref->callbackID = callbackID;
4134

4135 4136
    rv = 0;

4137
 cleanup:
4138
    remoteEventCallbackFree(callback);
4139
    if (rv < 0)
4140 4141
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
4142
    return rv;
O
Osier Yang 已提交
4143 4144
}

4145
static int
4146
remoteDispatchConnectDomainEventDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
4147
                                           virNetServerClientPtr client,
4148 4149 4150
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                           remote_connect_domain_event_deregister_ret *ret ATTRIBUTE_UNUSED)
4151
{
4152
    int callbackID = -1;
4153
    int rv = -1;
4154
    size_t i;
4155 4156
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4157

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

4163 4164
    virMutexLock(&priv->lock);

4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175
    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);
4176
        goto cleanup;
4177
    }
4178

4179
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
4180
        goto cleanup;
4181

4182 4183
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
4184

4185 4186
    rv = 0;

4187
 cleanup:
4188
    if (rv < 0)
4189 4190
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
4191 4192 4193 4194
    return rv;
}

static void
4195
remoteDispatchObjectEventSend(virNetServerClientPtr client,
4196
                              virNetServerProgramPtr program,
4197 4198 4199 4200
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
4201
    virNetMessagePtr msg;
4202

4203
    if (!(msg = virNetMessageNew(false)))
4204
        goto cleanup;
4205

4206 4207 4208 4209 4210 4211
    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;
4212

4213
    if (virNetMessageEncodeHeader(msg) < 0)
4214 4215
        goto cleanup;

4216 4217
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
4218

4219
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
4220 4221
    if (virNetServerClientSendMessage(client, msg) < 0)
        goto cleanup;
4222

4223
    xdr_free(proc, data);
4224 4225
    return;

4226
 cleanup:
4227
    virNetMessageFree(msg);
4228
    xdr_free(proc, data);
4229 4230
}

4231
static int
4232
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
4233
                             virNetServerClientPtr client,
4234
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
4235
                             virNetMessageErrorPtr rerr,
4236 4237
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
4238
{
4239 4240 4241
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
4242
    int rv = -1;
4243 4244
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4245

4246
    if (!priv->secretConn) {
4247
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4248
        goto cleanup;
4249 4250
    }

4251
    if (!(secret = get_nonnull_secret(priv->secretConn, args->secret)))
4252
        goto cleanup;
4253

4254
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
4255
        goto cleanup;
4256

4257 4258 4259
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

4260 4261
    rv = 0;

4262
 cleanup:
4263
    if (rv < 0)
4264
        virNetMessageSaveError(rerr);
4265
    virObjectUnref(secret);
4266
    return rv;
4267 4268
}

4269
static int
4270
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
4271
                             virNetServerClientPtr client,
4272
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
4273
                             virNetMessageErrorPtr rerr,
4274 4275 4276 4277 4278
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
4279 4280
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4281

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

4287
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4288 4289 4290 4291 4292 4293 4294
        goto cleanup;

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

    rv = 0;

4295
 cleanup:
4296
    if (rv < 0)
4297
        virNetMessageSaveError(rerr);
4298
    virObjectUnref(dom);
4299 4300 4301
    return rv;
}

4302 4303 4304 4305 4306 4307

/* 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.  */
4308
static int
4309
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
4310
                                            virNetServerClientPtr client,
4311 4312 4313
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            remote_connect_domain_event_register_any_args *args)
4314 4315
{
    int callbackID;
4316
    int rv = -1;
4317 4318
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
4319 4320
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4321

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

4327 4328
    virMutexLock(&priv->lock);

4329 4330 4331 4332
    /* 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 ||
4333
        args->eventID < 0) {
4334 4335
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
4336
        goto cleanup;
4337 4338
    }

4339 4340 4341 4342 4343 4344 4345 4346
    /* 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;
4347
    callback->client = virObjectRef(client);
4348 4349
    callback->eventID = args->eventID;
    callback->callbackID = -1;
4350
    callback->legacy = true;
4351 4352 4353 4354
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
4355
        goto cleanup;
4356

4357
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
4358 4359 4360
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
4361 4362 4363 4364 4365
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
4366
        goto cleanup;
4367
    }
4368

4369
    ref->callbackID = callbackID;
4370

4371 4372
    rv = 0;

4373
 cleanup:
4374
    remoteEventCallbackFree(callback);
4375
    if (rv < 0)
4376 4377
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
4378
    return rv;
4379 4380 4381
}


4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408
static int
remoteDispatchConnectDomainEventCallbackRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                    virNetServerClientPtr client,
                                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                    virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                                    remote_connect_domain_event_callback_register_any_args *args,
                                                    remote_connect_domain_event_callback_register_any_ret *ret)
{
    int callbackID;
    int rv = -1;
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

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

    virMutexLock(&priv->lock);

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

4409
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST || args->eventID < 0) {
4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422
        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;
4423
    callback->client = virObjectRef(client);
4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
        goto cleanup;

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

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

    rv = 0;

4449
 cleanup:
4450
    remoteEventCallbackFree(callback);
4451 4452
    if (rv < 0)
        virNetMessageSaveError(rerr);
4453
    virObjectUnref(dom);
4454 4455 4456 4457 4458
    virMutexUnlock(&priv->lock);
    return rv;
}


4459
static int
4460
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
4461
                                              virNetServerClientPtr client,
4462 4463 4464
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              remote_connect_domain_event_deregister_any_args *args)
4465 4466
{
    int callbackID = -1;
4467
    int rv = -1;
4468
    size_t i;
4469 4470
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4471

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

4477 4478
    virMutexLock(&priv->lock);

4479 4480 4481 4482
    /* 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 ||
4483
        args->eventID < 0) {
4484 4485
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
4486
        goto cleanup;
4487 4488
    }

4489 4490 4491 4492 4493 4494
    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->eventID == args->eventID) {
            callbackID = priv->domainEventCallbacks[i]->callbackID;
            break;
        }
    }
4495
    if (callbackID < 0) {
4496 4497
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event %d not registered"), args->eventID);
4498
        goto cleanup;
4499 4500
    }

4501
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
4502
        goto cleanup;
4503

4504 4505
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
4506

4507 4508
    rv = 0;

4509
 cleanup:
4510
    if (rv < 0)
4511 4512
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
4513
    return rv;
4514 4515
}

4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554

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

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

    virMutexLock(&priv->lock);

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

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

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

    rv = 0;

4555
 cleanup:
4556 4557 4558 4559 4560 4561 4562
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}


C
Chris Lalancette 已提交
4563
static int
4564
qemuDispatchDomainMonitorCommand(virNetServerPtr server ATTRIBUTE_UNUSED,
4565
                                 virNetServerClientPtr client,
4566 4567 4568 4569
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                 virNetMessageErrorPtr rerr,
                                 qemu_domain_monitor_command_args *args,
                                 qemu_domain_monitor_command_ret *ret)
C
Chris Lalancette 已提交
4570
{
4571
    virDomainPtr dom = NULL;
4572
    int rv = -1;
4573 4574
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
4575

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

4581
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4582
        goto cleanup;
C
Chris Lalancette 已提交
4583

4584
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
4585
                                    args->flags) < 0)
4586
        goto cleanup;
C
Chris Lalancette 已提交
4587

4588
    rv = 0;
C
Chris Lalancette 已提交
4589

4590
 cleanup:
4591
    if (rv < 0)
4592
        virNetMessageSaveError(rerr);
4593
    virObjectUnref(dom);
4594
    return rv;
C
Chris Lalancette 已提交
4595 4596
}

4597

4598
static int
4599
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
4600
                                  virNetServerClientPtr client,
4601
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
4602
                                  virNetMessageErrorPtr rerr,
4603 4604 4605 4606 4607 4608
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
4609
    char *xmlin;
4610 4611 4612
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4613 4614
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4615

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

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

4624
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4625 4626
    dname = args->dname == NULL ? NULL : *args->dname;

4627
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640
                                       &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;

4641
 cleanup:
4642
    if (rv < 0)
4643
        virNetMessageSaveError(rerr);
4644
    virObjectUnref(dom);
4645 4646 4647 4648 4649
    return rv;
}


static int
4650
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
4651
                                    virNetServerClientPtr client,
4652
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4653
                                    virNetMessageErrorPtr rerr,
4654 4655 4656 4657 4658 4659 4660 4661 4662
                                    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;
4663 4664
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4665

4666
    if (!priv->conn) {
4667
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4668 4669 4670 4671 4672 4673 4674
        goto cleanup;
    }

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

    /* Wacky world of XDR ... */
4675
    if (VIR_ALLOC(uri_out) < 0)
4676 4677
        goto cleanup;

4678
    if (virDomainMigratePrepare3(priv->conn,
4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695
                                 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;

4696
 cleanup:
4697
    if (rv < 0) {
4698
        virNetMessageSaveError(rerr);
4699 4700 4701 4702 4703
        VIR_FREE(uri_out);
    }
    return rv;
}

4704

4705
static int
4706
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
4707
                                    virNetServerClientPtr client,
4708
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4709
                                    virNetMessageErrorPtr rerr,
4710 4711 4712 4713
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
4714
    char *xmlin;
4715
    char *dname;
4716 4717
    char *uri;
    char *dconnuri;
4718 4719 4720
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4721 4722
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4723

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

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

4732
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4733
    dname = args->dname == NULL ? NULL : *args->dname;
4734 4735
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
4736

4737
    if (virDomainMigratePerform3(dom, xmlin,
4738 4739 4740
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
4741
                                 dconnuri, uri,
4742 4743 4744 4745 4746 4747 4748 4749 4750 4751
                                 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;

4752
 cleanup:
4753
    if (rv < 0)
4754
        virNetMessageSaveError(rerr);
4755
    virObjectUnref(dom);
4756 4757 4758 4759 4760
    return rv;
}


static int
4761
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
4762
                                   virNetServerClientPtr client,
4763
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
4764
                                   virNetMessageErrorPtr rerr,
4765 4766 4767 4768 4769 4770
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
4771 4772
    char *uri;
    char *dconnuri;
4773
    int rv = -1;
4774 4775
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4776

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

4782 4783 4784
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

4785
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
4786 4787 4788 4789 4790 4791
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
4792 4793
        goto cleanup;

4794 4795
    if (make_nonnull_domain(&ret->dom, dom) < 0)
        goto cleanup;
4796 4797 4798 4799 4800 4801 4802 4803

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

    rv = 0;

4804
 cleanup:
4805
    if (rv < 0) {
4806
        virNetMessageSaveError(rerr);
4807 4808
        VIR_FREE(cookieout);
    }
4809
    virObjectUnref(dom);
4810 4811 4812 4813 4814
    return rv;
}


static int
4815
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
4816
                                    virNetServerClientPtr client,
4817
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4818 4819
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
4820 4821 4822
{
    virDomainPtr dom = NULL;
    int rv = -1;
4823 4824
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4825

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

4831
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4832 4833 4834 4835 4836 4837 4838 4839 4840 4841
        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;

4842
 cleanup:
4843
    if (rv < 0)
4844
        virNetMessageSaveError(rerr);
4845
    virObjectUnref(dom);
4846 4847 4848 4849
    return rv;
}


4850 4851 4852 4853 4854 4855
static int remoteDispatchConnectSupportsFeature(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client,
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                virNetMessageErrorPtr rerr,
                                                remote_connect_supports_feature_args *args,
                                                remote_connect_supports_feature_ret *ret)
4856 4857
{
    int rv = -1;
4858
    int supported = -1;
4859 4860 4861
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

4862 4863 4864 4865 4866 4867 4868 4869 4870 4871
    /* 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;
    }

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

4877
    switch ((virDrvFeature) args->feature) {
4878
    case VIR_DRV_FEATURE_FD_PASSING:
4879
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
4880
    case VIR_DRV_FEATURE_REMOTE_CLOSE_CALLBACK:
4881 4882
        supported = 1;
        break;
4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893
    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:
4894
    default:
4895
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
4896 4897
            goto cleanup;
        break;
4898 4899 4900
    case VIR_DRV_FEATURE_PROGRAM_KEEPALIVE:
        /* should not be possible! */
        goto cleanup;
4901 4902
    }

4903
 done:
4904 4905 4906
    ret->supported = supported;
    rv = 0;

4907
 cleanup:
4908 4909 4910 4911 4912 4913
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


4914 4915
static int
remoteDispatchDomainOpenGraphics(virNetServerPtr server ATTRIBUTE_UNUSED,
4916
                                 virNetServerClientPtr client,
4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927
                                 virNetMessagePtr msg,
                                 virNetMessageErrorPtr rerr,
                                 remote_domain_open_graphics_args *args)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    int fd = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4928
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945
        goto cleanup;
    }

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

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

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

    rv = 0;

4946
 cleanup:
4947 4948 4949
    VIR_FORCE_CLOSE(fd);
    if (rv < 0)
        virNetMessageSaveError(rerr);
4950
    virObjectUnref(dom);
4951 4952 4953
    return rv;
}

4954

4955 4956
static int
remoteDispatchDomainOpenGraphicsFd(virNetServerPtr server ATTRIBUTE_UNUSED,
4957
                                   virNetServerClientPtr client,
4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975
                                   virNetMessagePtr msg,
                                   virNetMessageErrorPtr rerr,
                                   remote_domain_open_graphics_fd_args *args)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    int fd = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

4976 4977 4978
    if ((fd = virDomainOpenGraphicsFD(dom,
                                      args->idx,
                                      args->flags)) < 0)
4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989
        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);
4990
    if (rv < 0)
4991 4992
        virNetMessageSaveError(rerr);

4993
    virObjectUnref(dom);
4994 4995
    return rv;
}
4996 4997


4998 4999
static int
remoteDispatchDomainGetInterfaceParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
5000
                                           virNetServerClientPtr client,
5001 5002 5003 5004 5005 5006 5007 5008
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr,
                                           remote_domain_get_interface_parameters_args *args,
                                           remote_domain_get_interface_parameters_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    const char *device = args->device;
5009
    int nparams = 0;
5010 5011 5012 5013 5014 5015
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
5016
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
5017 5018 5019 5020 5021
        goto cleanup;
    }

    flags = args->flags;

5022
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
5023
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
5024 5025
        goto cleanup;
    }
5026
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
5027
        goto cleanup;
5028
    nparams = args->nparams;
5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043

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

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

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

5044 5045 5046 5047
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                flags) < 0)
5048 5049
        goto cleanup;

5050
 success:
5051 5052
    rv = 0;

5053
 cleanup:
5054 5055
    if (rv < 0)
        virNetMessageSaveError(rerr);
5056
    virTypedParamsFree(params, nparams);
5057
    virObjectUnref(dom);
5058 5059
    return rv;
}
5060

5061 5062
static int
remoteDispatchDomainGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
5063
                                virNetServerClientPtr client,
5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076
                                virNetMessagePtr hdr ATTRIBUTE_UNUSED,
                                virNetMessageErrorPtr rerr,
                                remote_domain_get_cpu_stats_args *args,
                                remote_domain_get_cpu_stats_ret *ret)
{
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv;
    virTypedParameterPtr params = NULL;
    int rv = -1;
    int percpu_len = 0;

    priv = virNetServerClientGetPrivateData(client);
    if (!priv->conn) {
5077
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
5078 5079 5080 5081
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
5082
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
5083 5084 5085
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
5086
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
5087 5088 5089 5090
        goto cleanup;
    }

    if (args->nparams > 0 &&
5091
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105
        goto cleanup;

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

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

5106 5107 5108 5109
    if (virTypedParamsSerialize(params, args->nparams * args->ncpus,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
5110 5111
        goto cleanup;

5112
 success:
5113 5114
    rv = 0;
    ret->nparams = percpu_len;
5115
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
5116
        size_t i;
5117 5118 5119 5120 5121 5122

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

5124
 cleanup:
5125 5126
    if (rv < 0)
         virNetMessageSaveError(rerr);
5127
    virTypedParamsFree(params, args->ncpus * args->nparams);
5128
    virObjectUnref(dom);
5129 5130 5131
    return rv;
}

5132 5133 5134 5135 5136 5137 5138
static int
remoteDispatchDomainGetDiskErrors(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client,
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                  virNetMessageErrorPtr rerr,
                                  remote_domain_get_disk_errors_args *args,
                                  remote_domain_get_disk_errors_ret *ret)
5139 5140 5141 5142
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
5143
    int len = 0;
5144 5145 5146 5147
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
5148
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
5149 5150 5151 5152 5153 5154 5155
        goto cleanup;
    }

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
5156 5157
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
5158 5159 5160 5161
        goto cleanup;
    }

    if (args->maxerrors &&
5162
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178
        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;

5179
 cleanup:
5180 5181
    if (rv < 0)
        virNetMessageSaveError(rerr);
5182
    virObjectUnref(dom);
5183
    if (errors && len > 0) {
5184
        size_t i;
5185 5186 5187 5188 5189 5190 5191
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

5192

5193 5194
static int
remoteDispatchNodeGetSevInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
5195
                             virNetServerClientPtr client,
5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                             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;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

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


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

    rv = 0;

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


5237 5238
static int
remoteDispatchNodeGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
5239
                                      virNetServerClientPtr client,
5240 5241 5242 5243 5244 5245
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_node_get_memory_parameters_args *args,
                                      remote_node_get_memory_parameters_ret *ret)
{
    virTypedParameterPtr params = NULL;
5246
    int nparams = 0;
5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    flags = args->flags;

5259
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
5260 5261 5262
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
5263
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
5264
        goto cleanup;
5265
    nparams = args->nparams;
5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277

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

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

5278 5279 5280 5281
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
5282 5283
        goto cleanup;

5284
 success:
5285 5286
    rv = 0;

5287
 cleanup:
5288 5289
    if (rv < 0)
        virNetMessageSaveError(rerr);
5290
    virTypedParamsFree(params, nparams);
5291 5292 5293
    return rv;
}

5294 5295
static int
remoteDispatchNodeGetCPUMap(virNetServerPtr server ATTRIBUTE_UNUSED,
5296
                            virNetServerClientPtr client,
5297 5298 5299 5300 5301 5302
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                            virNetMessageErrorPtr rerr,
                            remote_node_get_cpu_map_args *args,
                            remote_node_get_cpu_map_ret *ret)
{
    unsigned char *cpumap = NULL;
5303
    unsigned int online = 0;
5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316
    unsigned int flags;
    int cpunum;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    flags = args->flags;

5317 5318
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
5319 5320 5321 5322
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
5323
    if (args->need_map) {
5324 5325 5326 5327 5328 5329 5330 5331 5332 5333
        ret->cpumap.cpumap_len = VIR_CPU_MAPLEN(cpunum);
        ret->cpumap.cpumap_val = (char *) cpumap;
        cpumap = NULL;
    }

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

    rv = 0;

5334
 cleanup:
5335 5336 5337 5338 5339 5340
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumap);
    return rv;
}

5341 5342
static int
lxcDispatchDomainOpenNamespace(virNetServerPtr server ATTRIBUTE_UNUSED,
5343
                               virNetServerClientPtr client,
5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                               virNetMessageErrorPtr rerr,
                               lxc_domain_open_namespace_args *args)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *fdlist = NULL;
    int ret;
    virDomainPtr dom = NULL;
    size_t i;

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

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

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

    /* We shouldn't have received any from the client,
     * but in case they're playing games with us, prevent
     * a resource leak
     */
5374
    for (i = 0; i < msg->nfds; i++)
5375 5376 5377 5378 5379 5380 5381 5382 5383
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

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

    rv = 1;

5384
 cleanup:
5385 5386
    if (rv < 0)
        virNetMessageSaveError(rerr);
5387
    virObjectUnref(dom);
5388 5389 5390
    return rv;
}

5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417
static int
remoteDispatchDomainGetJobStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client,
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                virNetMessageErrorPtr rerr,
                                remote_domain_get_job_stats_args *args,
                                remote_domain_get_job_stats_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

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

5418 5419 5420 5421 5422 5423 5424
    if (nparams > REMOTE_DOMAIN_JOB_STATS_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many job stats '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_JOB_STATS_MAX);
        goto cleanup;
    }

5425 5426 5427 5428
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                0) < 0)
5429 5430 5431 5432
        goto cleanup;

    rv = 0;

5433
 cleanup:
5434 5435 5436
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virTypedParamsFree(params, nparams);
5437
    virObjectUnref(dom);
5438 5439 5440
    return rv;
}

5441
static int
5442
remoteDispatchDomainMigrateBegin3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
5443
                                        virNetServerClientPtr client,
5444 5445 5446 5447
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_migrate_begin3_params_args *args,
                                        remote_domain_migrate_begin3_params_ret *ret)
5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

5464 5465 5466 5467 5468 5469 5470
    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;
    }

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

5474 5475 5476
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489
        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;

5490
 cleanup:
5491 5492 5493
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5494
    virObjectUnref(dom);
5495 5496 5497 5498
    return rv;
}

static int
5499
remoteDispatchDomainMigratePrepare3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
5500
                                          virNetServerClientPtr client,
5501 5502 5503 5504
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_prepare3_params_args *args,
                                          remote_domain_migrate_prepare3_params_ret *ret)
5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    char **uri_out;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

5520 5521 5522 5523 5524 5525 5526
    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;
    }

5527 5528 5529
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5530 5531 5532
        goto cleanup;

    /* Wacky world of XDR ... */
5533
    if (VIR_ALLOC(uri_out) < 0)
5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548
        goto cleanup;

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

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

    rv = 0;

5549
 cleanup:
5550 5551 5552 5553 5554 5555 5556 5557 5558
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(uri_out);
    }
    return rv;
}

static int
5559 5560 5561 5562 5563 5564
remoteDispatchDomainMigratePrepareTunnel3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client,
                                                virNetMessagePtr msg,
                                                virNetMessageErrorPtr rerr,
                                                remote_domain_migrate_prepare_tunnel3_params_args *args,
                                                remote_domain_migrate_prepare_tunnel3_params_ret *ret)
5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virStreamPtr st = NULL;
    daemonClientStreamPtr stream = NULL;

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

5581 5582 5583 5584 5585 5586 5587
    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;
    }

5588 5589 5590
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5591 5592 5593 5594
        goto cleanup;

    if (!(st = virStreamNew(priv->conn, VIR_STREAM_NONBLOCK)) ||
        !(stream = daemonCreateClientStream(client, st, remoteProgram,
5595
                                            &msg->header, false)))
5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611
        goto cleanup;

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

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

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

5612
 cleanup:
5613 5614 5615 5616 5617 5618 5619 5620
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
        if (stream) {
            virStreamAbort(st);
            daemonFreeClientStream(client, stream);
        } else {
5621
            virObjectUnref(st);
5622 5623 5624 5625 5626 5627 5628
        }
    }
    return rv;
}


static int
5629
remoteDispatchDomainMigratePerform3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
5630
                                          virNetServerClientPtr client,
5631 5632 5633 5634
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_perform3_params_args *args,
                                          remote_domain_migrate_perform3_params_ret *ret)
5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    char *dconnuri;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

5661 5662 5663
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679
        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;

5680
 cleanup:
5681 5682 5683
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5684
    virObjectUnref(dom);
5685 5686 5687 5688 5689
    return rv;
}


static int
5690
remoteDispatchDomainMigrateFinish3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
5691
                                         virNetServerClientPtr client,
5692 5693 5694 5695
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_domain_migrate_finish3_params_args *args,
                                         remote_domain_migrate_finish3_params_ret *ret)
5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

5711 5712 5713 5714 5715 5716 5717
    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;
    }

5718 5719 5720
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5721 5722 5723 5724 5725 5726 5727 5728 5729 5730
        goto cleanup;

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

5731 5732
    if (make_nonnull_domain(&ret->dom, dom) < 0)
        goto cleanup;
5733 5734 5735 5736 5737 5738

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

    rv = 0;

5739
 cleanup:
5740 5741 5742 5743 5744
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
    }
5745
    virObjectUnref(dom);
5746 5747 5748 5749 5750
    return rv;
}


static int
5751
remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
5752
                                          virNetServerClientPtr client,
5753 5754 5755
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_confirm3_params_args *args)
5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768
{
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virDomainPtr dom = NULL;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

5769 5770 5771 5772 5773 5774 5775
    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;
    }

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

5779 5780 5781
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5782 5783 5784 5785 5786 5787 5788 5789 5790 5791
        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;

5792
 cleanup:
5793 5794 5795
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5796
    virObjectUnref(dom);
5797 5798 5799 5800
    return rv;
}


5801 5802
static int
remoteDispatchConnectGetCPUModelNames(virNetServerPtr server ATTRIBUTE_UNUSED,
5803
                                      virNetServerClientPtr client,
5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_connect_get_cpu_model_names_args *args,
                                      remote_connect_get_cpu_model_names_ret *ret)
{
    int len, rv = -1;
    char **models = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

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

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

    ret->ret = len;

    rv = 0;

5845
 cleanup:
5846 5847
    if (rv < 0)
        virNetMessageSaveError(rerr);
5848
    virStringListFree(models);
5849 5850 5851 5852
    return rv;
}


5853 5854 5855 5856 5857 5858 5859
static int
remoteDispatchDomainCreateXMLWithFiles(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_create_xml_with_files_args *args,
                                       remote_domain_create_xml_with_files_ret *ret)
5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886
{
    int rv = -1;
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *files = NULL;
    unsigned int nfiles = 0;
    size_t i;

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

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

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

5887 5888 5889
    if (make_nonnull_domain(&ret->dom, dom) < 0)
        goto cleanup;

5890 5891
    rv = 0;

5892
 cleanup:
5893
    for (i = 0; i < nfiles; i++)
5894 5895 5896 5897
        VIR_FORCE_CLOSE(files[i]);
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5898
    virObjectUnref(dom);
5899 5900 5901 5902
    return rv;
}


5903 5904 5905 5906 5907 5908
static int remoteDispatchDomainCreateWithFiles(virNetServerPtr server ATTRIBUTE_UNUSED,
                                               virNetServerClientPtr client,
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr,
                                               remote_domain_create_with_files_args *args,
                                               remote_domain_create_with_files_ret *ret)
5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938
{
    int rv = -1;
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *files = NULL;
    unsigned int nfiles = 0;
    size_t i;

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

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

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

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

5939 5940 5941
    if (make_nonnull_domain(&ret->dom, dom) < 0)
        goto cleanup;

5942 5943
    rv = 0;

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


5955 5956
static int
remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5957
                                             virNetServerClientPtr client,
5958 5959 5960
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                             remote_connect_network_event_register_any_args *args,
5961
                                             remote_connect_network_event_register_any_ret *ret)
5962 5963 5964
{
    int callbackID;
    int rv = -1;
5965 5966
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
5967 5968
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
5969
    virNetworkPtr net = NULL;
5970

5971
    if (!priv->networkConn) {
5972 5973 5974 5975 5976 5977
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

5978
    if (args->net &&
5979
        !(net = get_nonnull_network(priv->networkConn, *args->net)))
5980 5981
        goto cleanup;

5982 5983 5984
    if (args->eventID >= VIR_NETWORK_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network event ID %d"), args->eventID);
5985 5986 5987
        goto cleanup;
    }

5988 5989 5990 5991 5992 5993 5994 5995
    /* 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;
5996
    callback->client = virObjectRef(client);
5997 5998 5999 6000 6001 6002
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->networkEventCallbacks,
                           priv->nnetworkEventCallbacks,
                           callback) < 0)
6003 6004
        goto cleanup;

6005
    if ((callbackID = virConnectNetworkEventRegisterAny(priv->networkConn,
6006
                                                        net,
6007 6008
                                                        args->eventID,
                                                        networkEventCallbacks[args->eventID],
6009 6010 6011 6012 6013
                                                        ref,
                                                        remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->networkEventCallbacks,
                     priv->nnetworkEventCallbacks, 1);
        callback = ref;
6014
        goto cleanup;
6015
    }
6016

6017
    ref->callbackID = callbackID;
6018
    ret->callbackID = callbackID;
6019 6020 6021

    rv = 0;

6022
 cleanup:
6023
    remoteEventCallbackFree(callback);
6024 6025
    if (rv < 0)
        virNetMessageSaveError(rerr);
6026
    virObjectUnref(net);
6027 6028 6029 6030 6031 6032 6033
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
6034
                                               virNetServerClientPtr client,
6035 6036
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
6037
                                               remote_connect_network_event_deregister_any_args *args)
6038 6039
{
    int rv = -1;
6040
    size_t i;
6041 6042 6043
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

6044
    if (!priv->networkConn) {
6045 6046 6047 6048 6049 6050
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

6051
    for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
6052
        if (priv->networkEventCallbacks[i]->callbackID == args->callbackID)
6053 6054
            break;
    }
6055
    if (i == priv->nnetworkEventCallbacks) {
6056
        virReportError(VIR_ERR_INTERNAL_ERROR,
6057 6058
                       _("network event callback %d not registered"),
                       args->callbackID);
6059 6060 6061
        goto cleanup;
    }

6062
    if (virConnectNetworkEventDeregisterAny(priv->networkConn, args->callbackID) < 0)
6063 6064
        goto cleanup;

6065 6066
    VIR_DELETE_ELEMENT(priv->networkEventCallbacks, i,
                       priv->nnetworkEventCallbacks);
6067 6068 6069

    rv = 0;

6070
 cleanup:
6071 6072 6073 6074 6075 6076
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}

6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092
static int
remoteDispatchConnectStoragePoolEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                 virNetServerClientPtr client,
                                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                 virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                                 remote_connect_storage_pool_event_register_any_args *args,
                                                 remote_connect_storage_pool_event_register_any_ret *ret)
{
    int callbackID;
    int rv = -1;
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virStoragePoolPtr  pool = NULL;

6093
    if (!priv->storageConn) {
6094 6095 6096 6097 6098 6099 6100
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

    if (args->pool &&
6101
        !(pool = get_nonnull_storage_pool(priv->storageConn, *args->pool)))
6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117
        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;
6118
    callback->client = virObjectRef(client);
6119 6120 6121 6122 6123 6124 6125 6126
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->storageEventCallbacks,
                           priv->nstorageEventCallbacks,
                           callback) < 0)
        goto cleanup;

6127
    if ((callbackID = virConnectStoragePoolEventRegisterAny(priv->storageConn,
6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144
                                                            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:
6145
    remoteEventCallbackFree(callback);
6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virObjectUnref(pool);
    virMutexUnlock(&priv->lock);
    return rv;
}

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

6165
    if (!priv->storageConn) {
6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

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

6183
    if (virConnectStoragePoolEventDeregisterAny(priv->storageConn, args->callbackID) < 0)
6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197
        goto cleanup;

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

    rv = 0;

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

6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213
static int
remoteDispatchConnectNodeDeviceEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client,
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                                virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                                remote_connect_node_device_event_register_any_args *args,
                                                remote_connect_node_device_event_register_any_ret *ret)
{
    int callbackID;
    int rv = -1;
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virNodeDevicePtr  dev = NULL;

6214
    if (!priv->nodedevConn) {
6215 6216 6217 6218 6219 6220 6221
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

    if (args->dev &&
6222
        !(dev = get_nonnull_node_device(priv->nodedevConn, *args->dev)))
6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238
        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;
6239
    callback->client = virObjectRef(client);
6240 6241 6242 6243 6244 6245 6246 6247
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->nodeDeviceEventCallbacks,
                           priv->nnodeDeviceEventCallbacks,
                           callback) < 0)
        goto cleanup;

6248
    if ((callbackID = virConnectNodeDeviceEventRegisterAny(priv->nodedevConn,
6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265
                                                           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:
6266
    remoteEventCallbackFree(callback);
6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virObjectUnref(dev);
    virMutexUnlock(&priv->lock);
    return rv;
}

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

6286
    if (!priv->nodedevConn) {
6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

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

6304
    if (virConnectNodeDeviceEventDeregisterAny(priv->nodedevConn, args->callbackID) < 0)
6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317
        goto cleanup;

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

    rv = 0;

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

6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334
static int
remoteDispatchConnectSecretEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                            virNetServerClientPtr client,
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            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;

6335
    if (!priv->secretConn) {
6336 6337 6338 6339 6340 6341 6342
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

    if (args->secret &&
6343
        !(secret = get_nonnull_secret(priv->secretConn, *args->secret)))
6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359
        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;
6360
    callback->client = virObjectRef(client);
6361 6362 6363 6364 6365 6366 6367 6368
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->secretEventCallbacks,
                           priv->nsecretEventCallbacks,
                           callback) < 0)
        goto cleanup;

6369
    if ((callbackID = virConnectSecretEventRegisterAny(priv->secretConn,
6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386
                                                       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:
6387
    remoteEventCallbackFree(callback);
6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virObjectUnref(secret);
    virMutexUnlock(&priv->lock);
    return rv;
}

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

6407
    if (!priv->secretConn) {
6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    virMutexLock(&priv->lock);

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

6425
    if (virConnectSecretEventDeregisterAny(priv->secretConn, args->callbackID) < 0)
6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439
        goto cleanup;

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

    rv = 0;

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

6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475
static int
qemuDispatchConnectDomainMonitorEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                              virNetServerClientPtr client,
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              qemu_connect_domain_monitor_event_register_args *args,
                                              qemu_connect_domain_monitor_event_register_ret *ret)
{
    int callbackID;
    int rv = -1;
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;
    const char *event = args->event ? *args->event : NULL;

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

    virMutexLock(&priv->lock);

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

    /* If we call register first, we could append a complete callback
     * to our array, but on OOM append failure, we'd have to then hope
     * deregister works to undo our register.  So instead we append an
     * incomplete callback to our array, then register, then fix up
     * our callback; but since VIR_APPEND_ELEMENT clears 'callback' on
     * success, we use 'ref' to save a copy of the pointer.  */
    if (VIR_ALLOC(callback) < 0)
        goto cleanup;
6476
    callback->client = virObjectRef(client);
6477
    callback->eventID = -1;
6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->qemuEventCallbacks,
                           priv->nqemuEventCallbacks,
                           callback) < 0)
        goto cleanup;

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

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

    rv = 0;

6503
 cleanup:
6504
    remoteEventCallbackFree(callback);
6505 6506
    if (rv < 0)
        virNetMessageSaveError(rerr);
6507
    virObjectUnref(dom);
6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551
    virMutexUnlock(&priv->lock);
    return rv;
}


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

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

    virMutexLock(&priv->lock);

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

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

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

    rv = 0;

6552
 cleanup:
6553 6554 6555 6556 6557 6558
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}

6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591
static int
remoteDispatchDomainGetTime(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client,
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                            virNetMessageErrorPtr rerr,
                            remote_domain_get_time_args *args,
                            remote_domain_get_time_ret *ret)
{
    int rv = -1;
    virDomainPtr dom = NULL;
    long long seconds;
    unsigned int nseconds;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if (virDomainGetTime(dom, &seconds, &nseconds, args->flags) < 0)
        goto cleanup;

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

 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
6592
    virObjectUnref(dom);
6593 6594
    return rv;
}
6595

M
Michal Privoznik 已提交
6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643

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

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

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

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

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

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

 cleanup:
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(ret->counts.counts_val);
    }
    return rv;
6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658
}

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

6659
    if (VIR_STRDUP(lease_dst->iface, lease_src->iface) < 0 ||
J
Ján Tomko 已提交
6660
        VIR_STRDUP(lease_dst->ipaddr, lease_src->ipaddr) < 0)
6661 6662
        goto error;

J
Ján Tomko 已提交
6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687
    if (lease_src->mac) {
        if (VIR_ALLOC(mac_tmp) < 0 ||
            VIR_STRDUP(*mac_tmp, lease_src->mac) < 0)
            goto error;
    }
    if (lease_src->iaid) {
        if (VIR_ALLOC(iaid_tmp) < 0 ||
            VIR_STRDUP(*iaid_tmp, lease_src->iaid) < 0)
            goto error;
    }
    if (lease_src->hostname) {
        if (VIR_ALLOC(hostname_tmp) < 0 ||
            VIR_STRDUP(*hostname_tmp, lease_src->hostname) < 0)
            goto error;
    }
    if (lease_src->clientid) {
        if (VIR_ALLOC(clientid_tmp) < 0 ||
            VIR_STRDUP(*clientid_tmp, lease_src->clientid) < 0)
            goto error;
    }

    lease_dst->mac = mac_tmp;
    lease_dst->iaid = iaid_tmp;
    lease_dst->hostname = hostname_tmp;
    lease_dst->clientid = clientid_tmp;
6688 6689 6690 6691

    return 0;

 error:
J
Ján Tomko 已提交
6692 6693 6694 6695 6696 6697 6698 6699
    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);
6700 6701 6702 6703 6704
    VIR_FREE(mac_tmp);
    VIR_FREE(iaid_tmp);
    VIR_FREE(hostname_tmp);
    VIR_FREE(clientid_tmp);
    VIR_FREE(lease_dst->ipaddr);
6705
    VIR_FREE(lease_dst->iface);
6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724
    return -1;
}


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

6725
    if (!priv->networkConn) {
6726 6727 6728 6729
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

6730
    if (!(net = get_nonnull_network(priv->networkConn, args->net)))
6731 6732 6733
        goto cleanup;

    if ((nleases = virNetworkGetDHCPLeases(net,
6734
                                           args->mac ? *args->mac : NULL,
6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768
                                           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);
6769
    if (leases && nleases > 0)
6770 6771
        for (i = 0; i < nleases; i++)
            virNetworkDHCPLeaseFree(leases[i]);
6772
    VIR_FREE(leases);
6773
    virObjectUnref(net);
6774
    return rv;
M
Michal Privoznik 已提交
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 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823
static int
remoteDispatchConnectGetAllDomainStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_connect_get_all_domain_stats_args *args,
                                       remote_connect_get_all_domain_stats_ret *ret)
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainStatsRecordPtr *retStats = NULL;
    int nrecords = 0;
    virDomainPtr *doms = NULL;

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

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

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

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

    if (nrecords > REMOTE_CONNECT_GET_ALL_DOMAIN_STATS_MAX) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Number of domain stats records is %d, "
                         "which exceeds max limit: %d"),
C
Cole Robinson 已提交
6824
                       nrecords, REMOTE_CONNECT_GET_ALL_DOMAIN_STATS_MAX);
6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836
        goto cleanup;
    }

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

        ret->retStats.retStats_len = nrecords;

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

6837 6838
            if (make_nonnull_domain(&dst->dom, retStats[i]->dom) < 0)
                goto cleanup;
6839

6840 6841 6842 6843 6844
            if (virTypedParamsSerialize(retStats[i]->params,
                                        retStats[i]->nparams,
                                        (virTypedParameterRemotePtr *) &dst->params.params_val,
                                        &dst->params.params_len,
                                        VIR_TYPED_PARAM_STRING_OKAY) < 0)
6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858
                goto cleanup;
        }
    } else {
        ret->retStats.retStats_len = 0;
        ret->retStats.retStats_val = NULL;
    }

    rv = 0;

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

    virDomainStatsRecordListFree(retStats);
6859
    virObjectListFree(doms);
6860 6861 6862 6863 6864

    return rv;
}


M
Michal Privoznik 已提交
6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901
static int
remoteDispatchNodeAllocPages(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client,
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                             virNetMessageErrorPtr rerr,
                             remote_node_alloc_pages_args *args,
                             remote_node_alloc_pages_ret *ret)
{
    int rv = -1;
    int len;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    ret->ret = len;
    rv = 0;

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


6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004
static int
remoteDispatchDomainGetFSInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client,
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                              virNetMessageErrorPtr rerr,
                              remote_domain_get_fsinfo_args *args,
                              remote_domain_get_fsinfo_ret *ret)
{
    int rv = -1;
    size_t i, j;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainFSInfoPtr *info = NULL;
    virDomainPtr dom = NULL;
    remote_domain_fsinfo *dst;
    int ninfo = 0;
    size_t ndisk;

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

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

    if ((ninfo = virDomainGetFSInfo(dom, &info, args->flags)) < 0)
        goto cleanup;

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

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

        ret->info.info_len = ninfo;

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

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

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

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

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

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

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

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

    ret->ret = ninfo;

    rv = 0;

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

        if (ret->info.info_val && ninfo > 0) {
            for (i = 0; i < ninfo; i++) {
                dst = &ret->info.info_val[i];
                VIR_FREE(dst->mountpoint);
                if (dst->dev_aliases.dev_aliases_val) {
                    for (j = 0; j < dst->dev_aliases.dev_aliases_len; j++)
                        VIR_FREE(dst->dev_aliases.dev_aliases_val[j]);
                    VIR_FREE(dst->dev_aliases.dev_aliases_val);
                }
            }
            VIR_FREE(ret->info.info_val);
        }
    }
7005
    virObjectUnref(dom);
7006 7007 7008 7009 7010 7011 7012 7013 7014
    if (ninfo >= 0)
        for (i = 0; i < ninfo; i++)
            virDomainFSInfoFree(info[i]);
    VIR_FREE(info);

    return rv;
}


7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040
static int
remoteSerializeDomainInterface(virDomainInterfacePtr *ifaces,
                               unsigned int ifaces_count,
                               remote_domain_interface_addresses_ret *ret)
{
    size_t i, j;

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

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

    ret->ifaces.ifaces_len = ifaces_count;

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

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

7041 7042 7043
        if (iface->hwaddr &&
            (VIR_ALLOC(iface_ret->hwaddr) < 0 ||
             VIR_STRDUP(*iface_ret->hwaddr, iface->hwaddr) < 0))
7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078
            goto cleanup;

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

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

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

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

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

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

    return 0;

 cleanup:
    if (ret->ifaces.ifaces_val) {
        for (i = 0; i < ifaces_count; i++) {
            remote_domain_interface *iface_ret = &(ret->ifaces.ifaces_val[i]);
            VIR_FREE(iface_ret->name);
7079 7080 7081 7082
            if (iface_ret->hwaddr) {
                VIR_FREE(*iface_ret->hwaddr);
                VIR_FREE(iface_ret->hwaddr);
            }
7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143
            for (j = 0; j < iface_ret->addrs.addrs_len; j++) {
                remote_domain_ip_addr *ip_addr =
                    &(iface_ret->addrs.addrs_val[j]);
                VIR_FREE(ip_addr->addr);
            }
        }
        VIR_FREE(ret->ifaces.ifaces_val);
    }

    return -1;
}


static int
remoteDispatchDomainInterfaceAddresses(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_interface_addresses_args *args,
                                       remote_domain_interface_addresses_ret *ret)
{
    size_t i;
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainInterfacePtr *ifaces = NULL;
    int ifaces_count = 0;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if ((ifaces_count = virDomainInterfaceAddresses(dom, &ifaces, args->source, args->flags)) < 0)
        goto cleanup;

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

    rv = 0;

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

    virObjectUnref(dom);

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

    return rv;
}


7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157
static int
remoteDispatchStorageVolGetInfoFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_storage_vol_get_info_flags_args *args,
                                     remote_storage_vol_get_info_flags_ret *ret)
{
    int rv = -1;
    virStorageVolPtr vol = NULL;
    virStorageVolInfo tmp;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

7158
    if (!priv->storageConn) {
7159 7160 7161 7162
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

7163
    if (!(vol = get_nonnull_storage_vol(priv->storageConn, args->vol)))
7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181
        goto cleanup;

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

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

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


7182 7183
static int
remoteDispatchNetworkPortGetParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
7184
                                       virNetServerClientPtr client,
7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       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;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    if (!(port = get_nonnull_network_port(priv->networkConn, args->port)))
        goto cleanup;

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

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

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

    rv = 0;

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


7230 7231 7232 7233 7234 7235 7236 7237 7238
/*----- 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
7239
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
7240 7241 7242 7243
{
    /* Should we believe the domain.id sent by the client?  Maybe
     * this should be a check rather than an assignment? XXX
     */
7244
    return virGetDomain(conn, domain.name, BAD_CAST domain.uuid, domain.id);
7245 7246 7247
}

static virNetworkPtr
7248
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
7249
{
7250
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
7251 7252
}

7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265
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 已提交
7266
static virInterfacePtr
7267
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
7268
{
7269
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
7270 7271
}

7272
static virStoragePoolPtr
7273
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
7274
{
7275 7276
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
7277 7278 7279
}

static virStorageVolPtr
7280
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
7281 7282
{
    virStorageVolPtr ret;
7283 7284
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
7285 7286 7287
    return ret;
}

7288
static virSecretPtr
7289
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
7290
{
7291
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
7292 7293
}

7294
static virNWFilterPtr
7295
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
7296
{
7297
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
7298 7299
}

7300 7301 7302 7303 7304 7305
static virNWFilterBindingPtr
get_nonnull_nwfilter_binding(virConnectPtr conn, remote_nonnull_nwfilter_binding binding)
{
    return virGetNWFilterBinding(conn, binding.portdev, binding.filtername);
}

7306 7307 7308 7309 7310 7311
static virDomainCheckpointPtr
get_nonnull_domain_checkpoint(virDomainPtr dom, remote_nonnull_domain_checkpoint checkpoint)
{
    return virGetDomainCheckpoint(dom, checkpoint.name);
}

C
Chris Lalancette 已提交
7312
static virDomainSnapshotPtr
7313
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
7314
{
7315
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
7316 7317
}

7318 7319 7320 7321 7322 7323
static virNodeDevicePtr
get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev)
{
    return virGetNodeDevice(conn, dev.name);
}

7324
/* Make remote_nonnull_domain and remote_nonnull_network. */
7325
static int
7326
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
7327 7328
{
    dom_dst->id = dom_src->id;
7329 7330
    if (VIR_STRDUP(dom_dst->name, dom_src->name) < 0)
        return -1;
7331
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
7332
    return 0;
7333 7334
}

7335
static int
7336
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
7337
{
7338 7339
    if (VIR_STRDUP(net_dst->name, net_src->name) < 0)
        return -1;
7340
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
7341
    return 0;
7342 7343
}

7344 7345 7346 7347 7348 7349 7350 7351 7352 7353
static int
make_nonnull_network_port(remote_nonnull_network_port *port_dst, virNetworkPortPtr port_src)
{
    if (VIR_STRDUP(port_dst->net.name, port_src->net->name) < 0)
        return -1;
    memcpy(port_dst->net.uuid, port_src->net->uuid, VIR_UUID_BUFLEN);
    memcpy(port_dst->uuid, port_src->uuid, VIR_UUID_BUFLEN);
    return 0;
}

7354
static int
7355 7356
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
7357
{
7358 7359 7360 7361 7362 7363 7364
    if (VIR_STRDUP(interface_dst->name, interface_src->name) < 0)
        return -1;
    if (VIR_STRDUP(interface_dst->mac, interface_src->mac) < 0) {
        VIR_FREE(interface_dst->name);
        return -1;
    }
    return 0;
D
Daniel Veillard 已提交
7365 7366
}

7367
static int
7368
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
7369
{
7370 7371
    if (VIR_STRDUP(pool_dst->name, pool_src->name) < 0)
        return -1;
7372
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
7373
    return 0;
7374 7375
}

7376
static int
7377
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
7378
{
7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390
    if (VIR_STRDUP(vol_dst->pool, vol_src->pool) < 0)
        return -1;
    if (VIR_STRDUP(vol_dst->name, vol_src->name) < 0) {
        VIR_FREE(vol_dst->pool);
        return -1;
    }
    if (VIR_STRDUP(vol_dst->key, vol_src->key) < 0) {
        VIR_FREE(vol_dst->pool);
        VIR_FREE(vol_dst->name);
        return -1;
    }
    return 0;
7391
}
7392

7393
static int
7394
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
7395
{
7396 7397 7398
    if (VIR_STRDUP(dev_dst->name, dev_src->name) < 0)
        return -1;
    return 0;
7399
}
7400

7401
static int
7402
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
7403
{
7404
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
7405
    secret_dst->usageType = secret_src->usageType;
7406 7407 7408
    if (VIR_STRDUP(secret_dst->usageID, secret_src->usageID) < 0)
        return -1;
    return 0;
7409
}
7410

7411
static int
7412
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
7413
{
7414 7415
    if (VIR_STRDUP(nwfilter_dst->name, nwfilter_src->name) < 0)
        return -1;
7416
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
7417
    return 0;
7418
}
C
Chris Lalancette 已提交
7419

7420
static int
7421 7422
make_nonnull_nwfilter_binding(remote_nonnull_nwfilter_binding *binding_dst, virNWFilterBindingPtr binding_src)
{
7423 7424 7425 7426 7427 7428 7429
    if (VIR_STRDUP(binding_dst->portdev, binding_src->portdev) < 0)
        return -1;
    if (VIR_STRDUP(binding_dst->filtername, binding_src->filtername) < 0) {
        VIR_FREE(binding_dst->portdev);
        return -1;
    }
    return 0;
7430 7431
}

7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443
static int
make_nonnull_domain_checkpoint(remote_nonnull_domain_checkpoint *checkpoint_dst, virDomainCheckpointPtr checkpoint_src)
{
    if (VIR_STRDUP(checkpoint_dst->name, checkpoint_src->name) < 0)
        return -1;
    if (make_nonnull_domain(&checkpoint_dst->dom, checkpoint_src->domain) < 0) {
        VIR_FREE(checkpoint_dst->name);
        return -1;
    }
    return 0;
}

7444
static int
7445
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
7446
{
7447 7448 7449 7450 7451 7452 7453
    if (VIR_STRDUP(snapshot_dst->name, snapshot_src->name) < 0)
        return -1;
    if (make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain) < 0) {
        VIR_FREE(snapshot_dst->name);
        return -1;
    }
    return 0;
C
Chris Lalancette 已提交
7454
}
7455 7456 7457 7458 7459 7460 7461 7462

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;
7463
    size_t i = 0;
7464

7465
    if (VIR_ALLOC_N(val, nerrors) < 0)
7466
        goto error;
7467 7468

    for (i = 0; i < nerrors; i++) {
7469 7470
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
7471 7472 7473 7474 7475 7476 7477 7478
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

7479
 error:
7480
    if (val) {
7481
        size_t j;
7482 7483 7484 7485 7486 7487
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}