remote.c 209.7 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007-2014 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 507
    }

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

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

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

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

    return 0;
544

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

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

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

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

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

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

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

606

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

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

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

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

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

    return 0;
}


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

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

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

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

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

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

    make_nonnull_domain(&data.dom, dom);

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

    return 0;

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


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

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

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

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

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

    make_nonnull_domain(&data.dom, dom);

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

    return 0;
}

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

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

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

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

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

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

    return 0;
}

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

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

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

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

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

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

    return 0;
}

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

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

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

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

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

    return 0;
}


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

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

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

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

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

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

    return 0;
}

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

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

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

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

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

    make_nonnull_domain(&data.dom, dom);

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

    return 0;
}

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


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

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

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


1012
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
1013
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
1014
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
1015
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
1016
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
1017
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
1018
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
1019
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
1020
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
1021
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
1022
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
1023
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
1024
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
1025
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
1026
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
1027
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
1028
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
1029
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob2),
1030
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTunable),
1031 1032 1033 1034
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

1035
static int
1036
remoteRelayNetworkEventLifecycle(virConnectPtr conn,
1037 1038 1039 1040
                                 virNetworkPtr net,
                                 int event,
                                 int detail,
                                 void *opaque)
1041
{
1042
    daemonClientEventCallbackPtr callback = opaque;
1043 1044
    remote_network_event_lifecycle_msg data;

1045 1046
    if (callback->callbackID < 0 ||
        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
1047 1048
        return -1;

1049 1050
    VIR_DEBUG("Relaying network lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);
1051 1052 1053 1054

    /* build return data */
    memset(&data, 0, sizeof(data));
    make_nonnull_network(&data.net, net);
1055
    data.callbackID = callback->callbackID;
1056 1057 1058
    data.event = event;
    data.detail = detail;

1059
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
                                  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);

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
static 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;

1113
 error:
1114 1115 1116 1117
    VIR_FREE(data.event);
    VIR_FREE(details_p);
}

1118 1119 1120 1121 1122 1123 1124
/*
 * 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
 */
1125
void remoteClientFreeFunc(void *data)
1126 1127 1128 1129 1130
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
1131
        virIdentityPtr sysident = virIdentityGetSystem();
1132
        size_t i;
1133

1134 1135
        virIdentitySetCurrent(sysident);

1136 1137 1138 1139 1140
        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;
1141
            }
1142 1143 1144 1145 1146
            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");
1147
        }
1148
        VIR_FREE(priv->domainEventCallbacks);
1149

1150 1151 1152 1153 1154
        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;
1155
            }
1156 1157 1158 1159 1160 1161
            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");
1162
        }
1163
        VIR_FREE(priv->networkEventCallbacks);
1164

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
        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);

1180
        virConnectClose(priv->conn);
1181 1182 1183

        virIdentitySetCurrent(NULL);
        virObjectUnref(sysident);
1184 1185 1186 1187 1188 1189
    }

    VIR_FREE(priv);
}


1190 1191 1192 1193 1194 1195 1196
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

1197

1198 1199
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
1200 1201 1202
{
    struct daemonClientPrivate *priv;

1203
    if (VIR_ALLOC(priv) < 0)
1204
        return NULL;
1205 1206 1207

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
1208
        virReportSystemError(errno, "%s", _("unable to init mutex"));
1209
        return NULL;
1210 1211
    }

1212
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
1213
    return priv;
1214 1215
}

1216 1217 1218
/*----- Functions. -----*/

static int
1219 1220 1221 1222 1223
remoteDispatchConnectOpen(virNetServerPtr server,
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
1224 1225
{
    const char *name;
1226
    unsigned int flags;
1227
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
1228
    int rv = -1;
1229

1230 1231 1232 1233
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
1234
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
1235 1236 1237
        goto cleanup;
    }

1238
    if (virNetServerKeepAliveRequired(server) && !priv->keepalive_supported) {
1239 1240
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("keepalive support is required to connect"));
1241 1242 1243
        goto cleanup;
    }

1244 1245 1246 1247 1248 1249
    name = args->name ? *args->name : NULL;

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

1253
    priv->conn =
1254
        flags & VIR_CONNECT_RO
1255 1256
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
1257

1258
    if (priv->conn == NULL)
1259 1260 1261
        goto cleanup;

    rv = 0;
1262

1263
 cleanup:
1264
    if (rv < 0)
1265 1266
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
1267
    return rv;
1268 1269 1270 1271
}


static int
1272 1273 1274 1275
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
1276
{
1277
    virNetServerClientDelayedClose(client);
1278
    return 0;
1279 1280
}

1281

1282
static int
1283 1284
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1285
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1286
                                     virNetMessageErrorPtr rerr,
1287 1288
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
1289
{
1290
    virDomainPtr dom = NULL;
1291 1292
    char *type;
    int nparams;
1293
    int rv = -1;
1294 1295
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1296

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

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

1305
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
1306
        goto cleanup;
1307 1308 1309

    ret->type = type;
    ret->nparams = nparams;
1310 1311
    rv = 0;

1312
 cleanup:
1313
    if (rv < 0)
1314
        virNetMessageSaveError(rerr);
1315 1316 1317
    if (dom)
        virDomainFree(dom);
    return rv;
1318 1319
}

1320 1321
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
1322 1323 1324
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
1325
                               remote_typed_param **ret_params_val,
1326 1327
                               u_int *ret_params_len,
                               unsigned int flags)
1328
{
1329 1330
    size_t i;
    size_t j;
1331 1332 1333 1334
    int rv = -1;
    remote_typed_param *val;

    *ret_params_len = nparams;
1335
    if (VIR_ALLOC_N(val, nparams) < 0)
1336 1337
        goto cleanup;

1338
    for (i = 0, j = 0; i < nparams; ++i) {
1339 1340 1341 1342 1343
        /* 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)) {
1344 1345 1346 1347
            --*ret_params_len;
            continue;
        }

1348
        /* remoteDispatchClientRequest will free this: */
1349
        if (VIR_STRDUP(val[j].field, params[i].field) < 0)
1350
            goto cleanup;
1351
        val[j].value.type = params[i].type;
1352
        switch (params[i].type) {
1353
        case VIR_TYPED_PARAM_INT:
1354
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
1355 1356
            break;
        case VIR_TYPED_PARAM_UINT:
1357
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
1358 1359
            break;
        case VIR_TYPED_PARAM_LLONG:
1360
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
1361 1362
            break;
        case VIR_TYPED_PARAM_ULLONG:
1363
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
1364 1365
            break;
        case VIR_TYPED_PARAM_DOUBLE:
1366
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
1367 1368
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
1369 1370 1371
            val[j].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
        case VIR_TYPED_PARAM_STRING:
1372
            if (VIR_STRDUP(val[j].value.remote_typed_param_value_u.s, params[i].value.s) < 0)
1373
                goto cleanup;
1374 1375
            break;
        default:
1376 1377
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
1378 1379
            goto cleanup;
        }
1380
        j++;
1381 1382 1383 1384 1385 1386
    }

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

1387
 cleanup:
1388
    if (val) {
1389
        for (i = 0; i < nparams; i++) {
1390
            VIR_FREE(val[i].field);
1391
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
1392 1393
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
1394 1395 1396 1397 1398 1399 1400
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
1401 1402
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
1403 1404 1405
                                 int limit,
                                 int *nparams)
{
1406
    size_t i = 0;
1407 1408 1409 1410
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
1411
    if (limit && args_params_len > limit) {
1412
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1413 1414
        goto cleanup;
    }
1415
    if (VIR_ALLOC_N(params, args_params_len) < 0)
1416 1417 1418 1419 1420 1421 1422 1423
        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) {
1424 1425 1426
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Parameter %s too big for destination"),
                           args_params_val[i].field);
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
            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;
1455
        case VIR_TYPED_PARAM_STRING:
1456 1457
            if (VIR_STRDUP(params[i].value.s,
                           args_params_val[i].value.remote_typed_param_value_u.s) < 0)
1458 1459
                goto cleanup;
            break;
1460
        default:
1461 1462
            virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                           params[i].type);
1463 1464 1465 1466 1467 1468
            goto cleanup;
        }
    }

    rv = 0;

1469
 cleanup:
1470
    if (rv < 0) {
1471 1472
        virTypedParamsFree(params, i);
        params = NULL;
1473
    }
1474 1475 1476
    return params;
}

1477
static int
1478 1479
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
1480
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
1481
                                           virNetMessageErrorPtr rerr,
1482 1483
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
1484
{
1485
    virDomainPtr dom = NULL;
1486
    virTypedParameterPtr params = NULL;
1487
    int nparams = 0;
1488
    int rv = -1;
1489 1490
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1491

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

1497
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1498
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1499
        goto cleanup;
1500
    }
1501
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1502
        goto cleanup;
1503
    nparams = args->nparams;
1504

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

1508
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1509
        goto cleanup;
1510

1511
    if (remoteSerializeTypedParameters(params, nparams,
1512
                                       &ret->params.params_val,
1513 1514
                                       &ret->params.params_len,
                                       0) < 0)
1515 1516 1517 1518
        goto cleanup;

    rv = 0;

1519
 cleanup:
1520
    if (rv < 0)
1521
        virNetMessageSaveError(rerr);
1522
    virTypedParamsFree(params, nparams);
1523 1524 1525 1526 1527
    if (dom)
        virDomainFree(dom);
    return rv;
}

1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
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;
1538
    size_t i;
1539 1540 1541 1542
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1543
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1544 1545 1546 1547 1548 1549 1550 1551
        goto cleanup;
    }

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

1552 1553 1554 1555 1556 1557 1558
    if (ndomains > REMOTE_DOMAIN_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domains '%d' for limit '%d'"),
                       ndomains, REMOTE_DOMAIN_LIST_MAX);
        goto cleanup;
    }

1559
    if (doms && ndomains) {
1560
        if (VIR_ALLOC_N(ret->domains.domains_val, ndomains) < 0)
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
            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;

1576
 cleanup:
1577 1578
    if (rv < 0)
        virNetMessageSaveError(rerr);
1579
    if (doms && ndomains > 0) {
1580 1581 1582 1583 1584 1585 1586
        for (i = 0; i < ndomains; i++)
            virDomainFree(doms[i]);
        VIR_FREE(doms);
    }
    return rv;
}

1587
static int
1588 1589
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1590
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1591
                                                virNetMessageErrorPtr rerr,
1592 1593 1594 1595 1596
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1597
    int nparams = 0;
1598
    int rv = -1;
1599 1600
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1601

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

1607
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1608
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1609 1610
        goto cleanup;
    }
1611
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1612
        goto cleanup;
1613
    nparams = args->nparams;
1614

1615
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1616 1617 1618 1619 1620 1621 1622
        goto cleanup;

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

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

1628
    rv = 0;
1629

1630
 cleanup:
1631
    if (rv < 0)
1632
        virNetMessageSaveError(rerr);
1633
    virTypedParamsFree(params, nparams);
1634 1635 1636
    if (dom)
        virDomainFree(dom);
    return rv;
1637 1638
}

1639
static int
1640 1641
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1642
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1643
                                virNetMessageErrorPtr rerr,
1644 1645
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1646
{
1647
    virDomainPtr dom = NULL;
1648
    virDomainMemoryStatPtr stats = NULL;
1649 1650
    int nr_stats;
    size_t i;
1651
    int rv = -1;
1652 1653
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1654

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

1660
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1661 1662
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1663
        goto cleanup;
1664 1665
    }

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

    /* Allocate stats array for making dispatch call */
1670
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
1671
        goto cleanup;
1672

1673
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1674
    if (nr_stats < 0)
1675
        goto cleanup;
1676 1677

    /* Allocate return buffer */
1678
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
1679
        goto cleanup;
1680 1681 1682 1683 1684 1685 1686

    /* 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;
1687 1688
    rv = 0;

1689
 cleanup:
1690
    if (rv < 0)
1691
        virNetMessageSaveError(rerr);
1692 1693
    if (dom)
        virDomainFree(dom);
1694
    VIR_FREE(stats);
1695
    return rv;
1696 1697
}

1698
static int
1699 1700
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1701
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1702
                              virNetMessageErrorPtr rerr,
1703 1704
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1705
{
1706
    virDomainPtr dom = NULL;
1707 1708 1709 1710
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1711
    int rv = -1;
1712 1713
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1714

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

1720
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1721
        goto cleanup;
1722 1723 1724 1725 1726 1727
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1728 1729
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1730
        goto cleanup;
1731 1732 1733
    }

    ret->buffer.buffer_len = size;
1734
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1735
        goto cleanup;
1736

1737
    if (virDomainBlockPeek(dom, path, offset, size,
1738
                           ret->buffer.buffer_val, flags) < 0)
1739
        goto cleanup;
1740

1741 1742
    rv = 0;

1743
 cleanup:
1744
    if (rv < 0) {
1745
        virNetMessageSaveError(rerr);
1746 1747 1748 1749 1750
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
1751 1752
}

1753 1754 1755
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1756
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1757 1758 1759 1760 1761 1762 1763
                                    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;
1764
    int nparams = 0;
1765 1766 1767 1768 1769 1770
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1771
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1772 1773 1774 1775 1776 1777 1778
        goto cleanup;
    }

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

1779
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1780
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1781 1782
        goto cleanup;
    }
1783
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1784
        goto cleanup;
1785
    nparams = args->nparams;
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800

    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,
1801 1802
                                       &ret->params.params_len,
                                       args->flags) < 0)
1803 1804
        goto cleanup;

1805
 success:
1806 1807
    rv = 0;

1808
 cleanup:
1809
    if (rv < 0)
1810
        virNetMessageSaveError(rerr);
1811
    virTypedParamsFree(params, nparams);
A
ajia@redhat.com 已提交
1812 1813
    if (dom)
        virDomainFree(dom);
1814 1815 1816
    return rv;
}

R
Richard W.M. Jones 已提交
1817
static int
1818 1819
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1820
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1821
                               virNetMessageErrorPtr rerr,
1822 1823
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1824
{
1825
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1826 1827 1828
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1829
    int rv = -1;
1830 1831
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1832

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

1838
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1839
        goto cleanup;
R
Richard W.M. Jones 已提交
1840 1841 1842 1843 1844
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1845 1846
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1847
        goto cleanup;
R
Richard W.M. Jones 已提交
1848 1849 1850
    }

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

1854
    if (virDomainMemoryPeek(dom, offset, size,
1855
                            ret->buffer.buffer_val, flags) < 0)
1856
        goto cleanup;
R
Richard W.M. Jones 已提交
1857

1858 1859
    rv = 0;

1860
 cleanup:
1861
    if (rv < 0) {
1862
        virNetMessageSaveError(rerr);
1863 1864 1865 1866 1867
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
1868 1869
}

1870
static int
1871 1872
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1873
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1874
                                     virNetMessageErrorPtr rerr,
1875 1876
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1877
{
1878 1879
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1880
    int rv = -1;
1881 1882
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1883

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

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

1892
    if (VIR_ALLOC(seclabel) < 0)
1893 1894 1895 1896 1897 1898
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
1899
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
1900
        goto cleanup;
1901 1902
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1903

1904 1905
    rv = 0;

1906
 cleanup:
1907
    if (rv < 0)
1908
        virNetMessageSaveError(rerr);
1909 1910 1911
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
1912
    return rv;
1913 1914
}

M
Marcelo Cerri 已提交
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
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;
1925 1926
    int len, rv = -1;
    size_t i;
M
Marcelo Cerri 已提交
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
    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;
    }

1945
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
1946 1947 1948 1949 1950
        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];
1951
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0)
M
Marcelo Cerri 已提交
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
            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;

1963
 done:
M
Marcelo Cerri 已提交
1964 1965
    rv = 0;

1966
 cleanup:
M
Marcelo Cerri 已提交
1967 1968 1969 1970 1971 1972 1973 1974
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabels);
    return rv;
}

1975
static int
1976 1977
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1978
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1979
                                   virNetMessageErrorPtr rerr,
1980
                                   remote_node_get_security_model_ret *ret)
1981
{
1982
    virSecurityModel secmodel;
1983
    int rv = -1;
1984 1985
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1986

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

1992
    memset(&secmodel, 0, sizeof(secmodel));
1993
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
1994 1995 1996
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
1997
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
1998 1999 2000 2001
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
2002
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
2003
        goto cleanup;
2004
    strcpy(ret->doi.doi_val, secmodel.doi);
2005

2006 2007
    rv = 0;

2008
 cleanup:
2009
    if (rv < 0)
2010
        virNetMessageSaveError(rerr);
2011
    return rv;
2012 2013
}

2014
static int
2015 2016
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2017
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2018
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
2019 2020
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
2021 2022 2023 2024 2025
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
2026 2027
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2028

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

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

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
2038
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
2039 2040 2041 2042 2043
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
2044
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2045 2046 2047 2048 2049 2050
        goto cleanup;
    }

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

E
Eric Blake 已提交
2053
    if ((num = virDomainGetVcpuPinInfo(dom,
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
                                       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;
2067 2068 2069 2070
    cpumaps = NULL;

    rv = 0;

2071
 cleanup:
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    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;

2108
 cleanup:
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    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)
2143
        goto cleanup;
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153

    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;
2154 2155 2156 2157
    cpumaps = NULL;

    rv = 0;

2158
 cleanup:
2159
    if (rv < 0)
2160
        virNetMessageSaveError(rerr);
2161 2162 2163 2164 2165 2166
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;
}

2167
static int
2168 2169
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
2170
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
2171
                             virNetMessageErrorPtr rerr,
2172 2173
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
2174
{
2175
    virDomainPtr dom = NULL;
2176 2177
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
2178 2179
    int info_len;
    size_t i;
2180
    int rv = -1;
2181 2182
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2183

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

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

2192
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
2193
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
2194
        goto cleanup;
2195
    }
2196

2197 2198
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
2199
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2200 2201 2202 2203 2204
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
2205
        goto cleanup;
2206 2207
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
2208
        goto cleanup;
2209 2210 2211 2212 2213 2214 2215 2216 2217

    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)
2218
        goto cleanup;
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235

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

2237
 cleanup:
2238
    if (rv < 0) {
2239
        virNetMessageSaveError(rerr);
2240 2241 2242 2243
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
2244 2245 2246
    if (dom)
        virDomainFree(dom);
    return rv;
2247 2248 2249
}

static int
2250 2251
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2252
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2253
                                   virNetMessageErrorPtr rerr,
2254 2255
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
2256
{
2257 2258 2259 2260 2261
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2262
    int rv = -1;
2263 2264
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2265

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

2271 2272 2273 2274
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
2275
    if (VIR_ALLOC(uri_out) < 0)
2276
        goto cleanup;
2277

2278
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
2279 2280
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
2281
        goto cleanup;
2282

2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
    /* 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;
    }
2294

2295
    rv = 0;
2296

2297
 cleanup:
2298
    if (rv < 0)
2299
        virNetMessageSaveError(rerr);
2300
    VIR_FREE(uri_out);
2301
    return rv;
2302 2303
}

2304
static int
2305 2306
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2307
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2308
                                    virNetMessageErrorPtr rerr,
2309 2310
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
2311
{
2312 2313 2314 2315 2316
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2317
    int rv = -1;
2318 2319
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2320

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

2326 2327
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
2328

2329
    /* Wacky world of XDR ... */
2330
    if (VIR_ALLOC(uri_out) < 0)
2331
        goto cleanup;
2332

2333
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
2334 2335 2336
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
2337
        goto cleanup;
2338

2339 2340 2341 2342 2343 2344
    /* 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;
2345

2346 2347
    rv = 0;

2348
 cleanup:
2349
    if (rv < 0) {
2350
        virNetMessageSaveError(rerr);
2351 2352
        VIR_FREE(uri_out);
    }
2353
    return rv;
2354 2355
}

C
Chris Lalancette 已提交
2356
static int
2357 2358
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
2359
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
2360 2361 2362
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
2363
{
2364
    virDomainPtr dom = NULL;
2365
    virTypedParameterPtr params = NULL;
2366
    int nparams = 0;
2367
    unsigned int flags;
2368
    int rv = -1;
2369 2370
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2371

2372
    if (!priv->conn) {
2373
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2374
        goto cleanup;
2375
    }
C
Chris Lalancette 已提交
2376

2377
    flags = args->flags;
C
Chris Lalancette 已提交
2378

2379
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2380
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2381 2382
        goto cleanup;
    }
2383
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2384
        goto cleanup;
2385
    nparams = args->nparams;
C
Chris Lalancette 已提交
2386

2387
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2388
        goto cleanup;
C
Chris Lalancette 已提交
2389

2390
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
2391
        goto cleanup;
C
Chris Lalancette 已提交
2392

2393 2394 2395 2396 2397 2398
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
2399 2400
    }

2401
    if (remoteSerializeTypedParameters(params, nparams,
2402
                                       &ret->params.params_val,
2403 2404
                                       &ret->params.params_len,
                                       args->flags) < 0)
2405
        goto cleanup;
2406

2407
 success:
2408 2409
    rv = 0;

2410
 cleanup:
2411
    if (rv < 0)
2412
        virNetMessageSaveError(rerr);
2413
    virTypedParamsFree(params, nparams);
2414 2415 2416
    if (dom)
        virDomainFree(dom);
    return rv;
2417 2418
}

2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
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;
2429
    int nparams = 0;
2430 2431 2432 2433 2434 2435
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2436
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2437 2438 2439 2440 2441
        goto cleanup;
    }

    flags = args->flags;

2442
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
2443
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2444 2445
        goto cleanup;
    }
2446
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2447
        goto cleanup;
2448
    nparams = args->nparams;
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469

    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;

2470
 success:
2471 2472
    rv = 0;

2473
 cleanup:
2474 2475
    if (rv < 0)
        virNetMessageSaveError(rerr);
2476
    virTypedParamsFree(params, nparams);
2477 2478 2479 2480 2481
    if (dom)
        virDomainFree(dom);
    return rv;
}

2482
static int
2483 2484
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
2485
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2486 2487 2488
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
2489
{
2490
    virDomainPtr dom = NULL;
2491
    virTypedParameterPtr params = NULL;
2492
    int nparams = 0;
2493
    unsigned int flags;
2494
    int rv = -1;
2495 2496
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2497

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

2503 2504
    flags = args->flags;

2505
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2506
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2507
        goto cleanup;
2508
    }
2509
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2510
        goto cleanup;
2511
    nparams = args->nparams;
2512

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

2516
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2517
        goto cleanup;
2518

2519 2520 2521 2522 2523 2524 2525 2526
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2527
    if (remoteSerializeTypedParameters(params, nparams,
2528
                                       &ret->params.params_val,
2529 2530
                                       &ret->params.params_len,
                                       args->flags) < 0)
2531
        goto cleanup;
2532

2533
 success:
2534
    rv = 0;
2535

2536
 cleanup:
2537
    if (rv < 0)
2538
        virNetMessageSaveError(rerr);
2539
    virTypedParamsFree(params, nparams);
2540 2541 2542
    if (dom)
        virDomainFree(dom);
    return rv;
2543 2544
}

2545
static int
2546 2547
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2548
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2549 2550 2551
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2552
{
2553
    virNodeCPUStatsPtr params = NULL;
2554
    size_t i;
2555
    int cpuNum = args->cpuNum;
2556
    int nparams = 0;
2557 2558
    unsigned int flags;
    int rv = -1;
2559 2560
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2561

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

    flags = args->flags;

2569
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
2570
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2571 2572
        goto cleanup;
    }
2573
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2574
        goto cleanup;
2575
    nparams = args->nparams;
2576

2577
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
        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)
2591
        goto cleanup;
2592 2593 2594

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2595 2596
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2597 2598 2599 2600

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

2601
 success:
2602 2603
    rv = 0;

2604
 cleanup:
2605
    if (rv < 0) {
2606
        virNetMessageSaveError(rerr);
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617
        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
2618 2619
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2620
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2621 2622 2623
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2624
{
2625
    virNodeMemoryStatsPtr params = NULL;
2626
    size_t i;
2627
    int cellNum = args->cellNum;
2628
    int nparams = 0;
2629 2630
    unsigned int flags;
    int rv = -1;
2631 2632
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2633

2634
    if (!priv->conn) {
2635
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2636 2637 2638 2639 2640
        goto cleanup;
    }

    flags = args->flags;

2641
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
2642
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2643 2644
        goto cleanup;
    }
2645
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2646
        goto cleanup;
2647
    nparams = args->nparams;
2648

2649
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
        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)
2663
        goto cleanup;
2664 2665 2666

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2667 2668
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2669 2670 2671 2672

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

2673
 success:
2674 2675
    rv = 0;

2676
 cleanup:
2677
    if (rv < 0) {
2678
        virNetMessageSaveError(rerr);
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
        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;
}

2689 2690 2691
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2692
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
                                    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) {
2704
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721
        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;

2722
 cleanup:
2723 2724 2725 2726 2727 2728 2729
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
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;
2741
    int nparams = 0;
2742 2743 2744 2745
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

2750
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
2751
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2752 2753 2754
        goto cleanup;
    }

2755
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2756
        goto cleanup;
2757
    nparams = args->nparams;
2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780

    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;

2781
 success:
2782 2783
    rv = 0;

2784
 cleanup:
2785 2786
    if (rv < 0)
        virNetMessageSaveError(rerr);
2787
    virTypedParamsFree(params, nparams);
2788 2789 2790 2791
    if (dom)
        virDomainFree(dom);
    return rv;
}
2792

D
Daniel Veillard 已提交
2793 2794
/*-------------------------------------------------------------*/

2795
static int
2796
remoteDispatchAuthList(virNetServerPtr server,
2797
                       virNetServerClientPtr client,
2798
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2799
                       virNetMessageErrorPtr rerr,
2800
                       remote_auth_list_ret *ret)
2801
{
2802
    int rv = -1;
2803 2804
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2805
    gid_t callerGid;
2806
    pid_t callerPid;
2807
    unsigned long long timestamp;
2808 2809 2810 2811 2812 2813

    /* 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) {
2814
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2815
                                              &callerPid, &timestamp) < 0) {
2816 2817 2818 2819
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2820 2821
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
2822
                            (long long) callerPid, (int) callerUid) < 0)
J
Jim Fehlig 已提交
2823 2824
                goto cleanup;
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
2825
            virNetServerClientSetAuth(client, 0);
2826
            virNetServerTrackCompletedAuth(server);
2827
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
2828
            VIR_FREE(ident);
2829 2830
        }
    }
2831

2832
    ret->types.types_len = 1;
2833
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
2834
        goto cleanup;
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848

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

2850 2851
    rv = 0;

2852
 cleanup:
2853
    if (rv < 0)
2854
        virNetMessageSaveError(rerr);
2855
    return rv;
2856 2857 2858
}


2859
#ifdef WITH_SASL
2860 2861
/*
 * Initializes the SASL session in prepare for authentication
2862
 * and gives the client a list of allowed mechanisms to choose
2863 2864
 */
static int
2865 2866
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2867
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2868
                           virNetMessageErrorPtr rerr,
2869
                           remote_auth_sasl_init_ret *ret)
2870
{
2871 2872 2873
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2874

2875
    virMutexLock(&priv->lock);
2876

2877 2878 2879
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2880
        VIR_ERROR(_("client tried invalid SASL init request"));
2881
        goto authfail;
2882 2883
    }

2884 2885 2886 2887 2888
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2889
        goto authfail;
2890

2891
# if WITH_GNUTLS
2892
    /* Inform SASL that we've got an external SSF layer from TLS */
2893 2894 2895 2896
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2897
            goto authfail;
2898 2899 2900 2901 2902

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2903
            goto authfail;
2904
    }
2905
# endif
2906

2907
    if (virNetServerClientIsSecure(client))
2908
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2909 2910
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2911
        /* Plain TCP, better get an SSF layer */
2912 2913 2914 2915
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
2916

2917
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
2918
        goto authfail;
2919
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
2920

2921 2922
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
2923
    return 0;
2924

2925
 authfail:
2926
    virResetLastError();
2927 2928
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2929
    virNetMessageSaveError(rerr);
2930 2931 2932
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2933
    virObjectUnref(sasl);
2934
    virMutexUnlock(&priv->lock);
2935
    return -1;
2936 2937
}

2938
/*
2939 2940
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
2941
static int
2942 2943
remoteSASLFinish(virNetServerPtr server,
                 virNetServerClientPtr client)
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
{
    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;
        }
2959
    }
2960 2961

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
2962
        return -2;
2963

2964 2965
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
2966

2967
    virNetServerClientSetAuth(client, 0);
2968
    virNetServerTrackCompletedAuth(server);
2969
    virNetServerClientSetSASLSession(client, priv->sasl);
2970

2971
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
2972

2973 2974 2975
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2976

2977
    virObjectUnref(priv->sasl);
2978
    priv->sasl = NULL;
2979

2980
    return 0;
2981

2982
 error:
2983 2984
    return -1;
}
2985

2986 2987 2988 2989
/*
 * This starts the SASL authentication negotiation.
 */
static int
2990
remoteDispatchAuthSaslStart(virNetServerPtr server,
2991
                            virNetServerClientPtr client,
2992
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2993
                            virNetMessageErrorPtr rerr,
2994 2995
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2996 2997
{
    const char *serverout;
2998
    size_t serveroutlen;
2999
    int err;
3000 3001 3002
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3003
    const char *identity;
3004

3005
    virMutexLock(&priv->lock);
3006

3007 3008 3009
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
3010
        VIR_ERROR(_("client tried invalid SASL start request"));
3011
        goto authfail;
3012 3013
    }

3014 3015
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
3016 3017 3018 3019 3020 3021 3022 3023 3024
    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)
3025
        goto authfail;
3026

3027
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3028
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
3029
        goto authfail;
3030 3031 3032 3033
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3034 3035
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3036 3037 3038 3039 3040 3041 3042
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3043
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3044
    if (err == VIR_NET_SASL_CONTINUE) {
3045 3046
        ret->complete = 0;
    } else {
3047
        /* Check username whitelist ACL */
3048
        if ((err = remoteSASLFinish(server, client)) < 0) {
3049 3050 3051 3052 3053
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3054

3055 3056 3057
        ret->complete = 1;
    }

3058
    virMutexUnlock(&priv->lock);
3059
    return 0;
3060

3061
 authfail:
3062 3063 3064
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3065 3066
    goto error;

3067
 authdeny:
3068
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3069 3070 3071
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3072 3073
    goto error;

3074
 error:
3075
    virObjectUnref(priv->sasl);
3076 3077
    priv->sasl = NULL;
    virResetLastError();
3078 3079
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3080 3081 3082
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3083
    return -1;
3084 3085 3086 3087
}


static int
3088
remoteDispatchAuthSaslStep(virNetServerPtr server,
3089
                           virNetServerClientPtr client,
3090
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3091
                           virNetMessageErrorPtr rerr,
3092 3093
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
3094 3095
{
    const char *serverout;
3096
    size_t serveroutlen;
3097
    int err;
3098 3099 3100
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3101
    const char *identity;
3102

3103 3104 3105 3106 3107
    virMutexLock(&priv->lock);

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

3112
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
3113
              args->data.data_len, args->nil);
3114 3115 3116 3117 3118 3119 3120 3121
    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)
3122
        goto authfail;
3123 3124

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3125
        VIR_ERROR(_("sasl step reply data too long %d"),
3126
                  (int)serveroutlen);
3127
        goto authfail;
3128 3129 3130 3131
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3132 3133
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3134 3135 3136 3137 3138 3139 3140
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3141
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3142
    if (err == VIR_NET_SASL_CONTINUE) {
3143 3144
        ret->complete = 0;
    } else {
3145
        /* Check username whitelist ACL */
3146
        if ((err = remoteSASLFinish(server, client)) < 0) {
3147 3148 3149 3150 3151
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3152

3153 3154 3155
        ret->complete = 1;
    }

3156
    virMutexUnlock(&priv->lock);
3157
    return 0;
3158

3159
 authfail:
3160 3161 3162
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3163 3164
    goto error;

3165
 authdeny:
3166
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3167 3168 3169
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3170 3171
    goto error;

3172
 error:
3173
    virObjectUnref(priv->sasl);
3174 3175
    priv->sasl = NULL;
    virResetLastError();
3176 3177
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3178 3179 3180
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3181 3182
    return -1;
}
3183 3184 3185 3186
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3187
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3188 3189 3190 3191
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3192 3193
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3194 3195 3196 3197 3198 3199
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
3200
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3201 3202 3203 3204 3205
                            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");
3206 3207
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3208 3209 3210 3211 3212 3213
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3214
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3215 3216 3217 3218 3219
                           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");
3220 3221
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3222 3223 3224 3225
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
3226 3227 3228



3229
static int
3230
remoteDispatchAuthPolkit(virNetServerPtr server,
3231
                         virNetServerClientPtr client,
3232
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3233
                         virNetMessageErrorPtr rerr,
3234
                         remote_auth_polkit_ret *ret)
3235
{
3236
    pid_t callerPid = -1;
3237
    gid_t callerGid = -1;
3238
    uid_t callerUid = -1;
3239
    unsigned long long timestamp;
3240
    const char *action;
3241
    char *ident = NULL;
3242 3243
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3244
    int rv;
3245

3246 3247
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
3248 3249 3250
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

3251 3252
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3253
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3254 3255 3256
        goto authfail;
    }

3257
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
3258
                                          &callerPid, &timestamp) < 0) {
3259 3260 3261
        goto authfail;
    }

3262 3263 3264 3265 3266 3267
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

3268 3269
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3270

3271 3272 3273 3274 3275 3276 3277
    rv = virPolkitCheckAuth(action,
                            callerPid,
                            timestamp,
                            callerUid,
                            NULL,
                            true);
    if (rv == -1)
3278
        goto authfail;
3279
    else if (rv == -2)
3280
        goto authdeny;
3281

3282 3283 3284
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3285 3286
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
3287 3288
    ret->complete = 1;

3289
    virNetServerClientSetAuth(client, 0);
3290
    virNetServerTrackCompletedAuth(server);
3291
    virMutexUnlock(&priv->lock);
3292

3293
    return 0;
3294

3295
 error:
3296
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3297
    virMutexUnlock(&priv->lock);
3298 3299
    return -1;

3300
 authfail:
3301 3302 3303
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3304
    goto error;
3305

3306
 authdeny:
3307 3308 3309
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3310
    goto error;
3311 3312
}

3313

3314 3315 3316
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3317

3318
static int
3319 3320
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3321
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3322
                                  virNetMessageErrorPtr rerr,
3323 3324
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3325
{
3326 3327
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3328
    int rv = -1;
3329 3330
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3331

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

3337
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3338 3339
        goto cleanup;

3340 3341 3342 3343 3344 3345 3346
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
3347
        if (VIR_ALLOC(parent_p) < 0)
3348
            goto cleanup;
3349
        if (VIR_STRDUP(*parent_p, parent) < 0) {
3350 3351 3352 3353 3354 3355
            VIR_FREE(parent_p);
            goto cleanup;
        }
        ret->parent = parent_p;
    }

3356 3357
    rv = 0;

3358
 cleanup:
3359
    if (rv < 0)
3360
        virNetMessageSaveError(rerr);
3361 3362
    if (dev)
        virNodeDeviceFree(dev);
3363
    return rv;
3364 3365
}

3366 3367 3368 3369 3370

/***************************
 * Register / deregister events
 ***************************/
static int
3371
remoteDispatchConnectDomainEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
3372
                                         virNetServerClientPtr client,
3373 3374 3375
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                         remote_connect_domain_event_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
3376
{
3377
    int callbackID;
3378
    int rv = -1;
3379 3380
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3381 3382
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3383

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

3389 3390
    virMutexLock(&priv->lock);

3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401
    /* 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;
3402
    callback->legacy = true;
3403 3404 3405 3406
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3407
        goto cleanup;
O
Osier Yang 已提交
3408

3409
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3410 3411 3412
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
3413 3414 3415 3416 3417
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3418
        goto cleanup;
3419
    }
O
Osier Yang 已提交
3420

3421
    ref->callbackID = callbackID;
3422

3423 3424
    rv = 0;

3425
 cleanup:
3426
    VIR_FREE(callback);
3427
    if (rv < 0)
3428 3429
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3430
    return rv;
O
Osier Yang 已提交
3431 3432
}

3433
static int
3434
remoteDispatchConnectDomainEventDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
3435
                                           virNetServerClientPtr client,
3436 3437 3438
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                           remote_connect_domain_event_deregister_ret *ret ATTRIBUTE_UNUSED)
3439
{
3440
    int callbackID = -1;
3441
    int rv = -1;
3442
    size_t i;
3443 3444
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3445

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

3451 3452
    virMutexLock(&priv->lock);

3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463
    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);
3464
        goto cleanup;
3465
    }
3466

3467
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3468
        goto cleanup;
3469

3470 3471
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3472

3473 3474
    rv = 0;

3475
 cleanup:
3476
    if (rv < 0)
3477 3478
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3479 3480 3481 3482
    return rv;
}

static void
3483
remoteDispatchObjectEventSend(virNetServerClientPtr client,
3484
                              virNetServerProgramPtr program,
3485 3486 3487 3488
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3489
    virNetMessagePtr msg;
3490

3491
    if (!(msg = virNetMessageNew(false)))
3492
        goto cleanup;
3493

3494 3495 3496 3497 3498 3499
    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;
3500

3501
    if (virNetMessageEncodeHeader(msg) < 0)
3502 3503
        goto cleanup;

3504 3505
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3506

3507 3508
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3509

3510
    xdr_free(proc, data);
3511 3512
    return;

3513
 cleanup:
3514
    virNetMessageFree(msg);
3515
    xdr_free(proc, data);
3516 3517
}

3518
static int
3519 3520
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3521
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3522
                             virNetMessageErrorPtr rerr,
3523 3524
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3525
{
3526 3527 3528
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3529
    int rv = -1;
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
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3539
        goto cleanup;
3540

3541
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3542
        goto cleanup;
3543

3544 3545 3546
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3547 3548
    rv = 0;

3549
 cleanup:
3550
    if (rv < 0)
3551
        virNetMessageSaveError(rerr);
3552 3553
    if (secret)
        virSecretFree(secret);
3554
    return rv;
3555 3556
}

3557
static int
3558 3559
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3560
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3561
                             virNetMessageErrorPtr rerr,
3562 3563 3564 3565 3566
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3567 3568
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3569

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

3575
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3576 3577 3578 3579 3580 3581 3582
        goto cleanup;

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

    rv = 0;

3583
 cleanup:
3584
    if (rv < 0)
3585
        virNetMessageSaveError(rerr);
3586 3587 3588 3589 3590
    if (dom)
        virDomainFree(dom);
    return rv;
}

3591 3592 3593 3594 3595 3596

/* 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.  */
3597
static int
3598
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3599
                                            virNetServerClientPtr client,
3600 3601 3602
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            remote_connect_domain_event_register_any_args *args)
3603 3604
{
    int callbackID;
3605
    int rv = -1;
3606 3607
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3608 3609
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3610

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

3616 3617
    virMutexLock(&priv->lock);

3618 3619 3620 3621
    /* 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 ||
3622
        args->eventID < 0) {
3623 3624
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3625
        goto cleanup;
3626 3627
    }

3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638
    /* 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;
3639
    callback->legacy = true;
3640 3641 3642 3643
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3644
        goto cleanup;
3645

3646
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3647 3648 3649
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3650 3651 3652 3653 3654
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3655
        goto cleanup;
3656
    }
3657

3658
    ref->callbackID = callbackID;
3659

3660 3661
    rv = 0;

3662
 cleanup:
3663
    VIR_FREE(callback);
3664
    if (rv < 0)
3665 3666
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3667
    return rv;
3668 3669 3670
}


3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697
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;

3698
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST || args->eventID < 0) {
3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737
        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;

3738
 cleanup:
3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    virMutexUnlock(&priv->lock);
    return rv;
}


3749
static int
3750
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3751
                                              virNetServerClientPtr client,
3752 3753 3754
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              remote_connect_domain_event_deregister_any_args *args)
3755 3756
{
    int callbackID = -1;
3757
    int rv = -1;
3758
    size_t i;
3759 3760
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3761

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

3767 3768
    virMutexLock(&priv->lock);

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

3779 3780 3781 3782 3783 3784
    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->eventID == args->eventID) {
            callbackID = priv->domainEventCallbacks[i]->callbackID;
            break;
        }
    }
3785
    if (callbackID < 0) {
3786 3787
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event %d not registered"), args->eventID);
3788
        goto cleanup;
3789 3790
    }

3791
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3792
        goto cleanup;
3793

3794 3795
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3796

3797 3798
    rv = 0;

3799
 cleanup:
3800
    if (rv < 0)
3801 3802
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3803
    return rv;
3804 3805
}

3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844

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;

3845
 cleanup:
3846 3847 3848 3849 3850 3851 3852
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}


C
Chris Lalancette 已提交
3853
static int
3854 3855 3856 3857 3858 3859
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 已提交
3860
{
3861
    virDomainPtr dom = NULL;
3862
    int rv = -1;
3863 3864
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
3865

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

3871
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3872
        goto cleanup;
C
Chris Lalancette 已提交
3873

3874
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
3875
                                    args->flags) < 0)
3876
        goto cleanup;
C
Chris Lalancette 已提交
3877

3878
    rv = 0;
C
Chris Lalancette 已提交
3879

3880
 cleanup:
3881
    if (rv < 0)
3882
        virNetMessageSaveError(rerr);
3883 3884
    if (dom)
        virDomainFree(dom);
3885
    return rv;
C
Chris Lalancette 已提交
3886 3887
}

3888

3889
static int
3890 3891
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3892
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3893
                                  virNetMessageErrorPtr rerr,
3894 3895 3896 3897 3898 3899
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
3900
    char *xmlin;
3901 3902 3903
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3904 3905
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3906

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

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

3915
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3916 3917
    dname = args->dname == NULL ? NULL : *args->dname;

3918
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931
                                       &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;

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


static int
3942 3943
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3944
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3945
                                    virNetMessageErrorPtr rerr,
3946 3947 3948 3949 3950 3951 3952 3953 3954
                                    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;
3955 3956
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3957

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

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

    /* Wacky world of XDR ... */
3967
    if (VIR_ALLOC(uri_out) < 0)
3968 3969
        goto cleanup;

3970
    if (virDomainMigratePrepare3(priv->conn,
3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987
                                 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;

3988
 cleanup:
3989
    if (rv < 0) {
3990
        virNetMessageSaveError(rerr);
3991 3992 3993 3994 3995
        VIR_FREE(uri_out);
    }
    return rv;
}

3996

3997
static int
3998 3999
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4000
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4001
                                    virNetMessageErrorPtr rerr,
4002 4003 4004 4005
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
4006
    char *xmlin;
4007
    char *dname;
4008 4009
    char *uri;
    char *dconnuri;
4010 4011 4012
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4013 4014
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4015

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

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

4024
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4025
    dname = args->dname == NULL ? NULL : *args->dname;
4026 4027
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
4028

4029
    if (virDomainMigratePerform3(dom, xmlin,
4030 4031 4032
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
4033
                                 dconnuri, uri,
4034 4035 4036 4037 4038 4039 4040 4041 4042 4043
                                 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;

4044
 cleanup:
4045
    if (rv < 0)
4046
        virNetMessageSaveError(rerr);
4047 4048 4049 4050 4051 4052 4053
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
4054 4055
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
4056
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
4057
                                   virNetMessageErrorPtr rerr,
4058 4059 4060 4061 4062 4063
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
4064 4065
    char *uri;
    char *dconnuri;
4066
    int rv = -1;
4067 4068
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4069

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

4075 4076 4077
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

4078
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
4079 4080 4081 4082 4083 4084
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
4085 4086
        goto cleanup;

4087
    make_nonnull_domain(&ret->dom, dom);
4088 4089 4090 4091 4092 4093 4094 4095

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

    rv = 0;

4096
 cleanup:
4097
    if (rv < 0) {
4098
        virNetMessageSaveError(rerr);
4099 4100 4101 4102 4103 4104 4105 4106 4107
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
4108 4109
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4110
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4111 4112
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
4113 4114 4115
{
    virDomainPtr dom = NULL;
    int rv = -1;
4116 4117
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4118

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

4124
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4125 4126 4127 4128 4129 4130 4131 4132 4133 4134
        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;

4135
 cleanup:
4136
    if (rv < 0)
4137
        virNetMessageSaveError(rerr);
4138 4139 4140 4141 4142 4143
    if (dom)
        virDomainFree(dom);
    return rv;
}


4144 4145 4146 4147 4148 4149
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)
4150 4151 4152 4153 4154 4155
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

4156 4157 4158 4159 4160 4161 4162 4163 4164 4165
    /* 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;
    }

4166
    if (!priv->conn) {
4167
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4168 4169 4170 4171 4172
        goto cleanup;
    }

    switch (args->feature) {
    case VIR_DRV_FEATURE_FD_PASSING:
4173
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
4174 4175 4176 4177
        supported = 1;
        break;

    default:
4178
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
4179 4180 4181 4182
            goto cleanup;
        break;
    }

4183
 done:
4184 4185 4186
    ret->supported = supported;
    rv = 0;

4187
 cleanup:
4188 4189 4190 4191 4192 4193
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207
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) {
4208
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225
        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;

4226
 cleanup:
4227 4228 4229 4230 4231 4232 4233 4234
    VIR_FORCE_CLOSE(fd);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

4235

4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256
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;

4257 4258 4259
    if ((fd = virDomainOpenGraphicsFD(dom,
                                      args->idx,
                                      args->flags)) < 0)
4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278
        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);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
    }

    if (dom)
        virDomainFree(dom);
    return rv;
}
4279 4280


4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291
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;
4292
    int nparams = 0;
4293 4294 4295 4296 4297 4298
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4299
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4300 4301 4302 4303 4304
        goto cleanup;
    }

    flags = args->flags;

4305
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
4306
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4307 4308
        goto cleanup;
    }
4309
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4310
        goto cleanup;
4311
    nparams = args->nparams;
4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332

    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;

4333
 success:
4334 4335
    rv = 0;

4336
 cleanup:
4337 4338
    if (rv < 0)
        virNetMessageSaveError(rerr);
4339
    virTypedParamsFree(params, nparams);
4340 4341 4342 4343
    if (dom)
        virDomainFree(dom);
    return rv;
}
4344

4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360
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) {
4361
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4362 4363 4364 4365
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
4366
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4367 4368 4369
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
4370
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
4371 4372 4373 4374
        goto cleanup;
    }

    if (args->nparams > 0 &&
4375
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395
        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;

4396
 success:
4397 4398
    rv = 0;
    ret->nparams = percpu_len;
4399
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
4400
        size_t i;
4401 4402 4403 4404 4405 4406

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

4408
 cleanup:
4409 4410
    if (rv < 0)
         virNetMessageSaveError(rerr);
4411
    virTypedParamsFree(params, args->ncpus * args->nparams);
4412 4413 4414 4415 4416
    if (dom)
        virDomainFree(dom);
    return rv;
}

4417 4418 4419 4420 4421 4422 4423
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)
4424 4425 4426 4427
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
4428
    int len = 0;
4429 4430 4431 4432
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
4441 4442
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
4443 4444 4445 4446
        goto cleanup;
    }

    if (args->maxerrors &&
4447
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463
        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;

4464
 cleanup:
4465 4466 4467 4468
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
4469
    if (errors && len > 0) {
4470
        size_t i;
4471 4472 4473 4474 4475 4476 4477
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

4478 4479 4480 4481 4482 4483 4484 4485 4486 4487
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;
4488
    size_t i;
4489 4490 4491 4492 4493
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

    if (!priv->conn) {
4494
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505
        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;

4506 4507 4508 4509 4510 4511 4512
    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;
    }

4513
    if (snaps && nsnaps) {
4514
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529
            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;

4530
 cleanup:
4531 4532 4533 4534
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
4535
    if (snaps && nsnaps > 0) {
4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552
        for (i = 0; i < nsnaps; i++)
            virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }
    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;
4553
    size_t i;
4554 4555 4556 4557 4558 4559
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;
    virDomainSnapshotPtr snapshot = NULL;

    if (!priv->conn) {
4560
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574
        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;

4575 4576 4577 4578 4579 4580 4581
    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;
    }

4582
    if (snaps && nsnaps) {
4583
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598
            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;

4599
 cleanup:
4600 4601 4602 4603 4604 4605
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (snapshot)
        virDomainSnapshotFree(snapshot);
    if (dom)
        virDomainFree(dom);
4606
    if (snaps && nsnaps > 0) {
4607 4608 4609 4610 4611 4612 4613
        for (i = 0; i < nsnaps; i++)
            virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }
    return rv;
}

4614 4615 4616 4617 4618 4619 4620 4621 4622 4623
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;
4624
    size_t i;
4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637
    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;

4638 4639 4640 4641 4642 4643 4644
    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;
    }

4645
    if (pools && npools) {
4646
        if (VIR_ALLOC_N(ret->pools.pools_val, npools) < 0)
4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661
            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;

4662
 cleanup:
4663 4664
    if (rv < 0)
        virNetMessageSaveError(rerr);
4665
    if (pools && npools > 0) {
4666 4667 4668 4669 4670 4671 4672
        for (i = 0; i < npools; i++)
            virStoragePoolFree(pools[i]);
        VIR_FREE(pools);
    }
    return rv;
}

4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683
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;
4684
    size_t i;
4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700
    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;

4701 4702 4703 4704 4705 4706 4707
    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;
    }

4708
    if (vols && nvols) {
4709
        if (VIR_ALLOC_N(ret->vols.vols_val, nvols) < 0)
4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724
            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;

4725
 cleanup:
4726 4727
    if (rv < 0)
        virNetMessageSaveError(rerr);
4728
    if (vols && nvols > 0) {
4729 4730 4731 4732
        for (i = 0; i < nvols; i++)
            virStorageVolFree(vols[i]);
        VIR_FREE(vols);
    }
4733 4734
    if (pool)
        virStoragePoolFree(pool);
4735 4736 4737
    return rv;
}

4738 4739 4740 4741 4742 4743 4744 4745 4746 4747
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;
4748
    size_t i;
4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761
    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;

4762 4763 4764 4765 4766 4767 4768
    if (nnets > REMOTE_NETWORK_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many networks '%d' for limit '%d'"),
                       nnets, REMOTE_NETWORK_LIST_MAX);
        goto cleanup;
    }

4769
    if (nets && nnets) {
4770
        if (VIR_ALLOC_N(ret->nets.nets_val, nnets) < 0)
4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785
            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;

4786
 cleanup:
4787 4788
    if (rv < 0)
        virNetMessageSaveError(rerr);
4789
    if (nets && nnets > 0) {
4790 4791 4792 4793 4794 4795 4796
        for (i = 0; i < nnets; i++)
            virNetworkFree(nets[i]);
        VIR_FREE(nets);
    }
    return rv;
}

4797 4798 4799 4800 4801 4802 4803 4804 4805 4806
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;
4807
    size_t i;
4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820
    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;

4821 4822 4823 4824 4825 4826 4827
    if (nifaces > REMOTE_INTERFACE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many interfaces '%d' for limit '%d'"),
                       nifaces, REMOTE_INTERFACE_LIST_MAX);
        goto cleanup;
    }

4828
    if (ifaces && nifaces) {
4829
        if (VIR_ALLOC_N(ret->ifaces.ifaces_val, nifaces) < 0)
4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844
            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;

4845
 cleanup:
4846 4847
    if (rv < 0)
        virNetMessageSaveError(rerr);
4848
    if (ifaces && nifaces > 0) {
4849 4850 4851 4852 4853 4854 4855
        for (i = 0; i < nifaces; i++)
            virInterfaceFree(ifaces[i]);
        VIR_FREE(ifaces);
    }
    return rv;
}

4856 4857 4858 4859 4860 4861 4862 4863 4864 4865
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;
4866
    size_t i;
4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879
    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;

4880 4881 4882 4883 4884 4885 4886
    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;
    }

4887
    if (devices && ndevices) {
4888
        if (VIR_ALLOC_N(ret->devices.devices_val, ndevices) < 0)
4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903
            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;

4904
 cleanup:
4905 4906
    if (rv < 0)
        virNetMessageSaveError(rerr);
4907
    if (devices && ndevices > 0) {
4908 4909 4910 4911 4912 4913
        for (i = 0; i < ndevices; i++)
            virNodeDeviceFree(devices[i]);
        VIR_FREE(devices);
    }
    return rv;
}
4914

4915 4916 4917 4918 4919 4920 4921 4922 4923 4924
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;
4925
    size_t i;
4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938
    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;

4939 4940 4941 4942 4943 4944 4945
    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;
    }

4946
    if (filters && nfilters) {
4947
        if (VIR_ALLOC_N(ret->filters.filters_val, nfilters) < 0)
4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962
            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;

4963
 cleanup:
4964 4965
    if (rv < 0)
        virNetMessageSaveError(rerr);
4966
    if (filters && nfilters > 0) {
4967 4968 4969 4970 4971 4972 4973
        for (i = 0; i < nfilters; i++)
            virNWFilterFree(filters[i]);
        VIR_FREE(filters);
    }
    return rv;
}

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

4998 4999 5000 5001 5002 5003 5004
    if (nsecrets > REMOTE_SECRET_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many secrets '%d' for limit '%d'"),
                       nsecrets, REMOTE_SECRET_LIST_MAX);
        goto cleanup;
    }

5005
    if (secrets && nsecrets) {
5006
        if (VIR_ALLOC_N(ret->secrets.secrets_val, nsecrets) < 0)
5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021
            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;

5022
 cleanup:
5023 5024
    if (rv < 0)
        virNetMessageSaveError(rerr);
5025
    if (secrets && nsecrets > 0) {
5026 5027 5028 5029 5030 5031 5032
        for (i = 0; i < nsecrets; i++)
            virSecretFree(secrets[i]);
        VIR_FREE(secrets);
    }
    return rv;
}

5033 5034 5035 5036 5037 5038 5039 5040 5041
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;
5042
    int nparams = 0;
5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054
    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;

5055
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
5056 5057 5058
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
5059
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
5060
        goto cleanup;
5061
    nparams = args->nparams;
5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079

    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;

5080
 success:
5081 5082
    rv = 0;

5083
 cleanup:
5084 5085
    if (rv < 0)
        virNetMessageSaveError(rerr);
5086
    virTypedParamsFree(params, nparams);
5087 5088 5089
    return rv;
}

5090 5091 5092 5093 5094 5095 5096 5097 5098
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;
5099
    unsigned int online = 0;
5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112
    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;

5113 5114
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
5115 5116 5117 5118
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
5119
    if (args->need_map) {
5120 5121 5122 5123 5124 5125 5126 5127 5128 5129
        ret->cpumap.cpumap_len = VIR_CPU_MAPLEN(cpunum);
        ret->cpumap.cpumap_val = (char *) cpumap;
        cpumap = NULL;
    }

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

    rv = 0;

5130
 cleanup:
5131 5132 5133 5134 5135 5136
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumap);
    return rv;
}

5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169
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
     */
5170
    for (i = 0; i < msg->nfds; i++)
5171 5172 5173 5174 5175 5176 5177 5178 5179
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

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

    rv = 1;

5180
 cleanup:
5181 5182
    if (rv < 0)
        virNetMessageSaveError(rerr);
5183 5184
    if (dom)
        virDomainFree(dom);
5185 5186 5187
    return rv;
}

5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214
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;

5215 5216 5217 5218 5219 5220 5221
    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;
    }

5222 5223 5224 5225 5226 5227 5228 5229
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       0) < 0)
        goto cleanup;

    rv = 0;

5230
 cleanup:
5231 5232 5233 5234 5235 5236 5237 5238
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virTypedParamsFree(params, nparams);
    if (dom)
        virDomainFree(dom);
    return rv;
}

5239
static int
5240 5241 5242 5243 5244 5245
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)
5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261
{
    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;
    }

5262 5263 5264 5265 5266 5267 5268
    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;
    }

5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287
    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;

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

static int
5298 5299 5300 5301 5302 5303
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)
5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318
{
    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;
    }

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

5326 5327 5328 5329 5330 5331
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    /* Wacky world of XDR ... */
5332
    if (VIR_ALLOC(uri_out) < 0)
5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347
        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;

5348
 cleanup:
5349 5350 5351 5352 5353 5354 5355 5356 5357
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(uri_out);
    }
    return rv;
}

static int
5358 5359 5360 5361 5362 5363
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)
5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379
{
    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;
    }

5380 5381 5382 5383 5384 5385 5386
    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;
    }

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

5411
 cleanup:
5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
        if (stream) {
            virStreamAbort(st);
            daemonFreeClientStream(client, stream);
        } else {
            virStreamFree(st);
        }
    }
    return rv;
}


static int
5428 5429 5430 5431 5432 5433
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)
5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449
{
    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;
    }

5450 5451 5452 5453 5454 5455 5456
    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;
    }

5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478
    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;

5479
 cleanup:
5480 5481 5482 5483 5484 5485 5486 5487 5488 5489
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
5490 5491 5492 5493 5494 5495
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)
5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510
{
    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;
    }

5511 5512 5513 5514 5515 5516 5517
    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;
    }

5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537
    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;

5538
 cleanup:
5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
5551 5552 5553 5554 5555
remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_confirm3_params_args *args)
5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568
{
    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;
    }

5569 5570 5571 5572 5573 5574 5575
    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;
    }

5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591
    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;

5592
 cleanup:
5593 5594 5595 5596 5597 5598 5599 5600 5601
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645
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;

5646
 cleanup:
5647 5648 5649 5650 5651 5652 5653
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virStringFreeList(models);
    return rv;
}


5654 5655 5656 5657 5658 5659 5660
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)
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
{
    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;

5691
 cleanup:
5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703
    for (i = 0; i < nfiles; i++) {
        VIR_FORCE_CLOSE(files[i]);
    }
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


5704 5705 5706 5707 5708 5709
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)
5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742
{
    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;

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


5756 5757
static int
remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5758
                                             virNetServerClientPtr client,
5759 5760 5761
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                             remote_connect_network_event_register_any_args *args,
5762
                                             remote_connect_network_event_register_any_ret *ret)
5763 5764 5765
{
    int callbackID;
    int rv = -1;
5766 5767
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
5768 5769
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
5770
    virNetworkPtr net = NULL;
5771 5772 5773 5774 5775 5776 5777 5778

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

    virMutexLock(&priv->lock);

5779 5780 5781 5782
    if (args->net &&
        !(net = get_nonnull_network(priv->conn, *args->net)))
        goto cleanup;

5783 5784 5785
    if (args->eventID >= VIR_NETWORK_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network event ID %d"), args->eventID);
5786 5787 5788
        goto cleanup;
    }

5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803
    /* 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)
5804 5805 5806
        goto cleanup;

    if ((callbackID = virConnectNetworkEventRegisterAny(priv->conn,
5807
                                                        net,
5808 5809
                                                        args->eventID,
                                                        networkEventCallbacks[args->eventID],
5810 5811 5812 5813 5814
                                                        ref,
                                                        remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->networkEventCallbacks,
                     priv->nnetworkEventCallbacks, 1);
        callback = ref;
5815
        goto cleanup;
5816
    }
5817

5818
    ref->callbackID = callbackID;
5819
    ret->callbackID = callbackID;
5820 5821 5822

    rv = 0;

5823
 cleanup:
5824
    VIR_FREE(callback);
5825 5826
    if (rv < 0)
        virNetMessageSaveError(rerr);
5827 5828
    if (net)
        virNetworkFree(net);
5829 5830 5831 5832 5833 5834 5835
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5836
                                               virNetServerClientPtr client,
5837 5838
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
5839
                                               remote_connect_network_event_deregister_any_args *args)
5840 5841
{
    int rv = -1;
5842
    size_t i;
5843 5844 5845 5846 5847 5848 5849 5850 5851 5852
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    virMutexLock(&priv->lock);

5853
    for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
5854
        if (priv->networkEventCallbacks[i]->callbackID == args->callbackID)
5855 5856
            break;
    }
5857
    if (i == priv->nnetworkEventCallbacks) {
5858
        virReportError(VIR_ERR_INTERNAL_ERROR,
5859 5860
                       _("network event callback %d not registered"),
                       args->callbackID);
5861 5862 5863
        goto cleanup;
    }

5864
    if (virConnectNetworkEventDeregisterAny(priv->conn, args->callbackID) < 0)
5865 5866
        goto cleanup;

5867 5868
    VIR_DELETE_ELEMENT(priv->networkEventCallbacks, i,
                       priv->nnetworkEventCallbacks);
5869 5870 5871

    rv = 0;

5872
 cleanup:
5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941
    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;

5942
 cleanup:
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 5991
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    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;

5992
 cleanup:
5993 5994 5995 5996 5997 5998
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}

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
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);
    if (dom)
        virDomainFree(dom);
    return rv;
}
6036

M
Michal Privoznik 已提交
6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 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 6080 6081 6082 6083 6084

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;
6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099
}

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

6100
    if (VIR_STRDUP(lease_dst->iface, lease_src->iface) < 0 ||
J
Ján Tomko 已提交
6101
        VIR_STRDUP(lease_dst->ipaddr, lease_src->ipaddr) < 0)
6102 6103
        goto error;

J
Ján Tomko 已提交
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
    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;
6129 6130 6131 6132

    return 0;

 error:
J
Ján Tomko 已提交
6133 6134 6135 6136 6137 6138 6139 6140
    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);
6141 6142 6143 6144 6145
    VIR_FREE(mac_tmp);
    VIR_FREE(iaid_tmp);
    VIR_FREE(hostname_tmp);
    VIR_FREE(clientid_tmp);
    VIR_FREE(lease_dst->ipaddr);
6146
    VIR_FREE(lease_dst->iface);
6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174
    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,
6175
                                           args->mac ? *args->mac : NULL,
6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209
                                           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);
6210
    if (leases && nleases > 0) {
6211 6212 6213 6214 6215 6216
        for (i = 0; i < nleases; i++)
            virNetworkDHCPLeaseFree(leases[i]);
        VIR_FREE(leases);
    }
    virNetworkFree(net);
    return rv;
M
Michal Privoznik 已提交
6217 6218 6219
}


6220 6221 6222 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 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305
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;
}


6306 6307 6308 6309 6310 6311 6312 6313 6314
/*----- 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
6315
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
6316 6317
{
    virDomainPtr dom;
6318
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
6319 6320 6321 6322 6323 6324 6325 6326
    /* 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
6327
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
6328
{
6329
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
6330 6331
}

D
Daniel Veillard 已提交
6332
static virInterfacePtr
6333
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
6334
{
6335
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
6336 6337
}

6338
static virStoragePoolPtr
6339
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
6340
{
6341 6342
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
6343 6344 6345
}

static virStorageVolPtr
6346
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
6347 6348
{
    virStorageVolPtr ret;
6349 6350
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
6351 6352 6353
    return ret;
}

6354
static virSecretPtr
6355
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
6356
{
6357
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
6358 6359
}

6360
static virNWFilterPtr
6361
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
6362
{
6363
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
6364 6365
}

C
Chris Lalancette 已提交
6366
static virDomainSnapshotPtr
6367
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
6368
{
6369
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
6370 6371
}

6372 6373
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
6374
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
6375 6376
{
    dom_dst->id = dom_src->id;
6377
    ignore_value(VIR_STRDUP_QUIET(dom_dst->name, dom_src->name));
6378
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
6379 6380 6381
}

static void
6382
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
6383
{
6384
    ignore_value(VIR_STRDUP_QUIET(net_dst->name, net_src->name));
6385
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
6386 6387
}

D
Daniel Veillard 已提交
6388
static void
6389 6390
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
6391
{
6392 6393
    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 已提交
6394 6395
}

6396
static void
6397
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
6398
{
6399
    ignore_value(VIR_STRDUP_QUIET(pool_dst->name, pool_src->name));
6400
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
6401 6402 6403
}

static void
6404
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
6405
{
6406 6407 6408
    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));
6409
}
6410 6411

static void
6412
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
6413
{
6414
    ignore_value(VIR_STRDUP_QUIET(dev_dst->name, dev_src->name));
6415
}
6416 6417

static void
6418
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
6419
{
6420
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
6421
    secret_dst->usageType = secret_src->usageType;
6422
    ignore_value(VIR_STRDUP_QUIET(secret_dst->usageID, secret_src->usageID));
6423
}
6424 6425

static void
6426
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
6427
{
6428
    ignore_value(VIR_STRDUP_QUIET(nwfilter_dst->name, nwfilter_src->name));
6429
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
6430
}
C
Chris Lalancette 已提交
6431 6432

static void
6433
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
6434
{
6435
    ignore_value(VIR_STRDUP_QUIET(snapshot_dst->name, snapshot_src->name));
6436
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
6437
}
6438 6439 6440 6441 6442 6443 6444 6445

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;
6446
    size_t i = 0;
6447

6448
    if (VIR_ALLOC_N(val, nerrors) < 0)
6449
        goto error;
6450 6451

    for (i = 0; i < nerrors; i++) {
6452 6453
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
6454 6455 6456 6457 6458 6459 6460 6461
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

6462
 error:
6463
    if (val) {
6464
        size_t j;
6465 6466 6467 6468 6469 6470
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}