remote_driver.c 293.7 KB
Newer Older
1
/*
2
 * remote_driver.c: driver to provide access to libvirtd running
3 4
 *   on a remote machine
 *
5
 * Copyright (C) 2007-2015 Red Hat, Inc.
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21
 */

22
#include <config.h>
23

24
#include <unistd.h>
25

26 27 28
#include "virnetclient.h"
#include "virnetclientprogram.h"
#include "virnetclientstream.h"
29
#include "virerror.h"
30
#include "virlog.h"
31
#include "datatypes.h"
32
#include "domain_event.h"
33
#include "network_event.h"
34
#include "storage_event.h"
35
#include "node_device_event.h"
36
#include "secret_event.h"
37
#include "driver.h"
38
#include "virbuffer.h"
39
#include "remote_driver.h"
40
#include "remote_protocol.h"
41
#include "lxc_protocol.h"
C
Chris Lalancette 已提交
42
#include "qemu_protocol.h"
43
#include "viralloc.h"
E
Eric Blake 已提交
44
#include "virfile.h"
45
#include "vircommand.h"
46
#include "intprops.h"
A
Alex Jia 已提交
47
#include "virtypedparam.h"
M
Martin Kletzander 已提交
48
#include "viruri.h"
49 50
#include "virauth.h"
#include "virauthconfig.h"
51
#include "virstring.h"
52

53 54
#define VIR_FROM_THIS VIR_FROM_REMOTE

55 56
VIR_LOG_INIT("remote.remote_driver");

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

75
static bool inside_daemon;
76

77 78
struct private_data {
    virMutex lock;
79

80 81 82
    virNetClientPtr client;
    virNetClientProgramPtr remoteProgram;
    virNetClientProgramPtr qemuProgram;
83
    virNetClientProgramPtr lxcProgram;
84

85
    int counter; /* Serial number for RPC */
86

87
#ifdef WITH_GNUTLS
88
    virNetTLSContextPtr tls;
89
#endif
90

91
    int is_secure;              /* Secure if TLS or SASL or UNIX sockets */
92
    char *type;                 /* Cached return from remoteType. */
93
    int localUses;              /* Ref count for private data */
94
    char *hostname;             /* Original hostname */
95
    bool serverKeepAlive;       /* Does server support keepalive protocol? */
96
    bool serverEventFilter;     /* Does server support modern event filtering */
97
    bool serverCloseCallback;   /* Does server support driver close callback */
98

99
    virObjectEventStatePtr eventState;
100
    virConnectCloseCallbackDataPtr closeCallback;
101 102
};

103
enum {
104
    REMOTE_CALL_QEMU              = (1 << 0),
105
    REMOTE_CALL_LXC               = (1 << 1),
106 107 108
};


109 110 111 112 113 114 115 116 117 118
static void remoteDriverLock(struct private_data *driver)
{
    virMutexLock(&driver->lock);
}

static void remoteDriverUnlock(struct private_data *driver)
{
    virMutexUnlock(&driver->lock);
}

119 120 121 122
static int call(virConnectPtr conn, struct private_data *priv,
                unsigned int flags, int proc_nr,
                xdrproc_t args_filter, char *args,
                xdrproc_t ret_filter, char *ret);
123 124 125 126 127 128 129
static int callFull(virConnectPtr conn, struct private_data *priv,
                    unsigned int flags,
                    int *fdin, size_t fdinlen,
                    int **fdout, size_t *fdoutlen,
                    int proc_nr,
                    xdrproc_t args_filter, char *args,
                    xdrproc_t ret_filter, char *ret);
130 131
static int remoteAuthenticate(virConnectPtr conn, struct private_data *priv,
                              virConnectAuthPtr auth, const char *authtype);
132
#if WITH_SASL
133 134
static int remoteAuthSASL(virConnectPtr conn, struct private_data *priv,
                          virConnectAuthPtr auth, const char *mech);
135
#endif /* WITH_SASL */
136 137
static int remoteAuthPolkit(virConnectPtr conn, struct private_data *priv,
                            virConnectAuthPtr auth);
138

139 140
static virDomainPtr get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network(virConnectPtr conn, remote_nonnull_network network);
141
static virNetworkPortPtr get_nonnull_network_port(virConnectPtr conn, remote_nonnull_network_port port);
142
static virNWFilterPtr get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter);
143
static virNWFilterBindingPtr get_nonnull_nwfilter_binding(virConnectPtr conn, remote_nonnull_nwfilter_binding binding);
144 145 146 147 148 149 150 151
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 virNodeDevicePtr get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev);
static virSecretPtr get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret);
static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr domain, remote_nonnull_domain_snapshot snapshot);
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);
152
static void make_nonnull_network_port(remote_nonnull_network_port *port_dst, virNetworkPortPtr port_src);
153 154 155
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 vol_src);
static void make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src);
156 157
static void
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src);
158 159
static void make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src);
static void make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src);
160
static void make_nonnull_nwfilter_binding(remote_nonnull_nwfilter_binding *binding_dst, virNWFilterBindingPtr binding_src);
161
static void make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src);
162

163 164 165
/*----------------------------------------------------------------------*/

/* Helper functions for remoteOpen. */
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
static int remoteSplitURIScheme(virURIPtr uri,
                                char **driver,
                                char **transport)
{
    char *p = strchr(uri->scheme, '+');

    *driver = *transport = NULL;

    if (VIR_STRNDUP(*driver, uri->scheme, p ? p - uri->scheme : -1) < 0)
        return -1;

    if (p &&
        VIR_STRDUP(*transport, p + 1) < 0) {
        VIR_FREE(*driver);
        return -1;
    }

    return 0;
}

186

187
static int
188 189 190
remoteStateInitialize(bool privileged ATTRIBUTE_UNUSED,
                      virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                      void *opaque ATTRIBUTE_UNUSED)
191 192 193 194
{
    /* Mark that we're inside the daemon so we can avoid
     * re-entering ourselves
     */
195
    inside_daemon = true;
196 197 198
    return 0;
}

199

200 201 202 203 204
static void
remoteDomainBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque);
static void
205 206 207 208 209
remoteDomainBuildEventCallbackLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                        virNetClientPtr client ATTRIBUTE_UNUSED,
                                        void *evdata, void *opaque);

static void
210 211 212 213
remoteDomainBuildEventReboot(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                             virNetClientPtr client ATTRIBUTE_UNUSED,
                             void *evdata, void *opaque);
static void
214 215 216 217 218
remoteDomainBuildEventCallbackReboot(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                     virNetClientPtr client ATTRIBUTE_UNUSED,
                                     void *evdata, void *opaque);

static void
219 220 221 222
remoteDomainBuildEventRTCChange(virNetClientProgramPtr prog,
                                virNetClientPtr client,
                                void *evdata, void *opaque);
static void
223 224 225 226 227
remoteDomainBuildEventCallbackRTCChange(virNetClientProgramPtr prog,
                                        virNetClientPtr client,
                                        void *evdata, void *opaque);

static void
228 229 230 231
remoteDomainBuildEventWatchdog(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
static void
232 233 234 235 236
remoteDomainBuildEventCallbackWatchdog(virNetClientProgramPtr prog,
                                       virNetClientPtr client,
                                       void *evdata, void *opaque);

static void
237 238 239 240
remoteDomainBuildEventIOError(virNetClientProgramPtr prog,
                              virNetClientPtr client,
                              void *evdata, void *opaque);
static void
241 242 243 244 245
remoteDomainBuildEventCallbackIOError(virNetClientProgramPtr prog,
                                      virNetClientPtr client,
                                      void *evdata, void *opaque);

static void
246 247 248 249
remoteDomainBuildEventIOErrorReason(virNetClientProgramPtr prog,
                                    virNetClientPtr client,
                                    void *evdata, void *opaque);
static void
250 251 252 253 254
remoteDomainBuildEventCallbackIOErrorReason(virNetClientProgramPtr prog,
                                            virNetClientPtr client,
                                            void *evdata, void *opaque);

static void
255 256 257 258
remoteDomainBuildEventGraphics(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
static void
259 260 261 262 263
remoteDomainBuildEventCallbackGraphics(virNetClientProgramPtr prog,
                                       virNetClientPtr client,
                                       void *evdata, void *opaque);

static void
264 265 266
remoteDomainBuildEventControlError(virNetClientProgramPtr prog,
                                   virNetClientPtr client,
                                   void *evdata, void *opaque);
267 268 269 270
static void
remoteDomainBuildEventCallbackControlError(virNetClientProgramPtr prog,
                                           virNetClientPtr client,
                                           void *evdata, void *opaque);
271

272 273 274 275
static void
remoteDomainBuildEventBlockJob(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
276 277 278 279 280
static void
remoteDomainBuildEventCallbackBlockJob(virNetClientProgramPtr prog,
                                       virNetClientPtr client,
                                       void *evdata, void *opaque);

281

282 283 284 285
static void
remoteDomainBuildEventDiskChange(virNetClientProgramPtr prog,
                                 virNetClientPtr client,
                                 void *evdata, void *opaque);
286 287 288 289
static void
remoteDomainBuildEventCallbackDiskChange(virNetClientProgramPtr prog,
                                         virNetClientPtr client,
                                         void *evdata, void *opaque);
290

291 292 293 294
static void
remoteDomainBuildEventTrayChange(virNetClientProgramPtr prog,
                                 virNetClientPtr client,
                                 void *evdata, void *opaque);
295 296 297 298
static void
remoteDomainBuildEventCallbackTrayChange(virNetClientProgramPtr prog,
                                         virNetClientPtr client,
                                         void *evdata, void *opaque);
299

O
Osier Yang 已提交
300 301 302 303
static void
remoteDomainBuildEventPMWakeup(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
304 305 306 307
static void
remoteDomainBuildEventCallbackPMWakeup(virNetClientProgramPtr prog,
                                       virNetClientPtr client,
                                       void *evdata, void *opaque);
O
Osier Yang 已提交
308

O
Osier Yang 已提交
309 310 311 312
static void
remoteDomainBuildEventPMSuspend(virNetClientProgramPtr prog,
                                virNetClientPtr client,
                                void *evdata, void *opaque);
313
static void
314 315 316 317 318
remoteDomainBuildEventCallbackPMSuspend(virNetClientProgramPtr prog,
                                        virNetClientPtr client,
                                        void *evdata, void *opaque);

static void
319 320 321
remoteDomainBuildEventBalloonChange(virNetClientProgramPtr prog,
                                    virNetClientPtr client,
                                    void *evdata, void *opaque);
322
static void
323 324 325 326 327
remoteDomainBuildEventCallbackBalloonChange(virNetClientProgramPtr prog,
                                            virNetClientPtr client,
                                            void *evdata, void *opaque);

static void
328 329 330
remoteDomainBuildEventPMSuspendDisk(virNetClientProgramPtr prog,
                                  virNetClientPtr client,
                                  void *evdata, void *opaque);
331 332 333 334
static void
remoteDomainBuildEventCallbackPMSuspendDisk(virNetClientProgramPtr prog,
                                            virNetClientPtr client,
                                            void *evdata, void *opaque);
O
Osier Yang 已提交
335

336 337 338 339
static void
remoteDomainBuildEventDeviceRemoved(virNetClientProgramPtr prog,
                                    virNetClientPtr client,
                                    void *evdata, void *opaque);
340 341 342 343
static void
remoteDomainBuildEventCallbackDeviceRemoved(virNetClientProgramPtr prog,
                                            virNetClientPtr client,
                                            void *evdata, void *opaque);
344 345 346 347
static void
remoteDomainBuildEventCallbackDeviceAdded(virNetClientProgramPtr prog,
                                          virNetClientPtr client,
                                          void *evdata, void *opaque);
348 349 350 351
static void
remoteDomainBuildEventCallbackDeviceRemovalFailed(virNetClientProgramPtr prog,
                                                  virNetClientPtr client,
                                                  void *evdata, void *opaque);
352

353 354 355 356 357
static void
remoteDomainBuildEventBlockJob2(virNetClientProgramPtr prog,
                                virNetClientPtr client,
                                void *evdata, void *opaque);

358 359 360 361 362
static void
remoteDomainBuildEventCallbackTunable(virNetClientProgramPtr prog,
                                      virNetClientPtr client,
                                      void *evdata, void *opaque);

363 364 365 366 367
static void
remoteDomainBuildEventCallbackAgentLifecycle(virNetClientProgramPtr prog,
                                             virNetClientPtr client,
                                             void *evdata, void *opaque);

368 369 370 371 372
static void
remoteDomainBuildEventCallbackMigrationIteration(virNetClientProgramPtr prog,
                                                 virNetClientPtr client,
                                                 void *evdata, void *opaque);

J
Jiri Denemark 已提交
373 374 375 376 377
static void
remoteDomainBuildEventCallbackJobCompleted(virNetClientProgramPtr prog,
                                           virNetClientPtr client,
                                           void *evdata, void *opaque);

378 379 380 381 382
static void
remoteDomainBuildEventCallbackMetadataChange(virNetClientProgramPtr prog,
                                             virNetClientPtr client,
                                             void *evdata, void *opaque);

383 384 385 386 387
static void
remoteNetworkBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque);

388 389 390 391 392
static void
remoteStoragePoolBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                     virNetClientPtr client ATTRIBUTE_UNUSED,
                                     void *evdata, void *opaque);

393 394 395 396 397
static void
remoteStoragePoolBuildEventRefresh(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                   virNetClientPtr client ATTRIBUTE_UNUSED,
                                   void *evdata, void *opaque);

398 399 400 401 402
static void
remoteNodeDeviceBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque);

403 404 405 406 407
static void
remoteNodeDeviceBuildEventUpdate(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque);

408 409 410 411 412
static void
remoteSecretBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque);

413 414 415 416 417
static void
remoteSecretBuildEventValueChanged(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                   virNetClientPtr client ATTRIBUTE_UNUSED,
                                   void *evdata, void *opaque);

418 419 420 421 422
static void
remoteDomainBuildEventBlockThreshold(virNetClientProgramPtr prog,
                                     virNetClientPtr client,
                                     void *evdata, void *opaque);

423 424 425 426 427
static void
remoteConnectNotifyEventConnectionClosed(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                         virNetClientPtr client ATTRIBUTE_UNUSED,
                                         void *evdata, void *opaque);

428
static virNetClientProgramEvent remoteEvents[] = {
429 430 431 432
    { REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
      remoteDomainBuildEventLifecycle,
      sizeof(remote_domain_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_domain_event_lifecycle_msg },
433 434 435 436 437 438 439 440
    { REMOTE_PROC_DOMAIN_EVENT_REBOOT,
      remoteDomainBuildEventReboot,
      sizeof(remote_domain_event_reboot_msg),
      (xdrproc_t)xdr_remote_domain_event_reboot_msg },
    { REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
      remoteDomainBuildEventRTCChange,
      sizeof(remote_domain_event_rtc_change_msg),
      (xdrproc_t)xdr_remote_domain_event_rtc_change_msg },
441 442 443 444 445 446 447 448 449 450 451 452
    { REMOTE_PROC_DOMAIN_EVENT_WATCHDOG,
      remoteDomainBuildEventWatchdog,
      sizeof(remote_domain_event_watchdog_msg),
      (xdrproc_t)xdr_remote_domain_event_watchdog_msg},
    { REMOTE_PROC_DOMAIN_EVENT_IO_ERROR,
      remoteDomainBuildEventIOError,
      sizeof(remote_domain_event_io_error_msg),
      (xdrproc_t)xdr_remote_domain_event_io_error_msg },
    { REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
      remoteDomainBuildEventGraphics,
      sizeof(remote_domain_event_graphics_msg),
      (xdrproc_t)xdr_remote_domain_event_graphics_msg },
453 454 455 456
    { REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON,
      remoteDomainBuildEventIOErrorReason,
      sizeof(remote_domain_event_io_error_reason_msg),
      (xdrproc_t)xdr_remote_domain_event_io_error_reason_msg },
457 458 459 460
    { REMOTE_PROC_DOMAIN_EVENT_CONTROL_ERROR,
      remoteDomainBuildEventControlError,
      sizeof(remote_domain_event_control_error_msg),
      (xdrproc_t)xdr_remote_domain_event_control_error_msg },
461 462 463 464
    { REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB,
      remoteDomainBuildEventBlockJob,
      sizeof(remote_domain_event_block_job_msg),
      (xdrproc_t)xdr_remote_domain_event_block_job_msg },
465 466 467 468
    { REMOTE_PROC_DOMAIN_EVENT_DISK_CHANGE,
      remoteDomainBuildEventDiskChange,
      sizeof(remote_domain_event_disk_change_msg),
      (xdrproc_t)xdr_remote_domain_event_disk_change_msg },
469 470 471 472
    { REMOTE_PROC_DOMAIN_EVENT_TRAY_CHANGE,
      remoteDomainBuildEventTrayChange,
      sizeof(remote_domain_event_tray_change_msg),
      (xdrproc_t)xdr_remote_domain_event_tray_change_msg },
O
Osier Yang 已提交
473 474 475 476
    { REMOTE_PROC_DOMAIN_EVENT_PMWAKEUP,
      remoteDomainBuildEventPMWakeup,
      sizeof(remote_domain_event_pmwakeup_msg),
      (xdrproc_t)xdr_remote_domain_event_pmwakeup_msg },
O
Osier Yang 已提交
477 478 479 480
    { REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND,
      remoteDomainBuildEventPMSuspend,
      sizeof(remote_domain_event_pmsuspend_msg),
      (xdrproc_t)xdr_remote_domain_event_pmsuspend_msg },
481 482 483 484
    { REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE,
      remoteDomainBuildEventBalloonChange,
      sizeof(remote_domain_event_balloon_change_msg),
      (xdrproc_t)xdr_remote_domain_event_balloon_change_msg },
485 486 487 488
    { REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND_DISK,
      remoteDomainBuildEventPMSuspendDisk,
      sizeof(remote_domain_event_pmsuspend_disk_msg),
      (xdrproc_t)xdr_remote_domain_event_pmsuspend_disk_msg },
489 490 491 492
    { REMOTE_PROC_DOMAIN_EVENT_DEVICE_REMOVED,
      remoteDomainBuildEventDeviceRemoved,
      sizeof(remote_domain_event_device_removed_msg),
      (xdrproc_t)xdr_remote_domain_event_device_removed_msg },
493 494 495 496 497 498 499 500 501
    /* All events above here are legacy events, missing the callback
     * ID, which means the server has a single global registration and
     * we do full filtering in the client.  If the server lacks
     * VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK, those are the only
     * events we should ever receive.  Conversely, all events below
     * here should only be triggered by modern servers, and all
     * contain a callbackID.  Although we have to duplicate the first
     * 16 domain events in both styles for back-compat, any future
     * domain event additions should only use the modern style.  */
502 503 504 505
    { REMOTE_PROC_NETWORK_EVENT_LIFECYCLE,
      remoteNetworkBuildEventLifecycle,
      sizeof(remote_network_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_network_event_lifecycle_msg },
506 507 508 509
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_LIFECYCLE,
      remoteDomainBuildEventCallbackLifecycle,
      sizeof(remote_domain_event_callback_lifecycle_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_lifecycle_msg },
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_REBOOT,
      remoteDomainBuildEventCallbackReboot,
      sizeof(remote_domain_event_callback_reboot_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_reboot_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_RTC_CHANGE,
      remoteDomainBuildEventCallbackRTCChange,
      sizeof(remote_domain_event_callback_rtc_change_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_rtc_change_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_WATCHDOG,
      remoteDomainBuildEventCallbackWatchdog,
      sizeof(remote_domain_event_callback_watchdog_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_watchdog_msg},
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_IO_ERROR,
      remoteDomainBuildEventCallbackIOError,
      sizeof(remote_domain_event_callback_io_error_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_io_error_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_GRAPHICS,
      remoteDomainBuildEventCallbackGraphics,
      sizeof(remote_domain_event_callback_graphics_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_graphics_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_IO_ERROR_REASON,
      remoteDomainBuildEventCallbackIOErrorReason,
      sizeof(remote_domain_event_callback_io_error_reason_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_io_error_reason_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_CONTROL_ERROR,
      remoteDomainBuildEventCallbackControlError,
      sizeof(remote_domain_event_callback_control_error_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_control_error_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_BLOCK_JOB,
      remoteDomainBuildEventCallbackBlockJob,
      sizeof(remote_domain_event_callback_block_job_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_block_job_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DISK_CHANGE,
      remoteDomainBuildEventCallbackDiskChange,
      sizeof(remote_domain_event_callback_disk_change_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_disk_change_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_TRAY_CHANGE,
      remoteDomainBuildEventCallbackTrayChange,
      sizeof(remote_domain_event_callback_tray_change_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_tray_change_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMWAKEUP,
      remoteDomainBuildEventCallbackPMWakeup,
      sizeof(remote_domain_event_callback_pmwakeup_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_pmwakeup_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND,
      remoteDomainBuildEventCallbackPMSuspend,
      sizeof(remote_domain_event_callback_pmsuspend_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_BALLOON_CHANGE,
      remoteDomainBuildEventCallbackBalloonChange,
      sizeof(remote_domain_event_callback_balloon_change_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_balloon_change_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_PMSUSPEND_DISK,
      remoteDomainBuildEventCallbackPMSuspendDisk,
      sizeof(remote_domain_event_callback_pmsuspend_disk_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_pmsuspend_disk_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DEVICE_REMOVED,
      remoteDomainBuildEventCallbackDeviceRemoved,
      sizeof(remote_domain_event_callback_device_removed_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_device_removed_msg },
570 571 572 573
    { REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB_2,
      remoteDomainBuildEventBlockJob2,
      sizeof(remote_domain_event_block_job_2_msg),
      (xdrproc_t)xdr_remote_domain_event_block_job_2_msg },
574 575 576 577
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_TUNABLE,
      remoteDomainBuildEventCallbackTunable,
      sizeof(remote_domain_event_callback_tunable_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_tunable_msg },
578 579 580 581
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_AGENT_LIFECYCLE,
      remoteDomainBuildEventCallbackAgentLifecycle,
      sizeof(remote_domain_event_callback_agent_lifecycle_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_agent_lifecycle_msg },
582 583 584 585
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DEVICE_ADDED,
      remoteDomainBuildEventCallbackDeviceAdded,
      sizeof(remote_domain_event_callback_device_added_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_device_added_msg },
586 587 588 589
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_MIGRATION_ITERATION,
      remoteDomainBuildEventCallbackMigrationIteration,
      sizeof(remote_domain_event_callback_migration_iteration_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_migration_iteration_msg },
590 591 592 593
    { REMOTE_PROC_CONNECT_EVENT_CONNECTION_CLOSED,
      remoteConnectNotifyEventConnectionClosed,
      sizeof(remote_connect_event_connection_closed_msg),
      (xdrproc_t)xdr_remote_connect_event_connection_closed_msg },
J
Jiri Denemark 已提交
594 595 596 597
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_JOB_COMPLETED,
      remoteDomainBuildEventCallbackJobCompleted,
      sizeof(remote_domain_event_callback_job_completed_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_job_completed_msg },
598 599 600 601
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_DEVICE_REMOVAL_FAILED,
      remoteDomainBuildEventCallbackDeviceRemovalFailed,
      sizeof(remote_domain_event_callback_device_removal_failed_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_device_removal_failed_msg },
602 603 604 605
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_METADATA_CHANGE,
      remoteDomainBuildEventCallbackMetadataChange,
      sizeof(remote_domain_event_callback_metadata_change_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_metadata_change_msg },
606 607 608 609
    { REMOTE_PROC_STORAGE_POOL_EVENT_LIFECYCLE,
      remoteStoragePoolBuildEventLifecycle,
      sizeof(remote_storage_pool_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_storage_pool_event_lifecycle_msg },
610 611 612 613
    { REMOTE_PROC_STORAGE_POOL_EVENT_REFRESH,
      remoteStoragePoolBuildEventRefresh,
      sizeof(remote_storage_pool_event_refresh_msg),
      (xdrproc_t)xdr_remote_storage_pool_event_refresh_msg },
614 615 616 617
    { REMOTE_PROC_NODE_DEVICE_EVENT_LIFECYCLE,
      remoteNodeDeviceBuildEventLifecycle,
      sizeof(remote_node_device_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_node_device_event_lifecycle_msg },
618 619 620 621
    { REMOTE_PROC_NODE_DEVICE_EVENT_UPDATE,
      remoteNodeDeviceBuildEventUpdate,
      sizeof(remote_node_device_event_update_msg),
      (xdrproc_t)xdr_remote_node_device_event_update_msg },
622 623 624 625
    { REMOTE_PROC_SECRET_EVENT_LIFECYCLE,
      remoteSecretBuildEventLifecycle,
      sizeof(remote_secret_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_secret_event_lifecycle_msg },
626 627 628 629
    { REMOTE_PROC_SECRET_EVENT_VALUE_CHANGED,
      remoteSecretBuildEventValueChanged,
      sizeof(remote_secret_event_value_changed_msg),
      (xdrproc_t)xdr_remote_secret_event_value_changed_msg },
630 631 632 633
    { REMOTE_PROC_DOMAIN_EVENT_BLOCK_THRESHOLD,
      remoteDomainBuildEventBlockThreshold,
      sizeof(remote_domain_event_block_threshold_msg),
      (xdrproc_t)xdr_remote_domain_event_block_threshold_msg },
634
};
635

636 637 638 639 640 641 642 643 644 645 646
static void
remoteConnectNotifyEventConnectionClosed(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                         virNetClientPtr client ATTRIBUTE_UNUSED,
                                         void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_connect_event_connection_closed_msg *msg = evdata;

    virConnectCloseCallbackDataCall(priv->closeCallback, msg->reason);
}
647 648 649 650 651 652 653 654 655 656 657 658 659

static void
remoteDomainBuildQemuMonitorEvent(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                  virNetClientPtr client ATTRIBUTE_UNUSED,
                                  void *evdata, void *opaque);

static virNetClientProgramEvent qemuEvents[] = {
    { QEMU_PROC_DOMAIN_MONITOR_EVENT,
      remoteDomainBuildQemuMonitorEvent,
      sizeof(qemu_domain_monitor_event_msg),
      (xdrproc_t)xdr_qemu_domain_monitor_event_msg },
};

660
enum virDrvOpenRemoteFlags {
661
    VIR_DRV_OPEN_REMOTE_RO = (1 << 0),
662 663
    VIR_DRV_OPEN_REMOTE_USER      = (1 << 1), /* Use the per-user socket path */
    VIR_DRV_OPEN_REMOTE_AUTOSTART = (1 << 2), /* Autostart a per-user daemon */
664
};
665

666

667 668 669 670
static void
remoteClientCloseFunc(virNetClientPtr client ATTRIBUTE_UNUSED,
                      int reason,
                      void *opaque)
671
{
672 673
    virConnectCloseCallbackDataCall((virConnectCloseCallbackDataPtr)opaque,
                                    reason);
674 675
}

676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
static bool
remoteConnectSupportsFeatureUnlocked(virConnectPtr conn,
                                     struct private_data *priv,
                                     int feature)
{
    remote_connect_supports_feature_args args = { feature };
    remote_connect_supports_feature_ret ret = { 0 };
    int rc;

    rc = call(conn, priv, 0, REMOTE_PROC_CONNECT_SUPPORTS_FEATURE,
              (xdrproc_t)xdr_remote_connect_supports_feature_args, (char *) &args,
              (xdrproc_t)xdr_remote_connect_supports_feature_ret, (char *) &ret);

    return rc != -1 && ret.supported;
}

692
/* helper macro to ease extraction of arguments from the URI */
693 694 695 696 697 698 699
#define EXTRACT_URI_ARG_STR(ARG_NAME, ARG_VAR) \
    if (STRCASEEQ(var->name, ARG_NAME)) { \
        VIR_FREE(ARG_VAR); \
        if (VIR_STRDUP(ARG_VAR, var->value) < 0) \
            goto failed; \
        var->ignore = 1; \
        continue; \
700 701
    }

702 703 704 705 706 707 708 709 710 711 712 713
#define EXTRACT_URI_ARG_BOOL(ARG_NAME, ARG_VAR) \
    if (STRCASEEQ(var->name, ARG_NAME)) { \
        int tmp; \
        if (virStrToLong_i(var->value, NULL, 10, &tmp) < 0) { \
            virReportError(VIR_ERR_INVALID_ARG, \
                           _("Failed to parse value of URI component %s"), \
                           var->name); \
            goto failed; \
        } \
        ARG_VAR = tmp == 0; \
        var->ignore = 1; \
        continue; \
714
    }
715 716


717
#ifndef WIN32
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
static char *remoteGetUNIXSocketNonRoot(void)
{
    char *sockname = NULL;
    char *userdir = virGetUserRuntimeDirectory();

    if (!userdir)
        return NULL;

    if (virAsprintf(&sockname, "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) {
        VIR_FREE(userdir);
        return NULL;
    }
    VIR_FREE(userdir);

    VIR_DEBUG("Chosen UNIX sockname %s", sockname);
    return sockname;
}
735
#endif /* WIN32 */
736 737 738 739 740 741 742 743 744 745 746 747 748 749

static char *remoteGetUNIXSocketRoot(unsigned int flags)
{
    char *sockname = NULL;

    if (VIR_STRDUP(sockname,
                   flags & VIR_DRV_OPEN_REMOTE_RO ?
                   LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
        return NULL;

    VIR_DEBUG("Chosen UNIX sockname %s", sockname);
    return sockname;
}

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
/*
 * URIs that this driver needs to handle:
 *
 * The easy answer:
 *   - Everything that no one else has yet claimed, but nothing if
 *     we're inside the libvirtd daemon
 *
 * The hard answer:
 *   - Plain paths (///var/lib/xen/xend-socket)  -> UNIX domain socket
 *   - xxx://servername/      -> TLS connection
 *   - xxx+tls://servername/  -> TLS connection
 *   - xxx+tls:///            -> TLS connection to localhost
 *   - xxx+tcp://servername/  -> TCP connection
 *   - xxx+tcp:///            -> TCP connection to localhost
 *   - xxx+unix:///           -> UNIX domain socket
 *   - xxx:///                -> UNIX domain socket
766 767
 *   - xxx+ssh:///            -> SSH connection (legacy)
 *   - xxx+libssh2:///        -> SSH connection (using libssh2)
768
 *   - xxx+libssh:///         -> SSH connection (using libssh)
769
 */
770
static int
771 772
doRemoteOpen(virConnectPtr conn,
             struct private_data *priv,
773 774
             const char *driver_str,
             const char *transport_str,
775
             virConnectAuthPtr auth ATTRIBUTE_UNUSED,
776
             virConfPtr conf,
777
             unsigned int flags)
778
{
779 780 781 782
    enum {
        trans_tls,
        trans_unix,
        trans_ssh,
783
        trans_libssh2,
784 785
        trans_ext,
        trans_tcp,
786
        trans_libssh,
787
    } transport;
788
#ifndef WIN32
M
Michal Privoznik 已提交
789
    char *daemonPath = NULL;
790
#endif
791
    char *tls_priority = NULL;
792

793
    /* We handle *ALL* URIs here. The caller has rejected any
794
     * URIs we don't care about */
795

796
    if (conn->uri) {
797 798 799 800 801 802 803 804 805 806 807 808 809 810
        if (!transport_str) {
            if (conn->uri->server)
                transport = trans_tls;
            else
                transport = trans_unix;
        } else {
            if (STRCASEEQ(transport_str, "tls")) {
                transport = trans_tls;
            } else if (STRCASEEQ(transport_str, "unix")) {
                if (conn->uri->server) {
                    virReportError(VIR_ERR_INVALID_ARG,
                                   _("using unix socket and remote "
                                     "server '%s' is not supported."),
                                   conn->uri->server);
811
                    return VIR_DRV_OPEN_ERROR;
812 813
                } else {
                    transport = trans_unix;
814
                }
815 816 817 818 819 820 821 822 823 824 825 826 827
            } else if (STRCASEEQ(transport_str, "ssh")) {
                transport = trans_ssh;
            } else if (STRCASEEQ(transport_str, "libssh2")) {
                transport = trans_libssh2;
            } else if (STRCASEEQ(transport_str, "ext")) {
                transport = trans_ext;
            } else if (STRCASEEQ(transport_str, "tcp")) {
                transport = trans_tcp;
            } else if (STRCASEEQ(transport_str, "libssh")) {
                transport = trans_libssh;
            } else {
                virReportError(VIR_ERR_INVALID_ARG, "%s",
                               _("remote_open: transport in URL not recognised "
828
                                 "(should be tls|unix|ssh|ext|tcp|libssh2|libssh)"));
829
                return VIR_DRV_OPEN_ERROR;
830 831 832 833 834
            }
        }
    } else {
        /* No URI, then must be probing so use UNIX socket */
        transport = trans_unix;
835
    }
836

837 838 839 840 841 842 843 844 845 846 847 848 849 850
    /*
     * We don't want to be executing external programs in setuid mode,
     * so this rules out 'ext' and 'ssh' transports. Exclude libssh
     * and tls too, since we're not confident the libraries are safe
     * for setuid usage. Just allow UNIX sockets, since that does
     * not require any external libraries or command execution
     */
    if (virIsSUID() &&
        transport != trans_unix) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Only Unix socket URI transport is allowed in setuid mode"));
        return VIR_DRV_OPEN_ERROR;
    }

E
Eric Blake 已提交
851
    /* Local variables which we will initialize. These can
852 853
     * get freed in the failed: path.
     */
854 855
    char *name = NULL, *command = NULL, *sockname = NULL, *netcat = NULL;
    char *port = NULL, *authtype = NULL, *username = NULL;
L
Laine Stump 已提交
856
    bool sanity = true, verify = true, tty ATTRIBUTE_UNUSED = true;
857 858 859
    char *pkipath = NULL, *keyfile = NULL, *sshauth = NULL;

    char *knownHostsVerify = NULL,  *knownHosts = NULL;
860

861 862 863
    /* Return code from this function, and the private data. */
    int retcode = VIR_DRV_OPEN_ERROR;

864
    /* Remote server defaults to "localhost" if not specified. */
865
    if (conn->uri && conn->uri->port != 0) {
866
        if (virAsprintf(&port, "%d", conn->uri->port) < 0)
867
            goto failed;
868
    } else if (transport == trans_tls) {
869 870
        if (VIR_STRDUP(port, LIBVIRTD_TLS_PORT) < 0)
            goto failed;
871
    } else if (transport == trans_tcp) {
872 873
        if (VIR_STRDUP(port, LIBVIRTD_TCP_PORT) < 0)
            goto failed;
874
    } /* Port not used for unix, ext., default for ssh */
875

876 877 878 879
    if (VIR_STRDUP(priv->hostname,
                   conn->uri && conn->uri->server ?
                   conn->uri->server : "localhost") < 0)
        goto failed;
880

881 882
    if (conn->uri && VIR_STRDUP(username, conn->uri->user) < 0)
        goto failed;
883

884 885 886 887 888
    /* Get the variables from the query string.
     * Then we need to reconstruct the query string (because
     * feasibly it might contain variables needed by the real driver,
     * although that won't be the case for now).
     */
889
    size_t i;
890

891
    if (conn->uri) {
892
        for (i = 0; i < conn->uri->paramsCount; i++) {
893
            virURIParamPtr var = &conn->uri->params[i];
894 895 896 897
            EXTRACT_URI_ARG_STR("name", name);
            EXTRACT_URI_ARG_STR("command", command);
            EXTRACT_URI_ARG_STR("socket", sockname);
            EXTRACT_URI_ARG_STR("auth", authtype);
898
            EXTRACT_URI_ARG_STR("sshauth", sshauth);
899 900 901
            EXTRACT_URI_ARG_STR("netcat", netcat);
            EXTRACT_URI_ARG_STR("keyfile", keyfile);
            EXTRACT_URI_ARG_STR("pkipath", pkipath);
902 903
            EXTRACT_URI_ARG_STR("known_hosts", knownHosts);
            EXTRACT_URI_ARG_STR("known_hosts_verify", knownHostsVerify);
904
            EXTRACT_URI_ARG_STR("tls_priority", tls_priority);
905 906 907 908 909 910

            EXTRACT_URI_ARG_BOOL("no_sanity", sanity);
            EXTRACT_URI_ARG_BOOL("no_verify", verify);
            EXTRACT_URI_ARG_BOOL("no_tty", tty);

            if (STRCASEEQ(var->name, "authfile")) {
911 912
                /* Strip this param, used by virauth.c */
                var->ignore = 1;
913
                continue;
914
            }
915 916 917

            VIR_DEBUG("passing through variable '%s' ('%s') to remote end",
                       var->name, var->value);
918
        }
919

920 921
        /* Construct the original name. */
        if (!name) {
922 923 924
            if (conn->uri->scheme &&
                (STREQ(conn->uri->scheme, "remote") ||
                 STRPREFIX(conn->uri->scheme, "remote+"))) {
925
                /* Allow remote serve to probe */
926 927
                if (VIR_STRDUP(name, "") < 0)
                    goto failed;
928
            } else {
M
Martin Kletzander 已提交
929
                virURI tmpuri = {
930
                    .scheme = (char *)driver_str,
931
                    .query = virURIFormatParams(conn->uri),
932 933 934 935
                    .path = conn->uri->path,
                    .fragment = conn->uri->fragment,
                };

M
Martin Kletzander 已提交
936
                name = virURIFormat(&tmpuri);
937

938 939
                VIR_FREE(tmpuri.query);

940 941
                if (!name)
                    goto failed;
942
            }
943
        }
944 945
    } else {
        /* Probe URI server side */
946 947
        if (VIR_STRDUP(name, "") < 0)
            goto failed;
948 949
    }

950
    VIR_DEBUG("proceeding with name = %s", name);
951

952 953
    /* For ext transport, command is required. */
    if (transport == trans_ext && !command) {
954 955
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("remote_open: for 'ext' transport, command is required"));
956 957 958
        goto failed;
    }

959
    VIR_DEBUG("Connecting with transport %d", transport);
960 961 962
    /* Connect to the remote service. */
    switch (transport) {
    case trans_tls:
963 964 965
        if (conf && !tls_priority &&
            virConfGetValueString(conf, "tls_priority", &tls_priority) < 0)
            goto failed;
966

967
#ifdef WITH_GNUTLS
968 969
        priv->tls = virNetTLSContextNewClientPath(pkipath,
                                                  geteuid() != 0 ? true : false,
970
                                                  tls_priority,
971
                                                  sanity, verify);
972 973
        if (!priv->tls)
            goto failed;
974
        priv->is_secure = 1;
975
        ATTRIBUTE_FALLTHROUGH;
976
#else
977
        (void)tls_priority;
978
        (void)sanity;
979
        (void)verify;
980 981 982 983
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("GNUTLS support not available in this build"));
        goto failed;
#endif
984

985
    case trans_tcp:
986
        priv->client = virNetClientNewTCP(priv->hostname, port, AF_UNSPEC);
987
        if (!priv->client)
988 989
            goto failed;

990
#ifdef WITH_GNUTLS
991 992 993 994
        if (priv->tls) {
            VIR_DEBUG("Starting TLS session");
            if (virNetClientSetTLSSession(priv->client, priv->tls) < 0)
                goto failed;
995
        }
996
#endif
997 998 999

        break;

1000
    case trans_libssh2:
1001 1002
        if (!sockname) {
            /* Right now we don't support default session connections */
1003
            if (flags & VIR_DRV_OPEN_REMOTE_USER) {
1004 1005 1006 1007 1008 1009 1010
                virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                               _("Connecting to session instance without "
                                 "socket path is not supported by the libssh2 "
                                 "connection driver"));
                goto failed;
            }

1011
            if (!(sockname = remoteGetUNIXSocketRoot(flags)))
1012 1013
                goto failed;
        }
1014 1015 1016 1017 1018

        VIR_DEBUG("Starting LibSSH2 session");

        priv->client = virNetClientNewLibSSH2(priv->hostname,
                                              port,
1019
                                              AF_UNSPEC,
1020 1021 1022 1023 1024 1025 1026
                                              username,
                                              keyfile,
                                              knownHosts,
                                              knownHostsVerify,
                                              sshauth,
                                              netcat,
                                              sockname,
1027 1028
                                              auth,
                                              conn->uri);
1029 1030 1031 1032 1033 1034
        if (!priv->client)
            goto failed;

        priv->is_secure = 1;
        break;

1035 1036 1037
    case trans_libssh:
        if (!sockname) {
            /* Right now we don't support default session connections */
1038
            if (flags & VIR_DRV_OPEN_REMOTE_USER) {
1039 1040 1041 1042 1043 1044 1045
                virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                               _("Connecting to session instance without "
                                 "socket path is not supported by the libssh "
                                 "connection driver"));
                goto failed;
            }

1046
            if (!(sockname = remoteGetUNIXSocketRoot(flags)))
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
                goto failed;
        }

        VIR_DEBUG("Starting libssh session");

        priv->client = virNetClientNewLibssh(priv->hostname,
                                             port,
                                             AF_UNSPEC,
                                             username,
                                             keyfile,
                                             knownHosts,
                                             knownHostsVerify,
                                             sshauth,
                                             netcat,
                                             sockname,
                                             auth,
                                             conn->uri);
        if (!priv->client)
            goto failed;

        priv->is_secure = 1;
        break;

1070
#ifndef WIN32
1071
    case trans_unix:
1072
        if (!sockname) {
1073 1074 1075 1076 1077 1078
            if (flags & VIR_DRV_OPEN_REMOTE_USER)
                sockname = remoteGetUNIXSocketNonRoot();
            else
                sockname = remoteGetUNIXSocketRoot(flags);
            if (!sockname)
                goto failed;
1079 1080
        }

1081
        if ((flags & VIR_DRV_OPEN_REMOTE_AUTOSTART) &&
1082 1083
            !(daemonPath = virFileFindResourceFull("libvirtd",
                                                   NULL, NULL,
1084
                                                   abs_top_builddir "/src",
1085 1086
                                                   SBINDIR,
                                                   "LIBVIRTD_PATH")))
1087
            goto failed;
1088

1089 1090
        if (!(priv->client = virNetClientNewUNIX(sockname,
                                                 flags & VIR_DRV_OPEN_REMOTE_AUTOSTART,
1091
                                                 daemonPath)))
C
Chris Lalancette 已提交
1092
            goto failed;
1093

1094
        priv->is_secure = 1;
1095 1096
        break;

1097
    case trans_ssh:
1098 1099
        if (!command && VIR_STRDUP(command, "ssh") < 0)
            goto failed;
J
Jim Meyering 已提交
1100

1101 1102
        if (!sockname) {
            /* Right now we don't support default session connections */
1103
            if (flags & VIR_DRV_OPEN_REMOTE_USER) {
1104 1105 1106 1107 1108 1109 1110
                virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                               _("Connecting to session instance without "
                                 "socket path is not supported by the ssh "
                                 "connection driver"));
                goto failed;
            }

1111
            if (!(sockname = remoteGetUNIXSocketRoot(flags)))
1112 1113
                goto failed;
        }
1114 1115 1116 1117 1118

        if (!(priv->client = virNetClientNewSSH(priv->hostname,
                                                port,
                                                command,
                                                username,
1119 1120
                                                !tty,
                                                !verify,
1121
                                                netcat ? netcat : "nc",
1122
                                                keyfile,
1123 1124
                                                sockname)))
            goto failed;
1125 1126

        priv->is_secure = 1;
1127
        break;
1128 1129

    case trans_ext: {
1130 1131
        char const *cmd_argv[] = { command, NULL };
        if (!(priv->client = virNetClientNewExternal(cmd_argv)))
1132 1133
            goto failed;

E
Eric Blake 已提交
1134
        /* Do not set 'is_secure' flag since we can't guarantee
1135 1136
         * an external program is secure, and this flag must be
         * pessimistic */
1137 1138
    }   break;

1139 1140 1141 1142 1143
#else /* WIN32 */

    case trans_unix:
    case trans_ssh:
    case trans_ext:
1144 1145 1146
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("transport methods unix, ssh and ext are not supported "
                         "under Windows"));
1147
        goto failed;
1148 1149

#endif /* WIN32 */
1150 1151
    } /* switch (transport) */

1152

1153 1154 1155 1156 1157 1158 1159 1160 1161
    if (virNetClientRegisterAsyncIO(priv->client) < 0) {
        VIR_DEBUG("Failed to add event watch, disabling events and support for"
                  " keepalive messages");
        virResetLastError();
    } else {
        if (virNetClientRegisterKeepAlive(priv->client) < 0)
            goto failed;
    }

1162 1163
    if (!(priv->closeCallback = virNewConnectCloseCallbackData()))
        goto failed;
M
Michal Privoznik 已提交
1164
    /* ref on behalf of netclient */
1165
    virObjectRef(priv->closeCallback);
1166 1167
    virNetClientSetCloseCallback(priv->client,
                                 remoteClientCloseFunc,
1168
                                 priv->closeCallback, virObjectFreeCallback);
1169

1170 1171
    if (!(priv->remoteProgram = virNetClientProgramNew(REMOTE_PROGRAM,
                                                       REMOTE_PROTOCOL_VERSION,
1172 1173
                                                       remoteEvents,
                                                       ARRAY_CARDINALITY(remoteEvents),
1174
                                                       conn)))
1175
        goto failed;
1176 1177 1178 1179 1180 1181
    if (!(priv->lxcProgram = virNetClientProgramNew(LXC_PROGRAM,
                                                    LXC_PROTOCOL_VERSION,
                                                    NULL,
                                                    0,
                                                    NULL)))
        goto failed;
1182 1183
    if (!(priv->qemuProgram = virNetClientProgramNew(QEMU_PROGRAM,
                                                     QEMU_PROTOCOL_VERSION,
1184 1185 1186
                                                     qemuEvents,
                                                     ARRAY_CARDINALITY(qemuEvents),
                                                     conn)))
1187 1188
        goto failed;

1189
    if (virNetClientAddProgram(priv->client, priv->remoteProgram) < 0 ||
1190
        virNetClientAddProgram(priv->client, priv->lxcProgram) < 0 ||
1191
        virNetClientAddProgram(priv->client, priv->qemuProgram) < 0)
1192
        goto failed;
1193 1194

    /* Try and authenticate with server */
1195 1196
    VIR_DEBUG("Trying authentication");
    if (remoteAuthenticate(conn, priv, auth, authtype) == -1)
1197 1198
        goto failed;

1199
    if (virNetClientKeepAliveIsSupported(priv->client)) {
1200 1201 1202
        priv->serverKeepAlive = remoteConnectSupportsFeatureUnlocked(conn,
                                    priv, VIR_DRV_FEATURE_PROGRAM_KEEPALIVE);
        if (!priv->serverKeepAlive) {
1203 1204 1205 1206 1207
            VIR_INFO("Disabling keepalive protocol since it is not supported"
                     " by the server");
        }
    }

1208
    /* Finally we can call the remote side's open function. */
1209
    {
1210
        remote_connect_open_args args = { &name, flags };
1211

1212
        VIR_DEBUG("Trying to open URI %s", name);
1213 1214
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_OPEN,
                 (xdrproc_t) xdr_remote_connect_open_args, (char *) &args,
1215
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
1216 1217
            goto failed;
    }
1218

1219 1220
    /* Now try and find out what URI the daemon used */
    if (conn->uri == NULL) {
1221
        remote_connect_get_uri_ret uriret;
1222

1223
        VIR_DEBUG("Trying to query remote URI");
1224 1225
        memset(&uriret, 0, sizeof(uriret));
        if (call(conn, priv, 0,
1226
                 REMOTE_PROC_CONNECT_GET_URI,
1227
                 (xdrproc_t) xdr_void, (char *) NULL,
1228
                 (xdrproc_t) xdr_remote_connect_get_uri_ret, (char *) &uriret) < 0)
1229 1230
            goto failed;

1231
        VIR_DEBUG("Auto-probed URI is %s", uriret.uri);
M
Martin Kletzander 已提交
1232
        conn->uri = virURIParse(uriret.uri);
1233
        VIR_FREE(uriret.uri);
1234
        if (!conn->uri)
1235 1236 1237
            goto failed;
    }

1238
    /* Set up events */
1239
    if (!(priv->eventState = virObjectEventStateNew()))
1240
        goto failed;
1241

1242 1243 1244 1245 1246
    priv->serverEventFilter = remoteConnectSupportsFeatureUnlocked(conn,
                                priv, VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK);
    if (!priv->serverEventFilter) {
        VIR_INFO("Avoiding server event filtering since it is not "
                 "supported by the server");
1247
    }
1248

1249 1250 1251 1252 1253 1254 1255
    priv->serverCloseCallback = remoteConnectSupportsFeatureUnlocked(conn,
                                    priv, VIR_DRV_FEATURE_REMOTE_CLOSE_CALLBACK);
    if (!priv->serverCloseCallback) {
        VIR_INFO("Close callback registering isn't supported "
                 "by the remote side.");
    }

1256 1257 1258
    /* Successful. */
    retcode = VIR_DRV_OPEN_SUCCESS;

1259
 cleanup:
1260
    /* Free up the URL and strings. */
1261 1262 1263 1264 1265
    VIR_FREE(name);
    VIR_FREE(command);
    VIR_FREE(sockname);
    VIR_FREE(authtype);
    VIR_FREE(netcat);
1266
    VIR_FREE(sshauth);
1267
    VIR_FREE(keyfile);
1268 1269
    VIR_FREE(username);
    VIR_FREE(port);
1270
    VIR_FREE(pkipath);
1271
    VIR_FREE(tls_priority);
1272 1273
    VIR_FREE(knownHostsVerify);
    VIR_FREE(knownHosts);
1274 1275 1276
#ifndef WIN32
    VIR_FREE(daemonPath);
#endif
1277 1278

    return retcode;
1279 1280

 failed:
1281
    virObjectUnref(priv->remoteProgram);
1282
    virObjectUnref(priv->lxcProgram);
1283
    virObjectUnref(priv->qemuProgram);
1284
    virNetClientClose(priv->client);
1285
    virObjectUnref(priv->client);
1286
    priv->client = NULL;
1287 1288
    virObjectUnref(priv->closeCallback);
    priv->closeCallback = NULL;
1289 1290 1291 1292
#ifdef WITH_GNUTLS
    virObjectUnref(priv->tls);
    priv->tls = NULL;
#endif
1293

1294
    VIR_FREE(priv->hostname);
1295
    goto cleanup;
1296
}
1297 1298
#undef EXTRACT_URI_ARG_STR
#undef EXTRACT_URI_ARG_BOOL
1299

1300
static struct private_data *
1301
remoteAllocPrivateData(void)
1302
{
1303
    struct private_data *priv;
1304
    if (VIR_ALLOC(priv) < 0)
1305
        return NULL;
1306

1307
    if (virMutexInit(&priv->lock) < 0) {
1308 1309
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot initialize mutex"));
1310
        VIR_FREE(priv);
1311
        return NULL;
1312 1313 1314
    }
    remoteDriverLock(priv);
    priv->localUses = 1;
1315 1316 1317 1318 1319

    return priv;
}

static virDrvOpenStatus
1320 1321
remoteConnectOpen(virConnectPtr conn,
                  virConnectAuthPtr auth,
1322
                  virConfPtr conf,
1323
                  unsigned int flags)
1324 1325
{
    struct private_data *priv;
1326 1327
    int ret = VIR_DRV_OPEN_ERROR;
    int rflags = 0;
1328
    const char *autostart = virGetEnvBlockSUID("LIBVIRT_AUTOSTART");
1329 1330 1331 1332 1333 1334
    char *driver = NULL;
    char *transport = NULL;

    if (conn->uri &&
        remoteSplitURIScheme(conn->uri, &driver, &transport) < 0)
        goto cleanup;
1335

1336 1337 1338 1339
    if (inside_daemon && (!conn->uri || !conn->uri->server)) {
        ret = VIR_DRV_OPEN_DECLINED;
        goto cleanup;
    }
1340

1341
    if (!(priv = remoteAllocPrivateData()))
1342
        goto cleanup;
1343

1344
    if (flags & VIR_CONNECT_RO)
1345 1346
        rflags |= VIR_DRV_OPEN_REMOTE_RO;

1347
    /*
1348 1349 1350 1351 1352 1353
     * User session daemon is used for
     *
     *  - Any URI with /session suffix
     *  - Test driver, if a protocol is given
     *
     * provided we are running non-root
1354 1355 1356
     */
    if (conn->uri &&
        conn->uri->path &&
D
Daniel P. Berrange 已提交
1357
        conn->uri->scheme &&
1358 1359
        (STREQ(conn->uri->path, "/session") ||
         STRPREFIX(conn->uri->scheme, "test+")) &&
1360
        geteuid() > 0) {
1361
        VIR_DEBUG("User session daemon required");
1362
        rflags |= VIR_DRV_OPEN_REMOTE_USER;
1363 1364 1365 1366 1367 1368

        /*
         * Furthermore if no servername is given, and no +XXX
         * transport is listed, or transport is unix,
         * and uid is unprivileged then auto-spawn a daemon.
         */
1369
        if (!virIsSUID() &&
1370 1371
            !conn->uri->server &&
            (transport == NULL || STREQ(transport, "unix")) &&
1372
            (!autostart ||
1373 1374
             STRNEQ(autostart, "0"))) {
            VIR_DEBUG("Try daemon autostart");
1375
            rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1376
        }
1377 1378 1379
    }

    /*
J
John Levon 已提交
1380
     * If URI is NULL, then do a UNIX connection possibly auto-spawning
1381
     * unprivileged server and probe remote server for URI.
1382 1383
     */
    if (!conn->uri) {
1384
        VIR_DEBUG("Auto-probe remote URI");
1385
        if (geteuid() > 0) {
1386
            VIR_DEBUG("Auto-spawn user daemon instance");
1387
            rflags |= VIR_DRV_OPEN_REMOTE_USER;
1388 1389 1390
            if (!virIsSUID() &&
                (!autostart ||
                 STRNEQ(autostart, "0")))
1391
                rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1392
        }
1393
    }
1394

1395
    ret = doRemoteOpen(conn, priv, driver, transport, auth, conf, rflags);
1396 1397
    if (ret != VIR_DRV_OPEN_SUCCESS) {
        conn->privateData = NULL;
1398
        remoteDriverUnlock(priv);
1399
        VIR_FREE(priv);
1400 1401
    } else {
        conn->privateData = priv;
1402
        remoteDriverUnlock(priv);
1403
    }
1404 1405 1406 1407

 cleanup:
    VIR_FREE(driver);
    VIR_FREE(transport);
1408 1409 1410 1411
    return ret;
}


1412
/*----------------------------------------------------------------------*/
1413

1414 1415

static int
1416
doRemoteClose(virConnectPtr conn, struct private_data *priv)
1417
{
1418 1419
    int ret = 0;

1420
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_CLOSE,
1421 1422
             (xdrproc_t) xdr_void, (char *) NULL,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
1423
        ret = -1;
1424

1425
#ifdef WITH_GNUTLS
1426
    virObjectUnref(priv->tls);
1427
    priv->tls = NULL;
1428
#endif
1429

1430 1431
    virNetClientSetCloseCallback(priv->client,
                                 NULL,
1432
                                 priv->closeCallback, virObjectFreeCallback);
1433

1434
    virNetClientClose(priv->client);
1435
    virObjectUnref(priv->client);
1436
    priv->client = NULL;
1437 1438
    virObjectUnref(priv->closeCallback);
    priv->closeCallback = NULL;
1439
    virObjectUnref(priv->remoteProgram);
1440
    virObjectUnref(priv->lxcProgram);
1441
    virObjectUnref(priv->qemuProgram);
1442
    priv->remoteProgram = priv->qemuProgram = priv->lxcProgram = NULL;
1443 1444 1445 1446 1447 1448 1449

    /* Free hostname copy */
    VIR_FREE(priv->hostname);

    /* See comment for remoteType. */
    VIR_FREE(priv->type);

1450
    virObjectUnref(priv->eventState);
1451
    priv->eventState = NULL;
1452

1453
    return ret;
1454 1455
}

1456
static int
1457
remoteConnectClose(virConnectPtr conn)
1458
{
1459 1460
    int ret = 0;
    struct private_data *priv = conn->privateData;
1461

1462 1463 1464 1465 1466 1467 1468
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        ret = doRemoteClose(conn, priv);
        conn->privateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
1469
        VIR_FREE(priv);
1470
    }
1471 1472
    if (priv)
        remoteDriverUnlock(priv);
1473

1474 1475
    return ret;
}
1476

1477

1478 1479 1480 1481 1482 1483 1484 1485 1486
/* Unfortunately this function is defined to return a static string.
 * Since the remote end always answers with the same type (for a
 * single connection anyway) we cache the type in the connection's
 * private data, and free it when we close the connection.
 *
 * See also:
 * http://www.redhat.com/archives/libvir-list/2007-February/msg00096.html
 */
static const char *
1487
remoteConnectGetType(virConnectPtr conn)
1488 1489
{
    char *rv = NULL;
1490
    remote_connect_get_type_ret ret;
1491
    struct private_data *priv = conn->privateData;
1492

1493
    remoteDriverLock(priv);
1494

1495 1496 1497 1498 1499
    /* Cached? */
    if (priv->type) {
        rv = priv->type;
        goto done;
    }
1500

1501
    memset(&ret, 0, sizeof(ret));
1502
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_GET_TYPE,
1503
             (xdrproc_t) xdr_void, (char *) NULL,
1504
             (xdrproc_t) xdr_remote_connect_get_type_ret, (char *) &ret) == -1)
1505
        goto done;
1506

1507 1508
    /* Stash. */
    rv = priv->type = ret.type;
1509

1510
 done:
1511 1512 1513
    remoteDriverUnlock(priv);
    return rv;
}
1514

1515
static int remoteConnectIsSecure(virConnectPtr conn)
1516 1517 1518
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
1519
    remote_connect_is_secure_ret ret;
1520
    remoteDriverLock(priv);
1521

1522
    memset(&ret, 0, sizeof(ret));
1523
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_IS_SECURE,
1524
             (xdrproc_t) xdr_void, (char *) NULL,
1525
             (xdrproc_t) xdr_remote_connect_is_secure_ret, (char *) &ret) == -1)
1526
        goto done;
1527

1528 1529 1530 1531 1532 1533 1534 1535 1536
    /* We claim to be secure, if the remote driver
     * transport itself is secure, and the remote
     * HV connection is secure
     *
     * ie, we don't want to claim to be secure if the
     * remote driver is used to connect to a XenD
     * driver using unencrypted HTTP:/// access
     */
    rv = priv->is_secure && ret.secure ? 1 : 0;
1537

1538
 done:
1539 1540 1541
    remoteDriverUnlock(priv);
    return rv;
}
1542

1543
static int remoteConnectIsEncrypted(virConnectPtr conn)
1544 1545
{
    int rv = -1;
1546
    bool encrypted;
1547
    struct private_data *priv = conn->privateData;
1548
    remote_connect_is_secure_ret ret;
1549
    remoteDriverLock(priv);
1550

1551
    memset(&ret, 0, sizeof(ret));
1552
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_IS_SECURE,
1553
             (xdrproc_t) xdr_void, (char *) NULL,
1554
             (xdrproc_t) xdr_remote_connect_is_secure_ret, (char *) &ret) == -1)
1555
        goto done;
1556

1557
    encrypted = virNetClientIsEncrypted(priv->client);
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568

    /* We claim to be encrypted, if the remote driver
     * transport itself is encrypted, and the remote
     * HV connection is secure.
     *
     * Yes, we really don't check the remote 'encrypted'
     * option, since it will almost always be false,
     * even if secure (eg UNIX sockets).
     */
    rv = encrypted && ret.secure ? 1 : 0;

1569
 done:
1570 1571 1572 1573
    remoteDriverUnlock(priv);
    return rv;
}

1574
static int
1575 1576 1577 1578
remoteNodeGetCPUStats(virConnectPtr conn,
                      int cpuNum,
                      virNodeCPUStatsPtr params, int *nparams,
                      unsigned int flags)
1579 1580 1581 1582
{
    int rv = -1;
    remote_node_get_cpu_stats_args args;
    remote_node_get_cpu_stats_ret ret;
1583
    size_t i;
1584 1585 1586 1587 1588 1589 1590 1591
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.nparams = *nparams;
    args.cpuNum = cpuNum;
    args.flags = flags;

1592 1593 1594 1595 1596 1597
    memset(&ret, 0, sizeof(ret));
    if (call(conn, priv, 0, REMOTE_PROC_NODE_GET_CPU_STATS,
             (xdrproc_t) xdr_remote_node_get_cpu_stats_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_node_get_cpu_stats_ret,
             (char *) &ret) == -1)
1598 1599 1600 1601 1602
        goto done;

    /* Check the length of the returned list carefully. */
    if (ret.params.params_len > REMOTE_NODE_CPU_STATS_MAX ||
        ret.params.params_len > *nparams) {
1603 1604 1605
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteNodeGetCPUStats: "
                         "returned number of stats exceeds limit"));
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
        goto cleanup;
    }
    /* Handle the case when the caller does not know the number of stats
     * and is asking for the number of stats supported
     */
    if (*nparams == 0) {
        *nparams = ret.nparams;
        rv = 0;
        goto cleanup;
    }

    *nparams = ret.params.params_len;

    /* Deserialise the result. */
    for (i = 0; i < *nparams; ++i) {
1621
        if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) < 0) {
1622 1623 1624
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Stats %s too big for destination"),
                           ret.params.params_val[i].field);
1625 1626 1627 1628 1629 1630 1631
            goto cleanup;
        }
        params[i].value = ret.params.params_val[i].value;
    }

    rv = 0;

1632
 cleanup:
1633
    xdr_free((xdrproc_t) xdr_remote_node_get_cpu_stats_ret, (char *) &ret);
1634
 done:
1635 1636 1637 1638
    remoteDriverUnlock(priv);
    return rv;
}

1639
static int
1640 1641 1642 1643 1644
remoteNodeGetMemoryStats(virConnectPtr conn,
                         int cellNum,
                         virNodeMemoryStatsPtr params,
                         int *nparams,
                         unsigned int flags)
1645 1646 1647 1648
{
    int rv = -1;
    remote_node_get_memory_stats_args args;
    remote_node_get_memory_stats_ret ret;
1649
    size_t i;
1650 1651 1652 1653 1654 1655 1656 1657
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.nparams = *nparams;
    args.cellNum = cellNum;
    args.flags = flags;

1658
    memset(&ret, 0, sizeof(ret));
1659 1660 1661
    if (call(conn, priv, 0, REMOTE_PROC_NODE_GET_MEMORY_STATS,
             (xdrproc_t) xdr_remote_node_get_memory_stats_args, (char *) &args,
             (xdrproc_t) xdr_remote_node_get_memory_stats_ret, (char *) &ret) == -1)
1662 1663 1664 1665 1666
        goto done;

    /* Check the length of the returned list carefully. */
    if (ret.params.params_len > REMOTE_NODE_MEMORY_STATS_MAX ||
        ret.params.params_len > *nparams) {
1667 1668 1669
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteNodeGetMemoryStats: "
                         "returned number of stats exceeds limit"));
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
        goto cleanup;
    }
    /* Handle the case when the caller does not know the number of stats
     * and is asking for the number of stats supported
     */
    if (*nparams == 0) {
        *nparams = ret.nparams;
        rv = 0;
        goto cleanup;
    }

    *nparams = ret.params.params_len;

    /* Deserialise the result. */
    for (i = 0; i < *nparams; ++i) {
1685
        if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) < 0) {
1686 1687 1688
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Stats %s too big for destination"),
                           ret.params.params_val[i].field);
1689 1690 1691 1692 1693 1694 1695
            goto cleanup;
        }
        params[i].value = ret.params.params_val[i].value;
    }

    rv = 0;

1696
 cleanup:
1697
    xdr_free((xdrproc_t) xdr_remote_node_get_memory_stats_ret, (char *) &ret);
1698
 done:
1699 1700 1701 1702
    remoteDriverUnlock(priv);
    return rv;
}

1703 1704
static int
remoteNodeGetCellsFreeMemory(virConnectPtr conn,
1705 1706 1707
                             unsigned long long *freeMems,
                             int startCell,
                             int maxCells)
1708
{
1709
    int rv = -1;
1710 1711
    remote_node_get_cells_free_memory_args args;
    remote_node_get_cells_free_memory_ret ret;
1712
    size_t i;
1713
    struct private_data *priv = conn->privateData;
1714

1715 1716
    remoteDriverLock(priv);

1717
    if (maxCells > REMOTE_NODE_MAX_CELLS) {
1718 1719 1720
        virReportError(VIR_ERR_RPC,
                       _("too many NUMA cells: %d > %d"),
                       maxCells, REMOTE_NODE_MAX_CELLS);
1721
        goto done;
1722 1723 1724
    }

    args.startCell = startCell;
1725
    args.maxcells = maxCells;
1726

1727 1728 1729 1730
    memset(&ret, 0, sizeof(ret));
    if (call(conn, priv, 0, REMOTE_PROC_NODE_GET_CELLS_FREE_MEMORY,
             (xdrproc_t) xdr_remote_node_get_cells_free_memory_args, (char *)&args,
             (xdrproc_t) xdr_remote_node_get_cells_free_memory_ret, (char *)&ret) == -1)
1731
        goto done;
1732

1733
    for (i = 0; i < ret.cells.cells_len; i++)
1734
        freeMems[i] = ret.cells.cells_val[i];
1735 1736 1737

    xdr_free((xdrproc_t) xdr_remote_node_get_cells_free_memory_ret, (char *) &ret);

1738
    rv = ret.cells.cells_len;
1739

1740
 done:
1741
    remoteDriverUnlock(priv);
1742
    return rv;
1743 1744
}

1745
static int
1746
remoteConnectListDomains(virConnectPtr conn, int *ids, int maxids)
1747
{
1748
    int rv = -1;
1749
    size_t i;
1750 1751
    remote_connect_list_domains_args args;
    remote_connect_list_domains_ret ret;
1752
    struct private_data *priv = conn->privateData;
1753

1754 1755
    remoteDriverLock(priv);

1756
    if (maxids > REMOTE_DOMAIN_LIST_MAX) {
1757
        virReportError(VIR_ERR_RPC,
1758 1759
                       _("Too many domains '%d' for limit '%d'"),
                       maxids, REMOTE_DOMAIN_LIST_MAX);
1760
        goto done;
1761 1762 1763
    }
    args.maxids = maxids;

1764
    memset(&ret, 0, sizeof(ret));
1765 1766 1767
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_LIST_DOMAINS,
             (xdrproc_t) xdr_remote_connect_list_domains_args, (char *) &args,
             (xdrproc_t) xdr_remote_connect_list_domains_ret, (char *) &ret) == -1)
1768
        goto done;
1769 1770

    if (ret.ids.ids_len > maxids) {
1771
        virReportError(VIR_ERR_RPC,
1772
                       _("Too many domains '%d' for limit '%d'"),
1773
                       ret.ids.ids_len, maxids);
1774
        goto cleanup;
1775 1776 1777 1778 1779
    }

    for (i = 0; i < ret.ids.ids_len; ++i)
        ids[i] = ret.ids.ids_val[i];

1780 1781
    rv = ret.ids.ids_len;

1782
 cleanup:
1783
    xdr_free((xdrproc_t) xdr_remote_connect_list_domains_ret, (char *) &ret);
1784

1785
 done:
1786
    remoteDriverUnlock(priv);
1787
    return rv;
1788 1789
}

1790 1791 1792 1793 1794 1795 1796
static int
remoteDeserializeDomainDiskErrors(remote_domain_disk_error *ret_errors_val,
                                  u_int ret_errors_len,
                                  int limit,
                                  virDomainDiskErrorPtr errors,
                                  int maxerrors)
{
1797 1798
    size_t i = 0;
    size_t j;
1799 1800

    if (ret_errors_len > limit || ret_errors_len > maxerrors) {
1801 1802
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned number of disk errors exceeds limit"));
1803 1804 1805 1806
        goto error;
    }

    for (i = 0; i < ret_errors_len; i++) {
1807
        if (VIR_STRDUP(errors[i].disk, ret_errors_val[i].disk) < 0)
1808 1809 1810 1811 1812 1813
            goto error;
        errors[i].error = ret_errors_val[i].error;
    }

    return 0;

1814
 error:
1815 1816 1817 1818 1819 1820
    for (j = 0; j < i; j++)
        VIR_FREE(errors[i].disk);

    return -1;
}

1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
static int
remoteDomainBlockStatsFlags(virDomainPtr domain,
                            const char *path,
                            virTypedParameterPtr params,
                            int *nparams,
                            unsigned int flags)
{
    int rv = -1;
    remote_domain_block_stats_flags_args args;
    remote_domain_block_stats_flags_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

1835
    make_nonnull_domain(&args.dom, domain);
1836 1837 1838 1839
    args.nparams = *nparams;
    args.path = (char *) path;
    args.flags = flags;

1840 1841 1842 1843
    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_BLOCK_STATS_FLAGS,
             (xdrproc_t) xdr_remote_domain_block_stats_flags_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_block_stats_flags_ret, (char *) &ret) == -1)
1844 1845 1846 1847 1848
        goto done;

    /* Check the length of the returned list carefully. */
    if (ret.params.params_len > REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX ||
        ret.params.params_len > *nparams) {
1849 1850 1851
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteDomainBlockStatsFlags: "
                         "returned number of stats exceeds limit"));
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
        goto cleanup;
    }

    /* Handle the case when the caller does not know the number of stats
     * and is asking for the number of stats supported
     */
    if (*nparams == 0) {
        *nparams = ret.nparams;
        rv = 0;
        goto cleanup;
    }

    *nparams = ret.params.params_len;

1866 1867 1868
    /* Deserialize the result. */
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
1869
                                  REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX,
1870 1871
                                  &params,
                                  nparams) < 0)
1872 1873 1874 1875
        goto cleanup;

    rv = 0;

1876
 cleanup:
1877 1878
    xdr_free((xdrproc_t) xdr_remote_domain_block_stats_flags_ret,
             (char *) &ret);
1879
 done:
1880 1881 1882 1883
    remoteDriverUnlock(priv);
    return rv;
}

1884
static int
1885 1886 1887
remoteDomainGetMemoryParameters(virDomainPtr domain,
                                virTypedParameterPtr params, int *nparams,
                                unsigned int flags)
1888 1889 1890 1891 1892 1893 1894 1895
{
    int rv = -1;
    remote_domain_get_memory_parameters_args args;
    remote_domain_get_memory_parameters_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

1896
    make_nonnull_domain(&args.dom, domain);
1897 1898 1899
    args.nparams = *nparams;
    args.flags = flags;

1900 1901 1902 1903
    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_MEMORY_PARAMETERS,
             (xdrproc_t) xdr_remote_domain_get_memory_parameters_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_memory_parameters_ret, (char *) &ret) == -1)
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
        goto done;

    /* Handle the case when the caller does not know the number of parameters
     * and is asking for the number of parameters supported
     */
    if (*nparams == 0) {
        *nparams = ret.nparams;
        rv = 0;
        goto cleanup;
    }

1915 1916 1917 1918 1919
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX,
                                  &params,
                                  nparams) < 0)
1920
        goto cleanup;
1921 1922 1923

    rv = 0;

1924
 cleanup:
1925 1926
    xdr_free((xdrproc_t) xdr_remote_domain_get_memory_parameters_ret,
             (char *) &ret);
1927
 done:
1928 1929 1930 1931
    remoteDriverUnlock(priv);
    return rv;
}

1932
static int
1933 1934 1935
remoteDomainGetNumaParameters(virDomainPtr domain,
                              virTypedParameterPtr params, int *nparams,
                              unsigned int flags)
1936 1937 1938 1939 1940 1941 1942 1943
{
    int rv = -1;
    remote_domain_get_numa_parameters_args args;
    remote_domain_get_numa_parameters_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

1944
    make_nonnull_domain(&args.dom, domain);
1945 1946 1947
    args.nparams = *nparams;
    args.flags = flags;

1948 1949 1950 1951
    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_NUMA_PARAMETERS,
             (xdrproc_t) xdr_remote_domain_get_numa_parameters_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_numa_parameters_ret, (char *) &ret) == -1)
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
        goto done;

    /* Handle the case when the caller does not know the number of parameters
     * and is asking for the number of parameters supported
     */
    if (*nparams == 0) {
        *nparams = ret.nparams;
        rv = 0;
        goto cleanup;
    }

1963 1964 1965 1966 1967
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_NUMA_PARAMETERS_MAX,
                                  &params,
                                  nparams) < 0)
1968 1969 1970 1971
        goto cleanup;

    rv = 0;

1972
 cleanup:
1973 1974
    xdr_free((xdrproc_t) xdr_remote_domain_get_numa_parameters_ret,
             (char *) &ret);
1975
 done:
1976 1977 1978 1979
    remoteDriverUnlock(priv);
    return rv;
}

1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
static int
remoteDomainGetLaunchSecurityInfo(virDomainPtr domain,
                                  virTypedParameterPtr *params,
                                  int *nparams,
                                  unsigned int flags)
{
    int rv = -1;
    remote_domain_get_launch_security_info_args args;
    remote_domain_get_launch_security_info_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, domain);
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_LAUNCH_SECURITY_INFO,
             (xdrproc_t) xdr_remote_domain_get_launch_security_info_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_launch_security_info_ret, (char *) &ret) == -1)
        goto done;

    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_LAUNCH_SECURITY_INFO_PARAMS_MAX,
                                  params,
                                  nparams) < 0)
        goto cleanup;

    rv = 0;

 cleanup:
    xdr_free((xdrproc_t) xdr_remote_domain_get_launch_security_info_ret,
             (char *) &ret);
 done:
    remoteDriverUnlock(priv);
    return rv;
}

2019 2020 2021
static int
remoteDomainGetPerfEvents(virDomainPtr domain,
                          virTypedParameterPtr *params,
2022 2023
                          int *nparams,
                          unsigned int flags)
2024 2025 2026 2027 2028 2029 2030 2031 2032
{
    int rv = -1;
    remote_domain_get_perf_events_args args;
    remote_domain_get_perf_events_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, domain);
2033
    args.flags = flags;
2034 2035 2036 2037 2038 2039 2040 2041

    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_PERF_EVENTS,
             (xdrproc_t) xdr_remote_domain_get_perf_events_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_perf_events_ret, (char *) &ret) == -1)
        goto done;

    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
2042 2043 2044 2045
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_PERF_EVENTS_MAX,
                                  params,
                                  nparams) < 0)
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
        goto cleanup;

    rv = 0;

 cleanup:
    xdr_free((xdrproc_t) xdr_remote_domain_get_perf_events_ret,
             (char *) &ret);
 done:
    remoteDriverUnlock(priv);
    return rv;
}

2058
static int
2059 2060 2061
remoteDomainGetBlkioParameters(virDomainPtr domain,
                               virTypedParameterPtr params, int *nparams,
                               unsigned int flags)
2062 2063 2064 2065 2066 2067 2068 2069
{
    int rv = -1;
    remote_domain_get_blkio_parameters_args args;
    remote_domain_get_blkio_parameters_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

2070
    make_nonnull_domain(&args.dom, domain);
2071 2072 2073
    args.nparams = *nparams;
    args.flags = flags;

2074 2075 2076 2077
    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_BLKIO_PARAMETERS,
             (xdrproc_t) xdr_remote_domain_get_blkio_parameters_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_blkio_parameters_ret, (char *) &ret) == -1)
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
        goto done;

    /* Handle the case when the caller does not know the number of parameters
     * and is asking for the number of parameters supported
     */
    if (*nparams == 0) {
        *nparams = ret.nparams;
        rv = 0;
        goto cleanup;
    }

2089 2090 2091 2092 2093
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX,
                                  &params,
                                  nparams) < 0)
2094
        goto cleanup;
2095 2096 2097

    rv = 0;

2098
 cleanup:
2099 2100
    xdr_free((xdrproc_t) xdr_remote_domain_get_blkio_parameters_ret,
             (char *) &ret);
2101
 done:
2102 2103 2104 2105
    remoteDriverUnlock(priv);
    return rv;
}

2106
static int
2107 2108 2109 2110 2111
remoteDomainGetVcpuPinInfo(virDomainPtr domain,
                           int ncpumaps,
                           unsigned char *cpumaps,
                           int maplen,
                           unsigned int flags)
2112 2113
{
    int rv = -1;
2114
    size_t i;
E
Eric Blake 已提交
2115 2116
    remote_domain_get_vcpu_pin_info_args args;
    remote_domain_get_vcpu_pin_info_ret ret;
2117 2118 2119 2120 2121
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    if (ncpumaps > REMOTE_VCPUINFO_MAX) {
2122 2123 2124
        virReportError(VIR_ERR_RPC,
                       _("vCPU count exceeds maximum: %d > %d"),
                       ncpumaps, REMOTE_VCPUINFO_MAX);
2125 2126 2127 2128 2129
        goto done;
    }

    if (INT_MULTIPLY_OVERFLOW(ncpumaps, maplen) ||
        ncpumaps * maplen > REMOTE_CPUMAPS_MAX) {
2130 2131 2132
        virReportError(VIR_ERR_RPC,
                       _("vCPU map buffer length exceeds maximum: %d > %d"),
                       ncpumaps * maplen, REMOTE_CPUMAPS_MAX);
2133 2134 2135
        goto done;
    }

2136
    make_nonnull_domain(&args.dom, domain);
2137 2138 2139 2140
    args.ncpumaps = ncpumaps;
    args.maplen = maplen;
    args.flags = flags;

2141
    memset(&ret, 0, sizeof(ret));
2142

2143 2144 2145 2146 2147
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_VCPU_PIN_INFO,
             (xdrproc_t) xdr_remote_domain_get_vcpu_pin_info_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_vcpu_pin_info_ret,
             (char *) &ret) == -1)
2148 2149 2150
        goto done;

    if (ret.num > ncpumaps) {
2151 2152 2153
        virReportError(VIR_ERR_RPC,
                       _("host reports too many vCPUs: %d > %d"),
                       ret.num, ncpumaps);
2154 2155 2156 2157
        goto cleanup;
    }

    if (ret.cpumaps.cpumaps_len > ncpumaps * maplen) {
2158 2159 2160
        virReportError(VIR_ERR_RPC,
                       _("host reports map buffer length exceeds maximum: %d > %d"),
                       ret.cpumaps.cpumaps_len, ncpumaps * maplen);
2161 2162 2163
        goto cleanup;
    }

2164
    memset(cpumaps, 0, ncpumaps * maplen);
2165 2166 2167 2168 2169 2170

    for (i = 0; i < ret.cpumaps.cpumaps_len; ++i)
        cpumaps[i] = ret.cpumaps.cpumaps_val[i];

    rv = ret.num;

2171
 cleanup:
2172
    xdr_free((xdrproc_t) xdr_remote_domain_get_vcpu_pin_info_ret, (char *) &ret);
2173

2174
 done:
2175 2176 2177 2178
    remoteDriverUnlock(priv);
    return rv;
}

2179
static int
2180 2181 2182 2183
remoteDomainPinEmulator(virDomainPtr dom,
                        unsigned char *cpumap,
                        int cpumaplen,
                        unsigned int flags)
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
{
    int rv = -1;
    struct private_data *priv = dom->conn->privateData;
    remote_domain_pin_emulator_args args;

    remoteDriverLock(priv);

    if (cpumaplen > REMOTE_CPUMAP_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("%s length greater than maximum: %d > %d"),
                       "cpumap", cpumaplen, REMOTE_CPUMAP_MAX);
        goto done;
    }

    make_nonnull_domain(&args.dom, dom);
    args.cpumap.cpumap_val = (char *)cpumap;
    args.cpumap.cpumap_len = cpumaplen;
    args.flags = flags;

    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_PIN_EMULATOR,
             (xdrproc_t) xdr_remote_domain_pin_emulator_args,
             (char *) &args,
             (xdrproc_t) xdr_void, (char *) NULL) == -1) {
        goto done;
    }

    rv = 0;

2212
 done:
2213 2214 2215 2216 2217 2218
    remoteDriverUnlock(priv);
    return rv;
}


static int
2219 2220 2221 2222
remoteDomainGetEmulatorPinInfo(virDomainPtr domain,
                               unsigned char *cpumaps,
                               int maplen,
                               unsigned int flags)
2223 2224
{
    int rv = -1;
2225
    size_t i;
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
    remote_domain_get_emulator_pin_info_args args;
    remote_domain_get_emulator_pin_info_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    /* There is only one cpumap for all emulator threads */
    if (maplen > REMOTE_CPUMAPS_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("vCPU map buffer length exceeds maximum: %d > %d"),
                       maplen, REMOTE_CPUMAPS_MAX);
        goto done;
    }

    make_nonnull_domain(&args.dom, domain);
    args.maplen = maplen;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));

2246 2247 2248 2249 2250
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_EMULATOR_PIN_INFO,
             (xdrproc_t) xdr_remote_domain_get_emulator_pin_info_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_emulator_pin_info_ret,
             (char *) &ret) == -1)
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
        goto done;

    if (ret.cpumaps.cpumaps_len > maplen) {
        virReportError(VIR_ERR_RPC,
                       _("host reports map buffer length exceeds maximum: %d > %d"),
                       ret.cpumaps.cpumaps_len, maplen);
        goto cleanup;
    }

    memset(cpumaps, 0, maplen);

    for (i = 0; i < ret.cpumaps.cpumaps_len; ++i)
        cpumaps[i] = ret.cpumaps.cpumaps_val[i];

    rv = ret.ret;

2267
 cleanup:
2268 2269
    xdr_free((xdrproc_t) xdr_remote_domain_get_emulator_pin_info_ret,
             (char *) &ret);
2270

2271
 done:
2272 2273 2274 2275
    remoteDriverUnlock(priv);
    return rv;
}

2276
static int
2277 2278 2279 2280 2281
remoteDomainGetVcpus(virDomainPtr domain,
                     virVcpuInfoPtr info,
                     int maxinfo,
                     unsigned char *cpumaps,
                     int maplen)
2282
{
2283
    int rv = -1;
2284
    size_t i;
2285 2286
    remote_domain_get_vcpus_args args;
    remote_domain_get_vcpus_ret ret;
2287
    struct private_data *priv = domain->conn->privateData;
2288

2289 2290
    remoteDriverLock(priv);

2291
    if (maxinfo > REMOTE_VCPUINFO_MAX) {
2292 2293 2294
        virReportError(VIR_ERR_RPC,
                       _("vCPU count exceeds maximum: %d > %d"),
                       maxinfo, REMOTE_VCPUINFO_MAX);
2295
        goto done;
2296
    }
2297 2298
    if (INT_MULTIPLY_OVERFLOW(maxinfo, maplen) ||
        maxinfo * maplen > REMOTE_CPUMAPS_MAX) {
2299 2300 2301
        virReportError(VIR_ERR_RPC,
                       _("vCPU map buffer length exceeds maximum: %d > %d"),
                       maxinfo * maplen, REMOTE_CPUMAPS_MAX);
2302
        goto done;
2303 2304
    }

2305
    make_nonnull_domain(&args.dom, domain);
2306 2307 2308
    args.maxinfo = maxinfo;
    args.maplen = maplen;

2309 2310 2311 2312
    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_VCPUS,
             (xdrproc_t) xdr_remote_domain_get_vcpus_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_vcpus_ret, (char *) &ret) == -1)
2313
        goto done;
2314 2315

    if (ret.info.info_len > maxinfo) {
2316 2317 2318
        virReportError(VIR_ERR_RPC,
                       _("host reports too many vCPUs: %d > %d"),
                       ret.info.info_len, maxinfo);
2319
        goto cleanup;
2320
    }
2321
    if (ret.cpumaps.cpumaps_len > maxinfo * maplen) {
2322 2323 2324
        virReportError(VIR_ERR_RPC,
                       _("host reports map buffer length exceeds maximum: %d > %d"),
                       ret.cpumaps.cpumaps_len, maxinfo * maplen);
2325
        goto cleanup;
2326 2327
    }

2328 2329
    memset(info, 0, sizeof(virVcpuInfo) * maxinfo);
    memset(cpumaps, 0, maxinfo * maplen);
2330

2331 2332 2333 2334 2335 2336 2337 2338 2339 2340
    for (i = 0; i < ret.info.info_len; ++i) {
        info[i].number = ret.info.info_val[i].number;
        info[i].state = ret.info.info_val[i].state;
        info[i].cpuTime = ret.info.info_val[i].cpu_time;
        info[i].cpu = ret.info.info_val[i].cpu;
    }

    for (i = 0; i < ret.cpumaps.cpumaps_len; ++i)
        cpumaps[i] = ret.cpumaps.cpumaps_val[i];

2341 2342
    rv = ret.info.info_len;

2343
 cleanup:
2344
    xdr_free((xdrproc_t) xdr_remote_domain_get_vcpus_ret, (char *) &ret);
2345

2346
 done:
2347
    remoteDriverUnlock(priv);
2348
    return rv;
2349 2350
}

2351
static int
2352 2353 2354
remoteDomainGetIOThreadInfo(virDomainPtr dom,
                            virDomainIOThreadInfoPtr **info,
                            unsigned int flags)
2355 2356 2357 2358
{
    int rv = -1;
    size_t i;
    struct private_data *priv = dom->conn->privateData;
2359 2360
    remote_domain_get_iothread_info_args args;
    remote_domain_get_iothread_info_ret ret;
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
    remote_domain_iothread_info *src;
    virDomainIOThreadInfoPtr *info_ret = NULL;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, dom);

    args.flags = flags;

    memset(&ret, 0, sizeof(ret));

2372 2373
    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_IOTHREAD_INFO,
             (xdrproc_t)xdr_remote_domain_get_iothread_info_args,
2374
             (char *)&args,
2375
             (xdrproc_t)xdr_remote_domain_get_iothread_info_ret,
2376 2377 2378
             (char *)&ret) == -1)
        goto done;

2379
    if (ret.info.info_len > REMOTE_IOTHREAD_INFO_MAX) {
2380 2381
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Too many IOThreads in info: %d for limit %d"),
2382
                       ret.info.info_len, REMOTE_IOTHREAD_INFO_MAX);
2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
        goto cleanup;
    }

    if (info) {
        if (!ret.info.info_len) {
            *info = NULL;
            rv = ret.ret;
            goto cleanup;
        }

        if (VIR_ALLOC_N(info_ret, ret.info.info_len) < 0)
            goto cleanup;

        for (i = 0; i < ret.info.info_len; i++) {
            src = &ret.info.info_val[i];

            if (VIR_ALLOC(info_ret[i]) < 0)
                goto cleanup;

            info_ret[i]->iothread_id = src->iothread_id;
            if (VIR_ALLOC_N(info_ret[i]->cpumap, src->cpumap.cpumap_len) < 0)
                goto cleanup;
            memcpy(info_ret[i]->cpumap, src->cpumap.cpumap_val,
                   src->cpumap.cpumap_len);
            info_ret[i]->cpumaplen = src->cpumap.cpumap_len;
        }
        *info = info_ret;
        info_ret = NULL;
    }

    rv = ret.ret;

 cleanup:
    if (info_ret) {
        for (i = 0; i < ret.info.info_len; i++)
2418
            virDomainIOThreadInfoFree(info_ret[i]);
2419 2420
        VIR_FREE(info_ret);
    }
2421
    xdr_free((xdrproc_t)xdr_remote_domain_get_iothread_info_ret,
2422 2423 2424 2425 2426 2427 2428
             (char *) &ret);

 done:
    remoteDriverUnlock(priv);
    return rv;
}

2429
static int
2430
remoteDomainGetSecurityLabel(virDomainPtr domain, virSecurityLabelPtr seclabel)
2431 2432 2433 2434
{
    remote_domain_get_security_label_args args;
    remote_domain_get_security_label_ret ret;
    struct private_data *priv = domain->conn->privateData;
2435 2436 2437
    int rv = -1;

    remoteDriverLock(priv);
2438

2439 2440 2441
    make_nonnull_domain(&args.dom, domain);
    memset(&ret, 0, sizeof(ret));
    memset(seclabel, 0, sizeof(*seclabel));
2442

2443 2444 2445
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_SECURITY_LABEL,
             (xdrproc_t) xdr_remote_domain_get_security_label_args, (char *)&args,
             (xdrproc_t) xdr_remote_domain_get_security_label_ret, (char *)&ret) == -1) {
2446
        goto done;
2447 2448 2449
    }

    if (ret.label.label_val != NULL) {
2450
        if (strlen(ret.label.label_val) >= sizeof(seclabel->label)) {
2451 2452
            virReportError(VIR_ERR_RPC, _("security label exceeds maximum: %zu"),
                           sizeof(seclabel->label) - 1);
A
Alex Jia 已提交
2453
            goto cleanup;
2454
        }
2455
        strcpy(seclabel->label, ret.label.label_val);
2456 2457 2458
        seclabel->enforcing = ret.enforcing;
    }

2459 2460
    rv = 0;

2461
 cleanup:
A
Alex Jia 已提交
2462 2463
    xdr_free((xdrproc_t) xdr_remote_domain_get_security_label_ret, (char *)&ret);

2464
 done:
2465 2466
    remoteDriverUnlock(priv);
    return rv;
2467 2468
}

M
Marcelo Cerri 已提交
2469
static int
2470
remoteDomainGetSecurityLabelList(virDomainPtr domain, virSecurityLabelPtr* seclabels)
M
Marcelo Cerri 已提交
2471 2472 2473 2474
{
    remote_domain_get_security_label_list_args args;
    remote_domain_get_security_label_list_ret ret;
    struct private_data *priv = domain->conn->privateData;
2475 2476
    size_t i;
    int rv = -1;
M
Marcelo Cerri 已提交
2477 2478 2479

    remoteDriverLock(priv);

2480
    make_nonnull_domain(&args.dom, domain);
M
Marcelo Cerri 已提交
2481 2482
    memset(&ret, 0, sizeof(ret));

2483 2484 2485
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_SECURITY_LABEL_LIST,
             (xdrproc_t) xdr_remote_domain_get_security_label_list_args, (char *)&args,
             (xdrproc_t) xdr_remote_domain_get_security_label_list_ret, (char *)&ret) == -1) {
M
Marcelo Cerri 已提交
2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
        goto done;
    }

    if (VIR_ALLOC_N(*seclabels, ret.labels.labels_len) < 0)
        goto cleanup;

    for (i = 0; i < ret.labels.labels_len; i++) {
        remote_domain_get_security_label_ret *cur = &ret.labels.labels_val[i];
        if (cur->label.label_val != NULL) {
            if (strlen(cur->label.label_val) >= sizeof((*seclabels)->label)) {
                virReportError(VIR_ERR_RPC, _("security label exceeds maximum: %zd"),
                               sizeof((*seclabels)->label) - 1);
                VIR_FREE(*seclabels);
                goto cleanup;
            }
            strcpy((*seclabels)[i].label, cur->label.label_val);
            (*seclabels)[i].enforcing = cur->enforcing;
        }
    }
    rv = ret.ret;

2507
 cleanup:
M
Marcelo Cerri 已提交
2508 2509
    xdr_free((xdrproc_t) xdr_remote_domain_get_security_label_list_ret, (char *)&ret);

2510
 done:
M
Marcelo Cerri 已提交
2511 2512 2513 2514
    remoteDriverUnlock(priv);
    return rv;
}

2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
static int
remoteDomainGetState(virDomainPtr domain,
                     int *state,
                     int *reason,
                     unsigned int flags)
{
    int rv = -1;
    remote_domain_get_state_args args;
    remote_domain_get_state_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, domain);
    args.flags = flags;

2531
    memset(&ret, 0, sizeof(ret));
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_STATE,
             (xdrproc_t) xdr_remote_domain_get_state_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_state_ret, (char *) &ret) == -1)
        goto done;

    *state = ret.state;
    if (reason)
        *reason = ret.reason;

    rv = 0;

2543
 done:
2544 2545 2546 2547
    remoteDriverUnlock(priv);
    return rv;
}

2548
static int
2549
remoteNodeGetSecurityModel(virConnectPtr conn, virSecurityModelPtr secmodel)
2550 2551 2552
{
    remote_node_get_security_model_ret ret;
    struct private_data *priv = conn->privateData;
2553 2554 2555
    int rv = -1;

    remoteDriverLock(priv);
2556

2557 2558
    memset(&ret, 0, sizeof(ret));
    memset(secmodel, 0, sizeof(*secmodel));
2559

2560 2561 2562
    if (call(conn, priv, 0, REMOTE_PROC_NODE_GET_SECURITY_MODEL,
             (xdrproc_t) xdr_void, NULL,
             (xdrproc_t) xdr_remote_node_get_security_model_ret, (char *)&ret) == -1) {
2563
        goto done;
2564 2565 2566
    }

    if (ret.model.model_val != NULL) {
2567
        if (strlen(ret.model.model_val) >= sizeof(secmodel->model)) {
2568 2569
            virReportError(VIR_ERR_RPC, _("security model exceeds maximum: %zu"),
                           sizeof(secmodel->model) - 1);
A
Alex Jia 已提交
2570
            goto cleanup;
2571
        }
2572
        strcpy(secmodel->model, ret.model.model_val);
2573 2574 2575
    }

    if (ret.doi.doi_val != NULL) {
2576
        if (strlen(ret.doi.doi_val) >= sizeof(secmodel->doi)) {
2577 2578
            virReportError(VIR_ERR_RPC, _("security doi exceeds maximum: %zu"),
                           sizeof(secmodel->doi) - 1);
A
Alex Jia 已提交
2579
            goto cleanup;
2580
        }
2581
        strcpy(secmodel->doi, ret.doi.doi_val);
2582
    }
2583 2584 2585

    rv = 0;

2586
 cleanup:
A
Alex Jia 已提交
2587 2588
    xdr_free((xdrproc_t) xdr_remote_node_get_security_model_ret, (char *)&ret);

2589
 done:
2590 2591
    remoteDriverUnlock(priv);
    return rv;
2592 2593
}

2594
static int
2595 2596 2597 2598 2599
remoteDomainMigratePrepare(virConnectPtr dconn,
                           char **cookie, int *cookielen,
                           const char *uri_in, char **uri_out,
                           unsigned long flags, const char *dname,
                           unsigned long resource)
2600
{
2601
    int rv = -1;
2602 2603
    remote_domain_migrate_prepare_args args;
    remote_domain_migrate_prepare_ret ret;
2604
    struct private_data *priv = dconn->privateData;
2605

2606 2607
    remoteDriverLock(priv);

2608 2609 2610 2611 2612
    args.uri_in = uri_in == NULL ? NULL : (char **) &uri_in;
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;

2613 2614 2615 2616
    memset(&ret, 0, sizeof(ret));
    if (call(dconn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_PREPARE,
             (xdrproc_t) xdr_remote_domain_migrate_prepare_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_prepare_ret, (char *) &ret) == -1)
2617
        goto done;
2618 2619 2620 2621 2622 2623 2624 2625

    if (ret.cookie.cookie_len > 0) {
        *cookie = ret.cookie.cookie_val; /* Caller frees. */
        *cookielen = ret.cookie.cookie_len;
    }
    if (ret.uri_out)
        *uri_out = *ret.uri_out; /* Caller frees. */

O
Osier Yang 已提交
2626
    VIR_FREE(ret.uri_out);
2627 2628
    rv = 0;

2629
 done:
2630
    remoteDriverUnlock(priv);
2631
    return rv;
2632 2633
}

D
Daniel Veillard 已提交
2634
static int
2635 2636 2637 2638 2639 2640
remoteDomainMigratePrepare2(virConnectPtr dconn,
                            char **cookie, int *cookielen,
                            const char *uri_in, char **uri_out,
                            unsigned long flags, const char *dname,
                            unsigned long resource,
                            const char *dom_xml)
D
Daniel Veillard 已提交
2641
{
2642
    int rv = -1;
D
Daniel Veillard 已提交
2643 2644
    remote_domain_migrate_prepare2_args args;
    remote_domain_migrate_prepare2_ret ret;
2645
    struct private_data *priv = dconn->privateData;
D
Daniel Veillard 已提交
2646

2647 2648
    remoteDriverLock(priv);

D
Daniel Veillard 已提交
2649 2650 2651 2652 2653 2654
    args.uri_in = uri_in == NULL ? NULL : (char **) &uri_in;
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;
    args.dom_xml = (char *) dom_xml;

2655 2656 2657 2658
    memset(&ret, 0, sizeof(ret));
    if (call(dconn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_PREPARE2,
             (xdrproc_t) xdr_remote_domain_migrate_prepare2_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_prepare2_ret, (char *) &ret) == -1)
2659
        goto done;
D
Daniel Veillard 已提交
2660 2661

    if (ret.cookie.cookie_len > 0) {
2662
        if (!cookie || !cookielen) {
2663 2664
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookie or cookielen"));
2665 2666
            goto error;
        }
D
Daniel Veillard 已提交
2667 2668 2669
        *cookie = ret.cookie.cookie_val; /* Caller frees. */
        *cookielen = ret.cookie.cookie_len;
    }
2670 2671
    if (ret.uri_out) {
        if (!uri_out) {
2672 2673
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores uri_out"));
2674 2675
            goto error;
        }
D
Daniel Veillard 已提交
2676
        *uri_out = *ret.uri_out; /* Caller frees. */
2677
    }
D
Daniel Veillard 已提交
2678

2679 2680
    rv = 0;

2681
 done:
O
Osier Yang 已提交
2682
    VIR_FREE(ret.uri_out);
2683
    remoteDriverUnlock(priv);
2684
    return rv;
2685
 error:
2686 2687 2688 2689 2690
    if (ret.cookie.cookie_len)
        VIR_FREE(ret.cookie.cookie_val);
    if (ret.uri_out)
        VIR_FREE(*ret.uri_out);
    goto done;
D
Daniel Veillard 已提交
2691 2692
}

2693
static int
2694
remoteDomainCreate(virDomainPtr domain)
2695
{
2696
    int rv = -1;
2697
    remote_domain_create_args args;
2698 2699
    remote_domain_lookup_by_uuid_args args2;
    remote_domain_lookup_by_uuid_ret ret2;
2700
    struct private_data *priv = domain->conn->privateData;
2701

2702 2703
    remoteDriverLock(priv);

2704
    make_nonnull_domain(&args.dom, domain);
2705

2706 2707 2708
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_CREATE,
             (xdrproc_t) xdr_remote_domain_create_args, (char *) &args,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
2709
        goto done;
2710

2711 2712 2713 2714
    /* Need to do a lookup figure out ID of newly started guest, because
     * bug in design of REMOTE_PROC_DOMAIN_CREATE means we aren't getting
     * it returned.
     */
2715 2716 2717 2718 2719
    memcpy(args2.uuid, domain->uuid, VIR_UUID_BUFLEN);
    memset(&ret2, 0, sizeof(ret2));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_LOOKUP_BY_UUID,
             (xdrproc_t) xdr_remote_domain_lookup_by_uuid_args, (char *) &args2,
             (xdrproc_t) xdr_remote_domain_lookup_by_uuid_ret, (char *) &ret2) == -1)
2720 2721 2722
        goto done;

    domain->id = ret2.dom.id;
2723
    xdr_free((xdrproc_t) &xdr_remote_domain_lookup_by_uuid_ret, (char *) &ret2);
2724

2725 2726
    rv = 0;

2727
 done:
2728
    remoteDriverUnlock(priv);
2729
    return rv;
2730 2731
}

2732
static char *
2733
remoteDomainGetSchedulerType(virDomainPtr domain, int *nparams)
2734
{
2735 2736 2737
    char *rv = NULL;
    remote_domain_get_scheduler_type_args args;
    remote_domain_get_scheduler_type_ret ret;
2738
    struct private_data *priv = domain->conn->privateData;
2739

2740 2741
    remoteDriverLock(priv);

2742
    make_nonnull_domain(&args.dom, domain);
2743

2744 2745 2746 2747
    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_SCHEDULER_TYPE,
             (xdrproc_t) xdr_remote_domain_get_scheduler_type_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_scheduler_type_ret, (char *) &ret) == -1)
2748
        goto done;
2749 2750 2751 2752

    if (nparams) *nparams = ret.nparams;

    /* Caller frees this. */
2753 2754
    rv = ret.type;

2755
 done:
2756
    remoteDriverUnlock(priv);
2757
    return rv;
2758 2759
}

2760
static int
2761
remoteDomainMemoryStats(virDomainPtr domain,
2762
                        virDomainMemoryStatPtr stats,
2763 2764
                        unsigned int nr_stats,
                        unsigned int flags)
2765 2766 2767 2768 2769
{
    int rv = -1;
    remote_domain_memory_stats_args args;
    remote_domain_memory_stats_ret ret;
    struct private_data *priv = domain->conn->privateData;
2770
    size_t i;
2771 2772 2773

    remoteDriverLock(priv);

2774
    make_nonnull_domain(&args.dom, domain);
2775
    if (nr_stats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
2776 2777 2778
        virReportError(VIR_ERR_RPC,
                       _("too many memory stats requested: %d > %d"), nr_stats,
                       REMOTE_DOMAIN_MEMORY_STATS_MAX);
2779 2780 2781
        goto done;
    }
    args.maxStats = nr_stats;
2782
    args.flags = flags;
2783
    memset(&ret, 0, sizeof(ret));
2784

2785 2786 2787 2788 2789
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_MEMORY_STATS,
             (xdrproc_t) xdr_remote_domain_memory_stats_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_memory_stats_ret,
             (char *) &ret) == -1)
2790 2791 2792 2793 2794 2795 2796 2797 2798
        goto done;

    for (i = 0; i < ret.stats.stats_len; i++) {
        stats[i].tag = ret.stats.stats_val[i].tag;
        stats[i].val = ret.stats.stats_val[i].val;
    }
    rv = ret.stats.stats_len;
    xdr_free((xdrproc_t) xdr_remote_domain_memory_stats_ret, (char *) &ret);

2799
 done:
2800 2801 2802 2803
    remoteDriverUnlock(priv);
    return rv;
}

2804
static int
2805 2806 2807 2808 2809 2810
remoteDomainBlockPeek(virDomainPtr domain,
                      const char *path,
                      unsigned long long offset,
                      size_t size,
                      void *buffer,
                      unsigned int flags)
2811
{
2812
    int rv = -1;
2813 2814
    remote_domain_block_peek_args args;
    remote_domain_block_peek_ret ret;
2815
    struct private_data *priv = domain->conn->privateData;
2816

2817 2818
    remoteDriverLock(priv);

2819
    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
2820 2821 2822
        virReportError(VIR_ERR_RPC,
                       _("block peek request too large for remote protocol, %zi > %d"),
                       size, REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX);
2823
        goto done;
2824 2825
    }

2826
    make_nonnull_domain(&args.dom, domain);
2827 2828 2829 2830 2831
    args.path = (char *) path;
    args.offset = offset;
    args.size = size;
    args.flags = flags;

2832 2833 2834 2835 2836 2837
    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_BLOCK_PEEK,
             (xdrproc_t) xdr_remote_domain_block_peek_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_block_peek_ret,
             (char *) &ret) == -1)
2838
        goto done;
2839 2840

    if (ret.buffer.buffer_len != size) {
2841 2842
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned buffer is not same size as requested"));
2843
        goto cleanup;
2844 2845
    }

2846
    memcpy(buffer, ret.buffer.buffer_val, size);
2847 2848
    rv = 0;

2849
 cleanup:
2850
    VIR_FREE(ret.buffer.buffer_val);
2851

2852
 done:
2853
    remoteDriverUnlock(priv);
2854
    return rv;
2855 2856
}

R
Richard W.M. Jones 已提交
2857
static int
2858 2859 2860 2861 2862
remoteDomainMemoryPeek(virDomainPtr domain,
                       unsigned long long offset,
                       size_t size,
                       void *buffer,
                       unsigned int flags)
R
Richard W.M. Jones 已提交
2863
{
2864
    int rv = -1;
R
Richard W.M. Jones 已提交
2865 2866
    remote_domain_memory_peek_args args;
    remote_domain_memory_peek_ret ret;
2867
    struct private_data *priv = domain->conn->privateData;
R
Richard W.M. Jones 已提交
2868

2869 2870
    remoteDriverLock(priv);

R
Richard W.M. Jones 已提交
2871
    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
2872 2873 2874
        virReportError(VIR_ERR_RPC,
                       _("memory peek request too large for remote protocol, %zi > %d"),
                       size, REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX);
2875
        goto done;
R
Richard W.M. Jones 已提交
2876 2877
    }

2878
    make_nonnull_domain(&args.dom, domain);
R
Richard W.M. Jones 已提交
2879 2880 2881 2882
    args.offset = offset;
    args.size = size;
    args.flags = flags;

2883 2884 2885 2886 2887 2888
    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_MEMORY_PEEK,
             (xdrproc_t) xdr_remote_domain_memory_peek_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_memory_peek_ret,
             (char *) &ret) == -1)
2889
        goto done;
R
Richard W.M. Jones 已提交
2890 2891

    if (ret.buffer.buffer_len != size) {
2892 2893
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned buffer is not same size as requested"));
2894
        goto cleanup;
R
Richard W.M. Jones 已提交
2895 2896
    }

2897
    memcpy(buffer, ret.buffer.buffer_val, size);
2898 2899
    rv = 0;

2900
 cleanup:
2901
    VIR_FREE(ret.buffer.buffer_val);
R
Richard W.M. Jones 已提交
2902

2903
 done:
2904
    remoteDriverUnlock(priv);
2905
    return rv;
R
Richard W.M. Jones 已提交
2906 2907
}

2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940
static int remoteDomainGetBlockJobInfo(virDomainPtr domain,
                                       const char *path,
                                       virDomainBlockJobInfoPtr info,
                                       unsigned int flags)
{
    int rv = -1;
    remote_domain_get_block_job_info_args args;
    remote_domain_get_block_job_info_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, domain);
    args.path = (char *)path;
    args.flags = flags;

    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_BLOCK_JOB_INFO,
             (xdrproc_t)xdr_remote_domain_get_block_job_info_args,
               (char *)&args,
             (xdrproc_t)xdr_remote_domain_get_block_job_info_ret,
               (char *)&ret) == -1)
        goto done;

    if (ret.found) {
        info->type = ret.type;
        info->bandwidth = ret.bandwidth;
        info->cur = ret.cur;
        info->end = ret.end;
        rv = 1;
    } else {
        rv = 0;
    }

2941
 done:
2942 2943 2944 2945
    remoteDriverUnlock(priv);
    return rv;
}

2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
static int remoteDomainGetBlockIoTune(virDomainPtr domain,
                                      const char *disk,
                                      virTypedParameterPtr params,
                                      int *nparams,
                                      unsigned int flags)
{
    int rv = -1;
    remote_domain_get_block_io_tune_args args;
    remote_domain_get_block_io_tune_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, domain);
    args.disk = disk ? (char **)&disk : NULL;
    args.nparams = *nparams;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));


    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_BLOCK_IO_TUNE,
             (xdrproc_t) xdr_remote_domain_get_block_io_tune_args,
               (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_block_io_tune_ret,
               (char *) &ret) == -1) {
        goto done;
    }

    /* Handle the case when the caller does not know the number of parameters
     * and is asking for the number of parameters supported
     */
    if (*nparams == 0) {
        *nparams = ret.nparams;
        rv = 0;
        goto cleanup;
    }

2984 2985
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
2986
                                  REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX,
2987 2988
                                  &params,
                                  nparams) < 0)
2989 2990 2991 2992
        goto cleanup;

    rv = 0;

2993
 cleanup:
2994 2995
    xdr_free((xdrproc_t) xdr_remote_domain_get_block_io_tune_ret,
             (char *) &ret);
2996
 done:
2997 2998 2999 3000
    remoteDriverUnlock(priv);
    return rv;
}

3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
static int remoteDomainGetCPUStats(virDomainPtr domain,
                                   virTypedParameterPtr params,
                                   unsigned int nparams,
                                   int start_cpu,
                                   unsigned int ncpus,
                                   unsigned int flags)
{
    struct private_data *priv = domain->conn->privateData;
    remote_domain_get_cpu_stats_args args;
    remote_domain_get_cpu_stats_ret ret;
    int rv = -1;
    int cpu;

    remoteDriverLock(priv);

    if (nparams > REMOTE_NODE_CPU_STATS_MAX) {
3017 3018 3019
        virReportError(VIR_ERR_RPC,
                       _("nparams count exceeds maximum: %u > %u"),
                       nparams, REMOTE_NODE_CPU_STATS_MAX);
3020 3021 3022
        goto done;
    }
    if (ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
3023 3024 3025
        virReportError(VIR_ERR_RPC,
                       _("ncpus count exceeds maximum: %u > %u"),
                       ncpus, REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX);
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046
        goto done;
    }

    make_nonnull_domain(&args.dom, domain);
    args.nparams = nparams;
    args.start_cpu = start_cpu;
    args.ncpus = ncpus;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));

    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_CPU_STATS,
             (xdrproc_t) xdr_remote_domain_get_cpu_stats_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_cpu_stats_ret,
             (char *) &ret) == -1)
        goto done;

    /* Check the length of the returned list carefully. */
    if (ret.params.params_len > nparams * ncpus ||
        (ret.params.params_len &&
3047
         ((ret.params.params_len % ret.nparams) || ret.nparams > nparams))) {
3048 3049 3050
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteDomainGetCPUStats: "
                         "returned number of stats exceeds limit"));
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063
        memset(params, 0, sizeof(*params) * nparams * ncpus);
        goto cleanup;
    }

    /* Handle the case when the caller does not know the number of stats
     * and is asking for the number of stats supported
     */
    if (nparams == 0) {
        rv = ret.nparams;
        goto cleanup;
    }

    /* The remote side did not send back any zero entries, so we have
3064 3065
     * to expand things back into a possibly sparse array, where the
     * tail of the array may be omitted.
3066 3067
     */
    memset(params, 0, sizeof(*params) * nparams * ncpus);
3068
    ncpus = ret.params.params_len / ret.nparams;
3069 3070
    for (cpu = 0; cpu < ncpus; cpu++) {
        int tmp = nparams;
3071
        virTypedParameterPtr cpu_params = &params[cpu * nparams];
3072 3073
        remote_typed_param *stride = &ret.params.params_val[cpu * ret.nparams];

3074 3075 3076 3077
        if (virTypedParamsDeserialize((virTypedParameterRemotePtr) stride,
                                      ret.nparams,
                                      REMOTE_NODE_CPU_STATS_MAX,
                                      &cpu_params, &tmp) < 0)
3078 3079 3080 3081
            goto cleanup;
    }

    rv = ret.nparams;
3082
 cleanup:
A
Alex Jia 已提交
3083
    if (rv < 0)
3084
        virTypedParamsClear(params, nparams * ncpus);
3085

3086 3087
    xdr_free((xdrproc_t) xdr_remote_domain_get_cpu_stats_ret,
             (char *) &ret);
3088
 done:
3089 3090 3091 3092
    remoteDriverUnlock(priv);
    return rv;
}

3093

3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104
static int
remoteConnectNetworkEventRegisterAny(virConnectPtr conn,
                                     virNetworkPtr net,
                                     int eventID,
                                     virConnectNetworkEventGenericCallback callback,
                                     void *opaque,
                                     virFreeCallback freecb)
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
    remote_connect_network_event_register_any_args args;
3105
    remote_connect_network_event_register_any_ret ret;
3106 3107
    int callbackID;
    int count;
3108
    remote_nonnull_network network;
3109 3110 3111

    remoteDriverLock(priv);

3112 3113 3114 3115
    if ((count = virNetworkEventStateRegisterClient(conn, priv->eventState,
                                                    net, eventID, callback,
                                                    opaque, freecb,
                                                    &callbackID)) < 0)
3116 3117 3118 3119 3120 3121
        goto done;

    /* If this is the first callback for this eventID, we need to enable
     * events on the server */
    if (count == 1) {
        args.eventID = eventID;
3122 3123 3124 3125 3126 3127
        if (net) {
            make_nonnull_network(&network, net);
            args.net = &network;
        } else {
            args.net = NULL;
        }
3128

3129
        memset(&ret, 0, sizeof(ret));
3130 3131
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_NETWORK_EVENT_REGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_network_event_register_any_args, (char *) &args,
3132
                 (xdrproc_t) xdr_remote_connect_network_event_register_any_ret, (char *) &ret) == -1) {
3133
            virObjectEventStateDeregisterID(conn, priv->eventState,
3134
                                            callbackID, false);
3135 3136
            goto done;
        }
3137 3138
        virObjectEventStateSetRemote(conn, priv->eventState, callbackID,
                                     ret.callbackID);
3139 3140 3141 3142
    }

    rv = callbackID;

3143
 done:
3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156
    remoteDriverUnlock(priv);
    return rv;
}


static int
remoteConnectNetworkEventDeregisterAny(virConnectPtr conn,
                                       int callbackID)
{
    struct private_data *priv = conn->privateData;
    int rv = -1;
    remote_connect_network_event_deregister_any_args args;
    int eventID;
3157
    int remoteID;
3158 3159 3160 3161
    int count;

    remoteDriverLock(priv);

3162
    if ((eventID = virObjectEventStateEventID(conn, priv->eventState,
3163
                                              callbackID, &remoteID)) < 0)
3164 3165
        goto done;

3166
    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
3167
                                                 callbackID, true)) < 0)
3168 3169 3170 3171 3172
        goto done;

    /* If that was the last callback for this eventID, we need to disable
     * events on the server */
    if (count == 0) {
3173
        args.callbackID = remoteID;
3174 3175 3176 3177 3178 3179 3180 3181 3182

        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_NETWORK_EVENT_DEREGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_network_event_deregister_any_args, (char *) &args,
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
            goto done;
    }

    rv = 0;

3183
 done:
3184 3185 3186 3187
    remoteDriverUnlock(priv);
    return rv;
}

3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227
static int
remoteConnectStoragePoolEventRegisterAny(virConnectPtr conn,
                                         virStoragePoolPtr pool,
                                         int eventID,
                                         virConnectStoragePoolEventGenericCallback callback,
                                         void *opaque,
                                         virFreeCallback freecb)
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
    remote_connect_storage_pool_event_register_any_args args;
    remote_connect_storage_pool_event_register_any_ret ret;
    int callbackID;
    int count;
    remote_nonnull_storage_pool storage_pool;

    remoteDriverLock(priv);

    if ((count = virStoragePoolEventStateRegisterClient(conn, priv->eventState,
                                                        pool, eventID, callback,
                                                        opaque, freecb,
                                                        &callbackID)) < 0)
        goto done;

    /* If this is the first callback for this eventID, we need to enable
     * events on the server */
    if (count == 1) {
        args.eventID = eventID;
        if (pool) {
            make_nonnull_storage_pool(&storage_pool, pool);
            args.pool = &storage_pool;
        } else {
            args.pool = NULL;
        }

        memset(&ret, 0, sizeof(ret));
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_STORAGE_POOL_EVENT_REGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_storage_pool_event_register_any_args, (char *) &args,
                 (xdrproc_t) xdr_remote_connect_storage_pool_event_register_any_ret, (char *) &ret) == -1) {
            virObjectEventStateDeregisterID(conn, priv->eventState,
3228
                                            callbackID, false);
3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260
            goto done;
        }

        virObjectEventStateSetRemote(conn, priv->eventState, callbackID,
                                     ret.callbackID);
    }

    rv = callbackID;

 done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteConnectStoragePoolEventDeregisterAny(virConnectPtr conn,
                                           int callbackID)
{
    struct private_data *priv = conn->privateData;
    int rv = -1;
    remote_connect_storage_pool_event_deregister_any_args args;
    int eventID;
    int remoteID;
    int count;

    remoteDriverLock(priv);

    if ((eventID = virObjectEventStateEventID(conn, priv->eventState,
                                              callbackID, &remoteID)) < 0)
        goto done;

    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
3261
                                                 callbackID, true)) < 0)
3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282
        goto done;

    /* If that was the last callback for this eventID, we need to disable
     * events on the server */
    if (count == 0) {
        args.callbackID = remoteID;

        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_STORAGE_POOL_EVENT_DEREGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_storage_pool_event_deregister_any_args, (char *) &args,
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
            goto done;

    }

    rv = 0;

 done:
    remoteDriverUnlock(priv);
    return rv;
}

3283

3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323
static int
remoteConnectNodeDeviceEventRegisterAny(virConnectPtr conn,
                                        virNodeDevicePtr dev,
                                        int eventID,
                                        virConnectNodeDeviceEventGenericCallback callback,
                                        void *opaque,
                                        virFreeCallback freecb)
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
    remote_connect_node_device_event_register_any_args args;
    remote_connect_node_device_event_register_any_ret ret;
    int callbackID;
    int count;
    remote_nonnull_node_device node_device;

    remoteDriverLock(priv);

    if ((count = virNodeDeviceEventStateRegisterClient(conn, priv->eventState,
                                                       dev, eventID, callback,
                                                       opaque, freecb,
                                                       &callbackID)) < 0)
        goto done;

    /* If this is the first callback for this eventID, we need to enable
     * events on the server */
    if (count == 1) {
        args.eventID = eventID;
        if (dev) {
            make_nonnull_node_device(&node_device, dev);
            args.dev = &node_device;
        } else {
            args.dev = NULL;
        }

        memset(&ret, 0, sizeof(ret));
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_NODE_DEVICE_EVENT_REGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_node_device_event_register_any_args, (char *) &args,
                 (xdrproc_t) xdr_remote_connect_node_device_event_register_any_ret, (char *) &ret) == -1) {
            virObjectEventStateDeregisterID(conn, priv->eventState,
3324
                                            callbackID, false);
3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357
            goto done;
        }

        virObjectEventStateSetRemote(conn, priv->eventState, callbackID,
                                     ret.callbackID);
    }

    rv = callbackID;

 done:
    remoteDriverUnlock(priv);
    return rv;
}


static int
remoteConnectNodeDeviceEventDeregisterAny(virConnectPtr conn,
                                          int callbackID)
{
    struct private_data *priv = conn->privateData;
    int rv = -1;
    remote_connect_node_device_event_deregister_any_args args;
    int eventID;
    int remoteID;
    int count;

    remoteDriverLock(priv);

    if ((eventID = virObjectEventStateEventID(conn, priv->eventState,
                                              callbackID, &remoteID)) < 0)
        goto done;

    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
3358
                                                 callbackID, true)) < 0)
3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380
        goto done;

    /* If that was the last callback for this eventID, we need to disable
     * events on the server */
    if (count == 0) {
        args.callbackID = remoteID;

        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_NODE_DEVICE_EVENT_DEREGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_node_device_event_deregister_any_args, (char *) &args,
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
            goto done;

    }

    rv = 0;

 done:
    remoteDriverUnlock(priv);
    return rv;
}


3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420
static int
remoteConnectSecretEventRegisterAny(virConnectPtr conn,
                                    virSecretPtr secret,
                                    int eventID,
                                    virConnectSecretEventGenericCallback callback,
                                    void *opaque,
                                    virFreeCallback freecb)
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
    remote_connect_secret_event_register_any_args args;
    remote_connect_secret_event_register_any_ret ret;
    int callbackID;
    int count;
    remote_nonnull_secret sec;

    remoteDriverLock(priv);

    if ((count = virSecretEventStateRegisterClient(conn, priv->eventState,
                                                   secret, eventID, callback,
                                                   opaque, freecb,
                                                   &callbackID)) < 0)
        goto done;

    /* If this is the first callback for this eventID, we need to enable
     * events on the server */
    if (count == 1) {
        args.eventID = eventID;
        if (secret) {
            make_nonnull_secret(&sec, secret);
            args.secret = &sec;
        } else {
            args.secret = NULL;
        }

        memset(&ret, 0, sizeof(ret));
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_SECRET_EVENT_REGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_secret_event_register_any_args, (char *) &args,
                 (xdrproc_t) xdr_remote_connect_secret_event_register_any_ret, (char *) &ret) == -1) {
            virObjectEventStateDeregisterID(conn, priv->eventState,
3421
                                            callbackID, false);
3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454
            goto done;
        }

        virObjectEventStateSetRemote(conn, priv->eventState, callbackID,
                                     ret.callbackID);
    }

    rv = callbackID;

 done:
    remoteDriverUnlock(priv);
    return rv;
}


static int
remoteConnectSecretEventDeregisterAny(virConnectPtr conn,
                                          int callbackID)
{
    struct private_data *priv = conn->privateData;
    int rv = -1;
    remote_connect_secret_event_deregister_any_args args;
    int eventID;
    int remoteID;
    int count;

    remoteDriverLock(priv);

    if ((eventID = virObjectEventStateEventID(conn, priv->eventState,
                                              callbackID, &remoteID)) < 0)
        goto done;

    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
3455
                                                 callbackID, true)) < 0)
3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477
        goto done;

    /* If that was the last callback for this eventID, we need to disable
     * events on the server */
    if (count == 0) {
        args.callbackID = remoteID;

        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_SECRET_EVENT_DEREGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_secret_event_deregister_any_args, (char *) &args,
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
            goto done;

    }

    rv = 0;

 done:
    remoteDriverUnlock(priv);
    return rv;
}


3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520
static int
remoteConnectDomainQemuMonitorEventRegister(virConnectPtr conn,
                                            virDomainPtr dom,
                                            const char *event,
                                            virConnectDomainQemuMonitorEventCallback callback,
                                            void *opaque,
                                            virFreeCallback freecb,
                                            unsigned int flags)
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
    qemu_connect_domain_monitor_event_register_args args;
    qemu_connect_domain_monitor_event_register_ret ret;
    int callbackID;
    int count;
    remote_nonnull_domain domain;

    remoteDriverLock(priv);

    if ((count = virDomainQemuMonitorEventStateRegisterID(conn,
                                                          priv->eventState,
                                                          dom, event, callback,
                                                          opaque, freecb, -1,
                                                          &callbackID)) < 0)
        goto done;

    /* If this is the first callback for this event, we need to enable
     * events on the server */
    if (count == 1) {
        if (dom) {
            make_nonnull_domain(&domain, dom);
            args.dom = &domain;
        } else {
            args.dom = NULL;
        }
        args.event = event ? (char **) &event : NULL;
        args.flags = flags;

        memset(&ret, 0, sizeof(ret));
        if (call(conn, priv, REMOTE_CALL_QEMU, QEMU_PROC_CONNECT_DOMAIN_MONITOR_EVENT_REGISTER,
                 (xdrproc_t) xdr_qemu_connect_domain_monitor_event_register_args, (char *) &args,
                 (xdrproc_t) xdr_qemu_connect_domain_monitor_event_register_ret, (char *) &ret) == -1) {
            virObjectEventStateDeregisterID(conn, priv->eventState,
3521
                                            callbackID, false);
3522 3523 3524 3525 3526 3527 3528 3529
            goto done;
        }
        virObjectEventStateSetRemote(conn, priv->eventState, callbackID,
                                     ret.callbackID);
    }

    rv = callbackID;

3530
 done:
3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552
    remoteDriverUnlock(priv);
    return rv;
}


static int
remoteConnectDomainQemuMonitorEventDeregister(virConnectPtr conn,
                                              int callbackID)
{
    struct private_data *priv = conn->privateData;
    int rv = -1;
    qemu_connect_domain_monitor_event_deregister_args args;
    int remoteID;
    int count;

    remoteDriverLock(priv);

    if (virObjectEventStateEventID(conn, priv->eventState,
                                   callbackID, &remoteID) < 0)
        goto done;

    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
3553
                                                 callbackID, true)) < 0)
3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568
        goto done;

    /* If that was the last callback for this event, we need to disable
     * events on the server */
    if (count == 0) {
        args.callbackID = remoteID;

        if (call(conn, priv, REMOTE_CALL_QEMU, QEMU_PROC_CONNECT_DOMAIN_MONITOR_EVENT_DEREGISTER,
                 (xdrproc_t) xdr_qemu_connect_domain_monitor_event_deregister_args, (char *) &args,
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
            goto done;
    }

    rv = 0;

3569
 done:
3570 3571 3572 3573
    remoteDriverUnlock(priv);
    return rv;
}

3574 3575
/*----------------------------------------------------------------------*/

3576
static char *
3577 3578 3579 3580
remoteConnectFindStoragePoolSources(virConnectPtr conn,
                                    const char *type,
                                    const char *srcSpec,
                                    unsigned int flags)
3581
{
3582
    char *rv = NULL;
3583 3584
    remote_connect_find_storage_pool_sources_args args;
    remote_connect_find_storage_pool_sources_ret ret;
3585
    struct private_data *priv = conn->privateData;
3586

3587 3588
    remoteDriverLock(priv);

3589
    args.type = (char*)type;
3590
    args.srcSpec = srcSpec ? (char **)&srcSpec : NULL;
3591 3592
    args.flags = flags;

3593
    memset(&ret, 0, sizeof(ret));
3594 3595 3596
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_FIND_STORAGE_POOL_SOURCES,
             (xdrproc_t) xdr_remote_connect_find_storage_pool_sources_args, (char *) &args,
             (xdrproc_t) xdr_remote_connect_find_storage_pool_sources_ret, (char *) &ret) == -1)
3597
        goto done;
3598

3599 3600 3601
    rv = ret.xml;
    ret.xml = NULL; /* To stop xdr_free free'ing it */

3602
    xdr_free((xdrproc_t) xdr_remote_connect_find_storage_pool_sources_ret, (char *) &ret);
3603

3604
 done:
3605
    remoteDriverUnlock(priv);
3606
    return rv;
3607 3608
}

3609 3610
/*----------------------------------------------------------------------*/

3611
static int
3612
remoteNodeDeviceDettach(virNodeDevicePtr dev)
3613 3614 3615 3616 3617 3618 3619 3620 3621
{
    int rv = -1;
    remote_node_device_dettach_args args;
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3622 3623 3624
    if (call(dev->conn, priv, 0, REMOTE_PROC_NODE_DEVICE_DETTACH,
             (xdrproc_t) xdr_remote_node_device_dettach_args, (char *) &args,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
3625 3626 3627 3628
        goto done;

    rv = 0;

3629
 done:
3630 3631 3632 3633
    remoteDriverUnlock(priv);
    return rv;
}

3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655
static int
remoteNodeDeviceDetachFlags(virNodeDevicePtr dev,
                            const char *driverName,
                            unsigned int flags)
{
    int rv = -1;
    remote_node_device_detach_flags_args args;
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;
    args.driverName = driverName ? (char**)&driverName : NULL;
    args.flags = flags;

    if (call(dev->conn, priv, 0, REMOTE_PROC_NODE_DEVICE_DETACH_FLAGS,
             (xdrproc_t) xdr_remote_node_device_detach_flags_args,
             (char *) &args, (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

3656
 done:
3657 3658 3659 3660
    remoteDriverUnlock(priv);
    return rv;
}

3661
static int
3662
remoteNodeDeviceReAttach(virNodeDevicePtr dev)
3663 3664 3665 3666 3667 3668 3669 3670 3671
{
    int rv = -1;
    remote_node_device_re_attach_args args;
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3672 3673 3674
    if (call(dev->conn, priv, 0, REMOTE_PROC_NODE_DEVICE_RE_ATTACH,
             (xdrproc_t) xdr_remote_node_device_re_attach_args, (char *) &args,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
3675 3676 3677 3678
        goto done;

    rv = 0;

3679
 done:
3680 3681 3682 3683 3684
    remoteDriverUnlock(priv);
    return rv;
}

static int
3685
remoteNodeDeviceReset(virNodeDevicePtr dev)
3686 3687 3688
{
    int rv = -1;
    remote_node_device_reset_args args;
3689
    /* This method is unusual in that it uses the HV driver, not the devMon driver
3690
     * hence its use of privateData, instead of nodeDevicePrivateData */
3691 3692 3693 3694 3695 3696
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3697 3698 3699
    if (call(dev->conn, priv, 0, REMOTE_PROC_NODE_DEVICE_RESET,
             (xdrproc_t) xdr_remote_node_device_reset_args, (char *) &args,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
3700 3701 3702 3703
        goto done;

    rv = 0;

3704
 done:
3705 3706 3707 3708
    remoteDriverUnlock(priv);
    return rv;
}

3709

3710 3711
/*----------------------------------------------------------------------*/

3712
static int
3713 3714 3715
remoteAuthenticate(virConnectPtr conn, struct private_data *priv,
                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                   const char *authtype)
3716 3717
{
    struct remote_auth_list_ret ret;
3718
    int err, type = REMOTE_AUTH_NONE;
3719

3720
    memset(&ret, 0, sizeof(ret));
3721 3722 3723 3724
    err = call(conn, priv, 0,
               REMOTE_PROC_AUTH_LIST,
               (xdrproc_t) xdr_void, (char *) NULL,
               (xdrproc_t) xdr_remote_auth_list_ret, (char *) &ret);
3725
    if (err < 0) {
3726
        if (virGetLastErrorCode() == VIR_ERR_NO_SUPPORT) {
3727 3728 3729 3730
            /* Missing RPC - old server - ignore */
            virResetLastError();
            return 0;
        }
3731
        return -1;
3732
    }
3733 3734 3735 3736

    if (ret.types.types_len == 0)
        return 0;

3737
    if (authtype) {
3738 3739
        int want;
        size_t i;
3740 3741 3742 3743 3744 3745
        if (STRCASEEQ(authtype, "sasl") ||
            STRCASEEQLEN(authtype, "sasl.", 5)) {
            want = REMOTE_AUTH_SASL;
        } else if (STRCASEEQ(authtype, "polkit")) {
            want = REMOTE_AUTH_POLKIT;
        } else {
3746 3747
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("unknown authentication type %s"), authtype);
3748 3749
            return -1;
        }
3750
        for (i = 0; i < ret.types.types_len; i++) {
3751 3752 3753 3754
            if (ret.types.types_val[i] == want)
                type = want;
        }
        if (type == REMOTE_AUTH_NONE) {
3755 3756 3757
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("requested authentication type %s rejected"),
                           authtype);
3758 3759 3760 3761 3762 3763 3764
            return -1;
        }
    } else {
        type = ret.types.types_val[0];
    }

    switch (type) {
3765
#if WITH_SASL
3766 3767 3768 3769 3770 3771
    case REMOTE_AUTH_SASL: {
        const char *mech = NULL;
        if (authtype &&
            STRCASEEQLEN(authtype, "sasl.", 5))
            mech = authtype + 5;

3772
        if (remoteAuthSASL(conn, priv, auth, mech) < 0) {
3773
            VIR_FREE(ret.types.types_val);
3774 3775 3776
            return -1;
        }
        break;
3777
    }
3778 3779
#endif

3780
    case REMOTE_AUTH_POLKIT:
3781
        if (remoteAuthPolkit(conn, priv, auth) < 0) {
3782
            VIR_FREE(ret.types.types_val);
3783 3784 3785 3786
            return -1;
        }
        break;

3787 3788 3789 3790 3791
    case REMOTE_AUTH_NONE:
        /* Nothing todo, hurrah ! */
        break;

    default:
3792 3793 3794
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("unsupported authentication type %d"),
                       ret.types.types_val[0]);
3795
        VIR_FREE(ret.types.types_val);
3796 3797 3798
        return -1;
    }

3799
    VIR_FREE(ret.types.types_val);
3800 3801 3802 3803 3804 3805

    return 0;
}



3806
#if WITH_SASL
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875
static int remoteAuthCredVir2SASL(int vircred)
{
    switch (vircred) {
    case VIR_CRED_USERNAME:
        return SASL_CB_USER;

    case VIR_CRED_AUTHNAME:
        return SASL_CB_AUTHNAME;

    case VIR_CRED_LANGUAGE:
        return SASL_CB_LANGUAGE;

    case VIR_CRED_CNONCE:
        return SASL_CB_CNONCE;

    case VIR_CRED_PASSPHRASE:
        return SASL_CB_PASS;

    case VIR_CRED_ECHOPROMPT:
        return SASL_CB_ECHOPROMPT;

    case VIR_CRED_NOECHOPROMPT:
        return SASL_CB_NOECHOPROMPT;

    case VIR_CRED_REALM:
        return SASL_CB_GETREALM;
    }

    return 0;
}

static int remoteAuthCredSASL2Vir(int vircred)
{
    switch (vircred) {
    case SASL_CB_USER:
        return VIR_CRED_USERNAME;

    case SASL_CB_AUTHNAME:
        return VIR_CRED_AUTHNAME;

    case SASL_CB_LANGUAGE:
        return VIR_CRED_LANGUAGE;

    case SASL_CB_CNONCE:
        return VIR_CRED_CNONCE;

    case SASL_CB_PASS:
        return VIR_CRED_PASSPHRASE;

    case SASL_CB_ECHOPROMPT:
        return VIR_CRED_ECHOPROMPT;

    case SASL_CB_NOECHOPROMPT:
        return VIR_CRED_NOECHOPROMPT;

    case SASL_CB_GETREALM:
        return VIR_CRED_REALM;
    }

    return 0;
}

/*
 * @param credtype array of credential types client supports
 * @param ncredtype size of credtype array
 * @return the SASL callback structure, or NULL on error
 *
 * Build up the SASL callback structure. We register one callback for
 * each credential type that the libvirt client indicated they support.
3876
 * We explicitly leave the callback function pointer at NULL though,
3877 3878 3879 3880 3881 3882 3883 3884
 * because we don't actually want to get SASL callbacks triggered.
 * Instead, we want the start/step functions to return SASL_INTERACT.
 * This lets us give the libvirt client a list of all required
 * credentials in one go, rather than triggering the callback one
 * credential at a time,
 */
static sasl_callback_t *remoteAuthMakeCallbacks(int *credtype, int ncredtype)
{
3885
    sasl_callback_t *cbs;
3886 3887
    size_t i;
    int n;
3888
    if (VIR_ALLOC_N(cbs, ncredtype+1) < 0)
3889 3890
        return NULL;

3891
    for (i = 0, n = 0; i < ncredtype; i++) {
3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906
        int id = remoteAuthCredVir2SASL(credtype[i]);
        if (id != 0)
            cbs[n++].id = id;
        /* Don't fill proc or context fields of sasl_callback_t
         * because we want to use interactions instead */
    }
    cbs[n].id = 0;
    return cbs;
}


/*
 * @param interact SASL interactions required
 * @param cred populated with libvirt credential metadata
 * @return the size of the cred array returned
3907
 *
3908 3909 3910 3911 3912
 * Builds up an array of libvirt credential structs, populating
 * with data from the SASL interaction struct. These two structs
 * are basically a 1-to-1 copy of each other.
 */
static int remoteAuthMakeCredentials(sasl_interact_t *interact,
3913 3914
                                     virConnectCredentialPtr *cred,
                                     size_t *ncred)
3915 3916 3917 3918 3919
{
    int ninteract;
    if (!cred)
        return -1;

3920
    for (ninteract = 0, *ncred = 0; interact[ninteract].id != 0; ninteract++) {
3921 3922 3923 3924
        if (interact[ninteract].result)
            continue;
        (*ncred)++;
    }
3925

3926
    if (VIR_ALLOC_N(*cred, *ncred) < 0)
3927 3928
        return -1;

3929
    for (ninteract = 0, *ncred = 0; interact[ninteract].id != 0; ninteract++) {
3930 3931 3932 3933 3934 3935
        if (interact[ninteract].result)
            continue;

        (*cred)[*ncred].type = remoteAuthCredSASL2Vir(interact[ninteract].id);
        if (!(*cred)[*ncred].type) {
            *ncred = 0;
3936
            VIR_FREE(*cred);
3937 3938
            return -1;
        }
3939 3940 3941 3942 3943 3944 3945 3946
        if (interact[*ncred].challenge)
            (*cred)[*ncred].challenge = interact[ninteract].challenge;
        (*cred)[*ncred].prompt = interact[ninteract].prompt;
        if (interact[*ncred].defresult)
            (*cred)[*ncred].defresult = interact[ninteract].defresult;
        (*cred)[*ncred].result = NULL;

        (*ncred)++;
3947 3948
    }

3949
    return 0;
3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962
}


/*
 * @param cred the populated libvirt credentials
 * @param interact the SASL interactions to fill in results for
 *
 * Fills the SASL interactions with the result from the libvirt
 * callbacks
 */
static void remoteAuthFillInteract(virConnectCredentialPtr cred,
                                   sasl_interact_t *interact)
{
3963
    int ninteract, ncred;
3964
    for (ninteract = 0, ncred = 0; interact[ninteract].id != 0; ninteract++) {
3965 3966 3967 3968 3969
        if (interact[ninteract].result)
            continue;
        interact[ninteract].result = cred[ncred].result;
        interact[ninteract].len = cred[ncred].resultlen;
        ncred++;
3970 3971 3972
    }
}

3973 3974 3975 3976
struct remoteAuthInteractState {
    sasl_interact_t *interact;
    virConnectCredentialPtr cred;
    size_t ncred;
3977
    virAuthConfigPtr config;
3978 3979 3980
};


3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003

static int remoteAuthFillFromConfig(virConnectPtr conn,
                                    struct remoteAuthInteractState *state)
{
    int ret = -1;
    int ninteract;
    const char *credname;
    char *path = NULL;

    VIR_DEBUG("Trying to fill auth parameters from config file");

    if (!state->config) {
        if (virAuthGetConfigFilePath(conn, &path) < 0)
            goto cleanup;
        if (path == NULL) {
            ret = 0;
            goto cleanup;
        }

        if (!(state->config = virAuthConfigNew(path)))
            goto cleanup;
    }

4004
    for (ninteract = 0; state->interact[ninteract].id != 0; ninteract++) {
4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024
        const char *value = NULL;

        switch (state->interact[ninteract].id) {
        case SASL_CB_USER:
            credname = "username";
            break;
        case SASL_CB_AUTHNAME:
            credname = "authname";
            break;
        case SASL_CB_PASS:
            credname = "password";
            break;
        case SASL_CB_GETREALM:
            credname = "realm";
            break;
        default:
            credname = NULL;
            break;
        }

4025 4026
        if (credname &&
            virAuthConfigLookup(state->config,
4027
                                "libvirt",
4028
                                VIR_URI_SERVER(conn->uri),
4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040
                                credname,
                                &value) < 0)
            goto cleanup;

        if (value) {
            state->interact[ninteract].result = value;
            state->interact[ninteract].len = strlen(value);
        }
    }

    ret = 0;

4041
 cleanup:
4042 4043 4044 4045 4046 4047 4048
    VIR_FREE(path);
    return ret;
}


static void remoteAuthInteractStateClear(struct remoteAuthInteractState *state,
                                         bool final)
4049 4050 4051 4052 4053
{
    size_t i;
    if (!state)
        return;

4054
    for (i = 0; i < state->ncred; i++)
4055 4056 4057
        VIR_FREE(state->cred[i].result);
    VIR_FREE(state->cred);
    state->ncred = 0;
4058 4059 4060

    if (final)
        virAuthConfigFree(state->config);
4061 4062 4063
}


4064 4065
static int remoteAuthInteract(virConnectPtr conn,
                              struct remoteAuthInteractState *state,
4066 4067 4068 4069
                              virConnectAuthPtr auth)
{
    int ret = -1;

4070 4071 4072
    VIR_DEBUG("Starting SASL interaction");
    remoteAuthInteractStateClear(state, false);

4073
    /* Fills state->interact with any values from the auth config file */
4074 4075
    if (remoteAuthFillFromConfig(conn, state) < 0)
        goto cleanup;
4076

4077
    /* Populates state->cred for anything not found in the auth config */
4078
    if (remoteAuthMakeCredentials(state->interact, &state->cred, &state->ncred) < 0) {
4079 4080
        virReportError(VIR_ERR_AUTH_FAILED, "%s",
                       _("Failed to make auth credentials"));
4081 4082 4083
        goto cleanup;
    }

4084 4085 4086 4087 4088 4089 4090 4091 4092 4093
    /* If there was anything not in the auth config, we need to
     * run the interactive callback
     */
    if (state->ncred) {
        /* Run the authentication callback */
        if (!auth || !auth->cb) {
            virReportError(VIR_ERR_AUTH_FAILED, "%s",
                           _("No authentication callback available"));
            goto cleanup;
        }
4094

4095 4096 4097 4098 4099 4100 4101 4102
        if ((*(auth->cb))(state->cred, state->ncred, auth->cbdata) < 0) {
            virReportError(VIR_ERR_AUTH_FAILED, "%s",
                           _("Failed to collect auth credentials"));
            goto cleanup;
        }

        /* Copy user's responses from cred into interact */
        remoteAuthFillInteract(state->cred, state->interact);
4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114
    }

    /*
     * 'interact' now has pointers to strings in 'state->cred'
     * so we must not free state->cred until the *next*
     * sasl_start/step function is complete. Hence we
     * call remoteAuthInteractStateClear() at the *start*
     * of this method, rather than the end.
     */

    ret = 0;

4115
 cleanup:
4116 4117 4118 4119
    return ret;
}


4120
/* Perform the SASL authentication process
4121 4122
 */
static int
4123 4124
remoteAuthSASL(virConnectPtr conn, struct private_data *priv,
               virConnectAuthPtr auth, const char *wantmech)
4125 4126 4127 4128 4129 4130 4131
{
    remote_auth_sasl_init_ret iret;
    remote_auth_sasl_start_args sargs;
    remote_auth_sasl_start_ret sret;
    remote_auth_sasl_step_args pargs;
    remote_auth_sasl_step_ret pret;
    const char *clientout;
4132
    char *serverin = NULL;
4133
    size_t clientoutlen, serverinlen;
4134 4135
    const char *mech;
    int err, complete;
4136
    int ssf;
4137 4138 4139
    sasl_callback_t *saslcb = NULL;
    int ret = -1;
    const char *mechlist;
4140
    virNetSASLContextPtr saslCtxt;
E
Eric Blake 已提交
4141
    virNetSASLSessionPtr sasl = NULL;
4142 4143 4144
    struct remoteAuthInteractState state;

    memset(&state, 0, sizeof(state));
4145

4146
    VIR_DEBUG("Client initialize SASL authentication");
4147

4148
    if (!(saslCtxt = virNetSASLContextNewClient()))
4149 4150
        goto cleanup;

4151 4152 4153 4154 4155 4156
    if (auth) {
        if ((saslcb = remoteAuthMakeCallbacks(auth->credtype, auth->ncredtype)) == NULL)
            goto cleanup;
    } else {
        saslcb = NULL;
    }
4157 4158

    /* Setup a handle for being a client */
4159 4160 4161
    if (!(sasl = virNetSASLSessionNewClient(saslCtxt,
                                            "libvirt",
                                            priv->hostname,
J
Ján Tomko 已提交
4162 4163
                                            virNetClientLocalAddrStringSASL(priv->client),
                                            virNetClientRemoteAddrStringSASL(priv->client),
4164
                                            saslcb)))
4165
        goto cleanup;
4166 4167
    /* saslcb is now owned by sasl */
    saslcb = NULL;
4168

4169
# ifdef WITH_GNUTLS
4170
    /* Initialize some connection props we care about */
4171 4172
    if (priv->tls) {
        if ((ssf = virNetClientGetTLSKeySize(priv->client)) < 0)
4173
            goto cleanup;
4174

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

4177
        VIR_DEBUG("Setting external SSF %d", ssf);
4178
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
4179
            goto cleanup;
4180
    }
4181
# endif
4182

4183 4184
    /* If we've got a secure channel (TLS or UNIX sock), we don't care about SSF */
    /* If we're not secure, then forbid any anonymous or trivially crackable auth */
4185 4186 4187 4188
    if (virNetSASLSessionSecProps(sasl,
                                  priv->is_secure ? 0 : 56, /* Equiv to DES supported by all Kerberos */
                                  priv->is_secure ? 0 : 100000, /* Very strong ! AES == 256 */
                                  priv->is_secure ? true : false) < 0)
4189
        goto cleanup;
4190

4191
    /* First call is to inquire about supported mechanisms in the server */
4192 4193 4194 4195
    memset(&iret, 0, sizeof(iret));
    if (call(conn, priv, 0, REMOTE_PROC_AUTH_SASL_INIT,
             (xdrproc_t) xdr_void, (char *)NULL,
             (xdrproc_t) xdr_remote_auth_sasl_init_ret, (char *) &iret) != 0)
4196
        goto cleanup;
4197 4198


4199 4200 4201
    mechlist = iret.mechlist;
    if (wantmech) {
        if (strstr(mechlist, wantmech) == NULL) {
4202 4203 4204
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("SASL mechanism %s not supported by server"),
                           wantmech);
4205
            VIR_FREE(iret.mechlist);
4206 4207 4208 4209 4210
            goto cleanup;
        }
        mechlist = wantmech;
    }
 restart:
4211
    /* Start the auth negotiation on the client end first */
4212
    VIR_DEBUG("Client start negotiation mechlist '%s'", mechlist);
4213 4214
    if ((err = virNetSASLSessionClientStart(sasl,
                                            mechlist,
4215
                                            &state.interact,
4216 4217 4218
                                            &clientout,
                                            &clientoutlen,
                                            &mech)) < 0)
4219 4220 4221
        goto cleanup;

    /* Need to gather some credentials from the client */
4222
    if (err == VIR_NET_SASL_INTERACT) {
4223
        if (remoteAuthInteract(conn, &state, auth) < 0) {
4224
            VIR_FREE(iret.mechlist);
4225 4226
            goto cleanup;
        }
4227
        goto restart;
4228
    }
4229
    VIR_FREE(iret.mechlist);
4230 4231

    if (clientoutlen > REMOTE_AUTH_SASL_DATA_MAX) {
4232 4233 4234
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("SASL negotiation data too long: %zu bytes"),
                       clientoutlen);
4235
        goto cleanup;
4236 4237
    }
    /* NB, distinction of NULL vs "" is *critical* in SASL */
4238
    memset(&sargs, 0, sizeof(sargs));
4239 4240 4241 4242
    sargs.nil = clientout ? 0 : 1;
    sargs.data.data_val = (char*)clientout;
    sargs.data.data_len = clientoutlen;
    sargs.mech = (char*)mech;
4243 4244
    VIR_DEBUG("Server start negotiation with mech %s. Data %zu bytes %p",
              mech, clientoutlen, clientout);
4245 4246

    /* Now send the initial auth data to the server */
4247 4248 4249 4250
    memset(&sret, 0, sizeof(sret));
    if (call(conn, priv, 0, REMOTE_PROC_AUTH_SASL_START,
             (xdrproc_t) xdr_remote_auth_sasl_start_args, (char *) &sargs,
             (xdrproc_t) xdr_remote_auth_sasl_start_ret, (char *) &sret) != 0)
4251
        goto cleanup;
4252 4253 4254 4255 4256

    complete = sret.complete;
    /* NB, distinction of NULL vs "" is *critical* in SASL */
    serverin = sret.nil ? NULL : sret.data.data_val;
    serverinlen = sret.data.data_len;
4257 4258
    VIR_DEBUG("Client step result complete: %d. Data %zu bytes %p",
              complete, serverinlen, serverin);
4259

4260 4261 4262 4263 4264
    /* Previous server call showed completion & sasl_client_start() told us
     * we are locally complete too */
    if (complete && err == VIR_NET_SASL_COMPLETE)
        goto done;

4265 4266
    /* Loop-the-loop...
     * Even if the server has completed, the client must *always* do at least one step
D
Daniel Veillard 已提交
4267
     * in this loop to verify the server isn't lying about something. Mutual auth */
4268
    for (;;) {
4269 4270 4271
        if ((err = virNetSASLSessionClientStep(sasl,
                                               serverin,
                                               serverinlen,
4272
                                               &state.interact,
4273 4274
                                               &clientout,
                                               &clientoutlen)) < 0)
4275
            goto cleanup;
4276

4277
        /* Need to gather some credentials from the client */
4278
        if (err == VIR_NET_SASL_INTERACT) {
4279
            if (remoteAuthInteract(conn, &state, auth) < 0) {
4280
                VIR_FREE(iret.mechlist);
4281 4282
                goto cleanup;
            }
4283
            continue;
4284 4285
        }

4286
        VIR_FREE(serverin);
4287 4288
        VIR_DEBUG("Client step result %d. Data %zu bytes %p",
                  err, clientoutlen, clientout);
4289 4290

        /* Previous server call showed completion & we're now locally complete too */
4291
        if (complete && err == VIR_NET_SASL_COMPLETE)
4292 4293 4294 4295
            break;

        /* Not done, prepare to talk with the server for another iteration */
        /* NB, distinction of NULL vs "" is *critical* in SASL */
4296
        memset(&pargs, 0, sizeof(pargs));
4297 4298 4299
        pargs.nil = clientout ? 0 : 1;
        pargs.data.data_val = (char*)clientout;
        pargs.data.data_len = clientoutlen;
4300 4301
        VIR_DEBUG("Server step with %zu bytes %p",
                  clientoutlen, clientout);
4302

4303 4304 4305 4306
        memset(&pret, 0, sizeof(pret));
        if (call(conn, priv, 0, REMOTE_PROC_AUTH_SASL_STEP,
                 (xdrproc_t) xdr_remote_auth_sasl_step_args, (char *) &pargs,
                 (xdrproc_t) xdr_remote_auth_sasl_step_ret, (char *) &pret) != 0)
4307
            goto cleanup;
4308 4309 4310 4311 4312 4313

        complete = pret.complete;
        /* NB, distinction of NULL vs "" is *critical* in SASL */
        serverin = pret.nil ? NULL : pret.data.data_val;
        serverinlen = pret.data.data_len;

4314 4315
        VIR_DEBUG("Client step result complete: %d. Data %zu bytes %p",
                  complete, serverinlen, serverin);
4316 4317

        /* This server call shows complete, and earlier client step was OK */
4318
        if (complete && err == VIR_NET_SASL_COMPLETE) {
4319
            VIR_FREE(serverin);
4320 4321 4322 4323
            break;
        }
    }

4324 4325
    /* Check for suitable SSF if not already secure (TLS or UNIX sock) */
    if (!priv->is_secure) {
4326
        if ((ssf = virNetSASLSessionGetKeySize(sasl)) < 0)
4327
            goto cleanup;
4328

4329
        VIR_DEBUG("SASL SSF value %d", ssf);
4330
        if (ssf < 56) { /* 56 == DES level, good for Kerberos */
4331 4332
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("negotiation SSF %d was not strong enough"), ssf);
4333
            goto cleanup;
4334
        }
4335
        priv->is_secure = 1;
4336 4337
    }

4338
 done:
4339
    VIR_DEBUG("SASL authentication complete");
4340
    virNetClientSetSASLSession(priv->client, sasl);
4341 4342 4343
    ret = 0;

 cleanup:
4344
    VIR_FREE(serverin);
4345

4346
    remoteAuthInteractStateClear(&state, true);
4347
    VIR_FREE(saslcb);
4348 4349
    virObjectUnref(sasl);
    virObjectUnref(saslCtxt);
4350

4351
    return ret;
4352
}
4353
#endif /* WITH_SASL */
4354

4355

4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373
static int
remoteAuthPolkit(virConnectPtr conn, struct private_data *priv,
                 virConnectAuthPtr auth ATTRIBUTE_UNUSED)
{
    remote_auth_polkit_ret ret;
    VIR_DEBUG("Client initialize PolicyKit authentication");

    memset(&ret, 0, sizeof(ret));
    if (call(conn, priv, 0, REMOTE_PROC_AUTH_POLKIT,
             (xdrproc_t) xdr_void, (char *)NULL,
             (xdrproc_t) xdr_remote_auth_polkit_ret, (char *) &ret) != 0) {
        return -1; /* virError already set by call */
    }

    VIR_DEBUG("PolicyKit authentication complete");
    return 0;
}

4374 4375
/*----------------------------------------------------------------------*/

4376 4377 4378 4379 4380
static int
remoteConnectDomainEventRegister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback,
                                 void *opaque,
                                 virFreeCallback freecb)
4381
{
4382
    int callbackID;
4383
    int rv = -1;
4384
    struct private_data *priv = conn->privateData;
4385
    int count;
4386

4387 4388
    remoteDriverLock(priv);

4389 4390 4391 4392 4393
    if ((count = virDomainEventStateRegisterClient(conn, priv->eventState,
                                                   NULL,
                                                   VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                   VIR_DOMAIN_EVENT_CALLBACK(callback),
                                                   opaque, freecb, true,
4394 4395
                                                   &callbackID,
                                                   priv->serverEventFilter)) < 0)
4396
         goto done;
4397

4398
    if (count == 1) {
4399
        /* Tell the server when we are the first callback registering */
4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412
        if (priv->serverEventFilter) {
            remote_connect_domain_event_callback_register_any_args args;
            remote_connect_domain_event_callback_register_any_ret ret;

            args.eventID = VIR_DOMAIN_EVENT_ID_LIFECYCLE;
            args.dom = NULL;

            memset(&ret, 0, sizeof(ret));
            if (call(conn, priv, 0,
                     REMOTE_PROC_CONNECT_DOMAIN_EVENT_CALLBACK_REGISTER_ANY,
                     (xdrproc_t) xdr_remote_connect_domain_event_callback_register_any_args, (char *) &args,
                     (xdrproc_t) xdr_remote_connect_domain_event_callback_register_any_ret, (char *) &ret) == -1) {
                virObjectEventStateDeregisterID(conn, priv->eventState,
4413
                                                callbackID, false);
4414 4415 4416 4417 4418 4419 4420 4421 4422
                goto done;
            }
            virObjectEventStateSetRemote(conn, priv->eventState, callbackID,
                                         ret.callbackID);
        } else {
            if (call(conn, priv, 0, REMOTE_PROC_CONNECT_DOMAIN_EVENT_REGISTER,
                     (xdrproc_t) xdr_void, (char *) NULL,
                     (xdrproc_t) xdr_void, (char *) NULL) == -1) {
                virObjectEventStateDeregisterID(conn, priv->eventState,
4423
                                                callbackID, false);
4424 4425
                goto done;
            }
4426
        }
4427 4428
    }

4429 4430
    rv = 0;

4431
 done:
4432
    remoteDriverUnlock(priv);
4433
    return rv;
4434 4435
}

4436

4437 4438 4439
static int
remoteConnectDomainEventDeregister(virConnectPtr conn,
                                   virConnectDomainEventCallback callback)
4440 4441
{
    struct private_data *priv = conn->privateData;
4442
    int rv = -1;
4443 4444 4445
    remote_connect_domain_event_callback_deregister_any_args args;
    int callbackID;
    int remoteID;
4446
    int count;
4447

4448 4449
    remoteDriverLock(priv);

4450 4451 4452 4453 4454 4455
    if ((callbackID = virDomainEventStateCallbackID(conn, priv->eventState,
                                                    callback,
                                                    &remoteID)) < 0)
        goto done;

    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
4456
                                                 callbackID, true)) < 0)
4457
        goto done;
4458

4459
    if (count == 0) {
4460
        /* Tell the server when we are the last callback deregistering */
4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474
        if (priv->serverEventFilter) {
            args.callbackID = remoteID;

            if (call(conn, priv, 0,
                     REMOTE_PROC_CONNECT_DOMAIN_EVENT_CALLBACK_DEREGISTER_ANY,
                     (xdrproc_t) xdr_remote_connect_domain_event_callback_deregister_any_args, (char *) &args,
                     (xdrproc_t) xdr_void, (char *) NULL) == -1)
                goto done;
        } else {
            if (call(conn, priv, 0, REMOTE_PROC_CONNECT_DOMAIN_EVENT_DEREGISTER,
                     (xdrproc_t) xdr_void, (char *) NULL,
                     (xdrproc_t) xdr_void, (char *) NULL) == -1)
                goto done;
        }
4475 4476
    }

4477 4478
    rv = 0;

4479
 done:
4480
    remoteDriverUnlock(priv);
4481
    return rv;
4482
}
4483

4484 4485

static void
4486 4487 4488
remoteDomainBuildEventLifecycleHelper(virConnectPtr conn,
                                      remote_domain_event_lifecycle_msg *msg,
                                      int callbackID)
4489
{
4490
    struct private_data *priv = conn->privateData;
4491
    virDomainPtr dom;
4492
    virObjectEventPtr event = NULL;
4493

4494
    dom = get_nonnull_domain(conn, msg->dom);
4495
    if (!dom)
4496
        return;
4497

4498
    event = virDomainEventLifecycleNewFromDom(dom, msg->event, msg->detail);
4499
    virObjectUnref(dom);
4500

4501
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519
}
static void
remoteDomainBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_lifecycle_msg *msg = evdata;
    remoteDomainBuildEventLifecycleHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                        virNetClientPtr client ATTRIBUTE_UNUSED,
                                        void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_lifecycle_msg *msg = evdata;
    remoteDomainBuildEventLifecycleHelper(conn, &msg->msg, msg->callbackID);
4520 4521 4522
}


4523
static void
4524 4525 4526
remoteDomainBuildEventRebootHelper(virConnectPtr conn,
                                   remote_domain_event_reboot_msg *msg,
                                   int callbackID)
4527
{
4528
    struct private_data *priv = conn->privateData;
4529
    virDomainPtr dom;
4530
    virObjectEventPtr event = NULL;
4531

4532
    dom = get_nonnull_domain(conn, msg->dom);
4533
    if (!dom)
4534
        return;
4535 4536

    event = virDomainEventRebootNewFromDom(dom);
4537
    virObjectUnref(dom);
4538

4539
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4540
}
4541
static void
4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553
remoteDomainBuildEventReboot(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                             virNetClientPtr client ATTRIBUTE_UNUSED,
                             void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_reboot_msg *msg = evdata;
    remoteDomainBuildEventRebootHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackReboot(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                     virNetClientPtr client ATTRIBUTE_UNUSED,
                                     void *evdata, void *opaque)
4554
{
4555
    virConnectPtr conn = opaque;
4556 4557 4558 4559 4560 4561 4562 4563 4564
    remote_domain_event_callback_reboot_msg *msg = evdata;
    remoteDomainBuildEventRebootHelper(conn, &msg->msg, msg->callbackID);
}

static void
remoteDomainBuildEventRTCChangeHelper(virConnectPtr conn,
                                      remote_domain_event_rtc_change_msg *msg,
                                      int callbackID)
{
4565
    struct private_data *priv = conn->privateData;
4566
    virDomainPtr dom;
4567
    virObjectEventPtr event = NULL;
4568

4569
    dom = get_nonnull_domain(conn, msg->dom);
4570
    if (!dom)
4571
        return;
4572

4573
    event = virDomainEventRTCChangeNewFromDom(dom, msg->offset);
4574
    virObjectUnref(dom);
4575

4576
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4577
}
4578
static void
4579 4580 4581
remoteDomainBuildEventRTCChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
4582
{
4583
    virConnectPtr conn = opaque;
4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601
    remote_domain_event_rtc_change_msg *msg = evdata;
    remoteDomainBuildEventRTCChangeHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackRTCChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                        virNetClientPtr client ATTRIBUTE_UNUSED,
                                        void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_rtc_change_msg *msg = evdata;
    remoteDomainBuildEventRTCChangeHelper(conn, &msg->msg, msg->callbackID);
}

static void
remoteDomainBuildEventWatchdogHelper(virConnectPtr conn,
                                     remote_domain_event_watchdog_msg *msg,
                                     int callbackID)
{
4602
    struct private_data *priv = conn->privateData;
4603
    virDomainPtr dom;
4604
    virObjectEventPtr event = NULL;
4605

4606
    dom = get_nonnull_domain(conn, msg->dom);
4607
    if (!dom)
4608
        return;
4609

4610
    event = virDomainEventWatchdogNewFromDom(dom, msg->action);
4611
    virObjectUnref(dom);
4612

4613
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4614
}
4615
static void
4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627
remoteDomainBuildEventWatchdog(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_watchdog_msg *msg = evdata;
    remoteDomainBuildEventWatchdogHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackWatchdog(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                       virNetClientPtr client ATTRIBUTE_UNUSED,
                                       void *evdata, void *opaque)
4628
{
4629
    virConnectPtr conn = opaque;
4630 4631 4632 4633 4634 4635 4636 4637 4638
    remote_domain_event_callback_watchdog_msg *msg = evdata;
    remoteDomainBuildEventWatchdogHelper(conn, &msg->msg, msg->callbackID);
}

static void
remoteDomainBuildEventIOErrorHelper(virConnectPtr conn,
                                    remote_domain_event_io_error_msg *msg,
                                    int callbackID)
{
4639
    struct private_data *priv = conn->privateData;
4640
    virDomainPtr dom;
4641
    virObjectEventPtr event = NULL;
4642

4643
    dom = get_nonnull_domain(conn, msg->dom);
4644
    if (!dom)
4645
        return;
4646 4647

    event = virDomainEventIOErrorNewFromDom(dom,
4648 4649 4650
                                            msg->srcPath,
                                            msg->devAlias,
                                            msg->action);
4651
    virObjectUnref(dom);
4652

4653
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4654
}
4655
static void
4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667
remoteDomainBuildEventIOError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                              virNetClientPtr client ATTRIBUTE_UNUSED,
                              void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_io_error_msg *msg = evdata;
    remoteDomainBuildEventIOErrorHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackIOError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                      virNetClientPtr client ATTRIBUTE_UNUSED,
                                      void *evdata, void *opaque)
4668
{
4669
    virConnectPtr conn = opaque;
4670 4671 4672 4673 4674 4675 4676 4677 4678
    remote_domain_event_callback_io_error_msg *msg = evdata;
    remoteDomainBuildEventIOErrorHelper(conn, &msg->msg, msg->callbackID);
}

static void
remoteDomainBuildEventIOErrorReasonHelper(virConnectPtr conn,
                                          remote_domain_event_io_error_reason_msg *msg,
                                          int callbackID)
{
4679
    struct private_data *priv = conn->privateData;
4680
    virDomainPtr dom;
4681
    virObjectEventPtr event = NULL;
4682

4683
    dom = get_nonnull_domain(conn, msg->dom);
4684
    if (!dom)
4685
        return;
4686 4687

    event = virDomainEventIOErrorReasonNewFromDom(dom,
4688 4689 4690 4691
                                                  msg->srcPath,
                                                  msg->devAlias,
                                                  msg->action,
                                                  msg->reason);
4692

4693
    virObjectUnref(dom);
4694

4695
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4696
}
4697
static void
4698 4699 4700
remoteDomainBuildEventIOErrorReason(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
4701 4702
{
    virConnectPtr conn = opaque;
4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720
    remote_domain_event_io_error_reason_msg *msg = evdata;
    remoteDomainBuildEventIOErrorReasonHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackIOErrorReason(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                            virNetClientPtr client ATTRIBUTE_UNUSED,
                                            void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_io_error_reason_msg *msg = evdata;
    remoteDomainBuildEventIOErrorReasonHelper(conn, &msg->msg, msg->callbackID);
}

static void
remoteDomainBuildEventBlockJobHelper(virConnectPtr conn,
                                     remote_domain_event_block_job_msg *msg,
                                     int callbackID)
{
4721 4722
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
4723
    virObjectEventPtr event = NULL;
4724 4725 4726 4727 4728 4729 4730 4731

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom)
        return;

    event = virDomainEventBlockJobNewFromDom(dom, msg->path, msg->type,
                                             msg->status);

4732
    virObjectUnref(dom);
4733

4734
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4735
}
4736
static void
4737
remoteDomainBuildEventBlockJob(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
4738 4739
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
4740
{
4741
    virConnectPtr conn = opaque;
4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753
    remote_domain_event_block_job_msg *msg = evdata;
    remoteDomainBuildEventBlockJobHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackBlockJob(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                       virNetClientPtr client ATTRIBUTE_UNUSED,
                                       void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_block_job_msg *msg = evdata;
    remoteDomainBuildEventBlockJobHelper(conn, &msg->msg, msg->callbackID);
}
4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771
static void
remoteDomainBuildEventBlockJob2(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_block_job_2_msg *msg = evdata;
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom)
        return;

    event = virDomainEventBlockJob2NewFromDom(dom, msg->dst, msg->type,
                                              msg->status);

4772
    virObjectUnref(dom);
4773

4774
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
4775
}
4776 4777 4778 4779 4780 4781

static void
remoteDomainBuildEventGraphicsHelper(virConnectPtr conn,
                                     remote_domain_event_graphics_msg *msg,
                                     int callbackID)
{
4782
    struct private_data *priv = conn->privateData;
4783
    virDomainPtr dom;
4784
    virObjectEventPtr event = NULL;
4785 4786 4787
    virDomainEventGraphicsAddressPtr localAddr = NULL;
    virDomainEventGraphicsAddressPtr remoteAddr = NULL;
    virDomainEventGraphicsSubjectPtr subject = NULL;
4788
    size_t i;
4789

4790
    dom = get_nonnull_domain(conn, msg->dom);
4791
    if (!dom)
4792
        return;
4793 4794

    if (VIR_ALLOC(localAddr) < 0)
4795
        goto error;
4796
    localAddr->family = msg->local.family;
4797 4798 4799
    if (VIR_STRDUP(localAddr->service, msg->local.service) < 0 ||
        VIR_STRDUP(localAddr->node, msg->local.node) < 0)
        goto error;
4800 4801

    if (VIR_ALLOC(remoteAddr) < 0)
4802
        goto error;
4803
    remoteAddr->family = msg->remote.family;
4804 4805 4806
    if (VIR_STRDUP(remoteAddr->service, msg->remote.service) < 0 ||
        VIR_STRDUP(remoteAddr->node, msg->remote.node) < 0)
        goto error;
4807 4808

    if (VIR_ALLOC(subject) < 0)
4809
        goto error;
4810
    if (VIR_ALLOC_N(subject->identities, msg->subject.subject_len) < 0)
4811
        goto error;
4812
    subject->nidentity = msg->subject.subject_len;
4813
    for (i = 0; i < subject->nidentity; i++) {
4814 4815 4816
        if (VIR_STRDUP(subject->identities[i].type, msg->subject.subject_val[i].type) < 0 ||
            VIR_STRDUP(subject->identities[i].name, msg->subject.subject_val[i].name) < 0)
            goto error;
4817 4818 4819
    }

    event = virDomainEventGraphicsNewFromDom(dom,
4820
                                             msg->phase,
4821 4822
                                             localAddr,
                                             remoteAddr,
4823
                                             msg->authScheme,
4824 4825
                                             subject);

4826
    virObjectUnref(dom);
4827

4828
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4829
    return;
4830

4831
 error:
4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842
    if (localAddr) {
        VIR_FREE(localAddr->service);
        VIR_FREE(localAddr->node);
        VIR_FREE(localAddr);
    }
    if (remoteAddr) {
        VIR_FREE(remoteAddr->service);
        VIR_FREE(remoteAddr->node);
        VIR_FREE(remoteAddr);
    }
    if (subject) {
4843
        for (i = 0; i < subject->nidentity; i++) {
4844 4845 4846 4847 4848 4849
            VIR_FREE(subject->identities[i].type);
            VIR_FREE(subject->identities[i].name);
        }
        VIR_FREE(subject->identities);
        VIR_FREE(subject);
    }
4850
    virObjectUnref(dom);
4851
    return;
4852
}
4853
static void
4854 4855 4856
remoteDomainBuildEventGraphics(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
4857
{
4858
    virConnectPtr conn = opaque;
4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876
    remote_domain_event_graphics_msg *msg = evdata;
    remoteDomainBuildEventGraphicsHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackGraphics(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                       virNetClientPtr client ATTRIBUTE_UNUSED,
                                       void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_graphics_msg *msg = evdata;
    remoteDomainBuildEventGraphicsHelper(conn, &msg->msg, msg->callbackID);
}

static void
remoteDomainBuildEventControlErrorHelper(virConnectPtr conn,
                                         remote_domain_event_control_error_msg *msg,
                                         int callbackID)
{
4877
    struct private_data *priv = conn->privateData;
4878
    virDomainPtr dom;
4879
    virObjectEventPtr event = NULL;
4880

4881
    dom = get_nonnull_domain(conn, msg->dom);
4882
    if (!dom)
4883
        return;
4884 4885 4886

    event = virDomainEventControlErrorNewFromDom(dom);

4887
    virObjectUnref(dom);
4888

4889
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907
}
static void
remoteDomainBuildEventControlError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                   virNetClientPtr client ATTRIBUTE_UNUSED,
                                   void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_control_error_msg *msg = evdata;
    remoteDomainBuildEventControlErrorHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackControlError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                           virNetClientPtr client ATTRIBUTE_UNUSED,
                                           void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_control_error_msg *msg = evdata;
    remoteDomainBuildEventControlErrorHelper(conn, &msg->msg, msg->callbackID);
4908 4909 4910
}


4911
static void
4912 4913 4914
remoteDomainBuildEventDiskChangeHelper(virConnectPtr conn,
                                       remote_domain_event_disk_change_msg *msg,
                                       int callbackID)
4915 4916 4917
{
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
4918
    virObjectEventPtr event = NULL;
4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom)
        return;

    event = virDomainEventDiskChangeNewFromDom(dom,
                                               msg->oldSrcPath ? *msg->oldSrcPath : NULL,
                                               msg->newSrcPath ? *msg->newSrcPath : NULL,
                                               msg->devAlias,
                                               msg->reason);

4930
    virObjectUnref(dom);
4931

4932
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4933
}
4934
static void
4935
remoteDomainBuildEventDiskChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
4936 4937 4938 4939
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958
    remote_domain_event_disk_change_msg *msg = evdata;
    remoteDomainBuildEventDiskChangeHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackDiskChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                         virNetClientPtr client ATTRIBUTE_UNUSED,
                                         void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_disk_change_msg *msg = evdata;
    remoteDomainBuildEventDiskChangeHelper(conn, &msg->msg, msg->callbackID);
}


static void
remoteDomainBuildEventTrayChangeHelper(virConnectPtr conn,
                                       remote_domain_event_tray_change_msg *msg,
                                       int callbackID)
{
4959 4960
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
4961
    virObjectEventPtr event = NULL;
4962 4963 4964 4965 4966 4967 4968 4969 4970

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom)
        return;

    event = virDomainEventTrayChangeNewFromDom(dom,
                                               msg->devAlias,
                                               msg->reason);

4971
    virObjectUnref(dom);
4972

4973
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4974
}
O
Osier Yang 已提交
4975
static void
4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987
remoteDomainBuildEventTrayChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_tray_change_msg *msg = evdata;
    remoteDomainBuildEventTrayChangeHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackTrayChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                         virNetClientPtr client ATTRIBUTE_UNUSED,
                                         void *evdata, void *opaque)
O
Osier Yang 已提交
4988 4989
{
    virConnectPtr conn = opaque;
4990 4991 4992 4993 4994 4995 4996
    remote_domain_event_callback_tray_change_msg *msg = evdata;
    remoteDomainBuildEventTrayChangeHelper(conn, &msg->msg, msg->callbackID);
}

static void
remoteDomainBuildEventPMWakeupHelper(virConnectPtr conn,
                                     remote_domain_event_pmwakeup_msg *msg,
E
Eric Blake 已提交
4997 4998
                                     int callbackID,
                                     int reason)
4999
{
O
Osier Yang 已提交
5000 5001
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5002
    virObjectEventPtr event = NULL;
O
Osier Yang 已提交
5003 5004 5005 5006 5007

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom)
        return;

E
Eric Blake 已提交
5008
    event = virDomainEventPMWakeupNewFromDom(dom, reason);
O
Osier Yang 已提交
5009

5010
    virObjectUnref(dom);
O
Osier Yang 已提交
5011

5012
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
O
Osier Yang 已提交
5013
}
O
Osier Yang 已提交
5014
static void
5015 5016 5017 5018 5019 5020
remoteDomainBuildEventPMWakeup(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_pmwakeup_msg *msg = evdata;
E
Eric Blake 已提交
5021
    remoteDomainBuildEventPMWakeupHelper(conn, msg, -1, 0);
5022 5023 5024 5025 5026
}
static void
remoteDomainBuildEventCallbackPMWakeup(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                       virNetClientPtr client ATTRIBUTE_UNUSED,
                                       void *evdata, void *opaque)
O
Osier Yang 已提交
5027 5028
{
    virConnectPtr conn = opaque;
5029
    remote_domain_event_callback_pmwakeup_msg *msg = evdata;
E
Eric Blake 已提交
5030 5031
    remoteDomainBuildEventPMWakeupHelper(conn, &msg->msg, msg->callbackID,
                                         msg->reason);
5032 5033 5034 5035 5036
}

static void
remoteDomainBuildEventPMSuspendHelper(virConnectPtr conn,
                                      remote_domain_event_pmsuspend_msg *msg,
E
Eric Blake 已提交
5037 5038
                                      int callbackID,
                                      int reason)
5039
{
O
Osier Yang 已提交
5040 5041
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5042
    virObjectEventPtr event = NULL;
O
Osier Yang 已提交
5043 5044 5045 5046 5047

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom)
        return;

E
Eric Blake 已提交
5048
    event = virDomainEventPMSuspendNewFromDom(dom, reason);
O
Osier Yang 已提交
5049

5050
    virObjectUnref(dom);
O
Osier Yang 已提交
5051

5052
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
5053 5054 5055 5056 5057 5058 5059 5060
}
static void
remoteDomainBuildEventPMSuspend(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_pmsuspend_msg *msg = evdata;
E
Eric Blake 已提交
5061
    remoteDomainBuildEventPMSuspendHelper(conn, msg, -1, 0);
5062 5063 5064 5065 5066 5067 5068 5069
}
static void
remoteDomainBuildEventCallbackPMSuspend(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                        virNetClientPtr client ATTRIBUTE_UNUSED,
                                        void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_pmsuspend_msg *msg = evdata;
E
Eric Blake 已提交
5070 5071
    remoteDomainBuildEventPMSuspendHelper(conn, &msg->msg, msg->callbackID,
                                          msg->reason);
O
Osier Yang 已提交
5072 5073
}

5074 5075

static void
5076 5077 5078
remoteDomainBuildEventBalloonChangeHelper(virConnectPtr conn,
                                          remote_domain_event_balloon_change_msg *msg,
                                          int callbackID)
5079 5080 5081
{
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5082
    virObjectEventPtr event = NULL;
5083 5084 5085 5086 5087 5088

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom)
        return;

    event = virDomainEventBalloonChangeNewFromDom(dom, msg->actual);
5089
    virObjectUnref(dom);
5090

5091
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
5092
}
5093
static void
5094
remoteDomainBuildEventBalloonChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
5095 5096 5097 5098
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115
    remote_domain_event_balloon_change_msg *msg = evdata;
    remoteDomainBuildEventBalloonChangeHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackBalloonChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                            virNetClientPtr client ATTRIBUTE_UNUSED,
                                            void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_balloon_change_msg *msg = evdata;
    remoteDomainBuildEventBalloonChangeHelper(conn, &msg->msg, msg->callbackID);
}


static void
remoteDomainBuildEventPMSuspendDiskHelper(virConnectPtr conn,
                                          remote_domain_event_pmsuspend_disk_msg *msg,
E
Eric Blake 已提交
5116 5117
                                          int callbackID,
                                          int reason)
5118
{
5119 5120
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5121
    virObjectEventPtr event = NULL;
5122 5123 5124 5125 5126

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom)
        return;

E
Eric Blake 已提交
5127
    event = virDomainEventPMSuspendDiskNewFromDom(dom, reason);
5128

5129
    virObjectUnref(dom);
5130

5131
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
5132
}
5133
static void
5134
remoteDomainBuildEventPMSuspendDisk(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
5135 5136 5137 5138
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
5139
    remote_domain_event_pmsuspend_disk_msg *msg = evdata;
E
Eric Blake 已提交
5140
    remoteDomainBuildEventPMSuspendDiskHelper(conn, msg, -1, 0);
5141 5142 5143 5144 5145 5146 5147 5148
}
static void
remoteDomainBuildEventCallbackPMSuspendDisk(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                            virNetClientPtr client ATTRIBUTE_UNUSED,
                                            void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_pmsuspend_disk_msg *msg = evdata;
E
Eric Blake 已提交
5149 5150
    remoteDomainBuildEventPMSuspendDiskHelper(conn, &msg->msg, msg->callbackID,
                                              msg->reason);
5151 5152 5153 5154 5155 5156 5157 5158
}


static void
remoteDomainBuildEventDeviceRemovedHelper(virConnectPtr conn,
                                          remote_domain_event_device_removed_msg *msg,
                                          int callbackID)
{
5159 5160
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5161
    virObjectEventPtr event = NULL;
5162 5163 5164 5165 5166 5167 5168

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom)
        return;

    event = virDomainEventDeviceRemovedNewFromDom(dom, msg->devAlias);

5169
    virObjectUnref(dom);
5170

5171
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189
}
static void
remoteDomainBuildEventDeviceRemoved(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_device_removed_msg *msg = evdata;
    remoteDomainBuildEventDeviceRemovedHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackDeviceRemoved(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                            virNetClientPtr client ATTRIBUTE_UNUSED,
                                            void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_device_removed_msg *msg = evdata;
    remoteDomainBuildEventDeviceRemovedHelper(conn, &msg->msg, msg->callbackID);
5190 5191
}

5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210
static void
remoteDomainBuildEventCallbackDeviceAdded(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                          virNetClientPtr client ATTRIBUTE_UNUSED,
                                          void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_device_added_msg *msg = evdata;
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom)
        return;

    event = virDomainEventDeviceAddedNewFromDom(dom, msg->devAlias);

    virObjectUnref(dom);

5211
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5212
}
5213

5214 5215 5216 5217 5218 5219 5220

static void
remoteDomainBuildEventCallbackDeviceRemovalFailed(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                                  virNetClientPtr client ATTRIBUTE_UNUSED,
                                                  void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
5221
    remote_domain_event_callback_device_removal_failed_msg *msg = evdata;
5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;

    if (!(dom = get_nonnull_domain(conn, msg->dom)))
        return;

    event = virDomainEventDeviceRemovalFailedNewFromDom(dom, msg->devAlias);

    virObjectUnref(dom);

5233
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5234 5235
}

5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248
static void
remoteDomainBuildEventCallbackTunable(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                      virNetClientPtr client ATTRIBUTE_UNUSED,
                                      void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_tunable_msg *msg = evdata;
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virObjectEventPtr event = NULL;

5249 5250 5251 5252
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) msg->params.params_val,
                                  msg->params.params_len,
                                  REMOTE_DOMAIN_EVENT_TUNABLE_MAX,
                                  &params, &nparams) < 0)
5253 5254 5255 5256 5257 5258 5259 5260 5261 5262
        return;

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom) {
        virTypedParamsFree(params, nparams);
        return;
    }

    event = virDomainEventTunableNewFromDom(dom, params, nparams);

5263
    virObjectUnref(dom);
5264

5265
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5266 5267 5268
}


5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285
static void
remoteDomainBuildEventCallbackAgentLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                             virNetClientPtr client ATTRIBUTE_UNUSED,
                                             void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_agent_lifecycle_msg *msg = evdata;
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;

    if (!(dom = get_nonnull_domain(conn, msg->dom)))
        return;

    event = virDomainEventAgentLifecycleNewFromDom(dom, msg->state,
                                                   msg->reason);

5286
    virObjectUnref(dom);
5287

5288
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5289 5290
}

5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310

static void
remoteDomainBuildEventCallbackMigrationIteration(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                                 void *evdata,
                                                 void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_migration_iteration_msg *msg = evdata;
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;

    if (!(dom = get_nonnull_domain(conn, msg->dom)))
        return;

    event = virDomainEventMigrationIterationNewFromDom(dom, msg->iteration);

    virObjectUnref(dom);

5311
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5312 5313 5314
}


J
Jiri Denemark 已提交
5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343
static void
remoteDomainBuildEventCallbackJobCompleted(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                           virNetClientPtr client ATTRIBUTE_UNUSED,
                                           void *evdata,
                                           void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_job_completed_msg *msg = evdata;
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;
    virTypedParameterPtr params = NULL;
    int nparams = 0;

    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) msg->params.params_val,
                                  msg->params.params_len,
                                  REMOTE_DOMAIN_JOB_STATS_MAX,
                                  &params, &nparams) < 0)
        return;

    if (!(dom = get_nonnull_domain(conn, msg->dom))) {
        virTypedParamsFree(params, nparams);
        return;
    }

    event = virDomainEventJobCompletedNewFromDom(dom, params, nparams);

    virObjectUnref(dom);

5344
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
J
Jiri Denemark 已提交
5345 5346 5347
}


5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365
static void
remoteDomainBuildEventCallbackMetadataChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                             virNetClientPtr client ATTRIBUTE_UNUSED,
                                             void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_metadata_change_msg *msg = evdata;
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;

    if (!(dom = get_nonnull_domain(conn, msg->dom)))
        return;

    event = virDomainEventMetadataChangeNewFromDom(dom, msg->type, msg->nsuri ? *msg->nsuri : NULL);

    virObjectUnref(dom);

5366
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5367 5368 5369
}


5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384
static void
remoteNetworkBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_network_event_lifecycle_msg *msg = evdata;
    virNetworkPtr net;
    virObjectEventPtr event = NULL;

    net = get_nonnull_network(conn, msg->net);
    if (!net)
        return;

5385 5386
    event = virNetworkEventLifecycleNew(net->name, net->uuid, msg->event,
                                        msg->detail);
5387
    virObjectUnref(net);
5388

5389
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5390 5391
}

5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410
static void
remoteStoragePoolBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                     virNetClientPtr client ATTRIBUTE_UNUSED,
                                     void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_storage_pool_event_lifecycle_msg *msg = evdata;
    virStoragePoolPtr pool;
    virObjectEventPtr event = NULL;

    pool = get_nonnull_storage_pool(conn, msg->pool);
    if (!pool)
        return;

    event = virStoragePoolEventLifecycleNew(pool->name, pool->uuid, msg->event,
                                            msg->detail);
    virObjectUnref(pool);

5411
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5412
}
5413

5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431
static void
remoteStoragePoolBuildEventRefresh(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                   virNetClientPtr client ATTRIBUTE_UNUSED,
                                   void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_storage_pool_event_refresh_msg *msg = evdata;
    virStoragePoolPtr pool;
    virObjectEventPtr event = NULL;

    pool = get_nonnull_storage_pool(conn, msg->pool);
    if (!pool)
        return;

    event = virStoragePoolEventRefreshNew(pool->name, pool->uuid);
    virObjectUnref(pool);

5432
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5433 5434
}

5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453
static void
remoteNodeDeviceBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_node_device_event_lifecycle_msg *msg = evdata;
    virNodeDevicePtr dev;
    virObjectEventPtr event = NULL;

    dev = get_nonnull_node_device(conn, msg->dev);
    if (!dev)
        return;

    event = virNodeDeviceEventLifecycleNew(dev->name, msg->event,
                                           msg->detail);
    virObjectUnref(dev);

5454
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5455 5456
}

5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474
static void
remoteNodeDeviceBuildEventUpdate(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_node_device_event_update_msg *msg = evdata;
    virNodeDevicePtr dev;
    virObjectEventPtr event = NULL;

    dev = get_nonnull_node_device(conn, msg->dev);
    if (!dev)
        return;

    event = virNodeDeviceEventUpdateNew(dev->name);
    virObjectUnref(dev);

5475
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5476 5477
}

5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496
static void
remoteSecretBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_secret_event_lifecycle_msg *msg = evdata;
    virSecretPtr secret;
    virObjectEventPtr event = NULL;

    secret = get_nonnull_secret(conn, msg->secret);
    if (!secret)
        return;

    event = virSecretEventLifecycleNew(secret->uuid, secret->usageType, secret->usageID,
                                       msg->event, msg->detail);
    virObjectUnref(secret);

5497
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5498 5499
}

5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517
static void
remoteSecretBuildEventValueChanged(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                   virNetClientPtr client ATTRIBUTE_UNUSED,
                                   void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_secret_event_value_changed_msg *msg = evdata;
    virSecretPtr secret;
    virObjectEventPtr event = NULL;

    secret = get_nonnull_secret(conn, msg->secret);
    if (!secret)
        return;

    event = virSecretEventValueChangedNew(secret->uuid, secret->usageType, secret->usageID);
    virObjectUnref(secret);

5518
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5519 5520
}

5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539
static void
remoteDomainBuildQemuMonitorEvent(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                  virNetClientPtr client ATTRIBUTE_UNUSED,
                                  void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    qemu_domain_monitor_event_msg *msg = evdata;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;

    dom = get_nonnull_domain(conn, msg->dom);
    if (!dom)
        return;

    event = virDomainQemuMonitorEventNew(dom->id, dom->name, dom->uuid,
                                         msg->event, msg->seconds,
                                         msg->micros,
                                         msg->details ? *msg->details : NULL);
5540
    virObjectUnref(dom);
5541

5542
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5543 5544 5545
}


5546
static unsigned char *
5547 5548
remoteSecretGetValue(virSecretPtr secret, size_t *value_size,
                     unsigned int flags, unsigned int internalFlags)
5549 5550 5551 5552
{
    unsigned char *rv = NULL;
    remote_secret_get_value_args args;
    remote_secret_get_value_ret ret;
5553
    struct private_data *priv = secret->conn->privateData;
5554

5555
    remoteDriverLock(priv);
5556

5557 5558
    /* internalFlags intentionally do not go over the wire */
    if (internalFlags) {
5559
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("no internalFlags support"));
5560 5561 5562
        goto done;
    }

5563
    make_nonnull_secret(&args.secret, secret);
5564 5565
    args.flags = flags;

5566 5567 5568 5569
    memset(&ret, 0, sizeof(ret));
    if (call(secret->conn, priv, 0, REMOTE_PROC_SECRET_GET_VALUE,
             (xdrproc_t) xdr_remote_secret_get_value_args, (char *) &args,
             (xdrproc_t) xdr_remote_secret_get_value_ret, (char *) &ret) == -1)
5570 5571 5572 5573 5574
        goto done;

    *value_size = ret.value.value_len;
    rv = (unsigned char *) ret.value.value_val; /* Caller frees. */

5575
 done:
5576
    remoteDriverUnlock(priv);
5577 5578 5579
    return rv;
}

5580

5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600
static void
remoteDomainBuildEventBlockThreshold(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                     virNetClientPtr client ATTRIBUTE_UNUSED,
                                     void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_block_threshold_msg *msg = evdata;
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;

    if (!(dom = get_nonnull_domain(conn, msg->dom)))
        return;

    event = virDomainEventBlockThresholdNewFromDom(dom, msg->dev,
                                                   msg->path ? *msg->path : NULL,
                                                   msg->threshold, msg->excess);

    virObjectUnref(dom);

5601
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5602 5603 5604
}


5605 5606 5607 5608 5609
static int
remoteStreamSend(virStreamPtr st,
                 const char *data,
                 size_t nbytes)
{
5610
    VIR_DEBUG("st=%p data=%p nbytes=%zu", st, data, nbytes);
5611
    struct private_data *priv = st->conn->privateData;
5612
    virNetClientStreamPtr privst = st->privateData;
5613
    int rv;
5614

5615 5616 5617
    remoteDriverLock(priv);
    priv->localUses++;
    remoteDriverUnlock(priv);
5618

5619 5620 5621 5622 5623
    rv = virNetClientStreamSendPacket(privst,
                                      priv->client,
                                      VIR_NET_CONTINUE,
                                      data,
                                      nbytes);
5624

5625 5626
    remoteDriverLock(priv);
    priv->localUses--;
5627 5628 5629 5630 5631 5632
    remoteDriverUnlock(priv);
    return rv;
}


static int
5633 5634 5635 5636
remoteStreamRecvFlags(virStreamPtr st,
                      char *data,
                      size_t nbytes,
                      unsigned int flags)
5637
{
5638
    VIR_DEBUG("st=%p data=%p nbytes=%zu flags=0x%x",
5639
              st, data, nbytes, flags);
5640
    struct private_data *priv = st->conn->privateData;
5641
    virNetClientStreamPtr privst = st->privateData;
5642
    int rv;
5643

5644
    virCheckFlags(VIR_STREAM_RECV_STOP_AT_HOLE, -1);
5645

5646 5647 5648
    remoteDriverLock(priv);
    priv->localUses++;
    remoteDriverUnlock(priv);
5649

5650 5651 5652 5653
    rv = virNetClientStreamRecvPacket(privst,
                                      priv->client,
                                      data,
                                      nbytes,
5654 5655
                                      (st->flags & VIR_STREAM_NONBLOCK),
                                      flags);
5656

5657
    VIR_DEBUG("Done %d", rv);
5658

5659 5660
    remoteDriverLock(priv);
    priv->localUses--;
5661 5662 5663 5664
    remoteDriverUnlock(priv);
    return rv;
}

5665 5666 5667 5668 5669 5670 5671 5672
static int
remoteStreamRecv(virStreamPtr st,
                 char *data,
                 size_t nbytes)
{
    return remoteStreamRecvFlags(st, data, nbytes, 0);
}

5673 5674 5675 5676 5677 5678

static int
remoteStreamSendHole(virStreamPtr st,
                     long long length,
                     unsigned int flags)
{
5679
    VIR_DEBUG("st=%p length=%lld flags=0x%x",
5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700
              st, length, flags);
    struct private_data *priv = st->conn->privateData;
    virNetClientStreamPtr privst = st->privateData;
    int rv;

    remoteDriverLock(priv);
    priv->localUses++;
    remoteDriverUnlock(priv);

    rv = virNetClientStreamSendHole(privst,
                                    priv->client,
                                    length,
                                    flags);

    remoteDriverLock(priv);
    priv->localUses--;
    remoteDriverUnlock(priv);
    return rv;
}


5701 5702 5703 5704 5705 5706 5707 5708 5709
static int
remoteStreamRecvHole(virStreamPtr st,
                     long long *length,
                     unsigned int flags)
{
    struct private_data *priv = st->conn->privateData;
    virNetClientStreamPtr privst = st->privateData;
    int rv;

5710
    VIR_DEBUG("st=%p length=%p flags=0x%x",
5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727
              st, length, flags);

    virCheckFlags(0, -1);

    remoteDriverLock(priv);
    priv->localUses++;
    remoteDriverUnlock(priv);

    rv = virNetClientStreamRecvHole(priv->client, privst, length);

    remoteDriverLock(priv);
    priv->localUses--;
    remoteDriverUnlock(priv);
    return rv;
}


5728 5729 5730 5731 5732 5733
struct remoteStreamCallbackData {
    virStreamPtr st;
    virStreamEventCallback cb;
    void *opaque;
    virFreeCallback ff;
};
5734

5735 5736 5737
static void remoteStreamEventCallback(virNetClientStreamPtr stream ATTRIBUTE_UNUSED,
                                      int events,
                                      void *opaque)
5738
{
5739
    struct remoteStreamCallbackData *cbdata = opaque;
5740

5741
    (cbdata->cb)(cbdata->st, events, cbdata->opaque);
5742 5743 5744
}


5745
static void remoteStreamCallbackFree(void *opaque)
5746
{
5747 5748 5749 5750 5751
    struct remoteStreamCallbackData *cbdata = opaque;

    if (!cbdata->cb && cbdata->ff)
        (cbdata->ff)(cbdata->opaque);

5752
    virObjectUnref(cbdata->st);
5753
    VIR_FREE(opaque);
5754 5755 5756
}


5757
static int
5758 5759 5760 5761 5762
remoteStreamEventAddCallback(virStreamPtr st,
                             int events,
                             virStreamEventCallback cb,
                             void *opaque,
                             virFreeCallback ff)
5763
{
5764
    struct private_data *priv = st->conn->privateData;
5765
    virNetClientStreamPtr privst = st->privateData;
5766
    int ret = -1;
5767
    struct remoteStreamCallbackData *cbdata;
5768

5769
    if (VIR_ALLOC(cbdata) < 0)
5770 5771 5772 5773 5774
        return -1;
    cbdata->cb = cb;
    cbdata->opaque = opaque;
    cbdata->ff = ff;
    cbdata->st = st;
5775 5776
    virStreamRef(st);

5777
    remoteDriverLock(priv);
5778

5779 5780 5781 5782 5783 5784 5785 5786
    if ((ret = virNetClientStreamEventAddCallback(privst,
                                                  events,
                                                  remoteStreamEventCallback,
                                                  cbdata,
                                                  remoteStreamCallbackFree)) < 0) {
        VIR_FREE(cbdata);
        goto cleanup;
    }
5787

5788
 cleanup:
5789
    remoteDriverUnlock(priv);
5790
    /* coverity[leaked_storage] - cbdata is not leaked */
5791
    return ret;
5792 5793
}

5794

5795
static int
5796 5797
remoteStreamEventUpdateCallback(virStreamPtr st,
                                int events)
5798
{
5799
    struct private_data *priv = st->conn->privateData;
5800
    virNetClientStreamPtr privst = st->privateData;
5801 5802 5803 5804
    int ret = -1;

    remoteDriverLock(priv);

5805
    ret = virNetClientStreamEventUpdateCallback(privst, events);
5806

5807 5808 5809
    remoteDriverUnlock(priv);
    return ret;
}
5810 5811 5812


static int
5813
remoteStreamEventRemoveCallback(virStreamPtr st)
5814
{
5815
    struct private_data *priv = st->conn->privateData;
5816
    virNetClientStreamPtr privst = st->privateData;
5817 5818 5819 5820
    int ret = -1;

    remoteDriverLock(priv);

5821
    ret = virNetClientStreamEventRemoveCallback(privst);
5822 5823 5824

    remoteDriverUnlock(priv);
    return ret;
5825 5826
}

5827

5828
static int
5829
remoteStreamCloseInt(virStreamPtr st, bool streamAbort)
5830 5831
{
    struct private_data *priv = st->conn->privateData;
5832
    virNetClientStreamPtr privst = st->privateData;
5833 5834 5835 5836
    int ret = -1;

    remoteDriverLock(priv);

5837 5838 5839
    priv->localUses++;
    remoteDriverUnlock(priv);

5840 5841
    ret = virNetClientStreamSendPacket(privst,
                                       priv->client,
5842
                                       streamAbort ? VIR_NET_ERROR : VIR_NET_OK,
5843 5844
                                       NULL,
                                       0);
5845

5846 5847 5848
    remoteDriverLock(priv);
    priv->localUses--;

5849
    virNetClientRemoveStream(priv->client, privst);
5850 5851 5852 5853 5854

    remoteDriverUnlock(priv);
    return ret;
}

5855

5856
static int
5857
remoteStreamFinish(virStreamPtr st)
5858
{
5859 5860
    return remoteStreamCloseInt(st, false);
}
5861

5862

5863 5864 5865 5866
static int
remoteStreamAbort(virStreamPtr st)
{
    return remoteStreamCloseInt(st, true);
5867 5868 5869 5870 5871
}


static virStreamDriver remoteStreamDrv = {
    .streamRecv = remoteStreamRecv,
5872
    .streamRecvFlags = remoteStreamRecvFlags,
5873
    .streamSend = remoteStreamSend,
5874
    .streamSendHole = remoteStreamSendHole,
5875
    .streamRecvHole = remoteStreamRecvHole,
5876 5877
    .streamFinish = remoteStreamFinish,
    .streamAbort = remoteStreamAbort,
5878 5879 5880
    .streamEventAddCallback = remoteStreamEventAddCallback,
    .streamEventUpdateCallback = remoteStreamEventUpdateCallback,
    .streamEventRemoveCallback = remoteStreamEventRemoveCallback,
5881 5882
};

5883

5884 5885 5886 5887 5888 5889 5890
static int
remoteConnectDomainEventRegisterAny(virConnectPtr conn,
                                    virDomainPtr dom,
                                    int eventID,
                                    virConnectDomainEventGenericCallback callback,
                                    void *opaque,
                                    virFreeCallback freecb)
5891 5892 5893 5894
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
    int callbackID;
5895
    int count;
5896
    remote_nonnull_domain domain;
5897 5898 5899

    remoteDriverLock(priv);

5900 5901 5902
    if ((count = virDomainEventStateRegisterClient(conn, priv->eventState,
                                                   dom, eventID, callback,
                                                   opaque, freecb, false,
5903
                                                   &callbackID,
5904
                                                   priv->serverEventFilter)) < 0)
5905
        goto done;
5906 5907 5908

    /* If this is the first callback for this eventID, we need to enable
     * events on the server */
5909
    if (count == 1) {
5910
        if (priv->serverEventFilter) {
5911 5912 5913 5914 5915 5916 5917 5918 5919 5920
            remote_connect_domain_event_callback_register_any_args args;
            remote_connect_domain_event_callback_register_any_ret ret;

            args.eventID = eventID;
            if (dom) {
                make_nonnull_domain(&domain, dom);
                args.dom = &domain;
            } else {
                args.dom = NULL;
            }
5921

5922 5923 5924 5925 5926
            memset(&ret, 0, sizeof(ret));
            if (call(conn, priv, 0, REMOTE_PROC_CONNECT_DOMAIN_EVENT_CALLBACK_REGISTER_ANY,
                     (xdrproc_t) xdr_remote_connect_domain_event_callback_register_any_args, (char *) &args,
                     (xdrproc_t) xdr_remote_connect_domain_event_callback_register_any_ret, (char *) &ret) == -1) {
                virObjectEventStateDeregisterID(conn, priv->eventState,
5927
                                                callbackID, false);
5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940
                goto done;
            }
            virObjectEventStateSetRemote(conn, priv->eventState, callbackID,
                                         ret.callbackID);
        } else {
            remote_connect_domain_event_register_any_args args;

            args.eventID = eventID;

            if (call(conn, priv, 0, REMOTE_PROC_CONNECT_DOMAIN_EVENT_REGISTER_ANY,
                     (xdrproc_t) xdr_remote_connect_domain_event_register_any_args, (char *) &args,
                     (xdrproc_t) xdr_void, (char *)NULL) == -1) {
                virObjectEventStateDeregisterID(conn, priv->eventState,
5941
                                                callbackID, false);
5942 5943
                goto done;
            }
5944 5945 5946 5947 5948
        }
    }

    rv = callbackID;

5949
 done:
5950 5951 5952 5953 5954
    remoteDriverUnlock(priv);
    return rv;
}


5955 5956 5957
static int
remoteConnectDomainEventDeregisterAny(virConnectPtr conn,
                                      int callbackID)
5958 5959 5960 5961
{
    struct private_data *priv = conn->privateData;
    int rv = -1;
    int eventID;
5962
    int remoteID;
5963
    int count;
5964 5965 5966

    remoteDriverLock(priv);

5967
    if ((eventID = virObjectEventStateEventID(conn, priv->eventState,
5968
                                              callbackID, &remoteID)) < 0)
5969 5970
        goto done;

5971
    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
5972
                                                 callbackID, true)) < 0)
5973
        goto done;
5974 5975 5976

    /* If that was the last callback for this eventID, we need to disable
     * events on the server */
5977
    if (count == 0) {
5978
        if (priv->serverEventFilter) {
5979
            remote_connect_domain_event_callback_deregister_any_args args;
5980

5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997
            args.callbackID = remoteID;

            if (call(conn, priv, 0,
                     REMOTE_PROC_CONNECT_DOMAIN_EVENT_CALLBACK_DEREGISTER_ANY,
                     (xdrproc_t) xdr_remote_connect_domain_event_callback_deregister_any_args, (char *) &args,
                     (xdrproc_t) xdr_void, (char *) NULL) == -1)
                goto done;
        } else {
            remote_connect_domain_event_deregister_any_args args;

            args.eventID = eventID;

            if (call(conn, priv, 0, REMOTE_PROC_CONNECT_DOMAIN_EVENT_DEREGISTER_ANY,
                     (xdrproc_t) xdr_remote_connect_domain_event_deregister_any_args, (char *) &args,
                     (xdrproc_t) xdr_void, (char *) NULL) == -1)
                goto done;
        }
5998 5999 6000 6001
    }

    rv = 0;

6002
 done:
6003 6004 6005 6006
    remoteDriverUnlock(priv);
    return rv;
}

6007

6008 6009
/*----------------------------------------------------------------------*/

C
Chris Lalancette 已提交
6010
static int
6011
remoteDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd,
6012
                               char **result, unsigned int flags)
C
Chris Lalancette 已提交
6013 6014
{
    int rv = -1;
6015 6016
    qemu_domain_monitor_command_args args;
    qemu_domain_monitor_command_ret ret;
C
Chris Lalancette 已提交
6017 6018 6019 6020
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

6021
    make_nonnull_domain(&args.dom, domain);
C
Chris Lalancette 已提交
6022 6023 6024
    args.cmd = (char *)cmd;
    args.flags = flags;

6025
    memset(&ret, 0, sizeof(ret));
6026 6027 6028
    if (call(domain->conn, priv, REMOTE_CALL_QEMU, QEMU_PROC_DOMAIN_MONITOR_COMMAND,
             (xdrproc_t) xdr_qemu_domain_monitor_command_args, (char *) &args,
             (xdrproc_t) xdr_qemu_domain_monitor_command_ret, (char *) &ret) == -1)
C
Chris Lalancette 已提交
6029 6030
        goto done;

6031
    if (VIR_STRDUP(*result, ret.result) < 0)
C
Chris Lalancette 已提交
6032 6033 6034 6035
        goto cleanup;

    rv = 0;

6036
 cleanup:
6037
    xdr_free((xdrproc_t) xdr_qemu_domain_monitor_command_ret, (char *) &ret);
C
Chris Lalancette 已提交
6038

6039
 done:
C
Chris Lalancette 已提交
6040 6041 6042 6043
    remoteDriverUnlock(priv);
    return rv;
}

6044 6045 6046

static char *
remoteDomainMigrateBegin3(virDomainPtr domain,
6047
                          const char *xmlin,
6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063
                          char **cookieout,
                          int *cookieoutlen,
                          unsigned long flags,
                          const char *dname,
                          unsigned long resource)
{
    char *rv = NULL;
    remote_domain_migrate_begin3_args args;
    remote_domain_migrate_begin3_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));
    memset(&ret, 0, sizeof(ret));

6064
    make_nonnull_domain(&args.dom, domain);
6065
    args.xmlin = xmlin == NULL ? NULL : (char **) &xmlin;
6066 6067 6068 6069
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;

6070 6071 6072
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_BEGIN3,
             (xdrproc_t) xdr_remote_domain_migrate_begin3_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_begin3_ret, (char *) &ret) == -1)
6073 6074 6075 6076
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6077 6078
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6079 6080 6081 6082 6083 6084 6085 6086
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = ret.xml; /* caller frees */

6087
 done:
6088 6089 6090
    remoteDriverUnlock(priv);
    return rv;

6091
 error:
6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto done;
}


static int
remoteDomainMigratePrepare3(virConnectPtr dconn,
                            const char *cookiein,
                            int cookieinlen,
                            char **cookieout,
                            int *cookieoutlen,
                            const char *uri_in,
                            char **uri_out,
                            unsigned long flags,
                            const char *dname,
                            unsigned long resource,
                            const char *dom_xml)
{
    int rv = -1;
    remote_domain_migrate_prepare3_args args;
    remote_domain_migrate_prepare3_ret ret;
    struct private_data *priv = dconn->privateData;

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));
    memset(&ret, 0, sizeof(ret));

    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.uri_in = uri_in == NULL ? NULL : (char **) &uri_in;
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;
    args.dom_xml = (char *) dom_xml;

6128 6129 6130 6131
    memset(&ret, 0, sizeof(ret));
    if (call(dconn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_PREPARE3,
             (xdrproc_t) xdr_remote_domain_migrate_prepare3_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_prepare3_ret, (char *) &ret) == -1)
6132 6133 6134 6135
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6136 6137
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6138 6139 6140 6141 6142 6143 6144
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }
    if (ret.uri_out) {
        if (!uri_out) {
6145 6146
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores uri_out"));
6147 6148 6149 6150 6151 6152 6153
            goto error;
        }
        *uri_out = *ret.uri_out; /* Caller frees. */
    }

    rv = 0;

6154
 done:
O
Osier Yang 已提交
6155
    VIR_FREE(ret.uri_out);
6156 6157
    remoteDriverUnlock(priv);
    return rv;
6158
 error:
6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181
    VIR_FREE(ret.cookie_out.cookie_out_val);
    if (ret.uri_out)
        VIR_FREE(*ret.uri_out);
    goto done;
}


static int
remoteDomainMigratePrepareTunnel3(virConnectPtr dconn,
                                  virStreamPtr st,
                                  const char *cookiein,
                                  int cookieinlen,
                                  char **cookieout,
                                  int *cookieoutlen,
                                  unsigned long flags,
                                  const char *dname,
                                  unsigned long resource,
                                  const char *dom_xml)
{
    struct private_data *priv = dconn->privateData;
    int rv = -1;
    remote_domain_migrate_prepare_tunnel3_args args;
    remote_domain_migrate_prepare_tunnel3_ret ret;
6182
    virNetClientStreamPtr netst;
6183 6184 6185 6186 6187 6188

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));
    memset(&ret, 0, sizeof(ret));

6189
    if (!(netst = virNetClientStreamNew(priv->remoteProgram,
6190
                                        REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3,
6191 6192
                                        priv->counter,
                                        false)))
6193 6194
        goto done;

6195
    if (virNetClientAddStream(priv->client, netst) < 0) {
6196
        virObjectUnref(netst);
6197 6198 6199
        goto done;
    }

6200
    st->driver = &remoteStreamDrv;
6201
    st->privateData = netst;
6202
    st->ff = virObjectFreeCallback;
6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213

    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;
    args.dom_xml = (char *) dom_xml;

    if (call(dconn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3,
             (xdrproc_t) xdr_remote_domain_migrate_prepare_tunnel3_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_prepare_tunnel3_ret, (char *) &ret) == -1) {
6214
        virNetClientRemoveStream(priv->client, netst);
6215
        virObjectUnref(netst);
6216 6217 6218 6219 6220
        goto done;
    }

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6221 6222
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6223 6224 6225 6226 6227 6228 6229 6230
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = 0;

6231
 done:
6232 6233 6234
    remoteDriverUnlock(priv);
    return rv;

6235
 error:
6236 6237 6238 6239 6240 6241 6242
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto done;
}


static int
remoteDomainMigratePerform3(virDomainPtr dom,
6243
                            const char *xmlin,
6244 6245 6246 6247
                            const char *cookiein,
                            int cookieinlen,
                            char **cookieout,
                            int *cookieoutlen,
6248
                            const char *dconnuri,
6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265
                            const char *uri,
                            unsigned long flags,
                            const char *dname,
                            unsigned long resource)
{
    int rv = -1;
    remote_domain_migrate_perform3_args args;
    remote_domain_migrate_perform3_ret ret;
    struct private_data *priv = dom->conn->privateData;

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));
    memset(&ret, 0, sizeof(ret));

    make_nonnull_domain(&args.dom, dom);

6266
    args.xmlin = xmlin == NULL ? NULL : (char **) &xmlin;
6267 6268 6269 6270
    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
6271 6272
    args.uri = uri == NULL ? NULL : (char **) &uri;
    args.dconnuri = dconnuri == NULL ? NULL : (char **) &dconnuri;
6273 6274
    args.resource = resource;

6275 6276 6277
    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_PERFORM3,
             (xdrproc_t) xdr_remote_domain_migrate_perform3_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_perform3_ret, (char *) &ret) == -1)
6278 6279 6280 6281
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6282 6283
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6284 6285 6286 6287 6288 6289 6290 6291
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = 0;

6292
 done:
6293 6294 6295
    remoteDriverUnlock(priv);
    return rv;

6296
 error:
6297 6298 6299 6300 6301
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto done;
}


6302
static virDomainPtr
6303 6304 6305 6306 6307 6308
remoteDomainMigrateFinish3(virConnectPtr dconn,
                           const char *dname,
                           const char *cookiein,
                           int cookieinlen,
                           char **cookieout,
                           int *cookieoutlen,
6309
                           const char *dconnuri,
6310 6311
                           const char *uri,
                           unsigned long flags,
6312
                           int cancelled)
6313 6314 6315 6316
{
    remote_domain_migrate_finish3_args args;
    remote_domain_migrate_finish3_ret ret;
    struct private_data *priv = dconn->privateData;
6317
    virDomainPtr rv = NULL;
6318 6319 6320 6321 6322 6323 6324 6325 6326

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));
    memset(&ret, 0, sizeof(ret));

    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.dname = (char *) dname;
6327 6328
    args.uri = uri == NULL ? NULL : (char **) &uri;
    args.dconnuri = dconnuri == NULL ? NULL : (char **) &dconnuri;
6329 6330 6331
    args.flags = flags;
    args.cancelled = cancelled;

6332 6333 6334
    if (call(dconn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_FINISH3,
             (xdrproc_t) xdr_remote_domain_migrate_finish3_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_finish3_ret, (char *) &ret) == -1)
6335 6336
        goto done;

6337
    rv = get_nonnull_domain(dconn, ret.dom);
6338 6339 6340

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6341 6342
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6343 6344 6345 6346 6347 6348 6349 6350
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
        ret.cookie_out.cookie_out_val = NULL;
        ret.cookie_out.cookie_out_len = 0;
    }

6351
    xdr_free((xdrproc_t) &xdr_remote_domain_migrate_finish3_ret, (char *) &ret);
6352

6353
 done:
6354 6355 6356
    remoteDriverUnlock(priv);
    return rv;

6357
 error:
6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto done;
}


static int
remoteDomainMigrateConfirm3(virDomainPtr domain,
                            const char *cookiein,
                            int cookieinlen,
                            unsigned long flags,
                            int cancelled)
{
    int rv = -1;
    remote_domain_migrate_confirm3_args args;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));

6378
    make_nonnull_domain(&args.dom, domain);
6379 6380 6381 6382 6383
    args.cookie_in.cookie_in_len = cookieinlen;
    args.cookie_in.cookie_in_val = (char *) cookiein;
    args.flags = flags;
    args.cancelled = cancelled;

6384 6385 6386
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_CONFIRM3,
             (xdrproc_t) xdr_remote_domain_migrate_confirm3_args, (char *) &args,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
6387 6388 6389 6390
        goto done;

    rv = 0;

6391
 done:
6392 6393 6394 6395 6396
    remoteDriverUnlock(priv);
    return rv;
}


6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447
static int
remoteConnectGetCPUModelNames(virConnectPtr conn,
                              const char *arch,
                              char ***models,
                              unsigned int flags)
{
    int rv = -1;
    size_t i;
    char **retmodels = NULL;
    remote_connect_get_cpu_model_names_args args;
    remote_connect_get_cpu_model_names_ret ret;

    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.arch = (char *) arch;
    args.need_results = !!models;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_GET_CPU_MODEL_NAMES,
             (xdrproc_t) xdr_remote_connect_get_cpu_model_names_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_connect_get_cpu_model_names_ret,
             (char *) &ret) < 0)
        goto done;

    /* Check the length of the returned list carefully. */
    if (ret.models.models_len > REMOTE_CONNECT_CPU_MODELS_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many model names '%d' for limit '%d'"),
                       ret.models.models_len,
                       REMOTE_CONNECT_CPU_MODELS_MAX);
        goto cleanup;
    }

    if (models) {
        if (VIR_ALLOC_N(retmodels, ret.models.models_len + 1) < 0)
            goto cleanup;

        for (i = 0; i < ret.models.models_len; i++) {
            retmodels[i] = ret.models.models_val[i];
            ret.models.models_val[i] = NULL;
        }
        *models = retmodels;
        retmodels = NULL;
    }

    rv = ret.ret;

6448
 cleanup:
6449
    virStringListFree(retmodels);
6450 6451 6452

    xdr_free((xdrproc_t) xdr_remote_connect_get_cpu_model_names_ret, (char *) &ret);

6453
 done:
6454 6455 6456 6457 6458
    remoteDriverUnlock(priv);
    return rv;
}


6459 6460 6461 6462 6463 6464 6465 6466 6467
static int
remoteDomainOpenGraphics(virDomainPtr dom,
                         unsigned int idx,
                         int fd,
                         unsigned int flags)
{
    int rv = -1;
    remote_domain_open_graphics_args args;
    struct private_data *priv = dom->conn->privateData;
6468 6469
    int fdin[] = { fd };
    size_t fdinlen = ARRAY_CARDINALITY(fdin);
6470 6471 6472

    remoteDriverLock(priv);

6473
    make_nonnull_domain(&args.dom, dom);
6474 6475 6476
    args.idx = idx;
    args.flags = flags;

6477 6478 6479 6480 6481 6482
    if (callFull(dom->conn, priv, 0,
                 fdin, fdinlen,
                 NULL, NULL,
                 REMOTE_PROC_DOMAIN_OPEN_GRAPHICS,
                 (xdrproc_t) xdr_remote_domain_open_graphics_args, (char *) &args,
                 (xdrproc_t) xdr_void, NULL) == -1)
6483 6484 6485 6486
        goto done;

    rv = 0;

6487
 done:
6488 6489 6490 6491 6492 6493
    remoteDriverUnlock(priv);

    return rv;
}


6494 6495 6496 6497 6498 6499
static int
remoteDomainOpenGraphicsFD(virDomainPtr dom,
                           unsigned int idx,
                           unsigned int flags)
{
    int rv = -1;
6500
    remote_domain_open_graphics_fd_args args;
6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519
    struct private_data *priv = dom->conn->privateData;
    int *fdout = NULL;
    size_t fdoutlen = 0;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, dom);
    args.idx = idx;
    args.flags = flags;

    if (callFull(dom->conn, priv, 0,
                 NULL, 0,
                 &fdout, &fdoutlen,
                 REMOTE_PROC_DOMAIN_OPEN_GRAPHICS_FD,
                 (xdrproc_t) xdr_remote_domain_open_graphics_fd_args, (char *) &args,
                 (xdrproc_t) xdr_void, NULL) == -1)
        goto done;

    if (fdoutlen != 1) {
6520 6521 6522 6523 6524 6525 6526 6527 6528
        if (fdoutlen) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("too many file descriptors received"));
            while (fdoutlen)
                VIR_FORCE_CLOSE(fdout[--fdoutlen]);
        } else {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("no file descriptor received"));
        }
6529 6530
        goto done;
    }
6531
    rv = fdout[0];
6532 6533

 done:
6534
    VIR_FREE(fdout);
6535 6536 6537 6538 6539 6540
    remoteDriverUnlock(priv);

    return rv;
}


6541
static int
6542
remoteConnectSetKeepAlive(virConnectPtr conn, int interval, unsigned int count)
6543 6544 6545 6546 6547 6548
{
    struct private_data *priv = conn->privateData;
    int ret = -1;

    remoteDriverLock(priv);
    if (!virNetClientKeepAliveIsSupported(priv->client)) {
6549 6550 6551
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("the caller doesn't support keepalive protocol;"
                         " perhaps it's missing event loop implementation"));
6552 6553 6554 6555 6556 6557 6558 6559
        goto cleanup;
    }

    if (!priv->serverKeepAlive) {
        ret = 1;
        goto cleanup;
    }

6560 6561 6562 6563 6564 6565
    if (interval > 0) {
        ret = virNetClientKeepAliveStart(priv->client, interval, count);
    } else {
        virNetClientKeepAliveStop(priv->client);
        ret = 0;
    }
6566

6567
 cleanup:
6568 6569 6570 6571 6572
    remoteDriverUnlock(priv);
    return ret;
}


6573
static int
6574
remoteConnectIsAlive(virConnectPtr conn)
6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589
{
    struct private_data *priv = conn->privateData;
    bool ret;

    remoteDriverLock(priv);
    ret = virNetClientIsOpen(priv->client);
    remoteDriverUnlock(priv);

    if (ret)
        return 1;
    else
        return 0;
}


6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606
static int
remoteDomainGetDiskErrors(virDomainPtr dom,
                          virDomainDiskErrorPtr errors,
                          unsigned int maxerrors,
                          unsigned int flags)
{
    int rv = -1;
    struct private_data *priv = dom->conn->privateData;
    remote_domain_get_disk_errors_args args;
    remote_domain_get_disk_errors_ret ret;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, dom);
    args.maxerrors = maxerrors;
    args.flags = flags;

6607
    memset(&ret, 0, sizeof(ret));
6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624

    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_DISK_ERRORS,
             (xdrproc_t) xdr_remote_domain_get_disk_errors_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_disk_errors_ret,
             (char *) &ret) == -1)
        goto done;

    if (remoteDeserializeDomainDiskErrors(ret.errors.errors_val,
                                          ret.errors.errors_len,
                                          REMOTE_DOMAIN_DISK_ERRORS_MAX,
                                          errors,
                                          maxerrors) < 0)
        goto cleanup;

    rv = ret.nerrors;

6625
 cleanup:
6626 6627
    xdr_free((xdrproc_t) xdr_remote_domain_get_disk_errors_ret, (char *) &ret);

6628
 done:
6629 6630 6631 6632
    remoteDriverUnlock(priv);
    return rv;
}

6633
#include "remote_client_bodies.h"
6634
#include "lxc_client_bodies.h"
6635
#include "qemu_client_bodies.h"
6636

6637 6638 6639 6640 6641
/*
 * Serial a set of arguments into a method call message,
 * send that to the server and wait for reply
 */
static int
6642 6643 6644 6645 6646 6647 6648 6649 6650 6651
callFull(virConnectPtr conn ATTRIBUTE_UNUSED,
         struct private_data *priv,
         unsigned int flags,
         int *fdin,
         size_t fdinlen,
         int **fdout,
         size_t *fdoutlen,
         int proc_nr,
         xdrproc_t args_filter, char *args,
         xdrproc_t ret_filter, char *ret)
6652
{
6653
    int rv;
6654
    virNetClientProgramPtr prog;
6655 6656 6657 6658
    int counter = priv->counter++;
    virNetClientPtr client = priv->client;
    priv->localUses++;

6659 6660 6661 6662 6663 6664 6665
    if (flags & REMOTE_CALL_QEMU)
        prog = priv->qemuProgram;
    else if (flags & REMOTE_CALL_LXC)
        prog = priv->lxcProgram;
    else
        prog = priv->remoteProgram;

6666 6667 6668 6669 6670 6671 6672 6673 6674
    /* Unlock, so that if we get any async events/stream data
     * while processing the RPC, we don't deadlock when our
     * callbacks for those are invoked
     */
    remoteDriverUnlock(priv);
    rv = virNetClientProgramCall(prog,
                                 client,
                                 counter,
                                 proc_nr,
6675 6676
                                 fdinlen, fdin,
                                 fdoutlen, fdout,
6677 6678 6679 6680
                                 args_filter, args,
                                 ret_filter, ret);
    remoteDriverLock(priv);
    priv->localUses--;
6681

6682
    return rv;
6683 6684
}

6685
static int
6686 6687 6688 6689 6690 6691
call(virConnectPtr conn,
     struct private_data *priv,
     unsigned int flags,
     int proc_nr,
     xdrproc_t args_filter, char *args,
     xdrproc_t ret_filter, char *ret)
6692
{
6693 6694 6695 6696 6697 6698
    return callFull(conn, priv, flags,
                    NULL, 0,
                    NULL, NULL,
                    proc_nr,
                    args_filter, args,
                    ret_filter, ret);
6699 6700
}

6701

6702
static int
6703 6704 6705 6706
remoteDomainGetInterfaceParameters(virDomainPtr domain,
                                   const char *device,
                                   virTypedParameterPtr params, int *nparams,
                                   unsigned int flags)
6707 6708 6709 6710 6711 6712 6713 6714
{
    int rv = -1;
    remote_domain_get_interface_parameters_args args;
    remote_domain_get_interface_parameters_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

6715
    make_nonnull_domain(&args.dom, domain);
6716 6717 6718 6719
    args.device = (char *)device;
    args.nparams = *nparams;
    args.flags = flags;

6720 6721 6722 6723
    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_INTERFACE_PARAMETERS,
             (xdrproc_t) xdr_remote_domain_get_interface_parameters_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_interface_parameters_ret, (char *) &ret) == -1)
6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734
        goto done;

    /* Handle the case when the caller does not know the number of parameters
     * and is asking for the number of parameters supported
     */
    if (*nparams == 0) {
        *nparams = ret.nparams;
        rv = 0;
        goto cleanup;
    }

6735 6736 6737 6738 6739
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX,
                                  &params,
                                  nparams) < 0)
6740 6741 6742 6743
        goto cleanup;

    rv = 0;

6744
 cleanup:
6745 6746
    xdr_free((xdrproc_t) xdr_remote_domain_get_interface_parameters_ret,
             (char *) &ret);
6747
 done:
6748 6749 6750 6751
    remoteDriverUnlock(priv);
    return rv;
}

6752

6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768
static int
remoteNodeGetMemoryParameters(virConnectPtr conn,
                              virTypedParameterPtr params,
                              int *nparams,
                              unsigned int flags)
{
    int rv = -1;
    remote_node_get_memory_parameters_args args;
    remote_node_get_memory_parameters_ret ret;
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.nparams = *nparams;
    args.flags = flags;

6769 6770 6771 6772
    memset(&ret, 0, sizeof(ret));
    if (call(conn, priv, 0, REMOTE_PROC_NODE_GET_MEMORY_PARAMETERS,
             (xdrproc_t) xdr_remote_node_get_memory_parameters_args, (char *) &args,
             (xdrproc_t) xdr_remote_node_get_memory_parameters_ret, (char *) &ret) == -1)
6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783
        goto done;

    /* Handle the case when the caller does not know the number of parameters
     * and is asking for the number of parameters supported
     */
    if (*nparams == 0) {
        *nparams = ret.nparams;
        rv = 0;
        goto cleanup;
    }

6784 6785 6786 6787 6788
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_NODE_MEMORY_PARAMETERS_MAX,
                                  &params,
                                  nparams) < 0)
6789 6790 6791 6792
        goto cleanup;

    rv = 0;

6793
 cleanup:
6794 6795
    xdr_free((xdrproc_t) xdr_remote_node_get_memory_parameters_ret,
             (char *) &ret);
6796
 done:
6797 6798 6799 6800
    remoteDriverUnlock(priv);
    return rv;
}

6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839

static int
remoteNodeGetSEVInfo(virConnectPtr conn,
                     virTypedParameterPtr *params,
                     int *nparams,
                     unsigned int flags)
{
    int rv = -1;
    remote_node_get_sev_info_args args;
    remote_node_get_sev_info_ret ret;
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(conn, priv, 0, REMOTE_PROC_NODE_GET_SEV_INFO,
             (xdrproc_t) xdr_remote_node_get_sev_info_args, (char *) &args,
             (xdrproc_t) xdr_remote_node_get_sev_info_ret, (char *) &ret) == -1)
        goto done;

    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_NODE_SEV_INFO_MAX,
                                  params,
                                  nparams) < 0)
        goto cleanup;

    rv = 0;

 cleanup:
    xdr_free((xdrproc_t) xdr_remote_node_get_sev_info_ret, (char *) &ret);
 done:
    remoteDriverUnlock(priv);
    return rv;
}


6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852
static int
remoteNodeGetCPUMap(virConnectPtr conn,
                    unsigned char **cpumap,
                    unsigned int *online,
                    unsigned int flags)
{
    int rv = -1;
    remote_node_get_cpu_map_args args;
    remote_node_get_cpu_map_ret ret;
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

6853 6854
    args.need_map = !!cpumap;
    args.need_online = !!online;
6855 6856
    args.flags = flags;

6857
    memset(&ret, 0, sizeof(ret));
6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868
    if (call(conn, priv, 0, REMOTE_PROC_NODE_GET_CPU_MAP,
             (xdrproc_t) xdr_remote_node_get_cpu_map_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_node_get_cpu_map_ret,
             (char *) &ret) == -1)
        goto done;

    if (ret.ret < 0)
        goto cleanup;

    if (cpumap) {
6869
        if (VIR_ALLOC_N(*cpumap, ret.cpumap.cpumap_len) < 0)
6870 6871 6872 6873 6874 6875 6876 6877 6878
            goto cleanup;
        memcpy(*cpumap, ret.cpumap.cpumap_val, ret.cpumap.cpumap_len);
    }

    if (online)
        *online = ret.online;

    rv = ret.ret;

6879
 cleanup:
6880
    xdr_free((xdrproc_t) xdr_remote_node_get_cpu_map_ret, (char *) &ret);
6881
 done:
6882 6883 6884 6885
    remoteDriverUnlock(priv);
    return rv;
}

6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913

static int
remoteDomainLxcOpenNamespace(virDomainPtr domain,
                             int **fdlist,
                             unsigned int flags)
{
    int rv = -1;
    lxc_domain_open_namespace_args args;
    struct private_data *priv = domain->conn->privateData;
    size_t nfds = 0;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, domain);
    args.flags = flags;

    *fdlist = NULL;

    if (callFull(domain->conn, priv, REMOTE_CALL_LXC,
                 NULL, 0,
                 fdlist, &nfds,
                 LXC_PROC_DOMAIN_OPEN_NAMESPACE,
                 (xdrproc_t) xdr_lxc_domain_open_namespace_args, (char *) &args,
                 (xdrproc_t) xdr_void, NULL) == -1)
        goto done;

    rv = nfds;

6914
 done:
6915 6916 6917 6918
    remoteDriverUnlock(priv);
    return rv;
}

6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943
static int
remoteDomainGetJobStats(virDomainPtr domain,
                        int *type,
                        virTypedParameterPtr *params,
                        int *nparams,
                        unsigned int flags)
{
    int rv = -1;
    remote_domain_get_job_stats_args args;
    remote_domain_get_job_stats_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, domain);
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_JOB_STATS,
             (xdrproc_t) xdr_remote_domain_get_job_stats_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_job_stats_ret, (char *) &ret) == -1)
        goto done;

    *type = ret.type;

6944 6945 6946 6947
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_JOB_STATS_MAX,
                                  params, nparams) < 0)
6948 6949 6950 6951
        goto cleanup;

    rv = 0;

6952
 cleanup:
6953 6954
    xdr_free((xdrproc_t) xdr_remote_domain_get_job_stats_ret,
             (char *) &ret);
6955
 done:
6956 6957 6958 6959
    remoteDriverUnlock(priv);
    return rv;
}

6960

6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981
static char *
remoteDomainMigrateBegin3Params(virDomainPtr domain,
                                virTypedParameterPtr params,
                                int nparams,
                                char **cookieout,
                                int *cookieoutlen,
                                unsigned int flags)
{
    char *rv = NULL;
    remote_domain_migrate_begin3_params_args args;
    remote_domain_migrate_begin3_params_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));
    memset(&ret, 0, sizeof(ret));

    make_nonnull_domain(&args.dom, domain);
    args.flags = flags;

6982 6983 6984 6985 6986 6987 6988
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

6989 6990
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &args.params.params_val,
6991 6992
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016
        xdr_free((xdrproc_t) xdr_remote_domain_migrate_begin3_params_args,
                 (char *) &args);
        goto cleanup;
    }

    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_BEGIN3_PARAMS,
             (xdrproc_t) xdr_remote_domain_migrate_begin3_params_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_begin3_params_ret,
             (char *) &ret) == -1)
        goto cleanup;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = ret.xml; /* caller frees */

7017
 cleanup:
7018 7019
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7020 7021 7022
    remoteDriverUnlock(priv);
    return rv;

7023
 error:
7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto cleanup;
}


static int
remoteDomainMigratePrepare3Params(virConnectPtr dconn,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  const char *cookiein,
                                  int cookieinlen,
                                  char **cookieout,
                                  int *cookieoutlen,
                                  char **uri_out,
                                  unsigned int flags)
{
    int rv = -1;
    remote_domain_migrate_prepare3_params_args args;
    remote_domain_migrate_prepare3_params_ret ret;
    struct private_data *priv = dconn->privateData;

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));
    memset(&ret, 0, sizeof(ret));

7050 7051 7052 7053 7054 7055 7056
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

7057 7058
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &args.params.params_val,
7059 7060
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096
        xdr_free((xdrproc_t) xdr_remote_domain_migrate_prepare3_params_args,
                 (char *) &args);
        goto cleanup;
    }

    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.flags = flags;

    if (call(dconn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_PREPARE3_PARAMS,
             (xdrproc_t) xdr_remote_domain_migrate_prepare3_params_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_prepare3_params_ret,
             (char *) &ret) == -1)
        goto cleanup;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }
    if (ret.uri_out) {
        if (!uri_out) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores uri_out"));
            goto error;
        }
        *uri_out = *ret.uri_out; /* Caller frees. */
    }

    rv = 0;

7097
 cleanup:
7098 7099
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7100 7101 7102 7103
    VIR_FREE(ret.uri_out);
    remoteDriverUnlock(priv);
    return rv;

7104
 error:
7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133
    VIR_FREE(ret.cookie_out.cookie_out_val);
    if (ret.uri_out)
        VIR_FREE(*ret.uri_out);
    goto cleanup;
}


static int
remoteDomainMigratePrepareTunnel3Params(virConnectPtr dconn,
                                        virStreamPtr st,
                                        virTypedParameterPtr params,
                                        int nparams,
                                        const char *cookiein,
                                        int cookieinlen,
                                        char **cookieout,
                                        int *cookieoutlen,
                                        unsigned int flags)
{
    struct private_data *priv = dconn->privateData;
    int rv = -1;
    remote_domain_migrate_prepare_tunnel3_params_args args;
    remote_domain_migrate_prepare_tunnel3_params_ret ret;
    virNetClientStreamPtr netst;

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));
    memset(&ret, 0, sizeof(ret));

7134 7135 7136 7137 7138 7139 7140
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

7141 7142 7143 7144
    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.flags = flags;

7145 7146
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &args.params.params_val,
7147 7148
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
7149 7150 7151 7152 7153
        xdr_free((xdrproc_t) xdr_remote_domain_migrate_prepare_tunnel3_params_args,
                 (char *) &args);
        goto cleanup;
    }

7154
    if (!(netst = virNetClientStreamNew(priv->remoteProgram,
7155
                                        REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3_PARAMS,
7156 7157
                                        priv->counter,
                                        false)))
7158 7159 7160 7161 7162 7163 7164 7165 7166
        goto cleanup;

    if (virNetClientAddStream(priv->client, netst) < 0) {
        virObjectUnref(netst);
        goto cleanup;
    }

    st->driver = &remoteStreamDrv;
    st->privateData = netst;
7167
    st->ff = virObjectFreeCallback;
7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190

    if (call(dconn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3_PARAMS,
             (xdrproc_t) xdr_remote_domain_migrate_prepare_tunnel3_params_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_prepare_tunnel3_params_ret,
             (char *) &ret) == -1) {
        virNetClientRemoveStream(priv->client, netst);
        virObjectUnref(netst);
        goto cleanup;
    }

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = 0;

7191
 cleanup:
7192 7193
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7194 7195 7196
    remoteDriverUnlock(priv);
    return rv;

7197
 error:
7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto cleanup;
}


static int
remoteDomainMigratePerform3Params(virDomainPtr dom,
                                  const char *dconnuri,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  const char *cookiein,
                                  int cookieinlen,
                                  char **cookieout,
                                  int *cookieoutlen,
                                  unsigned int flags)
{
    int rv = -1;
    remote_domain_migrate_perform3_params_args args;
    remote_domain_migrate_perform3_params_ret ret;
    struct private_data *priv = dom->conn->privateData;

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));
    memset(&ret, 0, sizeof(ret));

7224 7225 7226 7227 7228 7229 7230
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

7231 7232 7233 7234 7235 7236
    make_nonnull_domain(&args.dom, dom);
    args.dconnuri = dconnuri == NULL ? NULL : (char **) &dconnuri;
    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.flags = flags;

7237 7238
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &args.params.params_val,
7239 7240
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264
        xdr_free((xdrproc_t) xdr_remote_domain_migrate_perform3_params_args,
                 (char *) &args);
        goto cleanup;
    }

    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_PERFORM3_PARAMS,
             (xdrproc_t) xdr_remote_domain_migrate_perform3_params_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_perform3_params_ret,
             (char *) &ret) == -1)
        goto cleanup;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = 0;

7265
 cleanup:
7266 7267
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7268 7269 7270
    remoteDriverUnlock(priv);
    return rv;

7271
 error:
7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto cleanup;
}


static virDomainPtr
remoteDomainMigrateFinish3Params(virConnectPtr dconn,
                                 virTypedParameterPtr params,
                                 int nparams,
                                 const char *cookiein,
                                 int cookieinlen,
                                 char **cookieout,
                                 int *cookieoutlen,
                                 unsigned int flags,
                                 int cancelled)
{
    remote_domain_migrate_finish3_params_args args;
    remote_domain_migrate_finish3_params_ret ret;
    struct private_data *priv = dconn->privateData;
    virDomainPtr rv = NULL;

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));
    memset(&ret, 0, sizeof(ret));

7298 7299 7300 7301 7302 7303 7304
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

7305 7306 7307 7308 7309
    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.flags = flags;
    args.cancelled = cancelled;

7310 7311
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &args.params.params_val,
7312 7313
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342
        xdr_free((xdrproc_t) xdr_remote_domain_migrate_finish3_params_args,
                 (char *) &args);
        goto cleanup;
    }

    if (call(dconn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_FINISH3_PARAMS,
             (xdrproc_t) xdr_remote_domain_migrate_finish3_params_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_migrate_finish3_params_ret,
             (char *) &ret) == -1)
        goto cleanup;

    rv = get_nonnull_domain(dconn, ret.dom);

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
        ret.cookie_out.cookie_out_val = NULL;
        ret.cookie_out.cookie_out_len = 0;
    }

    xdr_free((xdrproc_t) &xdr_remote_domain_migrate_finish3_params_ret,
             (char *) &ret);

7343
 cleanup:
7344 7345
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7346 7347 7348
    remoteDriverUnlock(priv);
    return rv;

7349
 error:
7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto cleanup;
}


static int
remoteDomainMigrateConfirm3Params(virDomainPtr domain,
                                  virTypedParameterPtr params,
                                  int nparams,
                                  const char *cookiein,
                                  int cookieinlen,
                                  unsigned int flags,
                                  int cancelled)
{
    int rv = -1;
    remote_domain_migrate_confirm3_params_args args;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    memset(&args, 0, sizeof(args));

7372 7373 7374 7375 7376 7377 7378
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

7379 7380 7381 7382 7383 7384
    make_nonnull_domain(&args.dom, domain);
    args.cookie_in.cookie_in_len = cookieinlen;
    args.cookie_in.cookie_in_val = (char *) cookiein;
    args.flags = flags;
    args.cancelled = cancelled;

7385 7386
    if (virTypedParamsSerialize(params, nparams,
                                (virTypedParameterRemotePtr *) &args.params.params_val,
7387 7388
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400
        xdr_free((xdrproc_t) xdr_remote_domain_migrate_confirm3_params_args,
                 (char *) &args);
        goto cleanup;
    }

    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_CONFIRM3_PARAMS,
             (xdrproc_t) xdr_remote_domain_migrate_confirm3_params_args,
             (char *) &args, (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto cleanup;

    rv = 0;

7401
 cleanup:
7402 7403
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7404 7405 7406 7407 7408
    remoteDriverUnlock(priv);
    return rv;
}


7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436
static virDomainPtr
remoteDomainCreateXMLWithFiles(virConnectPtr conn, const char *xml_desc,
                               unsigned int nfiles, int *files, unsigned int flags)
{
    virDomainPtr rv = NULL;
    struct private_data *priv = conn->privateData;
    remote_domain_create_xml_with_files_args args;
    remote_domain_create_xml_with_files_ret ret;

    remoteDriverLock(priv);

    args.xml_desc = (char *)xml_desc;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));

    if (callFull(conn, priv, 0,
                 files, nfiles,
                 NULL, NULL,
                 REMOTE_PROC_DOMAIN_CREATE_XML_WITH_FILES,
                 (xdrproc_t)xdr_remote_domain_create_xml_with_files_args, (char *)&args,
                 (xdrproc_t)xdr_remote_domain_create_xml_with_files_ret, (char *)&ret) == -1) {
        goto done;
    }

    rv = get_nonnull_domain(conn, ret.dom);
    xdr_free((xdrproc_t)xdr_remote_domain_create_xml_with_files_ret, (char *)&ret);

7437
 done:
7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472
    remoteDriverUnlock(priv);
    return rv;
}


static int
remoteDomainCreateWithFiles(virDomainPtr dom,
                            unsigned int nfiles, int *files,
                            unsigned int flags)
{
    int rv = -1;
    struct private_data *priv = dom->conn->privateData;
    remote_domain_create_with_files_args args;
    remote_domain_create_with_files_ret ret;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, dom);
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));

    if (callFull(dom->conn, priv, 0,
                 files, nfiles,
                 NULL, NULL,
                 REMOTE_PROC_DOMAIN_CREATE_WITH_FILES,
                 (xdrproc_t)xdr_remote_domain_create_with_files_args, (char *)&args,
                 (xdrproc_t)xdr_remote_domain_create_with_files_ret, (char *)&ret) == -1) {
        goto done;
    }

    dom->id = ret.dom.id;
    xdr_free((xdrproc_t) &xdr_remote_domain_create_with_files_ret, (char *) &ret);
    rv = 0;

7473
 done:
7474 7475 7476 7477
    remoteDriverUnlock(priv);
    return rv;
}

7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510
static int
remoteDomainGetTime(virDomainPtr dom,
                    long long *seconds,
                    unsigned int *nseconds,
                    unsigned int flags)
{
    int rv = -1;
    struct private_data *priv = dom->conn->privateData;
    remote_domain_get_time_args args;
    remote_domain_get_time_ret ret;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, dom);
    args.flags = flags;

    *seconds = *nseconds = 0;

    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_TIME,
             (xdrproc_t) xdr_remote_domain_get_time_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_get_time_ret, (char *) &ret) == -1)
        goto cleanup;

    *seconds = ret.seconds;
    *nseconds = ret.nseconds;
    xdr_free((xdrproc_t) &xdr_remote_domain_get_time_ret, (char *) &ret);
    rv = 0;

 cleanup:
    remoteDriverUnlock(priv);
    return rv;
}

7511

M
Michal Privoznik 已提交
7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534
static int
remoteNodeGetFreePages(virConnectPtr conn,
                       unsigned int npages,
                       unsigned int *pages,
                       int startCell,
                       unsigned int cellCount,
                       unsigned long long *counts,
                       unsigned int flags)
{
    int rv = -1;
    remote_node_get_free_pages_args args;
    remote_node_get_free_pages_ret ret;
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    if (npages * cellCount > REMOTE_NODE_MAX_CELLS) {
        virReportError(VIR_ERR_RPC,
                       _("too many NUMA cells: %d > %d"),
                       npages * cellCount, REMOTE_NODE_MAX_CELLS);
        goto done;
    }

7535
    args.pages.pages_val = (u_int *) pages;
M
Michal Privoznik 已提交
7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558
    args.pages.pages_len = npages;
    args.startCell = startCell;
    args.cellCount = cellCount;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(conn, priv, 0, REMOTE_PROC_NODE_GET_FREE_PAGES,
             (xdrproc_t) xdr_remote_node_get_free_pages_args, (char *)&args,
             (xdrproc_t) xdr_remote_node_get_free_pages_ret, (char *)&ret) == -1)
        goto done;

    memcpy(counts, ret.counts.counts_val, ret.counts.counts_len * sizeof(*counts));

    xdr_free((xdrproc_t) xdr_remote_node_get_free_pages_ret, (char *) &ret);

    rv = ret.counts.counts_len;

 done:
    remoteDriverUnlock(priv);
    return rv;
}


7559 7560 7561 7562 7563 7564 7565 7566
/* Copy contents of remote_network_dhcp_lease to virNetworkDHCPLeasePtr */
static int
remoteSerializeDHCPLease(virNetworkDHCPLeasePtr lease_dst, remote_network_dhcp_lease *lease_src)
{
    lease_dst->expirytime = lease_src->expirytime;
    lease_dst->type = lease_src->type;
    lease_dst->prefix = lease_src->prefix;

7567
    if (VIR_STRDUP(lease_dst->iface, lease_src->iface) < 0)
7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610
        goto error;

    if (VIR_STRDUP(lease_dst->ipaddr, lease_src->ipaddr) < 0)
        goto error;

    if (lease_src->mac) {
        if (VIR_STRDUP(lease_dst->mac, *lease_src->mac) < 0)
            goto error;
    } else {
        lease_src->mac = NULL;
    }

    if (lease_src->iaid) {
        if (VIR_STRDUP(lease_dst->iaid, *lease_src->iaid) < 0)
            goto error;
    } else {
        lease_src->iaid = NULL;
    }

    if (lease_src->hostname) {
        if (VIR_STRDUP(lease_dst->hostname, *lease_src->hostname) < 0)
            goto error;
    } else {
        lease_src->hostname = NULL;
    }

    if (lease_src->clientid) {
        if (VIR_STRDUP(lease_dst->clientid, *lease_src->clientid) < 0)
            goto error;
    } else {
        lease_src->clientid = NULL;
    }

    return 0;

 error:
    virNetworkDHCPLeaseFree(lease_dst);
    return -1;
}


static int
remoteNetworkGetDHCPLeases(virNetworkPtr net,
7611
                           const char *mac,
7612 7613 7614 7615 7616
                           virNetworkDHCPLeasePtr **leases,
                           unsigned int flags)
{
    int rv = -1;
    size_t i;
7617
    struct private_data *priv = net->conn->privateData;
7618 7619 7620 7621 7622 7623 7624
    remote_network_get_dhcp_leases_args args;
    remote_network_get_dhcp_leases_ret ret;

    virNetworkDHCPLeasePtr *leases_ret = NULL;
    remoteDriverLock(priv);

    make_nonnull_network(&args.net, net);
7625
    args.mac = mac ? (char **) &mac : NULL;
7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676
    args.flags = flags;
    args.need_results = !!leases;

    memset(&ret, 0, sizeof(ret));

    if (call(net->conn, priv, 0, REMOTE_PROC_NETWORK_GET_DHCP_LEASES,
             (xdrproc_t)xdr_remote_network_get_dhcp_leases_args, (char *)&args,
             (xdrproc_t)xdr_remote_network_get_dhcp_leases_ret, (char *)&ret) == -1)
        goto done;

    if (ret.leases.leases_len > REMOTE_NETWORK_DHCP_LEASES_MAX) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Number of leases is %d, which exceeds max limit: %d"),
                       ret.leases.leases_len, REMOTE_NETWORK_DHCP_LEASES_MAX);
        goto cleanup;
    }

    if (leases) {
        if (ret.leases.leases_len &&
            VIR_ALLOC_N(leases_ret, ret.leases.leases_len + 1) < 0)
            goto cleanup;

        for (i = 0; i < ret.leases.leases_len; i++) {
            if (VIR_ALLOC(leases_ret[i]) < 0)
                goto cleanup;

            if (remoteSerializeDHCPLease(leases_ret[i], &ret.leases.leases_val[i]) < 0)
                goto cleanup;
        }

        *leases = leases_ret;
        leases_ret = NULL;
    }

    rv = ret.ret;

 cleanup:
    if (leases_ret) {
        for (i = 0; i < ret.leases.leases_len; i++)
            virNetworkDHCPLeaseFree(leases_ret[i]);
        VIR_FREE(leases_ret);
    }
    xdr_free((xdrproc_t)xdr_remote_network_get_dhcp_leases_ret,
             (char *) &ret);

 done:
    remoteDriverUnlock(priv);
    return rv;
}


7677 7678 7679 7680 7681 7682 7683 7684
static int
remoteConnectGetAllDomainStats(virConnectPtr conn,
                               virDomainPtr *doms,
                               unsigned int ndoms,
                               unsigned int stats,
                               virDomainStatsRecordPtr **retStats,
                               unsigned int flags)
{
7685
    struct private_data *priv = conn->privateData;
7686 7687 7688 7689
    int rv = -1;
    size_t i;
    remote_connect_get_all_domain_stats_args args;
    remote_connect_get_all_domain_stats_ret ret;
7690
    virDomainStatsRecordPtr elem = NULL;
7691 7692
    virDomainStatsRecordPtr *tmpret = NULL;

7693 7694
    memset(&args, 0, sizeof(args));

7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738
    if (ndoms) {
        if (VIR_ALLOC_N(args.doms.doms_val, ndoms) < 0)
            goto cleanup;

        for (i = 0; i < ndoms; i++)
            make_nonnull_domain(args.doms.doms_val + i, doms[i]);
    }
    args.doms.doms_len = ndoms;

    args.stats = stats;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));

    remoteDriverLock(priv);
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_GET_ALL_DOMAIN_STATS,
             (xdrproc_t)xdr_remote_connect_get_all_domain_stats_args, (char *)&args,
             (xdrproc_t)xdr_remote_connect_get_all_domain_stats_ret, (char *)&ret) == -1) {
        remoteDriverUnlock(priv);
        goto cleanup;
    }
    remoteDriverUnlock(priv);

    if (ret.retStats.retStats_len > REMOTE_DOMAIN_LIST_MAX) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Number of stats entries is %d, which exceeds max limit: %d"),
                       ret.retStats.retStats_len, REMOTE_DOMAIN_LIST_MAX);
        goto cleanup;
    }

    *retStats = NULL;

    if (VIR_ALLOC_N(tmpret, ret.retStats.retStats_len + 1) < 0)
        goto cleanup;

    for (i = 0; i < ret.retStats.retStats_len; i++) {
        remote_domain_stats_record *rec = ret.retStats.retStats_val + i;

        if (VIR_ALLOC(elem) < 0)
            goto cleanup;

        if (!(elem->dom = get_nonnull_domain(conn, rec->dom)))
            goto cleanup;

7739 7740 7741 7742 7743
        if (virTypedParamsDeserialize((virTypedParameterRemotePtr) rec->params.params_val,
                                      rec->params.params_len,
                                      REMOTE_CONNECT_GET_ALL_DOMAIN_STATS_MAX,
                                      &elem->params,
                                      &elem->nparams))
7744 7745 7746
            goto cleanup;

        tmpret[i] = elem;
7747
        elem = NULL;
7748 7749 7750 7751 7752 7753 7754
    }

    *retStats = tmpret;
    tmpret = NULL;
    rv = ret.retStats.retStats_len;

 cleanup:
7755 7756 7757 7758
    if (elem) {
        virObjectUnref(elem->dom);
        VIR_FREE(elem);
    }
7759
    virDomainStatsRecordListFree(tmpret);
7760
    VIR_FREE(args.doms.doms_val);
7761 7762 7763 7764 7765 7766
    xdr_free((xdrproc_t)xdr_remote_connect_get_all_domain_stats_ret,
             (char *) &ret);

    return rv;
}

M
Michal Privoznik 已提交
7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812

static int
remoteNodeAllocPages(virConnectPtr conn,
                     unsigned int npages,
                     unsigned int *pageSizes,
                     unsigned long long *pageCounts,
                     int startCell,
                     unsigned int cellCount,
                     unsigned int flags)
{
    int rv = -1;
    remote_node_alloc_pages_args args;
    remote_node_alloc_pages_ret ret;
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    if (npages > REMOTE_NODE_MAX_CELLS) {
        virReportError(VIR_ERR_RPC,
                       _("too many NUMA cells: %d > %d"),
                       npages, REMOTE_NODE_MAX_CELLS);
        goto done;
    }

    args.pageSizes.pageSizes_val = (u_int *) pageSizes;
    args.pageSizes.pageSizes_len = npages;
    args.pageCounts.pageCounts_val = (uint64_t *) pageCounts;
    args.pageCounts.pageCounts_len = npages;
    args.startCell = startCell;
    args.cellCount = cellCount;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(conn, priv, 0, REMOTE_PROC_NODE_ALLOC_PAGES,
             (xdrproc_t) xdr_remote_node_alloc_pages_args, (char *) &args,
             (xdrproc_t) xdr_remote_node_alloc_pages_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.ret;

 done:
    remoteDriverUnlock(priv);
    return rv;
}


7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904
static int
remoteDomainGetFSInfo(virDomainPtr dom,
                      virDomainFSInfoPtr **info,
                      unsigned int flags)
{
    int rv = -1;
    size_t i, j, len;
    struct private_data *priv = dom->conn->privateData;
    remote_domain_get_fsinfo_args args;
    remote_domain_get_fsinfo_ret ret;
    remote_domain_fsinfo *src;
    virDomainFSInfoPtr *info_ret = NULL;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, dom);

    args.flags = flags;

    memset(&ret, 0, sizeof(ret));

    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_FSINFO,
             (xdrproc_t)xdr_remote_domain_get_fsinfo_args, (char *)&args,
             (xdrproc_t)xdr_remote_domain_get_fsinfo_ret, (char *)&ret) == -1)
        goto done;

    if (ret.info.info_len > REMOTE_DOMAIN_FSINFO_MAX) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Too many mountpoints in fsinfo: %d for limit %d"),
                       ret.info.info_len, REMOTE_DOMAIN_FSINFO_MAX);
        goto cleanup;
    }

    if (info) {
        if (!ret.info.info_len) {
            *info = NULL;
            rv = ret.ret;
            goto cleanup;
        }

        if (VIR_ALLOC_N(info_ret, ret.info.info_len) < 0)
            goto cleanup;

        for (i = 0; i < ret.info.info_len; i++) {
            src = &ret.info.info_val[i];

            if (VIR_ALLOC(info_ret[i]) < 0)
                goto cleanup;

            if (VIR_STRDUP(info_ret[i]->mountpoint, src->mountpoint) < 0)
                goto cleanup;

            if (VIR_STRDUP(info_ret[i]->name, src->name) < 0)
                goto cleanup;

            if (VIR_STRDUP(info_ret[i]->fstype, src->fstype) < 0)
                goto cleanup;

            len = src->dev_aliases.dev_aliases_len;
            info_ret[i]->ndevAlias = len;
            if (len &&
                VIR_ALLOC_N(info_ret[i]->devAlias, len) < 0)
                goto cleanup;

            for (j = 0; j < len; j++) {
                if (VIR_STRDUP(info_ret[i]->devAlias[j],
                               src->dev_aliases.dev_aliases_val[j]) < 0)
                    goto cleanup;
            }
        }

        *info = info_ret;
        info_ret = NULL;
    }

    rv = ret.ret;

 cleanup:
    if (info_ret) {
        for (i = 0; i < ret.info.info_len; i++)
            virDomainFSInfoFree(info_ret[i]);
        VIR_FREE(info_ret);
    }
    xdr_free((xdrproc_t)xdr_remote_domain_get_fsinfo_ret,
             (char *) &ret);

 done:
    remoteDriverUnlock(priv);
    return rv;
}


7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958
static int
remoteDomainInterfaceAddresses(virDomainPtr dom,
                               virDomainInterfacePtr **ifaces,
                               unsigned int source,
                               unsigned int flags)
{
    int rv = -1;
    size_t i, j;

    virDomainInterfacePtr *ifaces_ret = NULL;
    remote_domain_interface_addresses_args args;
    remote_domain_interface_addresses_ret ret;

    struct private_data *priv = dom->conn->privateData;

    args.source = source;
    args.flags = flags;
    make_nonnull_domain(&args.dom, dom);

    remoteDriverLock(priv);

    memset(&ret, 0, sizeof(ret));

    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_INTERFACE_ADDRESSES,
             (xdrproc_t)xdr_remote_domain_interface_addresses_args,
             (char *)&args,
             (xdrproc_t)xdr_remote_domain_interface_addresses_ret,
             (char *)&ret) == -1) {
        goto done;
    }

    if (ret.ifaces.ifaces_len > REMOTE_DOMAIN_INTERFACE_MAX) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Number of interfaces, %d exceeds the max limit: %d"),
                       ret.ifaces.ifaces_len, REMOTE_DOMAIN_INTERFACE_MAX);
        goto cleanup;
    }

    if (ret.ifaces.ifaces_len &&
        VIR_ALLOC_N(ifaces_ret, ret.ifaces.ifaces_len) < 0)
        goto cleanup;

    for (i = 0; i < ret.ifaces.ifaces_len; i++) {
        virDomainInterfacePtr iface;
        remote_domain_interface *iface_ret = &(ret.ifaces.ifaces_val[i]);

        if (VIR_ALLOC(ifaces_ret[i]) < 0)
            goto cleanup;

        iface = ifaces_ret[i];

        if (VIR_STRDUP(iface->name, iface_ret->name) < 0)
            goto cleanup;

7959 7960
        if (iface_ret->hwaddr &&
            VIR_STRDUP(iface->hwaddr, *iface_ret->hwaddr) < 0)
7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006
            goto cleanup;

        if (iface_ret->addrs.addrs_len > REMOTE_DOMAIN_IP_ADDR_MAX) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Number of interfaces, %d exceeds the max limit: %d"),
                           iface_ret->addrs.addrs_len, REMOTE_DOMAIN_IP_ADDR_MAX);
            goto cleanup;
        }

        iface->naddrs = iface_ret->addrs.addrs_len;

        if (iface->naddrs) {
            if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0)
                goto cleanup;

           for (j = 0; j < iface->naddrs; j++) {
                virDomainIPAddressPtr ip_addr = &(iface->addrs[j]);
                remote_domain_ip_addr *ip_addr_ret =
                    &(iface_ret->addrs.addrs_val[j]);

                if (VIR_STRDUP(ip_addr->addr, ip_addr_ret->addr) < 0)
                    goto cleanup;

                ip_addr->prefix = ip_addr_ret->prefix;
                ip_addr->type = ip_addr_ret->type;
            }
        }
    }
    *ifaces = ifaces_ret;
    ifaces_ret = NULL;

    rv = ret.ifaces.ifaces_len;

 cleanup:
    if (ifaces_ret) {
        for (i = 0; i < ret.ifaces.ifaces_len; i++)
            virDomainInterfaceFree(ifaces_ret[i]);
        VIR_FREE(ifaces_ret);
    }
    xdr_free((xdrproc_t)xdr_remote_domain_interface_addresses_ret,
             (char *) &ret);
 done:
    remoteDriverUnlock(priv);
    return rv;
}

8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023
static int
remoteConnectRegisterCloseCallback(virConnectPtr conn,
                                   virConnectCloseFunc cb,
                                   void *opaque,
                                   virFreeCallback freecb)
{
    struct private_data *priv = conn->privateData;
    int ret = -1;

    remoteDriverLock(priv);

    if (virConnectCloseCallbackDataGetCallback(priv->closeCallback) != NULL) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("A close callback is already registered"));
        goto cleanup;
    }

8024
    if (priv->serverCloseCallback &&
8025
        call(conn, priv, 0, REMOTE_PROC_CONNECT_REGISTER_CLOSE_CALLBACK,
8026 8027 8028 8029
             (xdrproc_t) xdr_void, (char *) NULL,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto cleanup;

8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054
    virConnectCloseCallbackDataRegister(priv->closeCallback, conn, cb,
                                        opaque, freecb);
    ret = 0;

 cleanup:
    remoteDriverUnlock(priv);

    return ret;
}

static int
remoteConnectUnregisterCloseCallback(virConnectPtr conn,
                                     virConnectCloseFunc cb)
{
    struct private_data *priv = conn->privateData;
    int ret = -1;

    remoteDriverLock(priv);

    if (virConnectCloseCallbackDataGetCallback(priv->closeCallback) != cb) {
        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                       _("A different callback was requested"));
        goto cleanup;
    }

8055
    if (priv->serverCloseCallback &&
8056
        call(conn, priv, 0, REMOTE_PROC_CONNECT_UNREGISTER_CLOSE_CALLBACK,
8057 8058 8059 8060
             (xdrproc_t) xdr_void, (char *) NULL,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto cleanup;

8061 8062 8063 8064 8065 8066 8067 8068
    virConnectCloseCallbackDataUnregister(priv->closeCallback, cb);
    ret = 0;

 cleanup:
    remoteDriverUnlock(priv);

    return ret;
}
8069

8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110
static int
remoteDomainRename(virDomainPtr dom, const char *new_name, unsigned int flags)
{
    int rv = -1;
    struct private_data *priv = dom->conn->privateData;
    remote_domain_rename_args args;
    remote_domain_rename_ret ret;
    char *tmp = NULL;

    if (VIR_STRDUP(tmp, new_name) < 0)
        return -1;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, dom);
    args.new_name = new_name ? (char **)&new_name : NULL;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));

    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_RENAME,
             (xdrproc_t)xdr_remote_domain_rename_args, (char *)&args,
             (xdrproc_t)xdr_remote_domain_rename_ret, (char *)&ret) == -1) {
        goto done;
    }

    rv = ret.retcode;

    if (rv == 0) {
        VIR_FREE(dom->name);
        dom->name = tmp;
        tmp = NULL;
    }

 done:
    remoteDriverUnlock(priv);
    VIR_FREE(tmp);
    return rv;
}


8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146
static int
remoteStorageVolGetInfoFlags(virStorageVolPtr vol,
                             virStorageVolInfoPtr result,
                             unsigned int flags)
{
    int rv = -1;
    struct private_data *priv = vol->conn->privateData;
    remote_storage_vol_get_info_flags_args args;
    remote_storage_vol_get_info_flags_ret ret;

    remoteDriverLock(priv);

    make_nonnull_storage_vol(&args.vol, vol);
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));

    if (call(vol->conn, priv, 0, REMOTE_PROC_STORAGE_VOL_GET_INFO_FLAGS,
             (xdrproc_t)xdr_remote_storage_vol_get_info_flags_args,
             (char *)&args,
             (xdrproc_t)xdr_remote_storage_vol_get_info_flags_ret,
             (char *)&ret) == -1) {
        goto done;
    }

    result->type = ret.type;
    result->capacity = ret.capacity;
    result->allocation = ret.allocation;
    rv = 0;

 done:
    remoteDriverUnlock(priv);
    return rv;
}


8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185
static int
remoteNetworkPortGetParameters(virNetworkPortPtr port,
                               virTypedParameterPtr *params,
                               int *nparams,
                               unsigned int flags)
{
    int rv = -1;
    struct private_data *priv = port->net->conn->privateData;
    remote_network_port_get_parameters_args args;
    remote_network_port_get_parameters_ret ret;

    remoteDriverLock(priv);

    make_nonnull_network_port(&args.port, port);
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(port->net->conn, priv, 0, REMOTE_PROC_NETWORK_PORT_GET_PARAMETERS,
             (xdrproc_t) xdr_remote_network_port_get_parameters_args, (char *) &args,
             (xdrproc_t) xdr_remote_network_port_get_parameters_ret, (char *) &ret) == -1)
        goto done;

    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_NETWORK_PORT_PARAMETERS_MAX,
                                  params,
                                  nparams) < 0)
        goto cleanup;

    rv = 0;

 cleanup:
    xdr_free((xdrproc_t) xdr_remote_network_port_get_parameters_ret, (char *) &ret);
 done:
    remoteDriverUnlock(priv);
    return rv;
}


8186 8187 8188 8189 8190 8191
/* get_nonnull_domain and get_nonnull_network turn an on-wire
 * (name, uuid) pair into virDomainPtr or virNetworkPtr object.
 * These can return NULL if underlying memory allocations fail,
 * but if they do then virterror_internal.has been set.
 */
static virDomainPtr
8192
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
8193
{
8194
    return virGetDomain(conn, domain.name, BAD_CAST domain.uuid, domain.id);
8195 8196 8197
}

static virNetworkPtr
8198
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
8199
{
8200
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
8201 8202
}

8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215
static virNetworkPortPtr
get_nonnull_network_port(virConnectPtr conn, remote_nonnull_network_port port)
{
    virNetworkPortPtr ret;
    virNetworkPtr net;
    net = virGetNetwork(conn, port.net.name, BAD_CAST port.net.uuid);
    if (!net)
        return NULL;
    ret = virGetNetworkPort(net, BAD_CAST port.uuid);
    virObjectUnref(net);
    return ret;
}

D
Daniel Veillard 已提交
8216
static virInterfacePtr
8217
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
8218
{
8219
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
8220 8221
}

8222
static virStoragePoolPtr
8223
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
8224
{
8225 8226
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
8227 8228 8229
}

static virStorageVolPtr
8230
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
8231
{
8232 8233
    return virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                            NULL, NULL);
8234 8235
}

8236
static virNodeDevicePtr
8237
get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev)
8238 8239 8240 8241
{
    return virGetNodeDevice(conn, dev.name);
}

8242
static virSecretPtr
8243
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
8244
{
8245
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
8246 8247
}

8248
static virNWFilterPtr
8249
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
8250
{
8251
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
8252 8253
}

8254 8255 8256 8257 8258 8259
static virNWFilterBindingPtr
get_nonnull_nwfilter_binding(virConnectPtr conn, remote_nonnull_nwfilter_binding binding)
{
    return virGetNWFilterBinding(conn, binding.portdev, binding.filtername);
}

C
Chris Lalancette 已提交
8260
static virDomainSnapshotPtr
8261
get_nonnull_domain_snapshot(virDomainPtr domain, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
8262 8263 8264 8265
{
    return virGetDomainSnapshot(domain, snapshot.name);
}

8266

8267 8268
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
8269
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
8270
{
8271
    dom_dst->id = dom_src->id;
8272
    dom_dst->name = dom_src->name;
8273
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
8274 8275 8276
}

static void
8277
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
8278 8279
{
    net_dst->name = net_src->name;
8280
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
8281 8282
}

8283 8284 8285 8286 8287 8288 8289 8290
static void
make_nonnull_network_port(remote_nonnull_network_port *port_dst, virNetworkPortPtr port_src)
{
    port_dst->net.name = port_src->net->name;
    memcpy(port_dst->net.uuid, port_src->net->uuid, VIR_UUID_BUFLEN);
    memcpy(port_dst->uuid, port_src->uuid, VIR_UUID_BUFLEN);
}

D
Daniel Veillard 已提交
8291
static void
8292
make_nonnull_interface(remote_nonnull_interface *interface_dst,
D
Daniel Veillard 已提交
8293 8294 8295 8296 8297 8298
                        virInterfacePtr interface_src)
{
    interface_dst->name = interface_src->name;
    interface_dst->mac = interface_src->mac;
}

8299
static void
8300
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
8301 8302
{
    pool_dst->name = pool_src->name;
8303
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
8304 8305 8306
}

static void
8307
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
8308 8309 8310 8311 8312 8313
{
    vol_dst->pool = vol_src->pool;
    vol_dst->name = vol_src->name;
    vol_dst->key = vol_src->key;
}

8314
static void
8315
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
8316
{
8317
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
8318 8319
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = secret_src->usageID;
8320 8321
}

8322 8323 8324 8325 8326 8327
static void
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
{
    dev_dst->name = dev_src->name;
}

8328
static void
8329
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
8330 8331
{
    nwfilter_dst->name = nwfilter_src->name;
8332
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
8333 8334
}

8335 8336 8337 8338 8339 8340 8341
static void
make_nonnull_nwfilter_binding(remote_nonnull_nwfilter_binding *binding_dst, virNWFilterBindingPtr binding_src)
{
    binding_dst->portdev = binding_src->portdev;
    binding_dst->filtername = binding_src->filtername;
}

C
Chris Lalancette 已提交
8342
static void
8343
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
8344 8345
{
    snapshot_dst->name = snapshot_src->name;
8346
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
8347 8348
}

8349 8350
/*----------------------------------------------------------------------*/

8351 8352 8353 8354 8355
unsigned long remoteVersion(void)
{
    return REMOTE_PROTOCOL_VERSION;
}

8356
static virHypervisorDriver hypervisor_driver = {
8357
    .name = "remote",
8358 8359
    .connectOpen = remoteConnectOpen, /* 0.3.0 */
    .connectClose = remoteConnectClose, /* 0.3.0 */
8360 8361 8362 8363 8364 8365 8366
    .connectSupportsFeature = remoteConnectSupportsFeature, /* 0.3.0 */
    .connectGetType = remoteConnectGetType, /* 0.3.0 */
    .connectGetVersion = remoteConnectGetVersion, /* 0.3.0 */
    .connectGetLibVersion = remoteConnectGetLibVersion, /* 0.7.3 */
    .connectGetHostname = remoteConnectGetHostname, /* 0.3.0 */
    .connectGetSysinfo = remoteConnectGetSysinfo, /* 0.8.8 */
    .connectGetMaxVcpus = remoteConnectGetMaxVcpus, /* 0.3.0 */
8367
    .nodeGetInfo = remoteNodeGetInfo, /* 0.3.0 */
8368 8369 8370 8371
    .connectGetCapabilities = remoteConnectGetCapabilities, /* 0.3.0 */
    .connectListDomains = remoteConnectListDomains, /* 0.3.0 */
    .connectNumOfDomains = remoteConnectNumOfDomains, /* 0.3.0 */
    .connectListAllDomains = remoteConnectListAllDomains, /* 0.9.13 */
8372
    .domainCreateXML = remoteDomainCreateXML, /* 0.3.0 */
8373
    .domainCreateXMLWithFiles = remoteDomainCreateXMLWithFiles, /* 1.1.1 */
8374 8375 8376 8377 8378
    .domainLookupByID = remoteDomainLookupByID, /* 0.3.0 */
    .domainLookupByUUID = remoteDomainLookupByUUID, /* 0.3.0 */
    .domainLookupByName = remoteDomainLookupByName, /* 0.3.0 */
    .domainSuspend = remoteDomainSuspend, /* 0.3.0 */
    .domainResume = remoteDomainResume, /* 0.3.0 */
8379
    .domainPMSuspendForDuration = remoteDomainPMSuspendForDuration, /* 0.9.10 */
8380
    .domainPMWakeup = remoteDomainPMWakeup, /* 0.9.11 */
8381
    .domainShutdown = remoteDomainShutdown, /* 0.3.0 */
8382
    .domainShutdownFlags = remoteDomainShutdownFlags, /* 0.9.10 */
8383
    .domainReboot = remoteDomainReboot, /* 0.3.0 */
8384
    .domainReset = remoteDomainReset, /* 0.9.7 */
8385
    .domainDestroy = remoteDomainDestroy, /* 0.3.0 */
8386
    .domainDestroyFlags = remoteDomainDestroyFlags, /* 0.9.4 */
8387 8388 8389 8390 8391
    .domainGetOSType = remoteDomainGetOSType, /* 0.3.0 */
    .domainGetMaxMemory = remoteDomainGetMaxMemory, /* 0.3.0 */
    .domainSetMaxMemory = remoteDomainSetMaxMemory, /* 0.3.0 */
    .domainSetMemory = remoteDomainSetMemory, /* 0.3.0 */
    .domainSetMemoryFlags = remoteDomainSetMemoryFlags, /* 0.9.0 */
8392
    .domainSetMemoryStatsPeriod = remoteDomainSetMemoryStatsPeriod, /* 1.1.1 */
8393 8394 8395 8396
    .domainSetMemoryParameters = remoteDomainSetMemoryParameters, /* 0.8.5 */
    .domainGetMemoryParameters = remoteDomainGetMemoryParameters, /* 0.8.5 */
    .domainSetBlkioParameters = remoteDomainSetBlkioParameters, /* 0.9.0 */
    .domainGetBlkioParameters = remoteDomainGetBlkioParameters, /* 0.9.0 */
8397 8398
    .domainGetPerfEvents = remoteDomainGetPerfEvents, /* 1.3.3 */
    .domainSetPerfEvents = remoteDomainSetPerfEvents, /* 1.3.3 */
8399 8400
    .domainGetInfo = remoteDomainGetInfo, /* 0.3.0 */
    .domainGetState = remoteDomainGetState, /* 0.9.2 */
8401
    .domainGetControlInfo = remoteDomainGetControlInfo, /* 0.9.3 */
8402
    .domainSave = remoteDomainSave, /* 0.3.0 */
E
Eric Blake 已提交
8403
    .domainSaveFlags = remoteDomainSaveFlags, /* 0.9.4 */
8404
    .domainRestore = remoteDomainRestore, /* 0.3.0 */
E
Eric Blake 已提交
8405
    .domainRestoreFlags = remoteDomainRestoreFlags, /* 0.9.4 */
E
Eric Blake 已提交
8406 8407
    .domainSaveImageGetXMLDesc = remoteDomainSaveImageGetXMLDesc, /* 0.9.4 */
    .domainSaveImageDefineXML = remoteDomainSaveImageDefineXML, /* 0.9.4 */
8408
    .domainCoreDump = remoteDomainCoreDump, /* 0.3.0 */
8409
    .domainCoreDumpWithFormat = remoteDomainCoreDumpWithFormat, /* 1.2.3 */
8410 8411 8412 8413 8414
    .domainScreenshot = remoteDomainScreenshot, /* 0.9.2 */
    .domainSetVcpus = remoteDomainSetVcpus, /* 0.3.0 */
    .domainSetVcpusFlags = remoteDomainSetVcpusFlags, /* 0.8.5 */
    .domainGetVcpusFlags = remoteDomainGetVcpusFlags, /* 0.8.5 */
    .domainPinVcpu = remoteDomainPinVcpu, /* 0.3.0 */
8415
    .domainPinVcpuFlags = remoteDomainPinVcpuFlags, /* 0.9.3 */
E
Eric Blake 已提交
8416
    .domainGetVcpuPinInfo = remoteDomainGetVcpuPinInfo, /* 0.9.3 */
8417 8418
    .domainPinEmulator = remoteDomainPinEmulator, /* 0.10.0 */
    .domainGetEmulatorPinInfo = remoteDomainGetEmulatorPinInfo, /* 0.10.0 */
8419 8420
    .domainGetVcpus = remoteDomainGetVcpus, /* 0.3.0 */
    .domainGetMaxVcpus = remoteDomainGetMaxVcpus, /* 0.3.0 */
8421
    .domainGetIOThreadInfo = remoteDomainGetIOThreadInfo, /* 1.2.14 */
8422
    .domainPinIOThread = remoteDomainPinIOThread, /* 1.2.14 */
8423 8424
    .domainAddIOThread = remoteDomainAddIOThread, /* 1.2.15 */
    .domainDelIOThread = remoteDomainDelIOThread, /* 1.2.15 */
8425
    .domainSetIOThreadParams = remoteDomainSetIOThreadParams, /* 4.10.0 */
8426
    .domainGetSecurityLabel = remoteDomainGetSecurityLabel, /* 0.6.1 */
M
Marcelo Cerri 已提交
8427
    .domainGetSecurityLabelList = remoteDomainGetSecurityLabelList, /* 0.10.0 */
8428 8429
    .nodeGetSecurityModel = remoteNodeGetSecurityModel, /* 0.6.1 */
    .domainGetXMLDesc = remoteDomainGetXMLDesc, /* 0.3.0 */
8430 8431 8432 8433
    .connectDomainXMLFromNative = remoteConnectDomainXMLFromNative, /* 0.6.4 */
    .connectDomainXMLToNative = remoteConnectDomainXMLToNative, /* 0.6.4 */
    .connectListDefinedDomains = remoteConnectListDefinedDomains, /* 0.3.0 */
    .connectNumOfDefinedDomains = remoteConnectNumOfDefinedDomains, /* 0.3.0 */
8434 8435
    .domainCreate = remoteDomainCreate, /* 0.3.0 */
    .domainCreateWithFlags = remoteDomainCreateWithFlags, /* 0.8.2 */
8436
    .domainCreateWithFiles = remoteDomainCreateWithFiles, /* 1.1.1 */
8437
    .domainDefineXML = remoteDomainDefineXML, /* 0.3.0 */
8438
    .domainDefineXMLFlags = remoteDomainDefineXMLFlags, /* 1.2.12 */
8439
    .domainUndefine = remoteDomainUndefine, /* 0.3.0 */
8440
    .domainUndefineFlags = remoteDomainUndefineFlags, /* 0.9.4 */
8441 8442 8443 8444 8445
    .domainAttachDevice = remoteDomainAttachDevice, /* 0.3.0 */
    .domainAttachDeviceFlags = remoteDomainAttachDeviceFlags, /* 0.7.7 */
    .domainDetachDevice = remoteDomainDetachDevice, /* 0.3.0 */
    .domainDetachDeviceFlags = remoteDomainDetachDeviceFlags, /* 0.7.7 */
    .domainUpdateDeviceFlags = remoteDomainUpdateDeviceFlags, /* 0.8.0 */
8446
    .domainDetachDeviceAlias = remoteDomainDetachDeviceAlias, /* 4.4.0 */
8447 8448 8449 8450
    .domainGetAutostart = remoteDomainGetAutostart, /* 0.3.0 */
    .domainSetAutostart = remoteDomainSetAutostart, /* 0.3.0 */
    .domainGetSchedulerType = remoteDomainGetSchedulerType, /* 0.3.0 */
    .domainGetSchedulerParameters = remoteDomainGetSchedulerParameters, /* 0.3.0 */
8451
    .domainGetSchedulerParametersFlags = remoteDomainGetSchedulerParametersFlags, /* 0.9.2 */
8452
    .domainSetSchedulerParameters = remoteDomainSetSchedulerParameters, /* 0.3.0 */
8453
    .domainSetSchedulerParametersFlags = remoteDomainSetSchedulerParametersFlags, /* 0.9.2 */
8454 8455 8456
    .domainMigratePrepare = remoteDomainMigratePrepare, /* 0.3.2 */
    .domainMigratePerform = remoteDomainMigratePerform, /* 0.3.2 */
    .domainMigrateFinish = remoteDomainMigrateFinish, /* 0.3.2 */
8457
    .domainBlockResize = remoteDomainBlockResize, /* 0.9.8 */
8458
    .domainBlockStats = remoteDomainBlockStats, /* 0.3.2 */
8459
    .domainBlockStatsFlags = remoteDomainBlockStatsFlags, /* 0.9.5 */
8460
    .domainInterfaceStats = remoteDomainInterfaceStats, /* 0.3.2 */
8461 8462
    .domainSetInterfaceParameters = remoteDomainSetInterfaceParameters, /* 0.9.9 */
    .domainGetInterfaceParameters = remoteDomainGetInterfaceParameters, /* 0.9.9 */
8463 8464 8465 8466
    .domainMemoryStats = remoteDomainMemoryStats, /* 0.7.5 */
    .domainBlockPeek = remoteDomainBlockPeek, /* 0.4.2 */
    .domainMemoryPeek = remoteDomainMemoryPeek, /* 0.4.2 */
    .domainGetBlockInfo = remoteDomainGetBlockInfo, /* 0.8.1 */
8467
    .nodeGetCPUStats = remoteNodeGetCPUStats, /* 0.9.3 */
8468
    .nodeGetMemoryStats = remoteNodeGetMemoryStats, /* 0.9.3 */
8469 8470
    .nodeGetCellsFreeMemory = remoteNodeGetCellsFreeMemory, /* 0.3.3 */
    .nodeGetFreeMemory = remoteNodeGetFreeMemory, /* 0.3.3 */
8471 8472
    .connectDomainEventRegister = remoteConnectDomainEventRegister, /* 0.5.0 */
    .connectDomainEventDeregister = remoteConnectDomainEventDeregister, /* 0.5.0 */
8473 8474 8475
    .domainMigratePrepare2 = remoteDomainMigratePrepare2, /* 0.5.0 */
    .domainMigrateFinish2 = remoteDomainMigrateFinish2, /* 0.5.0 */
    .nodeDeviceDettach = remoteNodeDeviceDettach, /* 0.6.1 */
8476
    .nodeDeviceDetachFlags = remoteNodeDeviceDetachFlags, /* 1.0.5 */
8477 8478 8479
    .nodeDeviceReAttach = remoteNodeDeviceReAttach, /* 0.6.1 */
    .nodeDeviceReset = remoteNodeDeviceReset, /* 0.6.1 */
    .domainMigratePrepareTunnel = remoteDomainMigratePrepareTunnel, /* 0.7.2 */
8480 8481
    .connectIsEncrypted = remoteConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = remoteConnectIsSecure, /* 0.7.3 */
8482 8483 8484
    .domainIsActive = remoteDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = remoteDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = remoteDomainIsUpdated, /* 0.8.6 */
8485 8486
    .connectCompareCPU = remoteConnectCompareCPU, /* 0.7.5 */
    .connectBaselineCPU = remoteConnectBaselineCPU, /* 0.7.7 */
8487
    .domainGetJobInfo = remoteDomainGetJobInfo, /* 0.7.7 */
8488
    .domainGetJobStats = remoteDomainGetJobStats, /* 1.0.3 */
8489
    .domainAbortJob = remoteDomainAbortJob, /* 0.7.7 */
8490
    .domainMigrateGetMaxDowntime = remoteDomainMigrateGetMaxDowntime, /* 3.7.0 */
8491
    .domainMigrateSetMaxDowntime = remoteDomainMigrateSetMaxDowntime, /* 0.8.0 */
8492 8493
    .domainMigrateGetCompressionCache = remoteDomainMigrateGetCompressionCache, /* 1.0.3 */
    .domainMigrateSetCompressionCache = remoteDomainMigrateSetCompressionCache, /* 1.0.3 */
8494
    .domainMigrateSetMaxSpeed = remoteDomainMigrateSetMaxSpeed, /* 0.9.0 */
8495
    .domainMigrateGetMaxSpeed = remoteDomainMigrateGetMaxSpeed, /* 0.9.5 */
8496 8497
    .connectDomainEventRegisterAny = remoteConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = remoteConnectDomainEventDeregisterAny, /* 0.8.0 */
8498 8499 8500
    .domainManagedSave = remoteDomainManagedSave, /* 0.8.0 */
    .domainHasManagedSaveImage = remoteDomainHasManagedSaveImage, /* 0.8.0 */
    .domainManagedSaveRemove = remoteDomainManagedSaveRemove, /* 0.8.0 */
8501
    .domainManagedSaveGetXMLDesc = remoteDomainManagedSaveGetXMLDesc, /* 3.7.0 */
8502
    .domainManagedSaveDefineXML = remoteDomainManagedSaveDefineXML, /* 3.7.0 */
8503 8504 8505 8506
    .domainSnapshotCreateXML = remoteDomainSnapshotCreateXML, /* 0.8.0 */
    .domainSnapshotGetXMLDesc = remoteDomainSnapshotGetXMLDesc, /* 0.8.0 */
    .domainSnapshotNum = remoteDomainSnapshotNum, /* 0.8.0 */
    .domainSnapshotListNames = remoteDomainSnapshotListNames, /* 0.8.0 */
8507
    .domainListAllSnapshots = remoteDomainListAllSnapshots, /* 0.9.13 */
8508
    .domainSnapshotNumChildren = remoteDomainSnapshotNumChildren, /* 0.9.7 */
8509
    .domainSnapshotListAllChildren = remoteDomainSnapshotListAllChildren, /* 0.9.13 */
8510
    .domainSnapshotListChildrenNames = remoteDomainSnapshotListChildrenNames, /* 0.9.7 */
8511 8512
    .domainSnapshotLookupByName = remoteDomainSnapshotLookupByName, /* 0.8.0 */
    .domainHasCurrentSnapshot = remoteDomainHasCurrentSnapshot, /* 0.8.0 */
8513
    .domainSnapshotGetParent = remoteDomainSnapshotGetParent, /* 0.9.7 */
8514 8515
    .domainSnapshotCurrent = remoteDomainSnapshotCurrent, /* 0.8.0 */
    .domainRevertToSnapshot = remoteDomainRevertToSnapshot, /* 0.8.0 */
E
Eric Blake 已提交
8516 8517
    .domainSnapshotIsCurrent = remoteDomainSnapshotIsCurrent, /* 0.9.13 */
    .domainSnapshotHasMetadata = remoteDomainSnapshotHasMetadata, /* 0.9.13 */
8518
    .domainSnapshotDelete = remoteDomainSnapshotDelete, /* 0.8.0 */
8519 8520 8521
    .domainQemuMonitorCommand = remoteDomainQemuMonitorCommand, /* 0.8.3 */
    .domainQemuAttach = remoteDomainQemuAttach, /* 0.9.4 */
    .domainQemuAgentCommand = remoteDomainQemuAgentCommand, /* 0.10.0 */
8522 8523
    .connectDomainQemuMonitorEventRegister = remoteConnectDomainQemuMonitorEventRegister, /* 1.2.3 */
    .connectDomainQemuMonitorEventDeregister = remoteConnectDomainQemuMonitorEventDeregister, /* 1.2.3 */
8524
    .domainOpenConsole = remoteDomainOpenConsole, /* 0.8.6 */
8525
    .domainOpenChannel = remoteDomainOpenChannel, /* 1.0.2 */
8526
    .domainOpenGraphics = remoteDomainOpenGraphics, /* 0.9.7 */
8527
    .domainOpenGraphicsFD = remoteDomainOpenGraphicsFD, /* 1.2.8 */
8528
    .domainInjectNMI = remoteDomainInjectNMI, /* 0.9.2 */
8529 8530 8531 8532 8533 8534
    .domainMigrateBegin3 = remoteDomainMigrateBegin3, /* 0.9.2 */
    .domainMigratePrepare3 = remoteDomainMigratePrepare3, /* 0.9.2 */
    .domainMigratePrepareTunnel3 = remoteDomainMigratePrepareTunnel3, /* 0.9.2 */
    .domainMigratePerform3 = remoteDomainMigratePerform3, /* 0.9.2 */
    .domainMigrateFinish3 = remoteDomainMigrateFinish3, /* 0.9.2 */
    .domainMigrateConfirm3 = remoteDomainMigrateConfirm3, /* 0.9.2 */
8535
    .domainSendKey = remoteDomainSendKey, /* 0.9.3 */
8536
    .domainSendProcessSignal = remoteDomainSendProcessSignal, /* 1.0.1 */
8537 8538 8539 8540
    .domainBlockJobAbort = remoteDomainBlockJobAbort, /* 0.9.4 */
    .domainGetBlockJobInfo = remoteDomainGetBlockJobInfo, /* 0.9.4 */
    .domainBlockJobSetSpeed = remoteDomainBlockJobSetSpeed, /* 0.9.4 */
    .domainBlockPull = remoteDomainBlockPull, /* 0.9.4 */
8541
    .domainBlockRebase = remoteDomainBlockRebase, /* 0.9.10 */
8542
    .domainBlockCopy = remoteDomainBlockCopy, /* 1.2.9 */
8543
    .domainBlockCommit = remoteDomainBlockCommit, /* 0.10.2 */
8544 8545
    .connectSetKeepAlive = remoteConnectSetKeepAlive, /* 0.9.8 */
    .connectIsAlive = remoteConnectIsAlive, /* 0.9.8 */
8546
    .nodeSuspendForDuration = remoteNodeSuspendForDuration, /* 0.9.8 */
8547 8548
    .domainSetBlockIoTune = remoteDomainSetBlockIoTune, /* 0.9.8 */
    .domainGetBlockIoTune = remoteDomainGetBlockIoTune, /* 0.9.8 */
8549 8550
    .domainSetNumaParameters = remoteDomainSetNumaParameters, /* 0.9.9 */
    .domainGetNumaParameters = remoteDomainGetNumaParameters, /* 0.9.9 */
8551
    .domainGetCPUStats = remoteDomainGetCPUStats, /* 0.9.10 */
8552
    .domainGetDiskErrors = remoteDomainGetDiskErrors, /* 0.9.10 */
8553 8554
    .domainSetMetadata = remoteDomainSetMetadata, /* 0.9.10 */
    .domainGetMetadata = remoteDomainGetMetadata, /* 0.9.10 */
8555
    .domainGetHostname = remoteDomainGetHostname, /* 0.10.0 */
8556 8557
    .nodeSetMemoryParameters = remoteNodeSetMemoryParameters, /* 0.10.2 */
    .nodeGetMemoryParameters = remoteNodeGetMemoryParameters, /* 0.10.2 */
8558
    .nodeGetCPUMap = remoteNodeGetCPUMap, /* 1.0.0 */
8559
    .domainFSTrim = remoteDomainFSTrim, /* 1.0.1 */
8560
    .domainLxcOpenNamespace = remoteDomainLxcOpenNamespace, /* 1.0.2 */
8561 8562 8563 8564 8565 8566
    .domainMigrateBegin3Params = remoteDomainMigrateBegin3Params, /* 1.1.0 */
    .domainMigratePrepare3Params = remoteDomainMigratePrepare3Params, /* 1.1.0 */
    .domainMigratePrepareTunnel3Params = remoteDomainMigratePrepareTunnel3Params, /* 1.1.0 */
    .domainMigratePerform3Params = remoteDomainMigratePerform3Params, /* 1.1.0 */
    .domainMigrateFinish3Params = remoteDomainMigrateFinish3Params, /* 1.1.0 */
    .domainMigrateConfirm3Params = remoteDomainMigrateConfirm3Params, /* 1.1.0 */
8567
    .connectGetCPUModelNames = remoteConnectGetCPUModelNames, /* 1.1.3 */
8568 8569
    .domainFSFreeze = remoteDomainFSFreeze, /* 1.2.5 */
    .domainFSThaw = remoteDomainFSThaw, /* 1.2.5 */
8570 8571
    .domainGetTime = remoteDomainGetTime, /* 1.2.5 */
    .domainSetTime = remoteDomainSetTime, /* 1.2.5 */
M
Michal Privoznik 已提交
8572
    .nodeGetFreePages = remoteNodeGetFreePages, /* 1.2.6 */
8573
    .connectGetDomainCapabilities = remoteConnectGetDomainCapabilities, /* 1.2.7 */
8574
    .connectGetAllDomainStats = remoteConnectGetAllDomainStats, /* 1.2.8 */
M
Michal Privoznik 已提交
8575
    .nodeAllocPages = remoteNodeAllocPages, /* 1.2.9 */
8576
    .domainGetFSInfo = remoteDomainGetFSInfo, /* 1.2.11 */
8577
    .domainInterfaceAddresses = remoteDomainInterfaceAddresses, /* 1.2.14 */
8578
    .domainSetUserPassword = remoteDomainSetUserPassword, /* 1.2.16 */
T
Tomas Meszaros 已提交
8579
    .domainRename = remoteDomainRename, /* 1.2.19 */
8580 8581
    .connectRegisterCloseCallback = remoteConnectRegisterCloseCallback, /* 1.3.2 */
    .connectUnregisterCloseCallback = remoteConnectUnregisterCloseCallback, /* 1.3.2 */
8582
    .domainMigrateStartPostCopy = remoteDomainMigrateStartPostCopy, /* 1.3.3 */
8583
    .domainGetGuestVcpus = remoteDomainGetGuestVcpus, /* 2.0.0 */
8584
    .domainSetGuestVcpus = remoteDomainSetGuestVcpus, /* 2.0.0 */
8585
    .domainSetVcpu = remoteDomainSetVcpu, /* 3.1.0 */
8586
    .domainSetBlockThreshold = remoteDomainSetBlockThreshold, /* 3.2.0 */
8587 8588
    .domainSetLifecycleAction = remoteDomainSetLifecycleAction, /* 3.9.0 */
    .connectCompareHypervisorCPU = remoteConnectCompareHypervisorCPU, /* 4.4.0 */
8589
    .connectBaselineHypervisorCPU = remoteConnectBaselineHypervisorCPU, /* 4.4.0 */
8590
    .nodeGetSEVInfo = remoteNodeGetSEVInfo, /* 4.5.0 */
8591
    .domainGetLaunchSecurityInfo = remoteDomainGetLaunchSecurityInfo /* 4.5.0 */
8592 8593 8594
};

static virNetworkDriver network_driver = {
8595 8596 8597 8598 8599
    .connectNumOfNetworks = remoteConnectNumOfNetworks, /* 0.3.0 */
    .connectListNetworks = remoteConnectListNetworks, /* 0.3.0 */
    .connectNumOfDefinedNetworks = remoteConnectNumOfDefinedNetworks, /* 0.3.0 */
    .connectListDefinedNetworks = remoteConnectListDefinedNetworks, /* 0.3.0 */
    .connectListAllNetworks = remoteConnectListAllNetworks, /* 0.10.2 */
8600 8601
    .connectNetworkEventDeregisterAny = remoteConnectNetworkEventDeregisterAny, /* 1.2.1 */
    .connectNetworkEventRegisterAny = remoteConnectNetworkEventRegisterAny, /* 1.2.1 */
8602 8603 8604 8605 8606
    .networkLookupByUUID = remoteNetworkLookupByUUID, /* 0.3.0 */
    .networkLookupByName = remoteNetworkLookupByName, /* 0.3.0 */
    .networkCreateXML = remoteNetworkCreateXML, /* 0.3.0 */
    .networkDefineXML = remoteNetworkDefineXML, /* 0.3.0 */
    .networkUndefine = remoteNetworkUndefine, /* 0.3.0 */
8607
    .networkUpdate = remoteNetworkUpdate, /* 0.10.2 */
8608 8609 8610 8611 8612 8613 8614 8615
    .networkCreate = remoteNetworkCreate, /* 0.3.0 */
    .networkDestroy = remoteNetworkDestroy, /* 0.3.0 */
    .networkGetXMLDesc = remoteNetworkGetXMLDesc, /* 0.3.0 */
    .networkGetBridgeName = remoteNetworkGetBridgeName, /* 0.3.0 */
    .networkGetAutostart = remoteNetworkGetAutostart, /* 0.3.0 */
    .networkSetAutostart = remoteNetworkSetAutostart, /* 0.3.0 */
    .networkIsActive = remoteNetworkIsActive, /* 0.7.3 */
    .networkIsPersistent = remoteNetworkIsPersistent, /* 0.7.3 */
8616
    .networkGetDHCPLeases = remoteNetworkGetDHCPLeases, /* 1.2.6 */
8617 8618 8619 8620 8621 8622 8623
    .networkListAllPorts = remoteNetworkListAllPorts, /* 5.5.0 */
    .networkPortLookupByUUID = remoteNetworkPortLookupByUUID, /* 5.5.0 */
    .networkPortCreateXML = remoteNetworkPortCreateXML, /* 5.5.0 */
    .networkPortGetXMLDesc = remoteNetworkPortGetXMLDesc, /* 5.5.0 */
    .networkPortSetParameters = remoteNetworkPortSetParameters, /* 5.5.0 */
    .networkPortGetParameters = remoteNetworkPortGetParameters, /* 5.5.0 */
    .networkPortDelete = remoteNetworkPortDelete, /* 5.5.0 */
8624 8625
};

D
Daniel Veillard 已提交
8626
static virInterfaceDriver interface_driver = {
8627 8628 8629 8630 8631
    .connectNumOfInterfaces = remoteConnectNumOfInterfaces, /* 0.7.2 */
    .connectListInterfaces = remoteConnectListInterfaces, /* 0.7.2 */
    .connectNumOfDefinedInterfaces = remoteConnectNumOfDefinedInterfaces, /* 0.7.2 */
    .connectListDefinedInterfaces = remoteConnectListDefinedInterfaces, /* 0.7.2 */
    .connectListAllInterfaces = remoteConnectListAllInterfaces, /* 0.10.2 */
8632 8633 8634 8635 8636 8637 8638 8639
    .interfaceLookupByName = remoteInterfaceLookupByName, /* 0.7.2 */
    .interfaceLookupByMACString = remoteInterfaceLookupByMACString, /* 0.7.2 */
    .interfaceGetXMLDesc = remoteInterfaceGetXMLDesc, /* 0.7.2 */
    .interfaceDefineXML = remoteInterfaceDefineXML, /* 0.7.2 */
    .interfaceUndefine = remoteInterfaceUndefine, /* 0.7.2 */
    .interfaceCreate = remoteInterfaceCreate, /* 0.7.2 */
    .interfaceDestroy = remoteInterfaceDestroy, /* 0.7.2 */
    .interfaceIsActive = remoteInterfaceIsActive, /* 0.7.3 */
8640 8641 8642
    .interfaceChangeBegin = remoteInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = remoteInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = remoteInterfaceChangeRollback, /* 0.9.2 */
D
Daniel Veillard 已提交
8643 8644
};

8645
static virStorageDriver storage_driver = {
8646 8647 8648 8649 8650 8651
    .connectNumOfStoragePools = remoteConnectNumOfStoragePools, /* 0.4.1 */
    .connectListStoragePools = remoteConnectListStoragePools, /* 0.4.1 */
    .connectNumOfDefinedStoragePools = remoteConnectNumOfDefinedStoragePools, /* 0.4.1 */
    .connectListDefinedStoragePools = remoteConnectListDefinedStoragePools, /* 0.4.1 */
    .connectListAllStoragePools = remoteConnectListAllStoragePools, /* 0.10.2 */
    .connectFindStoragePoolSources = remoteConnectFindStoragePoolSources, /* 0.4.5 */
8652 8653
    .connectStoragePoolEventDeregisterAny = remoteConnectStoragePoolEventDeregisterAny, /* 2.0.0 */
    .connectStoragePoolEventRegisterAny = remoteConnectStoragePoolEventRegisterAny, /* 2.0.0 */
8654
    .connectGetStoragePoolCapabilities = remoteConnectGetStoragePoolCapabilities, /* 5.2.0 */
8655 8656 8657
    .storagePoolLookupByName = remoteStoragePoolLookupByName, /* 0.4.1 */
    .storagePoolLookupByUUID = remoteStoragePoolLookupByUUID, /* 0.4.1 */
    .storagePoolLookupByVolume = remoteStoragePoolLookupByVolume, /* 0.4.1 */
8658
    .storagePoolLookupByTargetPath = remoteStoragePoolLookupByTargetPath, /* 4.1.0 */
8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685
    .storagePoolCreateXML = remoteStoragePoolCreateXML, /* 0.4.1 */
    .storagePoolDefineXML = remoteStoragePoolDefineXML, /* 0.4.1 */
    .storagePoolBuild = remoteStoragePoolBuild, /* 0.4.1 */
    .storagePoolUndefine = remoteStoragePoolUndefine, /* 0.4.1 */
    .storagePoolCreate = remoteStoragePoolCreate, /* 0.4.1 */
    .storagePoolDestroy = remoteStoragePoolDestroy, /* 0.4.1 */
    .storagePoolDelete = remoteStoragePoolDelete, /* 0.4.1 */
    .storagePoolRefresh = remoteStoragePoolRefresh, /* 0.4.1 */
    .storagePoolGetInfo = remoteStoragePoolGetInfo, /* 0.4.1 */
    .storagePoolGetXMLDesc = remoteStoragePoolGetXMLDesc, /* 0.4.1 */
    .storagePoolGetAutostart = remoteStoragePoolGetAutostart, /* 0.4.1 */
    .storagePoolSetAutostart = remoteStoragePoolSetAutostart, /* 0.4.1 */
    .storagePoolNumOfVolumes = remoteStoragePoolNumOfVolumes, /* 0.4.1 */
    .storagePoolListVolumes = remoteStoragePoolListVolumes, /* 0.4.1 */
    .storagePoolListAllVolumes = remoteStoragePoolListAllVolumes, /* 0.10.0 */

    .storageVolLookupByName = remoteStorageVolLookupByName, /* 0.4.1 */
    .storageVolLookupByKey = remoteStorageVolLookupByKey, /* 0.4.1 */
    .storageVolLookupByPath = remoteStorageVolLookupByPath, /* 0.4.1 */
    .storageVolCreateXML = remoteStorageVolCreateXML, /* 0.4.1 */
    .storageVolCreateXMLFrom = remoteStorageVolCreateXMLFrom, /* 0.6.4 */
    .storageVolDownload = remoteStorageVolDownload, /* 0.9.0 */
    .storageVolUpload = remoteStorageVolUpload, /* 0.9.0 */
    .storageVolDelete = remoteStorageVolDelete, /* 0.4.1 */
    .storageVolWipe = remoteStorageVolWipe, /* 0.8.0 */
    .storageVolWipePattern = remoteStorageVolWipePattern, /* 0.9.10 */
    .storageVolGetInfo = remoteStorageVolGetInfo, /* 0.4.1 */
8686
    .storageVolGetInfoFlags = remoteStorageVolGetInfoFlags, /* 3.0.0 */
8687 8688 8689 8690 8691
    .storageVolGetXMLDesc = remoteStorageVolGetXMLDesc, /* 0.4.1 */
    .storageVolGetPath = remoteStorageVolGetPath, /* 0.4.1 */
    .storageVolResize = remoteStorageVolResize, /* 0.9.10 */
    .storagePoolIsActive = remoteStoragePoolIsActive, /* 0.7.3 */
    .storagePoolIsPersistent = remoteStoragePoolIsPersistent, /* 0.7.3 */
8692 8693
};

8694
static virSecretDriver secret_driver = {
8695 8696 8697 8698 8699 8700 8701 8702 8703
    .connectNumOfSecrets = remoteConnectNumOfSecrets, /* 0.7.1 */
    .connectListSecrets = remoteConnectListSecrets, /* 0.7.1 */
    .connectListAllSecrets = remoteConnectListAllSecrets, /* 0.10.2 */
    .secretLookupByUUID = remoteSecretLookupByUUID, /* 0.7.1 */
    .secretLookupByUsage = remoteSecretLookupByUsage, /* 0.7.1 */
    .secretDefineXML = remoteSecretDefineXML, /* 0.7.1 */
    .secretGetXMLDesc = remoteSecretGetXMLDesc, /* 0.7.1 */
    .secretSetValue = remoteSecretSetValue, /* 0.7.1 */
    .secretGetValue = remoteSecretGetValue, /* 0.7.1 */
8704 8705 8706
    .secretUndefine = remoteSecretUndefine, /* 0.7.1 */
    .connectSecretEventDeregisterAny = remoteConnectSecretEventDeregisterAny, /* 3.0.0 */
    .connectSecretEventRegisterAny = remoteConnectSecretEventRegisterAny, /* 3.0.0 */
8707 8708
};

8709
static virNodeDeviceDriver node_device_driver = {
8710 8711
    .connectNodeDeviceEventDeregisterAny = remoteConnectNodeDeviceEventDeregisterAny, /* 2.2.0 */
    .connectNodeDeviceEventRegisterAny = remoteConnectNodeDeviceEventRegisterAny, /* 2.2.0 */
8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722
    .nodeNumOfDevices = remoteNodeNumOfDevices, /* 0.5.0 */
    .nodeListDevices = remoteNodeListDevices, /* 0.5.0 */
    .connectListAllNodeDevices  = remoteConnectListAllNodeDevices, /* 0.10.2 */
    .nodeDeviceLookupByName = remoteNodeDeviceLookupByName, /* 0.5.0 */
    .nodeDeviceLookupSCSIHostByWWN = remoteNodeDeviceLookupSCSIHostByWWN, /* 1.0.2 */
    .nodeDeviceGetXMLDesc = remoteNodeDeviceGetXMLDesc, /* 0.5.0 */
    .nodeDeviceGetParent = remoteNodeDeviceGetParent, /* 0.5.0 */
    .nodeDeviceNumOfCaps = remoteNodeDeviceNumOfCaps, /* 0.5.0 */
    .nodeDeviceListCaps = remoteNodeDeviceListCaps, /* 0.5.0 */
    .nodeDeviceCreateXML = remoteNodeDeviceCreateXML, /* 0.6.3 */
    .nodeDeviceDestroy = remoteNodeDeviceDestroy /* 0.6.3 */
8723 8724
};

8725
static virNWFilterDriver nwfilter_driver = {
8726 8727
    .nwfilterLookupByUUID = remoteNWFilterLookupByUUID, /* 0.8.0 */
    .nwfilterLookupByName = remoteNWFilterLookupByName, /* 0.8.0 */
8728 8729 8730 8731 8732 8733
    .nwfilterGetXMLDesc           = remoteNWFilterGetXMLDesc, /* 0.8.0 */
    .nwfilterDefineXML            = remoteNWFilterDefineXML, /* 0.8.0 */
    .nwfilterUndefine             = remoteNWFilterUndefine, /* 0.8.0 */
    .connectNumOfNWFilters       = remoteConnectNumOfNWFilters, /* 0.8.0 */
    .connectListNWFilters        = remoteConnectListNWFilters, /* 0.8.0 */
    .connectListAllNWFilters     = remoteConnectListAllNWFilters, /* 0.10.2 */
8734 8735 8736 8737 8738
    .connectListAllNWFilterBindings = remoteConnectListAllNWFilterBindings, /* 4.5.0 */
    .nwfilterBindingLookupByPortDev = remoteNWFilterBindingLookupByPortDev, /* 4.5.0 */
    .nwfilterBindingCreateXML = remoteNWFilterBindingCreateXML, /* 4.5.0 */
    .nwfilterBindingDelete = remoteNWFilterBindingDelete, /* 4.5.0 */
    .nwfilterBindingGetXMLDesc = remoteNWFilterBindingGetXMLDesc, /* 4.5.0 */
8739 8740
};

8741 8742 8743 8744 8745 8746 8747 8748 8749
static virConnectDriver connect_driver = {
    .hypervisorDriver = &hypervisor_driver,
    .interfaceDriver = &interface_driver,
    .networkDriver = &network_driver,
    .nodeDeviceDriver = &node_device_driver,
    .nwfilterDriver = &nwfilter_driver,
    .secretDriver = &secret_driver,
    .storageDriver = &storage_driver,
};
8750

8751
static virStateDriver state_driver = {
8752
    .name = "Remote",
8753
    .stateInitialize = remoteStateInitialize,
8754 8755 8756
};


8757
/** remoteRegister:
8758 8759
 *
 * Register driver with libvirt driver system.
8760 8761
 *
 * Returns -1 on error.
8762 8763
 */
int
8764
remoteRegister(void)
8765
{
8766 8767
    if (virRegisterConnectDriver(&connect_driver,
                                 false) < 0)
8768
        return -1;
8769
    if (virRegisterStateDriver(&state_driver) < 0)
8770
        return -1;
8771 8772 8773

    return 0;
}