remote_driver.c 277.2 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-2014 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 23
 *
 * Author: Richard Jones <rjones@redhat.com>
 */

24
#include <config.h>
25

26 27
#include <unistd.h>
#include <assert.h>
28

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

53 54
#define VIR_FROM_THIS VIR_FROM_REMOTE

55 56
VIR_LOG_INIT("remote.remote_driver");

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

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

75 76 77 78 79
#define remoteDeserializeTypedParameters(ret_params_val, ret_params_len,      \
                                         limit, params, nparams)              \
    deserializeTypedParameters(__FUNCTION__, ret_params_val, ret_params_len,  \
                               limit, params, nparams)

80
static bool inside_daemon;
81

82 83
struct private_data {
    virMutex lock;
84

85 86 87
    virNetClientPtr client;
    virNetClientProgramPtr remoteProgram;
    virNetClientProgramPtr qemuProgram;
88
    virNetClientProgramPtr lxcProgram;
89

90
    int counter; /* Serial number for RPC */
91

92
#ifdef WITH_GNUTLS
93
    virNetTLSContextPtr tls;
94
#endif
95

96
    int is_secure;              /* Secure if TLS or SASL or UNIX sockets */
97
    char *type;                 /* Cached return from remoteType. */
98
    int localUses;              /* Ref count for private data */
99
    char *hostname;             /* Original hostname */
100
    bool serverKeepAlive;       /* Does server support keepalive protocol? */
101
    bool serverEventFilter;     /* Does server support modern event filtering */
102

103
    virObjectEventStatePtr eventState;
104 105
};

106
enum {
107
    REMOTE_CALL_QEMU              = (1 << 0),
108
    REMOTE_CALL_LXC               = (1 << 1),
109 110 111
};


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

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

122 123 124 125
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);
126 127 128 129 130 131 132
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);
133 134
static int remoteAuthenticate(virConnectPtr conn, struct private_data *priv,
                              virConnectAuthPtr auth, const char *authtype);
135
#if WITH_SASL
136 137
static int remoteAuthSASL(virConnectPtr conn, struct private_data *priv,
                          virConnectAuthPtr auth, const char *mech);
138
#endif
139
#if WITH_POLKIT
140 141
static int remoteAuthPolkit(virConnectPtr conn, struct private_data *priv,
                            virConnectAuthPtr auth);
142
#endif /* WITH_POLKIT */
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static virDomainPtr get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network(virConnectPtr conn, remote_nonnull_network network);
static virNWFilterPtr get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter);
static virInterfacePtr get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface);
static virStoragePoolPtr get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool);
static virStorageVolPtr get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol);
static virNodeDevicePtr get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev);
static virSecretPtr get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret);
static virDomainSnapshotPtr get_nonnull_domain_snapshot(virDomainPtr domain, remote_nonnull_domain_snapshot snapshot);
static void make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src);
static void make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src);
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);
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);
static void make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src);
161

162 163 164
/*----------------------------------------------------------------------*/

/* Helper functions for remoteOpen. */
165
static char *get_transport_from_scheme(char *scheme);
166

A
Atsushi SAKAI 已提交
167
#ifdef WITH_LIBVIRTD
168
static int
169 170 171
remoteStateInitialize(bool privileged ATTRIBUTE_UNUSED,
                      virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                      void *opaque ATTRIBUTE_UNUSED)
172 173 174 175
{
    /* Mark that we're inside the daemon so we can avoid
     * re-entering ourselves
     */
176
    inside_daemon = true;
177 178
    return 0;
}
A
Atsushi SAKAI 已提交
179
#endif
180

181

182 183 184 185 186
static void
remoteDomainBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque);
static void
187 188 189 190 191
remoteDomainBuildEventCallbackLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                        virNetClientPtr client ATTRIBUTE_UNUSED,
                                        void *evdata, void *opaque);

static void
192 193 194 195
remoteDomainBuildEventReboot(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                             virNetClientPtr client ATTRIBUTE_UNUSED,
                             void *evdata, void *opaque);
static void
196 197 198 199 200
remoteDomainBuildEventCallbackReboot(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                     virNetClientPtr client ATTRIBUTE_UNUSED,
                                     void *evdata, void *opaque);

static void
201 202 203 204
remoteDomainBuildEventRTCChange(virNetClientProgramPtr prog,
                                virNetClientPtr client,
                                void *evdata, void *opaque);
static void
205 206 207 208 209
remoteDomainBuildEventCallbackRTCChange(virNetClientProgramPtr prog,
                                        virNetClientPtr client,
                                        void *evdata, void *opaque);

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

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

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

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

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

254 255 256 257
static void
remoteDomainBuildEventBlockJob(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
258 259 260 261 262
static void
remoteDomainBuildEventCallbackBlockJob(virNetClientProgramPtr prog,
                                       virNetClientPtr client,
                                       void *evdata, void *opaque);

263

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

273 274 275 276
static void
remoteDomainBuildEventTrayChange(virNetClientProgramPtr prog,
                                 virNetClientPtr client,
                                 void *evdata, void *opaque);
277 278 279 280
static void
remoteDomainBuildEventCallbackTrayChange(virNetClientProgramPtr prog,
                                         virNetClientPtr client,
                                         void *evdata, void *opaque);
281

O
Osier Yang 已提交
282 283 284 285
static void
remoteDomainBuildEventPMWakeup(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
286 287 288 289
static void
remoteDomainBuildEventCallbackPMWakeup(virNetClientProgramPtr prog,
                                       virNetClientPtr client,
                                       void *evdata, void *opaque);
O
Osier Yang 已提交
290

O
Osier Yang 已提交
291 292 293 294
static void
remoteDomainBuildEventPMSuspend(virNetClientProgramPtr prog,
                                virNetClientPtr client,
                                void *evdata, void *opaque);
295
static void
296 297 298 299 300
remoteDomainBuildEventCallbackPMSuspend(virNetClientProgramPtr prog,
                                        virNetClientPtr client,
                                        void *evdata, void *opaque);

static void
301 302 303
remoteDomainBuildEventBalloonChange(virNetClientProgramPtr prog,
                                    virNetClientPtr client,
                                    void *evdata, void *opaque);
304
static void
305 306 307 308 309
remoteDomainBuildEventCallbackBalloonChange(virNetClientProgramPtr prog,
                                            virNetClientPtr client,
                                            void *evdata, void *opaque);

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

318 319 320 321
static void
remoteDomainBuildEventDeviceRemoved(virNetClientProgramPtr prog,
                                    virNetClientPtr client,
                                    void *evdata, void *opaque);
322 323 324 325
static void
remoteDomainBuildEventCallbackDeviceRemoved(virNetClientProgramPtr prog,
                                            virNetClientPtr client,
                                            void *evdata, void *opaque);
326

327 328 329 330 331
static void
remoteDomainBuildEventBlockJob2(virNetClientProgramPtr prog,
                                virNetClientPtr client,
                                void *evdata, void *opaque);

332 333 334 335 336
static void
remoteDomainBuildEventCallbackTunable(virNetClientProgramPtr prog,
                                      virNetClientPtr client,
                                      void *evdata, void *opaque);

337 338 339 340 341
static void
remoteDomainBuildEventCallbackAgentLifecycle(virNetClientProgramPtr prog,
                                             virNetClientPtr client,
                                             void *evdata, void *opaque);

342 343 344 345 346
static void
remoteNetworkBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque);

347
static virNetClientProgramEvent remoteEvents[] = {
348 349 350 351
    { REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
      remoteDomainBuildEventLifecycle,
      sizeof(remote_domain_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_domain_event_lifecycle_msg },
352 353 354 355 356 357 358 359
    { 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 },
360 361 362 363 364 365 366 367 368 369 370 371
    { 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 },
372 373 374 375
    { 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 },
376 377 378 379
    { REMOTE_PROC_DOMAIN_EVENT_CONTROL_ERROR,
      remoteDomainBuildEventControlError,
      sizeof(remote_domain_event_control_error_msg),
      (xdrproc_t)xdr_remote_domain_event_control_error_msg },
380 381 382 383
    { REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB,
      remoteDomainBuildEventBlockJob,
      sizeof(remote_domain_event_block_job_msg),
      (xdrproc_t)xdr_remote_domain_event_block_job_msg },
384 385 386 387
    { REMOTE_PROC_DOMAIN_EVENT_DISK_CHANGE,
      remoteDomainBuildEventDiskChange,
      sizeof(remote_domain_event_disk_change_msg),
      (xdrproc_t)xdr_remote_domain_event_disk_change_msg },
388 389 390 391
    { 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 已提交
392 393 394 395
    { REMOTE_PROC_DOMAIN_EVENT_PMWAKEUP,
      remoteDomainBuildEventPMWakeup,
      sizeof(remote_domain_event_pmwakeup_msg),
      (xdrproc_t)xdr_remote_domain_event_pmwakeup_msg },
O
Osier Yang 已提交
396 397 398 399
    { REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND,
      remoteDomainBuildEventPMSuspend,
      sizeof(remote_domain_event_pmsuspend_msg),
      (xdrproc_t)xdr_remote_domain_event_pmsuspend_msg },
400 401 402 403
    { REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE,
      remoteDomainBuildEventBalloonChange,
      sizeof(remote_domain_event_balloon_change_msg),
      (xdrproc_t)xdr_remote_domain_event_balloon_change_msg },
404 405 406 407
    { REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND_DISK,
      remoteDomainBuildEventPMSuspendDisk,
      sizeof(remote_domain_event_pmsuspend_disk_msg),
      (xdrproc_t)xdr_remote_domain_event_pmsuspend_disk_msg },
408 409 410 411
    { REMOTE_PROC_DOMAIN_EVENT_DEVICE_REMOVED,
      remoteDomainBuildEventDeviceRemoved,
      sizeof(remote_domain_event_device_removed_msg),
      (xdrproc_t)xdr_remote_domain_event_device_removed_msg },
412 413 414 415 416 417 418 419 420
    /* 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.  */
421 422 423 424
    { REMOTE_PROC_NETWORK_EVENT_LIFECYCLE,
      remoteNetworkBuildEventLifecycle,
      sizeof(remote_network_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_network_event_lifecycle_msg },
425 426 427 428
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_LIFECYCLE,
      remoteDomainBuildEventCallbackLifecycle,
      sizeof(remote_domain_event_callback_lifecycle_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_lifecycle_msg },
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    { 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 },
489 490 491 492
    { 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 },
493 494 495 496
    { REMOTE_PROC_DOMAIN_EVENT_CALLBACK_TUNABLE,
      remoteDomainBuildEventCallbackTunable,
      sizeof(remote_domain_event_callback_tunable_msg),
      (xdrproc_t)xdr_remote_domain_event_callback_tunable_msg },
497 498 499 500
    { 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 },
501
};
502

503 504 505 506 507 508 509 510 511 512 513 514 515

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

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

516
enum virDrvOpenRemoteFlags {
517
    VIR_DRV_OPEN_REMOTE_RO = (1 << 0),
518 519
    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 */
520
};
521

522

523 524 525 526
static void
remoteClientCloseFunc(virNetClientPtr client ATTRIBUTE_UNUSED,
                      int reason,
                      void *opaque)
527
{
528 529 530 531 532 533 534 535 536 537 538 539 540
    virConnectCloseCallbackDataPtr cbdata = opaque;

    virObjectLock(cbdata);

    if (cbdata->callback) {
        VIR_DEBUG("Triggering connection close callback %p reason=%d, opaque=%p",
                  cbdata->callback, reason, cbdata->opaque);
        cbdata->callback(cbdata->conn, reason, cbdata->opaque);

        if (cbdata->freeCallback)
            cbdata->freeCallback(cbdata->opaque);
        cbdata->callback = NULL;
        cbdata->freeCallback = NULL;
541
    }
542 543 544 545 546
    virObjectUnlock(cbdata);

    /* free the connection reference that comes along with the callback
     * registration */
    virObjectUnref(cbdata->conn);
547 548
}

549 550 551 552
/* helper macro to ease extraction of arguments from the URI */
#define EXTRACT_URI_ARG_STR(ARG_NAME, ARG_VAR)          \
    if (STRCASEEQ(var->name, ARG_NAME)) {               \
        VIR_FREE(ARG_VAR);                              \
553 554
        if (VIR_STRDUP(ARG_VAR, var->value) < 0)        \
            goto failed;                                \
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
        var->ignore = 1;                                \
        continue;                                       \
    }

#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;                                                           \
    }
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
/*
 * 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
588 589
 *   - xxx+ssh:///            -> SSH connection (legacy)
 *   - xxx+libssh2:///        -> SSH connection (using libssh2)
590
 */
591
static int
592 593 594 595
doRemoteOpen(virConnectPtr conn,
             struct private_data *priv,
             virConnectAuthPtr auth ATTRIBUTE_UNUSED,
             unsigned int flags)
596
{
597
    char *transport_str = NULL;
598 599 600 601
    enum {
        trans_tls,
        trans_unix,
        trans_ssh,
602
        trans_libssh2,
603 604 605
        trans_ext,
        trans_tcp,
    } transport;
606
#ifndef WIN32
M
Michal Privoznik 已提交
607
    char *daemonPath = NULL;
608
#endif
609

610
    /* We handle *ALL* URIs here. The caller has rejected any
611
     * URIs we don't care about */
612

613 614 615
    if (conn->uri) {
        if (!conn->uri->scheme) {
            /* This is the ///var/lib/xen/xend-socket local path style */
616 617 618 619 620
            if (!conn->uri->path)
                return VIR_DRV_OPEN_DECLINED;
            if (conn->uri->path[0] != '/')
                return VIR_DRV_OPEN_DECLINED;

621
            transport = trans_unix;
622
        } else {
623
            transport_str = get_transport_from_scheme(conn->uri->scheme);
624

625 626 627 628 629 630
            if (!transport_str) {
                if (conn->uri->server)
                    transport = trans_tls;
                else
                    transport = trans_unix;
            } else {
631
                if (STRCASEEQ(transport_str, "tls")) {
632
                    transport = trans_tls;
633
                } else if (STRCASEEQ(transport_str, "unix")) {
634
                    if (conn->uri->server) {
635 636 637 638
                        virReportError(VIR_ERR_INVALID_ARG,
                                       _("using unix socket and remote "
                                         "server '%s' is not supported."),
                                       conn->uri->server);
639 640 641 642
                        return VIR_DRV_OPEN_ERROR;
                    } else {
                        transport = trans_unix;
                    }
643
                } else if (STRCASEEQ(transport_str, "ssh")) {
644
                    transport = trans_ssh;
645
                } else if (STRCASEEQ(transport_str, "libssh2")) {
646
                    transport = trans_libssh2;
647
                } else if (STRCASEEQ(transport_str, "ext")) {
648
                    transport = trans_ext;
649
                } else if (STRCASEEQ(transport_str, "tcp")) {
650
                    transport = trans_tcp;
651
                } else {
652 653
                    virReportError(VIR_ERR_INVALID_ARG, "%s",
                                   _("remote_open: transport in URL not recognised "
654
                                     "(should be tls|unix|ssh|ext|tcp|libssh2)"));
655 656 657 658 659 660 661
                    return VIR_DRV_OPEN_ERROR;
                }
            }
        }
    } else {
        /* No URI, then must be probing so use UNIX socket */
        transport = trans_unix;
662
    }
663

664 665 666 667 668 669 670 671 672 673 674 675 676 677
    /*
     * We don't want to be executing external programs in setuid mode,
     * so this rules out 'ext' and 'ssh' transports. Exclude libssh
     * and tls too, since we're not confident the libraries are safe
     * for setuid usage. Just allow UNIX sockets, since that does
     * not require any external libraries or command execution
     */
    if (virIsSUID() &&
        transport != trans_unix) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Only Unix socket URI transport is allowed in setuid mode"));
        return VIR_DRV_OPEN_ERROR;
    }

E
Eric Blake 已提交
678
    /* Local variables which we will initialize. These can
679 680
     * get freed in the failed: path.
     */
681 682
    char *name = NULL, *command = NULL, *sockname = NULL, *netcat = NULL;
    char *port = NULL, *authtype = NULL, *username = NULL;
L
Laine Stump 已提交
683
    bool sanity = true, verify = true, tty ATTRIBUTE_UNUSED = true;
684 685 686
    char *pkipath = NULL, *keyfile = NULL, *sshauth = NULL;

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

688 689 690
    /* Return code from this function, and the private data. */
    int retcode = VIR_DRV_OPEN_ERROR;

691
    /* Remote server defaults to "localhost" if not specified. */
692
    if (conn->uri && conn->uri->port != 0) {
693
        if (virAsprintf(&port, "%d", conn->uri->port) < 0)
694
            goto failed;
695
    } else if (transport == trans_tls) {
696 697
        if (VIR_STRDUP(port, LIBVIRTD_TLS_PORT) < 0)
            goto failed;
698
    } else if (transport == trans_tcp) {
699 700
        if (VIR_STRDUP(port, LIBVIRTD_TCP_PORT) < 0)
            goto failed;
701
    } /* Port not used for unix, ext., default for ssh */
702

703 704 705 706
    if (VIR_STRDUP(priv->hostname,
                   conn->uri && conn->uri->server ?
                   conn->uri->server : "localhost") < 0)
        goto failed;
707

708 709
    if (conn->uri && VIR_STRDUP(username, conn->uri->user) < 0)
        goto failed;
710

711 712 713 714 715
    /* 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).
     */
716
    size_t i;
717

718
    if (conn->uri) {
719
        for (i = 0; i < conn->uri->paramsCount; i++) {
720
            virURIParamPtr var = &conn->uri->params[i];
721 722 723 724
            EXTRACT_URI_ARG_STR("name", name);
            EXTRACT_URI_ARG_STR("command", command);
            EXTRACT_URI_ARG_STR("socket", sockname);
            EXTRACT_URI_ARG_STR("auth", authtype);
725
            EXTRACT_URI_ARG_STR("sshauth", sshauth);
726 727 728
            EXTRACT_URI_ARG_STR("netcat", netcat);
            EXTRACT_URI_ARG_STR("keyfile", keyfile);
            EXTRACT_URI_ARG_STR("pkipath", pkipath);
729 730
            EXTRACT_URI_ARG_STR("known_hosts", knownHosts);
            EXTRACT_URI_ARG_STR("known_hosts_verify", knownHostsVerify);
731 732 733 734 735 736

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

            if (STRCASEEQ(var->name, "authfile")) {
737 738
                /* Strip this param, used by virauth.c */
                var->ignore = 1;
739
                continue;
740
            }
741 742 743

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

746 747
        /* Construct the original name. */
        if (!name) {
748 749 750
            if (conn->uri->scheme &&
                (STREQ(conn->uri->scheme, "remote") ||
                 STRPREFIX(conn->uri->scheme, "remote+"))) {
751
                /* Allow remote serve to probe */
752 753
                if (VIR_STRDUP(name, "") < 0)
                    goto failed;
754
            } else {
M
Martin Kletzander 已提交
755
                virURI tmpuri = {
756
                    .scheme = conn->uri->scheme,
757
                    .query = virURIFormatParams(conn->uri),
758 759 760 761 762 763
                    .path = conn->uri->path,
                    .fragment = conn->uri->fragment,
                };

                /* Evil, blank out transport scheme temporarily */
                if (transport_str) {
764
                    assert(transport_str[-1] == '+');
765 766
                    transport_str[-1] = '\0';
                }
767

M
Martin Kletzander 已提交
768
                name = virURIFormat(&tmpuri);
769

770 771 772 773 774
                VIR_FREE(tmpuri.query);

                /* Restore transport scheme */
                if (transport_str)
                    transport_str[-1] = '+';
775 776 777

                if (!name)
                    goto failed;
778
            }
779
        }
780 781
    } else {
        /* Probe URI server side */
782 783
        if (VIR_STRDUP(name, "") < 0)
            goto failed;
784 785
    }

786
    VIR_DEBUG("proceeding with name = %s", name);
787

788 789
    /* For ext transport, command is required. */
    if (transport == trans_ext && !command) {
790 791
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("remote_open: for 'ext' transport, command is required"));
792 793 794
        goto failed;
    }

795
    VIR_DEBUG("Connecting with transport %d", transport);
796 797 798
    /* Connect to the remote service. */
    switch (transport) {
    case trans_tls:
799
#ifdef WITH_GNUTLS
800 801
        priv->tls = virNetTLSContextNewClientPath(pkipath,
                                                  geteuid() != 0 ? true : false,
802
                                                  sanity, verify);
803 804
        if (!priv->tls)
            goto failed;
805
        priv->is_secure = 1;
806 807
#else
        (void)sanity;
808
        (void)verify;
809 810 811 812
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("GNUTLS support not available in this build"));
        goto failed;
#endif
813 814

        /*FALLTHROUGH*/
815 816 817
    case trans_tcp:
        priv->client = virNetClientNewTCP(priv->hostname, port);
        if (!priv->client)
818 819
            goto failed;

820
#ifdef WITH_GNUTLS
821 822 823 824
        if (priv->tls) {
            VIR_DEBUG("Starting TLS session");
            if (virNetClientSetTLSSession(priv->client, priv->tls) < 0)
                goto failed;
825
        }
826
#endif
827 828 829

        break;

830
    case trans_libssh2:
831 832
        if (!sockname) {
            /* Right now we don't support default session connections */
833
            if (STREQ_NULLABLE(conn->uri->path, "/session")) {
834 835 836 837 838 839 840 841 842 843 844 845
                virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                               _("Connecting to session instance without "
                                 "socket path is not supported by the libssh2 "
                                 "connection driver"));
                goto failed;
            }

            if (VIR_STRDUP(sockname,
                           flags & VIR_DRV_OPEN_REMOTE_RO ?
                           LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
                goto failed;
        }
846 847 848 849 850 851 852 853 854 855 856 857

        VIR_DEBUG("Starting LibSSH2 session");

        priv->client = virNetClientNewLibSSH2(priv->hostname,
                                              port,
                                              username,
                                              keyfile,
                                              knownHosts,
                                              knownHostsVerify,
                                              sshauth,
                                              netcat,
                                              sockname,
858 859
                                              auth,
                                              conn->uri);
860 861 862 863 864 865
        if (!priv->client)
            goto failed;

        priv->is_secure = 1;
        break;

866
#ifndef WIN32
867
    case trans_unix:
868
        if (!sockname) {
869
            if (flags & VIR_DRV_OPEN_REMOTE_USER) {
870
                char *userdir = virGetUserRuntimeDirectory();
871

872
                if (!userdir)
873
                    goto failed;
874

875
                if (virAsprintf(&sockname, "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) {
876
                    VIR_FREE(userdir);
877
                    goto failed;
878 879
                }
                VIR_FREE(userdir);
880
            } else {
881 882 883 884
                if (VIR_STRDUP(sockname,
                               flags & VIR_DRV_OPEN_REMOTE_RO ?
                               LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
                    goto failed;
885
            }
886
            VIR_DEBUG("Proceeding with sockname %s", sockname);
887 888
        }

889
        if ((flags & VIR_DRV_OPEN_REMOTE_AUTOSTART) &&
890 891 892 893 894
            !(daemonPath = virFileFindResourceFull("libvirtd",
                                                   NULL, NULL,
                                                   "daemon",
                                                   SBINDIR,
                                                   "LIBVIRTD_PATH")))
895
            goto failed;
896

897 898
        if (!(priv->client = virNetClientNewUNIX(sockname,
                                                 flags & VIR_DRV_OPEN_REMOTE_AUTOSTART,
899
                                                 daemonPath)))
C
Chris Lalancette 已提交
900
            goto failed;
901

902
        priv->is_secure = 1;
903 904
        break;

905
    case trans_ssh:
906 907
        if (!command && VIR_STRDUP(command, "ssh") < 0)
            goto failed;
J
Jim Meyering 已提交
908

909 910
        if (!sockname) {
            /* Right now we don't support default session connections */
911
            if (STREQ_NULLABLE(conn->uri->path, "/session")) {
912 913 914 915 916 917 918 919 920 921 922 923
                virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                               _("Connecting to session instance without "
                                 "socket path is not supported by the ssh "
                                 "connection driver"));
                goto failed;
            }

            if (VIR_STRDUP(sockname,
                           flags & VIR_DRV_OPEN_REMOTE_RO ?
                           LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
                goto failed;
        }
924 925 926 927 928

        if (!(priv->client = virNetClientNewSSH(priv->hostname,
                                                port,
                                                command,
                                                username,
929 930
                                                !tty,
                                                !verify,
931
                                                netcat ? netcat : "nc",
932
                                                keyfile,
933 934
                                                sockname)))
            goto failed;
935 936

        priv->is_secure = 1;
937
        break;
938 939

    case trans_ext: {
940 941
        char const *cmd_argv[] = { command, NULL };
        if (!(priv->client = virNetClientNewExternal(cmd_argv)))
942 943
            goto failed;

E
Eric Blake 已提交
944
        /* Do not set 'is_secure' flag since we can't guarantee
945 946
         * an external program is secure, and this flag must be
         * pessimistic */
947 948
    }   break;

949 950 951 952 953
#else /* WIN32 */

    case trans_unix:
    case trans_ssh:
    case trans_ext:
954 955 956
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("transport methods unix, ssh and ext are not supported "
                         "under Windows"));
957
        goto failed;
958 959

#endif /* WIN32 */
960 961
    } /* switch (transport) */

962

963 964 965 966 967 968 969 970 971
    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;
    }

972 973
    virObjectRef(conn->closeCallback);

974 975
    virNetClientSetCloseCallback(priv->client,
                                 remoteClientCloseFunc,
976
                                 conn->closeCallback, virObjectFreeCallback);
977

978 979
    if (!(priv->remoteProgram = virNetClientProgramNew(REMOTE_PROGRAM,
                                                       REMOTE_PROTOCOL_VERSION,
980 981
                                                       remoteEvents,
                                                       ARRAY_CARDINALITY(remoteEvents),
982
                                                       conn)))
983
        goto failed;
984 985 986 987 988 989
    if (!(priv->lxcProgram = virNetClientProgramNew(LXC_PROGRAM,
                                                    LXC_PROTOCOL_VERSION,
                                                    NULL,
                                                    0,
                                                    NULL)))
        goto failed;
990 991
    if (!(priv->qemuProgram = virNetClientProgramNew(QEMU_PROGRAM,
                                                     QEMU_PROTOCOL_VERSION,
992 993 994
                                                     qemuEvents,
                                                     ARRAY_CARDINALITY(qemuEvents),
                                                     conn)))
995 996
        goto failed;

997
    if (virNetClientAddProgram(priv->client, priv->remoteProgram) < 0 ||
998
        virNetClientAddProgram(priv->client, priv->lxcProgram) < 0 ||
999
        virNetClientAddProgram(priv->client, priv->qemuProgram) < 0)
1000
        goto failed;
1001 1002

    /* Try and authenticate with server */
1003 1004
    VIR_DEBUG("Trying authentication");
    if (remoteAuthenticate(conn, priv, auth, authtype) == -1)
1005 1006
        goto failed;

1007
    if (virNetClientKeepAliveIsSupported(priv->client)) {
1008
        remote_connect_supports_feature_args args =
1009
            { VIR_DRV_FEATURE_PROGRAM_KEEPALIVE };
1010
        remote_connect_supports_feature_ret ret = { 0 };
1011 1012
        int rc;

1013 1014 1015
        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);
1016

1017
        if (rc != -1 && ret.supported) {
1018 1019 1020 1021 1022 1023 1024
            priv->serverKeepAlive = true;
        } else {
            VIR_INFO("Disabling keepalive protocol since it is not supported"
                     " by the server");
        }
    }

1025
    /* Finally we can call the remote side's open function. */
1026
    {
1027
        remote_connect_open_args args = { &name, flags };
1028

1029
        VIR_DEBUG("Trying to open URI %s", name);
1030 1031
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_OPEN,
                 (xdrproc_t) xdr_remote_connect_open_args, (char *) &args,
1032
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
1033 1034
            goto failed;
    }
1035

1036 1037
    /* Now try and find out what URI the daemon used */
    if (conn->uri == NULL) {
1038
        remote_connect_get_uri_ret uriret;
1039

1040
        VIR_DEBUG("Trying to query remote URI");
1041 1042
        memset(&uriret, 0, sizeof(uriret));
        if (call(conn, priv, 0,
1043
                 REMOTE_PROC_CONNECT_GET_URI,
1044
                 (xdrproc_t) xdr_void, (char *) NULL,
1045
                 (xdrproc_t) xdr_remote_connect_get_uri_ret, (char *) &uriret) < 0)
1046 1047
            goto failed;

1048
        VIR_DEBUG("Auto-probed URI is %s", uriret.uri);
M
Martin Kletzander 已提交
1049
        conn->uri = virURIParse(uriret.uri);
1050
        VIR_FREE(uriret.uri);
1051
        if (!conn->uri)
1052 1053 1054
            goto failed;
    }

1055
    /* Set up events */
1056
    if (!(priv->eventState = virObjectEventStateNew()))
1057
        goto failed;
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
    {
        remote_connect_supports_feature_args args =
            { VIR_DRV_FEATURE_REMOTE_EVENT_CALLBACK };
        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);

        if (rc != -1 && ret.supported) {
            priv->serverEventFilter = true;
        } else {
            VIR_INFO("Avoiding server event filtering since it is not "
                     "supported by the server");
        }
    }
1075

1076 1077 1078
    /* Successful. */
    retcode = VIR_DRV_OPEN_SUCCESS;

1079
 cleanup:
1080
    /* Free up the URL and strings. */
1081 1082 1083 1084 1085
    VIR_FREE(name);
    VIR_FREE(command);
    VIR_FREE(sockname);
    VIR_FREE(authtype);
    VIR_FREE(netcat);
1086
    VIR_FREE(sshauth);
1087
    VIR_FREE(keyfile);
1088 1089
    VIR_FREE(username);
    VIR_FREE(port);
1090
    VIR_FREE(pkipath);
1091 1092
    VIR_FREE(knownHostsVerify);
    VIR_FREE(knownHosts);
1093 1094 1095
#ifndef WIN32
    VIR_FREE(daemonPath);
#endif
1096 1097

    return retcode;
1098 1099

 failed:
1100
    virObjectUnref(priv->remoteProgram);
1101
    virObjectUnref(priv->lxcProgram);
1102
    virObjectUnref(priv->qemuProgram);
1103
    virNetClientClose(priv->client);
1104
    virObjectUnref(priv->client);
1105
    priv->client = NULL;
1106 1107 1108 1109
#ifdef WITH_GNUTLS
    virObjectUnref(priv->tls);
    priv->tls = NULL;
#endif
1110

1111
    VIR_FREE(priv->hostname);
1112
    goto cleanup;
1113
}
1114 1115
#undef EXTRACT_URI_ARG_STR
#undef EXTRACT_URI_ARG_BOOL
1116

1117
static struct private_data *
1118
remoteAllocPrivateData(void)
1119
{
1120
    struct private_data *priv;
1121
    if (VIR_ALLOC(priv) < 0)
1122
        return NULL;
1123

1124
    if (virMutexInit(&priv->lock) < 0) {
1125 1126
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot initialize mutex"));
1127
        VIR_FREE(priv);
1128
        return NULL;
1129 1130 1131
    }
    remoteDriverLock(priv);
    priv->localUses = 1;
1132 1133 1134 1135 1136

    return priv;
}

static virDrvOpenStatus
1137 1138 1139
remoteConnectOpen(virConnectPtr conn,
                  virConnectAuthPtr auth,
                  unsigned int flags)
1140 1141 1142
{
    struct private_data *priv;
    int ret, rflags = 0;
1143
    const char *autostart = virGetEnvBlockSUID("LIBVIRT_AUTOSTART");
1144

1145
    if (inside_daemon && (!conn->uri || (conn->uri && !conn->uri->server)))
1146 1147
        return VIR_DRV_OPEN_DECLINED;

1148
    if (!(priv = remoteAllocPrivateData()))
1149
        return VIR_DRV_OPEN_ERROR;
1150

1151
    if (flags & VIR_CONNECT_RO)
1152 1153
        rflags |= VIR_DRV_OPEN_REMOTE_RO;

1154 1155 1156 1157 1158 1159 1160 1161 1162
    /*
     * If no servername is given, and no +XXX
     * transport is listed, or transport is unix,
     * and path is /session, and uid is unprivileged
     * then auto-spawn a daemon.
     */
    if (conn->uri &&
        !conn->uri->server &&
        conn->uri->path &&
D
Daniel P. Berrange 已提交
1163
        conn->uri->scheme &&
1164 1165
        ((strchr(conn->uri->scheme, '+') == 0)||
         (strstr(conn->uri->scheme, "+unix") != NULL)) &&
1166 1167
        (STREQ(conn->uri->path, "/session") ||
         STRPREFIX(conn->uri->scheme, "test+")) &&
1168
        geteuid() > 0) {
1169
        VIR_DEBUG("Auto-spawn user daemon instance");
1170
        rflags |= VIR_DRV_OPEN_REMOTE_USER;
1171 1172 1173
        if (!virIsSUID() &&
            (!autostart ||
             STRNEQ(autostart, "0")))
1174
            rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1175 1176 1177
    }

    /*
J
John Levon 已提交
1178 1179 1180 1181
     * If URI is NULL, then do a UNIX connection possibly auto-spawning
     * unprivileged server and probe remote server for URI. On Solaris,
     * this isn't supported, but we may be privileged enough to connect
     * to the UNIX socket anyway.
1182 1183
     */
    if (!conn->uri) {
1184
        VIR_DEBUG("Auto-probe remote URI");
J
John Levon 已提交
1185
#ifndef __sun
1186
        if (geteuid() > 0) {
1187
            VIR_DEBUG("Auto-spawn user daemon instance");
1188
            rflags |= VIR_DRV_OPEN_REMOTE_USER;
1189 1190 1191
            if (!virIsSUID() &&
                (!autostart ||
                 STRNEQ(autostart, "0")))
1192
                rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1193
        }
J
John Levon 已提交
1194
#endif
1195
    }
1196

1197
    ret = doRemoteOpen(conn, priv, auth, rflags);
1198 1199
    if (ret != VIR_DRV_OPEN_SUCCESS) {
        conn->privateData = NULL;
1200
        remoteDriverUnlock(priv);
1201
        VIR_FREE(priv);
1202 1203
    } else {
        conn->privateData = priv;
1204
        remoteDriverUnlock(priv);
1205 1206 1207 1208 1209
    }
    return ret;
}


1210 1211
/* In a string "driver+transport" return a pointer to "transport". */
static char *
1212
get_transport_from_scheme(char *scheme)
1213
{
1214 1215
    char *p = strchr(scheme, '+');
    return p ? p + 1 : NULL;
1216 1217
}

1218
/*----------------------------------------------------------------------*/
1219

1220 1221

static int
1222
doRemoteClose(virConnectPtr conn, struct private_data *priv)
1223
{
1224 1225
    int ret = 0;

1226
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_CLOSE,
1227 1228
             (xdrproc_t) xdr_void, (char *) NULL,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
1229
        ret = -1;
1230

1231
#ifdef WITH_GNUTLS
1232
    virObjectUnref(priv->tls);
1233
    priv->tls = NULL;
1234
#endif
1235

1236 1237
    virNetClientSetCloseCallback(priv->client,
                                 NULL,
1238 1239
                                 conn->closeCallback, virObjectFreeCallback);

1240
    virNetClientClose(priv->client);
1241
    virObjectUnref(priv->client);
1242
    priv->client = NULL;
1243
    virObjectUnref(priv->remoteProgram);
1244
    virObjectUnref(priv->lxcProgram);
1245
    virObjectUnref(priv->qemuProgram);
1246
    priv->remoteProgram = priv->qemuProgram = priv->lxcProgram = NULL;
1247 1248 1249 1250 1251 1252 1253

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

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

1254 1255
    virObjectEventStateFree(priv->eventState);
    priv->eventState = NULL;
1256

1257
    return ret;
1258 1259
}

1260
static int
1261
remoteConnectClose(virConnectPtr conn)
1262
{
1263 1264
    int ret = 0;
    struct private_data *priv = conn->privateData;
1265

1266 1267 1268 1269 1270 1271 1272
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        ret = doRemoteClose(conn, priv);
        conn->privateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
1273
        VIR_FREE(priv);
1274
    }
1275 1276
    if (priv)
        remoteDriverUnlock(priv);
1277

1278 1279
    return ret;
}
1280

1281

1282 1283 1284 1285 1286 1287 1288 1289 1290
/* 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 *
1291
remoteConnectGetType(virConnectPtr conn)
1292 1293
{
    char *rv = NULL;
1294
    remote_connect_get_type_ret ret;
1295
    struct private_data *priv = conn->privateData;
1296

1297
    remoteDriverLock(priv);
1298

1299 1300 1301 1302 1303
    /* Cached? */
    if (priv->type) {
        rv = priv->type;
        goto done;
    }
1304

1305
    memset(&ret, 0, sizeof(ret));
1306
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_GET_TYPE,
1307
             (xdrproc_t) xdr_void, (char *) NULL,
1308
             (xdrproc_t) xdr_remote_connect_get_type_ret, (char *) &ret) == -1)
1309
        goto done;
1310

1311 1312
    /* Stash. */
    rv = priv->type = ret.type;
1313

1314
 done:
1315 1316 1317
    remoteDriverUnlock(priv);
    return rv;
}
1318

1319
static int remoteConnectIsSecure(virConnectPtr conn)
1320 1321 1322
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
1323
    remote_connect_is_secure_ret ret;
1324
    remoteDriverLock(priv);
1325

1326
    memset(&ret, 0, sizeof(ret));
1327
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_IS_SECURE,
1328
             (xdrproc_t) xdr_void, (char *) NULL,
1329
             (xdrproc_t) xdr_remote_connect_is_secure_ret, (char *) &ret) == -1)
1330
        goto done;
1331

1332 1333 1334 1335 1336 1337 1338 1339 1340
    /* 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;
1341

1342
 done:
1343 1344 1345
    remoteDriverUnlock(priv);
    return rv;
}
1346

1347
static int remoteConnectIsEncrypted(virConnectPtr conn)
1348 1349
{
    int rv = -1;
1350
    bool encrypted;
1351
    struct private_data *priv = conn->privateData;
1352
    remote_connect_is_secure_ret ret;
1353
    remoteDriverLock(priv);
1354

1355
    memset(&ret, 0, sizeof(ret));
1356
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_IS_SECURE,
1357
             (xdrproc_t) xdr_void, (char *) NULL,
1358
             (xdrproc_t) xdr_remote_connect_is_secure_ret, (char *) &ret) == -1)
1359
        goto done;
1360

1361
    encrypted = virNetClientIsEncrypted(priv->client);
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372

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

1373
 done:
1374 1375 1376 1377
    remoteDriverUnlock(priv);
    return rv;
}

1378
static int
1379 1380 1381 1382
remoteNodeGetCPUStats(virConnectPtr conn,
                      int cpuNum,
                      virNodeCPUStatsPtr params, int *nparams,
                      unsigned int flags)
1383 1384 1385 1386
{
    int rv = -1;
    remote_node_get_cpu_stats_args args;
    remote_node_get_cpu_stats_ret ret;
1387
    size_t i;
1388 1389 1390 1391 1392 1393 1394 1395
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

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

1396 1397 1398 1399 1400 1401
    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)
1402 1403 1404 1405 1406
        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) {
1407 1408 1409
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteNodeGetCPUStats: "
                         "returned number of stats exceeds limit"));
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
        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) {
        if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) == NULL) {
1426 1427 1428
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Stats %s too big for destination"),
                           ret.params.params_val[i].field);
1429 1430 1431 1432 1433 1434 1435
            goto cleanup;
        }
        params[i].value = ret.params.params_val[i].value;
    }

    rv = 0;

1436
 cleanup:
1437
    xdr_free((xdrproc_t) xdr_remote_node_get_cpu_stats_ret, (char *) &ret);
1438
 done:
1439 1440 1441 1442
    remoteDriverUnlock(priv);
    return rv;
}

1443
static int
1444 1445 1446 1447 1448
remoteNodeGetMemoryStats(virConnectPtr conn,
                         int cellNum,
                         virNodeMemoryStatsPtr params,
                         int *nparams,
                         unsigned int flags)
1449 1450 1451 1452
{
    int rv = -1;
    remote_node_get_memory_stats_args args;
    remote_node_get_memory_stats_ret ret;
1453
    size_t i;
1454 1455 1456 1457 1458 1459 1460 1461
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

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

1462
    memset(&ret, 0, sizeof(ret));
1463 1464 1465
    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)
1466 1467 1468 1469 1470
        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) {
1471 1472 1473
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteNodeGetMemoryStats: "
                         "returned number of stats exceeds limit"));
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
        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) {
        if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) == NULL) {
1490 1491 1492
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Stats %s too big for destination"),
                           ret.params.params_val[i].field);
1493 1494 1495 1496 1497 1498 1499
            goto cleanup;
        }
        params[i].value = ret.params.params_val[i].value;
    }

    rv = 0;

1500
 cleanup:
1501
    xdr_free((xdrproc_t) xdr_remote_node_get_memory_stats_ret, (char *) &ret);
1502
 done:
1503 1504 1505 1506
    remoteDriverUnlock(priv);
    return rv;
}

1507 1508
static int
remoteNodeGetCellsFreeMemory(virConnectPtr conn,
1509 1510 1511
                             unsigned long long *freeMems,
                             int startCell,
                             int maxCells)
1512
{
1513
    int rv = -1;
1514 1515
    remote_node_get_cells_free_memory_args args;
    remote_node_get_cells_free_memory_ret ret;
1516
    size_t i;
1517
    struct private_data *priv = conn->privateData;
1518

1519 1520
    remoteDriverLock(priv);

1521
    if (maxCells > REMOTE_NODE_MAX_CELLS) {
1522 1523 1524
        virReportError(VIR_ERR_RPC,
                       _("too many NUMA cells: %d > %d"),
                       maxCells, REMOTE_NODE_MAX_CELLS);
1525
        goto done;
1526 1527 1528
    }

    args.startCell = startCell;
1529
    args.maxcells = maxCells;
1530

1531 1532 1533 1534
    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)
1535
        goto done;
1536

1537
    for (i = 0; i < ret.cells.cells_len; i++)
1538
        freeMems[i] = ret.cells.cells_val[i];
1539 1540 1541

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

1542
    rv = ret.cells.cells_len;
1543

1544
 done:
1545
    remoteDriverUnlock(priv);
1546
    return rv;
1547 1548
}

1549
static int
1550
remoteConnectListDomains(virConnectPtr conn, int *ids, int maxids)
1551
{
1552
    int rv = -1;
1553
    size_t i;
1554 1555
    remote_connect_list_domains_args args;
    remote_connect_list_domains_ret ret;
1556
    struct private_data *priv = conn->privateData;
1557

1558 1559
    remoteDriverLock(priv);

1560
    if (maxids > REMOTE_DOMAIN_LIST_MAX) {
1561
        virReportError(VIR_ERR_RPC,
1562 1563
                       _("Too many domains '%d' for limit '%d'"),
                       maxids, REMOTE_DOMAIN_LIST_MAX);
1564
        goto done;
1565 1566 1567
    }
    args.maxids = maxids;

1568
    memset(&ret, 0, sizeof(ret));
1569 1570 1571
    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)
1572
        goto done;
1573 1574

    if (ret.ids.ids_len > maxids) {
1575
        virReportError(VIR_ERR_RPC,
1576
                       _("Too many domains '%d' for limit '%d'"),
1577
                       ret.ids.ids_len, maxids);
1578
        goto cleanup;
1579 1580 1581 1582 1583
    }

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

1584 1585
    rv = ret.ids.ids_len;

1586
 cleanup:
1587
    xdr_free((xdrproc_t) xdr_remote_connect_list_domains_ret, (char *) &ret);
1588

1589
 done:
1590
    remoteDriverUnlock(priv);
1591
    return rv;
1592 1593
}

1594 1595 1596 1597 1598 1599
static int
remoteConnectListAllDomains(virConnectPtr conn,
                            virDomainPtr **domains,
                            unsigned int flags)
{
    int rv = -1;
1600
    size_t i;
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
    virDomainPtr *doms = NULL;
    remote_connect_list_all_domains_args args;
    remote_connect_list_all_domains_ret ret;

    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.need_results = !!domains;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(conn,
             priv,
             0,
             REMOTE_PROC_CONNECT_LIST_ALL_DOMAINS,
             (xdrproc_t) xdr_remote_connect_list_all_domains_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_connect_list_all_domains_ret,
             (char *) &ret) == -1)
        goto done;

1623 1624 1625 1626 1627 1628 1629
    if (ret.domains.domains_len > REMOTE_DOMAIN_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domains '%d' for limit '%d'"),
                       ret.domains.domains_len, REMOTE_DOMAIN_LIST_MAX);
        goto cleanup;
    }

1630
    if (domains) {
1631
        if (VIR_ALLOC_N(doms, ret.domains.domains_len + 1) < 0)
1632 1633 1634 1635
            goto cleanup;

        for (i = 0; i < ret.domains.domains_len; i++) {
            doms[i] = get_nonnull_domain(conn, ret.domains.domains_val[i]);
1636
            if (!doms[i])
1637 1638 1639 1640 1641 1642 1643 1644
                goto cleanup;
        }
        *domains = doms;
        doms = NULL;
    }

    rv = ret.ret;

1645
 cleanup:
1646 1647
    if (doms) {
        for (i = 0; i < ret.domains.domains_len; i++)
1648
            virObjectUnref(doms[i]);
1649 1650 1651 1652 1653
        VIR_FREE(doms);
    }

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

1654
 done:
1655 1656 1657 1658
    remoteDriverUnlock(priv);
    return rv;
}

1659 1660 1661 1662 1663
/* Helper to free typed parameters. */
static void
remoteFreeTypedParameters(remote_typed_param *args_params_val,
                          u_int args_params_len)
{
1664
    size_t i;
1665 1666 1667 1668

    if (args_params_val == NULL)
        return;

1669
    for (i = 0; i < args_params_len; i++) {
1670
        VIR_FREE(args_params_val[i].field);
1671 1672 1673
        if (args_params_val[i].value.type == VIR_TYPED_PARAM_STRING)
            VIR_FREE(args_params_val[i].value.remote_typed_param_value_u.s);
    }
1674 1675 1676 1677

    VIR_FREE(args_params_val);
}

1678
/* Helper to serialize typed parameters. */
1679
static int
1680 1681
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
1682 1683
                               remote_typed_param **args_params_val,
                               u_int *args_params_len)
1684
{
1685
    size_t i;
1686
    int rv = -1;
1687
    remote_typed_param *val;
1688

1689
    *args_params_len = nparams;
1690
    if (VIR_ALLOC_N(val, nparams) < 0)
1691
        goto cleanup;
1692 1693

    for (i = 0; i < nparams; ++i) {
1694
        /* call() will free this: */
1695
        if (VIR_STRDUP(val[i].field, params[i].field) < 0)
1696 1697
            goto cleanup;
        val[i].value.type = params[i].type;
1698
        switch (params[i].type) {
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
        case VIR_TYPED_PARAM_INT:
            val[i].value.remote_typed_param_value_u.i = params[i].value.i;
            break;
        case VIR_TYPED_PARAM_UINT:
            val[i].value.remote_typed_param_value_u.ui = params[i].value.ui;
            break;
        case VIR_TYPED_PARAM_LLONG:
            val[i].value.remote_typed_param_value_u.l = params[i].value.l;
            break;
        case VIR_TYPED_PARAM_ULLONG:
            val[i].value.remote_typed_param_value_u.ul = params[i].value.ul;
            break;
        case VIR_TYPED_PARAM_DOUBLE:
            val[i].value.remote_typed_param_value_u.d = params[i].value.d;
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
            val[i].value.remote_typed_param_value_u.b = params[i].value.b;
            break;
1717
        case VIR_TYPED_PARAM_STRING:
1718 1719
            if (VIR_STRDUP(val[i].value.remote_typed_param_value_u.s,
                           params[i].value.s) < 0)
1720 1721
                goto cleanup;
            break;
1722
        default:
1723 1724
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
1725
            goto cleanup;
1726 1727 1728
        }
    }

1729 1730 1731 1732
    *args_params_val = val;
    val = NULL;
    rv = 0;

1733
 cleanup:
1734
    remoteFreeTypedParameters(val, nparams);
1735 1736 1737 1738 1739
    return rv;
}

/* Helper to deserialize typed parameters. */
static int
1740 1741 1742 1743 1744 1745
deserializeTypedParameters(const char *funcname,
                           remote_typed_param *ret_params_val,
                           u_int ret_params_len,
                           int limit,
                           virTypedParameterPtr *params,
                           int *nparams)
1746
{
1747
    size_t i = 0;
1748
    int rv = -1;
1749
    bool userAllocated = *params != NULL;
1750

1751 1752 1753 1754 1755 1756 1757
    if (ret_params_len > limit) {
        virReportError(VIR_ERR_RPC,
                       _("%s: too many parameters '%u' for limit '%d'"),
                       funcname, ret_params_len, limit);
        goto cleanup;
    }

1758 1759
    if (userAllocated) {
        /* Check the length of the returned list carefully. */
1760 1761 1762 1763
        if (ret_params_len > *nparams) {
            virReportError(VIR_ERR_RPC,
                           _("%s: too many parameters '%u' for nparams '%d'"),
                           funcname, ret_params_len, *nparams);
1764 1765 1766
            goto cleanup;
        }
    } else {
1767
        if (VIR_ALLOC_N(*params, ret_params_len) < 0)
1768
            goto cleanup;
1769 1770 1771 1772 1773
    }
    *nparams = ret_params_len;

    /* Deserialise the result. */
    for (i = 0; i < ret_params_len; ++i) {
1774 1775 1776 1777 1778
        virTypedParameterPtr param = *params + i;
        remote_typed_param *ret_param = ret_params_val + i;

        if (virStrcpyStatic(param->field,
                            ret_param->field) == NULL) {
1779
            virReportError(VIR_ERR_INTERNAL_ERROR,
1780 1781
                           _("%s: parameter %s too big for destination"),
                           funcname, ret_param->field);
1782 1783
            goto cleanup;
        }
1784 1785 1786

        param->type = ret_param->value.type;
        switch (param->type) {
1787
        case VIR_TYPED_PARAM_INT:
1788 1789
            param->value.i =
                ret_param->value.remote_typed_param_value_u.i;
1790 1791
            break;
        case VIR_TYPED_PARAM_UINT:
1792 1793
            param->value.ui =
                ret_param->value.remote_typed_param_value_u.ui;
1794 1795
            break;
        case VIR_TYPED_PARAM_LLONG:
1796 1797
            param->value.l =
                ret_param->value.remote_typed_param_value_u.l;
1798 1799
            break;
        case VIR_TYPED_PARAM_ULLONG:
1800 1801
            param->value.ul =
                ret_param->value.remote_typed_param_value_u.ul;
1802 1803
            break;
        case VIR_TYPED_PARAM_DOUBLE:
1804 1805
            param->value.d =
                ret_param->value.remote_typed_param_value_u.d;
1806 1807
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
1808 1809
            param->value.b =
                ret_param->value.remote_typed_param_value_u.b;
1810
            break;
1811
        case VIR_TYPED_PARAM_STRING:
1812 1813
            if (VIR_STRDUP(param->value.s,
                           ret_param->value.remote_typed_param_value_u.s) < 0)
1814 1815
                goto cleanup;
            break;
1816
        default:
1817 1818
            virReportError(VIR_ERR_RPC, _("%s: unknown parameter type: %d"),
                           funcname, param->type);
1819 1820 1821 1822 1823 1824
            goto cleanup;
        }
    }

    rv = 0;

1825
 cleanup:
1826 1827 1828 1829 1830 1831 1832 1833
    if (rv < 0) {
        if (userAllocated) {
            virTypedParamsClear(*params, i);
        } else {
            virTypedParamsFree(*params, i);
            *params = NULL;
        }
    }
1834 1835 1836
    return rv;
}

1837 1838 1839 1840 1841 1842 1843
static int
remoteDeserializeDomainDiskErrors(remote_domain_disk_error *ret_errors_val,
                                  u_int ret_errors_len,
                                  int limit,
                                  virDomainDiskErrorPtr errors,
                                  int maxerrors)
{
1844 1845
    size_t i = 0;
    size_t j;
1846 1847

    if (ret_errors_len > limit || ret_errors_len > maxerrors) {
1848 1849
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned number of disk errors exceeds limit"));
1850 1851 1852 1853
        goto error;
    }

    for (i = 0; i < ret_errors_len; i++) {
1854
        if (VIR_STRDUP(errors[i].disk, ret_errors_val[i].disk) < 0)
1855 1856 1857 1858 1859 1860
            goto error;
        errors[i].error = ret_errors_val[i].error;
    }

    return 0;

1861
 error:
1862 1863 1864 1865 1866 1867
    for (j = 0; j < i; j++)
        VIR_FREE(errors[i].disk);

    return -1;
}

1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
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);

1882
    make_nonnull_domain(&args.dom, domain);
1883 1884 1885 1886
    args.nparams = *nparams;
    args.path = (char *) path;
    args.flags = flags;

1887 1888 1889 1890
    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)
1891 1892 1893 1894 1895
        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) {
1896 1897 1898
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteDomainBlockStatsFlags: "
                         "returned number of stats exceeds limit"));
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
        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. */
    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
                                         REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX,
1917
                                         &params,
1918 1919 1920 1921 1922
                                         nparams) < 0)
        goto cleanup;

    rv = 0;

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

1931
static int
1932 1933 1934
remoteDomainGetMemoryParameters(virDomainPtr domain,
                                virTypedParameterPtr params, int *nparams,
                                unsigned int flags)
1935 1936 1937 1938 1939 1940 1941 1942
{
    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);

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

1947 1948 1949 1950
    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)
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
        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;
    }

1962 1963
    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
1964
                                         REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX,
1965
                                         &params,
1966 1967
                                         nparams) < 0)
        goto cleanup;
1968 1969 1970

    rv = 0;

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

1979
static int
1980 1981 1982
remoteDomainGetNumaParameters(virDomainPtr domain,
                              virTypedParameterPtr params, int *nparams,
                              unsigned int flags)
1983 1984 1985 1986 1987 1988 1989 1990
{
    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);

1991
    make_nonnull_domain(&args.dom, domain);
1992 1993 1994
    args.nparams = *nparams;
    args.flags = flags;

1995 1996 1997 1998
    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)
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
        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;
    }

    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
                                         REMOTE_DOMAIN_NUMA_PARAMETERS_MAX,
2013
                                         &params,
2014 2015 2016 2017 2018
                                         nparams) < 0)
        goto cleanup;

    rv = 0;

2019
 cleanup:
2020 2021
    xdr_free((xdrproc_t) xdr_remote_domain_get_numa_parameters_ret,
             (char *) &ret);
2022
 done:
2023 2024 2025 2026
    remoteDriverUnlock(priv);
    return rv;
}

2027
static int
2028 2029 2030
remoteDomainGetBlkioParameters(virDomainPtr domain,
                               virTypedParameterPtr params, int *nparams,
                               unsigned int flags)
2031 2032 2033 2034 2035 2036 2037 2038
{
    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);

2039
    make_nonnull_domain(&args.dom, domain);
2040 2041 2042
    args.nparams = *nparams;
    args.flags = flags;

2043 2044 2045 2046
    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)
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
        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;
    }

2058 2059
    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
2060
                                         REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX,
2061
                                         &params,
2062 2063
                                         nparams) < 0)
        goto cleanup;
2064 2065 2066

    rv = 0;

2067
 cleanup:
2068 2069
    xdr_free((xdrproc_t) xdr_remote_domain_get_blkio_parameters_ret,
             (char *) &ret);
2070
 done:
2071 2072 2073 2074
    remoteDriverUnlock(priv);
    return rv;
}

2075
static int
2076 2077 2078 2079 2080
remoteDomainGetVcpuPinInfo(virDomainPtr domain,
                           int ncpumaps,
                           unsigned char *cpumaps,
                           int maplen,
                           unsigned int flags)
2081 2082
{
    int rv = -1;
2083
    size_t i;
E
Eric Blake 已提交
2084 2085
    remote_domain_get_vcpu_pin_info_args args;
    remote_domain_get_vcpu_pin_info_ret ret;
2086 2087 2088 2089 2090
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    if (ncpumaps > REMOTE_VCPUINFO_MAX) {
2091 2092 2093
        virReportError(VIR_ERR_RPC,
                       _("vCPU count exceeds maximum: %d > %d"),
                       ncpumaps, REMOTE_VCPUINFO_MAX);
2094 2095 2096 2097 2098
        goto done;
    }

    if (INT_MULTIPLY_OVERFLOW(ncpumaps, maplen) ||
        ncpumaps * maplen > REMOTE_CPUMAPS_MAX) {
2099 2100 2101
        virReportError(VIR_ERR_RPC,
                       _("vCPU map buffer length exceeds maximum: %d > %d"),
                       ncpumaps * maplen, REMOTE_CPUMAPS_MAX);
2102 2103 2104
        goto done;
    }

2105
    make_nonnull_domain(&args.dom, domain);
2106 2107 2108 2109
    args.ncpumaps = ncpumaps;
    args.maplen = maplen;
    args.flags = flags;

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

2112 2113 2114 2115 2116
    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)
2117 2118 2119
        goto done;

    if (ret.num > ncpumaps) {
2120 2121 2122
        virReportError(VIR_ERR_RPC,
                       _("host reports too many vCPUs: %d > %d"),
                       ret.num, ncpumaps);
2123 2124 2125 2126
        goto cleanup;
    }

    if (ret.cpumaps.cpumaps_len > ncpumaps * maplen) {
2127 2128 2129
        virReportError(VIR_ERR_RPC,
                       _("host reports map buffer length exceeds maximum: %d > %d"),
                       ret.cpumaps.cpumaps_len, ncpumaps * maplen);
2130 2131 2132
        goto cleanup;
    }

2133
    memset(cpumaps, 0, ncpumaps * maplen);
2134 2135 2136 2137 2138 2139

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

    rv = ret.num;

2140
 cleanup:
2141
    xdr_free((xdrproc_t) xdr_remote_domain_get_vcpu_pin_info_ret, (char *) &ret);
2142

2143
 done:
2144 2145 2146 2147
    remoteDriverUnlock(priv);
    return rv;
}

2148
static int
2149 2150 2151 2152
remoteDomainPinEmulator(virDomainPtr dom,
                        unsigned char *cpumap,
                        int cpumaplen,
                        unsigned int flags)
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
{
    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;

2181
 done:
2182 2183 2184 2185 2186 2187
    remoteDriverUnlock(priv);
    return rv;
}


static int
2188 2189 2190 2191
remoteDomainGetEmulatorPinInfo(virDomainPtr domain,
                               unsigned char *cpumaps,
                               int maplen,
                               unsigned int flags)
2192 2193
{
    int rv = -1;
2194
    size_t i;
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
    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));

2215 2216 2217 2218 2219
    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)
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
        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;

2236
 cleanup:
2237 2238
    xdr_free((xdrproc_t) xdr_remote_domain_get_emulator_pin_info_ret,
             (char *) &ret);
2239

2240
 done:
2241 2242 2243 2244
    remoteDriverUnlock(priv);
    return rv;
}

2245
static int
2246 2247 2248 2249 2250
remoteDomainGetVcpus(virDomainPtr domain,
                     virVcpuInfoPtr info,
                     int maxinfo,
                     unsigned char *cpumaps,
                     int maplen)
2251
{
2252
    int rv = -1;
2253
    size_t i;
2254 2255
    remote_domain_get_vcpus_args args;
    remote_domain_get_vcpus_ret ret;
2256
    struct private_data *priv = domain->conn->privateData;
2257

2258 2259
    remoteDriverLock(priv);

2260
    if (maxinfo > REMOTE_VCPUINFO_MAX) {
2261 2262 2263
        virReportError(VIR_ERR_RPC,
                       _("vCPU count exceeds maximum: %d > %d"),
                       maxinfo, REMOTE_VCPUINFO_MAX);
2264
        goto done;
2265
    }
2266 2267
    if (INT_MULTIPLY_OVERFLOW(maxinfo, maplen) ||
        maxinfo * maplen > REMOTE_CPUMAPS_MAX) {
2268 2269 2270
        virReportError(VIR_ERR_RPC,
                       _("vCPU map buffer length exceeds maximum: %d > %d"),
                       maxinfo * maplen, REMOTE_CPUMAPS_MAX);
2271
        goto done;
2272 2273
    }

2274
    make_nonnull_domain(&args.dom, domain);
2275 2276 2277
    args.maxinfo = maxinfo;
    args.maplen = maplen;

2278 2279 2280 2281
    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)
2282
        goto done;
2283 2284

    if (ret.info.info_len > maxinfo) {
2285 2286 2287
        virReportError(VIR_ERR_RPC,
                       _("host reports too many vCPUs: %d > %d"),
                       ret.info.info_len, maxinfo);
2288
        goto cleanup;
2289
    }
2290
    if (ret.cpumaps.cpumaps_len > maxinfo * maplen) {
2291 2292 2293
        virReportError(VIR_ERR_RPC,
                       _("host reports map buffer length exceeds maximum: %d > %d"),
                       ret.cpumaps.cpumaps_len, maxinfo * maplen);
2294
        goto cleanup;
2295 2296
    }

2297 2298
    memset(info, 0, sizeof(virVcpuInfo) * maxinfo);
    memset(cpumaps, 0, maxinfo * maplen);
2299

2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
    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];

2310 2311
    rv = ret.info.info_len;

2312
 cleanup:
2313
    xdr_free((xdrproc_t) xdr_remote_domain_get_vcpus_ret, (char *) &ret);
2314

2315
 done:
2316
    remoteDriverUnlock(priv);
2317
    return rv;
2318 2319
}

2320
static int
2321
remoteDomainGetSecurityLabel(virDomainPtr domain, virSecurityLabelPtr seclabel)
2322 2323 2324 2325
{
    remote_domain_get_security_label_args args;
    remote_domain_get_security_label_ret ret;
    struct private_data *priv = domain->conn->privateData;
2326 2327 2328
    int rv = -1;

    remoteDriverLock(priv);
2329

2330 2331 2332
    make_nonnull_domain(&args.dom, domain);
    memset(&ret, 0, sizeof(ret));
    memset(seclabel, 0, sizeof(*seclabel));
2333

2334 2335 2336
    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) {
2337
        goto done;
2338 2339 2340
    }

    if (ret.label.label_val != NULL) {
2341
        if (strlen(ret.label.label_val) >= sizeof(seclabel->label)) {
2342 2343
            virReportError(VIR_ERR_RPC, _("security label exceeds maximum: %zu"),
                           sizeof(seclabel->label) - 1);
A
Alex Jia 已提交
2344
            goto cleanup;
2345
        }
2346
        strcpy(seclabel->label, ret.label.label_val);
2347 2348 2349
        seclabel->enforcing = ret.enforcing;
    }

2350 2351
    rv = 0;

2352
 cleanup:
A
Alex Jia 已提交
2353 2354
    xdr_free((xdrproc_t) xdr_remote_domain_get_security_label_ret, (char *)&ret);

2355
 done:
2356 2357
    remoteDriverUnlock(priv);
    return rv;
2358 2359
}

M
Marcelo Cerri 已提交
2360
static int
2361
remoteDomainGetSecurityLabelList(virDomainPtr domain, virSecurityLabelPtr* seclabels)
M
Marcelo Cerri 已提交
2362 2363 2364 2365
{
    remote_domain_get_security_label_list_args args;
    remote_domain_get_security_label_list_ret ret;
    struct private_data *priv = domain->conn->privateData;
2366 2367
    size_t i;
    int rv = -1;
M
Marcelo Cerri 已提交
2368 2369 2370

    remoteDriverLock(priv);

2371
    make_nonnull_domain(&args.dom, domain);
M
Marcelo Cerri 已提交
2372 2373
    memset(&ret, 0, sizeof(ret));

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

2398
 cleanup:
M
Marcelo Cerri 已提交
2399 2400
    xdr_free((xdrproc_t) xdr_remote_domain_get_security_label_list_ret, (char *)&ret);

2401
 done:
M
Marcelo Cerri 已提交
2402 2403 2404 2405
    remoteDriverUnlock(priv);
    return rv;
}

2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421
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;

2422
    memset(&ret, 0, sizeof(ret));
2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433
    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;

2434
 done:
2435 2436 2437 2438
    remoteDriverUnlock(priv);
    return rv;
}

2439
static int
2440
remoteNodeGetSecurityModel(virConnectPtr conn, virSecurityModelPtr secmodel)
2441 2442 2443
{
    remote_node_get_security_model_ret ret;
    struct private_data *priv = conn->privateData;
2444 2445 2446
    int rv = -1;

    remoteDriverLock(priv);
2447

2448 2449
    memset(&ret, 0, sizeof(ret));
    memset(secmodel, 0, sizeof(*secmodel));
2450

2451 2452 2453
    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) {
2454
        goto done;
2455 2456 2457
    }

    if (ret.model.model_val != NULL) {
2458
        if (strlen(ret.model.model_val) >= sizeof(secmodel->model)) {
2459 2460
            virReportError(VIR_ERR_RPC, _("security model exceeds maximum: %zu"),
                           sizeof(secmodel->model) - 1);
A
Alex Jia 已提交
2461
            goto cleanup;
2462
        }
2463
        strcpy(secmodel->model, ret.model.model_val);
2464 2465 2466
    }

    if (ret.doi.doi_val != NULL) {
2467
        if (strlen(ret.doi.doi_val) >= sizeof(secmodel->doi)) {
2468 2469
            virReportError(VIR_ERR_RPC, _("security doi exceeds maximum: %zu"),
                           sizeof(secmodel->doi) - 1);
A
Alex Jia 已提交
2470
            goto cleanup;
2471
        }
2472
        strcpy(secmodel->doi, ret.doi.doi_val);
2473
    }
2474 2475 2476

    rv = 0;

2477
 cleanup:
A
Alex Jia 已提交
2478 2479
    xdr_free((xdrproc_t) xdr_remote_node_get_security_model_ret, (char *)&ret);

2480
 done:
2481 2482
    remoteDriverUnlock(priv);
    return rv;
2483 2484
}

2485
static int
2486 2487 2488 2489 2490
remoteDomainMigratePrepare(virConnectPtr dconn,
                           char **cookie, int *cookielen,
                           const char *uri_in, char **uri_out,
                           unsigned long flags, const char *dname,
                           unsigned long resource)
2491
{
2492
    int rv = -1;
2493 2494
    remote_domain_migrate_prepare_args args;
    remote_domain_migrate_prepare_ret ret;
2495
    struct private_data *priv = dconn->privateData;
2496

2497 2498
    remoteDriverLock(priv);

2499 2500 2501 2502 2503
    args.uri_in = uri_in == NULL ? NULL : (char **) &uri_in;
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;

2504 2505 2506 2507
    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)
2508
        goto done;
2509 2510 2511 2512 2513 2514 2515 2516

    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 已提交
2517
    VIR_FREE(ret.uri_out);
2518 2519
    rv = 0;

2520
 done:
2521
    remoteDriverUnlock(priv);
2522
    return rv;
2523 2524
}

D
Daniel Veillard 已提交
2525
static int
2526 2527 2528 2529 2530 2531
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 已提交
2532
{
2533
    int rv = -1;
D
Daniel Veillard 已提交
2534 2535
    remote_domain_migrate_prepare2_args args;
    remote_domain_migrate_prepare2_ret ret;
2536
    struct private_data *priv = dconn->privateData;
D
Daniel Veillard 已提交
2537

2538 2539
    remoteDriverLock(priv);

D
Daniel Veillard 已提交
2540 2541 2542 2543 2544 2545
    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;

2546 2547 2548 2549
    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)
2550
        goto done;
D
Daniel Veillard 已提交
2551 2552

    if (ret.cookie.cookie_len > 0) {
2553
        if (!cookie || !cookielen) {
2554 2555
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookie or cookielen"));
2556 2557
            goto error;
        }
D
Daniel Veillard 已提交
2558 2559 2560
        *cookie = ret.cookie.cookie_val; /* Caller frees. */
        *cookielen = ret.cookie.cookie_len;
    }
2561 2562
    if (ret.uri_out) {
        if (!uri_out) {
2563 2564
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores uri_out"));
2565 2566
            goto error;
        }
D
Daniel Veillard 已提交
2567
        *uri_out = *ret.uri_out; /* Caller frees. */
2568
    }
D
Daniel Veillard 已提交
2569

2570 2571
    rv = 0;

2572
 done:
O
Osier Yang 已提交
2573
    VIR_FREE(ret.uri_out);
2574
    remoteDriverUnlock(priv);
2575
    return rv;
2576
 error:
2577 2578 2579 2580 2581
    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 已提交
2582 2583
}

2584
static int
2585
remoteDomainCreate(virDomainPtr domain)
2586
{
2587
    int rv = -1;
2588
    remote_domain_create_args args;
2589 2590
    remote_domain_lookup_by_uuid_args args2;
    remote_domain_lookup_by_uuid_ret ret2;
2591
    struct private_data *priv = domain->conn->privateData;
2592

2593 2594
    remoteDriverLock(priv);

2595
    make_nonnull_domain(&args.dom, domain);
2596

2597 2598 2599
    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)
2600
        goto done;
2601

2602 2603 2604 2605
    /* 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.
     */
2606 2607 2608 2609 2610
    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)
2611 2612 2613
        goto done;

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

2616 2617
    rv = 0;

2618
 done:
2619
    remoteDriverUnlock(priv);
2620
    return rv;
2621 2622
}

2623 2624 2625 2626 2627 2628
static int
remoteDomainCreateWithFlags(virDomainPtr dom, unsigned int flags)
{
    int rv = -1;
    struct private_data *priv = dom->conn->privateData;
    remote_domain_create_with_flags_args args;
2629
    remote_domain_create_with_flags_args ret;
2630 2631 2632 2633 2634 2635

    remoteDriverLock(priv);

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

2636
    memset(&ret, 0, sizeof(ret));
2637 2638
    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_CREATE_WITH_FLAGS,
             (xdrproc_t)xdr_remote_domain_create_with_flags_args, (char *)&args,
2639
             (xdrproc_t)xdr_remote_domain_create_with_flags_ret, (char *)&ret) == -1) {
2640 2641 2642
        goto done;
    }

2643 2644
    dom->id = ret.dom.id;
    xdr_free((xdrproc_t) &xdr_remote_domain_create_with_flags_ret, (char *) &ret);
2645 2646
    rv = 0;

2647
 done:
2648 2649 2650 2651
    remoteDriverUnlock(priv);
    return rv;
}

2652
static char *
2653
remoteDomainGetSchedulerType(virDomainPtr domain, int *nparams)
2654
{
2655 2656 2657
    char *rv = NULL;
    remote_domain_get_scheduler_type_args args;
    remote_domain_get_scheduler_type_ret ret;
2658
    struct private_data *priv = domain->conn->privateData;
2659

2660 2661
    remoteDriverLock(priv);

2662
    make_nonnull_domain(&args.dom, domain);
2663

2664 2665 2666 2667
    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)
2668
        goto done;
2669 2670 2671 2672

    if (nparams) *nparams = ret.nparams;

    /* Caller frees this. */
2673 2674
    rv = ret.type;

2675
 done:
2676
    remoteDriverUnlock(priv);
2677
    return rv;
2678 2679
}

2680
static int
2681
remoteDomainMemoryStats(virDomainPtr domain,
2682
                        virDomainMemoryStatPtr stats,
2683 2684
                        unsigned int nr_stats,
                        unsigned int flags)
2685 2686 2687 2688 2689
{
    int rv = -1;
    remote_domain_memory_stats_args args;
    remote_domain_memory_stats_ret ret;
    struct private_data *priv = domain->conn->privateData;
2690
    size_t i;
2691 2692 2693

    remoteDriverLock(priv);

2694
    make_nonnull_domain(&args.dom, domain);
2695
    if (nr_stats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
2696 2697 2698
        virReportError(VIR_ERR_RPC,
                       _("too many memory stats requested: %d > %d"), nr_stats,
                       REMOTE_DOMAIN_MEMORY_STATS_MAX);
2699 2700 2701
        goto done;
    }
    args.maxStats = nr_stats;
2702
    args.flags = flags;
2703
    memset(&ret, 0, sizeof(ret));
2704

2705 2706 2707 2708 2709
    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)
2710 2711 2712 2713 2714 2715 2716 2717 2718
        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);

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

2724
static int
2725 2726 2727 2728 2729 2730
remoteDomainBlockPeek(virDomainPtr domain,
                      const char *path,
                      unsigned long long offset,
                      size_t size,
                      void *buffer,
                      unsigned int flags)
2731
{
2732
    int rv = -1;
2733 2734
    remote_domain_block_peek_args args;
    remote_domain_block_peek_ret ret;
2735
    struct private_data *priv = domain->conn->privateData;
2736

2737 2738
    remoteDriverLock(priv);

2739
    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
2740 2741 2742
        virReportError(VIR_ERR_RPC,
                       _("block peek request too large for remote protocol, %zi > %d"),
                       size, REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX);
2743
        goto done;
2744 2745
    }

2746
    make_nonnull_domain(&args.dom, domain);
2747 2748 2749 2750 2751
    args.path = (char *) path;
    args.offset = offset;
    args.size = size;
    args.flags = flags;

2752 2753 2754 2755 2756 2757
    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)
2758
        goto done;
2759 2760

    if (ret.buffer.buffer_len != size) {
2761 2762
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned buffer is not same size as requested"));
2763
        goto cleanup;
2764 2765
    }

2766
    memcpy(buffer, ret.buffer.buffer_val, size);
2767 2768
    rv = 0;

2769
 cleanup:
2770
    VIR_FREE(ret.buffer.buffer_val);
2771

2772
 done:
2773
    remoteDriverUnlock(priv);
2774
    return rv;
2775 2776
}

R
Richard W.M. Jones 已提交
2777
static int
2778 2779 2780 2781 2782
remoteDomainMemoryPeek(virDomainPtr domain,
                       unsigned long long offset,
                       size_t size,
                       void *buffer,
                       unsigned int flags)
R
Richard W.M. Jones 已提交
2783
{
2784
    int rv = -1;
R
Richard W.M. Jones 已提交
2785 2786
    remote_domain_memory_peek_args args;
    remote_domain_memory_peek_ret ret;
2787
    struct private_data *priv = domain->conn->privateData;
R
Richard W.M. Jones 已提交
2788

2789 2790
    remoteDriverLock(priv);

R
Richard W.M. Jones 已提交
2791
    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
2792 2793 2794
        virReportError(VIR_ERR_RPC,
                       _("memory peek request too large for remote protocol, %zi > %d"),
                       size, REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX);
2795
        goto done;
R
Richard W.M. Jones 已提交
2796 2797
    }

2798
    make_nonnull_domain(&args.dom, domain);
R
Richard W.M. Jones 已提交
2799 2800 2801 2802
    args.offset = offset;
    args.size = size;
    args.flags = flags;

2803 2804 2805 2806 2807 2808
    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)
2809
        goto done;
R
Richard W.M. Jones 已提交
2810 2811

    if (ret.buffer.buffer_len != size) {
2812 2813
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned buffer is not same size as requested"));
2814
        goto cleanup;
R
Richard W.M. Jones 已提交
2815 2816
    }

2817
    memcpy(buffer, ret.buffer.buffer_val, size);
2818 2819
    rv = 0;

2820
 cleanup:
2821
    VIR_FREE(ret.buffer.buffer_val);
R
Richard W.M. Jones 已提交
2822

2823
 done:
2824
    remoteDriverUnlock(priv);
2825
    return rv;
R
Richard W.M. Jones 已提交
2826 2827
}

2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
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;
    }

2861
 done:
2862 2863 2864 2865
    remoteDriverUnlock(priv);
    return rv;
}

2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906
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;
    }

    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
                                         REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX,
2907
                                         &params,
2908 2909 2910 2911 2912
                                         nparams) < 0)
        goto cleanup;

    rv = 0;

2913
 cleanup:
2914 2915
    xdr_free((xdrproc_t) xdr_remote_domain_get_block_io_tune_ret,
             (char *) &ret);
2916
 done:
2917 2918 2919 2920
    remoteDriverUnlock(priv);
    return rv;
}

2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936
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) {
2937 2938 2939
        virReportError(VIR_ERR_RPC,
                       _("nparams count exceeds maximum: %u > %u"),
                       nparams, REMOTE_NODE_CPU_STATS_MAX);
2940 2941 2942
        goto done;
    }
    if (ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
2943 2944 2945
        virReportError(VIR_ERR_RPC,
                       _("ncpus count exceeds maximum: %u > %u"),
                       ncpus, REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX);
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966
        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 &&
2967
         ((ret.params.params_len % ret.nparams) || ret.nparams > nparams))) {
2968 2969 2970
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteDomainGetCPUStats: "
                         "returned number of stats exceeds limit"));
2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
        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
2984 2985
     * to expand things back into a possibly sparse array, where the
     * tail of the array may be omitted.
2986 2987
     */
    memset(params, 0, sizeof(*params) * nparams * ncpus);
2988
    ncpus = ret.params.params_len / ret.nparams;
2989 2990
    for (cpu = 0; cpu < ncpus; cpu++) {
        int tmp = nparams;
2991
        virTypedParameterPtr cpu_params = &params[cpu * nparams];
2992 2993 2994 2995
        remote_typed_param *stride = &ret.params.params_val[cpu * ret.nparams];

        if (remoteDeserializeTypedParameters(stride, ret.nparams,
                                             REMOTE_NODE_CPU_STATS_MAX,
2996
                                             &cpu_params, &tmp) < 0)
2997 2998 2999 3000
            goto cleanup;
    }

    rv = ret.nparams;
3001
 cleanup:
A
Alex Jia 已提交
3002
    if (rv < 0)
3003
        virTypedParamsClear(params, nparams * ncpus);
3004

3005 3006
    xdr_free((xdrproc_t) xdr_remote_domain_get_cpu_stats_ret,
             (char *) &ret);
3007
 done:
3008 3009 3010 3011
    remoteDriverUnlock(priv);
    return rv;
}

3012 3013 3014 3015 3016 3017
static int
remoteConnectListAllNetworks(virConnectPtr conn,
                             virNetworkPtr **nets,
                             unsigned int flags)
{
    int rv = -1;
3018
    size_t i;
3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040
    virNetworkPtr *tmp_nets = NULL;
    remote_connect_list_all_networks_args args;
    remote_connect_list_all_networks_ret ret;

    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.need_results = !!nets;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(conn,
             priv,
             0,
             REMOTE_PROC_CONNECT_LIST_ALL_NETWORKS,
             (xdrproc_t) xdr_remote_connect_list_all_networks_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_connect_list_all_networks_ret,
             (char *) &ret) == -1)
        goto done;

3041 3042 3043 3044 3045 3046 3047
    if (ret.nets.nets_len > REMOTE_NETWORK_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many networks '%d' for limit '%d'"),
                       ret.nets.nets_len, REMOTE_NETWORK_LIST_MAX);
        goto cleanup;
    }

3048
    if (nets) {
3049
        if (VIR_ALLOC_N(tmp_nets, ret.nets.nets_len + 1) < 0)
3050 3051 3052
            goto cleanup;

        for (i = 0; i < ret.nets.nets_len; i++) {
3053
            tmp_nets[i] = get_nonnull_network(conn, ret.nets.nets_val[i]);
3054
            if (!tmp_nets[i])
3055 3056 3057 3058 3059 3060 3061 3062
                goto cleanup;
        }
        *nets = tmp_nets;
        tmp_nets = NULL;
    }

    rv = ret.ret;

3063
 cleanup:
3064 3065
    if (tmp_nets) {
        for (i = 0; i < ret.nets.nets_len; i++)
3066
            virObjectUnref(tmp_nets[i]);
3067 3068 3069 3070 3071
        VIR_FREE(tmp_nets);
    }

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

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

3077

3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
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;
3089
    remote_connect_network_event_register_any_ret ret;
3090 3091
    int callbackID;
    int count;
3092
    remote_nonnull_network network;
3093 3094 3095

    remoteDriverLock(priv);

3096 3097 3098 3099
    if ((count = virNetworkEventStateRegisterClient(conn, priv->eventState,
                                                    net, eventID, callback,
                                                    opaque, freecb,
                                                    &callbackID)) < 0)
3100 3101 3102 3103 3104 3105
        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;
3106 3107 3108 3109 3110 3111
        if (net) {
            make_nonnull_network(&network, net);
            args.net = &network;
        } else {
            args.net = NULL;
        }
3112

3113
        memset(&ret, 0, sizeof(ret));
3114 3115
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_NETWORK_EVENT_REGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_network_event_register_any_args, (char *) &args,
3116
                 (xdrproc_t) xdr_remote_connect_network_event_register_any_ret, (char *) &ret) == -1) {
3117
            virObjectEventStateDeregisterID(conn, priv->eventState,
3118 3119 3120
                                            callbackID);
            goto done;
        }
3121 3122
        virObjectEventStateSetRemote(conn, priv->eventState, callbackID,
                                     ret.callbackID);
3123 3124 3125 3126
    }

    rv = callbackID;

3127
 done:
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
    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;
3141
    int remoteID;
3142 3143 3144 3145
    int count;

    remoteDriverLock(priv);

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

3150
    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
3151
                                                 callbackID)) < 0)
3152 3153 3154 3155 3156
        goto done;

    /* If that was the last callback for this eventID, we need to disable
     * events on the server */
    if (count == 0) {
3157
        args.callbackID = remoteID;
3158 3159 3160 3161 3162 3163 3164 3165 3166

        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;

3167
 done:
3168 3169 3170 3171
    remoteDriverUnlock(priv);
    return rv;
}

3172

3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224
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,
                                            callbackID);
            goto done;
        }
        virObjectEventStateSetRemote(conn, priv->eventState, callbackID,
                                     ret.callbackID);
    }

    rv = callbackID;

3225
 done:
3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263
    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,
                                                 callbackID)) < 0)
        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;

3264
 done:
3265 3266 3267 3268 3269
    remoteDriverUnlock(priv);
    return rv;
}


3270 3271 3272 3273 3274 3275
static int
remoteConnectListAllInterfaces(virConnectPtr conn,
                               virInterfacePtr **ifaces,
                               unsigned int flags)
{
    int rv = -1;
3276
    size_t i;
3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298
    virInterfacePtr *tmp_ifaces = NULL;
    remote_connect_list_all_interfaces_args args;
    remote_connect_list_all_interfaces_ret ret;

    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.need_results = !!ifaces;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(conn,
             priv,
             0,
             REMOTE_PROC_CONNECT_LIST_ALL_INTERFACES,
             (xdrproc_t) xdr_remote_connect_list_all_interfaces_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_connect_list_all_interfaces_ret,
             (char *) &ret) == -1)
        goto done;

3299 3300 3301 3302 3303 3304 3305
    if (ret.ifaces.ifaces_len > REMOTE_INTERFACE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many interfaces '%d' for limit '%d'"),
                       ret.ifaces.ifaces_len, REMOTE_INTERFACE_LIST_MAX);
        goto cleanup;
    }

3306
    if (ifaces) {
3307
        if (VIR_ALLOC_N(tmp_ifaces, ret.ifaces.ifaces_len + 1) < 0)
3308 3309 3310
            goto cleanup;

        for (i = 0; i < ret.ifaces.ifaces_len; i++) {
3311
            tmp_ifaces[i] = get_nonnull_interface(conn, ret.ifaces.ifaces_val[i]);
3312
            if (!tmp_ifaces[i])
3313 3314 3315 3316 3317 3318 3319 3320
                goto cleanup;
        }
        *ifaces = tmp_ifaces;
        tmp_ifaces = NULL;
    }

    rv = ret.ret;

3321
 cleanup:
3322 3323
    if (tmp_ifaces) {
        for (i = 0; i < ret.ifaces.ifaces_len; i++)
3324
            virObjectUnref(tmp_ifaces[i]);
3325 3326 3327 3328 3329
    }
    VIR_FREE(tmp_ifaces);

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

3330
 done:
3331 3332 3333 3334
    remoteDriverUnlock(priv);
    return rv;
}

3335 3336 3337 3338 3339 3340
static int
remoteConnectListAllNodeDevices(virConnectPtr conn,
                                virNodeDevicePtr **devices,
                                unsigned int flags)
{
    int rv = -1;
3341
    size_t i;
3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363
    virNodeDevicePtr *tmp_devices = NULL;
    remote_connect_list_all_node_devices_args args;
    remote_connect_list_all_node_devices_ret ret;

    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.need_results = !!devices;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(conn,
             priv,
             0,
             REMOTE_PROC_CONNECT_LIST_ALL_NODE_DEVICES,
             (xdrproc_t) xdr_remote_connect_list_all_node_devices_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_connect_list_all_node_devices_ret,
             (char *) &ret) == -1)
        goto done;

3364 3365 3366 3367 3368 3369 3370
    if (ret.devices.devices_len > REMOTE_NODE_DEVICE_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many node devices '%d' for limit '%d'"),
                       ret.devices.devices_len, REMOTE_NODE_DEVICE_LIST_MAX);
        goto cleanup;
    }

3371
    if (devices) {
3372
        if (VIR_ALLOC_N(tmp_devices, ret.devices.devices_len + 1) < 0)
3373 3374 3375 3376
            goto cleanup;

        for (i = 0; i < ret.devices.devices_len; i++) {
            tmp_devices[i] = get_nonnull_node_device(conn, ret.devices.devices_val[i]);
3377
            if (!tmp_devices[i])
3378 3379 3380 3381 3382 3383 3384 3385
                goto cleanup;
        }
        *devices = tmp_devices;
        tmp_devices = NULL;
    }

    rv = ret.ret;

3386
 cleanup:
3387 3388
    if (tmp_devices) {
        for (i = 0; i < ret.devices.devices_len; i++)
3389
            virObjectUnref(tmp_devices[i]);
3390 3391 3392 3393 3394
        VIR_FREE(tmp_devices);
    }

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

3395
 done:
3396 3397 3398 3399
    remoteDriverUnlock(priv);
    return rv;
}

3400 3401 3402 3403 3404 3405
static int
remoteConnectListAllNWFilters(virConnectPtr conn,
                              virNWFilterPtr **filters,
                              unsigned int flags)
{
    int rv = -1;
3406
    size_t i;
3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
    virNWFilterPtr *tmp_filters = NULL;
    remote_connect_list_all_nwfilters_args args;
    remote_connect_list_all_nwfilters_ret ret;

    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.need_results = !!filters;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(conn,
             priv,
             0,
             REMOTE_PROC_CONNECT_LIST_ALL_NWFILTERS,
             (xdrproc_t) xdr_remote_connect_list_all_nwfilters_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_connect_list_all_nwfilters_ret,
             (char *) &ret) == -1)
        goto done;

3429 3430 3431 3432 3433 3434 3435
    if (ret.filters.filters_len > REMOTE_NWFILTER_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many network filters '%d' for limit '%d'"),
                       ret.filters.filters_len, REMOTE_NWFILTER_LIST_MAX);
        goto cleanup;
    }

3436
    if (filters) {
3437
        if (VIR_ALLOC_N(tmp_filters, ret.filters.filters_len + 1) < 0)
3438 3439 3440 3441
            goto cleanup;

        for (i = 0; i < ret.filters.filters_len; i++) {
            tmp_filters[i] = get_nonnull_nwfilter(conn, ret.filters.filters_val[i]);
3442
            if (!tmp_filters[i])
3443 3444 3445 3446 3447 3448 3449 3450
                goto cleanup;
        }
        *filters = tmp_filters;
        tmp_filters = NULL;
    }

    rv = ret.ret;

3451
 cleanup:
3452 3453
    if (tmp_filters) {
        for (i = 0; i < ret.filters.filters_len; i++)
3454
            virObjectUnref(tmp_filters[i]);
3455 3456 3457 3458 3459
        VIR_FREE(tmp_filters);
    }

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

3460
 done:
3461 3462 3463
    remoteDriverUnlock(priv);
    return rv;
}
3464

3465 3466 3467 3468 3469 3470
static int
remoteConnectListAllSecrets(virConnectPtr conn,
                            virSecretPtr **secrets,
                            unsigned int flags)
{
    int rv = -1;
3471
    size_t i;
3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493
    virSecretPtr *tmp_secrets = NULL;
    remote_connect_list_all_secrets_args args;
    remote_connect_list_all_secrets_ret ret;

    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.need_results = !!secrets;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(conn,
             priv,
             0,
             REMOTE_PROC_CONNECT_LIST_ALL_SECRETS,
             (xdrproc_t) xdr_remote_connect_list_all_secrets_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_connect_list_all_secrets_ret,
             (char *) &ret) == -1)
        goto done;

3494 3495 3496 3497 3498 3499 3500
    if (ret.secrets.secrets_len > REMOTE_SECRET_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many secrets '%d' for limit '%d'"),
                       ret.secrets.secrets_len, REMOTE_SECRET_LIST_MAX);
        goto cleanup;
    }

3501
    if (secrets) {
3502
        if (VIR_ALLOC_N(tmp_secrets, ret.secrets.secrets_len + 1) < 0)
3503 3504 3505
            goto cleanup;

        for (i = 0; i < ret.secrets.secrets_len; i++) {
3506
            tmp_secrets[i] = get_nonnull_secret(conn, ret.secrets.secrets_val[i]);
3507
            if (!tmp_secrets[i])
3508 3509 3510 3511 3512 3513 3514 3515
                goto cleanup;
        }
        *secrets = tmp_secrets;
        tmp_secrets = NULL;
    }

    rv = ret.ret;

3516
 cleanup:
3517 3518
    if (tmp_secrets) {
        for (i = 0; i < ret.secrets.secrets_len; i++)
3519
            virObjectUnref(tmp_secrets[i]);
3520 3521 3522 3523 3524
        VIR_FREE(tmp_secrets);
    }

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

3525
 done:
3526 3527 3528 3529
    remoteDriverUnlock(priv);
    return rv;
}

3530 3531
/*----------------------------------------------------------------------*/

3532
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3533
remoteGenericOpen(virConnectPtr conn)
3534
{
3535 3536 3537
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
3538
    if (conn->driver &&
3539
        STREQ(conn->driver->name, "remote")) {
3540
        return VIR_DRV_OPEN_SUCCESS;
3541
    }
3542 3543

    return VIR_DRV_OPEN_DECLINED;
3544 3545
}

3546
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3547
remoteNetworkOpen(virConnectPtr conn, virConnectAuthPtr auth ATTRIBUTE_UNUSED,
3548
                  unsigned int flags)
3549
{
3550 3551
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3552
    return remoteGenericOpen(conn);
3553 3554 3555
}

static int
3556
remoteNetworkClose(virConnectPtr conn ATTRIBUTE_UNUSED)
3557
{
3558
    return 0;
3559 3560
}

D
Daniel Veillard 已提交
3561 3562
/*----------------------------------------------------------------------*/

3563
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3564
remoteInterfaceOpen(virConnectPtr conn, virConnectAuthPtr auth ATTRIBUTE_UNUSED,
3565
                    unsigned int flags)
D
Daniel Veillard 已提交
3566
{
3567 3568
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3569
    return remoteGenericOpen(conn);
D
Daniel Veillard 已提交
3570 3571 3572
}

static int
3573
remoteInterfaceClose(virConnectPtr conn ATTRIBUTE_UNUSED)
D
Daniel Veillard 已提交
3574
{
3575
    return 0;
D
Daniel Veillard 已提交
3576 3577
}

3578 3579
/*----------------------------------------------------------------------*/

3580
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3581
remoteStorageOpen(virConnectPtr conn, virConnectAuthPtr auth ATTRIBUTE_UNUSED,
3582
                  unsigned int flags)
3583
{
3584 3585
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3586
    return remoteGenericOpen(conn);
3587 3588 3589
}

static int
3590
remoteStorageClose(virConnectPtr conn ATTRIBUTE_UNUSED)
3591
{
3592
    return 0;
3593 3594
}

3595
static char *
3596 3597 3598 3599
remoteConnectFindStoragePoolSources(virConnectPtr conn,
                                    const char *type,
                                    const char *srcSpec,
                                    unsigned int flags)
3600
{
3601
    char *rv = NULL;
3602 3603
    remote_connect_find_storage_pool_sources_args args;
    remote_connect_find_storage_pool_sources_ret ret;
3604
    struct private_data *priv = conn->privateData;
3605

3606 3607
    remoteDriverLock(priv);

3608
    args.type = (char*)type;
3609
    args.srcSpec = srcSpec ? (char **)&srcSpec : NULL;
3610 3611
    args.flags = flags;

3612
    memset(&ret, 0, sizeof(ret));
3613 3614 3615
    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)
3616
        goto done;
3617

3618 3619 3620
    rv = ret.xml;
    ret.xml = NULL; /* To stop xdr_free free'ing it */

3621
    xdr_free((xdrproc_t) xdr_remote_connect_find_storage_pool_sources_ret, (char *) &ret);
3622

3623
 done:
3624
    remoteDriverUnlock(priv);
3625
    return rv;
3626 3627
}

3628
static int
3629 3630 3631
remoteConnectListAllStoragePools(virConnectPtr conn,
                                 virStoragePoolPtr **pools,
                                 unsigned int flags)
3632 3633
{
    int rv = -1;
3634
    size_t i;
3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656
    virStoragePoolPtr *tmp_pools = NULL;
    remote_connect_list_all_storage_pools_args args;
    remote_connect_list_all_storage_pools_ret ret;

    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.need_results = !!pools;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(conn,
             priv,
             0,
             REMOTE_PROC_CONNECT_LIST_ALL_STORAGE_POOLS,
             (xdrproc_t) xdr_remote_connect_list_all_storage_pools_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_connect_list_all_storage_pools_ret,
             (char *) &ret) == -1)
        goto done;

3657 3658 3659 3660 3661 3662 3663
    if (ret.pools.pools_len > REMOTE_STORAGE_POOL_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many storage pools '%d' for limit '%d'"),
                       ret.pools.pools_len, REMOTE_STORAGE_POOL_LIST_MAX);
        goto cleanup;
    }

3664
    if (pools) {
3665
        if (VIR_ALLOC_N(tmp_pools, ret.pools.pools_len + 1) < 0)
3666 3667 3668 3669
            goto cleanup;

        for (i = 0; i < ret.pools.pools_len; i++) {
            tmp_pools[i] = get_nonnull_storage_pool(conn, ret.pools.pools_val[i]);
3670
            if (!tmp_pools[i])
3671 3672 3673 3674 3675 3676 3677 3678
                goto cleanup;
        }
        *pools = tmp_pools;
        tmp_pools = NULL;
    }

    rv = ret.ret;

3679
 cleanup:
3680 3681
    if (tmp_pools) {
        for (i = 0; i < ret.pools.pools_len; i++)
3682
            virObjectUnref(tmp_pools[i]);
3683 3684 3685 3686 3687
        VIR_FREE(tmp_pools);
    }

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

3688
 done:
3689 3690 3691 3692
    remoteDriverUnlock(priv);
    return rv;
}

3693 3694 3695 3696 3697 3698
static int
remoteStoragePoolListAllVolumes(virStoragePoolPtr pool,
                                virStorageVolPtr **vols,
                                unsigned int flags)
{
    int rv = -1;
3699
    size_t i;
3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722
    virStorageVolPtr *tmp_vols = NULL;
    remote_storage_pool_list_all_volumes_args args;
    remote_storage_pool_list_all_volumes_ret ret;

    struct private_data *priv = pool->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_storage_pool(&args.pool, pool);
    args.need_results = !!vols;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
    if (call(pool->conn,
             priv,
             0,
             REMOTE_PROC_STORAGE_POOL_LIST_ALL_VOLUMES,
             (xdrproc_t) xdr_remote_storage_pool_list_all_volumes_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_storage_pool_list_all_volumes_ret,
             (char *) &ret) == -1)
        goto done;

3723 3724 3725 3726 3727 3728 3729
    if (ret.vols.vols_len > REMOTE_STORAGE_VOL_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many storage volumes '%d' for limit '%d'"),
                       ret.vols.vols_len, REMOTE_STORAGE_VOL_LIST_MAX);
        goto cleanup;
    }

3730
    if (vols) {
3731
        if (VIR_ALLOC_N(tmp_vols, ret.vols.vols_len + 1) < 0)
3732 3733 3734 3735
            goto cleanup;

        for (i = 0; i < ret.vols.vols_len; i++) {
            tmp_vols[i] = get_nonnull_storage_vol(pool->conn, ret.vols.vols_val[i]);
3736
            if (!tmp_vols[i])
3737 3738 3739 3740 3741 3742 3743 3744
                goto cleanup;
        }
        *vols = tmp_vols;
        tmp_vols = NULL;
    }

    rv = ret.ret;

3745
 cleanup:
3746 3747
    if (tmp_vols) {
        for (i = 0; i < ret.vols.vols_len; i++)
3748
            virObjectUnref(tmp_vols[i]);
3749 3750 3751 3752 3753
        VIR_FREE(tmp_vols);
    }

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

3754
 done:
3755 3756 3757 3758 3759
    remoteDriverUnlock(priv);
    return rv;
}


3760 3761
/*----------------------------------------------------------------------*/

3762
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3763
remoteNodeDeviceOpen(virConnectPtr conn, virConnectAuthPtr auth ATTRIBUTE_UNUSED,
3764
                     unsigned int flags)
3765
{
3766 3767
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3768
    return remoteGenericOpen(conn);
3769 3770
}

3771
static int
3772
remoteNodeDeviceClose(virConnectPtr conn ATTRIBUTE_UNUSED)
3773
{
3774
    return 0;
3775 3776
}

3777
static int
3778
remoteNodeDeviceDettach(virNodeDevicePtr dev)
3779 3780 3781 3782 3783 3784 3785 3786 3787
{
    int rv = -1;
    remote_node_device_dettach_args args;
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3788 3789 3790
    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)
3791 3792 3793 3794
        goto done;

    rv = 0;

3795
 done:
3796 3797 3798 3799
    remoteDriverUnlock(priv);
    return rv;
}

3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821
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;

3822
 done:
3823 3824 3825 3826
    remoteDriverUnlock(priv);
    return rv;
}

3827
static int
3828
remoteNodeDeviceReAttach(virNodeDevicePtr dev)
3829 3830 3831 3832 3833 3834 3835 3836 3837
{
    int rv = -1;
    remote_node_device_re_attach_args args;
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3838 3839 3840
    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)
3841 3842 3843 3844
        goto done;

    rv = 0;

3845
 done:
3846 3847 3848 3849 3850
    remoteDriverUnlock(priv);
    return rv;
}

static int
3851
remoteNodeDeviceReset(virNodeDevicePtr dev)
3852 3853 3854
{
    int rv = -1;
    remote_node_device_reset_args args;
3855
    /* This method is unusual in that it uses the HV driver, not the devMon driver
3856
     * hence its use of privateData, instead of nodeDevicePrivateData */
3857 3858 3859 3860 3861 3862
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3863 3864 3865
    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)
3866 3867 3868 3869
        goto done;

    rv = 0;

3870
 done:
3871 3872 3873 3874
    remoteDriverUnlock(priv);
    return rv;
}

3875 3876
/* ------------------------------------------------------------- */

3877
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3878
remoteNWFilterOpen(virConnectPtr conn, virConnectAuthPtr auth ATTRIBUTE_UNUSED,
3879
                   unsigned int flags)
3880
{
3881 3882
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

3883
    return remoteGenericOpen(conn);
3884 3885 3886
}

static int
3887
remoteNWFilterClose(virConnectPtr conn ATTRIBUTE_UNUSED)
3888
{
3889
    return 0;
3890 3891
}

3892 3893
/*----------------------------------------------------------------------*/

3894
static int
3895 3896 3897
remoteAuthenticate(virConnectPtr conn, struct private_data *priv,
                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                   const char *authtype)
3898 3899
{
    struct remote_auth_list_ret ret;
3900
    int err, type = REMOTE_AUTH_NONE;
3901

3902
    memset(&ret, 0, sizeof(ret));
3903 3904 3905 3906
    err = call(conn, priv, 0,
               REMOTE_PROC_AUTH_LIST,
               (xdrproc_t) xdr_void, (char *) NULL,
               (xdrproc_t) xdr_remote_auth_list_ret, (char *) &ret);
3907 3908 3909 3910 3911 3912 3913
    if (err < 0) {
        virErrorPtr verr = virGetLastError();
        if (verr && verr->code == VIR_ERR_NO_SUPPORT) {
            /* Missing RPC - old server - ignore */
            virResetLastError();
            return 0;
        }
3914
        return -1;
3915
    }
3916 3917 3918 3919

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

3920
    if (authtype) {
3921 3922
        int want;
        size_t i;
3923 3924 3925 3926 3927 3928
        if (STRCASEEQ(authtype, "sasl") ||
            STRCASEEQLEN(authtype, "sasl.", 5)) {
            want = REMOTE_AUTH_SASL;
        } else if (STRCASEEQ(authtype, "polkit")) {
            want = REMOTE_AUTH_POLKIT;
        } else {
3929 3930
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("unknown authentication type %s"), authtype);
3931 3932
            return -1;
        }
3933
        for (i = 0; i < ret.types.types_len; i++) {
3934 3935 3936 3937
            if (ret.types.types_val[i] == want)
                type = want;
        }
        if (type == REMOTE_AUTH_NONE) {
3938 3939 3940
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("requested authentication type %s rejected"),
                           authtype);
3941 3942 3943 3944 3945 3946 3947
            return -1;
        }
    } else {
        type = ret.types.types_val[0];
    }

    switch (type) {
3948
#if WITH_SASL
3949 3950 3951 3952 3953 3954
    case REMOTE_AUTH_SASL: {
        const char *mech = NULL;
        if (authtype &&
            STRCASEEQLEN(authtype, "sasl.", 5))
            mech = authtype + 5;

3955
        if (remoteAuthSASL(conn, priv, auth, mech) < 0) {
3956
            VIR_FREE(ret.types.types_val);
3957 3958 3959
            return -1;
        }
        break;
3960
    }
3961 3962
#endif

3963
#if WITH_POLKIT
3964
    case REMOTE_AUTH_POLKIT:
3965
        if (remoteAuthPolkit(conn, priv, auth) < 0) {
3966
            VIR_FREE(ret.types.types_val);
3967 3968 3969 3970 3971
            return -1;
        }
        break;
#endif

3972 3973 3974 3975 3976
    case REMOTE_AUTH_NONE:
        /* Nothing todo, hurrah ! */
        break;

    default:
3977 3978 3979
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("unsupported authentication type %d"),
                       ret.types.types_val[0]);
3980
        VIR_FREE(ret.types.types_val);
3981 3982 3983
        return -1;
    }

3984
    VIR_FREE(ret.types.types_val);
3985 3986 3987 3988 3989 3990

    return 0;
}



3991
#if WITH_SASL
3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060
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.
4061
 * We explicitly leave the callback function pointer at NULL though,
4062 4063 4064 4065 4066 4067 4068 4069
 * 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)
{
4070
    sasl_callback_t *cbs;
4071 4072
    size_t i;
    int n;
4073
    if (VIR_ALLOC_N(cbs, ncredtype+1) < 0)
4074 4075
        return NULL;

4076
    for (i = 0, n = 0; i < ncredtype; i++) {
4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091
        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
4092
 *
4093 4094 4095 4096 4097
 * 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,
4098 4099
                                     virConnectCredentialPtr *cred,
                                     size_t *ncred)
4100 4101 4102 4103 4104
{
    int ninteract;
    if (!cred)
        return -1;

4105
    for (ninteract = 0, *ncred = 0; interact[ninteract].id != 0; ninteract++) {
4106 4107 4108 4109
        if (interact[ninteract].result)
            continue;
        (*ncred)++;
    }
4110

4111
    if (VIR_ALLOC_N(*cred, *ncred) < 0)
4112 4113
        return -1;

4114
    for (ninteract = 0, *ncred = 0; interact[ninteract].id != 0; ninteract++) {
4115 4116 4117 4118 4119 4120
        if (interact[ninteract].result)
            continue;

        (*cred)[*ncred].type = remoteAuthCredSASL2Vir(interact[ninteract].id);
        if (!(*cred)[*ncred].type) {
            *ncred = 0;
4121
            VIR_FREE(*cred);
4122 4123
            return -1;
        }
4124 4125 4126 4127 4128 4129 4130 4131
        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)++;
4132 4133
    }

4134
    return 0;
4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147
}


/*
 * @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)
{
4148
    int ninteract, ncred;
4149
    for (ninteract = 0, ncred = 0; interact[ninteract].id != 0; ninteract++) {
4150 4151 4152 4153 4154
        if (interact[ninteract].result)
            continue;
        interact[ninteract].result = cred[ncred].result;
        interact[ninteract].len = cred[ncred].resultlen;
        ncred++;
4155 4156 4157
    }
}

4158 4159 4160 4161
struct remoteAuthInteractState {
    sasl_interact_t *interact;
    virConnectCredentialPtr cred;
    size_t ncred;
4162
    virAuthConfigPtr config;
4163 4164 4165
};


4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188

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

4189
    for (ninteract = 0; state->interact[ninteract].id != 0; ninteract++) {
4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209
        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;
        }

4210 4211
        if (credname &&
            virAuthConfigLookup(state->config,
4212
                                "libvirt",
4213
                                VIR_URI_SERVER(conn->uri),
4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225
                                credname,
                                &value) < 0)
            goto cleanup;

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

    ret = 0;

4226
 cleanup:
4227 4228 4229 4230 4231 4232 4233
    VIR_FREE(path);
    return ret;
}


static void remoteAuthInteractStateClear(struct remoteAuthInteractState *state,
                                         bool final)
4234 4235 4236 4237 4238
{
    size_t i;
    if (!state)
        return;

4239
    for (i = 0; i < state->ncred; i++)
4240 4241 4242
        VIR_FREE(state->cred[i].result);
    VIR_FREE(state->cred);
    state->ncred = 0;
4243 4244 4245

    if (final)
        virAuthConfigFree(state->config);
4246 4247 4248
}


4249 4250
static int remoteAuthInteract(virConnectPtr conn,
                              struct remoteAuthInteractState *state,
4251 4252 4253 4254
                              virConnectAuthPtr auth)
{
    int ret = -1;

4255 4256 4257
    VIR_DEBUG("Starting SASL interaction");
    remoteAuthInteractStateClear(state, false);

4258
    /* Fills state->interact with any values from the auth config file */
4259 4260
    if (remoteAuthFillFromConfig(conn, state) < 0)
        goto cleanup;
4261

4262
    /* Populates state->cred for anything not found in the auth config */
4263
    if (remoteAuthMakeCredentials(state->interact, &state->cred, &state->ncred) < 0) {
4264 4265
        virReportError(VIR_ERR_AUTH_FAILED, "%s",
                       _("Failed to make auth credentials"));
4266 4267 4268
        goto cleanup;
    }

4269 4270 4271 4272 4273 4274 4275 4276 4277 4278
    /* 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;
        }
4279

4280 4281 4282 4283 4284 4285 4286 4287
        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);
4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299
    }

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

4300
 cleanup:
4301 4302 4303 4304
    return ret;
}


4305
/* Perform the SASL authentication process
4306 4307
 */
static int
4308 4309
remoteAuthSASL(virConnectPtr conn, struct private_data *priv,
               virConnectAuthPtr auth, const char *wantmech)
4310 4311 4312 4313 4314 4315 4316
{
    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;
4317
    char *serverin = NULL;
4318
    size_t clientoutlen, serverinlen;
4319 4320
    const char *mech;
    int err, complete;
4321
    int ssf;
4322 4323 4324
    sasl_callback_t *saslcb = NULL;
    int ret = -1;
    const char *mechlist;
4325
    virNetSASLContextPtr saslCtxt;
E
Eric Blake 已提交
4326
    virNetSASLSessionPtr sasl = NULL;
4327 4328 4329
    struct remoteAuthInteractState state;

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

4331
    VIR_DEBUG("Client initialize SASL authentication");
4332

4333
    if (!(saslCtxt = virNetSASLContextNewClient()))
4334 4335
        goto cleanup;

4336 4337 4338 4339 4340 4341
    if (auth) {
        if ((saslcb = remoteAuthMakeCallbacks(auth->credtype, auth->ncredtype)) == NULL)
            goto cleanup;
    } else {
        saslcb = NULL;
    }
4342 4343

    /* Setup a handle for being a client */
4344 4345 4346 4347 4348 4349
    if (!(sasl = virNetSASLSessionNewClient(saslCtxt,
                                            "libvirt",
                                            priv->hostname,
                                            virNetClientLocalAddrString(priv->client),
                                            virNetClientRemoteAddrString(priv->client),
                                            saslcb)))
4350
        goto cleanup;
4351 4352
    /* saslcb is now owned by sasl */
    saslcb = NULL;
4353

4354
# ifdef WITH_GNUTLS
4355
    /* Initialize some connection props we care about */
4356 4357
    if (priv->tls) {
        if ((ssf = virNetClientGetTLSKeySize(priv->client)) < 0)
4358
            goto cleanup;
4359

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

4362
        VIR_DEBUG("Setting external SSF %d", ssf);
4363
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
4364
            goto cleanup;
4365
    }
4366
# endif
4367

4368 4369
    /* 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 */
4370 4371 4372 4373
    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)
4374
        goto cleanup;
4375

4376
    /* First call is to inquire about supported mechanisms in the server */
4377 4378 4379 4380
    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)
4381
        goto cleanup;
4382 4383


4384 4385 4386
    mechlist = iret.mechlist;
    if (wantmech) {
        if (strstr(mechlist, wantmech) == NULL) {
4387 4388 4389
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("SASL mechanism %s not supported by server"),
                           wantmech);
4390
            VIR_FREE(iret.mechlist);
4391 4392 4393 4394 4395
            goto cleanup;
        }
        mechlist = wantmech;
    }
 restart:
4396
    /* Start the auth negotiation on the client end first */
4397
    VIR_DEBUG("Client start negotiation mechlist '%s'", mechlist);
4398 4399
    if ((err = virNetSASLSessionClientStart(sasl,
                                            mechlist,
4400
                                            &state.interact,
4401 4402 4403
                                            &clientout,
                                            &clientoutlen,
                                            &mech)) < 0)
4404 4405 4406
        goto cleanup;

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

    if (clientoutlen > REMOTE_AUTH_SASL_DATA_MAX) {
4417 4418 4419
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("SASL negotiation data too long: %zu bytes"),
                       clientoutlen);
4420
        goto cleanup;
4421 4422
    }
    /* NB, distinction of NULL vs "" is *critical* in SASL */
4423
    memset(&sargs, 0, sizeof(sargs));
4424 4425 4426 4427
    sargs.nil = clientout ? 0 : 1;
    sargs.data.data_val = (char*)clientout;
    sargs.data.data_len = clientoutlen;
    sargs.mech = (char*)mech;
4428 4429
    VIR_DEBUG("Server start negotiation with mech %s. Data %zu bytes %p",
              mech, clientoutlen, clientout);
4430 4431

    /* Now send the initial auth data to the server */
4432 4433 4434 4435
    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)
4436
        goto cleanup;
4437 4438 4439 4440 4441

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

4445 4446 4447 4448 4449
    /* Previous server call showed completion & sasl_client_start() told us
     * we are locally complete too */
    if (complete && err == VIR_NET_SASL_COMPLETE)
        goto done;

4450 4451
    /* Loop-the-loop...
     * Even if the server has completed, the client must *always* do at least one step
D
Daniel Veillard 已提交
4452
     * in this loop to verify the server isn't lying about something. Mutual auth */
4453
    for (;;) {
4454 4455 4456
        if ((err = virNetSASLSessionClientStep(sasl,
                                               serverin,
                                               serverinlen,
4457
                                               &state.interact,
4458 4459
                                               &clientout,
                                               &clientoutlen)) < 0)
4460
            goto cleanup;
4461

4462
        /* Need to gather some credentials from the client */
4463
        if (err == VIR_NET_SASL_INTERACT) {
4464
            if (remoteAuthInteract(conn, &state, auth) < 0) {
4465
                VIR_FREE(iret.mechlist);
4466 4467
                goto cleanup;
            }
4468
            continue;
4469 4470
        }

4471
        VIR_FREE(serverin);
4472 4473
        VIR_DEBUG("Client step result %d. Data %zu bytes %p",
                  err, clientoutlen, clientout);
4474 4475

        /* Previous server call showed completion & we're now locally complete too */
4476
        if (complete && err == VIR_NET_SASL_COMPLETE)
4477 4478 4479 4480
            break;

        /* Not done, prepare to talk with the server for another iteration */
        /* NB, distinction of NULL vs "" is *critical* in SASL */
4481
        memset(&pargs, 0, sizeof(pargs));
4482 4483 4484
        pargs.nil = clientout ? 0 : 1;
        pargs.data.data_val = (char*)clientout;
        pargs.data.data_len = clientoutlen;
4485 4486
        VIR_DEBUG("Server step with %zu bytes %p",
                  clientoutlen, clientout);
4487

4488 4489 4490 4491
        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)
4492
            goto cleanup;
4493 4494 4495 4496 4497 4498

        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;

4499 4500
        VIR_DEBUG("Client step result complete: %d. Data %zu bytes %p",
                  complete, serverinlen, serverin);
4501 4502

        /* This server call shows complete, and earlier client step was OK */
4503
        if (complete && err == VIR_NET_SASL_COMPLETE) {
4504
            VIR_FREE(serverin);
4505 4506 4507 4508
            break;
        }
    }

4509 4510
    /* Check for suitable SSF if not already secure (TLS or UNIX sock) */
    if (!priv->is_secure) {
4511
        if ((ssf = virNetSASLSessionGetKeySize(sasl)) < 0)
4512
            goto cleanup;
4513

4514
        VIR_DEBUG("SASL SSF value %d", ssf);
4515
        if (ssf < 56) { /* 56 == DES level, good for Kerberos */
4516 4517
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("negotiation SSF %d was not strong enough"), ssf);
4518
            goto cleanup;
4519
        }
4520
        priv->is_secure = 1;
4521 4522
    }

4523
 done:
4524
    VIR_DEBUG("SASL authentication complete");
4525
    virNetClientSetSASLSession(priv->client, sasl);
4526 4527 4528
    ret = 0;

 cleanup:
4529
    VIR_FREE(serverin);
4530

4531
    remoteAuthInteractStateClear(&state, true);
4532
    VIR_FREE(saslcb);
4533 4534
    virObjectUnref(sasl);
    virObjectUnref(saslCtxt);
4535

4536
    return ret;
4537
}
4538
#endif /* WITH_SASL */
4539

4540

4541 4542
#if WITH_POLKIT
# if WITH_POLKIT1
4543
static int
4544 4545
remoteAuthPolkit(virConnectPtr conn, struct private_data *priv,
                 virConnectAuthPtr auth ATTRIBUTE_UNUSED)
4546 4547
{
    remote_auth_polkit_ret ret;
4548
    VIR_DEBUG("Client initialize PolicyKit-1 authentication");
4549

4550 4551 4552 4553
    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) {
4554 4555 4556
        return -1; /* virError already set by call */
    }

4557
    VIR_DEBUG("PolicyKit-1 authentication complete");
4558 4559
    return 0;
}
4560
# elif WITH_POLKIT0
4561 4562 4563
/* Perform the PolicyKit authentication process
 */
static int
4564 4565
remoteAuthPolkit(virConnectPtr conn, struct private_data *priv,
                 virConnectAuthPtr auth)
4566 4567
{
    remote_auth_polkit_ret ret;
4568 4569
    size_t i;
    int allowcb = 0;
4570 4571 4572 4573 4574 4575 4576 4577
    virConnectCredential cred = {
        VIR_CRED_EXTERNAL,
        conn->flags & VIR_CONNECT_RO ? "org.libvirt.unix.monitor" : "org.libvirt.unix.manage",
        "PolicyKit",
        NULL,
        NULL,
        0,
    };
4578
    VIR_DEBUG("Client initialize PolicyKit-0 authentication");
4579

4580
    /* Check auth first and if it succeeds we are done. */
4581 4582 4583 4584
    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)
4585 4586 4587
        goto out;

    /* Auth failed.  Ask client to obtain it and check again. */
4588
    if (auth && auth->cb) {
4589
        /* Check if the necessary credential type for PolicyKit is supported */
4590
        for (i = 0; i < auth->ncredtype; i++) {
4591 4592 4593
            if (auth->credtype[i] == VIR_CRED_EXTERNAL)
                allowcb = 1;
        }
4594

4595
        if (allowcb) {
4596
            VIR_DEBUG("Client run callback for PolicyKit authentication");
4597 4598
            /* Run the authentication callback */
            if ((*(auth->cb))(&cred, 1, auth->cbdata) < 0) {
4599 4600
                virReportError(VIR_ERR_AUTH_FAILED, "%s",
                               _("Failed to collect auth credentials"));
4601 4602
                return -1;
            }
4603
        } else {
4604
            VIR_DEBUG("Client auth callback does not support PolicyKit");
4605
            return -1;
4606 4607
        }
    } else {
4608
        VIR_DEBUG("No auth callback provided");
4609
        return -1;
4610 4611
    }

4612 4613 4614 4615
    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) {
4616 4617 4618
        return -1; /* virError already set by call */
    }

4619
 out:
4620
    VIR_DEBUG("PolicyKit-0 authentication complete");
4621 4622
    return 0;
}
4623 4624
# endif /* WITH_POLKIT0 */
#endif /* WITH_POLKIT */
4625 4626
/*----------------------------------------------------------------------*/

4627 4628 4629 4630 4631
static int
remoteConnectDomainEventRegister(virConnectPtr conn,
                                 virConnectDomainEventCallback callback,
                                 void *opaque,
                                 virFreeCallback freecb)
4632
{
4633
    int callbackID;
4634
    int rv = -1;
4635
    struct private_data *priv = conn->privateData;
4636
    int count;
4637

4638 4639
    remoteDriverLock(priv);

4640 4641 4642 4643 4644
    if ((count = virDomainEventStateRegisterClient(conn, priv->eventState,
                                                   NULL,
                                                   VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                   VIR_DOMAIN_EVENT_CALLBACK(callback),
                                                   opaque, freecb, true,
4645 4646
                                                   &callbackID,
                                                   priv->serverEventFilter)) < 0)
4647
         goto done;
4648

4649
    if (count == 1) {
4650
        /* Tell the server when we are the first callback registering */
4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676
        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,
                                                callbackID);
                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,
                                                callbackID);
                goto done;
            }
4677
        }
4678 4679
    }

4680 4681
    rv = 0;

4682
 done:
4683
    remoteDriverUnlock(priv);
4684
    return rv;
4685 4686
}

4687

4688 4689 4690
static int
remoteConnectDomainEventDeregister(virConnectPtr conn,
                                   virConnectDomainEventCallback callback)
4691 4692
{
    struct private_data *priv = conn->privateData;
4693
    int rv = -1;
4694 4695 4696
    remote_connect_domain_event_callback_deregister_any_args args;
    int callbackID;
    int remoteID;
4697
    int count;
4698

4699 4700
    remoteDriverLock(priv);

4701 4702 4703 4704 4705 4706 4707
    if ((callbackID = virDomainEventStateCallbackID(conn, priv->eventState,
                                                    callback,
                                                    &remoteID)) < 0)
        goto done;

    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
                                                 callbackID)) < 0)
4708
        goto done;
4709

4710
    if (count == 0) {
4711
        /* Tell the server when we are the last callback deregistering */
4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725
        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;
        }
4726 4727
    }

4728 4729
    rv = 0;

4730
 done:
4731
    remoteDriverUnlock(priv);
4732
    return rv;
4733
}
4734

4735

4736 4737 4738 4739 4740 4741 4742 4743 4744
static void
remoteEventQueue(struct private_data *priv, virObjectEventPtr event,
                 int remoteID)
{
    if (event)
        virObjectEventStateQueueRemote(priv->eventState, event, remoteID);
}


4745
static void
4746 4747 4748
remoteDomainBuildEventLifecycleHelper(virConnectPtr conn,
                                      remote_domain_event_lifecycle_msg *msg,
                                      int callbackID)
4749
{
4750
    struct private_data *priv = conn->privateData;
4751
    virDomainPtr dom;
4752
    virObjectEventPtr event = NULL;
4753

4754
    dom = get_nonnull_domain(conn, msg->dom);
4755
    if (!dom)
4756
        return;
4757

4758
    event = virDomainEventLifecycleNewFromDom(dom, msg->event, msg->detail);
4759
    virObjectUnref(dom);
4760

4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779
    remoteEventQueue(priv, event, callbackID);
}
static void
remoteDomainBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_lifecycle_msg *msg = evdata;
    remoteDomainBuildEventLifecycleHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                        virNetClientPtr client ATTRIBUTE_UNUSED,
                                        void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_lifecycle_msg *msg = evdata;
    remoteDomainBuildEventLifecycleHelper(conn, &msg->msg, msg->callbackID);
4780 4781 4782
}


4783
static void
4784 4785 4786
remoteDomainBuildEventRebootHelper(virConnectPtr conn,
                                   remote_domain_event_reboot_msg *msg,
                                   int callbackID)
4787
{
4788
    struct private_data *priv = conn->privateData;
4789
    virDomainPtr dom;
4790
    virObjectEventPtr event = NULL;
4791

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

    event = virDomainEventRebootNewFromDom(dom);
4797
    virObjectUnref(dom);
4798

4799
    remoteEventQueue(priv, event, callbackID);
4800
}
4801
static void
4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813
remoteDomainBuildEventReboot(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                             virNetClientPtr client ATTRIBUTE_UNUSED,
                             void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_reboot_msg *msg = evdata;
    remoteDomainBuildEventRebootHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackReboot(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                     virNetClientPtr client ATTRIBUTE_UNUSED,
                                     void *evdata, void *opaque)
4814
{
4815
    virConnectPtr conn = opaque;
4816 4817 4818 4819 4820 4821 4822 4823 4824
    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)
{
4825
    struct private_data *priv = conn->privateData;
4826
    virDomainPtr dom;
4827
    virObjectEventPtr event = NULL;
4828

4829
    dom = get_nonnull_domain(conn, msg->dom);
4830
    if (!dom)
4831
        return;
4832

4833
    event = virDomainEventRTCChangeNewFromDom(dom, msg->offset);
4834
    virObjectUnref(dom);
4835

4836
    remoteEventQueue(priv, event, callbackID);
4837
}
4838
static void
4839 4840 4841
remoteDomainBuildEventRTCChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
4842
{
4843
    virConnectPtr conn = opaque;
4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861
    remote_domain_event_rtc_change_msg *msg = evdata;
    remoteDomainBuildEventRTCChangeHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackRTCChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                        virNetClientPtr client ATTRIBUTE_UNUSED,
                                        void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_rtc_change_msg *msg = evdata;
    remoteDomainBuildEventRTCChangeHelper(conn, &msg->msg, msg->callbackID);
}

static void
remoteDomainBuildEventWatchdogHelper(virConnectPtr conn,
                                     remote_domain_event_watchdog_msg *msg,
                                     int callbackID)
{
4862
    struct private_data *priv = conn->privateData;
4863
    virDomainPtr dom;
4864
    virObjectEventPtr event = NULL;
4865

4866
    dom = get_nonnull_domain(conn, msg->dom);
4867
    if (!dom)
4868
        return;
4869

4870
    event = virDomainEventWatchdogNewFromDom(dom, msg->action);
4871
    virObjectUnref(dom);
4872

4873
    remoteEventQueue(priv, event, callbackID);
4874
}
4875
static void
4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887
remoteDomainBuildEventWatchdog(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_watchdog_msg *msg = evdata;
    remoteDomainBuildEventWatchdogHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackWatchdog(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                       virNetClientPtr client ATTRIBUTE_UNUSED,
                                       void *evdata, void *opaque)
4888
{
4889
    virConnectPtr conn = opaque;
4890 4891 4892 4893 4894 4895 4896 4897 4898
    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)
{
4899
    struct private_data *priv = conn->privateData;
4900
    virDomainPtr dom;
4901
    virObjectEventPtr event = NULL;
4902

4903
    dom = get_nonnull_domain(conn, msg->dom);
4904
    if (!dom)
4905
        return;
4906 4907

    event = virDomainEventIOErrorNewFromDom(dom,
4908 4909 4910
                                            msg->srcPath,
                                            msg->devAlias,
                                            msg->action);
4911
    virObjectUnref(dom);
4912

4913
    remoteEventQueue(priv, event, callbackID);
4914
}
4915
static void
4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927
remoteDomainBuildEventIOError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                              virNetClientPtr client ATTRIBUTE_UNUSED,
                              void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_io_error_msg *msg = evdata;
    remoteDomainBuildEventIOErrorHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackIOError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                      virNetClientPtr client ATTRIBUTE_UNUSED,
                                      void *evdata, void *opaque)
4928
{
4929
    virConnectPtr conn = opaque;
4930 4931 4932 4933 4934 4935 4936 4937 4938
    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)
{
4939
    struct private_data *priv = conn->privateData;
4940
    virDomainPtr dom;
4941
    virObjectEventPtr event = NULL;
4942

4943
    dom = get_nonnull_domain(conn, msg->dom);
4944
    if (!dom)
4945
        return;
4946 4947

    event = virDomainEventIOErrorReasonNewFromDom(dom,
4948 4949 4950 4951
                                                  msg->srcPath,
                                                  msg->devAlias,
                                                  msg->action,
                                                  msg->reason);
4952

4953
    virObjectUnref(dom);
4954

4955
    remoteEventQueue(priv, event, callbackID);
4956
}
4957
static void
4958 4959 4960
remoteDomainBuildEventIOErrorReason(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
4961 4962
{
    virConnectPtr conn = opaque;
4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980
    remote_domain_event_io_error_reason_msg *msg = evdata;
    remoteDomainBuildEventIOErrorReasonHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackIOErrorReason(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                            virNetClientPtr client ATTRIBUTE_UNUSED,
                                            void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_io_error_reason_msg *msg = evdata;
    remoteDomainBuildEventIOErrorReasonHelper(conn, &msg->msg, msg->callbackID);
}

static void
remoteDomainBuildEventBlockJobHelper(virConnectPtr conn,
                                     remote_domain_event_block_job_msg *msg,
                                     int callbackID)
{
4981 4982
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
4983
    virObjectEventPtr event = NULL;
4984 4985 4986 4987 4988 4989 4990 4991

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

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

4992
    virObjectUnref(dom);
4993

4994
    remoteEventQueue(priv, event, callbackID);
4995
}
4996
static void
4997
remoteDomainBuildEventBlockJob(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
4998 4999
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
5000
{
5001
    virConnectPtr conn = opaque;
5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013
    remote_domain_event_block_job_msg *msg = evdata;
    remoteDomainBuildEventBlockJobHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackBlockJob(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                       virNetClientPtr client ATTRIBUTE_UNUSED,
                                       void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_block_job_msg *msg = evdata;
    remoteDomainBuildEventBlockJobHelper(conn, &msg->msg, msg->callbackID);
}
5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031
static void
remoteDomainBuildEventBlockJob2(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_block_job_2_msg *msg = evdata;
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;

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

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

5032
    virObjectUnref(dom);
5033 5034 5035

    remoteEventQueue(priv, event, msg->callbackID);
}
5036 5037 5038 5039 5040 5041

static void
remoteDomainBuildEventGraphicsHelper(virConnectPtr conn,
                                     remote_domain_event_graphics_msg *msg,
                                     int callbackID)
{
5042
    struct private_data *priv = conn->privateData;
5043
    virDomainPtr dom;
5044
    virObjectEventPtr event = NULL;
5045 5046 5047
    virDomainEventGraphicsAddressPtr localAddr = NULL;
    virDomainEventGraphicsAddressPtr remoteAddr = NULL;
    virDomainEventGraphicsSubjectPtr subject = NULL;
5048
    size_t i;
5049

5050
    dom = get_nonnull_domain(conn, msg->dom);
5051
    if (!dom)
5052
        return;
5053 5054

    if (VIR_ALLOC(localAddr) < 0)
5055
        goto error;
5056
    localAddr->family = msg->local.family;
5057 5058 5059
    if (VIR_STRDUP(localAddr->service, msg->local.service) < 0 ||
        VIR_STRDUP(localAddr->node, msg->local.node) < 0)
        goto error;
5060 5061

    if (VIR_ALLOC(remoteAddr) < 0)
5062
        goto error;
5063
    remoteAddr->family = msg->remote.family;
5064 5065 5066
    if (VIR_STRDUP(remoteAddr->service, msg->remote.service) < 0 ||
        VIR_STRDUP(remoteAddr->node, msg->remote.node) < 0)
        goto error;
5067 5068

    if (VIR_ALLOC(subject) < 0)
5069
        goto error;
5070
    if (VIR_ALLOC_N(subject->identities, msg->subject.subject_len) < 0)
5071
        goto error;
5072
    subject->nidentity = msg->subject.subject_len;
5073
    for (i = 0; i < subject->nidentity; i++) {
5074 5075 5076
        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;
5077 5078 5079
    }

    event = virDomainEventGraphicsNewFromDom(dom,
5080
                                             msg->phase,
5081 5082
                                             localAddr,
                                             remoteAddr,
5083
                                             msg->authScheme,
5084 5085
                                             subject);

5086
    virObjectUnref(dom);
5087

5088
    remoteEventQueue(priv, event, callbackID);
5089
    return;
5090

5091
 error:
5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102
    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) {
5103
        for (i = 0; i < subject->nidentity; i++) {
5104 5105 5106 5107 5108 5109
            VIR_FREE(subject->identities[i].type);
            VIR_FREE(subject->identities[i].name);
        }
        VIR_FREE(subject->identities);
        VIR_FREE(subject);
    }
5110
    virObjectUnref(dom);
5111
    return;
5112
}
5113
static void
5114 5115 5116
remoteDomainBuildEventGraphics(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
5117
{
5118
    virConnectPtr conn = opaque;
5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136
    remote_domain_event_graphics_msg *msg = evdata;
    remoteDomainBuildEventGraphicsHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackGraphics(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                       virNetClientPtr client ATTRIBUTE_UNUSED,
                                       void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_graphics_msg *msg = evdata;
    remoteDomainBuildEventGraphicsHelper(conn, &msg->msg, msg->callbackID);
}

static void
remoteDomainBuildEventControlErrorHelper(virConnectPtr conn,
                                         remote_domain_event_control_error_msg *msg,
                                         int callbackID)
{
5137
    struct private_data *priv = conn->privateData;
5138
    virDomainPtr dom;
5139
    virObjectEventPtr event = NULL;
5140

5141
    dom = get_nonnull_domain(conn, msg->dom);
5142
    if (!dom)
5143
        return;
5144 5145 5146

    event = virDomainEventControlErrorNewFromDom(dom);

5147
    virObjectUnref(dom);
5148

5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167
    remoteEventQueue(priv, event, callbackID);
}
static void
remoteDomainBuildEventControlError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                   virNetClientPtr client ATTRIBUTE_UNUSED,
                                   void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_control_error_msg *msg = evdata;
    remoteDomainBuildEventControlErrorHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackControlError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                           virNetClientPtr client ATTRIBUTE_UNUSED,
                                           void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_control_error_msg *msg = evdata;
    remoteDomainBuildEventControlErrorHelper(conn, &msg->msg, msg->callbackID);
5168 5169 5170
}


5171
static void
5172 5173 5174
remoteDomainBuildEventDiskChangeHelper(virConnectPtr conn,
                                       remote_domain_event_disk_change_msg *msg,
                                       int callbackID)
5175 5176 5177
{
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5178
    virObjectEventPtr event = NULL;
5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189

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

5190
    virObjectUnref(dom);
5191

5192
    remoteEventQueue(priv, event, callbackID);
5193
}
5194
static void
5195
remoteDomainBuildEventDiskChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
5196 5197 5198 5199
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218
    remote_domain_event_disk_change_msg *msg = evdata;
    remoteDomainBuildEventDiskChangeHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackDiskChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                         virNetClientPtr client ATTRIBUTE_UNUSED,
                                         void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_disk_change_msg *msg = evdata;
    remoteDomainBuildEventDiskChangeHelper(conn, &msg->msg, msg->callbackID);
}


static void
remoteDomainBuildEventTrayChangeHelper(virConnectPtr conn,
                                       remote_domain_event_tray_change_msg *msg,
                                       int callbackID)
{
5219 5220
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5221
    virObjectEventPtr event = NULL;
5222 5223 5224 5225 5226 5227 5228 5229 5230

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

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

5231
    virObjectUnref(dom);
5232

5233
    remoteEventQueue(priv, event, callbackID);
5234
}
O
Osier Yang 已提交
5235
static void
5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247
remoteDomainBuildEventTrayChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_tray_change_msg *msg = evdata;
    remoteDomainBuildEventTrayChangeHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackTrayChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                         virNetClientPtr client ATTRIBUTE_UNUSED,
                                         void *evdata, void *opaque)
O
Osier Yang 已提交
5248 5249
{
    virConnectPtr conn = opaque;
5250 5251 5252 5253 5254 5255 5256
    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 已提交
5257 5258
                                     int callbackID,
                                     int reason)
5259
{
O
Osier Yang 已提交
5260 5261
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5262
    virObjectEventPtr event = NULL;
O
Osier Yang 已提交
5263 5264 5265 5266 5267

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

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

5270
    virObjectUnref(dom);
O
Osier Yang 已提交
5271

5272
    remoteEventQueue(priv, event, callbackID);
O
Osier Yang 已提交
5273
}
O
Osier Yang 已提交
5274
static void
5275 5276 5277 5278 5279 5280
remoteDomainBuildEventPMWakeup(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_pmwakeup_msg *msg = evdata;
E
Eric Blake 已提交
5281
    remoteDomainBuildEventPMWakeupHelper(conn, msg, -1, 0);
5282 5283 5284 5285 5286
}
static void
remoteDomainBuildEventCallbackPMWakeup(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                       virNetClientPtr client ATTRIBUTE_UNUSED,
                                       void *evdata, void *opaque)
O
Osier Yang 已提交
5287 5288
{
    virConnectPtr conn = opaque;
5289
    remote_domain_event_callback_pmwakeup_msg *msg = evdata;
E
Eric Blake 已提交
5290 5291
    remoteDomainBuildEventPMWakeupHelper(conn, &msg->msg, msg->callbackID,
                                         msg->reason);
5292 5293 5294 5295 5296
}

static void
remoteDomainBuildEventPMSuspendHelper(virConnectPtr conn,
                                      remote_domain_event_pmsuspend_msg *msg,
E
Eric Blake 已提交
5297 5298
                                      int callbackID,
                                      int reason)
5299
{
O
Osier Yang 已提交
5300 5301
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5302
    virObjectEventPtr event = NULL;
O
Osier Yang 已提交
5303 5304 5305 5306 5307

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

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

5310
    virObjectUnref(dom);
O
Osier Yang 已提交
5311

5312 5313 5314 5315 5316 5317 5318 5319 5320
    remoteEventQueue(priv, event, callbackID);
}
static void
remoteDomainBuildEventPMSuspend(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_pmsuspend_msg *msg = evdata;
E
Eric Blake 已提交
5321
    remoteDomainBuildEventPMSuspendHelper(conn, msg, -1, 0);
5322 5323 5324 5325 5326 5327 5328 5329
}
static void
remoteDomainBuildEventCallbackPMSuspend(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                        virNetClientPtr client ATTRIBUTE_UNUSED,
                                        void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_pmsuspend_msg *msg = evdata;
E
Eric Blake 已提交
5330 5331
    remoteDomainBuildEventPMSuspendHelper(conn, &msg->msg, msg->callbackID,
                                          msg->reason);
O
Osier Yang 已提交
5332 5333
}

5334 5335

static void
5336 5337 5338
remoteDomainBuildEventBalloonChangeHelper(virConnectPtr conn,
                                          remote_domain_event_balloon_change_msg *msg,
                                          int callbackID)
5339 5340 5341
{
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5342
    virObjectEventPtr event = NULL;
5343 5344 5345 5346 5347 5348

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

    event = virDomainEventBalloonChangeNewFromDom(dom, msg->actual);
5349
    virObjectUnref(dom);
5350

5351
    remoteEventQueue(priv, event, callbackID);
5352
}
5353
static void
5354
remoteDomainBuildEventBalloonChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
5355 5356 5357 5358
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375
    remote_domain_event_balloon_change_msg *msg = evdata;
    remoteDomainBuildEventBalloonChangeHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackBalloonChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                            virNetClientPtr client ATTRIBUTE_UNUSED,
                                            void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_balloon_change_msg *msg = evdata;
    remoteDomainBuildEventBalloonChangeHelper(conn, &msg->msg, msg->callbackID);
}


static void
remoteDomainBuildEventPMSuspendDiskHelper(virConnectPtr conn,
                                          remote_domain_event_pmsuspend_disk_msg *msg,
E
Eric Blake 已提交
5376 5377
                                          int callbackID,
                                          int reason)
5378
{
5379 5380
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5381
    virObjectEventPtr event = NULL;
5382 5383 5384 5385 5386

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

E
Eric Blake 已提交
5387
    event = virDomainEventPMSuspendDiskNewFromDom(dom, reason);
5388

5389
    virObjectUnref(dom);
5390

5391
    remoteEventQueue(priv, event, callbackID);
5392
}
5393
static void
5394
remoteDomainBuildEventPMSuspendDisk(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
5395 5396 5397 5398
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
5399
    remote_domain_event_pmsuspend_disk_msg *msg = evdata;
E
Eric Blake 已提交
5400
    remoteDomainBuildEventPMSuspendDiskHelper(conn, msg, -1, 0);
5401 5402 5403 5404 5405 5406 5407 5408
}
static void
remoteDomainBuildEventCallbackPMSuspendDisk(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                            virNetClientPtr client ATTRIBUTE_UNUSED,
                                            void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_pmsuspend_disk_msg *msg = evdata;
E
Eric Blake 已提交
5409 5410
    remoteDomainBuildEventPMSuspendDiskHelper(conn, &msg->msg, msg->callbackID,
                                              msg->reason);
5411 5412 5413 5414 5415 5416 5417 5418
}


static void
remoteDomainBuildEventDeviceRemovedHelper(virConnectPtr conn,
                                          remote_domain_event_device_removed_msg *msg,
                                          int callbackID)
{
5419 5420
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
5421
    virObjectEventPtr event = NULL;
5422 5423 5424 5425 5426 5427 5428

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

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

5429
    virObjectUnref(dom);
5430

5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449
    remoteEventQueue(priv, event, callbackID);
}
static void
remoteDomainBuildEventDeviceRemoved(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_device_removed_msg *msg = evdata;
    remoteDomainBuildEventDeviceRemovedHelper(conn, msg, -1);
}
static void
remoteDomainBuildEventCallbackDeviceRemoved(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                            virNetClientPtr client ATTRIBUTE_UNUSED,
                                            void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_device_removed_msg *msg = evdata;
    remoteDomainBuildEventDeviceRemovedHelper(conn, &msg->msg, msg->callbackID);
5450 5451 5452
}


5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479
static void
remoteDomainBuildEventCallbackTunable(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                      virNetClientPtr client ATTRIBUTE_UNUSED,
                                      void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_tunable_msg *msg = evdata;
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virTypedParameterPtr params = NULL;
    int nparams = 0;
    virObjectEventPtr event = NULL;

    if (remoteDeserializeTypedParameters(msg->params.params_val,
                                         msg->params.params_len,
                                         REMOTE_DOMAIN_EVENT_TUNABLE_MAX,
                                         &params, &nparams) < 0)
        return;

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

    event = virDomainEventTunableNewFromDom(dom, params, nparams);

5480
    virObjectUnref(dom);
5481 5482 5483 5484 5485

    remoteEventQueue(priv, event, msg->callbackID);
}


5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502
static void
remoteDomainBuildEventCallbackAgentLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                             virNetClientPtr client ATTRIBUTE_UNUSED,
                                             void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    remote_domain_event_callback_agent_lifecycle_msg *msg = evdata;
    struct private_data *priv = conn->privateData;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;

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

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

5503
    virObjectUnref(dom);
5504 5505 5506 5507

    remoteEventQueue(priv, event, msg->callbackID);
}

5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522
static void
remoteNetworkBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_network_event_lifecycle_msg *msg = evdata;
    virNetworkPtr net;
    virObjectEventPtr event = NULL;

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

5523 5524
    event = virNetworkEventLifecycleNew(net->name, net->uuid, msg->event,
                                        msg->detail);
5525
    virObjectUnref(net);
5526

5527
    remoteEventQueue(priv, event, msg->callbackID);
5528 5529 5530
}


5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549
static void
remoteDomainBuildQemuMonitorEvent(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                  virNetClientPtr client ATTRIBUTE_UNUSED,
                                  void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    qemu_domain_monitor_event_msg *msg = evdata;
    virDomainPtr dom;
    virObjectEventPtr event = NULL;

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

    event = virDomainQemuMonitorEventNew(dom->id, dom->name, dom->uuid,
                                         msg->event, msg->seconds,
                                         msg->micros,
                                         msg->details ? *msg->details : NULL);
5550
    virObjectUnref(dom);
5551 5552 5553 5554 5555

    remoteEventQueue(priv, event, msg->callbackID);
}


5556
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
5557
remoteSecretOpen(virConnectPtr conn, virConnectAuthPtr auth ATTRIBUTE_UNUSED,
5558
                 unsigned int flags)
5559
{
5560 5561
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

5562
    return remoteGenericOpen(conn);
5563 5564 5565
}

static int
5566
remoteSecretClose(virConnectPtr conn ATTRIBUTE_UNUSED)
5567
{
5568
    return 0;
5569 5570 5571
}

static unsigned char *
5572 5573
remoteSecretGetValue(virSecretPtr secret, size_t *value_size,
                     unsigned int flags, unsigned int internalFlags)
5574 5575 5576 5577
{
    unsigned char *rv = NULL;
    remote_secret_get_value_args args;
    remote_secret_get_value_ret ret;
5578
    struct private_data *priv = secret->conn->privateData;
5579

5580
    remoteDriverLock(priv);
5581

5582 5583
    /* internalFlags intentionally do not go over the wire */
    if (internalFlags) {
5584
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("no internalFlags support"));
5585 5586 5587
        goto done;
    }

5588
    make_nonnull_secret(&args.secret, secret);
5589 5590
    args.flags = flags;

5591 5592 5593 5594
    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)
5595 5596 5597 5598 5599
        goto done;

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

5600
 done:
5601
    remoteDriverUnlock(priv);
5602 5603 5604
    return rv;
}

5605 5606 5607 5608 5609 5610

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

5616
    if (virNetClientStreamRaiseError(privst))
5617 5618 5619 5620 5621
        return -1;

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

5623 5624 5625 5626 5627
    rv = virNetClientStreamSendPacket(privst,
                                      priv->client,
                                      VIR_NET_CONTINUE,
                                      data,
                                      nbytes);
5628

5629 5630
    remoteDriverLock(priv);
    priv->localUses--;
5631 5632 5633 5634 5635 5636 5637 5638 5639 5640
    remoteDriverUnlock(priv);
    return rv;
}


static int
remoteStreamRecv(virStreamPtr st,
                 char *data,
                 size_t nbytes)
{
5641
    VIR_DEBUG("st=%p data=%p nbytes=%zu", st, data, nbytes);
5642
    struct private_data *priv = st->conn->privateData;
5643
    virNetClientStreamPtr privst = st->privateData;
5644
    int rv;
5645

5646
    if (virNetClientStreamRaiseError(privst))
5647 5648 5649 5650 5651
        return -1;

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

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

5659
    VIR_DEBUG("Done %d", rv);
5660

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

5667 5668 5669 5670 5671 5672
struct remoteStreamCallbackData {
    virStreamPtr st;
    virStreamEventCallback cb;
    void *opaque;
    virFreeCallback ff;
};
5673

5674 5675 5676
static void remoteStreamEventCallback(virNetClientStreamPtr stream ATTRIBUTE_UNUSED,
                                      int events,
                                      void *opaque)
5677
{
5678
    struct remoteStreamCallbackData *cbdata = opaque;
5679

5680
    (cbdata->cb)(cbdata->st, events, cbdata->opaque);
5681 5682 5683
}


5684
static void remoteStreamCallbackFree(void *opaque)
5685
{
5686 5687 5688 5689 5690
    struct remoteStreamCallbackData *cbdata = opaque;

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

5691
    virObjectUnref(cbdata->st);
5692
    VIR_FREE(opaque);
5693 5694 5695
}


5696
static int
5697 5698 5699 5700 5701
remoteStreamEventAddCallback(virStreamPtr st,
                             int events,
                             virStreamEventCallback cb,
                             void *opaque,
                             virFreeCallback ff)
5702
{
5703
    struct private_data *priv = st->conn->privateData;
5704
    virNetClientStreamPtr privst = st->privateData;
5705
    int ret = -1;
5706
    struct remoteStreamCallbackData *cbdata;
5707

5708
    if (VIR_ALLOC(cbdata) < 0)
5709 5710 5711 5712 5713
        return -1;
    cbdata->cb = cb;
    cbdata->opaque = opaque;
    cbdata->ff = ff;
    cbdata->st = st;
5714 5715
    virStreamRef(st);

5716
    remoteDriverLock(priv);
5717

5718 5719 5720 5721 5722 5723 5724 5725
    if ((ret = virNetClientStreamEventAddCallback(privst,
                                                  events,
                                                  remoteStreamEventCallback,
                                                  cbdata,
                                                  remoteStreamCallbackFree)) < 0) {
        VIR_FREE(cbdata);
        goto cleanup;
    }
5726

5727
 cleanup:
5728
    remoteDriverUnlock(priv);
5729
    /* coverity[leaked_storage] - cbdata is not leaked */
5730
    return ret;
5731 5732
}

5733

5734
static int
5735 5736
remoteStreamEventUpdateCallback(virStreamPtr st,
                                int events)
5737
{
5738
    struct private_data *priv = st->conn->privateData;
5739
    virNetClientStreamPtr privst = st->privateData;
5740 5741 5742 5743
    int ret = -1;

    remoteDriverLock(priv);

5744
    ret = virNetClientStreamEventUpdateCallback(privst, events);
5745

5746 5747 5748
    remoteDriverUnlock(priv);
    return ret;
}
5749 5750 5751


static int
5752
remoteStreamEventRemoveCallback(virStreamPtr st)
5753
{
5754
    struct private_data *priv = st->conn->privateData;
5755
    virNetClientStreamPtr privst = st->privateData;
5756 5757 5758 5759
    int ret = -1;

    remoteDriverLock(priv);

5760
    ret = virNetClientStreamEventRemoveCallback(privst);
5761 5762 5763

    remoteDriverUnlock(priv);
    return ret;
5764 5765
}

5766

5767 5768 5769 5770
static int
remoteStreamFinish(virStreamPtr st)
{
    struct private_data *priv = st->conn->privateData;
5771
    virNetClientStreamPtr privst = st->privateData;
5772 5773 5774 5775
    int ret = -1;

    remoteDriverLock(priv);

5776
    if (virNetClientStreamRaiseError(privst))
5777 5778
        goto cleanup;

5779 5780 5781
    priv->localUses++;
    remoteDriverUnlock(priv);

5782 5783 5784 5785 5786
    ret = virNetClientStreamSendPacket(privst,
                                       priv->client,
                                       VIR_NET_OK,
                                       NULL,
                                       0);
5787

5788 5789 5790
    remoteDriverLock(priv);
    priv->localUses--;

5791
 cleanup:
5792
    virNetClientRemoveStream(priv->client, privst);
5793
    virObjectUnref(privst);
5794 5795
    st->privateData = NULL;
    st->driver = NULL;
5796 5797 5798 5799 5800

    remoteDriverUnlock(priv);
    return ret;
}

5801

5802 5803 5804 5805
static int
remoteStreamAbort(virStreamPtr st)
{
    struct private_data *priv = st->conn->privateData;
5806
    virNetClientStreamPtr privst = st->privateData;
5807 5808 5809 5810
    int ret = -1;

    remoteDriverLock(priv);

5811
    if (virNetClientStreamRaiseError(privst))
5812 5813
        goto cleanup;

5814 5815 5816
    priv->localUses++;
    remoteDriverUnlock(priv);

5817 5818 5819 5820 5821
    ret = virNetClientStreamSendPacket(privst,
                                       priv->client,
                                       VIR_NET_ERROR,
                                       NULL,
                                       0);
5822

5823 5824 5825
    remoteDriverLock(priv);
    priv->localUses--;

5826
 cleanup:
5827
    virNetClientRemoveStream(priv->client, privst);
5828
    virObjectUnref(privst);
5829 5830
    st->privateData = NULL;
    st->driver = NULL;
5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841

    remoteDriverUnlock(priv);
    return ret;
}


static virStreamDriver remoteStreamDrv = {
    .streamRecv = remoteStreamRecv,
    .streamSend = remoteStreamSend,
    .streamFinish = remoteStreamFinish,
    .streamAbort = remoteStreamAbort,
5842 5843 5844
    .streamEventAddCallback = remoteStreamEventAddCallback,
    .streamEventUpdateCallback = remoteStreamEventUpdateCallback,
    .streamEventRemoveCallback = remoteStreamEventRemoveCallback,
5845 5846
};

5847

5848 5849 5850 5851 5852 5853 5854
static int
remoteConnectDomainEventRegisterAny(virConnectPtr conn,
                                    virDomainPtr dom,
                                    int eventID,
                                    virConnectDomainEventGenericCallback callback,
                                    void *opaque,
                                    virFreeCallback freecb)
5855 5856 5857 5858
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
    int callbackID;
5859
    int count;
5860
    remote_nonnull_domain domain;
5861 5862 5863

    remoteDriverLock(priv);

5864 5865 5866
    if ((count = virDomainEventStateRegisterClient(conn, priv->eventState,
                                                   dom, eventID, callback,
                                                   opaque, freecb, false,
5867
                                                   &callbackID,
5868
                                                   priv->serverEventFilter)) < 0)
5869
        goto done;
5870 5871 5872

    /* If this is the first callback for this eventID, we need to enable
     * events on the server */
5873
    if (count == 1) {
5874
        if (priv->serverEventFilter) {
5875 5876 5877 5878 5879 5880 5881 5882 5883 5884
            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;
            }
5885

5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907
            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,
                                                callbackID);
                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,
                                                callbackID);
                goto done;
            }
5908 5909 5910 5911 5912
        }
    }

    rv = callbackID;

5913
 done:
5914 5915 5916 5917 5918
    remoteDriverUnlock(priv);
    return rv;
}


5919 5920 5921
static int
remoteConnectDomainEventDeregisterAny(virConnectPtr conn,
                                      int callbackID)
5922 5923 5924 5925
{
    struct private_data *priv = conn->privateData;
    int rv = -1;
    int eventID;
5926
    int remoteID;
5927
    int count;
5928 5929 5930

    remoteDriverLock(priv);

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

5935
    if ((count = virObjectEventStateDeregisterID(conn, priv->eventState,
5936
                                                 callbackID)) < 0)
5937
        goto done;
5938 5939 5940

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

5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961
            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;
        }
5962 5963 5964 5965
    }

    rv = 0;

5966
 done:
5967 5968 5969 5970
    remoteDriverUnlock(priv);
    return rv;
}

5971

5972 5973
/*----------------------------------------------------------------------*/

C
Chris Lalancette 已提交
5974
static int
5975
remoteDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd,
5976
                               char **result, unsigned int flags)
C
Chris Lalancette 已提交
5977 5978
{
    int rv = -1;
5979 5980
    qemu_domain_monitor_command_args args;
    qemu_domain_monitor_command_ret ret;
C
Chris Lalancette 已提交
5981 5982 5983 5984
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

5985
    make_nonnull_domain(&args.dom, domain);
C
Chris Lalancette 已提交
5986 5987 5988
    args.cmd = (char *)cmd;
    args.flags = flags;

5989
    memset(&ret, 0, sizeof(ret));
5990 5991 5992
    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 已提交
5993 5994
        goto done;

5995
    if (VIR_STRDUP(*result, ret.result) < 0)
C
Chris Lalancette 已提交
5996 5997 5998 5999
        goto cleanup;

    rv = 0;

6000
 cleanup:
6001
    xdr_free((xdrproc_t) xdr_qemu_domain_monitor_command_ret, (char *) &ret);
C
Chris Lalancette 已提交
6002

6003
 done:
C
Chris Lalancette 已提交
6004 6005 6006 6007
    remoteDriverUnlock(priv);
    return rv;
}

6008 6009 6010

static char *
remoteDomainMigrateBegin3(virDomainPtr domain,
6011
                          const char *xmlin,
6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027
                          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));

6028
    make_nonnull_domain(&args.dom, domain);
6029
    args.xmlin = xmlin == NULL ? NULL : (char **) &xmlin;
6030 6031 6032 6033
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;

6034 6035 6036
    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)
6037 6038 6039 6040
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6041 6042
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6043 6044 6045 6046 6047 6048 6049 6050
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

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

6051
 done:
6052 6053 6054
    remoteDriverUnlock(priv);
    return rv;

6055
 error:
6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091
    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;

6092 6093 6094 6095
    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)
6096 6097 6098 6099
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6100 6101
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6102 6103 6104 6105 6106 6107 6108
            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) {
6109 6110
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores uri_out"));
6111 6112 6113 6114 6115 6116 6117
            goto error;
        }
        *uri_out = *ret.uri_out; /* Caller frees. */
    }

    rv = 0;

6118
 done:
O
Osier Yang 已提交
6119
    VIR_FREE(ret.uri_out);
6120 6121
    remoteDriverUnlock(priv);
    return rv;
6122
 error:
6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145
    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;
6146
    virNetClientStreamPtr netst;
6147 6148 6149 6150 6151 6152

    remoteDriverLock(priv);

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

6153
    if (!(netst = virNetClientStreamNew(priv->remoteProgram,
6154
                                        REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3,
6155
                                        priv->counter)))
6156 6157
        goto done;

6158
    if (virNetClientAddStream(priv->client, netst) < 0) {
6159
        virObjectUnref(netst);
6160 6161 6162
        goto done;
    }

6163
    st->driver = &remoteStreamDrv;
6164
    st->privateData = netst;
6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175

    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) {
6176
        virNetClientRemoveStream(priv->client, netst);
6177
        virObjectUnref(netst);
6178 6179 6180 6181 6182
        goto done;
    }

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6183 6184
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6185 6186 6187 6188 6189 6190 6191 6192
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = 0;

6193
 done:
6194 6195 6196
    remoteDriverUnlock(priv);
    return rv;

6197
 error:
6198 6199 6200 6201 6202 6203 6204
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto done;
}


static int
remoteDomainMigratePerform3(virDomainPtr dom,
6205
                            const char *xmlin,
6206 6207 6208 6209
                            const char *cookiein,
                            int cookieinlen,
                            char **cookieout,
                            int *cookieoutlen,
6210
                            const char *dconnuri,
6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227
                            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);

6228
    args.xmlin = xmlin == NULL ? NULL : (char **) &xmlin;
6229 6230 6231 6232
    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;
6233 6234
    args.uri = uri == NULL ? NULL : (char **) &uri;
    args.dconnuri = dconnuri == NULL ? NULL : (char **) &dconnuri;
6235 6236
    args.resource = resource;

6237 6238 6239
    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)
6240 6241 6242 6243
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6244 6245
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6246 6247 6248 6249 6250 6251 6252 6253
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = 0;

6254
 done:
6255 6256 6257
    remoteDriverUnlock(priv);
    return rv;

6258
 error:
6259 6260 6261 6262 6263
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto done;
}


6264
static virDomainPtr
6265 6266 6267 6268 6269 6270
remoteDomainMigrateFinish3(virConnectPtr dconn,
                           const char *dname,
                           const char *cookiein,
                           int cookieinlen,
                           char **cookieout,
                           int *cookieoutlen,
6271
                           const char *dconnuri,
6272 6273
                           const char *uri,
                           unsigned long flags,
6274
                           int cancelled)
6275 6276 6277 6278
{
    remote_domain_migrate_finish3_args args;
    remote_domain_migrate_finish3_ret ret;
    struct private_data *priv = dconn->privateData;
6279
    virDomainPtr rv = NULL;
6280 6281 6282 6283 6284 6285 6286 6287 6288

    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;
6289 6290
    args.uri = uri == NULL ? NULL : (char **) &uri;
    args.dconnuri = dconnuri == NULL ? NULL : (char **) &dconnuri;
6291 6292 6293
    args.flags = flags;
    args.cancelled = cancelled;

6294 6295 6296
    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)
6297 6298
        goto done;

6299
    rv = get_nonnull_domain(dconn, ret.dom);
6300 6301 6302

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
6303 6304
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
6305 6306 6307 6308 6309 6310 6311 6312
            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;
    }

6313
    xdr_free((xdrproc_t) &xdr_remote_domain_migrate_finish3_ret, (char *) &ret);
6314

6315
 done:
6316 6317 6318
    remoteDriverUnlock(priv);
    return rv;

6319
 error:
6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339
    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));

6340
    make_nonnull_domain(&args.dom, domain);
6341 6342 6343 6344 6345
    args.cookie_in.cookie_in_len = cookieinlen;
    args.cookie_in.cookie_in_val = (char *) cookiein;
    args.flags = flags;
    args.cancelled = cancelled;

6346 6347 6348
    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)
6349 6350 6351 6352
        goto done;

    rv = 0;

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


6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409
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;

6410
 cleanup:
6411 6412 6413 6414
    virStringFreeList(retmodels);

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

6415
 done:
6416 6417 6418 6419 6420
    remoteDriverUnlock(priv);
    return rv;
}


6421 6422 6423 6424 6425 6426 6427 6428 6429
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;
6430 6431
    int fdin[] = { fd };
    size_t fdinlen = ARRAY_CARDINALITY(fdin);
6432 6433 6434

    remoteDriverLock(priv);

6435
    make_nonnull_domain(&args.dom, dom);
6436 6437 6438
    args.idx = idx;
    args.flags = flags;

6439 6440 6441 6442 6443 6444
    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)
6445 6446 6447 6448
        goto done;

    rv = 0;

6449
 done:
6450 6451 6452 6453 6454 6455
    remoteDriverUnlock(priv);

    return rv;
}


6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481
static int
remoteDomainOpenGraphicsFD(virDomainPtr dom,
                           unsigned int idx,
                           unsigned int flags)
{
    int rv = -1;
    remote_domain_open_graphics_args args;
    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) {
6482 6483 6484 6485 6486 6487 6488 6489 6490
        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"));
        }
6491 6492
        goto done;
    }
6493
    rv = fdout[0];
6494 6495

 done:
6496
    VIR_FREE(fdout);
6497 6498 6499 6500 6501 6502
    remoteDriverUnlock(priv);

    return rv;
}


6503
static int
6504
remoteConnectSetKeepAlive(virConnectPtr conn, int interval, unsigned int count)
6505 6506 6507 6508 6509 6510
{
    struct private_data *priv = conn->privateData;
    int ret = -1;

    remoteDriverLock(priv);
    if (!virNetClientKeepAliveIsSupported(priv->client)) {
6511 6512 6513
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("the caller doesn't support keepalive protocol;"
                         " perhaps it's missing event loop implementation"));
6514 6515 6516 6517 6518 6519 6520 6521
        goto cleanup;
    }

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

6522 6523 6524 6525 6526 6527
    if (interval > 0) {
        ret = virNetClientKeepAliveStart(priv->client, interval, count);
    } else {
        virNetClientKeepAliveStop(priv->client);
        ret = 0;
    }
6528

6529
 cleanup:
6530 6531 6532 6533 6534
    remoteDriverUnlock(priv);
    return ret;
}


6535
static int
6536
remoteConnectIsAlive(virConnectPtr conn)
6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551
{
    struct private_data *priv = conn->privateData;
    bool ret;

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

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


6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568
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;

6569
    memset(&ret, 0, sizeof(ret));
6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586

    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;

6587
 cleanup:
6588 6589
    xdr_free((xdrproc_t) xdr_remote_domain_get_disk_errors_ret, (char *) &ret);

6590
 done:
6591 6592 6593 6594
    remoteDriverUnlock(priv);
    return rv;
}

6595
#include "remote_client_bodies.h"
6596
#include "lxc_client_bodies.h"
6597
#include "qemu_client_bodies.h"
6598

6599 6600 6601 6602 6603
/*
 * Serial a set of arguments into a method call message,
 * send that to the server and wait for reply
 */
static int
6604 6605 6606 6607 6608 6609 6610 6611 6612 6613
callFull(virConnectPtr conn ATTRIBUTE_UNUSED,
         struct private_data *priv,
         unsigned int flags,
         int *fdin,
         size_t fdinlen,
         int **fdout,
         size_t *fdoutlen,
         int proc_nr,
         xdrproc_t args_filter, char *args,
         xdrproc_t ret_filter, char *ret)
6614
{
6615
    int rv;
6616
    virNetClientProgramPtr prog;
6617 6618 6619 6620
    int counter = priv->counter++;
    virNetClientPtr client = priv->client;
    priv->localUses++;

6621 6622 6623 6624 6625 6626 6627
    if (flags & REMOTE_CALL_QEMU)
        prog = priv->qemuProgram;
    else if (flags & REMOTE_CALL_LXC)
        prog = priv->lxcProgram;
    else
        prog = priv->remoteProgram;

6628 6629 6630 6631 6632 6633 6634 6635 6636
    /* 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,
6637 6638
                                 fdinlen, fdin,
                                 fdoutlen, fdout,
6639 6640 6641 6642
                                 args_filter, args,
                                 ret_filter, ret);
    remoteDriverLock(priv);
    priv->localUses--;
6643

6644
    return rv;
6645 6646
}

6647
static int
6648 6649 6650 6651 6652 6653
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)
6654
{
6655 6656 6657 6658 6659 6660
    return callFull(conn, priv, flags,
                    NULL, 0,
                    NULL, NULL,
                    proc_nr,
                    args_filter, args,
                    ret_filter, ret);
6661 6662
}

6663

6664
static int
6665 6666 6667 6668
remoteDomainGetInterfaceParameters(virDomainPtr domain,
                                   const char *device,
                                   virTypedParameterPtr params, int *nparams,
                                   unsigned int flags)
6669 6670 6671 6672 6673 6674 6675 6676
{
    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);

6677
    make_nonnull_domain(&args.dom, domain);
6678 6679 6680 6681
    args.device = (char *)device;
    args.nparams = *nparams;
    args.flags = flags;

6682 6683 6684 6685
    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)
6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699
        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;
    }

    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
                                         REMOTE_DOMAIN_INTERFACE_PARAMETERS_MAX,
6700
                                         &params,
6701 6702 6703 6704 6705
                                         nparams) < 0)
        goto cleanup;

    rv = 0;

6706
 cleanup:
6707 6708
    xdr_free((xdrproc_t) xdr_remote_domain_get_interface_parameters_ret,
             (char *) &ret);
6709
 done:
6710 6711 6712 6713
    remoteDriverUnlock(priv);
    return rv;
}

6714 6715 6716 6717 6718 6719
static int
remoteDomainListAllSnapshots(virDomainPtr dom,
                             virDomainSnapshotPtr **snapshots,
                             unsigned int flags)
{
    int rv = -1;
6720
    size_t i;
6721 6722 6723 6724 6725 6726 6727 6728
    virDomainSnapshotPtr *snaps = NULL;
    remote_domain_list_all_snapshots_args args;
    remote_domain_list_all_snapshots_ret ret;

    struct private_data *priv = dom->conn->privateData;

    remoteDriverLock(priv);

6729
    make_nonnull_domain(&args.dom, dom);
6730 6731 6732 6733
    args.need_results = !!snapshots;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
6734 6735 6736 6737 6738 6739 6740 6741
    if (call(dom->conn,
             priv,
             0,
             REMOTE_PROC_DOMAIN_LIST_ALL_SNAPSHOTS,
             (xdrproc_t) xdr_remote_domain_list_all_snapshots_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_list_all_snapshots_ret,
             (char *) &ret) == -1)
6742 6743
        goto done;

6744 6745 6746 6747 6748 6749 6750 6751
    if (ret.snapshots.snapshots_len > REMOTE_DOMAIN_SNAPSHOT_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domain snapshots '%d' for limit '%d'"),
                       ret.snapshots.snapshots_len,
                       REMOTE_DOMAIN_SNAPSHOT_LIST_MAX);
        goto cleanup;
    }

6752
    if (snapshots) {
6753
        if (VIR_ALLOC_N(snaps, ret.snapshots.snapshots_len + 1) < 0)
6754 6755 6756
            goto cleanup;
        for (i = 0; i < ret.snapshots.snapshots_len; i++) {
            snaps[i] = get_nonnull_domain_snapshot(dom, ret.snapshots.snapshots_val[i]);
6757
            if (!snaps[i])
6758 6759 6760 6761 6762 6763 6764 6765
                goto cleanup;
        }
        *snapshots = snaps;
        snaps = NULL;
    }

    rv = ret.ret;

6766
 cleanup:
6767 6768 6769 6770 6771 6772 6773 6774 6775
    if (snaps) {
        for (i = 0; i < ret.snapshots.snapshots_len; i++)
            if (snaps[i])
                virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }

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

6776
 done:
6777 6778 6779 6780 6781 6782 6783 6784 6785 6786
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteDomainSnapshotListAllChildren(virDomainSnapshotPtr parent,
                                    virDomainSnapshotPtr **snapshots,
                                    unsigned int flags)
{
    int rv = -1;
6787
    size_t i;
6788 6789 6790 6791 6792 6793 6794 6795 6796 6797
    virDomainSnapshotPtr *snaps = NULL;
    remote_domain_snapshot_list_all_children_args args;
    remote_domain_snapshot_list_all_children_ret ret;

    struct private_data *priv = parent->domain->conn->privateData;

    remoteDriverLock(priv);

    args.need_results = !!snapshots;
    args.flags = flags;
6798
    make_nonnull_domain_snapshot(&args.snapshot, parent);
6799 6800

    memset(&ret, 0, sizeof(ret));
6801 6802 6803 6804 6805 6806 6807 6808
    if (call(parent->domain->conn,
             priv,
             0,
             REMOTE_PROC_DOMAIN_SNAPSHOT_LIST_ALL_CHILDREN,
             (xdrproc_t) xdr_remote_domain_snapshot_list_all_children_args,
             (char *) &args,
             (xdrproc_t) xdr_remote_domain_snapshot_list_all_children_ret,
             (char *) &ret) == -1)
6809 6810
        goto done;

6811 6812 6813 6814 6815 6816 6817 6818
    if (ret.snapshots.snapshots_len > REMOTE_DOMAIN_SNAPSHOT_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many domain snapshots '%d' for limit '%d'"),
                       ret.snapshots.snapshots_len,
                       REMOTE_DOMAIN_SNAPSHOT_LIST_MAX);
        goto cleanup;
    }

6819
    if (snapshots) {
6820
        if (VIR_ALLOC_N(snaps, ret.snapshots.snapshots_len + 1) < 0)
6821 6822 6823
            goto cleanup;
        for (i = 0; i < ret.snapshots.snapshots_len; i++) {
            snaps[i] = get_nonnull_domain_snapshot(parent->domain, ret.snapshots.snapshots_val[i]);
6824
            if (!snaps[i])
6825 6826 6827 6828 6829 6830 6831 6832
                goto cleanup;
        }
        *snapshots = snaps;
        snaps = NULL;
    }

    rv = ret.ret;

6833
 cleanup:
6834 6835 6836 6837 6838 6839 6840 6841 6842
    if (snaps) {
        for (i = 0; i < ret.snapshots.snapshots_len; i++)
            if (snaps[i])
                virDomainSnapshotFree(snaps[i]);
        VIR_FREE(snaps);
    }

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

6843
 done:
6844 6845 6846 6847
    remoteDriverUnlock(priv);
    return rv;
}

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

6864 6865 6866 6867
    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)
6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881
        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;
    }

    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
                                         REMOTE_NODE_MEMORY_PARAMETERS_MAX,
6882
                                         &params,
6883 6884 6885 6886 6887
                                         nparams) < 0)
        goto cleanup;

    rv = 0;

6888
 cleanup:
6889 6890
    xdr_free((xdrproc_t) xdr_remote_node_get_memory_parameters_ret,
             (char *) &ret);
6891
 done:
6892 6893 6894 6895
    remoteDriverUnlock(priv);
    return rv;
}

6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908
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);

6909 6910
    args.need_map = !!cpumap;
    args.need_online = !!online;
6911 6912
    args.flags = flags;

6913
    memset(&ret, 0, sizeof(ret));
6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924
    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) {
6925
        if (VIR_ALLOC_N(*cpumap, ret.cpumap.cpumap_len) < 0)
6926 6927 6928 6929 6930 6931 6932 6933 6934
            goto cleanup;
        memcpy(*cpumap, ret.cpumap.cpumap_val, ret.cpumap.cpumap_len);
    }

    if (online)
        *online = ret.online;

    rv = ret.ret;

6935
 cleanup:
6936
    xdr_free((xdrproc_t) xdr_remote_node_get_cpu_map_ret, (char *) &ret);
6937
 done:
6938 6939 6940 6941
    remoteDriverUnlock(priv);
    return rv;
}

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

6970
 done:
6971 6972 6973 6974
    remoteDriverUnlock(priv);
    return rv;
}

6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001
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;

    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
7002 7003
                                         REMOTE_DOMAIN_JOB_STATS_MAX,
                                         params, nparams) < 0)
7004 7005 7006 7007
        goto cleanup;

    rv = 0;

7008
 cleanup:
7009 7010
    xdr_free((xdrproc_t) xdr_remote_domain_get_job_stats_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
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;

7038 7039 7040 7041 7042 7043 7044
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071
    if (remoteSerializeTypedParameters(params, nparams,
                                       &args.params.params_val,
                                       &args.params.params_len) < 0) {
        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 */

7072
 cleanup:
7073 7074 7075 7076
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    remoteDriverUnlock(priv);
    return rv;

7077
 error:
7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103
    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));

7104 7105 7106 7107 7108 7109 7110
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149
    if (remoteSerializeTypedParameters(params, nparams,
                                       &args.params.params_val,
                                       &args.params.params_len) < 0) {
        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;

7150
 cleanup:
7151 7152 7153 7154 7155
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    VIR_FREE(ret.uri_out);
    remoteDriverUnlock(priv);
    return rv;

7156
 error:
7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185
    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));

7186 7187 7188 7189 7190 7191 7192
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239
    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.flags = flags;

    if (remoteSerializeTypedParameters(params, nparams,
                                       &args.params.params_val,
                                       &args.params.params_len) < 0) {
        xdr_free((xdrproc_t) xdr_remote_domain_migrate_prepare_tunnel3_params_args,
                 (char *) &args);
        goto cleanup;
    }

    if (!(netst = virNetClientStreamNew(priv->remoteProgram,
                                        REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3_PARAMS,
                                        priv->counter)))
        goto cleanup;

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

    st->driver = &remoteStreamDrv;
    st->privateData = netst;

    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;

7240
 cleanup:
7241 7242 7243 7244
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    remoteDriverUnlock(priv);
    return rv;

7245
 error:
7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271
    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));

7272 7273 7274 7275 7276 7277 7278
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311
    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;

    if (remoteSerializeTypedParameters(params, nparams,
                                       &args.params.params_val,
                                       &args.params.params_len) < 0) {
        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;

7312
 cleanup:
7313 7314 7315 7316
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    remoteDriverUnlock(priv);
    return rv;

7317
 error:
7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343
    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));

7344 7345 7346 7347 7348 7349 7350
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387
    args.cookie_in.cookie_in_val = (char *)cookiein;
    args.cookie_in.cookie_in_len = cookieinlen;
    args.flags = flags;
    args.cancelled = cancelled;

    if (remoteSerializeTypedParameters(params, nparams,
                                       &args.params.params_val,
                                       &args.params.params_len) < 0) {
        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);

7388
 cleanup:
7389 7390 7391 7392
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    remoteDriverUnlock(priv);
    return rv;

7393
 error:
7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415
    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));

7416 7417 7418 7419 7420 7421 7422
    if (nparams > REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many migration parameters '%d' for limit '%d'"),
                       nparams, REMOTE_DOMAIN_MIGRATE_PARAM_LIST_MAX);
        goto cleanup;
    }

7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443
    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;

    if (remoteSerializeTypedParameters(params, nparams,
                                       &args.params.params_val,
                                       &args.params.params_len) < 0) {
        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;

7444
 cleanup:
7445 7446 7447 7448 7449 7450
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    remoteDriverUnlock(priv);
    return rv;
}


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

7479
 done:
7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514
    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;

7515
 done:
7516 7517 7518 7519
    remoteDriverUnlock(priv);
    return rv;
}

7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552
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;
}

7553

M
Michal Privoznik 已提交
7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576
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;
    }

7577
    args.pages.pages_val = (u_int *) pages;
M
Michal Privoznik 已提交
7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600
    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;
}


7601 7602 7603 7604 7605 7606 7607 7608
/* 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;

7609
    if (VIR_STRDUP(lease_dst->iface, lease_src->iface) < 0)
7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652
        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,
7653
                           const char *mac,
7654 7655 7656 7657 7658
                           virNetworkDHCPLeasePtr **leases,
                           unsigned int flags)
{
    int rv = -1;
    size_t i;
7659
    struct private_data *priv = net->conn->privateData;
7660 7661 7662 7663 7664 7665 7666
    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);
7667
    args.mac = mac ? (char **) &mac : NULL;
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 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718
    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;
}


7719 7720 7721 7722 7723 7724 7725 7726
static int
remoteConnectGetAllDomainStats(virConnectPtr conn,
                               virDomainPtr *doms,
                               unsigned int ndoms,
                               unsigned int stats,
                               virDomainStatsRecordPtr **retStats,
                               unsigned int flags)
{
7727
    struct private_data *priv = conn->privateData;
7728 7729 7730 7731
    int rv = -1;
    size_t i;
    remote_connect_get_all_domain_stats_args args;
    remote_connect_get_all_domain_stats_ret ret;
7732
    virDomainStatsRecordPtr elem = NULL;
7733 7734
    virDomainStatsRecordPtr *tmpret = NULL;

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

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 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788
    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;

        if (remoteDeserializeTypedParameters(rec->params.params_val,
                                             rec->params.params_len,
                                             REMOTE_CONNECT_GET_ALL_DOMAIN_STATS_MAX,
                                             &elem->params,
                                             &elem->nparams))
            goto cleanup;

        tmpret[i] = elem;
7789
        elem = NULL;
7790 7791 7792 7793 7794 7795 7796
    }

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

 cleanup:
7797 7798 7799 7800
    if (elem) {
        virObjectUnref(elem->dom);
        VIR_FREE(elem);
    }
7801
    virDomainStatsRecordListFree(tmpret);
7802
    VIR_FREE(args.doms.doms_val);
7803 7804 7805 7806 7807 7808
    xdr_free((xdrproc_t)xdr_remote_connect_get_all_domain_stats_ret,
             (char *) &ret);

    return rv;
}

M
Michal Privoznik 已提交
7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854

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


7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946
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;
}


7947 7948 7949 7950 7951 7952
/* 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
7953
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
7954 7955
{
    virDomainPtr dom;
7956
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
7957 7958 7959 7960 7961
    if (dom) dom->id = domain.id;
    return dom;
}

static virNetworkPtr
7962
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
7963
{
7964
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
7965 7966
}

D
Daniel Veillard 已提交
7967
static virInterfacePtr
7968
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
7969
{
7970
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
7971 7972
}

7973
static virStoragePoolPtr
7974
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
7975
{
7976 7977
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
7978 7979 7980
}

static virStorageVolPtr
7981
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
7982
{
7983 7984
    return virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                            NULL, NULL);
7985 7986
}

7987
static virNodeDevicePtr
7988
get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev)
7989 7990 7991 7992
{
    return virGetNodeDevice(conn, dev.name);
}

7993
static virSecretPtr
7994
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
7995
{
7996
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
7997 7998
}

7999
static virNWFilterPtr
8000
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
8001
{
8002
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
8003 8004
}

C
Chris Lalancette 已提交
8005
static virDomainSnapshotPtr
8006
get_nonnull_domain_snapshot(virDomainPtr domain, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
8007 8008 8009 8010
{
    return virGetDomainSnapshot(domain, snapshot.name);
}

8011

8012 8013
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
8014
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
8015
{
8016
    dom_dst->id = dom_src->id;
8017
    dom_dst->name = dom_src->name;
8018
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
8019 8020 8021
}

static void
8022
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
8023 8024
{
    net_dst->name = net_src->name;
8025
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
8026 8027
}

D
Daniel Veillard 已提交
8028
static void
8029
make_nonnull_interface(remote_nonnull_interface *interface_dst,
D
Daniel Veillard 已提交
8030 8031 8032 8033 8034 8035
                        virInterfacePtr interface_src)
{
    interface_dst->name = interface_src->name;
    interface_dst->mac = interface_src->mac;
}

8036
static void
8037
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
8038 8039
{
    pool_dst->name = pool_src->name;
8040
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
8041 8042 8043
}

static void
8044
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
8045 8046 8047 8048 8049 8050
{
    vol_dst->pool = vol_src->pool;
    vol_dst->name = vol_src->name;
    vol_dst->key = vol_src->key;
}

8051
static void
8052
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
8053
{
8054
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
8055 8056
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = secret_src->usageID;
8057 8058
}

8059
static void
8060
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
8061 8062
{
    nwfilter_dst->name = nwfilter_src->name;
8063
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
8064 8065
}

C
Chris Lalancette 已提交
8066
static void
8067
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
8068 8069
{
    snapshot_dst->name = snapshot_src->name;
8070
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
8071 8072
}

8073 8074
/*----------------------------------------------------------------------*/

8075 8076 8077 8078 8079
unsigned long remoteVersion(void)
{
    return REMOTE_PROTOCOL_VERSION;
}

8080
static virHypervisorDriver hypervisor_driver = {
8081 8082
    .no = VIR_DRV_REMOTE,
    .name = "remote",
8083 8084
    .connectOpen = remoteConnectOpen, /* 0.3.0 */
    .connectClose = remoteConnectClose, /* 0.3.0 */
8085 8086 8087 8088 8089 8090 8091
    .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 */
8092
    .nodeGetInfo = remoteNodeGetInfo, /* 0.3.0 */
8093 8094 8095 8096
    .connectGetCapabilities = remoteConnectGetCapabilities, /* 0.3.0 */
    .connectListDomains = remoteConnectListDomains, /* 0.3.0 */
    .connectNumOfDomains = remoteConnectNumOfDomains, /* 0.3.0 */
    .connectListAllDomains = remoteConnectListAllDomains, /* 0.9.13 */
8097
    .domainCreateXML = remoteDomainCreateXML, /* 0.3.0 */
8098
    .domainCreateXMLWithFiles = remoteDomainCreateXMLWithFiles, /* 1.1.1 */
8099 8100 8101 8102 8103
    .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 */
8104
    .domainPMSuspendForDuration = remoteDomainPMSuspendForDuration, /* 0.9.10 */
8105
    .domainPMWakeup = remoteDomainPMWakeup, /* 0.9.11 */
8106
    .domainShutdown = remoteDomainShutdown, /* 0.3.0 */
8107
    .domainShutdownFlags = remoteDomainShutdownFlags, /* 0.9.10 */
8108
    .domainReboot = remoteDomainReboot, /* 0.3.0 */
8109
    .domainReset = remoteDomainReset, /* 0.9.7 */
8110
    .domainDestroy = remoteDomainDestroy, /* 0.3.0 */
8111
    .domainDestroyFlags = remoteDomainDestroyFlags, /* 0.9.4 */
8112 8113 8114 8115 8116
    .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 */
8117
    .domainSetMemoryStatsPeriod = remoteDomainSetMemoryStatsPeriod, /* 1.1.1 */
8118 8119 8120 8121 8122 8123
    .domainSetMemoryParameters = remoteDomainSetMemoryParameters, /* 0.8.5 */
    .domainGetMemoryParameters = remoteDomainGetMemoryParameters, /* 0.8.5 */
    .domainSetBlkioParameters = remoteDomainSetBlkioParameters, /* 0.9.0 */
    .domainGetBlkioParameters = remoteDomainGetBlkioParameters, /* 0.9.0 */
    .domainGetInfo = remoteDomainGetInfo, /* 0.3.0 */
    .domainGetState = remoteDomainGetState, /* 0.9.2 */
8124
    .domainGetControlInfo = remoteDomainGetControlInfo, /* 0.9.3 */
8125
    .domainSave = remoteDomainSave, /* 0.3.0 */
E
Eric Blake 已提交
8126
    .domainSaveFlags = remoteDomainSaveFlags, /* 0.9.4 */
8127
    .domainRestore = remoteDomainRestore, /* 0.3.0 */
E
Eric Blake 已提交
8128
    .domainRestoreFlags = remoteDomainRestoreFlags, /* 0.9.4 */
E
Eric Blake 已提交
8129 8130
    .domainSaveImageGetXMLDesc = remoteDomainSaveImageGetXMLDesc, /* 0.9.4 */
    .domainSaveImageDefineXML = remoteDomainSaveImageDefineXML, /* 0.9.4 */
8131
    .domainCoreDump = remoteDomainCoreDump, /* 0.3.0 */
8132
    .domainCoreDumpWithFormat = remoteDomainCoreDumpWithFormat, /* 1.2.3 */
8133 8134 8135 8136 8137
    .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 */
8138
    .domainPinVcpuFlags = remoteDomainPinVcpuFlags, /* 0.9.3 */
E
Eric Blake 已提交
8139
    .domainGetVcpuPinInfo = remoteDomainGetVcpuPinInfo, /* 0.9.3 */
8140 8141
    .domainPinEmulator = remoteDomainPinEmulator, /* 0.10.0 */
    .domainGetEmulatorPinInfo = remoteDomainGetEmulatorPinInfo, /* 0.10.0 */
8142 8143 8144
    .domainGetVcpus = remoteDomainGetVcpus, /* 0.3.0 */
    .domainGetMaxVcpus = remoteDomainGetMaxVcpus, /* 0.3.0 */
    .domainGetSecurityLabel = remoteDomainGetSecurityLabel, /* 0.6.1 */
M
Marcelo Cerri 已提交
8145
    .domainGetSecurityLabelList = remoteDomainGetSecurityLabelList, /* 0.10.0 */
8146 8147
    .nodeGetSecurityModel = remoteNodeGetSecurityModel, /* 0.6.1 */
    .domainGetXMLDesc = remoteDomainGetXMLDesc, /* 0.3.0 */
8148 8149 8150 8151
    .connectDomainXMLFromNative = remoteConnectDomainXMLFromNative, /* 0.6.4 */
    .connectDomainXMLToNative = remoteConnectDomainXMLToNative, /* 0.6.4 */
    .connectListDefinedDomains = remoteConnectListDefinedDomains, /* 0.3.0 */
    .connectNumOfDefinedDomains = remoteConnectNumOfDefinedDomains, /* 0.3.0 */
8152 8153
    .domainCreate = remoteDomainCreate, /* 0.3.0 */
    .domainCreateWithFlags = remoteDomainCreateWithFlags, /* 0.8.2 */
8154
    .domainCreateWithFiles = remoteDomainCreateWithFiles, /* 1.1.1 */
8155 8156
    .domainDefineXML = remoteDomainDefineXML, /* 0.3.0 */
    .domainUndefine = remoteDomainUndefine, /* 0.3.0 */
8157
    .domainUndefineFlags = remoteDomainUndefineFlags, /* 0.9.4 */
8158 8159 8160 8161 8162 8163 8164 8165 8166
    .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 */
    .domainGetAutostart = remoteDomainGetAutostart, /* 0.3.0 */
    .domainSetAutostart = remoteDomainSetAutostart, /* 0.3.0 */
    .domainGetSchedulerType = remoteDomainGetSchedulerType, /* 0.3.0 */
    .domainGetSchedulerParameters = remoteDomainGetSchedulerParameters, /* 0.3.0 */
8167
    .domainGetSchedulerParametersFlags = remoteDomainGetSchedulerParametersFlags, /* 0.9.2 */
8168
    .domainSetSchedulerParameters = remoteDomainSetSchedulerParameters, /* 0.3.0 */
8169
    .domainSetSchedulerParametersFlags = remoteDomainSetSchedulerParametersFlags, /* 0.9.2 */
8170 8171 8172
    .domainMigratePrepare = remoteDomainMigratePrepare, /* 0.3.2 */
    .domainMigratePerform = remoteDomainMigratePerform, /* 0.3.2 */
    .domainMigrateFinish = remoteDomainMigrateFinish, /* 0.3.2 */
8173
    .domainBlockResize = remoteDomainBlockResize, /* 0.9.8 */
8174
    .domainBlockStats = remoteDomainBlockStats, /* 0.3.2 */
8175
    .domainBlockStatsFlags = remoteDomainBlockStatsFlags, /* 0.9.5 */
8176
    .domainInterfaceStats = remoteDomainInterfaceStats, /* 0.3.2 */
8177 8178
    .domainSetInterfaceParameters = remoteDomainSetInterfaceParameters, /* 0.9.9 */
    .domainGetInterfaceParameters = remoteDomainGetInterfaceParameters, /* 0.9.9 */
8179 8180 8181 8182
    .domainMemoryStats = remoteDomainMemoryStats, /* 0.7.5 */
    .domainBlockPeek = remoteDomainBlockPeek, /* 0.4.2 */
    .domainMemoryPeek = remoteDomainMemoryPeek, /* 0.4.2 */
    .domainGetBlockInfo = remoteDomainGetBlockInfo, /* 0.8.1 */
8183
    .nodeGetCPUStats = remoteNodeGetCPUStats, /* 0.9.3 */
8184
    .nodeGetMemoryStats = remoteNodeGetMemoryStats, /* 0.9.3 */
8185 8186
    .nodeGetCellsFreeMemory = remoteNodeGetCellsFreeMemory, /* 0.3.3 */
    .nodeGetFreeMemory = remoteNodeGetFreeMemory, /* 0.3.3 */
8187 8188
    .connectDomainEventRegister = remoteConnectDomainEventRegister, /* 0.5.0 */
    .connectDomainEventDeregister = remoteConnectDomainEventDeregister, /* 0.5.0 */
8189 8190 8191
    .domainMigratePrepare2 = remoteDomainMigratePrepare2, /* 0.5.0 */
    .domainMigrateFinish2 = remoteDomainMigrateFinish2, /* 0.5.0 */
    .nodeDeviceDettach = remoteNodeDeviceDettach, /* 0.6.1 */
8192
    .nodeDeviceDetachFlags = remoteNodeDeviceDetachFlags, /* 1.0.5 */
8193 8194 8195
    .nodeDeviceReAttach = remoteNodeDeviceReAttach, /* 0.6.1 */
    .nodeDeviceReset = remoteNodeDeviceReset, /* 0.6.1 */
    .domainMigratePrepareTunnel = remoteDomainMigratePrepareTunnel, /* 0.7.2 */
8196 8197
    .connectIsEncrypted = remoteConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = remoteConnectIsSecure, /* 0.7.3 */
8198 8199 8200
    .domainIsActive = remoteDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = remoteDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = remoteDomainIsUpdated, /* 0.8.6 */
8201 8202
    .connectCompareCPU = remoteConnectCompareCPU, /* 0.7.5 */
    .connectBaselineCPU = remoteConnectBaselineCPU, /* 0.7.7 */
8203
    .domainGetJobInfo = remoteDomainGetJobInfo, /* 0.7.7 */
8204
    .domainGetJobStats = remoteDomainGetJobStats, /* 1.0.3 */
8205 8206
    .domainAbortJob = remoteDomainAbortJob, /* 0.7.7 */
    .domainMigrateSetMaxDowntime = remoteDomainMigrateSetMaxDowntime, /* 0.8.0 */
8207 8208
    .domainMigrateGetCompressionCache = remoteDomainMigrateGetCompressionCache, /* 1.0.3 */
    .domainMigrateSetCompressionCache = remoteDomainMigrateSetCompressionCache, /* 1.0.3 */
8209
    .domainMigrateSetMaxSpeed = remoteDomainMigrateSetMaxSpeed, /* 0.9.0 */
8210
    .domainMigrateGetMaxSpeed = remoteDomainMigrateGetMaxSpeed, /* 0.9.5 */
8211 8212
    .connectDomainEventRegisterAny = remoteConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = remoteConnectDomainEventDeregisterAny, /* 0.8.0 */
8213 8214 8215 8216 8217 8218 8219
    .domainManagedSave = remoteDomainManagedSave, /* 0.8.0 */
    .domainHasManagedSaveImage = remoteDomainHasManagedSaveImage, /* 0.8.0 */
    .domainManagedSaveRemove = remoteDomainManagedSaveRemove, /* 0.8.0 */
    .domainSnapshotCreateXML = remoteDomainSnapshotCreateXML, /* 0.8.0 */
    .domainSnapshotGetXMLDesc = remoteDomainSnapshotGetXMLDesc, /* 0.8.0 */
    .domainSnapshotNum = remoteDomainSnapshotNum, /* 0.8.0 */
    .domainSnapshotListNames = remoteDomainSnapshotListNames, /* 0.8.0 */
8220
    .domainListAllSnapshots = remoteDomainListAllSnapshots, /* 0.9.13 */
8221
    .domainSnapshotNumChildren = remoteDomainSnapshotNumChildren, /* 0.9.7 */
8222
    .domainSnapshotListAllChildren = remoteDomainSnapshotListAllChildren, /* 0.9.13 */
8223
    .domainSnapshotListChildrenNames = remoteDomainSnapshotListChildrenNames, /* 0.9.7 */
8224 8225
    .domainSnapshotLookupByName = remoteDomainSnapshotLookupByName, /* 0.8.0 */
    .domainHasCurrentSnapshot = remoteDomainHasCurrentSnapshot, /* 0.8.0 */
8226
    .domainSnapshotGetParent = remoteDomainSnapshotGetParent, /* 0.9.7 */
8227 8228
    .domainSnapshotCurrent = remoteDomainSnapshotCurrent, /* 0.8.0 */
    .domainRevertToSnapshot = remoteDomainRevertToSnapshot, /* 0.8.0 */
E
Eric Blake 已提交
8229 8230
    .domainSnapshotIsCurrent = remoteDomainSnapshotIsCurrent, /* 0.9.13 */
    .domainSnapshotHasMetadata = remoteDomainSnapshotHasMetadata, /* 0.9.13 */
8231
    .domainSnapshotDelete = remoteDomainSnapshotDelete, /* 0.8.0 */
8232 8233 8234
    .domainQemuMonitorCommand = remoteDomainQemuMonitorCommand, /* 0.8.3 */
    .domainQemuAttach = remoteDomainQemuAttach, /* 0.9.4 */
    .domainQemuAgentCommand = remoteDomainQemuAgentCommand, /* 0.10.0 */
8235 8236
    .connectDomainQemuMonitorEventRegister = remoteConnectDomainQemuMonitorEventRegister, /* 1.2.3 */
    .connectDomainQemuMonitorEventDeregister = remoteConnectDomainQemuMonitorEventDeregister, /* 1.2.3 */
8237
    .domainOpenConsole = remoteDomainOpenConsole, /* 0.8.6 */
8238
    .domainOpenChannel = remoteDomainOpenChannel, /* 1.0.2 */
8239
    .domainOpenGraphics = remoteDomainOpenGraphics, /* 0.9.7 */
8240
    .domainOpenGraphicsFD = remoteDomainOpenGraphicsFD, /* 1.2.8 */
8241
    .domainInjectNMI = remoteDomainInjectNMI, /* 0.9.2 */
8242 8243 8244 8245 8246 8247
    .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 */
8248
    .domainSendKey = remoteDomainSendKey, /* 0.9.3 */
8249
    .domainSendProcessSignal = remoteDomainSendProcessSignal, /* 1.0.1 */
8250 8251 8252 8253
    .domainBlockJobAbort = remoteDomainBlockJobAbort, /* 0.9.4 */
    .domainGetBlockJobInfo = remoteDomainGetBlockJobInfo, /* 0.9.4 */
    .domainBlockJobSetSpeed = remoteDomainBlockJobSetSpeed, /* 0.9.4 */
    .domainBlockPull = remoteDomainBlockPull, /* 0.9.4 */
8254
    .domainBlockRebase = remoteDomainBlockRebase, /* 0.9.10 */
8255
    .domainBlockCopy = remoteDomainBlockCopy, /* 1.2.9 */
8256
    .domainBlockCommit = remoteDomainBlockCommit, /* 0.10.2 */
8257 8258
    .connectSetKeepAlive = remoteConnectSetKeepAlive, /* 0.9.8 */
    .connectIsAlive = remoteConnectIsAlive, /* 0.9.8 */
8259
    .nodeSuspendForDuration = remoteNodeSuspendForDuration, /* 0.9.8 */
8260 8261
    .domainSetBlockIoTune = remoteDomainSetBlockIoTune, /* 0.9.8 */
    .domainGetBlockIoTune = remoteDomainGetBlockIoTune, /* 0.9.8 */
8262 8263
    .domainSetNumaParameters = remoteDomainSetNumaParameters, /* 0.9.9 */
    .domainGetNumaParameters = remoteDomainGetNumaParameters, /* 0.9.9 */
8264
    .domainGetCPUStats = remoteDomainGetCPUStats, /* 0.9.10 */
8265
    .domainGetDiskErrors = remoteDomainGetDiskErrors, /* 0.9.10 */
8266 8267
    .domainSetMetadata = remoteDomainSetMetadata, /* 0.9.10 */
    .domainGetMetadata = remoteDomainGetMetadata, /* 0.9.10 */
8268
    .domainGetHostname = remoteDomainGetHostname, /* 0.10.0 */
8269 8270
    .nodeSetMemoryParameters = remoteNodeSetMemoryParameters, /* 0.10.2 */
    .nodeGetMemoryParameters = remoteNodeGetMemoryParameters, /* 0.10.2 */
8271
    .nodeGetCPUMap = remoteNodeGetCPUMap, /* 1.0.0 */
8272
    .domainFSTrim = remoteDomainFSTrim, /* 1.0.1 */
8273
    .domainLxcOpenNamespace = remoteDomainLxcOpenNamespace, /* 1.0.2 */
8274 8275 8276 8277 8278 8279
    .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 */
8280
    .connectGetCPUModelNames = remoteConnectGetCPUModelNames, /* 1.1.3 */
8281 8282
    .domainFSFreeze = remoteDomainFSFreeze, /* 1.2.5 */
    .domainFSThaw = remoteDomainFSThaw, /* 1.2.5 */
8283 8284
    .domainGetTime = remoteDomainGetTime, /* 1.2.5 */
    .domainSetTime = remoteDomainSetTime, /* 1.2.5 */
M
Michal Privoznik 已提交
8285
    .nodeGetFreePages = remoteNodeGetFreePages, /* 1.2.6 */
8286
    .connectGetDomainCapabilities = remoteConnectGetDomainCapabilities, /* 1.2.7 */
8287
    .connectGetAllDomainStats = remoteConnectGetAllDomainStats, /* 1.2.8 */
M
Michal Privoznik 已提交
8288
    .nodeAllocPages = remoteNodeAllocPages, /* 1.2.9 */
8289
    .domainGetFSInfo = remoteDomainGetFSInfo, /* 1.2.11 */
8290 8291 8292
};

static virNetworkDriver network_driver = {
8293
    .name = "remote",
8294 8295
    .networkOpen = remoteNetworkOpen, /* 0.3.0 */
    .networkClose = remoteNetworkClose, /* 0.3.0 */
8296 8297 8298 8299 8300
    .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 */
8301 8302
    .connectNetworkEventDeregisterAny = remoteConnectNetworkEventDeregisterAny, /* 1.2.1 */
    .connectNetworkEventRegisterAny = remoteConnectNetworkEventRegisterAny, /* 1.2.1 */
8303 8304 8305 8306 8307
    .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 */
8308
    .networkUpdate = remoteNetworkUpdate, /* 0.10.2 */
8309 8310 8311 8312 8313 8314 8315 8316
    .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 */
8317
    .networkGetDHCPLeases = remoteNetworkGetDHCPLeases, /* 1.2.6 */
8318 8319
};

D
Daniel Veillard 已提交
8320 8321
static virInterfaceDriver interface_driver = {
    .name = "remote",
8322 8323
    .interfaceOpen = remoteInterfaceOpen, /* 0.7.2 */
    .interfaceClose = remoteInterfaceClose, /* 0.7.2 */
8324 8325 8326 8327 8328
    .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 */
8329 8330 8331 8332 8333 8334 8335 8336
    .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 */
8337 8338 8339
    .interfaceChangeBegin = remoteInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = remoteInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = remoteInterfaceChangeRollback, /* 0.9.2 */
D
Daniel Veillard 已提交
8340 8341
};

8342 8343
static virStorageDriver storage_driver = {
    .name = "remote",
8344 8345
    .storageOpen = remoteStorageOpen, /* 0.4.1 */
    .storageClose = remoteStorageClose, /* 0.4.1 */
8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386
    .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 */
    .storagePoolLookupByName = remoteStoragePoolLookupByName, /* 0.4.1 */
    .storagePoolLookupByUUID = remoteStoragePoolLookupByUUID, /* 0.4.1 */
    .storagePoolLookupByVolume = remoteStoragePoolLookupByVolume, /* 0.4.1 */
    .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 */
    .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 */
8387 8388
};

8389 8390
static virSecretDriver secret_driver = {
    .name = "remote",
8391 8392
    .secretOpen = remoteSecretOpen, /* 0.7.1 */
    .secretClose = remoteSecretClose, /* 0.7.1 */
8393 8394 8395 8396 8397 8398 8399 8400 8401 8402
    .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 */
    .secretUndefine = remoteSecretUndefine /* 0.7.1 */
8403 8404
};

8405
static virNodeDeviceDriver node_device_driver = {
8406
    .name = "remote",
8407 8408
    .nodeDeviceOpen = remoteNodeDeviceOpen, /* 0.5.0 */
    .nodeDeviceClose = remoteNodeDeviceClose, /* 0.5.0 */
8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419
    .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 */
8420 8421
};

8422 8423
static virNWFilterDriver nwfilter_driver = {
    .name = "remote",
8424 8425
    .nwfilterOpen = remoteNWFilterOpen, /* 0.8.0 */
    .nwfilterClose = remoteNWFilterClose, /* 0.8.0 */
8426 8427
    .nwfilterLookupByUUID = remoteNWFilterLookupByUUID, /* 0.8.0 */
    .nwfilterLookupByName = remoteNWFilterLookupByName, /* 0.8.0 */
8428 8429 8430 8431 8432 8433
    .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 */
8434 8435
};

8436

A
Atsushi SAKAI 已提交
8437
#ifdef WITH_LIBVIRTD
8438
static virStateDriver state_driver = {
8439
    .name = "Remote",
8440
    .stateInitialize = remoteStateInitialize,
8441
};
A
Atsushi SAKAI 已提交
8442
#endif
8443 8444


8445
/** remoteRegister:
8446 8447
 *
 * Register driver with libvirt driver system.
8448 8449
 *
 * Returns -1 on error.
8450 8451
 */
int
8452
remoteRegister(void)
8453
{
8454
    if (virRegisterHypervisorDriver(&hypervisor_driver) < 0)
8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467
        return -1;
    if (virRegisterNetworkDriver(&network_driver) < 0)
        return -1;
    if (virRegisterInterfaceDriver(&interface_driver) < 0)
        return -1;
    if (virRegisterStorageDriver(&storage_driver) < 0)
        return -1;
    if (virRegisterNodeDeviceDriver(&node_device_driver) < 0)
        return -1;
    if (virRegisterSecretDriver(&secret_driver) < 0)
        return -1;
    if (virRegisterNWFilterDriver(&nwfilter_driver) < 0)
        return -1;
A
Atsushi SAKAI 已提交
8468
#ifdef WITH_LIBVIRTD
8469
    if (virRegisterStateDriver(&state_driver) < 0)
8470
        return -1;
A
Atsushi SAKAI 已提交
8471
#endif
8472 8473 8474

    return 0;
}