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

#include <config.h>

25
#include "virerror.h"
26

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

#define VIR_FROM_THIS VIR_FROM_RPC
57

58 59
VIR_LOG_INIT("daemon.remote");

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

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

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

86 87 88 89 90 91 92
static virDomainPtr get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network(virConnectPtr conn, remote_nonnull_network network);
static virInterfacePtr get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface);
static virStoragePoolPtr get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool);
static virStorageVolPtr get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol);
static virSecretPtr get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret);
static virNWFilterPtr get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter);
93
static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot);
94 95 96 97 98 99 100 101 102
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);
103

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

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


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

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

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

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

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

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


185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
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);

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


212 213 214 215 216 217
static int
remoteRelayDomainEventLifecycle(virConnectPtr conn,
                                virDomainPtr dom,
                                int event,
                                int detail,
                                void *opaque)
218
{
219
    daemonClientEventCallbackPtr callback = opaque;
220
    remote_domain_event_lifecycle_msg data;
221

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

226 227
    VIR_DEBUG("Relaying domain lifecycle event %d %d, callback %d legacy %d",
              event, detail, callback->callbackID, callback->legacy);
228

229
    /* build return data */
230
    memset(&data, 0, sizeof(data));
231
    make_nonnull_domain(&data.dom, dom);
232 233
    data.event = event;
    data.detail = detail;
234

235 236 237 238 239 240 241 242 243 244 245 246 247 248
    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);
    }
249

250 251
    return 0;
}
252

253 254 255 256
static int
remoteRelayDomainEventReboot(virConnectPtr conn,
                             virDomainPtr dom,
                             void *opaque)
257
{
258
    daemonClientEventCallbackPtr callback = opaque;
259 260
    remote_domain_event_reboot_msg data;

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

265 266
    VIR_DEBUG("Relaying domain reboot event %s %d, callback %d legacy %d",
              dom->name, dom->id, callback->callbackID, callback->legacy);
267 268

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

272 273 274 275 276 277 278 279 280 281 282 283
    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);
    }
284 285 286 287

    return 0;
}

288

289 290 291 292 293
static int
remoteRelayDomainEventRTCChange(virConnectPtr conn,
                                virDomainPtr dom,
                                long long offset,
                                void *opaque)
294
{
295
    daemonClientEventCallbackPtr callback = opaque;
296 297
    remote_domain_event_rtc_change_msg data;

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

302 303 304
    VIR_DEBUG("Relaying domain rtc change event %s %d %lld, callback %d legacy %d",
              dom->name, dom->id, offset,
              callback->callbackID, callback->legacy);
305 306

    /* build return data */
307
    memset(&data, 0, sizeof(data));
308
    make_nonnull_domain(&data.dom, dom);
309 310
    data.offset = offset;

311 312 313 314 315 316 317 318 319 320 321 322
    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);
    }
323 324 325 326 327

    return 0;
}


328 329 330 331 332
static int
remoteRelayDomainEventWatchdog(virConnectPtr conn,
                               virDomainPtr dom,
                               int action,
                               void *opaque)
333
{
334
    daemonClientEventCallbackPtr callback = opaque;
335 336
    remote_domain_event_watchdog_msg data;

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

341 342
    VIR_DEBUG("Relaying domain watchdog event %s %d %d, callback %d",
              dom->name, dom->id, action, callback->callbackID);
343 344

    /* build return data */
345
    memset(&data, 0, sizeof(data));
346
    make_nonnull_domain(&data.dom, dom);
347 348
    data.action = action;

349 350 351 352 353 354 355 356 357 358 359 360
    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);
    }
361 362 363 364 365

    return 0;
}


366 367 368 369 370 371 372
static int
remoteRelayDomainEventIOError(virConnectPtr conn,
                              virDomainPtr dom,
                              const char *srcPath,
                              const char *devAlias,
                              int action,
                              void *opaque)
373
{
374
    daemonClientEventCallbackPtr callback = opaque;
375 376
    remote_domain_event_io_error_msg data;

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

381 382 383
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d, callback %d",
              dom->name, dom->id, srcPath, devAlias, action,
              callback->callbackID);
384 385

    /* build return data */
386
    memset(&data, 0, sizeof(data));
387 388 389
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
390
    make_nonnull_domain(&data.dom, dom);
391 392
    data.action = action;

393 394 395 396 397 398 399 400 401 402 403 404
    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);
    }
405 406

    return 0;
407
 error:
E
Eric Blake 已提交
408 409
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
410
    return -1;
411 412 413
}


414 415 416 417 418 419 420 421
static int
remoteRelayDomainEventIOErrorReason(virConnectPtr conn,
                                    virDomainPtr dom,
                                    const char *srcPath,
                                    const char *devAlias,
                                    int action,
                                    const char *reason,
                                    void *opaque)
422
{
423
    daemonClientEventCallbackPtr callback = opaque;
424 425
    remote_domain_event_io_error_reason_msg data;

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

430 431 432
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s, callback %d",
              dom->name, dom->id, srcPath, devAlias, action, reason,
              callback->callbackID);
433 434

    /* build return data */
435
    memset(&data, 0, sizeof(data));
436 437 438 439
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0 ||
        VIR_STRDUP(data.reason, reason) < 0)
        goto error;
440
    data.action = action;
441 442

    make_nonnull_domain(&data.dom, dom);
443

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

    return 0;
458

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


467 468 469 470 471 472 473 474 475
static int
remoteRelayDomainEventGraphics(virConnectPtr conn,
                               virDomainPtr dom,
                               int phase,
                               virDomainEventGraphicsAddressPtr local,
                               virDomainEventGraphicsAddressPtr remote,
                               const char *authScheme,
                               virDomainEventGraphicsSubjectPtr subject,
                               void *opaque)
476
{
477
    daemonClientEventCallbackPtr callback = opaque;
478
    remote_domain_event_graphics_msg data;
479
    size_t i;
480

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

485 486
    VIR_DEBUG("Relaying domain graphics event %s %d %d - %d %s %s  - %d %s %s - %s, callback %d",
              dom->name, dom->id, phase,
487 488
              local->family, local->service, local->node,
              remote->family, remote->service, remote->node,
489
              authScheme, callback->callbackID);
490

491
    VIR_DEBUG("Subject %d", subject->nidentity);
492
    for (i = 0; i < subject->nidentity; i++)
493
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
494 495

    /* build return data */
496
    memset(&data, 0, sizeof(data));
497 498 499
    data.phase = phase;
    data.local.family = local->family;
    data.remote.family = remote->family;
500 501 502 503 504 505
    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;
506 507

    data.subject.subject_len = subject->nidentity;
508
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0)
509
        goto error;
510

511
    for (i = 0; i < data.subject.subject_len; i++) {
512 513 514
        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;
515
    }
516
    make_nonnull_domain(&data.dom, dom);
517

518 519 520 521 522 523 524 525 526 527 528 529
    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);
    }
530 531

    return 0;
532

533
 error:
E
Eric Blake 已提交
534 535 536 537 538
    VIR_FREE(data.authScheme);
    VIR_FREE(data.local.node);
    VIR_FREE(data.local.service);
    VIR_FREE(data.remote.node);
    VIR_FREE(data.remote.service);
539
    if (data.subject.subject_val != NULL) {
540
        for (i = 0; i < data.subject.subject_len; i++) {
E
Eric Blake 已提交
541 542
            VIR_FREE(data.subject.subject_val[i].type);
            VIR_FREE(data.subject.subject_val[i].name);
543
        }
E
Eric Blake 已提交
544
        VIR_FREE(data.subject.subject_val);
545 546
    }
    return -1;
547 548
}

549 550 551 552 553 554 555
static int
remoteRelayDomainEventBlockJob(virConnectPtr conn,
                               virDomainPtr dom,
                               const char *path,
                               int type,
                               int status,
                               void *opaque)
556
{
557
    daemonClientEventCallbackPtr callback = opaque;
558 559
    remote_domain_event_block_job_msg data;

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

564 565
    VIR_DEBUG("Relaying domain block job event %s %d %s %i, %i, callback %d",
              dom->name, dom->id, path, type, status, callback->callbackID);
566 567

    /* build return data */
568
    memset(&data, 0, sizeof(data));
569 570
    if (VIR_STRDUP(data.path, path) < 0)
        goto error;
571 572
    data.type = type;
    data.status = status;
573
    make_nonnull_domain(&data.dom, dom);
574

575 576 577 578 579 580 581 582 583 584 585 586
    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);
    }
587 588

    return 0;
589
 error:
E
Eric Blake 已提交
590
    VIR_FREE(data.path);
591
    return -1;
592 593
}

594

595 596 597 598
static int
remoteRelayDomainEventControlError(virConnectPtr conn,
                                   virDomainPtr dom,
                                   void *opaque)
599
{
600
    daemonClientEventCallbackPtr callback = opaque;
601 602
    remote_domain_event_control_error_msg data;

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

607 608
    VIR_DEBUG("Relaying domain control error %s %d, callback %d",
              dom->name, dom->id, callback->callbackID);
609 610

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

614 615 616 617 618 619 620 621 622 623 624 625
    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);
    }
626 627 628 629 630

    return 0;
}


631 632 633 634 635 636 637 638
static int
remoteRelayDomainEventDiskChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *oldSrcPath,
                                 const char *newSrcPath,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
639
{
640
    daemonClientEventCallbackPtr callback = opaque;
641 642 643
    remote_domain_event_disk_change_msg data;
    char **oldSrcPath_p = NULL, **newSrcPath_p = NULL;

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

648 649 650
    VIR_DEBUG("Relaying domain %s %d disk change %s %s %s %d, callback %d",
              dom->name, dom->id, oldSrcPath, newSrcPath, devAlias, reason,
              callback->callbackID);
651 652

    /* build return data */
653
    memset(&data, 0, sizeof(data));
654 655
    if (oldSrcPath &&
        ((VIR_ALLOC(oldSrcPath_p) < 0) ||
656
         VIR_STRDUP(*oldSrcPath_p, oldSrcPath) < 0))
657
        goto error;
658 659 660

    if (newSrcPath &&
        ((VIR_ALLOC(newSrcPath_p) < 0) ||
661
         VIR_STRDUP(*newSrcPath_p, newSrcPath) < 0))
662
        goto error;
663 664 665

    data.oldSrcPath = oldSrcPath_p;
    data.newSrcPath = newSrcPath_p;
666 667
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
668 669 670 671
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

672 673 674 675 676 677 678 679 680 681 682 683
    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);
    }
684 685 686

    return 0;

687
 error:
M
Michal Privoznik 已提交
688 689
    VIR_FREE(oldSrcPath_p);
    VIR_FREE(newSrcPath_p);
690 691 692 693
    return -1;
}


694 695 696 697 698 699 700
static int
remoteRelayDomainEventTrayChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
{
701
    daemonClientEventCallbackPtr callback = opaque;
702 703
    remote_domain_event_tray_change_msg data;

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

708 709
    VIR_DEBUG("Relaying domain %s %d tray change devAlias: %s reason: %d, callback %d",
              dom->name, dom->id, devAlias, reason, callback->callbackID);
710 711

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

714
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
715 716 717 718 719
        return -1;
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

720 721 722 723 724 725 726 727 728 729 730 731
    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);
    }
732 733 734 735

    return 0;
}

736 737 738
static int
remoteRelayDomainEventPMWakeup(virConnectPtr conn,
                               virDomainPtr dom,
E
Eric Blake 已提交
739
                               int reason,
740 741
                               void *opaque)
{
742
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
743 744
    remote_domain_event_pmwakeup_msg data;

745 746
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
747 748
        return -1;

749 750
    VIR_DEBUG("Relaying domain %s %d system pmwakeup, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
751 752

    /* build return data */
753
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
754 755
    make_nonnull_domain(&data.dom, dom);

756 757 758 759 760 761
    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 已提交
762
                                                          reason, data };
763 764 765 766 767

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMWAKEUP,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmwakeup_msg, &msg);
    }
O
Osier Yang 已提交
768 769 770 771

    return 0;
}

772 773 774
static int
remoteRelayDomainEventPMSuspend(virConnectPtr conn,
                                virDomainPtr dom,
E
Eric Blake 已提交
775
                                int reason,
776 777
                                void *opaque)
{
778
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
779 780
    remote_domain_event_pmsuspend_msg data;

781 782
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
783 784
        return -1;

785 786
    VIR_DEBUG("Relaying domain %s %d system pmsuspend, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
787 788

    /* build return data */
789
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
790 791
    make_nonnull_domain(&data.dom, dom);

792 793 794 795 796 797
    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 已提交
798
                                                           reason, data };
799 800 801 802 803

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_msg, &msg);
    }
O
Osier Yang 已提交
804 805 806 807

    return 0;
}

808
static int
809
remoteRelayDomainEventBalloonChange(virConnectPtr conn,
810 811 812 813
                                    virDomainPtr dom,
                                    unsigned long long actual,
                                    void *opaque)
{
814
    daemonClientEventCallbackPtr callback = opaque;
815 816
    remote_domain_event_balloon_change_msg data;

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

821 822
    VIR_DEBUG("Relaying domain balloon change event %s %d %lld, callback %d",
              dom->name, dom->id, actual, callback->callbackID);
823 824 825 826 827 828

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

829 830 831 832 833 834 835 836 837 838 839 840
    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);
    }
841 842 843 844 845

    return 0;
}


846 847 848
static int
remoteRelayDomainEventPMSuspendDisk(virConnectPtr conn,
                                    virDomainPtr dom,
E
Eric Blake 已提交
849
                                    int reason,
850 851
                                    void *opaque)
{
852
    daemonClientEventCallbackPtr callback = opaque;
853 854
    remote_domain_event_pmsuspend_disk_msg data;

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

859 860
    VIR_DEBUG("Relaying domain %s %d system pmsuspend-disk, callback %d",
              dom->name, dom->id, callback->callbackID);
861 862 863 864 865

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

866 867 868 869 870 871
    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 已提交
872
                                                                reason, data };
873 874 875 876 877

        remoteDispatchObjectEventSend(callback->client, remoteProgram,
                                      REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND_DISK,
                                      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_disk_msg, &msg);
    }
878 879 880 881

    return 0;
}

882
static int
883
remoteRelayDomainEventDeviceRemoved(virConnectPtr conn,
884 885 886 887
                                    virDomainPtr dom,
                                    const char *devAlias,
                                    void *opaque)
{
888
    daemonClientEventCallbackPtr callback = opaque;
889 890
    remote_domain_event_device_removed_msg data;

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

895 896
    VIR_DEBUG("Relaying domain device removed event %s %d %s, callback %d",
              dom->name, dom->id, devAlias, callback->callbackID);
897 898 899 900 901 902 903 904 905

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

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

    make_nonnull_domain(&data.dom, dom);

906 907 908 909 910 911 912 913 914 915 916 917 918 919
    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);
    }
920 921 922 923

    return 0;
}

924

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
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;
}


963 964 965 966 967 968 969 970 971 972 973 974 975 976
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;

977 978
    VIR_DEBUG("Relaying domain tunable event %s %d, callback %d, params %p %d",
              dom->name, dom->id, callback->callbackID, params, nparams);
979 980 981 982 983 984

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

985 986 987 988
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &data.params.params_val,
                                &data.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0)
989 990 991 992 993 994 995 996 997 998 999
        return -1;

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

    return 0;
}


1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
static int
remoteRelayDomainEventAgentLifecycle(virConnectPtr conn,
                                     virDomainPtr dom,
                                     int state,
                                     int reason,
                                     void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_agent_lifecycle_msg data;

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

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

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

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

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

    return 0;
}


1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
static int
remoteRelayDomainEventDeviceAdded(virConnectPtr conn,
                                  virDomainPtr dom,
                                  const char *devAlias,
                                  void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_device_added_msg data;

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

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

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

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

    make_nonnull_domain(&data.dom, dom);
E
Eric Blake 已提交
1058
    data.callbackID = callback->callbackID;
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

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

    return 0;
}


1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
static int
remoteRelayDomainEventMigrationIteration(virConnectPtr conn,
                                         virDomainPtr dom,
                                         int iteration,
                                         void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_migration_iteration_msg data;

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

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

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

    data.iteration = iteration;

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

    return 0;
}
1100 1101


J
Jiri Denemark 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
static int
remoteRelayDomainEventJobCompleted(virConnectPtr conn,
                                   virDomainPtr dom,
                                   virTypedParameterPtr params,
                                   int nparams,
                                   void *opaque)
{
    daemonClientEventCallbackPtr callback = opaque;
    remote_domain_event_callback_job_completed_msg data;

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

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

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

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

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


1139
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
1140
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
1141
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
1142
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
1143
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
1144
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
1145
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
1146
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
1147
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
1148
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
1149
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
1150
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
1151
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
1152
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
1153
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
1154
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
1155
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
1156
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob2),
1157
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTunable),
1158
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventAgentLifecycle),
1159
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceAdded),
1160
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventMigrationIteration),
J
Jiri Denemark 已提交
1161
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventJobCompleted),
1162 1163 1164 1165
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

1166
static int
1167
remoteRelayNetworkEventLifecycle(virConnectPtr conn,
1168 1169 1170 1171
                                 virNetworkPtr net,
                                 int event,
                                 int detail,
                                 void *opaque)
1172
{
1173
    daemonClientEventCallbackPtr callback = opaque;
1174 1175
    remote_network_event_lifecycle_msg data;

1176 1177
    if (callback->callbackID < 0 ||
        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
1178 1179
        return -1;

1180 1181
    VIR_DEBUG("Relaying network lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);
1182 1183 1184 1185

    /* build return data */
    memset(&data, 0, sizeof(data));
    make_nonnull_network(&data.net, net);
1186
    data.callbackID = callback->callbackID;
1187 1188 1189
    data.event = event;
    data.detail = detail;

1190
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
                                  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);

1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
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;

1244
 error:
1245 1246 1247 1248
    VIR_FREE(data.event);
    VIR_FREE(details_p);
}

1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
static
void remoteRelayConnectionClosedEvent(virConnectPtr conn ATTRIBUTE_UNUSED, int reason, void *opaque)
{
    virNetServerClientPtr client = opaque;

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

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

1263 1264 1265 1266 1267 1268 1269
/*
 * 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
 */
1270
void remoteClientFreeFunc(void *data)
1271 1272 1273 1274 1275
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
1276
        virIdentityPtr sysident = virIdentityGetSystem();
1277
        size_t i;
1278

1279 1280
        virIdentitySetCurrent(sysident);

1281 1282 1283 1284 1285
        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;
1286
            }
1287 1288 1289 1290 1291
            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");
1292
        }
1293
        VIR_FREE(priv->domainEventCallbacks);
1294

1295 1296 1297 1298 1299
        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;
1300
            }
1301 1302 1303 1304 1305 1306
            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");
1307
        }
1308
        VIR_FREE(priv->networkEventCallbacks);
1309

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
        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);

1325 1326 1327 1328 1329 1330
        if (priv->closeRegistered) {
            if (virConnectUnregisterCloseCallback(priv->conn,
                                                  remoteRelayConnectionClosedEvent) < 0)
                VIR_WARN("unexpected close callback event deregister failure");
        }

1331
        virConnectClose(priv->conn);
1332 1333 1334

        virIdentitySetCurrent(NULL);
        virObjectUnref(sysident);
1335 1336 1337 1338 1339 1340
    }

    VIR_FREE(priv);
}


1341 1342 1343 1344 1345 1346 1347
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

1348

1349 1350
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
1351 1352 1353
{
    struct daemonClientPrivate *priv;

1354
    if (VIR_ALLOC(priv) < 0)
1355
        return NULL;
1356 1357 1358

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
1359
        virReportSystemError(errno, "%s", _("unable to init mutex"));
1360
        return NULL;
1361 1362
    }

1363
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
1364
    return priv;
1365 1366
}

1367 1368 1369
/*----- Functions. -----*/

static int
1370
remoteDispatchConnectOpen(virNetServerPtr server ATTRIBUTE_UNUSED,
1371 1372 1373 1374
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
1375 1376
{
    const char *name;
1377
    unsigned int flags;
1378
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
1379
    int rv = -1;
1380

1381 1382 1383 1384
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
1385
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
1386 1387 1388
        goto cleanup;
    }

1389 1390 1391 1392 1393 1394
    name = args->name ? *args->name : NULL;

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

1398
    priv->conn =
1399
        flags & VIR_CONNECT_RO
1400 1401
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
1402

1403
    if (priv->conn == NULL)
1404 1405 1406
        goto cleanup;

    rv = 0;
1407

1408
 cleanup:
1409
    if (rv < 0)
1410 1411
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
1412
    return rv;
1413 1414 1415 1416
}


static int
1417 1418 1419 1420
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
1421
{
1422
    virNetServerClientDelayedClose(client);
1423
    return 0;
1424 1425
}

1426

1427
static int
1428 1429
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1430
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1431
                                     virNetMessageErrorPtr rerr,
1432 1433
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
1434
{
1435
    virDomainPtr dom = NULL;
1436 1437
    char *type;
    int nparams;
1438
    int rv = -1;
1439 1440
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1441

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

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

1450
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
1451
        goto cleanup;
1452 1453 1454

    ret->type = type;
    ret->nparams = nparams;
1455 1456
    rv = 0;

1457
 cleanup:
1458
    if (rv < 0)
1459
        virNetMessageSaveError(rerr);
1460
    virObjectUnref(dom);
1461
    return rv;
1462 1463 1464
}

static int
1465 1466
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
1467
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
1468
                                           virNetMessageErrorPtr rerr,
1469 1470
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
1471
{
1472
    virDomainPtr dom = NULL;
1473
    virTypedParameterPtr params = NULL;
1474
    int nparams = 0;
1475
    int rv = -1;
1476 1477
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1478

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

1484
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1485
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1486
        goto cleanup;
1487
    }
1488
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1489
        goto cleanup;
1490
    nparams = args->nparams;
1491

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

1495
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1496
        goto cleanup;
1497

1498 1499 1500 1501
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                0) < 0)
1502 1503 1504 1505
        goto cleanup;

    rv = 0;

1506
 cleanup:
1507
    if (rv < 0)
1508
        virNetMessageSaveError(rerr);
1509
    virTypedParamsFree(params, nparams);
1510
    virObjectUnref(dom);
1511 1512 1513 1514
    return rv;
}

static int
1515 1516
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1517
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1518
                                                virNetMessageErrorPtr rerr,
1519 1520 1521 1522 1523
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1524
    int nparams = 0;
1525
    int rv = -1;
1526 1527
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1528

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

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

1542
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1543 1544 1545 1546 1547 1548
        goto cleanup;

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

1549 1550 1551 1552
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
1553
        goto cleanup;
1554

1555
    rv = 0;
1556

1557
 cleanup:
1558
    if (rv < 0)
1559
        virNetMessageSaveError(rerr);
1560
    virTypedParamsFree(params, nparams);
1561
    virObjectUnref(dom);
1562
    return rv;
1563 1564
}

1565
static int
1566 1567
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1568
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1569
                                virNetMessageErrorPtr rerr,
1570 1571
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1572
{
1573
    virDomainPtr dom = NULL;
1574
    virDomainMemoryStatPtr stats = NULL;
1575 1576
    int nr_stats;
    size_t i;
1577
    int rv = -1;
1578 1579
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1580

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

1586
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1587 1588
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1589
        goto cleanup;
1590 1591
    }

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

    /* Allocate stats array for making dispatch call */
1596
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
1597
        goto cleanup;
1598

1599
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1600
    if (nr_stats < 0)
1601
        goto cleanup;
1602 1603

    /* Allocate return buffer */
1604
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
1605
        goto cleanup;
1606 1607 1608 1609 1610 1611 1612

    /* 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;
1613 1614
    rv = 0;

1615
 cleanup:
1616
    if (rv < 0)
1617
        virNetMessageSaveError(rerr);
1618
    virObjectUnref(dom);
1619
    VIR_FREE(stats);
1620
    return rv;
1621 1622
}

1623
static int
1624 1625
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1626
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1627
                              virNetMessageErrorPtr rerr,
1628 1629
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1630
{
1631
    virDomainPtr dom = NULL;
1632 1633 1634 1635
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1636
    int rv = -1;
1637 1638
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1639

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

1645
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1646
        goto cleanup;
1647 1648 1649 1650 1651 1652
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1653 1654
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1655
        goto cleanup;
1656 1657 1658
    }

    ret->buffer.buffer_len = size;
1659
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1660
        goto cleanup;
1661

1662
    if (virDomainBlockPeek(dom, path, offset, size,
1663
                           ret->buffer.buffer_val, flags) < 0)
1664
        goto cleanup;
1665

1666 1667
    rv = 0;

1668
 cleanup:
1669
    if (rv < 0) {
1670
        virNetMessageSaveError(rerr);
1671 1672
        VIR_FREE(ret->buffer.buffer_val);
    }
1673
    virObjectUnref(dom);
1674
    return rv;
1675 1676
}

1677 1678 1679
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1680
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1681 1682 1683 1684 1685 1686 1687
                                    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;
1688
    int nparams = 0;
1689 1690 1691 1692 1693 1694
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

1703
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1704
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1705 1706
        goto cleanup;
    }
1707
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1708
        goto cleanup;
1709
    nparams = args->nparams;
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721

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

1722 1723 1724 1725 1726
    /* Serialize the block stats. */
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
1727 1728
        goto cleanup;

1729
 success:
1730 1731
    rv = 0;

1732
 cleanup:
1733
    if (rv < 0)
1734
        virNetMessageSaveError(rerr);
1735
    virTypedParamsFree(params, nparams);
1736
    virObjectUnref(dom);
1737 1738 1739
    return rv;
}

R
Richard W.M. Jones 已提交
1740
static int
1741 1742
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1743
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1744
                               virNetMessageErrorPtr rerr,
1745 1746
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1747
{
1748
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1749 1750 1751
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1752
    int rv = -1;
1753 1754
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1755

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

1761
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1762
        goto cleanup;
R
Richard W.M. Jones 已提交
1763 1764 1765 1766 1767
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1768 1769
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1770
        goto cleanup;
R
Richard W.M. Jones 已提交
1771 1772 1773
    }

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

1777
    if (virDomainMemoryPeek(dom, offset, size,
1778
                            ret->buffer.buffer_val, flags) < 0)
1779
        goto cleanup;
R
Richard W.M. Jones 已提交
1780

1781 1782
    rv = 0;

1783
 cleanup:
1784
    if (rv < 0) {
1785
        virNetMessageSaveError(rerr);
1786 1787
        VIR_FREE(ret->buffer.buffer_val);
    }
1788
    virObjectUnref(dom);
1789
    return rv;
R
Richard W.M. Jones 已提交
1790 1791
}

1792
static int
1793 1794
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1795
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1796
                                     virNetMessageErrorPtr rerr,
1797 1798
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1799
{
1800 1801
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1802
    int rv = -1;
1803 1804
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1805

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

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

1814
    if (VIR_ALLOC(seclabel) < 0)
1815 1816 1817 1818 1819 1820
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
1821
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
1822
        goto cleanup;
1823 1824
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1825

1826 1827
    rv = 0;

1828
 cleanup:
1829
    if (rv < 0)
1830
        virNetMessageSaveError(rerr);
1831
    virObjectUnref(dom);
1832
    VIR_FREE(seclabel);
1833
    return rv;
1834 1835
}

M
Marcelo Cerri 已提交
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
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;
1846 1847
    int len, rv = -1;
    size_t i;
M
Marcelo Cerri 已提交
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
    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;
    }

1866
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
1867 1868 1869 1870 1871
        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];
1872
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0)
M
Marcelo Cerri 已提交
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
            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;

1884
 done:
M
Marcelo Cerri 已提交
1885 1886
    rv = 0;

1887
 cleanup:
M
Marcelo Cerri 已提交
1888 1889
    if (rv < 0)
        virNetMessageSaveError(rerr);
1890
    virObjectUnref(dom);
M
Marcelo Cerri 已提交
1891 1892 1893 1894
    VIR_FREE(seclabels);
    return rv;
}

1895
static int
1896 1897
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1898
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1899
                                   virNetMessageErrorPtr rerr,
1900
                                   remote_node_get_security_model_ret *ret)
1901
{
1902
    virSecurityModel secmodel;
1903
    int rv = -1;
1904 1905
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1906

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

1912
    memset(&secmodel, 0, sizeof(secmodel));
1913
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
1914 1915 1916
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
1917
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
1918 1919 1920 1921
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
1922
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
1923
        goto cleanup;
1924
    strcpy(ret->doi.doi_val, secmodel.doi);
1925

1926 1927
    rv = 0;

1928
 cleanup:
1929
    if (rv < 0)
1930
        virNetMessageSaveError(rerr);
1931
    return rv;
1932 1933
}

1934
static int
1935 1936
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1937
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1938
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
1939 1940
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
1941 1942 1943 1944 1945
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
1946 1947
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1948

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

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

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
1958
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
1959 1960 1961 1962 1963
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
1964
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1965 1966 1967 1968 1969 1970
        goto cleanup;
    }

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

E
Eric Blake 已提交
1973
    if ((num = virDomainGetVcpuPinInfo(dom,
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
                                       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;
1987 1988 1989 1990
    cpumaps = NULL;

    rv = 0;

1991
 cleanup:
1992 1993 1994
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumaps);
1995
    virObjectUnref(dom);
1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
    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;

2027
 cleanup:
2028 2029
    if (rv < 0)
        virNetMessageSaveError(rerr);
2030
    virObjectUnref(dom);
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
    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)
2061
        goto cleanup;
2062 2063 2064 2065 2066 2067 2068 2069 2070 2071

    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;
2072 2073 2074 2075
    cpumaps = NULL;

    rv = 0;

2076
 cleanup:
2077
    if (rv < 0)
2078
        virNetMessageSaveError(rerr);
2079
    VIR_FREE(cpumaps);
2080
    virObjectUnref(dom);
2081 2082 2083
    return rv;
}

2084
static int
2085 2086
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
2087
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
2088
                             virNetMessageErrorPtr rerr,
2089 2090
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
2091
{
2092
    virDomainPtr dom = NULL;
2093 2094
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
2095 2096
    int info_len;
    size_t i;
2097
    int rv = -1;
2098 2099
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2100

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

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

2109
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
2110
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
2111
        goto cleanup;
2112
    }
2113

2114 2115
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
2116
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2117 2118 2119 2120 2121
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
2122
        goto cleanup;
2123 2124
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
2125
        goto cleanup;
2126 2127 2128 2129 2130 2131 2132 2133 2134

    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)
2135
        goto cleanup;
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152

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

2154
 cleanup:
2155
    if (rv < 0) {
2156
        virNetMessageSaveError(rerr);
2157 2158 2159 2160
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
2161
    virObjectUnref(dom);
2162
    return rv;
2163 2164
}

2165
static int
2166 2167 2168 2169 2170 2171
remoteDispatchDomainGetIOThreadInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_get_iothread_info_args *args,
                                    remote_domain_get_iothread_info_ret *ret)
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
{
    int rv = -1;
    size_t i;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainIOThreadInfoPtr *info = NULL;
    virDomainPtr dom = NULL;
    remote_domain_iothread_info *dst;
    int ninfo = 0;

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

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

2189
    if ((ninfo = virDomainGetIOThreadInfo(dom, &info, args->flags)) < 0)
2190 2191
        goto cleanup;

2192
    if (ninfo > REMOTE_IOTHREAD_INFO_MAX) {
2193 2194
        virReportError(VIR_ERR_RPC,
                       _("Too many IOThreads in info: %d for limit %d"),
2195
                       ninfo, REMOTE_IOTHREAD_INFO_MAX);
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
        goto cleanup;
    }

    if (ninfo) {
        if (VIR_ALLOC_N(ret->info.info_val, ninfo) < 0)
            goto cleanup;

        ret->info.info_len = ninfo;

        for (i = 0; i < ninfo; i++) {
            dst = &ret->info.info_val[i];
            dst->iothread_id = info[i]->iothread_id;

            /* No need to allocate/copy the cpumap if we make the reasonable
             * assumption that unsigned char and char are the same size.
             */
            dst->cpumap.cpumap_len = info[i]->cpumaplen;
            dst->cpumap.cpumap_val = (char *)info[i]->cpumap;
            info[i]->cpumap = NULL;
        }
    } else {
        ret->info.info_len = 0;
        ret->info.info_val = NULL;
    }

    ret->ret = ninfo;

    rv = 0;

 cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virObjectUnref(dom);
    if (ninfo >= 0)
        for (i = 0; i < ninfo; i++)
2231
            virDomainIOThreadInfoFree(info[i]);
2232 2233 2234 2235 2236
    VIR_FREE(info);

    return rv;
}

2237
static int
2238 2239
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2240
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2241
                                   virNetMessageErrorPtr rerr,
2242 2243
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
2244
{
2245 2246 2247 2248 2249
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2250
    int rv = -1;
2251 2252
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2253

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

2259 2260 2261 2262
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
2263
    if (VIR_ALLOC(uri_out) < 0)
2264
        goto cleanup;
2265

2266
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
2267 2268
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
2269
        goto cleanup;
2270

2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
    /* 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;
    }
2282

2283
    rv = 0;
2284

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

2292
static int
2293 2294
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2295
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2296
                                    virNetMessageErrorPtr rerr,
2297 2298
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
2299
{
2300 2301 2302 2303 2304
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2305
    int rv = -1;
2306 2307
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2308

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

2314 2315
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
2316

2317
    /* Wacky world of XDR ... */
2318
    if (VIR_ALLOC(uri_out) < 0)
2319
        goto cleanup;
2320

2321
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
2322 2323 2324
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
2325
        goto cleanup;
2326

2327 2328 2329 2330 2331 2332
    /* 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;
2333

2334 2335
    rv = 0;

2336
 cleanup:
2337
    if (rv < 0) {
2338
        virNetMessageSaveError(rerr);
2339 2340
        VIR_FREE(uri_out);
    }
2341
    return rv;
2342 2343
}

C
Chris Lalancette 已提交
2344
static int
2345 2346
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
2347
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
2348 2349 2350
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
2351
{
2352
    virDomainPtr dom = NULL;
2353
    virTypedParameterPtr params = NULL;
2354
    int nparams = 0;
2355
    unsigned int flags;
2356
    int rv = -1;
2357 2358
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2359

2360
    if (!priv->conn) {
2361
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2362
        goto cleanup;
2363
    }
C
Chris Lalancette 已提交
2364

2365
    flags = args->flags;
C
Chris Lalancette 已提交
2366

2367
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2368
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2369 2370
        goto cleanup;
    }
2371
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2372
        goto cleanup;
2373
    nparams = args->nparams;
C
Chris Lalancette 已提交
2374

2375
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2376
        goto cleanup;
C
Chris Lalancette 已提交
2377

2378
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
2379
        goto cleanup;
C
Chris Lalancette 已提交
2380

2381 2382 2383 2384 2385 2386
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
2387 2388
    }

2389 2390 2391 2392
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
2393
        goto cleanup;
2394

2395
 success:
2396 2397
    rv = 0;

2398
 cleanup:
2399
    if (rv < 0)
2400
        virNetMessageSaveError(rerr);
2401
    virTypedParamsFree(params, nparams);
2402
    virObjectUnref(dom);
2403
    return rv;
2404 2405
}

2406 2407 2408 2409 2410 2411 2412 2413 2414 2415
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;
2416
    int nparams = 0;
2417 2418 2419 2420 2421 2422
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2423
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2424 2425 2426 2427 2428
        goto cleanup;
    }

    flags = args->flags;

2429
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
2430
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2431 2432
        goto cleanup;
    }
2433
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2434
        goto cleanup;
2435
    nparams = args->nparams;
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450

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

2451 2452 2453 2454
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                flags) < 0)
2455 2456
        goto cleanup;

2457
 success:
2458 2459
    rv = 0;

2460
 cleanup:
2461 2462
    if (rv < 0)
        virNetMessageSaveError(rerr);
2463
    virTypedParamsFree(params, nparams);
2464
    virObjectUnref(dom);
2465 2466 2467
    return rv;
}

2468
static int
2469 2470
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
2471
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2472 2473 2474
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
2475
{
2476
    virDomainPtr dom = NULL;
2477
    virTypedParameterPtr params = NULL;
2478
    int nparams = 0;
2479
    unsigned int flags;
2480
    int rv = -1;
2481 2482
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2483

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

2489 2490
    flags = args->flags;

2491
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2492
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2493
        goto cleanup;
2494
    }
2495
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2496
        goto cleanup;
2497
    nparams = args->nparams;
2498

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

2502
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2503
        goto cleanup;
2504

2505 2506 2507 2508 2509 2510 2511 2512
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2513 2514 2515 2516
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
2517
        goto cleanup;
2518

2519
 success:
2520
    rv = 0;
2521

2522
 cleanup:
2523
    if (rv < 0)
2524
        virNetMessageSaveError(rerr);
2525
    virTypedParamsFree(params, nparams);
2526
    virObjectUnref(dom);
2527
    return rv;
2528 2529
}

2530
static int
2531 2532
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2533
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2534 2535 2536
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2537
{
2538
    virNodeCPUStatsPtr params = NULL;
2539
    size_t i;
2540
    int cpuNum = args->cpuNum;
2541
    int nparams = 0;
2542 2543
    unsigned int flags;
    int rv = -1;
2544 2545
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2546

2547
    if (!priv->conn) {
2548
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2549 2550 2551 2552 2553
        goto cleanup;
    }

    flags = args->flags;

2554
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
2555
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2556 2557
        goto cleanup;
    }
2558
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2559
        goto cleanup;
2560
    nparams = args->nparams;
2561

2562
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
        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)
2576
        goto cleanup;
2577 2578 2579

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2580 2581
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2582 2583 2584 2585

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

2586
 success:
2587 2588
    rv = 0;

2589
 cleanup:
2590
    if (rv < 0) {
2591
        virNetMessageSaveError(rerr);
2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602
        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
2603 2604
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2605
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2606 2607 2608
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2609
{
2610
    virNodeMemoryStatsPtr params = NULL;
2611
    size_t i;
2612
    int cellNum = args->cellNum;
2613
    int nparams = 0;
2614 2615
    unsigned int flags;
    int rv = -1;
2616 2617
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2618

2619
    if (!priv->conn) {
2620
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2621 2622 2623 2624 2625
        goto cleanup;
    }

    flags = args->flags;

2626
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
2627
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2628 2629
        goto cleanup;
    }
2630
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2631
        goto cleanup;
2632
    nparams = args->nparams;
2633

2634
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647
        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)
2648
        goto cleanup;
2649 2650 2651

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2652 2653
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2654 2655 2656 2657

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

2658
 success:
2659 2660
    rv = 0;

2661
 cleanup:
2662
    if (rv < 0) {
2663
        virNetMessageSaveError(rerr);
2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
        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;
}

2674 2675 2676
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2677
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
                                    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) {
2689
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706
        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;

2707
 cleanup:
2708 2709
    if (rv < 0)
        virNetMessageSaveError(rerr);
2710
    virObjectUnref(dom);
2711 2712 2713
    return rv;
}

2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
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;
2725
    int nparams = 0;
2726 2727 2728 2729
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

2734
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
2735
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2736 2737 2738
        goto cleanup;
    }

2739
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2740
        goto cleanup;
2741
    nparams = args->nparams;
2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757

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

2758 2759 2760 2761 2762
    /* Serialize the block I/O tuning parameters. */
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
2763 2764
        goto cleanup;

2765
 success:
2766 2767
    rv = 0;

2768
 cleanup:
2769 2770
    if (rv < 0)
        virNetMessageSaveError(rerr);
2771
    virTypedParamsFree(params, nparams);
2772
    virObjectUnref(dom);
2773 2774
    return rv;
}
2775

D
Daniel Veillard 已提交
2776 2777
/*-------------------------------------------------------------*/

2778
static int
2779
remoteDispatchAuthList(virNetServerPtr server,
2780
                       virNetServerClientPtr client,
2781
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2782
                       virNetMessageErrorPtr rerr,
2783
                       remote_auth_list_ret *ret)
2784
{
2785
    int rv = -1;
2786 2787
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2788
    gid_t callerGid;
2789
    pid_t callerPid;
2790
    unsigned long long timestamp;
2791 2792 2793 2794 2795 2796

    /* 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) {
2797
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2798
                                              &callerPid, &timestamp) < 0) {
2799 2800 2801 2802
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2803 2804
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
2805
                            (long long) callerPid, (int) callerUid) < 0)
J
Jim Fehlig 已提交
2806 2807
                goto cleanup;
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
2808
            virNetServerClientSetAuth(client, 0);
2809
            virNetServerTrackCompletedAuth(server);
2810
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
2811
            VIR_FREE(ident);
2812 2813
        }
    }
2814

2815
    ret->types.types_len = 1;
2816
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
2817
        goto cleanup;
2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831

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

2833 2834
    rv = 0;

2835
 cleanup:
2836
    if (rv < 0)
2837
        virNetMessageSaveError(rerr);
2838
    return rv;
2839 2840 2841
}


2842
#ifdef WITH_SASL
2843 2844
/*
 * Initializes the SASL session in prepare for authentication
2845
 * and gives the client a list of allowed mechanisms to choose
2846 2847
 */
static int
2848 2849
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2850
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2851
                           virNetMessageErrorPtr rerr,
2852
                           remote_auth_sasl_init_ret *ret)
2853
{
2854 2855 2856
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2857

2858
    virMutexLock(&priv->lock);
2859

2860 2861 2862
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2863
        VIR_ERROR(_("client tried invalid SASL init request"));
2864
        goto authfail;
2865 2866
    }

2867 2868 2869 2870 2871
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2872
        goto authfail;
2873

2874
# if WITH_GNUTLS
2875
    /* Inform SASL that we've got an external SSF layer from TLS */
2876 2877 2878 2879
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2880
            goto authfail;
2881 2882 2883 2884 2885

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2886
            goto authfail;
2887
    }
2888
# endif
2889

2890
    if (virNetServerClientIsSecure(client))
2891
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2892 2893
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2894
        /* Plain TCP, better get an SSF layer */
2895 2896 2897 2898
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
2899

2900
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
2901
        goto authfail;
2902
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
2903

2904 2905
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
2906
    return 0;
2907

2908
 authfail:
2909
    virResetLastError();
2910 2911
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2912
    virNetMessageSaveError(rerr);
2913 2914 2915
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2916
    virObjectUnref(sasl);
2917
    virMutexUnlock(&priv->lock);
2918
    return -1;
2919 2920
}

2921
/*
2922 2923
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
2924
static int
2925 2926
remoteSASLFinish(virNetServerPtr server,
                 virNetServerClientPtr client)
2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941
{
    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;
        }
2942
    }
2943 2944

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
2945
        return -2;
2946

2947 2948
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
2949

2950
    virNetServerClientSetAuth(client, 0);
2951
    virNetServerTrackCompletedAuth(server);
2952
    virNetServerClientSetSASLSession(client, priv->sasl);
2953

2954
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
2955

2956 2957 2958
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2959

2960
    virObjectUnref(priv->sasl);
2961
    priv->sasl = NULL;
2962

2963
    return 0;
2964

2965
 error:
2966 2967
    return -1;
}
2968

2969 2970 2971 2972
/*
 * This starts the SASL authentication negotiation.
 */
static int
2973
remoteDispatchAuthSaslStart(virNetServerPtr server,
2974
                            virNetServerClientPtr client,
2975
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2976
                            virNetMessageErrorPtr rerr,
2977 2978
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2979 2980
{
    const char *serverout;
2981
    size_t serveroutlen;
2982
    int err;
2983 2984 2985
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2986
    const char *identity;
2987

2988
    virMutexLock(&priv->lock);
2989

2990 2991 2992
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
2993
        VIR_ERROR(_("client tried invalid SASL start request"));
2994
        goto authfail;
2995 2996
    }

2997 2998
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
2999 3000 3001 3002 3003 3004 3005 3006 3007
    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)
3008
        goto authfail;
3009

3010
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3011
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
3012
        goto authfail;
3013 3014 3015 3016
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3017 3018
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3019 3020 3021 3022 3023 3024 3025
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3026
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3027
    if (err == VIR_NET_SASL_CONTINUE) {
3028 3029
        ret->complete = 0;
    } else {
3030
        /* Check username whitelist ACL */
3031
        if ((err = remoteSASLFinish(server, client)) < 0) {
3032 3033 3034 3035 3036
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3037

3038 3039 3040
        ret->complete = 1;
    }

3041
    virMutexUnlock(&priv->lock);
3042
    return 0;
3043

3044
 authfail:
3045 3046 3047
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3048 3049
    goto error;

3050
 authdeny:
3051
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3052 3053 3054
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3055 3056
    goto error;

3057
 error:
3058
    virObjectUnref(priv->sasl);
3059 3060
    priv->sasl = NULL;
    virResetLastError();
3061 3062
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3063 3064 3065
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3066
    return -1;
3067 3068 3069 3070
}


static int
3071
remoteDispatchAuthSaslStep(virNetServerPtr server,
3072
                           virNetServerClientPtr client,
3073
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3074
                           virNetMessageErrorPtr rerr,
3075 3076
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
3077 3078
{
    const char *serverout;
3079
    size_t serveroutlen;
3080
    int err;
3081 3082 3083
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3084
    const char *identity;
3085

3086 3087 3088 3089 3090
    virMutexLock(&priv->lock);

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

3095
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
3096
              args->data.data_len, args->nil);
3097 3098 3099 3100 3101 3102 3103 3104
    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)
3105
        goto authfail;
3106 3107

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3108
        VIR_ERROR(_("sasl step reply data too long %d"),
3109
                  (int)serveroutlen);
3110
        goto authfail;
3111 3112 3113 3114
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3115 3116
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3117 3118 3119 3120 3121 3122 3123
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3124
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3125
    if (err == VIR_NET_SASL_CONTINUE) {
3126 3127
        ret->complete = 0;
    } else {
3128
        /* Check username whitelist ACL */
3129
        if ((err = remoteSASLFinish(server, client)) < 0) {
3130 3131 3132 3133 3134
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3135

3136 3137 3138
        ret->complete = 1;
    }

3139
    virMutexUnlock(&priv->lock);
3140
    return 0;
3141

3142
 authfail:
3143 3144 3145
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3146 3147
    goto error;

3148
 authdeny:
3149
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3150 3151 3152
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3153 3154
    goto error;

3155
 error:
3156
    virObjectUnref(priv->sasl);
3157 3158
    priv->sasl = NULL;
    virResetLastError();
3159 3160
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3161 3162 3163
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3164 3165
    return -1;
}
3166 3167 3168 3169
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3170
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3171 3172 3173 3174
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3175 3176
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3177 3178 3179 3180 3181 3182
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
3183
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3184 3185 3186 3187 3188
                            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");
3189 3190
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3191 3192 3193 3194 3195 3196
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3197
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3198 3199 3200 3201 3202
                           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");
3203 3204
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3205 3206 3207 3208
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
3209 3210 3211



3212
static int
3213
remoteDispatchAuthPolkit(virNetServerPtr server,
3214
                         virNetServerClientPtr client,
3215
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3216
                         virNetMessageErrorPtr rerr,
3217
                         remote_auth_polkit_ret *ret)
3218
{
3219
    pid_t callerPid = -1;
3220
    gid_t callerGid = -1;
3221
    uid_t callerUid = -1;
3222
    unsigned long long timestamp;
3223
    const char *action;
3224
    char *ident = NULL;
3225 3226
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3227
    int rv;
3228

3229 3230
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
3231 3232 3233
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

3234 3235
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3236
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3237 3238 3239
        goto authfail;
    }

3240
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
3241
                                          &callerPid, &timestamp) < 0) {
3242 3243 3244
        goto authfail;
    }

3245 3246 3247 3248 3249 3250
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

3251 3252
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3253

3254 3255 3256 3257 3258 3259 3260
    rv = virPolkitCheckAuth(action,
                            callerPid,
                            timestamp,
                            callerUid,
                            NULL,
                            true);
    if (rv == -1)
3261
        goto authfail;
3262
    else if (rv == -2)
3263
        goto authdeny;
3264

3265 3266 3267
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3268 3269
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
3270 3271
    ret->complete = 1;

3272
    virNetServerClientSetAuth(client, 0);
3273
    virNetServerTrackCompletedAuth(server);
3274
    virMutexUnlock(&priv->lock);
3275

3276
    return 0;
3277

3278
 error:
3279
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3280
    virMutexUnlock(&priv->lock);
3281 3282
    return -1;

3283
 authfail:
3284 3285 3286
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3287
    goto error;
3288

3289
 authdeny:
3290 3291 3292
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3293
    goto error;
3294 3295
}

3296

3297 3298 3299
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3300

3301
static int
3302 3303
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3304
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3305
                                  virNetMessageErrorPtr rerr,
3306 3307
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3308
{
3309 3310
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3311
    int rv = -1;
3312 3313
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3314

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

3320
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3321 3322
        goto cleanup;

3323 3324 3325 3326 3327 3328 3329
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
3330
        if (VIR_ALLOC(parent_p) < 0)
3331
            goto cleanup;
3332
        if (VIR_STRDUP(*parent_p, parent) < 0) {
3333 3334 3335 3336 3337 3338
            VIR_FREE(parent_p);
            goto cleanup;
        }
        ret->parent = parent_p;
    }

3339 3340
    rv = 0;

3341
 cleanup:
3342
    if (rv < 0)
3343
        virNetMessageSaveError(rerr);
3344
    virObjectUnref(dev);
3345
    return rv;
3346 3347
}

3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366
static int
remoteDispatchConnectCloseCallbackRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client,
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    virMutexLock(&priv->lock);

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

    if (virConnectRegisterCloseCallback(priv->conn,
                                        remoteRelayConnectionClosedEvent,
3367
                                        client, NULL) < 0)
3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409
        goto cleanup;

    priv->closeRegistered = true;
    rv = 0;

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

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

    virMutexLock(&priv->lock);

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

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

    priv->closeRegistered = false;
    rv = 0;

 cleanup:
    virMutexUnlock(&priv->lock);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}
3410 3411 3412 3413 3414

/***************************
 * Register / deregister events
 ***************************/
static int
3415
remoteDispatchConnectDomainEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
3416
                                         virNetServerClientPtr client,
3417 3418 3419
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                         remote_connect_domain_event_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
3420
{
3421
    int callbackID;
3422
    int rv = -1;
3423 3424
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3425 3426
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3427

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

3433 3434
    virMutexLock(&priv->lock);

3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445
    /* 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;
3446
    callback->legacy = true;
3447 3448 3449 3450
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3451
        goto cleanup;
O
Osier Yang 已提交
3452

3453
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3454 3455 3456
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
3457 3458 3459 3460 3461
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3462
        goto cleanup;
3463
    }
O
Osier Yang 已提交
3464

3465
    ref->callbackID = callbackID;
3466

3467 3468
    rv = 0;

3469
 cleanup:
3470
    VIR_FREE(callback);
3471
    if (rv < 0)
3472 3473
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3474
    return rv;
O
Osier Yang 已提交
3475 3476
}

3477
static int
3478
remoteDispatchConnectDomainEventDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
3479
                                           virNetServerClientPtr client,
3480 3481 3482
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                           remote_connect_domain_event_deregister_ret *ret ATTRIBUTE_UNUSED)
3483
{
3484
    int callbackID = -1;
3485
    int rv = -1;
3486
    size_t i;
3487 3488
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3489

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

3495 3496
    virMutexLock(&priv->lock);

3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
    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);
3508
        goto cleanup;
3509
    }
3510

3511
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3512
        goto cleanup;
3513

3514 3515
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3516

3517 3518
    rv = 0;

3519
 cleanup:
3520
    if (rv < 0)
3521 3522
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3523 3524 3525 3526
    return rv;
}

static void
3527
remoteDispatchObjectEventSend(virNetServerClientPtr client,
3528
                              virNetServerProgramPtr program,
3529 3530 3531 3532
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3533
    virNetMessagePtr msg;
3534

3535
    if (!(msg = virNetMessageNew(false)))
3536
        goto cleanup;
3537

3538 3539 3540 3541 3542 3543
    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;
3544

3545
    if (virNetMessageEncodeHeader(msg) < 0)
3546 3547
        goto cleanup;

3548 3549
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3550

3551 3552
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3553

3554
    xdr_free(proc, data);
3555 3556
    return;

3557
 cleanup:
3558
    virNetMessageFree(msg);
3559
    xdr_free(proc, data);
3560 3561
}

3562
static int
3563 3564
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3565
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3566
                             virNetMessageErrorPtr rerr,
3567 3568
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3569
{
3570 3571 3572
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3573
    int rv = -1;
3574 3575
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3576

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

3582
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3583
        goto cleanup;
3584

3585
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3586
        goto cleanup;
3587

3588 3589 3590
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3591 3592
    rv = 0;

3593
 cleanup:
3594
    if (rv < 0)
3595
        virNetMessageSaveError(rerr);
3596
    virObjectUnref(secret);
3597
    return rv;
3598 3599
}

3600
static int
3601 3602
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3603
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3604
                             virNetMessageErrorPtr rerr,
3605 3606 3607 3608 3609
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3610 3611
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3612

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

3618
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3619 3620 3621 3622 3623 3624 3625
        goto cleanup;

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

    rv = 0;

3626
 cleanup:
3627
    if (rv < 0)
3628
        virNetMessageSaveError(rerr);
3629
    virObjectUnref(dom);
3630 3631 3632
    return rv;
}

3633 3634 3635 3636 3637 3638

/* 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.  */
3639
static int
3640
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3641
                                            virNetServerClientPtr client,
3642 3643 3644
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            remote_connect_domain_event_register_any_args *args)
3645 3646
{
    int callbackID;
3647
    int rv = -1;
3648 3649
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3650 3651
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3652

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

3658 3659
    virMutexLock(&priv->lock);

3660 3661 3662 3663
    /* 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 ||
3664
        args->eventID < 0) {
3665 3666
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3667
        goto cleanup;
3668 3669
    }

3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680
    /* 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;
3681
    callback->legacy = true;
3682 3683 3684 3685
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3686
        goto cleanup;
3687

3688
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3689 3690 3691
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3692 3693 3694 3695 3696
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3697
        goto cleanup;
3698
    }
3699

3700
    ref->callbackID = callbackID;
3701

3702 3703
    rv = 0;

3704
 cleanup:
3705
    VIR_FREE(callback);
3706
    if (rv < 0)
3707 3708
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3709
    return rv;
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 3738 3739
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;

3740
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST || args->eventID < 0) {
3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779
        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;

3780
 cleanup:
3781 3782 3783
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
3784
    virObjectUnref(dom);
3785 3786 3787 3788 3789
    virMutexUnlock(&priv->lock);
    return rv;
}


3790
static int
3791
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3792
                                              virNetServerClientPtr client,
3793 3794 3795
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              remote_connect_domain_event_deregister_any_args *args)
3796 3797
{
    int callbackID = -1;
3798
    int rv = -1;
3799
    size_t i;
3800 3801
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3802

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

3808 3809
    virMutexLock(&priv->lock);

3810 3811 3812 3813
    /* 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 ||
3814
        args->eventID < 0) {
3815 3816
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3817
        goto cleanup;
3818 3819
    }

3820 3821 3822 3823 3824 3825
    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->eventID == args->eventID) {
            callbackID = priv->domainEventCallbacks[i]->callbackID;
            break;
        }
    }
3826
    if (callbackID < 0) {
3827 3828
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event %d not registered"), args->eventID);
3829
        goto cleanup;
3830 3831
    }

3832
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3833
        goto cleanup;
3834

3835 3836
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3837

3838 3839
    rv = 0;

3840
 cleanup:
3841
    if (rv < 0)
3842 3843
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3844
    return rv;
3845 3846
}

3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885

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;

3886
 cleanup:
3887 3888 3889 3890 3891 3892 3893
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}


C
Chris Lalancette 已提交
3894
static int
3895 3896 3897 3898 3899 3900
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 已提交
3901
{
3902
    virDomainPtr dom = NULL;
3903
    int rv = -1;
3904 3905
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
3906

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

3912
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3913
        goto cleanup;
C
Chris Lalancette 已提交
3914

3915
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
3916
                                    args->flags) < 0)
3917
        goto cleanup;
C
Chris Lalancette 已提交
3918

3919
    rv = 0;
C
Chris Lalancette 已提交
3920

3921
 cleanup:
3922
    if (rv < 0)
3923
        virNetMessageSaveError(rerr);
3924
    virObjectUnref(dom);
3925
    return rv;
C
Chris Lalancette 已提交
3926 3927
}

3928

3929
static int
3930 3931
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3932
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3933
                                  virNetMessageErrorPtr rerr,
3934 3935 3936 3937 3938 3939
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
3940
    char *xmlin;
3941 3942 3943
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3944 3945
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3946

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

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

3955
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3956 3957
    dname = args->dname == NULL ? NULL : *args->dname;

3958
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971
                                       &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;

3972
 cleanup:
3973
    if (rv < 0)
3974
        virNetMessageSaveError(rerr);
3975
    virObjectUnref(dom);
3976 3977 3978 3979 3980
    return rv;
}


static int
3981 3982
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3983
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3984
                                    virNetMessageErrorPtr rerr,
3985 3986 3987 3988 3989 3990 3991 3992 3993
                                    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;
3994 3995
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3996

3997
    if (!priv->conn) {
3998
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3999 4000 4001 4002 4003 4004 4005
        goto cleanup;
    }

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

    /* Wacky world of XDR ... */
4006
    if (VIR_ALLOC(uri_out) < 0)
4007 4008
        goto cleanup;

4009
    if (virDomainMigratePrepare3(priv->conn,
4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026
                                 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;

4027
 cleanup:
4028
    if (rv < 0) {
4029
        virNetMessageSaveError(rerr);
4030 4031 4032 4033 4034
        VIR_FREE(uri_out);
    }
    return rv;
}

4035

4036
static int
4037 4038
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4039
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4040
                                    virNetMessageErrorPtr rerr,
4041 4042 4043 4044
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
4045
    char *xmlin;
4046
    char *dname;
4047 4048
    char *uri;
    char *dconnuri;
4049 4050 4051
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4052 4053
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4054

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

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

4063
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4064
    dname = args->dname == NULL ? NULL : *args->dname;
4065 4066
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
4067

4068
    if (virDomainMigratePerform3(dom, xmlin,
4069 4070 4071
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
4072
                                 dconnuri, uri,
4073 4074 4075 4076 4077 4078 4079 4080 4081 4082
                                 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;

4083
 cleanup:
4084
    if (rv < 0)
4085
        virNetMessageSaveError(rerr);
4086
    virObjectUnref(dom);
4087 4088 4089 4090 4091
    return rv;
}


static int
4092 4093
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
4094
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
4095
                                   virNetMessageErrorPtr rerr,
4096 4097 4098 4099 4100 4101
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
4102 4103
    char *uri;
    char *dconnuri;
4104
    int rv = -1;
4105 4106
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4107

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

4113 4114 4115
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

4116
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
4117 4118 4119 4120 4121 4122
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
4123 4124
        goto cleanup;

4125
    make_nonnull_domain(&ret->dom, dom);
4126 4127 4128 4129 4130 4131 4132 4133

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

    rv = 0;

4134
 cleanup:
4135
    if (rv < 0) {
4136
        virNetMessageSaveError(rerr);
4137 4138
        VIR_FREE(cookieout);
    }
4139
    virObjectUnref(dom);
4140 4141 4142 4143 4144
    return rv;
}


static int
4145 4146
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4147
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4148 4149
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
4150 4151 4152
{
    virDomainPtr dom = NULL;
    int rv = -1;
4153 4154
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4155

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

4161
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4162 4163 4164 4165 4166 4167 4168 4169 4170 4171
        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;

4172
 cleanup:
4173
    if (rv < 0)
4174
        virNetMessageSaveError(rerr);
4175
    virObjectUnref(dom);
4176 4177 4178 4179
    return rv;
}


4180 4181 4182 4183 4184 4185
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)
4186 4187 4188 4189 4190 4191
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

4192 4193 4194 4195 4196 4197 4198 4199 4200 4201
    /* 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;
    }

4202
    if (!priv->conn) {
4203
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4204 4205 4206 4207 4208
        goto cleanup;
    }

    switch (args->feature) {
    case VIR_DRV_FEATURE_FD_PASSING:
4209
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
4210
    case VIR_DRV_FEATURE_REMOTE_CLOSE_CALLBACK:
4211 4212 4213 4214
        supported = 1;
        break;

    default:
4215
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
4216 4217 4218 4219
            goto cleanup;
        break;
    }

4220
 done:
4221 4222 4223
    ret->supported = supported;
    rv = 0;

4224
 cleanup:
4225 4226 4227 4228 4229 4230
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244
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) {
4245
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262
        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;

4263
 cleanup:
4264 4265 4266
    VIR_FORCE_CLOSE(fd);
    if (rv < 0)
        virNetMessageSaveError(rerr);
4267
    virObjectUnref(dom);
4268 4269 4270
    return rv;
}

4271

4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292
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;

4293 4294 4295
    if ((fd = virDomainOpenGraphicsFD(dom,
                                      args->idx,
                                      args->flags)) < 0)
4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306
        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);
4307
    if (rv < 0)
4308 4309
        virNetMessageSaveError(rerr);

4310
    virObjectUnref(dom);
4311 4312
    return rv;
}
4313 4314


4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325
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;
4326
    int nparams = 0;
4327 4328 4329 4330 4331 4332
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4333
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4334 4335 4336 4337 4338
        goto cleanup;
    }

    flags = args->flags;

4339
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
4340
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4341 4342
        goto cleanup;
    }
4343
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4344
        goto cleanup;
4345
    nparams = args->nparams;
4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360

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

4361 4362 4363 4364
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                flags) < 0)
4365 4366
        goto cleanup;

4367
 success:
4368 4369
    rv = 0;

4370
 cleanup:
4371 4372
    if (rv < 0)
        virNetMessageSaveError(rerr);
4373
    virTypedParamsFree(params, nparams);
4374
    virObjectUnref(dom);
4375 4376
    return rv;
}
4377

4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393
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) {
4394
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4395 4396 4397 4398
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
4399
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4400 4401 4402
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
4403
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
4404 4405 4406 4407
        goto cleanup;
    }

    if (args->nparams > 0 &&
4408
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422
        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;

4423 4424 4425 4426
    if (virTypedParamsSerialize(params, args->nparams * args->ncpus,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
4427 4428
        goto cleanup;

4429
 success:
4430 4431
    rv = 0;
    ret->nparams = percpu_len;
4432
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
4433
        size_t i;
4434 4435 4436 4437 4438 4439

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

4441
 cleanup:
4442 4443
    if (rv < 0)
         virNetMessageSaveError(rerr);
4444
    virTypedParamsFree(params, args->ncpus * args->nparams);
4445
    virObjectUnref(dom);
4446 4447 4448
    return rv;
}

4449 4450 4451 4452 4453 4454 4455
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)
4456 4457 4458 4459
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
4460
    int len = 0;
4461 4462 4463 4464
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
4473 4474
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
4475 4476 4477 4478
        goto cleanup;
    }

    if (args->maxerrors &&
4479
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495
        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;

4496
 cleanup:
4497 4498
    if (rv < 0)
        virNetMessageSaveError(rerr);
4499
    virObjectUnref(dom);
4500
    if (errors && len > 0) {
4501
        size_t i;
4502 4503 4504 4505 4506 4507 4508
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

4509

4510 4511 4512 4513 4514 4515 4516 4517 4518
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;
4519
    int nparams = 0;
4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531
    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;

4532
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
4533 4534 4535
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
4536
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4537
        goto cleanup;
4538
    nparams = args->nparams;
4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550

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

4551 4552 4553 4554
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                args->flags) < 0)
4555 4556
        goto cleanup;

4557
 success:
4558 4559
    rv = 0;

4560
 cleanup:
4561 4562
    if (rv < 0)
        virNetMessageSaveError(rerr);
4563
    virTypedParamsFree(params, nparams);
4564 4565 4566
    return rv;
}

4567 4568 4569 4570 4571 4572 4573 4574 4575
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;
4576
    unsigned int online = 0;
4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589
    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;

4590 4591
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
4592 4593 4594 4595
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
4596
    if (args->need_map) {
4597 4598 4599 4600 4601 4602 4603 4604 4605 4606
        ret->cpumap.cpumap_len = VIR_CPU_MAPLEN(cpunum);
        ret->cpumap.cpumap_val = (char *) cpumap;
        cpumap = NULL;
    }

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

    rv = 0;

4607
 cleanup:
4608 4609 4610 4611 4612 4613
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumap);
    return rv;
}

4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646
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
     */
4647
    for (i = 0; i < msg->nfds; i++)
4648 4649 4650 4651 4652 4653 4654 4655 4656
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

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

    rv = 1;

4657
 cleanup:
4658 4659
    if (rv < 0)
        virNetMessageSaveError(rerr);
4660
    virObjectUnref(dom);
4661 4662 4663
    return rv;
}

4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690
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;

4691 4692 4693 4694 4695 4696 4697
    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;
    }

4698 4699 4700 4701
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &ret->params.params_val,
                                &ret->params.params_len,
                                0) < 0)
4702 4703 4704 4705
        goto cleanup;

    rv = 0;

4706
 cleanup:
4707 4708 4709
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virTypedParamsFree(params, nparams);
4710
    virObjectUnref(dom);
4711 4712 4713
    return rv;
}

4714
static int
4715 4716 4717 4718 4719 4720
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)
4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736
{
    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;
    }

4737 4738 4739 4740 4741 4742 4743
    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;
    }

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

4747 4748 4749
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762
        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;

4763
 cleanup:
4764 4765 4766
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
4767
    virObjectUnref(dom);
4768 4769 4770 4771
    return rv;
}

static int
4772 4773 4774 4775 4776 4777
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)
4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792
{
    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;
    }

4793 4794 4795 4796 4797 4798 4799
    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;
    }

4800 4801 4802
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
4803 4804 4805
        goto cleanup;

    /* Wacky world of XDR ... */
4806
    if (VIR_ALLOC(uri_out) < 0)
4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821
        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;

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

static int
4832 4833 4834 4835 4836 4837
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)
4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853
{
    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;
    }

4854 4855 4856 4857 4858 4859 4860
    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;
    }

4861 4862 4863
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884
        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;

4885
 cleanup:
4886 4887 4888 4889 4890 4891 4892 4893
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
        if (stream) {
            virStreamAbort(st);
            daemonFreeClientStream(client, stream);
        } else {
4894
            virObjectUnref(st);
4895 4896 4897 4898 4899 4900 4901
        }
    }
    return rv;
}


static int
4902 4903 4904 4905 4906 4907
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)
4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923
{
    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;
    }

4924 4925 4926 4927 4928 4929 4930
    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;
    }

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

4934 4935 4936
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952
        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;

4953
 cleanup:
4954 4955 4956
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
4957
    virObjectUnref(dom);
4958 4959 4960 4961 4962
    return rv;
}


static int
4963 4964 4965 4966 4967 4968
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)
4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983
{
    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;
    }

4984 4985 4986 4987 4988 4989 4990
    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;
    }

4991 4992 4993
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010
        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;

5011
 cleanup:
5012 5013 5014 5015 5016
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
    }
5017
    virObjectUnref(dom);
5018 5019 5020 5021 5022
    return rv;
}


static int
5023 5024 5025 5026 5027
remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_confirm3_params_args *args)
5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040
{
    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;
    }

5041 5042 5043 5044 5045 5046 5047
    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;
    }

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

5051 5052 5053
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) args->params.params_val,
                                  args->params.params_len,
                                  0, &params, &nparams) < 0)
5054 5055 5056 5057 5058 5059 5060 5061 5062 5063
        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;

5064
 cleanup:
5065 5066 5067
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5068
    virObjectUnref(dom);
5069 5070 5071 5072
    return rv;
}


5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116
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;

5117
 cleanup:
5118 5119 5120 5121 5122 5123 5124
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virStringFreeList(models);
    return rv;
}


5125 5126 5127 5128 5129 5130 5131
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)
5132 5133 5134 5135 5136 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
{
    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;

5162
 cleanup:
5163
    for (i = 0; i < nfiles; i++)
5164 5165 5166 5167
        VIR_FORCE_CLOSE(files[i]);
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5168
    virObjectUnref(dom);
5169 5170 5171 5172
    return rv;
}


5173 5174 5175 5176 5177 5178
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)
5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211
{
    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;

5212
 cleanup:
5213
    for (i = 0; i < nfiles; i++)
5214 5215 5216 5217
        VIR_FORCE_CLOSE(files[i]);
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5218
    virObjectUnref(dom);
5219 5220 5221 5222
    return rv;
}


5223 5224
static int
remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5225
                                             virNetServerClientPtr client,
5226 5227 5228
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                             remote_connect_network_event_register_any_args *args,
5229
                                             remote_connect_network_event_register_any_ret *ret)
5230 5231 5232
{
    int callbackID;
    int rv = -1;
5233 5234
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
5235 5236
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
5237
    virNetworkPtr net = NULL;
5238 5239 5240 5241 5242 5243 5244 5245

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

    virMutexLock(&priv->lock);

5246 5247 5248 5249
    if (args->net &&
        !(net = get_nonnull_network(priv->conn, *args->net)))
        goto cleanup;

5250 5251 5252
    if (args->eventID >= VIR_NETWORK_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network event ID %d"), args->eventID);
5253 5254 5255
        goto cleanup;
    }

5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270
    /* 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)
5271 5272 5273
        goto cleanup;

    if ((callbackID = virConnectNetworkEventRegisterAny(priv->conn,
5274
                                                        net,
5275 5276
                                                        args->eventID,
                                                        networkEventCallbacks[args->eventID],
5277 5278 5279 5280 5281
                                                        ref,
                                                        remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->networkEventCallbacks,
                     priv->nnetworkEventCallbacks, 1);
        callback = ref;
5282
        goto cleanup;
5283
    }
5284

5285
    ref->callbackID = callbackID;
5286
    ret->callbackID = callbackID;
5287 5288 5289

    rv = 0;

5290
 cleanup:
5291
    VIR_FREE(callback);
5292 5293
    if (rv < 0)
        virNetMessageSaveError(rerr);
5294
    virObjectUnref(net);
5295 5296 5297 5298 5299 5300 5301
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5302
                                               virNetServerClientPtr client,
5303 5304
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
5305
                                               remote_connect_network_event_deregister_any_args *args)
5306 5307
{
    int rv = -1;
5308
    size_t i;
5309 5310 5311 5312 5313 5314 5315 5316 5317 5318
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    virMutexLock(&priv->lock);

5319
    for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
5320
        if (priv->networkEventCallbacks[i]->callbackID == args->callbackID)
5321 5322
            break;
    }
5323
    if (i == priv->nnetworkEventCallbacks) {
5324
        virReportError(VIR_ERR_INTERNAL_ERROR,
5325 5326
                       _("network event callback %d not registered"),
                       args->callbackID);
5327 5328 5329
        goto cleanup;
    }

5330
    if (virConnectNetworkEventDeregisterAny(priv->conn, args->callbackID) < 0)
5331 5332
        goto cleanup;

5333 5334
    VIR_DELETE_ELEMENT(priv->networkEventCallbacks, i,
                       priv->nnetworkEventCallbacks);
5335 5336 5337

    rv = 0;

5338
 cleanup:
5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407
    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;

5408
 cleanup:
5409 5410 5411
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
5412
    virObjectUnref(dom);
5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456
    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;

5457
 cleanup:
5458 5459 5460 5461 5462 5463
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}

5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496
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);
5497
    virObjectUnref(dom);
5498 5499
    return rv;
}
5500

M
Michal Privoznik 已提交
5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548

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;
5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563
}

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

5564
    if (VIR_STRDUP(lease_dst->iface, lease_src->iface) < 0 ||
J
Ján Tomko 已提交
5565
        VIR_STRDUP(lease_dst->ipaddr, lease_src->ipaddr) < 0)
5566 5567
        goto error;

J
Ján Tomko 已提交
5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592
    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;
5593 5594 5595 5596

    return 0;

 error:
J
Ján Tomko 已提交
5597 5598 5599 5600 5601 5602 5603 5604
    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);
5605 5606 5607 5608 5609
    VIR_FREE(mac_tmp);
    VIR_FREE(iaid_tmp);
    VIR_FREE(hostname_tmp);
    VIR_FREE(clientid_tmp);
    VIR_FREE(lease_dst->ipaddr);
5610
    VIR_FREE(lease_dst->iface);
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
    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,
5639
                                           args->mac ? *args->mac : NULL,
5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673
                                           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);
5674
    if (leases && nleases > 0)
5675 5676
        for (i = 0; i < nleases; i++)
            virNetworkDHCPLeaseFree(leases[i]);
5677
    VIR_FREE(leases);
5678
    virObjectUnref(net);
5679
    return rv;
M
Michal Privoznik 已提交
5680 5681 5682
}


5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743
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);

5744 5745 5746 5747 5748
            if (virTypedParamsSerialize(retStats[i]->params,
                                        retStats[i]->nparams,
                                        (virTypedParameterRemotePtr *) &dst->params.params_val,
                                        &dst->params.params_len,
                                        VIR_TYPED_PARAM_STRING_OKAY) < 0)
5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762
                goto cleanup;
        }
    } else {
        ret->retStats.retStats_len = 0;
        ret->retStats.retStats_val = NULL;
    }

    rv = 0;

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

    virDomainStatsRecordListFree(retStats);
5763
    virObjectListFree(doms);
5764 5765 5766 5767 5768

    return rv;
}


M
Michal Privoznik 已提交
5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805
static int
remoteDispatchNodeAllocPages(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client,
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                             virNetMessageErrorPtr rerr,
                             remote_node_alloc_pages_args *args,
                             remote_node_alloc_pages_ret *ret)
{
    int rv = -1;
    int len;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    if ((len = virNodeAllocPages(priv->conn,
                                 args->pageSizes.pageSizes_len,
                                 args->pageSizes.pageSizes_val,
                                 (unsigned long long *) args->pageCounts.pageCounts_val,
                                 args->startCell,
                                 args->cellCount,
                                 args->flags)) < 0)
        goto cleanup;

    ret->ret = len;
    rv = 0;

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


5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908
static int
remoteDispatchDomainGetFSInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client,
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                              virNetMessageErrorPtr rerr,
                              remote_domain_get_fsinfo_args *args,
                              remote_domain_get_fsinfo_ret *ret)
{
    int rv = -1;
    size_t i, j;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainFSInfoPtr *info = NULL;
    virDomainPtr dom = NULL;
    remote_domain_fsinfo *dst;
    int ninfo = 0;
    size_t ndisk;

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

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

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

    if (ninfo > REMOTE_DOMAIN_FSINFO_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many mountpoints in fsinfo: %d for limit %d"),
                       ninfo, REMOTE_DOMAIN_FSINFO_MAX);
        goto cleanup;
    }

    if (ninfo) {
        if (VIR_ALLOC_N(ret->info.info_val, ninfo) < 0)
            goto cleanup;

        ret->info.info_len = ninfo;

        for (i = 0; i < ninfo; i++) {
            dst = &ret->info.info_val[i];
            if (VIR_STRDUP(dst->mountpoint, info[i]->mountpoint) < 0)
                goto cleanup;

            if (VIR_STRDUP(dst->name, info[i]->name) < 0)
                goto cleanup;

            if (VIR_STRDUP(dst->fstype, info[i]->fstype) < 0)
                goto cleanup;

            ndisk = info[i]->ndevAlias;
            if (ndisk > REMOTE_DOMAIN_FSINFO_DISKS_MAX) {
                virReportError(VIR_ERR_RPC,
                               _("Too many disks in fsinfo: %zd for limit %d"),
                               ndisk, REMOTE_DOMAIN_FSINFO_DISKS_MAX);
                goto cleanup;
            }

            if (ndisk > 0) {
                if (VIR_ALLOC_N(dst->dev_aliases.dev_aliases_val, ndisk) < 0)
                    goto cleanup;

                for (j = 0; j < ndisk; j++) {
                    if (VIR_STRDUP(dst->dev_aliases.dev_aliases_val[j],
                                   info[i]->devAlias[j]) < 0)
                        goto cleanup;
                }

                dst->dev_aliases.dev_aliases_len = ndisk;
            } else {
                dst->dev_aliases.dev_aliases_val = NULL;
                dst->dev_aliases.dev_aliases_len = 0;
            }
        }

    } else {
        ret->info.info_len = 0;
        ret->info.info_val = NULL;
    }

    ret->ret = ninfo;

    rv = 0;

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

        if (ret->info.info_val && ninfo > 0) {
            for (i = 0; i < ninfo; i++) {
                dst = &ret->info.info_val[i];
                VIR_FREE(dst->mountpoint);
                if (dst->dev_aliases.dev_aliases_val) {
                    for (j = 0; j < dst->dev_aliases.dev_aliases_len; j++)
                        VIR_FREE(dst->dev_aliases.dev_aliases_val[j]);
                    VIR_FREE(dst->dev_aliases.dev_aliases_val);
                }
            }
            VIR_FREE(ret->info.info_val);
        }
    }
5909
    virObjectUnref(dom);
5910 5911 5912 5913 5914 5915 5916 5917 5918
    if (ninfo >= 0)
        for (i = 0; i < ninfo; i++)
            virDomainFSInfoFree(info[i]);
    VIR_FREE(info);

    return rv;
}


5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944
static int
remoteSerializeDomainInterface(virDomainInterfacePtr *ifaces,
                               unsigned int ifaces_count,
                               remote_domain_interface_addresses_ret *ret)
{
    size_t i, j;

    if (ifaces_count > REMOTE_DOMAIN_INTERFACE_MAX) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Number of interfaces, %d exceeds the max limit: %d"),
                       ifaces_count, REMOTE_DOMAIN_INTERFACE_MAX);
        return -1;
    }

    if (VIR_ALLOC_N(ret->ifaces.ifaces_val, ifaces_count) < 0)
        return -1;

    ret->ifaces.ifaces_len = ifaces_count;

    for (i = 0; i < ifaces_count; i++) {
        virDomainInterfacePtr iface = ifaces[i];
        remote_domain_interface *iface_ret = &(ret->ifaces.ifaces_val[i]);

        if ((VIR_STRDUP(iface_ret->name, iface->name)) < 0)
            goto cleanup;

5945 5946 5947
        if (iface->hwaddr &&
            (VIR_ALLOC(iface_ret->hwaddr) < 0 ||
             VIR_STRDUP(*iface_ret->hwaddr, iface->hwaddr) < 0))
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
            goto cleanup;

        if (iface->naddrs > REMOTE_DOMAIN_IP_ADDR_MAX) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Number of interfaces, %d exceeds the max limit: %d"),
                           iface->naddrs, REMOTE_DOMAIN_IP_ADDR_MAX);
            goto cleanup;
        }

        if (VIR_ALLOC_N(iface_ret->addrs.addrs_val,
                        iface->naddrs) < 0)
            goto cleanup;

        iface_ret->addrs.addrs_len = iface->naddrs;

        for (j = 0; j < iface->naddrs; j++) {
            virDomainIPAddressPtr ip_addr = &(iface->addrs[j]);
            remote_domain_ip_addr *ip_addr_ret =
                &(iface_ret->addrs.addrs_val[j]);

            if (VIR_STRDUP(ip_addr_ret->addr, ip_addr->addr) < 0)
                goto cleanup;

            ip_addr_ret->prefix = ip_addr->prefix;
            ip_addr_ret->type = ip_addr->type;
        }
    }

    return 0;

 cleanup:
    if (ret->ifaces.ifaces_val) {
        for (i = 0; i < ifaces_count; i++) {
            remote_domain_interface *iface_ret = &(ret->ifaces.ifaces_val[i]);
            VIR_FREE(iface_ret->name);
5983 5984 5985 5986
            if (iface_ret->hwaddr) {
                VIR_FREE(*iface_ret->hwaddr);
                VIR_FREE(iface_ret->hwaddr);
            }
5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047
            for (j = 0; j < iface_ret->addrs.addrs_len; j++) {
                remote_domain_ip_addr *ip_addr =
                    &(iface_ret->addrs.addrs_val[j]);
                VIR_FREE(ip_addr->addr);
            }
        }
        VIR_FREE(ret->ifaces.ifaces_val);
    }

    return -1;
}


static int
remoteDispatchDomainInterfaceAddresses(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_interface_addresses_args *args,
                                       remote_domain_interface_addresses_ret *ret)
{
    size_t i;
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainInterfacePtr *ifaces = NULL;
    int ifaces_count = 0;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if ((ifaces_count = virDomainInterfaceAddresses(dom, &ifaces, args->source, args->flags)) < 0)
        goto cleanup;

    if (remoteSerializeDomainInterface(ifaces, ifaces_count, ret) < 0)
        goto cleanup;

    rv = 0;

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

    virObjectUnref(dom);

    if (ifaces && ifaces_count > 0) {
        for (i = 0; i < ifaces_count; i++)
            virDomainInterfaceFree(ifaces[i]);
    }
    VIR_FREE(ifaces);

    return rv;
}


6048 6049 6050 6051 6052 6053 6054 6055 6056
/*----- 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
6057
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
6058 6059
{
    virDomainPtr dom;
6060
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
6061 6062 6063 6064 6065 6066 6067 6068
    /* 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
6069
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
6070
{
6071
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
6072 6073
}

D
Daniel Veillard 已提交
6074
static virInterfacePtr
6075
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
6076
{
6077
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
6078 6079
}

6080
static virStoragePoolPtr
6081
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
6082
{
6083 6084
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
6085 6086 6087
}

static virStorageVolPtr
6088
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
6089 6090
{
    virStorageVolPtr ret;
6091 6092
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
6093 6094 6095
    return ret;
}

6096
static virSecretPtr
6097
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
6098
{
6099
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
6100 6101
}

6102
static virNWFilterPtr
6103
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
6104
{
6105
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
6106 6107
}

C
Chris Lalancette 已提交
6108
static virDomainSnapshotPtr
6109
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
6110
{
6111
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
6112 6113
}

6114 6115
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
6116
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
6117 6118
{
    dom_dst->id = dom_src->id;
6119
    ignore_value(VIR_STRDUP_QUIET(dom_dst->name, dom_src->name));
6120
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
6121 6122 6123
}

static void
6124
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
6125
{
6126
    ignore_value(VIR_STRDUP_QUIET(net_dst->name, net_src->name));
6127
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
6128 6129
}

D
Daniel Veillard 已提交
6130
static void
6131 6132
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
6133
{
6134 6135
    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 已提交
6136 6137
}

6138
static void
6139
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
6140
{
6141
    ignore_value(VIR_STRDUP_QUIET(pool_dst->name, pool_src->name));
6142
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
6143 6144 6145
}

static void
6146
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
6147
{
6148 6149 6150
    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));
6151
}
6152 6153

static void
6154
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
6155
{
6156
    ignore_value(VIR_STRDUP_QUIET(dev_dst->name, dev_src->name));
6157
}
6158 6159

static void
6160
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
6161
{
6162
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
6163
    secret_dst->usageType = secret_src->usageType;
6164
    ignore_value(VIR_STRDUP_QUIET(secret_dst->usageID, secret_src->usageID));
6165
}
6166 6167

static void
6168
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
6169
{
6170
    ignore_value(VIR_STRDUP_QUIET(nwfilter_dst->name, nwfilter_src->name));
6171
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
6172
}
C
Chris Lalancette 已提交
6173 6174

static void
6175
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
6176
{
6177
    ignore_value(VIR_STRDUP_QUIET(snapshot_dst->name, snapshot_src->name));
6178
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
6179
}
6180 6181 6182 6183 6184 6185 6186 6187

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;
6188
    size_t i = 0;
6189

6190
    if (VIR_ALLOC_N(val, nerrors) < 0)
6191
        goto error;
6192 6193

    for (i = 0; i < nerrors; i++) {
6194 6195
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
6196 6197 6198 6199 6200 6201 6202 6203
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

6204
 error:
6205
    if (val) {
6206
        size_t j;
6207 6208 6209 6210 6211 6212
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}