remote_driver.c 298.8 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
#include "c-ctype.h"
53

54 55
#define VIR_FROM_THIS VIR_FROM_REMOTE

56 57
VIR_LOG_INIT("remote.remote_driver");

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
typedef enum {
    REMOTE_DRIVER_TRANSPORT_TLS,
    REMOTE_DRIVER_TRANSPORT_UNIX,
    REMOTE_DRIVER_TRANSPORT_SSH,
    REMOTE_DRIVER_TRANSPORT_LIBSSH2,
    REMOTE_DRIVER_TRANSPORT_EXT,
    REMOTE_DRIVER_TRANSPORT_TCP,
    REMOTE_DRIVER_TRANSPORT_LIBSSH,

    REMOTE_DRIVER_TRANSPORT_LAST,
} remoteDriverTransport;

VIR_ENUM_DECL(remoteDriverTransport);
VIR_ENUM_IMPL(remoteDriverTransport,
              REMOTE_DRIVER_TRANSPORT_LAST,
              "tls",
              "unix",
              "ssh",
              "libssh2",
              "ext",
              "tcp",
              "libssh");

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
typedef enum {
    /* Try to figure out the "best" choice magically */
    REMOTE_DRIVER_MODE_AUTO,
    /* Always use the legacy libvirtd */
    REMOTE_DRIVER_MODE_LEGACY,
    /* Always use the per-driver virt*d daemons */
    REMOTE_DRIVER_MODE_DIRECT,

    REMOTE_DRIVER_MODE_LAST
} remoteDriverMode;

VIR_ENUM_DECL(remoteDriverMode);
VIR_ENUM_IMPL(remoteDriverMode,
              REMOTE_DRIVER_MODE_LAST,
              "auto",
              "legacy",
              "direct");

99
#if SIZEOF_LONG < 8
100 101 102 103
# define HYPER_TO_TYPE(_type, _to, _from) \
    do { \
        if ((_from) != (_type)(_from)) { \
            virReportError(VIR_ERR_INTERNAL_ERROR, \
104
                           _("conversion from hyper to %s overflowed"), #_type); \
105 106 107
            goto done; \
        } \
        (_to) = (_from); \
108 109 110 111 112 113 114 115 116
    } 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

117
static bool inside_daemon;
118

119 120
struct private_data {
    virMutex lock;
121

122 123 124
    virNetClientPtr client;
    virNetClientProgramPtr remoteProgram;
    virNetClientProgramPtr qemuProgram;
125
    virNetClientProgramPtr lxcProgram;
126

127
    int counter; /* Serial number for RPC */
128

129
#ifdef WITH_GNUTLS
130
    virNetTLSContextPtr tls;
131
#endif
132

133
    int is_secure;              /* Secure if TLS or SASL or UNIX sockets */
134
    char *type;                 /* Cached return from remoteType. */
135
    int localUses;              /* Ref count for private data */
136
    char *hostname;             /* Original hostname */
137
    bool serverKeepAlive;       /* Does server support keepalive protocol? */
138
    bool serverEventFilter;     /* Does server support modern event filtering */
139
    bool serverCloseCallback;   /* Does server support driver close callback */
140

141
    virObjectEventStatePtr eventState;
142
    virConnectCloseCallbackDataPtr closeCallback;
143 144
};

145
enum {
146
    REMOTE_CALL_QEMU              = (1 << 0),
147
    REMOTE_CALL_LXC               = (1 << 1),
148 149 150
};


151 152 153 154 155 156 157 158 159 160
static void remoteDriverLock(struct private_data *driver)
{
    virMutexLock(&driver->lock);
}

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

161 162 163 164
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);
165 166 167 168 169 170 171
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);
172 173
static int remoteAuthenticate(virConnectPtr conn, struct private_data *priv,
                              virConnectAuthPtr auth, const char *authtype);
174
#if WITH_SASL
175 176
static int remoteAuthSASL(virConnectPtr conn, struct private_data *priv,
                          virConnectAuthPtr auth, const char *mech);
177
#endif /* WITH_SASL */
178 179
static int remoteAuthPolkit(virConnectPtr conn, struct private_data *priv,
                            virConnectAuthPtr auth);
180

181 182
static virDomainPtr get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network(virConnectPtr conn, remote_nonnull_network network);
183
static virNetworkPortPtr get_nonnull_network_port(virConnectPtr conn, remote_nonnull_network_port port);
184
static virNWFilterPtr get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter);
185
static virNWFilterBindingPtr get_nonnull_nwfilter_binding(virConnectPtr conn, remote_nonnull_nwfilter_binding binding);
186 187 188 189 190
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);
191
static virDomainCheckpointPtr get_nonnull_domain_checkpoint(virDomainPtr domain, remote_nonnull_domain_checkpoint checkpoint);
192 193 194
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);
195
static void make_nonnull_network_port(remote_nonnull_network_port *port_dst, virNetworkPortPtr port_src);
196 197 198
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);
199 200
static void
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src);
201 202
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);
203
static void make_nonnull_nwfilter_binding(remote_nonnull_nwfilter_binding *binding_dst, virNWFilterBindingPtr binding_src);
204
static void make_nonnull_domain_checkpoint(remote_nonnull_domain_checkpoint *checkpoint_dst, virDomainCheckpointPtr checkpoint_src);
205
static void make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src);
206

207 208 209
/*----------------------------------------------------------------------*/

/* Helper functions for remoteOpen. */
210 211 212 213 214 215 216 217 218 219 220
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;

221 222 223 224 225 226 227 228 229 230 231
    if (p) {
        if (VIR_STRDUP(*transport, p + 1) < 0) {
            VIR_FREE(*driver);
            return -1;
        }

        p = *transport;
        while (*p) {
            *p = c_tolower(*p);
            p++;
        }
232 233 234 235 236
    }

    return 0;
}

237

238
static int
J
Ján Tomko 已提交
239 240 241
remoteStateInitialize(bool privileged G_GNUC_UNUSED,
                      virStateInhibitCallback callback G_GNUC_UNUSED,
                      void *opaque G_GNUC_UNUSED)
242 243 244 245
{
    /* Mark that we're inside the daemon so we can avoid
     * re-entering ourselves
     */
246
    inside_daemon = true;
247
    return VIR_DRV_STATE_INIT_COMPLETE;
248 249
}

250

251
static void
J
Ján Tomko 已提交
252 253
remoteDomainBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                virNetClientPtr client G_GNUC_UNUSED,
254 255
                                void *evdata, void *opaque);
static void
J
Ján Tomko 已提交
256 257
remoteDomainBuildEventCallbackLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                        virNetClientPtr client G_GNUC_UNUSED,
258 259 260
                                        void *evdata, void *opaque);

static void
J
Ján Tomko 已提交
261 262
remoteDomainBuildEventReboot(virNetClientProgramPtr prog G_GNUC_UNUSED,
                             virNetClientPtr client G_GNUC_UNUSED,
263 264
                             void *evdata, void *opaque);
static void
J
Ján Tomko 已提交
265 266
remoteDomainBuildEventCallbackReboot(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                     virNetClientPtr client G_GNUC_UNUSED,
267 268 269
                                     void *evdata, void *opaque);

static void
270 271 272 273
remoteDomainBuildEventRTCChange(virNetClientProgramPtr prog,
                                virNetClientPtr client,
                                void *evdata, void *opaque);
static void
274 275 276 277 278
remoteDomainBuildEventCallbackRTCChange(virNetClientProgramPtr prog,
                                        virNetClientPtr client,
                                        void *evdata, void *opaque);

static void
279 280 281 282
remoteDomainBuildEventWatchdog(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
static void
283 284 285 286 287
remoteDomainBuildEventCallbackWatchdog(virNetClientProgramPtr prog,
                                       virNetClientPtr client,
                                       void *evdata, void *opaque);

static void
288 289 290 291
remoteDomainBuildEventIOError(virNetClientProgramPtr prog,
                              virNetClientPtr client,
                              void *evdata, void *opaque);
static void
292 293 294 295 296
remoteDomainBuildEventCallbackIOError(virNetClientProgramPtr prog,
                                      virNetClientPtr client,
                                      void *evdata, void *opaque);

static void
297 298 299 300
remoteDomainBuildEventIOErrorReason(virNetClientProgramPtr prog,
                                    virNetClientPtr client,
                                    void *evdata, void *opaque);
static void
301 302 303 304 305
remoteDomainBuildEventCallbackIOErrorReason(virNetClientProgramPtr prog,
                                            virNetClientPtr client,
                                            void *evdata, void *opaque);

static void
306 307 308 309
remoteDomainBuildEventGraphics(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
static void
310 311 312 313 314
remoteDomainBuildEventCallbackGraphics(virNetClientProgramPtr prog,
                                       virNetClientPtr client,
                                       void *evdata, void *opaque);

static void
315 316 317
remoteDomainBuildEventControlError(virNetClientProgramPtr prog,
                                   virNetClientPtr client,
                                   void *evdata, void *opaque);
318 319 320 321
static void
remoteDomainBuildEventCallbackControlError(virNetClientProgramPtr prog,
                                           virNetClientPtr client,
                                           void *evdata, void *opaque);
322

323 324 325 326
static void
remoteDomainBuildEventBlockJob(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
327 328 329 330 331
static void
remoteDomainBuildEventCallbackBlockJob(virNetClientProgramPtr prog,
                                       virNetClientPtr client,
                                       void *evdata, void *opaque);

332

333 334 335 336
static void
remoteDomainBuildEventDiskChange(virNetClientProgramPtr prog,
                                 virNetClientPtr client,
                                 void *evdata, void *opaque);
337 338 339 340
static void
remoteDomainBuildEventCallbackDiskChange(virNetClientProgramPtr prog,
                                         virNetClientPtr client,
                                         void *evdata, void *opaque);
341

342 343 344 345
static void
remoteDomainBuildEventTrayChange(virNetClientProgramPtr prog,
                                 virNetClientPtr client,
                                 void *evdata, void *opaque);
346 347 348 349
static void
remoteDomainBuildEventCallbackTrayChange(virNetClientProgramPtr prog,
                                         virNetClientPtr client,
                                         void *evdata, void *opaque);
350

O
Osier Yang 已提交
351 352 353 354
static void
remoteDomainBuildEventPMWakeup(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
355 356 357 358
static void
remoteDomainBuildEventCallbackPMWakeup(virNetClientProgramPtr prog,
                                       virNetClientPtr client,
                                       void *evdata, void *opaque);
O
Osier Yang 已提交
359

O
Osier Yang 已提交
360 361 362 363
static void
remoteDomainBuildEventPMSuspend(virNetClientProgramPtr prog,
                                virNetClientPtr client,
                                void *evdata, void *opaque);
364
static void
365 366 367 368 369
remoteDomainBuildEventCallbackPMSuspend(virNetClientProgramPtr prog,
                                        virNetClientPtr client,
                                        void *evdata, void *opaque);

static void
370 371 372
remoteDomainBuildEventBalloonChange(virNetClientProgramPtr prog,
                                    virNetClientPtr client,
                                    void *evdata, void *opaque);
373
static void
374 375 376 377 378
remoteDomainBuildEventCallbackBalloonChange(virNetClientProgramPtr prog,
                                            virNetClientPtr client,
                                            void *evdata, void *opaque);

static void
379 380 381
remoteDomainBuildEventPMSuspendDisk(virNetClientProgramPtr prog,
                                  virNetClientPtr client,
                                  void *evdata, void *opaque);
382 383 384 385
static void
remoteDomainBuildEventCallbackPMSuspendDisk(virNetClientProgramPtr prog,
                                            virNetClientPtr client,
                                            void *evdata, void *opaque);
O
Osier Yang 已提交
386

387 388 389 390
static void
remoteDomainBuildEventDeviceRemoved(virNetClientProgramPtr prog,
                                    virNetClientPtr client,
                                    void *evdata, void *opaque);
391 392 393 394
static void
remoteDomainBuildEventCallbackDeviceRemoved(virNetClientProgramPtr prog,
                                            virNetClientPtr client,
                                            void *evdata, void *opaque);
395 396 397 398
static void
remoteDomainBuildEventCallbackDeviceAdded(virNetClientProgramPtr prog,
                                          virNetClientPtr client,
                                          void *evdata, void *opaque);
399 400 401 402
static void
remoteDomainBuildEventCallbackDeviceRemovalFailed(virNetClientProgramPtr prog,
                                                  virNetClientPtr client,
                                                  void *evdata, void *opaque);
403

404 405 406 407 408
static void
remoteDomainBuildEventBlockJob2(virNetClientProgramPtr prog,
                                virNetClientPtr client,
                                void *evdata, void *opaque);

409 410 411 412 413
static void
remoteDomainBuildEventCallbackTunable(virNetClientProgramPtr prog,
                                      virNetClientPtr client,
                                      void *evdata, void *opaque);

414 415 416 417 418
static void
remoteDomainBuildEventCallbackAgentLifecycle(virNetClientProgramPtr prog,
                                             virNetClientPtr client,
                                             void *evdata, void *opaque);

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

J
Jiri Denemark 已提交
424 425 426 427 428
static void
remoteDomainBuildEventCallbackJobCompleted(virNetClientProgramPtr prog,
                                           virNetClientPtr client,
                                           void *evdata, void *opaque);

429 430 431 432 433
static void
remoteDomainBuildEventCallbackMetadataChange(virNetClientProgramPtr prog,
                                             virNetClientPtr client,
                                             void *evdata, void *opaque);

434
static void
J
Ján Tomko 已提交
435 436
remoteNetworkBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                 virNetClientPtr client G_GNUC_UNUSED,
437 438
                                 void *evdata, void *opaque);

439
static void
J
Ján Tomko 已提交
440 441
remoteStoragePoolBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                     virNetClientPtr client G_GNUC_UNUSED,
442 443
                                     void *evdata, void *opaque);

444
static void
J
Ján Tomko 已提交
445 446
remoteStoragePoolBuildEventRefresh(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                   virNetClientPtr client G_GNUC_UNUSED,
447 448
                                   void *evdata, void *opaque);

449
static void
J
Ján Tomko 已提交
450 451
remoteNodeDeviceBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                    virNetClientPtr client G_GNUC_UNUSED,
452 453
                                    void *evdata, void *opaque);

454
static void
J
Ján Tomko 已提交
455 456
remoteNodeDeviceBuildEventUpdate(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                 virNetClientPtr client G_GNUC_UNUSED,
457 458
                                 void *evdata, void *opaque);

459
static void
J
Ján Tomko 已提交
460 461
remoteSecretBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                virNetClientPtr client G_GNUC_UNUSED,
462 463
                                void *evdata, void *opaque);

464
static void
J
Ján Tomko 已提交
465 466
remoteSecretBuildEventValueChanged(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                   virNetClientPtr client G_GNUC_UNUSED,
467 468
                                   void *evdata, void *opaque);

469 470 471 472 473
static void
remoteDomainBuildEventBlockThreshold(virNetClientProgramPtr prog,
                                     virNetClientPtr client,
                                     void *evdata, void *opaque);

474
static void
J
Ján Tomko 已提交
475 476
remoteConnectNotifyEventConnectionClosed(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                         virNetClientPtr client G_GNUC_UNUSED,
477 478
                                         void *evdata, void *opaque);

479
static virNetClientProgramEvent remoteEvents[] = {
480 481 482 483
    { REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
      remoteDomainBuildEventLifecycle,
      sizeof(remote_domain_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_domain_event_lifecycle_msg },
484 485 486 487 488 489 490 491
    { 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 },
492 493 494 495 496 497 498 499 500 501 502 503
    { 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 },
504 505 506 507
    { 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 },
508 509 510 511
    { REMOTE_PROC_DOMAIN_EVENT_CONTROL_ERROR,
      remoteDomainBuildEventControlError,
      sizeof(remote_domain_event_control_error_msg),
      (xdrproc_t)xdr_remote_domain_event_control_error_msg },
512 513 514 515
    { REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB,
      remoteDomainBuildEventBlockJob,
      sizeof(remote_domain_event_block_job_msg),
      (xdrproc_t)xdr_remote_domain_event_block_job_msg },
516 517 518 519
    { REMOTE_PROC_DOMAIN_EVENT_DISK_CHANGE,
      remoteDomainBuildEventDiskChange,
      sizeof(remote_domain_event_disk_change_msg),
      (xdrproc_t)xdr_remote_domain_event_disk_change_msg },
520 521 522 523
    { 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 已提交
524 525 526 527
    { REMOTE_PROC_DOMAIN_EVENT_PMWAKEUP,
      remoteDomainBuildEventPMWakeup,
      sizeof(remote_domain_event_pmwakeup_msg),
      (xdrproc_t)xdr_remote_domain_event_pmwakeup_msg },
O
Osier Yang 已提交
528 529 530 531
    { REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND,
      remoteDomainBuildEventPMSuspend,
      sizeof(remote_domain_event_pmsuspend_msg),
      (xdrproc_t)xdr_remote_domain_event_pmsuspend_msg },
532 533 534 535
    { REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE,
      remoteDomainBuildEventBalloonChange,
      sizeof(remote_domain_event_balloon_change_msg),
      (xdrproc_t)xdr_remote_domain_event_balloon_change_msg },
536 537 538 539
    { REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND_DISK,
      remoteDomainBuildEventPMSuspendDisk,
      sizeof(remote_domain_event_pmsuspend_disk_msg),
      (xdrproc_t)xdr_remote_domain_event_pmsuspend_disk_msg },
540 541 542 543
    { REMOTE_PROC_DOMAIN_EVENT_DEVICE_REMOVED,
      remoteDomainBuildEventDeviceRemoved,
      sizeof(remote_domain_event_device_removed_msg),
      (xdrproc_t)xdr_remote_domain_event_device_removed_msg },
544 545 546 547 548 549 550 551 552
    /* 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.  */
553 554 555 556
    { REMOTE_PROC_NETWORK_EVENT_LIFECYCLE,
      remoteNetworkBuildEventLifecycle,
      sizeof(remote_network_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_network_event_lifecycle_msg },
557 558 559 560
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_LIFECYCLE,
      remoteDomainBuildEventCallbackLifecycle,
      sizeof(remote_domain_event_callback_lifecycle_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_lifecycle_msg },
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
    { 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 },
621 622 623 624
    { 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 },
625 626 627 628
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_TUNABLE,
      remoteDomainBuildEventCallbackTunable,
      sizeof(remote_domain_event_callback_tunable_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_tunable_msg },
629 630 631 632
    { 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 },
633 634 635 636
    { 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 },
637 638 639 640
    { 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 },
641 642 643 644
    { 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 已提交
645 646 647 648
    { 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 },
649 650 651 652
    { 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 },
653 654 655 656
    { 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 },
657 658 659 660
    { REMOTE_PROC_STORAGE_POOL_EVENT_LIFECYCLE,
      remoteStoragePoolBuildEventLifecycle,
      sizeof(remote_storage_pool_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_storage_pool_event_lifecycle_msg },
661 662 663 664
    { REMOTE_PROC_STORAGE_POOL_EVENT_REFRESH,
      remoteStoragePoolBuildEventRefresh,
      sizeof(remote_storage_pool_event_refresh_msg),
      (xdrproc_t)xdr_remote_storage_pool_event_refresh_msg },
665 666 667 668
    { REMOTE_PROC_NODE_DEVICE_EVENT_LIFECYCLE,
      remoteNodeDeviceBuildEventLifecycle,
      sizeof(remote_node_device_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_node_device_event_lifecycle_msg },
669 670 671 672
    { REMOTE_PROC_NODE_DEVICE_EVENT_UPDATE,
      remoteNodeDeviceBuildEventUpdate,
      sizeof(remote_node_device_event_update_msg),
      (xdrproc_t)xdr_remote_node_device_event_update_msg },
673 674 675 676
    { REMOTE_PROC_SECRET_EVENT_LIFECYCLE,
      remoteSecretBuildEventLifecycle,
      sizeof(remote_secret_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_secret_event_lifecycle_msg },
677 678 679 680
    { REMOTE_PROC_SECRET_EVENT_VALUE_CHANGED,
      remoteSecretBuildEventValueChanged,
      sizeof(remote_secret_event_value_changed_msg),
      (xdrproc_t)xdr_remote_secret_event_value_changed_msg },
681 682 683 684
    { REMOTE_PROC_DOMAIN_EVENT_BLOCK_THRESHOLD,
      remoteDomainBuildEventBlockThreshold,
      sizeof(remote_domain_event_block_threshold_msg),
      (xdrproc_t)xdr_remote_domain_event_block_threshold_msg },
685
};
686

687
static void
J
Ján Tomko 已提交
688 689
remoteConnectNotifyEventConnectionClosed(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                         virNetClientPtr client G_GNUC_UNUSED,
690 691 692 693 694 695 696 697
                                         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);
}
698 699

static void
J
Ján Tomko 已提交
700 701
remoteDomainBuildQemuMonitorEvent(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                  virNetClientPtr client G_GNUC_UNUSED,
702 703 704 705 706 707 708 709 710
                                  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 },
};

711
enum virDrvOpenRemoteFlags {
712
    VIR_DRV_OPEN_REMOTE_RO = (1 << 0),
713 714
    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 */
715
};
716

717

718
static void
J
Ján Tomko 已提交
719
remoteClientCloseFunc(virNetClientPtr client G_GNUC_UNUSED,
720 721
                      int reason,
                      void *opaque)
722
{
723 724
    virConnectCloseCallbackDataCall((virConnectCloseCallbackDataPtr)opaque,
                                    reason);
725 726
}

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
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;
}

743
/* helper macro to ease extraction of arguments from the URI */
744 745 746 747 748 749 750
#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; \
751 752
    }

753 754 755 756 757 758 759 760 761 762 763 764
#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; \
765
    }
766 767


768
static char *
769 770 771
remoteGetUNIXSocketHelper(remoteDriverTransport transport,
                          const char *sock_prefix,
                          unsigned int flags)
772 773
{
    char *sockname = NULL;
774
    g_autofree char *userdir = NULL;
775 776 777 778 779 780 781 782 783 784 785 786

    if (flags & VIR_DRV_OPEN_REMOTE_USER) {
        if (transport != REMOTE_DRIVER_TRANSPORT_UNIX) {
            virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
                           _("Connecting to session instance without "
                             "socket path is not supported by the %s "
                             "transport"),
                           remoteDriverTransportTypeToString(transport));
            return NULL;
        }
        if (!(userdir = virGetUserRuntimeDirectory()))
            return NULL;
787

788 789
        if (virAsprintf(&sockname, "%s/%s-sock",
                        userdir, sock_prefix) < 0)
790 791
            return NULL;
    } else {
792 793 794 795 796 797
        /* Intentionally do *NOT* use RUNSTATEDIR here. We might
         * be connecting to a remote machine, and cannot assume
         * the remote host has /run. The converse is ok though,
         * any machine with /run will have a /var/run symlink.
         * The portable option is to thus use $LOCALSTATEDIR/run
         */
798 799 800 801
        if (virAsprintf(&sockname, "%s/run/libvirt/%s-%s",
                        LOCALSTATEDIR, sock_prefix,
                        flags & VIR_DRV_OPEN_REMOTE_RO ?
                        "sock-ro" : "sock") < 0)
802
            return NULL;
803 804
    }

805 806 807
    VIR_DEBUG("Built UNIX sockname %s for transport %s prefix %s flags=0x%x",
              sockname, remoteDriverTransportTypeToString(transport),
              sock_prefix, flags);
808 809 810
    return sockname;
}

811 812 813 814 815 816 817 818 819

static char *
remoteGetUNIXSocket(remoteDriverTransport transport,
                    remoteDriverMode mode,
                    const char *driver,
                    char **daemon,
                    unsigned int flags)
{
    char *sock_name = NULL;
820 821 822 823
    g_autofree char *direct_daemon = NULL;
    g_autofree char *legacy_daemon = NULL;
    g_autofree char *direct_sock_name = NULL;
    g_autofree char *legacy_sock_name = NULL;
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860

    if (driver &&
        virAsprintf(&direct_daemon, "virt%sd", driver) < 0)
        return NULL;

    if (VIR_STRDUP(legacy_daemon, "libvirtd") < 0)
        return NULL;

    if (driver &&
        !(direct_sock_name = remoteGetUNIXSocketHelper(transport, direct_daemon, flags)))
        return NULL;

    if (!(legacy_sock_name = remoteGetUNIXSocketHelper(transport, "libvirt", flags)))
        return NULL;

    if (mode == REMOTE_DRIVER_MODE_AUTO) {
        if (transport == REMOTE_DRIVER_TRANSPORT_UNIX) {
            if (direct_sock_name && virFileExists(direct_sock_name)) {
                mode = REMOTE_DRIVER_MODE_DIRECT;
            } else if (virFileExists(legacy_sock_name)) {
                mode = REMOTE_DRIVER_MODE_LEGACY;
            } else if (driver) {
                /*
                 * This constant comes from the configure script and
                 * maps to either the direct or legacy mode constant
                 */
                mode = REMOTE_DRIVER_MODE_DEFAULT;
            } else {
                mode = REMOTE_DRIVER_MODE_LEGACY;
            }
        } else {
            mode = REMOTE_DRIVER_MODE_LEGACY;
        }
    }

    switch ((remoteDriverMode)mode) {
    case REMOTE_DRIVER_MODE_LEGACY:
861 862
        sock_name = g_steal_pointer(&legacy_sock_name);
        *daemon = g_steal_pointer(&legacy_daemon);
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
        break;

    case REMOTE_DRIVER_MODE_DIRECT:
        if (transport != REMOTE_DRIVER_TRANSPORT_UNIX) {
            virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
                           _("Cannot use direct socket mode for %s transport"),
                           remoteDriverTransportTypeToString(transport));
            return NULL;
        }

        if (!direct_sock_name) {
            virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                           _("Cannot use direct socket mode if no URI is set"));
            return NULL;
        }

879 880
        sock_name = g_steal_pointer(&direct_sock_name);
        *daemon = g_steal_pointer(&direct_daemon);
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
        break;

    case REMOTE_DRIVER_MODE_AUTO:
    case REMOTE_DRIVER_MODE_LAST:
    default:
        virReportEnumRangeError(remoteDriverMode, mode);
        return NULL;
    }

    VIR_DEBUG("Chosen UNIX sockname %s daemon %s "
              "for mode %s transport %s flags=0x%x",
              sock_name, NULLSTR(*daemon),
              remoteDriverModeTypeToString(mode),
              remoteDriverTransportTypeToString(transport),
              flags);
    return sock_name;
}


#ifndef WIN32
static const char *
remoteGetDaemonPathEnv(void)
{
    /* We prefer a VIRTD_PATH env var to use for all daemons,
     * but if it is not set we will fallback to LIBVIRTD_PATH
     * for previous behaviour
     */
    if (getenv("VIRTD_PATH") != NULL) {
        return "VIRTD_PATH";
    } else {
        return "LIBVIRTD_PATH";
    }
}
#endif /* WIN32 */


917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
/*
 * 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
933 934
 *   - xxx+ssh:///            -> SSH connection (legacy)
 *   - xxx+libssh2:///        -> SSH connection (using libssh2)
935
 *   - xxx+libssh:///         -> SSH connection (using libssh)
936
 */
937
static int
938 939
doRemoteOpen(virConnectPtr conn,
             struct private_data *priv,
940 941
             const char *driver_str,
             const char *transport_str,
J
Ján Tomko 已提交
942
             virConnectAuthPtr auth G_GNUC_UNUSED,
943
             virConfPtr conf,
944
             unsigned int flags)
945
{
946
    int transport;
947
#ifndef WIN32
948
    g_autofree char *daemonPath = NULL;
949
#endif
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
    g_autofree char *tls_priority = NULL;
    g_autofree char *name = NULL;
    g_autofree char *command = NULL;
    g_autofree char *sockname = NULL;
    g_autofree char *netcat = NULL;
    g_autofree char *port = NULL;
    g_autofree char *authtype = NULL;
    g_autofree char *username = NULL;
    g_autofree char *pkipath = NULL;
    g_autofree char *keyfile = NULL;
    g_autofree char *sshauth = NULL;
    g_autofree char *knownHostsVerify = NULL;
    g_autofree char *knownHosts = NULL;
    g_autofree char *mode_str = NULL;
    g_autofree char *daemon_name = NULL;
965 966
    bool sanity = true;
    bool verify = true;
967 968 969
#ifndef WIN32
    bool tty = true;
#endif
970 971 972 973 974 975 976
    int mode;

    if (inside_daemon && !conn->uri->server) {
        mode = REMOTE_DRIVER_MODE_DIRECT;
    } else {
        mode = REMOTE_DRIVER_MODE_AUTO;
    }
977

978
    /* We handle *ALL* URIs here. The caller has rejected any
979
     * URIs we don't care about */
980

981
    if (conn->uri) {
982 983
        if (!transport_str) {
            if (conn->uri->server)
984
                transport = REMOTE_DRIVER_TRANSPORT_TLS;
985
            else
986
                transport = REMOTE_DRIVER_TRANSPORT_UNIX;
987
        } else {
988
            if ((transport = remoteDriverTransportTypeFromString(transport_str)) < 0) {
989 990
                virReportError(VIR_ERR_INVALID_ARG, "%s",
                               _("remote_open: transport in URL not recognised "
991
                                 "(should be tls|unix|ssh|ext|tcp|libssh2|libssh)"));
992
                return VIR_DRV_OPEN_ERROR;
993
            }
994 995 996 997 998 999 1000 1001 1002

            if (transport == REMOTE_DRIVER_TRANSPORT_UNIX &&
                conn->uri->server) {
                virReportError(VIR_ERR_INVALID_ARG,
                               _("using unix socket and remote "
                                 "server '%s' is not supported."),
                               conn->uri->server);
                return VIR_DRV_OPEN_ERROR;
            }
1003 1004 1005
        }
    } else {
        /* No URI, then must be probing so use UNIX socket */
1006
        transport = REMOTE_DRIVER_TRANSPORT_UNIX;
1007
    }
1008

1009
    /* Remote server defaults to "localhost" if not specified. */
1010
    if (conn->uri && conn->uri->port != 0) {
1011
        if (virAsprintf(&port, "%d", conn->uri->port) < 0)
1012
            goto failed;
1013
    } else if (transport == REMOTE_DRIVER_TRANSPORT_TLS) {
1014 1015
        if (VIR_STRDUP(port, LIBVIRTD_TLS_PORT) < 0)
            goto failed;
1016
    } else if (transport == REMOTE_DRIVER_TRANSPORT_TCP) {
1017 1018
        if (VIR_STRDUP(port, LIBVIRTD_TCP_PORT) < 0)
            goto failed;
1019
    } /* Port not used for unix, ext., default for ssh */
1020

1021 1022 1023 1024
    if (VIR_STRDUP(priv->hostname,
                   conn->uri && conn->uri->server ?
                   conn->uri->server : "localhost") < 0)
        goto failed;
1025

1026 1027
    if (conn->uri && VIR_STRDUP(username, conn->uri->user) < 0)
        goto failed;
1028

1029 1030 1031 1032 1033
    /* 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).
     */
1034
    size_t i;
1035

1036
    if (conn->uri) {
1037
        for (i = 0; i < conn->uri->paramsCount; i++) {
1038
            virURIParamPtr var = &conn->uri->params[i];
1039 1040 1041 1042
            EXTRACT_URI_ARG_STR("name", name);
            EXTRACT_URI_ARG_STR("command", command);
            EXTRACT_URI_ARG_STR("socket", sockname);
            EXTRACT_URI_ARG_STR("auth", authtype);
1043
            EXTRACT_URI_ARG_STR("sshauth", sshauth);
1044 1045 1046
            EXTRACT_URI_ARG_STR("netcat", netcat);
            EXTRACT_URI_ARG_STR("keyfile", keyfile);
            EXTRACT_URI_ARG_STR("pkipath", pkipath);
1047 1048
            EXTRACT_URI_ARG_STR("known_hosts", knownHosts);
            EXTRACT_URI_ARG_STR("known_hosts_verify", knownHostsVerify);
1049
            EXTRACT_URI_ARG_STR("tls_priority", tls_priority);
1050
            EXTRACT_URI_ARG_STR("mode", mode_str);
1051 1052
            EXTRACT_URI_ARG_BOOL("no_sanity", sanity);
            EXTRACT_URI_ARG_BOOL("no_verify", verify);
1053
#ifndef WIN32
1054
            EXTRACT_URI_ARG_BOOL("no_tty", tty);
1055
#endif
1056 1057

            if (STRCASEEQ(var->name, "authfile")) {
1058 1059
                /* Strip this param, used by virauth.c */
                var->ignore = 1;
1060
                continue;
1061
            }
1062 1063 1064

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

1067 1068
        /* Construct the original name. */
        if (!name) {
1069 1070 1071
            if (conn->uri->scheme &&
                (STREQ(conn->uri->scheme, "remote") ||
                 STRPREFIX(conn->uri->scheme, "remote+"))) {
1072
                /* Allow remote serve to probe */
1073 1074
                if (VIR_STRDUP(name, "") < 0)
                    goto failed;
1075
            } else {
M
Martin Kletzander 已提交
1076
                virURI tmpuri = {
1077
                    .scheme = (char *)driver_str,
1078
                    .query = virURIFormatParams(conn->uri),
1079 1080 1081 1082
                    .path = conn->uri->path,
                    .fragment = conn->uri->fragment,
                };

M
Martin Kletzander 已提交
1083
                name = virURIFormat(&tmpuri);
1084

1085 1086
                VIR_FREE(tmpuri.query);

1087 1088
                if (!name)
                    goto failed;
1089
            }
1090
        }
1091 1092
    } else {
        /* Probe URI server side */
1093 1094
        if (VIR_STRDUP(name, "") < 0)
            goto failed;
1095 1096
    }

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    if (conf && !mode_str &&
        virConfGetValueString(conf, "remote_mode", &mode_str) < 0)
        goto failed;

    if (mode_str &&
        (mode = remoteDriverModeTypeFromString(mode_str)) < 0)
        goto failed;

    /* Sanity check that nothing requested !direct mode by mistake */
    if (inside_daemon && !conn->uri->server && mode != REMOTE_DRIVER_MODE_DIRECT) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("Connections from inside daemon must be direct"));
        return VIR_DRV_OPEN_ERROR;
    }

1112
    VIR_DEBUG("proceeding with name = %s", name);
1113

1114
    /* For ext transport, command is required. */
1115
    if (transport == REMOTE_DRIVER_TRANSPORT_EXT && !command) {
1116 1117
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("remote_open: for 'ext' transport, command is required"));
1118 1119 1120
        goto failed;
    }

1121
    VIR_DEBUG("Connecting with transport %d", transport);
1122 1123 1124 1125 1126 1127 1128

    switch ((remoteDriverTransport)transport) {
    case REMOTE_DRIVER_TRANSPORT_UNIX:
    case REMOTE_DRIVER_TRANSPORT_SSH:
    case REMOTE_DRIVER_TRANSPORT_LIBSSH:
    case REMOTE_DRIVER_TRANSPORT_LIBSSH2:
        if (!sockname &&
1129 1130
            !(sockname = remoteGetUNIXSocket(transport, mode, driver_str,
                                             &daemon_name, flags)))
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
            goto failed;
        break;

    case REMOTE_DRIVER_TRANSPORT_TCP:
    case REMOTE_DRIVER_TRANSPORT_TLS:
    case REMOTE_DRIVER_TRANSPORT_EXT:
        break;

    case REMOTE_DRIVER_TRANSPORT_LAST:
    default:
        virReportEnumRangeError(remoteDriverTransport, transport);
        goto failed;
    }

    VIR_DEBUG("Chosen UNIX socket %s", NULLSTR(sockname));

1147
    /* Connect to the remote service. */
1148 1149
    switch ((remoteDriverTransport)transport) {
    case REMOTE_DRIVER_TRANSPORT_TLS:
1150 1151 1152
        if (conf && !tls_priority &&
            virConfGetValueString(conf, "tls_priority", &tls_priority) < 0)
            goto failed;
1153

1154
#ifdef WITH_GNUTLS
1155 1156
        priv->tls = virNetTLSContextNewClientPath(pkipath,
                                                  geteuid() != 0 ? true : false,
1157
                                                  tls_priority,
1158
                                                  sanity, verify);
1159 1160
        if (!priv->tls)
            goto failed;
1161
        priv->is_secure = 1;
1162
        G_GNUC_FALLTHROUGH;
1163
#else
1164
        (void)tls_priority;
1165
        (void)sanity;
1166
        (void)verify;
1167 1168 1169 1170
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("GNUTLS support not available in this build"));
        goto failed;
#endif
1171

1172
    case REMOTE_DRIVER_TRANSPORT_TCP:
1173
        priv->client = virNetClientNewTCP(priv->hostname, port, AF_UNSPEC);
1174
        if (!priv->client)
1175 1176
            goto failed;

1177
#ifdef WITH_GNUTLS
1178 1179 1180 1181
        if (priv->tls) {
            VIR_DEBUG("Starting TLS session");
            if (virNetClientSetTLSSession(priv->client, priv->tls) < 0)
                goto failed;
1182
        }
1183
#endif
1184 1185 1186

        break;

1187
    case REMOTE_DRIVER_TRANSPORT_LIBSSH2:
1188 1189 1190 1191
        VIR_DEBUG("Starting LibSSH2 session");

        priv->client = virNetClientNewLibSSH2(priv->hostname,
                                              port,
1192
                                              AF_UNSPEC,
1193 1194 1195 1196 1197 1198 1199
                                              username,
                                              keyfile,
                                              knownHosts,
                                              knownHostsVerify,
                                              sshauth,
                                              netcat,
                                              sockname,
1200 1201
                                              auth,
                                              conn->uri);
1202 1203 1204 1205 1206 1207
        if (!priv->client)
            goto failed;

        priv->is_secure = 1;
        break;

1208
    case REMOTE_DRIVER_TRANSPORT_LIBSSH:
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
        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;

1229
#ifndef WIN32
1230
    case REMOTE_DRIVER_TRANSPORT_UNIX:
1231 1232 1233 1234 1235 1236 1237 1238 1239
        if (flags & VIR_DRV_OPEN_REMOTE_AUTOSTART) {
            const char *env_name = remoteGetDaemonPathEnv();
            if (!(daemonPath = virFileFindResourceFull(daemon_name,
                                                       NULL, NULL,
                                                       abs_top_builddir "/src",
                                                       SBINDIR,
                                                       env_name)))
                goto failed;
        }
1240

1241 1242
        if (!(priv->client = virNetClientNewUNIX(sockname,
                                                 flags & VIR_DRV_OPEN_REMOTE_AUTOSTART,
1243
                                                 daemonPath)))
C
Chris Lalancette 已提交
1244
            goto failed;
1245

1246
        priv->is_secure = 1;
1247 1248
        break;

1249
    case REMOTE_DRIVER_TRANSPORT_SSH:
1250 1251
        if (!command && VIR_STRDUP(command, "ssh") < 0)
            goto failed;
J
Jim Meyering 已提交
1252

1253 1254 1255 1256
        if (!(priv->client = virNetClientNewSSH(priv->hostname,
                                                port,
                                                command,
                                                username,
1257 1258
                                                !tty,
                                                !verify,
1259
                                                netcat ? netcat : "nc",
1260
                                                keyfile,
1261 1262
                                                sockname)))
            goto failed;
1263 1264

        priv->is_secure = 1;
1265
        break;
1266

1267
    case REMOTE_DRIVER_TRANSPORT_EXT: {
1268 1269
        char const *cmd_argv[] = { command, NULL };
        if (!(priv->client = virNetClientNewExternal(cmd_argv)))
1270 1271
            goto failed;

E
Eric Blake 已提交
1272
        /* Do not set 'is_secure' flag since we can't guarantee
1273 1274
         * an external program is secure, and this flag must be
         * pessimistic */
1275 1276
    }   break;

1277 1278
#else /* WIN32 */

1279 1280 1281
    case REMOTE_DRIVER_TRANSPORT_UNIX:
    case REMOTE_DRIVER_TRANSPORT_SSH:
    case REMOTE_DRIVER_TRANSPORT_EXT:
1282 1283 1284
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("transport methods unix, ssh and ext are not supported "
                         "under Windows"));
1285
        goto failed;
1286 1287

#endif /* WIN32 */
1288 1289 1290 1291 1292

    case REMOTE_DRIVER_TRANSPORT_LAST:
    default:
        virReportEnumRangeError(remoteDriverTransport, transport);
        goto failed;
1293 1294
    } /* switch (transport) */

1295

1296 1297 1298 1299 1300 1301 1302 1303 1304
    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;
    }

1305 1306
    if (!(priv->closeCallback = virNewConnectCloseCallbackData()))
        goto failed;
M
Michal Privoznik 已提交
1307
    /* ref on behalf of netclient */
1308
    virObjectRef(priv->closeCallback);
1309 1310
    virNetClientSetCloseCallback(priv->client,
                                 remoteClientCloseFunc,
1311
                                 priv->closeCallback, virObjectFreeCallback);
1312

1313 1314
    if (!(priv->remoteProgram = virNetClientProgramNew(REMOTE_PROGRAM,
                                                       REMOTE_PROTOCOL_VERSION,
1315
                                                       remoteEvents,
1316
                                                       G_N_ELEMENTS(remoteEvents),
1317
                                                       conn)))
1318
        goto failed;
1319 1320 1321 1322 1323 1324
    if (!(priv->lxcProgram = virNetClientProgramNew(LXC_PROGRAM,
                                                    LXC_PROTOCOL_VERSION,
                                                    NULL,
                                                    0,
                                                    NULL)))
        goto failed;
1325 1326
    if (!(priv->qemuProgram = virNetClientProgramNew(QEMU_PROGRAM,
                                                     QEMU_PROTOCOL_VERSION,
1327
                                                     qemuEvents,
1328
                                                     G_N_ELEMENTS(qemuEvents),
1329
                                                     conn)))
1330 1331
        goto failed;

1332
    if (virNetClientAddProgram(priv->client, priv->remoteProgram) < 0 ||
1333
        virNetClientAddProgram(priv->client, priv->lxcProgram) < 0 ||
1334
        virNetClientAddProgram(priv->client, priv->qemuProgram) < 0)
1335
        goto failed;
1336 1337

    /* Try and authenticate with server */
1338 1339
    VIR_DEBUG("Trying authentication");
    if (remoteAuthenticate(conn, priv, auth, authtype) == -1)
1340 1341
        goto failed;

1342
    if (virNetClientKeepAliveIsSupported(priv->client)) {
1343 1344 1345
        priv->serverKeepAlive = remoteConnectSupportsFeatureUnlocked(conn,
                                    priv, VIR_DRV_FEATURE_PROGRAM_KEEPALIVE);
        if (!priv->serverKeepAlive) {
1346 1347 1348 1349 1350
            VIR_INFO("Disabling keepalive protocol since it is not supported"
                     " by the server");
        }
    }

1351
    /* Finally we can call the remote side's open function. */
1352
    {
1353
        remote_connect_open_args args = { &name, flags };
1354

1355
        VIR_DEBUG("Trying to open URI '%s'", name);
1356 1357
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_OPEN,
                 (xdrproc_t) xdr_remote_connect_open_args, (char *) &args,
1358
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
1359 1360
            goto failed;
    }
1361

1362 1363
    /* Now try and find out what URI the daemon used */
    if (conn->uri == NULL) {
1364
        remote_connect_get_uri_ret uriret;
1365

1366
        VIR_DEBUG("Trying to query remote URI");
1367 1368
        memset(&uriret, 0, sizeof(uriret));
        if (call(conn, priv, 0,
1369
                 REMOTE_PROC_CONNECT_GET_URI,
1370
                 (xdrproc_t) xdr_void, (char *) NULL,
1371
                 (xdrproc_t) xdr_remote_connect_get_uri_ret, (char *) &uriret) < 0)
1372 1373
            goto failed;

1374
        VIR_DEBUG("Auto-probed URI is %s", uriret.uri);
M
Martin Kletzander 已提交
1375
        conn->uri = virURIParse(uriret.uri);
1376
        VIR_FREE(uriret.uri);
1377
        if (!conn->uri)
1378 1379 1380
            goto failed;
    }

1381
    /* Set up events */
1382
    if (!(priv->eventState = virObjectEventStateNew()))
1383
        goto failed;
1384

1385 1386 1387 1388 1389
    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");
1390
    }
1391

1392 1393 1394 1395 1396 1397 1398
    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.");
    }

1399
    return VIR_DRV_OPEN_SUCCESS;
1400 1401

 failed:
1402
    virObjectUnref(priv->remoteProgram);
1403
    virObjectUnref(priv->lxcProgram);
1404
    virObjectUnref(priv->qemuProgram);
1405
    virNetClientClose(priv->client);
1406
    virObjectUnref(priv->client);
1407
    priv->client = NULL;
1408 1409
    virObjectUnref(priv->closeCallback);
    priv->closeCallback = NULL;
1410 1411 1412 1413
#ifdef WITH_GNUTLS
    virObjectUnref(priv->tls);
    priv->tls = NULL;
#endif
1414

1415
    VIR_FREE(priv->hostname);
1416
    return VIR_DRV_OPEN_ERROR;
1417
}
1418 1419
#undef EXTRACT_URI_ARG_STR
#undef EXTRACT_URI_ARG_BOOL
1420

1421
static struct private_data *
1422
remoteAllocPrivateData(void)
1423
{
1424
    struct private_data *priv;
1425
    if (VIR_ALLOC(priv) < 0)
1426
        return NULL;
1427

1428
    if (virMutexInit(&priv->lock) < 0) {
1429 1430
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot initialize mutex"));
1431
        VIR_FREE(priv);
1432
        return NULL;
1433 1434 1435
    }
    remoteDriverLock(priv);
    priv->localUses = 1;
1436 1437 1438 1439 1440

    return priv;
}

static virDrvOpenStatus
1441 1442
remoteConnectOpen(virConnectPtr conn,
                  virConnectAuthPtr auth,
1443
                  virConfPtr conf,
1444
                  unsigned int flags)
1445 1446
{
    struct private_data *priv;
1447 1448
    int ret = VIR_DRV_OPEN_ERROR;
    int rflags = 0;
1449
    const char *autostart = getenv("LIBVIRT_AUTOSTART");
1450 1451 1452 1453 1454 1455
    char *driver = NULL;
    char *transport = NULL;

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

1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
    if (inside_daemon) {
        if (!conn->uri) {
            ret = VIR_DRV_OPEN_DECLINED;
            goto cleanup;
        }

        /* If there's a driver registered we must defer to that.
         * If there isn't a driver, we must connect in "direct"
         * mode - see doRemoteOpen */
        if (!conn->uri->server &&
            virHasDriverForURIScheme(driver)) {
            ret = VIR_DRV_OPEN_DECLINED;
            goto cleanup;
        }
1471
    }
1472

1473
    if (!(priv = remoteAllocPrivateData()))
1474
        goto cleanup;
1475

1476
    if (flags & VIR_CONNECT_RO)
1477 1478
        rflags |= VIR_DRV_OPEN_REMOTE_RO;

1479
    /*
1480 1481 1482 1483 1484 1485
     * User session daemon is used for
     *
     *  - Any URI with /session suffix
     *  - Test driver, if a protocol is given
     *
     * provided we are running non-root
1486 1487 1488
     */
    if (conn->uri &&
        conn->uri->path &&
D
Daniel P. Berrange 已提交
1489
        conn->uri->scheme &&
1490 1491
        (STREQ(conn->uri->path, "/session") ||
         STRPREFIX(conn->uri->scheme, "test+")) &&
1492
        geteuid() > 0) {
1493
        VIR_DEBUG("User session daemon required");
1494
        rflags |= VIR_DRV_OPEN_REMOTE_USER;
1495 1496 1497 1498 1499 1500

        /*
         * 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.
         */
1501
        if (!conn->uri->server &&
1502
            (transport == NULL || STREQ(transport, "unix")) &&
1503
            (!autostart ||
1504 1505
             STRNEQ(autostart, "0"))) {
            VIR_DEBUG("Try daemon autostart");
1506
            rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1507
        }
1508 1509 1510
    }

    /*
J
John Levon 已提交
1511
     * If URI is NULL, then do a UNIX connection possibly auto-spawning
1512
     * unprivileged server and probe remote server for URI.
1513 1514
     */
    if (!conn->uri) {
1515
        VIR_DEBUG("Auto-probe remote URI");
1516
        if (geteuid() > 0) {
1517
            VIR_DEBUG("Auto-spawn user daemon instance");
1518
            rflags |= VIR_DRV_OPEN_REMOTE_USER;
1519 1520
            if (!autostart ||
                STRNEQ(autostart, "0"))
1521
                rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1522
        }
1523
    }
1524

1525
    ret = doRemoteOpen(conn, priv, driver, transport, auth, conf, rflags);
1526 1527
    if (ret != VIR_DRV_OPEN_SUCCESS) {
        conn->privateData = NULL;
1528
        remoteDriverUnlock(priv);
1529
        VIR_FREE(priv);
1530 1531
    } else {
        conn->privateData = priv;
1532
        remoteDriverUnlock(priv);
1533
    }
1534 1535 1536 1537

 cleanup:
    VIR_FREE(driver);
    VIR_FREE(transport);
1538 1539 1540 1541
    return ret;
}


1542
/*----------------------------------------------------------------------*/
1543

1544 1545

static int
1546
doRemoteClose(virConnectPtr conn, struct private_data *priv)
1547
{
1548 1549
    int ret = 0;

1550
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_CLOSE,
1551 1552
             (xdrproc_t) xdr_void, (char *) NULL,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
1553
        ret = -1;
1554

1555
#ifdef WITH_GNUTLS
1556
    virObjectUnref(priv->tls);
1557
    priv->tls = NULL;
1558
#endif
1559

1560 1561
    virNetClientSetCloseCallback(priv->client,
                                 NULL,
1562
                                 priv->closeCallback, virObjectFreeCallback);
1563

1564
    virNetClientClose(priv->client);
1565
    virObjectUnref(priv->client);
1566
    priv->client = NULL;
1567 1568
    virObjectUnref(priv->closeCallback);
    priv->closeCallback = NULL;
1569
    virObjectUnref(priv->remoteProgram);
1570
    virObjectUnref(priv->lxcProgram);
1571
    virObjectUnref(priv->qemuProgram);
1572
    priv->remoteProgram = priv->qemuProgram = priv->lxcProgram = NULL;
1573 1574 1575 1576 1577 1578 1579

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

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

1580
    virObjectUnref(priv->eventState);
1581
    priv->eventState = NULL;
1582

1583
    return ret;
1584 1585
}

1586
static int
1587
remoteConnectClose(virConnectPtr conn)
1588
{
1589 1590
    int ret = 0;
    struct private_data *priv = conn->privateData;
1591

1592 1593 1594 1595 1596 1597 1598
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        ret = doRemoteClose(conn, priv);
        conn->privateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
1599
        VIR_FREE(priv);
1600
    }
1601 1602
    if (priv)
        remoteDriverUnlock(priv);
1603

1604 1605
    return ret;
}
1606

1607

1608 1609 1610 1611 1612 1613 1614 1615 1616
/* 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 *
1617
remoteConnectGetType(virConnectPtr conn)
1618 1619
{
    char *rv = NULL;
1620
    remote_connect_get_type_ret ret;
1621
    struct private_data *priv = conn->privateData;
1622

1623
    remoteDriverLock(priv);
1624

1625 1626 1627 1628 1629
    /* Cached? */
    if (priv->type) {
        rv = priv->type;
        goto done;
    }
1630

1631
    memset(&ret, 0, sizeof(ret));
1632
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_GET_TYPE,
1633
             (xdrproc_t) xdr_void, (char *) NULL,
1634
             (xdrproc_t) xdr_remote_connect_get_type_ret, (char *) &ret) == -1)
1635
        goto done;
1636

1637 1638
    /* Stash. */
    rv = priv->type = ret.type;
1639

1640
 done:
1641 1642 1643
    remoteDriverUnlock(priv);
    return rv;
}
1644

1645
static int remoteConnectIsSecure(virConnectPtr conn)
1646 1647 1648
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
1649
    remote_connect_is_secure_ret ret;
1650
    remoteDriverLock(priv);
1651

1652
    memset(&ret, 0, sizeof(ret));
1653
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_IS_SECURE,
1654
             (xdrproc_t) xdr_void, (char *) NULL,
1655
             (xdrproc_t) xdr_remote_connect_is_secure_ret, (char *) &ret) == -1)
1656
        goto done;
1657

1658 1659 1660 1661 1662 1663 1664 1665 1666
    /* 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;
1667

1668
 done:
1669 1670 1671
    remoteDriverUnlock(priv);
    return rv;
}
1672

1673
static int remoteConnectIsEncrypted(virConnectPtr conn)
1674 1675
{
    int rv = -1;
1676
    bool encrypted;
1677
    struct private_data *priv = conn->privateData;
1678
    remote_connect_is_secure_ret ret;
1679
    remoteDriverLock(priv);
1680

1681
    memset(&ret, 0, sizeof(ret));
1682
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_IS_SECURE,
1683
             (xdrproc_t) xdr_void, (char *) NULL,
1684
             (xdrproc_t) xdr_remote_connect_is_secure_ret, (char *) &ret) == -1)
1685
        goto done;
1686

1687
    encrypted = virNetClientIsEncrypted(priv->client);
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698

    /* 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;

1699
 done:
1700 1701 1702 1703
    remoteDriverUnlock(priv);
    return rv;
}

1704
static int
1705 1706 1707 1708
remoteNodeGetCPUStats(virConnectPtr conn,
                      int cpuNum,
                      virNodeCPUStatsPtr params, int *nparams,
                      unsigned int flags)
1709 1710 1711 1712
{
    int rv = -1;
    remote_node_get_cpu_stats_args args;
    remote_node_get_cpu_stats_ret ret;
1713
    size_t i;
1714 1715 1716 1717 1718 1719 1720 1721
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

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

1722 1723 1724 1725 1726 1727
    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)
1728 1729 1730 1731 1732
        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) {
1733 1734 1735
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteNodeGetCPUStats: "
                         "returned number of stats exceeds limit"));
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
        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) {
1751
        if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) < 0) {
1752 1753 1754
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Stats %s too big for destination"),
                           ret.params.params_val[i].field);
1755 1756 1757 1758 1759 1760 1761
            goto cleanup;
        }
        params[i].value = ret.params.params_val[i].value;
    }

    rv = 0;

1762
 cleanup:
1763
    xdr_free((xdrproc_t) xdr_remote_node_get_cpu_stats_ret, (char *) &ret);
1764
 done:
1765 1766 1767 1768
    remoteDriverUnlock(priv);
    return rv;
}

1769
static int
1770 1771 1772 1773 1774
remoteNodeGetMemoryStats(virConnectPtr conn,
                         int cellNum,
                         virNodeMemoryStatsPtr params,
                         int *nparams,
                         unsigned int flags)
1775 1776 1777 1778
{
    int rv = -1;
    remote_node_get_memory_stats_args args;
    remote_node_get_memory_stats_ret ret;
1779
    size_t i;
1780 1781 1782 1783 1784 1785 1786 1787
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

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

1788
    memset(&ret, 0, sizeof(ret));
1789 1790 1791
    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)
1792 1793 1794 1795 1796
        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) {
1797 1798 1799
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteNodeGetMemoryStats: "
                         "returned number of stats exceeds limit"));
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
        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) {
1815
        if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) < 0) {
1816 1817 1818
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Stats %s too big for destination"),
                           ret.params.params_val[i].field);
1819 1820 1821 1822 1823 1824 1825
            goto cleanup;
        }
        params[i].value = ret.params.params_val[i].value;
    }

    rv = 0;

1826
 cleanup:
1827
    xdr_free((xdrproc_t) xdr_remote_node_get_memory_stats_ret, (char *) &ret);
1828
 done:
1829 1830 1831 1832
    remoteDriverUnlock(priv);
    return rv;
}

1833 1834
static int
remoteNodeGetCellsFreeMemory(virConnectPtr conn,
1835 1836 1837
                             unsigned long long *freeMems,
                             int startCell,
                             int maxCells)
1838
{
1839
    int rv = -1;
1840 1841
    remote_node_get_cells_free_memory_args args;
    remote_node_get_cells_free_memory_ret ret;
1842
    size_t i;
1843
    struct private_data *priv = conn->privateData;
1844

1845 1846
    remoteDriverLock(priv);

1847
    if (maxCells > REMOTE_NODE_MAX_CELLS) {
1848 1849 1850
        virReportError(VIR_ERR_RPC,
                       _("too many NUMA cells: %d > %d"),
                       maxCells, REMOTE_NODE_MAX_CELLS);
1851
        goto done;
1852 1853 1854
    }

    args.startCell = startCell;
1855
    args.maxcells = maxCells;
1856

1857 1858 1859 1860
    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)
1861
        goto done;
1862

1863
    for (i = 0; i < ret.cells.cells_len; i++)
1864
        freeMems[i] = ret.cells.cells_val[i];
1865 1866 1867

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

1868
    rv = ret.cells.cells_len;
1869

1870
 done:
1871
    remoteDriverUnlock(priv);
1872
    return rv;
1873 1874
}

1875
static int
1876
remoteConnectListDomains(virConnectPtr conn, int *ids, int maxids)
1877
{
1878
    int rv = -1;
1879
    size_t i;
1880 1881
    remote_connect_list_domains_args args;
    remote_connect_list_domains_ret ret;
1882
    struct private_data *priv = conn->privateData;
1883

1884 1885
    remoteDriverLock(priv);

1886
    if (maxids > REMOTE_DOMAIN_LIST_MAX) {
1887
        virReportError(VIR_ERR_RPC,
1888 1889
                       _("Too many domains '%d' for limit '%d'"),
                       maxids, REMOTE_DOMAIN_LIST_MAX);
1890
        goto done;
1891 1892 1893
    }
    args.maxids = maxids;

1894
    memset(&ret, 0, sizeof(ret));
1895 1896 1897
    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)
1898
        goto done;
1899 1900

    if (ret.ids.ids_len > maxids) {
1901
        virReportError(VIR_ERR_RPC,
1902
                       _("Too many domains '%d' for limit '%d'"),
1903
                       ret.ids.ids_len, maxids);
1904
        goto cleanup;
1905 1906 1907 1908 1909
    }

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

1910 1911
    rv = ret.ids.ids_len;

1912
 cleanup:
1913
    xdr_free((xdrproc_t) xdr_remote_connect_list_domains_ret, (char *) &ret);
1914

1915
 done:
1916
    remoteDriverUnlock(priv);
1917
    return rv;
1918 1919
}

1920 1921 1922 1923 1924 1925 1926
static int
remoteDeserializeDomainDiskErrors(remote_domain_disk_error *ret_errors_val,
                                  u_int ret_errors_len,
                                  int limit,
                                  virDomainDiskErrorPtr errors,
                                  int maxerrors)
{
1927 1928
    size_t i = 0;
    size_t j;
1929 1930

    if (ret_errors_len > limit || ret_errors_len > maxerrors) {
1931 1932
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned number of disk errors exceeds limit"));
1933 1934 1935 1936
        goto error;
    }

    for (i = 0; i < ret_errors_len; i++) {
1937
        if (VIR_STRDUP(errors[i].disk, ret_errors_val[i].disk) < 0)
1938 1939 1940 1941 1942 1943
            goto error;
        errors[i].error = ret_errors_val[i].error;
    }

    return 0;

1944
 error:
1945
    for (j = 0; j < i; j++)
X
Xu Yandong 已提交
1946
        VIR_FREE(errors[j].disk);
1947 1948 1949 1950

    return -1;
}

1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
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);

1965
    make_nonnull_domain(&args.dom, domain);
1966 1967 1968 1969
    args.nparams = *nparams;
    args.path = (char *) path;
    args.flags = flags;

1970 1971 1972 1973
    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)
1974 1975 1976 1977 1978
        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) {
1979 1980 1981
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteDomainBlockStatsFlags: "
                         "returned number of stats exceeds limit"));
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
        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;

1996 1997 1998
    /* Deserialize the result. */
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
1999
                                  REMOTE_DOMAIN_BLOCK_STATS_PARAMETERS_MAX,
2000 2001
                                  &params,
                                  nparams) < 0)
2002 2003 2004 2005
        goto cleanup;

    rv = 0;

2006
 cleanup:
2007 2008
    xdr_free((xdrproc_t) xdr_remote_domain_block_stats_flags_ret,
             (char *) &ret);
2009
 done:
2010 2011 2012 2013
    remoteDriverUnlock(priv);
    return rv;
}

2014
static int
2015 2016 2017
remoteDomainGetMemoryParameters(virDomainPtr domain,
                                virTypedParameterPtr params, int *nparams,
                                unsigned int flags)
2018 2019 2020 2021 2022 2023 2024 2025
{
    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);

2026
    make_nonnull_domain(&args.dom, domain);
2027 2028 2029
    args.nparams = *nparams;
    args.flags = flags;

2030 2031 2032 2033
    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)
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
        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;
    }

2045 2046 2047 2048 2049
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX,
                                  &params,
                                  nparams) < 0)
2050
        goto cleanup;
2051 2052 2053

    rv = 0;

2054
 cleanup:
2055 2056
    xdr_free((xdrproc_t) xdr_remote_domain_get_memory_parameters_ret,
             (char *) &ret);
2057
 done:
2058 2059 2060 2061
    remoteDriverUnlock(priv);
    return rv;
}

2062
static int
2063 2064 2065
remoteDomainGetNumaParameters(virDomainPtr domain,
                              virTypedParameterPtr params, int *nparams,
                              unsigned int flags)
2066 2067 2068 2069 2070 2071 2072 2073
{
    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);

2074
    make_nonnull_domain(&args.dom, domain);
2075 2076 2077
    args.nparams = *nparams;
    args.flags = flags;

2078 2079 2080 2081
    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)
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
        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;
    }

2093 2094 2095 2096 2097
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_NUMA_PARAMETERS_MAX,
                                  &params,
                                  nparams) < 0)
2098 2099 2100 2101
        goto cleanup;

    rv = 0;

2102
 cleanup:
2103 2104
    xdr_free((xdrproc_t) xdr_remote_domain_get_numa_parameters_ret,
             (char *) &ret);
2105
 done:
2106 2107 2108 2109
    remoteDriverUnlock(priv);
    return rv;
}

2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
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;
}

2149 2150 2151
static int
remoteDomainGetPerfEvents(virDomainPtr domain,
                          virTypedParameterPtr *params,
2152 2153
                          int *nparams,
                          unsigned int flags)
2154 2155 2156 2157 2158 2159 2160 2161 2162
{
    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);
2163
    args.flags = flags;
2164 2165 2166 2167 2168 2169 2170 2171

    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,
2172 2173 2174 2175
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_PERF_EVENTS_MAX,
                                  params,
                                  nparams) < 0)
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
        goto cleanup;

    rv = 0;

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

2188
static int
2189 2190 2191
remoteDomainGetBlkioParameters(virDomainPtr domain,
                               virTypedParameterPtr params, int *nparams,
                               unsigned int flags)
2192 2193 2194 2195 2196 2197 2198 2199
{
    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);

2200
    make_nonnull_domain(&args.dom, domain);
2201 2202 2203
    args.nparams = *nparams;
    args.flags = flags;

2204 2205 2206 2207
    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)
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
        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;
    }

2219 2220 2221 2222 2223
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX,
                                  &params,
                                  nparams) < 0)
2224
        goto cleanup;
2225 2226 2227

    rv = 0;

2228
 cleanup:
2229 2230
    xdr_free((xdrproc_t) xdr_remote_domain_get_blkio_parameters_ret,
             (char *) &ret);
2231
 done:
2232 2233 2234 2235
    remoteDriverUnlock(priv);
    return rv;
}

2236
static int
2237 2238 2239 2240 2241
remoteDomainGetVcpuPinInfo(virDomainPtr domain,
                           int ncpumaps,
                           unsigned char *cpumaps,
                           int maplen,
                           unsigned int flags)
2242 2243
{
    int rv = -1;
2244
    size_t i;
E
Eric Blake 已提交
2245 2246
    remote_domain_get_vcpu_pin_info_args args;
    remote_domain_get_vcpu_pin_info_ret ret;
2247 2248 2249 2250 2251
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    if (ncpumaps > REMOTE_VCPUINFO_MAX) {
2252 2253 2254
        virReportError(VIR_ERR_RPC,
                       _("vCPU count exceeds maximum: %d > %d"),
                       ncpumaps, REMOTE_VCPUINFO_MAX);
2255 2256 2257 2258 2259
        goto done;
    }

    if (INT_MULTIPLY_OVERFLOW(ncpumaps, maplen) ||
        ncpumaps * maplen > REMOTE_CPUMAPS_MAX) {
2260 2261 2262
        virReportError(VIR_ERR_RPC,
                       _("vCPU map buffer length exceeds maximum: %d > %d"),
                       ncpumaps * maplen, REMOTE_CPUMAPS_MAX);
2263 2264 2265
        goto done;
    }

2266
    make_nonnull_domain(&args.dom, domain);
2267 2268 2269 2270
    args.ncpumaps = ncpumaps;
    args.maplen = maplen;
    args.flags = flags;

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

2273 2274 2275 2276 2277
    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)
2278 2279 2280
        goto done;

    if (ret.num > ncpumaps) {
2281 2282 2283
        virReportError(VIR_ERR_RPC,
                       _("host reports too many vCPUs: %d > %d"),
                       ret.num, ncpumaps);
2284 2285 2286 2287
        goto cleanup;
    }

    if (ret.cpumaps.cpumaps_len > ncpumaps * maplen) {
2288 2289 2290
        virReportError(VIR_ERR_RPC,
                       _("host reports map buffer length exceeds maximum: %d > %d"),
                       ret.cpumaps.cpumaps_len, ncpumaps * maplen);
2291 2292 2293
        goto cleanup;
    }

2294
    memset(cpumaps, 0, ncpumaps * maplen);
2295 2296 2297 2298 2299 2300

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

    rv = ret.num;

2301
 cleanup:
2302
    xdr_free((xdrproc_t) xdr_remote_domain_get_vcpu_pin_info_ret, (char *) &ret);
2303

2304
 done:
2305 2306 2307 2308
    remoteDriverUnlock(priv);
    return rv;
}

2309
static int
2310 2311 2312 2313
remoteDomainPinEmulator(virDomainPtr dom,
                        unsigned char *cpumap,
                        int cpumaplen,
                        unsigned int flags)
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
{
    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;

2342
 done:
2343 2344 2345 2346 2347 2348
    remoteDriverUnlock(priv);
    return rv;
}


static int
2349 2350 2351 2352
remoteDomainGetEmulatorPinInfo(virDomainPtr domain,
                               unsigned char *cpumaps,
                               int maplen,
                               unsigned int flags)
2353 2354
{
    int rv = -1;
2355
    size_t i;
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
    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));

2376 2377 2378 2379 2380
    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)
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
        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;

2397
 cleanup:
2398 2399
    xdr_free((xdrproc_t) xdr_remote_domain_get_emulator_pin_info_ret,
             (char *) &ret);
2400

2401
 done:
2402 2403 2404 2405
    remoteDriverUnlock(priv);
    return rv;
}

2406
static int
2407 2408 2409 2410 2411
remoteDomainGetVcpus(virDomainPtr domain,
                     virVcpuInfoPtr info,
                     int maxinfo,
                     unsigned char *cpumaps,
                     int maplen)
2412
{
2413
    int rv = -1;
2414
    size_t i;
2415 2416
    remote_domain_get_vcpus_args args;
    remote_domain_get_vcpus_ret ret;
2417
    struct private_data *priv = domain->conn->privateData;
2418

2419 2420
    remoteDriverLock(priv);

2421
    if (maxinfo > REMOTE_VCPUINFO_MAX) {
2422 2423 2424
        virReportError(VIR_ERR_RPC,
                       _("vCPU count exceeds maximum: %d > %d"),
                       maxinfo, REMOTE_VCPUINFO_MAX);
2425
        goto done;
2426
    }
2427 2428
    if (INT_MULTIPLY_OVERFLOW(maxinfo, maplen) ||
        maxinfo * maplen > REMOTE_CPUMAPS_MAX) {
2429 2430 2431
        virReportError(VIR_ERR_RPC,
                       _("vCPU map buffer length exceeds maximum: %d > %d"),
                       maxinfo * maplen, REMOTE_CPUMAPS_MAX);
2432
        goto done;
2433 2434
    }

2435
    make_nonnull_domain(&args.dom, domain);
2436 2437 2438
    args.maxinfo = maxinfo;
    args.maplen = maplen;

2439 2440 2441 2442
    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)
2443
        goto done;
2444 2445

    if (ret.info.info_len > maxinfo) {
2446 2447 2448
        virReportError(VIR_ERR_RPC,
                       _("host reports too many vCPUs: %d > %d"),
                       ret.info.info_len, maxinfo);
2449
        goto cleanup;
2450
    }
2451
    if (ret.cpumaps.cpumaps_len > maxinfo * maplen) {
2452 2453 2454
        virReportError(VIR_ERR_RPC,
                       _("host reports map buffer length exceeds maximum: %d > %d"),
                       ret.cpumaps.cpumaps_len, maxinfo * maplen);
2455
        goto cleanup;
2456 2457
    }

2458 2459
    memset(info, 0, sizeof(virVcpuInfo) * maxinfo);
    memset(cpumaps, 0, maxinfo * maplen);
2460

2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
    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];

2471 2472
    rv = ret.info.info_len;

2473
 cleanup:
2474
    xdr_free((xdrproc_t) xdr_remote_domain_get_vcpus_ret, (char *) &ret);
2475

2476
 done:
2477
    remoteDriverUnlock(priv);
2478
    return rv;
2479 2480
}

2481
static int
2482 2483 2484
remoteDomainGetIOThreadInfo(virDomainPtr dom,
                            virDomainIOThreadInfoPtr **info,
                            unsigned int flags)
2485 2486 2487 2488
{
    int rv = -1;
    size_t i;
    struct private_data *priv = dom->conn->privateData;
2489 2490
    remote_domain_get_iothread_info_args args;
    remote_domain_get_iothread_info_ret ret;
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
    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));

2502 2503
    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_IOTHREAD_INFO,
             (xdrproc_t)xdr_remote_domain_get_iothread_info_args,
2504
             (char *)&args,
2505
             (xdrproc_t)xdr_remote_domain_get_iothread_info_ret,
2506 2507 2508
             (char *)&ret) == -1)
        goto done;

2509
    if (ret.info.info_len > REMOTE_IOTHREAD_INFO_MAX) {
2510 2511
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Too many IOThreads in info: %d for limit %d"),
2512
                       ret.info.info_len, REMOTE_IOTHREAD_INFO_MAX);
2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
        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++)
2548
            virDomainIOThreadInfoFree(info_ret[i]);
2549 2550
        VIR_FREE(info_ret);
    }
2551
    xdr_free((xdrproc_t)xdr_remote_domain_get_iothread_info_ret,
2552 2553 2554 2555 2556 2557 2558
             (char *) &ret);

 done:
    remoteDriverUnlock(priv);
    return rv;
}

2559
static int
2560
remoteDomainGetSecurityLabel(virDomainPtr domain, virSecurityLabelPtr seclabel)
2561 2562 2563 2564
{
    remote_domain_get_security_label_args args;
    remote_domain_get_security_label_ret ret;
    struct private_data *priv = domain->conn->privateData;
2565 2566 2567
    int rv = -1;

    remoteDriverLock(priv);
2568

2569 2570 2571
    make_nonnull_domain(&args.dom, domain);
    memset(&ret, 0, sizeof(ret));
    memset(seclabel, 0, sizeof(*seclabel));
2572

2573 2574 2575
    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) {
2576
        goto done;
2577 2578 2579
    }

    if (ret.label.label_val != NULL) {
2580
        if (strlen(ret.label.label_val) >= sizeof(seclabel->label)) {
2581 2582
            virReportError(VIR_ERR_RPC, _("security label exceeds maximum: %zu"),
                           sizeof(seclabel->label) - 1);
A
Alex Jia 已提交
2583
            goto cleanup;
2584
        }
2585
        strcpy(seclabel->label, ret.label.label_val);
2586 2587 2588
        seclabel->enforcing = ret.enforcing;
    }

2589 2590
    rv = 0;

2591
 cleanup:
A
Alex Jia 已提交
2592 2593
    xdr_free((xdrproc_t) xdr_remote_domain_get_security_label_ret, (char *)&ret);

2594
 done:
2595 2596
    remoteDriverUnlock(priv);
    return rv;
2597 2598
}

M
Marcelo Cerri 已提交
2599
static int
2600
remoteDomainGetSecurityLabelList(virDomainPtr domain, virSecurityLabelPtr* seclabels)
M
Marcelo Cerri 已提交
2601 2602 2603 2604
{
    remote_domain_get_security_label_list_args args;
    remote_domain_get_security_label_list_ret ret;
    struct private_data *priv = domain->conn->privateData;
2605 2606
    size_t i;
    int rv = -1;
M
Marcelo Cerri 已提交
2607 2608 2609

    remoteDriverLock(priv);

2610
    make_nonnull_domain(&args.dom, domain);
M
Marcelo Cerri 已提交
2611 2612
    memset(&ret, 0, sizeof(ret));

2613 2614 2615
    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 已提交
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
        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;

2637
 cleanup:
M
Marcelo Cerri 已提交
2638 2639
    xdr_free((xdrproc_t) xdr_remote_domain_get_security_label_list_ret, (char *)&ret);

2640
 done:
M
Marcelo Cerri 已提交
2641 2642 2643 2644
    remoteDriverUnlock(priv);
    return rv;
}

2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660
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;

2661
    memset(&ret, 0, sizeof(ret));
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672
    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;

2673
 done:
2674 2675 2676 2677
    remoteDriverUnlock(priv);
    return rv;
}

2678
static int
2679
remoteNodeGetSecurityModel(virConnectPtr conn, virSecurityModelPtr secmodel)
2680 2681 2682
{
    remote_node_get_security_model_ret ret;
    struct private_data *priv = conn->privateData;
2683 2684 2685
    int rv = -1;

    remoteDriverLock(priv);
2686

2687 2688
    memset(&ret, 0, sizeof(ret));
    memset(secmodel, 0, sizeof(*secmodel));
2689

2690 2691 2692
    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) {
2693
        goto done;
2694 2695 2696
    }

    if (ret.model.model_val != NULL) {
2697
        if (strlen(ret.model.model_val) >= sizeof(secmodel->model)) {
2698 2699
            virReportError(VIR_ERR_RPC, _("security model exceeds maximum: %zu"),
                           sizeof(secmodel->model) - 1);
A
Alex Jia 已提交
2700
            goto cleanup;
2701
        }
2702
        strcpy(secmodel->model, ret.model.model_val);
2703 2704 2705
    }

    if (ret.doi.doi_val != NULL) {
2706
        if (strlen(ret.doi.doi_val) >= sizeof(secmodel->doi)) {
2707 2708
            virReportError(VIR_ERR_RPC, _("security doi exceeds maximum: %zu"),
                           sizeof(secmodel->doi) - 1);
A
Alex Jia 已提交
2709
            goto cleanup;
2710
        }
2711
        strcpy(secmodel->doi, ret.doi.doi_val);
2712
    }
2713 2714 2715

    rv = 0;

2716
 cleanup:
A
Alex Jia 已提交
2717 2718
    xdr_free((xdrproc_t) xdr_remote_node_get_security_model_ret, (char *)&ret);

2719
 done:
2720 2721
    remoteDriverUnlock(priv);
    return rv;
2722 2723
}

2724
static int
2725 2726 2727 2728 2729
remoteDomainMigratePrepare(virConnectPtr dconn,
                           char **cookie, int *cookielen,
                           const char *uri_in, char **uri_out,
                           unsigned long flags, const char *dname,
                           unsigned long resource)
2730
{
2731
    int rv = -1;
2732 2733
    remote_domain_migrate_prepare_args args;
    remote_domain_migrate_prepare_ret ret;
2734
    struct private_data *priv = dconn->privateData;
2735

2736 2737
    remoteDriverLock(priv);

2738 2739 2740 2741 2742
    args.uri_in = uri_in == NULL ? NULL : (char **) &uri_in;
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;

2743 2744 2745 2746
    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)
2747
        goto done;
2748 2749 2750 2751 2752 2753 2754 2755

    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 已提交
2756
    VIR_FREE(ret.uri_out);
2757 2758
    rv = 0;

2759
 done:
2760
    remoteDriverUnlock(priv);
2761
    return rv;
2762 2763
}

D
Daniel Veillard 已提交
2764
static int
2765 2766 2767 2768 2769 2770
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 已提交
2771
{
2772
    int rv = -1;
D
Daniel Veillard 已提交
2773 2774
    remote_domain_migrate_prepare2_args args;
    remote_domain_migrate_prepare2_ret ret;
2775
    struct private_data *priv = dconn->privateData;
D
Daniel Veillard 已提交
2776

2777 2778
    remoteDriverLock(priv);

D
Daniel Veillard 已提交
2779 2780 2781 2782 2783 2784
    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;

2785 2786 2787 2788
    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)
2789
        goto done;
D
Daniel Veillard 已提交
2790 2791

    if (ret.cookie.cookie_len > 0) {
2792
        if (!cookie || !cookielen) {
2793 2794
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookie or cookielen"));
2795 2796
            goto error;
        }
D
Daniel Veillard 已提交
2797 2798 2799
        *cookie = ret.cookie.cookie_val; /* Caller frees. */
        *cookielen = ret.cookie.cookie_len;
    }
2800 2801
    if (ret.uri_out) {
        if (!uri_out) {
2802 2803
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores uri_out"));
2804 2805
            goto error;
        }
D
Daniel Veillard 已提交
2806
        *uri_out = *ret.uri_out; /* Caller frees. */
2807
    }
D
Daniel Veillard 已提交
2808

2809 2810
    rv = 0;

2811
 done:
O
Osier Yang 已提交
2812
    VIR_FREE(ret.uri_out);
2813
    remoteDriverUnlock(priv);
2814
    return rv;
2815
 error:
2816 2817 2818 2819 2820
    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 已提交
2821 2822
}

2823
static int
2824
remoteDomainCreate(virDomainPtr domain)
2825
{
2826
    int rv = -1;
2827
    remote_domain_create_args args;
2828 2829
    remote_domain_lookup_by_uuid_args args2;
    remote_domain_lookup_by_uuid_ret ret2;
2830
    struct private_data *priv = domain->conn->privateData;
2831

2832 2833
    remoteDriverLock(priv);

2834
    make_nonnull_domain(&args.dom, domain);
2835

2836 2837 2838
    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)
2839
        goto done;
2840

2841 2842 2843 2844
    /* 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.
     */
2845 2846 2847 2848 2849
    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)
2850 2851 2852
        goto done;

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

2855 2856
    rv = 0;

2857
 done:
2858
    remoteDriverUnlock(priv);
2859
    return rv;
2860 2861
}

2862
static char *
2863
remoteDomainGetSchedulerType(virDomainPtr domain, int *nparams)
2864
{
2865 2866 2867
    char *rv = NULL;
    remote_domain_get_scheduler_type_args args;
    remote_domain_get_scheduler_type_ret ret;
2868
    struct private_data *priv = domain->conn->privateData;
2869

2870 2871
    remoteDriverLock(priv);

2872
    make_nonnull_domain(&args.dom, domain);
2873

2874 2875 2876 2877
    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)
2878
        goto done;
2879 2880 2881 2882

    if (nparams) *nparams = ret.nparams;

    /* Caller frees this. */
2883 2884
    rv = ret.type;

2885
 done:
2886
    remoteDriverUnlock(priv);
2887
    return rv;
2888 2889
}

2890
static int
2891
remoteDomainMemoryStats(virDomainPtr domain,
2892
                        virDomainMemoryStatPtr stats,
2893 2894
                        unsigned int nr_stats,
                        unsigned int flags)
2895 2896 2897 2898 2899
{
    int rv = -1;
    remote_domain_memory_stats_args args;
    remote_domain_memory_stats_ret ret;
    struct private_data *priv = domain->conn->privateData;
2900
    size_t i;
2901 2902 2903

    remoteDriverLock(priv);

2904
    make_nonnull_domain(&args.dom, domain);
2905
    if (nr_stats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
2906 2907 2908
        virReportError(VIR_ERR_RPC,
                       _("too many memory stats requested: %d > %d"), nr_stats,
                       REMOTE_DOMAIN_MEMORY_STATS_MAX);
2909 2910 2911
        goto done;
    }
    args.maxStats = nr_stats;
2912
    args.flags = flags;
2913
    memset(&ret, 0, sizeof(ret));
2914

2915 2916 2917 2918 2919
    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)
2920 2921 2922 2923 2924 2925 2926 2927 2928
        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);

2929
 done:
2930 2931 2932 2933
    remoteDriverUnlock(priv);
    return rv;
}

2934
static int
2935 2936 2937 2938 2939 2940
remoteDomainBlockPeek(virDomainPtr domain,
                      const char *path,
                      unsigned long long offset,
                      size_t size,
                      void *buffer,
                      unsigned int flags)
2941
{
2942
    int rv = -1;
2943 2944
    remote_domain_block_peek_args args;
    remote_domain_block_peek_ret ret;
2945
    struct private_data *priv = domain->conn->privateData;
2946

2947 2948
    remoteDriverLock(priv);

2949
    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
2950 2951 2952
        virReportError(VIR_ERR_RPC,
                       _("block peek request too large for remote protocol, %zi > %d"),
                       size, REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX);
2953
        goto done;
2954 2955
    }

2956
    make_nonnull_domain(&args.dom, domain);
2957 2958 2959 2960 2961
    args.path = (char *) path;
    args.offset = offset;
    args.size = size;
    args.flags = flags;

2962 2963 2964 2965 2966 2967
    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)
2968
        goto done;
2969 2970

    if (ret.buffer.buffer_len != size) {
2971 2972
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned buffer is not same size as requested"));
2973
        goto cleanup;
2974 2975
    }

2976
    memcpy(buffer, ret.buffer.buffer_val, size);
2977 2978
    rv = 0;

2979
 cleanup:
2980
    VIR_FREE(ret.buffer.buffer_val);
2981

2982
 done:
2983
    remoteDriverUnlock(priv);
2984
    return rv;
2985 2986
}

R
Richard W.M. Jones 已提交
2987
static int
2988 2989 2990 2991 2992
remoteDomainMemoryPeek(virDomainPtr domain,
                       unsigned long long offset,
                       size_t size,
                       void *buffer,
                       unsigned int flags)
R
Richard W.M. Jones 已提交
2993
{
2994
    int rv = -1;
R
Richard W.M. Jones 已提交
2995 2996
    remote_domain_memory_peek_args args;
    remote_domain_memory_peek_ret ret;
2997
    struct private_data *priv = domain->conn->privateData;
R
Richard W.M. Jones 已提交
2998

2999 3000
    remoteDriverLock(priv);

R
Richard W.M. Jones 已提交
3001
    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
3002 3003 3004
        virReportError(VIR_ERR_RPC,
                       _("memory peek request too large for remote protocol, %zi > %d"),
                       size, REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX);
3005
        goto done;
R
Richard W.M. Jones 已提交
3006 3007
    }

3008
    make_nonnull_domain(&args.dom, domain);
R
Richard W.M. Jones 已提交
3009 3010 3011 3012
    args.offset = offset;
    args.size = size;
    args.flags = flags;

3013 3014 3015 3016 3017 3018
    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)
3019
        goto done;
R
Richard W.M. Jones 已提交
3020 3021

    if (ret.buffer.buffer_len != size) {
3022 3023
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned buffer is not same size as requested"));
3024
        goto cleanup;
R
Richard W.M. Jones 已提交
3025 3026
    }

3027
    memcpy(buffer, ret.buffer.buffer_val, size);
3028 3029
    rv = 0;

3030
 cleanup:
3031
    VIR_FREE(ret.buffer.buffer_val);
R
Richard W.M. Jones 已提交
3032

3033
 done:
3034
    remoteDriverUnlock(priv);
3035
    return rv;
R
Richard W.M. Jones 已提交
3036 3037
}

3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070
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;
    }

3071
 done:
3072 3073 3074 3075
    remoteDriverUnlock(priv);
    return rv;
}

3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
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;
    }

3114 3115
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
3116
                                  REMOTE_DOMAIN_BLOCK_IO_TUNE_PARAMETERS_MAX,
3117 3118
                                  &params,
                                  nparams) < 0)
3119 3120 3121 3122
        goto cleanup;

    rv = 0;

3123
 cleanup:
3124 3125
    xdr_free((xdrproc_t) xdr_remote_domain_get_block_io_tune_ret,
             (char *) &ret);
3126
 done:
3127 3128 3129 3130
    remoteDriverUnlock(priv);
    return rv;
}

3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146
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) {
3147 3148 3149
        virReportError(VIR_ERR_RPC,
                       _("nparams count exceeds maximum: %u > %u"),
                       nparams, REMOTE_NODE_CPU_STATS_MAX);
3150 3151 3152
        goto done;
    }
    if (ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
3153 3154 3155
        virReportError(VIR_ERR_RPC,
                       _("ncpus count exceeds maximum: %u > %u"),
                       ncpus, REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX);
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176
        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 &&
3177
         ((ret.params.params_len % ret.nparams) || ret.nparams > nparams))) {
3178 3179 3180
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteDomainGetCPUStats: "
                         "returned number of stats exceeds limit"));
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193
        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
3194 3195
     * to expand things back into a possibly sparse array, where the
     * tail of the array may be omitted.
3196 3197
     */
    memset(params, 0, sizeof(*params) * nparams * ncpus);
3198
    ncpus = ret.params.params_len / ret.nparams;
3199 3200
    for (cpu = 0; cpu < ncpus; cpu++) {
        int tmp = nparams;
3201
        virTypedParameterPtr cpu_params = &params[cpu * nparams];
3202 3203
        remote_typed_param *stride = &ret.params.params_val[cpu * ret.nparams];

3204 3205 3206 3207
        if (virTypedParamsDeserialize((virTypedParameterRemotePtr) stride,
                                      ret.nparams,
                                      REMOTE_NODE_CPU_STATS_MAX,
                                      &cpu_params, &tmp) < 0)
3208 3209 3210 3211
            goto cleanup;
    }

    rv = ret.nparams;
3212
 cleanup:
A
Alex Jia 已提交
3213
    if (rv < 0)
3214
        virTypedParamsClear(params, nparams * ncpus);
3215

3216 3217
    xdr_free((xdrproc_t) xdr_remote_domain_get_cpu_stats_ret,
             (char *) &ret);
3218
 done:
3219 3220 3221 3222
    remoteDriverUnlock(priv);
    return rv;
}

3223

3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234
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;
3235
    remote_connect_network_event_register_any_ret ret;
3236 3237
    int callbackID;
    int count;
3238
    remote_nonnull_network network;
3239 3240 3241

    remoteDriverLock(priv);

3242 3243 3244 3245
    if ((count = virNetworkEventStateRegisterClient(conn, priv->eventState,
                                                    net, eventID, callback,
                                                    opaque, freecb,
                                                    &callbackID)) < 0)
3246 3247 3248 3249 3250 3251
        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;
3252 3253 3254 3255 3256 3257
        if (net) {
            make_nonnull_network(&network, net);
            args.net = &network;
        } else {
            args.net = NULL;
        }
3258

3259
        memset(&ret, 0, sizeof(ret));
3260 3261
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_NETWORK_EVENT_REGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_network_event_register_any_args, (char *) &args,
3262
                 (xdrproc_t) xdr_remote_connect_network_event_register_any_ret, (char *) &ret) == -1) {
3263
            virObjectEventStateDeregisterID(conn, priv->eventState,
3264
                                            callbackID, false);
3265 3266
            goto done;
        }
3267 3268
        virObjectEventStateSetRemote(conn, priv->eventState, callbackID,
                                     ret.callbackID);
3269 3270 3271 3272
    }

    rv = callbackID;

3273
 done:
3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286
    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;
3287
    int remoteID;
3288 3289 3290 3291
    int count;

    remoteDriverLock(priv);

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

3296
    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
3297
                                                 callbackID, true)) < 0)
3298 3299 3300 3301 3302
        goto done;

    /* If that was the last callback for this eventID, we need to disable
     * events on the server */
    if (count == 0) {
3303
        args.callbackID = remoteID;
3304 3305 3306 3307 3308 3309 3310 3311 3312

        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;

3313
 done:
3314 3315 3316 3317
    remoteDriverUnlock(priv);
    return rv;
}

3318 3319 3320 3321 3322 3323 3324 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
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,
3358
                                            callbackID, false);
3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390
            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,
3391
                                                 callbackID, true)) < 0)
3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412
        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;
}

3413

3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453
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,
3454
                                            callbackID, false);
3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487
            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,
3488
                                                 callbackID, true)) < 0)
3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510
        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;
}


3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550
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,
3551
                                            callbackID, false);
3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584
            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,
3585
                                                 callbackID, true)) < 0)
3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607
        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;
}


3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650
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,
3651
                                            callbackID, false);
3652 3653 3654 3655 3656 3657 3658 3659
            goto done;
        }
        virObjectEventStateSetRemote(conn, priv->eventState, callbackID,
                                     ret.callbackID);
    }

    rv = callbackID;

3660
 done:
3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682
    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,
3683
                                                 callbackID, true)) < 0)
3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698
        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;

3699
 done:
3700 3701 3702 3703
    remoteDriverUnlock(priv);
    return rv;
}

3704 3705
/*----------------------------------------------------------------------*/

3706
static char *
3707 3708 3709 3710
remoteConnectFindStoragePoolSources(virConnectPtr conn,
                                    const char *type,
                                    const char *srcSpec,
                                    unsigned int flags)
3711
{
3712
    char *rv = NULL;
3713 3714
    remote_connect_find_storage_pool_sources_args args;
    remote_connect_find_storage_pool_sources_ret ret;
3715
    struct private_data *priv = conn->privateData;
3716

3717 3718
    remoteDriverLock(priv);

3719
    args.type = (char*)type;
3720
    args.srcSpec = srcSpec ? (char **)&srcSpec : NULL;
3721 3722
    args.flags = flags;

3723
    memset(&ret, 0, sizeof(ret));
3724 3725 3726
    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)
3727
        goto done;
3728

3729 3730 3731
    rv = ret.xml;
    ret.xml = NULL; /* To stop xdr_free free'ing it */

3732
    xdr_free((xdrproc_t) xdr_remote_connect_find_storage_pool_sources_ret, (char *) &ret);
3733

3734
 done:
3735
    remoteDriverUnlock(priv);
3736
    return rv;
3737 3738
}

3739 3740
/*----------------------------------------------------------------------*/

3741
static int
3742
remoteNodeDeviceDettach(virNodeDevicePtr dev)
3743 3744 3745 3746 3747 3748 3749 3750 3751
{
    int rv = -1;
    remote_node_device_dettach_args args;
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3752 3753 3754
    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)
3755 3756 3757 3758
        goto done;

    rv = 0;

3759
 done:
3760 3761 3762 3763
    remoteDriverUnlock(priv);
    return rv;
}

3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785
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;

3786
 done:
3787 3788 3789 3790
    remoteDriverUnlock(priv);
    return rv;
}

3791
static int
3792
remoteNodeDeviceReAttach(virNodeDevicePtr dev)
3793 3794 3795 3796 3797 3798 3799 3800 3801
{
    int rv = -1;
    remote_node_device_re_attach_args args;
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3802 3803 3804
    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)
3805 3806 3807 3808
        goto done;

    rv = 0;

3809
 done:
3810 3811 3812 3813 3814
    remoteDriverUnlock(priv);
    return rv;
}

static int
3815
remoteNodeDeviceReset(virNodeDevicePtr dev)
3816 3817 3818
{
    int rv = -1;
    remote_node_device_reset_args args;
3819
    /* This method is unusual in that it uses the HV driver, not the devMon driver
3820
     * hence its use of privateData, instead of nodeDevicePrivateData */
3821 3822 3823 3824 3825 3826
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3827 3828 3829
    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)
3830 3831 3832 3833
        goto done;

    rv = 0;

3834
 done:
3835 3836 3837 3838
    remoteDriverUnlock(priv);
    return rv;
}

3839

3840 3841
/*----------------------------------------------------------------------*/

3842
static int
3843
remoteAuthenticate(virConnectPtr conn, struct private_data *priv,
J
Ján Tomko 已提交
3844
                   virConnectAuthPtr auth G_GNUC_UNUSED,
3845
                   const char *authtype)
3846 3847
{
    struct remote_auth_list_ret ret;
3848
    int err, type = REMOTE_AUTH_NONE;
3849

3850
    memset(&ret, 0, sizeof(ret));
3851 3852 3853 3854
    err = call(conn, priv, 0,
               REMOTE_PROC_AUTH_LIST,
               (xdrproc_t) xdr_void, (char *) NULL,
               (xdrproc_t) xdr_remote_auth_list_ret, (char *) &ret);
3855
    if (err < 0) {
3856
        if (virGetLastErrorCode() == VIR_ERR_NO_SUPPORT) {
3857 3858 3859 3860
            /* Missing RPC - old server - ignore */
            virResetLastError();
            return 0;
        }
3861
        return -1;
3862
    }
3863 3864 3865 3866

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

3867
    if (authtype) {
3868 3869
        int want;
        size_t i;
3870 3871 3872 3873 3874 3875
        if (STRCASEEQ(authtype, "sasl") ||
            STRCASEEQLEN(authtype, "sasl.", 5)) {
            want = REMOTE_AUTH_SASL;
        } else if (STRCASEEQ(authtype, "polkit")) {
            want = REMOTE_AUTH_POLKIT;
        } else {
3876 3877
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("unknown authentication type %s"), authtype);
3878 3879
            return -1;
        }
3880
        for (i = 0; i < ret.types.types_len; i++) {
3881 3882 3883 3884
            if (ret.types.types_val[i] == want)
                type = want;
        }
        if (type == REMOTE_AUTH_NONE) {
3885 3886 3887
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("requested authentication type %s rejected"),
                           authtype);
3888 3889 3890 3891 3892 3893 3894
            return -1;
        }
    } else {
        type = ret.types.types_val[0];
    }

    switch (type) {
3895
#if WITH_SASL
3896 3897 3898 3899 3900 3901
    case REMOTE_AUTH_SASL: {
        const char *mech = NULL;
        if (authtype &&
            STRCASEEQLEN(authtype, "sasl.", 5))
            mech = authtype + 5;

3902
        if (remoteAuthSASL(conn, priv, auth, mech) < 0) {
3903
            VIR_FREE(ret.types.types_val);
3904 3905 3906
            return -1;
        }
        break;
3907
    }
3908 3909
#endif

3910
    case REMOTE_AUTH_POLKIT:
3911
        if (remoteAuthPolkit(conn, priv, auth) < 0) {
3912
            VIR_FREE(ret.types.types_val);
3913 3914 3915 3916
            return -1;
        }
        break;

3917 3918 3919 3920 3921
    case REMOTE_AUTH_NONE:
        /* Nothing todo, hurrah ! */
        break;

    default:
3922 3923 3924
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("unsupported authentication type %d"),
                       ret.types.types_val[0]);
3925
        VIR_FREE(ret.types.types_val);
3926 3927 3928
        return -1;
    }

3929
    VIR_FREE(ret.types.types_val);
3930 3931 3932 3933 3934 3935

    return 0;
}



3936
#if WITH_SASL
3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005
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.
4006
 * We explicitly leave the callback function pointer at NULL though,
4007 4008 4009 4010 4011 4012 4013 4014
 * 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)
{
4015
    sasl_callback_t *cbs;
4016 4017
    size_t i;
    int n;
4018
    if (VIR_ALLOC_N(cbs, ncredtype+1) < 0)
4019 4020
        return NULL;

4021
    for (i = 0, n = 0; i < ncredtype; i++) {
4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036
        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
4037
 *
4038 4039 4040 4041 4042
 * 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,
4043 4044
                                     virConnectCredentialPtr *cred,
                                     size_t *ncred)
4045 4046 4047 4048 4049
{
    int ninteract;
    if (!cred)
        return -1;

4050
    for (ninteract = 0, *ncred = 0; interact[ninteract].id != 0; ninteract++) {
4051 4052 4053 4054
        if (interact[ninteract].result)
            continue;
        (*ncred)++;
    }
4055

4056
    if (VIR_ALLOC_N(*cred, *ncred) < 0)
4057 4058
        return -1;

4059
    for (ninteract = 0, *ncred = 0; interact[ninteract].id != 0; ninteract++) {
4060 4061 4062 4063 4064 4065
        if (interact[ninteract].result)
            continue;

        (*cred)[*ncred].type = remoteAuthCredSASL2Vir(interact[ninteract].id);
        if (!(*cred)[*ncred].type) {
            *ncred = 0;
4066
            VIR_FREE(*cred);
4067 4068
            return -1;
        }
4069 4070 4071 4072 4073 4074 4075 4076
        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)++;
4077 4078
    }

4079
    return 0;
4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092
}


/*
 * @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)
{
4093
    int ninteract, ncred;
4094
    for (ninteract = 0, ncred = 0; interact[ninteract].id != 0; ninteract++) {
4095 4096 4097 4098 4099
        if (interact[ninteract].result)
            continue;
        interact[ninteract].result = cred[ncred].result;
        interact[ninteract].len = cred[ncred].resultlen;
        ncred++;
4100 4101 4102
    }
}

4103 4104 4105 4106
struct remoteAuthInteractState {
    sasl_interact_t *interact;
    virConnectCredentialPtr cred;
    size_t ncred;
4107
    virAuthConfigPtr config;
4108 4109 4110
};


4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133

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

4134
    for (ninteract = 0; state->interact[ninteract].id != 0; ninteract++) {
4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154
        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;
        }

4155 4156
        if (credname &&
            virAuthConfigLookup(state->config,
4157
                                "libvirt",
4158
                                VIR_URI_SERVER(conn->uri),
4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170
                                credname,
                                &value) < 0)
            goto cleanup;

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

    ret = 0;

4171
 cleanup:
4172 4173 4174 4175 4176 4177 4178
    VIR_FREE(path);
    return ret;
}


static void remoteAuthInteractStateClear(struct remoteAuthInteractState *state,
                                         bool final)
4179 4180 4181 4182 4183
{
    size_t i;
    if (!state)
        return;

4184
    for (i = 0; i < state->ncred; i++)
4185 4186 4187
        VIR_FREE(state->cred[i].result);
    VIR_FREE(state->cred);
    state->ncred = 0;
4188 4189 4190

    if (final)
        virAuthConfigFree(state->config);
4191 4192 4193
}


4194 4195
static int remoteAuthInteract(virConnectPtr conn,
                              struct remoteAuthInteractState *state,
4196 4197 4198 4199
                              virConnectAuthPtr auth)
{
    int ret = -1;

4200 4201 4202
    VIR_DEBUG("Starting SASL interaction");
    remoteAuthInteractStateClear(state, false);

4203
    /* Fills state->interact with any values from the auth config file */
4204 4205
    if (remoteAuthFillFromConfig(conn, state) < 0)
        goto cleanup;
4206

4207
    /* Populates state->cred for anything not found in the auth config */
4208
    if (remoteAuthMakeCredentials(state->interact, &state->cred, &state->ncred) < 0) {
4209 4210
        virReportError(VIR_ERR_AUTH_FAILED, "%s",
                       _("Failed to make auth credentials"));
4211 4212 4213
        goto cleanup;
    }

4214 4215 4216 4217 4218 4219 4220 4221 4222 4223
    /* 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;
        }
4224

4225 4226 4227 4228 4229 4230 4231 4232
        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);
4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244
    }

    /*
     * '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;

4245
 cleanup:
4246 4247 4248 4249
    return ret;
}


4250
/* Perform the SASL authentication process
4251 4252
 */
static int
4253 4254
remoteAuthSASL(virConnectPtr conn, struct private_data *priv,
               virConnectAuthPtr auth, const char *wantmech)
4255 4256 4257 4258 4259 4260 4261
{
    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;
4262
    char *serverin = NULL;
4263
    size_t clientoutlen, serverinlen;
4264 4265
    const char *mech;
    int err, complete;
4266
    int ssf;
4267 4268 4269
    sasl_callback_t *saslcb = NULL;
    int ret = -1;
    const char *mechlist;
4270
    virNetSASLContextPtr saslCtxt;
E
Eric Blake 已提交
4271
    virNetSASLSessionPtr sasl = NULL;
4272 4273 4274
    struct remoteAuthInteractState state;

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

4276
    VIR_DEBUG("Client initialize SASL authentication");
4277

4278
    if (!(saslCtxt = virNetSASLContextNewClient()))
4279 4280
        goto cleanup;

4281 4282 4283 4284 4285 4286
    if (auth) {
        if ((saslcb = remoteAuthMakeCallbacks(auth->credtype, auth->ncredtype)) == NULL)
            goto cleanup;
    } else {
        saslcb = NULL;
    }
4287 4288

    /* Setup a handle for being a client */
4289 4290 4291
    if (!(sasl = virNetSASLSessionNewClient(saslCtxt,
                                            "libvirt",
                                            priv->hostname,
J
Ján Tomko 已提交
4292 4293
                                            virNetClientLocalAddrStringSASL(priv->client),
                                            virNetClientRemoteAddrStringSASL(priv->client),
4294
                                            saslcb)))
4295
        goto cleanup;
4296 4297
    /* saslcb is now owned by sasl */
    saslcb = NULL;
4298

4299
# ifdef WITH_GNUTLS
4300
    /* Initialize some connection props we care about */
4301 4302
    if (priv->tls) {
        if ((ssf = virNetClientGetTLSKeySize(priv->client)) < 0)
4303
            goto cleanup;
4304

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

4307
        VIR_DEBUG("Setting external SSF %d", ssf);
4308
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
4309
            goto cleanup;
4310
    }
4311
# endif
4312

4313 4314
    /* 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 */
4315 4316 4317 4318
    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)
4319
        goto cleanup;
4320

4321
    /* First call is to inquire about supported mechanisms in the server */
4322 4323 4324 4325
    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)
4326
        goto cleanup;
4327 4328


4329 4330 4331
    mechlist = iret.mechlist;
    if (wantmech) {
        if (strstr(mechlist, wantmech) == NULL) {
4332 4333 4334
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("SASL mechanism %s not supported by server"),
                           wantmech);
4335
            VIR_FREE(iret.mechlist);
4336 4337 4338 4339 4340
            goto cleanup;
        }
        mechlist = wantmech;
    }
 restart:
4341
    /* Start the auth negotiation on the client end first */
4342
    VIR_DEBUG("Client start negotiation mechlist '%s'", mechlist);
4343 4344
    if ((err = virNetSASLSessionClientStart(sasl,
                                            mechlist,
4345
                                            &state.interact,
4346 4347 4348
                                            &clientout,
                                            &clientoutlen,
                                            &mech)) < 0)
4349 4350 4351
        goto cleanup;

    /* Need to gather some credentials from the client */
4352
    if (err == VIR_NET_SASL_INTERACT) {
4353
        if (remoteAuthInteract(conn, &state, auth) < 0) {
4354
            VIR_FREE(iret.mechlist);
4355 4356
            goto cleanup;
        }
4357
        goto restart;
4358
    }
4359
    VIR_FREE(iret.mechlist);
4360 4361

    if (clientoutlen > REMOTE_AUTH_SASL_DATA_MAX) {
4362 4363 4364
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("SASL negotiation data too long: %zu bytes"),
                       clientoutlen);
4365
        goto cleanup;
4366 4367
    }
    /* NB, distinction of NULL vs "" is *critical* in SASL */
4368
    memset(&sargs, 0, sizeof(sargs));
4369 4370 4371 4372
    sargs.nil = clientout ? 0 : 1;
    sargs.data.data_val = (char*)clientout;
    sargs.data.data_len = clientoutlen;
    sargs.mech = (char*)mech;
4373 4374
    VIR_DEBUG("Server start negotiation with mech %s. Data %zu bytes %p",
              mech, clientoutlen, clientout);
4375 4376

    /* Now send the initial auth data to the server */
4377 4378 4379 4380
    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)
4381
        goto cleanup;
4382 4383 4384 4385 4386

    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;
4387 4388
    VIR_DEBUG("Client step result complete: %d. Data %zu bytes %p",
              complete, serverinlen, serverin);
4389

4390 4391 4392 4393 4394
    /* Previous server call showed completion & sasl_client_start() told us
     * we are locally complete too */
    if (complete && err == VIR_NET_SASL_COMPLETE)
        goto done;

4395 4396
    /* Loop-the-loop...
     * Even if the server has completed, the client must *always* do at least one step
D
Daniel Veillard 已提交
4397
     * in this loop to verify the server isn't lying about something. Mutual auth */
4398
    for (;;) {
4399 4400 4401
        if ((err = virNetSASLSessionClientStep(sasl,
                                               serverin,
                                               serverinlen,
4402
                                               &state.interact,
4403 4404
                                               &clientout,
                                               &clientoutlen)) < 0)
4405
            goto cleanup;
4406

4407
        /* Need to gather some credentials from the client */
4408
        if (err == VIR_NET_SASL_INTERACT) {
4409
            if (remoteAuthInteract(conn, &state, auth) < 0) {
4410
                VIR_FREE(iret.mechlist);
4411 4412
                goto cleanup;
            }
4413
            continue;
4414 4415
        }

4416
        VIR_FREE(serverin);
4417 4418
        VIR_DEBUG("Client step result %d. Data %zu bytes %p",
                  err, clientoutlen, clientout);
4419 4420

        /* Previous server call showed completion & we're now locally complete too */
4421
        if (complete && err == VIR_NET_SASL_COMPLETE)
4422 4423 4424 4425
            break;

        /* Not done, prepare to talk with the server for another iteration */
        /* NB, distinction of NULL vs "" is *critical* in SASL */
4426
        memset(&pargs, 0, sizeof(pargs));
4427 4428 4429
        pargs.nil = clientout ? 0 : 1;
        pargs.data.data_val = (char*)clientout;
        pargs.data.data_len = clientoutlen;
4430 4431
        VIR_DEBUG("Server step with %zu bytes %p",
                  clientoutlen, clientout);
4432

4433 4434 4435 4436
        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)
4437
            goto cleanup;
4438 4439 4440 4441 4442 4443

        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;

4444 4445
        VIR_DEBUG("Client step result complete: %d. Data %zu bytes %p",
                  complete, serverinlen, serverin);
4446 4447

        /* This server call shows complete, and earlier client step was OK */
4448
        if (complete && err == VIR_NET_SASL_COMPLETE) {
4449
            VIR_FREE(serverin);
4450 4451 4452 4453
            break;
        }
    }

4454 4455
    /* Check for suitable SSF if not already secure (TLS or UNIX sock) */
    if (!priv->is_secure) {
4456
        if ((ssf = virNetSASLSessionGetKeySize(sasl)) < 0)
4457
            goto cleanup;
4458

4459
        VIR_DEBUG("SASL SSF value %d", ssf);
4460
        if (ssf < 56) { /* 56 == DES level, good for Kerberos */
4461 4462
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("negotiation SSF %d was not strong enough"), ssf);
4463
            goto cleanup;
4464
        }
4465
        priv->is_secure = 1;
4466 4467
    }

4468
 done:
4469
    VIR_DEBUG("SASL authentication complete");
4470
    virNetClientSetSASLSession(priv->client, sasl);
4471 4472 4473
    ret = 0;

 cleanup:
4474
    VIR_FREE(serverin);
4475

4476
    remoteAuthInteractStateClear(&state, true);
4477
    VIR_FREE(saslcb);
4478 4479
    virObjectUnref(sasl);
    virObjectUnref(saslCtxt);
4480

4481
    return ret;
4482
}
4483
#endif /* WITH_SASL */
4484

4485

4486 4487
static int
remoteAuthPolkit(virConnectPtr conn, struct private_data *priv,
J
Ján Tomko 已提交
4488
                 virConnectAuthPtr auth G_GNUC_UNUSED)
4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503
{
    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;
}

4504 4505
/*----------------------------------------------------------------------*/

4506 4507 4508 4509 4510
static int
remoteConnectDomainEventRegister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback,
                                 void *opaque,
                                 virFreeCallback freecb)
4511
{
4512
    int callbackID;
4513
    int rv = -1;
4514
    struct private_data *priv = conn->privateData;
4515
    int count;
4516

4517 4518
    remoteDriverLock(priv);

4519 4520 4521 4522 4523
    if ((count = virDomainEventStateRegisterClient(conn, priv->eventState,
                                                   NULL,
                                                   VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                   VIR_DOMAIN_EVENT_CALLBACK(callback),
                                                   opaque, freecb, true,
4524 4525
                                                   &callbackID,
                                                   priv->serverEventFilter)) < 0)
4526
         goto done;
4527

4528
    if (count == 1) {
4529
        /* Tell the server when we are the first callback registering */
4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542
        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,
4543
                                                callbackID, false);
4544 4545 4546 4547 4548 4549 4550 4551 4552
                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,
4553
                                                callbackID, false);
4554 4555
                goto done;
            }
4556
        }
4557 4558
    }

4559 4560
    rv = 0;

4561
 done:
4562
    remoteDriverUnlock(priv);
4563
    return rv;
4564 4565
}

4566

4567 4568 4569
static int
remoteConnectDomainEventDeregister(virConnectPtr conn,
                                   virConnectDomainEventCallback callback)
4570 4571
{
    struct private_data *priv = conn->privateData;
4572
    int rv = -1;
4573 4574 4575
    remote_connect_domain_event_callback_deregister_any_args args;
    int callbackID;
    int remoteID;
4576
    int count;
4577

4578 4579
    remoteDriverLock(priv);

4580 4581 4582 4583 4584 4585
    if ((callbackID = virDomainEventStateCallbackID(conn, priv->eventState,
                                                    callback,
                                                    &remoteID)) < 0)
        goto done;

    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
4586
                                                 callbackID, true)) < 0)
4587
        goto done;
4588

4589
    if (count == 0) {
4590
        /* Tell the server when we are the last callback deregistering */
4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604
        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;
        }
4605 4606
    }

4607 4608
    rv = 0;

4609
 done:
4610
    remoteDriverUnlock(priv);
4611
    return rv;
4612
}
4613

4614 4615

static void
4616 4617 4618
remoteDomainBuildEventLifecycleHelper(virConnectPtr conn,
                                      remote_domain_event_lifecycle_msg *msg,
                                      int callbackID)
4619
{
4620
    struct private_data *priv = conn->privateData;
4621
    virDomainPtr dom;
4622
    virObjectEventPtr event = NULL;
4623

4624
    dom = get_nonnull_domain(conn, msg->dom);
4625
    if (!dom)
4626
        return;
4627

4628
    event = virDomainEventLifecycleNewFromDom(dom, msg->event, msg->detail);
4629
    virObjectUnref(dom);
4630

4631
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4632 4633
}
static void
J
Ján Tomko 已提交
4634 4635
remoteDomainBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                virNetClientPtr client G_GNUC_UNUSED,
4636 4637 4638 4639 4640 4641 4642
                                void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_lifecycle_msg *msg = evdata;
    remoteDomainBuildEventLifecycleHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
4643 4644
remoteDomainBuildEventCallbackLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                        virNetClientPtr client G_GNUC_UNUSED,
4645 4646 4647 4648 4649
                                        void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_lifecycle_msg *msg = evdata;
    remoteDomainBuildEventLifecycleHelper(conn, &msg->msg, msg->callbackID);
4650 4651 4652
}


4653
static void
4654 4655 4656
remoteDomainBuildEventRebootHelper(virConnectPtr conn,
                                   remote_domain_event_reboot_msg *msg,
                                   int callbackID)
4657
{
4658
    struct private_data *priv = conn->privateData;
4659
    virDomainPtr dom;
4660
    virObjectEventPtr event = NULL;
4661

4662
    dom = get_nonnull_domain(conn, msg->dom);
4663
    if (!dom)
4664
        return;
4665 4666

    event = virDomainEventRebootNewFromDom(dom);
4667
    virObjectUnref(dom);
4668

4669
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4670
}
4671
static void
J
Ján Tomko 已提交
4672 4673
remoteDomainBuildEventReboot(virNetClientProgramPtr prog G_GNUC_UNUSED,
                             virNetClientPtr client G_GNUC_UNUSED,
4674 4675 4676 4677 4678 4679 4680
                             void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_reboot_msg *msg = evdata;
    remoteDomainBuildEventRebootHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
4681 4682
remoteDomainBuildEventCallbackReboot(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                     virNetClientPtr client G_GNUC_UNUSED,
4683
                                     void *evdata, void *opaque)
4684
{
4685
    virConnectPtr conn = opaque;
4686 4687 4688 4689 4690 4691 4692 4693 4694
    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)
{
4695
    struct private_data *priv = conn->privateData;
4696
    virDomainPtr dom;
4697
    virObjectEventPtr event = NULL;
4698

4699
    dom = get_nonnull_domain(conn, msg->dom);
4700
    if (!dom)
4701
        return;
4702

4703
    event = virDomainEventRTCChangeNewFromDom(dom, msg->offset);
4704
    virObjectUnref(dom);
4705

4706
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4707
}
4708
static void
J
Ján Tomko 已提交
4709 4710
remoteDomainBuildEventRTCChange(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                virNetClientPtr client G_GNUC_UNUSED,
4711
                                void *evdata, void *opaque)
4712
{
4713
    virConnectPtr conn = opaque;
4714 4715 4716 4717
    remote_domain_event_rtc_change_msg *msg = evdata;
    remoteDomainBuildEventRTCChangeHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
4718 4719
remoteDomainBuildEventCallbackRTCChange(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                        virNetClientPtr client G_GNUC_UNUSED,
4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731
                                        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)
{
4732
    struct private_data *priv = conn->privateData;
4733
    virDomainPtr dom;
4734
    virObjectEventPtr event = NULL;
4735

4736
    dom = get_nonnull_domain(conn, msg->dom);
4737
    if (!dom)
4738
        return;
4739

4740
    event = virDomainEventWatchdogNewFromDom(dom, msg->action);
4741
    virObjectUnref(dom);
4742

4743
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4744
}
4745
static void
J
Ján Tomko 已提交
4746 4747
remoteDomainBuildEventWatchdog(virNetClientProgramPtr prog G_GNUC_UNUSED,
                               virNetClientPtr client G_GNUC_UNUSED,
4748 4749 4750 4751 4752 4753 4754
                               void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_watchdog_msg *msg = evdata;
    remoteDomainBuildEventWatchdogHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
4755 4756
remoteDomainBuildEventCallbackWatchdog(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                       virNetClientPtr client G_GNUC_UNUSED,
4757
                                       void *evdata, void *opaque)
4758
{
4759
    virConnectPtr conn = opaque;
4760 4761 4762 4763 4764 4765 4766 4767 4768
    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)
{
4769
    struct private_data *priv = conn->privateData;
4770
    virDomainPtr dom;
4771
    virObjectEventPtr event = NULL;
4772

4773
    dom = get_nonnull_domain(conn, msg->dom);
4774
    if (!dom)
4775
        return;
4776 4777

    event = virDomainEventIOErrorNewFromDom(dom,
4778 4779 4780
                                            msg->srcPath,
                                            msg->devAlias,
                                            msg->action);
4781
    virObjectUnref(dom);
4782

4783
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4784
}
4785
static void
J
Ján Tomko 已提交
4786 4787
remoteDomainBuildEventIOError(virNetClientProgramPtr prog G_GNUC_UNUSED,
                              virNetClientPtr client G_GNUC_UNUSED,
4788 4789 4790 4791 4792 4793 4794
                              void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_io_error_msg *msg = evdata;
    remoteDomainBuildEventIOErrorHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
4795 4796
remoteDomainBuildEventCallbackIOError(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                      virNetClientPtr client G_GNUC_UNUSED,
4797
                                      void *evdata, void *opaque)
4798
{
4799
    virConnectPtr conn = opaque;
4800 4801 4802 4803 4804 4805 4806 4807 4808
    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)
{
4809
    struct private_data *priv = conn->privateData;
4810
    virDomainPtr dom;
4811
    virObjectEventPtr event = NULL;
4812

4813
    dom = get_nonnull_domain(conn, msg->dom);
4814
    if (!dom)
4815
        return;
4816 4817

    event = virDomainEventIOErrorReasonNewFromDom(dom,
4818 4819 4820 4821
                                                  msg->srcPath,
                                                  msg->devAlias,
                                                  msg->action,
                                                  msg->reason);
4822

4823
    virObjectUnref(dom);
4824

4825
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4826
}
4827
static void
J
Ján Tomko 已提交
4828 4829
remoteDomainBuildEventIOErrorReason(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                    virNetClientPtr client G_GNUC_UNUSED,
4830
                                    void *evdata, void *opaque)
4831 4832
{
    virConnectPtr conn = opaque;
4833 4834 4835 4836
    remote_domain_event_io_error_reason_msg *msg = evdata;
    remoteDomainBuildEventIOErrorReasonHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
4837 4838
remoteDomainBuildEventCallbackIOErrorReason(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                            virNetClientPtr client G_GNUC_UNUSED,
4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850
                                            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)
{
4851 4852
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
4853
    virObjectEventPtr event = NULL;
4854 4855 4856 4857 4858 4859 4860 4861

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

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

4862
    virObjectUnref(dom);
4863

4864
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4865
}
4866
static void
J
Ján Tomko 已提交
4867 4868
remoteDomainBuildEventBlockJob(virNetClientProgramPtr prog G_GNUC_UNUSED,
                               virNetClientPtr client G_GNUC_UNUSED,
4869
                               void *evdata, void *opaque)
4870
{
4871
    virConnectPtr conn = opaque;
4872 4873 4874 4875
    remote_domain_event_block_job_msg *msg = evdata;
    remoteDomainBuildEventBlockJobHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
4876 4877
remoteDomainBuildEventCallbackBlockJob(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                       virNetClientPtr client G_GNUC_UNUSED,
4878 4879 4880 4881 4882 4883
                                       void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_block_job_msg *msg = evdata;
    remoteDomainBuildEventBlockJobHelper(conn, &msg->msg, msg->callbackID);
}
4884
static void
J
Ján Tomko 已提交
4885 4886
remoteDomainBuildEventBlockJob2(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                virNetClientPtr client G_GNUC_UNUSED,
4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901
                                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);

4902
    virObjectUnref(dom);
4903

4904
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
4905
}
4906 4907 4908 4909 4910 4911

static void
remoteDomainBuildEventGraphicsHelper(virConnectPtr conn,
                                     remote_domain_event_graphics_msg *msg,
                                     int callbackID)
{
4912
    struct private_data *priv = conn->privateData;
4913
    virDomainPtr dom;
4914
    virObjectEventPtr event = NULL;
4915 4916 4917
    virDomainEventGraphicsAddressPtr localAddr = NULL;
    virDomainEventGraphicsAddressPtr remoteAddr = NULL;
    virDomainEventGraphicsSubjectPtr subject = NULL;
4918
    size_t i;
4919

4920
    dom = get_nonnull_domain(conn, msg->dom);
4921
    if (!dom)
4922
        return;
4923 4924

    if (VIR_ALLOC(localAddr) < 0)
4925
        goto error;
4926
    localAddr->family = msg->local.family;
4927 4928 4929
    if (VIR_STRDUP(localAddr->service, msg->local.service) < 0 ||
        VIR_STRDUP(localAddr->node, msg->local.node) < 0)
        goto error;
4930 4931

    if (VIR_ALLOC(remoteAddr) < 0)
4932
        goto error;
4933
    remoteAddr->family = msg->remote.family;
4934 4935 4936
    if (VIR_STRDUP(remoteAddr->service, msg->remote.service) < 0 ||
        VIR_STRDUP(remoteAddr->node, msg->remote.node) < 0)
        goto error;
4937 4938

    if (VIR_ALLOC(subject) < 0)
4939
        goto error;
4940
    if (VIR_ALLOC_N(subject->identities, msg->subject.subject_len) < 0)
4941
        goto error;
4942
    subject->nidentity = msg->subject.subject_len;
4943
    for (i = 0; i < subject->nidentity; i++) {
4944 4945 4946
        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;
4947 4948 4949
    }

    event = virDomainEventGraphicsNewFromDom(dom,
4950
                                             msg->phase,
4951 4952
                                             localAddr,
                                             remoteAddr,
4953
                                             msg->authScheme,
4954 4955
                                             subject);

4956
    virObjectUnref(dom);
4957

4958
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
4959
    return;
4960

4961
 error:
4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972
    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) {
4973
        for (i = 0; i < subject->nidentity; i++) {
4974 4975 4976 4977 4978 4979
            VIR_FREE(subject->identities[i].type);
            VIR_FREE(subject->identities[i].name);
        }
        VIR_FREE(subject->identities);
        VIR_FREE(subject);
    }
4980
    virObjectUnref(dom);
4981
    return;
4982
}
4983
static void
J
Ján Tomko 已提交
4984 4985
remoteDomainBuildEventGraphics(virNetClientProgramPtr prog G_GNUC_UNUSED,
                               virNetClientPtr client G_GNUC_UNUSED,
4986
                               void *evdata, void *opaque)
4987
{
4988
    virConnectPtr conn = opaque;
4989 4990 4991 4992
    remote_domain_event_graphics_msg *msg = evdata;
    remoteDomainBuildEventGraphicsHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
4993 4994
remoteDomainBuildEventCallbackGraphics(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                       virNetClientPtr client G_GNUC_UNUSED,
4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006
                                       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)
{
5007
    struct private_data *priv = conn->privateData;
5008
    virDomainPtr dom;
5009
    virObjectEventPtr event = NULL;
5010

5011
    dom = get_nonnull_domain(conn, msg->dom);
5012
    if (!dom)
5013
        return;
5014 5015 5016

    event = virDomainEventControlErrorNewFromDom(dom);

5017
    virObjectUnref(dom);
5018

5019
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
5020 5021
}
static void
J
Ján Tomko 已提交
5022 5023
remoteDomainBuildEventControlError(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                   virNetClientPtr client G_GNUC_UNUSED,
5024 5025 5026 5027 5028 5029 5030
                                   void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_control_error_msg *msg = evdata;
    remoteDomainBuildEventControlErrorHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
5031 5032
remoteDomainBuildEventCallbackControlError(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                           virNetClientPtr client G_GNUC_UNUSED,
5033 5034 5035 5036 5037
                                           void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_control_error_msg *msg = evdata;
    remoteDomainBuildEventControlErrorHelper(conn, &msg->msg, msg->callbackID);
5038 5039 5040
}


5041
static void
5042 5043 5044
remoteDomainBuildEventDiskChangeHelper(virConnectPtr conn,
                                       remote_domain_event_disk_change_msg *msg,
                                       int callbackID)
5045 5046 5047
{
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5048
    virObjectEventPtr event = NULL;
5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059

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

5060
    virObjectUnref(dom);
5061

5062
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
5063
}
5064
static void
J
Ján Tomko 已提交
5065 5066
remoteDomainBuildEventDiskChange(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                 virNetClientPtr client G_GNUC_UNUSED,
5067 5068 5069
                                 void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
5070 5071 5072 5073
    remote_domain_event_disk_change_msg *msg = evdata;
    remoteDomainBuildEventDiskChangeHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
5074 5075
remoteDomainBuildEventCallbackDiskChange(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                         virNetClientPtr client G_GNUC_UNUSED,
5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088
                                         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)
{
5089 5090
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5091
    virObjectEventPtr event = NULL;
5092 5093 5094 5095 5096 5097 5098 5099 5100

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

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

5101
    virObjectUnref(dom);
5102

5103
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
5104
}
O
Osier Yang 已提交
5105
static void
J
Ján Tomko 已提交
5106 5107
remoteDomainBuildEventTrayChange(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                 virNetClientPtr client G_GNUC_UNUSED,
5108 5109 5110 5111 5112 5113 5114
                                 void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_tray_change_msg *msg = evdata;
    remoteDomainBuildEventTrayChangeHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
5115 5116
remoteDomainBuildEventCallbackTrayChange(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                         virNetClientPtr client G_GNUC_UNUSED,
5117
                                         void *evdata, void *opaque)
O
Osier Yang 已提交
5118 5119
{
    virConnectPtr conn = opaque;
5120 5121 5122 5123 5124 5125 5126
    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 已提交
5127 5128
                                     int callbackID,
                                     int reason)
5129
{
O
Osier Yang 已提交
5130 5131
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5132
    virObjectEventPtr event = NULL;
O
Osier Yang 已提交
5133 5134 5135 5136 5137

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

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

5140
    virObjectUnref(dom);
O
Osier Yang 已提交
5141

5142
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
O
Osier Yang 已提交
5143
}
O
Osier Yang 已提交
5144
static void
J
Ján Tomko 已提交
5145 5146
remoteDomainBuildEventPMWakeup(virNetClientProgramPtr prog G_GNUC_UNUSED,
                               virNetClientPtr client G_GNUC_UNUSED,
5147 5148 5149 5150
                               void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_pmwakeup_msg *msg = evdata;
E
Eric Blake 已提交
5151
    remoteDomainBuildEventPMWakeupHelper(conn, msg, -1, 0);
5152 5153
}
static void
J
Ján Tomko 已提交
5154 5155
remoteDomainBuildEventCallbackPMWakeup(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                       virNetClientPtr client G_GNUC_UNUSED,
5156
                                       void *evdata, void *opaque)
O
Osier Yang 已提交
5157 5158
{
    virConnectPtr conn = opaque;
5159
    remote_domain_event_callback_pmwakeup_msg *msg = evdata;
E
Eric Blake 已提交
5160 5161
    remoteDomainBuildEventPMWakeupHelper(conn, &msg->msg, msg->callbackID,
                                         msg->reason);
5162 5163 5164 5165 5166
}

static void
remoteDomainBuildEventPMSuspendHelper(virConnectPtr conn,
                                      remote_domain_event_pmsuspend_msg *msg,
E
Eric Blake 已提交
5167 5168
                                      int callbackID,
                                      int reason)
5169
{
O
Osier Yang 已提交
5170 5171
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5172
    virObjectEventPtr event = NULL;
O
Osier Yang 已提交
5173 5174 5175 5176 5177

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

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

5180
    virObjectUnref(dom);
O
Osier Yang 已提交
5181

5182
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
5183 5184
}
static void
J
Ján Tomko 已提交
5185 5186
remoteDomainBuildEventPMSuspend(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                virNetClientPtr client G_GNUC_UNUSED,
5187 5188 5189 5190
                                void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_pmsuspend_msg *msg = evdata;
E
Eric Blake 已提交
5191
    remoteDomainBuildEventPMSuspendHelper(conn, msg, -1, 0);
5192 5193
}
static void
J
Ján Tomko 已提交
5194 5195
remoteDomainBuildEventCallbackPMSuspend(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                        virNetClientPtr client G_GNUC_UNUSED,
5196 5197 5198 5199
                                        void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_pmsuspend_msg *msg = evdata;
E
Eric Blake 已提交
5200 5201
    remoteDomainBuildEventPMSuspendHelper(conn, &msg->msg, msg->callbackID,
                                          msg->reason);
O
Osier Yang 已提交
5202 5203
}

5204 5205

static void
5206 5207 5208
remoteDomainBuildEventBalloonChangeHelper(virConnectPtr conn,
                                          remote_domain_event_balloon_change_msg *msg,
                                          int callbackID)
5209 5210 5211
{
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5212
    virObjectEventPtr event = NULL;
5213 5214 5215 5216 5217 5218

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

    event = virDomainEventBalloonChangeNewFromDom(dom, msg->actual);
5219
    virObjectUnref(dom);
5220

5221
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
5222
}
5223
static void
J
Ján Tomko 已提交
5224 5225
remoteDomainBuildEventBalloonChange(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                    virNetClientPtr client G_GNUC_UNUSED,
5226 5227 5228
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
5229 5230 5231 5232
    remote_domain_event_balloon_change_msg *msg = evdata;
    remoteDomainBuildEventBalloonChangeHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
5233 5234
remoteDomainBuildEventCallbackBalloonChange(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                            virNetClientPtr client G_GNUC_UNUSED,
5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245
                                            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 已提交
5246 5247
                                          int callbackID,
                                          int reason)
5248
{
5249 5250
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5251
    virObjectEventPtr event = NULL;
5252 5253 5254 5255 5256

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

E
Eric Blake 已提交
5257
    event = virDomainEventPMSuspendDiskNewFromDom(dom, reason);
5258

5259
    virObjectUnref(dom);
5260

5261
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
5262
}
5263
static void
J
Ján Tomko 已提交
5264 5265
remoteDomainBuildEventPMSuspendDisk(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                    virNetClientPtr client G_GNUC_UNUSED,
5266 5267 5268
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
5269
    remote_domain_event_pmsuspend_disk_msg *msg = evdata;
E
Eric Blake 已提交
5270
    remoteDomainBuildEventPMSuspendDiskHelper(conn, msg, -1, 0);
5271 5272
}
static void
J
Ján Tomko 已提交
5273 5274
remoteDomainBuildEventCallbackPMSuspendDisk(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                            virNetClientPtr client G_GNUC_UNUSED,
5275 5276 5277 5278
                                            void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_pmsuspend_disk_msg *msg = evdata;
E
Eric Blake 已提交
5279 5280
    remoteDomainBuildEventPMSuspendDiskHelper(conn, &msg->msg, msg->callbackID,
                                              msg->reason);
5281 5282 5283 5284 5285 5286 5287 5288
}


static void
remoteDomainBuildEventDeviceRemovedHelper(virConnectPtr conn,
                                          remote_domain_event_device_removed_msg *msg,
                                          int callbackID)
{
5289 5290
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5291
    virObjectEventPtr event = NULL;
5292 5293 5294 5295 5296 5297 5298

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

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

5299
    virObjectUnref(dom);
5300

5301
    virObjectEventStateQueueRemote(priv->eventState, event, callbackID);
5302 5303
}
static void
J
Ján Tomko 已提交
5304 5305
remoteDomainBuildEventDeviceRemoved(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                    virNetClientPtr client G_GNUC_UNUSED,
5306 5307 5308 5309 5310 5311 5312
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_device_removed_msg *msg = evdata;
    remoteDomainBuildEventDeviceRemovedHelper(conn, msg, -1);
}
static void
J
Ján Tomko 已提交
5313 5314
remoteDomainBuildEventCallbackDeviceRemoved(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                            virNetClientPtr client G_GNUC_UNUSED,
5315 5316 5317 5318 5319
                                            void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_device_removed_msg *msg = evdata;
    remoteDomainBuildEventDeviceRemovedHelper(conn, &msg->msg, msg->callbackID);
5320 5321
}

5322
static void
J
Ján Tomko 已提交
5323 5324
remoteDomainBuildEventCallbackDeviceAdded(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                          virNetClientPtr client G_GNUC_UNUSED,
5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340
                                          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);

5341
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5342
}
5343

5344 5345

static void
J
Ján Tomko 已提交
5346 5347
remoteDomainBuildEventCallbackDeviceRemovalFailed(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                                  virNetClientPtr client G_GNUC_UNUSED,
5348 5349 5350
                                                  void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
5351
    remote_domain_event_callback_device_removal_failed_msg *msg = evdata;
5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362
    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);

5363
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5364 5365
}

5366
static void
J
Ján Tomko 已提交
5367 5368
remoteDomainBuildEventCallbackTunable(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                      virNetClientPtr client G_GNUC_UNUSED,
5369 5370 5371 5372 5373 5374 5375 5376 5377 5378
                                      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;

5379 5380 5381 5382
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) msg->params.params_val,
                                  msg->params.params_len,
                                  REMOTE_DOMAIN_EVENT_TUNABLE_MAX,
                                  &params, &nparams) < 0)
5383 5384 5385 5386 5387 5388 5389 5390 5391 5392
        return;

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

    event = virDomainEventTunableNewFromDom(dom, params, nparams);

5393
    virObjectUnref(dom);
5394

5395
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5396 5397 5398
}


5399
static void
J
Ján Tomko 已提交
5400 5401
remoteDomainBuildEventCallbackAgentLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                             virNetClientPtr client G_GNUC_UNUSED,
5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415
                                             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);

5416
    virObjectUnref(dom);
5417

5418
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5419 5420
}

5421 5422

static void
J
Ján Tomko 已提交
5423 5424
remoteDomainBuildEventCallbackMigrationIteration(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                                 virNetClientPtr client G_GNUC_UNUSED,
5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440
                                                 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);

5441
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5442 5443 5444
}


J
Jiri Denemark 已提交
5445
static void
J
Ján Tomko 已提交
5446 5447
remoteDomainBuildEventCallbackJobCompleted(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                           virNetClientPtr client G_GNUC_UNUSED,
J
Jiri Denemark 已提交
5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473
                                           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);

5474
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
J
Jiri Denemark 已提交
5475 5476 5477
}


5478
static void
J
Ján Tomko 已提交
5479 5480
remoteDomainBuildEventCallbackMetadataChange(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                             virNetClientPtr client G_GNUC_UNUSED,
5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495
                                             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);

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


5500
static void
J
Ján Tomko 已提交
5501 5502
remoteNetworkBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                 virNetClientPtr client G_GNUC_UNUSED,
5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514
                                 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;

5515 5516
    event = virNetworkEventLifecycleNew(net->name, net->uuid, msg->event,
                                        msg->detail);
5517
    virObjectUnref(net);
5518

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

5522
static void
J
Ján Tomko 已提交
5523 5524
remoteStoragePoolBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                     virNetClientPtr client G_GNUC_UNUSED,
5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540
                                     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);

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

5544
static void
J
Ján Tomko 已提交
5545 5546
remoteStoragePoolBuildEventRefresh(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                   virNetClientPtr client G_GNUC_UNUSED,
5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561
                                   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);

5562
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5563 5564
}

5565
static void
J
Ján Tomko 已提交
5566 5567
remoteNodeDeviceBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                    virNetClientPtr client G_GNUC_UNUSED,
5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583
                                    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);

5584
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5585 5586
}

5587
static void
J
Ján Tomko 已提交
5588 5589
remoteNodeDeviceBuildEventUpdate(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                 virNetClientPtr client G_GNUC_UNUSED,
5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604
                                 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);

5605
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5606 5607
}

5608
static void
J
Ján Tomko 已提交
5609 5610
remoteSecretBuildEventLifecycle(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                virNetClientPtr client G_GNUC_UNUSED,
5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626
                                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);

5627
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5628 5629
}

5630
static void
J
Ján Tomko 已提交
5631 5632
remoteSecretBuildEventValueChanged(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                   virNetClientPtr client G_GNUC_UNUSED,
5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647
                                   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);

5648
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5649 5650
}

5651
static void
J
Ján Tomko 已提交
5652 5653
remoteDomainBuildQemuMonitorEvent(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                  virNetClientPtr client G_GNUC_UNUSED,
5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669
                                  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);
5670
    virObjectUnref(dom);
5671

5672
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5673 5674 5675
}


5676
static unsigned char *
5677 5678
remoteSecretGetValue(virSecretPtr secret, size_t *value_size,
                     unsigned int flags, unsigned int internalFlags)
5679 5680 5681 5682
{
    unsigned char *rv = NULL;
    remote_secret_get_value_args args;
    remote_secret_get_value_ret ret;
5683
    struct private_data *priv = secret->conn->privateData;
5684

5685
    remoteDriverLock(priv);
5686

5687 5688
    /* internalFlags intentionally do not go over the wire */
    if (internalFlags) {
5689
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("no internalFlags support"));
5690 5691 5692
        goto done;
    }

5693
    make_nonnull_secret(&args.secret, secret);
5694 5695
    args.flags = flags;

5696 5697 5698 5699
    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)
5700 5701 5702 5703 5704
        goto done;

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

5705
 done:
5706
    remoteDriverUnlock(priv);
5707 5708 5709
    return rv;
}

5710

5711
static void
J
Ján Tomko 已提交
5712 5713
remoteDomainBuildEventBlockThreshold(virNetClientProgramPtr prog G_GNUC_UNUSED,
                                     virNetClientPtr client G_GNUC_UNUSED,
5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730
                                     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);

5731
    virObjectEventStateQueueRemote(priv->eventState, event, msg->callbackID);
5732 5733 5734
}


5735 5736 5737 5738 5739
static int
remoteStreamSend(virStreamPtr st,
                 const char *data,
                 size_t nbytes)
{
5740
    VIR_DEBUG("st=%p data=%p nbytes=%zu", st, data, nbytes);
5741
    struct private_data *priv = st->conn->privateData;
5742
    virNetClientStreamPtr privst = st->privateData;
5743
    int rv;
5744

5745 5746 5747
    remoteDriverLock(priv);
    priv->localUses++;
    remoteDriverUnlock(priv);
5748

5749 5750 5751 5752 5753
    rv = virNetClientStreamSendPacket(privst,
                                      priv->client,
                                      VIR_NET_CONTINUE,
                                      data,
                                      nbytes);
5754

5755 5756
    remoteDriverLock(priv);
    priv->localUses--;
5757 5758 5759 5760 5761 5762
    remoteDriverUnlock(priv);
    return rv;
}


static int
5763 5764 5765 5766
remoteStreamRecvFlags(virStreamPtr st,
                      char *data,
                      size_t nbytes,
                      unsigned int flags)
5767
{
5768
    VIR_DEBUG("st=%p data=%p nbytes=%zu flags=0x%x",
5769
              st, data, nbytes, flags);
5770
    struct private_data *priv = st->conn->privateData;
5771
    virNetClientStreamPtr privst = st->privateData;
5772
    int rv;
5773

5774
    virCheckFlags(VIR_STREAM_RECV_STOP_AT_HOLE, -1);
5775

5776 5777 5778
    remoteDriverLock(priv);
    priv->localUses++;
    remoteDriverUnlock(priv);
5779

5780 5781 5782 5783
    rv = virNetClientStreamRecvPacket(privst,
                                      priv->client,
                                      data,
                                      nbytes,
5784 5785
                                      (st->flags & VIR_STREAM_NONBLOCK),
                                      flags);
5786

5787
    VIR_DEBUG("Done %d", rv);
5788

5789 5790
    remoteDriverLock(priv);
    priv->localUses--;
5791 5792 5793 5794
    remoteDriverUnlock(priv);
    return rv;
}

5795 5796 5797 5798 5799 5800 5801 5802
static int
remoteStreamRecv(virStreamPtr st,
                 char *data,
                 size_t nbytes)
{
    return remoteStreamRecvFlags(st, data, nbytes, 0);
}

5803 5804 5805 5806 5807 5808

static int
remoteStreamSendHole(virStreamPtr st,
                     long long length,
                     unsigned int flags)
{
5809
    VIR_DEBUG("st=%p length=%lld flags=0x%x",
5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830
              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;
}


5831 5832 5833 5834 5835 5836 5837 5838 5839
static int
remoteStreamRecvHole(virStreamPtr st,
                     long long *length,
                     unsigned int flags)
{
    struct private_data *priv = st->conn->privateData;
    virNetClientStreamPtr privst = st->privateData;
    int rv;

5840
    VIR_DEBUG("st=%p length=%p flags=0x%x",
5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857
              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;
}


5858 5859 5860 5861 5862 5863
struct remoteStreamCallbackData {
    virStreamPtr st;
    virStreamEventCallback cb;
    void *opaque;
    virFreeCallback ff;
};
5864

J
Ján Tomko 已提交
5865
static void remoteStreamEventCallback(virNetClientStreamPtr stream G_GNUC_UNUSED,
5866 5867
                                      int events,
                                      void *opaque)
5868
{
5869
    struct remoteStreamCallbackData *cbdata = opaque;
5870

5871
    (cbdata->cb)(cbdata->st, events, cbdata->opaque);
5872 5873 5874
}


5875
static void remoteStreamCallbackFree(void *opaque)
5876
{
5877 5878 5879 5880 5881
    struct remoteStreamCallbackData *cbdata = opaque;

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

5882
    virObjectUnref(cbdata->st);
5883
    VIR_FREE(opaque);
5884 5885 5886
}


5887
static int
5888 5889 5890 5891 5892
remoteStreamEventAddCallback(virStreamPtr st,
                             int events,
                             virStreamEventCallback cb,
                             void *opaque,
                             virFreeCallback ff)
5893
{
5894
    struct private_data *priv = st->conn->privateData;
5895
    virNetClientStreamPtr privst = st->privateData;
5896
    int ret = -1;
5897
    struct remoteStreamCallbackData *cbdata;
5898

5899
    if (VIR_ALLOC(cbdata) < 0)
5900 5901 5902 5903 5904
        return -1;
    cbdata->cb = cb;
    cbdata->opaque = opaque;
    cbdata->ff = ff;
    cbdata->st = st;
5905 5906
    virStreamRef(st);

5907
    remoteDriverLock(priv);
5908

5909 5910 5911 5912 5913 5914 5915 5916
    if ((ret = virNetClientStreamEventAddCallback(privst,
                                                  events,
                                                  remoteStreamEventCallback,
                                                  cbdata,
                                                  remoteStreamCallbackFree)) < 0) {
        VIR_FREE(cbdata);
        goto cleanup;
    }
5917

5918
 cleanup:
5919
    remoteDriverUnlock(priv);
5920
    /* coverity[leaked_storage] - cbdata is not leaked */
5921
    return ret;
5922 5923
}

5924

5925
static int
5926 5927
remoteStreamEventUpdateCallback(virStreamPtr st,
                                int events)
5928
{
5929
    struct private_data *priv = st->conn->privateData;
5930
    virNetClientStreamPtr privst = st->privateData;
5931 5932 5933 5934
    int ret = -1;

    remoteDriverLock(priv);

5935
    ret = virNetClientStreamEventUpdateCallback(privst, events);
5936

5937 5938 5939
    remoteDriverUnlock(priv);
    return ret;
}
5940 5941 5942


static int
5943
remoteStreamEventRemoveCallback(virStreamPtr st)
5944
{
5945
    struct private_data *priv = st->conn->privateData;
5946
    virNetClientStreamPtr privst = st->privateData;
5947 5948 5949 5950
    int ret = -1;

    remoteDriverLock(priv);

5951
    ret = virNetClientStreamEventRemoveCallback(privst);
5952 5953 5954

    remoteDriverUnlock(priv);
    return ret;
5955 5956
}

5957

5958
static int
5959
remoteStreamCloseInt(virStreamPtr st, bool streamAbort)
5960 5961
{
    struct private_data *priv = st->conn->privateData;
5962
    virNetClientStreamPtr privst = st->privateData;
5963 5964 5965 5966
    int ret = -1;

    remoteDriverLock(priv);

5967 5968 5969
    priv->localUses++;
    remoteDriverUnlock(priv);

5970 5971
    ret = virNetClientStreamSendPacket(privst,
                                       priv->client,
5972
                                       streamAbort ? VIR_NET_ERROR : VIR_NET_OK,
5973 5974
                                       NULL,
                                       0);
5975

5976 5977 5978
    remoteDriverLock(priv);
    priv->localUses--;

5979
    virNetClientRemoveStream(priv->client, privst);
5980 5981 5982 5983 5984

    remoteDriverUnlock(priv);
    return ret;
}

5985

5986
static int
5987
remoteStreamFinish(virStreamPtr st)
5988
{
5989 5990
    return remoteStreamCloseInt(st, false);
}
5991

5992

5993 5994 5995 5996
static int
remoteStreamAbort(virStreamPtr st)
{
    return remoteStreamCloseInt(st, true);
5997 5998 5999 6000 6001
}


static virStreamDriver remoteStreamDrv = {
    .streamRecv = remoteStreamRecv,
6002
    .streamRecvFlags = remoteStreamRecvFlags,
6003
    .streamSend = remoteStreamSend,
6004
    .streamSendHole = remoteStreamSendHole,
6005
    .streamRecvHole = remoteStreamRecvHole,
6006 6007
    .streamFinish = remoteStreamFinish,
    .streamAbort = remoteStreamAbort,
6008 6009 6010
    .streamEventAddCallback = remoteStreamEventAddCallback,
    .streamEventUpdateCallback = remoteStreamEventUpdateCallback,
    .streamEventRemoveCallback = remoteStreamEventRemoveCallback,
6011 6012
};

6013

6014 6015 6016 6017 6018 6019 6020
static int
remoteConnectDomainEventRegisterAny(virConnectPtr conn,
                                    virDomainPtr dom,
                                    int eventID,
                                    virConnectDomainEventGenericCallback callback,
                                    void *opaque,
                                    virFreeCallback freecb)
6021 6022 6023 6024
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
    int callbackID;
6025
    int count;
6026
    remote_nonnull_domain domain;
6027 6028 6029

    remoteDriverLock(priv);

6030 6031 6032
    if ((count = virDomainEventStateRegisterClient(conn, priv->eventState,
                                                   dom, eventID, callback,
                                                   opaque, freecb, false,
6033
                                                   &callbackID,
6034
                                                   priv->serverEventFilter)) < 0)
6035
        goto done;
6036 6037 6038

    /* If this is the first callback for this eventID, we need to enable
     * events on the server */
6039
    if (count == 1) {
6040
        if (priv->serverEventFilter) {
6041 6042 6043 6044 6045 6046 6047 6048 6049 6050
            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;
            }
6051

6052 6053 6054 6055 6056
            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,
6057
                                                callbackID, false);
6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070
                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,
6071
                                                callbackID, false);
6072 6073
                goto done;
            }
6074 6075 6076 6077 6078
        }
    }

    rv = callbackID;

6079
 done:
6080 6081 6082 6083 6084
    remoteDriverUnlock(priv);
    return rv;
}


6085 6086 6087
static int
remoteConnectDomainEventDeregisterAny(virConnectPtr conn,
                                      int callbackID)
6088 6089 6090 6091
{
    struct private_data *priv = conn->privateData;
    int rv = -1;
    int eventID;
6092
    int remoteID;
6093
    int count;
6094 6095 6096

    remoteDriverLock(priv);

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

6101
    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
6102
                                                 callbackID, true)) < 0)
6103
        goto done;
6104 6105 6106

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

6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127
            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;
        }
6128 6129 6130 6131
    }

    rv = 0;

6132
 done:
6133 6134 6135 6136
    remoteDriverUnlock(priv);
    return rv;
}

6137

6138 6139
/*----------------------------------------------------------------------*/

C
Chris Lalancette 已提交
6140
static int
6141
remoteDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd,
6142
                               char **result, unsigned int flags)
C
Chris Lalancette 已提交
6143 6144
{
    int rv = -1;
6145 6146
    qemu_domain_monitor_command_args args;
    qemu_domain_monitor_command_ret ret;
C
Chris Lalancette 已提交
6147 6148 6149 6150
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

6151
    make_nonnull_domain(&args.dom, domain);
C
Chris Lalancette 已提交
6152 6153 6154
    args.cmd = (char *)cmd;
    args.flags = flags;

6155
    memset(&ret, 0, sizeof(ret));
6156 6157 6158
    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 已提交
6159 6160
        goto done;

6161
    if (VIR_STRDUP(*result, ret.result) < 0)
C
Chris Lalancette 已提交
6162 6163 6164 6165
        goto cleanup;

    rv = 0;

6166
 cleanup:
6167
    xdr_free((xdrproc_t) xdr_qemu_domain_monitor_command_ret, (char *) &ret);
C
Chris Lalancette 已提交
6168

6169
 done:
C
Chris Lalancette 已提交
6170 6171 6172 6173
    remoteDriverUnlock(priv);
    return rv;
}

6174 6175 6176

static char *
remoteDomainMigrateBegin3(virDomainPtr domain,
6177
                          const char *xmlin,
6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193
                          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));

6194
    make_nonnull_domain(&args.dom, domain);
6195
    args.xmlin = xmlin == NULL ? NULL : (char **) &xmlin;
6196 6197 6198 6199
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;

6200 6201 6202
    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)
6203 6204 6205 6206
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6207 6208
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6209 6210 6211 6212 6213 6214 6215 6216
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

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

6217
 done:
6218 6219 6220
    remoteDriverUnlock(priv);
    return rv;

6221
 error:
6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257
    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;

6258 6259 6260 6261
    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)
6262 6263 6264 6265
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6266 6267
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6268 6269 6270 6271 6272 6273 6274
            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) {
6275 6276
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores uri_out"));
6277 6278 6279 6280 6281 6282 6283
            goto error;
        }
        *uri_out = *ret.uri_out; /* Caller frees. */
    }

    rv = 0;

6284
 done:
O
Osier Yang 已提交
6285
    VIR_FREE(ret.uri_out);
6286 6287
    remoteDriverUnlock(priv);
    return rv;
6288
 error:
6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311
    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;
6312
    virNetClientStreamPtr netst;
6313 6314 6315 6316 6317 6318

    remoteDriverLock(priv);

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

6319
    if (!(netst = virNetClientStreamNew(priv->remoteProgram,
6320
                                        REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3,
6321 6322
                                        priv->counter,
                                        false)))
6323 6324
        goto done;

6325
    if (virNetClientAddStream(priv->client, netst) < 0) {
6326
        virObjectUnref(netst);
6327 6328 6329
        goto done;
    }

6330
    st->driver = &remoteStreamDrv;
6331
    st->privateData = netst;
6332
    st->ff = virObjectFreeCallback;
6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343

    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) {
6344
        virNetClientRemoveStream(priv->client, netst);
6345
        virObjectUnref(netst);
6346 6347 6348 6349 6350
        goto done;
    }

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6351 6352
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6353 6354 6355 6356 6357 6358 6359 6360
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = 0;

6361
 done:
6362 6363 6364
    remoteDriverUnlock(priv);
    return rv;

6365
 error:
6366 6367 6368 6369 6370 6371 6372
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto done;
}


static int
remoteDomainMigratePerform3(virDomainPtr dom,
6373
                            const char *xmlin,
6374 6375 6376 6377
                            const char *cookiein,
                            int cookieinlen,
                            char **cookieout,
                            int *cookieoutlen,
6378
                            const char *dconnuri,
6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395
                            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);

6396
    args.xmlin = xmlin == NULL ? NULL : (char **) &xmlin;
6397 6398 6399 6400
    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;
6401 6402
    args.uri = uri == NULL ? NULL : (char **) &uri;
    args.dconnuri = dconnuri == NULL ? NULL : (char **) &dconnuri;
6403 6404
    args.resource = resource;

6405 6406 6407
    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)
6408 6409 6410 6411
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6412 6413
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6414 6415 6416 6417 6418 6419 6420 6421
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = 0;

6422
 done:
6423 6424 6425
    remoteDriverUnlock(priv);
    return rv;

6426
 error:
6427 6428 6429 6430 6431
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto done;
}


6432
static virDomainPtr
6433 6434 6435 6436 6437 6438
remoteDomainMigrateFinish3(virConnectPtr dconn,
                           const char *dname,
                           const char *cookiein,
                           int cookieinlen,
                           char **cookieout,
                           int *cookieoutlen,
6439
                           const char *dconnuri,
6440 6441
                           const char *uri,
                           unsigned long flags,
6442
                           int cancelled)
6443 6444 6445 6446
{
    remote_domain_migrate_finish3_args args;
    remote_domain_migrate_finish3_ret ret;
    struct private_data *priv = dconn->privateData;
6447
    virDomainPtr rv = NULL;
6448 6449 6450 6451 6452 6453 6454 6455 6456

    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;
6457 6458
    args.uri = uri == NULL ? NULL : (char **) &uri;
    args.dconnuri = dconnuri == NULL ? NULL : (char **) &dconnuri;
6459 6460 6461
    args.flags = flags;
    args.cancelled = cancelled;

6462 6463 6464
    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)
6465 6466
        goto done;

6467
    rv = get_nonnull_domain(dconn, ret.dom);
6468 6469 6470

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6471 6472
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6473 6474 6475 6476 6477 6478 6479 6480
            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;
    }

6481
    xdr_free((xdrproc_t) &xdr_remote_domain_migrate_finish3_ret, (char *) &ret);
6482

6483
 done:
6484 6485 6486
    remoteDriverUnlock(priv);
    return rv;

6487
 error:
6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507
    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));

6508
    make_nonnull_domain(&args.dom, domain);
6509 6510 6511 6512 6513
    args.cookie_in.cookie_in_len = cookieinlen;
    args.cookie_in.cookie_in_val = (char *) cookiein;
    args.flags = flags;
    args.cancelled = cancelled;

6514 6515 6516
    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)
6517 6518 6519 6520
        goto done;

    rv = 0;

6521
 done:
6522 6523 6524 6525 6526
    remoteDriverUnlock(priv);
    return rv;
}


6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577
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;

6578
 cleanup:
6579
    virStringListFree(retmodels);
6580 6581 6582

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

6583
 done:
6584 6585 6586 6587 6588
    remoteDriverUnlock(priv);
    return rv;
}


6589 6590 6591 6592 6593 6594 6595 6596 6597
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;
6598
    int fdin[] = { fd };
6599
    size_t fdinlen = G_N_ELEMENTS(fdin);
6600 6601 6602

    remoteDriverLock(priv);

6603
    make_nonnull_domain(&args.dom, dom);
6604 6605 6606
    args.idx = idx;
    args.flags = flags;

6607 6608 6609 6610 6611 6612
    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)
6613 6614 6615 6616
        goto done;

    rv = 0;

6617
 done:
6618 6619 6620 6621 6622 6623
    remoteDriverUnlock(priv);

    return rv;
}


6624 6625 6626 6627 6628 6629
static int
remoteDomainOpenGraphicsFD(virDomainPtr dom,
                           unsigned int idx,
                           unsigned int flags)
{
    int rv = -1;
6630
    remote_domain_open_graphics_fd_args args;
6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649
    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) {
6650 6651 6652 6653 6654 6655 6656 6657 6658
        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"));
        }
6659 6660
        goto done;
    }
6661
    rv = fdout[0];
6662 6663

 done:
6664
    VIR_FREE(fdout);
6665 6666 6667 6668 6669 6670
    remoteDriverUnlock(priv);

    return rv;
}


6671
static int
6672
remoteConnectSetKeepAlive(virConnectPtr conn, int interval, unsigned int count)
6673 6674 6675 6676 6677 6678
{
    struct private_data *priv = conn->privateData;
    int ret = -1;

    remoteDriverLock(priv);
    if (!virNetClientKeepAliveIsSupported(priv->client)) {
6679 6680 6681
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("the caller doesn't support keepalive protocol;"
                         " perhaps it's missing event loop implementation"));
6682 6683 6684 6685 6686 6687 6688 6689
        goto cleanup;
    }

    if (!priv->serverKeepAlive) {
        ret = 1;
        goto cleanup;
    }

6690 6691 6692 6693 6694 6695
    if (interval > 0) {
        ret = virNetClientKeepAliveStart(priv->client, interval, count);
    } else {
        virNetClientKeepAliveStop(priv->client);
        ret = 0;
    }
6696

6697
 cleanup:
6698 6699 6700 6701 6702
    remoteDriverUnlock(priv);
    return ret;
}


6703
static int
6704
remoteConnectIsAlive(virConnectPtr conn)
6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719
{
    struct private_data *priv = conn->privateData;
    bool ret;

    remoteDriverLock(priv);
    ret = virNetClientIsOpen(priv->client);
    remoteDriverUnlock(priv);

    if (ret)
        return 1;
    else
        return 0;
}


6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736
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;

6737
    memset(&ret, 0, sizeof(ret));
6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754

    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;

6755
 cleanup:
6756 6757
    xdr_free((xdrproc_t) xdr_remote_domain_get_disk_errors_ret, (char *) &ret);

6758
 done:
6759 6760 6761 6762
    remoteDriverUnlock(priv);
    return rv;
}

6763
#include "remote_client_bodies.h"
6764
#include "lxc_client_bodies.h"
6765
#include "qemu_client_bodies.h"
6766

6767 6768 6769 6770 6771
/*
 * Serial a set of arguments into a method call message,
 * send that to the server and wait for reply
 */
static int
J
Ján Tomko 已提交
6772
callFull(virConnectPtr conn G_GNUC_UNUSED,
6773 6774 6775 6776 6777 6778 6779 6780 6781
         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)
6782
{
6783
    int rv;
6784
    virNetClientProgramPtr prog;
6785 6786 6787 6788
    int counter = priv->counter++;
    virNetClientPtr client = priv->client;
    priv->localUses++;

6789 6790 6791 6792 6793 6794 6795
    if (flags & REMOTE_CALL_QEMU)
        prog = priv->qemuProgram;
    else if (flags & REMOTE_CALL_LXC)
        prog = priv->lxcProgram;
    else
        prog = priv->remoteProgram;

6796 6797 6798 6799 6800 6801 6802 6803 6804
    /* 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,
6805 6806
                                 fdinlen, fdin,
                                 fdoutlen, fdout,
6807 6808 6809 6810
                                 args_filter, args,
                                 ret_filter, ret);
    remoteDriverLock(priv);
    priv->localUses--;
6811

6812
    return rv;
6813 6814
}

6815
static int
6816 6817 6818 6819 6820 6821
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)
6822
{
6823 6824 6825 6826 6827 6828
    return callFull(conn, priv, flags,
                    NULL, 0,
                    NULL, NULL,
                    proc_nr,
                    args_filter, args,
                    ret_filter, ret);
6829 6830
}

6831

6832
static int
6833 6834 6835 6836
remoteDomainGetInterfaceParameters(virDomainPtr domain,
                                   const char *device,
                                   virTypedParameterPtr params, int *nparams,
                                   unsigned int flags)
6837 6838 6839 6840 6841 6842 6843 6844
{
    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);

6845
    make_nonnull_domain(&args.dom, domain);
6846 6847 6848 6849
    args.device = (char *)device;
    args.nparams = *nparams;
    args.flags = flags;

6850 6851 6852 6853
    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)
6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864
        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;
    }

6865 6866 6867 6868 6869
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX,
                                  &params,
                                  nparams) < 0)
6870 6871 6872 6873
        goto cleanup;

    rv = 0;

6874
 cleanup:
6875 6876
    xdr_free((xdrproc_t) xdr_remote_domain_get_interface_parameters_ret,
             (char *) &ret);
6877
 done:
6878 6879 6880 6881
    remoteDriverUnlock(priv);
    return rv;
}

6882

6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898
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;

6899 6900 6901 6902
    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)
6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913
        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;
    }

6914 6915 6916 6917 6918
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_NODE_MEMORY_PARAMETERS_MAX,
                                  &params,
                                  nparams) < 0)
6919 6920 6921 6922
        goto cleanup;

    rv = 0;

6923
 cleanup:
6924 6925
    xdr_free((xdrproc_t) xdr_remote_node_get_memory_parameters_ret,
             (char *) &ret);
6926
 done:
6927 6928 6929 6930
    remoteDriverUnlock(priv);
    return rv;
}

6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969

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


6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982
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);

6983 6984
    args.need_map = !!cpumap;
    args.need_online = !!online;
6985 6986
    args.flags = flags;

6987
    memset(&ret, 0, sizeof(ret));
6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998
    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) {
6999
        if (VIR_ALLOC_N(*cpumap, ret.cpumap.cpumap_len) < 0)
7000 7001 7002 7003 7004 7005 7006 7007 7008
            goto cleanup;
        memcpy(*cpumap, ret.cpumap.cpumap_val, ret.cpumap.cpumap_len);
    }

    if (online)
        *online = ret.online;

    rv = ret.ret;

7009
 cleanup:
7010
    xdr_free((xdrproc_t) xdr_remote_node_get_cpu_map_ret, (char *) &ret);
7011
 done:
7012 7013 7014 7015
    remoteDriverUnlock(priv);
    return rv;
}

7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043

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;

7044
 done:
7045 7046 7047 7048
    remoteDriverUnlock(priv);
    return rv;
}

7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073
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;

7074 7075 7076 7077
    if (virTypedParamsDeserialize((virTypedParameterRemotePtr) ret.params.params_val,
                                  ret.params.params_len,
                                  REMOTE_DOMAIN_JOB_STATS_MAX,
                                  params, nparams) < 0)
7078 7079 7080 7081
        goto cleanup;

    rv = 0;

7082
 cleanup:
7083 7084
    xdr_free((xdrproc_t) xdr_remote_domain_get_job_stats_ret,
             (char *) &ret);
7085
 done:
7086 7087 7088 7089
    remoteDriverUnlock(priv);
    return rv;
}

7090

7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111
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;

7112
    if (virTypedParamsSerialize(params, nparams,
7113
                                REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX,
7114
                                (virTypedParameterRemotePtr *) &args.params.params_val,
7115 7116
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140
        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 */

7141
 cleanup:
7142 7143
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7144 7145 7146
    remoteDriverUnlock(priv);
    return rv;

7147
 error:
7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173
    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));

7174
    if (virTypedParamsSerialize(params, nparams,
7175
                                REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX,
7176
                                (virTypedParameterRemotePtr *) &args.params.params_val,
7177 7178
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214
        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;

7215
 cleanup:
7216 7217
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7218 7219 7220 7221
    VIR_FREE(ret.uri_out);
    remoteDriverUnlock(priv);
    return rv;

7222
 error:
7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255
    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));

    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.flags = flags;

7256
    if (virTypedParamsSerialize(params, nparams,
7257
                                REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX,
7258
                                (virTypedParameterRemotePtr *) &args.params.params_val,
7259 7260
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
7261 7262 7263 7264 7265
        xdr_free((xdrproc_t) xdr_remote_domain_migrate_prepare_tunnel3_params_args,
                 (char *) &args);
        goto cleanup;
    }

7266
    if (!(netst = virNetClientStreamNew(priv->remoteProgram,
7267
                                        REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3_PARAMS,
7268 7269
                                        priv->counter,
                                        false)))
7270 7271 7272 7273 7274 7275 7276 7277 7278
        goto cleanup;

    if (virNetClientAddStream(priv->client, netst) < 0) {
        virObjectUnref(netst);
        goto cleanup;
    }

    st->driver = &remoteStreamDrv;
    st->privateData = netst;
7279
    st->ff = virObjectFreeCallback;
7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302

    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;

7303
 cleanup:
7304 7305
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7306 7307 7308
    remoteDriverUnlock(priv);
    return rv;

7309
 error:
7310 7311 7312 7313 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
    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));

    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;

7342
    if (virTypedParamsSerialize(params, nparams,
7343
                                REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX,
7344
                                (virTypedParameterRemotePtr *) &args.params.params_val,
7345 7346
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370
        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;

7371
 cleanup:
7372 7373
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7374 7375 7376
    remoteDriverUnlock(priv);
    return rv;

7377
 error:
7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408
    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));

    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.flags = flags;
    args.cancelled = cancelled;

7409
    if (virTypedParamsSerialize(params, nparams,
7410
                                REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX,
7411
                                (virTypedParameterRemotePtr *) &args.params.params_val,
7412 7413
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442
        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);

7443
 cleanup:
7444 7445
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7446 7447 7448
    remoteDriverUnlock(priv);
    return rv;

7449
 error:
7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477
    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));

    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;

7478
    if (virTypedParamsSerialize(params, nparams,
7479
                                REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX,
7480
                                (virTypedParameterRemotePtr *) &args.params.params_val,
7481 7482
                                &args.params.params_len,
                                VIR_TYPED_PARAM_STRING_OKAY) < 0) {
7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494
        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;

7495
 cleanup:
7496 7497
    virTypedParamsRemoteFree((virTypedParameterRemotePtr) args.params.params_val,
                             args.params.params_len);
7498 7499 7500 7501 7502
    remoteDriverUnlock(priv);
    return rv;
}


7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530
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);

7531
 done:
7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566
    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;

7567
 done:
7568 7569 7570 7571
    remoteDriverUnlock(priv);
    return rv;
}

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

7605

M
Michal Privoznik 已提交
7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628
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;
    }

7629
    args.pages.pages_val = (u_int *) pages;
M
Michal Privoznik 已提交
7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652
    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;
}


7653 7654 7655 7656 7657 7658 7659 7660
/* 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;

7661
    if (VIR_STRDUP(lease_dst->iface, lease_src->iface) < 0)
7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704
        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,
7705
                           const char *mac,
7706 7707 7708 7709 7710
                           virNetworkDHCPLeasePtr **leases,
                           unsigned int flags)
{
    int rv = -1;
    size_t i;
7711
    struct private_data *priv = net->conn->privateData;
7712 7713 7714 7715 7716 7717 7718
    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);
7719
    args.mac = mac ? (char **) &mac : NULL;
7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770
    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;
}


7771 7772 7773 7774 7775 7776 7777 7778
static int
remoteConnectGetAllDomainStats(virConnectPtr conn,
                               virDomainPtr *doms,
                               unsigned int ndoms,
                               unsigned int stats,
                               virDomainStatsRecordPtr **retStats,
                               unsigned int flags)
{
7779
    struct private_data *priv = conn->privateData;
7780 7781 7782 7783
    int rv = -1;
    size_t i;
    remote_connect_get_all_domain_stats_args args;
    remote_connect_get_all_domain_stats_ret ret;
7784
    virDomainStatsRecordPtr elem = NULL;
7785 7786
    virDomainStatsRecordPtr *tmpret = NULL;

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

7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832
    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;

7833 7834 7835 7836 7837
        if (virTypedParamsDeserialize((virTypedParameterRemotePtr) rec->params.params_val,
                                      rec->params.params_len,
                                      REMOTE_CONNECT_GET_ALL_DOMAIN_STATS_MAX,
                                      &elem->params,
                                      &elem->nparams))
7838 7839 7840
            goto cleanup;

        tmpret[i] = elem;
7841
        elem = NULL;
7842 7843 7844 7845 7846 7847 7848
    }

    *retStats = tmpret;
    tmpret = NULL;
    rv = ret.retStats.retStats_len;

 cleanup:
7849 7850 7851 7852
    if (elem) {
        virObjectUnref(elem->dom);
        VIR_FREE(elem);
    }
7853
    virDomainStatsRecordListFree(tmpret);
7854
    VIR_FREE(args.doms.doms_val);
7855 7856 7857 7858 7859 7860
    xdr_free((xdrproc_t)xdr_remote_connect_get_all_domain_stats_ret,
             (char *) &ret);

    return rv;
}

M
Michal Privoznik 已提交
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 7905 7906

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


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


7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052
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;

8053 8054
        if (iface_ret->hwaddr &&
            VIR_STRDUP(iface->hwaddr, *iface_ret->hwaddr) < 0)
8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 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
            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;
}

8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117
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;
    }

8118
    if (priv->serverCloseCallback &&
8119
        call(conn, priv, 0, REMOTE_PROC_CONNECT_REGISTER_CLOSE_CALLBACK,
8120 8121 8122 8123
             (xdrproc_t) xdr_void, (char *) NULL,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto cleanup;

8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148
    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;
    }

8149
    if (priv->serverCloseCallback &&
8150
        call(conn, priv, 0, REMOTE_PROC_CONNECT_UNREGISTER_CLOSE_CALLBACK,
8151 8152 8153 8154
             (xdrproc_t) xdr_void, (char *) NULL,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto cleanup;

8155 8156 8157 8158 8159 8160 8161 8162
    virConnectCloseCallbackDataUnregister(priv->closeCallback, cb);
    ret = 0;

 cleanup:
    remoteDriverUnlock(priv);

    return ret;
}
8163

8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204
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;
}


8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240
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;
}


8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278
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;
}

8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321
static int
remoteDomainGetGuestInfo(virDomainPtr dom,
                         unsigned int types,
                         virTypedParameterPtr *params,
                         int *nparams,
                         unsigned int flags)
{
    int rv = -1;
    struct private_data *priv = dom->conn->privateData;
    remote_domain_get_guest_info_args args;
    remote_domain_get_guest_info_ret ret;

    remoteDriverLock(priv);

    make_nonnull_domain(&args.dom, dom);

    args.types = types;
    args.flags = flags;

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

    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_GUEST_INFO,
             (xdrproc_t)xdr_remote_domain_get_guest_info_args, (char *)&args,
             (xdrproc_t)xdr_remote_domain_get_guest_info_ret, (char *)&ret) == -1)
        goto done;

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

    rv = 0;

 cleanup:
    xdr_free((xdrproc_t)xdr_remote_domain_get_guest_info_ret,
             (char *) &ret);

 done:
    remoteDriverUnlock(priv);
    return rv;
}
8322

8323 8324 8325 8326 8327 8328
/* 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
8329
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
8330
{
8331
    return virGetDomain(conn, domain.name, BAD_CAST domain.uuid, domain.id);
8332 8333 8334
}

static virNetworkPtr
8335
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
8336
{
8337
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
8338 8339
}

8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352
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 已提交
8353
static virInterfacePtr
8354
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
8355
{
8356
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
8357 8358
}

8359
static virStoragePoolPtr
8360
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
8361
{
8362 8363
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
8364 8365 8366
}

static virStorageVolPtr
8367
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
8368
{
8369 8370
    return virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                            NULL, NULL);
8371 8372
}

8373
static virNodeDevicePtr
8374
get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev)
8375 8376 8377 8378
{
    return virGetNodeDevice(conn, dev.name);
}

8379
static virSecretPtr
8380
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
8381
{
8382
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
8383 8384
}

8385
static virNWFilterPtr
8386
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
8387
{
8388
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
8389 8390
}

8391 8392 8393 8394 8395 8396
static virNWFilterBindingPtr
get_nonnull_nwfilter_binding(virConnectPtr conn, remote_nonnull_nwfilter_binding binding)
{
    return virGetNWFilterBinding(conn, binding.portdev, binding.filtername);
}

8397 8398 8399 8400 8401 8402
static virDomainCheckpointPtr
get_nonnull_domain_checkpoint(virDomainPtr domain, remote_nonnull_domain_checkpoint checkpoint)
{
    return virGetDomainCheckpoint(domain, checkpoint.name);
}

C
Chris Lalancette 已提交
8403
static virDomainSnapshotPtr
8404
get_nonnull_domain_snapshot(virDomainPtr domain, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
8405 8406 8407 8408
{
    return virGetDomainSnapshot(domain, snapshot.name);
}

8409

8410 8411
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
8412
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
8413
{
8414
    dom_dst->id = dom_src->id;
8415
    dom_dst->name = dom_src->name;
8416
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
8417 8418 8419
}

static void
8420
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
8421 8422
{
    net_dst->name = net_src->name;
8423
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
8424 8425
}

8426 8427 8428 8429 8430 8431 8432 8433
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 已提交
8434
static void
8435
make_nonnull_interface(remote_nonnull_interface *interface_dst,
D
Daniel Veillard 已提交
8436 8437 8438 8439 8440 8441
                        virInterfacePtr interface_src)
{
    interface_dst->name = interface_src->name;
    interface_dst->mac = interface_src->mac;
}

8442
static void
8443
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
8444 8445
{
    pool_dst->name = pool_src->name;
8446
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
8447 8448 8449
}

static void
8450
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
8451 8452 8453 8454 8455 8456
{
    vol_dst->pool = vol_src->pool;
    vol_dst->name = vol_src->name;
    vol_dst->key = vol_src->key;
}

8457
static void
8458
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
8459
{
8460
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
8461 8462
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = secret_src->usageID;
8463 8464
}

8465 8466 8467 8468 8469 8470
static void
make_nonnull_node_device(remote_nonnull_node_device *dev_dst, virNodeDevicePtr dev_src)
{
    dev_dst->name = dev_src->name;
}

8471
static void
8472
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
8473 8474
{
    nwfilter_dst->name = nwfilter_src->name;
8475
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
8476 8477
}

8478 8479 8480 8481 8482 8483 8484
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;
}

8485 8486 8487 8488 8489 8490 8491
static void
make_nonnull_domain_checkpoint(remote_nonnull_domain_checkpoint *checkpoint_dst, virDomainCheckpointPtr checkpoint_src)
{
    checkpoint_dst->name = checkpoint_src->name;
    make_nonnull_domain(&checkpoint_dst->dom, checkpoint_src->domain);
}

C
Chris Lalancette 已提交
8492
static void
8493
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
8494 8495
{
    snapshot_dst->name = snapshot_src->name;
8496
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
8497 8498
}

8499 8500
/*----------------------------------------------------------------------*/

8501 8502 8503 8504 8505
unsigned long remoteVersion(void)
{
    return REMOTE_PROTOCOL_VERSION;
}

8506
static virHypervisorDriver hypervisor_driver = {
8507
    .name = "remote",
8508 8509
    .connectOpen = remoteConnectOpen, /* 0.3.0 */
    .connectClose = remoteConnectClose, /* 0.3.0 */
8510
    .connectSetIdentity = remoteConnectSetIdentity, /* 5.8.0 */
8511 8512 8513 8514 8515 8516 8517
    .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 */
8518
    .nodeGetInfo = remoteNodeGetInfo, /* 0.3.0 */
8519 8520 8521 8522
    .connectGetCapabilities = remoteConnectGetCapabilities, /* 0.3.0 */
    .connectListDomains = remoteConnectListDomains, /* 0.3.0 */
    .connectNumOfDomains = remoteConnectNumOfDomains, /* 0.3.0 */
    .connectListAllDomains = remoteConnectListAllDomains, /* 0.9.13 */
8523
    .domainCreateXML = remoteDomainCreateXML, /* 0.3.0 */
8524
    .domainCreateXMLWithFiles = remoteDomainCreateXMLWithFiles, /* 1.1.1 */
8525 8526 8527 8528 8529
    .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 */
8530
    .domainPMSuspendForDuration = remoteDomainPMSuspendForDuration, /* 0.9.10 */
8531
    .domainPMWakeup = remoteDomainPMWakeup, /* 0.9.11 */
8532
    .domainShutdown = remoteDomainShutdown, /* 0.3.0 */
8533
    .domainShutdownFlags = remoteDomainShutdownFlags, /* 0.9.10 */
8534
    .domainReboot = remoteDomainReboot, /* 0.3.0 */
8535
    .domainReset = remoteDomainReset, /* 0.9.7 */
8536
    .domainDestroy = remoteDomainDestroy, /* 0.3.0 */
8537
    .domainDestroyFlags = remoteDomainDestroyFlags, /* 0.9.4 */
8538 8539 8540 8541 8542
    .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 */
8543
    .domainSetMemoryStatsPeriod = remoteDomainSetMemoryStatsPeriod, /* 1.1.1 */
8544 8545 8546 8547
    .domainSetMemoryParameters = remoteDomainSetMemoryParameters, /* 0.8.5 */
    .domainGetMemoryParameters = remoteDomainGetMemoryParameters, /* 0.8.5 */
    .domainSetBlkioParameters = remoteDomainSetBlkioParameters, /* 0.9.0 */
    .domainGetBlkioParameters = remoteDomainGetBlkioParameters, /* 0.9.0 */
8548 8549
    .domainGetPerfEvents = remoteDomainGetPerfEvents, /* 1.3.3 */
    .domainSetPerfEvents = remoteDomainSetPerfEvents, /* 1.3.3 */
8550 8551
    .domainGetInfo = remoteDomainGetInfo, /* 0.3.0 */
    .domainGetState = remoteDomainGetState, /* 0.9.2 */
8552
    .domainGetControlInfo = remoteDomainGetControlInfo, /* 0.9.3 */
8553
    .domainSave = remoteDomainSave, /* 0.3.0 */
E
Eric Blake 已提交
8554
    .domainSaveFlags = remoteDomainSaveFlags, /* 0.9.4 */
8555
    .domainRestore = remoteDomainRestore, /* 0.3.0 */
E
Eric Blake 已提交
8556
    .domainRestoreFlags = remoteDomainRestoreFlags, /* 0.9.4 */
E
Eric Blake 已提交
8557 8558
    .domainSaveImageGetXMLDesc = remoteDomainSaveImageGetXMLDesc, /* 0.9.4 */
    .domainSaveImageDefineXML = remoteDomainSaveImageDefineXML, /* 0.9.4 */
8559
    .domainCoreDump = remoteDomainCoreDump, /* 0.3.0 */
8560
    .domainCoreDumpWithFormat = remoteDomainCoreDumpWithFormat, /* 1.2.3 */
8561 8562 8563 8564 8565
    .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 */
8566
    .domainPinVcpuFlags = remoteDomainPinVcpuFlags, /* 0.9.3 */
E
Eric Blake 已提交
8567
    .domainGetVcpuPinInfo = remoteDomainGetVcpuPinInfo, /* 0.9.3 */
8568 8569
    .domainPinEmulator = remoteDomainPinEmulator, /* 0.10.0 */
    .domainGetEmulatorPinInfo = remoteDomainGetEmulatorPinInfo, /* 0.10.0 */
8570 8571
    .domainGetVcpus = remoteDomainGetVcpus, /* 0.3.0 */
    .domainGetMaxVcpus = remoteDomainGetMaxVcpus, /* 0.3.0 */
8572
    .domainGetIOThreadInfo = remoteDomainGetIOThreadInfo, /* 1.2.14 */
8573
    .domainPinIOThread = remoteDomainPinIOThread, /* 1.2.14 */
8574 8575
    .domainAddIOThread = remoteDomainAddIOThread, /* 1.2.15 */
    .domainDelIOThread = remoteDomainDelIOThread, /* 1.2.15 */
8576
    .domainSetIOThreadParams = remoteDomainSetIOThreadParams, /* 4.10.0 */
8577
    .domainGetSecurityLabel = remoteDomainGetSecurityLabel, /* 0.6.1 */
M
Marcelo Cerri 已提交
8578
    .domainGetSecurityLabelList = remoteDomainGetSecurityLabelList, /* 0.10.0 */
8579 8580
    .nodeGetSecurityModel = remoteNodeGetSecurityModel, /* 0.6.1 */
    .domainGetXMLDesc = remoteDomainGetXMLDesc, /* 0.3.0 */
8581 8582 8583 8584
    .connectDomainXMLFromNative = remoteConnectDomainXMLFromNative, /* 0.6.4 */
    .connectDomainXMLToNative = remoteConnectDomainXMLToNative, /* 0.6.4 */
    .connectListDefinedDomains = remoteConnectListDefinedDomains, /* 0.3.0 */
    .connectNumOfDefinedDomains = remoteConnectNumOfDefinedDomains, /* 0.3.0 */
8585 8586
    .domainCreate = remoteDomainCreate, /* 0.3.0 */
    .domainCreateWithFlags = remoteDomainCreateWithFlags, /* 0.8.2 */
8587
    .domainCreateWithFiles = remoteDomainCreateWithFiles, /* 1.1.1 */
8588
    .domainDefineXML = remoteDomainDefineXML, /* 0.3.0 */
8589
    .domainDefineXMLFlags = remoteDomainDefineXMLFlags, /* 1.2.12 */
8590
    .domainUndefine = remoteDomainUndefine, /* 0.3.0 */
8591
    .domainUndefineFlags = remoteDomainUndefineFlags, /* 0.9.4 */
8592 8593 8594 8595 8596
    .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 */
8597
    .domainDetachDeviceAlias = remoteDomainDetachDeviceAlias, /* 4.4.0 */
8598 8599 8600 8601
    .domainGetAutostart = remoteDomainGetAutostart, /* 0.3.0 */
    .domainSetAutostart = remoteDomainSetAutostart, /* 0.3.0 */
    .domainGetSchedulerType = remoteDomainGetSchedulerType, /* 0.3.0 */
    .domainGetSchedulerParameters = remoteDomainGetSchedulerParameters, /* 0.3.0 */
8602
    .domainGetSchedulerParametersFlags = remoteDomainGetSchedulerParametersFlags, /* 0.9.2 */
8603
    .domainSetSchedulerParameters = remoteDomainSetSchedulerParameters, /* 0.3.0 */
8604
    .domainSetSchedulerParametersFlags = remoteDomainSetSchedulerParametersFlags, /* 0.9.2 */
8605 8606 8607
    .domainMigratePrepare = remoteDomainMigratePrepare, /* 0.3.2 */
    .domainMigratePerform = remoteDomainMigratePerform, /* 0.3.2 */
    .domainMigrateFinish = remoteDomainMigrateFinish, /* 0.3.2 */
8608
    .domainBlockResize = remoteDomainBlockResize, /* 0.9.8 */
8609
    .domainBlockStats = remoteDomainBlockStats, /* 0.3.2 */
8610
    .domainBlockStatsFlags = remoteDomainBlockStatsFlags, /* 0.9.5 */
8611
    .domainInterfaceStats = remoteDomainInterfaceStats, /* 0.3.2 */
8612 8613
    .domainSetInterfaceParameters = remoteDomainSetInterfaceParameters, /* 0.9.9 */
    .domainGetInterfaceParameters = remoteDomainGetInterfaceParameters, /* 0.9.9 */
8614 8615 8616 8617
    .domainMemoryStats = remoteDomainMemoryStats, /* 0.7.5 */
    .domainBlockPeek = remoteDomainBlockPeek, /* 0.4.2 */
    .domainMemoryPeek = remoteDomainMemoryPeek, /* 0.4.2 */
    .domainGetBlockInfo = remoteDomainGetBlockInfo, /* 0.8.1 */
8618
    .nodeGetCPUStats = remoteNodeGetCPUStats, /* 0.9.3 */
8619
    .nodeGetMemoryStats = remoteNodeGetMemoryStats, /* 0.9.3 */
8620 8621
    .nodeGetCellsFreeMemory = remoteNodeGetCellsFreeMemory, /* 0.3.3 */
    .nodeGetFreeMemory = remoteNodeGetFreeMemory, /* 0.3.3 */
8622 8623
    .connectDomainEventRegister = remoteConnectDomainEventRegister, /* 0.5.0 */
    .connectDomainEventDeregister = remoteConnectDomainEventDeregister, /* 0.5.0 */
8624 8625 8626
    .domainMigratePrepare2 = remoteDomainMigratePrepare2, /* 0.5.0 */
    .domainMigrateFinish2 = remoteDomainMigrateFinish2, /* 0.5.0 */
    .nodeDeviceDettach = remoteNodeDeviceDettach, /* 0.6.1 */
8627
    .nodeDeviceDetachFlags = remoteNodeDeviceDetachFlags, /* 1.0.5 */
8628 8629 8630
    .nodeDeviceReAttach = remoteNodeDeviceReAttach, /* 0.6.1 */
    .nodeDeviceReset = remoteNodeDeviceReset, /* 0.6.1 */
    .domainMigratePrepareTunnel = remoteDomainMigratePrepareTunnel, /* 0.7.2 */
8631 8632
    .connectIsEncrypted = remoteConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = remoteConnectIsSecure, /* 0.7.3 */
8633 8634 8635
    .domainIsActive = remoteDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = remoteDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = remoteDomainIsUpdated, /* 0.8.6 */
8636 8637
    .connectCompareCPU = remoteConnectCompareCPU, /* 0.7.5 */
    .connectBaselineCPU = remoteConnectBaselineCPU, /* 0.7.7 */
8638
    .domainGetJobInfo = remoteDomainGetJobInfo, /* 0.7.7 */
8639
    .domainGetJobStats = remoteDomainGetJobStats, /* 1.0.3 */
8640
    .domainAbortJob = remoteDomainAbortJob, /* 0.7.7 */
8641
    .domainMigrateGetMaxDowntime = remoteDomainMigrateGetMaxDowntime, /* 3.7.0 */
8642
    .domainMigrateSetMaxDowntime = remoteDomainMigrateSetMaxDowntime, /* 0.8.0 */
8643 8644
    .domainMigrateGetCompressionCache = remoteDomainMigrateGetCompressionCache, /* 1.0.3 */
    .domainMigrateSetCompressionCache = remoteDomainMigrateSetCompressionCache, /* 1.0.3 */
8645
    .domainMigrateSetMaxSpeed = remoteDomainMigrateSetMaxSpeed, /* 0.9.0 */
8646
    .domainMigrateGetMaxSpeed = remoteDomainMigrateGetMaxSpeed, /* 0.9.5 */
8647 8648
    .connectDomainEventRegisterAny = remoteConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = remoteConnectDomainEventDeregisterAny, /* 0.8.0 */
8649 8650 8651
    .domainManagedSave = remoteDomainManagedSave, /* 0.8.0 */
    .domainHasManagedSaveImage = remoteDomainHasManagedSaveImage, /* 0.8.0 */
    .domainManagedSaveRemove = remoteDomainManagedSaveRemove, /* 0.8.0 */
8652
    .domainManagedSaveGetXMLDesc = remoteDomainManagedSaveGetXMLDesc, /* 3.7.0 */
8653
    .domainManagedSaveDefineXML = remoteDomainManagedSaveDefineXML, /* 3.7.0 */
8654 8655 8656 8657
    .domainSnapshotCreateXML = remoteDomainSnapshotCreateXML, /* 0.8.0 */
    .domainSnapshotGetXMLDesc = remoteDomainSnapshotGetXMLDesc, /* 0.8.0 */
    .domainSnapshotNum = remoteDomainSnapshotNum, /* 0.8.0 */
    .domainSnapshotListNames = remoteDomainSnapshotListNames, /* 0.8.0 */
8658
    .domainListAllSnapshots = remoteDomainListAllSnapshots, /* 0.9.13 */
8659
    .domainSnapshotNumChildren = remoteDomainSnapshotNumChildren, /* 0.9.7 */
8660
    .domainSnapshotListAllChildren = remoteDomainSnapshotListAllChildren, /* 0.9.13 */
8661
    .domainSnapshotListChildrenNames = remoteDomainSnapshotListChildrenNames, /* 0.9.7 */
8662 8663
    .domainSnapshotLookupByName = remoteDomainSnapshotLookupByName, /* 0.8.0 */
    .domainHasCurrentSnapshot = remoteDomainHasCurrentSnapshot, /* 0.8.0 */
8664
    .domainSnapshotGetParent = remoteDomainSnapshotGetParent, /* 0.9.7 */
8665 8666
    .domainSnapshotCurrent = remoteDomainSnapshotCurrent, /* 0.8.0 */
    .domainRevertToSnapshot = remoteDomainRevertToSnapshot, /* 0.8.0 */
E
Eric Blake 已提交
8667 8668
    .domainSnapshotIsCurrent = remoteDomainSnapshotIsCurrent, /* 0.9.13 */
    .domainSnapshotHasMetadata = remoteDomainSnapshotHasMetadata, /* 0.9.13 */
8669
    .domainSnapshotDelete = remoteDomainSnapshotDelete, /* 0.8.0 */
8670 8671 8672
    .domainQemuMonitorCommand = remoteDomainQemuMonitorCommand, /* 0.8.3 */
    .domainQemuAttach = remoteDomainQemuAttach, /* 0.9.4 */
    .domainQemuAgentCommand = remoteDomainQemuAgentCommand, /* 0.10.0 */
8673 8674
    .connectDomainQemuMonitorEventRegister = remoteConnectDomainQemuMonitorEventRegister, /* 1.2.3 */
    .connectDomainQemuMonitorEventDeregister = remoteConnectDomainQemuMonitorEventDeregister, /* 1.2.3 */
8675
    .domainOpenConsole = remoteDomainOpenConsole, /* 0.8.6 */
8676
    .domainOpenChannel = remoteDomainOpenChannel, /* 1.0.2 */
8677
    .domainOpenGraphics = remoteDomainOpenGraphics, /* 0.9.7 */
8678
    .domainOpenGraphicsFD = remoteDomainOpenGraphicsFD, /* 1.2.8 */
8679
    .domainInjectNMI = remoteDomainInjectNMI, /* 0.9.2 */
8680 8681 8682 8683 8684 8685
    .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 */
8686
    .domainSendKey = remoteDomainSendKey, /* 0.9.3 */
8687
    .domainSendProcessSignal = remoteDomainSendProcessSignal, /* 1.0.1 */
8688 8689 8690 8691
    .domainBlockJobAbort = remoteDomainBlockJobAbort, /* 0.9.4 */
    .domainGetBlockJobInfo = remoteDomainGetBlockJobInfo, /* 0.9.4 */
    .domainBlockJobSetSpeed = remoteDomainBlockJobSetSpeed, /* 0.9.4 */
    .domainBlockPull = remoteDomainBlockPull, /* 0.9.4 */
8692
    .domainBlockRebase = remoteDomainBlockRebase, /* 0.9.10 */
8693
    .domainBlockCopy = remoteDomainBlockCopy, /* 1.2.9 */
8694
    .domainBlockCommit = remoteDomainBlockCommit, /* 0.10.2 */
8695 8696
    .connectSetKeepAlive = remoteConnectSetKeepAlive, /* 0.9.8 */
    .connectIsAlive = remoteConnectIsAlive, /* 0.9.8 */
8697
    .nodeSuspendForDuration = remoteNodeSuspendForDuration, /* 0.9.8 */
8698 8699
    .domainSetBlockIoTune = remoteDomainSetBlockIoTune, /* 0.9.8 */
    .domainGetBlockIoTune = remoteDomainGetBlockIoTune, /* 0.9.8 */
8700 8701
    .domainSetNumaParameters = remoteDomainSetNumaParameters, /* 0.9.9 */
    .domainGetNumaParameters = remoteDomainGetNumaParameters, /* 0.9.9 */
8702
    .domainGetCPUStats = remoteDomainGetCPUStats, /* 0.9.10 */
8703
    .domainGetDiskErrors = remoteDomainGetDiskErrors, /* 0.9.10 */
8704 8705
    .domainSetMetadata = remoteDomainSetMetadata, /* 0.9.10 */
    .domainGetMetadata = remoteDomainGetMetadata, /* 0.9.10 */
8706
    .domainGetHostname = remoteDomainGetHostname, /* 0.10.0 */
8707 8708
    .nodeSetMemoryParameters = remoteNodeSetMemoryParameters, /* 0.10.2 */
    .nodeGetMemoryParameters = remoteNodeGetMemoryParameters, /* 0.10.2 */
8709
    .nodeGetCPUMap = remoteNodeGetCPUMap, /* 1.0.0 */
8710
    .domainFSTrim = remoteDomainFSTrim, /* 1.0.1 */
8711
    .domainLxcOpenNamespace = remoteDomainLxcOpenNamespace, /* 1.0.2 */
8712 8713 8714 8715 8716 8717
    .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 */
8718
    .connectGetCPUModelNames = remoteConnectGetCPUModelNames, /* 1.1.3 */
8719 8720
    .domainFSFreeze = remoteDomainFSFreeze, /* 1.2.5 */
    .domainFSThaw = remoteDomainFSThaw, /* 1.2.5 */
8721 8722
    .domainGetTime = remoteDomainGetTime, /* 1.2.5 */
    .domainSetTime = remoteDomainSetTime, /* 1.2.5 */
M
Michal Privoznik 已提交
8723
    .nodeGetFreePages = remoteNodeGetFreePages, /* 1.2.6 */
8724
    .connectGetDomainCapabilities = remoteConnectGetDomainCapabilities, /* 1.2.7 */
8725
    .connectGetAllDomainStats = remoteConnectGetAllDomainStats, /* 1.2.8 */
M
Michal Privoznik 已提交
8726
    .nodeAllocPages = remoteNodeAllocPages, /* 1.2.9 */
8727
    .domainGetFSInfo = remoteDomainGetFSInfo, /* 1.2.11 */
8728
    .domainInterfaceAddresses = remoteDomainInterfaceAddresses, /* 1.2.14 */
8729
    .domainSetUserPassword = remoteDomainSetUserPassword, /* 1.2.16 */
T
Tomas Meszaros 已提交
8730
    .domainRename = remoteDomainRename, /* 1.2.19 */
8731 8732
    .connectRegisterCloseCallback = remoteConnectRegisterCloseCallback, /* 1.3.2 */
    .connectUnregisterCloseCallback = remoteConnectUnregisterCloseCallback, /* 1.3.2 */
8733
    .domainMigrateStartPostCopy = remoteDomainMigrateStartPostCopy, /* 1.3.3 */
8734
    .domainGetGuestVcpus = remoteDomainGetGuestVcpus, /* 2.0.0 */
8735
    .domainSetGuestVcpus = remoteDomainSetGuestVcpus, /* 2.0.0 */
8736
    .domainSetVcpu = remoteDomainSetVcpu, /* 3.1.0 */
8737
    .domainSetBlockThreshold = remoteDomainSetBlockThreshold, /* 3.2.0 */
8738 8739
    .domainSetLifecycleAction = remoteDomainSetLifecycleAction, /* 3.9.0 */
    .connectCompareHypervisorCPU = remoteConnectCompareHypervisorCPU, /* 4.4.0 */
8740
    .connectBaselineHypervisorCPU = remoteConnectBaselineHypervisorCPU, /* 4.4.0 */
8741
    .nodeGetSEVInfo = remoteNodeGetSEVInfo, /* 4.5.0 */
8742 8743 8744 8745 8746 8747 8748 8749
    .domainGetLaunchSecurityInfo = remoteDomainGetLaunchSecurityInfo, /* 4.5.0 */
    .domainCheckpointCreateXML = remoteDomainCheckpointCreateXML, /* 5.6.0 */
    .domainCheckpointGetXMLDesc = remoteDomainCheckpointGetXMLDesc, /* 5.6.0 */
    .domainListAllCheckpoints = remoteDomainListAllCheckpoints, /* 5.6.0 */
    .domainCheckpointListAllChildren = remoteDomainCheckpointListAllChildren, /* 5.6.0 */
    .domainCheckpointLookupByName = remoteDomainCheckpointLookupByName, /* 5.6.0 */
    .domainCheckpointGetParent = remoteDomainCheckpointGetParent, /* 5.6.0 */
    .domainCheckpointDelete = remoteDomainCheckpointDelete, /* 5.6.0 */
8750
    .domainGetGuestInfo = remoteDomainGetGuestInfo, /* 5.7.0 */
8751 8752 8753
};

static virNetworkDriver network_driver = {
8754 8755 8756 8757 8758
    .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 */
8759 8760
    .connectNetworkEventDeregisterAny = remoteConnectNetworkEventDeregisterAny, /* 1.2.1 */
    .connectNetworkEventRegisterAny = remoteConnectNetworkEventRegisterAny, /* 1.2.1 */
8761 8762 8763 8764 8765
    .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 */
8766
    .networkUpdate = remoteNetworkUpdate, /* 0.10.2 */
8767 8768 8769 8770 8771 8772 8773 8774
    .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 */
8775
    .networkGetDHCPLeases = remoteNetworkGetDHCPLeases, /* 1.2.6 */
8776 8777 8778 8779 8780 8781 8782
    .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 */
8783 8784
};

D
Daniel Veillard 已提交
8785
static virInterfaceDriver interface_driver = {
8786 8787 8788 8789 8790
    .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 */
8791 8792 8793 8794 8795 8796 8797 8798
    .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 */
8799 8800 8801
    .interfaceChangeBegin = remoteInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = remoteInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = remoteInterfaceChangeRollback, /* 0.9.2 */
D
Daniel Veillard 已提交
8802 8803
};

8804
static virStorageDriver storage_driver = {
8805 8806 8807 8808 8809 8810
    .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 */
8811 8812
    .connectStoragePoolEventDeregisterAny = remoteConnectStoragePoolEventDeregisterAny, /* 2.0.0 */
    .connectStoragePoolEventRegisterAny = remoteConnectStoragePoolEventRegisterAny, /* 2.0.0 */
8813
    .connectGetStoragePoolCapabilities = remoteConnectGetStoragePoolCapabilities, /* 5.2.0 */
8814 8815 8816
    .storagePoolLookupByName = remoteStoragePoolLookupByName, /* 0.4.1 */
    .storagePoolLookupByUUID = remoteStoragePoolLookupByUUID, /* 0.4.1 */
    .storagePoolLookupByVolume = remoteStoragePoolLookupByVolume, /* 0.4.1 */
8817
    .storagePoolLookupByTargetPath = remoteStoragePoolLookupByTargetPath, /* 4.1.0 */
8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844
    .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 */
8845
    .storageVolGetInfoFlags = remoteStorageVolGetInfoFlags, /* 3.0.0 */
8846 8847 8848 8849 8850
    .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 */
8851 8852
};

8853
static virSecretDriver secret_driver = {
8854 8855 8856 8857 8858 8859 8860 8861 8862
    .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 */
8863 8864 8865
    .secretUndefine = remoteSecretUndefine, /* 0.7.1 */
    .connectSecretEventDeregisterAny = remoteConnectSecretEventDeregisterAny, /* 3.0.0 */
    .connectSecretEventRegisterAny = remoteConnectSecretEventRegisterAny, /* 3.0.0 */
8866 8867
};

8868
static virNodeDeviceDriver node_device_driver = {
8869 8870
    .connectNodeDeviceEventDeregisterAny = remoteConnectNodeDeviceEventDeregisterAny, /* 2.2.0 */
    .connectNodeDeviceEventRegisterAny = remoteConnectNodeDeviceEventRegisterAny, /* 2.2.0 */
8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881
    .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 */
8882 8883
};

8884
static virNWFilterDriver nwfilter_driver = {
8885 8886
    .nwfilterLookupByUUID = remoteNWFilterLookupByUUID, /* 0.8.0 */
    .nwfilterLookupByName = remoteNWFilterLookupByName, /* 0.8.0 */
8887 8888 8889 8890 8891 8892
    .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 */
8893 8894 8895 8896 8897
    .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 */
8898 8899
};

8900 8901 8902 8903 8904 8905 8906 8907 8908
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,
};
8909

8910
static virStateDriver state_driver = {
8911
    .name = "Remote",
8912
    .stateInitialize = remoteStateInitialize,
8913 8914 8915
};


8916
/** remoteRegister:
8917 8918
 *
 * Register driver with libvirt driver system.
8919 8920
 *
 * Returns -1 on error.
8921 8922
 */
int
8923
remoteRegister(void)
8924
{
8925 8926
    if (virRegisterConnectDriver(&connect_driver,
                                 false) < 0)
8927
        return -1;
8928
    if (virRegisterStateDriver(&state_driver) < 0)
8929
        return -1;
8930 8931 8932

    return 0;
}