remote.c 188.6 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 55
#include "domain_conf.h"
#include "network_conf.h"
#include "viraccessapicheck.h"
56 57

#define VIR_FROM_THIS VIR_FROM_RPC
58

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

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

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

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

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

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

115 116
#include "remote_dispatch.h"
#include "qemu_dispatch.h"
117
#include "lxc_dispatch.h"
C
Chris Lalancette 已提交
118 119


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

128 129 130 131 132 133
static void
remoteEventCallbackFree(void *opaque)
{
    VIR_FREE(opaque);
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

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

cleanup:
    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);

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


static int
remoteRelayDomainEventLifecycle(virConnectPtr conn,
                                virDomainPtr dom,
                                int event,
                                int detail,
                                void *opaque)
195
{
196
    daemonClientEventCallbackPtr callback = opaque;
197
    remote_domain_event_lifecycle_msg data;
198

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

203 204
    VIR_DEBUG("Relaying domain lifecycle event %d %d, callback %d legacy %d",
              event, detail, callback->callbackID, callback->legacy);
205

206
    /* build return data */
207
    memset(&data, 0, sizeof(data));
208
    make_nonnull_domain(&data.dom, dom);
209 210
    data.event = event;
    data.detail = detail;
211

212 213 214 215 216 217 218 219 220 221 222 223 224 225
    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);
    }
226

227 228
    return 0;
}
229

230 231 232 233
static int
remoteRelayDomainEventReboot(virConnectPtr conn,
                             virDomainPtr dom,
                             void *opaque)
234
{
235
    daemonClientEventCallbackPtr callback = opaque;
236 237
    remote_domain_event_reboot_msg data;

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

242 243
    VIR_DEBUG("Relaying domain reboot event %s %d, callback %d",
              dom->name, dom->id, callback->callbackID);
244 245

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

249
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
250 251
                                  REMOTE_PROC_DOMAIN_EVENT_REBOOT,
                                  (xdrproc_t)xdr_remote_domain_event_reboot_msg, &data);
252 253 254 255

    return 0;
}

256

257 258 259 260 261
static int
remoteRelayDomainEventRTCChange(virConnectPtr conn,
                                virDomainPtr dom,
                                long long offset,
                                void *opaque)
262
{
263
    daemonClientEventCallbackPtr callback = opaque;
264 265
    remote_domain_event_rtc_change_msg data;

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

270 271
    VIR_DEBUG("Relaying domain rtc change event %s %d %lld, callback %d",
              dom->name, dom->id, offset, callback->callbackID);
272 273

    /* build return data */
274
    memset(&data, 0, sizeof(data));
275
    make_nonnull_domain(&data.dom, dom);
276 277
    data.offset = offset;

278
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
279 280
                                  REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_rtc_change_msg, &data);
281 282 283 284 285

    return 0;
}


286 287 288 289 290
static int
remoteRelayDomainEventWatchdog(virConnectPtr conn,
                               virDomainPtr dom,
                               int action,
                               void *opaque)
291
{
292
    daemonClientEventCallbackPtr callback = opaque;
293 294
    remote_domain_event_watchdog_msg data;

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

299 300
    VIR_DEBUG("Relaying domain watchdog event %s %d %d, callback %d",
              dom->name, dom->id, action, callback->callbackID);
301 302

    /* build return data */
303
    memset(&data, 0, sizeof(data));
304
    make_nonnull_domain(&data.dom, dom);
305 306
    data.action = action;

307
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
308 309
                                  REMOTE_PROC_DOMAIN_EVENT_WATCHDOG,
                                  (xdrproc_t)xdr_remote_domain_event_watchdog_msg, &data);
310 311 312 313 314

    return 0;
}


315 316 317 318 319 320 321
static int
remoteRelayDomainEventIOError(virConnectPtr conn,
                              virDomainPtr dom,
                              const char *srcPath,
                              const char *devAlias,
                              int action,
                              void *opaque)
322
{
323
    daemonClientEventCallbackPtr callback = opaque;
324 325
    remote_domain_event_io_error_msg data;

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

330 331 332
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d, callback %d",
              dom->name, dom->id, srcPath, devAlias, action,
              callback->callbackID);
333 334

    /* build return data */
335
    memset(&data, 0, sizeof(data));
336 337 338
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
339
    make_nonnull_domain(&data.dom, dom);
340 341
    data.action = action;

342
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
343 344
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_msg, &data);
345 346

    return 0;
347
error:
E
Eric Blake 已提交
348 349
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
350
    return -1;
351 352 353
}


354 355 356 357 358 359 360 361
static int
remoteRelayDomainEventIOErrorReason(virConnectPtr conn,
                                    virDomainPtr dom,
                                    const char *srcPath,
                                    const char *devAlias,
                                    int action,
                                    const char *reason,
                                    void *opaque)
362
{
363
    daemonClientEventCallbackPtr callback = opaque;
364 365
    remote_domain_event_io_error_reason_msg data;

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

370 371 372
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s, callback %d",
              dom->name, dom->id, srcPath, devAlias, action, reason,
              callback->callbackID);
373 374

    /* build return data */
375
    memset(&data, 0, sizeof(data));
376 377 378 379
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0 ||
        VIR_STRDUP(data.reason, reason) < 0)
        goto error;
380
    data.action = action;
381 382

    make_nonnull_domain(&data.dom, dom);
383

384
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
385 386
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_reason_msg, &data);
387 388

    return 0;
389

390
error:
E
Eric Blake 已提交
391 392 393
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
    VIR_FREE(data.reason);
394
    return -1;
395 396 397
}


398 399 400 401 402 403 404 405 406
static int
remoteRelayDomainEventGraphics(virConnectPtr conn,
                               virDomainPtr dom,
                               int phase,
                               virDomainEventGraphicsAddressPtr local,
                               virDomainEventGraphicsAddressPtr remote,
                               const char *authScheme,
                               virDomainEventGraphicsSubjectPtr subject,
                               void *opaque)
407
{
408
    daemonClientEventCallbackPtr callback = opaque;
409
    remote_domain_event_graphics_msg data;
410
    size_t i;
411

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

416 417
    VIR_DEBUG("Relaying domain graphics event %s %d %d - %d %s %s  - %d %s %s - %s, callback %d",
              dom->name, dom->id, phase,
418 419
              local->family, local->service, local->node,
              remote->family, remote->service, remote->node,
420
              authScheme, callback->callbackID);
421

422
    VIR_DEBUG("Subject %d", subject->nidentity);
423
    for (i = 0; i < subject->nidentity; i++) {
424
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
425 426 427
    }

    /* build return data */
428
    memset(&data, 0, sizeof(data));
429 430 431
    data.phase = phase;
    data.local.family = local->family;
    data.remote.family = remote->family;
432 433 434 435 436 437
    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;
438 439

    data.subject.subject_len = subject->nidentity;
440
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0)
441
        goto error;
442

443
    for (i = 0; i < data.subject.subject_len; i++) {
444 445 446
        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;
447
    }
448
    make_nonnull_domain(&data.dom, dom);
449

450
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
451 452
                                  REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
                                  (xdrproc_t)xdr_remote_domain_event_graphics_msg, &data);
453 454

    return 0;
455

456
error:
E
Eric Blake 已提交
457 458 459 460 461
    VIR_FREE(data.authScheme);
    VIR_FREE(data.local.node);
    VIR_FREE(data.local.service);
    VIR_FREE(data.remote.node);
    VIR_FREE(data.remote.service);
462
    if (data.subject.subject_val != NULL) {
463
        for (i = 0; i < data.subject.subject_len; i++) {
E
Eric Blake 已提交
464 465
            VIR_FREE(data.subject.subject_val[i].type);
            VIR_FREE(data.subject.subject_val[i].name);
466
        }
E
Eric Blake 已提交
467
        VIR_FREE(data.subject.subject_val);
468 469
    }
    return -1;
470 471
}

472 473 474 475 476 477 478
static int
remoteRelayDomainEventBlockJob(virConnectPtr conn,
                               virDomainPtr dom,
                               const char *path,
                               int type,
                               int status,
                               void *opaque)
479
{
480
    daemonClientEventCallbackPtr callback = opaque;
481 482
    remote_domain_event_block_job_msg data;

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

487 488
    VIR_DEBUG("Relaying domain block job event %s %d %s %i, %i, callback %d",
              dom->name, dom->id, path, type, status, callback->callbackID);
489 490

    /* build return data */
491
    memset(&data, 0, sizeof(data));
492 493
    if (VIR_STRDUP(data.path, path) < 0)
        goto error;
494 495
    data.type = type;
    data.status = status;
496
    make_nonnull_domain(&data.dom, dom);
497

498
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
499 500 501 502
                                  REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB,
                                  (xdrproc_t)xdr_remote_domain_event_block_job_msg, &data);

    return 0;
503
error:
E
Eric Blake 已提交
504
    VIR_FREE(data.path);
505
    return -1;
506 507
}

508

509 510 511 512
static int
remoteRelayDomainEventControlError(virConnectPtr conn,
                                   virDomainPtr dom,
                                   void *opaque)
513
{
514
    daemonClientEventCallbackPtr callback = opaque;
515 516
    remote_domain_event_control_error_msg data;

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

521 522
    VIR_DEBUG("Relaying domain control error %s %d, callback %d",
              dom->name, dom->id, callback->callbackID);
523 524

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

528
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
529 530 531 532 533 534 535
                                  REMOTE_PROC_DOMAIN_EVENT_CONTROL_ERROR,
                                  (xdrproc_t)xdr_remote_domain_event_control_error_msg, &data);

    return 0;
}


536 537 538 539 540 541 542 543
static int
remoteRelayDomainEventDiskChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *oldSrcPath,
                                 const char *newSrcPath,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
544
{
545
    daemonClientEventCallbackPtr callback = opaque;
546 547 548
    remote_domain_event_disk_change_msg data;
    char **oldSrcPath_p = NULL, **newSrcPath_p = NULL;

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

553 554 555
    VIR_DEBUG("Relaying domain %s %d disk change %s %s %s %d, callback %d",
              dom->name, dom->id, oldSrcPath, newSrcPath, devAlias, reason,
              callback->callbackID);
556 557

    /* build return data */
558
    memset(&data, 0, sizeof(data));
559 560
    if (oldSrcPath &&
        ((VIR_ALLOC(oldSrcPath_p) < 0) ||
561
         VIR_STRDUP(*oldSrcPath_p, oldSrcPath) < 0))
562
        goto error;
563 564 565

    if (newSrcPath &&
        ((VIR_ALLOC(newSrcPath_p) < 0) ||
566
         VIR_STRDUP(*newSrcPath_p, newSrcPath) < 0))
567
        goto error;
568 569 570

    data.oldSrcPath = oldSrcPath_p;
    data.newSrcPath = newSrcPath_p;
571 572
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
573 574 575 576
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

577
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
578 579 580 581 582
                                  REMOTE_PROC_DOMAIN_EVENT_DISK_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_disk_change_msg, &data);

    return 0;

583
error:
M
Michal Privoznik 已提交
584 585
    VIR_FREE(oldSrcPath_p);
    VIR_FREE(newSrcPath_p);
586 587 588 589
    return -1;
}


590 591 592 593 594 595 596
static int
remoteRelayDomainEventTrayChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
{
597
    daemonClientEventCallbackPtr callback = opaque;
598 599
    remote_domain_event_tray_change_msg data;

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

604 605
    VIR_DEBUG("Relaying domain %s %d tray change devAlias: %s reason: %d, callback %d",
              dom->name, dom->id, devAlias, reason, callback->callbackID);
606 607

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

610
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
611 612 613 614 615
        return -1;
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

616
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
617 618 619 620 621 622
                                  REMOTE_PROC_DOMAIN_EVENT_TRAY_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_tray_change_msg, &data);

    return 0;
}

623 624 625 626 627 628
static int
remoteRelayDomainEventPMWakeup(virConnectPtr conn,
                               virDomainPtr dom,
                               int reason ATTRIBUTE_UNUSED,
                               void *opaque)
{
629
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
630 631
    remote_domain_event_pmwakeup_msg data;

632 633
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
634 635
        return -1;

636 637
    VIR_DEBUG("Relaying domain %s %d system pmwakeup, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
638 639

    /* build return data */
640
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
641 642
    make_nonnull_domain(&data.dom, dom);

643
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
O
Osier Yang 已提交
644 645 646 647 648 649
                                  REMOTE_PROC_DOMAIN_EVENT_PMWAKEUP,
                                  (xdrproc_t)xdr_remote_domain_event_pmwakeup_msg, &data);

    return 0;
}

650 651 652 653 654 655
static int
remoteRelayDomainEventPMSuspend(virConnectPtr conn,
                                virDomainPtr dom,
                                int reason ATTRIBUTE_UNUSED,
                                void *opaque)
{
656
    daemonClientEventCallbackPtr callback = opaque;
O
Osier Yang 已提交
657 658
    remote_domain_event_pmsuspend_msg data;

659 660
    if (callback->callbackID < 0 ||
        !remoteRelayDomainEventCheckACL(callback->client, conn, dom))
O
Osier Yang 已提交
661 662
        return -1;

663 664
    VIR_DEBUG("Relaying domain %s %d system pmsuspend, callback %d",
              dom->name, dom->id, callback->callbackID);
O
Osier Yang 已提交
665 666

    /* build return data */
667
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
668 669
    make_nonnull_domain(&data.dom, dom);

670
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
O
Osier Yang 已提交
671 672 673 674 675 676
                                  REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND,
                                  (xdrproc_t)xdr_remote_domain_event_pmsuspend_msg, &data);

    return 0;
}

677
static int
678
remoteRelayDomainEventBalloonChange(virConnectPtr conn,
679 680 681 682
                                    virDomainPtr dom,
                                    unsigned long long actual,
                                    void *opaque)
{
683
    daemonClientEventCallbackPtr callback = opaque;
684 685
    remote_domain_event_balloon_change_msg data;

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

690 691
    VIR_DEBUG("Relaying domain balloon change event %s %d %lld, callback %d",
              dom->name, dom->id, actual, callback->callbackID);
692 693 694 695 696 697

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

698
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
699 700 701 702 703 704 705
                                  REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_balloon_change_msg, &data);

    return 0;
}


706 707 708 709 710 711
static int
remoteRelayDomainEventPMSuspendDisk(virConnectPtr conn,
                                    virDomainPtr dom,
                                    int reason ATTRIBUTE_UNUSED,
                                    void *opaque)
{
712
    daemonClientEventCallbackPtr callback = opaque;
713 714
    remote_domain_event_pmsuspend_disk_msg data;

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

719 720
    VIR_DEBUG("Relaying domain %s %d system pmsuspend-disk, callback %d",
              dom->name, dom->id, callback->callbackID);
721 722 723 724 725

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

726
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
727 728 729 730 731 732
                                  REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND_DISK,
                                  (xdrproc_t)xdr_remote_domain_event_pmsuspend_disk_msg, &data);

    return 0;
}

733
static int
734
remoteRelayDomainEventDeviceRemoved(virConnectPtr conn,
735 736 737 738
                                    virDomainPtr dom,
                                    const char *devAlias,
                                    void *opaque)
{
739
    daemonClientEventCallbackPtr callback = opaque;
740 741
    remote_domain_event_device_removed_msg data;

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

746 747
    VIR_DEBUG("Relaying domain device removed event %s %d %s, callback %d",
              dom->name, dom->id, devAlias, callback->callbackID);
748 749 750 751 752 753 754 755 756

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

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

    make_nonnull_domain(&data.dom, dom);

757
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
758 759 760 761 762 763 764
                                  REMOTE_PROC_DOMAIN_EVENT_DEVICE_REMOVED,
                                  (xdrproc_t)xdr_remote_domain_event_device_removed_msg,
                                  &data);

    return 0;
}

765

766
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
767
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
768
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
769
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
770
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
771
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
772
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
773
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
774
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
775
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
776
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
777
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
778
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
779
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
780
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
781
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
782
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
783 784 785 786
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

787
static int
788
remoteRelayNetworkEventLifecycle(virConnectPtr conn,
789 790 791 792
                                 virNetworkPtr net,
                                 int event,
                                 int detail,
                                 void *opaque)
793
{
794
    daemonClientEventCallbackPtr callback = opaque;
795 796
    remote_network_event_lifecycle_msg data;

797 798
    if (callback->callbackID < 0 ||
        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
799 800
        return -1;

801 802
    VIR_DEBUG("Relaying network lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);
803 804 805 806

    /* build return data */
    memset(&data, 0, sizeof(data));
    make_nonnull_network(&data.net, net);
807
    data.callbackID = callback->callbackID;
808 809 810
    data.event = event;
    data.detail = detail;

811
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
812 813 814 815 816 817 818 819 820 821 822 823
                                  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);

824 825 826 827 828 829 830
/*
 * 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
 */
831
void remoteClientFreeFunc(void *data)
832 833 834 835 836
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
837
        virIdentityPtr sysident = virIdentityGetSystem();
838
        size_t i;
839

840 841
        virIdentitySetCurrent(sysident);

842 843 844 845 846
        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;
847
            }
848 849 850 851 852
            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");
853
        }
854
        VIR_FREE(priv->domainEventCallbacks);
855

856 857 858 859 860
        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;
861
            }
862 863 864 865 866 867
            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");
868
        }
869
        VIR_FREE(priv->networkEventCallbacks);
870

871
        virConnectClose(priv->conn);
872 873 874

        virIdentitySetCurrent(NULL);
        virObjectUnref(sysident);
875 876 877 878 879 880
    }

    VIR_FREE(priv);
}


881 882 883 884 885 886 887
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

888

889 890
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
891 892 893
{
    struct daemonClientPrivate *priv;

894
    if (VIR_ALLOC(priv) < 0)
895
        return NULL;
896 897 898

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
899
        virReportSystemError(errno, "%s", _("unable to init mutex"));
900
        return NULL;
901 902
    }

903
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
904
    return priv;
905 906
}

907 908 909
/*----- Functions. -----*/

static int
910 911 912 913 914
remoteDispatchConnectOpen(virNetServerPtr server,
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
915 916
{
    const char *name;
917
    unsigned int flags;
918
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
919
    int rv = -1;
920

921 922 923 924
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
925
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
926 927 928
        goto cleanup;
    }

929
    if (virNetServerKeepAliveRequired(server) && !priv->keepalive_supported) {
930 931
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("keepalive support is required to connect"));
932 933 934
        goto cleanup;
    }

935 936 937 938 939 940
    name = args->name ? *args->name : NULL;

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

944
    priv->conn =
945
        flags & VIR_CONNECT_RO
946 947
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
948

949
    if (priv->conn == NULL)
950 951 952
        goto cleanup;

    rv = 0;
953

954 955
cleanup:
    if (rv < 0)
956 957
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
958
    return rv;
959 960 961 962
}


static int
963 964 965 966
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
967
{
968
    virNetServerClientDelayedClose(client);
969
    return 0;
970 971
}

972

973
static int
974 975
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
976
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
977
                                     virNetMessageErrorPtr rerr,
978 979
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
980
{
981
    virDomainPtr dom = NULL;
982 983
    char *type;
    int nparams;
984
    int rv = -1;
985 986
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
987

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

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

996
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
997
        goto cleanup;
998 999 1000

    ret->type = type;
    ret->nparams = nparams;
1001 1002 1003 1004
    rv = 0;

cleanup:
    if (rv < 0)
1005
        virNetMessageSaveError(rerr);
1006 1007 1008
    if (dom)
        virDomainFree(dom);
    return rv;
1009 1010
}

1011 1012
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
1013 1014 1015
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
1016
                               remote_typed_param **ret_params_val,
1017 1018
                               u_int *ret_params_len,
                               unsigned int flags)
1019
{
1020 1021
    size_t i;
    size_t j;
1022 1023 1024 1025
    int rv = -1;
    remote_typed_param *val;

    *ret_params_len = nparams;
1026
    if (VIR_ALLOC_N(val, nparams) < 0)
1027 1028
        goto cleanup;

1029
    for (i = 0, j = 0; i < nparams; ++i) {
1030 1031 1032 1033 1034
        /* 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)) {
1035 1036 1037 1038
            --*ret_params_len;
            continue;
        }

1039
        /* remoteDispatchClientRequest will free this: */
1040
        if (VIR_STRDUP(val[j].field, params[i].field) < 0)
1041
            goto cleanup;
1042
        val[j].value.type = params[i].type;
1043
        switch (params[i].type) {
1044
        case VIR_TYPED_PARAM_INT:
1045
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
1046 1047
            break;
        case VIR_TYPED_PARAM_UINT:
1048
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
1049 1050
            break;
        case VIR_TYPED_PARAM_LLONG:
1051
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
1052 1053
            break;
        case VIR_TYPED_PARAM_ULLONG:
1054
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
1055 1056
            break;
        case VIR_TYPED_PARAM_DOUBLE:
1057
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
1058 1059
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
1060 1061 1062
            val[j].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
        case VIR_TYPED_PARAM_STRING:
1063
            if (VIR_STRDUP(val[j].value.remote_typed_param_value_u.s, params[i].value.s) < 0)
1064
                goto cleanup;
1065 1066
            break;
        default:
1067 1068
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
1069 1070
            goto cleanup;
        }
1071
        j++;
1072 1073 1074 1075 1076 1077 1078 1079
    }

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

cleanup:
    if (val) {
1080
        for (i = 0; i < nparams; i++) {
1081
            VIR_FREE(val[i].field);
1082
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
1083 1084
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
1085 1086 1087 1088 1089 1090 1091
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
1092 1093
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
1094 1095 1096
                                 int limit,
                                 int *nparams)
{
1097
    size_t i = 0;
1098 1099 1100 1101
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
1102
    if (limit && args_params_len > limit) {
1103
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1104 1105
        goto cleanup;
    }
1106
    if (VIR_ALLOC_N(params, args_params_len) < 0)
1107 1108 1109 1110 1111 1112 1113 1114
        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) {
1115 1116 1117
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Parameter %s too big for destination"),
                           args_params_val[i].field);
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
            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;
1146
        case VIR_TYPED_PARAM_STRING:
1147 1148
            if (VIR_STRDUP(params[i].value.s,
                           args_params_val[i].value.remote_typed_param_value_u.s) < 0)
1149 1150
                goto cleanup;
            break;
1151
        default:
1152 1153
            virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                           params[i].type);
1154 1155 1156 1157 1158 1159 1160
            goto cleanup;
        }
    }

    rv = 0;

cleanup:
1161
    if (rv < 0) {
1162 1163
        virTypedParamsFree(params, i);
        params = NULL;
1164
    }
1165 1166 1167
    return params;
}

1168
static int
1169 1170
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
1171
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
1172
                                           virNetMessageErrorPtr rerr,
1173 1174
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
1175
{
1176
    virDomainPtr dom = NULL;
1177
    virTypedParameterPtr params = NULL;
1178
    int nparams = 0;
1179
    int rv = -1;
1180 1181
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1182

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

1188
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1189
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1190
        goto cleanup;
1191
    }
1192
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1193
        goto cleanup;
1194
    nparams = args->nparams;
1195

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

1199
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1200
        goto cleanup;
1201

1202
    if (remoteSerializeTypedParameters(params, nparams,
1203
                                       &ret->params.params_val,
1204 1205
                                       &ret->params.params_len,
                                       0) < 0)
1206 1207 1208 1209 1210 1211
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
1212
        virNetMessageSaveError(rerr);
1213
    virTypedParamsFree(params, nparams);
1214 1215 1216 1217 1218
    if (dom)
        virDomainFree(dom);
    return rv;
}

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
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;
1229
    size_t i;
1230 1231 1232 1233
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1234
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1235 1236 1237 1238 1239 1240 1241 1242
        goto cleanup;
    }

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

1243 1244 1245 1246 1247 1248 1249
    if (ndomains > REMOTE_DOMAIN_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domains '%d' for limit '%d'"),
                       ndomains, REMOTE_DOMAIN_LIST_MAX);
        goto cleanup;
    }

1250
    if (doms && ndomains) {
1251
        if (VIR_ALLOC_N(ret->domains.domains_val, ndomains) < 0)
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
            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;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (doms) {
        for (i = 0; i < ndomains; i++)
            virDomainFree(doms[i]);
        VIR_FREE(doms);
    }
    return rv;
}

1278
static int
1279 1280
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1281
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1282
                                                virNetMessageErrorPtr rerr,
1283 1284 1285 1286 1287
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1288
    int nparams = 0;
1289
    int rv = -1;
1290 1291
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1292

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

1298
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1299
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1300 1301
        goto cleanup;
    }
1302
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1303
        goto cleanup;
1304
    nparams = args->nparams;
1305

1306
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1307 1308 1309 1310 1311 1312 1313
        goto cleanup;

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

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

1319
    rv = 0;
1320 1321

cleanup:
1322
    if (rv < 0)
1323
        virNetMessageSaveError(rerr);
1324
    virTypedParamsFree(params, nparams);
1325 1326 1327
    if (dom)
        virDomainFree(dom);
    return rv;
1328 1329
}

1330
static int
1331 1332
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1333
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1334
                                virNetMessageErrorPtr rerr,
1335 1336
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1337
{
1338
    virDomainPtr dom = NULL;
1339
    struct _virDomainMemoryStat *stats = NULL;
1340 1341
    int nr_stats;
    size_t i;
1342
    int rv = -1;
1343 1344
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1345

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

1351
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1352 1353
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1354
        goto cleanup;
1355 1356
    }

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

    /* Allocate stats array for making dispatch call */
1361
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
1362
        goto cleanup;
1363

1364
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1365
    if (nr_stats < 0)
1366
        goto cleanup;
1367 1368

    /* Allocate return buffer */
1369
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
1370
        goto cleanup;
1371 1372 1373 1374 1375 1376 1377

    /* 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;
1378 1379 1380 1381
    rv = 0;

cleanup:
    if (rv < 0)
1382
        virNetMessageSaveError(rerr);
1383 1384
    if (dom)
        virDomainFree(dom);
1385
    VIR_FREE(stats);
1386
    return rv;
1387 1388
}

1389
static int
1390 1391
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1392
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1393
                              virNetMessageErrorPtr rerr,
1394 1395
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1396
{
1397
    virDomainPtr dom = NULL;
1398 1399 1400 1401
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1402
    int rv = -1;
1403 1404
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1405

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

1411
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1412
        goto cleanup;
1413 1414 1415 1416 1417 1418
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1419 1420
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1421
        goto cleanup;
1422 1423 1424
    }

    ret->buffer.buffer_len = size;
1425
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1426
        goto cleanup;
1427

1428
    if (virDomainBlockPeek(dom, path, offset, size,
1429
                           ret->buffer.buffer_val, flags) < 0)
1430
        goto cleanup;
1431

1432 1433 1434 1435
    rv = 0;

cleanup:
    if (rv < 0) {
1436
        virNetMessageSaveError(rerr);
1437 1438 1439 1440 1441
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
1442 1443
}

1444 1445 1446
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1447
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1448 1449 1450 1451 1452 1453 1454
                                    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;
1455
    int nparams = 0;
1456 1457 1458 1459 1460 1461
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1462
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1463 1464 1465 1466 1467 1468 1469
        goto cleanup;
    }

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

1470
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1471
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1472 1473
        goto cleanup;
    }
1474
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1475
        goto cleanup;
1476
    nparams = args->nparams;
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491

    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,
1492 1493
                                       &ret->params.params_len,
                                       args->flags) < 0)
1494 1495 1496 1497 1498 1499
        goto cleanup;

success:
    rv = 0;

cleanup:
1500
    if (rv < 0)
1501
        virNetMessageSaveError(rerr);
1502
    virTypedParamsFree(params, nparams);
A
ajia@redhat.com 已提交
1503 1504
    if (dom)
        virDomainFree(dom);
1505 1506 1507
    return rv;
}

R
Richard W.M. Jones 已提交
1508
static int
1509 1510
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1511
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1512
                               virNetMessageErrorPtr rerr,
1513 1514
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1515
{
1516
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1517 1518 1519
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1520
    int rv = -1;
1521 1522
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1523

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

1529
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1530
        goto cleanup;
R
Richard W.M. Jones 已提交
1531 1532 1533 1534 1535
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1536 1537
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1538
        goto cleanup;
R
Richard W.M. Jones 已提交
1539 1540 1541
    }

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

1545
    if (virDomainMemoryPeek(dom, offset, size,
1546
                            ret->buffer.buffer_val, flags) < 0)
1547
        goto cleanup;
R
Richard W.M. Jones 已提交
1548

1549 1550 1551 1552
    rv = 0;

cleanup:
    if (rv < 0) {
1553
        virNetMessageSaveError(rerr);
1554 1555 1556 1557 1558
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
1559 1560
}

1561
static int
1562 1563
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1564
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1565
                                     virNetMessageErrorPtr rerr,
1566 1567
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1568
{
1569 1570
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1571
    int rv = -1;
1572 1573
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1574

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

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

1583
    if (VIR_ALLOC(seclabel) < 0)
1584 1585 1586 1587 1588 1589
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
1590
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
1591
        goto cleanup;
1592 1593
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1594

1595 1596 1597 1598
    rv = 0;

cleanup:
    if (rv < 0)
1599
        virNetMessageSaveError(rerr);
1600 1601 1602
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
1603
    return rv;
1604 1605
}

M
Marcelo Cerri 已提交
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
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;
1616 1617
    int len, rv = -1;
    size_t i;
M
Marcelo Cerri 已提交
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
    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;
    }

1636
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
1637 1638 1639 1640 1641
        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];
1642
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0)
M
Marcelo Cerri 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
            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;

done:
    rv = 0;

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

1666
static int
1667 1668
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1669
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1670
                                   virNetMessageErrorPtr rerr,
1671
                                   remote_node_get_security_model_ret *ret)
1672
{
1673
    virSecurityModel secmodel;
1674
    int rv = -1;
1675 1676
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1677

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

1683
    memset(&secmodel, 0, sizeof(secmodel));
1684
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
1685 1686 1687
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
1688
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
1689 1690 1691 1692
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
1693
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
1694
        goto cleanup;
1695
    strcpy(ret->doi.doi_val, secmodel.doi);
1696

1697 1698 1699 1700
    rv = 0;

cleanup:
    if (rv < 0)
1701
        virNetMessageSaveError(rerr);
1702
    return rv;
1703 1704
}

1705
static int
1706 1707
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1708
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1709
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
1710 1711
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
1712 1713 1714 1715 1716
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
1717 1718
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1719

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

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

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
1729
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
1730 1731 1732 1733 1734
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
1735
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1736 1737 1738 1739 1740 1741
        goto cleanup;
    }

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

E
Eric Blake 已提交
1744
    if ((num = virDomainGetVcpuPinInfo(dom,
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
                                       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;
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
    cpumaps = NULL;

    rv = 0;

cleanup:
    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;

cleanup:
    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)
1834
        goto cleanup;
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844

    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;
1845 1846 1847 1848 1849 1850
    cpumaps = NULL;

    rv = 0;

cleanup:
    if (rv < 0)
1851
        virNetMessageSaveError(rerr);
1852 1853 1854 1855 1856 1857
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;
}

1858
static int
1859 1860
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
1861
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
1862
                             virNetMessageErrorPtr rerr,
1863 1864
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
1865
{
1866
    virDomainPtr dom = NULL;
1867 1868
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
1869 1870
    int info_len;
    size_t i;
1871
    int rv = -1;
1872 1873
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1874

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

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

1883
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1884
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
1885
        goto cleanup;
1886
    }
1887

1888 1889
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1890
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1891 1892 1893 1894 1895
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
1896
        goto cleanup;
1897 1898
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
1899
        goto cleanup;
1900 1901 1902 1903 1904 1905 1906 1907 1908

    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)
1909
        goto cleanup;
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926

    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;
1927 1928

cleanup:
1929
    if (rv < 0) {
1930
        virNetMessageSaveError(rerr);
1931 1932 1933 1934
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
1935 1936 1937
    if (dom)
        virDomainFree(dom);
    return rv;
1938 1939 1940
}

static int
1941 1942
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1943
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1944
                                   virNetMessageErrorPtr rerr,
1945 1946
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
1947
{
1948 1949 1950 1951 1952
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1953
    int rv = -1;
1954 1955
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1956

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

1962 1963 1964 1965
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
1966
    if (VIR_ALLOC(uri_out) < 0)
1967
        goto cleanup;
1968

1969
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
1970 1971
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
1972
        goto cleanup;
1973

1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
    /* 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;
    }
1985

1986
    rv = 0;
1987

1988 1989
cleanup:
    if (rv < 0)
1990
        virNetMessageSaveError(rerr);
1991
    VIR_FREE(uri_out);
1992
    return rv;
1993 1994
}

1995
static int
1996 1997
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1998
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1999
                                    virNetMessageErrorPtr rerr,
2000 2001
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
2002
{
2003 2004 2005 2006 2007
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
2008
    int rv = -1;
2009 2010
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2011

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

2017 2018
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
2019

2020
    /* Wacky world of XDR ... */
2021
    if (VIR_ALLOC(uri_out) < 0)
2022
        goto cleanup;
2023

2024
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
2025 2026 2027
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
2028
        goto cleanup;
2029

2030 2031 2032 2033 2034 2035
    /* 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;
2036

2037 2038 2039 2040
    rv = 0;

cleanup:
    if (rv < 0)
2041
        virNetMessageSaveError(rerr);
2042
    return rv;
2043 2044
}

C
Chris Lalancette 已提交
2045
static int
2046 2047
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
2048
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
2049 2050 2051
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
2052
{
2053
    virDomainPtr dom = NULL;
2054
    virTypedParameterPtr params = NULL;
2055
    int nparams = 0;
2056
    unsigned int flags;
2057
    int rv = -1;
2058 2059
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2060

2061
    if (!priv->conn) {
2062
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2063
        goto cleanup;
2064
    }
C
Chris Lalancette 已提交
2065

2066
    flags = args->flags;
C
Chris Lalancette 已提交
2067

2068
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2069
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2070 2071
        goto cleanup;
    }
2072
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2073
        goto cleanup;
2074
    nparams = args->nparams;
C
Chris Lalancette 已提交
2075

2076
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2077
        goto cleanup;
C
Chris Lalancette 已提交
2078

2079
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
2080
        goto cleanup;
C
Chris Lalancette 已提交
2081

2082 2083 2084 2085 2086 2087
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
2088 2089
    }

2090
    if (remoteSerializeTypedParameters(params, nparams,
2091
                                       &ret->params.params_val,
2092 2093
                                       &ret->params.params_len,
                                       args->flags) < 0)
2094
        goto cleanup;
2095

2096
success:
2097 2098 2099
    rv = 0;

cleanup:
2100
    if (rv < 0)
2101
        virNetMessageSaveError(rerr);
2102
    virTypedParamsFree(params, nparams);
2103 2104 2105
    if (dom)
        virDomainFree(dom);
    return rv;
2106 2107
}

2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
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;
2118
    int nparams = 0;
2119 2120 2121 2122 2123 2124
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2125
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2126 2127 2128 2129 2130
        goto cleanup;
    }

    flags = args->flags;

2131
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
2132
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2133 2134
        goto cleanup;
    }
2135
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2136
        goto cleanup;
2137
    nparams = args->nparams;
2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164

    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;

success:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
2165
    virTypedParamsFree(params, nparams);
2166 2167 2168 2169 2170
    if (dom)
        virDomainFree(dom);
    return rv;
}

2171
static int
2172 2173
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
2174
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2175 2176 2177
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
2178
{
2179
    virDomainPtr dom = NULL;
2180
    virTypedParameterPtr params = NULL;
2181
    int nparams = 0;
2182
    unsigned int flags;
2183
    int rv = -1;
2184 2185
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2186

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

2192 2193
    flags = args->flags;

2194
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2195
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2196
        goto cleanup;
2197
    }
2198
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2199
        goto cleanup;
2200
    nparams = args->nparams;
2201

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

2205
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2206
        goto cleanup;
2207

2208 2209 2210 2211 2212 2213 2214 2215
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2216
    if (remoteSerializeTypedParameters(params, nparams,
2217
                                       &ret->params.params_val,
2218 2219
                                       &ret->params.params_len,
                                       args->flags) < 0)
2220
        goto cleanup;
2221

2222 2223
success:
    rv = 0;
2224

2225
cleanup:
2226
    if (rv < 0)
2227
        virNetMessageSaveError(rerr);
2228
    virTypedParamsFree(params, nparams);
2229 2230 2231
    if (dom)
        virDomainFree(dom);
    return rv;
2232 2233
}

2234
static int
2235 2236
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2237
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2238 2239 2240
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2241
{
2242
    virNodeCPUStatsPtr params = NULL;
2243
    size_t i;
2244
    int cpuNum = args->cpuNum;
2245
    int nparams = 0;
2246 2247
    unsigned int flags;
    int rv = -1;
2248 2249
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2250

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

    flags = args->flags;

2258
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
2259
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2260 2261
        goto cleanup;
    }
2262
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2263
        goto cleanup;
2264
    nparams = args->nparams;
2265

2266
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
        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)
2280
        goto cleanup;
2281 2282 2283

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2284 2285
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2286 2287 2288 2289 2290 2291 2292

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

success:
    rv = 0;

2293 2294
cleanup:
    if (rv < 0) {
2295
        virNetMessageSaveError(rerr);
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
        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
2307 2308
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2309
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2310 2311 2312
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2313
{
2314
    virNodeMemoryStatsPtr params = NULL;
2315
    size_t i;
2316
    int cellNum = args->cellNum;
2317
    int nparams = 0;
2318 2319
    unsigned int flags;
    int rv = -1;
2320 2321
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2322

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

    flags = args->flags;

2330
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
2331
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2332 2333
        goto cleanup;
    }
2334
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2335
        goto cleanup;
2336
    nparams = args->nparams;
2337

2338
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
        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)
2352
        goto cleanup;
2353 2354 2355

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2356 2357
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2358 2359 2360 2361 2362 2363 2364

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

success:
    rv = 0;

2365 2366
cleanup:
    if (rv < 0) {
2367
        virNetMessageSaveError(rerr);
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
        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;
}

2378 2379 2380
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2381
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
                                    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) {
2393
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
        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;

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

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

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

2439
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
2440
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2441 2442 2443
        goto cleanup;
    }

2444
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2445
        goto cleanup;
2446
    nparams = args->nparams;
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475

    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;

success:
    rv = 0;

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

D
Daniel Veillard 已提交
2482 2483
/*-------------------------------------------------------------*/

2484
static int
2485 2486
remoteDispatchAuthList(virNetServerPtr server ATTRIBUTE_UNUSED,
                       virNetServerClientPtr client,
2487
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2488
                       virNetMessageErrorPtr rerr,
2489
                       remote_auth_list_ret *ret)
2490
{
2491
    int rv = -1;
2492 2493
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2494
    gid_t callerGid;
2495
    pid_t callerPid;
2496
    unsigned long long timestamp;
2497 2498 2499 2500 2501 2502

    /* 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) {
2503
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2504
                                              &callerPid, &timestamp) < 0) {
2505 2506 2507 2508
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2509 2510
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
2511
                            (long long) callerPid, (int) callerUid) < 0)
J
Jim Fehlig 已提交
2512 2513
                goto cleanup;
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
2514 2515
            virNetServerClientSetAuth(client, 0);
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
2516
            VIR_FREE(ident);
2517 2518
        }
    }
2519

2520
    ret->types.types_len = 1;
2521
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
2522
        goto cleanup;
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536

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

2538 2539 2540 2541
    rv = 0;

cleanup:
    if (rv < 0)
2542
        virNetMessageSaveError(rerr);
2543
    return rv;
2544 2545 2546
}


2547
#ifdef WITH_SASL
2548 2549
/*
 * Initializes the SASL session in prepare for authentication
2550
 * and gives the client a list of allowed mechanisms to choose
2551 2552
 */
static int
2553 2554
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2555
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2556
                           virNetMessageErrorPtr rerr,
2557
                           remote_auth_sasl_init_ret *ret)
2558
{
2559 2560 2561
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2562

2563
    virMutexLock(&priv->lock);
2564

2565 2566 2567
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2568
        VIR_ERROR(_("client tried invalid SASL init request"));
2569
        goto authfail;
2570 2571
    }

2572 2573 2574 2575 2576
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2577
        goto authfail;
2578

2579
# if WITH_GNUTLS
2580
    /* Inform SASL that we've got an external SSF layer from TLS */
2581 2582 2583 2584
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2585
            goto authfail;
2586 2587 2588 2589 2590

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2591
            goto authfail;
2592
    }
2593
# endif
2594

2595
    if (virNetServerClientIsSecure(client))
2596
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2597 2598
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2599
        /* Plain TCP, better get an SSF layer */
2600 2601 2602 2603
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
2604

2605
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
2606
        goto authfail;
2607
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
2608

2609 2610
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
2611
    return 0;
2612 2613

authfail:
2614
    virResetLastError();
2615 2616
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2617
    virNetMessageSaveError(rerr);
2618 2619 2620
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2621
    virObjectUnref(sasl);
2622
    virMutexUnlock(&priv->lock);
2623
    return -1;
2624 2625
}

2626
/*
2627 2628
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
2629
static int
2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645
remoteSASLFinish(virNetServerClientPtr client)
{
    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;
        }
2646
    }
2647 2648

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
2649
        return -2;
2650

2651 2652
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
2653

2654
    virNetServerClientSetAuth(client, 0);
2655
    virNetServerClientSetSASLSession(client, priv->sasl);
2656

2657
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
2658

2659 2660 2661
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2662

2663
    virObjectUnref(priv->sasl);
2664
    priv->sasl = NULL;
2665

2666
    return 0;
2667

2668 2669 2670
error:
    return -1;
}
2671

2672 2673 2674 2675
/*
 * This starts the SASL authentication negotiation.
 */
static int
2676 2677
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client,
2678
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2679
                            virNetMessageErrorPtr rerr,
2680 2681
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2682 2683
{
    const char *serverout;
2684
    size_t serveroutlen;
2685
    int err;
2686 2687 2688
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2689
    const char *identity;
2690

2691
    virMutexLock(&priv->lock);
2692

2693 2694 2695
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
2696
        VIR_ERROR(_("client tried invalid SASL start request"));
2697
        goto authfail;
2698 2699
    }

2700 2701
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
2702 2703 2704 2705 2706 2707 2708 2709 2710
    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)
2711
        goto authfail;
2712

2713
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2714
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
2715
        goto authfail;
2716 2717 2718 2719
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2720 2721
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2722 2723 2724 2725 2726 2727 2728
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2729
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2730
    if (err == VIR_NET_SASL_CONTINUE) {
2731 2732
        ret->complete = 0;
    } else {
2733
        /* Check username whitelist ACL */
2734
        if ((err = remoteSASLFinish(client)) < 0) {
2735 2736 2737 2738 2739
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2740

2741 2742 2743
        ret->complete = 1;
    }

2744
    virMutexUnlock(&priv->lock);
2745
    return 0;
2746 2747

authfail:
2748 2749 2750
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2751 2752 2753
    goto error;

authdeny:
2754
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2755 2756 2757
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2758 2759
    goto error;

2760
error:
2761
    virObjectUnref(priv->sasl);
2762 2763
    priv->sasl = NULL;
    virResetLastError();
2764 2765
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2766 2767 2768
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2769
    return -1;
2770 2771 2772 2773
}


static int
2774 2775
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2776
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2777
                           virNetMessageErrorPtr rerr,
2778 2779
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
2780 2781
{
    const char *serverout;
2782
    size_t serveroutlen;
2783
    int err;
2784 2785 2786
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2787
    const char *identity;
2788

2789 2790 2791 2792 2793
    virMutexLock(&priv->lock);

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

2798
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
2799
              args->data.data_len, args->nil);
2800 2801 2802 2803 2804 2805 2806 2807
    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)
2808
        goto authfail;
2809 2810

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2811
        VIR_ERROR(_("sasl step reply data too long %d"),
2812
                  (int)serveroutlen);
2813
        goto authfail;
2814 2815 2816 2817
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2818 2819
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2820 2821 2822 2823 2824 2825 2826
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2827
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2828
    if (err == VIR_NET_SASL_CONTINUE) {
2829 2830
        ret->complete = 0;
    } else {
2831
        /* Check username whitelist ACL */
2832
        if ((err = remoteSASLFinish(client)) < 0) {
2833 2834 2835 2836 2837
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2838

2839 2840 2841
        ret->complete = 1;
    }

2842
    virMutexUnlock(&priv->lock);
2843
    return 0;
2844 2845

authfail:
2846 2847 2848
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2849 2850 2851
    goto error;

authdeny:
2852
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2853 2854 2855
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2856 2857
    goto error;

2858
error:
2859
    virObjectUnref(priv->sasl);
2860 2861
    priv->sasl = NULL;
    virResetLastError();
2862 2863
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2864 2865 2866
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2867 2868
    return -1;
}
2869 2870 2871 2872
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2873
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2874 2875 2876 2877
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
2878 2879
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2880 2881 2882 2883 2884 2885
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
2886
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2887 2888 2889 2890 2891
                            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");
2892 2893
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2894 2895 2896 2897 2898 2899
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2900
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2901 2902 2903 2904 2905
                           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");
2906 2907
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2908 2909 2910 2911
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
2912 2913 2914



2915
#if WITH_POLKIT1
2916
static int
2917 2918
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
                         virNetServerClientPtr client,
2919
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2920
                         virNetMessageErrorPtr rerr,
2921
                         remote_auth_polkit_ret *ret)
2922
{
2923
    pid_t callerPid = -1;
2924
    gid_t callerGid = -1;
2925
    uid_t callerUid = -1;
2926
    unsigned long long timestamp;
2927 2928
    const char *action;
    int status = -1;
2929
    char *ident = NULL;
2930
    bool authdismissed = 0;
2931
    char *pkout = NULL;
2932 2933
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2934
    virCommandPtr cmd = NULL;
2935
# ifndef PKCHECK_SUPPORTS_UID
2936
    static bool polkitInsecureWarned;
2937
# endif
2938

2939 2940
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
2941 2942 2943
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

2944
    cmd = virCommandNewArgList(PKCHECK_PATH, "--action-id", action, NULL);
2945
    virCommandSetOutputBuffer(cmd, &pkout);
2946
    virCommandSetErrorBuffer(cmd, &pkout);
2947

2948 2949
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2950
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2951 2952 2953
        goto authfail;
    }

2954
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2955
                                          &callerPid, &timestamp) < 0) {
2956 2957 2958
        goto authfail;
    }

2959 2960 2961 2962 2963 2964
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

2965 2966
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2967

2968
    virCommandAddArg(cmd, "--process");
2969

2970
# ifdef PKCHECK_SUPPORTS_UID
2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
    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);
2982
# endif
2983

2984
    virCommandAddArg(cmd, "--allow-user-interaction");
2985

2986
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
2987
                    (long long) callerPid, callerUid) < 0)
2988 2989
        goto authfail;

2990
    if (virCommandRun(cmd, &status) < 0)
2991
        goto authfail;
2992

2993
    authdismissed = (pkout && strstr(pkout, "dismissed=true"));
2994
    if (status != 0) {
2995
        char *tmp = virProcessTranslateStatus(status);
2996 2997
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d: %s"),
                  action, (long long) callerPid, callerUid, NULLSTR(tmp));
2998
        VIR_FREE(tmp);
2999
        goto authdeny;
3000
    }
3001 3002 3003
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3004 3005
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
3006 3007
    ret->complete = 1;

3008
    virNetServerClientSetAuth(client, 0);
3009
    virMutexUnlock(&priv->lock);
3010
    virCommandFree(cmd);
E
Eric Blake 已提交
3011
    VIR_FREE(pkout);
3012
    VIR_FREE(ident);
3013

3014 3015
    return 0;

3016
error:
3017 3018
    virCommandFree(cmd);
    VIR_FREE(ident);
3019
    virResetLastError();
3020

3021
    if (authdismissed) {
3022 3023
        virReportError(VIR_ERR_AUTH_CANCELLED, "%s",
                       _("authentication cancelled by user"));
3024 3025
    } else if (pkout && *pkout) {
        virReportError(VIR_ERR_AUTH_FAILED, _("polkit: %s"), pkout);
3026
    } else {
3027
        virReportError(VIR_ERR_AUTH_FAILED, "%s", _("authentication failed"));
3028
    }
3029 3030

    VIR_FREE(pkout);
3031 3032 3033 3034
    virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return -1;

3035
authfail:
3036 3037 3038
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3039 3040 3041
    goto error;

authdeny:
3042 3043
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
3044
          client, REMOTE_AUTH_POLKIT, ident);
3045
    goto error;
3046
}
3047
#elif WITH_POLKIT0
3048
static int
3049
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
3050
                         virNetServerClientPtr client,
3051
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3052
                         virNetMessageErrorPtr rerr,
3053
                         remote_auth_polkit_ret *ret)
3054 3055
{
    pid_t callerPid;
3056
    gid_t callerGid;
3057
    uid_t callerUid;
3058 3059 3060 3061 3062 3063
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
3064
    const char *action;
3065
    char *ident = NULL;
3066 3067
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3068
    DBusConnection *sysbus;
J
Jim Fehlig 已提交
3069
    unsigned long long timestamp;
3070

J
Jim Fehlig 已提交
3071
    virMutexLock(&priv->lock);
3072

J
Jim Fehlig 已提交
3073
    action = virNetServerClientGetReadonly(client) ?
3074 3075
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
3076

3077
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
J
Jim Fehlig 已提交
3078
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3079
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3080
        goto authfail;
3081 3082
    }

3083
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
J
Jim Fehlig 已提交
3084
                                          &callerPid, &timestamp) < 0) {
3085
        VIR_ERROR(_("cannot get peer socket identity"));
3086
        goto authfail;
3087 3088
    }

3089
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
3090
                    (long long) callerPid, callerUid) < 0)
3091 3092
        goto authfail;

3093 3094 3095
    if (!(sysbus = virDBusGetSystemBus()))
        goto authfail;

3096 3097
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3098
    dbus_error_init(&err);
3099
    if (!(pkcaller = polkit_caller_new_from_pid(sysbus,
3100
                                                callerPid, &err))) {
3101
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
3102
        dbus_error_free(&err);
3103
        goto authfail;
3104
    }
3105

3106
    if (!(pkaction = polkit_action_new())) {
3107
        char ebuf[1024];
3108
        VIR_ERROR(_("Failed to create polkit action %s"),
3109
                  virStrerror(errno, ebuf, sizeof(ebuf)));
3110
        polkit_caller_unref(pkcaller);
3111
        goto authfail;
3112 3113 3114 3115 3116
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
3117
        char ebuf[1024];
3118
        VIR_ERROR(_("Failed to create polkit context %s"),
3119
                  (pkerr ? polkit_error_get_error_message(pkerr)
3120
                   : virStrerror(errno, ebuf, sizeof(ebuf))));
3121 3122 3123 3124 3125
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
3126
        goto authfail;
3127
    }
3128

3129
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
3130 3131 3132 3133 3134 3135
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
3136 3137 3138
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
3139
        goto authfail;
3140
    }
3141
# else
3142 3143 3144
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
3145
# endif
3146 3147 3148 3149
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
3150 3151
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d, result: %s"),
                  action, (long long) callerPid, callerUid,
3152
                  polkit_result_to_string_representation(pkresult));
3153
        goto authdeny;
3154
    }
3155 3156 3157
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3158 3159
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d, result %s",
             action, (long long) callerPid, callerUid,
3160 3161
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
3162

3163
    virNetServerClientSetAuth(client, 0);
J
Jim Fehlig 已提交
3164
    virMutexUnlock(&priv->lock);
3165
    VIR_FREE(ident);
3166
    return 0;
3167

3168
error:
3169
    VIR_FREE(ident);
3170
    virResetLastError();
3171 3172
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3173
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3174
    virMutexUnlock(&priv->lock);
3175 3176
    return -1;

3177
authfail:
3178 3179 3180
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3181
    goto error;
3182

3183
authdeny:
3184 3185 3186
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3187
    goto error;
3188 3189
}

3190
#else /* !WITH_POLKIT0 & !HAVE_POLKIT1*/
3191 3192

static int
3193
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
3194
                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
3195
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3196
                         virNetMessageErrorPtr rerr,
3197
                         remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
3198
{
3199
    VIR_ERROR(_("client tried unsupported PolicyKit init request"));
3200 3201
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3202
    virNetMessageSaveError(rerr);
3203 3204
    return -1;
}
3205
#endif /* WITH_POLKIT1 */
3206 3207


3208 3209 3210
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3211

3212
static int
3213 3214
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3215
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3216
                                  virNetMessageErrorPtr rerr,
3217 3218
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3219
{
3220 3221
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3222
    int rv = -1;
3223 3224
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3225

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

3231
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3232 3233
        goto cleanup;

3234 3235 3236 3237 3238 3239 3240
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
3241
        if (VIR_ALLOC(parent_p) < 0)
3242
            goto cleanup;
3243
        if (VIR_STRDUP(*parent_p, parent) < 0) {
3244 3245 3246 3247 3248 3249
            VIR_FREE(parent_p);
            goto cleanup;
        }
        ret->parent = parent_p;
    }

3250 3251 3252 3253
    rv = 0;

cleanup:
    if (rv < 0)
3254
        virNetMessageSaveError(rerr);
3255 3256
    if (dev)
        virNodeDeviceFree(dev);
3257
    return rv;
3258 3259
}

3260 3261 3262 3263 3264

/***************************
 * Register / deregister events
 ***************************/
static int
3265
remoteDispatchConnectDomainEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
3266
                                         virNetServerClientPtr client,
3267 3268 3269
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                         remote_connect_domain_event_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
3270
{
3271
    int callbackID;
3272
    int rv = -1;
3273 3274
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3275 3276
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3277

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

3283 3284
    virMutexLock(&priv->lock);

3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295
    /* 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;
3296
    callback->legacy = true;
3297 3298 3299 3300
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3301
        goto cleanup;
O
Osier Yang 已提交
3302

3303
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3304 3305 3306
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
3307 3308 3309 3310 3311
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3312
        goto cleanup;
3313
    }
O
Osier Yang 已提交
3314

3315
    ref->callbackID = callbackID;
3316

3317 3318 3319
    rv = 0;

cleanup:
3320
    VIR_FREE(callback);
3321
    if (rv < 0)
3322 3323
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3324
    return rv;
O
Osier Yang 已提交
3325 3326
}

3327
static int
3328
remoteDispatchConnectDomainEventDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
3329
                                           virNetServerClientPtr client,
3330 3331 3332
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                           remote_connect_domain_event_deregister_ret *ret ATTRIBUTE_UNUSED)
3333
{
3334
    int callbackID = -1;
3335
    int rv = -1;
3336
    size_t i;
3337 3338
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3339

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

3345 3346
    virMutexLock(&priv->lock);

3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357
    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);
3358
        goto cleanup;
3359
    }
3360

3361
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3362
        goto cleanup;
3363

3364 3365
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3366

3367 3368 3369
    rv = 0;

cleanup:
3370
    if (rv < 0)
3371 3372
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3373 3374 3375 3376
    return rv;
}

static void
3377
remoteDispatchObjectEventSend(virNetServerClientPtr client,
3378
                              virNetServerProgramPtr program,
3379 3380 3381 3382
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3383
    virNetMessagePtr msg;
3384

3385
    if (!(msg = virNetMessageNew(false)))
3386
        goto cleanup;
3387

3388 3389 3390 3391 3392 3393
    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;
3394

3395
    if (virNetMessageEncodeHeader(msg) < 0)
3396 3397
        goto cleanup;

3398 3399
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3400

3401 3402
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3403

3404
    xdr_free(proc, data);
3405 3406 3407
    return;

cleanup:
3408
    virNetMessageFree(msg);
3409
    xdr_free(proc, data);
3410 3411
}

3412
static int
3413 3414
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3415
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3416
                             virNetMessageErrorPtr rerr,
3417 3418
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3419
{
3420 3421 3422
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3423
    int rv = -1;
3424 3425
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3426

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

3432
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3433
        goto cleanup;
3434

3435
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3436
        goto cleanup;
3437

3438 3439 3440
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3441 3442 3443 3444
    rv = 0;

cleanup:
    if (rv < 0)
3445
        virNetMessageSaveError(rerr);
3446 3447
    if (secret)
        virSecretFree(secret);
3448
    return rv;
3449 3450
}

3451
static int
3452 3453
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3454
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3455
                             virNetMessageErrorPtr rerr,
3456 3457 3458 3459 3460
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3461 3462
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3463

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

3469
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3470 3471 3472 3473 3474 3475 3476 3477 3478
        goto cleanup;

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

    rv = 0;

cleanup:
    if (rv < 0)
3479
        virNetMessageSaveError(rerr);
3480 3481 3482 3483 3484
    if (dom)
        virDomainFree(dom);
    return rv;
}

3485 3486 3487 3488 3489 3490

/* 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.  */
3491
static int
3492
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3493
                                            virNetServerClientPtr client,
3494 3495 3496
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            remote_connect_domain_event_register_any_args *args)
3497 3498
{
    int callbackID;
3499
    int rv = -1;
3500 3501
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
3502 3503
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3504

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

3510 3511
    virMutexLock(&priv->lock);

3512 3513 3514 3515
    /* 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 ||
3516
        args->eventID < 0) {
3517 3518
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"),
                       args->eventID);
3519
        goto cleanup;
3520 3521
    }

3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
    /* 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;
3533
    callback->legacy = true;
3534 3535 3536 3537
    ref = callback;
    if (VIR_APPEND_ELEMENT(priv->domainEventCallbacks,
                           priv->ndomainEventCallbacks,
                           callback) < 0)
3538
        goto cleanup;
3539

3540
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3541 3542 3543
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3544 3545 3546 3547 3548
                                                       ref,
                                                       remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->domainEventCallbacks,
                     priv->ndomainEventCallbacks, 1);
        callback = ref;
3549
        goto cleanup;
3550
    }
3551

3552
    ref->callbackID = callbackID;
3553

3554 3555 3556
    rv = 0;

cleanup:
3557
    VIR_FREE(callback);
3558
    if (rv < 0)
3559 3560
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3561
    return rv;
3562 3563 3564
}


3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
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;

    /* FIXME: support all domain events */
    if (args->eventID != VIR_DOMAIN_EVENT_ID_LIFECYCLE) {
        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;

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


3644
static int
3645
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
3646
                                              virNetServerClientPtr client,
3647 3648 3649
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              remote_connect_domain_event_deregister_any_args *args)
3650 3651
{
    int callbackID = -1;
3652
    int rv = -1;
3653
    size_t i;
3654 3655
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3656

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

3662 3663
    virMutexLock(&priv->lock);

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

3674 3675 3676 3677 3678 3679
    for (i = 0; i < priv->ndomainEventCallbacks; i++) {
        if (priv->domainEventCallbacks[i]->eventID == args->eventID) {
            callbackID = priv->domainEventCallbacks[i]->callbackID;
            break;
        }
    }
3680
    if (callbackID < 0) {
3681 3682
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("domain event %d not registered"), args->eventID);
3683
        goto cleanup;
3684 3685
    }

3686
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3687
        goto cleanup;
3688

3689 3690
    VIR_DELETE_ELEMENT(priv->domainEventCallbacks, i,
                       priv->ndomainEventCallbacks);
3691

3692 3693 3694 3695
    rv = 0;

cleanup:
    if (rv < 0)
3696 3697
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3698
    return rv;
3699 3700
}

3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747

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;

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


C
Chris Lalancette 已提交
3748
static int
3749 3750 3751 3752 3753 3754
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 已提交
3755
{
3756
    virDomainPtr dom = NULL;
3757
    int rv = -1;
3758 3759
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
3760

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

3766
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3767
        goto cleanup;
C
Chris Lalancette 已提交
3768

3769
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
3770
                                    args->flags) < 0)
3771
        goto cleanup;
C
Chris Lalancette 已提交
3772

3773
    rv = 0;
C
Chris Lalancette 已提交
3774

3775 3776
cleanup:
    if (rv < 0)
3777
        virNetMessageSaveError(rerr);
3778 3779
    if (dom)
        virDomainFree(dom);
3780
    return rv;
C
Chris Lalancette 已提交
3781 3782
}

3783

3784
static int
3785 3786
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3787
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3788
                                  virNetMessageErrorPtr rerr,
3789 3790 3791 3792 3793 3794
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
3795
    char *xmlin;
3796 3797 3798
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3799 3800
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3801

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

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

3810
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3811 3812
    dname = args->dname == NULL ? NULL : *args->dname;

3813
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828
                                       &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;

cleanup:
    if (rv < 0)
3829
        virNetMessageSaveError(rerr);
3830 3831 3832 3833 3834 3835 3836
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3837 3838
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3839
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3840
                                    virNetMessageErrorPtr rerr,
3841 3842 3843 3844 3845 3846 3847 3848 3849
                                    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;
3850 3851
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3852

3853
    if (!priv->conn) {
3854
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3855 3856 3857 3858 3859 3860 3861
        goto cleanup;
    }

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

    /* Wacky world of XDR ... */
3862
    if (VIR_ALLOC(uri_out) < 0)
3863 3864
        goto cleanup;

3865
    if (virDomainMigratePrepare3(priv->conn,
3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884
                                 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;

cleanup:
    if (rv < 0) {
3885
        virNetMessageSaveError(rerr);
3886 3887 3888 3889 3890
        VIR_FREE(uri_out);
    }
    return rv;
}

3891

3892
static int
3893 3894
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3895
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3896
                                    virNetMessageErrorPtr rerr,
3897 3898 3899 3900
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
3901
    char *xmlin;
3902
    char *dname;
3903 3904
    char *uri;
    char *dconnuri;
3905 3906 3907
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3908 3909
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3910

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

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

3919
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3920
    dname = args->dname == NULL ? NULL : *args->dname;
3921 3922
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
3923

3924
    if (virDomainMigratePerform3(dom, xmlin,
3925 3926 3927
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
3928
                                 dconnuri, uri,
3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940
                                 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;

cleanup:
    if (rv < 0)
3941
        virNetMessageSaveError(rerr);
3942 3943 3944 3945 3946 3947 3948
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3949 3950
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
3951
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
3952
                                   virNetMessageErrorPtr rerr,
3953 3954 3955 3956 3957 3958
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
3959 3960
    char *uri;
    char *dconnuri;
3961
    int rv = -1;
3962 3963
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3964

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

3970 3971 3972
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

3973
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
3974 3975 3976 3977 3978 3979
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
3980 3981
        goto cleanup;

3982
    make_nonnull_domain(&ret->dom, dom);
3983 3984 3985 3986 3987 3988 3989 3990 3991 3992

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

    rv = 0;

cleanup:
    if (rv < 0) {
3993
        virNetMessageSaveError(rerr);
3994 3995 3996 3997 3998 3999 4000 4001 4002
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
4003 4004
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
4005
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
4006 4007
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
4008 4009 4010
{
    virDomainPtr dom = NULL;
    int rv = -1;
4011 4012
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
4013

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

4019
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
        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;

cleanup:
    if (rv < 0)
4032
        virNetMessageSaveError(rerr);
4033 4034 4035 4036 4037 4038
    if (dom)
        virDomainFree(dom);
    return rv;
}


4039 4040 4041 4042 4043 4044
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)
4045 4046 4047 4048 4049 4050
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

4051 4052 4053 4054 4055 4056 4057 4058 4059 4060
    /* 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;
    }

4061
    if (!priv->conn) {
4062
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4063 4064 4065 4066 4067
        goto cleanup;
    }

    switch (args->feature) {
    case VIR_DRV_FEATURE_FD_PASSING:
4068
    case VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK:
4069 4070 4071 4072
        supported = 1;
        break;

    default:
4073
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
4074 4075 4076 4077
            goto cleanup;
        break;
    }

4078
done:
4079 4080 4081 4082 4083 4084 4085 4086 4087 4088
    ret->supported = supported;
    rv = 0;

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


4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102
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) {
4103
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129
        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;

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

4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140
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;
4141
    int nparams = 0;
4142 4143 4144 4145 4146 4147
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    flags = args->flags;

4154
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
4155
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4156 4157
        goto cleanup;
    }
4158
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4159
        goto cleanup;
4160
    nparams = args->nparams;
4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187

    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;

success:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
4188
    virTypedParamsFree(params, nparams);
4189 4190 4191 4192
    if (dom)
        virDomainFree(dom);
    return rv;
}
4193

4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209
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) {
4210
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4211 4212 4213 4214
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
4215
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
4216 4217 4218
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
4219
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
4220 4221 4222 4223
        goto cleanup;
    }

    if (args->nparams > 0 &&
4224
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247
        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;

success:
    rv = 0;
    ret->nparams = percpu_len;
4248
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
4249
        size_t i;
4250 4251 4252 4253 4254 4255

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

cleanup:
    if (rv < 0)
         virNetMessageSaveError(rerr);
4260
    virTypedParamsFree(params, args->ncpus * args->nparams);
4261 4262 4263 4264 4265
    if (dom)
        virDomainFree(dom);
    return rv;
}

4266 4267 4268 4269 4270 4271 4272
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)
4273 4274 4275 4276
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
4277
    int len = 0;
4278 4279 4280 4281
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
4290 4291
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
4292 4293 4294 4295
        goto cleanup;
    }

    if (args->maxerrors &&
4296
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318
        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;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    if (errors) {
4319
        size_t i;
4320 4321 4322 4323 4324 4325 4326
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

4327 4328 4329 4330 4331 4332 4333 4334 4335 4336
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;
4337
    size_t i;
4338 4339 4340 4341 4342
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

    if (!priv->conn) {
4343
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354
        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;

4355 4356 4357 4358 4359 4360 4361
    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;
    }

4362
    if (snaps && nsnaps) {
4363
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401
            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;

cleanup:
    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;
4402
    size_t i;
4403 4404 4405 4406 4407 4408
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;
    virDomainSnapshotPtr snapshot = NULL;

    if (!priv->conn) {
4409
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423
        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;

4424 4425 4426 4427 4428 4429 4430
    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;
    }

4431
    if (snaps && nsnaps) {
4432
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462
            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;

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

4463 4464 4465 4466 4467 4468 4469 4470 4471 4472
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;
4473
    size_t i;
4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486
    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;

4487 4488 4489 4490 4491 4492 4493
    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;
    }

4494
    if (pools && npools) {
4495
        if (VIR_ALLOC_N(ret->pools.pools_val, npools) < 0)
4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521
            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;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (pools) {
        for (i = 0; i < npools; i++)
            virStoragePoolFree(pools[i]);
        VIR_FREE(pools);
    }
    return rv;
}

4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532
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;
4533
    size_t i;
4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549
    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;

4550 4551 4552 4553 4554 4555 4556
    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;
    }

4557
    if (vols && nvols) {
4558
        if (VIR_ALLOC_N(ret->vols.vols_val, nvols) < 0)
4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581
            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;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (vols) {
        for (i = 0; i < nvols; i++)
            virStorageVolFree(vols[i]);
        VIR_FREE(vols);
    }
4582 4583
    if (pool)
        virStoragePoolFree(pool);
4584 4585 4586
    return rv;
}

4587 4588 4589 4590 4591 4592 4593 4594 4595 4596
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;
4597
    size_t i;
4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610
    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;

4611 4612 4613 4614 4615 4616 4617
    if (nnets > REMOTE_NETWORK_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many networks '%d' for limit '%d'"),
                       nnets, REMOTE_NETWORK_LIST_MAX);
        goto cleanup;
    }

4618
    if (nets && nnets) {
4619
        if (VIR_ALLOC_N(ret->nets.nets_val, nnets) < 0)
4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645
            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;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (nets) {
        for (i = 0; i < nnets; i++)
            virNetworkFree(nets[i]);
        VIR_FREE(nets);
    }
    return rv;
}

4646 4647 4648 4649 4650 4651 4652 4653 4654 4655
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;
4656
    size_t i;
4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669
    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;

4670 4671 4672 4673 4674 4675 4676
    if (nifaces > REMOTE_INTERFACE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many interfaces '%d' for limit '%d'"),
                       nifaces, REMOTE_INTERFACE_LIST_MAX);
        goto cleanup;
    }

4677
    if (ifaces && nifaces) {
4678
        if (VIR_ALLOC_N(ret->ifaces.ifaces_val, nifaces) < 0)
4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704
            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;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (ifaces) {
        for (i = 0; i < nifaces; i++)
            virInterfaceFree(ifaces[i]);
        VIR_FREE(ifaces);
    }
    return rv;
}

4705 4706 4707 4708 4709 4710 4711 4712 4713 4714
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;
4715
    size_t i;
4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728
    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;

4729 4730 4731 4732 4733 4734 4735
    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;
    }

4736
    if (devices && ndevices) {
4737
        if (VIR_ALLOC_N(ret->devices.devices_val, ndevices) < 0)
4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762
            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;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (devices) {
        for (i = 0; i < ndevices; i++)
            virNodeDeviceFree(devices[i]);
        VIR_FREE(devices);
    }
    return rv;
}
4763

4764 4765 4766 4767 4768 4769 4770 4771 4772 4773
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;
4774
    size_t i;
4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787
    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;

4788 4789 4790 4791 4792 4793 4794
    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;
    }

4795
    if (filters && nfilters) {
4796
        if (VIR_ALLOC_N(ret->filters.filters_val, nfilters) < 0)
4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822
            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;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (filters) {
        for (i = 0; i < nfilters; i++)
            virNWFilterFree(filters[i]);
        VIR_FREE(filters);
    }
    return rv;
}

4823 4824 4825 4826 4827 4828 4829 4830 4831 4832
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;
4833
    size_t i;
4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846
    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;

4847 4848 4849 4850 4851 4852 4853
    if (nsecrets > REMOTE_SECRET_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many secrets '%d' for limit '%d'"),
                       nsecrets, REMOTE_SECRET_LIST_MAX);
        goto cleanup;
    }

4854
    if (secrets && nsecrets) {
4855
        if (VIR_ALLOC_N(ret->secrets.secrets_val, nsecrets) < 0)
4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881
            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;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (secrets) {
        for (i = 0; i < nsecrets; i++)
            virSecretFree(secrets[i]);
        VIR_FREE(secrets);
    }
    return rv;
}

4882 4883 4884 4885 4886 4887 4888 4889 4890
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;
4891
    int nparams = 0;
4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903
    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;

4904
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
4905 4906 4907
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
4908
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4909
        goto cleanup;
4910
    nparams = args->nparams;
4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934

    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;

success:
    rv = 0;

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

4939 4940 4941 4942 4943 4944 4945 4946 4947
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;
4948
    unsigned int online = 0;
4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961
    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;

4962 4963
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
4964 4965 4966 4967
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
4968
    if (args->need_map) {
4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985
        ret->cpumap.cpumap_len = VIR_CPU_MAPLEN(cpunum);
        ret->cpumap.cpumap_val = (char *) cpumap;
        cpumap = NULL;
    }

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

    rv = 0;

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

4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018
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
     */
5019
    for (i = 0; i < msg->nfds; i++)
5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

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

    rv = 1;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
5032 5033
    if (dom)
        virDomainFree(dom);
5034 5035 5036
    return rv;
}

5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063
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;

5064 5065 5066 5067 5068 5069 5070
    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;
    }

5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
                                       &ret->params.params_len,
                                       0) < 0)
        goto cleanup;

    rv = 0;

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

5088
static int
5089 5090 5091 5092 5093 5094
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)
5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110
{
    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;
    }

5111 5112 5113 5114 5115 5116 5117
    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;
    }

5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146
    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;

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

static int
5147 5148 5149 5150 5151 5152
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)
5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167
{
    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;
    }

5168 5169 5170 5171 5172 5173 5174
    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;
    }

5175 5176 5177 5178 5179 5180
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    /* Wacky world of XDR ... */
5181
    if (VIR_ALLOC(uri_out) < 0)
5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206
        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;

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

static int
5207 5208 5209 5210 5211 5212
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)
5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228
{
    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;
    }

5229 5230 5231 5232 5233 5234 5235
    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;
    }

5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276
    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;

cleanup:
    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
5277 5278 5279 5280 5281 5282
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)
5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298
{
    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;
    }

5299 5300 5301 5302 5303 5304 5305
    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;
    }

5306 5307 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 5335 5336 5337 5338
    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;

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


static int
5339 5340 5341 5342 5343 5344
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)
5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359
{
    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;
    }

5360 5361 5362 5363 5364 5365 5366
    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;
    }

5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399
    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;

cleanup:
    virTypedParamsFree(params, nparams);
    if (rv < 0) {
        virNetMessageSaveError(rerr);
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
5400 5401 5402 5403 5404
remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_confirm3_params_args *args)
5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417
{
    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;
    }

5418 5419 5420 5421 5422 5423 5424
    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;
    }

5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450
    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;

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


5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502
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;

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


5503 5504 5505 5506 5507 5508 5509
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)
5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552
{
    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;

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


5553 5554 5555 5556 5557 5558
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)
5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604
{
    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;

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


5605 5606
static int
remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5607
                                             virNetServerClientPtr client,
5608 5609 5610
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                             remote_connect_network_event_register_any_args *args,
5611
                                             remote_connect_network_event_register_any_ret *ret)
5612 5613 5614
{
    int callbackID;
    int rv = -1;
5615 5616
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
5617 5618
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
5619
    virNetworkPtr net = NULL;
5620 5621 5622 5623 5624 5625 5626 5627

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

    virMutexLock(&priv->lock);

5628 5629 5630 5631
    if (args->net &&
        !(net = get_nonnull_network(priv->conn, *args->net)))
        goto cleanup;

5632 5633 5634
    if (args->eventID >= VIR_NETWORK_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network event ID %d"), args->eventID);
5635 5636 5637
        goto cleanup;
    }

5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652
    /* 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)
5653 5654 5655
        goto cleanup;

    if ((callbackID = virConnectNetworkEventRegisterAny(priv->conn,
5656
                                                        net,
5657 5658
                                                        args->eventID,
                                                        networkEventCallbacks[args->eventID],
5659 5660 5661 5662 5663
                                                        ref,
                                                        remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->networkEventCallbacks,
                     priv->nnetworkEventCallbacks, 1);
        callback = ref;
5664
        goto cleanup;
5665
    }
5666

5667
    ref->callbackID = callbackID;
5668
    ret->callbackID = callbackID;
5669 5670 5671 5672

    rv = 0;

cleanup:
5673
    VIR_FREE(callback);
5674 5675
    if (rv < 0)
        virNetMessageSaveError(rerr);
5676 5677
    if (net)
        virNetworkFree(net);
5678 5679 5680 5681 5682 5683 5684
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5685
                                               virNetServerClientPtr client,
5686 5687
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
5688
                                               remote_connect_network_event_deregister_any_args *args)
5689 5690
{
    int rv = -1;
5691
    size_t i;
5692 5693 5694 5695 5696 5697 5698 5699 5700 5701
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    virMutexLock(&priv->lock);

5702
    for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
5703
        if (priv->networkEventCallbacks[i]->callbackID == args->callbackID)
5704 5705
            break;
    }
5706
    if (i == priv->nnetworkEventCallbacks) {
5707
        virReportError(VIR_ERR_INTERNAL_ERROR,
5708 5709
                       _("network event callback %d not registered"),
                       args->callbackID);
5710 5711 5712
        goto cleanup;
    }

5713
    if (virConnectNetworkEventDeregisterAny(priv->conn, args->callbackID) < 0)
5714 5715
        goto cleanup;

5716 5717
    VIR_DELETE_ELEMENT(priv->networkEventCallbacks, i,
                       priv->nnetworkEventCallbacks);
5718 5719 5720 5721 5722 5723 5724 5725 5726 5727

    rv = 0;

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

5728

5729 5730 5731 5732 5733 5734 5735 5736 5737
/*----- 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
5738
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
5739 5740
{
    virDomainPtr dom;
5741
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
5742 5743 5744 5745 5746 5747 5748 5749
    /* 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
5750
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
5751
{
5752
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
5753 5754
}

D
Daniel Veillard 已提交
5755
static virInterfacePtr
5756
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
5757
{
5758
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
5759 5760
}

5761
static virStoragePoolPtr
5762
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
5763
{
5764 5765
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
5766 5767 5768
}

static virStorageVolPtr
5769
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
5770 5771
{
    virStorageVolPtr ret;
5772 5773
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
5774 5775 5776
    return ret;
}

5777
static virSecretPtr
5778
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
5779
{
5780
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
5781 5782
}

5783
static virNWFilterPtr
5784
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
5785
{
5786
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
5787 5788
}

C
Chris Lalancette 已提交
5789
static virDomainSnapshotPtr
5790
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
5791
{
5792
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
5793 5794
}

5795 5796
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
5797
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
5798 5799
{
    dom_dst->id = dom_src->id;
5800
    ignore_value(VIR_STRDUP_QUIET(dom_dst->name, dom_src->name));
5801
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
5802 5803 5804
}

static void
5805
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
5806
{
5807
    ignore_value(VIR_STRDUP_QUIET(net_dst->name, net_src->name));
5808
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
5809 5810
}

D
Daniel Veillard 已提交
5811
static void
5812 5813
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
5814
{
5815 5816
    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 已提交
5817 5818
}

5819
static void
5820
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
5821
{
5822
    ignore_value(VIR_STRDUP_QUIET(pool_dst->name, pool_src->name));
5823
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
5824 5825 5826
}

static void
5827
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
5828
{
5829 5830 5831
    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));
5832
}
5833 5834

static void
5835
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
5836
{
5837
    ignore_value(VIR_STRDUP_QUIET(dev_dst->name, dev_src->name));
5838
}
5839 5840

static void
5841
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
5842
{
5843
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
5844
    secret_dst->usageType = secret_src->usageType;
5845
    ignore_value(VIR_STRDUP_QUIET(secret_dst->usageID, secret_src->usageID));
5846
}
5847 5848

static void
5849
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
5850
{
5851
    ignore_value(VIR_STRDUP_QUIET(nwfilter_dst->name, nwfilter_src->name));
5852
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
5853
}
C
Chris Lalancette 已提交
5854 5855

static void
5856
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
5857
{
5858
    ignore_value(VIR_STRDUP_QUIET(snapshot_dst->name, snapshot_src->name));
5859
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
5860
}
5861 5862 5863 5864 5865 5866 5867 5868

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;
5869
    size_t i = 0;
5870

5871
    if (VIR_ALLOC_N(val, nerrors) < 0)
5872
        goto error;
5873 5874

    for (i = 0; i < nerrors; i++) {
5875 5876
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
5877 5878 5879 5880 5881 5882 5883 5884
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

5885
error:
5886
    if (val) {
5887
        size_t j;
5888 5889 5890 5891 5892 5893
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}