remote.c 216.8 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007-2015 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24
 *
 * Author: Richard W.M. Jones <rjones@redhat.com>
 */

#include <config.h>

25
#include "virerror.h"
26

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

#define VIR_FROM_THIS VIR_FROM_RPC
56

57 58
VIR_LOG_INIT("daemon.remote");

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

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

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

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

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

109 110 111 112 113 114 115
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
                               remote_typed_param **ret_params_val,
                               u_int *ret_params_len,
                               unsigned int flags);

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

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


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

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

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

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

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

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

162
 cleanup:
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    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);

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


196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
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);

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


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

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

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

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

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

261 262
    return 0;
}
263

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

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

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

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

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

    return 0;
}

299

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

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

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

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

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

    return 0;
}


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

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

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

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

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

    return 0;
}


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

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

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

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

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

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


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

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

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

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

    make_nonnull_domain(&data.dom, dom);
454

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

    return 0;
469

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


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

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

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

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

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

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

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

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

    return 0;
543

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

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

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

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

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

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

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

605

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

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

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

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

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

    return 0;
}


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

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

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

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

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

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

    make_nonnull_domain(&data.dom, dom);

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

    return 0;

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


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

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

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

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

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

    make_nonnull_domain(&data.dom, dom);

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

    return 0;
}

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

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

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

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

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

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

    return 0;
}

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

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

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

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

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

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

    return 0;
}

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

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

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

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

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

    return 0;
}


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

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

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

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

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

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

    return 0;
}

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

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

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

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

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

    make_nonnull_domain(&data.dom, dom);

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

    return 0;
}

935

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

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

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

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

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

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


974 975 976 977 978 979 980 981 982 983 984 985 986 987
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;

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

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

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

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

    return 0;
}


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

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

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

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

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

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

    return 0;
}


1046
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
1047
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
1048
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
1049
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
1050
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
1051
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
1052
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
1053
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
1054
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
1055
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
1056
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
1057
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
1058
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
1059
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
1060
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
1061
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
1062
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
1063
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob2),
1064
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTunable),
1065
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventAgentLifecycle),
1066 1067 1068 1069
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

1070
static int
1071
remoteRelayNetworkEventLifecycle(virConnectPtr conn,
1072 1073 1074 1075
                                 virNetworkPtr net,
                                 int event,
                                 int detail,
                                 void *opaque)
1076
{
1077
    daemonClientEventCallbackPtr callback = opaque;
1078 1079
    remote_network_event_lifecycle_msg data;

1080 1081
    if (callback->callbackID < 0 ||
        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
1082 1083
        return -1;

1084 1085
    VIR_DEBUG("Relaying network lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);
1086 1087 1088 1089

    /* build return data */
    memset(&data, 0, sizeof(data));
    make_nonnull_network(&data.net, net);
1090
    data.callbackID = callback->callbackID;
1091 1092 1093
    data.event = event;
    data.detail = detail;

1094
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
                                  REMOTE_PROC_NETWORK_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_network_event_lifecycle_msg, &data);

    return 0;
}

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

verify(ARRAY_CARDINALITY(networkEventCallbacks) == VIR_NETWORK_EVENT_ID_LAST);

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
static void
remoteRelayDomainQemuMonitorEvent(virConnectPtr conn,
                                  virDomainPtr dom,
                                  const char *event,
                                  long long seconds,
                                  unsigned int micros,
                                  const char *details,
                                  void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    qemu_domain_monitor_event_msg data;
    char **details_p = NULL;

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

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

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

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

1148
 error:
1149 1150 1151 1152
    VIR_FREE(data.event);
    VIR_FREE(details_p);
}

1153 1154 1155 1156 1157 1158 1159
/*
 * 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
 */
1160
void remoteClientFreeFunc(void *data)
1161 1162 1163 1164 1165
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
1166
        virIdentityPtr sysident = virIdentityGetSystem();
1167
        size_t i;
1168

1169 1170
        virIdentitySetCurrent(sysident);

1171 1172 1173 1174 1175
        for (i = 0; i < priv->ndomainEventCallbacks; i++) {
            int callbackID = priv->domainEventCallbacks[i]->callbackID;
            if (callbackID < 0) {
                VIR_WARN("unexpected incomplete domain callback %zu", i);
                continue;
1176
            }
1177 1178 1179 1180 1181
            VIR_DEBUG("Deregistering remote domain event relay %d",
                      callbackID);
            priv->domainEventCallbacks[i]->callbackID = -1;
            if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
                VIR_WARN("unexpected domain event deregister failure");
1182
        }
1183
        VIR_FREE(priv->domainEventCallbacks);
1184

1185 1186 1187 1188 1189
        for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
            int callbackID = priv->networkEventCallbacks[i]->callbackID;
            if (callbackID < 0) {
                VIR_WARN("unexpected incomplete network callback %zu", i);
                continue;
1190
            }
1191 1192 1193 1194 1195 1196
            VIR_DEBUG("Deregistering remote network event relay %d",
                      callbackID);
            priv->networkEventCallbacks[i]->callbackID = -1;
            if (virConnectNetworkEventDeregisterAny(priv->conn,
                                                    callbackID) < 0)
                VIR_WARN("unexpected network event deregister failure");
1197
        }
1198
        VIR_FREE(priv->networkEventCallbacks);
1199

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
        for (i = 0; i < priv->nqemuEventCallbacks; i++) {
            int callbackID = priv->qemuEventCallbacks[i]->callbackID;
            if (callbackID < 0) {
                VIR_WARN("unexpected incomplete qemu monitor callback %zu", i);
                continue;
            }
            VIR_DEBUG("Deregistering remote qemu monitor event relay %d",
                      callbackID);
            priv->qemuEventCallbacks[i]->callbackID = -1;
            if (virConnectDomainQemuMonitorEventDeregister(priv->conn,
                                                           callbackID) < 0)
                VIR_WARN("unexpected qemu monitor event deregister failure");
        }
        VIR_FREE(priv->qemuEventCallbacks);

1215
        virConnectClose(priv->conn);
1216 1217 1218

        virIdentitySetCurrent(NULL);
        virObjectUnref(sysident);
1219 1220 1221 1222 1223 1224
    }

    VIR_FREE(priv);
}


1225 1226 1227 1228 1229 1230 1231
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

1232

1233 1234
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
1235 1236 1237
{
    struct daemonClientPrivate *priv;

1238
    if (VIR_ALLOC(priv) < 0)
1239
        return NULL;
1240 1241 1242

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
1243
        virReportSystemError(errno, "%s", _("unable to init mutex"));
1244
        return NULL;
1245 1246
    }

1247
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
1248
    return priv;
1249 1250
}

1251 1252 1253
/*----- Functions. -----*/

static int
1254 1255 1256 1257 1258
remoteDispatchConnectOpen(virNetServerPtr server,
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
1259 1260
{
    const char *name;
1261
    unsigned int flags;
1262
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
1263
    int rv = -1;
1264

1265 1266 1267 1268
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
1269
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
1270 1271 1272
        goto cleanup;
    }

1273
    if (virNetServerKeepAliveRequired(server) && !priv->keepalive_supported) {
1274 1275
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("keepalive support is required to connect"));
1276 1277 1278
        goto cleanup;
    }

1279 1280 1281 1282 1283 1284
    name = args->name ? *args->name : NULL;

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

1288
    priv->conn =
1289
        flags & VIR_CONNECT_RO
1290 1291
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
1292

1293
    if (priv->conn == NULL)
1294 1295 1296
        goto cleanup;

    rv = 0;
1297

1298
 cleanup:
1299
    if (rv < 0)
1300 1301
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
1302
    return rv;
1303 1304 1305 1306
}


static int
1307 1308 1309 1310
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
1311
{
1312
    virNetServerClientDelayedClose(client);
1313
    return 0;
1314 1315
}

1316

1317
static int
1318 1319
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1320
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1321
                                     virNetMessageErrorPtr rerr,
1322 1323
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
1324
{
1325
    virDomainPtr dom = NULL;
1326 1327
    char *type;
    int nparams;
1328
    int rv = -1;
1329 1330
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1331

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

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

1340
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
1341
        goto cleanup;
1342 1343 1344

    ret->type = type;
    ret->nparams = nparams;
1345 1346
    rv = 0;

1347
 cleanup:
1348
    if (rv < 0)
1349
        virNetMessageSaveError(rerr);
1350
    virObjectUnref(dom);
1351
    return rv;
1352 1353
}

1354 1355
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
1356 1357 1358
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
1359
                               remote_typed_param **ret_params_val,
1360 1361
                               u_int *ret_params_len,
                               unsigned int flags)
1362
{
1363 1364
    size_t i;
    size_t j;
1365 1366 1367 1368
    int rv = -1;
    remote_typed_param *val;

    *ret_params_len = nparams;
1369
    if (VIR_ALLOC_N(val, nparams) < 0)
1370 1371
        goto cleanup;

1372
    for (i = 0, j = 0; i < nparams; ++i) {
1373 1374 1375 1376 1377
        /* virDomainGetCPUStats can return a sparse array; also, we
         * can't pass back strings to older clients.  */
        if (!params[i].type ||
            (!(flags & VIR_TYPED_PARAM_STRING_OKAY) &&
             params[i].type == VIR_TYPED_PARAM_STRING)) {
1378 1379 1380 1381
            --*ret_params_len;
            continue;
        }

1382
        /* remoteDispatchClientRequest will free this: */
1383
        if (VIR_STRDUP(val[j].field, params[i].field) < 0)
1384
            goto cleanup;
1385
        val[j].value.type = params[i].type;
1386
        switch (params[i].type) {
1387
        case VIR_TYPED_PARAM_INT:
1388
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
1389 1390
            break;
        case VIR_TYPED_PARAM_UINT:
1391
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
1392 1393
            break;
        case VIR_TYPED_PARAM_LLONG:
1394
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
1395 1396
            break;
        case VIR_TYPED_PARAM_ULLONG:
1397
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
1398 1399
            break;
        case VIR_TYPED_PARAM_DOUBLE:
1400
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
1401 1402
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
1403 1404 1405
            val[j].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
        case VIR_TYPED_PARAM_STRING:
1406
            if (VIR_STRDUP(val[j].value.remote_typed_param_value_u.s, params[i].value.s) < 0)
1407
                goto cleanup;
1408 1409
            break;
        default:
1410 1411
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
1412 1413
            goto cleanup;
        }
1414
        j++;
1415 1416 1417 1418 1419 1420
    }

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

1421
 cleanup:
1422
    if (val) {
1423
        for (i = 0; i < nparams; i++) {
1424
            VIR_FREE(val[i].field);
1425
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
1426 1427
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
1428 1429 1430 1431 1432 1433 1434
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
1435 1436
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
1437 1438 1439
                                 int limit,
                                 int *nparams)
{
1440
    size_t i = 0;
1441 1442 1443 1444
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
1445
    if (limit && args_params_len > limit) {
1446
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1447 1448
        goto cleanup;
    }
1449
    if (VIR_ALLOC_N(params, args_params_len) < 0)
1450 1451 1452 1453 1454 1455 1456 1457
        goto cleanup;

    *nparams = args_params_len;

    /* Deserialise the result. */
    for (i = 0; i < args_params_len; ++i) {
        if (virStrcpyStatic(params[i].field,
                            args_params_val[i].field) == NULL) {
1458 1459 1460
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Parameter %s too big for destination"),
                           args_params_val[i].field);
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
            goto cleanup;
        }
        params[i].type = args_params_val[i].value.type;
        switch (params[i].type) {
        case VIR_TYPED_PARAM_INT:
            params[i].value.i =
                args_params_val[i].value.remote_typed_param_value_u.i;
            break;
        case VIR_TYPED_PARAM_UINT:
            params[i].value.ui =
                args_params_val[i].value.remote_typed_param_value_u.ui;
            break;
        case VIR_TYPED_PARAM_LLONG:
            params[i].value.l =
                args_params_val[i].value.remote_typed_param_value_u.l;
            break;
        case VIR_TYPED_PARAM_ULLONG:
            params[i].value.ul =
                args_params_val[i].value.remote_typed_param_value_u.ul;
            break;
        case VIR_TYPED_PARAM_DOUBLE:
            params[i].value.d =
                args_params_val[i].value.remote_typed_param_value_u.d;
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
            params[i].value.b =
                args_params_val[i].value.remote_typed_param_value_u.b;
            break;
1489
        case VIR_TYPED_PARAM_STRING:
1490 1491
            if (VIR_STRDUP(params[i].value.s,
                           args_params_val[i].value.remote_typed_param_value_u.s) < 0)
1492 1493
                goto cleanup;
            break;
1494
        default:
1495 1496
            virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                           params[i].type);
1497 1498 1499 1500 1501 1502
            goto cleanup;
        }
    }

    rv = 0;

1503
 cleanup:
1504
    if (rv < 0) {
1505 1506
        virTypedParamsFree(params, i);
        params = NULL;
1507
    }
1508 1509 1510
    return params;
}

1511
static int
1512 1513
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
1514
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
1515
                                           virNetMessageErrorPtr rerr,
1516 1517
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
1518
{
1519
    virDomainPtr dom = NULL;
1520
    virTypedParameterPtr params = NULL;
1521
    int nparams = 0;
1522
    int rv = -1;
1523 1524
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1525

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

1531
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1532
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1533
        goto cleanup;
1534
    }
1535
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1536
        goto cleanup;
1537
    nparams = args->nparams;
1538

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

1542
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1543
        goto cleanup;
1544

1545
    if (remoteSerializeTypedParameters(params, nparams,
1546
                                       &ret->params.params_val,
1547 1548
                                       &ret->params.params_len,
                                       0) < 0)
1549 1550 1551 1552
        goto cleanup;

    rv = 0;

1553
 cleanup:
1554
    if (rv < 0)
1555
        virNetMessageSaveError(rerr);
1556
    virTypedParamsFree(params, nparams);
1557
    virObjectUnref(dom);
1558 1559 1560
    return rv;
}

1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
static int
remoteDispatchConnectListAllDomains(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_connect_list_all_domains_args *args,
                                    remote_connect_list_all_domains_ret *ret)
{
    virDomainPtr *doms = NULL;
    int ndomains = 0;
1571
    size_t i;
1572 1573 1574 1575
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1576
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1577 1578 1579 1580 1581 1582 1583 1584
        goto cleanup;
    }

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

1585 1586 1587 1588 1589 1590 1591
    if (ndomains > REMOTE_DOMAIN_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domains '%d' for limit '%d'"),
                       ndomains, REMOTE_DOMAIN_LIST_MAX);
        goto cleanup;
    }

1592
    if (doms && ndomains) {
1593
        if (VIR_ALLOC_N(ret->domains.domains_val, ndomains) < 0)
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
            goto cleanup;

        ret->domains.domains_len = ndomains;

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

    ret->ret = ndomains;

    rv = 0;

1609
 cleanup:
1610 1611
    if (rv < 0)
        virNetMessageSaveError(rerr);
1612
    if (doms && ndomains > 0)
1613
        for (i = 0; i < ndomains; i++)
1614
            virObjectUnref(doms[i]);
1615
    VIR_FREE(doms);
1616 1617 1618
    return rv;
}

1619
static int
1620 1621
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1622
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1623
                                                virNetMessageErrorPtr rerr,
1624 1625 1626 1627 1628
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1629
    int nparams = 0;
1630
    int rv = -1;
1631 1632
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1633

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

1639
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1640
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1641 1642
        goto cleanup;
    }
1643
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1644
        goto cleanup;
1645
    nparams = args->nparams;
1646

1647
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1648 1649 1650 1651 1652 1653 1654
        goto cleanup;

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

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

1660
    rv = 0;
1661

1662
 cleanup:
1663
    if (rv < 0)
1664
        virNetMessageSaveError(rerr);
1665
    virTypedParamsFree(params, nparams);
1666
    virObjectUnref(dom);
1667
    return rv;
1668 1669
}

1670
static int
1671 1672
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1673
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1674
                                virNetMessageErrorPtr rerr,
1675 1676
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1677
{
1678
    virDomainPtr dom = NULL;
1679
    virDomainMemoryStatPtr stats = NULL;
1680 1681
    int nr_stats;
    size_t i;
1682
    int rv = -1;
1683 1684
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1685

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

1691
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1692 1693
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1694
        goto cleanup;
1695 1696
    }

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

    /* Allocate stats array for making dispatch call */
1701
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
1702
        goto cleanup;
1703

1704
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1705
    if (nr_stats < 0)
1706
        goto cleanup;
1707 1708

    /* Allocate return buffer */
1709
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
1710
        goto cleanup;
1711 1712 1713 1714 1715 1716 1717

    /* 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;
1718 1719
    rv = 0;

1720
 cleanup:
1721
    if (rv < 0)
1722
        virNetMessageSaveError(rerr);
1723
    virObjectUnref(dom);
1724
    VIR_FREE(stats);
1725
    return rv;
1726 1727
}

1728
static int
1729 1730
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1731
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1732
                              virNetMessageErrorPtr rerr,
1733 1734
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1735
{
1736
    virDomainPtr dom = NULL;
1737 1738 1739 1740
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1741
    int rv = -1;
1742 1743
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1744

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

1750
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1751
        goto cleanup;
1752 1753 1754 1755 1756 1757
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1758 1759
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1760
        goto cleanup;
1761 1762 1763
    }

    ret->buffer.buffer_len = size;
1764
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1765
        goto cleanup;
1766

1767
    if (virDomainBlockPeek(dom, path, offset, size,
1768
                           ret->buffer.buffer_val, flags) < 0)
1769
        goto cleanup;
1770

1771 1772
    rv = 0;

1773
 cleanup:
1774
    if (rv < 0) {
1775
        virNetMessageSaveError(rerr);
1776 1777
        VIR_FREE(ret->buffer.buffer_val);
    }
1778
    virObjectUnref(dom);
1779
    return rv;
1780 1781
}

1782 1783 1784
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1785
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1786 1787 1788 1789 1790 1791 1792
                                    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;
1793
    int nparams = 0;
1794 1795 1796 1797 1798 1799
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1800
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1801 1802 1803 1804 1805 1806 1807
        goto cleanup;
    }

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

1808
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1809
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1810 1811
        goto cleanup;
    }
1812
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1813
        goto cleanup;
1814
    nparams = args->nparams;
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829

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

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

    /* Serialise the block stats. */
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
1830 1831
                                       &ret->params.params_len,
                                       args->flags) < 0)
1832 1833
        goto cleanup;

1834
 success:
1835 1836
    rv = 0;

1837
 cleanup:
1838
    if (rv < 0)
1839
        virNetMessageSaveError(rerr);
1840
    virTypedParamsFree(params, nparams);
1841
    virObjectUnref(dom);
1842 1843 1844
    return rv;
}

R
Richard W.M. Jones 已提交
1845
static int
1846 1847
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1848
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1849
                               virNetMessageErrorPtr rerr,
1850 1851
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1852
{
1853
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1854 1855 1856
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1857
    int rv = -1;
1858 1859
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1860

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

1866
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1867
        goto cleanup;
R
Richard W.M. Jones 已提交
1868 1869 1870 1871 1872
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1873 1874
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1875
        goto cleanup;
R
Richard W.M. Jones 已提交
1876 1877 1878
    }

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

1882
    if (virDomainMemoryPeek(dom, offset, size,
1883
                            ret->buffer.buffer_val, flags) < 0)
1884
        goto cleanup;
R
Richard W.M. Jones 已提交
1885

1886 1887
    rv = 0;

1888
 cleanup:
1889
    if (rv < 0) {
1890
        virNetMessageSaveError(rerr);
1891 1892
        VIR_FREE(ret->buffer.buffer_val);
    }
1893
    virObjectUnref(dom);
1894
    return rv;
R
Richard W.M. Jones 已提交
1895 1896
}

1897
static int
1898 1899
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1900
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1901
                                     virNetMessageErrorPtr rerr,
1902 1903
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1904
{
1905 1906
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1907
    int rv = -1;
1908 1909
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1910

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

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

1919
    if (VIR_ALLOC(seclabel) < 0)
1920 1921 1922 1923 1924 1925
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
1926
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
1927
        goto cleanup;
1928 1929
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1930

1931 1932
    rv = 0;

1933
 cleanup:
1934
    if (rv < 0)
1935
        virNetMessageSaveError(rerr);
1936
    virObjectUnref(dom);
1937
    VIR_FREE(seclabel);
1938
    return rv;
1939 1940
}

M
Marcelo Cerri 已提交
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
static int
remoteDispatchDomainGetSecurityLabelList(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_domain_get_security_label_list_args *args,
                                         remote_domain_get_security_label_list_ret *ret)
{
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabels = NULL;
1951 1952
    int len, rv = -1;
    size_t i;
M
Marcelo Cerri 已提交
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
    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;
    }

1971
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
1972 1973 1974 1975 1976
        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];
1977
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0)
M
Marcelo Cerri 已提交
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
            goto cleanup;
        if (virStrcpy(cur->label.label_val, seclabels[i].label, label_len) == NULL) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("failed to copy security label"));
            goto cleanup;
        }
        cur->label.label_len = label_len;
        cur->enforcing = seclabels[i].enforcing;
    }
    ret->labels.labels_len = ret->ret = len;

1989
 done:
M
Marcelo Cerri 已提交
1990 1991
    rv = 0;

1992
 cleanup:
M
Marcelo Cerri 已提交
1993 1994
    if (rv < 0)
        virNetMessageSaveError(rerr);
1995
    virObjectUnref(dom);
M
Marcelo Cerri 已提交
1996 1997 1998 1999
    VIR_FREE(seclabels);
    return rv;
}

2000
static int
2001 2002
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2003
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2004
                                   virNetMessageErrorPtr rerr,
2005
                                   remote_node_get_security_model_ret *ret)
2006
{
2007
    virSecurityModel secmodel;
2008
    int rv = -1;
2009 2010
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2011

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

2017
    memset(&secmodel, 0, sizeof(secmodel));
2018
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
2019 2020 2021
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
2022
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
2023 2024 2025 2026
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
2027
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
2028
        goto cleanup;
2029
    strcpy(ret->doi.doi_val, secmodel.doi);
2030

2031 2032
    rv = 0;

2033
 cleanup:
2034
    if (rv < 0)
2035
        virNetMessageSaveError(rerr);
2036
    return rv;
2037 2038
}

2039
static int
2040 2041
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2042
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2043
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
2044 2045
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
2046 2047 2048 2049 2050
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
2051 2052
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2053

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

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

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
2063
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
2064 2065 2066 2067 2068
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
2069
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2070 2071 2072 2073 2074 2075
        goto cleanup;
    }

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

E
Eric Blake 已提交
2078
    if ((num = virDomainGetVcpuPinInfo(dom,
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
                                       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;
2092 2093 2094 2095
    cpumaps = NULL;

    rv = 0;

2096
 cleanup:
2097 2098 2099
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumaps);
2100
    virObjectUnref(dom);
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
    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;

2132
 cleanup:
2133 2134
    if (rv < 0)
        virNetMessageSaveError(rerr);
2135
    virObjectUnref(dom);
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
    return rv;
}


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

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

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

    /* Allocate buffers to take the results */
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maplen) < 0)
2166
        goto cleanup;
2167 2168 2169 2170 2171 2172 2173 2174 2175 2176

    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;
2177 2178 2179 2180
    cpumaps = NULL;

    rv = 0;

2181
 cleanup:
2182
    if (rv < 0)
2183
        virNetMessageSaveError(rerr);
2184
    VIR_FREE(cpumaps);
2185
    virObjectUnref(dom);
2186 2187 2188
    return rv;
}

2189
static int
2190 2191
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
2192
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
2193
                             virNetMessageErrorPtr rerr,
2194 2195
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
2196
{
2197
    virDomainPtr dom = NULL;
2198 2199
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
2200 2201
    int info_len;
    size_t i;
2202
    int rv = -1;
2203 2204
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2205

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

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

2214
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
2215
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
2216
        goto cleanup;
2217
    }
2218

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

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
2227
        goto cleanup;
2228 2229
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
2230
        goto cleanup;
2231 2232 2233 2234 2235 2236 2237 2238 2239

    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)
2240
        goto cleanup;
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257

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

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

2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
static int
remoteDispatchDomainGetIOThreadsInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_domain_get_iothreads_info_args *args,
                                     remote_domain_get_iothreads_info_ret *ret)
{
    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;

    if ((ninfo = virDomainGetIOThreadsInfo(dom, &info, args->flags)) < 0)
        goto cleanup;

    if (ninfo > REMOTE_IOTHREADS_INFO_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many IOThreads in info: %d for limit %d"),
                       ninfo, REMOTE_IOTHREADS_INFO_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];
            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++)
            virDomainIOThreadsInfoFree(info[i]);
    VIR_FREE(info);

    return rv;
}

2342
static int
2343 2344
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2345
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2346
                                   virNetMessageErrorPtr rerr,
2347 2348
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
2349
{
2350 2351 2352 2353 2354
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2355
    int rv = -1;
2356 2357
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2358

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

2364 2365 2366 2367
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
2368
    if (VIR_ALLOC(uri_out) < 0)
2369
        goto cleanup;
2370

2371
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
2372 2373
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
2374
        goto cleanup;
2375

2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386
    /* 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;
    }
2387

2388
    rv = 0;
2389

2390
 cleanup:
2391
    if (rv < 0)
2392
        virNetMessageSaveError(rerr);
2393
    VIR_FREE(uri_out);
2394
    return rv;
2395 2396
}

2397
static int
2398 2399
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2400
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2401
                                    virNetMessageErrorPtr rerr,
2402 2403
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
2404
{
2405 2406 2407 2408 2409
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2410
    int rv = -1;
2411 2412
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2413

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

2419 2420
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
2421

2422
    /* Wacky world of XDR ... */
2423
    if (VIR_ALLOC(uri_out) < 0)
2424
        goto cleanup;
2425

2426
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
2427 2428 2429
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
2430
        goto cleanup;
2431

2432 2433 2434 2435 2436 2437
    /* 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;
2438

2439 2440
    rv = 0;

2441
 cleanup:
2442
    if (rv < 0) {
2443
        virNetMessageSaveError(rerr);
2444 2445
        VIR_FREE(uri_out);
    }
2446
    return rv;
2447 2448
}

C
Chris Lalancette 已提交
2449
static int
2450 2451
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
2452
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
2453 2454 2455
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
2456
{
2457
    virDomainPtr dom = NULL;
2458
    virTypedParameterPtr params = NULL;
2459
    int nparams = 0;
2460
    unsigned int flags;
2461
    int rv = -1;
2462 2463
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2464

2465
    if (!priv->conn) {
2466
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2467
        goto cleanup;
2468
    }
C
Chris Lalancette 已提交
2469

2470
    flags = args->flags;
C
Chris Lalancette 已提交
2471

2472
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2473
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2474 2475
        goto cleanup;
    }
2476
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2477
        goto cleanup;
2478
    nparams = args->nparams;
C
Chris Lalancette 已提交
2479

2480
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2481
        goto cleanup;
C
Chris Lalancette 已提交
2482

2483
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
2484
        goto cleanup;
C
Chris Lalancette 已提交
2485

2486 2487 2488 2489 2490 2491
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
2492 2493
    }

2494
    if (remoteSerializeTypedParameters(params, nparams,
2495
                                       &ret->params.params_val,
2496 2497
                                       &ret->params.params_len,
                                       args->flags) < 0)
2498
        goto cleanup;
2499

2500
 success:
2501 2502
    rv = 0;

2503
 cleanup:
2504
    if (rv < 0)
2505
        virNetMessageSaveError(rerr);
2506
    virTypedParamsFree(params, nparams);
2507
    virObjectUnref(dom);
2508
    return rv;
2509 2510
}

2511 2512 2513 2514 2515 2516 2517 2518 2519 2520
static int
remoteDispatchDomainGetNumaParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_domain_get_numa_parameters_args *args,
                                      remote_domain_get_numa_parameters_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
2521
    int nparams = 0;
2522 2523 2524 2525 2526 2527
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2528
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2529 2530 2531 2532 2533
        goto cleanup;
    }

    flags = args->flags;

2534
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
2535
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2536 2537
        goto cleanup;
    }
2538
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2539
        goto cleanup;
2540
    nparams = args->nparams;
2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561

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

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

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

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

2562
 success:
2563 2564
    rv = 0;

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

2573
static int
2574 2575
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
2576
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2577 2578 2579
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
2580
{
2581
    virDomainPtr dom = NULL;
2582
    virTypedParameterPtr params = NULL;
2583
    int nparams = 0;
2584
    unsigned int flags;
2585
    int rv = -1;
2586 2587
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2588

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

2594 2595
    flags = args->flags;

2596
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2597
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2598
        goto cleanup;
2599
    }
2600
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2601
        goto cleanup;
2602
    nparams = args->nparams;
2603

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

2607
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2608
        goto cleanup;
2609

2610 2611 2612 2613 2614 2615 2616 2617
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2618
    if (remoteSerializeTypedParameters(params, nparams,
2619
                                       &ret->params.params_val,
2620 2621
                                       &ret->params.params_len,
                                       args->flags) < 0)
2622
        goto cleanup;
2623

2624
 success:
2625
    rv = 0;
2626

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

2635
static int
2636 2637
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2638
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2639 2640 2641
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2642
{
2643
    virNodeCPUStatsPtr params = NULL;
2644
    size_t i;
2645
    int cpuNum = args->cpuNum;
2646
    int nparams = 0;
2647 2648
    unsigned int flags;
    int rv = -1;
2649 2650
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2651

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

    flags = args->flags;

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

2667
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
        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)
2681
        goto cleanup;
2682 2683 2684

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2685 2686
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2687 2688 2689 2690

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

2691
 success:
2692 2693
    rv = 0;

2694
 cleanup:
2695
    if (rv < 0) {
2696
        virNetMessageSaveError(rerr);
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
        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
2708 2709
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2710
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2711 2712 2713
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2714
{
2715
    virNodeMemoryStatsPtr params = NULL;
2716
    size_t i;
2717
    int cellNum = args->cellNum;
2718
    int nparams = 0;
2719 2720
    unsigned int flags;
    int rv = -1;
2721 2722
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2723

2724
    if (!priv->conn) {
2725
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2726 2727 2728 2729 2730
        goto cleanup;
    }

    flags = args->flags;

2731
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
2732
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2733 2734
        goto cleanup;
    }
2735
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2736
        goto cleanup;
2737
    nparams = args->nparams;
2738

2739
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752
        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)
2753
        goto cleanup;
2754 2755 2756

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2757 2758
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2759 2760 2761 2762

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

2763
 success:
2764 2765
    rv = 0;

2766
 cleanup:
2767
    if (rv < 0) {
2768
        virNetMessageSaveError(rerr);
2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
        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;
}

2779 2780 2781
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2782
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
                                    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) {
2794
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811
        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;

2812
 cleanup:
2813 2814
    if (rv < 0)
        virNetMessageSaveError(rerr);
2815
    virObjectUnref(dom);
2816 2817 2818
    return rv;
}

2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
static int
remoteDispatchDomainGetBlockIoTune(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                   virNetMessagePtr hdr ATTRIBUTE_UNUSED,
                                   virNetMessageErrorPtr rerr,
                                   remote_domain_get_block_io_tune_args *args,
                                   remote_domain_get_block_io_tune_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    virTypedParameterPtr params = NULL;
2830
    int nparams = 0;
2831 2832 2833 2834
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

2839
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
2840
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2841 2842 2843
        goto cleanup;
    }

2844
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2845
        goto cleanup;
2846
    nparams = args->nparams;
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869

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

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

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

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

2870
 success:
2871 2872
    rv = 0;

2873
 cleanup:
2874 2875
    if (rv < 0)
        virNetMessageSaveError(rerr);
2876
    virTypedParamsFree(params, nparams);
2877
    virObjectUnref(dom);
2878 2879
    return rv;
}
2880

D
Daniel Veillard 已提交
2881 2882
/*-------------------------------------------------------------*/

2883
static int
2884
remoteDispatchAuthList(virNetServerPtr server,
2885
                       virNetServerClientPtr client,
2886
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2887
                       virNetMessageErrorPtr rerr,
2888
                       remote_auth_list_ret *ret)
2889
{
2890
    int rv = -1;
2891 2892
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2893
    gid_t callerGid;
2894
    pid_t callerPid;
2895
    unsigned long long timestamp;
2896 2897 2898 2899 2900 2901

    /* 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) {
2902
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2903
                                              &callerPid, &timestamp) < 0) {
2904 2905 2906 2907
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2908 2909
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
2910
                            (long long) callerPid, (int) callerUid) < 0)
J
Jim Fehlig 已提交
2911 2912
                goto cleanup;
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
2913
            virNetServerClientSetAuth(client, 0);
2914
            virNetServerTrackCompletedAuth(server);
2915
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
2916
            VIR_FREE(ident);
2917 2918
        }
    }
2919

2920
    ret->types.types_len = 1;
2921
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
2922
        goto cleanup;
2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936

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

2938 2939
    rv = 0;

2940
 cleanup:
2941
    if (rv < 0)
2942
        virNetMessageSaveError(rerr);
2943
    return rv;
2944 2945 2946
}


2947
#ifdef WITH_SASL
2948 2949
/*
 * Initializes the SASL session in prepare for authentication
2950
 * and gives the client a list of allowed mechanisms to choose
2951 2952
 */
static int
2953 2954
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2955
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2956
                           virNetMessageErrorPtr rerr,
2957
                           remote_auth_sasl_init_ret *ret)
2958
{
2959 2960 2961
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2962

2963
    virMutexLock(&priv->lock);
2964

2965 2966 2967
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2968
        VIR_ERROR(_("client tried invalid SASL init request"));
2969
        goto authfail;
2970 2971
    }

2972 2973 2974 2975 2976
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2977
        goto authfail;
2978

2979
# if WITH_GNUTLS
2980
    /* Inform SASL that we've got an external SSF layer from TLS */
2981 2982 2983 2984
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2985
            goto authfail;
2986 2987 2988 2989 2990

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2991
            goto authfail;
2992
    }
2993
# endif
2994

2995
    if (virNetServerClientIsSecure(client))
2996
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2997 2998
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2999
        /* Plain TCP, better get an SSF layer */
3000 3001 3002 3003
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
3004

3005
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
3006
        goto authfail;
3007
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
3008

3009 3010
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
3011
    return 0;
3012

3013
 authfail:
3014
    virResetLastError();
3015 3016
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3017
    virNetMessageSaveError(rerr);
3018 3019 3020
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3021
    virObjectUnref(sasl);
3022
    virMutexUnlock(&priv->lock);
3023
    return -1;
3024 3025
}

3026
/*
3027 3028
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
3029
static int
3030 3031
remoteSASLFinish(virNetServerPtr server,
                 virNetServerClientPtr client)
3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046
{
    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;
        }
3047
    }
3048 3049

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
3050
        return -2;
3051

3052 3053
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
3054

3055
    virNetServerClientSetAuth(client, 0);
3056
    virNetServerTrackCompletedAuth(server);
3057
    virNetServerClientSetSASLSession(client, priv->sasl);
3058

3059
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
3060

3061 3062 3063
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3064

3065
    virObjectUnref(priv->sasl);
3066
    priv->sasl = NULL;
3067

3068
    return 0;
3069

3070
 error:
3071 3072
    return -1;
}
3073

3074 3075 3076 3077
/*
 * This starts the SASL authentication negotiation.
 */
static int
3078
remoteDispatchAuthSaslStart(virNetServerPtr server,
3079
                            virNetServerClientPtr client,
3080
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3081
                            virNetMessageErrorPtr rerr,
3082 3083
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
3084 3085
{
    const char *serverout;
3086
    size_t serveroutlen;
3087
    int err;
3088 3089 3090
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3091
    const char *identity;
3092

3093
    virMutexLock(&priv->lock);
3094

3095 3096 3097
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
3098
        VIR_ERROR(_("client tried invalid SASL start request"));
3099
        goto authfail;
3100 3101
    }

3102 3103
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
3104 3105 3106 3107 3108 3109 3110 3111 3112
    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)
3113
        goto authfail;
3114

3115
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3116
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
3117
        goto authfail;
3118 3119 3120 3121
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3122 3123
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3124 3125 3126 3127 3128 3129 3130
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3131
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3132
    if (err == VIR_NET_SASL_CONTINUE) {
3133 3134
        ret->complete = 0;
    } else {
3135
        /* Check username whitelist ACL */
3136
        if ((err = remoteSASLFinish(server, client)) < 0) {
3137 3138 3139 3140 3141
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3142

3143 3144 3145
        ret->complete = 1;
    }

3146
    virMutexUnlock(&priv->lock);
3147
    return 0;
3148

3149
 authfail:
3150 3151 3152
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3153 3154
    goto error;

3155
 authdeny:
3156
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3157 3158 3159
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3160 3161
    goto error;

3162
 error:
3163
    virObjectUnref(priv->sasl);
3164 3165
    priv->sasl = NULL;
    virResetLastError();
3166 3167
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3168 3169 3170
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3171
    return -1;
3172 3173 3174 3175
}


static int
3176
remoteDispatchAuthSaslStep(virNetServerPtr server,
3177
                           virNetServerClientPtr client,
3178
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3179
                           virNetMessageErrorPtr rerr,
3180 3181
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
3182 3183
{
    const char *serverout;
3184
    size_t serveroutlen;
3185
    int err;
3186 3187 3188
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3189
    const char *identity;
3190

3191 3192 3193 3194 3195
    virMutexLock(&priv->lock);

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

3200
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
3201
              args->data.data_len, args->nil);
3202 3203 3204 3205 3206 3207 3208 3209
    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)
3210
        goto authfail;
3211 3212

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3213
        VIR_ERROR(_("sasl step reply data too long %d"),
3214
                  (int)serveroutlen);
3215
        goto authfail;
3216 3217 3218 3219
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3220 3221
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3222 3223 3224 3225 3226 3227 3228
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3229
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3230
    if (err == VIR_NET_SASL_CONTINUE) {
3231 3232
        ret->complete = 0;
    } else {
3233
        /* Check username whitelist ACL */
3234
        if ((err = remoteSASLFinish(server, client)) < 0) {
3235 3236 3237 3238 3239
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3240

3241 3242 3243
        ret->complete = 1;
    }

3244
    virMutexUnlock(&priv->lock);
3245
    return 0;
3246

3247
 authfail:
3248 3249 3250
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3251 3252
    goto error;

3253
 authdeny:
3254
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3255 3256 3257
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3258 3259
    goto error;

3260
 error:
3261
    virObjectUnref(priv->sasl);
3262 3263
    priv->sasl = NULL;
    virResetLastError();
3264 3265
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3266 3267 3268
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3269 3270
    return -1;
}
3271 3272 3273 3274
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3275
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3276 3277 3278 3279
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3280 3281
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3282 3283 3284 3285 3286 3287
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
3288
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3289 3290 3291 3292 3293
                            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");
3294 3295
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3296 3297 3298 3299 3300 3301
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3302
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3303 3304 3305 3306 3307
                           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");
3308 3309
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3310 3311 3312 3313
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
3314 3315 3316



3317
static int
3318
remoteDispatchAuthPolkit(virNetServerPtr server,
3319
                         virNetServerClientPtr client,
3320
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3321
                         virNetMessageErrorPtr rerr,
3322
                         remote_auth_polkit_ret *ret)
3323
{
3324
    pid_t callerPid = -1;
3325
    gid_t callerGid = -1;
3326
    uid_t callerUid = -1;
3327
    unsigned long long timestamp;
3328
    const char *action;
3329
    char *ident = NULL;
3330 3331
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3332
    int rv;
3333

3334 3335
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
3336 3337 3338
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

3339 3340
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3341
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3342 3343 3344
        goto authfail;
    }

3345
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
3346
                                          &callerPid, &timestamp) < 0) {
3347 3348 3349
        goto authfail;
    }

3350 3351 3352 3353 3354 3355
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

3356 3357
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3358

3359 3360 3361 3362 3363 3364 3365
    rv = virPolkitCheckAuth(action,
                            callerPid,
                            timestamp,
                            callerUid,
                            NULL,
                            true);
    if (rv == -1)
3366
        goto authfail;
3367
    else if (rv == -2)
3368
        goto authdeny;
3369

3370 3371 3372
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3373 3374
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
3375 3376
    ret->complete = 1;

3377
    virNetServerClientSetAuth(client, 0);
3378
    virNetServerTrackCompletedAuth(server);
3379
    virMutexUnlock(&priv->lock);
3380

3381
    return 0;
3382

3383
 error:
3384
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3385
    virMutexUnlock(&priv->lock);
3386 3387
    return -1;

3388
 authfail:
3389 3390 3391
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3392
    goto error;
3393

3394
 authdeny:
3395 3396 3397
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3398
    goto error;
3399 3400
}

3401

3402 3403 3404
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3405

3406
static int
3407 3408
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3409
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3410
                                  virNetMessageErrorPtr rerr,
3411 3412
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3413
{
3414 3415
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3416
    int rv = -1;
3417 3418
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3419

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

3425
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3426 3427
        goto cleanup;

3428 3429 3430 3431 3432 3433 3434
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
3435
        if (VIR_ALLOC(parent_p) < 0)
3436
            goto cleanup;
3437
        if (VIR_STRDUP(*parent_p, parent) < 0) {
3438 3439 3440 3441 3442 3443
            VIR_FREE(parent_p);
            goto cleanup;
        }
        ret->parent = parent_p;
    }

3444 3445
    rv = 0;

3446
 cleanup:
3447
    if (rv < 0)
3448
        virNetMessageSaveError(rerr);
3449
    virObjectUnref(dev);
3450
    return rv;
3451 3452
}

3453 3454 3455 3456 3457

/***************************
 * Register / deregister events
 ***************************/
static int
3458
remoteDispatchConnectDomainEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
3459
                                         virNetServerClientPtr client,
3460 3461 3462
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                         remote_connect_domain_event_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
3463
{
3464
    int callbackID;
3465
    int rv = -1;
3466 3467
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3468 3469
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3470

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

3476 3477
    virMutexLock(&priv->lock);

3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488
    /* If we call register first, we could append a complete callback
     * to our array, but on OOM append failure, we'd have to then hope
     * deregister works to undo our register.  So instead we append an
     * incomplete callback to our array, then register, then fix up
     * our callback; but since VIR_APPEND_ELEMENT clears 'callback' on
     * success, we use 'ref' to save a copy of the pointer.  */
    if (VIR_ALLOC(callback) < 0)
        goto cleanup;
    callback->client = client;
    callback->eventID = VIR_DOMAIN_EVENT_ID_LIFECYCLE;
    callback->callbackID = -1;
3489
    callback->legacy = true;
3490 3491 3492 3493
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3494
        goto cleanup;
O
Osier Yang 已提交
3495

3496
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3497 3498 3499
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
3500 3501 3502 3503 3504
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3505
        goto cleanup;
3506
    }
O
Osier Yang 已提交
3507

3508
    ref->callbackID = callbackID;
3509

3510 3511
    rv = 0;

3512
 cleanup:
3513
    VIR_FREE(callback);
3514
    if (rv < 0)
3515 3516
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3517
    return rv;
O
Osier Yang 已提交
3518 3519
}

3520
static int
3521
remoteDispatchConnectDomainEventDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
3522
                                           virNetServerClientPtr client,
3523 3524 3525
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                           remote_connect_domain_event_deregister_ret *ret ATTRIBUTE_UNUSED)
3526
{
3527
    int callbackID = -1;
3528
    int rv = -1;
3529
    size_t i;
3530 3531
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3532

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

3538 3539
    virMutexLock(&priv->lock);

3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550
    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);
3551
        goto cleanup;
3552
    }
3553

3554
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3555
        goto cleanup;
3556

3557 3558
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3559

3560 3561
    rv = 0;

3562
 cleanup:
3563
    if (rv < 0)
3564 3565
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3566 3567 3568 3569
    return rv;
}

static void
3570
remoteDispatchObjectEventSend(virNetServerClientPtr client,
3571
                              virNetServerProgramPtr program,
3572 3573 3574 3575
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3576
    virNetMessagePtr msg;
3577

3578
    if (!(msg = virNetMessageNew(false)))
3579
        goto cleanup;
3580

3581 3582 3583 3584 3585 3586
    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;
3587

3588
    if (virNetMessageEncodeHeader(msg) < 0)
3589 3590
        goto cleanup;

3591 3592
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3593

3594 3595
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3596

3597
    xdr_free(proc, data);
3598 3599
    return;

3600
 cleanup:
3601
    virNetMessageFree(msg);
3602
    xdr_free(proc, data);
3603 3604
}

3605
static int
3606 3607
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3608
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3609
                             virNetMessageErrorPtr rerr,
3610 3611
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3612
{
3613 3614 3615
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3616
    int rv = -1;
3617 3618
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3619

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

3625
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3626
        goto cleanup;
3627

3628
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3629
        goto cleanup;
3630

3631 3632 3633
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3634 3635
    rv = 0;

3636
 cleanup:
3637
    if (rv < 0)
3638
        virNetMessageSaveError(rerr);
3639
    virObjectUnref(secret);
3640
    return rv;
3641 3642
}

3643
static int
3644 3645
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3646
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3647
                             virNetMessageErrorPtr rerr,
3648 3649 3650 3651 3652
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3653 3654
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3655

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

3661
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3662 3663 3664 3665 3666 3667 3668
        goto cleanup;

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

    rv = 0;

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

3676 3677 3678 3679 3680 3681

/* 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.  */
3682
static int
3683
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3684
                                            virNetServerClientPtr client,
3685 3686 3687
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            remote_connect_domain_event_register_any_args *args)
3688 3689
{
    int callbackID;
3690
    int rv = -1;
3691 3692
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3693 3694
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3695

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

3701 3702
    virMutexLock(&priv->lock);

3703 3704 3705 3706
    /* 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 ||
3707
        args->eventID < 0) {
3708 3709
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3710
        goto cleanup;
3711 3712
    }

3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723
    /* If we call register first, we could append a complete callback
     * to our array, but on OOM append failure, we'd have to then hope
     * deregister works to undo our register.  So instead we append an
     * incomplete callback to our array, then register, then fix up
     * our callback; but since VIR_APPEND_ELEMENT clears 'callback' on
     * success, we use 'ref' to save a copy of the pointer.  */
    if (VIR_ALLOC(callback) < 0)
        goto cleanup;
    callback->client = client;
    callback->eventID = args->eventID;
    callback->callbackID = -1;
3724
    callback->legacy = true;
3725 3726 3727 3728
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3729
        goto cleanup;
3730

3731
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3732 3733 3734
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3735 3736 3737 3738 3739
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3740
        goto cleanup;
3741
    }
3742

3743
    ref->callbackID = callbackID;
3744

3745 3746
    rv = 0;

3747
 cleanup:
3748
    VIR_FREE(callback);
3749
    if (rv < 0)
3750 3751
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3752
    return rv;
3753 3754 3755
}


3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782
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;

3783
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST || args->eventID < 0) {
3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
        goto cleanup;
    }

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

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

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

    rv = 0;

3823
 cleanup:
3824 3825 3826
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
3827
    virObjectUnref(dom);
3828 3829 3830 3831 3832
    virMutexUnlock(&priv->lock);
    return rv;
}


3833
static int
3834
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3835
                                              virNetServerClientPtr client,
3836 3837 3838
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              remote_connect_domain_event_deregister_any_args *args)
3839 3840
{
    int callbackID = -1;
3841
    int rv = -1;
3842
    size_t i;
3843 3844
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3845

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

3851 3852
    virMutexLock(&priv->lock);

3853 3854 3855 3856
    /* 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 ||
3857
        args->eventID < 0) {
3858 3859
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3860
        goto cleanup;
3861 3862
    }

3863 3864 3865 3866 3867 3868
    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->eventID == args->eventID) {
            callbackID = priv->domainEventCallbacks[i]->callbackID;
            break;
        }
    }
3869
    if (callbackID < 0) {
3870 3871
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event %d not registered"), args->eventID);
3872
        goto cleanup;
3873 3874
    }

3875
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3876
        goto cleanup;
3877

3878 3879
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3880

3881 3882
    rv = 0;

3883
 cleanup:
3884
    if (rv < 0)
3885 3886
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3887
    return rv;
3888 3889
}

3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928

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;

3929
 cleanup:
3930 3931 3932 3933 3934 3935 3936
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}


C
Chris Lalancette 已提交
3937
static int
3938 3939 3940 3941 3942 3943
qemuDispatchDomainMonitorCommand(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                 virNetMessageErrorPtr rerr,
                                 qemu_domain_monitor_command_args *args,
                                 qemu_domain_monitor_command_ret *ret)
C
Chris Lalancette 已提交
3944
{
3945
    virDomainPtr dom = NULL;
3946
    int rv = -1;
3947 3948
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
3949

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

3955
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3956
        goto cleanup;
C
Chris Lalancette 已提交
3957

3958
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
3959
                                    args->flags) < 0)
3960
        goto cleanup;
C
Chris Lalancette 已提交
3961

3962
    rv = 0;
C
Chris Lalancette 已提交
3963

3964
 cleanup:
3965
    if (rv < 0)
3966
        virNetMessageSaveError(rerr);
3967
    virObjectUnref(dom);
3968
    return rv;
C
Chris Lalancette 已提交
3969 3970
}

3971

3972
static int
3973 3974
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3975
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3976
                                  virNetMessageErrorPtr rerr,
3977 3978 3979 3980 3981 3982
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
3983
    char *xmlin;
3984 3985 3986
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3987 3988
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3989

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

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

3998
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3999 4000
    dname = args->dname == NULL ? NULL : *args->dname;

4001
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014
                                       &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;

4015
 cleanup:
4016
    if (rv < 0)
4017
        virNetMessageSaveError(rerr);
4018
    virObjectUnref(dom);
4019 4020 4021 4022 4023
    return rv;
}


static int
4024 4025
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4026
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4027
                                    virNetMessageErrorPtr rerr,
4028 4029 4030 4031 4032 4033 4034 4035 4036
                                    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;
4037 4038
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4039

4040
    if (!priv->conn) {
4041
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4042 4043 4044 4045 4046 4047 4048
        goto cleanup;
    }

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

    /* Wacky world of XDR ... */
4049
    if (VIR_ALLOC(uri_out) < 0)
4050 4051
        goto cleanup;

4052
    if (virDomainMigratePrepare3(priv->conn,
4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069
                                 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;

4070
 cleanup:
4071
    if (rv < 0) {
4072
        virNetMessageSaveError(rerr);
4073 4074 4075 4076 4077
        VIR_FREE(uri_out);
    }
    return rv;
}

4078

4079
static int
4080 4081
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4082
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4083
                                    virNetMessageErrorPtr rerr,
4084 4085 4086 4087
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
4088
    char *xmlin;
4089
    char *dname;
4090 4091
    char *uri;
    char *dconnuri;
4092 4093 4094
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4095 4096
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4097

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

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

4106
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4107
    dname = args->dname == NULL ? NULL : *args->dname;
4108 4109
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
4110

4111
    if (virDomainMigratePerform3(dom, xmlin,
4112 4113 4114
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
4115
                                 dconnuri, uri,
4116 4117 4118 4119 4120 4121 4122 4123 4124 4125
                                 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;

4126
 cleanup:
4127
    if (rv < 0)
4128
        virNetMessageSaveError(rerr);
4129
    virObjectUnref(dom);
4130 4131 4132 4133 4134
    return rv;
}


static int
4135 4136
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
4137
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
4138
                                   virNetMessageErrorPtr rerr,
4139 4140 4141 4142 4143 4144
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
4145 4146
    char *uri;
    char *dconnuri;
4147
    int rv = -1;
4148 4149
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4150

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

4156 4157 4158
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

4159
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
4160 4161 4162 4163 4164 4165
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
4166 4167
        goto cleanup;

4168
    make_nonnull_domain(&ret->dom, dom);
4169 4170 4171 4172 4173 4174 4175 4176

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

    rv = 0;

4177
 cleanup:
4178
    if (rv < 0) {
4179
        virNetMessageSaveError(rerr);
4180 4181
        VIR_FREE(cookieout);
    }
4182
    virObjectUnref(dom);
4183 4184 4185 4186 4187
    return rv;
}


static int
4188 4189
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4190
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4191 4192
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
4193 4194 4195
{
    virDomainPtr dom = NULL;
    int rv = -1;
4196 4197
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4198

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

4204
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4205 4206 4207 4208 4209 4210 4211 4212 4213 4214
        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;

4215
 cleanup:
4216
    if (rv < 0)
4217
        virNetMessageSaveError(rerr);
4218
    virObjectUnref(dom);
4219 4220 4221 4222
    return rv;
}


4223 4224 4225 4226 4227 4228
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)
4229 4230 4231 4232 4233 4234
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

4235 4236 4237 4238 4239 4240 4241 4242 4243 4244
    /* 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;
    }

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

    switch (args->feature) {
    case VIR_DRV_FEATURE_FD_PASSING:
4252
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
4253 4254 4255 4256
        supported = 1;
        break;

    default:
4257
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
4258 4259 4260 4261
            goto cleanup;
        break;
    }

4262
 done:
4263 4264 4265
    ret->supported = supported;
    rv = 0;

4266
 cleanup:
4267 4268 4269 4270 4271 4272
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286
static int
remoteDispatchDomainOpenGraphics(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                 virNetMessagePtr msg,
                                 virNetMessageErrorPtr rerr,
                                 remote_domain_open_graphics_args *args)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    int fd = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4287
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304
        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;

4305
 cleanup:
4306 4307 4308
    VIR_FORCE_CLOSE(fd);
    if (rv < 0)
        virNetMessageSaveError(rerr);
4309
    virObjectUnref(dom);
4310 4311 4312
    return rv;
}

4313

4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334
static int
remoteDispatchDomainOpenGraphicsFd(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                   virNetMessagePtr msg,
                                   virNetMessageErrorPtr rerr,
                                   remote_domain_open_graphics_fd_args *args)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    int fd = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

4335 4336 4337
    if ((fd = virDomainOpenGraphicsFD(dom,
                                      args->idx,
                                      args->flags)) < 0)
4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348
        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);
4349
    if (rv < 0)
4350 4351
        virNetMessageSaveError(rerr);

4352
    virObjectUnref(dom);
4353 4354
    return rv;
}
4355 4356


4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367
static int
remoteDispatchDomainGetInterfaceParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr,
                                           remote_domain_get_interface_parameters_args *args,
                                           remote_domain_get_interface_parameters_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    const char *device = args->device;
4368
    int nparams = 0;
4369 4370 4371 4372 4373 4374
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4375
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4376 4377 4378 4379 4380
        goto cleanup;
    }

    flags = args->flags;

4381
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
4382
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4383 4384
        goto cleanup;
    }
4385
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4386
        goto cleanup;
4387
    nparams = args->nparams;
4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408

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

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

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

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

4409
 success:
4410 4411
    rv = 0;

4412
 cleanup:
4413 4414
    if (rv < 0)
        virNetMessageSaveError(rerr);
4415
    virTypedParamsFree(params, nparams);
4416
    virObjectUnref(dom);
4417 4418
    return rv;
}
4419

4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435
static int
remoteDispatchDomainGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                virNetMessagePtr hdr ATTRIBUTE_UNUSED,
                                virNetMessageErrorPtr rerr,
                                remote_domain_get_cpu_stats_args *args,
                                remote_domain_get_cpu_stats_ret *ret)
{
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv;
    virTypedParameterPtr params = NULL;
    int rv = -1;
    int percpu_len = 0;

    priv = virNetServerClientGetPrivateData(client);
    if (!priv->conn) {
4436
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4437 4438 4439 4440
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
4441
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4442 4443 4444
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
4445
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
4446 4447 4448 4449
        goto cleanup;
    }

    if (args->nparams > 0 &&
4450
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470
        goto cleanup;

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

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

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

4471
 success:
4472 4473
    rv = 0;
    ret->nparams = percpu_len;
4474
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
4475
        size_t i;
4476 4477 4478 4479 4480 4481

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

4483
 cleanup:
4484 4485
    if (rv < 0)
         virNetMessageSaveError(rerr);
4486
    virTypedParamsFree(params, args->ncpus * args->nparams);
4487
    virObjectUnref(dom);
4488 4489 4490
    return rv;
}

4491 4492 4493 4494 4495 4496 4497
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)
4498 4499 4500 4501
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
4502
    int len = 0;
4503 4504 4505 4506
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
4515 4516
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
4517 4518 4519 4520
        goto cleanup;
    }

    if (args->maxerrors &&
4521
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537
        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;

4538
 cleanup:
4539 4540
    if (rv < 0)
        virNetMessageSaveError(rerr);
4541
    virObjectUnref(dom);
4542
    if (errors && len > 0) {
4543
        size_t i;
4544 4545 4546 4547 4548 4549 4550
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

4551 4552 4553 4554 4555 4556 4557 4558 4559 4560
static int
remoteDispatchDomainListAllSnapshots(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_domain_list_all_snapshots_args *args,
                                     remote_domain_list_all_snapshots_ret *ret)
{
    virDomainSnapshotPtr *snaps = NULL;
    int nsnaps = 0;
4561
    size_t i;
4562 4563 4564 4565 4566
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

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

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

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

4579 4580 4581 4582 4583 4584 4585
    if (nsnaps > REMOTE_DOMAIN_SNAPSHOT_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domain snapshots '%d' for limit '%d'"),
                       nsnaps, REMOTE_DOMAIN_SNAPSHOT_LIST_MAX);
        goto cleanup;
    }

4586
    if (snaps && nsnaps) {
4587
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602
            goto cleanup;

        ret->snapshots.snapshots_len = nsnaps;

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

    ret->ret = nsnaps;
    rv = 0;

4603
 cleanup:
4604 4605
    if (rv < 0)
        virNetMessageSaveError(rerr);
4606
    virObjectUnref(dom);
4607
    if (snaps && nsnaps > 0)
4608
        for (i = 0; i < nsnaps; i++)
4609
            virObjectUnref(snaps[i]);
4610
    VIR_FREE(snaps);
4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623
    return rv;
}

static int
remoteDispatchDomainSnapshotListAllChildren(virNetServerPtr server ATTRIBUTE_UNUSED,
                                            virNetServerClientPtr client,
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr,
                                            remote_domain_snapshot_list_all_children_args *args,
                                            remote_domain_snapshot_list_all_children_ret *ret)
{
    virDomainSnapshotPtr *snaps = NULL;
    int nsnaps = 0;
4624
    size_t i;
4625 4626 4627 4628 4629 4630
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;
    virDomainSnapshotPtr snapshot = NULL;

    if (!priv->conn) {
4631
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645
        goto cleanup;
    }

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

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

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

4646 4647 4648 4649 4650 4651 4652
    if (nsnaps > REMOTE_DOMAIN_SNAPSHOT_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domain snapshots '%d' for limit '%d'"),
                       nsnaps, REMOTE_DOMAIN_SNAPSHOT_LIST_MAX);
        goto cleanup;
    }

4653
    if (snaps && nsnaps) {
4654
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669
            goto cleanup;

        ret->snapshots.snapshots_len = nsnaps;

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

    ret->ret = nsnaps;
    rv = 0;

4670
 cleanup:
4671 4672
    if (rv < 0)
        virNetMessageSaveError(rerr);
4673
    virObjectUnref(snapshot);
4674
    virObjectUnref(dom);
4675
    if (snaps && nsnaps > 0)
4676
        for (i = 0; i < nsnaps; i++)
4677
            virObjectUnref(snaps[i]);
4678
    VIR_FREE(snaps);
4679 4680 4681
    return rv;
}

4682 4683 4684 4685 4686 4687 4688 4689 4690 4691
static int
remoteDispatchConnectListAllStoragePools(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_connect_list_all_storage_pools_args *args,
                                         remote_connect_list_all_storage_pools_ret *ret)
{
    virStoragePoolPtr *pools = NULL;
    int npools = 0;
4692
    size_t i;
4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

4706 4707 4708 4709 4710 4711 4712
    if (npools > REMOTE_STORAGE_POOL_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many storage pools '%d' for limit '%d'"),
                       npools, REMOTE_STORAGE_POOL_LIST_MAX);
        goto cleanup;
    }

4713
    if (pools && npools) {
4714
        if (VIR_ALLOC_N(ret->pools.pools_val, npools) < 0)
4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729
            goto cleanup;

        ret->pools.pools_len = npools;

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

    ret->ret = npools;

    rv = 0;

4730
 cleanup:
4731 4732
    if (rv < 0)
        virNetMessageSaveError(rerr);
4733
    if (pools && npools > 0)
4734
        for (i = 0; i < npools; i++)
4735
            virObjectUnref(pools[i]);
4736
    VIR_FREE(pools);
4737 4738 4739
    return rv;
}

4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750
static int
remoteDispatchStoragePoolListAllVolumes(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_storage_pool_list_all_volumes_args *args,
                                        remote_storage_pool_list_all_volumes_ret *ret)
{
    virStorageVolPtr *vols = NULL;
    virStoragePoolPtr pool = NULL;
    int nvols = 0;
4751
    size_t i;
4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

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

4768 4769 4770 4771 4772 4773 4774
    if (nvols > REMOTE_STORAGE_VOL_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many storage volumes '%d' for limit '%d'"),
                       nvols, REMOTE_STORAGE_VOL_LIST_MAX);
        goto cleanup;
    }

4775
    if (vols && nvols) {
4776
        if (VIR_ALLOC_N(ret->vols.vols_val, nvols) < 0)
4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791
            goto cleanup;

        ret->vols.vols_len = nvols;

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

    ret->ret = nvols;

    rv = 0;

4792
 cleanup:
4793 4794
    if (rv < 0)
        virNetMessageSaveError(rerr);
4795
    if (vols && nvols > 0)
4796
        for (i = 0; i < nvols; i++)
4797
            virObjectUnref(vols[i]);
4798
    VIR_FREE(vols);
4799
    virObjectUnref(pool);
4800 4801 4802
    return rv;
}

4803 4804 4805 4806 4807 4808 4809 4810 4811 4812
static int
remoteDispatchConnectListAllNetworks(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_connect_list_all_networks_args *args,
                                     remote_connect_list_all_networks_ret *ret)
{
    virNetworkPtr *nets = NULL;
    int nnets = 0;
4813
    size_t i;
4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

4827 4828 4829 4830 4831 4832 4833
    if (nnets > REMOTE_NETWORK_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many networks '%d' for limit '%d'"),
                       nnets, REMOTE_NETWORK_LIST_MAX);
        goto cleanup;
    }

4834
    if (nets && nnets) {
4835
        if (VIR_ALLOC_N(ret->nets.nets_val, nnets) < 0)
4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850
            goto cleanup;

        ret->nets.nets_len = nnets;

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

    ret->ret = nnets;

    rv = 0;

4851
 cleanup:
4852 4853
    if (rv < 0)
        virNetMessageSaveError(rerr);
4854
    if (nets && nnets > 0)
4855
        for (i = 0; i < nnets; i++)
4856
            virObjectUnref(nets[i]);
4857
    VIR_FREE(nets);
4858 4859 4860
    return rv;
}

4861 4862 4863 4864 4865 4866 4867 4868 4869 4870
static int
remoteDispatchConnectListAllInterfaces(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_connect_list_all_interfaces_args *args,
                                       remote_connect_list_all_interfaces_ret *ret)
{
    virInterfacePtr *ifaces = NULL;
    int nifaces = 0;
4871
    size_t i;
4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

4885 4886 4887 4888 4889 4890 4891
    if (nifaces > REMOTE_INTERFACE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many interfaces '%d' for limit '%d'"),
                       nifaces, REMOTE_INTERFACE_LIST_MAX);
        goto cleanup;
    }

4892
    if (ifaces && nifaces) {
4893
        if (VIR_ALLOC_N(ret->ifaces.ifaces_val, nifaces) < 0)
4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908
            goto cleanup;

        ret->ifaces.ifaces_len = nifaces;

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

    ret->ret = nifaces;

    rv = 0;

4909
 cleanup:
4910 4911
    if (rv < 0)
        virNetMessageSaveError(rerr);
4912
    if (ifaces && nifaces > 0)
4913
        for (i = 0; i < nifaces; i++)
4914
            virObjectUnref(ifaces[i]);
4915
    VIR_FREE(ifaces);
4916 4917 4918
    return rv;
}

4919 4920 4921 4922 4923 4924 4925 4926 4927 4928
static int
remoteDispatchConnectListAllNodeDevices(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_connect_list_all_node_devices_args *args,
                                        remote_connect_list_all_node_devices_ret *ret)
{
    virNodeDevicePtr *devices = NULL;
    int ndevices = 0;
4929
    size_t i;
4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

4943 4944 4945 4946 4947 4948 4949
    if (ndevices > REMOTE_NODE_DEVICE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many node devices '%d' for limit '%d'"),
                       ndevices, REMOTE_NODE_DEVICE_LIST_MAX);
        goto cleanup;
    }

4950
    if (devices && ndevices) {
4951
        if (VIR_ALLOC_N(ret->devices.devices_val, ndevices) < 0)
4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966
            goto cleanup;

        ret->devices.devices_len = ndevices;

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

    ret->ret = ndevices;

    rv = 0;

4967
 cleanup:
4968 4969
    if (rv < 0)
        virNetMessageSaveError(rerr);
4970
    if (devices && ndevices > 0)
4971
        for (i = 0; i < ndevices; i++)
4972
            virObjectUnref(devices[i]);
4973
    VIR_FREE(devices);
4974 4975
    return rv;
}
4976

4977 4978 4979 4980 4981 4982 4983 4984 4985 4986
static int
remoteDispatchConnectListAllNWFilters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_connect_list_all_nwfilters_args *args,
                                      remote_connect_list_all_nwfilters_ret *ret)
{
    virNWFilterPtr *filters = NULL;
    int nfilters = 0;
4987
    size_t i;
4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

5001 5002 5003 5004 5005 5006 5007
    if (nfilters > REMOTE_NWFILTER_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many network filters '%d' for limit '%d'"),
                       nfilters, REMOTE_NWFILTER_LIST_MAX);
        goto cleanup;
    }

5008
    if (filters && nfilters) {
5009
        if (VIR_ALLOC_N(ret->filters.filters_val, nfilters) < 0)
5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024
            goto cleanup;

        ret->filters.filters_len = nfilters;

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

    ret->ret = nfilters;

    rv = 0;

5025
 cleanup:
5026 5027
    if (rv < 0)
        virNetMessageSaveError(rerr);
5028
    if (filters && nfilters > 0)
5029
        for (i = 0; i < nfilters; i++)
5030
            virObjectUnref(filters[i]);
5031
    VIR_FREE(filters);
5032 5033 5034
    return rv;
}

5035 5036 5037 5038 5039 5040 5041 5042 5043 5044
static int
remoteDispatchConnectListAllSecrets(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_connect_list_all_secrets_args *args,
                                    remote_connect_list_all_secrets_ret *ret)
{
    virSecretPtr *secrets = NULL;
    int nsecrets = 0;
5045
    size_t i;
5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

5059 5060 5061 5062 5063 5064 5065
    if (nsecrets > REMOTE_SECRET_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many secrets '%d' for limit '%d'"),
                       nsecrets, REMOTE_SECRET_LIST_MAX);
        goto cleanup;
    }

5066
    if (secrets && nsecrets) {
5067
        if (VIR_ALLOC_N(ret->secrets.secrets_val, nsecrets) < 0)
5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082
            goto cleanup;

        ret->secrets.secrets_len = nsecrets;

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

    ret->ret = nsecrets;

    rv = 0;

5083
 cleanup:
5084 5085
    if (rv < 0)
        virNetMessageSaveError(rerr);
5086
    if (secrets && nsecrets > 0)
5087
        for (i = 0; i < nsecrets; i++)
5088
            virObjectUnref(secrets[i]);
5089
    VIR_FREE(secrets);
5090 5091 5092
    return rv;
}

5093 5094 5095 5096 5097 5098 5099 5100 5101
static int
remoteDispatchNodeGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_node_get_memory_parameters_args *args,
                                      remote_node_get_memory_parameters_ret *ret)
{
    virTypedParameterPtr params = NULL;
5102
    int nparams = 0;
5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114
    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;

5115
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
5116 5117 5118
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
5119
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
5120
        goto cleanup;
5121
    nparams = args->nparams;
5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139

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

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

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

5140
 success:
5141 5142
    rv = 0;

5143
 cleanup:
5144 5145
    if (rv < 0)
        virNetMessageSaveError(rerr);
5146
    virTypedParamsFree(params, nparams);
5147 5148 5149
    return rv;
}

5150 5151 5152 5153 5154 5155 5156 5157 5158
static int
remoteDispatchNodeGetCPUMap(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                            virNetMessageErrorPtr rerr,
                            remote_node_get_cpu_map_args *args,
                            remote_node_get_cpu_map_ret *ret)
{
    unsigned char *cpumap = NULL;
5159
    unsigned int online = 0;
5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172
    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;

5173 5174
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
5175 5176 5177 5178
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
5179
    if (args->need_map) {
5180 5181 5182 5183 5184 5185 5186 5187 5188 5189
        ret->cpumap.cpumap_len = VIR_CPU_MAPLEN(cpunum);
        ret->cpumap.cpumap_val = (char *) cpumap;
        cpumap = NULL;
    }

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

    rv = 0;

5190
 cleanup:
5191 5192 5193 5194 5195 5196
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumap);
    return rv;
}

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
static int
lxcDispatchDomainOpenNamespace(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                               virNetMessageErrorPtr rerr,
                               lxc_domain_open_namespace_args *args)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *fdlist = NULL;
    int ret;
    virDomainPtr dom = NULL;
    size_t i;

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

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

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

    /* We shouldn't have received any from the client,
     * but in case they're playing games with us, prevent
     * a resource leak
     */
5230
    for (i = 0; i < msg->nfds; i++)
5231 5232 5233 5234 5235 5236 5237 5238 5239
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

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

    rv = 1;

5240
 cleanup:
5241 5242
    if (rv < 0)
        virNetMessageSaveError(rerr);
5243
    virObjectUnref(dom);
5244 5245 5246
    return rv;
}

5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273
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;

5274 5275 5276 5277 5278 5279 5280
    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;
    }

5281 5282 5283 5284 5285 5286 5287 5288
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       0) < 0)
        goto cleanup;

    rv = 0;

5289
 cleanup:
5290 5291 5292
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virTypedParamsFree(params, nparams);
5293
    virObjectUnref(dom);
5294 5295 5296
    return rv;
}

5297
static int
5298 5299 5300 5301 5302 5303
remoteDispatchDomainMigrateBegin3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_migrate_begin3_params_args *args,
                                        remote_domain_migrate_begin3_params_ret *ret)
5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319
{
    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;
    }

5320 5321 5322 5323 5324 5325 5326
    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;
    }

5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

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

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

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

    rv = 0;

5346
 cleanup:
5347 5348 5349
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5350
    virObjectUnref(dom);
5351 5352 5353 5354
    return rv;
}

static int
5355 5356 5357 5358 5359 5360
remoteDispatchDomainMigratePrepare3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_prepare3_params_args *args,
                                          remote_domain_migrate_prepare3_params_ret *ret)
5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375
{
    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;
    }

5376 5377 5378 5379 5380 5381 5382
    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;
    }

5383 5384 5385 5386 5387 5388
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    /* Wacky world of XDR ... */
5389
    if (VIR_ALLOC(uri_out) < 0)
5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404
        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;

5405
 cleanup:
5406 5407 5408 5409 5410 5411 5412 5413 5414
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(uri_out);
    }
    return rv;
}

static int
5415 5416 5417 5418 5419 5420
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)
5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436
{
    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;
    }

5437 5438 5439 5440 5441 5442 5443
    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;
    }

5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

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

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

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

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

5468
 cleanup:
5469 5470 5471 5472 5473 5474 5475 5476
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
        if (stream) {
            virStreamAbort(st);
            daemonFreeClientStream(client, stream);
        } else {
5477
            virObjectUnref(st);
5478 5479 5480 5481 5482 5483 5484
        }
    }
    return rv;
}


static int
5485 5486 5487 5488 5489 5490
remoteDispatchDomainMigratePerform3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_perform3_params_args *args,
                                          remote_domain_migrate_perform3_params_ret *ret)
5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506
{
    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;
    }

5507 5508 5509 5510 5511 5512 5513
    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;
    }

5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

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

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

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

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

    rv = 0;

5536
 cleanup:
5537 5538 5539
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5540
    virObjectUnref(dom);
5541 5542 5543 5544 5545
    return rv;
}


static int
5546 5547 5548 5549 5550 5551
remoteDispatchDomainMigrateFinish3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_domain_migrate_finish3_params_args *args,
                                         remote_domain_migrate_finish3_params_ret *ret)
5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566
{
    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;
    }

5567 5568 5569 5570 5571 5572 5573
    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;
    }

5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

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

    make_nonnull_domain(&ret->dom, dom);

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

    rv = 0;

5594
 cleanup:
5595 5596 5597 5598 5599
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
    }
5600
    virObjectUnref(dom);
5601 5602 5603 5604 5605
    return rv;
}


static int
5606 5607 5608 5609 5610
remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_confirm3_params_args *args)
5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623
{
    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;
    }

5624 5625 5626 5627 5628 5629 5630
    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;
    }

5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

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

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

    rv = 0;

5647
 cleanup:
5648 5649 5650
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5651
    virObjectUnref(dom);
5652 5653 5654 5655
    return rv;
}


5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699
static int
remoteDispatchConnectGetCPUModelNames(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_connect_get_cpu_model_names_args *args,
                                      remote_connect_get_cpu_model_names_ret *ret)
{
    int len, rv = -1;
    char **models = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

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

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

    ret->ret = len;

    rv = 0;

5700
 cleanup:
5701 5702 5703 5704 5705 5706 5707
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virStringFreeList(models);
    return rv;
}


5708 5709 5710 5711 5712 5713 5714
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)
5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744
{
    int rv = -1;
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *files = NULL;
    unsigned int nfiles = 0;
    size_t i;

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

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

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

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

5745
 cleanup:
5746
    for (i = 0; i < nfiles; i++)
5747 5748 5749 5750
        VIR_FORCE_CLOSE(files[i]);
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5751
    virObjectUnref(dom);
5752 5753 5754 5755
    return rv;
}


5756 5757 5758 5759 5760 5761
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)
5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794
{
    int rv = -1;
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *files = NULL;
    unsigned int nfiles = 0;
    size_t i;

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

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

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

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

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

5795
 cleanup:
5796
    for (i = 0; i < nfiles; i++)
5797 5798 5799 5800
        VIR_FORCE_CLOSE(files[i]);
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5801
    virObjectUnref(dom);
5802 5803 5804 5805
    return rv;
}


5806 5807
static int
remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5808
                                             virNetServerClientPtr client,
5809 5810 5811
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                             remote_connect_network_event_register_any_args *args,
5812
                                             remote_connect_network_event_register_any_ret *ret)
5813 5814 5815
{
    int callbackID;
    int rv = -1;
5816 5817
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
5818 5819
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
5820
    virNetworkPtr net = NULL;
5821 5822 5823 5824 5825 5826 5827 5828

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

    virMutexLock(&priv->lock);

5829 5830 5831 5832
    if (args->net &&
        !(net = get_nonnull_network(priv->conn, *args->net)))
        goto cleanup;

5833 5834 5835
    if (args->eventID >= VIR_NETWORK_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network event ID %d"), args->eventID);
5836 5837 5838
        goto cleanup;
    }

5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853
    /* If we call register first, we could append a complete callback
     * to our array, but on OOM append failure, we'd have to then hope
     * deregister works to undo our register.  So instead we append an
     * incomplete callback to our array, then register, then fix up
     * our callback; but since VIR_APPEND_ELEMENT clears 'callback' on
     * success, we use 'ref' to save a copy of the pointer.  */
    if (VIR_ALLOC(callback) < 0)
        goto cleanup;
    callback->client = client;
    callback->eventID = args->eventID;
    callback->callbackID = -1;
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->networkEventCallbacks,
                           priv->nnetworkEventCallbacks,
                           callback) < 0)
5854 5855 5856
        goto cleanup;

    if ((callbackID = virConnectNetworkEventRegisterAny(priv->conn,
5857
                                                        net,
5858 5859
                                                        args->eventID,
                                                        networkEventCallbacks[args->eventID],
5860 5861 5862 5863 5864
                                                        ref,
                                                        remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->networkEventCallbacks,
                     priv->nnetworkEventCallbacks, 1);
        callback = ref;
5865
        goto cleanup;
5866
    }
5867

5868
    ref->callbackID = callbackID;
5869
    ret->callbackID = callbackID;
5870 5871 5872

    rv = 0;

5873
 cleanup:
5874
    VIR_FREE(callback);
5875 5876
    if (rv < 0)
        virNetMessageSaveError(rerr);
5877
    virObjectUnref(net);
5878 5879 5880 5881 5882 5883 5884
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5885
                                               virNetServerClientPtr client,
5886 5887
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
5888
                                               remote_connect_network_event_deregister_any_args *args)
5889 5890
{
    int rv = -1;
5891
    size_t i;
5892 5893 5894 5895 5896 5897 5898 5899 5900 5901
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    virMutexLock(&priv->lock);

5902
    for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
5903
        if (priv->networkEventCallbacks[i]->callbackID == args->callbackID)
5904 5905
            break;
    }
5906
    if (i == priv->nnetworkEventCallbacks) {
5907
        virReportError(VIR_ERR_INTERNAL_ERROR,
5908 5909
                       _("network event callback %d not registered"),
                       args->callbackID);
5910 5911 5912
        goto cleanup;
    }

5913
    if (virConnectNetworkEventDeregisterAny(priv->conn, args->callbackID) < 0)
5914 5915
        goto cleanup;

5916 5917
    VIR_DELETE_ELEMENT(priv->networkEventCallbacks, i,
                       priv->nnetworkEventCallbacks);
5918 5919 5920

    rv = 0;

5921
 cleanup:
5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}


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

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

    virMutexLock(&priv->lock);

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

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

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

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

    rv = 0;

5991
 cleanup:
5992 5993 5994
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5995
    virObjectUnref(dom);
5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039
    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;

6040
 cleanup:
6041 6042 6043 6044 6045 6046
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}

6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079
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);
6080
    virObjectUnref(dom);
6081 6082
    return rv;
}
6083

M
Michal Privoznik 已提交
6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131

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;
6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146
}

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

6147
    if (VIR_STRDUP(lease_dst->iface, lease_src->iface) < 0 ||
J
Ján Tomko 已提交
6148
        VIR_STRDUP(lease_dst->ipaddr, lease_src->ipaddr) < 0)
6149 6150
        goto error;

J
Ján Tomko 已提交
6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175
    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;
6176 6177 6178 6179

    return 0;

 error:
J
Ján Tomko 已提交
6180 6181 6182 6183 6184 6185 6186 6187
    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);
6188 6189 6190 6191 6192
    VIR_FREE(mac_tmp);
    VIR_FREE(iaid_tmp);
    VIR_FREE(hostname_tmp);
    VIR_FREE(clientid_tmp);
    VIR_FREE(lease_dst->ipaddr);
6193
    VIR_FREE(lease_dst->iface);
6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221
    return -1;
}


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

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

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

    if ((nleases = virNetworkGetDHCPLeases(net,
6222
                                           args->mac ? *args->mac : NULL,
6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256
                                           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);
6257
    if (leases && nleases > 0)
6258 6259
        for (i = 0; i < nleases; i++)
            virNetworkDHCPLeaseFree(leases[i]);
6260
    VIR_FREE(leases);
6261
    virObjectUnref(net);
6262
    return rv;
M
Michal Privoznik 已提交
6263 6264 6265
}


6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351
static int
remoteDispatchConnectGetAllDomainStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_connect_get_all_domain_stats_args *args,
                                       remote_connect_get_all_domain_stats_ret *ret)
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainStatsRecordPtr *retStats = NULL;
    int nrecords = 0;
    virDomainPtr *doms = NULL;

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

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

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

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

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

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

        ret->retStats.retStats_len = nrecords;

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

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

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

    rv = 0;

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

    virDomainStatsRecordListFree(retStats);
    virDomainListFree(doms);

    return rv;
}


M
Michal Privoznik 已提交
6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388
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;
}


6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491
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);
        }
    }
6492
    virObjectUnref(dom);
6493 6494 6495 6496 6497 6498 6499 6500 6501
    if (ninfo >= 0)
        for (i = 0; i < ninfo; i++)
            virDomainFSInfoFree(info[i]);
    VIR_FREE(info);

    return rv;
}


6502 6503 6504 6505 6506 6507 6508 6509 6510
/*----- 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
6511
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
6512 6513
{
    virDomainPtr dom;
6514
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
6515 6516 6517 6518 6519 6520 6521 6522
    /* Should we believe the domain.id sent by the client?  Maybe
     * this should be a check rather than an assignment? XXX
     */
    if (dom) dom->id = domain.id;
    return dom;
}

static virNetworkPtr
6523
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
6524
{
6525
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
6526 6527
}

D
Daniel Veillard 已提交
6528
static virInterfacePtr
6529
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
6530
{
6531
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
6532 6533
}

6534
static virStoragePoolPtr
6535
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
6536
{
6537 6538
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
6539 6540 6541
}

static virStorageVolPtr
6542
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
6543 6544
{
    virStorageVolPtr ret;
6545 6546
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
6547 6548 6549
    return ret;
}

6550
static virSecretPtr
6551
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
6552
{
6553
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
6554 6555
}

6556
static virNWFilterPtr
6557
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
6558
{
6559
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
6560 6561
}

C
Chris Lalancette 已提交
6562
static virDomainSnapshotPtr
6563
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
6564
{
6565
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
6566 6567
}

6568 6569
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
6570
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
6571 6572
{
    dom_dst->id = dom_src->id;
6573
    ignore_value(VIR_STRDUP_QUIET(dom_dst->name, dom_src->name));
6574
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
6575 6576 6577
}

static void
6578
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
6579
{
6580
    ignore_value(VIR_STRDUP_QUIET(net_dst->name, net_src->name));
6581
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
6582 6583
}

D
Daniel Veillard 已提交
6584
static void
6585 6586
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
6587
{
6588 6589
    ignore_value(VIR_STRDUP_QUIET(interface_dst->name, interface_src->name));
    ignore_value(VIR_STRDUP_QUIET(interface_dst->mac, interface_src->mac));
D
Daniel Veillard 已提交
6590 6591
}

6592
static void
6593
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
6594
{
6595
    ignore_value(VIR_STRDUP_QUIET(pool_dst->name, pool_src->name));
6596
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
6597 6598 6599
}

static void
6600
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
6601
{
6602 6603 6604
    ignore_value(VIR_STRDUP_QUIET(vol_dst->pool, vol_src->pool));
    ignore_value(VIR_STRDUP_QUIET(vol_dst->name, vol_src->name));
    ignore_value(VIR_STRDUP_QUIET(vol_dst->key, vol_src->key));
6605
}
6606 6607

static void
6608
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
6609
{
6610
    ignore_value(VIR_STRDUP_QUIET(dev_dst->name, dev_src->name));
6611
}
6612 6613

static void
6614
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
6615
{
6616
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
6617
    secret_dst->usageType = secret_src->usageType;
6618
    ignore_value(VIR_STRDUP_QUIET(secret_dst->usageID, secret_src->usageID));
6619
}
6620 6621

static void
6622
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
6623
{
6624
    ignore_value(VIR_STRDUP_QUIET(nwfilter_dst->name, nwfilter_src->name));
6625
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
6626
}
C
Chris Lalancette 已提交
6627 6628

static void
6629
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
6630
{
6631
    ignore_value(VIR_STRDUP_QUIET(snapshot_dst->name, snapshot_src->name));
6632
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
6633
}
6634 6635 6636 6637 6638 6639 6640 6641

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;
6642
    size_t i = 0;
6643

6644
    if (VIR_ALLOC_N(val, nerrors) < 0)
6645
        goto error;
6646 6647

    for (i = 0; i < nerrors; i++) {
6648 6649
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
6650 6651 6652 6653 6654 6655 6656 6657
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

6658
 error:
6659
    if (val) {
6660
        size_t j;
6661 6662 6663 6664 6665 6666
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}