remote.c 118.3 KB
Newer Older
1
/*
2
 * remote.c: handlers for RPC method calls
3
 *
4
 * Copyright (C) 2007-2012 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Richard W.M. Jones <rjones@redhat.com>
 */

#include <config.h>

25
#include "virterror_internal.h"
26

27
#if HAVE_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 "memory.h"
37
#include "logging.h"
38
#include "util.h"
C
Chris Lalancette 已提交
39
#include "stream.h"
40
#include "uuid.h"
C
Chris Lalancette 已提交
41
#include "libvirt/libvirt-qemu.h"
42
#include "command.h"
43
#include "intprops.h"
44
#include "virnetserverservice.h"
J
Jim Fehlig 已提交
45
#include "virnetserver.h"
46
#include "virfile.h"
47
#include "virtypedparam.h"
48 49 50

#include "remote_protocol.h"
#include "qemu_protocol.h"
51

52 53

#define VIR_FROM_THIS VIR_FROM_RPC
54

55 56 57 58
#define virNetError(code, ...)                                    \
    virReportErrorHelper(VIR_FROM_THIS, code, __FILE__,           \
                         __FUNCTION__, __LINE__, __VA_ARGS__)

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#if SIZEOF_LONG < 8
# define HYPER_TO_TYPE(_type, _to, _from)                                     \
    do {                                                                      \
        if ((_from) != (_type)(_from)) {                                      \
            virNetError(VIR_ERR_INTERNAL_ERROR,                               \
                        _("conversion from hyper to %s overflowed"), #_type); \
            goto cleanup;                                                     \
        }                                                                     \
        (_to) = (_from);                                                      \
    } 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

77 78 79 80 81 82 83
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);
84
static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot);
85 86 87 88 89 90 91 92 93
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);
94

95 96 97 98 99
static virTypedParameterPtr
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
                                 int limit,
                                 int *nparams);
100

101 102 103 104 105 106
static int
remoteSerializeDomainDiskErrors(virDomainDiskErrorPtr errors,
                                int nerrors,
                                remote_domain_disk_error **ret_errors_val,
                                u_int *ret_errors_len);

107 108
#include "remote_dispatch.h"
#include "qemu_dispatch.h"
C
Chris Lalancette 已提交
109 110


111 112
/* Prototypes */
static void
113 114
remoteDispatchDomainEventSend(virNetServerClientPtr client,
                              virNetServerProgramPtr program,
115 116 117
                              int procnr,
                              xdrproc_t proc,
                              void *data);
118

119 120 121 122 123
static int remoteRelayDomainEventLifecycle(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           int event,
                                           int detail,
                                           void *opaque)
124
{
125
    virNetServerClientPtr client = opaque;
126
    remote_domain_event_lifecycle_msg data;
127

128 129 130
    if (!client)
        return -1;

131
    VIR_DEBUG("Relaying domain lifecycle event %d %d", event, detail);
132

133 134
    /* build return data */
    memset(&data, 0, sizeof data);
135
    make_nonnull_domain(&data.dom, dom);
136 137
    data.event = event;
    data.detail = detail;
138

139
    remoteDispatchDomainEventSend(client, remoteProgram,
140 141
                                  REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
                                  (xdrproc_t)xdr_remote_domain_event_lifecycle_msg, &data);
142

143 144
    return 0;
}
145

146 147 148 149
static int remoteRelayDomainEventReboot(virConnectPtr conn ATTRIBUTE_UNUSED,
                                        virDomainPtr dom,
                                        void *opaque)
{
150
    virNetServerClientPtr client = opaque;
151 152 153 154 155
    remote_domain_event_reboot_msg data;

    if (!client)
        return -1;

156
    VIR_DEBUG("Relaying domain reboot event %s %d", dom->name, dom->id);
157 158 159

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

162
    remoteDispatchDomainEventSend(client, remoteProgram,
163 164
                                  REMOTE_PROC_DOMAIN_EVENT_REBOOT,
                                  (xdrproc_t)xdr_remote_domain_event_reboot_msg, &data);
165 166 167 168

    return 0;
}

169

170 171 172 173 174
static int remoteRelayDomainEventRTCChange(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           long long offset,
                                           void *opaque)
{
175
    virNetServerClientPtr client = opaque;
176 177 178 179 180
    remote_domain_event_rtc_change_msg data;

    if (!client)
        return -1;

181
    VIR_DEBUG("Relaying domain rtc change event %s %d %lld", dom->name, dom->id, offset);
182 183 184

    /* build return data */
    memset(&data, 0, sizeof data);
185
    make_nonnull_domain(&data.dom, dom);
186 187
    data.offset = offset;

188
    remoteDispatchDomainEventSend(client, remoteProgram,
189 190
                                  REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_rtc_change_msg, &data);
191 192 193 194 195

    return 0;
}


196 197 198 199 200
static int remoteRelayDomainEventWatchdog(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          int action,
                                          void *opaque)
{
201
    virNetServerClientPtr client = opaque;
202 203 204 205 206
    remote_domain_event_watchdog_msg data;

    if (!client)
        return -1;

207
    VIR_DEBUG("Relaying domain watchdog event %s %d %d", dom->name, dom->id, action);
208 209 210

    /* build return data */
    memset(&data, 0, sizeof data);
211
    make_nonnull_domain(&data.dom, dom);
212 213
    data.action = action;

214
    remoteDispatchDomainEventSend(client, remoteProgram,
215 216
                                  REMOTE_PROC_DOMAIN_EVENT_WATCHDOG,
                                  (xdrproc_t)xdr_remote_domain_event_watchdog_msg, &data);
217 218 219 220 221

    return 0;
}


222 223 224 225 226 227 228
static int remoteRelayDomainEventIOError(virConnectPtr conn ATTRIBUTE_UNUSED,
                                         virDomainPtr dom,
                                         const char *srcPath,
                                         const char *devAlias,
                                         int action,
                                         void *opaque)
{
229
    virNetServerClientPtr client = opaque;
230 231 232 233 234
    remote_domain_event_io_error_msg data;

    if (!client)
        return -1;

235
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d", dom->name, dom->id, srcPath, devAlias, action);
236 237 238

    /* build return data */
    memset(&data, 0, sizeof data);
239 240 241 242 243 244
    data.srcPath = strdup(srcPath);
    if (data.srcPath == NULL)
        goto mem_error;
    data.devAlias = strdup(devAlias);
    if (data.devAlias == NULL)
        goto mem_error;
245
    make_nonnull_domain(&data.dom, dom);
246 247
    data.action = action;

248
    remoteDispatchDomainEventSend(client, remoteProgram,
249 250
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_msg, &data);
251 252

    return 0;
253 254
mem_error:
    virReportOOMError();
E
Eric Blake 已提交
255 256
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
257
    return -1;
258 259 260
}


261 262 263 264 265 266 267 268
static int remoteRelayDomainEventIOErrorReason(virConnectPtr conn ATTRIBUTE_UNUSED,
                                               virDomainPtr dom,
                                               const char *srcPath,
                                               const char *devAlias,
                                               int action,
                                               const char *reason,
                                               void *opaque)
{
269
    virNetServerClientPtr client = opaque;
270 271 272 273 274
    remote_domain_event_io_error_reason_msg data;

    if (!client)
        return -1;

275 276
    VIR_DEBUG("Relaying domain io error %s %d %s %s %d %s",
              dom->name, dom->id, srcPath, devAlias, action, reason);
277 278 279

    /* build return data */
    memset(&data, 0, sizeof data);
280 281 282 283 284 285
    data.srcPath = strdup(srcPath);
    if (data.srcPath == NULL)
        goto mem_error;
    data.devAlias = strdup(devAlias);
    if (data.devAlias == NULL)
        goto mem_error;
286
    data.action = action;
287 288 289 290 291
    data.reason = strdup(reason);
    if (data.reason == NULL)
        goto mem_error;

    make_nonnull_domain(&data.dom, dom);
292

293
    remoteDispatchDomainEventSend(client, remoteProgram,
294 295
                                  REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON,
                                  (xdrproc_t)xdr_remote_domain_event_io_error_reason_msg, &data);
296 297

    return 0;
298 299 300

mem_error:
    virReportOOMError();
E
Eric Blake 已提交
301 302 303
    VIR_FREE(data.srcPath);
    VIR_FREE(data.devAlias);
    VIR_FREE(data.reason);
304
    return -1;
305 306 307
}


308 309 310 311 312 313 314 315 316
static int remoteRelayDomainEventGraphics(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          int phase,
                                          virDomainEventGraphicsAddressPtr local,
                                          virDomainEventGraphicsAddressPtr remote,
                                          const char *authScheme,
                                          virDomainEventGraphicsSubjectPtr subject,
                                          void *opaque)
{
317
    virNetServerClientPtr client = opaque;
318 319 320 321 322 323
    remote_domain_event_graphics_msg data;
    int i;

    if (!client)
        return -1;

324 325 326 327
    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);
328

329
    VIR_DEBUG("Subject %d", subject->nidentity);
330
    for (i = 0 ; i < subject->nidentity ; i++) {
331
        VIR_DEBUG("  %s=%s", subject->identities[i].type, subject->identities[i].name);
332 333 334 335 336 337 338
    }

    /* build return data */
    memset(&data, 0, sizeof data);
    data.phase = phase;
    data.local.family = local->family;
    data.remote.family = remote->family;
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    data.authScheme = strdup(authScheme);
    if (data.authScheme == NULL)
        goto mem_error;

    data.local.node = strdup(local->node);
    if (data.local.node == NULL)
        goto mem_error;
    data.local.service = strdup(local->service);
    if (data.local.service == NULL)
        goto mem_error;

    data.remote.node = strdup(remote->node);
    if (data.remote.node == NULL)
        goto mem_error;
    data.remote.service = strdup(remote->service);
    if (data.remote.service == NULL)
        goto mem_error;
356 357

    data.subject.subject_len = subject->nidentity;
358 359 360
    if (VIR_ALLOC_N(data.subject.subject_val, data.subject.subject_len) < 0)
        goto mem_error;

361
    for (i = 0 ; i < data.subject.subject_len ; i++) {
362 363 364 365 366 367
        data.subject.subject_val[i].type = strdup(subject->identities[i].type);
        if (data.subject.subject_val[i].type == NULL)
            goto mem_error;
        data.subject.subject_val[i].name = strdup(subject->identities[i].name);
        if (data.subject.subject_val[i].name == NULL)
            goto mem_error;
368
    }
369
    make_nonnull_domain(&data.dom, dom);
370

371
    remoteDispatchDomainEventSend(client, remoteProgram,
372 373
                                  REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
                                  (xdrproc_t)xdr_remote_domain_event_graphics_msg, &data);
374 375

    return 0;
376 377 378

mem_error:
    virReportOOMError();
E
Eric Blake 已提交
379 380 381 382 383
    VIR_FREE(data.authScheme);
    VIR_FREE(data.local.node);
    VIR_FREE(data.local.service);
    VIR_FREE(data.remote.node);
    VIR_FREE(data.remote.service);
384 385
    if (data.subject.subject_val != NULL) {
        for (i = 0 ; i < data.subject.subject_len ; i++) {
E
Eric Blake 已提交
386 387
            VIR_FREE(data.subject.subject_val[i].type);
            VIR_FREE(data.subject.subject_val[i].name);
388
        }
E
Eric Blake 已提交
389
        VIR_FREE(data.subject.subject_val);
390 391
    }
    return -1;
392 393
}

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
static int remoteRelayDomainEventBlockJob(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          const char *path,
                                          int type,
                                          int status,
                                          void *opaque)
{
    virNetServerClientPtr client = opaque;
    remote_domain_event_block_job_msg data;

    if (!client)
        return -1;

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

    /* build return data */
    memset(&data, 0, sizeof data);
412 413 414
    data.path = strdup(path);
    if (data.path == NULL)
        goto mem_error;
415 416
    data.type = type;
    data.status = status;
417
    make_nonnull_domain(&data.dom, dom);
418 419 420 421 422 423

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB,
                                  (xdrproc_t)xdr_remote_domain_event_block_job_msg, &data);

    return 0;
424 425 426

mem_error:
    virReportOOMError();
E
Eric Blake 已提交
427
    VIR_FREE(data.path);
428
    return -1;
429 430
}

431

432 433 434 435
static int remoteRelayDomainEventControlError(virConnectPtr conn ATTRIBUTE_UNUSED,
                                              virDomainPtr dom,
                                              void *opaque)
{
436
    virNetServerClientPtr client = opaque;
437 438 439 440 441 442 443 444 445 446 447
    remote_domain_event_control_error_msg data;

    if (!client)
        return -1;

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

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

448
    remoteDispatchDomainEventSend(client, remoteProgram,
449 450 451 452 453 454 455
                                  REMOTE_PROC_DOMAIN_EVENT_CONTROL_ERROR,
                                  (xdrproc_t)xdr_remote_domain_event_control_error_msg, &data);

    return 0;
}


456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
static int remoteRelayDomainEventDiskChange(virConnectPtr conn ATTRIBUTE_UNUSED,
                                            virDomainPtr dom,
                                            const char *oldSrcPath,
                                            const char *newSrcPath,
                                            const char *devAlias,
                                            int reason,
                                            void *opaque)
{
    virNetServerClientPtr client = opaque;
    remote_domain_event_disk_change_msg data;
    char **oldSrcPath_p = NULL, **newSrcPath_p = NULL;

    if (!client)
        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 */
    memset(&data, 0, sizeof data);
    if (oldSrcPath &&
        ((VIR_ALLOC(oldSrcPath_p) < 0) ||
         !(*oldSrcPath_p = strdup(oldSrcPath))))
        goto mem_error;

    if (newSrcPath &&
        ((VIR_ALLOC(newSrcPath_p) < 0) ||
         !(*newSrcPath_p = strdup(newSrcPath))))
        goto mem_error;

    data.oldSrcPath = oldSrcPath_p;
    data.newSrcPath = newSrcPath_p;
    if (!(data.devAlias = strdup(devAlias)))
        goto mem_error;
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_DISK_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_disk_change_msg, &data);

    return 0;

mem_error:
M
Michal Privoznik 已提交
501 502
    VIR_FREE(oldSrcPath_p);
    VIR_FREE(newSrcPath_p);
503 504 505 506 507
    virReportOOMError();
    return -1;
}


508
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
509
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
510
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
511
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
512
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
513
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
514
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
515
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
516
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
517
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
518
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
519 520 521 522
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
/*
 * 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
 */
static void remoteClientFreeFunc(void *data)
{
    struct daemonClientPrivate *priv = data;

    /* Deregister event delivery callback */
    if (priv->conn) {
        int i;

        for (i = 0 ; i < VIR_DOMAIN_EVENT_ID_LAST ; i++) {
            if (priv->domainEventCallbackID[i] != -1) {
                VIR_DEBUG("Deregistering to relay remote events %d", i);
                virConnectDomainEventDeregisterAny(priv->conn,
                                                   priv->domainEventCallbackID[i]);
            }
            priv->domainEventCallbackID[i] = -1;
        }

        virConnectClose(priv->conn);
    }

    VIR_FREE(priv);
}


554 555 556 557 558 559 560
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581

int remoteClientInitHook(virNetServerPtr srv ATTRIBUTE_UNUSED,
                         virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv;
    int i;

    if (VIR_ALLOC(priv) < 0) {
        virReportOOMError();
        return -1;
    }

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
        virReportOOMError();
        return -1;
    }

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

582 583 584
    virNetServerClientSetPrivateData(client, priv,
                                     remoteClientFreeFunc);
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
585 586 587
    return 0;
}

588 589 590
/*----- Functions. -----*/

static int
591
remoteDispatchOpen(virNetServerPtr server,
592
                   virNetServerClientPtr client,
593
                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
594 595
                   virNetMessageErrorPtr rerr,
                   struct remote_open_args *args)
596 597
{
    const char *name;
598
    unsigned int flags;
599
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
600
    int rv = -1;
601

602 603 604 605
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
606 607 608 609
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
        goto cleanup;
    }

610 611 612 613 614 615
    if (virNetServerKeepAliveRequired(server) && !priv->keepalive_supported) {
        virNetError(VIR_ERR_OPERATION_FAILED, "%s",
                    _("keepalive support is required to connect"));
        goto cleanup;
    }

616 617 618 619 620 621
    name = args->name ? *args->name : NULL;

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

625
    priv->conn =
626
        flags & VIR_CONNECT_RO
627 628
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
629

630
    if (priv->conn == NULL)
631 632 633
        goto cleanup;

    rv = 0;
634

635 636
cleanup:
    if (rv < 0)
637 638
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
639
    return rv;
640 641 642 643
}


static int
644
remoteDispatchClose(virNetServerPtr server ATTRIBUTE_UNUSED,
645
                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
646
                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
647
                    virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
648
{
649
    virNetServerClientDelayedClose(client);
650
    return 0;
651 652
}

653

654
static int
655 656
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
657
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
658
                                     virNetMessageErrorPtr rerr,
659 660
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
661
{
662
    virDomainPtr dom = NULL;
663 664
    char *type;
    int nparams;
665
    int rv = -1;
666 667
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
668

669
    if (!priv->conn) {
670 671
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
672 673
    }

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

677
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
678
        goto cleanup;
679 680 681

    ret->type = type;
    ret->nparams = nparams;
682 683 684 685
    rv = 0;

cleanup:
    if (rv < 0)
686
        virNetMessageSaveError(rerr);
687 688 689
    if (dom)
        virDomainFree(dom);
    return rv;
690 691
}

692 693
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
694 695 696
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
697
                               remote_typed_param **ret_params_val,
698 699
                               u_int *ret_params_len,
                               unsigned int flags)
700 701
{
    int i;
702
    int j;
703 704 705 706 707 708 709 710 711
    int rv = -1;
    remote_typed_param *val;

    *ret_params_len = nparams;
    if (VIR_ALLOC_N(val, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

712
    for (i = 0, j = 0; i < nparams; ++i) {
713 714 715 716 717
        /* 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)) {
718 719 720 721
            --*ret_params_len;
            continue;
        }

722
        /* remoteDispatchClientRequest will free this: */
723 724
        val[j].field = strdup(params[i].field);
        if (val[j].field == NULL) {
725 726 727
            virReportOOMError();
            goto cleanup;
        }
728
        val[j].value.type = params[i].type;
729
        switch (params[i].type) {
730
        case VIR_TYPED_PARAM_INT:
731
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
732 733
            break;
        case VIR_TYPED_PARAM_UINT:
734
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
735 736
            break;
        case VIR_TYPED_PARAM_LLONG:
737
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
738 739
            break;
        case VIR_TYPED_PARAM_ULLONG:
740
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
741 742
            break;
        case VIR_TYPED_PARAM_DOUBLE:
743
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
744 745
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
746 747 748 749 750 751 752 753 754
            val[j].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
        case VIR_TYPED_PARAM_STRING:
            val[j].value.remote_typed_param_value_u.s =
                strdup(params[i].value.s);
            if (val[j].value.remote_typed_param_value_u.s == NULL) {
                virReportOOMError();
                goto cleanup;
            }
755 756 757 758 759 760
            break;
        default:
            virNetError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                        params[i].type);
            goto cleanup;
        }
761
        j++;
762 763 764 765 766 767 768 769
    }

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

cleanup:
    if (val) {
770
        for (i = 0; i < nparams; i++) {
771
            VIR_FREE(val[i].field);
772
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
773 774
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
775 776 777 778 779 780 781
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
782 783
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
784 785 786
                                 int limit,
                                 int *nparams)
{
787
    int i = 0;
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
    if (args_params_len > limit) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, args_params_len) < 0) {
        virReportOOMError();
        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) {
            virNetError(VIR_ERR_INTERNAL_ERROR,
                        _("Parameter %s too big for destination"),
                        args_params_val[i].field);
            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;
838 839 840 841 842 843 844 845
        case VIR_TYPED_PARAM_STRING:
            params[i].value.s =
                strdup(args_params_val[i].value.remote_typed_param_value_u.s);
            if (params[i].value.s == NULL) {
                virReportOOMError();
                goto cleanup;
            }
            break;
846 847 848 849 850 851 852 853 854 855
        default:
            virNetError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                        params[i].type);
            goto cleanup;
        }
    }

    rv = 0;

cleanup:
856
    if (rv < 0) {
A
Alex Jia 已提交
857
        virTypedParameterArrayClear(params, i);
858
        VIR_FREE(params);
859
    }
860 861 862
    return params;
}

863
static int
864 865
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
866
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
867
                                           virNetMessageErrorPtr rerr,
868 869
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
870
{
871
    virDomainPtr dom = NULL;
872
    virTypedParameterPtr params = NULL;
873
    int nparams = args->nparams;
874
    int rv = -1;
875 876
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
877

878
    if (!priv->conn) {
879 880
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
881 882
    }

883
    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
884 885
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
886
    }
887 888
    if (VIR_ALLOC_N(params, nparams) < 0)
        goto no_memory;
889

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

893
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
894
        goto cleanup;
895

896
    if (remoteSerializeTypedParameters(params, nparams,
897
                                       &ret->params.params_val,
898 899
                                       &ret->params.params_len,
                                       0) < 0)
900 901 902 903 904 905
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
906
        virNetMessageSaveError(rerr);
907 908
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
909 910 911 912 913 914 915 916 917 918
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

static int
919 920
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
921
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
922
                                                virNetMessageErrorPtr rerr,
923 924 925 926 927 928 929
                                                remote_domain_get_scheduler_parameters_flags_args *args,
                                                remote_domain_get_scheduler_parameters_flags_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = args->nparams;
    int rv = -1;
930 931
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
932

933
    if (!priv->conn) {
934 935 936 937 938 939 940 941 942 943 944
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0)
        goto no_memory;

945
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
946 947 948 949 950 951 952
        goto cleanup;

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

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

958
    rv = 0;
959 960

cleanup:
961
    if (rv < 0)
962
        virNetMessageSaveError(rerr);
963 964
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
965 966 967 968 969 970 971
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
972 973
}

974
static int
975 976
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
977
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
978
                                virNetMessageErrorPtr rerr,
979 980
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
981
{
982
    virDomainPtr dom = NULL;
983
    struct _virDomainMemoryStat *stats;
984
    int nr_stats, i;
985
    int rv = -1;
986 987
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
988

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

994
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
995 996 997
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
        goto cleanup;
998 999
    }

1000
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1001
        goto cleanup;
1002 1003 1004

    /* Allocate stats array for making dispatch call */
    if (VIR_ALLOC_N(stats, args->maxStats) < 0) {
1005 1006
        virReportOOMError();
        goto cleanup;
1007
    }
1008

1009
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1010
    if (nr_stats < 0)
1011
        goto cleanup;
1012 1013 1014

    /* Allocate return buffer */
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0) {
1015 1016
        virReportOOMError();
        goto cleanup;
1017 1018 1019 1020 1021 1022 1023 1024
    }

    /* 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;
1025 1026 1027 1028
    rv = 0;

cleanup:
    if (rv < 0)
1029
        virNetMessageSaveError(rerr);
1030 1031
    if (dom)
        virDomainFree(dom);
1032
    VIR_FREE(stats);
1033
    return rv;
1034 1035
}

1036
static int
1037 1038
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1039
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1040
                              virNetMessageErrorPtr rerr,
1041 1042
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1043
{
1044
    virDomainPtr dom = NULL;
1045 1046 1047 1048
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1049
    int rv = -1;
1050 1051
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1052

1053
    if (!priv->conn) {
1054 1055
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1056 1057
    }

1058
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1059
        goto cleanup;
1060 1061 1062 1063 1064 1065
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1066 1067 1068
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("size > maximum buffer size"));
        goto cleanup;
1069 1070 1071
    }

    ret->buffer.buffer_len = size;
1072
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
1073 1074
        virReportOOMError();
        goto cleanup;
1075 1076
    }

1077
    if (virDomainBlockPeek(dom, path, offset, size,
1078
                           ret->buffer.buffer_val, flags) < 0)
1079
        goto cleanup;
1080

1081 1082 1083 1084
    rv = 0;

cleanup:
    if (rv < 0) {
1085
        virNetMessageSaveError(rerr);
1086 1087 1088 1089 1090
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
1091 1092
}

1093 1094 1095
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1096
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
                                    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;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if (nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    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,
1142 1143
                                       &ret->params.params_len,
                                       args->flags) < 0)
1144 1145 1146 1147 1148 1149
        goto cleanup;

success:
    rv = 0;

cleanup:
1150
    if (rv < 0)
1151
        virNetMessageSaveError(rerr);
1152 1153
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
A
ajia@redhat.com 已提交
1154 1155
    if (dom)
        virDomainFree(dom);
1156 1157 1158
    return rv;
}

R
Richard W.M. Jones 已提交
1159
static int
1160 1161
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1162
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1163
                               virNetMessageErrorPtr rerr,
1164 1165
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1166
{
1167
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1168 1169 1170
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1171
    int rv = -1;
1172 1173
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1174

1175
    if (!priv->conn) {
1176 1177
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1178 1179
    }

1180
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1181
        goto cleanup;
R
Richard W.M. Jones 已提交
1182 1183 1184 1185 1186
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1187 1188 1189
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("size > maximum buffer size"));
        goto cleanup;
R
Richard W.M. Jones 已提交
1190 1191 1192
    }

    ret->buffer.buffer_len = size;
1193
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
1194 1195
        virReportOOMError();
        goto cleanup;
R
Richard W.M. Jones 已提交
1196 1197
    }

1198
    if (virDomainMemoryPeek(dom, offset, size,
1199
                            ret->buffer.buffer_val, flags) < 0)
1200
        goto cleanup;
R
Richard W.M. Jones 已提交
1201

1202 1203 1204 1205
    rv = 0;

cleanup:
    if (rv < 0) {
1206
        virNetMessageSaveError(rerr);
1207 1208 1209 1210 1211
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
1212 1213
}

1214
static int
1215 1216
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1217
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1218
                                     virNetMessageErrorPtr rerr,
1219 1220
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1221
{
1222 1223
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1224
    int rv = -1;
1225 1226
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1227

1228
    if (!priv->conn) {
1229 1230
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1231 1232
    }

1233
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
        goto cleanup;

    if (VIR_ALLOC(seclabel) < 0) {
        virReportOOMError();
        goto cleanup;
    }

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

    ret->label.label_len = strlen(seclabel->label) + 1;
    if (VIR_ALLOC_N(ret->label.label_val, ret->label.label_len) < 0) {
        virReportOOMError();
1247
        goto cleanup;
1248 1249 1250
    }
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1251

1252 1253 1254 1255
    rv = 0;

cleanup:
    if (rv < 0)
1256
        virNetMessageSaveError(rerr);
1257 1258 1259
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
1260
    return rv;
1261 1262 1263
}

static int
1264 1265
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1266
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1267
                                   virNetMessageErrorPtr rerr,
1268
                                   remote_node_get_security_model_ret *ret)
1269
{
1270
    virSecurityModel secmodel;
1271
    int rv = -1;
1272 1273
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1274

1275
    if (!priv->conn) {
1276 1277
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1278 1279
    }

1280
    memset(&secmodel, 0, sizeof secmodel);
1281
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
        goto cleanup;

    ret->model.model_len = strlen(secmodel.model) + 1;
    if (VIR_ALLOC_N(ret->model.model_val, ret->model.model_len) < 0) {
        virReportOOMError();
        goto cleanup;
    }
    strcpy(ret->model.model_val, secmodel.model);

    ret->doi.doi_len = strlen(secmodel.doi) + 1;
    if (VIR_ALLOC_N(ret->doi.doi_val, ret->doi.doi_len) < 0) {
        virReportOOMError();
1294
        goto cleanup;
1295 1296
    }
    strcpy(ret->doi.doi_val, secmodel.doi);
1297

1298 1299 1300 1301
    rv = 0;

cleanup:
    if (rv < 0)
1302
        virNetMessageSaveError(rerr);
1303
    return rv;
1304 1305
}

1306
static int
1307 1308
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1309
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1310
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
1311 1312
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
1313 1314 1315 1316 1317
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
1318 1319
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1320

1321
    if (!priv->conn) {
1322 1323 1324 1325
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

1326
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
        goto cleanup;

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
        goto cleanup;
    }

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

E
Eric Blake 已提交
1345
    if ((num = virDomainGetVcpuPinInfo(dom,
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
                                       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;
    cpumaps = NULL;

    rv = 0;

cleanup:
    if (rv < 0)
1365
        virNetMessageSaveError(rerr);
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

1376
static int
1377 1378
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
1379
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
1380
                             virNetMessageErrorPtr rerr,
1381 1382
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
1383
{
1384
    virDomainPtr dom = NULL;
1385 1386 1387
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
    int info_len, i;
1388
    int rv = -1;
1389 1390
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1391

1392
    if (!priv->conn) {
1393 1394
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1395 1396
    }

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

1400 1401
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
1402
        goto cleanup;
1403
    }
1404

1405 1406
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
        goto cleanup;
    }

    /* Allocate buffers to take the results. */
    if (VIR_ALLOC_N(info, args->maxinfo) < 0)
        goto no_memory;
    if (args->maplen > 0 &&
        VIR_ALLOC_N(cpumaps, args->maxinfo * args->maplen) < 0)
        goto no_memory;

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

    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;
1444 1445

cleanup:
1446
    if (rv < 0) {
1447
        virNetMessageSaveError(rerr);
1448 1449 1450 1451
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
1452 1453 1454
    if (dom)
        virDomainFree(dom);
    return rv;
1455 1456 1457 1458

no_memory:
    virReportOOMError();
    goto cleanup;
1459 1460 1461
}

static int
1462 1463
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1464
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1465
                                   virNetMessageErrorPtr rerr,
1466 1467
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
1468
{
1469 1470 1471 1472 1473
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1474
    int rv = -1;
1475 1476
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1477

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

1483 1484 1485 1486 1487 1488
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;

    /* Wacky world of XDR ... */
    if (VIR_ALLOC(uri_out) < 0) {
        virReportOOMError();
1489
        goto cleanup;
1490
    }
1491

1492
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
1493 1494
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
1495
        goto cleanup;
1496

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
    /* 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;
    }
1508

1509
    rv = 0;
1510

1511 1512
cleanup:
    if (rv < 0)
1513
        virNetMessageSaveError(rerr);
1514
    VIR_FREE(uri_out);
1515
    return rv;
1516 1517
}

1518
static int
1519 1520
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1521
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1522
                                    virNetMessageErrorPtr rerr,
1523 1524
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
1525
{
1526 1527 1528 1529 1530
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1531
    int rv = -1;
1532 1533
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1534

1535
    if (!priv->conn) {
1536 1537
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1538 1539
    }

1540 1541
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
1542

1543 1544
    /* Wacky world of XDR ... */
    if (VIR_ALLOC(uri_out) < 0) {
1545 1546
        virReportOOMError();
        goto cleanup;
1547 1548
    }

1549
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
1550 1551 1552
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
1553
        goto cleanup;
1554

1555 1556 1557 1558 1559 1560
    /* 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;
1561

1562 1563 1564 1565
    rv = 0;

cleanup:
    if (rv < 0)
1566
        virNetMessageSaveError(rerr);
1567
    return rv;
1568 1569
}

C
Chris Lalancette 已提交
1570
static int
1571 1572
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
1573
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
1574 1575 1576
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
1577
{
1578
    virDomainPtr dom = NULL;
1579
    virTypedParameterPtr params = NULL;
1580 1581
    int nparams = args->nparams;
    unsigned int flags;
1582
    int rv = -1;
1583 1584
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1585

1586
    if (!priv->conn) {
1587 1588
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1589
    }
C
Chris Lalancette 已提交
1590

1591
    flags = args->flags;
C
Chris Lalancette 已提交
1592

1593 1594 1595 1596 1597
    if (nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
1598 1599
        virReportOOMError();
        goto cleanup;
C
Chris Lalancette 已提交
1600 1601
    }

1602
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1603
        goto cleanup;
C
Chris Lalancette 已提交
1604

1605
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
1606
        goto cleanup;
C
Chris Lalancette 已提交
1607

1608 1609 1610 1611 1612 1613
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
1614 1615
    }

1616
    if (remoteSerializeTypedParameters(params, nparams,
1617
                                       &ret->params.params_val,
1618 1619
                                       &ret->params.params_len,
                                       args->flags) < 0)
1620
        goto cleanup;
1621

1622
success:
1623 1624 1625
    rv = 0;

cleanup:
1626
    if (rv < 0)
1627
        virNetMessageSaveError(rerr);
1628 1629
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
1630 1631 1632
    if (dom)
        virDomainFree(dom);
    return rv;
1633 1634
}

1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
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;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    flags = args->flags;

    if (nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    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);
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
    if (dom)
        virDomainFree(dom);
    return rv;
}

1700
static int
1701 1702
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
1703
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
1704 1705 1706
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
1707
{
1708
    virDomainPtr dom = NULL;
1709
    virTypedParameterPtr params = NULL;
1710
    int nparams = args->nparams;
1711
    unsigned int flags;
1712
    int rv = -1;
1713 1714
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1715

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

1721 1722
    flags = args->flags;

1723
    if (nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
1724 1725
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
1726 1727
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
1728 1729
        virReportOOMError();
        goto cleanup;
1730 1731
    }

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

1735
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
1736
        goto cleanup;
1737

1738 1739 1740 1741 1742 1743 1744 1745
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

1746
    if (remoteSerializeTypedParameters(params, nparams,
1747
                                       &ret->params.params_val,
1748 1749
                                       &ret->params.params_len,
                                       args->flags) < 0)
1750
        goto cleanup;
1751

1752 1753
success:
    rv = 0;
1754

1755
cleanup:
1756
    if (rv < 0)
1757
        virNetMessageSaveError(rerr);
1758
    virTypedParameterArrayClear(params, nparams);
1759
    VIR_FREE(params);
1760 1761 1762
    if (dom)
        virDomainFree(dom);
    return rv;
1763 1764
}

1765
static int
1766 1767
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1768
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1769 1770 1771
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
1772
{
1773
    virNodeCPUStatsPtr params = NULL;
1774 1775 1776 1777 1778
    int i;
    int cpuNum = args->cpuNum;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
1779 1780
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1781

1782
    if (!priv->conn) {
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    flags = args->flags;

    if (nparams > REMOTE_NODE_CPU_STATS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

1798
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
        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)
        goto no_memory;

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
        ret->params.params_val[i].field = strdup(params[i].field);
        if (ret->params.params_val[i].field == NULL)
            goto no_memory;

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

success:
    rv = 0;

1826 1827
cleanup:
    if (rv < 0) {
1828
        virNetMessageSaveError(rerr);
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
        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;

no_memory:
    virReportOOMError();
    goto cleanup;
}

static int
1844 1845
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
1846
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
1847 1848 1849
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
1850
{
1851
    virNodeMemoryStatsPtr params = NULL;
1852 1853 1854 1855 1856
    int i;
    int cellNum = args->cellNum;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
1857 1858
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1859

1860
    if (!priv->conn) {
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    flags = args->flags;

    if (nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

1876
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
        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)
        goto no_memory;

    for (i = 0; i < nparams; ++i) {
        /* remoteDispatchClientRequest will free this: */
        ret->params.params_val[i].field = strdup(params[i].field);
        if (ret->params.params_val[i].field == NULL)
            goto no_memory;

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

success:
    rv = 0;

1904 1905
cleanup:
    if (rv < 0) {
1906
        virNetMessageSaveError(rerr);
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
        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;

no_memory:
    virReportOOMError();
    goto cleanup;
}

1921 1922 1923
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1924
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
                                    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) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        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;
}

1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
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;
    int nparams = args->nparams;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    if (nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }

    if (VIR_ALLOC_N(params, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    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);
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
    if (dom)
        virDomainFree(dom);
    return rv;
}
2026

D
Daniel Veillard 已提交
2027 2028
/*-------------------------------------------------------------*/

2029
static int
2030 2031
remoteDispatchAuthList(virNetServerPtr server ATTRIBUTE_UNUSED,
                       virNetServerClientPtr client,
2032
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2033
                       virNetMessageErrorPtr rerr,
2034
                       remote_auth_list_ret *ret)
2035
{
2036
    int rv = -1;
2037 2038
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2039
    gid_t callerGid;
2040 2041 2042 2043 2044 2045 2046
    pid_t callerPid;

    /* 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) {
2047 2048
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
                                              &callerPid) < 0) {
2049 2050 2051 2052
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2053 2054
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
J
Jim Fehlig 已提交
2055 2056 2057
                            (long long) callerPid, callerUid) < 0) {
                virReportOOMError();
                goto cleanup;
2058
            }
J
Jim Fehlig 已提交
2059 2060 2061 2062 2063 2064
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
            if (virNetServerClientSetIdentity(client, ident) < 0)
                virResetLastError();
            else
                auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
            VIR_FREE(ident);
2065 2066
        }
    }
2067

2068
    ret->types.types_len = 1;
2069
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
2070 2071
        virReportOOMError();
        goto cleanup;
2072
    }
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086

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

2088 2089 2090 2091
    rv = 0;

cleanup:
    if (rv < 0)
2092
        virNetMessageSaveError(rerr);
2093
    return rv;
2094 2095 2096
}


2097
#ifdef HAVE_SASL
2098 2099
/*
 * Initializes the SASL session in prepare for authentication
2100
 * and gives the client a list of allowed mechanisms to choose
2101 2102
 */
static int
2103 2104
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2105
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2106
                           virNetMessageErrorPtr rerr,
2107
                           remote_auth_sasl_init_ret *ret)
2108
{
2109 2110 2111
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2112

2113
    virMutexLock(&priv->lock);
2114

2115 2116 2117
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2118
        VIR_ERROR(_("client tried invalid SASL init request"));
2119
        goto authfail;
2120 2121
    }

2122 2123 2124 2125 2126
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2127
        goto authfail;
2128

2129
    /* Inform SASL that we've got an external SSF layer from TLS */
2130 2131 2132 2133
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2134
            goto authfail;
2135 2136 2137 2138 2139

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2140
            goto authfail;
2141 2142
    }

2143
    if (virNetServerClientIsSecure(client))
2144
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2145 2146
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2147
        /* Plain TCP, better get an SSF layer */
2148 2149 2150 2151
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
2152

2153
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
2154
        goto authfail;
2155
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
2156

2157 2158
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
2159
    return 0;
2160 2161

authfail:
2162 2163 2164 2165
    virResetLastError();
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
2166 2167 2168
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2169 2170
    virNetSASLSessionFree(sasl);
    virMutexUnlock(&priv->lock);
2171
    return -1;
2172 2173
}

2174
/*
2175 2176
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
2177
static int
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
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;
        }
2194
    }
2195 2196

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
2197
        return -2;
2198

2199 2200
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
2201

2202 2203
    if (virNetServerClientSetIdentity(client, identity) < 0)
        goto error;
2204

2205
    virNetServerClientSetSASLSession(client, priv->sasl);
2206

2207
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
2208

2209 2210 2211
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2212

2213 2214
    virNetSASLSessionFree(priv->sasl);
    priv->sasl = NULL;
2215

2216
    return 0;
2217

2218 2219 2220
error:
    return -1;
}
2221

2222 2223 2224 2225
/*
 * This starts the SASL authentication negotiation.
 */
static int
2226 2227
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client,
2228
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2229
                            virNetMessageErrorPtr rerr,
2230 2231
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2232 2233
{
    const char *serverout;
2234
    size_t serveroutlen;
2235
    int err;
2236 2237 2238
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2239
    const char *identity;
2240

2241
    virMutexLock(&priv->lock);
2242

2243 2244 2245
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
2246
        VIR_ERROR(_("client tried invalid SASL start request"));
2247
        goto authfail;
2248 2249
    }

2250 2251
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
2252 2253 2254 2255 2256 2257 2258 2259 2260
    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)
2261
        goto authfail;
2262

2263
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2264
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
2265
        goto authfail;
2266 2267 2268 2269
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2270 2271
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2272 2273 2274 2275 2276 2277 2278
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2279
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2280
    if (err == VIR_NET_SASL_CONTINUE) {
2281 2282
        ret->complete = 0;
    } else {
2283
        /* Check username whitelist ACL */
2284
        if ((err = remoteSASLFinish(client)) < 0) {
2285 2286 2287 2288 2289
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2290

2291 2292 2293
        ret->complete = 1;
    }

2294
    virMutexUnlock(&priv->lock);
2295
    return 0;
2296 2297

authfail:
2298 2299 2300
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2301 2302 2303
    goto error;

authdeny:
2304
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2305 2306 2307
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2308 2309
    goto error;

2310
error:
2311 2312 2313 2314 2315 2316 2317 2318
    virNetSASLSessionFree(priv->sasl);
    priv->sasl = NULL;
    virResetLastError();
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2319
    return -1;
2320 2321 2322 2323
}


static int
2324 2325
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2326
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2327
                           virNetMessageErrorPtr rerr,
2328 2329
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
2330 2331
{
    const char *serverout;
2332
    size_t serveroutlen;
2333
    int err;
2334 2335 2336
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2337
    const char *identity;
2338

2339 2340 2341 2342 2343
    virMutexLock(&priv->lock);

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

2348
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
2349
              args->data.data_len, args->nil);
2350 2351 2352 2353 2354 2355 2356 2357
    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)
2358
        goto authfail;
2359 2360

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2361
        VIR_ERROR(_("sasl step reply data too long %d"),
2362
                  (int)serveroutlen);
2363
        goto authfail;
2364 2365 2366 2367
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2368 2369
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2370 2371 2372 2373 2374 2375 2376
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2377
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2378
    if (err == VIR_NET_SASL_CONTINUE) {
2379 2380
        ret->complete = 0;
    } else {
2381
        /* Check username whitelist ACL */
2382
        if ((err = remoteSASLFinish(client)) < 0) {
2383 2384 2385 2386 2387
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2388

2389 2390 2391
        ret->complete = 1;
    }

2392
    virMutexUnlock(&priv->lock);
2393
    return 0;
2394 2395

authfail:
2396 2397 2398
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2399 2400 2401
    goto error;

authdeny:
2402
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2403 2404 2405
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2406 2407
    goto error;

2408
error:
2409 2410 2411 2412 2413 2414 2415 2416
    virNetSASLSessionFree(priv->sasl);
    priv->sasl = NULL;
    virResetLastError();
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2417 2418
    return -1;
}
2419 2420 2421 2422
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2423
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
2436
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
                            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");
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2450
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
                           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");
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
2462 2463 2464



2465 2466
#if HAVE_POLKIT1
static int
2467 2468
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
                         virNetServerClientPtr client,
2469
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2470
                         virNetMessageErrorPtr rerr,
2471
                         remote_auth_polkit_ret *ret)
2472
{
2473
    pid_t callerPid = -1;
2474
    gid_t callerGid = -1;
2475
    uid_t callerUid = -1;
2476 2477
    const char *action;
    int status = -1;
2478
    char *ident = NULL;
2479
    bool authdismissed = 0;
2480
    char *pkout = NULL;
2481 2482
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2483
    virCommandPtr cmd = NULL;
2484

2485 2486
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
2487 2488 2489
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

2490
    cmd = virCommandNewArgList(PKCHECK_PATH, "--action-id", action, NULL);
2491
    virCommandSetOutputBuffer(cmd, &pkout);
2492
    virCommandSetErrorBuffer(cmd, &pkout);
2493

2494 2495
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2496
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2497 2498 2499
        goto authfail;
    }

2500 2501
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
                                          &callerPid) < 0) {
2502 2503 2504
        goto authfail;
    }

2505 2506
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2507

2508
    virCommandAddArg(cmd, "--process");
2509
    virCommandAddArgFormat(cmd, "%lld", (long long) callerPid);
2510
    virCommandAddArg(cmd, "--allow-user-interaction");
2511

2512 2513
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
                    (long long) callerPid, callerUid) < 0) {
2514
        virReportOOMError();
2515 2516 2517
        goto authfail;
    }

2518
    if (virCommandRun(cmd, &status) < 0)
2519
        goto authfail;
2520

2521
    authdismissed = (pkout && strstr(pkout, "dismissed=true"));
2522
    if (status != 0) {
2523
        char *tmp = virCommandTranslateStatus(status);
2524 2525
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d: %s"),
                  action, (long long) callerPid, callerUid, NULLSTR(tmp));
2526
        VIR_FREE(tmp);
2527
        goto authdeny;
2528
    }
2529 2530 2531
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2532 2533
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
2534 2535
    ret->complete = 1;

2536 2537
    virNetServerClientSetIdentity(client, ident);
    virMutexUnlock(&priv->lock);
2538
    virCommandFree(cmd);
E
Eric Blake 已提交
2539
    VIR_FREE(pkout);
2540
    VIR_FREE(ident);
2541

2542 2543
    return 0;

2544
error:
2545 2546
    virCommandFree(cmd);
    VIR_FREE(ident);
2547
    virResetLastError();
2548

2549 2550 2551 2552 2553
    if (authdismissed) {
        virNetError(VIR_ERR_AUTH_CANCELLED, "%s",
                    _("authentication cancelled by user"));
    } else {
        virNetError(VIR_ERR_AUTH_FAILED, "%s",
2554
                    pkout && *pkout ? pkout : _("authentication failed"));
2555
    }
2556 2557

    VIR_FREE(pkout);
2558 2559 2560 2561
    virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return -1;

2562
authfail:
2563 2564 2565
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
2566 2567 2568
    goto error;

authdeny:
2569 2570
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
2571
          client, REMOTE_AUTH_POLKIT, ident);
2572
    goto error;
2573 2574
}
#elif HAVE_POLKIT0
2575
static int
J
Jim Fehlig 已提交
2576
remoteDispatchAuthPolkit(virNetServerPtr server,
2577
                         virNetServerClientPtr client,
2578
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2579
                         virNetMessageErrorPtr rerr,
2580
                         remote_auth_polkit_ret *ret)
2581 2582
{
    pid_t callerPid;
2583
    gid_t callerGid;
2584
    uid_t callerUid;
2585 2586 2587 2588 2589 2590
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
2591
    const char *action;
2592
    char *ident = NULL;
2593 2594
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2595

J
Jim Fehlig 已提交
2596
    virMutexLock(&priv->lock);
2597

J
Jim Fehlig 已提交
2598
    action = virNetServerClientGetReadonly(client) ?
2599 2600
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
2601

2602
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
J
Jim Fehlig 已提交
2603
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2604
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2605
        goto authfail;
2606 2607
    }

2608 2609
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
                                          &callerPid) < 0) {
2610
        VIR_ERROR(_("cannot get peer socket identity"));
2611
        goto authfail;
2612 2613
    }

2614 2615 2616
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
                    (long long) callerPid, callerUid) < 0) {
        virReportOOMError();
2617 2618 2619
        goto authfail;
    }

2620 2621
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2622
    dbus_error_init(&err);
J
Jim Fehlig 已提交
2623
    if (!(pkcaller = polkit_caller_new_from_pid(virNetServerGetDBusConn(server),
2624
                                                callerPid, &err))) {
2625
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
2626
        dbus_error_free(&err);
2627
        goto authfail;
2628
    }
2629

2630
    if (!(pkaction = polkit_action_new())) {
2631
        char ebuf[1024];
2632
        VIR_ERROR(_("Failed to create polkit action %s"),
2633
                  virStrerror(errno, ebuf, sizeof ebuf));
2634
        polkit_caller_unref(pkcaller);
2635
        goto authfail;
2636 2637 2638 2639 2640
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
2641
        char ebuf[1024];
2642
        VIR_ERROR(_("Failed to create polkit context %s"),
2643
                  (pkerr ? polkit_error_get_error_message(pkerr)
2644
                   : virStrerror(errno, ebuf, sizeof ebuf)));
2645 2646 2647 2648 2649
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
2650
        goto authfail;
2651
    }
2652

2653
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
2654 2655 2656 2657 2658 2659
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
2660 2661 2662
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
2663
        goto authfail;
2664
    }
2665
# else
2666 2667 2668
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
2669
# endif
2670 2671 2672 2673
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
2674 2675
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d, result: %s"),
                  action, (long long) callerPid, callerUid,
2676
                  polkit_result_to_string_representation(pkresult));
2677
        goto authdeny;
2678
    }
2679 2680 2681
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2682 2683
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d, result %s",
             action, (long long) callerPid, callerUid,
2684 2685
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
J
Jim Fehlig 已提交
2686
    virNetServerClientSetIdentity(client, ident);
2687

J
Jim Fehlig 已提交
2688
    virMutexUnlock(&priv->lock);
2689
    VIR_FREE(ident);
2690
    return 0;
2691

2692
error:
2693
    VIR_FREE(ident);
2694 2695 2696 2697
    virResetLastError();
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
2698
    virMutexUnlock(&priv->lock);
2699 2700
    return -1;

2701
authfail:
2702 2703 2704
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
2705
    goto error;
2706

2707
authdeny:
2708 2709 2710
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2711
    goto error;
2712 2713
}

2714
#else /* !HAVE_POLKIT0 & !HAVE_POLKIT1*/
2715 2716

static int
2717
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
2718
                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
2719
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2720
                         virNetMessageErrorPtr rerr,
2721
                         remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
2722
{
2723
    VIR_ERROR(_("client tried unsupported PolicyKit init request"));
2724 2725 2726
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
2727 2728 2729
    return -1;
}
#endif /* HAVE_POLKIT1 */
2730 2731


2732 2733 2734
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
2735

2736
static int
2737 2738
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
2739
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
2740
                                  virNetMessageErrorPtr rerr,
2741 2742
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
2743
{
2744 2745
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
2746
    int rv = -1;
2747 2748
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2749

2750
    if (!priv->conn) {
2751 2752
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2753 2754
    }

2755
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
2756 2757
        goto cleanup;

2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776
    parent = virNodeDeviceGetParent(dev);

    if (parent == NULL) {
        ret->parent = NULL;
    } else {
        /* remoteDispatchClientRequest will free this. */
        char **parent_p;
        if (VIR_ALLOC(parent_p) < 0) {
            virReportOOMError();
            goto cleanup;
        }
        if (!(*parent_p = strdup(parent))) {
            VIR_FREE(parent_p);
            virReportOOMError();
            goto cleanup;
        }
        ret->parent = parent_p;
    }

2777 2778 2779 2780
    rv = 0;

cleanup:
    if (rv < 0)
2781
        virNetMessageSaveError(rerr);
2782 2783
    if (dev)
        virNodeDeviceFree(dev);
2784
    return rv;
2785 2786
}

2787 2788 2789 2790 2791

/***************************
 * Register / deregister events
 ***************************/
static int
2792 2793
remoteDispatchDomainEventsRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2794
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2795
                                   virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
2796
                                   remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
2797
{
2798
    int callbackID;
2799
    int rv = -1;
2800 2801
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
2802

2803
    if (!priv->conn) {
2804 2805
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2806 2807
    }

2808 2809 2810
    virMutexLock(&priv->lock);

    if (priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] != -1) {
2811
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
2812
        goto cleanup;
2813
    }
O
Osier Yang 已提交
2814

2815
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
2816 2817 2818 2819
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
                                                       client, NULL)) < 0)
2820
        goto cleanup;
O
Osier Yang 已提交
2821

2822
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;
2823

2824 2825 2826 2827
    rv = 0;

cleanup:
    if (rv < 0)
2828 2829
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2830
    return rv;
O
Osier Yang 已提交
2831 2832
}

2833
static int
2834 2835
remoteDispatchDomainEventsDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
2836
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
2837
                                     virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
2838
                                     remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
2839
{
2840
    int rv = -1;
2841 2842
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2843

2844
    if (!priv->conn) {
2845 2846
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2847 2848
    }

2849 2850 2851
    virMutexLock(&priv->lock);

    if (priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] < 0) {
2852
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
2853
        goto cleanup;
2854
    }
2855

2856 2857
    if (virConnectDomainEventDeregisterAny(priv->conn,
                                           priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0)
2858
        goto cleanup;
2859

2860 2861
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;

2862 2863 2864
    rv = 0;

cleanup:
2865
    if (rv < 0)
2866 2867
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2868 2869 2870 2871
    return rv;
}

static void
2872 2873
remoteDispatchDomainEventSend(virNetServerClientPtr client,
                              virNetServerProgramPtr program,
2874 2875 2876 2877
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
2878
    virNetMessagePtr msg;
2879

2880
    if (!(msg = virNetMessageNew(false)))
2881
        goto cleanup;
2882

2883 2884 2885 2886 2887 2888
    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;
2889

2890
    if (virNetMessageEncodeHeader(msg) < 0)
2891 2892
        goto cleanup;

2893 2894
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
2895

2896 2897
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
2898

2899
    xdr_free(proc, data);
2900 2901 2902
    return;

cleanup:
2903
    virNetMessageFree(msg);
2904
    xdr_free(proc, data);
2905 2906
}

2907
static int
2908 2909
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
2910
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
2911
                             virNetMessageErrorPtr rerr,
2912 2913
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
2914
{
2915 2916 2917
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
2918
    int rv = -1;
2919 2920
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2921

2922
    if (!priv->conn) {
2923 2924
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2925 2926
    }

2927
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
2928
        goto cleanup;
2929

2930
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
2931
        goto cleanup;
2932

2933 2934 2935
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

2936 2937 2938 2939
    rv = 0;

cleanup:
    if (rv < 0)
2940
        virNetMessageSaveError(rerr);
2941 2942
    if (secret)
        virSecretFree(secret);
2943
    return rv;
2944 2945
}

2946
static int
2947 2948
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
2949
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
2950
                             virNetMessageErrorPtr rerr,
2951 2952 2953 2954 2955
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
2956 2957
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2958

2959
    if (!priv->conn) {
2960 2961 2962 2963
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

2964
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
2965 2966 2967 2968 2969 2970 2971 2972 2973
        goto cleanup;

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

    rv = 0;

cleanup:
    if (rv < 0)
2974
        virNetMessageSaveError(rerr);
2975 2976 2977 2978 2979
    if (dom)
        virDomainFree(dom);
    return rv;
}

2980
static int
2981 2982
remoteDispatchDomainEventsRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
2983
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
2984 2985
                                      virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                      remote_domain_events_register_any_args *args)
2986 2987
{
    int callbackID;
2988
    int rv = -1;
2989 2990
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2991

2992
    if (!priv->conn) {
2993 2994
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
2995 2996
    }

2997 2998
    virMutexLock(&priv->lock);

2999 3000
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3001 3002
        virNetError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
        goto cleanup;
3003 3004
    }

3005
    if (priv->domainEventCallbackID[args->eventID] != -1)  {
3006 3007
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), args->eventID);
        goto cleanup;
3008 3009
    }

3010
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3011 3012 3013
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3014
                                                       client, NULL)) < 0)
3015
        goto cleanup;
3016

3017
    priv->domainEventCallbackID[args->eventID] = callbackID;
3018

3019 3020 3021 3022
    rv = 0;

cleanup:
    if (rv < 0)
3023 3024
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3025
    return rv;
3026 3027 3028 3029
}


static int
3030 3031
remoteDispatchDomainEventsDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
3032
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
3033 3034
                                        virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                        remote_domain_events_deregister_any_args *args)
3035 3036
{
    int callbackID = -1;
3037
    int rv = -1;
3038 3039
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3040

3041
    if (!priv->conn) {
3042 3043
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3044 3045
    }

3046 3047
    virMutexLock(&priv->lock);

3048 3049
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3050 3051
        virNetError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
        goto cleanup;
3052 3053
    }

3054 3055
    callbackID = priv->domainEventCallbackID[args->eventID];
    if (callbackID < 0) {
3056 3057
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), args->eventID);
        goto cleanup;
3058 3059
    }

3060
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3061
        goto cleanup;
3062

3063 3064
    priv->domainEventCallbackID[args->eventID] = -1;

3065 3066 3067 3068
    rv = 0;

cleanup:
    if (rv < 0)
3069 3070
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3071
    return rv;
3072 3073
}

C
Chris Lalancette 已提交
3074
static int
3075 3076
qemuDispatchMonitorCommand(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3077
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3078
                           virNetMessageErrorPtr rerr,
3079 3080
                           qemu_monitor_command_args *args,
                           qemu_monitor_command_ret *ret)
C
Chris Lalancette 已提交
3081
{
3082
    virDomainPtr dom = NULL;
3083
    int rv = -1;
3084 3085
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
3086

3087
    if (!priv->conn) {
3088 3089
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
3090 3091
    }

3092
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3093
        goto cleanup;
C
Chris Lalancette 已提交
3094

3095
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
3096
                                    args->flags) < 0)
3097
        goto cleanup;
C
Chris Lalancette 已提交
3098

3099
    rv = 0;
C
Chris Lalancette 已提交
3100

3101 3102
cleanup:
    if (rv < 0)
3103
        virNetMessageSaveError(rerr);
3104 3105
    if (dom)
        virDomainFree(dom);
3106
    return rv;
C
Chris Lalancette 已提交
3107 3108
}

3109

3110
static int
3111 3112
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3113
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3114
                                  virNetMessageErrorPtr rerr,
3115 3116 3117 3118 3119 3120
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
3121
    char *xmlin;
3122 3123 3124
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3125 3126
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3127

3128
    if (!priv->conn) {
3129 3130 3131 3132
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

3136
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3137 3138
    dname = args->dname == NULL ? NULL : *args->dname;

3139
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154
                                       &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)
3155
        virNetMessageSaveError(rerr);
3156 3157 3158 3159 3160 3161 3162
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3163 3164
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3165
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3166
                                    virNetMessageErrorPtr rerr,
3167 3168 3169 3170 3171 3172 3173 3174 3175
                                    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;
3176 3177
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3178

3179
    if (!priv->conn) {
3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

    /* Wacky world of XDR ... */
    if (VIR_ALLOC(uri_out) < 0) {
        virReportOOMError();
        goto cleanup;
    }

3193
    if (virDomainMigratePrepare3(priv->conn,
3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212
                                 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) {
3213
        virNetMessageSaveError(rerr);
3214 3215 3216 3217 3218
        VIR_FREE(uri_out);
    }
    return rv;
}

3219

3220
static int
3221 3222
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3223
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3224
                                    virNetMessageErrorPtr rerr,
3225 3226 3227 3228
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
3229
    char *xmlin;
3230
    char *dname;
3231 3232
    char *uri;
    char *dconnuri;
3233 3234 3235
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3236 3237
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3238

3239
    if (!priv->conn) {
3240 3241 3242 3243
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

3247
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3248
    dname = args->dname == NULL ? NULL : *args->dname;
3249 3250
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
3251

3252
    if (virDomainMigratePerform3(dom, xmlin,
3253 3254 3255
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
3256
                                 dconnuri, uri,
3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268
                                 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)
3269
        virNetMessageSaveError(rerr);
3270 3271 3272 3273 3274 3275 3276
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3277 3278
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
3279
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
3280
                                   virNetMessageErrorPtr rerr,
3281 3282 3283 3284 3285 3286
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
3287 3288
    char *uri;
    char *dconnuri;
3289
    int rv = -1;
3290 3291
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3292

3293
    if (!priv->conn) {
3294 3295 3296 3297
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

3298 3299 3300
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

3301
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
3302 3303 3304 3305 3306 3307
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
3308 3309
        goto cleanup;

3310
    make_nonnull_domain(&ret->dom, dom);
3311 3312 3313 3314 3315 3316 3317 3318 3319 3320

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

    rv = 0;

cleanup:
    if (rv < 0) {
3321
        virNetMessageSaveError(rerr);
3322 3323 3324 3325 3326 3327 3328 3329 3330
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3331 3332
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3333
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3334 3335
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
3336 3337 3338
{
    virDomainPtr dom = NULL;
    int rv = -1;
3339 3340
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3341

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

3347
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359
        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)
3360
        virNetMessageSaveError(rerr);
3361 3362 3363 3364 3365 3366
    if (dom)
        virDomainFree(dom);
    return rv;
}


3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379
static int remoteDispatchSupportsFeature(
    virNetServerPtr server ATTRIBUTE_UNUSED,
    virNetServerClientPtr client,
    virNetMessagePtr msg ATTRIBUTE_UNUSED,
    virNetMessageErrorPtr rerr,
    remote_supports_feature_args *args,
    remote_supports_feature_ret *ret)
{
    int rv = -1;
    int supported;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
    /* 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;
    }

3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405
    if (!priv->conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

    default:
        if ((supported = virDrvSupportsFeature(priv->conn, args->feature)) < 0)
            goto cleanup;
        break;
    }

3406
done:
3407 3408 3409 3410 3411 3412 3413 3414 3415 3416
    ret->supported = supported;
    rv = 0;

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


3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457
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) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        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;
}

3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516
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;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    flags = args->flags;

    if (nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    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);
3517 3518
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
3519 3520 3521 3522
    if (dom)
        virDomainFree(dom);
    return rv;
}
3523

3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592
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) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
        goto cleanup;
    }

    if (args->nparams > 0 &&
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0) {
        virReportOOMError();
        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;

    percpu_len = ret->params.params_len / args->ncpus;

success:
    rv = 0;
    ret->nparams = percpu_len;

cleanup:
    if (rv < 0)
         virNetMessageSaveError(rerr);
    virTypedParameterArrayClear(params, args->ncpus * args->nparams);
    VIR_FREE(params);
    if (dom)
        virDomainFree(dom);
    return rv;
}

3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603
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)
{
    int rv = -1;
    virDomainPtr dom = NULL;
    virDomainDiskErrorPtr errors = NULL;
3604
    int len = 0;
3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("maxerrors too large"));
        goto cleanup;
    }

    if (args->maxerrors &&
        VIR_ALLOC_N(errors, args->maxerrors) < 0) {
        virReportOOMError();
        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) {
        int i;
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

3656 3657 3658 3659 3660 3661 3662 3663 3664
/*----- 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
3665
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
3666 3667
{
    virDomainPtr dom;
3668
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
3669 3670 3671 3672 3673 3674 3675 3676
    /* 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
3677
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
3678
{
3679
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
3680 3681
}

D
Daniel Veillard 已提交
3682
static virInterfacePtr
3683
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
3684
{
3685
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
3686 3687
}

3688
static virStoragePoolPtr
3689
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
3690
{
3691
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid);
3692 3693 3694
}

static virStorageVolPtr
3695
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
3696 3697
{
    virStorageVolPtr ret;
3698
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key);
3699 3700 3701
    return ret;
}

3702
static virSecretPtr
3703
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
3704
{
3705
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
3706 3707
}

3708
static virNWFilterPtr
3709
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
3710
{
3711
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
3712 3713
}

C
Chris Lalancette 已提交
3714
static virDomainSnapshotPtr
3715
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
3716
{
3717
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
3718 3719
}

3720 3721
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
3722
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
3723 3724
{
    dom_dst->id = dom_src->id;
3725 3726
    dom_dst->name = strdup(dom_src->name);
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
3727 3728 3729
}

static void
3730
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
3731
{
3732 3733
    net_dst->name = strdup(net_src->name);
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
3734 3735
}

D
Daniel Veillard 已提交
3736
static void
3737 3738
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
3739
{
3740 3741
    interface_dst->name = strdup(interface_src->name);
    interface_dst->mac = strdup(interface_src->mac);
D
Daniel Veillard 已提交
3742 3743
}

3744
static void
3745
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
3746
{
3747 3748
    pool_dst->name = strdup(pool_src->name);
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
3749 3750 3751
}

static void
3752
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
3753
{
3754 3755 3756
    vol_dst->pool = strdup(vol_src->pool);
    vol_dst->name = strdup(vol_src->name);
    vol_dst->key = strdup(vol_src->key);
3757
}
3758 3759

static void
3760
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
3761 3762 3763
{
    dev_dst->name = strdup(dev_src->name);
}
3764 3765

static void
3766
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
3767
{
3768
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
3769
    secret_dst->usageType = secret_src->usageType;
3770
    secret_dst->usageID = strdup(secret_src->usageID);
3771
}
3772 3773

static void
3774
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
3775
{
3776 3777
    nwfilter_dst->name = strdup(nwfilter_src->name);
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
3778
}
C
Chris Lalancette 已提交
3779 3780

static void
3781
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
3782 3783
{
    snapshot_dst->name = strdup(snapshot_src->name);
3784
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
3785
}
3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819

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;
    int i = 0;

    if (VIR_ALLOC_N(val, nerrors) < 0)
        goto no_memory;

    for (i = 0; i < nerrors; i++) {
        if (!(val[i].disk = strdup(errors[i].disk)))
            goto no_memory;
        val[i].error = errors[i].error;
    }

    *ret_errors_len = nerrors;
    *ret_errors_val = val;

    return 0;

no_memory:
    if (val) {
        int j;
        for (j = 0; j < i; j++)
            VIR_FREE(val[j].disk);
        VIR_FREE(val);
    }
    virReportOOMError();
    return -1;
}