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

#include <config.h>

25
#include "virterror_internal.h"
26

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

32
#include "remote.h"
33
#include "libvirtd.h"
34 35
#include "libvirt_internal.h"
#include "datatypes.h"
36
#include "memory.h"
37
#include "logging.h"
38
#include "util.h"
C
Chris Lalancette 已提交
39
#include "stream.h"
40
#include "uuid.h"
C
Chris Lalancette 已提交
41
#include "libvirt/libvirt-qemu.h"
42
#include "command.h"
43
#include "intprops.h"
44
#include "virnetserverservice.h"
J
Jim Fehlig 已提交
45
#include "virnetserver.h"
46
#include "virfile.h"
47
#include "virtypedparam.h"
48
#include "virdbus.h"
49 50
#include "remote_protocol.h"
#include "qemu_protocol.h"
51

52 53

#define VIR_FROM_THIS VIR_FROM_RPC
54

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

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

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

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

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

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

108 109
#include "remote_dispatch.h"
#include "qemu_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 543 544 545 546 547 548 549 550 551 552
static int remoteRelayDomainEventPMWakeup(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          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 */
553
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
554 555 556 557 558 559 560 561 562
    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 已提交
563 564 565 566 567 568 569 570 571 572 573 574
static int remoteRelayDomainEventPMSuspend(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           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 */
575
    memset(&data, 0, sizeof(data));
O
Osier Yang 已提交
576 577 578 579 580 581 582 583 584
    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;
}

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


612
static virConnectDomainEventGenericCallback domainEventCallbacks[] = {
613
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
614
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventReboot),
615
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventRTCChange),
616
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventWatchdog),
617
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOError),
618
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventGraphics),
619
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventIOErrorReason),
620
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventControlError),
621
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBlockJob),
622
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventDiskChange),
623
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventTrayChange),
O
Osier Yang 已提交
624
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMWakeup),
O
Osier Yang 已提交
625
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventPMSuspend),
626
    VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventBalloonChange),
627 628 629 630
};

verify(ARRAY_CARDINALITY(domainEventCallbacks) == VIR_DOMAIN_EVENT_ID_LAST);

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
/*
 * You must hold lock for at least the client
 * We don't free stuff here, merely disconnect the client's
 * network socket & resources.
 * We keep the libvirt connection open until any async
 * jobs have finished, then clean it up elsewhere
 */
static void remoteClientFreeFunc(void *data)
{
    struct daemonClientPrivate *priv = data;

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

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

        virConnectClose(priv->conn);
    }

    VIR_FREE(priv);
}


662 663 664 665 666 667 668
static void remoteClientCloseFunc(virNetServerClientPtr client)
{
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);

    daemonRemoveAllClientStreams(priv->streams);
}

669 670

int remoteClientInitHook(virNetServerPtr srv ATTRIBUTE_UNUSED,
671 672
                         virNetServerClientPtr client,
                         void *opaque ATTRIBUTE_UNUSED)
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
{
    struct daemonClientPrivate *priv;
    int i;

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

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

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

691 692 693
    virNetServerClientSetPrivateData(client, priv,
                                     remoteClientFreeFunc);
    virNetServerClientSetCloseHook(client, remoteClientCloseFunc);
694 695 696
    return 0;
}

697 698 699
/*----- Functions. -----*/

static int
700
remoteDispatchOpen(virNetServerPtr server,
701
                   virNetServerClientPtr client,
702
                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
703 704
                   virNetMessageErrorPtr rerr,
                   struct remote_open_args *args)
705 706
{
    const char *name;
707
    unsigned int flags;
708
    struct daemonClientPrivate *priv = virNetServerClientGetPrivateData(client);
709
    int rv = -1;
710

711 712 713 714
    VIR_DEBUG("priv=%p conn=%p", priv, priv->conn);
    virMutexLock(&priv->lock);
    /* Already opened? */
    if (priv->conn) {
715 716 717 718
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection already open"));
        goto cleanup;
    }

719 720 721 722 723 724
    if (virNetServerKeepAliveRequired(server) && !priv->keepalive_supported) {
        virNetError(VIR_ERR_OPERATION_FAILED, "%s",
                    _("keepalive support is required to connect"));
        goto cleanup;
    }

725 726 727 728 729 730
    name = args->name ? *args->name : NULL;

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

734
    priv->conn =
735
        flags & VIR_CONNECT_RO
736 737
        ? virConnectOpenReadOnly(name)
        : virConnectOpen(name);
738

739
    if (priv->conn == NULL)
740 741 742
        goto cleanup;

    rv = 0;
743

744 745
cleanup:
    if (rv < 0)
746 747
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
748
    return rv;
749 750 751 752
}


static int
753
remoteDispatchClose(virNetServerPtr server ATTRIBUTE_UNUSED,
754
                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
755
                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
756
                    virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED)
757
{
758
    virNetServerClientDelayedClose(client);
759
    return 0;
760 761
}

762

763
static int
764 765
remoteDispatchDomainGetSchedulerType(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
766
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
767
                                     virNetMessageErrorPtr rerr,
768 769
                                     remote_domain_get_scheduler_type_args *args,
                                     remote_domain_get_scheduler_type_ret *ret)
770
{
771
    virDomainPtr dom = NULL;
772 773
    char *type;
    int nparams;
774
    int rv = -1;
775 776
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
777

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

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

786
    if (!(type = virDomainGetSchedulerType(dom, &nparams)))
787
        goto cleanup;
788 789 790

    ret->type = type;
    ret->nparams = nparams;
791 792 793 794
    rv = 0;

cleanup:
    if (rv < 0)
795
        virNetMessageSaveError(rerr);
796 797 798
    if (dom)
        virDomainFree(dom);
    return rv;
799 800
}

801 802
/* Helper to serialize typed parameters. This also filters out any string
 * parameters that must not be returned to older clients.  */
803 804 805
static int
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
806
                               remote_typed_param **ret_params_val,
807 808
                               u_int *ret_params_len,
                               unsigned int flags)
809 810
{
    int i;
811
    int j;
812 813 814 815 816 817 818 819 820
    int rv = -1;
    remote_typed_param *val;

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

821
    for (i = 0, j = 0; i < nparams; ++i) {
822 823 824 825 826
        /* 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)) {
827 828 829 830
            --*ret_params_len;
            continue;
        }

831
        /* remoteDispatchClientRequest will free this: */
832 833
        val[j].field = strdup(params[i].field);
        if (val[j].field == NULL) {
834 835 836
            virReportOOMError();
            goto cleanup;
        }
837
        val[j].value.type = params[i].type;
838
        switch (params[i].type) {
839
        case VIR_TYPED_PARAM_INT:
840
            val[j].value.remote_typed_param_value_u.i = params[i].value.i;
841 842
            break;
        case VIR_TYPED_PARAM_UINT:
843
            val[j].value.remote_typed_param_value_u.ui = params[i].value.ui;
844 845
            break;
        case VIR_TYPED_PARAM_LLONG:
846
            val[j].value.remote_typed_param_value_u.l = params[i].value.l;
847 848
            break;
        case VIR_TYPED_PARAM_ULLONG:
849
            val[j].value.remote_typed_param_value_u.ul = params[i].value.ul;
850 851
            break;
        case VIR_TYPED_PARAM_DOUBLE:
852
            val[j].value.remote_typed_param_value_u.d = params[i].value.d;
853 854
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
855 856 857 858 859 860 861 862 863
            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;
            }
864 865 866 867 868 869
            break;
        default:
            virNetError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                        params[i].type);
            goto cleanup;
        }
870
        j++;
871 872 873 874 875 876 877 878
    }

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

cleanup:
    if (val) {
879
        for (i = 0; i < nparams; i++) {
880
            VIR_FREE(val[i].field);
881
            if (val[i].value.type == VIR_TYPED_PARAM_STRING)
882 883
                VIR_FREE(val[i].value.remote_typed_param_value_u.s);
        }
884 885 886 887 888 889 890
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static virTypedParameterPtr
891 892
remoteDeserializeTypedParameters(remote_typed_param *args_params_val,
                                 u_int args_params_len,
893 894 895
                                 int limit,
                                 int *nparams)
{
896
    int i = 0;
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
    int rv = -1;
    virTypedParameterPtr params = NULL;

    /* Check the length of the returned list carefully. */
    if (args_params_len > limit) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, args_params_len) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    *nparams = args_params_len;

    /* Deserialise the result. */
    for (i = 0; i < args_params_len; ++i) {
        if (virStrcpyStatic(params[i].field,
                            args_params_val[i].field) == NULL) {
            virNetError(VIR_ERR_INTERNAL_ERROR,
                        _("Parameter %s too big for destination"),
                        args_params_val[i].field);
            goto cleanup;
        }
        params[i].type = args_params_val[i].value.type;
        switch (params[i].type) {
        case VIR_TYPED_PARAM_INT:
            params[i].value.i =
                args_params_val[i].value.remote_typed_param_value_u.i;
            break;
        case VIR_TYPED_PARAM_UINT:
            params[i].value.ui =
                args_params_val[i].value.remote_typed_param_value_u.ui;
            break;
        case VIR_TYPED_PARAM_LLONG:
            params[i].value.l =
                args_params_val[i].value.remote_typed_param_value_u.l;
            break;
        case VIR_TYPED_PARAM_ULLONG:
            params[i].value.ul =
                args_params_val[i].value.remote_typed_param_value_u.ul;
            break;
        case VIR_TYPED_PARAM_DOUBLE:
            params[i].value.d =
                args_params_val[i].value.remote_typed_param_value_u.d;
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
            params[i].value.b =
                args_params_val[i].value.remote_typed_param_value_u.b;
            break;
947 948 949 950 951 952 953 954
        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;
955 956 957 958 959 960 961 962 963 964
        default:
            virNetError(VIR_ERR_INTERNAL_ERROR, _("unknown parameter type: %d"),
                        params[i].type);
            goto cleanup;
        }
    }

    rv = 0;

cleanup:
965
    if (rv < 0) {
A
Alex Jia 已提交
966
        virTypedParameterArrayClear(params, i);
967
        VIR_FREE(params);
968
    }
969 970 971
    return params;
}

972
static int
973 974
remoteDispatchDomainGetSchedulerParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
975
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
976
                                           virNetMessageErrorPtr rerr,
977 978
                                           remote_domain_get_scheduler_parameters_args *args,
                                           remote_domain_get_scheduler_parameters_ret *ret)
979
{
980
    virDomainPtr dom = NULL;
981
    virTypedParameterPtr params = NULL;
982
    int nparams = args->nparams;
983
    int rv = -1;
984 985
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
986

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

992
    if (nparams > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX) {
993 994
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
995
    }
996 997
    if (VIR_ALLOC_N(params, nparams) < 0)
        goto no_memory;
998

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

1002
    if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0)
1003
        goto cleanup;
1004

1005
    if (remoteSerializeTypedParameters(params, nparams,
1006
                                       &ret->params.params_val,
1007 1008
                                       &ret->params.params_len,
                                       0) < 0)
1009 1010 1011 1012 1013 1014
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
1015
        virNetMessageSaveError(rerr);
1016 1017
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
1018 1019 1020 1021 1022 1023 1024 1025 1026
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
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) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        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;
}

1081
static int
1082 1083
remoteDispatchDomainGetSchedulerParametersFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1084
                                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1085
                                                virNetMessageErrorPtr rerr,
1086 1087 1088 1089 1090 1091 1092
                                                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;
1093 1094
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1095

1096
    if (!priv->conn) {
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

1108
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1109 1110 1111 1112 1113 1114 1115
        goto cleanup;

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

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

1121
    rv = 0;
1122 1123

cleanup:
1124
    if (rv < 0)
1125
        virNetMessageSaveError(rerr);
1126 1127
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
1128 1129 1130 1131 1132 1133 1134
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
1135 1136
}

1137
static int
1138 1139
remoteDispatchDomainMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
1140
                                virNetMessagePtr msg ATTRIBUTE_UNUSED,
1141
                                virNetMessageErrorPtr rerr,
1142 1143
                                remote_domain_memory_stats_args *args,
                                remote_domain_memory_stats_ret *ret)
1144
{
1145
    virDomainPtr dom = NULL;
1146
    struct _virDomainMemoryStat *stats;
1147
    int nr_stats, i;
1148
    int rv = -1;
1149 1150
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1151

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

1157
    if (args->maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1158 1159 1160
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("maxStats > REMOTE_DOMAIN_MEMORY_STATS_MAX"));
        goto cleanup;
1161 1162
    }

1163
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1164
        goto cleanup;
1165 1166 1167

    /* Allocate stats array for making dispatch call */
    if (VIR_ALLOC_N(stats, args->maxStats) < 0) {
1168 1169
        virReportOOMError();
        goto cleanup;
1170
    }
1171

1172
    nr_stats = virDomainMemoryStats(dom, stats, args->maxStats, args->flags);
1173
    if (nr_stats < 0)
1174
        goto cleanup;
1175 1176 1177

    /* Allocate return buffer */
    if (VIR_ALLOC_N(ret->stats.stats_val, args->maxStats) < 0) {
1178 1179
        virReportOOMError();
        goto cleanup;
1180 1181 1182 1183 1184 1185 1186 1187
    }

    /* 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;
1188 1189 1190 1191
    rv = 0;

cleanup:
    if (rv < 0)
1192
        virNetMessageSaveError(rerr);
1193 1194
    if (dom)
        virDomainFree(dom);
1195
    VIR_FREE(stats);
1196
    return rv;
1197 1198
}

1199
static int
1200 1201
remoteDispatchDomainBlockPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1202
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1203
                              virNetMessageErrorPtr rerr,
1204 1205
                              remote_domain_block_peek_args *args,
                              remote_domain_block_peek_ret *ret)
1206
{
1207
    virDomainPtr dom = NULL;
1208 1209 1210 1211
    char *path;
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1212
    int rv = -1;
1213 1214
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1215

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

1221
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1222
        goto cleanup;
1223 1224 1225 1226 1227 1228
    path = args->path;
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1229 1230 1231
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("size > maximum buffer size"));
        goto cleanup;
1232 1233 1234
    }

    ret->buffer.buffer_len = size;
1235
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
1236 1237
        virReportOOMError();
        goto cleanup;
1238 1239
    }

1240
    if (virDomainBlockPeek(dom, path, offset, size,
1241
                           ret->buffer.buffer_val, flags) < 0)
1242
        goto cleanup;
1243

1244 1245 1246 1247
    rv = 0;

cleanup:
    if (rv < 0) {
1248
        virNetMessageSaveError(rerr);
1249 1250 1251 1252 1253
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
1254 1255
}

1256 1257 1258
static int
remoteDispatchDomainBlockStatsFlags(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1259
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_block_stats_flags_args *args,
                                    remote_domain_block_stats_flags_ret *ret)
{
    virTypedParameterPtr params = NULL;
    virDomainPtr dom = NULL;
    const char *path = args->path;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

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

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

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

    /* Serialise the block stats. */
    if (remoteSerializeTypedParameters(params, nparams,
                                       &ret->params.params_val,
1305 1306
                                       &ret->params.params_len,
                                       args->flags) < 0)
1307 1308 1309 1310 1311 1312
        goto cleanup;

success:
    rv = 0;

cleanup:
1313
    if (rv < 0)
1314
        virNetMessageSaveError(rerr);
1315 1316
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
A
ajia@redhat.com 已提交
1317 1318
    if (dom)
        virDomainFree(dom);
1319 1320 1321
    return rv;
}

R
Richard W.M. Jones 已提交
1322
static int
1323 1324
remoteDispatchDomainMemoryPeek(virNetServerPtr server ATTRIBUTE_UNUSED,
                               virNetServerClientPtr client ATTRIBUTE_UNUSED,
1325
                               virNetMessagePtr msg ATTRIBUTE_UNUSED,
1326
                               virNetMessageErrorPtr rerr,
1327 1328
                               remote_domain_memory_peek_args *args,
                               remote_domain_memory_peek_ret *ret)
R
Richard W.M. Jones 已提交
1329
{
1330
    virDomainPtr dom = NULL;
R
Richard W.M. Jones 已提交
1331 1332 1333
    unsigned long long offset;
    size_t size;
    unsigned int flags;
1334
    int rv = -1;
1335 1336
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
R
Richard W.M. Jones 已提交
1337

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

1343
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1344
        goto cleanup;
R
Richard W.M. Jones 已提交
1345 1346 1347 1348 1349
    offset = args->offset;
    size = args->size;
    flags = args->flags;

    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1350 1351 1352
        virNetError(VIR_ERR_INTERNAL_ERROR,
                    "%s", _("size > maximum buffer size"));
        goto cleanup;
R
Richard W.M. Jones 已提交
1353 1354 1355
    }

    ret->buffer.buffer_len = size;
1356
    if (VIR_ALLOC_N(ret->buffer.buffer_val, size) < 0) {
1357 1358
        virReportOOMError();
        goto cleanup;
R
Richard W.M. Jones 已提交
1359 1360
    }

1361
    if (virDomainMemoryPeek(dom, offset, size,
1362
                            ret->buffer.buffer_val, flags) < 0)
1363
        goto cleanup;
R
Richard W.M. Jones 已提交
1364

1365 1366 1367 1368
    rv = 0;

cleanup:
    if (rv < 0) {
1369
        virNetMessageSaveError(rerr);
1370 1371 1372 1373 1374
        VIR_FREE(ret->buffer.buffer_val);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
R
Richard W.M. Jones 已提交
1375 1376
}

1377
static int
1378 1379
remoteDispatchDomainGetSecurityLabel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
1380
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
1381
                                     virNetMessageErrorPtr rerr,
1382 1383
                                     remote_domain_get_security_label_args *args,
                                     remote_domain_get_security_label_ret *ret)
1384
{
1385 1386
    virDomainPtr dom = NULL;
    virSecurityLabelPtr seclabel = NULL;
1387
    int rv = -1;
1388 1389
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1390

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

1396
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
        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();
1410
        goto cleanup;
1411 1412 1413
    }
    strcpy(ret->label.label_val, seclabel->label);
    ret->enforcing = seclabel->enforcing;
1414

1415 1416 1417 1418
    rv = 0;

cleanup:
    if (rv < 0)
1419
        virNetMessageSaveError(rerr);
1420 1421 1422
    if (dom)
        virDomainFree(dom);
    VIR_FREE(seclabel);
1423
    return rv;
1424 1425 1426
}

static int
1427 1428
remoteDispatchNodeGetSecurityModel(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1429
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1430
                                   virNetMessageErrorPtr rerr,
1431
                                   remote_node_get_security_model_ret *ret)
1432
{
1433
    virSecurityModel secmodel;
1434
    int rv = -1;
1435 1436
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1437

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

1443
    memset(&secmodel, 0, sizeof(secmodel));
1444
    if (virNodeGetSecurityModel(priv->conn, &secmodel) < 0)
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
        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();
1457
        goto cleanup;
1458 1459
    }
    strcpy(ret->doi.doi_val, secmodel.doi);
1460

1461 1462 1463 1464
    rv = 0;

cleanup:
    if (rv < 0)
1465
        virNetMessageSaveError(rerr);
1466
    return rv;
1467 1468
}

1469
static int
1470 1471
remoteDispatchDomainGetVcpuPinInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1472
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1473
                                   virNetMessageErrorPtr rerr,
E
Eric Blake 已提交
1474 1475
                                   remote_domain_get_vcpu_pin_info_args *args,
                                   remote_domain_get_vcpu_pin_info_ret *ret)
1476 1477 1478 1479 1480
{
    virDomainPtr dom = NULL;
    unsigned char *cpumaps = NULL;
    int num;
    int rv = -1;
1481 1482
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1483

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

1489
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
        goto cleanup;

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

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

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

E
Eric Blake 已提交
1508
    if ((num = virDomainGetVcpuPinInfo(dom,
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
                                       args->ncpumaps,
                                       cpumaps,
                                       args->maplen,
                                       args->flags)) < 0)
        goto cleanup;

    ret->num = num;
    /* Don't need to allocate/copy the cpumaps if we make the reasonable
     * assumption that unsigned char and char are the same size.
     * Note that remoteDispatchClientRequest will free.
     */
    ret->cpumaps.cpumaps_len = args->ncpumaps * args->maplen;
    ret->cpumaps.cpumaps_val = (char *) cpumaps;
    cpumaps = NULL;

    rv = 0;

cleanup:
    if (rv < 0)
1528
        virNetMessageSaveError(rerr);
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
    VIR_FREE(cpumaps);
    if (dom)
        virDomainFree(dom);
    return rv;

no_memory:
    virReportOOMError();
    goto cleanup;
}

1539
static int
1540 1541
remoteDispatchDomainGetVcpus(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
1542
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
1543
                             virNetMessageErrorPtr rerr,
1544 1545
                             remote_domain_get_vcpus_args *args,
                             remote_domain_get_vcpus_ret *ret)
1546
{
1547
    virDomainPtr dom = NULL;
1548 1549 1550
    virVcpuInfoPtr info = NULL;
    unsigned char *cpumaps = NULL;
    int info_len, i;
1551
    int rv = -1;
1552 1553
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1554

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

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

1563 1564
    if (args->maxinfo > REMOTE_VCPUINFO_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo > REMOTE_VCPUINFO_MAX"));
1565
        goto cleanup;
1566
    }
1567

1568 1569
    if (INT_MULTIPLY_OVERFLOW(args->maxinfo, args->maplen) ||
        args->maxinfo * args->maplen > REMOTE_CPUMAPS_MAX) {
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("maxinfo * maplen > REMOTE_CPUMAPS_MAX"));
        goto cleanup;
    }

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

    if ((info_len = virDomainGetVcpus(dom,
                                      info, args->maxinfo,
                                      cpumaps, args->maplen)) < 0)
        goto cleanup;

    /* Allocate the return buffer for info. */
    ret->info.info_len = info_len;
    if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
        goto no_memory;

    for (i = 0; i < info_len; ++i) {
        ret->info.info_val[i].number = info[i].number;
        ret->info.info_val[i].state = info[i].state;
        ret->info.info_val[i].cpu_time = info[i].cpuTime;
        ret->info.info_val[i].cpu = info[i].cpu;
    }

    /* Don't need to allocate/copy the cpumaps if we make the reasonable
     * assumption that unsigned char and char are the same size.
     * Note that remoteDispatchClientRequest will free.
     */
    ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
    ret->cpumaps.cpumaps_val = (char *) cpumaps;
    cpumaps = NULL;

    rv = 0;
1607 1608

cleanup:
1609
    if (rv < 0) {
1610
        virNetMessageSaveError(rerr);
1611 1612 1613 1614
        VIR_FREE(ret->info.info_val);
    }
    VIR_FREE(cpumaps);
    VIR_FREE(info);
1615 1616 1617
    if (dom)
        virDomainFree(dom);
    return rv;
1618 1619 1620 1621

no_memory:
    virReportOOMError();
    goto cleanup;
1622 1623 1624
}

static int
1625 1626
remoteDispatchDomainMigratePrepare(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
1627
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
1628
                                   virNetMessageErrorPtr rerr,
1629 1630
                                   remote_domain_migrate_prepare_args *args,
                                   remote_domain_migrate_prepare_ret *ret)
1631
{
1632 1633 1634 1635 1636
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1637
    int rv = -1;
1638 1639
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1640

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

1646 1647 1648 1649 1650 1651
    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();
1652
        goto cleanup;
1653
    }
1654

1655
    if (virDomainMigratePrepare(priv->conn, &cookie, &cookielen,
1656 1657
                                uri_in, uri_out,
                                args->flags, dname, args->resource) < 0)
1658
        goto cleanup;
1659

1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
    /* 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;
    }
1671

1672
    rv = 0;
1673

1674 1675
cleanup:
    if (rv < 0)
1676
        virNetMessageSaveError(rerr);
1677
    VIR_FREE(uri_out);
1678
    return rv;
1679 1680
}

1681
static int
1682 1683
remoteDispatchDomainMigratePrepare2(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
1684
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
1685
                                    virNetMessageErrorPtr rerr,
1686 1687
                                    remote_domain_migrate_prepare2_args *args,
                                    remote_domain_migrate_prepare2_ret *ret)
1688
{
1689 1690 1691 1692 1693
    char *cookie = NULL;
    int cookielen = 0;
    char *uri_in;
    char **uri_out;
    char *dname;
1694
    int rv = -1;
1695 1696
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1697

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

1703 1704
    uri_in = args->uri_in == NULL ? NULL : *args->uri_in;
    dname = args->dname == NULL ? NULL : *args->dname;
1705

1706 1707
    /* Wacky world of XDR ... */
    if (VIR_ALLOC(uri_out) < 0) {
1708 1709
        virReportOOMError();
        goto cleanup;
1710 1711
    }

1712
    if (virDomainMigratePrepare2(priv->conn, &cookie, &cookielen,
1713 1714 1715
                                 uri_in, uri_out,
                                 args->flags, dname, args->resource,
                                 args->dom_xml) < 0)
1716
        goto cleanup;
1717

1718 1719 1720 1721 1722 1723
    /* 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;
1724

1725 1726 1727 1728
    rv = 0;

cleanup:
    if (rv < 0)
1729
        virNetMessageSaveError(rerr);
1730
    return rv;
1731 1732
}

C
Chris Lalancette 已提交
1733
static int
1734 1735
remoteDispatchDomainGetMemoryParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
1736
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
1737 1738 1739
                                        virNetMessageErrorPtr rerr,
                                        remote_domain_get_memory_parameters_args *args,
                                        remote_domain_get_memory_parameters_ret *ret)
C
Chris Lalancette 已提交
1740
{
1741
    virDomainPtr dom = NULL;
1742
    virTypedParameterPtr params = NULL;
1743 1744
    int nparams = args->nparams;
    unsigned int flags;
1745
    int rv = -1;
1746 1747
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1748

1749
    if (!priv->conn) {
1750 1751
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
1752
    }
C
Chris Lalancette 已提交
1753

1754
    flags = args->flags;
C
Chris Lalancette 已提交
1755

1756 1757 1758 1759 1760
    if (nparams > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
1761 1762
        virReportOOMError();
        goto cleanup;
C
Chris Lalancette 已提交
1763 1764
    }

1765
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
1766
        goto cleanup;
C
Chris Lalancette 已提交
1767

1768
    if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0)
1769
        goto cleanup;
C
Chris Lalancette 已提交
1770

1771 1772 1773 1774 1775 1776
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
1777 1778
    }

1779
    if (remoteSerializeTypedParameters(params, nparams,
1780
                                       &ret->params.params_val,
1781 1782
                                       &ret->params.params_len,
                                       args->flags) < 0)
1783
        goto cleanup;
1784

1785
success:
1786 1787 1788
    rv = 0;

cleanup:
1789
    if (rv < 0)
1790
        virNetMessageSaveError(rerr);
1791 1792
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
1793 1794 1795
    if (dom)
        virDomainFree(dom);
    return rv;
1796 1797
}

1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
static int
remoteDispatchDomainGetNumaParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                      virNetMessageErrorPtr rerr,
                                      remote_domain_get_numa_parameters_args *args,
                                      remote_domain_get_numa_parameters_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    flags = args->flags;

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

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

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

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

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

success:
    rv = 0;

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

1863
static int
1864 1865
remoteDispatchDomainGetBlkioParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                       virNetServerClientPtr client ATTRIBUTE_UNUSED,
1866
                                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
1867 1868 1869
                                       virNetMessageErrorPtr rerr,
                                       remote_domain_get_blkio_parameters_args *args,
                                       remote_domain_get_blkio_parameters_ret *ret)
1870
{
1871
    virDomainPtr dom = NULL;
1872
    virTypedParameterPtr params = NULL;
1873
    int nparams = args->nparams;
1874
    unsigned int flags;
1875
    int rv = -1;
1876 1877
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1878

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

1884 1885
    flags = args->flags;

1886
    if (nparams > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX) {
1887 1888
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("nparams too large"));
        goto cleanup;
1889 1890
    }
    if (VIR_ALLOC_N(params, nparams) < 0) {
1891 1892
        virReportOOMError();
        goto cleanup;
1893 1894
    }

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

1898
    if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0)
1899
        goto cleanup;
1900

1901 1902 1903 1904 1905 1906 1907 1908
    /* In this case, we need to send back the number of parameters
     * supported
     */
    if (args->nparams == 0) {
        ret->nparams = nparams;
        goto success;
    }

1909
    if (remoteSerializeTypedParameters(params, nparams,
1910
                                       &ret->params.params_val,
1911 1912
                                       &ret->params.params_len,
                                       args->flags) < 0)
1913
        goto cleanup;
1914

1915 1916
success:
    rv = 0;
1917

1918
cleanup:
1919
    if (rv < 0)
1920
        virNetMessageSaveError(rerr);
1921
    virTypedParameterArrayClear(params, nparams);
1922
    VIR_FREE(params);
1923 1924 1925
    if (dom)
        virDomainFree(dom);
    return rv;
1926 1927
}

1928
static int
1929 1930
remoteDispatchNodeGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                              virNetServerClientPtr client ATTRIBUTE_UNUSED,
1931
                              virNetMessagePtr msg ATTRIBUTE_UNUSED,
1932 1933 1934
                              virNetMessageErrorPtr rerr,
                              remote_node_get_cpu_stats_args *args,
                              remote_node_get_cpu_stats_ret *ret)
1935
{
1936
    virNodeCPUStatsPtr params = NULL;
1937 1938 1939 1940 1941
    int i;
    int cpuNum = args->cpuNum;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
1942 1943
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
1944

1945
    if (!priv->conn) {
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    flags = args->flags;

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

1961
    if (virNodeGetCPUStats(priv->conn, cpuNum, params, &nparams, flags) < 0)
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
        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;

1989 1990
cleanup:
    if (rv < 0) {
1991
        virNetMessageSaveError(rerr);
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
        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
2007 2008
remoteDispatchNodeGetMemoryStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
2009
                                 virNetMessagePtr msg ATTRIBUTE_UNUSED,
2010 2011 2012
                                 virNetMessageErrorPtr rerr,
                                 remote_node_get_memory_stats_args *args,
                                 remote_node_get_memory_stats_ret *ret)
2013
{
2014
    virNodeMemoryStatsPtr params = NULL;
2015 2016 2017 2018 2019
    int i;
    int cellNum = args->cellNum;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
2020 2021
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2022

2023
    if (!priv->conn) {
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

    flags = args->flags;

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

2039
    if (virNodeGetMemoryStats(priv->conn, cellNum, params, &nparams, flags) < 0)
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
        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;

2067 2068
cleanup:
    if (rv < 0) {
2069
        virNetMessageSaveError(rerr);
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
        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;
}

2084 2085 2086
static int
remoteDispatchDomainGetBlockJobInfo(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
2087
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_get_block_job_info_args *args,
                                    remote_domain_get_block_job_info_ret *ret)
{
    virDomainPtr dom = NULL;
    virDomainBlockJobInfo tmp;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    rv = virDomainGetBlockJobInfo(dom, args->path, &tmp, args->flags);
    if (rv <= 0)
        goto cleanup;

    ret->type = tmp.type;
    ret->bandwidth = tmp.bandwidth;
    ret->cur = tmp.cur;
    ret->end = tmp.end;
    ret->found = 1;
    rv = 0;

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

2125 2126 2127 2128 2129 2130 2131 2132 2133 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 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
static int
remoteDispatchDomainGetBlockIoTune(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                   virNetMessagePtr hdr ATTRIBUTE_UNUSED,
                                   virNetMessageErrorPtr rerr,
                                   remote_domain_get_block_io_tune_args *args,
                                   remote_domain_get_block_io_tune_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    virTypedParameterPtr params = NULL;
    int nparams = args->nparams;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

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

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

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

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

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

success:
    rv = 0;

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

D
Daniel Veillard 已提交
2190 2191
/*-------------------------------------------------------------*/

2192
static int
2193 2194
remoteDispatchAuthList(virNetServerPtr server ATTRIBUTE_UNUSED,
                       virNetServerClientPtr client,
2195
                       virNetMessagePtr msg ATTRIBUTE_UNUSED,
2196
                       virNetMessageErrorPtr rerr,
2197
                       remote_auth_list_ret *ret)
2198
{
2199
    int rv = -1;
2200 2201
    int auth = virNetServerClientGetAuth(client);
    uid_t callerUid;
2202
    gid_t callerGid;
2203 2204 2205 2206 2207 2208 2209
    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) {
2210 2211
        if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
                                              &callerPid) < 0) {
2212 2213 2214 2215
            /* Don't do anything on error - it'll be validated at next
             * phase of auth anyway */
            virResetLastError();
        } else if (callerUid == 0) {
2216 2217
            char *ident;
            if (virAsprintf(&ident, "pid:%lld,uid:%d",
J
Jim Fehlig 已提交
2218 2219 2220
                            (long long) callerPid, callerUid) < 0) {
                virReportOOMError();
                goto cleanup;
2221
            }
J
Jim Fehlig 已提交
2222 2223 2224 2225 2226 2227
            VIR_INFO("Bypass polkit auth for privileged client %s", ident);
            if (virNetServerClientSetIdentity(client, ident) < 0)
                virResetLastError();
            else
                auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
            VIR_FREE(ident);
2228 2229
        }
    }
2230

2231
    ret->types.types_len = 1;
2232
    if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
2233 2234
        virReportOOMError();
        goto cleanup;
2235
    }
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249

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

2251 2252 2253 2254
    rv = 0;

cleanup:
    if (rv < 0)
2255
        virNetMessageSaveError(rerr);
2256
    return rv;
2257 2258 2259
}


2260
#ifdef HAVE_SASL
2261 2262
/*
 * Initializes the SASL session in prepare for authentication
2263
 * and gives the client a list of allowed mechanisms to choose
2264 2265
 */
static int
2266 2267
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2268
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2269
                           virNetMessageErrorPtr rerr,
2270
                           remote_auth_sasl_init_ret *ret)
2271
{
2272 2273 2274
    virNetSASLSessionPtr sasl = NULL;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2275

2276
    virMutexLock(&priv->lock);
2277

2278 2279 2280
    VIR_DEBUG("Initialize SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl != NULL) {
2281
        VIR_ERROR(_("client tried invalid SASL init request"));
2282
        goto authfail;
2283 2284
    }

2285 2286 2287 2288 2289
    sasl = virNetSASLSessionNewServer(saslCtxt,
                                      "libvirt",
                                      virNetServerClientLocalAddrString(client),
                                      virNetServerClientRemoteAddrString(client));
    if (!sasl)
2290
        goto authfail;
2291

2292
    /* Inform SASL that we've got an external SSF layer from TLS */
2293 2294 2295 2296
    if (virNetServerClientHasTLSSession(client)) {
        int ssf;

        if ((ssf = virNetServerClientGetTLSKeySize(client)) < 0)
2297
            goto authfail;
2298 2299 2300 2301 2302

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

        VIR_DEBUG("Setting external SSF %d", ssf);
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2303
            goto authfail;
2304 2305
    }

2306
    if (virNetServerClientIsSecure(client))
2307
        /* If we've got TLS or UNIX domain sock, we don't care about SSF */
2308 2309
        virNetSASLSessionSecProps(sasl, 0, 0, true);
    else
2310
        /* Plain TCP, better get an SSF layer */
2311 2312 2313 2314
        virNetSASLSessionSecProps(sasl,
                                  56,  /* Good enough to require kerberos */
                                  100000,  /* Arbitrary big number */
                                  false); /* No anonymous */
2315

2316
    if (!(ret->mechlist = virNetSASLSessionListMechanisms(sasl)))
2317
        goto authfail;
2318
    VIR_DEBUG("Available mechanisms for client: '%s'", ret->mechlist);
2319

2320 2321
    priv->sasl = sasl;
    virMutexUnlock(&priv->lock);
2322
    return 0;
2323 2324

authfail:
2325 2326 2327 2328
    virResetLastError();
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
2329 2330 2331
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2332 2333
    virNetSASLSessionFree(sasl);
    virMutexUnlock(&priv->lock);
2334
    return -1;
2335 2336
}

2337
/*
2338 2339
 * Returns 0 if ok, -1 on error, -2 if rejected
 */
2340
static int
2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
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;
        }
2357
    }
2358 2359

    if (!(identity = virNetSASLSessionGetIdentity(priv->sasl)))
2360
        return -2;
2361

2362 2363
    if (!virNetSASLContextCheckIdentity(saslCtxt, identity))
        return -2;
2364

2365 2366
    if (virNetServerClientSetIdentity(client, identity) < 0)
        goto error;
2367

2368
    virNetServerClientSetSASLSession(client, priv->sasl);
2369

2370
    VIR_DEBUG("Authentication successful %d", virNetServerClientGetFD(client));
2371

2372 2373 2374
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2375

2376 2377
    virNetSASLSessionFree(priv->sasl);
    priv->sasl = NULL;
2378

2379
    return 0;
2380

2381 2382 2383
error:
    return -1;
}
2384

2385 2386 2387 2388
/*
 * This starts the SASL authentication negotiation.
 */
static int
2389 2390
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client,
2391
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2392
                            virNetMessageErrorPtr rerr,
2393 2394
                            remote_auth_sasl_start_args *args,
                            remote_auth_sasl_start_ret *ret)
2395 2396
{
    const char *serverout;
2397
    size_t serveroutlen;
2398
    int err;
2399 2400 2401
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2402
    const char *identity;
2403

2404
    virMutexLock(&priv->lock);
2405

2406 2407 2408
    VIR_DEBUG("Start SASL auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_SASL ||
        priv->sasl == NULL) {
2409
        VIR_ERROR(_("client tried invalid SASL start request"));
2410
        goto authfail;
2411 2412
    }

2413 2414
    VIR_DEBUG("Using SASL mechanism %s. Data %d bytes, nil: %d",
              args->mech, args->data.data_len, args->nil);
2415 2416 2417 2418 2419 2420 2421 2422 2423
    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)
2424
        goto authfail;
2425

2426
    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2427
        VIR_ERROR(_("sasl start reply data too long %d"), (int)serveroutlen);
2428
        goto authfail;
2429 2430 2431 2432
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2433 2434
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2435 2436 2437 2438 2439 2440 2441
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2442
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2443
    if (err == VIR_NET_SASL_CONTINUE) {
2444 2445
        ret->complete = 0;
    } else {
2446
        /* Check username whitelist ACL */
2447
        if ((err = remoteSASLFinish(client)) < 0) {
2448 2449 2450 2451 2452
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2453

2454 2455 2456
        ret->complete = 1;
    }

2457
    virMutexUnlock(&priv->lock);
2458
    return 0;
2459 2460

authfail:
2461 2462 2463
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2464 2465 2466
    goto error;

authdeny:
2467
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2468 2469 2470
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2471 2472
    goto error;

2473
error:
2474 2475 2476 2477 2478 2479 2480 2481
    virNetSASLSessionFree(priv->sasl);
    priv->sasl = NULL;
    virResetLastError();
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2482
    return -1;
2483 2484 2485 2486
}


static int
2487 2488
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client,
2489
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2490
                           virNetMessageErrorPtr rerr,
2491 2492
                           remote_auth_sasl_step_args *args,
                           remote_auth_sasl_step_ret *ret)
2493 2494
{
    const char *serverout;
2495
    size_t serveroutlen;
2496
    int err;
2497 2498 2499
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2500
    const char *identity;
2501

2502 2503 2504 2505 2506
    virMutexLock(&priv->lock);

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

2511
    VIR_DEBUG("Step using SASL Data %d bytes, nil: %d",
2512
              args->data.data_len, args->nil);
2513 2514 2515 2516 2517 2518 2519 2520
    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)
2521
        goto authfail;
2522 2523

    if (serveroutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2524
        VIR_ERROR(_("sasl step reply data too long %d"),
2525
                  (int)serveroutlen);
2526
        goto authfail;
2527 2528 2529 2530
    }

    /* NB, distinction of NULL vs "" is *critical* in SASL */
    if (serverout) {
2531 2532
        if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0)
            goto authfail;
2533 2534 2535 2536 2537 2538 2539
        memcpy(ret->data.data_val, serverout, serveroutlen);
    } else {
        ret->data.data_val = NULL;
    }
    ret->nil = serverout ? 0 : 1;
    ret->data.data_len = serveroutlen;

2540
    VIR_DEBUG("SASL return data %d bytes, nil; %d", ret->data.data_len, ret->nil);
2541
    if (err == VIR_NET_SASL_CONTINUE) {
2542 2543
        ret->complete = 0;
    } else {
2544
        /* Check username whitelist ACL */
2545
        if ((err = remoteSASLFinish(client)) < 0) {
2546 2547 2548 2549 2550
            if (err == -2)
                goto authdeny;
            else
                goto authfail;
        }
2551

2552 2553 2554
        ret->complete = 1;
    }

2555
    virMutexUnlock(&priv->lock);
2556
    return 0;
2557 2558

authfail:
2559 2560 2561
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_SASL);
2562 2563 2564
    goto error;

authdeny:
2565
    identity = virNetSASLSessionGetIdentity(priv->sasl);
2566 2567 2568
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_SASL, identity);
2569 2570
    goto error;

2571
error:
2572 2573 2574 2575 2576 2577 2578 2579
    virNetSASLSessionFree(priv->sasl);
    priv->sasl = NULL;
    virResetLastError();
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    if (rv < 0)
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2580 2581
    return -1;
}
2582 2583 2584 2585
#else
static int
remoteDispatchAuthSaslInit(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2586
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_init_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStart(virNetServerPtr server ATTRIBUTE_UNUSED,
                            virNetServerClientPtr client ATTRIBUTE_UNUSED,
2599
                            virNetMessagePtr msg ATTRIBUTE_UNUSED,
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
                            virNetMessageErrorPtr rerr,
                            remote_auth_sasl_start_args *args ATTRIBUTE_UNUSED,
                            remote_auth_sasl_start_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
    return -1;
}
static int
remoteDispatchAuthSaslStep(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
2613
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
                           virNetMessageErrorPtr rerr,
                           remote_auth_sasl_step_args *args ATTRIBUTE_UNUSED,
                           remote_auth_sasl_step_ret *ret ATTRIBUTE_UNUSED)
{
    VIR_WARN("Client tried unsupported SASL auth");
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
    return -1;
}
#endif
2625 2626 2627



2628 2629
#if HAVE_POLKIT1
static int
2630 2631
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
                         virNetServerClientPtr client,
2632
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2633
                         virNetMessageErrorPtr rerr,
2634
                         remote_auth_polkit_ret *ret)
2635
{
2636
    pid_t callerPid = -1;
2637
    gid_t callerGid = -1;
2638
    uid_t callerUid = -1;
2639 2640
    const char *action;
    int status = -1;
2641
    char *ident = NULL;
2642
    bool authdismissed = 0;
2643
    char *pkout = NULL;
2644 2645
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2646
    virCommandPtr cmd = NULL;
2647

2648 2649
    virMutexLock(&priv->lock);
    action = virNetServerClientGetReadonly(client) ?
2650 2651 2652
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";

2653
    cmd = virCommandNewArgList(PKCHECK_PATH, "--action-id", action, NULL);
2654
    virCommandSetOutputBuffer(cmd, &pkout);
2655
    virCommandSetErrorBuffer(cmd, &pkout);
2656

2657 2658
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2659
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2660 2661 2662
        goto authfail;
    }

2663 2664
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
                                          &callerPid) < 0) {
2665 2666 2667
        goto authfail;
    }

2668 2669
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2670

2671
    virCommandAddArg(cmd, "--process");
2672
    virCommandAddArgFormat(cmd, "%lld", (long long) callerPid);
2673
    virCommandAddArg(cmd, "--allow-user-interaction");
2674

2675 2676
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
                    (long long) callerPid, callerUid) < 0) {
2677
        virReportOOMError();
2678 2679 2680
        goto authfail;
    }

2681
    if (virCommandRun(cmd, &status) < 0)
2682
        goto authfail;
2683

2684
    authdismissed = (pkout && strstr(pkout, "dismissed=true"));
2685
    if (status != 0) {
2686
        char *tmp = virCommandTranslateStatus(status);
2687 2688
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d: %s"),
                  action, (long long) callerPid, callerUid, NULLSTR(tmp));
2689
        VIR_FREE(tmp);
2690
        goto authdeny;
2691
    }
2692 2693 2694
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2695 2696
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d",
             action, (long long) callerPid, callerUid);
2697 2698
    ret->complete = 1;

2699 2700
    virNetServerClientSetIdentity(client, ident);
    virMutexUnlock(&priv->lock);
2701
    virCommandFree(cmd);
E
Eric Blake 已提交
2702
    VIR_FREE(pkout);
2703
    VIR_FREE(ident);
2704

2705 2706
    return 0;

2707
error:
2708 2709
    virCommandFree(cmd);
    VIR_FREE(ident);
2710
    virResetLastError();
2711

2712 2713 2714 2715 2716
    if (authdismissed) {
        virNetError(VIR_ERR_AUTH_CANCELLED, "%s",
                    _("authentication cancelled by user"));
    } else {
        virNetError(VIR_ERR_AUTH_FAILED, "%s",
2717
                    pkout && *pkout ? pkout : _("authentication failed"));
2718
    }
2719 2720

    VIR_FREE(pkout);
2721 2722 2723 2724
    virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
    return -1;

2725
authfail:
2726 2727 2728
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
2729 2730 2731
    goto error;

authdeny:
2732 2733
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
2734
          client, REMOTE_AUTH_POLKIT, ident);
2735
    goto error;
2736 2737
}
#elif HAVE_POLKIT0
2738
static int
2739
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
2740
                         virNetServerClientPtr client,
2741
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2742
                         virNetMessageErrorPtr rerr,
2743
                         remote_auth_polkit_ret *ret)
2744 2745
{
    pid_t callerPid;
2746
    gid_t callerGid;
2747
    uid_t callerUid;
2748 2749 2750 2751 2752 2753
    PolKitCaller *pkcaller = NULL;
    PolKitAction *pkaction = NULL;
    PolKitContext *pkcontext = NULL;
    PolKitError *pkerr = NULL;
    PolKitResult pkresult;
    DBusError err;
2754
    const char *action;
2755
    char *ident = NULL;
2756 2757
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2758
    DBusConnection *sysbus;
2759

J
Jim Fehlig 已提交
2760
    virMutexLock(&priv->lock);
2761

J
Jim Fehlig 已提交
2762
    action = virNetServerClientGetReadonly(client) ?
2763 2764
        "org.libvirt.unix.monitor" :
        "org.libvirt.unix.manage";
2765

2766
    VIR_DEBUG("Start PolicyKit auth %d", virNetServerClientGetFD(client));
J
Jim Fehlig 已提交
2767
    if (virNetServerClientGetAuth(client) != VIR_NET_SERVER_SERVICE_AUTH_POLKIT) {
2768
        VIR_ERROR(_("client tried invalid PolicyKit init request"));
2769
        goto authfail;
2770 2771
    }

2772 2773
    if (virNetServerClientGetUNIXIdentity(client, &callerUid, &callerGid,
                                          &callerPid) < 0) {
2774
        VIR_ERROR(_("cannot get peer socket identity"));
2775
        goto authfail;
2776 2777
    }

2778 2779 2780
    if (virAsprintf(&ident, "pid:%lld,uid:%d",
                    (long long) callerPid, callerUid) < 0) {
        virReportOOMError();
2781 2782 2783
        goto authfail;
    }

2784 2785 2786
    if (!(sysbus = virDBusGetSystemBus()))
        goto authfail;

2787 2788
    VIR_INFO("Checking PID %lld running as %d",
             (long long) callerPid, callerUid);
2789
    dbus_error_init(&err);
2790
    if (!(pkcaller = polkit_caller_new_from_pid(sysbus,
2791
                                                callerPid, &err))) {
2792
        VIR_ERROR(_("Failed to lookup policy kit caller: %s"), err.message);
2793
        dbus_error_free(&err);
2794
        goto authfail;
2795
    }
2796

2797
    if (!(pkaction = polkit_action_new())) {
2798
        char ebuf[1024];
2799
        VIR_ERROR(_("Failed to create polkit action %s"),
2800
                  virStrerror(errno, ebuf, sizeof(ebuf)));
2801
        polkit_caller_unref(pkcaller);
2802
        goto authfail;
2803 2804 2805 2806 2807
    }
    polkit_action_set_action_id(pkaction, action);

    if (!(pkcontext = polkit_context_new()) ||
        !polkit_context_init(pkcontext, &pkerr)) {
2808
        char ebuf[1024];
2809
        VIR_ERROR(_("Failed to create polkit context %s"),
2810
                  (pkerr ? polkit_error_get_error_message(pkerr)
2811
                   : virStrerror(errno, ebuf, sizeof(ebuf))));
2812 2813 2814 2815 2816
        if (pkerr)
            polkit_error_free(pkerr);
        polkit_caller_unref(pkcaller);
        polkit_action_unref(pkaction);
        dbus_error_free(&err);
2817
        goto authfail;
2818
    }
2819

2820
# if HAVE_POLKIT_CONTEXT_IS_CALLER_AUTHORIZED
2821 2822 2823 2824 2825 2826
    pkresult = polkit_context_is_caller_authorized(pkcontext,
                                                   pkaction,
                                                   pkcaller,
                                                   0,
                                                   &pkerr);
    if (pkerr && polkit_error_is_set(pkerr)) {
2827 2828 2829
        VIR_ERROR(_("Policy kit failed to check authorization %d %s"),
                  polkit_error_get_error_code(pkerr),
                  polkit_error_get_error_message(pkerr));
2830
        goto authfail;
2831
    }
2832
# else
2833 2834 2835
    pkresult = polkit_context_can_caller_do_action(pkcontext,
                                                   pkaction,
                                                   pkcaller);
2836
# endif
2837 2838 2839 2840
    polkit_context_unref(pkcontext);
    polkit_caller_unref(pkcaller);
    polkit_action_unref(pkaction);
    if (pkresult != POLKIT_RESULT_YES) {
2841 2842
        VIR_ERROR(_("Policy kit denied action %s from pid %lld, uid %d, result: %s"),
                  action, (long long) callerPid, callerUid,
2843
                  polkit_result_to_string_representation(pkresult));
2844
        goto authdeny;
2845
    }
2846 2847 2848
    PROBE(RPC_SERVER_CLIENT_AUTH_ALLOW,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2849 2850
    VIR_INFO("Policy allowed action %s from pid %lld, uid %d, result %s",
             action, (long long) callerPid, callerUid,
2851 2852
             polkit_result_to_string_representation(pkresult));
    ret->complete = 1;
J
Jim Fehlig 已提交
2853
    virNetServerClientSetIdentity(client, ident);
2854

J
Jim Fehlig 已提交
2855
    virMutexUnlock(&priv->lock);
2856
    VIR_FREE(ident);
2857
    return 0;
2858

2859
error:
2860
    VIR_FREE(ident);
2861 2862 2863 2864
    virResetLastError();
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
J
Jim Fehlig 已提交
2865
    virMutexUnlock(&priv->lock);
2866 2867
    return -1;

2868
authfail:
2869 2870 2871
    PROBE(RPC_SERVER_CLIENT_AUTH_FAIL,
          "client=%p auth=%d",
          client, REMOTE_AUTH_POLKIT);
2872
    goto error;
2873

2874
authdeny:
2875 2876 2877
    PROBE(RPC_SERVER_CLIENT_AUTH_DENY,
          "client=%p auth=%d identity=%s",
          client, REMOTE_AUTH_POLKIT, ident);
2878
    goto error;
2879 2880
}

2881
#else /* !HAVE_POLKIT0 & !HAVE_POLKIT1*/
2882 2883

static int
2884
remoteDispatchAuthPolkit(virNetServerPtr server ATTRIBUTE_UNUSED,
J
Jiri Denemark 已提交
2885
                         virNetServerClientPtr client ATTRIBUTE_UNUSED,
2886
                         virNetMessagePtr msg ATTRIBUTE_UNUSED,
2887
                         virNetMessageErrorPtr rerr,
2888
                         remote_auth_polkit_ret *ret ATTRIBUTE_UNUSED)
2889
{
2890
    VIR_ERROR(_("client tried unsupported PolicyKit init request"));
2891 2892 2893
    virNetError(VIR_ERR_AUTH_FAILED, "%s",
                _("authentication failed"));
    virNetMessageSaveError(rerr);
2894 2895 2896
    return -1;
}
#endif /* HAVE_POLKIT1 */
2897 2898


2899 2900 2901
/***************************************************************
 *     NODE INFO APIS
 **************************************************************/
2902

2903
static int
2904 2905
remoteDispatchNodeDeviceGetParent(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
2906
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
2907
                                  virNetMessageErrorPtr rerr,
2908 2909
                                  remote_node_device_get_parent_args *args,
                                  remote_node_device_get_parent_ret *ret)
2910
{
2911 2912
    virNodeDevicePtr dev = NULL;
    const char *parent = NULL;
2913
    int rv = -1;
2914 2915
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
2916

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

2922
    if (!(dev = virNodeDeviceLookupByName(priv->conn, args->name)))
2923 2924
        goto cleanup;

2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943
    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;
    }

2944 2945 2946 2947
    rv = 0;

cleanup:
    if (rv < 0)
2948
        virNetMessageSaveError(rerr);
2949 2950
    if (dev)
        virNodeDeviceFree(dev);
2951
    return rv;
2952 2953
}

2954 2955 2956 2957 2958

/***************************
 * Register / deregister events
 ***************************/
static int
2959 2960
remoteDispatchDomainEventsRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
2961
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
2962
                                   virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
2963
                                   remote_domain_events_register_ret *ret ATTRIBUTE_UNUSED)
O
Osier Yang 已提交
2964
{
2965
    int callbackID;
2966
    int rv = -1;
2967 2968
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
O
Osier Yang 已提交
2969

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

2975 2976 2977
    virMutexLock(&priv->lock);

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

2982
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
2983 2984 2985 2986
                                                       NULL,
                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                       VIR_DOMAIN_EVENT_CALLBACK(remoteRelayDomainEventLifecycle),
                                                       client, NULL)) < 0)
2987
        goto cleanup;
O
Osier Yang 已提交
2988

2989
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = callbackID;
2990

2991 2992 2993 2994
    rv = 0;

cleanup:
    if (rv < 0)
2995 2996
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
2997
    return rv;
O
Osier Yang 已提交
2998 2999
}

3000
static int
3001 3002
remoteDispatchDomainEventsDeregister(virNetServerPtr server ATTRIBUTE_UNUSED,
                                     virNetServerClientPtr client ATTRIBUTE_UNUSED,
3003
                                     virNetMessagePtr msg ATTRIBUTE_UNUSED,
3004
                                     virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
3005
                                     remote_domain_events_deregister_ret *ret ATTRIBUTE_UNUSED)
3006
{
3007
    int rv = -1;
3008 3009
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3010

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

3016 3017 3018
    virMutexLock(&priv->lock);

    if (priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] < 0) {
3019
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), VIR_DOMAIN_EVENT_ID_LIFECYCLE);
3020
        goto cleanup;
3021
    }
3022

3023 3024
    if (virConnectDomainEventDeregisterAny(priv->conn,
                                           priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE]) < 0)
3025
        goto cleanup;
3026

3027 3028
    priv->domainEventCallbackID[VIR_DOMAIN_EVENT_ID_LIFECYCLE] = -1;

3029 3030 3031
    rv = 0;

cleanup:
3032
    if (rv < 0)
3033 3034
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3035 3036 3037 3038
    return rv;
}

static void
3039 3040
remoteDispatchDomainEventSend(virNetServerClientPtr client,
                              virNetServerProgramPtr program,
3041 3042 3043 3044
                              int procnr,
                              xdrproc_t proc,
                              void *data)
{
3045
    virNetMessagePtr msg;
3046

3047
    if (!(msg = virNetMessageNew(false)))
3048
        goto cleanup;
3049

3050 3051 3052 3053 3054 3055
    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;
3056

3057
    if (virNetMessageEncodeHeader(msg) < 0)
3058 3059
        goto cleanup;

3060 3061
    if (virNetMessageEncodePayload(msg, proc, data) < 0)
        goto cleanup;
3062

3063 3064
    VIR_DEBUG("Queue event %d %zu", procnr, msg->bufferLength);
    virNetServerClientSendMessage(client, msg);
3065

3066
    xdr_free(proc, data);
3067 3068 3069
    return;

cleanup:
3070
    virNetMessageFree(msg);
3071
    xdr_free(proc, data);
3072 3073
}

3074
static int
3075 3076
remoteDispatchSecretGetValue(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3077
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3078
                             virNetMessageErrorPtr rerr,
3079 3080
                             remote_secret_get_value_args *args,
                             remote_secret_get_value_ret *ret)
3081
{
3082 3083 3084
    virSecretPtr secret = NULL;
    size_t value_size;
    unsigned char *value;
3085
    int rv = -1;
3086 3087
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3088

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

3094
    if (!(secret = get_nonnull_secret(priv->conn, args->secret)))
3095
        goto cleanup;
3096

3097
    if (!(value = virSecretGetValue(secret, &value_size, args->flags)))
3098
        goto cleanup;
3099

3100 3101 3102
    ret->value.value_len = value_size;
    ret->value.value_val = (char *)value;

3103 3104 3105 3106
    rv = 0;

cleanup:
    if (rv < 0)
3107
        virNetMessageSaveError(rerr);
3108 3109
    if (secret)
        virSecretFree(secret);
3110
    return rv;
3111 3112
}

3113
static int
3114 3115
remoteDispatchDomainGetState(virNetServerPtr server ATTRIBUTE_UNUSED,
                             virNetServerClientPtr client ATTRIBUTE_UNUSED,
3116
                             virNetMessagePtr msg ATTRIBUTE_UNUSED,
3117
                             virNetMessageErrorPtr rerr,
3118 3119 3120 3121 3122
                             remote_domain_get_state_args *args,
                             remote_domain_get_state_ret *ret)
{
    virDomainPtr dom = NULL;
    int rv = -1;
3123 3124
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3125

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

3131
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3132 3133 3134 3135 3136 3137 3138 3139 3140
        goto cleanup;

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

    rv = 0;

cleanup:
    if (rv < 0)
3141
        virNetMessageSaveError(rerr);
3142 3143 3144 3145 3146
    if (dom)
        virDomainFree(dom);
    return rv;
}

3147
static int
3148 3149
remoteDispatchDomainEventsRegisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                      virNetServerClientPtr client ATTRIBUTE_UNUSED,
3150
                                      virNetMessagePtr msg ATTRIBUTE_UNUSED,
3151 3152
                                      virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                      remote_domain_events_register_any_args *args)
3153 3154
{
    int callbackID;
3155
    int rv = -1;
3156 3157
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3158

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

3164 3165
    virMutexLock(&priv->lock);

3166 3167
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3168 3169
        virNetError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
        goto cleanup;
3170 3171
    }

3172
    if (priv->domainEventCallbackID[args->eventID] != -1)  {
3173 3174
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d already registered"), args->eventID);
        goto cleanup;
3175 3176
    }

3177
    if ((callbackID = virConnectDomainEventRegisterAny(priv->conn,
3178 3179 3180
                                                       NULL,
                                                       args->eventID,
                                                       domainEventCallbacks[args->eventID],
3181
                                                       client, NULL)) < 0)
3182
        goto cleanup;
3183

3184
    priv->domainEventCallbackID[args->eventID] = callbackID;
3185

3186 3187 3188 3189
    rv = 0;

cleanup:
    if (rv < 0)
3190 3191
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3192
    return rv;
3193 3194 3195 3196
}


static int
3197 3198
remoteDispatchDomainEventsDeregisterAny(virNetServerPtr server ATTRIBUTE_UNUSED,
                                        virNetServerClientPtr client ATTRIBUTE_UNUSED,
3199
                                        virNetMessagePtr msg ATTRIBUTE_UNUSED,
3200 3201
                                        virNetMessageErrorPtr rerr ATTRIBUTE_UNUSED,
                                        remote_domain_events_deregister_any_args *args)
3202 3203
{
    int callbackID = -1;
3204
    int rv = -1;
3205 3206
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3207

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

3213 3214
    virMutexLock(&priv->lock);

3215 3216
    if (args->eventID >= VIR_DOMAIN_EVENT_ID_LAST ||
        args->eventID < 0) {
3217 3218
        virNetError(VIR_ERR_INTERNAL_ERROR, _("unsupported event ID %d"), args->eventID);
        goto cleanup;
3219 3220
    }

3221 3222
    callbackID = priv->domainEventCallbackID[args->eventID];
    if (callbackID < 0) {
3223 3224
        virNetError(VIR_ERR_INTERNAL_ERROR, _("domain event %d not registered"), args->eventID);
        goto cleanup;
3225 3226
    }

3227
    if (virConnectDomainEventDeregisterAny(priv->conn, callbackID) < 0)
3228
        goto cleanup;
3229

3230 3231
    priv->domainEventCallbackID[args->eventID] = -1;

3232 3233 3234 3235
    rv = 0;

cleanup:
    if (rv < 0)
3236 3237
        virNetMessageSaveError(rerr);
    virMutexUnlock(&priv->lock);
3238
    return rv;
3239 3240
}

C
Chris Lalancette 已提交
3241
static int
3242 3243
qemuDispatchMonitorCommand(virNetServerPtr server ATTRIBUTE_UNUSED,
                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
3244
                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
3245
                           virNetMessageErrorPtr rerr,
3246 3247
                           qemu_monitor_command_args *args,
                           qemu_monitor_command_ret *ret)
C
Chris Lalancette 已提交
3248
{
3249
    virDomainPtr dom = NULL;
3250
    int rv = -1;
3251 3252
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
C
Chris Lalancette 已提交
3253

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

3259
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3260
        goto cleanup;
C
Chris Lalancette 已提交
3261

3262
    if (virDomainQemuMonitorCommand(dom, args->cmd, &ret->result,
3263
                                    args->flags) < 0)
3264
        goto cleanup;
C
Chris Lalancette 已提交
3265

3266
    rv = 0;
C
Chris Lalancette 已提交
3267

3268 3269
cleanup:
    if (rv < 0)
3270
        virNetMessageSaveError(rerr);
3271 3272
    if (dom)
        virDomainFree(dom);
3273
    return rv;
C
Chris Lalancette 已提交
3274 3275
}

3276

3277
static int
3278 3279
remoteDispatchDomainMigrateBegin3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                  virNetServerClientPtr client ATTRIBUTE_UNUSED,
3280
                                  virNetMessagePtr msg ATTRIBUTE_UNUSED,
3281
                                  virNetMessageErrorPtr rerr,
3282 3283 3284 3285 3286 3287
                                  remote_domain_migrate_begin3_args *args,
                                  remote_domain_migrate_begin3_ret *ret)
{
    char *xml = NULL;
    virDomainPtr dom = NULL;
    char *dname;
3288
    char *xmlin;
3289 3290 3291
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3292 3293
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3294

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

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

3303
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3304 3305
    dname = args->dname == NULL ? NULL : *args->dname;

3306
    if (!(xml = virDomainMigrateBegin3(dom, xmlin,
3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321
                                       &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)
3322
        virNetMessageSaveError(rerr);
3323 3324 3325 3326 3327 3328 3329
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3330 3331
remoteDispatchDomainMigratePrepare3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3332
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3333
                                    virNetMessageErrorPtr rerr,
3334 3335 3336 3337 3338 3339 3340 3341 3342
                                    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;
3343 3344
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3345

3346
    if (!priv->conn) {
3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

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

3360
    if (virDomainMigratePrepare3(priv->conn,
3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379
                                 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) {
3380
        virNetMessageSaveError(rerr);
3381 3382 3383 3384 3385
        VIR_FREE(uri_out);
    }
    return rv;
}

3386

3387
static int
3388 3389
remoteDispatchDomainMigratePerform3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3390
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3391
                                    virNetMessageErrorPtr rerr,
3392 3393 3394 3395
                                    remote_domain_migrate_perform3_args *args,
                                    remote_domain_migrate_perform3_ret *ret)
{
    virDomainPtr dom = NULL;
3396
    char *xmlin;
3397
    char *dname;
3398 3399
    char *uri;
    char *dconnuri;
3400 3401 3402
    char *cookieout = NULL;
    int cookieoutlen = 0;
    int rv = -1;
3403 3404
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3405

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

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

3414
    xmlin = args->xmlin == NULL ? NULL : *args->xmlin;
3415
    dname = args->dname == NULL ? NULL : *args->dname;
3416 3417
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;
3418

3419
    if (virDomainMigratePerform3(dom, xmlin,
3420 3421 3422
                                 args->cookie_in.cookie_in_val,
                                 args->cookie_in.cookie_in_len,
                                 &cookieout, &cookieoutlen,
3423
                                 dconnuri, uri,
3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435
                                 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)
3436
        virNetMessageSaveError(rerr);
3437 3438 3439 3440 3441 3442 3443
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3444 3445
remoteDispatchDomainMigrateFinish3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                   virNetServerClientPtr client ATTRIBUTE_UNUSED,
3446
                                   virNetMessagePtr msg ATTRIBUTE_UNUSED,
3447
                                   virNetMessageErrorPtr rerr,
3448 3449 3450 3451 3452 3453
                                   remote_domain_migrate_finish3_args *args,
                                   remote_domain_migrate_finish3_ret *ret)
{
    virDomainPtr dom = NULL;
    char *cookieout = NULL;
    int cookieoutlen = 0;
3454 3455
    char *uri;
    char *dconnuri;
3456
    int rv = -1;
3457 3458
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3459

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

3465 3466 3467
    uri = args->uri == NULL ? NULL : *args->uri;
    dconnuri = args->dconnuri == NULL ? NULL : *args->dconnuri;

3468
    if (!(dom = virDomainMigrateFinish3(priv->conn, args->dname,
3469 3470 3471 3472 3473 3474
                                        args->cookie_in.cookie_in_val,
                                        args->cookie_in.cookie_in_len,
                                        &cookieout, &cookieoutlen,
                                        dconnuri, uri,
                                        args->flags,
                                        args->cancelled)))
3475 3476
        goto cleanup;

3477
    make_nonnull_domain(&ret->dom, dom);
3478 3479 3480 3481 3482 3483 3484 3485 3486 3487

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

    rv = 0;

cleanup:
    if (rv < 0) {
3488
        virNetMessageSaveError(rerr);
3489 3490 3491 3492 3493 3494 3495 3496 3497
        VIR_FREE(cookieout);
    }
    if (dom)
        virDomainFree(dom);
    return rv;
}


static int
3498 3499
remoteDispatchDomainMigrateConfirm3(virNetServerPtr server ATTRIBUTE_UNUSED,
                                    virNetServerClientPtr client ATTRIBUTE_UNUSED,
3500
                                    virNetMessagePtr msg ATTRIBUTE_UNUSED,
3501 3502
                                    virNetMessageErrorPtr rerr,
                                    remote_domain_migrate_confirm3_args *args)
3503 3504 3505
{
    virDomainPtr dom = NULL;
    int rv = -1;
3506 3507
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);
3508

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

3514
    if (!(dom = get_nonnull_domain(priv->conn, args->dom)))
3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526
        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)
3527
        virNetMessageSaveError(rerr);
3528 3529 3530 3531 3532 3533
    if (dom)
        virDomainFree(dom);
    return rv;
}


3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546
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);

3547 3548 3549 3550 3551 3552 3553 3554 3555 3556
    /* 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;
    }

3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572
    if (!priv->conn) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        goto cleanup;
    }

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

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

3573
done:
3574 3575 3576 3577 3578 3579 3580 3581 3582 3583
    ret->supported = supported;
    rv = 0;

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


3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624
static int
remoteDispatchDomainOpenGraphics(virNetServerPtr server ATTRIBUTE_UNUSED,
                                 virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                 virNetMessagePtr msg,
                                 virNetMessageErrorPtr rerr,
                                 remote_domain_open_graphics_args *args)
{
    virDomainPtr dom = NULL;
    int rv = -1;
    int fd = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

    if ((fd = virNetMessageDupFD(msg, 0)) < 0)
        goto cleanup;

    if (virDomainOpenGraphics(dom,
                              args->idx,
                              fd,
                              args->flags) < 0)
        goto cleanup;

    rv = 0;

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

3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683
static int
remoteDispatchDomainGetInterfaceParameters(virNetServerPtr server ATTRIBUTE_UNUSED,
                                           virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                           virNetMessagePtr msg ATTRIBUTE_UNUSED,
                                           virNetMessageErrorPtr rerr,
                                           remote_domain_get_interface_parameters_args *args,
                                           remote_domain_get_interface_parameters_ret *ret)
{
    virDomainPtr dom = NULL;
    virTypedParameterPtr params = NULL;
    const char *device = args->device;
    int nparams = args->nparams;
    unsigned int flags;
    int rv = -1;
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

    flags = args->flags;

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

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

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

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

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

success:
    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
3684 3685
    virTypedParameterArrayClear(params, nparams);
    VIR_FREE(params);
3686 3687 3688 3689
    if (dom)
        virDomainFree(dom);
    return rv;
}
3690

3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746
static int
remoteDispatchDomainGetCPUStats(virNetServerPtr server ATTRIBUTE_UNUSED,
                                virNetServerClientPtr client ATTRIBUTE_UNUSED,
                                virNetMessagePtr hdr ATTRIBUTE_UNUSED,
                                virNetMessageErrorPtr rerr,
                                remote_domain_get_cpu_stats_args *args,
                                remote_domain_get_cpu_stats_ret *ret)
{
    virDomainPtr dom = NULL;
    struct daemonClientPrivate *priv;
    virTypedParameterPtr params = NULL;
    int rv = -1;
    int percpu_len = 0;

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

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

    if (args->nparams > 0 &&
        VIR_ALLOC_N(params, args->ncpus * args->nparams) < 0) {
        virReportOOMError();
        goto cleanup;
    }

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

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

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

success:
    rv = 0;
    ret->nparams = percpu_len;
3747 3748 3749 3750 3751 3752 3753 3754
    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--;
        }
    }
3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765

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

3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776
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;
3777
    int len = 0;
3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828
    struct daemonClientPrivate *priv =
        virNetServerClientGetPrivateData(client);

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

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

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

    if (args->maxerrors &&
        VIR_ALLOC_N(errors, args->maxerrors) < 0) {
        virReportOOMError();
        goto cleanup;
    }

    if ((len = virDomainGetDiskErrors(dom, errors,
                                      args->maxerrors,
                                      args->flags)) < 0)
        goto cleanup;

    ret->nerrors = len;
    if (errors &&
        remoteSerializeDomainDiskErrors(errors, len,
                                        &ret->errors.errors_val,
                                        &ret->errors.errors_len) < 0)
        goto cleanup;

    rv = 0;

cleanup:
    if (rv < 0)
        virNetMessageSaveError(rerr);
    if (dom)
        virDomainFree(dom);
    if (errors) {
        int i;
        for (i = 0; i < len; i++)
            VIR_FREE(errors[i].disk);
    }
    VIR_FREE(errors);
    return rv;
}

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 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 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 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954
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) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        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) {
        virNetError(VIR_ERR_INTERNAL_ERROR, "%s", _("connection not open"));
        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;
}

3955 3956 3957 3958 3959 3960 3961 3962 3963
/*----- 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
3964
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
3965 3966
{
    virDomainPtr dom;
3967
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
3968 3969 3970 3971 3972 3973 3974 3975
    /* 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
3976
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
3977
{
3978
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
3979 3980
}

D
Daniel Veillard 已提交
3981
static virInterfacePtr
3982
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
3983
{
3984
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
3985 3986
}

3987
static virStoragePoolPtr
3988
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
3989
{
3990
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid);
3991 3992 3993
}

static virStorageVolPtr
3994
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
3995 3996
{
    virStorageVolPtr ret;
3997
    ret = virGetStorageVol(conn, vol.pool, vol.name, vol.key);
3998 3999 4000
    return ret;
}

4001
static virSecretPtr
4002
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
4003
{
4004
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
4005 4006
}

4007
static virNWFilterPtr
4008
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
4009
{
4010
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
4011 4012
}

C
Chris Lalancette 已提交
4013
static virDomainSnapshotPtr
4014
get_nonnull_domain_snapshot(virDomainPtr dom, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
4015
{
4016
    return virGetDomainSnapshot(dom, snapshot.name);
C
Chris Lalancette 已提交
4017 4018
}

4019 4020
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
4021
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
4022 4023
{
    dom_dst->id = dom_src->id;
4024 4025
    dom_dst->name = strdup(dom_src->name);
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
4026 4027 4028
}

static void
4029
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
4030
{
4031 4032
    net_dst->name = strdup(net_src->name);
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
4033 4034
}

D
Daniel Veillard 已提交
4035
static void
4036 4037
make_nonnull_interface(remote_nonnull_interface *interface_dst,
                       virInterfacePtr interface_src)
D
Daniel Veillard 已提交
4038
{
4039 4040
    interface_dst->name = strdup(interface_src->name);
    interface_dst->mac = strdup(interface_src->mac);
D
Daniel Veillard 已提交
4041 4042
}

4043
static void
4044
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
4045
{
4046 4047
    pool_dst->name = strdup(pool_src->name);
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
4048 4049 4050
}

static void
4051
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
4052
{
4053 4054 4055
    vol_dst->pool = strdup(vol_src->pool);
    vol_dst->name = strdup(vol_src->name);
    vol_dst->key = strdup(vol_src->key);
4056
}
4057 4058

static void
4059
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
4060 4061 4062
{
    dev_dst->name = strdup(dev_src->name);
}
4063 4064

static void
4065
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
4066
{
4067
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
4068
    secret_dst->usageType = secret_src->usageType;
4069
    secret_dst->usageID = strdup(secret_src->usageID);
4070
}
4071 4072

static void
4073
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
4074
{
4075 4076
    nwfilter_dst->name = strdup(nwfilter_src->name);
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
4077
}
C
Chris Lalancette 已提交
4078 4079

static void
4080
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
4081 4082
{
    snapshot_dst->name = strdup(snapshot_src->name);
4083
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
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

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