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

#include <config.h>

25
#include "virerror.h"
26

27
#if WITH_POLKIT0
28 29
# include <polkit/polkit.h>
# include <polkit-dbus/polkit-dbus.h>
30 31
#endif

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

#define VIR_FROM_THIS VIR_FROM_RPC
60

61 62
VIR_LOG_INIT("daemon.remote");

63
#if SIZEOF_LONG < 8
E
Eric Blake 已提交
64 65 66
# define HYPER_TO_TYPE(_type, _to, _from)                               \
    do {                                                                \
        if ((_from) != (_type)(_from)) {                                \
67 68 69
            virReportError(VIR_ERR_OVERFLOW,                            \
                           _("conversion from hyper to %s overflowed"), \
                           #_type);                                     \
E
Eric Blake 已提交
70 71 72
            goto cleanup;                                               \
        }                                                               \
        (_to) = (_from);                                                \
73 74 75 76 77 78 79 80 81
    } 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

82 83 84 85
struct daemonClientEventCallback {
    virNetServerClientPtr client;
    int eventID;
    int callbackID;
86
    bool legacy;
87 88
};

89 90 91 92 93 94 95
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);
96
static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot);
97 98 99 100 101 102 103 104 105
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);
106

107 108 109 110 111
static virTypedParameterPtr
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
                                 int limit,
                                 int *nparams);
112

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

119 120
#include "remote_dispatch.h"
#include "qemu_dispatch.h"
121
#include "lxc_dispatch.h"
C
Chris Lalancette 已提交
122 123


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

132 133 134 135 136 137
static void
remoteEventCallbackFree(void *opaque)
{
    VIR_FREE(opaque);
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

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

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

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

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

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


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

213
 cleanup:
214 215 216 217 218 219
    ignore_value(virIdentitySetCurrent(NULL));
    virObjectUnref(identity);
    return ret;
}


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

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

234 235
    VIR_DEBUG("Relaying domain lifecycle event %d %d, callback %d legacy %d",
              event, detail, callback->callbackID, callback->legacy);
236

237
    /* build return data */
238
    memset(&data, 0, sizeof(data));
239
    make_nonnull_domain(&data.dom, dom);
240 241
    data.event = event;
    data.detail = detail;
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256
    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);
    }
257

258 259
    return 0;
}
260

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

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

273 274
    VIR_DEBUG("Relaying domain reboot event %s %d, callback %d legacy %d",
              dom->name, dom->id, callback->callbackID, callback->legacy);
275 276

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

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

    return 0;
}

296

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

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

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

    /* build return data */
315
    memset(&data, 0, sizeof(data));
316
    make_nonnull_domain(&data.dom, dom);
317 318
    data.offset = offset;

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

    return 0;
}


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

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

349 350
    VIR_DEBUG("Relaying domain watchdog event %s %d %d, callback %d",
              dom->name, dom->id, action, callback->callbackID);
351 352

    /* build return data */
353
    memset(&data, 0, sizeof(data));
354
    make_nonnull_domain(&data.dom, dom);
355 356
    data.action = action;

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

    return 0;
}


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

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

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

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

401 402 403 404 405 406 407 408 409 410 411 412
    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);
    }
413 414

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


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

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

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

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

    make_nonnull_domain(&data.dom, dom);
451

452 453 454 455 456 457 458 459 460 461 462 463
    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);
    }
464 465

    return 0;
466

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


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

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

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

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

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

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

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

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

    return 0;
541

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

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

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

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

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

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

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

603

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

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

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

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

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

    return 0;
}


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

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

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

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

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

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

    make_nonnull_domain(&data.dom, dom);

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

    return 0;

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


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

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

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

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

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

    make_nonnull_domain(&data.dom, dom);

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

    return 0;
}

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

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

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

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

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

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

    return 0;
}

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

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

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

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

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

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

    return 0;
}

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

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

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

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

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

    return 0;
}


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

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

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

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

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

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

    return 0;
}

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

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

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

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

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

    make_nonnull_domain(&data.dom, dom);

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

    return 0;
}

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 963 964 965 966 967 968 969 970 971
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;
}


972
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
973
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
974
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
975
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
976
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
977
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
978
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
979
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
980
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
981
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
982
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
983
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
984
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
985
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
986
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
987
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
988
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
989
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob2),
990 991 992 993
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

994
static int
995
remoteRelayNetworkEventLifecycle(virConnectPtr conn,
996 997 998 999
                                 virNetworkPtr net,
                                 int event,
                                 int detail,
                                 void *opaque)
1000
{
1001
    daemonClientEventCallbackPtr callback = opaque;
1002 1003
    remote_network_event_lifecycle_msg data;

1004 1005
    if (callback->callbackID < 0 ||
        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
1006 1007
        return -1;

1008 1009
    VIR_DEBUG("Relaying network lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);
1010 1011 1012 1013

    /* build return data */
    memset(&data, 0, sizeof(data));
    make_nonnull_network(&data.net, net);
1014
    data.callbackID = callback->callbackID;
1015 1016 1017
    data.event = event;
    data.detail = detail;

1018
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
                                  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);

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
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;

1072
 error:
1073 1074 1075 1076
    VIR_FREE(data.event);
    VIR_FREE(details_p);
}

1077 1078 1079 1080 1081 1082 1083
/*
 * 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
 */
1084
void remoteClientFreeFunc(void *data)
1085 1086 1087 1088 1089
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
1090
        virIdentityPtr sysident = virIdentityGetSystem();
1091
        size_t i;
1092

1093 1094
        virIdentitySetCurrent(sysident);

1095 1096 1097 1098 1099
        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;
1100
            }
1101 1102 1103 1104 1105
            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");
1106
        }
1107
        VIR_FREE(priv->domainEventCallbacks);
1108

1109 1110 1111 1112 1113
        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;
1114
            }
1115 1116 1117 1118 1119 1120
            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");
1121
        }
1122
        VIR_FREE(priv->networkEventCallbacks);
1123

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
        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);

1139
        virConnectClose(priv->conn);
1140 1141 1142

        virIdentitySetCurrent(NULL);
        virObjectUnref(sysident);
1143 1144 1145 1146 1147 1148
    }

    VIR_FREE(priv);
}


1149 1150 1151 1152 1153 1154 1155
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

1156

1157 1158
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
1159 1160 1161
{
    struct daemonClientPrivate *priv;

1162
    if (VIR_ALLOC(priv) < 0)
1163
        return NULL;
1164 1165 1166

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
1167
        virReportSystemError(errno, "%s", _("unable to init mutex"));
1168
        return NULL;
1169 1170
    }

1171
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
1172
    return priv;
1173 1174
}

1175 1176 1177
/*----- Functions. -----*/

static int
1178 1179 1180 1181 1182
remoteDispatchConnectOpen(virNetServerPtr server,
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
1183 1184
{
    const char *name;
1185
    unsigned int flags;
1186
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
1187
    int rv = -1;
1188

1189 1190 1191 1192
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
1193
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
1194 1195 1196
        goto cleanup;
    }

1197
    if (virNetServerKeepAliveRequired(server) && !priv->keepalive_supported) {
1198 1199
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("keepalive support is required to connect"));
1200 1201 1202
        goto cleanup;
    }

1203 1204 1205 1206 1207 1208
    name = args->name ? *args->name : NULL;

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

1212
    priv->conn =
1213
        flags & VIR_CONNECT_RO
1214 1215
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
1216

1217
    if (priv->conn == NULL)
1218 1219 1220
        goto cleanup;

    rv = 0;
1221

1222
 cleanup:
1223
    if (rv < 0)
1224 1225
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
1226
    return rv;
1227 1228 1229 1230
}


static int
1231 1232 1233 1234
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
1235
{
1236
    virNetServerClientDelayedClose(client);
1237
    return 0;
1238 1239
}

1240

1241
static int
1242 1243
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1244
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1245
                                     virNetMessageErrorPtr rerr,
1246 1247
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
1248
{
1249
    virDomainPtr dom = NULL;
1250 1251
    char *type;
    int nparams;
1252
    int rv = -1;
1253 1254
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1255

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

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

1264
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
1265
        goto cleanup;
1266 1267 1268

    ret->type = type;
    ret->nparams = nparams;
1269 1270
    rv = 0;

1271
 cleanup:
1272
    if (rv < 0)
1273
        virNetMessageSaveError(rerr);
1274 1275 1276
    if (dom)
        virDomainFree(dom);
    return rv;
1277 1278
}

1279 1280
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
1281 1282 1283
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
1284
                               remote_typed_param **ret_params_val,
1285 1286
                               u_int *ret_params_len,
                               unsigned int flags)
1287
{
1288 1289
    size_t i;
    size_t j;
1290 1291 1292 1293
    int rv = -1;
    remote_typed_param *val;

    *ret_params_len = nparams;
1294
    if (VIR_ALLOC_N(val, nparams) < 0)
1295 1296
        goto cleanup;

1297
    for (i = 0, j = 0; i < nparams; ++i) {
1298 1299 1300 1301 1302
        /* virDomainGetCPUStats can return a sparse array; also, we
         * can't pass back strings to older clients.  */
        if (!params[i].type ||
            (!(flags & VIR_TYPED_PARAM_STRING_OKAY) &&
             params[i].type == VIR_TYPED_PARAM_STRING)) {
1303 1304 1305 1306
            --*ret_params_len;
            continue;
        }

1307
        /* remoteDispatchClientRequest will free this: */
1308
        if (VIR_STRDUP(val[j].field, params[i].field) < 0)
1309
            goto cleanup;
1310
        val[j].value.type = params[i].type;
1311
        switch (params[i].type) {
1312
        case VIR_TYPED_PARAM_INT:
1313
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
1314 1315
            break;
        case VIR_TYPED_PARAM_UINT:
1316
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
1317 1318
            break;
        case VIR_TYPED_PARAM_LLONG:
1319
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
1320 1321
            break;
        case VIR_TYPED_PARAM_ULLONG:
1322
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
1323 1324
            break;
        case VIR_TYPED_PARAM_DOUBLE:
1325
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
1326 1327
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
1328 1329 1330
            val[j].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
        case VIR_TYPED_PARAM_STRING:
1331
            if (VIR_STRDUP(val[j].value.remote_typed_param_value_u.s, params[i].value.s) < 0)
1332
                goto cleanup;
1333 1334
            break;
        default:
1335 1336
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
1337 1338
            goto cleanup;
        }
1339
        j++;
1340 1341 1342 1343 1344 1345
    }

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

1346
 cleanup:
1347
    if (val) {
1348
        for (i = 0; i < nparams; i++) {
1349
            VIR_FREE(val[i].field);
1350
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
1351 1352
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
1353 1354 1355 1356 1357 1358 1359
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
1360 1361
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
1362 1363 1364
                                 int limit,
                                 int *nparams)
{
1365
    size_t i = 0;
1366 1367 1368 1369
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
1370
    if (limit && args_params_len > limit) {
1371
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1372 1373
        goto cleanup;
    }
1374
    if (VIR_ALLOC_N(params, args_params_len) < 0)
1375 1376 1377 1378 1379 1380 1381 1382
        goto cleanup;

    *nparams = args_params_len;

    /* Deserialise the result. */
    for (i = 0; i < args_params_len; ++i) {
        if (virStrcpyStatic(params[i].field,
                            args_params_val[i].field) == NULL) {
1383 1384 1385
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Parameter %s too big for destination"),
                           args_params_val[i].field);
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
            goto cleanup;
        }
        params[i].type = args_params_val[i].value.type;
        switch (params[i].type) {
        case VIR_TYPED_PARAM_INT:
            params[i].value.i =
                args_params_val[i].value.remote_typed_param_value_u.i;
            break;
        case VIR_TYPED_PARAM_UINT:
            params[i].value.ui =
                args_params_val[i].value.remote_typed_param_value_u.ui;
            break;
        case VIR_TYPED_PARAM_LLONG:
            params[i].value.l =
                args_params_val[i].value.remote_typed_param_value_u.l;
            break;
        case VIR_TYPED_PARAM_ULLONG:
            params[i].value.ul =
                args_params_val[i].value.remote_typed_param_value_u.ul;
            break;
        case VIR_TYPED_PARAM_DOUBLE:
            params[i].value.d =
                args_params_val[i].value.remote_typed_param_value_u.d;
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
            params[i].value.b =
                args_params_val[i].value.remote_typed_param_value_u.b;
            break;
1414
        case VIR_TYPED_PARAM_STRING:
1415 1416
            if (VIR_STRDUP(params[i].value.s,
                           args_params_val[i].value.remote_typed_param_value_u.s) < 0)
1417 1418
                goto cleanup;
            break;
1419
        default:
1420 1421
            virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                           params[i].type);
1422 1423 1424 1425 1426 1427
            goto cleanup;
        }
    }

    rv = 0;

1428
 cleanup:
1429
    if (rv < 0) {
1430 1431
        virTypedParamsFree(params, i);
        params = NULL;
1432
    }
1433 1434 1435
    return params;
}

1436
static int
1437 1438
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
1439
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
1440
                                           virNetMessageErrorPtr rerr,
1441 1442
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
1443
{
1444
    virDomainPtr dom = NULL;
1445
    virTypedParameterPtr params = NULL;
1446
    int nparams = 0;
1447
    int rv = -1;
1448 1449
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1450

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

1456
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1457
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1458
        goto cleanup;
1459
    }
1460
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1461
        goto cleanup;
1462
    nparams = args->nparams;
1463

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

1467
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1468
        goto cleanup;
1469

1470
    if (remoteSerializeTypedParameters(params, nparams,
1471
                                       &ret->params.params_val,
1472 1473
                                       &ret->params.params_len,
                                       0) < 0)
1474 1475 1476 1477
        goto cleanup;

    rv = 0;

1478
 cleanup:
1479
    if (rv < 0)
1480
        virNetMessageSaveError(rerr);
1481
    virTypedParamsFree(params, nparams);
1482 1483 1484 1485 1486
    if (dom)
        virDomainFree(dom);
    return rv;
}

1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
static int
remoteDispatchConnectListAllDomains(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_connect_list_all_domains_args *args,
                                    remote_connect_list_all_domains_ret *ret)
{
    virDomainPtr *doms = NULL;
    int ndomains = 0;
1497
    size_t i;
1498 1499 1500 1501
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1502
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1503 1504 1505 1506 1507 1508 1509 1510
        goto cleanup;
    }

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

1511 1512 1513 1514 1515 1516 1517
    if (ndomains > REMOTE_DOMAIN_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domains '%d' for limit '%d'"),
                       ndomains, REMOTE_DOMAIN_LIST_MAX);
        goto cleanup;
    }

1518
    if (doms && ndomains) {
1519
        if (VIR_ALLOC_N(ret->domains.domains_val, ndomains) < 0)
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
            goto cleanup;

        ret->domains.domains_len = ndomains;

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

    ret->ret = ndomains;

    rv = 0;

1535
 cleanup:
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (doms) {
        for (i = 0; i < ndomains; i++)
            virDomainFree(doms[i]);
        VIR_FREE(doms);
    }
    return rv;
}

1546
static int
1547 1548
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1549
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1550
                                                virNetMessageErrorPtr rerr,
1551 1552 1553 1554 1555
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1556
    int nparams = 0;
1557
    int rv = -1;
1558 1559
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1560

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

1566
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1567
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1568 1569
        goto cleanup;
    }
1570
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1571
        goto cleanup;
1572
    nparams = args->nparams;
1573

1574
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1575 1576 1577 1578 1579 1580 1581
        goto cleanup;

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

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

1587
    rv = 0;
1588

1589
 cleanup:
1590
    if (rv < 0)
1591
        virNetMessageSaveError(rerr);
1592
    virTypedParamsFree(params, nparams);
1593 1594 1595
    if (dom)
        virDomainFree(dom);
    return rv;
1596 1597
}

1598
static int
1599 1600
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1601
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1602
                                virNetMessageErrorPtr rerr,
1603 1604
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1605
{
1606
    virDomainPtr dom = NULL;
1607
    struct _virDomainMemoryStat *stats = NULL;
1608 1609
    int nr_stats;
    size_t i;
1610
    int rv = -1;
1611 1612
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1613

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

1619
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1620 1621
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1622
        goto cleanup;
1623 1624
    }

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

    /* Allocate stats array for making dispatch call */
1629
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
1630
        goto cleanup;
1631

1632
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1633
    if (nr_stats < 0)
1634
        goto cleanup;
1635 1636

    /* Allocate return buffer */
1637
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
1638
        goto cleanup;
1639 1640 1641 1642 1643 1644 1645

    /* 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;
1646 1647
    rv = 0;

1648
 cleanup:
1649
    if (rv < 0)
1650
        virNetMessageSaveError(rerr);
1651 1652
    if (dom)
        virDomainFree(dom);
1653
    VIR_FREE(stats);
1654
    return rv;
1655 1656
}

1657
static int
1658 1659
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1660
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1661
                              virNetMessageErrorPtr rerr,
1662 1663
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1664
{
1665
    virDomainPtr dom = NULL;
1666 1667 1668 1669
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1670
    int rv = -1;
1671 1672
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1673

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

1679
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1680
        goto cleanup;
1681 1682 1683 1684 1685 1686
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1687 1688
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1689
        goto cleanup;
1690 1691 1692
    }

    ret->buffer.buffer_len = size;
1693
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1694
        goto cleanup;
1695

1696
    if (virDomainBlockPeek(dom, path, offset, size,
1697
                           ret->buffer.buffer_val, flags) < 0)
1698
        goto cleanup;
1699

1700 1701
    rv = 0;

1702
 cleanup:
1703
    if (rv < 0) {
1704
        virNetMessageSaveError(rerr);
1705 1706 1707 1708 1709
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
1710 1711
}

1712 1713 1714
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1715
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1716 1717 1718 1719 1720 1721 1722
                                    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;
1723
    int nparams = 0;
1724 1725 1726 1727 1728 1729
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1730
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1731 1732 1733 1734 1735 1736 1737
        goto cleanup;
    }

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

1738
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1739
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1740 1741
        goto cleanup;
    }
1742
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1743
        goto cleanup;
1744
    nparams = args->nparams;
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759

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

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

    /* Serialise the block stats. */
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
1760 1761
                                       &ret->params.params_len,
                                       args->flags) < 0)
1762 1763
        goto cleanup;

1764
 success:
1765 1766
    rv = 0;

1767
 cleanup:
1768
    if (rv < 0)
1769
        virNetMessageSaveError(rerr);
1770
    virTypedParamsFree(params, nparams);
A
ajia@redhat.com 已提交
1771 1772
    if (dom)
        virDomainFree(dom);
1773 1774 1775
    return rv;
}

R
Richard W.M. Jones 已提交
1776
static int
1777 1778
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1779
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1780
                               virNetMessageErrorPtr rerr,
1781 1782
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1783
{
1784
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1785 1786 1787
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1788
    int rv = -1;
1789 1790
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1791

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

1797
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1798
        goto cleanup;
R
Richard W.M. Jones 已提交
1799 1800 1801 1802 1803
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1804 1805
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1806
        goto cleanup;
R
Richard W.M. Jones 已提交
1807 1808 1809
    }

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

1813
    if (virDomainMemoryPeek(dom, offset, size,
1814
                            ret->buffer.buffer_val, flags) < 0)
1815
        goto cleanup;
R
Richard W.M. Jones 已提交
1816

1817 1818
    rv = 0;

1819
 cleanup:
1820
    if (rv < 0) {
1821
        virNetMessageSaveError(rerr);
1822 1823 1824 1825 1826
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
1827 1828
}

1829
static int
1830 1831
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1832
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1833
                                     virNetMessageErrorPtr rerr,
1834 1835
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1836
{
1837 1838
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1839
    int rv = -1;
1840 1841
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1842

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

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

1851
    if (VIR_ALLOC(seclabel) < 0)
1852 1853 1854 1855 1856 1857
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
1858
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
1859
        goto cleanup;
1860 1861
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1862

1863 1864
    rv = 0;

1865
 cleanup:
1866
    if (rv < 0)
1867
        virNetMessageSaveError(rerr);
1868 1869 1870
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
1871
    return rv;
1872 1873
}

M
Marcelo Cerri 已提交
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
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;
1884 1885
    int len, rv = -1;
    size_t i;
M
Marcelo Cerri 已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
    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;
    }

1904
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
1905 1906 1907 1908 1909
        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];
1910
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0)
M
Marcelo Cerri 已提交
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
            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;

1922
 done:
M
Marcelo Cerri 已提交
1923 1924
    rv = 0;

1925
 cleanup:
M
Marcelo Cerri 已提交
1926 1927 1928 1929 1930 1931 1932 1933
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabels);
    return rv;
}

1934
static int
1935 1936
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1937
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1938
                                   virNetMessageErrorPtr rerr,
1939
                                   remote_node_get_security_model_ret *ret)
1940
{
1941
    virSecurityModel secmodel;
1942
    int rv = -1;
1943 1944
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1945

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

1951
    memset(&secmodel, 0, sizeof(secmodel));
1952
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
1953 1954 1955
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
1956
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
1957 1958 1959 1960
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
1961
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
1962
        goto cleanup;
1963
    strcpy(ret->doi.doi_val, secmodel.doi);
1964

1965 1966
    rv = 0;

1967
 cleanup:
1968
    if (rv < 0)
1969
        virNetMessageSaveError(rerr);
1970
    return rv;
1971 1972
}

1973
static int
1974 1975
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1976
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1977
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
1978 1979
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
1980 1981 1982 1983 1984
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
1985 1986
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1987

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

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

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
1997
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
1998 1999 2000 2001 2002
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
2003
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2004 2005 2006 2007 2008 2009
        goto cleanup;
    }

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

E
Eric Blake 已提交
2012
    if ((num = virDomainGetVcpuPinInfo(dom,
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
                                       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;
2026 2027 2028 2029
    cpumaps = NULL;

    rv = 0;

2030
 cleanup:
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 2061 2062 2063 2064 2065 2066
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;
}

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

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

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

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

    rv = 0;

2067
 cleanup:
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


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

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

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

    /* Allocate buffers to take the results */
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maplen) < 0)
2102
        goto cleanup;
2103 2104 2105 2106 2107 2108 2109 2110 2111 2112

    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;
2113 2114 2115 2116
    cpumaps = NULL;

    rv = 0;

2117
 cleanup:
2118
    if (rv < 0)
2119
        virNetMessageSaveError(rerr);
2120 2121 2122 2123 2124 2125
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;
}

2126
static int
2127 2128
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
2129
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
2130
                             virNetMessageErrorPtr rerr,
2131 2132
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
2133
{
2134
    virDomainPtr dom = NULL;
2135 2136
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
2137 2138
    int info_len;
    size_t i;
2139
    int rv = -1;
2140 2141
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2142

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

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

2151
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
2152
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
2153
        goto cleanup;
2154
    }
2155

2156 2157
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
2158
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
2159 2160 2161 2162 2163
        goto cleanup;
    }

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

    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)
2177
        goto cleanup;
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194

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

2196
 cleanup:
2197
    if (rv < 0) {
2198
        virNetMessageSaveError(rerr);
2199 2200 2201 2202
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
2203 2204 2205
    if (dom)
        virDomainFree(dom);
    return rv;
2206 2207 2208
}

static int
2209 2210
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2211
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2212
                                   virNetMessageErrorPtr rerr,
2213 2214
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
2215
{
2216 2217 2218 2219 2220
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2221
    int rv = -1;
2222 2223
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2224

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

2230 2231 2232 2233
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
2234
    if (VIR_ALLOC(uri_out) < 0)
2235
        goto cleanup;
2236

2237
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
2238 2239
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
2240
        goto cleanup;
2241

2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
    /* 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;
    }
2253

2254
    rv = 0;
2255

2256
 cleanup:
2257
    if (rv < 0)
2258
        virNetMessageSaveError(rerr);
2259
    VIR_FREE(uri_out);
2260
    return rv;
2261 2262
}

2263
static int
2264 2265
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2266
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2267
                                    virNetMessageErrorPtr rerr,
2268 2269
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
2270
{
2271 2272 2273 2274 2275
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2276
    int rv = -1;
2277 2278
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2279

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

2285 2286
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
2287

2288
    /* Wacky world of XDR ... */
2289
    if (VIR_ALLOC(uri_out) < 0)
2290
        goto cleanup;
2291

2292
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
2293 2294 2295
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
2296
        goto cleanup;
2297

2298 2299 2300 2301 2302 2303
    /* 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;
2304

2305 2306
    rv = 0;

2307
 cleanup:
2308
    if (rv < 0)
2309
        virNetMessageSaveError(rerr);
2310
    return rv;
2311 2312
}

C
Chris Lalancette 已提交
2313
static int
2314 2315
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
2316
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
2317 2318 2319
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
2320
{
2321
    virDomainPtr dom = NULL;
2322
    virTypedParameterPtr params = NULL;
2323
    int nparams = 0;
2324
    unsigned int flags;
2325
    int rv = -1;
2326 2327
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2328

2329
    if (!priv->conn) {
2330
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2331
        goto cleanup;
2332
    }
C
Chris Lalancette 已提交
2333

2334
    flags = args->flags;
C
Chris Lalancette 已提交
2335

2336
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2337
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2338 2339
        goto cleanup;
    }
2340
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2341
        goto cleanup;
2342
    nparams = args->nparams;
C
Chris Lalancette 已提交
2343

2344
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2345
        goto cleanup;
C
Chris Lalancette 已提交
2346

2347
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
2348
        goto cleanup;
C
Chris Lalancette 已提交
2349

2350 2351 2352 2353 2354 2355
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
2356 2357
    }

2358
    if (remoteSerializeTypedParameters(params, nparams,
2359
                                       &ret->params.params_val,
2360 2361
                                       &ret->params.params_len,
                                       args->flags) < 0)
2362
        goto cleanup;
2363

2364
 success:
2365 2366
    rv = 0;

2367
 cleanup:
2368
    if (rv < 0)
2369
        virNetMessageSaveError(rerr);
2370
    virTypedParamsFree(params, nparams);
2371 2372 2373
    if (dom)
        virDomainFree(dom);
    return rv;
2374 2375
}

2376 2377 2378 2379 2380 2381 2382 2383 2384 2385
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;
2386
    int nparams = 0;
2387 2388 2389 2390 2391 2392
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2393
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2394 2395 2396 2397 2398
        goto cleanup;
    }

    flags = args->flags;

2399
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
2400
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2401 2402
        goto cleanup;
    }
2403
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2404
        goto cleanup;
2405
    nparams = args->nparams;
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426

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

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

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

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

2427
 success:
2428 2429
    rv = 0;

2430
 cleanup:
2431 2432
    if (rv < 0)
        virNetMessageSaveError(rerr);
2433
    virTypedParamsFree(params, nparams);
2434 2435 2436 2437 2438
    if (dom)
        virDomainFree(dom);
    return rv;
}

2439
static int
2440 2441
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
2442
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2443 2444 2445
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
2446
{
2447
    virDomainPtr dom = NULL;
2448
    virTypedParameterPtr params = NULL;
2449
    int nparams = 0;
2450
    unsigned int flags;
2451
    int rv = -1;
2452 2453
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2454

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

2460 2461
    flags = args->flags;

2462
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2463
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2464
        goto cleanup;
2465
    }
2466
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2467
        goto cleanup;
2468
    nparams = args->nparams;
2469

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

2473
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2474
        goto cleanup;
2475

2476 2477 2478 2479 2480 2481 2482 2483
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2484
    if (remoteSerializeTypedParameters(params, nparams,
2485
                                       &ret->params.params_val,
2486 2487
                                       &ret->params.params_len,
                                       args->flags) < 0)
2488
        goto cleanup;
2489

2490
 success:
2491
    rv = 0;
2492

2493
 cleanup:
2494
    if (rv < 0)
2495
        virNetMessageSaveError(rerr);
2496
    virTypedParamsFree(params, nparams);
2497 2498 2499
    if (dom)
        virDomainFree(dom);
    return rv;
2500 2501
}

2502
static int
2503 2504
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2505
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2506 2507 2508
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2509
{
2510
    virNodeCPUStatsPtr params = NULL;
2511
    size_t i;
2512
    int cpuNum = args->cpuNum;
2513
    int nparams = 0;
2514 2515
    unsigned int flags;
    int rv = -1;
2516 2517
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2518

2519
    if (!priv->conn) {
2520
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2521 2522 2523 2524 2525
        goto cleanup;
    }

    flags = args->flags;

2526
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
2527
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2528 2529
        goto cleanup;
    }
2530
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2531
        goto cleanup;
2532
    nparams = args->nparams;
2533

2534
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
        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)
2548
        goto cleanup;
2549 2550 2551

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2552 2553
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2554 2555 2556 2557

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

2558
 success:
2559 2560
    rv = 0;

2561
 cleanup:
2562
    if (rv < 0) {
2563
        virNetMessageSaveError(rerr);
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574
        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
2575 2576
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2577
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2578 2579 2580
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2581
{
2582
    virNodeMemoryStatsPtr params = NULL;
2583
    size_t i;
2584
    int cellNum = args->cellNum;
2585
    int nparams = 0;
2586 2587
    unsigned int flags;
    int rv = -1;
2588 2589
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2590

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

    flags = args->flags;

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

2606
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
        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)
2620
        goto cleanup;
2621 2622 2623

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2624 2625
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2626 2627 2628 2629

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

2630
 success:
2631 2632
    rv = 0;

2633
 cleanup:
2634
    if (rv < 0) {
2635
        virNetMessageSaveError(rerr);
2636 2637 2638 2639 2640 2641 2642 2643 2644 2645
        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;
}

2646 2647 2648
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2649
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660
                                    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) {
2661
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678
        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;

2679
 cleanup:
2680 2681 2682 2683 2684 2685 2686
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697
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;
2698
    int nparams = 0;
2699 2700 2701 2702
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

2707
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
2708
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2709 2710 2711
        goto cleanup;
    }

2712
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2713
        goto cleanup;
2714
    nparams = args->nparams;
2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737

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

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

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

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

2738
 success:
2739 2740
    rv = 0;

2741
 cleanup:
2742 2743
    if (rv < 0)
        virNetMessageSaveError(rerr);
2744
    virTypedParamsFree(params, nparams);
2745 2746 2747 2748
    if (dom)
        virDomainFree(dom);
    return rv;
}
2749

D
Daniel Veillard 已提交
2750 2751
/*-------------------------------------------------------------*/

2752
static int
2753
remoteDispatchAuthList(virNetServerPtr server,
2754
                       virNetServerClientPtr client,
2755
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2756
                       virNetMessageErrorPtr rerr,
2757
                       remote_auth_list_ret *ret)
2758
{
2759
    int rv = -1;
2760 2761
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2762
    gid_t callerGid;
2763
    pid_t callerPid;
2764
    unsigned long long timestamp;
2765 2766 2767 2768 2769 2770

    /* 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) {
2771
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2772
                                              &callerPid, &timestamp) < 0) {
2773 2774 2775 2776
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2777 2778
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
2779
                            (long long) callerPid, (int) callerUid) < 0)
J
Jim Fehlig 已提交
2780 2781
                goto cleanup;
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
2782
            virNetServerClientSetAuth(client, 0);
2783
            virNetServerTrackCompletedAuth(server);
2784
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
2785
            VIR_FREE(ident);
2786 2787
        }
    }
2788

2789
    ret->types.types_len = 1;
2790
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
2791
        goto cleanup;
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805

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

2807 2808
    rv = 0;

2809
 cleanup:
2810
    if (rv < 0)
2811
        virNetMessageSaveError(rerr);
2812
    return rv;
2813 2814 2815
}


2816
#ifdef WITH_SASL
2817 2818
/*
 * Initializes the SASL session in prepare for authentication
2819
 * and gives the client a list of allowed mechanisms to choose
2820 2821
 */
static int
2822 2823
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2824
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2825
                           virNetMessageErrorPtr rerr,
2826
                           remote_auth_sasl_init_ret *ret)
2827
{
2828 2829 2830
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2831

2832
    virMutexLock(&priv->lock);
2833

2834 2835 2836
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2837
        VIR_ERROR(_("client tried invalid SASL init request"));
2838
        goto authfail;
2839 2840
    }

2841 2842 2843 2844 2845
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2846
        goto authfail;
2847

2848
# if WITH_GNUTLS
2849
    /* Inform SASL that we've got an external SSF layer from TLS */
2850 2851 2852 2853
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2854
            goto authfail;
2855 2856 2857 2858 2859

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2860
            goto authfail;
2861
    }
2862
# endif
2863

2864
    if (virNetServerClientIsSecure(client))
2865
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2866 2867
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2868
        /* Plain TCP, better get an SSF layer */
2869 2870 2871 2872
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
2873

2874
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
2875
        goto authfail;
2876
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
2877

2878 2879
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
2880
    return 0;
2881

2882
 authfail:
2883
    virResetLastError();
2884 2885
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2886
    virNetMessageSaveError(rerr);
2887 2888 2889
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2890
    virObjectUnref(sasl);
2891
    virMutexUnlock(&priv->lock);
2892
    return -1;
2893 2894
}

2895
/*
2896 2897
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
2898
static int
2899 2900
remoteSASLFinish(virNetServerPtr server,
                 virNetServerClientPtr client)
2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915
{
    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;
        }
2916
    }
2917 2918

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
2919
        return -2;
2920

2921 2922
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
2923

2924
    virNetServerClientSetAuth(client, 0);
2925
    virNetServerTrackCompletedAuth(server);
2926
    virNetServerClientSetSASLSession(client, priv->sasl);
2927

2928
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
2929

2930 2931 2932
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2933

2934
    virObjectUnref(priv->sasl);
2935
    priv->sasl = NULL;
2936

2937
    return 0;
2938

2939
 error:
2940 2941
    return -1;
}
2942

2943 2944 2945 2946
/*
 * This starts the SASL authentication negotiation.
 */
static int
2947
remoteDispatchAuthSaslStart(virNetServerPtr server,
2948
                            virNetServerClientPtr client,
2949
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2950
                            virNetMessageErrorPtr rerr,
2951 2952
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2953 2954
{
    const char *serverout;
2955
    size_t serveroutlen;
2956
    int err;
2957 2958 2959
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2960
    const char *identity;
2961

2962
    virMutexLock(&priv->lock);
2963

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

2971 2972
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
2973 2974 2975 2976 2977 2978 2979 2980 2981
    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)
2982
        goto authfail;
2983

2984
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2985
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
2986
        goto authfail;
2987 2988 2989 2990
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2991 2992
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2993 2994 2995 2996 2997 2998 2999
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3000
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3001
    if (err == VIR_NET_SASL_CONTINUE) {
3002 3003
        ret->complete = 0;
    } else {
3004
        /* Check username whitelist ACL */
3005
        if ((err = remoteSASLFinish(server, client)) < 0) {
3006 3007 3008 3009 3010
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3011

3012 3013 3014
        ret->complete = 1;
    }

3015
    virMutexUnlock(&priv->lock);
3016
    return 0;
3017

3018
 authfail:
3019 3020 3021
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3022 3023
    goto error;

3024
 authdeny:
3025
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3026 3027 3028
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3029 3030
    goto error;

3031
 error:
3032
    virObjectUnref(priv->sasl);
3033 3034
    priv->sasl = NULL;
    virResetLastError();
3035 3036
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3037 3038 3039
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3040
    return -1;
3041 3042 3043 3044
}


static int
3045
remoteDispatchAuthSaslStep(virNetServerPtr server,
3046
                           virNetServerClientPtr client,
3047
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3048
                           virNetMessageErrorPtr rerr,
3049 3050
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
3051 3052
{
    const char *serverout;
3053
    size_t serveroutlen;
3054
    int err;
3055 3056 3057
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3058
    const char *identity;
3059

3060 3061 3062 3063 3064
    virMutexLock(&priv->lock);

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

3069
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
3070
              args->data.data_len, args->nil);
3071 3072 3073 3074 3075 3076 3077 3078
    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)
3079
        goto authfail;
3080 3081

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
3082
        VIR_ERROR(_("sasl step reply data too long %d"),
3083
                  (int)serveroutlen);
3084
        goto authfail;
3085 3086 3087 3088
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
3089 3090
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
3091 3092 3093 3094 3095 3096 3097
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

3098
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
3099
    if (err == VIR_NET_SASL_CONTINUE) {
3100 3101
        ret->complete = 0;
    } else {
3102
        /* Check username whitelist ACL */
3103
        if ((err = remoteSASLFinish(server, client)) < 0) {
3104 3105 3106 3107 3108
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
3109

3110 3111 3112
        ret->complete = 1;
    }

3113
    virMutexUnlock(&priv->lock);
3114
    return 0;
3115

3116
 authfail:
3117 3118 3119
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
3120 3121
    goto error;

3122
 authdeny:
3123
    identity = virNetSASLSessionGetIdentity(priv->sasl);
3124 3125 3126
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
3127 3128
    goto error;

3129
 error:
3130
    virObjectUnref(priv->sasl);
3131 3132
    priv->sasl = NULL;
    virResetLastError();
3133 3134
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3135 3136 3137
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3138 3139
    return -1;
}
3140 3141 3142 3143
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3144
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3145 3146 3147 3148
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
3149 3150
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3151 3152 3153 3154 3155 3156
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
3157
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
3158 3159 3160 3161 3162
                            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");
3163 3164
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3165 3166 3167 3168 3169 3170
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3171
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3172 3173 3174 3175 3176
                           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");
3177 3178
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3179 3180 3181 3182
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
3183 3184 3185



3186
#if WITH_POLKIT1
3187
static int
3188
remoteDispatchAuthPolkit(virNetServerPtr server,
3189
                         virNetServerClientPtr client,
3190
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3191
                         virNetMessageErrorPtr rerr,
3192
                         remote_auth_polkit_ret *ret)
3193
{
3194
    pid_t callerPid = -1;
3195
    gid_t callerGid = -1;
3196
    uid_t callerUid = -1;
3197
    unsigned long long timestamp;
3198 3199
    const char *action;
    int status = -1;
3200
    char *ident = NULL;
3201
    bool authdismissed = 0;
3202
    char *pkout = NULL;
3203 3204
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3205
    virCommandPtr cmd = NULL;
3206
# ifndef PKCHECK_SUPPORTS_UID
3207
    static bool polkitInsecureWarned;
3208
# endif
3209

3210 3211
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
3212 3213 3214
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

3215
    cmd = virCommandNewArgList(PKCHECK_PATH, "--action-id", action, NULL);
3216
    virCommandSetOutputBuffer(cmd, &pkout);
3217
    virCommandSetErrorBuffer(cmd, &pkout);
3218

3219 3220
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3221
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3222 3223 3224
        goto authfail;
    }

3225
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
3226
                                          &callerPid, &timestamp) < 0) {
3227 3228 3229
        goto authfail;
    }

3230 3231 3232 3233 3234 3235
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

3236 3237
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3238

3239
    virCommandAddArg(cmd, "--process");
3240

3241
# ifdef PKCHECK_SUPPORTS_UID
3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252
    virCommandAddArgFormat(cmd, "%lld,%llu,%lu",
                           (long long) callerPid,
                           timestamp,
                           (unsigned long) callerUid);
# else
    if (!polkitInsecureWarned) {
        VIR_WARN("No support for caller UID with pkcheck. "
                 "This deployment is known to be insecure.");
        polkitInsecureWarned = true;
    }
    virCommandAddArgFormat(cmd, "%lld,%llu", (long long) callerPid, timestamp);
3253
# endif
3254

3255
    virCommandAddArg(cmd, "--allow-user-interaction");
3256

3257
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
3258
                    (long long) callerPid, callerUid) < 0)
3259 3260
        goto authfail;

3261
    if (virCommandRun(cmd, &status) < 0)
3262
        goto authfail;
3263

3264
    authdismissed = (pkout && strstr(pkout, "dismissed=true"));
3265
    if (status != 0) {
3266 3267 3268
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d "
                    "with status %d"),
                  action, (long long) callerPid, callerUid, status);
3269
        goto authdeny;
3270
    }
3271 3272 3273
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3274 3275
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
3276 3277
    ret->complete = 1;

3278
    virNetServerClientSetAuth(client, 0);
3279
    virNetServerTrackCompletedAuth(server);
3280
    virMutexUnlock(&priv->lock);
3281
    virCommandFree(cmd);
E
Eric Blake 已提交
3282
    VIR_FREE(pkout);
3283
    VIR_FREE(ident);
3284

3285 3286
    return 0;

3287
 error:
3288 3289
    virCommandFree(cmd);
    VIR_FREE(ident);
3290
    virResetLastError();
3291

3292
    if (authdismissed) {
3293 3294
        virReportError(VIR_ERR_AUTH_CANCELLED, "%s",
                       _("authentication cancelled by user"));
3295 3296
    } else if (pkout && *pkout) {
        virReportError(VIR_ERR_AUTH_FAILED, _("polkit: %s"), pkout);
3297
    } else {
3298
        virReportError(VIR_ERR_AUTH_FAILED, "%s", _("authentication failed"));
3299
    }
3300 3301

    VIR_FREE(pkout);
3302 3303 3304 3305
    virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return -1;

3306
 authfail:
3307 3308 3309
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3310 3311
    goto error;

3312
 authdeny:
3313 3314
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
3315
          client, REMOTE_AUTH_POLKIT, ident);
3316
    goto error;
3317
}
3318
#elif WITH_POLKIT0
3319
static int
3320
remoteDispatchAuthPolkit(virNetServerPtr server,
3321
                         virNetServerClientPtr client,
3322
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3323
                         virNetMessageErrorPtr rerr,
3324
                         remote_auth_polkit_ret *ret)
3325 3326
{
    pid_t callerPid;
3327
    gid_t callerGid;
3328
    uid_t callerUid;
3329 3330 3331 3332 3333 3334
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
3335
    const char *action;
3336
    char *ident = NULL;
3337 3338
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3339
    DBusConnection *sysbus;
J
Jim Fehlig 已提交
3340
    unsigned long long timestamp;
3341

J
Jim Fehlig 已提交
3342
    virMutexLock(&priv->lock);
3343

J
Jim Fehlig 已提交
3344
    action = virNetServerClientGetReadonly(client) ?
3345 3346
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
3347

3348
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
J
Jim Fehlig 已提交
3349
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3350
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3351
        goto authfail;
3352 3353
    }

3354
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
J
Jim Fehlig 已提交
3355
                                          &callerPid, &timestamp) < 0) {
3356
        VIR_ERROR(_("cannot get peer socket identity"));
3357
        goto authfail;
3358 3359
    }

3360
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
3361
                    (long long) callerPid, callerUid) < 0)
3362 3363
        goto authfail;

3364 3365 3366
    if (!(sysbus = virDBusGetSystemBus()))
        goto authfail;

3367 3368
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3369
    dbus_error_init(&err);
3370
    if (!(pkcaller = polkit_caller_new_from_pid(sysbus,
3371
                                                callerPid, &err))) {
3372
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
3373
        dbus_error_free(&err);
3374
        goto authfail;
3375
    }
3376

3377
    if (!(pkaction = polkit_action_new())) {
3378
        char ebuf[1024];
3379
        VIR_ERROR(_("Failed to create polkit action %s"),
3380
                  virStrerror(errno, ebuf, sizeof(ebuf)));
3381
        polkit_caller_unref(pkcaller);
3382
        goto authfail;
3383 3384 3385 3386 3387
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
3388
        char ebuf[1024];
3389
        VIR_ERROR(_("Failed to create polkit context %s"),
3390
                  (pkerr ? polkit_error_get_error_message(pkerr)
3391
                   : virStrerror(errno, ebuf, sizeof(ebuf))));
3392 3393 3394 3395 3396
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
3397
        goto authfail;
3398
    }
3399

3400
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
3401 3402 3403 3404 3405 3406
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
3407 3408 3409
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
3410
        goto authfail;
3411
    }
3412
# else
3413 3414 3415
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
3416
# endif
3417 3418 3419 3420
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
3421 3422
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d, result: %s"),
                  action, (long long) callerPid, callerUid,
3423
                  polkit_result_to_string_representation(pkresult));
3424
        goto authdeny;
3425
    }
3426 3427 3428
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3429 3430
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d, result %s",
             action, (long long) callerPid, callerUid,
3431 3432
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
3433

3434
    virNetServerClientSetAuth(client, 0);
3435
    virNetServerTrackCompletedAuth(server);
J
Jim Fehlig 已提交
3436
    virMutexUnlock(&priv->lock);
3437
    VIR_FREE(ident);
3438
    return 0;
3439

3440
 error:
3441
    VIR_FREE(ident);
3442
    virResetLastError();
3443 3444
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3445
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3446
    virMutexUnlock(&priv->lock);
3447 3448
    return -1;

3449
 authfail:
3450 3451 3452
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3453
    goto error;
3454

3455
 authdeny:
3456 3457 3458
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3459
    goto error;
3460 3461
}

3462
#else /* !WITH_POLKIT0 & !HAVE_POLKIT1*/
3463 3464

static int
3465
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
3466
                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
3467
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3468
                         virNetMessageErrorPtr rerr,
3469
                         remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
3470
{
3471
    VIR_ERROR(_("client tried unsupported PolicyKit init request"));
3472 3473
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3474
    virNetMessageSaveError(rerr);
3475 3476
    return -1;
}
3477
#endif /* WITH_POLKIT1 */
3478 3479


3480 3481 3482
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3483

3484
static int
3485 3486
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3487
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3488
                                  virNetMessageErrorPtr rerr,
3489 3490
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3491
{
3492 3493
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3494
    int rv = -1;
3495 3496
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3497

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

3503
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3504 3505
        goto cleanup;

3506 3507 3508 3509 3510 3511 3512
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
3513
        if (VIR_ALLOC(parent_p) < 0)
3514
            goto cleanup;
3515
        if (VIR_STRDUP(*parent_p, parent) < 0) {
3516 3517 3518 3519 3520 3521
            VIR_FREE(parent_p);
            goto cleanup;
        }
        ret->parent = parent_p;
    }

3522 3523
    rv = 0;

3524
 cleanup:
3525
    if (rv < 0)
3526
        virNetMessageSaveError(rerr);
3527 3528
    if (dev)
        virNodeDeviceFree(dev);
3529
    return rv;
3530 3531
}

3532 3533 3534 3535 3536

/***************************
 * Register / deregister events
 ***************************/
static int
3537
remoteDispatchConnectDomainEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
3538
                                         virNetServerClientPtr client,
3539 3540 3541
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                         remote_connect_domain_event_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
3542
{
3543
    int callbackID;
3544
    int rv = -1;
3545 3546
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3547 3548
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3549

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

3555 3556
    virMutexLock(&priv->lock);

3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567
    /* 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;
3568
    callback->legacy = true;
3569 3570 3571 3572
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3573
        goto cleanup;
O
Osier Yang 已提交
3574

3575
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3576 3577 3578
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
3579 3580 3581 3582 3583
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3584
        goto cleanup;
3585
    }
O
Osier Yang 已提交
3586

3587
    ref->callbackID = callbackID;
3588

3589 3590
    rv = 0;

3591
 cleanup:
3592
    VIR_FREE(callback);
3593
    if (rv < 0)
3594 3595
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3596
    return rv;
O
Osier Yang 已提交
3597 3598
}

3599
static int
3600
remoteDispatchConnectDomainEventDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
3601
                                           virNetServerClientPtr client,
3602 3603 3604
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                           remote_connect_domain_event_deregister_ret *ret ATTRIBUTE_UNUSED)
3605
{
3606
    int callbackID = -1;
3607
    int rv = -1;
3608
    size_t i;
3609 3610
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3611

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

3617 3618
    virMutexLock(&priv->lock);

3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629
    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);
3630
        goto cleanup;
3631
    }
3632

3633
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3634
        goto cleanup;
3635

3636 3637
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3638

3639 3640
    rv = 0;

3641
 cleanup:
3642
    if (rv < 0)
3643 3644
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3645 3646 3647 3648
    return rv;
}

static void
3649
remoteDispatchObjectEventSend(virNetServerClientPtr client,
3650
                              virNetServerProgramPtr program,
3651 3652 3653 3654
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3655
    virNetMessagePtr msg;
3656

3657
    if (!(msg = virNetMessageNew(false)))
3658
        goto cleanup;
3659

3660 3661 3662 3663 3664 3665
    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;
3666

3667
    if (virNetMessageEncodeHeader(msg) < 0)
3668 3669
        goto cleanup;

3670 3671
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3672

3673 3674
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3675

3676
    xdr_free(proc, data);
3677 3678
    return;

3679
 cleanup:
3680
    virNetMessageFree(msg);
3681
    xdr_free(proc, data);
3682 3683
}

3684
static int
3685 3686
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3687
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3688
                             virNetMessageErrorPtr rerr,
3689 3690
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3691
{
3692 3693 3694
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3695
    int rv = -1;
3696 3697
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3698

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

3704
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3705
        goto cleanup;
3706

3707
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3708
        goto cleanup;
3709

3710 3711 3712
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3713 3714
    rv = 0;

3715
 cleanup:
3716
    if (rv < 0)
3717
        virNetMessageSaveError(rerr);
3718 3719
    if (secret)
        virSecretFree(secret);
3720
    return rv;
3721 3722
}

3723
static int
3724 3725
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3726
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3727
                             virNetMessageErrorPtr rerr,
3728 3729 3730 3731 3732
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3733 3734
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3735

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

3741
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3742 3743 3744 3745 3746 3747 3748
        goto cleanup;

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

    rv = 0;

3749
 cleanup:
3750
    if (rv < 0)
3751
        virNetMessageSaveError(rerr);
3752 3753 3754 3755 3756
    if (dom)
        virDomainFree(dom);
    return rv;
}

3757 3758 3759 3760 3761 3762

/* 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.  */
3763
static int
3764
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3765
                                            virNetServerClientPtr client,
3766 3767 3768
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            remote_connect_domain_event_register_any_args *args)
3769 3770
{
    int callbackID;
3771
    int rv = -1;
3772 3773
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3774 3775
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3776

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

3782 3783
    virMutexLock(&priv->lock);

3784 3785 3786 3787
    /* 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 ||
3788
        args->eventID < 0) {
3789 3790
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3791
        goto cleanup;
3792 3793
    }

3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804
    /* 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;
3805
    callback->legacy = true;
3806 3807 3808 3809
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3810
        goto cleanup;
3811

3812
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3813 3814 3815
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3816 3817 3818 3819 3820
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3821
        goto cleanup;
3822
    }
3823

3824
    ref->callbackID = callbackID;
3825

3826 3827
    rv = 0;

3828
 cleanup:
3829
    VIR_FREE(callback);
3830
    if (rv < 0)
3831 3832
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3833
    return rv;
3834 3835 3836
}


3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863
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;

3864
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST || args->eventID < 0) {
3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903
        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;

3904
 cleanup:
3905 3906 3907 3908 3909 3910 3911 3912 3913 3914
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    virMutexUnlock(&priv->lock);
    return rv;
}


3915
static int
3916
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3917
                                              virNetServerClientPtr client,
3918 3919 3920
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              remote_connect_domain_event_deregister_any_args *args)
3921 3922
{
    int callbackID = -1;
3923
    int rv = -1;
3924
    size_t i;
3925 3926
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3927

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

3933 3934
    virMutexLock(&priv->lock);

3935 3936 3937 3938
    /* 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 ||
3939
        args->eventID < 0) {
3940 3941
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3942
        goto cleanup;
3943 3944
    }

3945 3946 3947 3948 3949 3950
    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->eventID == args->eventID) {
            callbackID = priv->domainEventCallbacks[i]->callbackID;
            break;
        }
    }
3951
    if (callbackID < 0) {
3952 3953
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event %d not registered"), args->eventID);
3954
        goto cleanup;
3955 3956
    }

3957
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3958
        goto cleanup;
3959

3960 3961
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3962

3963 3964
    rv = 0;

3965
 cleanup:
3966
    if (rv < 0)
3967 3968
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3969
    return rv;
3970 3971
}

3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010

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;

4011
 cleanup:
4012 4013 4014 4015 4016 4017 4018
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}


C
Chris Lalancette 已提交
4019
static int
4020 4021 4022 4023 4024 4025
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 已提交
4026
{
4027
    virDomainPtr dom = NULL;
4028
    int rv = -1;
4029 4030
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
4031

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

4037
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4038
        goto cleanup;
C
Chris Lalancette 已提交
4039

4040
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
4041
                                    args->flags) < 0)
4042
        goto cleanup;
C
Chris Lalancette 已提交
4043

4044
    rv = 0;
C
Chris Lalancette 已提交
4045

4046
 cleanup:
4047
    if (rv < 0)
4048
        virNetMessageSaveError(rerr);
4049 4050
    if (dom)
        virDomainFree(dom);
4051
    return rv;
C
Chris Lalancette 已提交
4052 4053
}

4054

4055
static int
4056 4057
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
4058
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
4059
                                  virNetMessageErrorPtr rerr,
4060 4061 4062 4063 4064 4065
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
4066
    char *xmlin;
4067 4068 4069
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4070 4071
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4072

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

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

4081
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4082 4083
    dname = args->dname == NULL ? NULL : *args->dname;

4084
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097
                                       &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;

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


static int
4108 4109
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4110
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4111
                                    virNetMessageErrorPtr rerr,
4112 4113 4114 4115 4116 4117 4118 4119 4120
                                    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;
4121 4122
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4123

4124
    if (!priv->conn) {
4125
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4126 4127 4128 4129 4130 4131 4132
        goto cleanup;
    }

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

    /* Wacky world of XDR ... */
4133
    if (VIR_ALLOC(uri_out) < 0)
4134 4135
        goto cleanup;

4136
    if (virDomainMigratePrepare3(priv->conn,
4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153
                                 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;

4154
 cleanup:
4155
    if (rv < 0) {
4156
        virNetMessageSaveError(rerr);
4157 4158 4159 4160 4161
        VIR_FREE(uri_out);
    }
    return rv;
}

4162

4163
static int
4164 4165
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4166
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4167
                                    virNetMessageErrorPtr rerr,
4168 4169 4170 4171
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
4172
    char *xmlin;
4173
    char *dname;
4174 4175
    char *uri;
    char *dconnuri;
4176 4177 4178
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
4179 4180
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4181

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

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

4190
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
4191
    dname = args->dname == NULL ? NULL : *args->dname;
4192 4193
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
4194

4195
    if (virDomainMigratePerform3(dom, xmlin,
4196 4197 4198
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
4199
                                 dconnuri, uri,
4200 4201 4202 4203 4204 4205 4206 4207 4208 4209
                                 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;

4210
 cleanup:
4211
    if (rv < 0)
4212
        virNetMessageSaveError(rerr);
4213 4214 4215 4216 4217 4218 4219
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
4220 4221
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
4222
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
4223
                                   virNetMessageErrorPtr rerr,
4224 4225 4226 4227 4228 4229
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
4230 4231
    char *uri;
    char *dconnuri;
4232
    int rv = -1;
4233 4234
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4235

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

4241 4242 4243
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

4244
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
4245 4246 4247 4248 4249 4250
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
4251 4252
        goto cleanup;

4253
    make_nonnull_domain(&ret->dom, dom);
4254 4255 4256 4257 4258 4259 4260 4261

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

    rv = 0;

4262
 cleanup:
4263
    if (rv < 0) {
4264
        virNetMessageSaveError(rerr);
4265 4266 4267 4268 4269 4270 4271 4272 4273
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


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

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

4290
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4291 4292 4293 4294 4295 4296 4297 4298 4299 4300
        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;

4301
 cleanup:
4302
    if (rv < 0)
4303
        virNetMessageSaveError(rerr);
4304 4305 4306 4307 4308 4309
    if (dom)
        virDomainFree(dom);
    return rv;
}


4310 4311 4312 4313 4314 4315
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)
4316 4317 4318 4319 4320 4321
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

4322 4323 4324 4325 4326 4327 4328 4329 4330 4331
    /* 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;
    }

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

    switch (args->feature) {
    case VIR_DRV_FEATURE_FD_PASSING:
4339
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
4340 4341 4342 4343
        supported = 1;
        break;

    default:
4344
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
4345 4346 4347 4348
            goto cleanup;
        break;
    }

4349
 done:
4350 4351 4352
    ret->supported = supported;
    rv = 0;

4353
 cleanup:
4354 4355 4356 4357 4358 4359
    if (rv < 0)
        virNetMessageSaveError(rerr);
    return rv;
}


4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373
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) {
4374
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391
        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;

4392
 cleanup:
4393 4394 4395 4396 4397 4398 4399 4400
    VIR_FORCE_CLOSE(fd);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411
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;
4412
    int nparams = 0;
4413 4414 4415 4416 4417 4418
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4419
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4420 4421 4422 4423 4424
        goto cleanup;
    }

    flags = args->flags;

4425
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
4426
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4427 4428
        goto cleanup;
    }
4429
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4430
        goto cleanup;
4431
    nparams = args->nparams;
4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452

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

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

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

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

4453
 success:
4454 4455
    rv = 0;

4456
 cleanup:
4457 4458
    if (rv < 0)
        virNetMessageSaveError(rerr);
4459
    virTypedParamsFree(params, nparams);
4460 4461 4462 4463
    if (dom)
        virDomainFree(dom);
    return rv;
}
4464

4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480
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) {
4481
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4482 4483 4484 4485
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
4486
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4487 4488 4489
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
4490
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
4491 4492 4493 4494
        goto cleanup;
    }

    if (args->nparams > 0 &&
4495
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515
        goto cleanup;

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

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

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

4516
 success:
4517 4518
    rv = 0;
    ret->nparams = percpu_len;
4519
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
4520
        size_t i;
4521 4522 4523 4524 4525 4526

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

4528
 cleanup:
4529 4530
    if (rv < 0)
         virNetMessageSaveError(rerr);
4531
    virTypedParamsFree(params, args->ncpus * args->nparams);
4532 4533 4534 4535 4536
    if (dom)
        virDomainFree(dom);
    return rv;
}

4537 4538 4539 4540 4541 4542 4543
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)
4544 4545 4546 4547
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
4548
    int len = 0;
4549 4550 4551 4552
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
4553
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4554 4555 4556 4557 4558 4559 4560
        goto cleanup;
    }

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
4561 4562
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
4563 4564 4565 4566
        goto cleanup;
    }

    if (args->maxerrors &&
4567
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583
        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;

4584
 cleanup:
4585 4586 4587 4588 4589
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    if (errors) {
4590
        size_t i;
4591 4592 4593 4594 4595 4596 4597
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

4598 4599 4600 4601 4602 4603 4604 4605 4606 4607
static int
remoteDispatchDomainListAllSnapshots(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_domain_list_all_snapshots_args *args,
                                     remote_domain_list_all_snapshots_ret *ret)
{
    virDomainSnapshotPtr *snaps = NULL;
    int nsnaps = 0;
4608
    size_t i;
4609 4610 4611 4612 4613
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

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

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

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

4626 4627 4628 4629 4630 4631 4632
    if (nsnaps > REMOTE_DOMAIN_SNAPSHOT_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domain snapshots '%d' for limit '%d'"),
                       nsnaps, REMOTE_DOMAIN_SNAPSHOT_LIST_MAX);
        goto cleanup;
    }

4633
    if (snaps && nsnaps) {
4634
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649
            goto cleanup;

        ret->snapshots.snapshots_len = nsnaps;

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

    ret->ret = nsnaps;
    rv = 0;

4650
 cleanup:
4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    if (snaps) {
        for (i = 0; i < nsnaps; i++)
            virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }
    return rv;
}

static int
remoteDispatchDomainSnapshotListAllChildren(virNetServerPtr server ATTRIBUTE_UNUSED,
                                            virNetServerClientPtr client,
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr,
                                            remote_domain_snapshot_list_all_children_args *args,
                                            remote_domain_snapshot_list_all_children_ret *ret)
{
    virDomainSnapshotPtr *snaps = NULL;
    int nsnaps = 0;
4673
    size_t i;
4674 4675 4676 4677 4678 4679
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;
    virDomainSnapshotPtr snapshot = NULL;

    if (!priv->conn) {
4680
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694
        goto cleanup;
    }

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

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

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

4695 4696 4697 4698 4699 4700 4701
    if (nsnaps > REMOTE_DOMAIN_SNAPSHOT_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domain snapshots '%d' for limit '%d'"),
                       nsnaps, REMOTE_DOMAIN_SNAPSHOT_LIST_MAX);
        goto cleanup;
    }

4702
    if (snaps && nsnaps) {
4703
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718
            goto cleanup;

        ret->snapshots.snapshots_len = nsnaps;

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

    ret->ret = nsnaps;
    rv = 0;

4719
 cleanup:
4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (snapshot)
        virDomainSnapshotFree(snapshot);
    if (dom)
        virDomainFree(dom);
    if (snaps) {
        for (i = 0; i < nsnaps; i++)
            virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }
    return rv;
}

4734 4735 4736 4737 4738 4739 4740 4741 4742 4743
static int
remoteDispatchConnectListAllStoragePools(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_connect_list_all_storage_pools_args *args,
                                         remote_connect_list_all_storage_pools_ret *ret)
{
    virStoragePoolPtr *pools = NULL;
    int npools = 0;
4744
    size_t i;
4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

4758 4759 4760 4761 4762 4763 4764
    if (npools > REMOTE_STORAGE_POOL_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many storage pools '%d' for limit '%d'"),
                       npools, REMOTE_STORAGE_POOL_LIST_MAX);
        goto cleanup;
    }

4765
    if (pools && npools) {
4766
        if (VIR_ALLOC_N(ret->pools.pools_val, npools) < 0)
4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781
            goto cleanup;

        ret->pools.pools_len = npools;

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

    ret->ret = npools;

    rv = 0;

4782
 cleanup:
4783 4784 4785 4786 4787 4788 4789 4790 4791 4792
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (pools) {
        for (i = 0; i < npools; i++)
            virStoragePoolFree(pools[i]);
        VIR_FREE(pools);
    }
    return rv;
}

4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803
static int
remoteDispatchStoragePoolListAllVolumes(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_storage_pool_list_all_volumes_args *args,
                                        remote_storage_pool_list_all_volumes_ret *ret)
{
    virStorageVolPtr *vols = NULL;
    virStoragePoolPtr pool = NULL;
    int nvols = 0;
4804
    size_t i;
4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

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

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

4828
    if (vols && nvols) {
4829
        if (VIR_ALLOC_N(ret->vols.vols_val, nvols) < 0)
4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844
            goto cleanup;

        ret->vols.vols_len = nvols;

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

    ret->ret = nvols;

    rv = 0;

4845
 cleanup:
4846 4847 4848 4849 4850 4851 4852
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (vols) {
        for (i = 0; i < nvols; i++)
            virStorageVolFree(vols[i]);
        VIR_FREE(vols);
    }
4853 4854
    if (pool)
        virStoragePoolFree(pool);
4855 4856 4857
    return rv;
}

4858 4859 4860 4861 4862 4863 4864 4865 4866 4867
static int
remoteDispatchConnectListAllNetworks(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_connect_list_all_networks_args *args,
                                     remote_connect_list_all_networks_ret *ret)
{
    virNetworkPtr *nets = NULL;
    int nnets = 0;
4868
    size_t i;
4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

4882 4883 4884 4885 4886 4887 4888
    if (nnets > REMOTE_NETWORK_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many networks '%d' for limit '%d'"),
                       nnets, REMOTE_NETWORK_LIST_MAX);
        goto cleanup;
    }

4889
    if (nets && nnets) {
4890
        if (VIR_ALLOC_N(ret->nets.nets_val, nnets) < 0)
4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905
            goto cleanup;

        ret->nets.nets_len = nnets;

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

    ret->ret = nnets;

    rv = 0;

4906
 cleanup:
4907 4908 4909 4910 4911 4912 4913 4914 4915 4916
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (nets) {
        for (i = 0; i < nnets; i++)
            virNetworkFree(nets[i]);
        VIR_FREE(nets);
    }
    return rv;
}

4917 4918 4919 4920 4921 4922 4923 4924 4925 4926
static int
remoteDispatchConnectListAllInterfaces(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_connect_list_all_interfaces_args *args,
                                       remote_connect_list_all_interfaces_ret *ret)
{
    virInterfacePtr *ifaces = NULL;
    int nifaces = 0;
4927
    size_t i;
4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

4941 4942 4943 4944 4945 4946 4947
    if (nifaces > REMOTE_INTERFACE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many interfaces '%d' for limit '%d'"),
                       nifaces, REMOTE_INTERFACE_LIST_MAX);
        goto cleanup;
    }

4948
    if (ifaces && nifaces) {
4949
        if (VIR_ALLOC_N(ret->ifaces.ifaces_val, nifaces) < 0)
4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964
            goto cleanup;

        ret->ifaces.ifaces_len = nifaces;

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

    ret->ret = nifaces;

    rv = 0;

4965
 cleanup:
4966 4967 4968 4969 4970 4971 4972 4973 4974 4975
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (ifaces) {
        for (i = 0; i < nifaces; i++)
            virInterfaceFree(ifaces[i]);
        VIR_FREE(ifaces);
    }
    return rv;
}

4976 4977 4978 4979 4980 4981 4982 4983 4984 4985
static int
remoteDispatchConnectListAllNodeDevices(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_connect_list_all_node_devices_args *args,
                                        remote_connect_list_all_node_devices_ret *ret)
{
    virNodeDevicePtr *devices = NULL;
    int ndevices = 0;
4986
    size_t i;
4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

5000 5001 5002 5003 5004 5005 5006
    if (ndevices > REMOTE_NODE_DEVICE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many node devices '%d' for limit '%d'"),
                       ndevices, REMOTE_NODE_DEVICE_LIST_MAX);
        goto cleanup;
    }

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

        ret->devices.devices_len = ndevices;

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

    ret->ret = ndevices;

    rv = 0;

5024
 cleanup:
5025 5026 5027 5028 5029 5030 5031 5032 5033
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (devices) {
        for (i = 0; i < ndevices; i++)
            virNodeDeviceFree(devices[i]);
        VIR_FREE(devices);
    }
    return rv;
}
5034

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

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

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

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

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

        ret->filters.filters_len = nfilters;

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

    ret->ret = nfilters;

    rv = 0;

5083
 cleanup:
5084 5085 5086 5087 5088 5089 5090 5091 5092 5093
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (filters) {
        for (i = 0; i < nfilters; i++)
            virNWFilterFree(filters[i]);
        VIR_FREE(filters);
    }
    return rv;
}

5094 5095 5096 5097 5098 5099 5100 5101 5102 5103
static int
remoteDispatchConnectListAllSecrets(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_connect_list_all_secrets_args *args,
                                    remote_connect_list_all_secrets_ret *ret)
{
    virSecretPtr *secrets = NULL;
    int nsecrets = 0;
5104
    size_t i;
5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

5118 5119 5120 5121 5122 5123 5124
    if (nsecrets > REMOTE_SECRET_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many secrets '%d' for limit '%d'"),
                       nsecrets, REMOTE_SECRET_LIST_MAX);
        goto cleanup;
    }

5125
    if (secrets && nsecrets) {
5126
        if (VIR_ALLOC_N(ret->secrets.secrets_val, nsecrets) < 0)
5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141
            goto cleanup;

        ret->secrets.secrets_len = nsecrets;

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

    ret->ret = nsecrets;

    rv = 0;

5142
 cleanup:
5143 5144 5145 5146 5147 5148 5149 5150 5151 5152
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (secrets) {
        for (i = 0; i < nsecrets; i++)
            virSecretFree(secrets[i]);
        VIR_FREE(secrets);
    }
    return rv;
}

5153 5154 5155 5156 5157 5158 5159 5160 5161
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;
5162
    int nparams = 0;
5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174
    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;

5175
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
5176 5177 5178
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
5179
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
5180
        goto cleanup;
5181
    nparams = args->nparams;
5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199

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

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

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

5200
 success:
5201 5202
    rv = 0;

5203
 cleanup:
5204 5205
    if (rv < 0)
        virNetMessageSaveError(rerr);
5206
    virTypedParamsFree(params, nparams);
5207 5208 5209
    return rv;
}

5210 5211 5212 5213 5214 5215 5216 5217 5218
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;
5219
    unsigned int online = 0;
5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232
    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;

5233 5234
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
5235 5236 5237 5238
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
5239
    if (args->need_map) {
5240 5241 5242 5243 5244 5245 5246 5247 5248 5249
        ret->cpumap.cpumap_len = VIR_CPU_MAPLEN(cpunum);
        ret->cpumap.cpumap_val = (char *) cpumap;
        cpumap = NULL;
    }

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

    rv = 0;

5250
 cleanup:
5251 5252 5253 5254 5255 5256
    if (rv < 0)
        virNetMessageSaveError(rerr);
    VIR_FREE(cpumap);
    return rv;
}

5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289
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
     */
5290
    for (i = 0; i < msg->nfds; i++)
5291 5292 5293 5294 5295 5296 5297 5298 5299
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

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

    rv = 1;

5300
 cleanup:
5301 5302
    if (rv < 0)
        virNetMessageSaveError(rerr);
5303 5304
    if (dom)
        virDomainFree(dom);
5305 5306 5307
    return rv;
}

5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334
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;

5335 5336 5337 5338 5339 5340 5341
    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;
    }

5342 5343 5344 5345 5346 5347 5348 5349
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       0) < 0)
        goto cleanup;

    rv = 0;

5350
 cleanup:
5351 5352 5353 5354 5355 5356 5357 5358
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virTypedParamsFree(params, nparams);
    if (dom)
        virDomainFree(dom);
    return rv;
}

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

5382 5383 5384 5385 5386 5387 5388
    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;
    }

5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

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

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

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

    rv = 0;

5408
 cleanup:
5409 5410 5411 5412 5413 5414 5415 5416 5417
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}

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

5439 5440 5441 5442 5443 5444 5445
    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;
    }

5446 5447 5448 5449 5450 5451
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    /* Wacky world of XDR ... */
5452
    if (VIR_ALLOC(uri_out) < 0)
5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467
        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;

5468
 cleanup:
5469 5470 5471 5472 5473 5474 5475 5476 5477
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(uri_out);
    }
    return rv;
}

static int
5478 5479 5480 5481 5482 5483
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)
5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499
{
    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;
    }

5500 5501 5502 5503 5504 5505 5506
    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;
    }

5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

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

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

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

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

5531
 cleanup:
5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
        if (stream) {
            virStreamAbort(st);
            daemonFreeClientStream(client, stream);
        } else {
            virStreamFree(st);
        }
    }
    return rv;
}


static int
5548 5549 5550 5551 5552 5553
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)
5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569
{
    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;
    }

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

5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

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

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

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

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

    rv = 0;

5599
 cleanup:
5600 5601 5602 5603 5604 5605 5606 5607 5608 5609
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
5610 5611 5612 5613 5614 5615
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)
5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630
{
    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;
    }

5631 5632 5633 5634 5635 5636 5637
    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;
    }

5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

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

    make_nonnull_domain(&ret->dom, dom);

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

    rv = 0;

5658
 cleanup:
5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
5671 5672 5673 5674 5675
remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_confirm3_params_args *args)
5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688
{
    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;
    }

5689 5690 5691 5692 5693 5694 5695
    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;
    }

5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
        goto cleanup;

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

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

    rv = 0;

5712
 cleanup:
5713 5714 5715 5716 5717 5718 5719 5720 5721
    virTypedParamsFree(params, nparams);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765
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;

5766
 cleanup:
5767 5768 5769 5770 5771 5772 5773
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virStringFreeList(models);
    return rv;
}


5774 5775 5776 5777 5778 5779 5780
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)
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 5806 5807 5808 5809 5810
{
    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;

5811
 cleanup:
5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823
    for (i = 0; i < nfiles; i++) {
        VIR_FORCE_CLOSE(files[i]);
    }
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


5824 5825 5826 5827 5828 5829
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)
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
{
    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;

5863
 cleanup:
5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875
    for (i = 0; i < nfiles; i++) {
        VIR_FORCE_CLOSE(files[i]);
    }
    VIR_FREE(files);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    return rv;
}


5876 5877
static int
remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5878
                                             virNetServerClientPtr client,
5879 5880 5881
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                             remote_connect_network_event_register_any_args *args,
5882
                                             remote_connect_network_event_register_any_ret *ret)
5883 5884 5885
{
    int callbackID;
    int rv = -1;
5886 5887
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
5888 5889
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
5890
    virNetworkPtr net = NULL;
5891 5892 5893 5894 5895 5896 5897 5898

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

    virMutexLock(&priv->lock);

5899 5900 5901 5902
    if (args->net &&
        !(net = get_nonnull_network(priv->conn, *args->net)))
        goto cleanup;

5903 5904 5905
    if (args->eventID >= VIR_NETWORK_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network event ID %d"), args->eventID);
5906 5907 5908
        goto cleanup;
    }

5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923
    /* 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)
5924 5925 5926
        goto cleanup;

    if ((callbackID = virConnectNetworkEventRegisterAny(priv->conn,
5927
                                                        net,
5928 5929
                                                        args->eventID,
                                                        networkEventCallbacks[args->eventID],
5930 5931 5932 5933 5934
                                                        ref,
                                                        remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->networkEventCallbacks,
                     priv->nnetworkEventCallbacks, 1);
        callback = ref;
5935
        goto cleanup;
5936
    }
5937

5938
    ref->callbackID = callbackID;
5939
    ret->callbackID = callbackID;
5940 5941 5942

    rv = 0;

5943
 cleanup:
5944
    VIR_FREE(callback);
5945 5946
    if (rv < 0)
        virNetMessageSaveError(rerr);
5947 5948
    if (net)
        virNetworkFree(net);
5949 5950 5951 5952 5953 5954 5955
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5956
                                               virNetServerClientPtr client,
5957 5958
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
5959
                                               remote_connect_network_event_deregister_any_args *args)
5960 5961
{
    int rv = -1;
5962
    size_t i;
5963 5964 5965 5966 5967 5968 5969 5970 5971 5972
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    virMutexLock(&priv->lock);

5973
    for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
5974
        if (priv->networkEventCallbacks[i]->callbackID == args->callbackID)
5975 5976
            break;
    }
5977
    if (i == priv->nnetworkEventCallbacks) {
5978
        virReportError(VIR_ERR_INTERNAL_ERROR,
5979 5980
                       _("network event callback %d not registered"),
                       args->callbackID);
5981 5982 5983
        goto cleanup;
    }

5984
    if (virConnectNetworkEventDeregisterAny(priv->conn, args->callbackID) < 0)
5985 5986
        goto cleanup;

5987 5988
    VIR_DELETE_ELEMENT(priv->networkEventCallbacks, i,
                       priv->nnetworkEventCallbacks);
5989 5990 5991

    rv = 0;

5992
 cleanup:
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 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061
    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;

6062
 cleanup:
6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111
    VIR_FREE(callback);
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    virMutexUnlock(&priv->lock);
    return rv;
}


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

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

    virMutexLock(&priv->lock);

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

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

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

    rv = 0;

6112
 cleanup:
6113 6114 6115 6116 6117 6118
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return rv;
}

6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155
static int
remoteDispatchDomainGetTime(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client,
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                            virNetMessageErrorPtr rerr,
                            remote_domain_get_time_args *args,
                            remote_domain_get_time_ret *ret)
{
    int rv = -1;
    virDomainPtr dom = NULL;
    long long seconds;
    unsigned int nseconds;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if (virDomainGetTime(dom, &seconds, &nseconds, args->flags) < 0)
        goto cleanup;

    ret->seconds = seconds;
    ret->nseconds = nseconds;
    rv = 0;

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

M
Michal Privoznik 已提交
6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204

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;
6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219
}

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

6220
    if (VIR_STRDUP(lease_dst->iface, lease_src->iface) < 0 ||
J
Ján Tomko 已提交
6221
        VIR_STRDUP(lease_dst->ipaddr, lease_src->ipaddr) < 0)
6222 6223
        goto error;

J
Ján Tomko 已提交
6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248
    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;
6249 6250 6251 6252

    return 0;

 error:
J
Ján Tomko 已提交
6253 6254 6255 6256 6257 6258 6259 6260
    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);
6261 6262 6263 6264 6265
    VIR_FREE(mac_tmp);
    VIR_FREE(iaid_tmp);
    VIR_FREE(hostname_tmp);
    VIR_FREE(clientid_tmp);
    VIR_FREE(lease_dst->ipaddr);
6266
    VIR_FREE(lease_dst->iface);
6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365
    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,
                                           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);
    if (leases) {
        for (i = 0; i < nleases; i++)
            virNetworkDHCPLeaseFree(leases[i]);
        VIR_FREE(leases);
    }
    virNetworkFree(net);
    return rv;
}


static int
remoteDispatchNetworkGetDHCPLeasesForMAC(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_network_get_dhcp_leases_for_mac_args *args,
                                         remote_network_get_dhcp_leases_for_mac_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 = virNetworkGetDHCPLeasesForMAC(net, args->mac,
                                                 args->need_results ? &leases : NULL,
                                                 args->flags)) < 0)
        goto cleanup;
M
Michal Privoznik 已提交
6366

6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403
    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);
        return -1;
    }

    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);
    if (leases) {
        for (i = 0; i < nleases; i++)
            virNetworkDHCPLeaseFree(leases[i]);
        VIR_FREE(leases);
    }
    virNetworkFree(net);
    return rv;
M
Michal Privoznik 已提交
6404 6405 6406
}


6407 6408 6409 6410 6411 6412 6413 6414 6415
/*----- 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
6416
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
6417 6418
{
    virDomainPtr dom;
6419
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
6420 6421 6422 6423 6424 6425 6426 6427
    /* 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
6428
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
6429
{
6430
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
6431 6432
}

D
Daniel Veillard 已提交
6433
static virInterfacePtr
6434
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
6435
{
6436
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
6437 6438
}

6439
static virStoragePoolPtr
6440
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
6441
{
6442 6443
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
6444 6445 6446
}

static virStorageVolPtr
6447
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
6448 6449
{
    virStorageVolPtr ret;
6450 6451
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
6452 6453 6454
    return ret;
}

6455
static virSecretPtr
6456
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
6457
{
6458
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
6459 6460
}

6461
static virNWFilterPtr
6462
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
6463
{
6464
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
6465 6466
}

C
Chris Lalancette 已提交
6467
static virDomainSnapshotPtr
6468
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
6469
{
6470
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
6471 6472
}

6473 6474
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
6475
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
6476 6477
{
    dom_dst->id = dom_src->id;
6478
    ignore_value(VIR_STRDUP_QUIET(dom_dst->name, dom_src->name));
6479
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
6480 6481 6482
}

static void
6483
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
6484
{
6485
    ignore_value(VIR_STRDUP_QUIET(net_dst->name, net_src->name));
6486
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
6487 6488
}

D
Daniel Veillard 已提交
6489
static void
6490 6491
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
6492
{
6493 6494
    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 已提交
6495 6496
}

6497
static void
6498
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
6499
{
6500
    ignore_value(VIR_STRDUP_QUIET(pool_dst->name, pool_src->name));
6501
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
6502 6503 6504
}

static void
6505
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
6506
{
6507 6508 6509
    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));
6510
}
6511 6512

static void
6513
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
6514
{
6515
    ignore_value(VIR_STRDUP_QUIET(dev_dst->name, dev_src->name));
6516
}
6517 6518

static void
6519
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
6520
{
6521
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
6522
    secret_dst->usageType = secret_src->usageType;
6523
    ignore_value(VIR_STRDUP_QUIET(secret_dst->usageID, secret_src->usageID));
6524
}
6525 6526

static void
6527
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
6528
{
6529
    ignore_value(VIR_STRDUP_QUIET(nwfilter_dst->name, nwfilter_src->name));
6530
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
6531
}
C
Chris Lalancette 已提交
6532 6533

static void
6534
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
6535
{
6536
    ignore_value(VIR_STRDUP_QUIET(snapshot_dst->name, snapshot_src->name));
6537
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
6538
}
6539 6540 6541 6542 6543 6544 6545 6546

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;
6547
    size_t i = 0;
6548

6549
    if (VIR_ALLOC_N(val, nerrors) < 0)
6550
        goto error;
6551 6552

    for (i = 0; i < nerrors; i++) {
6553 6554
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
6555 6556 6557 6558 6559 6560 6561 6562
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

6563
 error:
6564
    if (val) {
6565
        size_t j;
6566 6567 6568 6569 6570 6571
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}