remote.c 179.0 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 82 83
struct daemonClientEventCallback {
    virNetServerClientPtr client;
    int eventID;
    int callbackID;
};

84 85 86 87 88 89 90
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);
91
static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot);
92 93 94 95 96 97 98 99 100
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);
101

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

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

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


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

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

133 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

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)
194
{
195
    virNetServerClientPtr client = opaque;
196
    remote_domain_event_lifecycle_msg data;
197

198
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
199 200
        return -1;

201
    VIR_DEBUG("Relaying domain lifecycle event %d %d", event, detail);
202

203
    /* build return data */
204
    memset(&data, 0, sizeof(data));
205
    make_nonnull_domain(&data.dom, dom);
206 207
    data.event = event;
    data.detail = detail;
208

209
    remoteDispatchObjectEventSend(client, remoteProgram,
210 211
                                  REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_domain_event_lifecycle_msg, &data);
212

213 214
    return 0;
}
215

216 217 218 219
static int
remoteRelayDomainEventReboot(virConnectPtr conn,
                             virDomainPtr dom,
                             void *opaque)
220
{
221
    virNetServerClientPtr client = opaque;
222 223
    remote_domain_event_reboot_msg data;

224
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
225 226
        return -1;

227
    VIR_DEBUG("Relaying domain reboot event %s %d", dom->name, dom->id);
228 229

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

233
    remoteDispatchObjectEventSend(client, remoteProgram,
234 235
                                  REMOTE_PROC_DOMAIN_EVENT_REBOOT,
                                  (xdrproc_t)xdr_remote_domain_event_reboot_msg, &data);
236 237 238 239

    return 0;
}

240

241 242 243 244 245
static int
remoteRelayDomainEventRTCChange(virConnectPtr conn,
                                virDomainPtr dom,
                                long long offset,
                                void *opaque)
246
{
247
    virNetServerClientPtr client = opaque;
248 249
    remote_domain_event_rtc_change_msg data;

250
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
251 252
        return -1;

253
    VIR_DEBUG("Relaying domain rtc change event %s %d %lld", dom->name, dom->id, offset);
254 255

    /* build return data */
256
    memset(&data, 0, sizeof(data));
257
    make_nonnull_domain(&data.dom, dom);
258 259
    data.offset = offset;

260
    remoteDispatchObjectEventSend(client, remoteProgram,
261 262
                                  REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_rtc_change_msg, &data);
263 264 265 266 267

    return 0;
}


268 269 270 271 272
static int
remoteRelayDomainEventWatchdog(virConnectPtr conn,
                               virDomainPtr dom,
                               int action,
                               void *opaque)
273
{
274
    virNetServerClientPtr client = opaque;
275 276
    remote_domain_event_watchdog_msg data;

277
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
278 279
        return -1;

280
    VIR_DEBUG("Relaying domain watchdog event %s %d %d", dom->name, dom->id, action);
281 282

    /* build return data */
283
    memset(&data, 0, sizeof(data));
284
    make_nonnull_domain(&data.dom, dom);
285 286
    data.action = action;

287
    remoteDispatchObjectEventSend(client, remoteProgram,
288 289
                                  REMOTE_PROC_DOMAIN_EVENT_WATCHDOG,
                                  (xdrproc_t)xdr_remote_domain_event_watchdog_msg, &data);
290 291 292 293 294

    return 0;
}


295 296 297 298 299 300 301
static int
remoteRelayDomainEventIOError(virConnectPtr conn,
                              virDomainPtr dom,
                              const char *srcPath,
                              const char *devAlias,
                              int action,
                              void *opaque)
302
{
303
    virNetServerClientPtr client = opaque;
304 305
    remote_domain_event_io_error_msg data;

306
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
307 308
        return -1;

309
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d", dom->name, dom->id, srcPath, devAlias, action);
310 311

    /* build return data */
312
    memset(&data, 0, sizeof(data));
313 314 315
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
316
    make_nonnull_domain(&data.dom, dom);
317 318
    data.action = action;

319
    remoteDispatchObjectEventSend(client, remoteProgram,
320 321
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_msg, &data);
322 323

    return 0;
324
error:
E
Eric Blake 已提交
325 326
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
327
    return -1;
328 329 330
}


331 332 333 334 335 336 337 338
static int
remoteRelayDomainEventIOErrorReason(virConnectPtr conn,
                                    virDomainPtr dom,
                                    const char *srcPath,
                                    const char *devAlias,
                                    int action,
                                    const char *reason,
                                    void *opaque)
339
{
340
    virNetServerClientPtr client = opaque;
341 342
    remote_domain_event_io_error_reason_msg data;

343
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
344 345
        return -1;

346 347
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s",
              dom->name, dom->id, srcPath, devAlias, action, reason);
348 349

    /* build return data */
350
    memset(&data, 0, sizeof(data));
351 352 353 354
    if (VIR_STRDUP(data.srcPath, srcPath) < 0 ||
        VIR_STRDUP(data.devAlias, devAlias) < 0 ||
        VIR_STRDUP(data.reason, reason) < 0)
        goto error;
355
    data.action = action;
356 357

    make_nonnull_domain(&data.dom, dom);
358

359
    remoteDispatchObjectEventSend(client, remoteProgram,
360 361
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_reason_msg, &data);
362 363

    return 0;
364

365
error:
E
Eric Blake 已提交
366 367 368
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
    VIR_FREE(data.reason);
369
    return -1;
370 371 372
}


373 374 375 376 377 378 379 380 381
static int
remoteRelayDomainEventGraphics(virConnectPtr conn,
                               virDomainPtr dom,
                               int phase,
                               virDomainEventGraphicsAddressPtr local,
                               virDomainEventGraphicsAddressPtr remote,
                               const char *authScheme,
                               virDomainEventGraphicsSubjectPtr subject,
                               void *opaque)
382
{
383
    virNetServerClientPtr client = opaque;
384
    remote_domain_event_graphics_msg data;
385
    size_t i;
386

387
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
388 389
        return -1;

390 391 392 393
    VIR_DEBUG("Relaying domain graphics event %s %d %d - %d %s %s  - %d %s %s - %s", dom->name, dom->id, phase,
              local->family, local->service, local->node,
              remote->family, remote->service, remote->node,
              authScheme);
394

395
    VIR_DEBUG("Subject %d", subject->nidentity);
396
    for (i = 0; i < subject->nidentity; i++) {
397
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
398 399 400
    }

    /* build return data */
401
    memset(&data, 0, sizeof(data));
402 403 404
    data.phase = phase;
    data.local.family = local->family;
    data.remote.family = remote->family;
405 406 407 408 409 410
    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;
411 412

    data.subject.subject_len = subject->nidentity;
413
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0)
414
        goto error;
415

416
    for (i = 0; i < data.subject.subject_len; i++) {
417 418 419
        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;
420
    }
421
    make_nonnull_domain(&data.dom, dom);
422

423
    remoteDispatchObjectEventSend(client, remoteProgram,
424 425
                                  REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
                                  (xdrproc_t)xdr_remote_domain_event_graphics_msg, &data);
426 427

    return 0;
428

429
error:
E
Eric Blake 已提交
430 431 432 433 434
    VIR_FREE(data.authScheme);
    VIR_FREE(data.local.node);
    VIR_FREE(data.local.service);
    VIR_FREE(data.remote.node);
    VIR_FREE(data.remote.service);
435
    if (data.subject.subject_val != NULL) {
436
        for (i = 0; i < data.subject.subject_len; i++) {
E
Eric Blake 已提交
437 438
            VIR_FREE(data.subject.subject_val[i].type);
            VIR_FREE(data.subject.subject_val[i].name);
439
        }
E
Eric Blake 已提交
440
        VIR_FREE(data.subject.subject_val);
441 442
    }
    return -1;
443 444
}

445 446 447 448 449 450 451
static int
remoteRelayDomainEventBlockJob(virConnectPtr conn,
                               virDomainPtr dom,
                               const char *path,
                               int type,
                               int status,
                               void *opaque)
452 453 454 455
{
    virNetServerClientPtr client = opaque;
    remote_domain_event_block_job_msg data;

456
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
457 458 459 460 461 462
        return -1;

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

    /* build return data */
463
    memset(&data, 0, sizeof(data));
464 465
    if (VIR_STRDUP(data.path, path) < 0)
        goto error;
466 467
    data.type = type;
    data.status = status;
468
    make_nonnull_domain(&data.dom, dom);
469

470
    remoteDispatchObjectEventSend(client, remoteProgram,
471 472 473 474
                                  REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB,
                                  (xdrproc_t)xdr_remote_domain_event_block_job_msg, &data);

    return 0;
475
error:
E
Eric Blake 已提交
476
    VIR_FREE(data.path);
477
    return -1;
478 479
}

480

481 482 483 484
static int
remoteRelayDomainEventControlError(virConnectPtr conn,
                                   virDomainPtr dom,
                                   void *opaque)
485
{
486
    virNetServerClientPtr client = opaque;
487 488
    remote_domain_event_control_error_msg data;

489
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
490 491 492 493 494
        return -1;

    VIR_DEBUG("Relaying domain control error %s %d", dom->name, dom->id);

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

498
    remoteDispatchObjectEventSend(client, remoteProgram,
499 500 501 502 503 504 505
                                  REMOTE_PROC_DOMAIN_EVENT_CONTROL_ERROR,
                                  (xdrproc_t)xdr_remote_domain_event_control_error_msg, &data);

    return 0;
}


506 507 508 509 510 511 512 513
static int
remoteRelayDomainEventDiskChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *oldSrcPath,
                                 const char *newSrcPath,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
514 515 516 517 518
{
    virNetServerClientPtr client = opaque;
    remote_domain_event_disk_change_msg data;
    char **oldSrcPath_p = NULL, **newSrcPath_p = NULL;

519
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
520 521 522 523 524 525
        return -1;

    VIR_DEBUG("Relaying domain %s %d disk change %s %s %s %d",
              dom->name, dom->id, oldSrcPath, newSrcPath, devAlias, reason);

    /* build return data */
526
    memset(&data, 0, sizeof(data));
527 528
    if (oldSrcPath &&
        ((VIR_ALLOC(oldSrcPath_p) < 0) ||
529
         VIR_STRDUP(*oldSrcPath_p, oldSrcPath) < 0))
530
        goto error;
531 532 533

    if (newSrcPath &&
        ((VIR_ALLOC(newSrcPath_p) < 0) ||
534
         VIR_STRDUP(*newSrcPath_p, newSrcPath) < 0))
535
        goto error;
536 537 538

    data.oldSrcPath = oldSrcPath_p;
    data.newSrcPath = newSrcPath_p;
539 540
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
        goto error;
541 542 543 544
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

545
    remoteDispatchObjectEventSend(client, remoteProgram,
546 547 548 549 550
                                  REMOTE_PROC_DOMAIN_EVENT_DISK_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_disk_change_msg, &data);

    return 0;

551
error:
M
Michal Privoznik 已提交
552 553
    VIR_FREE(oldSrcPath_p);
    VIR_FREE(newSrcPath_p);
554 555 556 557
    return -1;
}


558 559 560 561 562 563 564
static int
remoteRelayDomainEventTrayChange(virConnectPtr conn,
                                 virDomainPtr dom,
                                 const char *devAlias,
                                 int reason,
                                 void *opaque)
{
565 566 567
    virNetServerClientPtr client = opaque;
    remote_domain_event_tray_change_msg data;

568
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
569 570 571 572 573 574
        return -1;

    VIR_DEBUG("Relaying domain %s %d tray change devAlias: %s reason: %d",
              dom->name, dom->id, devAlias, reason);

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

577
    if (VIR_STRDUP(data.devAlias, devAlias) < 0)
578 579 580 581 582
        return -1;
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

583
    remoteDispatchObjectEventSend(client, remoteProgram,
584 585 586 587 588 589
                                  REMOTE_PROC_DOMAIN_EVENT_TRAY_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_tray_change_msg, &data);

    return 0;
}

590 591 592 593 594 595
static int
remoteRelayDomainEventPMWakeup(virConnectPtr conn,
                               virDomainPtr dom,
                               int reason ATTRIBUTE_UNUSED,
                               void *opaque)
{
O
Osier Yang 已提交
596 597 598
    virNetServerClientPtr client = opaque;
    remote_domain_event_pmwakeup_msg data;

599
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
O
Osier Yang 已提交
600 601 602 603 604
        return -1;

    VIR_DEBUG("Relaying domain %s %d system pmwakeup", dom->name, dom->id);

    /* build return data */
605
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
606 607
    make_nonnull_domain(&data.dom, dom);

608
    remoteDispatchObjectEventSend(client, remoteProgram,
O
Osier Yang 已提交
609 610 611 612 613 614
                                  REMOTE_PROC_DOMAIN_EVENT_PMWAKEUP,
                                  (xdrproc_t)xdr_remote_domain_event_pmwakeup_msg, &data);

    return 0;
}

615 616 617 618 619 620
static int
remoteRelayDomainEventPMSuspend(virConnectPtr conn,
                                virDomainPtr dom,
                                int reason ATTRIBUTE_UNUSED,
                                void *opaque)
{
O
Osier Yang 已提交
621 622 623
    virNetServerClientPtr client = opaque;
    remote_domain_event_pmsuspend_msg data;

624
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
O
Osier Yang 已提交
625 626 627 628 629
        return -1;

    VIR_DEBUG("Relaying domain %s %d system pmsuspend", dom->name, dom->id);

    /* build return data */
630
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
631 632
    make_nonnull_domain(&data.dom, dom);

633
    remoteDispatchObjectEventSend(client, remoteProgram,
O
Osier Yang 已提交
634 635 636 637 638 639
                                  REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND,
                                  (xdrproc_t)xdr_remote_domain_event_pmsuspend_msg, &data);

    return 0;
}

640
static int
641
remoteRelayDomainEventBalloonChange(virConnectPtr conn,
642 643 644 645 646 647 648
                                    virDomainPtr dom,
                                    unsigned long long actual,
                                    void *opaque)
{
    virNetServerClientPtr client = opaque;
    remote_domain_event_balloon_change_msg data;

649
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
650 651 652 653 654 655 656 657 658
        return -1;

    VIR_DEBUG("Relaying domain balloon change event %s %d %lld", dom->name, dom->id, actual);

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

659
    remoteDispatchObjectEventSend(client, remoteProgram,
660 661 662 663 664 665 666
                                  REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_balloon_change_msg, &data);

    return 0;
}


667 668 669 670 671 672
static int
remoteRelayDomainEventPMSuspendDisk(virConnectPtr conn,
                                    virDomainPtr dom,
                                    int reason ATTRIBUTE_UNUSED,
                                    void *opaque)
{
673 674 675
    virNetServerClientPtr client = opaque;
    remote_domain_event_pmsuspend_disk_msg data;

676
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
677 678 679 680 681 682 683 684
        return -1;

    VIR_DEBUG("Relaying domain %s %d system pmsuspend-disk", dom->name, dom->id);

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

685
    remoteDispatchObjectEventSend(client, remoteProgram,
686 687 688 689 690 691
                                  REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND_DISK,
                                  (xdrproc_t)xdr_remote_domain_event_pmsuspend_disk_msg, &data);

    return 0;
}

692
static int
693
remoteRelayDomainEventDeviceRemoved(virConnectPtr conn,
694 695 696 697 698 699 700
                                    virDomainPtr dom,
                                    const char *devAlias,
                                    void *opaque)
{
    virNetServerClientPtr client = opaque;
    remote_domain_event_device_removed_msg data;

701
    if (!client || !remoteRelayDomainEventCheckACL(client, conn, dom))
702 703 704 705 706 707 708 709 710 711 712 713 714
        return -1;

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

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

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

    make_nonnull_domain(&data.dom, dom);

715
    remoteDispatchObjectEventSend(client, remoteProgram,
716 717 718 719 720 721 722
                                  REMOTE_PROC_DOMAIN_EVENT_DEVICE_REMOVED,
                                  (xdrproc_t)xdr_remote_domain_event_device_removed_msg,
                                  &data);

    return 0;
}

723

724
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
725
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
726
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
727
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
728
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
729
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
730
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
731
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
732
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
733
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
734
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
735
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
736
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
737
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
738
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
739
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
740
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDeviceRemoved),
741 742 743 744
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

745
static int
746
remoteRelayNetworkEventLifecycle(virConnectPtr conn,
747 748 749 750
                                 virNetworkPtr net,
                                 int event,
                                 int detail,
                                 void *opaque)
751
{
752
    daemonClientEventCallbackPtr callback = opaque;
753 754
    remote_network_event_lifecycle_msg data;

755 756
    if (callback->callbackID < 0 ||
        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
757 758
        return -1;

759 760
    VIR_DEBUG("Relaying network lifecycle event %d, detail %d, callback %d",
              event, detail, callback->callbackID);
761 762 763 764

    /* build return data */
    memset(&data, 0, sizeof(data));
    make_nonnull_network(&data.net, net);
765
    data.callbackID = callback->callbackID;
766 767 768
    data.event = event;
    data.detail = detail;

769
    remoteDispatchObjectEventSend(callback->client, remoteProgram,
770 771 772 773 774 775 776 777 778 779 780 781
                                  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);

782 783 784 785 786 787 788
/*
 * 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
 */
789
void remoteClientFreeFunc(void *data)
790 791 792 793 794
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
795
        virIdentityPtr sysident = virIdentityGetSystem();
796
        size_t i;
797

798 799
        virIdentitySetCurrent(sysident);

800
        for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) {
801
            if (priv->domainEventCallbackID[i] != -1) {
802
                VIR_DEBUG("Deregistering to relay remote events %zu", i);
803 804 805 806 807 808
                virConnectDomainEventDeregisterAny(priv->conn,
                                                   priv->domainEventCallbackID[i]);
            }
            priv->domainEventCallbackID[i] = -1;
        }

809 810 811 812 813
        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;
814
            }
815 816 817 818 819 820
            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");
821
        }
822
        VIR_FREE(priv->networkEventCallbacks);
823

824
        virConnectClose(priv->conn);
825 826 827

        virIdentitySetCurrent(NULL);
        virObjectUnref(sysident);
828 829 830 831 832 833
    }

    VIR_FREE(priv);
}


834 835 836 837 838 839 840
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

841

842 843
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
844 845
{
    struct daemonClientPrivate *priv;
846
    size_t i;
847

848
    if (VIR_ALLOC(priv) < 0)
849
        return NULL;
850 851 852

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
853
        virReportSystemError(errno, "%s", _("unable to init mutex"));
854
        return NULL;
855 856
    }

857
    for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++)
858 859
        priv->domainEventCallbackID[i] = -1;

860
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
861
    return priv;
862 863
}

864 865 866
/*----- Functions. -----*/

static int
867 868 869 870 871
remoteDispatchConnectOpen(virNetServerPtr server,
                          virNetServerClientPtr client,
                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                          virNetMessageErrorPtr rerr,
                          struct remote_connect_open_args *args)
872 873
{
    const char *name;
874
    unsigned int flags;
875
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
876
    int rv = -1;
877

878 879 880 881
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
882
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
883 884 885
        goto cleanup;
    }

886
    if (virNetServerKeepAliveRequired(server) && !priv->keepalive_supported) {
887 888
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("keepalive support is required to connect"));
889 890 891
        goto cleanup;
    }

892 893 894 895 896 897
    name = args->name ? *args->name : NULL;

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

901
    priv->conn =
902
        flags & VIR_CONNECT_RO
903 904
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
905

906
    if (priv->conn == NULL)
907 908 909
        goto cleanup;

    rv = 0;
910

911 912
cleanup:
    if (rv < 0)
913 914
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
915
    return rv;
916 917 918 919
}


static int
920 921 922 923
remoteDispatchConnectClose(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
924
{
925
    virNetServerClientDelayedClose(client);
926
    return 0;
927 928
}

929

930
static int
931 932
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
933
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
934
                                     virNetMessageErrorPtr rerr,
935 936
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
937
{
938
    virDomainPtr dom = NULL;
939 940
    char *type;
    int nparams;
941
    int rv = -1;
942 943
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
944

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

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

953
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
954
        goto cleanup;
955 956 957

    ret->type = type;
    ret->nparams = nparams;
958 959 960 961
    rv = 0;

cleanup:
    if (rv < 0)
962
        virNetMessageSaveError(rerr);
963 964 965
    if (dom)
        virDomainFree(dom);
    return rv;
966 967
}

968 969
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
970 971 972
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
973
                               remote_typed_param **ret_params_val,
974 975
                               u_int *ret_params_len,
                               unsigned int flags)
976
{
977 978
    size_t i;
    size_t j;
979 980 981 982
    int rv = -1;
    remote_typed_param *val;

    *ret_params_len = nparams;
983
    if (VIR_ALLOC_N(val, nparams) < 0)
984 985
        goto cleanup;

986
    for (i = 0, j = 0; i < nparams; ++i) {
987 988 989 990 991
        /* 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)) {
992 993 994 995
            --*ret_params_len;
            continue;
        }

996
        /* remoteDispatchClientRequest will free this: */
997
        if (VIR_STRDUP(val[j].field, params[i].field) < 0)
998
            goto cleanup;
999
        val[j].value.type = params[i].type;
1000
        switch (params[i].type) {
1001
        case VIR_TYPED_PARAM_INT:
1002
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
1003 1004
            break;
        case VIR_TYPED_PARAM_UINT:
1005
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
1006 1007
            break;
        case VIR_TYPED_PARAM_LLONG:
1008
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
1009 1010
            break;
        case VIR_TYPED_PARAM_ULLONG:
1011
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
1012 1013
            break;
        case VIR_TYPED_PARAM_DOUBLE:
1014
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
1015 1016
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
1017 1018 1019
            val[j].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
        case VIR_TYPED_PARAM_STRING:
1020
            if (VIR_STRDUP(val[j].value.remote_typed_param_value_u.s, params[i].value.s) < 0)
1021
                goto cleanup;
1022 1023
            break;
        default:
1024 1025
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
1026 1027
            goto cleanup;
        }
1028
        j++;
1029 1030 1031 1032 1033 1034 1035 1036
    }

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

cleanup:
    if (val) {
1037
        for (i = 0; i < nparams; i++) {
1038
            VIR_FREE(val[i].field);
1039
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
1040 1041
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
1042 1043 1044 1045 1046 1047 1048
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
1049 1050
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
1051 1052 1053
                                 int limit,
                                 int *nparams)
{
1054
    size_t i = 0;
1055 1056 1057 1058
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
1059
    if (limit && args_params_len > limit) {
1060
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1061 1062
        goto cleanup;
    }
1063
    if (VIR_ALLOC_N(params, args_params_len) < 0)
1064 1065 1066 1067 1068 1069 1070 1071
        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) {
1072 1073 1074
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Parameter %s too big for destination"),
                           args_params_val[i].field);
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
            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;
1103
        case VIR_TYPED_PARAM_STRING:
1104 1105
            if (VIR_STRDUP(params[i].value.s,
                           args_params_val[i].value.remote_typed_param_value_u.s) < 0)
1106 1107
                goto cleanup;
            break;
1108
        default:
1109 1110
            virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                           params[i].type);
1111 1112 1113 1114 1115 1116 1117
            goto cleanup;
        }
    }

    rv = 0;

cleanup:
1118
    if (rv < 0) {
1119 1120
        virTypedParamsFree(params, i);
        params = NULL;
1121
    }
1122 1123 1124
    return params;
}

1125
static int
1126 1127
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
1128
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
1129
                                           virNetMessageErrorPtr rerr,
1130 1131
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
1132
{
1133
    virDomainPtr dom = NULL;
1134
    virTypedParameterPtr params = NULL;
1135
    int nparams = 0;
1136
    int rv = -1;
1137 1138
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1139

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

1145
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1146
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1147
        goto cleanup;
1148
    }
1149
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1150
        goto cleanup;
1151
    nparams = args->nparams;
1152

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

1156
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1157
        goto cleanup;
1158

1159
    if (remoteSerializeTypedParameters(params, nparams,
1160
                                       &ret->params.params_val,
1161 1162
                                       &ret->params.params_len,
                                       0) < 0)
1163 1164 1165 1166 1167 1168
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
1169
        virNetMessageSaveError(rerr);
1170
    virTypedParamsFree(params, nparams);
1171 1172 1173 1174 1175
    if (dom)
        virDomainFree(dom);
    return rv;
}

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
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;
1186
    size_t i;
1187 1188 1189 1190
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1191
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1192 1193 1194 1195 1196 1197 1198 1199
        goto cleanup;
    }

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

1200 1201 1202 1203 1204 1205 1206
    if (ndomains > REMOTE_DOMAIN_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domains '%d' for limit '%d'"),
                       ndomains, REMOTE_DOMAIN_LIST_MAX);
        goto cleanup;
    }

1207
    if (doms && ndomains) {
1208
        if (VIR_ALLOC_N(ret->domains.domains_val, ndomains) < 0)
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
            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;
}

1235
static int
1236 1237
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1238
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1239
                                                virNetMessageErrorPtr rerr,
1240 1241 1242 1243 1244
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
1245
    int nparams = 0;
1246
    int rv = -1;
1247 1248
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1249

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

1255
    if (args->nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1256
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1257 1258
        goto cleanup;
    }
1259
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1260
        goto cleanup;
1261
    nparams = args->nparams;
1262

1263
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1264 1265 1266 1267 1268 1269 1270
        goto cleanup;

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

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

1276
    rv = 0;
1277 1278

cleanup:
1279
    if (rv < 0)
1280
        virNetMessageSaveError(rerr);
1281
    virTypedParamsFree(params, nparams);
1282 1283 1284
    if (dom)
        virDomainFree(dom);
    return rv;
1285 1286
}

1287
static int
1288 1289
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1290
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1291
                                virNetMessageErrorPtr rerr,
1292 1293
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1294
{
1295
    virDomainPtr dom = NULL;
1296
    struct _virDomainMemoryStat *stats = NULL;
1297 1298
    int nr_stats;
    size_t i;
1299
    int rv = -1;
1300 1301
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1302

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

1308
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1309 1310
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1311
        goto cleanup;
1312 1313
    }

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

    /* Allocate stats array for making dispatch call */
1318
    if (VIR_ALLOC_N(stats, args->maxStats) < 0)
1319
        goto cleanup;
1320

1321
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1322
    if (nr_stats < 0)
1323
        goto cleanup;
1324 1325

    /* Allocate return buffer */
1326
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0)
1327
        goto cleanup;
1328 1329 1330 1331 1332 1333 1334

    /* 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;
1335 1336 1337 1338
    rv = 0;

cleanup:
    if (rv < 0)
1339
        virNetMessageSaveError(rerr);
1340 1341
    if (dom)
        virDomainFree(dom);
1342
    VIR_FREE(stats);
1343
    return rv;
1344 1345
}

1346
static int
1347 1348
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1349
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1350
                              virNetMessageErrorPtr rerr,
1351 1352
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1353
{
1354
    virDomainPtr dom = NULL;
1355 1356 1357 1358
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1359
    int rv = -1;
1360 1361
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1362

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

1368
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1369
        goto cleanup;
1370 1371 1372 1373 1374 1375
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1376 1377
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1378
        goto cleanup;
1379 1380 1381
    }

    ret->buffer.buffer_len = size;
1382
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0)
1383
        goto cleanup;
1384

1385
    if (virDomainBlockPeek(dom, path, offset, size,
1386
                           ret->buffer.buffer_val, flags) < 0)
1387
        goto cleanup;
1388

1389 1390 1391 1392
    rv = 0;

cleanup:
    if (rv < 0) {
1393
        virNetMessageSaveError(rerr);
1394 1395 1396 1397 1398
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
1399 1400
}

1401 1402 1403
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1404
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1405 1406 1407 1408 1409 1410 1411
                                    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;
1412
    int nparams = 0;
1413 1414 1415 1416 1417 1418
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1419
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1420 1421 1422 1423 1424 1425 1426
        goto cleanup;
    }

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

1427
    if (args->nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1428
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1429 1430
        goto cleanup;
    }
1431
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
1432
        goto cleanup;
1433
    nparams = args->nparams;
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448

    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,
1449 1450
                                       &ret->params.params_len,
                                       args->flags) < 0)
1451 1452 1453 1454 1455 1456
        goto cleanup;

success:
    rv = 0;

cleanup:
1457
    if (rv < 0)
1458
        virNetMessageSaveError(rerr);
1459
    virTypedParamsFree(params, nparams);
A
ajia@redhat.com 已提交
1460 1461
    if (dom)
        virDomainFree(dom);
1462 1463 1464
    return rv;
}

R
Richard W.M. Jones 已提交
1465
static int
1466 1467
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1468
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1469
                               virNetMessageErrorPtr rerr,
1470 1471
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1472
{
1473
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1474 1475 1476
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1477
    int rv = -1;
1478 1479
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1480

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

1486
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1487
        goto cleanup;
R
Richard W.M. Jones 已提交
1488 1489 1490 1491 1492
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1493 1494
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1495
        goto cleanup;
R
Richard W.M. Jones 已提交
1496 1497 1498
    }

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

1502
    if (virDomainMemoryPeek(dom, offset, size,
1503
                            ret->buffer.buffer_val, flags) < 0)
1504
        goto cleanup;
R
Richard W.M. Jones 已提交
1505

1506 1507 1508 1509
    rv = 0;

cleanup:
    if (rv < 0) {
1510
        virNetMessageSaveError(rerr);
1511 1512 1513 1514 1515
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
1516 1517
}

1518
static int
1519 1520
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1521
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1522
                                     virNetMessageErrorPtr rerr,
1523 1524
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1525
{
1526 1527
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1528
    int rv = -1;
1529 1530
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1531

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

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

1540
    if (VIR_ALLOC(seclabel) < 0)
1541 1542 1543 1544 1545 1546
        goto cleanup;

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

    ret->label.label_len = strlen(seclabel->label) + 1;
1547
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0)
1548
        goto cleanup;
1549 1550
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1551

1552 1553 1554 1555
    rv = 0;

cleanup:
    if (rv < 0)
1556
        virNetMessageSaveError(rerr);
1557 1558 1559
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
1560
    return rv;
1561 1562
}

M
Marcelo Cerri 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
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;
1573 1574
    int len, rv = -1;
    size_t i;
M
Marcelo Cerri 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
    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;
    }

1593
    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0)
M
Marcelo Cerri 已提交
1594 1595 1596 1597 1598
        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];
1599
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0)
M
Marcelo Cerri 已提交
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
            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;
}

1623
static int
1624 1625
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1626
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1627
                                   virNetMessageErrorPtr rerr,
1628
                                   remote_node_get_security_model_ret *ret)
1629
{
1630
    virSecurityModel secmodel;
1631
    int rv = -1;
1632 1633
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1634

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

1640
    memset(&secmodel, 0, sizeof(secmodel));
1641
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
1642 1643 1644
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
1645
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0)
1646 1647 1648 1649
        goto cleanup;
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
1650
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0)
1651
        goto cleanup;
1652
    strcpy(ret->doi.doi_val, secmodel.doi);
1653

1654 1655 1656 1657
    rv = 0;

cleanup:
    if (rv < 0)
1658
        virNetMessageSaveError(rerr);
1659
    return rv;
1660 1661
}

1662
static int
1663 1664
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1665
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1666
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
1667 1668
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
1669 1670 1671 1672 1673
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
1674 1675
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1676

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

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

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
1686
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
1687 1688 1689 1690 1691
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
1692
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1693 1694 1695 1696 1697 1698
        goto cleanup;
    }

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

E
Eric Blake 已提交
1701
    if ((num = virDomainGetVcpuPinInfo(dom,
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
                                       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;
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 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
    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)
1791
        goto cleanup;
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801

    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;
1802 1803 1804 1805 1806 1807
    cpumaps = NULL;

    rv = 0;

cleanup:
    if (rv < 0)
1808
        virNetMessageSaveError(rerr);
1809 1810 1811 1812 1813 1814
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;
}

1815
static int
1816 1817
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
1818
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
1819
                             virNetMessageErrorPtr rerr,
1820 1821
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
1822
{
1823
    virDomainPtr dom = NULL;
1824 1825
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
1826 1827
    int info_len;
    size_t i;
1828
    int rv = -1;
1829 1830
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1831

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

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

1840
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1841
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
1842
        goto cleanup;
1843
    }
1844

1845 1846
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1847
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1848 1849 1850 1851 1852
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
1853
        goto cleanup;
1854 1855
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
1856
        goto cleanup;
1857 1858 1859 1860 1861 1862 1863 1864 1865

    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)
1866
        goto cleanup;
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883

    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;
1884 1885

cleanup:
1886
    if (rv < 0) {
1887
        virNetMessageSaveError(rerr);
1888 1889 1890 1891
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
1892 1893 1894
    if (dom)
        virDomainFree(dom);
    return rv;
1895 1896 1897
}

static int
1898 1899
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1900
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1901
                                   virNetMessageErrorPtr rerr,
1902 1903
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
1904
{
1905 1906 1907 1908 1909
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1910
    int rv = -1;
1911 1912
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1913

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

1919 1920 1921 1922
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
1923
    if (VIR_ALLOC(uri_out) < 0)
1924
        goto cleanup;
1925

1926
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
1927 1928
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
1929
        goto cleanup;
1930

1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
    /* 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;
    }
1942

1943
    rv = 0;
1944

1945 1946
cleanup:
    if (rv < 0)
1947
        virNetMessageSaveError(rerr);
1948
    VIR_FREE(uri_out);
1949
    return rv;
1950 1951
}

1952
static int
1953 1954
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1955
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1956
                                    virNetMessageErrorPtr rerr,
1957 1958
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
1959
{
1960 1961 1962 1963 1964
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1965
    int rv = -1;
1966 1967
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1968

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

1974 1975
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
1976

1977
    /* Wacky world of XDR ... */
1978
    if (VIR_ALLOC(uri_out) < 0)
1979
        goto cleanup;
1980

1981
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
1982 1983 1984
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
1985
        goto cleanup;
1986

1987 1988 1989 1990 1991 1992
    /* 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;
1993

1994 1995 1996 1997
    rv = 0;

cleanup:
    if (rv < 0)
1998
        virNetMessageSaveError(rerr);
1999
    return rv;
2000 2001
}

C
Chris Lalancette 已提交
2002
static int
2003 2004
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
2005
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
2006 2007 2008
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
2009
{
2010
    virDomainPtr dom = NULL;
2011
    virTypedParameterPtr params = NULL;
2012
    int nparams = 0;
2013
    unsigned int flags;
2014
    int rv = -1;
2015 2016
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2017

2018
    if (!priv->conn) {
2019
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2020
        goto cleanup;
2021
    }
C
Chris Lalancette 已提交
2022

2023
    flags = args->flags;
C
Chris Lalancette 已提交
2024

2025
    if (args->nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
2026
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2027 2028
        goto cleanup;
    }
2029
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2030
        goto cleanup;
2031
    nparams = args->nparams;
C
Chris Lalancette 已提交
2032

2033
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2034
        goto cleanup;
C
Chris Lalancette 已提交
2035

2036
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
2037
        goto cleanup;
C
Chris Lalancette 已提交
2038

2039 2040 2041 2042 2043 2044
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
2045 2046
    }

2047
    if (remoteSerializeTypedParameters(params, nparams,
2048
                                       &ret->params.params_val,
2049 2050
                                       &ret->params.params_len,
                                       args->flags) < 0)
2051
        goto cleanup;
2052

2053
success:
2054 2055 2056
    rv = 0;

cleanup:
2057
    if (rv < 0)
2058
        virNetMessageSaveError(rerr);
2059
    virTypedParamsFree(params, nparams);
2060 2061 2062
    if (dom)
        virDomainFree(dom);
    return rv;
2063 2064
}

2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
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;
2075
    int nparams = 0;
2076 2077 2078 2079 2080 2081
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
2082
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2083 2084 2085 2086 2087
        goto cleanup;
    }

    flags = args->flags;

2088
    if (args->nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
2089
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2090 2091
        goto cleanup;
    }
2092
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2093
        goto cleanup;
2094
    nparams = args->nparams;
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121

    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);
2122
    virTypedParamsFree(params, nparams);
2123 2124 2125 2126 2127
    if (dom)
        virDomainFree(dom);
    return rv;
}

2128
static int
2129 2130
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
2131
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2132 2133 2134
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
2135
{
2136
    virDomainPtr dom = NULL;
2137
    virTypedParameterPtr params = NULL;
2138
    int nparams = 0;
2139
    unsigned int flags;
2140
    int rv = -1;
2141 2142
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2143

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

2149 2150
    flags = args->flags;

2151
    if (args->nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2152
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2153
        goto cleanup;
2154
    }
2155
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2156
        goto cleanup;
2157
    nparams = args->nparams;
2158

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

2162
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2163
        goto cleanup;
2164

2165 2166 2167 2168 2169 2170 2171 2172
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2173
    if (remoteSerializeTypedParameters(params, nparams,
2174
                                       &ret->params.params_val,
2175 2176
                                       &ret->params.params_len,
                                       args->flags) < 0)
2177
        goto cleanup;
2178

2179 2180
success:
    rv = 0;
2181

2182
cleanup:
2183
    if (rv < 0)
2184
        virNetMessageSaveError(rerr);
2185
    virTypedParamsFree(params, nparams);
2186 2187 2188
    if (dom)
        virDomainFree(dom);
    return rv;
2189 2190
}

2191
static int
2192 2193
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2194
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2195 2196 2197
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2198
{
2199
    virNodeCPUStatsPtr params = NULL;
2200
    size_t i;
2201
    int cpuNum = args->cpuNum;
2202
    int nparams = 0;
2203 2204
    unsigned int flags;
    int rv = -1;
2205 2206
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2207

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

    flags = args->flags;

2215
    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
2216
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2217 2218
        goto cleanup;
    }
2219
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2220
        goto cleanup;
2221
    nparams = args->nparams;
2222

2223
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
        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)
2237
        goto cleanup;
2238 2239 2240

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2241 2242
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2243 2244 2245 2246 2247 2248 2249

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

success:
    rv = 0;

2250 2251
cleanup:
    if (rv < 0) {
2252
        virNetMessageSaveError(rerr);
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263
        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
2264 2265
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2266
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2267 2268 2269
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2270
{
2271
    virNodeMemoryStatsPtr params = NULL;
2272
    size_t i;
2273
    int cellNum = args->cellNum;
2274
    int nparams = 0;
2275 2276
    unsigned int flags;
    int rv = -1;
2277 2278
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2279

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

    flags = args->flags;

2287
    if (args->nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
2288
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2289 2290
        goto cleanup;
    }
2291
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2292
        goto cleanup;
2293
    nparams = args->nparams;
2294

2295
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
        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)
2309
        goto cleanup;
2310 2311 2312

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
2313 2314
        if (VIR_STRDUP(ret->params.params_val[i].field, params[i].field) < 0)
            goto cleanup;
2315 2316 2317 2318 2319 2320 2321

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

success:
    rv = 0;

2322 2323
cleanup:
    if (rv < 0) {
2324
        virNetMessageSaveError(rerr);
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
        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;
}

2335 2336 2337
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2338
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
                                    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) {
2350
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
        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;
}

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

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

2396
    if (args->nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
2397
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2398 2399 2400
        goto cleanup;
    }

2401
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
2402
        goto cleanup;
2403
    nparams = args->nparams;
2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432

    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);
2433
    virTypedParamsFree(params, nparams);
2434 2435 2436 2437
    if (dom)
        virDomainFree(dom);
    return rv;
}
2438

D
Daniel Veillard 已提交
2439 2440
/*-------------------------------------------------------------*/

2441
static int
2442 2443
remoteDispatchAuthList(virNetServerPtr server ATTRIBUTE_UNUSED,
                       virNetServerClientPtr client,
2444
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2445
                       virNetMessageErrorPtr rerr,
2446
                       remote_auth_list_ret *ret)
2447
{
2448
    int rv = -1;
2449 2450
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2451
    gid_t callerGid;
2452
    pid_t callerPid;
2453
    unsigned long long timestamp;
2454 2455 2456 2457 2458 2459

    /* 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) {
2460
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2461
                                              &callerPid, &timestamp) < 0) {
2462 2463 2464 2465
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2466 2467
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
2468
                            (long long) callerPid, (int) callerUid) < 0)
J
Jim Fehlig 已提交
2469 2470
                goto cleanup;
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
2471 2472
            virNetServerClientSetAuth(client, 0);
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
2473
            VIR_FREE(ident);
2474 2475
        }
    }
2476

2477
    ret->types.types_len = 1;
2478
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0)
2479
        goto cleanup;
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493

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

2495 2496 2497 2498
    rv = 0;

cleanup:
    if (rv < 0)
2499
        virNetMessageSaveError(rerr);
2500
    return rv;
2501 2502 2503
}


2504
#ifdef WITH_SASL
2505 2506
/*
 * Initializes the SASL session in prepare for authentication
2507
 * and gives the client a list of allowed mechanisms to choose
2508 2509
 */
static int
2510 2511
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2512
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2513
                           virNetMessageErrorPtr rerr,
2514
                           remote_auth_sasl_init_ret *ret)
2515
{
2516 2517 2518
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2519

2520
    virMutexLock(&priv->lock);
2521

2522 2523 2524
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2525
        VIR_ERROR(_("client tried invalid SASL init request"));
2526
        goto authfail;
2527 2528
    }

2529 2530 2531 2532 2533
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2534
        goto authfail;
2535

2536
# if WITH_GNUTLS
2537
    /* Inform SASL that we've got an external SSF layer from TLS */
2538 2539 2540 2541
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2542
            goto authfail;
2543 2544 2545 2546 2547

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2548
            goto authfail;
2549
    }
2550
# endif
2551

2552
    if (virNetServerClientIsSecure(client))
2553
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2554 2555
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2556
        /* Plain TCP, better get an SSF layer */
2557 2558 2559 2560
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
2561

2562
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
2563
        goto authfail;
2564
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
2565

2566 2567
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
2568
    return 0;
2569 2570

authfail:
2571
    virResetLastError();
2572 2573
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2574
    virNetMessageSaveError(rerr);
2575 2576 2577
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2578
    virObjectUnref(sasl);
2579
    virMutexUnlock(&priv->lock);
2580
    return -1;
2581 2582
}

2583
/*
2584 2585
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
2586
static int
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602
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;
        }
2603
    }
2604 2605

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
2606
        return -2;
2607

2608 2609
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
2610

2611
    virNetServerClientSetAuth(client, 0);
2612
    virNetServerClientSetSASLSession(client, priv->sasl);
2613

2614
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
2615

2616 2617 2618
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2619

2620
    virObjectUnref(priv->sasl);
2621
    priv->sasl = NULL;
2622

2623
    return 0;
2624

2625 2626 2627
error:
    return -1;
}
2628

2629 2630 2631 2632
/*
 * This starts the SASL authentication negotiation.
 */
static int
2633 2634
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client,
2635
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2636
                            virNetMessageErrorPtr rerr,
2637 2638
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2639 2640
{
    const char *serverout;
2641
    size_t serveroutlen;
2642
    int err;
2643 2644 2645
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2646
    const char *identity;
2647

2648
    virMutexLock(&priv->lock);
2649

2650 2651 2652
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
2653
        VIR_ERROR(_("client tried invalid SASL start request"));
2654
        goto authfail;
2655 2656
    }

2657 2658
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
2659 2660 2661 2662 2663 2664 2665 2666 2667
    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)
2668
        goto authfail;
2669

2670
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2671
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
2672
        goto authfail;
2673 2674 2675 2676
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2677 2678
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2679 2680 2681 2682 2683 2684 2685
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2686
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2687
    if (err == VIR_NET_SASL_CONTINUE) {
2688 2689
        ret->complete = 0;
    } else {
2690
        /* Check username whitelist ACL */
2691
        if ((err = remoteSASLFinish(client)) < 0) {
2692 2693 2694 2695 2696
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2697

2698 2699 2700
        ret->complete = 1;
    }

2701
    virMutexUnlock(&priv->lock);
2702
    return 0;
2703 2704

authfail:
2705 2706 2707
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2708 2709 2710
    goto error;

authdeny:
2711
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2712 2713 2714
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2715 2716
    goto error;

2717
error:
2718
    virObjectUnref(priv->sasl);
2719 2720
    priv->sasl = NULL;
    virResetLastError();
2721 2722
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2723 2724 2725
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2726
    return -1;
2727 2728 2729 2730
}


static int
2731 2732
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2733
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2734
                           virNetMessageErrorPtr rerr,
2735 2736
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
2737 2738
{
    const char *serverout;
2739
    size_t serveroutlen;
2740
    int err;
2741 2742 2743
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2744
    const char *identity;
2745

2746 2747 2748 2749 2750
    virMutexLock(&priv->lock);

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

2755
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
2756
              args->data.data_len, args->nil);
2757 2758 2759 2760 2761 2762 2763 2764
    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)
2765
        goto authfail;
2766 2767

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2768
        VIR_ERROR(_("sasl step reply data too long %d"),
2769
                  (int)serveroutlen);
2770
        goto authfail;
2771 2772 2773 2774
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2775 2776
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2777 2778 2779 2780 2781 2782 2783
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2784
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2785
    if (err == VIR_NET_SASL_CONTINUE) {
2786 2787
        ret->complete = 0;
    } else {
2788
        /* Check username whitelist ACL */
2789
        if ((err = remoteSASLFinish(client)) < 0) {
2790 2791 2792 2793 2794
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2795

2796 2797 2798
        ret->complete = 1;
    }

2799
    virMutexUnlock(&priv->lock);
2800
    return 0;
2801 2802

authfail:
2803 2804 2805
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2806 2807 2808
    goto error;

authdeny:
2809
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2810 2811 2812
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2813 2814
    goto error;

2815
error:
2816
    virObjectUnref(priv->sasl);
2817 2818
    priv->sasl = NULL;
    virResetLastError();
2819 2820
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2821 2822 2823
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2824 2825
    return -1;
}
2826 2827 2828 2829
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2830
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2831 2832 2833 2834
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
2835 2836
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2837 2838 2839 2840 2841 2842
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
2843
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2844 2845 2846 2847 2848
                            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");
2849 2850
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2851 2852 2853 2854 2855 2856
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2857
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2858 2859 2860 2861 2862
                           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");
2863 2864
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2865 2866 2867 2868
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
2869 2870 2871



2872
#if WITH_POLKIT1
2873
static int
2874 2875
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
                         virNetServerClientPtr client,
2876
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2877
                         virNetMessageErrorPtr rerr,
2878
                         remote_auth_polkit_ret *ret)
2879
{
2880
    pid_t callerPid = -1;
2881
    gid_t callerGid = -1;
2882
    uid_t callerUid = -1;
2883
    unsigned long long timestamp;
2884 2885
    const char *action;
    int status = -1;
2886
    char *ident = NULL;
2887
    bool authdismissed = 0;
2888
    char *pkout = NULL;
2889 2890
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2891
    virCommandPtr cmd = NULL;
2892
# ifndef PKCHECK_SUPPORTS_UID
2893
    static bool polkitInsecureWarned;
2894
# endif
2895

2896 2897
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
2898 2899 2900
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

2901
    cmd = virCommandNewArgList(PKCHECK_PATH, "--action-id", action, NULL);
2902
    virCommandSetOutputBuffer(cmd, &pkout);
2903
    virCommandSetErrorBuffer(cmd, &pkout);
2904

2905 2906
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2907
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2908 2909 2910
        goto authfail;
    }

2911
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
2912
                                          &callerPid, &timestamp) < 0) {
2913 2914 2915
        goto authfail;
    }

2916 2917 2918 2919 2920 2921
    if (timestamp == 0) {
        VIR_WARN("Failing polkit auth due to missing client (pid=%lld) start time",
                 (long long)callerPid);
        goto authfail;
    }

2922 2923
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2924

2925
    virCommandAddArg(cmd, "--process");
2926

2927
# ifdef PKCHECK_SUPPORTS_UID
2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938
    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);
2939
# endif
2940

2941
    virCommandAddArg(cmd, "--allow-user-interaction");
2942

2943
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
2944
                    (long long) callerPid, callerUid) < 0)
2945 2946
        goto authfail;

2947
    if (virCommandRun(cmd, &status) < 0)
2948
        goto authfail;
2949

2950
    authdismissed = (pkout && strstr(pkout, "dismissed=true"));
2951
    if (status != 0) {
2952
        char *tmp = virProcessTranslateStatus(status);
2953 2954
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d: %s"),
                  action, (long long) callerPid, callerUid, NULLSTR(tmp));
2955
        VIR_FREE(tmp);
2956
        goto authdeny;
2957
    }
2958 2959 2960
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2961 2962
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
2963 2964
    ret->complete = 1;

2965
    virNetServerClientSetAuth(client, 0);
2966
    virMutexUnlock(&priv->lock);
2967
    virCommandFree(cmd);
E
Eric Blake 已提交
2968
    VIR_FREE(pkout);
2969
    VIR_FREE(ident);
2970

2971 2972
    return 0;

2973
error:
2974 2975
    virCommandFree(cmd);
    VIR_FREE(ident);
2976
    virResetLastError();
2977

2978
    if (authdismissed) {
2979 2980
        virReportError(VIR_ERR_AUTH_CANCELLED, "%s",
                       _("authentication cancelled by user"));
2981 2982
    } else if (pkout && *pkout) {
        virReportError(VIR_ERR_AUTH_FAILED, _("polkit: %s"), pkout);
2983
    } else {
2984
        virReportError(VIR_ERR_AUTH_FAILED, "%s", _("authentication failed"));
2985
    }
2986 2987

    VIR_FREE(pkout);
2988 2989 2990 2991
    virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return -1;

2992
authfail:
2993 2994 2995
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
2996 2997 2998
    goto error;

authdeny:
2999 3000
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
3001
          client, REMOTE_AUTH_POLKIT, ident);
3002
    goto error;
3003
}
3004
#elif WITH_POLKIT0
3005
static int
3006
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
3007
                         virNetServerClientPtr client,
3008
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3009
                         virNetMessageErrorPtr rerr,
3010
                         remote_auth_polkit_ret *ret)
3011 3012
{
    pid_t callerPid;
3013
    gid_t callerGid;
3014
    uid_t callerUid;
3015 3016 3017 3018 3019 3020
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
3021
    const char *action;
3022
    char *ident = NULL;
3023 3024
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3025
    DBusConnection *sysbus;
J
Jim Fehlig 已提交
3026
    unsigned long long timestamp;
3027

J
Jim Fehlig 已提交
3028
    virMutexLock(&priv->lock);
3029

J
Jim Fehlig 已提交
3030
    action = virNetServerClientGetReadonly(client) ?
3031 3032
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
3033

3034
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
J
Jim Fehlig 已提交
3035
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
3036
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
3037
        goto authfail;
3038 3039
    }

3040
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
J
Jim Fehlig 已提交
3041
                                          &callerPid, &timestamp) < 0) {
3042
        VIR_ERROR(_("cannot get peer socket identity"));
3043
        goto authfail;
3044 3045
    }

3046
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
3047
                    (long long) callerPid, callerUid) < 0)
3048 3049
        goto authfail;

3050 3051 3052
    if (!(sysbus = virDBusGetSystemBus()))
        goto authfail;

3053 3054
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
3055
    dbus_error_init(&err);
3056
    if (!(pkcaller = polkit_caller_new_from_pid(sysbus,
3057
                                                callerPid, &err))) {
3058
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
3059
        dbus_error_free(&err);
3060
        goto authfail;
3061
    }
3062

3063
    if (!(pkaction = polkit_action_new())) {
3064
        char ebuf[1024];
3065
        VIR_ERROR(_("Failed to create polkit action %s"),
3066
                  virStrerror(errno, ebuf, sizeof(ebuf)));
3067
        polkit_caller_unref(pkcaller);
3068
        goto authfail;
3069 3070 3071 3072 3073
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
3074
        char ebuf[1024];
3075
        VIR_ERROR(_("Failed to create polkit context %s"),
3076
                  (pkerr ? polkit_error_get_error_message(pkerr)
3077
                   : virStrerror(errno, ebuf, sizeof(ebuf))));
3078 3079 3080 3081 3082
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
3083
        goto authfail;
3084
    }
3085

3086
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
3087 3088 3089 3090 3091 3092
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
3093 3094 3095
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
3096
        goto authfail;
3097
    }
3098
# else
3099 3100 3101
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
3102
# endif
3103 3104 3105 3106
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
3107 3108
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d, result: %s"),
                  action, (long long) callerPid, callerUid,
3109
                  polkit_result_to_string_representation(pkresult));
3110
        goto authdeny;
3111
    }
3112 3113 3114
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3115 3116
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d, result %s",
             action, (long long) callerPid, callerUid,
3117 3118
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
3119

3120
    virNetServerClientSetAuth(client, 0);
J
Jim Fehlig 已提交
3121
    virMutexUnlock(&priv->lock);
3122
    VIR_FREE(ident);
3123
    return 0;
3124

3125
error:
3126
    VIR_FREE(ident);
3127
    virResetLastError();
3128 3129
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3130
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3131
    virMutexUnlock(&priv->lock);
3132 3133
    return -1;

3134
authfail:
3135 3136 3137
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
3138
    goto error;
3139

3140
authdeny:
3141 3142 3143
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3144
    goto error;
3145 3146
}

3147
#else /* !WITH_POLKIT0 & !HAVE_POLKIT1*/
3148 3149

static int
3150
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
3151
                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
3152
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3153
                         virNetMessageErrorPtr rerr,
3154
                         remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
3155
{
3156
    VIR_ERROR(_("client tried unsupported PolicyKit init request"));
3157 3158
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3159
    virNetMessageSaveError(rerr);
3160 3161
    return -1;
}
3162
#endif /* WITH_POLKIT1 */
3163 3164


3165 3166 3167
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3168

3169
static int
3170 3171
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3172
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3173
                                  virNetMessageErrorPtr rerr,
3174 3175
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3176
{
3177 3178
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3179
    int rv = -1;
3180 3181
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3182

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

3188
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3189 3190
        goto cleanup;

3191 3192 3193 3194 3195 3196 3197
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
3198
        if (VIR_ALLOC(parent_p) < 0)
3199
            goto cleanup;
3200
        if (VIR_STRDUP(*parent_p, parent) < 0) {
3201 3202 3203 3204 3205 3206
            VIR_FREE(parent_p);
            goto cleanup;
        }
        ret->parent = parent_p;
    }

3207 3208 3209 3210
    rv = 0;

cleanup:
    if (rv < 0)
3211
        virNetMessageSaveError(rerr);
3212 3213
    if (dev)
        virNodeDeviceFree(dev);
3214
    return rv;
3215 3216
}

3217 3218 3219 3220 3221

/***************************
 * Register / deregister events
 ***************************/
static int
3222 3223 3224 3225 3226
remoteDispatchConnectDomainEventRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                         remote_connect_domain_event_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
3227
{
3228
    int callbackID;
3229
    int rv = -1;
3230 3231
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3232

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

3238 3239 3240
    virMutexLock(&priv->lock);

    if (priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] != -1) {
3241
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
3242
        goto cleanup;
3243
    }
O
Osier Yang 已提交
3244

3245
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3246 3247 3248 3249
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
                                                       client, NULL)) < 0)
3250
        goto cleanup;
O
Osier Yang 已提交
3251

3252
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;
3253

3254 3255 3256 3257
    rv = 0;

cleanup:
    if (rv < 0)
3258 3259
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3260
    return rv;
O
Osier Yang 已提交
3261 3262
}

3263
static int
3264 3265 3266 3267 3268
remoteDispatchConnectDomainEventDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                           remote_connect_domain_event_deregister_ret *ret ATTRIBUTE_UNUSED)
3269
{
3270
    int rv = -1;
3271 3272
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3273

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

3279 3280 3281
    virMutexLock(&priv->lock);

    if (priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] < 0) {
3282
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
3283
        goto cleanup;
3284
    }
3285

3286 3287
    if (virConnectDomainEventDeregisterAny(priv->conn,
                                           priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0)
3288
        goto cleanup;
3289

3290 3291
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;

3292 3293 3294
    rv = 0;

cleanup:
3295
    if (rv < 0)
3296 3297
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3298 3299 3300 3301
    return rv;
}

static void
3302
remoteDispatchObjectEventSend(virNetServerClientPtr client,
3303
                              virNetServerProgramPtr program,
3304 3305 3306 3307
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3308
    virNetMessagePtr msg;
3309

3310
    if (!(msg = virNetMessageNew(false)))
3311
        goto cleanup;
3312

3313 3314 3315 3316 3317 3318
    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;
3319

3320
    if (virNetMessageEncodeHeader(msg) < 0)
3321 3322
        goto cleanup;

3323 3324
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3325

3326 3327
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3328

3329
    xdr_free(proc, data);
3330 3331 3332
    return;

cleanup:
3333
    virNetMessageFree(msg);
3334
    xdr_free(proc, data);
3335 3336
}

3337
static int
3338 3339
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3340
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3341
                             virNetMessageErrorPtr rerr,
3342 3343
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3344
{
3345 3346 3347
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3348
    int rv = -1;
3349 3350
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3351

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

3357
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3358
        goto cleanup;
3359

3360
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3361
        goto cleanup;
3362

3363 3364 3365
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3366 3367 3368 3369
    rv = 0;

cleanup:
    if (rv < 0)
3370
        virNetMessageSaveError(rerr);
3371 3372
    if (secret)
        virSecretFree(secret);
3373
    return rv;
3374 3375
}

3376
static int
3377 3378
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3379
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3380
                             virNetMessageErrorPtr rerr,
3381 3382 3383 3384 3385
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3386 3387
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3388

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

3394
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3395 3396 3397 3398 3399 3400 3401 3402 3403
        goto cleanup;

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

    rv = 0;

cleanup:
    if (rv < 0)
3404
        virNetMessageSaveError(rerr);
3405 3406 3407 3408 3409
    if (dom)
        virDomainFree(dom);
    return rv;
}

3410
static int
3411 3412 3413 3414 3415
remoteDispatchConnectDomainEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                            virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                            remote_connect_domain_event_register_any_args *args)
3416 3417
{
    int callbackID;
3418
    int rv = -1;
3419 3420
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3421

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

3427 3428
    virMutexLock(&priv->lock);

3429 3430
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3431
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
3432
        goto cleanup;
3433 3434
    }

3435
    if (priv->domainEventCallbackID[args->eventID] != -1)  {
3436
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), args->eventID);
3437
        goto cleanup;
3438 3439
    }

3440
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3441 3442 3443
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3444
                                                       client, NULL)) < 0)
3445
        goto cleanup;
3446

3447
    priv->domainEventCallbackID[args->eventID] = callbackID;
3448

3449 3450 3451 3452
    rv = 0;

cleanup:
    if (rv < 0)
3453 3454
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3455
    return rv;
3456 3457 3458 3459
}


static int
3460 3461 3462 3463 3464
remoteDispatchConnectDomainEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                              virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                              remote_connect_domain_event_deregister_any_args *args)
3465 3466
{
    int callbackID = -1;
3467
    int rv = -1;
3468 3469
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3470

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

3476 3477
    virMutexLock(&priv->lock);

3478 3479
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3480
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
3481
        goto cleanup;
3482 3483
    }

3484 3485
    callbackID = priv->domainEventCallbackID[args->eventID];
    if (callbackID < 0) {
3486
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), args->eventID);
3487
        goto cleanup;
3488 3489
    }

3490
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3491
        goto cleanup;
3492

3493 3494
    priv->domainEventCallbackID[args->eventID] = -1;

3495 3496 3497 3498
    rv = 0;

cleanup:
    if (rv < 0)
3499 3500
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3501
    return rv;
3502 3503
}

C
Chris Lalancette 已提交
3504
static int
3505 3506 3507 3508 3509 3510
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 已提交
3511
{
3512
    virDomainPtr dom = NULL;
3513
    int rv = -1;
3514 3515
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
3516

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

3522
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3523
        goto cleanup;
C
Chris Lalancette 已提交
3524

3525
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
3526
                                    args->flags) < 0)
3527
        goto cleanup;
C
Chris Lalancette 已提交
3528

3529
    rv = 0;
C
Chris Lalancette 已提交
3530

3531 3532
cleanup:
    if (rv < 0)
3533
        virNetMessageSaveError(rerr);
3534 3535
    if (dom)
        virDomainFree(dom);
3536
    return rv;
C
Chris Lalancette 已提交
3537 3538
}

3539

3540
static int
3541 3542
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3543
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3544
                                  virNetMessageErrorPtr rerr,
3545 3546 3547 3548 3549 3550
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
3551
    char *xmlin;
3552 3553 3554
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3555 3556
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3557

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

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

3566
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3567 3568
    dname = args->dname == NULL ? NULL : *args->dname;

3569
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584
                                       &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)
3585
        virNetMessageSaveError(rerr);
3586 3587 3588 3589 3590 3591 3592
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3593 3594
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3595
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3596
                                    virNetMessageErrorPtr rerr,
3597 3598 3599 3600 3601 3602 3603 3604 3605
                                    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;
3606 3607
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3608

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

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

    /* Wacky world of XDR ... */
3618
    if (VIR_ALLOC(uri_out) < 0)
3619 3620
        goto cleanup;

3621
    if (virDomainMigratePrepare3(priv->conn,
3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640
                                 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) {
3641
        virNetMessageSaveError(rerr);
3642 3643 3644 3645 3646
        VIR_FREE(uri_out);
    }
    return rv;
}

3647

3648
static int
3649 3650
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3651
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3652
                                    virNetMessageErrorPtr rerr,
3653 3654 3655 3656
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
3657
    char *xmlin;
3658
    char *dname;
3659 3660
    char *uri;
    char *dconnuri;
3661 3662 3663
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3664 3665
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3666

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

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

3675
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3676
    dname = args->dname == NULL ? NULL : *args->dname;
3677 3678
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
3679

3680
    if (virDomainMigratePerform3(dom, xmlin,
3681 3682 3683
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
3684
                                 dconnuri, uri,
3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696
                                 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)
3697
        virNetMessageSaveError(rerr);
3698 3699 3700 3701 3702 3703 3704
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3705 3706
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
3707
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
3708
                                   virNetMessageErrorPtr rerr,
3709 3710 3711 3712 3713 3714
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
3715 3716
    char *uri;
    char *dconnuri;
3717
    int rv = -1;
3718 3719
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3720

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

3726 3727 3728
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

3729
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
3730 3731 3732 3733 3734 3735
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
3736 3737
        goto cleanup;

3738
    make_nonnull_domain(&ret->dom, dom);
3739 3740 3741 3742 3743 3744 3745 3746 3747 3748

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

    rv = 0;

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


static int
3759 3760
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3761
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3762 3763
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
3764 3765 3766
{
    virDomainPtr dom = NULL;
    int rv = -1;
3767 3768
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3769

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

3775
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787
        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)
3788
        virNetMessageSaveError(rerr);
3789 3790 3791 3792 3793 3794
    if (dom)
        virDomainFree(dom);
    return rv;
}


3795 3796 3797 3798 3799 3800
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)
3801 3802 3803 3804 3805 3806
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

3807 3808 3809 3810 3811 3812 3813 3814 3815 3816
    /* 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;
    }

3817
    if (!priv->conn) {
3818
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3819 3820 3821 3822 3823 3824 3825 3826 3827
        goto cleanup;
    }

    switch (args->feature) {
    case VIR_DRV_FEATURE_FD_PASSING:
        supported = 1;
        break;

    default:
3828
        if ((supported = virConnectSupportsFeature(priv->conn, args->feature)) < 0)
3829 3830 3831 3832
            goto cleanup;
        break;
    }

3833
done:
3834 3835 3836 3837 3838 3839 3840 3841 3842 3843
    ret->supported = supported;
    rv = 0;

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


3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857
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) {
3858
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884
        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;
}

3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895
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;
3896
    int nparams = 0;
3897 3898 3899 3900 3901 3902
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
3903
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3904 3905 3906 3907 3908
        goto cleanup;
    }

    flags = args->flags;

3909
    if (args->nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
3910
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3911 3912
        goto cleanup;
    }
3913
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
3914
        goto cleanup;
3915
    nparams = args->nparams;
3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942

    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);
3943
    virTypedParamsFree(params, nparams);
3944 3945 3946 3947
    if (dom)
        virDomainFree(dom);
    return rv;
}
3948

3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964
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) {
3965
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3966 3967 3968 3969
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
3970
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3971 3972 3973
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
3974
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
3975 3976 3977 3978
        goto cleanup;
    }

    if (args->nparams > 0 &&
3979
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0)
3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002
        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;
4003
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
4004
        size_t i;
4005 4006 4007 4008 4009 4010

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

cleanup:
    if (rv < 0)
         virNetMessageSaveError(rerr);
4015
    virTypedParamsFree(params, args->ncpus * args->nparams);
4016 4017 4018 4019 4020
    if (dom)
        virDomainFree(dom);
    return rv;
}

4021 4022 4023 4024 4025 4026 4027
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)
4028 4029 4030 4031
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
4032
    int len = 0;
4033 4034 4035 4036
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
4045 4046
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
4047 4048 4049 4050
        goto cleanup;
    }

    if (args->maxerrors &&
4051
        VIR_ALLOC_N(errors, args->maxerrors) < 0)
4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073
        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) {
4074
        size_t i;
4075 4076 4077 4078 4079 4080 4081
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

4082 4083 4084 4085 4086 4087 4088 4089 4090 4091
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;
4092
    size_t i;
4093 4094 4095 4096 4097
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

    if (!priv->conn) {
4098
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109
        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;

4110 4111 4112 4113 4114 4115 4116
    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;
    }

4117
    if (snaps && nsnaps) {
4118
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156
            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;
4157
    size_t i;
4158 4159 4160 4161 4162 4163
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;
    virDomainSnapshotPtr snapshot = NULL;

    if (!priv->conn) {
4164
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178
        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;

4179 4180 4181 4182 4183 4184 4185
    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;
    }

4186
    if (snaps && nsnaps) {
4187
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0)
4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217
            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;
}

4218 4219 4220 4221 4222 4223 4224 4225 4226 4227
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;
4228
    size_t i;
4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241
    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;

4242 4243 4244 4245 4246 4247 4248
    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;
    }

4249
    if (pools && npools) {
4250
        if (VIR_ALLOC_N(ret->pools.pools_val, npools) < 0)
4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276
            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;
}

4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287
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;
4288
    size_t i;
4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304
    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;

4305 4306 4307 4308 4309 4310 4311
    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;
    }

4312
    if (vols && nvols) {
4313
        if (VIR_ALLOC_N(ret->vols.vols_val, nvols) < 0)
4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336
            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);
    }
4337 4338
    if (pool)
        virStoragePoolFree(pool);
4339 4340 4341
    return rv;
}

4342 4343 4344 4345 4346 4347 4348 4349 4350 4351
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;
4352
    size_t i;
4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365
    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;

4366 4367 4368 4369 4370 4371 4372
    if (nnets > REMOTE_NETWORK_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many networks '%d' for limit '%d'"),
                       nnets, REMOTE_NETWORK_LIST_MAX);
        goto cleanup;
    }

4373
    if (nets && nnets) {
4374
        if (VIR_ALLOC_N(ret->nets.nets_val, nnets) < 0)
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
            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;
}

4401 4402 4403 4404 4405 4406 4407 4408 4409 4410
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;
4411
    size_t i;
4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424
    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;

4425 4426 4427 4428 4429 4430 4431
    if (nifaces > REMOTE_INTERFACE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many interfaces '%d' for limit '%d'"),
                       nifaces, REMOTE_INTERFACE_LIST_MAX);
        goto cleanup;
    }

4432
    if (ifaces && nifaces) {
4433
        if (VIR_ALLOC_N(ret->ifaces.ifaces_val, nifaces) < 0)
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
            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;
}

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

4484 4485 4486 4487 4488 4489 4490
    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;
    }

4491
    if (devices && ndevices) {
4492
        if (VIR_ALLOC_N(ret->devices.devices_val, ndevices) < 0)
4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517
            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;
}
4518

4519 4520 4521 4522 4523 4524 4525 4526 4527 4528
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;
4529
    size_t i;
4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542
    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;

4543 4544 4545 4546 4547 4548 4549
    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;
    }

4550
    if (filters && nfilters) {
4551
        if (VIR_ALLOC_N(ret->filters.filters_val, nfilters) < 0)
4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577
            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;
}

4578 4579 4580 4581 4582 4583 4584 4585 4586 4587
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;
4588
    size_t i;
4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601
    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;

4602 4603 4604 4605 4606 4607 4608
    if (nsecrets > REMOTE_SECRET_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many secrets '%d' for limit '%d'"),
                       nsecrets, REMOTE_SECRET_LIST_MAX);
        goto cleanup;
    }

4609
    if (secrets && nsecrets) {
4610
        if (VIR_ALLOC_N(ret->secrets.secrets_val, nsecrets) < 0)
4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636
            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;
}

4637 4638 4639 4640 4641 4642 4643 4644 4645
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;
4646
    int nparams = 0;
4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658
    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;

4659
    if (args->nparams > REMOTE_NODE_MEMORY_PARAMETERS_MAX) {
4660 4661 4662
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
4663
    if (args->nparams && VIR_ALLOC_N(params, args->nparams) < 0)
4664
        goto cleanup;
4665
    nparams = args->nparams;
4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689

    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);
4690
    virTypedParamsFree(params, nparams);
4691 4692 4693
    return rv;
}

4694 4695 4696 4697 4698 4699 4700 4701 4702
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;
4703
    unsigned int online = 0;
4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716
    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;

4717 4718
    cpunum = virNodeGetCPUMap(priv->conn, args->need_map ? &cpumap : NULL,
                              args->need_online ? &online : NULL, flags);
4719 4720 4721 4722
    if (cpunum < 0)
        goto cleanup;

    /* 'serialize' return cpumap */
4723
    if (args->need_map) {
4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740
        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;
}

4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773
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
     */
4774
    for (i = 0; i < msg->nfds; i++)
4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786
        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);
4787 4788
    if (dom)
        virDomainFree(dom);
4789 4790 4791
    return rv;
}

4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818
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;

4819 4820 4821 4822 4823 4824 4825
    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;
    }

4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842
    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;
}

4843
static int
4844 4845 4846 4847 4848 4849
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)
4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865
{
    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;
    }

4866 4867 4868 4869 4870 4871 4872
    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;
    }

4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901
    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
4902 4903 4904 4905 4906 4907
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)
4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922
{
    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;
    }

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

4930 4931 4932 4933 4934 4935
    if (!(params = remoteDeserializeTypedParameters(args->params.params_val,
                                                    args->params.params_len,
                                                    0, &nparams)))
        goto cleanup;

    /* Wacky world of XDR ... */
4936
    if (VIR_ALLOC(uri_out) < 0)
4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961
        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
4962 4963 4964 4965 4966 4967
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)
4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983
{
    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;
    }

4984 4985 4986 4987 4988 4989 4990
    if (args->params.params_len > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       args->params.params_len, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

4991 4992 4993 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 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031
    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
5032 5033 5034 5035 5036 5037
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)
5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053
{
    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;
    }

5054 5055 5056 5057 5058 5059 5060
    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;
    }

5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093
    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
5094 5095 5096 5097 5098 5099
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)
5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114
{
    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;
    }

5115 5116 5117 5118 5119 5120 5121
    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;
    }

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 5147 5148 5149 5150 5151 5152 5153 5154
    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
5155 5156 5157 5158 5159
remoteDispatchDomainMigrateConfirm3Params(virNetServerPtr server ATTRIBUTE_UNUSED,
                                          virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                          virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                          virNetMessageErrorPtr rerr,
                                          remote_domain_migrate_confirm3_params_args *args)
5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172
{
    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;
    }

5173 5174 5175 5176 5177 5178 5179
    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;
    }

5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205
    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;
}


5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257
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;
}


5258 5259 5260 5261 5262 5263 5264
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)
5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307
{
    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;
}


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


5360 5361
static int
remoteDispatchConnectNetworkEventRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5362
                                             virNetServerClientPtr client,
5363 5364 5365
                                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                             virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                             remote_connect_network_event_register_any_args *args,
5366
                                             remote_connect_network_event_register_any_ret *ret)
5367 5368 5369
{
    int callbackID;
    int rv = -1;
5370 5371
    daemonClientEventCallbackPtr callback = NULL;
    daemonClientEventCallbackPtr ref;
5372 5373
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
5374
    virNetworkPtr net = NULL;
5375 5376 5377 5378 5379 5380 5381 5382

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

    virMutexLock(&priv->lock);

5383 5384 5385 5386
    if (args->net &&
        !(net = get_nonnull_network(priv->conn, *args->net)))
        goto cleanup;

5387 5388 5389
    if (args->eventID >= VIR_NETWORK_EVENT_ID_LAST || args->eventID < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unsupported network event ID %d"), args->eventID);
5390 5391 5392
        goto cleanup;
    }

5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407
    /* 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)
5408 5409 5410
        goto cleanup;

    if ((callbackID = virConnectNetworkEventRegisterAny(priv->conn,
5411
                                                        net,
5412 5413
                                                        args->eventID,
                                                        networkEventCallbacks[args->eventID],
5414 5415 5416 5417 5418
                                                        ref,
                                                        remoteEventCallbackFree)) < 0) {
        VIR_SHRINK_N(priv->networkEventCallbacks,
                     priv->nnetworkEventCallbacks, 1);
        callback = ref;
5419
        goto cleanup;
5420
    }
5421

5422
    ref->callbackID = callbackID;
5423
    ret->callbackID = callbackID;
5424 5425 5426 5427

    rv = 0;

cleanup:
5428
    VIR_FREE(callback);
5429 5430
    if (rv < 0)
        virNetMessageSaveError(rerr);
5431 5432
    if (net)
        virNetworkFree(net);
5433 5434 5435 5436 5437 5438 5439
    virMutexUnlock(&priv->lock);
    return rv;
}


static int
remoteDispatchConnectNetworkEventDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
5440
                                               virNetServerClientPtr client,
5441 5442
                                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                               virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
5443
                                               remote_connect_network_event_deregister_any_args *args)
5444 5445
{
    int rv = -1;
5446
    size_t i;
5447 5448 5449 5450 5451 5452 5453 5454 5455 5456
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    virMutexLock(&priv->lock);

5457
    for (i = 0; i < priv->nnetworkEventCallbacks; i++) {
5458
        if (priv->networkEventCallbacks[i]->callbackID == args->callbackID)
5459 5460
            break;
    }
5461
    if (i == priv->nnetworkEventCallbacks) {
5462
        virReportError(VIR_ERR_INTERNAL_ERROR,
5463 5464
                       _("network event callback %d not registered"),
                       args->callbackID);
5465 5466 5467
        goto cleanup;
    }

5468
    if (virConnectNetworkEventDeregisterAny(priv->conn, args->callbackID) < 0)
5469 5470
        goto cleanup;

5471 5472
    VIR_DELETE_ELEMENT(priv->networkEventCallbacks, i,
                       priv->nnetworkEventCallbacks);
5473 5474 5475 5476 5477 5478 5479 5480 5481 5482

    rv = 0;

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

5483

5484 5485 5486 5487 5488 5489 5490 5491 5492
/*----- 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
5493
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
5494 5495
{
    virDomainPtr dom;
5496
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
5497 5498 5499 5500 5501 5502 5503 5504
    /* 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
5505
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
5506
{
5507
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
5508 5509
}

D
Daniel Veillard 已提交
5510
static virInterfacePtr
5511
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
5512
{
5513
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
5514 5515
}

5516
static virStoragePoolPtr
5517
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
5518
{
5519 5520
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
5521 5522 5523
}

static virStorageVolPtr
5524
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
5525 5526
{
    virStorageVolPtr ret;
5527 5528
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
5529 5530 5531
    return ret;
}

5532
static virSecretPtr
5533
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
5534
{
5535
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
5536 5537
}

5538
static virNWFilterPtr
5539
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
5540
{
5541
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
5542 5543
}

C
Chris Lalancette 已提交
5544
static virDomainSnapshotPtr
5545
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
5546
{
5547
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
5548 5549
}

5550 5551
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
5552
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
5553 5554
{
    dom_dst->id = dom_src->id;
5555
    ignore_value(VIR_STRDUP_QUIET(dom_dst->name, dom_src->name));
5556
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
5557 5558 5559
}

static void
5560
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
5561
{
5562
    ignore_value(VIR_STRDUP_QUIET(net_dst->name, net_src->name));
5563
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
5564 5565
}

D
Daniel Veillard 已提交
5566
static void
5567 5568
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
5569
{
5570 5571
    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 已提交
5572 5573
}

5574
static void
5575
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
5576
{
5577
    ignore_value(VIR_STRDUP_QUIET(pool_dst->name, pool_src->name));
5578
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
5579 5580 5581
}

static void
5582
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
5583
{
5584 5585 5586
    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));
5587
}
5588 5589

static void
5590
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
5591
{
5592
    ignore_value(VIR_STRDUP_QUIET(dev_dst->name, dev_src->name));
5593
}
5594 5595

static void
5596
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
5597
{
5598
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
5599
    secret_dst->usageType = secret_src->usageType;
5600
    ignore_value(VIR_STRDUP_QUIET(secret_dst->usageID, secret_src->usageID));
5601
}
5602 5603

static void
5604
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
5605
{
5606
    ignore_value(VIR_STRDUP_QUIET(nwfilter_dst->name, nwfilter_src->name));
5607
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
5608
}
C
Chris Lalancette 已提交
5609 5610

static void
5611
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
5612
{
5613
    ignore_value(VIR_STRDUP_QUIET(snapshot_dst->name, snapshot_src->name));
5614
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
5615
}
5616 5617 5618 5619 5620 5621 5622 5623

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;
5624
    size_t i = 0;
5625

5626
    if (VIR_ALLOC_N(val, nerrors) < 0)
5627
        goto error;
5628 5629

    for (i = 0; i < nerrors; i++) {
5630 5631
        if (VIR_STRDUP(val[i].disk, errors[i].disk) < 0)
            goto error;
5632 5633 5634 5635 5636 5637 5638 5639
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

5640
error:
5641
    if (val) {
5642
        size_t j;
5643 5644 5645 5646 5647 5648
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    return -1;
}