remote.c 150.5 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
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23 24
 *
 * Author: Richard W.M. Jones <rjones@redhat.com>
 */

#include <config.h>

25
#include "virerror.h"
26

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

32
#include "remote.h"
33
#include "libvirtd.h"
34 35
#include "libvirt_internal.h"
#include "datatypes.h"
36
#include "viralloc.h"
37
#include "virlog.h"
38
#include "virutil.h"
C
Chris Lalancette 已提交
39
#include "stream.h"
40
#include "viruuid.h"
C
Chris Lalancette 已提交
41
#include "libvirt/libvirt-qemu.h"
42
#include "libvirt/libvirt-lxc.h"
43
#include "vircommand.h"
44
#include "intprops.h"
45
#include "virnetserverservice.h"
J
Jim Fehlig 已提交
46
#include "virnetserver.h"
47
#include "virfile.h"
48
#include "virtypedparam.h"
49
#include "virdbus.h"
50
#include "virprocess.h"
51 52
#include "remote_protocol.h"
#include "qemu_protocol.h"
53
#include "lxc_protocol.h"
54

55 56

#define VIR_FROM_THIS VIR_FROM_RPC
57

58
#if SIZEOF_LONG < 8
E
Eric Blake 已提交
59 60 61
# define HYPER_TO_TYPE(_type, _to, _from)                               \
    do {                                                                \
        if ((_from) != (_type)(_from)) {                                \
62 63 64
            virReportError(VIR_ERR_OVERFLOW,                            \
                           _("conversion from hyper to %s overflowed"), \
                           #_type);                                     \
E
Eric Blake 已提交
65 66 67
            goto cleanup;                                               \
        }                                                               \
        (_to) = (_from);                                                \
68 69 70 71 72 73 74 75 76
    } 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"
109
#include "lxc_dispatch.h"
C
Chris Lalancette 已提交
110 111


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

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

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

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

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

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

144 145
    return 0;
}
146

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

    if (!client)
        return -1;

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

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

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

    return 0;
}

170

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

    if (!client)
        return -1;

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

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

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

    return 0;
}


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

    if (!client)
        return -1;

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

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

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

    return 0;
}


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

    if (!client)
        return -1;

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

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

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

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


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

    if (!client)
        return -1;

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

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

    make_nonnull_domain(&data.dom, dom);
293

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

    return 0;
299 300 301

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


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

    if (!client)
        return -1;

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

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

    /* build return data */
336
    memset(&data, 0, sizeof(data));
337 338 339
    data.phase = phase;
    data.local.family = local->family;
    data.remote.family = remote->family;
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    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;
357 358

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

362
    for (i = 0 ; i < data.subject.subject_len ; i++) {
363 364 365 366 367 368
        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;
369
    }
370
    make_nonnull_domain(&data.dom, dom);
371

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

    return 0;
377 378 379

mem_error:
    virReportOOMError();
E
Eric Blake 已提交
380 381 382 383 384
    VIR_FREE(data.authScheme);
    VIR_FREE(data.local.node);
    VIR_FREE(data.local.service);
    VIR_FREE(data.remote.node);
    VIR_FREE(data.remote.service);
385 386
    if (data.subject.subject_val != NULL) {
        for (i = 0 ; i < data.subject.subject_len ; i++) {
E
Eric Blake 已提交
387 388
            VIR_FREE(data.subject.subject_val[i].type);
            VIR_FREE(data.subject.subject_val[i].name);
389
        }
E
Eric Blake 已提交
390
        VIR_FREE(data.subject.subject_val);
391 392
    }
    return -1;
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 */
412
    memset(&data, 0, sizeof(data));
413 414 415
    data.path = strdup(path);
    if (data.path == NULL)
        goto mem_error;
416 417
    data.type = type;
    data.status = status;
418
    make_nonnull_domain(&data.dom, dom);
419 420 421 422 423 424

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

    return 0;
425 426 427

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

432

433 434 435 436
static int remoteRelayDomainEventControlError(virConnectPtr conn ATTRIBUTE_UNUSED,
                                              virDomainPtr dom,
                                              void *opaque)
{
437
    virNetServerClientPtr client = opaque;
438 439 440 441 442 443 444 445
    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 */
446
    memset(&data, 0, sizeof(data));
447 448
    make_nonnull_domain(&data.dom, dom);

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

    return 0;
}


457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
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 */
476
    memset(&data, 0, sizeof(data));
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    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 已提交
502 503
    VIR_FREE(oldSrcPath_p);
    VIR_FREE(newSrcPath_p);
504 505 506 507 508
    virReportOOMError();
    return -1;
}


509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
static int remoteRelayDomainEventTrayChange(virConnectPtr conn ATTRIBUTE_UNUSED,
                                            virDomainPtr dom,
                                            const char *devAlias,
                                            int reason,
                                            void *opaque) {
    virNetServerClientPtr client = opaque;
    remote_domain_event_tray_change_msg data;

    if (!client)
        return -1;

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

    /* build return data */
524
    memset(&data, 0, sizeof(data));
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

    if (!(data.devAlias = strdup(devAlias))) {
        virReportOOMError();
        return -1;
    }
    data.reason = reason;

    make_nonnull_domain(&data.dom, dom);

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_TRAY_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_tray_change_msg, &data);

    return 0;
}

O
Osier Yang 已提交
541 542
static int remoteRelayDomainEventPMWakeup(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
543
                                          int reason ATTRIBUTE_UNUSED,
O
Osier Yang 已提交
544 545 546 547 548 549 550 551 552 553
                                          void *opaque) {
    virNetServerClientPtr client = opaque;
    remote_domain_event_pmwakeup_msg data;

    if (!client)
        return -1;

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

    /* build return data */
554
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
555 556 557 558 559 560 561 562 563
    make_nonnull_domain(&data.dom, dom);

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_PMWAKEUP,
                                  (xdrproc_t)xdr_remote_domain_event_pmwakeup_msg, &data);

    return 0;
}

O
Osier Yang 已提交
564 565
static int remoteRelayDomainEventPMSuspend(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
566
                                           int reason ATTRIBUTE_UNUSED,
O
Osier Yang 已提交
567 568 569 570 571 572 573 574 575 576
                                           void *opaque) {
    virNetServerClientPtr client = opaque;
    remote_domain_event_pmsuspend_msg data;

    if (!client)
        return -1;

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

    /* build return data */
577
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
578 579 580 581 582 583 584 585 586
    make_nonnull_domain(&data.dom, dom);

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND,
                                  (xdrproc_t)xdr_remote_domain_event_pmsuspend_msg, &data);

    return 0;
}

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
static int
remoteRelayDomainEventBalloonChange(virConnectPtr conn ATTRIBUTE_UNUSED,
                                    virDomainPtr dom,
                                    unsigned long long actual,
                                    void *opaque)
{
    virNetServerClientPtr client = opaque;
    remote_domain_event_balloon_change_msg data;

    if (!client)
        return -1;

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

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

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE,
                                  (xdrproc_t)xdr_remote_domain_event_balloon_change_msg, &data);

    return 0;
}


614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
static int remoteRelayDomainEventPMSuspendDisk(virConnectPtr conn ATTRIBUTE_UNUSED,
                                               virDomainPtr dom,
                                               int reason ATTRIBUTE_UNUSED,
                                               void *opaque) {
    virNetServerClientPtr client = opaque;
    remote_domain_event_pmsuspend_disk_msg data;

    if (!client)
        return -1;

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

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

    remoteDispatchDomainEventSend(client, remoteProgram,
                                  REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND_DISK,
                                  (xdrproc_t)xdr_remote_domain_event_pmsuspend_disk_msg, &data);

    return 0;
}


638
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
639
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
640
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
641
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
642
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
643
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
644
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
645
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
646
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
647
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
648
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
649
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
650
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
651
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
652
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
653
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspendDisk),
654 655 656 657
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

658 659 660 661 662 663 664
/*
 * 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
 */
665
void remoteClientFreeFunc(void *data)
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
{
    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);
}


689 690 691 692 693 694 695
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

696

697 698
void *remoteClientInitHook(virNetServerClientPtr client,
                           void *opaque ATTRIBUTE_UNUSED)
699 700 701 702 703 704
{
    struct daemonClientPrivate *priv;
    int i;

    if (VIR_ALLOC(priv) < 0) {
        virReportOOMError();
705
        return NULL;
706 707 708 709 710
    }

    if (virMutexInit(&priv->lock) < 0) {
        VIR_FREE(priv);
        virReportOOMError();
711
        return NULL;
712 713 714 715 716
    }

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

717
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
718
    return priv;
719 720
}

721 722 723
/*----- Functions. -----*/

static int
724
remoteDispatchOpen(virNetServerPtr server,
725
                   virNetServerClientPtr client,
726
                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
727 728
                   virNetMessageErrorPtr rerr,
                   struct remote_open_args *args)
729 730
{
    const char *name;
731
    unsigned int flags;
732
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
733
    int rv = -1;
734

735 736 737 738
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
739
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
740 741 742
        goto cleanup;
    }

743
    if (virNetServerKeepAliveRequired(server) && !priv->keepalive_supported) {
744 745
        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                       _("keepalive support is required to connect"));
746 747 748
        goto cleanup;
    }

749 750 751 752 753 754
    name = args->name ? *args->name : NULL;

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

758
    priv->conn =
759
        flags & VIR_CONNECT_RO
760 761
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
762

763
    if (priv->conn == NULL)
764 765 766
        goto cleanup;

    rv = 0;
767

768 769
cleanup:
    if (rv < 0)
770 771
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
772
    return rv;
773 774 775 776
}


static int
777
remoteDispatchClose(virNetServerPtr server ATTRIBUTE_UNUSED,
778
                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
779
                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
780
                    virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
781
{
782
    virNetServerClientDelayedClose(client);
783
    return 0;
784 785
}

786

787
static int
788 789
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
790
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
791
                                     virNetMessageErrorPtr rerr,
792 793
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
794
{
795
    virDomainPtr dom = NULL;
796 797
    char *type;
    int nparams;
798
    int rv = -1;
799 800
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
801

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

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

810
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
811
        goto cleanup;
812 813 814

    ret->type = type;
    ret->nparams = nparams;
815 816 817 818
    rv = 0;

cleanup:
    if (rv < 0)
819
        virNetMessageSaveError(rerr);
820 821 822
    if (dom)
        virDomainFree(dom);
    return rv;
823 824
}

825 826
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
827 828 829
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
830
                               remote_typed_param **ret_params_val,
831 832
                               u_int *ret_params_len,
                               unsigned int flags)
833 834
{
    int i;
835
    int j;
836 837 838 839 840 841 842 843 844
    int rv = -1;
    remote_typed_param *val;

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

845
    for (i = 0, j = 0; i < nparams; ++i) {
846 847 848 849 850
        /* 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)) {
851 852 853 854
            --*ret_params_len;
            continue;
        }

855
        /* remoteDispatchClientRequest will free this: */
856 857
        val[j].field = strdup(params[i].field);
        if (val[j].field == NULL) {
858 859 860
            virReportOOMError();
            goto cleanup;
        }
861
        val[j].value.type = params[i].type;
862
        switch (params[i].type) {
863
        case VIR_TYPED_PARAM_INT:
864
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
865 866
            break;
        case VIR_TYPED_PARAM_UINT:
867
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
868 869
            break;
        case VIR_TYPED_PARAM_LLONG:
870
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
871 872
            break;
        case VIR_TYPED_PARAM_ULLONG:
873
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
874 875
            break;
        case VIR_TYPED_PARAM_DOUBLE:
876
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
877 878
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
879 880 881 882 883 884 885 886 887
            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;
            }
888 889
            break;
        default:
890 891
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
892 893
            goto cleanup;
        }
894
        j++;
895 896 897 898 899 900 901 902
    }

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

cleanup:
    if (val) {
903
        for (i = 0; i < nparams; i++) {
904
            VIR_FREE(val[i].field);
905
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
906 907
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
908 909 910 911 912 913 914
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
915 916
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
917 918 919
                                 int limit,
                                 int *nparams)
{
920
    int i = 0;
921 922 923 924 925
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
    if (args_params_len > limit) {
926
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
927 928 929 930 931 932 933 934 935 936 937 938 939
        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) {
940 941 942
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Parameter %s too big for destination"),
                           args_params_val[i].field);
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
            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;
971 972 973 974 975 976 977 978
        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;
979
        default:
980 981
            virReportError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                           params[i].type);
982 983 984 985 986 987 988
            goto cleanup;
        }
    }

    rv = 0;

cleanup:
989
    if (rv < 0) {
990 991
        virTypedParamsFree(params, i);
        params = NULL;
992
    }
993 994 995
    return params;
}

996
static int
997 998
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
999
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
1000
                                           virNetMessageErrorPtr rerr,
1001 1002
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
1003
{
1004
    virDomainPtr dom = NULL;
1005
    virTypedParameterPtr params = NULL;
1006
    int nparams = args->nparams;
1007
    int rv = -1;
1008 1009
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1010

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

1016
    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1017
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1018
        goto cleanup;
1019
    }
1020
    if (nparams && VIR_ALLOC_N(params, nparams) < 0)
1021
        goto no_memory;
1022

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

1026
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1027
        goto cleanup;
1028

1029
    if (remoteSerializeTypedParameters(params, nparams,
1030
                                       &ret->params.params_val,
1031 1032
                                       &ret->params.params_len,
                                       0) < 0)
1033 1034 1035 1036 1037 1038
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
1039
        virNetMessageSaveError(rerr);
1040
    virTypedParamsFree(params, nparams);
1041 1042 1043 1044 1045 1046 1047 1048 1049
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
static int
remoteDispatchConnectListAllDomains(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_connect_list_all_domains_args *args,
                                    remote_connect_list_all_domains_ret *ret)
{
    virDomainPtr *doms = NULL;
    int ndomains = 0;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
1065
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
        goto cleanup;
    }

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

    if (doms && ndomains) {
        if (VIR_ALLOC_N(ret->domains.domains_val, ndomains) < 0) {
            virReportOOMError();
            goto cleanup;
        }

        ret->domains.domains_len = ndomains;

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

    ret->ret = ndomains;

    rv = 0;

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

1104
static int
1105 1106
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1107
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1108
                                                virNetMessageErrorPtr rerr,
1109 1110 1111 1112 1113 1114 1115
                                                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;
1116 1117
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1118

1119
    if (!priv->conn) {
1120
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1121 1122 1123 1124
        goto cleanup;
    }

    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
1125
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1126 1127
        goto cleanup;
    }
1128
    if (nparams && VIR_ALLOC_N(params, nparams) < 0)
1129 1130
        goto no_memory;

1131
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1132 1133 1134 1135 1136 1137 1138
        goto cleanup;

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

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

1144
    rv = 0;
1145 1146

cleanup:
1147
    if (rv < 0)
1148
        virNetMessageSaveError(rerr);
1149
    virTypedParamsFree(params, nparams);
1150 1151 1152 1153 1154 1155 1156
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
1157 1158
}

1159
static int
1160 1161
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1162
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1163
                                virNetMessageErrorPtr rerr,
1164 1165
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1166
{
1167
    virDomainPtr dom = NULL;
1168
    struct _virDomainMemoryStat *stats;
1169
    int nr_stats, i;
1170
    int rv = -1;
1171 1172
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1173

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

1179
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1180 1181
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
1182
        goto cleanup;
1183 1184
    }

1185
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1186
        goto cleanup;
1187 1188 1189

    /* Allocate stats array for making dispatch call */
    if (VIR_ALLOC_N(stats, args->maxStats) < 0) {
1190 1191
        virReportOOMError();
        goto cleanup;
1192
    }
1193

1194
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1195
    if (nr_stats < 0)
1196
        goto cleanup;
1197 1198 1199

    /* Allocate return buffer */
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0) {
1200 1201
        virReportOOMError();
        goto cleanup;
1202 1203 1204 1205 1206 1207 1208 1209
    }

    /* 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;
1210 1211 1212 1213
    rv = 0;

cleanup:
    if (rv < 0)
1214
        virNetMessageSaveError(rerr);
1215 1216
    if (dom)
        virDomainFree(dom);
1217
    VIR_FREE(stats);
1218
    return rv;
1219 1220
}

1221
static int
1222 1223
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1224
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1225
                              virNetMessageErrorPtr rerr,
1226 1227
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1228
{
1229
    virDomainPtr dom = NULL;
1230 1231 1232 1233
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1234
    int rv = -1;
1235 1236
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1237

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

1243
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1244
        goto cleanup;
1245 1246 1247 1248 1249 1250
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1251 1252
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1253
        goto cleanup;
1254 1255 1256
    }

    ret->buffer.buffer_len = size;
1257
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
1258 1259
        virReportOOMError();
        goto cleanup;
1260 1261
    }

1262
    if (virDomainBlockPeek(dom, path, offset, size,
1263
                           ret->buffer.buffer_val, flags) < 0)
1264
        goto cleanup;
1265

1266 1267 1268 1269
    rv = 0;

cleanup:
    if (rv < 0) {
1270
        virNetMessageSaveError(rerr);
1271 1272 1273 1274 1275
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
1276 1277
}

1278 1279 1280
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1281
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
                                    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) {
1296
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1297 1298 1299 1300 1301 1302 1303 1304
        goto cleanup;
    }

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

    if (nparams > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX) {
1305
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1306 1307
        goto cleanup;
    }
1308
    if (nparams && VIR_ALLOC_N(params, nparams) < 0) {
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
        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,
1327 1328
                                       &ret->params.params_len,
                                       args->flags) < 0)
1329 1330 1331 1332 1333 1334
        goto cleanup;

success:
    rv = 0;

cleanup:
1335
    if (rv < 0)
1336
        virNetMessageSaveError(rerr);
1337
    virTypedParamsFree(params, nparams);
A
ajia@redhat.com 已提交
1338 1339
    if (dom)
        virDomainFree(dom);
1340 1341 1342
    return rv;
}

R
Richard W.M. Jones 已提交
1343
static int
1344 1345
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1346
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1347
                               virNetMessageErrorPtr rerr,
1348 1349
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1350
{
1351
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1352 1353 1354
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1355
    int rv = -1;
1356 1357
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1358

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

1364
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1365
        goto cleanup;
R
Richard W.M. Jones 已提交
1366 1367 1368 1369 1370
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1371 1372
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("size > maximum buffer size"));
1373
        goto cleanup;
R
Richard W.M. Jones 已提交
1374 1375 1376
    }

    ret->buffer.buffer_len = size;
1377
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
1378 1379
        virReportOOMError();
        goto cleanup;
R
Richard W.M. Jones 已提交
1380 1381
    }

1382
    if (virDomainMemoryPeek(dom, offset, size,
1383
                            ret->buffer.buffer_val, flags) < 0)
1384
        goto cleanup;
R
Richard W.M. Jones 已提交
1385

1386 1387 1388 1389
    rv = 0;

cleanup:
    if (rv < 0) {
1390
        virNetMessageSaveError(rerr);
1391 1392 1393 1394 1395
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
1396 1397
}

1398
static int
1399 1400
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1401
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1402
                                     virNetMessageErrorPtr rerr,
1403 1404
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1405
{
1406 1407
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1408
    int rv = -1;
1409 1410
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1411

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

1417
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
        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();
1431
        goto cleanup;
1432 1433 1434
    }
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1435

1436 1437 1438 1439
    rv = 0;

cleanup:
    if (rv < 0)
1440
        virNetMessageSaveError(rerr);
1441 1442 1443
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
1444
    return rv;
1445 1446
}

M
Marcelo Cerri 已提交
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
static int
remoteDispatchDomainGetSecurityLabelList(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_domain_get_security_label_list_args *args,
                                         remote_domain_get_security_label_list_ret *ret)
{
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabels = NULL;
    int i, len, rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if ((len = virDomainGetSecurityLabelList(dom, &seclabels)) < 0) {
        ret->ret = len;
        ret->labels.labels_len = 0;
        ret->labels.labels_val = NULL;
        goto done;
    }

    if (VIR_ALLOC_N(ret->labels.labels_val, len) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    for (i = 0; i < len; i++) {
        size_t label_len = strlen(seclabels[i].label) + 1;
        remote_domain_get_security_label_ret *cur = &ret->labels.labels_val[i];
        if (VIR_ALLOC_N(cur->label.label_val, label_len) < 0) {
            virReportOOMError();
            goto cleanup;
        }
        if (virStrcpy(cur->label.label_val, seclabels[i].label, label_len) == NULL) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("failed to copy security label"));
            goto cleanup;
        }
        cur->label.label_len = label_len;
        cur->enforcing = seclabels[i].enforcing;
    }
    ret->labels.labels_len = ret->ret = len;

done:
    rv = 0;

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

1510
static int
1511 1512
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1513
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1514
                                   virNetMessageErrorPtr rerr,
1515
                                   remote_node_get_security_model_ret *ret)
1516
{
1517
    virSecurityModel secmodel;
1518
    int rv = -1;
1519 1520
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1521

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

1527
    memset(&secmodel, 0, sizeof(secmodel));
1528
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
        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();
1541
        goto cleanup;
1542 1543
    }
    strcpy(ret->doi.doi_val, secmodel.doi);
1544

1545 1546 1547 1548
    rv = 0;

cleanup:
    if (rv < 0)
1549
        virNetMessageSaveError(rerr);
1550
    return rv;
1551 1552
}

1553
static int
1554 1555
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1556
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1557
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
1558 1559
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
1560 1561 1562 1563 1564
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
1565 1566
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1567

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

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

    if (args->ncpumaps > REMOTE_VCPUINFO_MAX) {
1577
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpumaps > REMOTE_VCPUINFO_MAX"));
1578 1579 1580 1581 1582
        goto cleanup;
    }

    if (INT_MULTIPLY_OVERFLOW(args->ncpumaps, args->maplen) ||
        args->ncpumaps * args->maplen > REMOTE_CPUMAPS_MAX) {
1583
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1584 1585 1586 1587 1588 1589 1590 1591
        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 已提交
1592
    if ((num = virDomainGetVcpuPinInfo(dom,
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
                                       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;
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 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
    cpumaps = NULL;

    rv = 0;

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

no_memory:
    virReportOOMError();
    goto cleanup;
}

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

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

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

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

    rv = 0;

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


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

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

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

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

    if ((r = virDomainGetEmulatorPinInfo(dom,
                                         cpumaps,
                                         args->maplen,
                                         args->flags)) < 0)
        goto cleanup;

    ret->ret = r;
    ret->cpumaps.cpumaps_len = args->maplen;
    ret->cpumaps.cpumaps_val = (char *) cpumaps;
1697 1698 1699 1700 1701 1702
    cpumaps = NULL;

    rv = 0;

cleanup:
    if (rv < 0)
1703
        virNetMessageSaveError(rerr);
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

1714
static int
1715 1716
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
1717
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
1718
                             virNetMessageErrorPtr rerr,
1719 1720
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
1721
{
1722
    virDomainPtr dom = NULL;
1723 1724 1725
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
    int info_len, i;
1726
    int rv = -1;
1727 1728
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1729

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

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

1738
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
1739
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
1740
        goto cleanup;
1741
    }
1742

1743 1744
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1745
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
        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;
1782 1783

cleanup:
1784
    if (rv < 0) {
1785
        virNetMessageSaveError(rerr);
1786 1787 1788 1789
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
1790 1791 1792
    if (dom)
        virDomainFree(dom);
    return rv;
1793 1794 1795 1796

no_memory:
    virReportOOMError();
    goto cleanup;
1797 1798 1799
}

static int
1800 1801
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1802
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1803
                                   virNetMessageErrorPtr rerr,
1804 1805
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
1806
{
1807 1808 1809 1810 1811
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1812
    int rv = -1;
1813 1814
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1815

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

1821 1822 1823 1824 1825 1826
    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();
1827
        goto cleanup;
1828
    }
1829

1830
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
1831 1832
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
1833
        goto cleanup;
1834

1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
    /* 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;
    }
1846

1847
    rv = 0;
1848

1849 1850
cleanup:
    if (rv < 0)
1851
        virNetMessageSaveError(rerr);
1852
    VIR_FREE(uri_out);
1853
    return rv;
1854 1855
}

1856
static int
1857 1858
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1859
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1860
                                    virNetMessageErrorPtr rerr,
1861 1862
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
1863
{
1864 1865 1866 1867 1868
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1869
    int rv = -1;
1870 1871
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1872

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

1878 1879
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
1880

1881 1882
    /* Wacky world of XDR ... */
    if (VIR_ALLOC(uri_out) < 0) {
1883 1884
        virReportOOMError();
        goto cleanup;
1885 1886
    }

1887
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
1888 1889 1890
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
1891
        goto cleanup;
1892

1893 1894 1895 1896 1897 1898
    /* 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;
1899

1900 1901 1902 1903
    rv = 0;

cleanup:
    if (rv < 0)
1904
        virNetMessageSaveError(rerr);
1905
    return rv;
1906 1907
}

C
Chris Lalancette 已提交
1908
static int
1909 1910
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
1911
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
1912 1913 1914
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
1915
{
1916
    virDomainPtr dom = NULL;
1917
    virTypedParameterPtr params = NULL;
1918 1919
    int nparams = args->nparams;
    unsigned int flags;
1920
    int rv = -1;
1921 1922
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1923

1924
    if (!priv->conn) {
1925
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1926
        goto cleanup;
1927
    }
C
Chris Lalancette 已提交
1928

1929
    flags = args->flags;
C
Chris Lalancette 已提交
1930

1931
    if (nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
1932
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1933 1934
        goto cleanup;
    }
1935
    if (nparams && VIR_ALLOC_N(params, nparams) < 0) {
1936 1937
        virReportOOMError();
        goto cleanup;
C
Chris Lalancette 已提交
1938 1939
    }

1940
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1941
        goto cleanup;
C
Chris Lalancette 已提交
1942

1943
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
1944
        goto cleanup;
C
Chris Lalancette 已提交
1945

1946 1947 1948 1949 1950 1951
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
1952 1953
    }

1954
    if (remoteSerializeTypedParameters(params, nparams,
1955
                                       &ret->params.params_val,
1956 1957
                                       &ret->params.params_len,
                                       args->flags) < 0)
1958
        goto cleanup;
1959

1960
success:
1961 1962 1963
    rv = 0;

cleanup:
1964
    if (rv < 0)
1965
        virNetMessageSaveError(rerr);
1966
    virTypedParamsFree(params, nparams);
1967 1968 1969
    if (dom)
        virDomainFree(dom);
    return rv;
1970 1971
}

1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
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) {
1989
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
1990 1991 1992 1993 1994 1995
        goto cleanup;
    }

    flags = args->flags;

    if (nparams > REMOTE_DOMAIN_NUMA_PARAMETERS_MAX) {
1996
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
1997 1998
        goto cleanup;
    }
1999
    if (nparams && VIR_ALLOC_N(params, nparams) < 0) {
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
        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);
2030
    virTypedParamsFree(params, nparams);
2031 2032 2033 2034 2035
    if (dom)
        virDomainFree(dom);
    return rv;
}

2036
static int
2037 2038
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
2039
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2040 2041 2042
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
2043
{
2044
    virDomainPtr dom = NULL;
2045
    virTypedParameterPtr params = NULL;
2046
    int nparams = args->nparams;
2047
    unsigned int flags;
2048
    int rv = -1;
2049 2050
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2051

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

2057 2058
    flags = args->flags;

2059
    if (nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
2060
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2061
        goto cleanup;
2062
    }
2063
    if (nparams && VIR_ALLOC_N(params, nparams) < 0) {
2064 2065
        virReportOOMError();
        goto cleanup;
2066 2067
    }

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

2071
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
2072
        goto cleanup;
2073

2074 2075 2076 2077 2078 2079 2080 2081
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

2082
    if (remoteSerializeTypedParameters(params, nparams,
2083
                                       &ret->params.params_val,
2084 2085
                                       &ret->params.params_len,
                                       args->flags) < 0)
2086
        goto cleanup;
2087

2088 2089
success:
    rv = 0;
2090

2091
cleanup:
2092
    if (rv < 0)
2093
        virNetMessageSaveError(rerr);
2094
    virTypedParamsFree(params, nparams);
2095 2096 2097
    if (dom)
        virDomainFree(dom);
    return rv;
2098 2099
}

2100
static int
2101 2102
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
2103
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
2104 2105 2106
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
2107
{
2108
    virNodeCPUStatsPtr params = NULL;
2109 2110 2111 2112 2113
    int i;
    int cpuNum = args->cpuNum;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
2114 2115
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2116

2117
    if (!priv->conn) {
2118
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2119 2120 2121 2122 2123 2124
        goto cleanup;
    }

    flags = args->flags;

    if (nparams > REMOTE_NODE_CPU_STATS_MAX) {
2125
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2126 2127 2128 2129 2130 2131 2132
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

2133
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
        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;

2161 2162
cleanup:
    if (rv < 0) {
2163
        virNetMessageSaveError(rerr);
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
        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
2179 2180
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2181
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2182 2183 2184
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2185
{
2186
    virNodeMemoryStatsPtr params = NULL;
2187 2188 2189 2190 2191
    int i;
    int cellNum = args->cellNum;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
2192 2193
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2194

2195
    if (!priv->conn) {
2196
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2197 2198 2199 2200 2201 2202
        goto cleanup;
    }

    flags = args->flags;

    if (nparams > REMOTE_NODE_MEMORY_STATS_MAX) {
2203
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2204 2205 2206 2207 2208 2209 2210
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

2211
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
        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;

2239 2240
cleanup:
    if (rv < 0) {
2241
        virNetMessageSaveError(rerr);
2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
        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;
}

2256 2257 2258
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2259
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
                                    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) {
2271
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
        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;
}

2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
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) {
2313
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
2314 2315 2316 2317
        goto cleanup;
    }

    if (nparams > REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX) {
2318
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
2319 2320 2321
        goto cleanup;
    }

2322
    if (nparams && VIR_ALLOC_N(params, nparams) < 0) {
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
        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);
2355
    virTypedParamsFree(params, nparams);
2356 2357 2358 2359
    if (dom)
        virDomainFree(dom);
    return rv;
}
2360

D
Daniel Veillard 已提交
2361 2362
/*-------------------------------------------------------------*/

2363
static int
2364 2365
remoteDispatchAuthList(virNetServerPtr server ATTRIBUTE_UNUSED,
                       virNetServerClientPtr client,
2366
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2367
                       virNetMessageErrorPtr rerr,
2368
                       remote_auth_list_ret *ret)
2369
{
2370
    int rv = -1;
2371 2372
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2373
    gid_t callerGid;
2374 2375 2376 2377 2378 2379 2380
    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) {
2381 2382
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
                                              &callerPid) < 0) {
2383 2384 2385 2386
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2387 2388
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
J
Jim Fehlig 已提交
2389 2390 2391
                            (long long) callerPid, callerUid) < 0) {
                virReportOOMError();
                goto cleanup;
2392
            }
J
Jim Fehlig 已提交
2393
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
2394 2395
            virNetServerClientSetAuth(client, 0);
            auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
J
Jim Fehlig 已提交
2396
            VIR_FREE(ident);
2397 2398
        }
    }
2399

2400
    ret->types.types_len = 1;
2401
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
2402 2403
        virReportOOMError();
        goto cleanup;
2404
    }
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418

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

2420 2421 2422 2423
    rv = 0;

cleanup:
    if (rv < 0)
2424
        virNetMessageSaveError(rerr);
2425
    return rv;
2426 2427 2428
}


2429
#ifdef WITH_SASL
2430 2431
/*
 * Initializes the SASL session in prepare for authentication
2432
 * and gives the client a list of allowed mechanisms to choose
2433 2434
 */
static int
2435 2436
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2437
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2438
                           virNetMessageErrorPtr rerr,
2439
                           remote_auth_sasl_init_ret *ret)
2440
{
2441 2442 2443
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2444

2445
    virMutexLock(&priv->lock);
2446

2447 2448 2449
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2450
        VIR_ERROR(_("client tried invalid SASL init request"));
2451
        goto authfail;
2452 2453
    }

2454 2455 2456 2457 2458
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2459
        goto authfail;
2460

2461
# if WITH_GNUTLS
2462
    /* Inform SASL that we've got an external SSF layer from TLS */
2463 2464 2465 2466
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2467
            goto authfail;
2468 2469 2470 2471 2472

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2473
            goto authfail;
2474
    }
2475
# endif
2476

2477
    if (virNetServerClientIsSecure(client))
2478
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2479 2480
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2481
        /* Plain TCP, better get an SSF layer */
2482 2483 2484 2485
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
2486

2487
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
2488
        goto authfail;
2489
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
2490

2491 2492
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
2493
    return 0;
2494 2495

authfail:
2496
    virResetLastError();
2497 2498
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2499
    virNetMessageSaveError(rerr);
2500 2501 2502
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2503
    virObjectUnref(sasl);
2504
    virMutexUnlock(&priv->lock);
2505
    return -1;
2506 2507
}

2508
/*
2509 2510
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
2511
static int
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527
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;
        }
2528
    }
2529 2530

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
2531
        return -2;
2532

2533 2534
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
2535

2536
    virNetServerClientSetAuth(client, 0);
2537
    virNetServerClientSetSASLSession(client, priv->sasl);
2538

2539
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
2540

2541 2542 2543
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2544

2545
    virObjectUnref(priv->sasl);
2546
    priv->sasl = NULL;
2547

2548
    return 0;
2549

2550 2551 2552
error:
    return -1;
}
2553

2554 2555 2556 2557
/*
 * This starts the SASL authentication negotiation.
 */
static int
2558 2559
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client,
2560
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2561
                            virNetMessageErrorPtr rerr,
2562 2563
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2564 2565
{
    const char *serverout;
2566
    size_t serveroutlen;
2567
    int err;
2568 2569 2570
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2571
    const char *identity;
2572

2573
    virMutexLock(&priv->lock);
2574

2575 2576 2577
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
2578
        VIR_ERROR(_("client tried invalid SASL start request"));
2579
        goto authfail;
2580 2581
    }

2582 2583
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
2584 2585 2586 2587 2588 2589 2590 2591 2592
    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)
2593
        goto authfail;
2594

2595
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2596
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
2597
        goto authfail;
2598 2599 2600 2601
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2602 2603
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2604 2605 2606 2607 2608 2609 2610
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2611
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2612
    if (err == VIR_NET_SASL_CONTINUE) {
2613 2614
        ret->complete = 0;
    } else {
2615
        /* Check username whitelist ACL */
2616
        if ((err = remoteSASLFinish(client)) < 0) {
2617 2618 2619 2620 2621
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2622

2623 2624 2625
        ret->complete = 1;
    }

2626
    virMutexUnlock(&priv->lock);
2627
    return 0;
2628 2629

authfail:
2630 2631 2632
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2633 2634 2635
    goto error;

authdeny:
2636
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2637 2638 2639
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2640 2641
    goto error;

2642
error:
2643
    virObjectUnref(priv->sasl);
2644 2645
    priv->sasl = NULL;
    virResetLastError();
2646 2647
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2648 2649 2650
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2651
    return -1;
2652 2653 2654 2655
}


static int
2656 2657
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2658
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2659
                           virNetMessageErrorPtr rerr,
2660 2661
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
2662 2663
{
    const char *serverout;
2664
    size_t serveroutlen;
2665
    int err;
2666 2667 2668
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2669
    const char *identity;
2670

2671 2672 2673 2674 2675
    virMutexLock(&priv->lock);

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

2680
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
2681
              args->data.data_len, args->nil);
2682 2683 2684 2685 2686 2687 2688 2689
    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)
2690
        goto authfail;
2691 2692

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2693
        VIR_ERROR(_("sasl step reply data too long %d"),
2694
                  (int)serveroutlen);
2695
        goto authfail;
2696 2697 2698 2699
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2700 2701
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2702 2703 2704 2705 2706 2707 2708
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2709
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2710
    if (err == VIR_NET_SASL_CONTINUE) {
2711 2712
        ret->complete = 0;
    } else {
2713
        /* Check username whitelist ACL */
2714
        if ((err = remoteSASLFinish(client)) < 0) {
2715 2716 2717 2718 2719
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2720

2721 2722 2723
        ret->complete = 1;
    }

2724
    virMutexUnlock(&priv->lock);
2725
    return 0;
2726 2727

authfail:
2728 2729 2730
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2731 2732 2733
    goto error;

authdeny:
2734
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2735 2736 2737
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2738 2739
    goto error;

2740
error:
2741
    virObjectUnref(priv->sasl);
2742 2743
    priv->sasl = NULL;
    virResetLastError();
2744 2745
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2746 2747 2748
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2749 2750
    return -1;
}
2751 2752 2753 2754
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2755
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2756 2757 2758 2759
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
2760 2761
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2762 2763 2764 2765 2766 2767
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
2768
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2769 2770 2771 2772 2773
                            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");
2774 2775
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2776 2777 2778 2779 2780 2781
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2782
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2783 2784 2785 2786 2787
                           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");
2788 2789
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
2790 2791 2792 2793
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
2794 2795 2796



2797
#if WITH_POLKIT1
2798
static int
2799 2800
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
                         virNetServerClientPtr client,
2801
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2802
                         virNetMessageErrorPtr rerr,
2803
                         remote_auth_polkit_ret *ret)
2804
{
2805
    pid_t callerPid = -1;
2806
    gid_t callerGid = -1;
2807
    uid_t callerUid = -1;
2808 2809
    const char *action;
    int status = -1;
2810
    char *ident = NULL;
2811
    bool authdismissed = 0;
2812
    char *pkout = NULL;
2813 2814
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2815
    virCommandPtr cmd = NULL;
2816

2817 2818
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
2819 2820 2821
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

2822
    cmd = virCommandNewArgList(PKCHECK_PATH, "--action-id", action, NULL);
2823
    virCommandSetOutputBuffer(cmd, &pkout);
2824
    virCommandSetErrorBuffer(cmd, &pkout);
2825

2826 2827
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2828
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2829 2830 2831
        goto authfail;
    }

2832 2833
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
                                          &callerPid) < 0) {
2834 2835 2836
        goto authfail;
    }

2837 2838
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2839

2840
    virCommandAddArg(cmd, "--process");
2841
    virCommandAddArgFormat(cmd, "%lld", (long long) callerPid);
2842
    virCommandAddArg(cmd, "--allow-user-interaction");
2843

2844 2845
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
                    (long long) callerPid, callerUid) < 0) {
2846
        virReportOOMError();
2847 2848 2849
        goto authfail;
    }

2850
    if (virCommandRun(cmd, &status) < 0)
2851
        goto authfail;
2852

2853
    authdismissed = (pkout && strstr(pkout, "dismissed=true"));
2854
    if (status != 0) {
2855
        char *tmp = virProcessTranslateStatus(status);
2856 2857
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d: %s"),
                  action, (long long) callerPid, callerUid, NULLSTR(tmp));
2858
        VIR_FREE(tmp);
2859
        goto authdeny;
2860
    }
2861 2862 2863
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2864 2865
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
2866 2867
    ret->complete = 1;

2868
    virNetServerClientSetAuth(client, 0);
2869
    virMutexUnlock(&priv->lock);
2870
    virCommandFree(cmd);
E
Eric Blake 已提交
2871
    VIR_FREE(pkout);
2872
    VIR_FREE(ident);
2873

2874 2875
    return 0;

2876
error:
2877 2878
    virCommandFree(cmd);
    VIR_FREE(ident);
2879
    virResetLastError();
2880

2881
    if (authdismissed) {
2882 2883
        virReportError(VIR_ERR_AUTH_CANCELLED, "%s",
                       _("authentication cancelled by user"));
2884 2885
    } else if (pkout && *pkout) {
        virReportError(VIR_ERR_AUTH_FAILED, _("polkit: %s"), pkout);
2886
    } else {
2887
        virReportError(VIR_ERR_AUTH_FAILED, "%s", _("authentication failed"));
2888
    }
2889 2890

    VIR_FREE(pkout);
2891 2892 2893 2894
    virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return -1;

2895
authfail:
2896 2897 2898
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
2899 2900 2901
    goto error;

authdeny:
2902 2903
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
2904
          client, REMOTE_AUTH_POLKIT, ident);
2905
    goto error;
2906
}
2907
#elif WITH_POLKIT0
2908
static int
2909
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
2910
                         virNetServerClientPtr client,
2911
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2912
                         virNetMessageErrorPtr rerr,
2913
                         remote_auth_polkit_ret *ret)
2914 2915
{
    pid_t callerPid;
2916
    gid_t callerGid;
2917
    uid_t callerUid;
2918 2919 2920 2921 2922 2923
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
2924
    const char *action;
2925
    char *ident = NULL;
2926 2927
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2928
    DBusConnection *sysbus;
2929

J
Jim Fehlig 已提交
2930
    virMutexLock(&priv->lock);
2931

J
Jim Fehlig 已提交
2932
    action = virNetServerClientGetReadonly(client) ?
2933 2934
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
2935

2936
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
J
Jim Fehlig 已提交
2937
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2938
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2939
        goto authfail;
2940 2941
    }

2942 2943
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
                                          &callerPid) < 0) {
2944
        VIR_ERROR(_("cannot get peer socket identity"));
2945
        goto authfail;
2946 2947
    }

2948 2949 2950
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
                    (long long) callerPid, callerUid) < 0) {
        virReportOOMError();
2951 2952 2953
        goto authfail;
    }

2954 2955 2956
    if (!(sysbus = virDBusGetSystemBus()))
        goto authfail;

2957 2958
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2959
    dbus_error_init(&err);
2960
    if (!(pkcaller = polkit_caller_new_from_pid(sysbus,
2961
                                                callerPid, &err))) {
2962
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
2963
        dbus_error_free(&err);
2964
        goto authfail;
2965
    }
2966

2967
    if (!(pkaction = polkit_action_new())) {
2968
        char ebuf[1024];
2969
        VIR_ERROR(_("Failed to create polkit action %s"),
2970
                  virStrerror(errno, ebuf, sizeof(ebuf)));
2971
        polkit_caller_unref(pkcaller);
2972
        goto authfail;
2973 2974 2975 2976 2977
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
2978
        char ebuf[1024];
2979
        VIR_ERROR(_("Failed to create polkit context %s"),
2980
                  (pkerr ? polkit_error_get_error_message(pkerr)
2981
                   : virStrerror(errno, ebuf, sizeof(ebuf))));
2982 2983 2984 2985 2986
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
2987
        goto authfail;
2988
    }
2989

2990
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
2991 2992 2993 2994 2995 2996
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
2997 2998 2999
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
3000
        goto authfail;
3001
    }
3002
# else
3003 3004 3005
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
3006
# endif
3007 3008 3009 3010
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
3011 3012
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d, result: %s"),
                  action, (long long) callerPid, callerUid,
3013
                  polkit_result_to_string_representation(pkresult));
3014
        goto authdeny;
3015
    }
3016 3017 3018
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3019 3020
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d, result %s",
             action, (long long) callerPid, callerUid,
3021 3022
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
3023

3024
    virNetServerClientSetAuth(client, 0);
J
Jim Fehlig 已提交
3025
    virMutexUnlock(&priv->lock);
3026
    VIR_FREE(ident);
3027
    return 0;
3028

3029
error:
3030
    VIR_FREE(ident);
3031
    virResetLastError();
3032 3033
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3034
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
3035
    virMutexUnlock(&priv->lock);
3036 3037
    return -1;

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

3044
authdeny:
3045 3046 3047
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
3048
    goto error;
3049 3050
}

3051
#else /* !WITH_POLKIT0 & !HAVE_POLKIT1*/
3052 3053

static int
3054
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
3055
                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
3056
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
3057
                         virNetMessageErrorPtr rerr,
3058
                         remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
3059
{
3060
    VIR_ERROR(_("client tried unsupported PolicyKit init request"));
3061 3062
    virReportError(VIR_ERR_AUTH_FAILED, "%s",
                   _("authentication failed"));
3063
    virNetMessageSaveError(rerr);
3064 3065
    return -1;
}
3066
#endif /* WITH_POLKIT1 */
3067 3068


3069 3070 3071
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
3072

3073
static int
3074 3075
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3076
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3077
                                  virNetMessageErrorPtr rerr,
3078 3079
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
3080
{
3081 3082
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
3083
    int rv = -1;
3084 3085
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3086

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

3092
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
3093 3094
        goto cleanup;

3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
    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;
    }

3114 3115 3116 3117
    rv = 0;

cleanup:
    if (rv < 0)
3118
        virNetMessageSaveError(rerr);
3119 3120
    if (dev)
        virNodeDeviceFree(dev);
3121
    return rv;
3122 3123
}

3124 3125 3126 3127 3128

/***************************
 * Register / deregister events
 ***************************/
static int
3129 3130
remoteDispatchDomainEventsRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
3131
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
3132
                                   virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
3133
                                   remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
3134
{
3135
    int callbackID;
3136
    int rv = -1;
3137 3138
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
3139

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

3145 3146 3147
    virMutexLock(&priv->lock);

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

3152
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3153 3154 3155 3156
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
                                                       client, NULL)) < 0)
3157
        goto cleanup;
O
Osier Yang 已提交
3158

3159
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;
3160

3161 3162 3163 3164
    rv = 0;

cleanup:
    if (rv < 0)
3165 3166
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3167
    return rv;
O
Osier Yang 已提交
3168 3169
}

3170
static int
3171 3172
remoteDispatchDomainEventsDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
3173
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
3174
                                     virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
3175
                                     remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
3176
{
3177
    int rv = -1;
3178 3179
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3180

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

3186 3187 3188
    virMutexLock(&priv->lock);

    if (priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] < 0) {
3189
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
3190
        goto cleanup;
3191
    }
3192

3193 3194
    if (virConnectDomainEventDeregisterAny(priv->conn,
                                           priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0)
3195
        goto cleanup;
3196

3197 3198
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;

3199 3200 3201
    rv = 0;

cleanup:
3202
    if (rv < 0)
3203 3204
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3205 3206 3207 3208
    return rv;
}

static void
3209 3210
remoteDispatchDomainEventSend(virNetServerClientPtr client,
                              virNetServerProgramPtr program,
3211 3212 3213 3214
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3215
    virNetMessagePtr msg;
3216

3217
    if (!(msg = virNetMessageNew(false)))
3218
        goto cleanup;
3219

3220 3221 3222 3223 3224 3225
    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;
3226

3227
    if (virNetMessageEncodeHeader(msg) < 0)
3228 3229
        goto cleanup;

3230 3231
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3232

3233 3234
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3235

3236
    xdr_free(proc, data);
3237 3238 3239
    return;

cleanup:
3240
    virNetMessageFree(msg);
3241
    xdr_free(proc, data);
3242 3243
}

3244
static int
3245 3246
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3247
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3248
                             virNetMessageErrorPtr rerr,
3249 3250
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3251
{
3252 3253 3254
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3255
    int rv = -1;
3256 3257
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3258

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

3264
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3265
        goto cleanup;
3266

3267
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3268
        goto cleanup;
3269

3270 3271 3272
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3273 3274 3275 3276
    rv = 0;

cleanup:
    if (rv < 0)
3277
        virNetMessageSaveError(rerr);
3278 3279
    if (secret)
        virSecretFree(secret);
3280
    return rv;
3281 3282
}

3283
static int
3284 3285
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3286
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3287
                             virNetMessageErrorPtr rerr,
3288 3289 3290 3291 3292
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3293 3294
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3295

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

3301
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3302 3303 3304 3305 3306 3307 3308 3309 3310
        goto cleanup;

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

    rv = 0;

cleanup:
    if (rv < 0)
3311
        virNetMessageSaveError(rerr);
3312 3313 3314 3315 3316
    if (dom)
        virDomainFree(dom);
    return rv;
}

3317
static int
3318 3319
remoteDispatchDomainEventsRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
3320
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
3321 3322
                                      virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                      remote_domain_events_register_any_args *args)
3323 3324
{
    int callbackID;
3325
    int rv = -1;
3326 3327
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3328

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

3334 3335
    virMutexLock(&priv->lock);

3336 3337
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3338
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
3339
        goto cleanup;
3340 3341
    }

3342
    if (priv->domainEventCallbackID[args->eventID] != -1)  {
3343
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), args->eventID);
3344
        goto cleanup;
3345 3346
    }

3347
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3348 3349 3350
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3351
                                                       client, NULL)) < 0)
3352
        goto cleanup;
3353

3354
    priv->domainEventCallbackID[args->eventID] = callbackID;
3355

3356 3357 3358 3359
    rv = 0;

cleanup:
    if (rv < 0)
3360 3361
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3362
    return rv;
3363 3364 3365 3366
}


static int
3367 3368
remoteDispatchDomainEventsDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
3369
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
3370 3371
                                        virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                        remote_domain_events_deregister_any_args *args)
3372 3373
{
    int callbackID = -1;
3374
    int rv = -1;
3375 3376
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3377

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

3383 3384
    virMutexLock(&priv->lock);

3385 3386
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3387
        virReportError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
3388
        goto cleanup;
3389 3390
    }

3391 3392
    callbackID = priv->domainEventCallbackID[args->eventID];
    if (callbackID < 0) {
3393
        virReportError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), args->eventID);
3394
        goto cleanup;
3395 3396
    }

3397
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3398
        goto cleanup;
3399

3400 3401
    priv->domainEventCallbackID[args->eventID] = -1;

3402 3403 3404 3405
    rv = 0;

cleanup:
    if (rv < 0)
3406 3407
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3408
    return rv;
3409 3410
}

C
Chris Lalancette 已提交
3411
static int
3412 3413
qemuDispatchMonitorCommand(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3414
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3415
                           virNetMessageErrorPtr rerr,
3416 3417
                           qemu_monitor_command_args *args,
                           qemu_monitor_command_ret *ret)
C
Chris Lalancette 已提交
3418
{
3419
    virDomainPtr dom = NULL;
3420
    int rv = -1;
3421 3422
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
3423

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

3429
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3430
        goto cleanup;
C
Chris Lalancette 已提交
3431

3432
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
3433
                                    args->flags) < 0)
3434
        goto cleanup;
C
Chris Lalancette 已提交
3435

3436
    rv = 0;
C
Chris Lalancette 已提交
3437

3438 3439
cleanup:
    if (rv < 0)
3440
        virNetMessageSaveError(rerr);
3441 3442
    if (dom)
        virDomainFree(dom);
3443
    return rv;
C
Chris Lalancette 已提交
3444 3445
}

3446

3447
static int
3448 3449
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3450
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3451
                                  virNetMessageErrorPtr rerr,
3452 3453 3454 3455 3456 3457
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
3458
    char *xmlin;
3459 3460 3461
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3462 3463
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3464

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

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

3473
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3474 3475
    dname = args->dname == NULL ? NULL : *args->dname;

3476
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491
                                       &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)
3492
        virNetMessageSaveError(rerr);
3493 3494 3495 3496 3497 3498 3499
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3500 3501
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3502
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3503
                                    virNetMessageErrorPtr rerr,
3504 3505 3506 3507 3508 3509 3510 3511 3512
                                    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;
3513 3514
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3515

3516
    if (!priv->conn) {
3517
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529
        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;
    }

3530
    if (virDomainMigratePrepare3(priv->conn,
3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549
                                 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) {
3550
        virNetMessageSaveError(rerr);
3551 3552 3553 3554 3555
        VIR_FREE(uri_out);
    }
    return rv;
}

3556

3557
static int
3558 3559
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3560
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3561
                                    virNetMessageErrorPtr rerr,
3562 3563 3564 3565
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
3566
    char *xmlin;
3567
    char *dname;
3568 3569
    char *uri;
    char *dconnuri;
3570 3571 3572
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3573 3574
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3575

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

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

3584
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3585
    dname = args->dname == NULL ? NULL : *args->dname;
3586 3587
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
3588

3589
    if (virDomainMigratePerform3(dom, xmlin,
3590 3591 3592
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
3593
                                 dconnuri, uri,
3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605
                                 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)
3606
        virNetMessageSaveError(rerr);
3607 3608 3609 3610 3611 3612 3613
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3614 3615
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
3616
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
3617
                                   virNetMessageErrorPtr rerr,
3618 3619 3620 3621 3622 3623
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
3624 3625
    char *uri;
    char *dconnuri;
3626
    int rv = -1;
3627 3628
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3629

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

3635 3636 3637
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

3638
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
3639 3640 3641 3642 3643 3644
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
3645 3646
        goto cleanup;

3647
    make_nonnull_domain(&ret->dom, dom);
3648 3649 3650 3651 3652 3653 3654 3655 3656 3657

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

    rv = 0;

cleanup:
    if (rv < 0) {
3658
        virNetMessageSaveError(rerr);
3659 3660 3661 3662 3663 3664 3665 3666 3667
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3668 3669
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3670
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3671 3672
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
3673 3674 3675
{
    virDomainPtr dom = NULL;
    int rv = -1;
3676 3677
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3678

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

3684
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696
        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)
3697
        virNetMessageSaveError(rerr);
3698 3699 3700 3701 3702 3703
    if (dom)
        virDomainFree(dom);
    return rv;
}


3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716
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);

3717 3718 3719 3720 3721 3722 3723 3724 3725 3726
    /* 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;
    }

3727
    if (!priv->conn) {
3728
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
        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;
    }

3743
done:
3744 3745 3746 3747 3748 3749 3750 3751 3752 3753
    ret->supported = supported;
    rv = 0;

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


3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767
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) {
3768
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794
        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;
}

3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812
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) {
3813
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3814 3815 3816 3817 3818 3819
        goto cleanup;
    }

    flags = args->flags;

    if (nparams > REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX) {
3820
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3821 3822
        goto cleanup;
    }
3823
    if (nparams && VIR_ALLOC_N(params, nparams) < 0) {
3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853
        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);
3854
    virTypedParamsFree(params, nparams);
3855 3856 3857 3858
    if (dom)
        virDomainFree(dom);
    return rv;
}
3859

3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875
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) {
3876
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3877 3878 3879 3880
        goto cleanup;
    }

    if (args->nparams > REMOTE_NODE_CPU_STATS_MAX) {
3881
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
3882 3883 3884
        goto cleanup;
    }
    if (args->ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
3885
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("ncpus too large"));
3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915
        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;

success:
    rv = 0;
    ret->nparams = percpu_len;
3916 3917 3918 3919 3920 3921 3922 3923
    if (args->nparams && !(args->flags & VIR_TYPED_PARAM_STRING_OKAY)) {
        int i;

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

cleanup:
    if (rv < 0)
         virNetMessageSaveError(rerr);
3928
    virTypedParamsFree(params, args->ncpus * args->nparams);
3929 3930 3931 3932 3933
    if (dom)
        virDomainFree(dom);
    return rv;
}

3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944
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;
3945
    int len = 0;
3946 3947 3948 3949
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

    if (!priv->conn) {
3950
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
3951 3952 3953 3954 3955 3956 3957
        goto cleanup;
    }

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

    if (args->maxerrors > REMOTE_DOMAIN_DISK_ERRORS_MAX) {
3958 3959
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("maxerrors too large"));
3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996
        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;
}

3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012
static int
remoteDispatchDomainListAllSnapshots(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_domain_list_all_snapshots_args *args,
                                     remote_domain_list_all_snapshots_ret *ret)
{
    virDomainSnapshotPtr *snaps = NULL;
    int nsnaps = 0;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
    virDomainPtr dom = NULL;

    if (!priv->conn) {
4013
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073
        goto cleanup;
    }

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

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

    if (snaps && nsnaps) {
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0) {
            virReportOOMError();
            goto cleanup;
        }

        ret->snapshots.snapshots_len = nsnaps;

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

    ret->ret = nsnaps;
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    if (snaps) {
        for (i = 0; i < nsnaps; i++)
            virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }
    return rv;
}

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

    if (!priv->conn) {
4074
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122
        goto cleanup;
    }

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

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

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

    if (snaps && nsnaps) {
        if (VIR_ALLOC_N(ret->snapshots.snapshots_val, nsnaps) < 0) {
            virReportOOMError();
            goto cleanup;
        }

        ret->snapshots.snapshots_len = nsnaps;

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

    ret->ret = nsnaps;
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (snapshot)
        virDomainSnapshotFree(snapshot);
    if (dom)
        virDomainFree(dom);
    if (snaps) {
        for (i = 0; i < nsnaps; i++)
            virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }
    return rv;
}

4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176
static int
remoteDispatchConnectListAllStoragePools(virNetServerPtr server ATTRIBUTE_UNUSED,
                                         virNetServerClientPtr client,
                                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                         virNetMessageErrorPtr rerr,
                                         remote_connect_list_all_storage_pools_args *args,
                                         remote_connect_list_all_storage_pools_ret *ret)
{
    virStoragePoolPtr *pools = NULL;
    int npools = 0;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

    if (pools && npools) {
        if (VIR_ALLOC_N(ret->pools.pools_val, npools) < 0) {
            virReportOOMError();
            goto cleanup;
        }

        ret->pools.pools_len = npools;

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

    ret->ret = npools;

    rv = 0;

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

4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234
static int
remoteDispatchStoragePoolListAllVolumes(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_storage_pool_list_all_volumes_args *args,
                                        remote_storage_pool_list_all_volumes_ret *ret)
{
    virStorageVolPtr *vols = NULL;
    virStoragePoolPtr pool = NULL;
    int nvols = 0;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

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

    if (vols && nvols) {
        if (VIR_ALLOC_N(ret->vols.vols_val, nvols) < 0) {
            virReportOOMError();
            goto cleanup;
        }

        ret->vols.vols_len = nvols;

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

    ret->ret = nvols;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (vols) {
        for (i = 0; i < nvols; i++)
            virStorageVolFree(vols[i]);
        VIR_FREE(vols);
    }
    return rv;
}

4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288
static int
remoteDispatchConnectListAllNetworks(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client,
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                     virNetMessageErrorPtr rerr,
                                     remote_connect_list_all_networks_args *args,
                                     remote_connect_list_all_networks_ret *ret)
{
    virNetworkPtr *nets = NULL;
    int nnets = 0;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

    if (nets && nnets) {
        if (VIR_ALLOC_N(ret->nets.nets_val, nnets) < 0) {
            virReportOOMError();
            goto cleanup;
        }

        ret->nets.nets_len = nnets;

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

    ret->ret = nnets;

    rv = 0;

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

4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342
static int
remoteDispatchConnectListAllInterfaces(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client,
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                       virNetMessageErrorPtr rerr,
                                       remote_connect_list_all_interfaces_args *args,
                                       remote_connect_list_all_interfaces_ret *ret)
{
    virInterfacePtr *ifaces = NULL;
    int nifaces = 0;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

    if (ifaces && nifaces) {
        if (VIR_ALLOC_N(ret->ifaces.ifaces_val, nifaces) < 0) {
            virReportOOMError();
            goto cleanup;
        }

        ret->ifaces.ifaces_len = nifaces;

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

    ret->ret = nifaces;

    rv = 0;

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

4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395
static int
remoteDispatchConnectListAllNodeDevices(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client,
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                        virNetMessageErrorPtr rerr,
                                        remote_connect_list_all_node_devices_args *args,
                                        remote_connect_list_all_node_devices_ret *ret)
{
    virNodeDevicePtr *devices = NULL;
    int ndevices = 0;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

    if (devices && ndevices) {
        if (VIR_ALLOC_N(ret->devices.devices_val, ndevices) < 0) {
            virReportOOMError();
            goto cleanup;
        }

        ret->devices.devices_len = ndevices;

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

    ret->ret = ndevices;

    rv = 0;

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

4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450
static int
remoteDispatchConnectListAllNWFilters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_connect_list_all_nwfilters_args *args,
                                      remote_connect_list_all_nwfilters_ret *ret)
{
    virNWFilterPtr *filters = NULL;
    int nfilters = 0;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

    if (filters && nfilters) {
        if (VIR_ALLOC_N(ret->filters.filters_val, nfilters) < 0) {
            virReportOOMError();
            goto cleanup;
        }

        ret->filters.filters_len = nfilters;

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

    ret->ret = nfilters;

    rv = 0;

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

4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504
static int
remoteDispatchConnectListAllSecrets(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client,
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                    virNetMessageErrorPtr rerr,
                                    remote_connect_list_all_secrets_args *args,
                                    remote_connect_list_all_secrets_ret *ret)
{
    virSecretPtr *secrets = NULL;
    int nsecrets = 0;
    int i;
    int rv = -1;
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

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

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

    if (secrets && nsecrets) {
        if (VIR_ALLOC_N(ret->secrets.secrets_val, nsecrets) < 0) {
            virReportOOMError();
            goto cleanup;
        }

        ret->secrets.secrets_len = nsecrets;

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

    ret->ret = nsecrets;

    rv = 0;

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

4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559
static int
remoteDispatchNodeGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_node_get_memory_parameters_args *args,
                                      remote_node_get_memory_parameters_ret *ret)
{
    virTypedParameterPtr params = NULL;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    flags = args->flags;

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


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

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

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

success:
    rv = 0;

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

4564 4565 4566 4567 4568 4569 4570 4571 4572
static int
remoteDispatchNodeGetCPUMap(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
                            virNetMessageErrorPtr rerr,
                            remote_node_get_cpu_map_args *args,
                            remote_node_get_cpu_map_ret *ret)
{
    unsigned char *cpumap = NULL;
4573
    unsigned int online = 0;
4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586
    unsigned int flags;
    int cpunum;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    flags = args->flags;

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

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

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

    rv = 0;

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

4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660
static int
lxcDispatchDomainOpenNamespace(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
                               virNetMessageErrorPtr rerr,
                               lxc_domain_open_namespace_args *args)
{
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
    int *fdlist = NULL;
    int ret;
    virDomainPtr dom = NULL;
    size_t i;

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

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

    ret = virDomainLxcOpenNamespace(dom,
                                    &fdlist,
                                    args->flags);
    if (ret < 0)
        goto cleanup;

    /* We shouldn't have received any from the client,
     * but in case they're playing games with us, prevent
     * a resource leak
     */
    for (i = 0 ; i < msg->nfds ; i++)
        VIR_FORCE_CLOSE(msg->fds[i]);
    VIR_FREE(msg->fds);
    msg->nfds = 0;

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

    rv = 1;

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

4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704
static int
remoteDispatchDomainGetJobStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client,
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                virNetMessageErrorPtr rerr,
                                remote_domain_get_job_stats_args *args,
                                remote_domain_get_job_stats_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

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

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

    rv = 0;

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

4705 4706 4707 4708 4709 4710 4711 4712 4713
/*----- 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
4714
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
4715 4716
{
    virDomainPtr dom;
4717
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
4718 4719 4720 4721 4722 4723 4724 4725
    /* 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
4726
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
4727
{
4728
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
4729 4730
}

D
Daniel Veillard 已提交
4731
static virInterfacePtr
4732
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
4733
{
4734
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
4735 4736
}

4737
static virStoragePoolPtr
4738
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
4739
{
4740 4741
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
4742 4743 4744
}

static virStorageVolPtr
4745
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
4746 4747
{
    virStorageVolPtr ret;
4748 4749
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                           NULL, NULL);
4750 4751 4752
    return ret;
}

4753
static virSecretPtr
4754
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
4755
{
4756
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
4757 4758
}

4759
static virNWFilterPtr
4760
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
4761
{
4762
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
4763 4764
}

C
Chris Lalancette 已提交
4765
static virDomainSnapshotPtr
4766
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
4767
{
4768
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
4769 4770
}

4771 4772
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
4773
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
4774 4775
{
    dom_dst->id = dom_src->id;
4776 4777
    dom_dst->name = strdup(dom_src->name);
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
4778 4779 4780
}

static void
4781
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
4782
{
4783 4784
    net_dst->name = strdup(net_src->name);
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
4785 4786
}

D
Daniel Veillard 已提交
4787
static void
4788 4789
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
4790
{
4791 4792
    interface_dst->name = strdup(interface_src->name);
    interface_dst->mac = strdup(interface_src->mac);
D
Daniel Veillard 已提交
4793 4794
}

4795
static void
4796
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
4797
{
4798 4799
    pool_dst->name = strdup(pool_src->name);
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
4800 4801 4802
}

static void
4803
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
4804
{
4805 4806 4807
    vol_dst->pool = strdup(vol_src->pool);
    vol_dst->name = strdup(vol_src->name);
    vol_dst->key = strdup(vol_src->key);
4808
}
4809 4810

static void
4811
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
4812 4813 4814
{
    dev_dst->name = strdup(dev_src->name);
}
4815 4816

static void
4817
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
4818
{
4819
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
4820
    secret_dst->usageType = secret_src->usageType;
4821
    secret_dst->usageID = strdup(secret_src->usageID);
4822
}
4823 4824

static void
4825
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
4826
{
4827 4828
    nwfilter_dst->name = strdup(nwfilter_src->name);
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
4829
}
C
Chris Lalancette 已提交
4830 4831

static void
4832
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
4833 4834
{
    snapshot_dst->name = strdup(snapshot_src->name);
4835
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
4836
}
4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870

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