remote_driver.c 230.0 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-2013 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 "driver.h"
37
#include "virbuffer.h"
38
#include "remote_driver.h"
39
#include "remote_protocol.h"
40
#include "lxc_protocol.h"
C
Chris Lalancette 已提交
41
#include "qemu_protocol.h"
42
#include "viralloc.h"
E
Eric Blake 已提交
43
#include "virfile.h"
44
#include "vircommand.h"
45
#include "intprops.h"
A
Alex Jia 已提交
46
#include "virtypedparam.h"
M
Martin Kletzander 已提交
47
#include "viruri.h"
48 49
#include "virauth.h"
#include "virauthconfig.h"
50
#include "virstring.h"
51

52 53
#define VIR_FROM_THIS VIR_FROM_REMOTE

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

72
static bool inside_daemon = false;
73
static virDriverPtr remoteDriver = NULL;
74

75 76
struct private_data {
    virMutex lock;
77

78 79 80
    virNetClientPtr client;
    virNetClientProgramPtr remoteProgram;
    virNetClientProgramPtr qemuProgram;
81
    virNetClientProgramPtr lxcProgram;
82

83
    int counter; /* Serial number for RPC */
84

85
#ifdef WITH_GNUTLS
86
    virNetTLSContextPtr tls;
87
#endif
88

89
    int is_secure;              /* Secure if TLS or SASL or UNIX sockets */
90
    char *type;                 /* Cached return from remoteType. */
91
    int localUses;              /* Ref count for private data */
92
    char *hostname;             /* Original hostname */
93
    bool serverKeepAlive;       /* Does server support keepalive protocol? */
94

95
    virDomainEventStatePtr domainEventState;
96 97
};

98
enum {
99
    REMOTE_CALL_QEMU              = (1 << 0),
100
    REMOTE_CALL_LXC               = (1 << 1),
101 102 103
};


104 105 106 107 108 109 110 111 112 113
static void remoteDriverLock(struct private_data *driver)
{
    virMutexLock(&driver->lock);
}

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

114 115 116 117
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);
118 119 120 121 122 123 124
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);
125 126
static int remoteAuthenticate(virConnectPtr conn, struct private_data *priv,
                              virConnectAuthPtr auth, const char *authtype);
127
#if WITH_SASL
128 129
static int remoteAuthSASL(virConnectPtr conn, struct private_data *priv,
                          virConnectAuthPtr auth, const char *mech);
130
#endif
131
#if WITH_POLKIT
132 133
static int remoteAuthPolkit(virConnectPtr conn, struct private_data *priv,
                            virConnectAuthPtr auth);
134
#endif /* WITH_POLKIT */
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
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);
153
static void remoteDomainEventQueue(struct private_data *priv, virDomainEventPtr event);
154 155 156
/*----------------------------------------------------------------------*/

/* Helper functions for remoteOpen. */
157
static char *get_transport_from_scheme(char *scheme);
158

A
Atsushi SAKAI 已提交
159
#ifdef WITH_LIBVIRTD
160
static int
161 162 163
remoteStateInitialize(bool privileged ATTRIBUTE_UNUSED,
                      virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                      void *opaque ATTRIBUTE_UNUSED)
164 165 166 167
{
    /* Mark that we're inside the daemon so we can avoid
     * re-entering ourselves
     */
168
    inside_daemon = true;
169 170
    return 0;
}
A
Atsushi SAKAI 已提交
171
#endif
172

173
#ifndef WIN32
174
/**
175
 * remoteFindDaemonPath:
176 177
 *
 * Tries to find the path to the libvirtd binary.
178
 *
179 180 181 182 183 184 185 186 187 188
 * Returns path on success or NULL in case of error.
 */
static const char *
remoteFindDaemonPath(void)
{
    static const char *serverPaths[] = {
        SBINDIR "/libvirtd",
        SBINDIR "/libvirtd_dbg",
        NULL
    };
189
    size_t i;
190
    const char *customDaemon = virGetEnvBlockSUID("LIBVIRTD_PATH");
191 192

    if (customDaemon)
193
        return customDaemon;
194 195

    for (i = 0; serverPaths[i]; i++) {
E
Eric Blake 已提交
196
        if (virFileIsExecutable(serverPaths[i])) {
197 198 199 200 201
            return serverPaths[i];
        }
    }
    return NULL;
}
202
#endif
203

204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
static void
remoteDomainBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque);
static void
remoteDomainBuildEventReboot(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                             virNetClientPtr client ATTRIBUTE_UNUSED,
                             void *evdata, void *opaque);
static void
remoteDomainBuildEventRTCChange(virNetClientProgramPtr prog,
                                virNetClientPtr client,
                                void *evdata, void *opaque);
static void
remoteDomainBuildEventWatchdog(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
static void
remoteDomainBuildEventIOError(virNetClientProgramPtr prog,
                              virNetClientPtr client,
                              void *evdata, void *opaque);
static void
remoteDomainBuildEventIOErrorReason(virNetClientProgramPtr prog,
                                    virNetClientPtr client,
                                    void *evdata, void *opaque);
static void
remoteDomainBuildEventGraphics(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);
static void
remoteDomainBuildEventControlError(virNetClientProgramPtr prog,
                                   virNetClientPtr client,
                                   void *evdata, void *opaque);

238 239 240 241 242
static void
remoteDomainBuildEventBlockJob(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);

243 244 245 246 247
static void
remoteDomainBuildEventDiskChange(virNetClientProgramPtr prog,
                                 virNetClientPtr client,
                                 void *evdata, void *opaque);

248 249 250 251 252
static void
remoteDomainBuildEventTrayChange(virNetClientProgramPtr prog,
                                 virNetClientPtr client,
                                 void *evdata, void *opaque);

O
Osier Yang 已提交
253 254 255 256 257
static void
remoteDomainBuildEventPMWakeup(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);

O
Osier Yang 已提交
258 259 260 261
static void
remoteDomainBuildEventPMSuspend(virNetClientProgramPtr prog,
                                virNetClientPtr client,
                                void *evdata, void *opaque);
262 263 264 265
static void
remoteDomainBuildEventBalloonChange(virNetClientProgramPtr prog,
                                    virNetClientPtr client,
                                    void *evdata, void *opaque);
266 267 268 269
static void
remoteDomainBuildEventPMSuspendDisk(virNetClientProgramPtr prog,
                                  virNetClientPtr client,
                                  void *evdata, void *opaque);
O
Osier Yang 已提交
270

271 272 273 274 275
static void
remoteDomainBuildEventDeviceRemoved(virNetClientProgramPtr prog,
                                    virNetClientPtr client,
                                    void *evdata, void *opaque);

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
static virNetClientProgramEvent remoteDomainEvents[] = {
    { REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE,
      remoteDomainBuildEventRTCChange,
      sizeof(remote_domain_event_rtc_change_msg),
      (xdrproc_t)xdr_remote_domain_event_rtc_change_msg },
    { REMOTE_PROC_DOMAIN_EVENT_REBOOT,
      remoteDomainBuildEventReboot,
      sizeof(remote_domain_event_reboot_msg),
      (xdrproc_t)xdr_remote_domain_event_reboot_msg },
    { REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE,
      remoteDomainBuildEventLifecycle,
      sizeof(remote_domain_event_lifecycle_msg),
      (xdrproc_t)xdr_remote_domain_event_lifecycle_msg },
    { 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_IO_ERROR_REASON,
      remoteDomainBuildEventIOErrorReason,
      sizeof(remote_domain_event_io_error_reason_msg),
      (xdrproc_t)xdr_remote_domain_event_io_error_reason_msg },
    { REMOTE_PROC_DOMAIN_EVENT_GRAPHICS,
      remoteDomainBuildEventGraphics,
      sizeof(remote_domain_event_graphics_msg),
      (xdrproc_t)xdr_remote_domain_event_graphics_msg },
    { REMOTE_PROC_DOMAIN_EVENT_CONTROL_ERROR,
      remoteDomainBuildEventControlError,
      sizeof(remote_domain_event_control_error_msg),
      (xdrproc_t)xdr_remote_domain_event_control_error_msg },
309 310 311 312
    { REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB,
      remoteDomainBuildEventBlockJob,
      sizeof(remote_domain_event_block_job_msg),
      (xdrproc_t)xdr_remote_domain_event_block_job_msg },
313 314 315 316
    { REMOTE_PROC_DOMAIN_EVENT_DISK_CHANGE,
      remoteDomainBuildEventDiskChange,
      sizeof(remote_domain_event_disk_change_msg),
      (xdrproc_t)xdr_remote_domain_event_disk_change_msg },
317 318 319 320
    { 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 已提交
321 322 323 324
    { REMOTE_PROC_DOMAIN_EVENT_PMWAKEUP,
      remoteDomainBuildEventPMWakeup,
      sizeof(remote_domain_event_pmwakeup_msg),
      (xdrproc_t)xdr_remote_domain_event_pmwakeup_msg },
O
Osier Yang 已提交
325 326 327 328
    { REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND,
      remoteDomainBuildEventPMSuspend,
      sizeof(remote_domain_event_pmsuspend_msg),
      (xdrproc_t)xdr_remote_domain_event_pmsuspend_msg },
329 330 331 332
    { REMOTE_PROC_DOMAIN_EVENT_BALLOON_CHANGE,
      remoteDomainBuildEventBalloonChange,
      sizeof(remote_domain_event_balloon_change_msg),
      (xdrproc_t)xdr_remote_domain_event_balloon_change_msg },
333 334 335 336
    { REMOTE_PROC_DOMAIN_EVENT_PMSUSPEND_DISK,
      remoteDomainBuildEventPMSuspendDisk,
      sizeof(remote_domain_event_pmsuspend_disk_msg),
      (xdrproc_t)xdr_remote_domain_event_pmsuspend_disk_msg },
337 338 339 340
    { REMOTE_PROC_DOMAIN_EVENT_DEVICE_REMOVED,
      remoteDomainBuildEventDeviceRemoved,
      sizeof(remote_domain_event_device_removed_msg),
      (xdrproc_t)xdr_remote_domain_event_device_removed_msg },
341
};
342

343
enum virDrvOpenRemoteFlags {
344
    VIR_DRV_OPEN_REMOTE_RO = (1 << 0),
345 346
    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 */
347
};
348

349

350 351 352 353
static void
remoteClientCloseFunc(virNetClientPtr client ATTRIBUTE_UNUSED,
                      int reason,
                      void *opaque)
354
{
355 356 357 358 359 360 361 362 363 364 365 366 367
    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;
368
    }
369 370 371 372 373
    virObjectUnlock(cbdata);

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

376 377 378 379
/* 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);                              \
380 381
        if (VIR_STRDUP(ARG_VAR, var->value) < 0)        \
            goto failed;                                \
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
        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;                                                           \
    }
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
/*
 * 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
415 416
 *   - xxx+ssh:///            -> SSH connection (legacy)
 *   - xxx+libssh2:///        -> SSH connection (using libssh2)
417
 */
418
static int
419 420 421 422
doRemoteOpen(virConnectPtr conn,
             struct private_data *priv,
             virConnectAuthPtr auth ATTRIBUTE_UNUSED,
             unsigned int flags)
423
{
424
    char *transport_str = NULL;
425 426 427 428
    enum {
        trans_tls,
        trans_unix,
        trans_ssh,
429
        trans_libssh2,
430 431 432
        trans_ext,
        trans_tcp,
    } transport;
433
#ifndef WIN32
434
    const char *daemonPath = NULL;
435
#endif
436

437
    /* We handle *ALL* URIs here. The caller has rejected any
438
     * URIs we don't care about */
439

440 441 442
    if (conn->uri) {
        if (!conn->uri->scheme) {
            /* This is the ///var/lib/xen/xend-socket local path style */
443 444 445 446 447
            if (!conn->uri->path)
                return VIR_DRV_OPEN_DECLINED;
            if (conn->uri->path[0] != '/')
                return VIR_DRV_OPEN_DECLINED;

448
            transport = trans_unix;
449
        } else {
450
            transport_str = get_transport_from_scheme(conn->uri->scheme);
451

452 453 454 455 456 457
            if (!transport_str) {
                if (conn->uri->server)
                    transport = trans_tls;
                else
                    transport = trans_unix;
            } else {
458
                if (STRCASEEQ(transport_str, "tls"))
459
                    transport = trans_tls;
460
                else if (STRCASEEQ(transport_str, "unix")) {
461
                    if (conn->uri->server) {
462 463 464 465
                        virReportError(VIR_ERR_INVALID_ARG,
                                       _("using unix socket and remote "
                                         "server '%s' is not supported."),
                                       conn->uri->server);
466 467 468 469
                        return VIR_DRV_OPEN_ERROR;
                    } else {
                        transport = trans_unix;
                    }
470
                } else if (STRCASEEQ(transport_str, "ssh"))
471
                    transport = trans_ssh;
472 473
                else if (STRCASEEQ(transport_str, "libssh2"))
                    transport = trans_libssh2;
474
                else if (STRCASEEQ(transport_str, "ext"))
475
                    transport = trans_ext;
476
                else if (STRCASEEQ(transport_str, "tcp"))
477 478
                    transport = trans_tcp;
                else {
479 480
                    virReportError(VIR_ERR_INVALID_ARG, "%s",
                                   _("remote_open: transport in URL not recognised "
481
                                     "(should be tls|unix|ssh|ext|tcp|libssh2)"));
482 483 484 485 486 487 488
                    return VIR_DRV_OPEN_ERROR;
                }
            }
        }
    } else {
        /* No URI, then must be probing so use UNIX socket */
        transport = trans_unix;
489
    }
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504
    /*
     * 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 已提交
505
    /* Local variables which we will initialize. These can
506 507
     * get freed in the failed: path.
     */
508 509
    char *name = NULL, *command = NULL, *sockname = NULL, *netcat = NULL;
    char *port = NULL, *authtype = NULL, *username = NULL;
L
Laine Stump 已提交
510
    bool sanity = true, verify = true, tty ATTRIBUTE_UNUSED = true;
511 512 513
    char *pkipath = NULL, *keyfile = NULL, *sshauth = NULL;

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

515 516 517
    /* Return code from this function, and the private data. */
    int retcode = VIR_DRV_OPEN_ERROR;

518
    /* Remote server defaults to "localhost" if not specified. */
519
    if (conn->uri && conn->uri->port != 0) {
520
        if (virAsprintf(&port, "%d", conn->uri->port) < 0)
521
            goto failed;
522
    } else if (transport == trans_tls) {
523 524
        if (VIR_STRDUP(port, LIBVIRTD_TLS_PORT) < 0)
            goto failed;
525
    } else if (transport == trans_tcp) {
526 527
        if (VIR_STRDUP(port, LIBVIRTD_TCP_PORT) < 0)
            goto failed;
528
    } /* Port not used for unix, ext., default for ssh */
529

530 531 532 533
    if (VIR_STRDUP(priv->hostname,
                   conn->uri && conn->uri->server ?
                   conn->uri->server : "localhost") < 0)
        goto failed;
534

535 536
    if (conn->uri && VIR_STRDUP(username, conn->uri->user) < 0)
        goto failed;
537

538 539 540 541 542
    /* 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).
     */
543
    size_t i;
544

545
    if (conn->uri) {
546
        for (i = 0; i < conn->uri->paramsCount; i++) {
547
            virURIParamPtr var = &conn->uri->params[i];
548 549 550 551
            EXTRACT_URI_ARG_STR("name", name);
            EXTRACT_URI_ARG_STR("command", command);
            EXTRACT_URI_ARG_STR("socket", sockname);
            EXTRACT_URI_ARG_STR("auth", authtype);
552
            EXTRACT_URI_ARG_STR("sshauth", sshauth);
553 554 555
            EXTRACT_URI_ARG_STR("netcat", netcat);
            EXTRACT_URI_ARG_STR("keyfile", keyfile);
            EXTRACT_URI_ARG_STR("pkipath", pkipath);
556 557
            EXTRACT_URI_ARG_STR("known_hosts", knownHosts);
            EXTRACT_URI_ARG_STR("known_hosts_verify", knownHostsVerify);
558 559 560 561 562 563

            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")) {
564 565
                /* Strip this param, used by virauth.c */
                var->ignore = 1;
566
                continue;
567
            }
568 569 570

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

573 574
        /* Construct the original name. */
        if (!name) {
575 576 577
            if (conn->uri->scheme &&
                (STREQ(conn->uri->scheme, "remote") ||
                 STRPREFIX(conn->uri->scheme, "remote+"))) {
578
                /* Allow remote serve to probe */
579 580
                if (VIR_STRDUP(name, "") < 0)
                    goto failed;
581
            } else {
M
Martin Kletzander 已提交
582
                virURI tmpuri = {
583
                    .scheme = conn->uri->scheme,
584
                    .query = virURIFormatParams(conn->uri),
585 586 587 588 589 590
                    .path = conn->uri->path,
                    .fragment = conn->uri->fragment,
                };

                /* Evil, blank out transport scheme temporarily */
                if (transport_str) {
591
                    assert(transport_str[-1] == '+');
592 593
                    transport_str[-1] = '\0';
                }
594

M
Martin Kletzander 已提交
595
                name = virURIFormat(&tmpuri);
596

597 598 599 600 601
                VIR_FREE(tmpuri.query);

                /* Restore transport scheme */
                if (transport_str)
                    transport_str[-1] = '+';
602 603 604

                if (!name)
                    goto failed;
605
            }
606
        }
607 608
    } else {
        /* Probe URI server side */
609 610
        if (VIR_STRDUP(name, "") < 0)
            goto failed;
611 612
    }

613
    VIR_DEBUG("proceeding with name = %s", name);
614

615 616
    /* For ext transport, command is required. */
    if (transport == trans_ext && !command) {
617 618
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("remote_open: for 'ext' transport, command is required"));
619 620 621
        goto failed;
    }

622
    VIR_DEBUG("Connecting with transport %d", transport);
623 624 625
    /* Connect to the remote service. */
    switch (transport) {
    case trans_tls:
626
#ifdef WITH_GNUTLS
627 628
        priv->tls = virNetTLSContextNewClientPath(pkipath,
                                                  geteuid() != 0 ? true : false,
629
                                                  sanity, verify);
630 631
        if (!priv->tls)
            goto failed;
632
        priv->is_secure = 1;
633 634
#else
        (void)sanity;
635
        (void)verify;
636 637 638 639
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("GNUTLS support not available in this build"));
        goto failed;
#endif
640 641

        /*FALLTHROUGH*/
642 643 644
    case trans_tcp:
        priv->client = virNetClientNewTCP(priv->hostname, port);
        if (!priv->client)
645 646
            goto failed;

647
#ifdef WITH_GNUTLS
648 649 650 651
        if (priv->tls) {
            VIR_DEBUG("Starting TLS session");
            if (virNetClientSetTLSSession(priv->client, priv->tls) < 0)
                goto failed;
652
        }
653
#endif
654 655 656

        break;

657
    case trans_libssh2:
658 659
        if (!sockname) {
            /* Right now we don't support default session connections */
660
            if (STREQ_NULLABLE(conn->uri->path, "/session")) {
661 662 663 664 665 666 667 668 669 670 671 672
                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;
        }
673 674 675 676 677 678 679 680 681 682 683 684

        VIR_DEBUG("Starting LibSSH2 session");

        priv->client = virNetClientNewLibSSH2(priv->hostname,
                                              port,
                                              username,
                                              keyfile,
                                              knownHosts,
                                              knownHostsVerify,
                                              sshauth,
                                              netcat,
                                              sockname,
685 686
                                              auth,
                                              conn->uri);
687 688 689 690 691 692
        if (!priv->client)
            goto failed;

        priv->is_secure = 1;
        break;

693
#ifndef WIN32
694
    case trans_unix:
695
        if (!sockname) {
696
            if (flags & VIR_DRV_OPEN_REMOTE_USER) {
697
                char *userdir = virGetUserRuntimeDirectory();
698

699
                if (!userdir)
700
                    goto failed;
701

702
                if (virAsprintf(&sockname, "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) {
703
                    VIR_FREE(userdir);
704
                    goto failed;
705 706
                }
                VIR_FREE(userdir);
707
            } else {
708 709 710 711
                if (VIR_STRDUP(sockname,
                               flags & VIR_DRV_OPEN_REMOTE_RO ?
                               LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
                    goto failed;
712
            }
713
            VIR_DEBUG("Proceeding with sockname %s", sockname);
714 715
        }

716 717
        if ((flags & VIR_DRV_OPEN_REMOTE_AUTOSTART) &&
            !(daemonPath = remoteFindDaemonPath())) {
718 719 720 721 722
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Unable to locate libvirtd daemon in %s "
                             "(to override, set $LIBVIRTD_PATH to the "
                             "name of the libvirtd binary)"),
                           SBINDIR);
723 724
            goto failed;
        }
725 726
        if (!(priv->client = virNetClientNewUNIX(sockname,
                                                 flags & VIR_DRV_OPEN_REMOTE_AUTOSTART,
727
                                                 daemonPath)))
C
Chris Lalancette 已提交
728
            goto failed;
729

730
        priv->is_secure = 1;
731 732
        break;

733
    case trans_ssh:
734 735
        if (!command && VIR_STRDUP(command, "ssh") < 0)
            goto failed;
J
Jim Meyering 已提交
736

737 738
        if (!sockname) {
            /* Right now we don't support default session connections */
739
            if (STREQ_NULLABLE(conn->uri->path, "/session")) {
740 741 742 743 744 745 746 747 748 749 750 751
                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;
        }
752 753 754 755 756

        if (!(priv->client = virNetClientNewSSH(priv->hostname,
                                                port,
                                                command,
                                                username,
757 758
                                                !tty,
                                                !verify,
759
                                                netcat ? netcat : "nc",
760
                                                keyfile,
761 762
                                                sockname)))
            goto failed;
763 764

        priv->is_secure = 1;
765
        break;
766 767

    case trans_ext: {
768 769
        char const *cmd_argv[] = { command, NULL };
        if (!(priv->client = virNetClientNewExternal(cmd_argv)))
770 771
            goto failed;

E
Eric Blake 已提交
772
        /* Do not set 'is_secure' flag since we can't guarantee
773 774
         * an external program is secure, and this flag must be
         * pessimistic */
775 776
    }   break;

777 778 779 780 781
#else /* WIN32 */

    case trans_unix:
    case trans_ssh:
    case trans_ext:
782 783 784
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("transport methods unix, ssh and ext are not supported "
                         "under Windows"));
785
        goto failed;
786 787

#endif /* WIN32 */
788 789
    } /* switch (transport) */

790

791 792 793 794 795 796 797 798 799
    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;
    }

800 801
    virObjectRef(conn->closeCallback);

802 803
    virNetClientSetCloseCallback(priv->client,
                                 remoteClientCloseFunc,
804
                                 conn->closeCallback, virObjectFreeCallback);
805

806 807 808 809 810
    if (!(priv->remoteProgram = virNetClientProgramNew(REMOTE_PROGRAM,
                                                       REMOTE_PROTOCOL_VERSION,
                                                       remoteDomainEvents,
                                                       ARRAY_CARDINALITY(remoteDomainEvents),
                                                       conn)))
811
        goto failed;
812 813 814 815 816 817
    if (!(priv->lxcProgram = virNetClientProgramNew(LXC_PROGRAM,
                                                    LXC_PROTOCOL_VERSION,
                                                    NULL,
                                                    0,
                                                    NULL)))
        goto failed;
818 819 820 821 822
    if (!(priv->qemuProgram = virNetClientProgramNew(QEMU_PROGRAM,
                                                     QEMU_PROTOCOL_VERSION,
                                                     NULL,
                                                     0,
                                                     NULL)))
823 824
        goto failed;

825
    if (virNetClientAddProgram(priv->client, priv->remoteProgram) < 0 ||
826
        virNetClientAddProgram(priv->client, priv->lxcProgram) < 0 ||
827
        virNetClientAddProgram(priv->client, priv->qemuProgram) < 0)
828
        goto failed;
829 830

    /* Try and authenticate with server */
831 832
    VIR_DEBUG("Trying authentication");
    if (remoteAuthenticate(conn, priv, auth, authtype) == -1)
833 834
        goto failed;

835
    if (virNetClientKeepAliveIsSupported(priv->client)) {
836
        remote_connect_supports_feature_args args =
837
            { VIR_DRV_FEATURE_PROGRAM_KEEPALIVE };
838
        remote_connect_supports_feature_ret ret = { 0 };
839 840
        int rc;

841 842 843
        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);
844

845
        if (rc != -1 && ret.supported) {
846 847 848 849 850 851 852
            priv->serverKeepAlive = true;
        } else {
            VIR_INFO("Disabling keepalive protocol since it is not supported"
                     " by the server");
        }
    }

853
    /* Finally we can call the remote side's open function. */
854
    {
855
        remote_connect_open_args args = { &name, flags };
856

857
        VIR_DEBUG("Trying to open URI %s", name);
858 859
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_OPEN,
                 (xdrproc_t) xdr_remote_connect_open_args, (char *) &args,
860
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
861 862
            goto failed;
    }
863

864 865
    /* Now try and find out what URI the daemon used */
    if (conn->uri == NULL) {
866
        remote_connect_get_uri_ret uriret;
867

868
        VIR_DEBUG("Trying to query remote URI");
869 870
        memset(&uriret, 0, sizeof(uriret));
        if (call(conn, priv, 0,
871
                 REMOTE_PROC_CONNECT_GET_URI,
872
                 (xdrproc_t) xdr_void, (char *) NULL,
873
                 (xdrproc_t) xdr_remote_connect_get_uri_ret, (char *) &uriret) < 0)
874 875
            goto failed;

876
        VIR_DEBUG("Auto-probed URI is %s", uriret.uri);
M
Martin Kletzander 已提交
877
        conn->uri = virURIParse(uriret.uri);
878
        VIR_FREE(uriret.uri);
879
        if (!conn->uri)
880 881 882
            goto failed;
    }

883
    if (!(priv->domainEventState = virDomainEventStateNew()))
884 885
        goto failed;

886 887 888
    /* Successful. */
    retcode = VIR_DRV_OPEN_SUCCESS;

889
 cleanup:
890
    /* Free up the URL and strings. */
891 892 893 894 895
    VIR_FREE(name);
    VIR_FREE(command);
    VIR_FREE(sockname);
    VIR_FREE(authtype);
    VIR_FREE(netcat);
896
    VIR_FREE(sshauth);
897
    VIR_FREE(keyfile);
898 899
    VIR_FREE(username);
    VIR_FREE(port);
900
    VIR_FREE(pkipath);
901 902
    VIR_FREE(knownHostsVerify);
    VIR_FREE(knownHosts);
903 904

    return retcode;
905 906

 failed:
907
    virObjectUnref(priv->remoteProgram);
908
    virObjectUnref(priv->lxcProgram);
909
    virObjectUnref(priv->qemuProgram);
910
    virNetClientClose(priv->client);
911
    virObjectUnref(priv->client);
912
    priv->client = NULL;
913

914
    VIR_FREE(priv->hostname);
915
    goto cleanup;
916
}
917 918
#undef EXTRACT_URI_ARG_STR
#undef EXTRACT_URI_ARG_BOOL
919

920
static struct private_data *
921
remoteAllocPrivateData(void)
922
{
923
    struct private_data *priv;
924
    if (VIR_ALLOC(priv) < 0)
925
        return NULL;
926

927
    if (virMutexInit(&priv->lock) < 0) {
928 929
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("cannot initialize mutex"));
930
        VIR_FREE(priv);
931
        return NULL;
932 933 934
    }
    remoteDriverLock(priv);
    priv->localUses = 1;
935 936 937 938 939 940 941

    return priv;
}

static int
remoteOpenSecondaryDriver(virConnectPtr conn,
                          virConnectAuthPtr auth,
E
Eric Blake 已提交
942
                          unsigned int flags,
943 944 945 946 947
                          struct private_data **priv)
{
    int ret;
    int rflags = 0;

948
    if (!((*priv) = remoteAllocPrivateData()))
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
        return VIR_DRV_OPEN_ERROR;

    if (flags & VIR_CONNECT_RO)
        rflags |= VIR_DRV_OPEN_REMOTE_RO;

    ret = doRemoteOpen(conn, *priv, auth, rflags);
    if (ret != VIR_DRV_OPEN_SUCCESS) {
        remoteDriverUnlock(*priv);
        VIR_FREE(*priv);
    } else {
        (*priv)->localUses = 1;
        remoteDriverUnlock(*priv);
    }

    return ret;
}

static virDrvOpenStatus
967 968 969
remoteConnectOpen(virConnectPtr conn,
                  virConnectAuthPtr auth,
                  unsigned int flags)
970 971 972
{
    struct private_data *priv;
    int ret, rflags = 0;
973
    const char *autostart = virGetEnvBlockSUID("LIBVIRT_AUTOSTART");
974

975
    if (inside_daemon && (!conn->uri || (conn->uri && !conn->uri->server)))
976 977
        return VIR_DRV_OPEN_DECLINED;

978
    if (!(priv = remoteAllocPrivateData()))
979
        return VIR_DRV_OPEN_ERROR;
980

981
    if (flags & VIR_CONNECT_RO)
982 983
        rflags |= VIR_DRV_OPEN_REMOTE_RO;

984 985 986 987 988 989 990 991 992
    /*
     * 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 已提交
993
        conn->uri->scheme &&
994 995
        ((strchr(conn->uri->scheme, '+') == 0)||
         (strstr(conn->uri->scheme, "+unix") != NULL)) &&
996 997
        (STREQ(conn->uri->path, "/session") ||
         STRPREFIX(conn->uri->scheme, "test+")) &&
998
        geteuid() > 0) {
999
        VIR_DEBUG("Auto-spawn user daemon instance");
1000
        rflags |= VIR_DRV_OPEN_REMOTE_USER;
1001 1002 1003
        if (!virIsSUID() &&
            (!autostart ||
             STRNEQ(autostart, "0")))
1004
            rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1005 1006 1007
    }

    /*
J
John Levon 已提交
1008 1009 1010 1011
     * 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.
1012 1013
     */
    if (!conn->uri) {
1014
        VIR_DEBUG("Auto-probe remote URI");
J
John Levon 已提交
1015
#ifndef __sun
1016
        if (geteuid() > 0) {
1017
            VIR_DEBUG("Auto-spawn user daemon instance");
1018
            rflags |= VIR_DRV_OPEN_REMOTE_USER;
1019 1020 1021
            if (!virIsSUID() &&
                (!autostart ||
                 STRNEQ(autostart, "0")))
1022
                rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1023
        }
J
John Levon 已提交
1024
#endif
1025
    }
1026

1027
    ret = doRemoteOpen(conn, priv, auth, rflags);
1028 1029
    if (ret != VIR_DRV_OPEN_SUCCESS) {
        conn->privateData = NULL;
1030
        remoteDriverUnlock(priv);
1031
        VIR_FREE(priv);
1032 1033
    } else {
        conn->privateData = priv;
1034
        remoteDriverUnlock(priv);
1035 1036 1037 1038 1039
    }
    return ret;
}


1040 1041
/* In a string "driver+transport" return a pointer to "transport". */
static char *
1042
get_transport_from_scheme(char *scheme)
1043
{
1044 1045
    char *p = strchr(scheme, '+');
    return p ? p + 1 : NULL;
1046 1047
}

1048
/*----------------------------------------------------------------------*/
1049

1050 1051

static int
1052
doRemoteClose(virConnectPtr conn, struct private_data *priv)
1053
{
1054 1055
    int ret = 0;

1056
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_CLOSE,
1057 1058
             (xdrproc_t) xdr_void, (char *) NULL,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
1059
        ret = -1;
1060

1061
#ifdef WITH_GNUTLS
1062
    virObjectUnref(priv->tls);
1063
    priv->tls = NULL;
1064
#endif
1065

1066 1067
    virNetClientSetCloseCallback(priv->client,
                                 NULL,
1068 1069
                                 conn->closeCallback, virObjectFreeCallback);

1070
    virNetClientClose(priv->client);
1071
    virObjectUnref(priv->client);
1072
    priv->client = NULL;
1073
    virObjectUnref(priv->remoteProgram);
1074
    virObjectUnref(priv->lxcProgram);
1075
    virObjectUnref(priv->qemuProgram);
1076
    priv->remoteProgram = priv->qemuProgram = priv->lxcProgram = NULL;
1077 1078 1079 1080 1081 1082 1083 1084 1085

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

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

    virDomainEventStateFree(priv->domainEventState);
    priv->domainEventState = NULL;
1086

1087
    return ret;
1088 1089
}

1090
static int
1091
remoteConnectClose(virConnectPtr conn)
1092
{
1093 1094
    int ret = 0;
    struct private_data *priv = conn->privateData;
1095

1096 1097 1098 1099 1100 1101 1102
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        ret = doRemoteClose(conn, priv);
        conn->privateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
1103
        VIR_FREE(priv);
1104
    }
1105 1106
    if (priv)
        remoteDriverUnlock(priv);
1107

1108 1109
    return ret;
}
1110

1111

1112 1113 1114 1115 1116 1117 1118 1119 1120
/* 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 *
1121
remoteConnectGetType(virConnectPtr conn)
1122 1123
{
    char *rv = NULL;
1124
    remote_connect_get_type_ret ret;
1125
    struct private_data *priv = conn->privateData;
1126

1127
    remoteDriverLock(priv);
1128

1129 1130 1131 1132 1133
    /* Cached? */
    if (priv->type) {
        rv = priv->type;
        goto done;
    }
1134

1135
    memset(&ret, 0, sizeof(ret));
1136
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_GET_TYPE,
1137
             (xdrproc_t) xdr_void, (char *) NULL,
1138
             (xdrproc_t) xdr_remote_connect_get_type_ret, (char *) &ret) == -1)
1139
        goto done;
1140

1141 1142
    /* Stash. */
    rv = priv->type = ret.type;
1143

1144 1145 1146 1147
done:
    remoteDriverUnlock(priv);
    return rv;
}
1148

1149
static int remoteConnectIsSecure(virConnectPtr conn)
1150 1151 1152
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
1153
    remote_connect_is_secure_ret ret;
1154
    remoteDriverLock(priv);
1155

1156
    memset(&ret, 0, sizeof(ret));
1157
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_IS_SECURE,
1158
             (xdrproc_t) xdr_void, (char *) NULL,
1159
             (xdrproc_t) xdr_remote_connect_is_secure_ret, (char *) &ret) == -1)
1160
        goto done;
1161

1162 1163 1164 1165 1166 1167 1168 1169 1170
    /* 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;
1171

1172 1173 1174 1175
done:
    remoteDriverUnlock(priv);
    return rv;
}
1176

1177
static int remoteConnectIsEncrypted(virConnectPtr conn)
1178 1179
{
    int rv = -1;
1180
    bool encrypted;
1181
    struct private_data *priv = conn->privateData;
1182
    remote_connect_is_secure_ret ret;
1183
    remoteDriverLock(priv);
1184

1185
    memset(&ret, 0, sizeof(ret));
1186
    if (call(conn, priv, 0, REMOTE_PROC_CONNECT_IS_SECURE,
1187
             (xdrproc_t) xdr_void, (char *) NULL,
1188
             (xdrproc_t) xdr_remote_connect_is_secure_ret, (char *) &ret) == -1)
1189
        goto done;
1190

1191
    encrypted = virNetClientIsEncrypted(priv->client);
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

1208
static int
1209 1210 1211 1212
remoteNodeGetCPUStats(virConnectPtr conn,
                      int cpuNum,
                      virNodeCPUStatsPtr params, int *nparams,
                      unsigned int flags)
1213 1214 1215 1216
{
    int rv = -1;
    remote_node_get_cpu_stats_args args;
    remote_node_get_cpu_stats_ret ret;
1217
    size_t i;
1218 1219 1220 1221 1222 1223 1224 1225
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

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

1226 1227 1228 1229 1230 1231
    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)
1232 1233 1234 1235 1236
        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) {
1237 1238 1239
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteNodeGetCPUStats: "
                         "returned number of stats exceeds limit"));
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
        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) {
1256 1257 1258
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Stats %s too big for destination"),
                           ret.params.params_val[i].field);
1259 1260 1261 1262 1263 1264 1265 1266
            goto cleanup;
        }
        params[i].value = ret.params.params_val[i].value;
    }

    rv = 0;

cleanup:
1267
    xdr_free((xdrproc_t) xdr_remote_node_get_cpu_stats_ret, (char *) &ret);
1268 1269 1270 1271 1272
done:
    remoteDriverUnlock(priv);
    return rv;
}

1273
static int
1274 1275 1276 1277 1278
remoteNodeGetMemoryStats(virConnectPtr conn,
                         int cellNum,
                         virNodeMemoryStatsPtr params,
                         int *nparams,
                         unsigned int flags)
1279 1280 1281 1282
{
    int rv = -1;
    remote_node_get_memory_stats_args args;
    remote_node_get_memory_stats_ret ret;
1283
    size_t i;
1284 1285 1286 1287 1288 1289 1290 1291
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

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

1292
    memset(&ret, 0, sizeof(ret));
1293 1294 1295
    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)
1296 1297 1298 1299 1300
        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) {
1301 1302 1303
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteNodeGetMemoryStats: "
                         "returned number of stats exceeds limit"));
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
        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) {
1320 1321 1322
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Stats %s too big for destination"),
                           ret.params.params_val[i].field);
1323 1324 1325 1326 1327 1328 1329 1330
            goto cleanup;
        }
        params[i].value = ret.params.params_val[i].value;
    }

    rv = 0;

cleanup:
1331
    xdr_free((xdrproc_t) xdr_remote_node_get_memory_stats_ret, (char *) &ret);
1332 1333 1334 1335 1336
done:
    remoteDriverUnlock(priv);
    return rv;
}

1337 1338
static int
remoteNodeGetCellsFreeMemory(virConnectPtr conn,
1339 1340 1341
                             unsigned long long *freeMems,
                             int startCell,
                             int maxCells)
1342
{
1343
    int rv = -1;
1344 1345
    remote_node_get_cells_free_memory_args args;
    remote_node_get_cells_free_memory_ret ret;
1346
    size_t i;
1347
    struct private_data *priv = conn->privateData;
1348

1349 1350
    remoteDriverLock(priv);

1351
    if (maxCells > REMOTE_NODE_MAX_CELLS) {
1352 1353 1354
        virReportError(VIR_ERR_RPC,
                       _("too many NUMA cells: %d > %d"),
                       maxCells, REMOTE_NODE_MAX_CELLS);
1355
        goto done;
1356 1357 1358
    }

    args.startCell = startCell;
1359
    args.maxcells = maxCells;
1360

1361 1362 1363 1364
    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)
1365
        goto done;
1366

1367
    for (i = 0; i < ret.cells.cells_len; i++)
1368
        freeMems[i] = ret.cells.cells_val[i];
1369 1370 1371

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

1372
    rv = ret.cells.cells_len;
1373 1374

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

1379
static int
1380
remoteConnectListDomains(virConnectPtr conn, int *ids, int maxids)
1381
{
1382
    int rv = -1;
1383
    size_t i;
1384 1385
    remote_connect_list_domains_args args;
    remote_connect_list_domains_ret ret;
1386
    struct private_data *priv = conn->privateData;
1387

1388 1389
    remoteDriverLock(priv);

1390
    if (maxids > REMOTE_DOMAIN_LIST_MAX) {
1391
        virReportError(VIR_ERR_RPC,
1392 1393
                       _("Too many domains '%d' for limit '%d'"),
                       maxids, REMOTE_DOMAIN_LIST_MAX);
1394
        goto done;
1395 1396 1397
    }
    args.maxids = maxids;

1398
    memset(&ret, 0, sizeof(ret));
1399 1400 1401
    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)
1402
        goto done;
1403 1404

    if (ret.ids.ids_len > maxids) {
1405
        virReportError(VIR_ERR_RPC,
1406
                       _("Too many domains '%d' for limit '%d'"),
1407
                       ret.ids.ids_len, maxids);
1408
        goto cleanup;
1409 1410 1411 1412 1413
    }

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

1414 1415 1416
    rv = ret.ids.ids_len;

cleanup:
1417
    xdr_free((xdrproc_t) xdr_remote_connect_list_domains_ret, (char *) &ret);
1418

1419
done:
1420
    remoteDriverUnlock(priv);
1421
    return rv;
1422 1423
}

1424 1425 1426 1427 1428 1429
static int
remoteConnectListAllDomains(virConnectPtr conn,
                            virDomainPtr **domains,
                            unsigned int flags)
{
    int rv = -1;
1430
    size_t i;
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
    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;

1453 1454 1455 1456 1457 1458 1459
    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;
    }

1460
    if (domains) {
1461
        if (VIR_ALLOC_N(doms, ret.domains.domains_len + 1) < 0)
1462 1463 1464 1465
            goto cleanup;

        for (i = 0; i < ret.domains.domains_len; i++) {
            doms[i] = get_nonnull_domain(conn, ret.domains.domains_val[i]);
1466
            if (!doms[i])
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
                goto cleanup;
        }
        *domains = doms;
        doms = NULL;
    }

    rv = ret.ret;

cleanup:
    if (doms) {
        for (i = 0; i < ret.domains.domains_len; i++)
            if (doms[i])
                virDomainFree(doms[i]);
        VIR_FREE(doms);
    }

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

1490 1491 1492 1493 1494
/* Helper to free typed parameters. */
static void
remoteFreeTypedParameters(remote_typed_param *args_params_val,
                          u_int args_params_len)
{
1495
    size_t i;
1496 1497 1498 1499

    if (args_params_val == NULL)
        return;

1500
    for (i = 0; i < args_params_len; i++) {
1501
        VIR_FREE(args_params_val[i].field);
1502 1503 1504
        if (args_params_val[i].value.type == VIR_TYPED_PARAM_STRING)
            VIR_FREE(args_params_val[i].value.remote_typed_param_value_u.s);
    }
1505 1506 1507 1508

    VIR_FREE(args_params_val);
}

1509
/* Helper to serialize typed parameters. */
1510
static int
1511 1512
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
1513 1514
                               remote_typed_param **args_params_val,
                               u_int *args_params_len)
1515
{
1516
    size_t i;
1517
    int rv = -1;
1518
    remote_typed_param *val;
1519

1520
    *args_params_len = nparams;
1521
    if (VIR_ALLOC_N(val, nparams) < 0)
1522
        goto cleanup;
1523 1524

    for (i = 0; i < nparams; ++i) {
1525
        /* call() will free this: */
1526
        if (VIR_STRDUP(val[i].field, params[i].field) < 0)
1527 1528
            goto cleanup;
        val[i].value.type = params[i].type;
1529
        switch (params[i].type) {
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
        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;
1548
        case VIR_TYPED_PARAM_STRING:
1549 1550
            if (VIR_STRDUP(val[i].value.remote_typed_param_value_u.s,
                           params[i].value.s) < 0)
1551 1552
                goto cleanup;
            break;
1553
        default:
1554 1555
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                           params[i].type);
1556
            goto cleanup;
1557 1558 1559
        }
    }

1560 1561 1562 1563 1564
    *args_params_val = val;
    val = NULL;
    rv = 0;

cleanup:
1565
    remoteFreeTypedParameters(val, nparams);
1566 1567 1568 1569 1570
    return rv;
}

/* Helper to deserialize typed parameters. */
static int
1571 1572
remoteDeserializeTypedParameters(remote_typed_param *ret_params_val,
                                 u_int ret_params_len,
1573
                                 int limit,
1574
                                 virTypedParameterPtr *params,
1575 1576
                                 int *nparams)
{
1577
    size_t i = 0;
1578
    int rv = -1;
1579
    bool userAllocated = *params != NULL;
1580

1581 1582 1583 1584 1585 1586 1587 1588
    if (userAllocated) {
        /* Check the length of the returned list carefully. */
        if (ret_params_len > limit || ret_params_len > *nparams) {
            virReportError(VIR_ERR_RPC, "%s",
                           _("returned number of parameters exceeds limit"));
            goto cleanup;
        }
    } else {
1589
        if (VIR_ALLOC_N(*params, ret_params_len) < 0)
1590
            goto cleanup;
1591 1592 1593 1594 1595
    }
    *nparams = ret_params_len;

    /* Deserialise the result. */
    for (i = 0; i < ret_params_len; ++i) {
1596 1597 1598 1599 1600
        virTypedParameterPtr param = *params + i;
        remote_typed_param *ret_param = ret_params_val + i;

        if (virStrcpyStatic(param->field,
                            ret_param->field) == NULL) {
1601 1602
            virReportError(VIR_ERR_INTERNAL_ERROR,
                           _("Parameter %s too big for destination"),
1603
                           ret_param->field);
1604 1605
            goto cleanup;
        }
1606 1607 1608

        param->type = ret_param->value.type;
        switch (param->type) {
1609
        case VIR_TYPED_PARAM_INT:
1610 1611
            param->value.i =
                ret_param->value.remote_typed_param_value_u.i;
1612 1613
            break;
        case VIR_TYPED_PARAM_UINT:
1614 1615
            param->value.ui =
                ret_param->value.remote_typed_param_value_u.ui;
1616 1617
            break;
        case VIR_TYPED_PARAM_LLONG:
1618 1619
            param->value.l =
                ret_param->value.remote_typed_param_value_u.l;
1620 1621
            break;
        case VIR_TYPED_PARAM_ULLONG:
1622 1623
            param->value.ul =
                ret_param->value.remote_typed_param_value_u.ul;
1624 1625
            break;
        case VIR_TYPED_PARAM_DOUBLE:
1626 1627
            param->value.d =
                ret_param->value.remote_typed_param_value_u.d;
1628 1629
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
1630 1631
            param->value.b =
                ret_param->value.remote_typed_param_value_u.b;
1632
            break;
1633
        case VIR_TYPED_PARAM_STRING:
1634 1635
            if (VIR_STRDUP(param->value.s,
                           ret_param->value.remote_typed_param_value_u.s) < 0)
1636 1637
                goto cleanup;
            break;
1638
        default:
1639
            virReportError(VIR_ERR_RPC, _("unknown parameter type: %d"),
1640
                           param->type);
1641 1642 1643 1644 1645 1646 1647
            goto cleanup;
        }
    }

    rv = 0;

cleanup:
1648 1649 1650 1651 1652 1653 1654 1655
    if (rv < 0) {
        if (userAllocated) {
            virTypedParamsClear(*params, i);
        } else {
            virTypedParamsFree(*params, i);
            *params = NULL;
        }
    }
1656 1657 1658
    return rv;
}

1659 1660 1661 1662 1663 1664 1665
static int
remoteDeserializeDomainDiskErrors(remote_domain_disk_error *ret_errors_val,
                                  u_int ret_errors_len,
                                  int limit,
                                  virDomainDiskErrorPtr errors,
                                  int maxerrors)
{
1666 1667
    size_t i = 0;
    size_t j;
1668 1669

    if (ret_errors_len > limit || ret_errors_len > maxerrors) {
1670 1671
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned number of disk errors exceeds limit"));
1672 1673 1674 1675
        goto error;
    }

    for (i = 0; i < ret_errors_len; i++) {
1676
        if (VIR_STRDUP(errors[i].disk, ret_errors_val[i].disk) < 0)
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689
            goto error;
        errors[i].error = ret_errors_val[i].error;
    }

    return 0;

error:
    for (j = 0; j < i; j++)
        VIR_FREE(errors[i].disk);

    return -1;
}

1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
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);

1704
    make_nonnull_domain(&args.dom, domain);
1705 1706 1707 1708
    args.nparams = *nparams;
    args.path = (char *) path;
    args.flags = flags;

1709 1710 1711 1712
    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)
1713 1714 1715 1716 1717
        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) {
1718 1719 1720
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteDomainBlockStatsFlags: "
                         "returned number of stats exceeds limit"));
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
        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,
1739
                                         &params,
1740 1741 1742 1743 1744 1745
                                         nparams) < 0)
        goto cleanup;

    rv = 0;

cleanup:
1746 1747
    xdr_free((xdrproc_t) xdr_remote_domain_block_stats_flags_ret,
             (char *) &ret);
1748 1749 1750 1751 1752
done:
    remoteDriverUnlock(priv);
    return rv;
}

1753
static int
1754 1755 1756
remoteDomainGetMemoryParameters(virDomainPtr domain,
                                virTypedParameterPtr params, int *nparams,
                                unsigned int flags)
1757 1758 1759 1760 1761 1762 1763 1764
{
    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);

1765
    make_nonnull_domain(&args.dom, domain);
1766 1767 1768
    args.nparams = *nparams;
    args.flags = flags;

1769 1770 1771 1772
    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)
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
        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;
    }

1784 1785
    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
1786
                                         REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX,
1787
                                         &params,
1788 1789
                                         nparams) < 0)
        goto cleanup;
1790 1791 1792 1793

    rv = 0;

cleanup:
1794 1795
    xdr_free((xdrproc_t) xdr_remote_domain_get_memory_parameters_ret,
             (char *) &ret);
1796 1797 1798 1799 1800
done:
    remoteDriverUnlock(priv);
    return rv;
}

1801
static int
1802 1803 1804
remoteDomainGetNumaParameters(virDomainPtr domain,
                              virTypedParameterPtr params, int *nparams,
                              unsigned int flags)
1805 1806 1807 1808 1809 1810 1811 1812
{
    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);

1813
    make_nonnull_domain(&args.dom, domain);
1814 1815 1816
    args.nparams = *nparams;
    args.flags = flags;

1817 1818 1819 1820
    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)
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
        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,
1835
                                         &params,
1836 1837 1838 1839 1840 1841
                                         nparams) < 0)
        goto cleanup;

    rv = 0;

cleanup:
1842 1843
    xdr_free((xdrproc_t) xdr_remote_domain_get_numa_parameters_ret,
             (char *) &ret);
1844 1845 1846 1847 1848
done:
    remoteDriverUnlock(priv);
    return rv;
}

1849
static int
1850 1851 1852
remoteDomainGetBlkioParameters(virDomainPtr domain,
                               virTypedParameterPtr params, int *nparams,
                               unsigned int flags)
1853 1854 1855 1856 1857 1858 1859 1860
{
    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);

1861
    make_nonnull_domain(&args.dom, domain);
1862 1863 1864
    args.nparams = *nparams;
    args.flags = flags;

1865 1866 1867 1868
    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)
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879
        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;
    }

1880 1881
    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
1882
                                         REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX,
1883
                                         &params,
1884 1885
                                         nparams) < 0)
        goto cleanup;
1886 1887 1888 1889

    rv = 0;

cleanup:
1890 1891
    xdr_free((xdrproc_t) xdr_remote_domain_get_blkio_parameters_ret,
             (char *) &ret);
1892 1893 1894 1895 1896
done:
    remoteDriverUnlock(priv);
    return rv;
}

1897
static int
1898 1899 1900 1901 1902
remoteDomainGetVcpuPinInfo(virDomainPtr domain,
                           int ncpumaps,
                           unsigned char *cpumaps,
                           int maplen,
                           unsigned int flags)
1903 1904
{
    int rv = -1;
1905
    size_t i;
E
Eric Blake 已提交
1906 1907
    remote_domain_get_vcpu_pin_info_args args;
    remote_domain_get_vcpu_pin_info_ret ret;
1908 1909 1910 1911 1912
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    if (ncpumaps > REMOTE_VCPUINFO_MAX) {
1913 1914 1915
        virReportError(VIR_ERR_RPC,
                       _("vCPU count exceeds maximum: %d > %d"),
                       ncpumaps, REMOTE_VCPUINFO_MAX);
1916 1917 1918 1919 1920
        goto done;
    }

    if (INT_MULTIPLY_OVERFLOW(ncpumaps, maplen) ||
        ncpumaps * maplen > REMOTE_CPUMAPS_MAX) {
1921 1922 1923
        virReportError(VIR_ERR_RPC,
                       _("vCPU map buffer length exceeds maximum: %d > %d"),
                       ncpumaps * maplen, REMOTE_CPUMAPS_MAX);
1924 1925 1926
        goto done;
    }

1927
    make_nonnull_domain(&args.dom, domain);
1928 1929 1930 1931
    args.ncpumaps = ncpumaps;
    args.maplen = maplen;
    args.flags = flags;

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

1934 1935 1936 1937 1938
    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)
1939 1940 1941
        goto done;

    if (ret.num > ncpumaps) {
1942 1943 1944
        virReportError(VIR_ERR_RPC,
                       _("host reports too many vCPUs: %d > %d"),
                       ret.num, ncpumaps);
1945 1946 1947 1948
        goto cleanup;
    }

    if (ret.cpumaps.cpumaps_len > ncpumaps * maplen) {
1949 1950 1951
        virReportError(VIR_ERR_RPC,
                       _("host reports map buffer length exceeds maximum: %d > %d"),
                       ret.cpumaps.cpumaps_len, ncpumaps * maplen);
1952 1953 1954
        goto cleanup;
    }

1955
    memset(cpumaps, 0, ncpumaps * maplen);
1956 1957 1958 1959 1960 1961 1962

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

    rv = ret.num;

cleanup:
1963
    xdr_free((xdrproc_t) xdr_remote_domain_get_vcpu_pin_info_ret, (char *) &ret);
1964 1965 1966 1967 1968 1969

done:
    remoteDriverUnlock(priv);
    return rv;
}

1970
static int
1971 1972 1973 1974
remoteDomainPinEmulator(virDomainPtr dom,
                        unsigned char *cpumap,
                        int cpumaplen,
                        unsigned int flags)
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
{
    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;

done:
    remoteDriverUnlock(priv);
    return rv;
}


static int
2010 2011 2012 2013
remoteDomainGetEmulatorPinInfo(virDomainPtr domain,
                               unsigned char *cpumaps,
                               int maplen,
                               unsigned int flags)
2014 2015
{
    int rv = -1;
2016
    size_t i;
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
    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));

2037 2038 2039 2040 2041
    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)
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058
        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;

cleanup:
2059 2060
    xdr_free((xdrproc_t) xdr_remote_domain_get_emulator_pin_info_ret,
             (char *) &ret);
2061 2062 2063 2064 2065 2066

done:
    remoteDriverUnlock(priv);
    return rv;
}

2067
static int
2068 2069 2070 2071 2072
remoteDomainGetVcpus(virDomainPtr domain,
                     virVcpuInfoPtr info,
                     int maxinfo,
                     unsigned char *cpumaps,
                     int maplen)
2073
{
2074
    int rv = -1;
2075
    size_t i;
2076 2077
    remote_domain_get_vcpus_args args;
    remote_domain_get_vcpus_ret ret;
2078
    struct private_data *priv = domain->conn->privateData;
2079

2080 2081
    remoteDriverLock(priv);

2082
    if (maxinfo > REMOTE_VCPUINFO_MAX) {
2083 2084 2085
        virReportError(VIR_ERR_RPC,
                       _("vCPU count exceeds maximum: %d > %d"),
                       maxinfo, REMOTE_VCPUINFO_MAX);
2086
        goto done;
2087
    }
2088 2089
    if (INT_MULTIPLY_OVERFLOW(maxinfo, maplen) ||
        maxinfo * maplen > REMOTE_CPUMAPS_MAX) {
2090 2091 2092
        virReportError(VIR_ERR_RPC,
                       _("vCPU map buffer length exceeds maximum: %d > %d"),
                       maxinfo * maplen, REMOTE_CPUMAPS_MAX);
2093
        goto done;
2094 2095
    }

2096
    make_nonnull_domain(&args.dom, domain);
2097 2098 2099
    args.maxinfo = maxinfo;
    args.maplen = maplen;

2100 2101 2102 2103
    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)
2104
        goto done;
2105 2106

    if (ret.info.info_len > maxinfo) {
2107 2108 2109
        virReportError(VIR_ERR_RPC,
                       _("host reports too many vCPUs: %d > %d"),
                       ret.info.info_len, maxinfo);
2110
        goto cleanup;
2111
    }
2112
    if (ret.cpumaps.cpumaps_len > maxinfo * maplen) {
2113 2114 2115
        virReportError(VIR_ERR_RPC,
                       _("host reports map buffer length exceeds maximum: %d > %d"),
                       ret.cpumaps.cpumaps_len, maxinfo * maplen);
2116
        goto cleanup;
2117 2118
    }

2119 2120
    memset(info, 0, sizeof(virVcpuInfo) * maxinfo);
    memset(cpumaps, 0, maxinfo * maplen);
2121

2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
    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];

2132 2133 2134
    rv = ret.info.info_len;

cleanup:
2135
    xdr_free((xdrproc_t) xdr_remote_domain_get_vcpus_ret, (char *) &ret);
2136 2137

done:
2138
    remoteDriverUnlock(priv);
2139
    return rv;
2140 2141
}

2142
static int
2143
remoteDomainGetSecurityLabel(virDomainPtr domain, virSecurityLabelPtr seclabel)
2144 2145 2146 2147
{
    remote_domain_get_security_label_args args;
    remote_domain_get_security_label_ret ret;
    struct private_data *priv = domain->conn->privateData;
2148 2149 2150
    int rv = -1;

    remoteDriverLock(priv);
2151

2152 2153 2154
    make_nonnull_domain(&args.dom, domain);
    memset(&ret, 0, sizeof(ret));
    memset(seclabel, 0, sizeof(*seclabel));
2155

2156 2157 2158
    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) {
2159
        goto done;
2160 2161 2162
    }

    if (ret.label.label_val != NULL) {
2163
        if (strlen(ret.label.label_val) >= sizeof(seclabel->label)) {
2164 2165
            virReportError(VIR_ERR_RPC, _("security label exceeds maximum: %zu"),
                           sizeof(seclabel->label) - 1);
A
Alex Jia 已提交
2166
            goto cleanup;
2167
        }
2168
        strcpy(seclabel->label, ret.label.label_val);
2169 2170 2171
        seclabel->enforcing = ret.enforcing;
    }

2172 2173
    rv = 0;

A
Alex Jia 已提交
2174 2175 2176
cleanup:
    xdr_free((xdrproc_t) xdr_remote_domain_get_security_label_ret, (char *)&ret);

2177 2178 2179
done:
    remoteDriverUnlock(priv);
    return rv;
2180 2181
}

M
Marcelo Cerri 已提交
2182
static int
2183
remoteDomainGetSecurityLabelList(virDomainPtr domain, virSecurityLabelPtr* seclabels)
M
Marcelo Cerri 已提交
2184 2185 2186 2187
{
    remote_domain_get_security_label_list_args args;
    remote_domain_get_security_label_list_ret ret;
    struct private_data *priv = domain->conn->privateData;
2188 2189
    size_t i;
    int rv = -1;
M
Marcelo Cerri 已提交
2190 2191 2192

    remoteDriverLock(priv);

2193
    make_nonnull_domain(&args.dom, domain);
M
Marcelo Cerri 已提交
2194 2195
    memset(&ret, 0, sizeof(ret));

2196 2197 2198
    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 已提交
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
        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;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243
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;

2244
    memset(&ret, 0, sizeof(ret));
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260
    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;

done:
    remoteDriverUnlock(priv);
    return rv;
}

2261
static int
2262
remoteNodeGetSecurityModel(virConnectPtr conn, virSecurityModelPtr secmodel)
2263 2264 2265
{
    remote_node_get_security_model_ret ret;
    struct private_data *priv = conn->privateData;
2266 2267 2268
    int rv = -1;

    remoteDriverLock(priv);
2269

2270 2271
    memset(&ret, 0, sizeof(ret));
    memset(secmodel, 0, sizeof(*secmodel));
2272

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

    if (ret.model.model_val != NULL) {
2280
        if (strlen(ret.model.model_val) >= sizeof(secmodel->model)) {
2281 2282
            virReportError(VIR_ERR_RPC, _("security model exceeds maximum: %zu"),
                           sizeof(secmodel->model) - 1);
A
Alex Jia 已提交
2283
            goto cleanup;
2284
        }
2285
        strcpy(secmodel->model, ret.model.model_val);
2286 2287 2288
    }

    if (ret.doi.doi_val != NULL) {
2289
        if (strlen(ret.doi.doi_val) >= sizeof(secmodel->doi)) {
2290 2291
            virReportError(VIR_ERR_RPC, _("security doi exceeds maximum: %zu"),
                           sizeof(secmodel->doi) - 1);
A
Alex Jia 已提交
2292
            goto cleanup;
2293
        }
2294
        strcpy(secmodel->doi, ret.doi.doi_val);
2295
    }
2296 2297 2298

    rv = 0;

A
Alex Jia 已提交
2299 2300 2301
cleanup:
    xdr_free((xdrproc_t) xdr_remote_node_get_security_model_ret, (char *)&ret);

2302 2303 2304
done:
    remoteDriverUnlock(priv);
    return rv;
2305 2306
}

2307
static int
2308 2309 2310 2311 2312
remoteDomainMigratePrepare(virConnectPtr dconn,
                           char **cookie, int *cookielen,
                           const char *uri_in, char **uri_out,
                           unsigned long flags, const char *dname,
                           unsigned long resource)
2313
{
2314
    int rv = -1;
2315 2316
    remote_domain_migrate_prepare_args args;
    remote_domain_migrate_prepare_ret ret;
2317
    struct private_data *priv = dconn->privateData;
2318

2319 2320
    remoteDriverLock(priv);

2321 2322 2323 2324 2325
    args.uri_in = uri_in == NULL ? NULL : (char **) &uri_in;
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;

2326 2327 2328 2329
    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)
2330
        goto done;
2331 2332 2333 2334 2335 2336 2337 2338

    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 已提交
2339
    VIR_FREE(ret.uri_out);
2340 2341 2342
    rv = 0;

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

D
Daniel Veillard 已提交
2347
static int
2348 2349 2350 2351 2352 2353
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 已提交
2354
{
2355
    int rv = -1;
D
Daniel Veillard 已提交
2356 2357
    remote_domain_migrate_prepare2_args args;
    remote_domain_migrate_prepare2_ret ret;
2358
    struct private_data *priv = dconn->privateData;
D
Daniel Veillard 已提交
2359

2360 2361
    remoteDriverLock(priv);

D
Daniel Veillard 已提交
2362 2363 2364 2365 2366 2367
    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;

2368 2369 2370 2371
    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)
2372
        goto done;
D
Daniel Veillard 已提交
2373 2374

    if (ret.cookie.cookie_len > 0) {
2375
        if (!cookie || !cookielen) {
2376 2377
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookie or cookielen"));
2378 2379
            goto error;
        }
D
Daniel Veillard 已提交
2380 2381 2382
        *cookie = ret.cookie.cookie_val; /* Caller frees. */
        *cookielen = ret.cookie.cookie_len;
    }
2383 2384
    if (ret.uri_out) {
        if (!uri_out) {
2385 2386
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores uri_out"));
2387 2388
            goto error;
        }
D
Daniel Veillard 已提交
2389
        *uri_out = *ret.uri_out; /* Caller frees. */
2390
    }
D
Daniel Veillard 已提交
2391

2392 2393 2394
    rv = 0;

done:
O
Osier Yang 已提交
2395
    VIR_FREE(ret.uri_out);
2396
    remoteDriverUnlock(priv);
2397
    return rv;
2398 2399 2400 2401 2402 2403
error:
    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 已提交
2404 2405
}

2406
static int
2407
remoteDomainCreate(virDomainPtr domain)
2408
{
2409
    int rv = -1;
2410
    remote_domain_create_args args;
2411 2412
    remote_domain_lookup_by_uuid_args args2;
    remote_domain_lookup_by_uuid_ret ret2;
2413
    struct private_data *priv = domain->conn->privateData;
2414

2415 2416
    remoteDriverLock(priv);

2417
    make_nonnull_domain(&args.dom, domain);
2418

2419 2420 2421
    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)
2422
        goto done;
2423

2424 2425 2426 2427
    /* 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.
     */
2428 2429 2430 2431 2432
    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)
2433 2434 2435
        goto done;

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

2438 2439 2440
    rv = 0;

done:
2441
    remoteDriverUnlock(priv);
2442
    return rv;
2443 2444
}

2445 2446 2447 2448 2449 2450
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;
2451
    remote_domain_create_with_flags_args ret;
2452 2453 2454 2455 2456 2457

    remoteDriverLock(priv);

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

2458
    memset(&ret, 0, sizeof(ret));
2459 2460
    if (call(dom->conn, priv, 0, REMOTE_PROC_DOMAIN_CREATE_WITH_FLAGS,
             (xdrproc_t)xdr_remote_domain_create_with_flags_args, (char *)&args,
2461
             (xdrproc_t)xdr_remote_domain_create_with_flags_ret, (char *)&ret) == -1) {
2462 2463 2464
        goto done;
    }

2465 2466
    dom->id = ret.dom.id;
    xdr_free((xdrproc_t) &xdr_remote_domain_create_with_flags_ret, (char *) &ret);
2467 2468 2469 2470 2471 2472 2473
    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

2474
static char *
2475
remoteDomainGetSchedulerType(virDomainPtr domain, int *nparams)
2476
{
2477 2478 2479
    char *rv = NULL;
    remote_domain_get_scheduler_type_args args;
    remote_domain_get_scheduler_type_ret ret;
2480
    struct private_data *priv = domain->conn->privateData;
2481

2482 2483
    remoteDriverLock(priv);

2484
    make_nonnull_domain(&args.dom, domain);
2485

2486 2487 2488 2489
    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)
2490
        goto done;
2491 2492 2493 2494

    if (nparams) *nparams = ret.nparams;

    /* Caller frees this. */
2495 2496 2497
    rv = ret.type;

done:
2498
    remoteDriverUnlock(priv);
2499
    return rv;
2500 2501
}

2502
static int
2503 2504 2505 2506
remoteDomainMemoryStats(virDomainPtr domain,
                        struct _virDomainMemoryStat *stats,
                        unsigned int nr_stats,
                        unsigned int flags)
2507 2508 2509 2510 2511
{
    int rv = -1;
    remote_domain_memory_stats_args args;
    remote_domain_memory_stats_ret ret;
    struct private_data *priv = domain->conn->privateData;
2512
    size_t i;
2513 2514 2515

    remoteDriverLock(priv);

2516
    make_nonnull_domain(&args.dom, domain);
2517
    if (nr_stats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
2518 2519 2520
        virReportError(VIR_ERR_RPC,
                       _("too many memory stats requested: %d > %d"), nr_stats,
                       REMOTE_DOMAIN_MEMORY_STATS_MAX);
2521 2522 2523
        goto done;
    }
    args.maxStats = nr_stats;
2524
    args.flags = flags;
2525
    memset(&ret, 0, sizeof(ret));
2526

2527 2528 2529 2530 2531
    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)
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545
        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);

done:
    remoteDriverUnlock(priv);
    return rv;
}

2546
static int
2547 2548 2549 2550 2551 2552
remoteDomainBlockPeek(virDomainPtr domain,
                      const char *path,
                      unsigned long long offset,
                      size_t size,
                      void *buffer,
                      unsigned int flags)
2553
{
2554
    int rv = -1;
2555 2556
    remote_domain_block_peek_args args;
    remote_domain_block_peek_ret ret;
2557
    struct private_data *priv = domain->conn->privateData;
2558

2559 2560
    remoteDriverLock(priv);

2561
    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
2562 2563 2564
        virReportError(VIR_ERR_RPC,
                       _("block peek request too large for remote protocol, %zi > %d"),
                       size, REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX);
2565
        goto done;
2566 2567
    }

2568
    make_nonnull_domain(&args.dom, domain);
2569 2570 2571 2572 2573
    args.path = (char *) path;
    args.offset = offset;
    args.size = size;
    args.flags = flags;

2574 2575 2576 2577 2578 2579
    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)
2580
        goto done;
2581 2582

    if (ret.buffer.buffer_len != size) {
2583 2584
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned buffer is not same size as requested"));
2585
        goto cleanup;
2586 2587
    }

2588
    memcpy(buffer, ret.buffer.buffer_val, size);
2589 2590 2591
    rv = 0;

cleanup:
2592
    VIR_FREE(ret.buffer.buffer_val);
2593

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

R
Richard W.M. Jones 已提交
2599
static int
2600 2601 2602 2603 2604
remoteDomainMemoryPeek(virDomainPtr domain,
                       unsigned long long offset,
                       size_t size,
                       void *buffer,
                       unsigned int flags)
R
Richard W.M. Jones 已提交
2605
{
2606
    int rv = -1;
R
Richard W.M. Jones 已提交
2607 2608
    remote_domain_memory_peek_args args;
    remote_domain_memory_peek_ret ret;
2609
    struct private_data *priv = domain->conn->privateData;
R
Richard W.M. Jones 已提交
2610

2611 2612
    remoteDriverLock(priv);

R
Richard W.M. Jones 已提交
2613
    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
2614 2615 2616
        virReportError(VIR_ERR_RPC,
                       _("memory peek request too large for remote protocol, %zi > %d"),
                       size, REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX);
2617
        goto done;
R
Richard W.M. Jones 已提交
2618 2619
    }

2620
    make_nonnull_domain(&args.dom, domain);
R
Richard W.M. Jones 已提交
2621 2622 2623 2624
    args.offset = offset;
    args.size = size;
    args.flags = flags;

2625 2626 2627 2628 2629 2630
    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)
2631
        goto done;
R
Richard W.M. Jones 已提交
2632 2633

    if (ret.buffer.buffer_len != size) {
2634 2635
        virReportError(VIR_ERR_RPC, "%s",
                       _("returned buffer is not same size as requested"));
2636
        goto cleanup;
R
Richard W.M. Jones 已提交
2637 2638
    }

2639
    memcpy(buffer, ret.buffer.buffer_val, size);
2640 2641 2642
    rv = 0;

cleanup:
2643
    VIR_FREE(ret.buffer.buffer_val);
R
Richard W.M. Jones 已提交
2644

2645
done:
2646
    remoteDriverUnlock(priv);
2647
    return rv;
R
Richard W.M. Jones 已提交
2648 2649
}

2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
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;
    }

done:
    remoteDriverUnlock(priv);
    return rv;
}

2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728
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,
2729
                                         &params,
2730 2731 2732 2733 2734 2735
                                         nparams) < 0)
        goto cleanup;

    rv = 0;

cleanup:
2736 2737
    xdr_free((xdrproc_t) xdr_remote_domain_get_block_io_tune_ret,
             (char *) &ret);
2738 2739 2740 2741 2742
done:
    remoteDriverUnlock(priv);
    return rv;
}

2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
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) {
2759 2760 2761
        virReportError(VIR_ERR_RPC,
                       _("nparams count exceeds maximum: %u > %u"),
                       nparams, REMOTE_NODE_CPU_STATS_MAX);
2762 2763 2764
        goto done;
    }
    if (ncpus > REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX) {
2765 2766 2767
        virReportError(VIR_ERR_RPC,
                       _("ncpus count exceeds maximum: %u > %u"),
                       ncpus, REMOTE_DOMAIN_GET_CPU_STATS_NCPUS_MAX);
2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788
        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 &&
2789
         ((ret.params.params_len % ret.nparams) || ret.nparams > nparams))) {
2790 2791 2792
        virReportError(VIR_ERR_RPC, "%s",
                       _("remoteDomainGetCPUStats: "
                         "returned number of stats exceeds limit"));
2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
        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
2806 2807
     * to expand things back into a possibly sparse array, where the
     * tail of the array may be omitted.
2808 2809
     */
    memset(params, 0, sizeof(*params) * nparams * ncpus);
2810
    ncpus = ret.params.params_len / ret.nparams;
2811 2812
    for (cpu = 0; cpu < ncpus; cpu++) {
        int tmp = nparams;
2813
        virTypedParameterPtr cpu_params = &params[cpu * nparams];
2814 2815 2816 2817
        remote_typed_param *stride = &ret.params.params_val[cpu * ret.nparams];

        if (remoteDeserializeTypedParameters(stride, ret.nparams,
                                             REMOTE_NODE_CPU_STATS_MAX,
2818
                                             &cpu_params, &tmp) < 0)
2819 2820 2821 2822 2823
            goto cleanup;
    }

    rv = ret.nparams;
cleanup:
A
Alex Jia 已提交
2824
    if (rv < 0)
2825
        virTypedParamsClear(params, nparams * ncpus);
2826

2827 2828
    xdr_free((xdrproc_t) xdr_remote_domain_get_cpu_stats_ret,
             (char *) &ret);
2829 2830 2831 2832 2833
done:
    remoteDriverUnlock(priv);
    return rv;
}

2834 2835 2836 2837 2838 2839
static int
remoteConnectListAllNetworks(virConnectPtr conn,
                             virNetworkPtr **nets,
                             unsigned int flags)
{
    int rv = -1;
2840
    size_t i;
2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862
    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;

2863 2864 2865 2866 2867 2868 2869
    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;
    }

2870
    if (nets) {
2871
        if (VIR_ALLOC_N(tmp_nets, ret.nets.nets_len + 1) < 0)
2872 2873 2874
            goto cleanup;

        for (i = 0; i < ret.nets.nets_len; i++) {
2875
            tmp_nets[i] = get_nonnull_network(conn, ret.nets.nets_val[i]);
2876
            if (!tmp_nets[i])
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
                goto cleanup;
        }
        *nets = tmp_nets;
        tmp_nets = NULL;
    }

    rv = ret.ret;

cleanup:
    if (tmp_nets) {
        for (i = 0; i < ret.nets.nets_len; i++)
            if (tmp_nets[i])
                virNetworkFree(tmp_nets[i]);
        VIR_FREE(tmp_nets);
    }

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

2900 2901 2902 2903 2904 2905
static int
remoteConnectListAllInterfaces(virConnectPtr conn,
                               virInterfacePtr **ifaces,
                               unsigned int flags)
{
    int rv = -1;
2906
    size_t i;
2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928
    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;

2929 2930 2931 2932 2933 2934 2935
    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;
    }

2936
    if (ifaces) {
2937
        if (VIR_ALLOC_N(tmp_ifaces, ret.ifaces.ifaces_len + 1) < 0)
2938 2939 2940
            goto cleanup;

        for (i = 0; i < ret.ifaces.ifaces_len; i++) {
2941
            tmp_ifaces[i] = get_nonnull_interface(conn, ret.ifaces.ifaces_val[i]);
2942
            if (!tmp_ifaces[i])
2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
                goto cleanup;
        }
        *ifaces = tmp_ifaces;
        tmp_ifaces = NULL;
    }

    rv = ret.ret;

cleanup:
    if (tmp_ifaces) {
        for (i = 0; i < ret.ifaces.ifaces_len; i++)
            if (tmp_ifaces[i])
                virInterfaceFree(tmp_ifaces[i]);
    }
    VIR_FREE(tmp_ifaces);

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

2966 2967 2968 2969 2970 2971
static int
remoteConnectListAllNodeDevices(virConnectPtr conn,
                                virNodeDevicePtr **devices,
                                unsigned int flags)
{
    int rv = -1;
2972
    size_t i;
2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
    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;

2995 2996 2997 2998 2999 3000 3001
    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;
    }

3002
    if (devices) {
3003
        if (VIR_ALLOC_N(tmp_devices, ret.devices.devices_len + 1) < 0)
3004 3005 3006 3007
            goto cleanup;

        for (i = 0; i < ret.devices.devices_len; i++) {
            tmp_devices[i] = get_nonnull_node_device(conn, ret.devices.devices_val[i]);
3008
            if (!tmp_devices[i])
3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031
                goto cleanup;
        }
        *devices = tmp_devices;
        tmp_devices = NULL;
    }

    rv = ret.ret;

cleanup:
    if (tmp_devices) {
        for (i = 0; i < ret.devices.devices_len; i++)
            if (tmp_devices[i])
                virNodeDeviceFree(tmp_devices[i]);
        VIR_FREE(tmp_devices);
    }

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

3032 3033 3034 3035 3036 3037
static int
remoteConnectListAllNWFilters(virConnectPtr conn,
                              virNWFilterPtr **filters,
                              unsigned int flags)
{
    int rv = -1;
3038
    size_t i;
3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060
    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;

3061 3062 3063 3064 3065 3066 3067
    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;
    }

3068
    if (filters) {
3069
        if (VIR_ALLOC_N(tmp_filters, ret.filters.filters_len + 1) < 0)
3070 3071 3072 3073
            goto cleanup;

        for (i = 0; i < ret.filters.filters_len; i++) {
            tmp_filters[i] = get_nonnull_nwfilter(conn, ret.filters.filters_val[i]);
3074
            if (!tmp_filters[i])
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096
                goto cleanup;
        }
        *filters = tmp_filters;
        tmp_filters = NULL;
    }

    rv = ret.ret;

cleanup:
    if (tmp_filters) {
        for (i = 0; i < ret.filters.filters_len; i++)
            if (tmp_filters[i])
                virNWFilterFree(tmp_filters[i]);
        VIR_FREE(tmp_filters);
    }

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

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

3098 3099 3100 3101 3102 3103
static int
remoteConnectListAllSecrets(virConnectPtr conn,
                            virSecretPtr **secrets,
                            unsigned int flags)
{
    int rv = -1;
3104
    size_t i;
3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126
    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;

3127 3128 3129 3130 3131 3132 3133
    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;
    }

3134
    if (secrets) {
3135
        if (VIR_ALLOC_N(tmp_secrets, ret.secrets.secrets_len + 1) < 0)
3136 3137 3138
            goto cleanup;

        for (i = 0; i < ret.secrets.secrets_len; i++) {
3139
            tmp_secrets[i] = get_nonnull_secret(conn, ret.secrets.secrets_val[i]);
3140
            if (!tmp_secrets[i])
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
                goto cleanup;
        }
        *secrets = tmp_secrets;
        tmp_secrets = NULL;
    }

    rv = ret.ret;

cleanup:
    if (tmp_secrets) {
        for (i = 0; i < ret.secrets.secrets_len; i++)
            if (tmp_secrets[i])
                virSecretFree(tmp_secrets[i]);
        VIR_FREE(tmp_secrets);
    }

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

3164 3165
/*----------------------------------------------------------------------*/

3166
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3167
remoteGenericOpen(virConnectPtr conn, virConnectAuthPtr auth,
3168
                  unsigned int flags, void **genericPrivateData)
3169
{
3170 3171 3172
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
3173
    if (conn->driver &&
3174
        STREQ(conn->driver->name, "remote")) {
3175 3176
        struct private_data *priv;

3177
        /* If we're here, the remote driver is already
3178
         * in use due to a) a QEMU uri, or b) a remote
3179
         * URI. So we can re-use existing connection */
3180 3181 3182
        priv = conn->privateData;
        remoteDriverLock(priv);
        priv->localUses++;
3183 3184 3185 3186
        *genericPrivateData = priv;
        remoteDriverUnlock(priv);
        return VIR_DRV_OPEN_SUCCESS;
    } else if (conn->networkDriver &&
3187
               STREQ(conn->networkDriver->name, "remote")) {
3188 3189 3190 3191
        struct private_data *priv = conn->networkPrivateData;
        remoteDriverLock(priv);
        *genericPrivateData = priv;
        priv->localUses++;
3192
        remoteDriverUnlock(priv);
3193
        return VIR_DRV_OPEN_SUCCESS;
3194 3195 3196
    } else {
        /* Using a non-remote driver, so we need to open a
         * new connection for network APIs, forcing it to
3197
         * use the UNIX transport. This handles Xen driver
3198
         * which doesn't have its own impl of the network APIs. */
3199
        struct private_data *priv;
3200 3201
        int ret = remoteOpenSecondaryDriver(conn, auth, flags, &priv);
        *genericPrivateData = priv;
3202 3203
        return ret;
    }
3204 3205 3206
}

static int
3207
remoteGenericClose(virConnectPtr conn, void **genericPrivateData)
3208
{
3209
    int rv = 0;
3210
    struct private_data *priv = *genericPrivateData;
3211

3212 3213 3214 3215
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        rv = doRemoteClose(conn, priv);
3216
        *genericPrivateData = NULL;
3217 3218 3219
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
3220
    }
3221 3222
    if (priv)
        remoteDriverUnlock(priv);
3223
    return rv;
3224 3225
}

3226
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3227 3228
remoteNetworkOpen(virConnectPtr conn, virConnectAuthPtr auth,
                  unsigned int flags)
3229 3230 3231 3232 3233 3234 3235 3236 3237 3238
{
    return remoteGenericOpen(conn, auth, flags, &conn->networkPrivateData);
}

static int
remoteNetworkClose(virConnectPtr conn)
{
    return remoteGenericClose(conn, &conn->networkPrivateData);
}

D
Daniel Veillard 已提交
3239 3240
/*----------------------------------------------------------------------*/

3241
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3242 3243
remoteInterfaceOpen(virConnectPtr conn, virConnectAuthPtr auth,
                    unsigned int flags)
D
Daniel Veillard 已提交
3244
{
3245
    return remoteGenericOpen(conn, auth, flags, &conn->interfacePrivateData);
D
Daniel Veillard 已提交
3246 3247 3248
}

static int
3249
remoteInterfaceClose(virConnectPtr conn)
D
Daniel Veillard 已提交
3250
{
3251
    return remoteGenericClose(conn, &conn->interfacePrivateData);
D
Daniel Veillard 已提交
3252 3253
}

3254 3255
/*----------------------------------------------------------------------*/

3256
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3257 3258
remoteStorageOpen(virConnectPtr conn, virConnectAuthPtr auth,
                  unsigned int flags)
3259
{
3260
    return remoteGenericOpen(conn, auth, flags, &conn->storagePrivateData);
3261 3262 3263
}

static int
3264
remoteStorageClose(virConnectPtr conn)
3265
{
3266
    return remoteGenericClose(conn, &conn->storagePrivateData);
3267 3268
}

3269
static char *
3270 3271 3272 3273
remoteConnectFindStoragePoolSources(virConnectPtr conn,
                                    const char *type,
                                    const char *srcSpec,
                                    unsigned int flags)
3274
{
3275
    char *rv = NULL;
3276 3277
    remote_connect_find_storage_pool_sources_args args;
    remote_connect_find_storage_pool_sources_ret ret;
3278
    struct private_data *priv = conn->storagePrivateData;
3279

3280 3281
    remoteDriverLock(priv);

3282
    args.type = (char*)type;
3283
    args.srcSpec = srcSpec ? (char **)&srcSpec : NULL;
3284 3285
    args.flags = flags;

3286
    memset(&ret, 0, sizeof(ret));
3287 3288 3289
    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)
3290
        goto done;
3291

3292 3293 3294
    rv = ret.xml;
    ret.xml = NULL; /* To stop xdr_free free'ing it */

3295
    xdr_free((xdrproc_t) xdr_remote_connect_find_storage_pool_sources_ret, (char *) &ret);
3296 3297

done:
3298
    remoteDriverUnlock(priv);
3299
    return rv;
3300 3301
}

3302
static int
3303 3304 3305
remoteConnectListAllStoragePools(virConnectPtr conn,
                                 virStoragePoolPtr **pools,
                                 unsigned int flags)
3306 3307
{
    int rv = -1;
3308
    size_t i;
3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330
    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;

3331 3332 3333 3334 3335 3336 3337
    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;
    }

3338
    if (pools) {
3339
        if (VIR_ALLOC_N(tmp_pools, ret.pools.pools_len + 1) < 0)
3340 3341 3342 3343
            goto cleanup;

        for (i = 0; i < ret.pools.pools_len; i++) {
            tmp_pools[i] = get_nonnull_storage_pool(conn, ret.pools.pools_val[i]);
3344
            if (!tmp_pools[i])
3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
                goto cleanup;
        }
        *pools = tmp_pools;
        tmp_pools = NULL;
    }

    rv = ret.ret;

cleanup:
    if (tmp_pools) {
        for (i = 0; i < ret.pools.pools_len; i++)
            if (tmp_pools[i])
                virStoragePoolFree(tmp_pools[i]);
        VIR_FREE(tmp_pools);
    }

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

3368 3369 3370 3371 3372 3373
static int
remoteStoragePoolListAllVolumes(virStoragePoolPtr pool,
                                virStorageVolPtr **vols,
                                unsigned int flags)
{
    int rv = -1;
3374
    size_t i;
3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397
    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;

3398 3399 3400 3401 3402 3403 3404
    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;
    }

3405
    if (vols) {
3406
        if (VIR_ALLOC_N(tmp_vols, ret.vols.vols_len + 1) < 0)
3407 3408 3409 3410
            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]);
3411
            if (!tmp_vols[i])
3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435
                goto cleanup;
        }
        *vols = tmp_vols;
        tmp_vols = NULL;
    }

    rv = ret.ret;

cleanup:
    if (tmp_vols) {
        for (i = 0; i < ret.vols.vols_len; i++)
            if (tmp_vols[i])
                virStorageVolFree(tmp_vols[i]);
        VIR_FREE(tmp_vols);
    }

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

done:
    remoteDriverUnlock(priv);
    return rv;
}


3436 3437
/*----------------------------------------------------------------------*/

3438
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3439 3440
remoteNodeDeviceOpen(virConnectPtr conn, virConnectAuthPtr auth,
                     unsigned int flags)
3441
{
3442
    return remoteGenericOpen(conn, auth, flags, &conn->nodeDevicePrivateData);
3443 3444
}

3445
static int
3446
remoteNodeDeviceClose(virConnectPtr conn)
3447
{
3448
    return remoteGenericClose(conn, &conn->nodeDevicePrivateData);
3449 3450
}

3451
static int
3452
remoteNodeDeviceDettach(virNodeDevicePtr dev)
3453 3454 3455
{
    int rv = -1;
    remote_node_device_dettach_args args;
3456
    /* This method is unusual in that it uses the HV driver, not the devMon driver
3457
     * hence its use of privateData, instead of nodeDevicePrivateData */
3458 3459 3460 3461 3462 3463
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3464 3465 3466
    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)
3467 3468 3469 3470 3471 3472 3473 3474 3475
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506
static int
remoteNodeDeviceDetachFlags(virNodeDevicePtr dev,
                            const char *driverName,
                            unsigned int flags)
{
    int rv = -1;
    remote_node_device_detach_flags_args args;
    /* This method is unusual in that it uses the HV driver, not the
     * devMon driver hence its use of privateData, instead of
     * nodeDevicePrivateData
     */
    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;

done:
    remoteDriverUnlock(priv);
    return rv;
}

3507
static int
3508
remoteNodeDeviceReAttach(virNodeDevicePtr dev)
3509 3510 3511
{
    int rv = -1;
    remote_node_device_re_attach_args args;
3512
    /* This method is unusual in that it uses the HV driver, not the devMon driver
3513
     * hence its use of privateData, instead of nodeDevicePrivateData */
3514 3515 3516 3517 3518 3519
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3520 3521 3522
    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)
3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
3533
remoteNodeDeviceReset(virNodeDevicePtr dev)
3534 3535 3536
{
    int rv = -1;
    remote_node_device_reset_args args;
3537
    /* This method is unusual in that it uses the HV driver, not the devMon driver
3538
     * hence its use of privateData, instead of nodeDevicePrivateData */
3539 3540 3541 3542 3543 3544
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

3545 3546 3547
    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)
3548 3549 3550 3551 3552 3553 3554 3555 3556
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

3557 3558
/* ------------------------------------------------------------- */

3559
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
3560 3561
remoteNWFilterOpen(virConnectPtr conn, virConnectAuthPtr auth,
                   unsigned int flags)
3562
{
3563
    return remoteGenericOpen(conn, auth, flags, &conn->nwfilterPrivateData);
3564 3565 3566
}

static int
3567
remoteNWFilterClose(virConnectPtr conn)
3568
{
3569
    return remoteGenericClose(conn, &conn->nwfilterPrivateData);
3570 3571
}

3572 3573
/*----------------------------------------------------------------------*/

3574
static int
3575 3576 3577
remoteAuthenticate(virConnectPtr conn, struct private_data *priv,
                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                   const char *authtype)
3578 3579
{
    struct remote_auth_list_ret ret;
3580
    int err, type = REMOTE_AUTH_NONE;
3581

3582
    memset(&ret, 0, sizeof(ret));
3583 3584 3585 3586
    err = call(conn, priv, 0,
               REMOTE_PROC_AUTH_LIST,
               (xdrproc_t) xdr_void, (char *) NULL,
               (xdrproc_t) xdr_remote_auth_list_ret, (char *) &ret);
3587 3588 3589 3590 3591 3592 3593
    if (err < 0) {
        virErrorPtr verr = virGetLastError();
        if (verr && verr->code == VIR_ERR_NO_SUPPORT) {
            /* Missing RPC - old server - ignore */
            virResetLastError();
            return 0;
        }
3594
        return -1;
3595
    }
3596 3597 3598 3599

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

3600
    if (authtype) {
3601 3602
        int want;
        size_t i;
3603 3604 3605 3606 3607 3608
        if (STRCASEEQ(authtype, "sasl") ||
            STRCASEEQLEN(authtype, "sasl.", 5)) {
            want = REMOTE_AUTH_SASL;
        } else if (STRCASEEQ(authtype, "polkit")) {
            want = REMOTE_AUTH_POLKIT;
        } else {
3609 3610
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("unknown authentication type %s"), authtype);
3611 3612
            return -1;
        }
3613
        for (i = 0; i < ret.types.types_len; i++) {
3614 3615 3616 3617
            if (ret.types.types_val[i] == want)
                type = want;
        }
        if (type == REMOTE_AUTH_NONE) {
3618 3619 3620
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("requested authentication type %s rejected"),
                           authtype);
3621 3622 3623 3624 3625 3626 3627
            return -1;
        }
    } else {
        type = ret.types.types_val[0];
    }

    switch (type) {
3628
#if WITH_SASL
3629 3630 3631 3632 3633 3634
    case REMOTE_AUTH_SASL: {
        const char *mech = NULL;
        if (authtype &&
            STRCASEEQLEN(authtype, "sasl.", 5))
            mech = authtype + 5;

3635
        if (remoteAuthSASL(conn, priv, auth, mech) < 0) {
3636
            VIR_FREE(ret.types.types_val);
3637 3638 3639
            return -1;
        }
        break;
3640
    }
3641 3642
#endif

3643
#if WITH_POLKIT
3644
    case REMOTE_AUTH_POLKIT:
3645
        if (remoteAuthPolkit(conn, priv, auth) < 0) {
3646
            VIR_FREE(ret.types.types_val);
3647 3648 3649 3650 3651
            return -1;
        }
        break;
#endif

3652 3653 3654 3655 3656
    case REMOTE_AUTH_NONE:
        /* Nothing todo, hurrah ! */
        break;

    default:
3657 3658 3659
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("unsupported authentication type %d"),
                       ret.types.types_val[0]);
3660
        VIR_FREE(ret.types.types_val);
3661 3662 3663
        return -1;
    }

3664
    VIR_FREE(ret.types.types_val);
3665 3666 3667 3668 3669 3670

    return 0;
}



3671
#if WITH_SASL
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749
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.
 * We explicitly leav the callback function pointer at NULL though,
 * 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)
{
3750
    sasl_callback_t *cbs;
3751 3752
    size_t i;
    int n;
3753
    if (VIR_ALLOC_N(cbs, ncredtype+1) < 0) {
3754 3755 3756
        return NULL;
    }

3757
    for (i = 0, n = 0; i < ncredtype; i++) {
3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772
        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
3773
 *
3774 3775 3776 3777 3778
 * 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,
3779 3780
                                     virConnectCredentialPtr *cred,
                                     size_t *ncred)
3781 3782 3783 3784 3785
{
    int ninteract;
    if (!cred)
        return -1;

3786
    for (ninteract = 0, *ncred = 0; interact[ninteract].id != 0; ninteract++) {
3787 3788 3789 3790
        if (interact[ninteract].result)
            continue;
        (*ncred)++;
    }
3791

3792
    if (VIR_ALLOC_N(*cred, *ncred) < 0)
3793 3794
        return -1;

3795
    for (ninteract = 0, *ncred = 0; interact[ninteract].id != 0; ninteract++) {
3796 3797 3798 3799 3800 3801
        if (interact[ninteract].result)
            continue;

        (*cred)[*ncred].type = remoteAuthCredSASL2Vir(interact[ninteract].id);
        if (!(*cred)[*ncred].type) {
            *ncred = 0;
3802
            VIR_FREE(*cred);
3803 3804
            return -1;
        }
3805 3806 3807 3808 3809 3810 3811 3812
        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)++;
3813 3814
    }

3815
    return 0;
3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828
}


/*
 * @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)
{
3829
    int ninteract, ncred;
3830
    for (ninteract = 0, ncred = 0; interact[ninteract].id != 0; ninteract++) {
3831 3832 3833 3834 3835
        if (interact[ninteract].result)
            continue;
        interact[ninteract].result = cred[ncred].result;
        interact[ninteract].len = cred[ncred].resultlen;
        ncred++;
3836 3837 3838
    }
}

3839 3840 3841 3842
struct remoteAuthInteractState {
    sasl_interact_t *interact;
    virConnectCredentialPtr cred;
    size_t ncred;
3843
    virAuthConfigPtr config;
3844 3845 3846
};


3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869

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

3870
    for (ninteract = 0; state->interact[ninteract].id != 0; ninteract++) {
3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890
        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;
        }

3891 3892
        if (credname &&
            virAuthConfigLookup(state->config,
3893
                                "libvirt",
3894
                                VIR_URI_SERVER(conn->uri),
3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914
                                credname,
                                &value) < 0)
            goto cleanup;

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

    ret = 0;

cleanup:
    VIR_FREE(path);
    return ret;
}


static void remoteAuthInteractStateClear(struct remoteAuthInteractState *state,
                                         bool final)
3915 3916 3917 3918 3919
{
    size_t i;
    if (!state)
        return;

3920
    for (i = 0; i < state->ncred; i++)
3921 3922 3923
        VIR_FREE(state->cred[i].result);
    VIR_FREE(state->cred);
    state->ncred = 0;
3924 3925 3926

    if (final)
        virAuthConfigFree(state->config);
3927 3928 3929
}


3930 3931
static int remoteAuthInteract(virConnectPtr conn,
                              struct remoteAuthInteractState *state,
3932 3933 3934 3935
                              virConnectAuthPtr auth)
{
    int ret = -1;

3936 3937 3938
    VIR_DEBUG("Starting SASL interaction");
    remoteAuthInteractStateClear(state, false);

3939
    /* Fills state->interact with any values from the auth config file */
3940 3941
    if (remoteAuthFillFromConfig(conn, state) < 0)
        goto cleanup;
3942

3943
    /* Populates state->cred for anything not found in the auth config */
3944
    if (remoteAuthMakeCredentials(state->interact, &state->cred, &state->ncred) < 0) {
3945 3946
        virReportError(VIR_ERR_AUTH_FAILED, "%s",
                       _("Failed to make auth credentials"));
3947 3948 3949
        goto cleanup;
    }

3950 3951 3952 3953 3954 3955 3956 3957 3958 3959
    /* 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;
        }
3960

3961 3962 3963 3964 3965 3966 3967 3968
        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);
3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985
    }

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

cleanup:
    return ret;
}


3986
/* Perform the SASL authentication process
3987 3988
 */
static int
3989 3990
remoteAuthSASL(virConnectPtr conn, struct private_data *priv,
               virConnectAuthPtr auth, const char *wantmech)
3991 3992 3993 3994 3995 3996 3997
{
    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;
3998
    char *serverin = NULL;
3999
    size_t clientoutlen, serverinlen;
4000 4001
    const char *mech;
    int err, complete;
4002
    int ssf;
4003 4004 4005
    sasl_callback_t *saslcb = NULL;
    int ret = -1;
    const char *mechlist;
4006
    virNetSASLContextPtr saslCtxt;
E
Eric Blake 已提交
4007
    virNetSASLSessionPtr sasl = NULL;
4008 4009 4010
    struct remoteAuthInteractState state;

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

4012
    VIR_DEBUG("Client initialize SASL authentication");
4013

4014
    if (!(saslCtxt = virNetSASLContextNewClient()))
4015 4016
        goto cleanup;

4017 4018 4019 4020 4021 4022
    if (auth) {
        if ((saslcb = remoteAuthMakeCallbacks(auth->credtype, auth->ncredtype)) == NULL)
            goto cleanup;
    } else {
        saslcb = NULL;
    }
4023 4024

    /* Setup a handle for being a client */
4025 4026 4027 4028 4029 4030
    if (!(sasl = virNetSASLSessionNewClient(saslCtxt,
                                            "libvirt",
                                            priv->hostname,
                                            virNetClientLocalAddrString(priv->client),
                                            virNetClientRemoteAddrString(priv->client),
                                            saslcb)))
4031
        goto cleanup;
4032 4033
    /* saslcb is now owned by sasl */
    saslcb = NULL;
4034

4035
# ifdef WITH_GNUTLS
4036
    /* Initialize some connection props we care about */
4037 4038
    if (priv->tls) {
        if ((ssf = virNetClientGetTLSKeySize(priv->client)) < 0)
4039
            goto cleanup;
4040

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

4043
        VIR_DEBUG("Setting external SSF %d", ssf);
4044
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
4045
            goto cleanup;
4046
    }
4047
# endif
4048

4049 4050
    /* 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 */
4051 4052 4053 4054
    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)
4055
        goto cleanup;
4056

4057
    /* First call is to inquire about supported mechanisms in the server */
4058 4059 4060 4061
    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)
4062
        goto cleanup;
4063 4064


4065 4066 4067
    mechlist = iret.mechlist;
    if (wantmech) {
        if (strstr(mechlist, wantmech) == NULL) {
4068 4069 4070
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("SASL mechanism %s not supported by server"),
                           wantmech);
4071
            VIR_FREE(iret.mechlist);
4072 4073 4074 4075 4076
            goto cleanup;
        }
        mechlist = wantmech;
    }
 restart:
4077
    /* Start the auth negotiation on the client end first */
4078
    VIR_DEBUG("Client start negotiation mechlist '%s'", mechlist);
4079 4080
    if ((err = virNetSASLSessionClientStart(sasl,
                                            mechlist,
4081
                                            &state.interact,
4082 4083 4084
                                            &clientout,
                                            &clientoutlen,
                                            &mech)) < 0)
4085 4086 4087
        goto cleanup;

    /* Need to gather some credentials from the client */
4088
    if (err == VIR_NET_SASL_INTERACT) {
4089
        if (remoteAuthInteract(conn, &state, auth) < 0) {
4090
            VIR_FREE(iret.mechlist);
4091 4092
            goto cleanup;
        }
4093
        goto restart;
4094
    }
4095
    VIR_FREE(iret.mechlist);
4096 4097

    if (clientoutlen > REMOTE_AUTH_SASL_DATA_MAX) {
4098 4099 4100
        virReportError(VIR_ERR_AUTH_FAILED,
                       _("SASL negotiation data too long: %zu bytes"),
                       clientoutlen);
4101
        goto cleanup;
4102 4103
    }
    /* NB, distinction of NULL vs "" is *critical* in SASL */
4104
    memset(&sargs, 0, sizeof(sargs));
4105 4106 4107 4108
    sargs.nil = clientout ? 0 : 1;
    sargs.data.data_val = (char*)clientout;
    sargs.data.data_len = clientoutlen;
    sargs.mech = (char*)mech;
4109 4110
    VIR_DEBUG("Server start negotiation with mech %s. Data %zu bytes %p",
              mech, clientoutlen, clientout);
4111 4112

    /* Now send the initial auth data to the server */
4113 4114 4115 4116
    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)
4117
        goto cleanup;
4118 4119 4120 4121 4122

    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;
4123 4124
    VIR_DEBUG("Client step result complete: %d. Data %zu bytes %p",
              complete, serverinlen, serverin);
4125 4126 4127

    /* Loop-the-loop...
     * Even if the server has completed, the client must *always* do at least one step
D
Daniel Veillard 已提交
4128
     * in this loop to verify the server isn't lying about something. Mutual auth */
4129
    for (;;) {
4130
    restep:
4131 4132 4133
        if ((err = virNetSASLSessionClientStep(sasl,
                                               serverin,
                                               serverinlen,
4134
                                               &state.interact,
4135 4136
                                               &clientout,
                                               &clientoutlen)) < 0)
4137
            goto cleanup;
4138

4139
        /* Need to gather some credentials from the client */
4140
        if (err == VIR_NET_SASL_INTERACT) {
4141
            if (remoteAuthInteract(conn, &state, auth) < 0) {
4142
                VIR_FREE(iret.mechlist);
4143 4144
                goto cleanup;
            }
4145
            goto restep;
4146 4147
        }

4148
        VIR_FREE(serverin);
4149 4150
        VIR_DEBUG("Client step result %d. Data %zu bytes %p",
                  err, clientoutlen, clientout);
4151 4152

        /* Previous server call showed completion & we're now locally complete too */
4153
        if (complete && err == VIR_NET_SASL_COMPLETE)
4154 4155 4156 4157
            break;

        /* Not done, prepare to talk with the server for another iteration */
        /* NB, distinction of NULL vs "" is *critical* in SASL */
4158
        memset(&pargs, 0, sizeof(pargs));
4159 4160 4161
        pargs.nil = clientout ? 0 : 1;
        pargs.data.data_val = (char*)clientout;
        pargs.data.data_len = clientoutlen;
4162 4163
        VIR_DEBUG("Server step with %zu bytes %p",
                  clientoutlen, clientout);
4164

4165 4166 4167 4168
        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)
4169
            goto cleanup;
4170 4171 4172 4173 4174 4175

        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;

4176 4177
        VIR_DEBUG("Client step result complete: %d. Data %zu bytes %p",
                  complete, serverinlen, serverin);
4178 4179

        /* This server call shows complete, and earlier client step was OK */
4180
        if (complete && err == VIR_NET_SASL_COMPLETE) {
4181
            VIR_FREE(serverin);
4182 4183 4184 4185
            break;
        }
    }

4186 4187
    /* Check for suitable SSF if not already secure (TLS or UNIX sock) */
    if (!priv->is_secure) {
4188
        if ((ssf = virNetSASLSessionGetKeySize(sasl)) < 0)
4189
            goto cleanup;
4190

4191
        VIR_DEBUG("SASL SSF value %d", ssf);
4192
        if (ssf < 56) { /* 56 == DES level, good for Kerberos */
4193 4194
            virReportError(VIR_ERR_AUTH_FAILED,
                           _("negotiation SSF %d was not strong enough"), ssf);
4195
            goto cleanup;
4196
        }
4197
        priv->is_secure = 1;
4198 4199
    }

4200
    VIR_DEBUG("SASL authentication complete");
4201
    virNetClientSetSASLSession(priv->client, sasl);
4202 4203 4204
    ret = 0;

 cleanup:
4205
    VIR_FREE(serverin);
4206

4207
    remoteAuthInteractStateClear(&state, true);
4208
    VIR_FREE(saslcb);
4209 4210
    virObjectUnref(sasl);
    virObjectUnref(saslCtxt);
4211

4212
    return ret;
4213
}
4214
#endif /* WITH_SASL */
4215

4216

4217 4218
#if WITH_POLKIT
# if WITH_POLKIT1
4219
static int
4220 4221
remoteAuthPolkit(virConnectPtr conn, struct private_data *priv,
                 virConnectAuthPtr auth ATTRIBUTE_UNUSED)
4222 4223
{
    remote_auth_polkit_ret ret;
4224
    VIR_DEBUG("Client initialize PolicyKit-1 authentication");
4225

4226 4227 4228 4229
    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) {
4230 4231 4232
        return -1; /* virError already set by call */
    }

4233
    VIR_DEBUG("PolicyKit-1 authentication complete");
4234 4235
    return 0;
}
4236
# elif WITH_POLKIT0
4237 4238 4239
/* Perform the PolicyKit authentication process
 */
static int
4240 4241
remoteAuthPolkit(virConnectPtr conn, struct private_data *priv,
                 virConnectAuthPtr auth)
4242 4243
{
    remote_auth_polkit_ret ret;
4244 4245
    size_t i;
    int allowcb = 0;
4246 4247 4248 4249 4250 4251 4252 4253
    virConnectCredential cred = {
        VIR_CRED_EXTERNAL,
        conn->flags & VIR_CONNECT_RO ? "org.libvirt.unix.monitor" : "org.libvirt.unix.manage",
        "PolicyKit",
        NULL,
        NULL,
        0,
    };
4254
    VIR_DEBUG("Client initialize PolicyKit-0 authentication");
4255

4256
    /* Check auth first and if it succeeds we are done. */
4257 4258 4259 4260
    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)
4261 4262 4263
        goto out;

    /* Auth failed.  Ask client to obtain it and check again. */
4264
    if (auth && auth->cb) {
4265
        /* Check if the necessary credential type for PolicyKit is supported */
4266
        for (i = 0; i < auth->ncredtype; i++) {
4267 4268 4269
            if (auth->credtype[i] == VIR_CRED_EXTERNAL)
                allowcb = 1;
        }
4270

4271
        if (allowcb) {
4272
            VIR_DEBUG("Client run callback for PolicyKit authentication");
4273 4274
            /* Run the authentication callback */
            if ((*(auth->cb))(&cred, 1, auth->cbdata) < 0) {
4275 4276
                virReportError(VIR_ERR_AUTH_FAILED, "%s",
                               _("Failed to collect auth credentials"));
4277 4278
                return -1;
            }
4279
        } else {
4280
            VIR_DEBUG("Client auth callback does not support PolicyKit");
4281
            return -1;
4282 4283
        }
    } else {
4284
        VIR_DEBUG("No auth callback provided");
4285
        return -1;
4286 4287
    }

4288 4289 4290 4291
    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) {
4292 4293 4294
        return -1; /* virError already set by call */
    }

4295
out:
4296
    VIR_DEBUG("PolicyKit-0 authentication complete");
4297 4298
    return 0;
}
4299 4300
# endif /* WITH_POLKIT0 */
#endif /* WITH_POLKIT */
4301 4302
/*----------------------------------------------------------------------*/

4303 4304 4305 4306
static int remoteConnectDomainEventRegister(virConnectPtr conn,
                                            virConnectDomainEventCallback callback,
                                            void *opaque,
                                            virFreeCallback freecb)
4307
{
4308
    int rv = -1;
4309
    struct private_data *priv = conn->privateData;
4310
    int count;
4311

4312 4313
    remoteDriverLock(priv);

4314 4315
    if ((count = virDomainEventStateRegister(conn, priv->domainEventState,
                                             callback, opaque, freecb)) < 0) {
4316
         virReportError(VIR_ERR_RPC, "%s", _("adding cb to list"));
4317
         goto done;
4318 4319
    }

4320
    if (count == 1) {
4321
        /* Tell the server when we are the first callback deregistering */
4322
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_DOMAIN_EVENT_REGISTER,
4323 4324
                 (xdrproc_t) xdr_void, (char *) NULL,
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
4325
            goto done;
4326 4327
    }

4328 4329 4330
    rv = 0;

done:
4331
    remoteDriverUnlock(priv);
4332
    return rv;
4333 4334
}

4335 4336
static int remoteConnectDomainEventDeregister(virConnectPtr conn,
                                              virConnectDomainEventCallback callback)
4337 4338
{
    struct private_data *priv = conn->privateData;
4339
    int rv = -1;
4340
    int count;
4341

4342 4343
    remoteDriverLock(priv);

4344 4345 4346
    if ((count = virDomainEventStateDeregister(conn,
                                               priv->domainEventState,
                                               callback)) < 0)
4347
        goto done;
4348

4349
    if (count == 0) {
4350
        /* Tell the server when we are the last callback deregistering */
4351
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_DOMAIN_EVENT_DEREGISTER,
4352 4353
                 (xdrproc_t) xdr_void, (char *) NULL,
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
4354
            goto done;
4355 4356
    }

4357 4358 4359
    rv = 0;

done:
4360
    remoteDriverUnlock(priv);
4361
    return rv;
4362
}
4363

4364 4365 4366 4367 4368

static void
remoteDomainBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
4369
{
4370 4371 4372
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_lifecycle_msg *msg = evdata;
4373 4374 4375
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

4376
    dom = get_nonnull_domain(conn, msg->dom);
4377
    if (!dom)
4378
        return;
4379

4380
    event = virDomainEventNewFromDom(dom, msg->event, msg->detail);
4381
    virDomainFree(dom);
4382 4383

    remoteDomainEventQueue(priv, event);
4384 4385 4386
}


4387 4388 4389 4390
static void
remoteDomainBuildEventReboot(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                             virNetClientPtr client ATTRIBUTE_UNUSED,
                             void *evdata, void *opaque)
4391
{
4392 4393 4394
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_reboot_msg *msg = evdata;
4395 4396 4397
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

4398
    dom = get_nonnull_domain(conn, msg->dom);
4399
    if (!dom)
4400
        return;
4401 4402 4403

    event = virDomainEventRebootNewFromDom(dom);
    virDomainFree(dom);
4404 4405

    remoteDomainEventQueue(priv, event);
4406 4407 4408
}


4409 4410 4411 4412
static void
remoteDomainBuildEventRTCChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
4413
{
4414 4415 4416
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_rtc_change_msg *msg = evdata;
4417 4418 4419
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

4420
    dom = get_nonnull_domain(conn, msg->dom);
4421
    if (!dom)
4422
        return;
4423

4424
    event = virDomainEventRTCChangeNewFromDom(dom, msg->offset);
4425
    virDomainFree(dom);
4426 4427

    remoteDomainEventQueue(priv, event);
4428 4429 4430
}


4431 4432 4433 4434
static void
remoteDomainBuildEventWatchdog(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
4435
{
4436 4437 4438
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_watchdog_msg *msg = evdata;
4439 4440 4441
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

4442
    dom = get_nonnull_domain(conn, msg->dom);
4443
    if (!dom)
4444
        return;
4445

4446
    event = virDomainEventWatchdogNewFromDom(dom, msg->action);
4447
    virDomainFree(dom);
4448 4449

    remoteDomainEventQueue(priv, event);
4450 4451 4452
}


4453 4454 4455 4456
static void
remoteDomainBuildEventIOError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                              virNetClientPtr client ATTRIBUTE_UNUSED,
                              void *evdata, void *opaque)
4457
{
4458 4459 4460
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_io_error_msg *msg = evdata;
4461 4462 4463
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

4464
    dom = get_nonnull_domain(conn, msg->dom);
4465
    if (!dom)
4466
        return;
4467 4468

    event = virDomainEventIOErrorNewFromDom(dom,
4469 4470 4471
                                            msg->srcPath,
                                            msg->devAlias,
                                            msg->action);
4472
    virDomainFree(dom);
4473 4474

    remoteDomainEventQueue(priv, event);
4475 4476 4477
}


4478 4479 4480 4481
static void
remoteDomainBuildEventIOErrorReason(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
4482
{
4483 4484 4485
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_io_error_reason_msg *msg = evdata;
4486 4487 4488
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

4489
    dom = get_nonnull_domain(conn,msg->dom);
4490
    if (!dom)
4491
        return;
4492 4493

    event = virDomainEventIOErrorReasonNewFromDom(dom,
4494 4495 4496 4497
                                                  msg->srcPath,
                                                  msg->devAlias,
                                                  msg->action,
                                                  msg->reason);
4498 4499

    virDomainFree(dom);
4500 4501

    remoteDomainEventQueue(priv, event);
4502 4503
}

4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525
static void
remoteDomainBuildEventBlockJob(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_block_job_msg *msg = evdata;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

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

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

    virDomainFree(dom);

    remoteDomainEventQueue(priv, event);
}
4526

4527 4528 4529 4530
static void
remoteDomainBuildEventGraphics(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
4531
{
4532 4533 4534
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_graphics_msg *msg = evdata;
4535 4536 4537 4538 4539
    virDomainPtr dom;
    virDomainEventPtr event = NULL;
    virDomainEventGraphicsAddressPtr localAddr = NULL;
    virDomainEventGraphicsAddressPtr remoteAddr = NULL;
    virDomainEventGraphicsSubjectPtr subject = NULL;
4540
    size_t i;
4541

4542
    dom = get_nonnull_domain(conn, msg->dom);
4543
    if (!dom)
4544
        return;
4545 4546

    if (VIR_ALLOC(localAddr) < 0)
4547
        goto error;
4548
    localAddr->family = msg->local.family;
4549 4550 4551
    if (VIR_STRDUP(localAddr->service, msg->local.service) < 0 ||
        VIR_STRDUP(localAddr->node, msg->local.node) < 0)
        goto error;
4552 4553

    if (VIR_ALLOC(remoteAddr) < 0)
4554
        goto error;
4555
    remoteAddr->family = msg->remote.family;
4556 4557 4558
    if (VIR_STRDUP(remoteAddr->service, msg->remote.service) < 0 ||
        VIR_STRDUP(remoteAddr->node, msg->remote.node) < 0)
        goto error;
4559 4560

    if (VIR_ALLOC(subject) < 0)
4561
        goto error;
4562
    if (VIR_ALLOC_N(subject->identities, msg->subject.subject_len) < 0)
4563
        goto error;
4564
    subject->nidentity = msg->subject.subject_len;
4565
    for (i = 0; i < subject->nidentity; i++) {
4566 4567 4568
        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;
4569 4570 4571
    }

    event = virDomainEventGraphicsNewFromDom(dom,
4572
                                             msg->phase,
4573 4574
                                             localAddr,
                                             remoteAddr,
4575
                                             msg->authScheme,
4576 4577 4578 4579
                                             subject);

    virDomainFree(dom);

4580 4581
    remoteDomainEventQueue(priv, event);
    return;
4582

4583
error:
4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594
    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) {
4595
        for (i = 0; i < subject->nidentity; i++) {
4596 4597 4598 4599 4600 4601
            VIR_FREE(subject->identities[i].type);
            VIR_FREE(subject->identities[i].name);
        }
        VIR_FREE(subject->identities);
        VIR_FREE(subject);
    }
A
ajia@redhat.com 已提交
4602
    virDomainFree(dom);
4603
    return;
4604 4605 4606
}


4607 4608 4609 4610
static void
remoteDomainBuildEventControlError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                   virNetClientPtr client ATTRIBUTE_UNUSED,
                                   void *evdata, void *opaque)
4611
{
4612 4613 4614
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_control_error_msg *msg = evdata;
4615 4616 4617
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

4618
    dom = get_nonnull_domain(conn, msg->dom);
4619
    if (!dom)
4620
        return;
4621 4622 4623 4624

    event = virDomainEventControlErrorNewFromDom(dom);

    virDomainFree(dom);
4625 4626

    remoteDomainEventQueue(priv, event);
4627 4628 4629
}


4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656
static void
remoteDomainBuildEventDiskChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_disk_change_msg *msg = evdata;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

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

    virDomainFree(dom);

    remoteDomainEventQueue(priv, event);
}


4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680
static void
remoteDomainBuildEventTrayChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                 virNetClientPtr client ATTRIBUTE_UNUSED,
                                 void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_tray_change_msg *msg = evdata;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

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

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

    virDomainFree(dom);

    remoteDomainEventQueue(priv, event);
}

O
Osier Yang 已提交
4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701
static void
remoteDomainBuildEventPMWakeup(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_pmwakeup_msg *msg = evdata;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

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

    event = virDomainEventPMWakeupNewFromDom(dom);

    virDomainFree(dom);

    remoteDomainEventQueue(priv, event);
}
4702

O
Osier Yang 已提交
4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724
static void
remoteDomainBuildEventPMSuspend(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_pmsuspend_msg *msg = evdata;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

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

    event = virDomainEventPMSuspendNewFromDom(dom);

    virDomainFree(dom);

    remoteDomainEventQueue(priv, event);
}

4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747

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

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

    event = virDomainEventBalloonChangeNewFromDom(dom, msg->actual);
    virDomainFree(dom);

    remoteDomainEventQueue(priv, event);
}


4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770
static void
remoteDomainBuildEventPMSuspendDisk(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_pmsuspend_disk_msg *msg = evdata;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

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

    event = virDomainEventPMSuspendDiskNewFromDom(dom);

    virDomainFree(dom);

    remoteDomainEventQueue(priv, event);
}


4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793
static void
remoteDomainBuildEventDeviceRemoved(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
{
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_device_removed_msg *msg = evdata;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

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

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

    virDomainFree(dom);

    remoteDomainEventQueue(priv, event);
}


4794
static virDrvOpenStatus ATTRIBUTE_NONNULL(1)
4795 4796
remoteSecretOpen(virConnectPtr conn, virConnectAuthPtr auth,
                 unsigned int flags)
4797
{
4798
    return remoteGenericOpen(conn, auth, flags, &conn->secretPrivateData);
4799 4800 4801
}

static int
4802
remoteSecretClose(virConnectPtr conn)
4803
{
4804
    return remoteGenericClose(conn, &conn->secretPrivateData);
4805 4806 4807
}

static unsigned char *
4808 4809
remoteSecretGetValue(virSecretPtr secret, size_t *value_size,
                     unsigned int flags, unsigned int internalFlags)
4810 4811 4812 4813 4814 4815
{
    unsigned char *rv = NULL;
    remote_secret_get_value_args args;
    remote_secret_get_value_ret ret;
    struct private_data *priv = secret->conn->secretPrivateData;

4816
    remoteDriverLock(priv);
4817

4818 4819
    /* internalFlags intentionally do not go over the wire */
    if (internalFlags) {
4820
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("no internalFlags support"));
4821 4822 4823
        goto done;
    }

4824
    make_nonnull_secret(&args.secret, secret);
4825 4826
    args.flags = flags;

4827 4828 4829 4830
    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)
4831 4832 4833 4834 4835 4836
        goto done;

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

done:
4837
    remoteDriverUnlock(priv);
4838 4839 4840
    return rv;
}

4841 4842 4843 4844 4845 4846

static int
remoteStreamSend(virStreamPtr st,
                 const char *data,
                 size_t nbytes)
{
4847
    VIR_DEBUG("st=%p data=%p nbytes=%zu", st, data, nbytes);
4848
    struct private_data *priv = st->conn->privateData;
4849
    virNetClientStreamPtr privst = st->privateData;
4850
    int rv;
4851

4852
    if (virNetClientStreamRaiseError(privst))
4853 4854 4855 4856 4857
        return -1;

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

4859 4860 4861 4862 4863
    rv = virNetClientStreamSendPacket(privst,
                                      priv->client,
                                      VIR_NET_CONTINUE,
                                      data,
                                      nbytes);
4864

4865 4866
    remoteDriverLock(priv);
    priv->localUses--;
4867 4868 4869 4870 4871 4872 4873 4874 4875 4876
    remoteDriverUnlock(priv);
    return rv;
}


static int
remoteStreamRecv(virStreamPtr st,
                 char *data,
                 size_t nbytes)
{
4877
    VIR_DEBUG("st=%p data=%p nbytes=%zu", st, data, nbytes);
4878
    struct private_data *priv = st->conn->privateData;
4879
    virNetClientStreamPtr privst = st->privateData;
4880
    int rv;
4881

4882
    if (virNetClientStreamRaiseError(privst))
4883 4884 4885 4886 4887
        return -1;

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

4889 4890 4891 4892 4893
    rv = virNetClientStreamRecvPacket(privst,
                                      priv->client,
                                      data,
                                      nbytes,
                                      (st->flags & VIR_STREAM_NONBLOCK));
4894

4895
    VIR_DEBUG("Done %d", rv);
4896

4897 4898
    remoteDriverLock(priv);
    priv->localUses--;
4899 4900 4901 4902
    remoteDriverUnlock(priv);
    return rv;
}

4903 4904 4905 4906 4907 4908
struct remoteStreamCallbackData {
    virStreamPtr st;
    virStreamEventCallback cb;
    void *opaque;
    virFreeCallback ff;
};
4909

4910 4911 4912
static void remoteStreamEventCallback(virNetClientStreamPtr stream ATTRIBUTE_UNUSED,
                                      int events,
                                      void *opaque)
4913
{
4914
    struct remoteStreamCallbackData *cbdata = opaque;
4915

4916
    (cbdata->cb)(cbdata->st, events, cbdata->opaque);
4917 4918 4919
}


4920
static void remoteStreamCallbackFree(void *opaque)
4921
{
4922 4923 4924 4925 4926 4927 4928
    struct remoteStreamCallbackData *cbdata = opaque;

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

    virStreamFree(cbdata->st);
    VIR_FREE(opaque);
4929 4930 4931
}


4932
static int
4933 4934 4935 4936 4937
remoteStreamEventAddCallback(virStreamPtr st,
                             int events,
                             virStreamEventCallback cb,
                             void *opaque,
                             virFreeCallback ff)
4938
{
4939
    struct private_data *priv = st->conn->privateData;
4940
    virNetClientStreamPtr privst = st->privateData;
4941
    int ret = -1;
4942
    struct remoteStreamCallbackData *cbdata;
4943

4944
    if (VIR_ALLOC(cbdata) < 0)
4945 4946 4947 4948 4949
        return -1;
    cbdata->cb = cb;
    cbdata->opaque = opaque;
    cbdata->ff = ff;
    cbdata->st = st;
4950 4951
    virStreamRef(st);

4952
    remoteDriverLock(priv);
4953

4954 4955 4956 4957 4958 4959 4960 4961
    if ((ret = virNetClientStreamEventAddCallback(privst,
                                                  events,
                                                  remoteStreamEventCallback,
                                                  cbdata,
                                                  remoteStreamCallbackFree)) < 0) {
        VIR_FREE(cbdata);
        goto cleanup;
    }
4962 4963 4964

cleanup:
    remoteDriverUnlock(priv);
4965
    /* coverity[leaked_storage] - cbdata is not leaked */
4966
    return ret;
4967 4968
}

4969

4970
static int
4971 4972
remoteStreamEventUpdateCallback(virStreamPtr st,
                                int events)
4973
{
4974
    struct private_data *priv = st->conn->privateData;
4975
    virNetClientStreamPtr privst = st->privateData;
4976 4977 4978 4979
    int ret = -1;

    remoteDriverLock(priv);

4980
    ret = virNetClientStreamEventUpdateCallback(privst, events);
4981

4982 4983 4984
    remoteDriverUnlock(priv);
    return ret;
}
4985 4986 4987


static int
4988
remoteStreamEventRemoveCallback(virStreamPtr st)
4989
{
4990
    struct private_data *priv = st->conn->privateData;
4991
    virNetClientStreamPtr privst = st->privateData;
4992 4993 4994 4995
    int ret = -1;

    remoteDriverLock(priv);

4996
    ret = virNetClientStreamEventRemoveCallback(privst);
4997 4998 4999

    remoteDriverUnlock(priv);
    return ret;
5000 5001
}

5002

5003 5004 5005 5006
static int
remoteStreamFinish(virStreamPtr st)
{
    struct private_data *priv = st->conn->privateData;
5007
    virNetClientStreamPtr privst = st->privateData;
5008 5009 5010 5011
    int ret = -1;

    remoteDriverLock(priv);

5012
    if (virNetClientStreamRaiseError(privst))
5013 5014
        goto cleanup;

5015 5016 5017
    priv->localUses++;
    remoteDriverUnlock(priv);

5018 5019 5020 5021 5022
    ret = virNetClientStreamSendPacket(privst,
                                       priv->client,
                                       VIR_NET_OK,
                                       NULL,
                                       0);
5023

5024 5025 5026
    remoteDriverLock(priv);
    priv->localUses--;

5027
cleanup:
5028
    virNetClientRemoveStream(priv->client, privst);
5029
    virObjectUnref(privst);
5030 5031
    st->privateData = NULL;
    st->driver = NULL;
5032 5033 5034 5035 5036

    remoteDriverUnlock(priv);
    return ret;
}

5037

5038 5039 5040 5041
static int
remoteStreamAbort(virStreamPtr st)
{
    struct private_data *priv = st->conn->privateData;
5042
    virNetClientStreamPtr privst = st->privateData;
5043 5044 5045 5046
    int ret = -1;

    remoteDriverLock(priv);

5047
    if (virNetClientStreamRaiseError(privst))
5048 5049
        goto cleanup;

5050 5051 5052
    priv->localUses++;
    remoteDriverUnlock(priv);

5053 5054 5055 5056 5057
    ret = virNetClientStreamSendPacket(privst,
                                       priv->client,
                                       VIR_NET_ERROR,
                                       NULL,
                                       0);
5058

5059 5060 5061
    remoteDriverLock(priv);
    priv->localUses--;

5062
cleanup:
5063
    virNetClientRemoveStream(priv->client, privst);
5064
    virObjectUnref(privst);
5065 5066
    st->privateData = NULL;
    st->driver = NULL;
5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077

    remoteDriverUnlock(priv);
    return ret;
}


static virStreamDriver remoteStreamDrv = {
    .streamRecv = remoteStreamRecv,
    .streamSend = remoteStreamSend,
    .streamFinish = remoteStreamFinish,
    .streamAbort = remoteStreamAbort,
5078 5079 5080
    .streamEventAddCallback = remoteStreamEventAddCallback,
    .streamEventUpdateCallback = remoteStreamEventUpdateCallback,
    .streamEventRemoveCallback = remoteStreamEventRemoveCallback,
5081 5082
};

5083

5084 5085 5086 5087 5088 5089
static int remoteConnectDomainEventRegisterAny(virConnectPtr conn,
                                               virDomainPtr dom,
                                               int eventID,
                                               virConnectDomainEventGenericCallback callback,
                                               void *opaque,
                                               virFreeCallback freecb)
5090 5091 5092
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
5093
    remote_connect_domain_event_register_any_args args;
5094
    int callbackID;
5095
    int count;
5096 5097 5098

    remoteDriverLock(priv);

5099 5100 5101 5102 5103
    if ((count = virDomainEventStateRegisterID(conn,
                                               priv->domainEventState,
                                               dom, eventID,
                                               callback, opaque, freecb,
                                               &callbackID)) < 0) {
5104
        virReportError(VIR_ERR_RPC, "%s", _("adding cb to list"));
5105
        goto done;
5106 5107 5108 5109
    }

    /* If this is the first callback for this eventID, we need to enable
     * events on the server */
5110
    if (count == 1) {
5111 5112
        args.eventID = eventID;

5113 5114
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_DOMAIN_EVENT_REGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_domain_event_register_any_args, (char *) &args,
5115
                 (xdrproc_t) xdr_void, (char *)NULL) == -1) {
5116 5117 5118
            virDomainEventStateDeregisterID(conn,
                                            priv->domainEventState,
                                            callbackID);
5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130
            goto done;
        }
    }

    rv = callbackID;

done:
    remoteDriverUnlock(priv);
    return rv;
}


5131 5132
static int remoteConnectDomainEventDeregisterAny(virConnectPtr conn,
                                                 int callbackID)
5133 5134 5135
{
    struct private_data *priv = conn->privateData;
    int rv = -1;
5136
    remote_connect_domain_event_deregister_any_args args;
5137
    int eventID;
5138
    int count;
5139 5140 5141

    remoteDriverLock(priv);

5142 5143 5144
    if ((eventID = virDomainEventStateEventID(conn,
                                              priv->domainEventState,
                                              callbackID)) < 0) {
5145
        virReportError(VIR_ERR_RPC, _("unable to find callback ID %d"), callbackID);
5146 5147 5148
        goto done;
    }

5149 5150 5151
    if ((count = virDomainEventStateDeregisterID(conn,
                                                 priv->domainEventState,
                                                 callbackID)) < 0) {
5152
        virReportError(VIR_ERR_RPC, _("unable to find callback ID %d"), callbackID);
5153
        goto done;
5154
    }
5155 5156 5157

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

5161 5162
        if (call(conn, priv, 0, REMOTE_PROC_CONNECT_DOMAIN_EVENT_DEREGISTER_ANY,
                 (xdrproc_t) xdr_remote_connect_domain_event_deregister_any_args, (char *) &args,
5163
                 (xdrproc_t) xdr_void, (char *) NULL) == -1)
5164 5165 5166 5167 5168 5169 5170 5171 5172 5173
            goto done;
    }

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

5174

5175 5176
/*----------------------------------------------------------------------*/

C
Chris Lalancette 已提交
5177
static int
5178
remoteDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd,
5179
                               char **result, unsigned int flags)
C
Chris Lalancette 已提交
5180 5181
{
    int rv = -1;
5182 5183
    qemu_domain_monitor_command_args args;
    qemu_domain_monitor_command_ret ret;
C
Chris Lalancette 已提交
5184 5185 5186 5187
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

5188
    make_nonnull_domain(&args.dom, domain);
C
Chris Lalancette 已提交
5189 5190 5191
    args.cmd = (char *)cmd;
    args.flags = flags;

5192
    memset(&ret, 0, sizeof(ret));
5193 5194 5195
    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 已提交
5196 5197
        goto done;

5198
    if (VIR_STRDUP(*result, ret.result) < 0)
C
Chris Lalancette 已提交
5199 5200 5201 5202 5203
        goto cleanup;

    rv = 0;

cleanup:
5204
    xdr_free((xdrproc_t) xdr_qemu_domain_monitor_command_ret, (char *) &ret);
C
Chris Lalancette 已提交
5205 5206 5207 5208 5209 5210

done:
    remoteDriverUnlock(priv);
    return rv;
}

5211 5212 5213

static char *
remoteDomainMigrateBegin3(virDomainPtr domain,
5214
                          const char *xmlin,
5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230
                          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));

5231
    make_nonnull_domain(&args.dom, domain);
5232
    args.xmlin = xmlin == NULL ? NULL : (char **) &xmlin;
5233 5234 5235 5236
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;

5237 5238 5239
    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)
5240 5241 5242 5243
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
5244 5245
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

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

done:
    remoteDriverUnlock(priv);
    return rv;

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

5295 5296 5297 5298
    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)
5299 5300 5301 5302
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
5303 5304
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
5305 5306 5307 5308 5309 5310 5311
            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) {
5312 5313
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores uri_out"));
5314 5315 5316 5317 5318 5319 5320 5321
            goto error;
        }
        *uri_out = *ret.uri_out; /* Caller frees. */
    }

    rv = 0;

done:
O
Osier Yang 已提交
5322
    VIR_FREE(ret.uri_out);
5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348
    remoteDriverUnlock(priv);
    return rv;
error:
    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;
5349
    virNetClientStreamPtr netst;
5350 5351 5352 5353 5354 5355

    remoteDriverLock(priv);

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

5356
    if (!(netst = virNetClientStreamNew(priv->remoteProgram,
5357
                                        REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3,
5358
                                        priv->counter)))
5359 5360
        goto done;

5361
    if (virNetClientAddStream(priv->client, netst) < 0) {
5362
        virObjectUnref(netst);
5363 5364 5365
        goto done;
    }

5366
    st->driver = &remoteStreamDrv;
5367
    st->privateData = netst;
5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378

    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) {
5379
        virNetClientRemoveStream(priv->client, netst);
5380
        virObjectUnref(netst);
5381 5382 5383 5384 5385
        goto done;
    }

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
5386 5387
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;

error:
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto done;
}


static int
remoteDomainMigratePerform3(virDomainPtr dom,
5408
                            const char *xmlin,
5409 5410 5411 5412
                            const char *cookiein,
                            int cookieinlen,
                            char **cookieout,
                            int *cookieoutlen,
5413
                            const char *dconnuri,
5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430
                            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);

5431
    args.xmlin = xmlin == NULL ? NULL : (char **) &xmlin;
5432 5433 5434 5435
    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;
5436 5437
    args.uri = uri == NULL ? NULL : (char **) &uri;
    args.dconnuri = dconnuri == NULL ? NULL : (char **) &dconnuri;
5438 5439
    args.resource = resource;

5440 5441 5442
    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)
5443 5444 5445 5446
        goto done;

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
5447 5448
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466
            goto error;
        }
        *cookieout = ret.cookie_out.cookie_out_val; /* Caller frees. */
        *cookieoutlen = ret.cookie_out.cookie_out_len;
    }

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;

error:
    VIR_FREE(ret.cookie_out.cookie_out_val);
    goto done;
}


5467
static virDomainPtr
5468 5469 5470 5471 5472 5473
remoteDomainMigrateFinish3(virConnectPtr dconn,
                           const char *dname,
                           const char *cookiein,
                           int cookieinlen,
                           char **cookieout,
                           int *cookieoutlen,
5474
                           const char *dconnuri,
5475 5476
                           const char *uri,
                           unsigned long flags,
5477
                           int cancelled)
5478 5479 5480 5481
{
    remote_domain_migrate_finish3_args args;
    remote_domain_migrate_finish3_ret ret;
    struct private_data *priv = dconn->privateData;
5482
    virDomainPtr rv = NULL;
5483 5484 5485 5486 5487 5488 5489 5490 5491

    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;
5492 5493
    args.uri = uri == NULL ? NULL : (char **) &uri;
    args.dconnuri = dconnuri == NULL ? NULL : (char **) &dconnuri;
5494 5495 5496
    args.flags = flags;
    args.cancelled = cancelled;

5497 5498 5499
    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)
5500 5501
        goto done;

5502
    rv = get_nonnull_domain(dconn, ret.dom);
5503 5504 5505

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
5506 5507
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("caller ignores cookieout or cookieoutlen"));
5508 5509 5510 5511 5512 5513 5514 5515
            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;
    }

5516
    xdr_free((xdrproc_t) &xdr_remote_domain_migrate_finish3_ret, (char *) &ret);
5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542

done:
    remoteDriverUnlock(priv);
    return rv;

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

5543
    make_nonnull_domain(&args.dom, domain);
5544 5545 5546 5547 5548
    args.cookie_in.cookie_in_len = cookieinlen;
    args.cookie_in.cookie_in_val = (char *) cookiein;
    args.flags = flags;
    args.cancelled = cancelled;

5549 5550 5551
    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)
5552 5553 5554 5555 5556 5557 5558 5559 5560 5561
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}


5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623
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;

cleanup:
    virStringFreeList(retmodels);

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

done:
    remoteDriverUnlock(priv);
    return rv;
}


5624 5625 5626 5627 5628 5629 5630 5631 5632
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;
5633 5634
    int fdin[] = { fd };
    size_t fdinlen = ARRAY_CARDINALITY(fdin);
5635 5636 5637

    remoteDriverLock(priv);

5638
    make_nonnull_domain(&args.dom, dom);
5639 5640 5641
    args.idx = idx;
    args.flags = flags;

5642 5643 5644 5645 5646 5647
    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)
5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);

    return rv;
}


5659
static int
5660
remoteConnectSetKeepAlive(virConnectPtr conn, int interval, unsigned int count)
5661 5662 5663 5664 5665 5666
{
    struct private_data *priv = conn->privateData;
    int ret = -1;

    remoteDriverLock(priv);
    if (!virNetClientKeepAliveIsSupported(priv->client)) {
5667 5668 5669
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("the caller doesn't support keepalive protocol;"
                         " perhaps it's missing event loop implementation"));
5670 5671 5672 5673 5674 5675 5676 5677
        goto cleanup;
    }

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

5678 5679 5680 5681 5682 5683
    if (interval > 0) {
        ret = virNetClientKeepAliveStart(priv->client, interval, count);
    } else {
        virNetClientKeepAliveStop(priv->client);
        ret = 0;
    }
5684 5685 5686 5687 5688 5689 5690

cleanup:
    remoteDriverUnlock(priv);
    return ret;
}


5691
static int
5692
remoteConnectIsAlive(virConnectPtr conn)
5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707
{
    struct private_data *priv = conn->privateData;
    bool ret;

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

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


5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724
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;

5725
    memset(&ret, 0, sizeof(ret));
5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750

    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;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

5751
#include "remote_client_bodies.h"
5752
#include "lxc_client_bodies.h"
5753
#include "qemu_client_bodies.h"
5754

5755 5756 5757 5758 5759
/*
 * Serial a set of arguments into a method call message,
 * send that to the server and wait for reply
 */
static int
5760 5761 5762 5763 5764 5765 5766 5767 5768 5769
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)
5770
{
5771
    int rv;
5772
    virNetClientProgramPtr prog;
5773 5774 5775 5776
    int counter = priv->counter++;
    virNetClientPtr client = priv->client;
    priv->localUses++;

5777 5778 5779 5780 5781 5782 5783
    if (flags & REMOTE_CALL_QEMU)
        prog = priv->qemuProgram;
    else if (flags & REMOTE_CALL_LXC)
        prog = priv->lxcProgram;
    else
        prog = priv->remoteProgram;

5784 5785 5786 5787 5788 5789 5790 5791 5792
    /* 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,
5793 5794
                                 fdinlen, fdin,
                                 fdoutlen, fdout,
5795 5796 5797 5798
                                 args_filter, args,
                                 ret_filter, ret);
    remoteDriverLock(priv);
    priv->localUses--;
5799

5800
    return rv;
5801 5802
}

5803
static int
5804 5805 5806 5807 5808 5809
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)
5810
{
5811 5812 5813 5814 5815 5816
    return callFull(conn, priv, flags,
                    NULL, 0,
                    NULL, NULL,
                    proc_nr,
                    args_filter, args,
                    ret_filter, ret);
5817 5818
}

5819

5820
static int
5821 5822 5823 5824
remoteDomainGetInterfaceParameters(virDomainPtr domain,
                                   const char *device,
                                   virTypedParameterPtr params, int *nparams,
                                   unsigned int flags)
5825 5826 5827 5828 5829 5830 5831 5832
{
    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);

5833
    make_nonnull_domain(&args.dom, domain);
5834 5835 5836 5837
    args.device = (char *)device;
    args.nparams = *nparams;
    args.flags = flags;

5838 5839 5840 5841
    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)
5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855
        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,
5856
                                         &params,
5857 5858 5859 5860 5861 5862
                                         nparams) < 0)
        goto cleanup;

    rv = 0;

cleanup:
5863 5864
    xdr_free((xdrproc_t) xdr_remote_domain_get_interface_parameters_ret,
             (char *) &ret);
5865 5866 5867 5868 5869
done:
    remoteDriverUnlock(priv);
    return rv;
}

5870 5871 5872 5873 5874 5875
static int
remoteDomainListAllSnapshots(virDomainPtr dom,
                             virDomainSnapshotPtr **snapshots,
                             unsigned int flags)
{
    int rv = -1;
5876
    size_t i;
5877 5878 5879 5880 5881 5882 5883 5884
    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);

5885
    make_nonnull_domain(&args.dom, dom);
5886 5887 5888 5889
    args.need_results = !!snapshots;
    args.flags = flags;

    memset(&ret, 0, sizeof(ret));
5890 5891 5892 5893 5894 5895 5896 5897
    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)
5898 5899
        goto done;

5900 5901 5902 5903 5904 5905 5906 5907
    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;
    }

5908
    if (snapshots) {
5909
        if (VIR_ALLOC_N(snaps, ret.snapshots.snapshots_len + 1) < 0)
5910 5911 5912
            goto cleanup;
        for (i = 0; i < ret.snapshots.snapshots_len; i++) {
            snaps[i] = get_nonnull_domain_snapshot(dom, ret.snapshots.snapshots_val[i]);
5913
            if (!snaps[i])
5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942
                goto cleanup;
        }
        *snapshots = snaps;
        snaps = NULL;
    }

    rv = ret.ret;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteDomainSnapshotListAllChildren(virDomainSnapshotPtr parent,
                                    virDomainSnapshotPtr **snapshots,
                                    unsigned int flags)
{
    int rv = -1;
5943
    size_t i;
5944 5945 5946 5947 5948 5949 5950 5951 5952 5953
    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;
5954
    make_nonnull_domain_snapshot(&args.snapshot, parent);
5955 5956

    memset(&ret, 0, sizeof(ret));
5957 5958 5959 5960 5961 5962 5963 5964
    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)
5965 5966
        goto done;

5967 5968 5969 5970 5971 5972 5973 5974
    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;
    }

5975
    if (snapshots) {
5976
        if (VIR_ALLOC_N(snaps, ret.snapshots.snapshots_len + 1) < 0)
5977 5978 5979
            goto cleanup;
        for (i = 0; i < ret.snapshots.snapshots_len; i++) {
            snaps[i] = get_nonnull_domain_snapshot(parent->domain, ret.snapshots.snapshots_val[i]);
5980
            if (!snaps[i])
5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003
                goto cleanup;
        }
        *snapshots = snaps;
        snaps = NULL;
    }

    rv = ret.ret;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019
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;

6020 6021 6022 6023
    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)
6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037
        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,
6038
                                         &params,
6039 6040 6041 6042 6043 6044
                                         nparams) < 0)
        goto cleanup;

    rv = 0;

cleanup:
6045 6046
    xdr_free((xdrproc_t) xdr_remote_node_get_memory_parameters_ret,
             (char *) &ret);
6047 6048 6049 6050 6051
done:
    remoteDriverUnlock(priv);
    return rv;
}

6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064
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);

6065 6066
    args.need_map = !!cpumap;
    args.need_online = !!online;
6067 6068
    args.flags = flags;

6069
    memset(&ret, 0, sizeof(ret));
6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080
    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) {
6081
        if (VIR_ALLOC_N(*cpumap, ret.cpumap.cpumap_len) < 0)
6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097
            goto cleanup;
        memcpy(*cpumap, ret.cpumap.cpumap_val, ret.cpumap.cpumap_len);
    }

    if (online)
        *online = ret.online;

    rv = ret.ret;

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

6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130

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;

done:
    remoteDriverUnlock(priv);
    return rv;
}

6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153
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;

6154 6155 6156 6157 6158 6159 6160 6161
    if (ret.params.params_len > REMOTE_DOMAIN_JOB_STATS_MAX) {
        virReportError(VIR_ERR_RPC,
                       _("Too many job stats '%d' for limit '%d'"),
                       ret.params.params_len,
                       REMOTE_DOMAIN_JOB_STATS_MAX);
        goto cleanup;
    }

6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178
    *type = ret.type;

    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
                                         0, params, nparams) < 0)
        goto cleanup;

    rv = 0;

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

6179

6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200
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;

6201 6202 6203 6204 6205 6206 6207
    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;
    }

6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266
    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 */

cleanup:
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    remoteDriverUnlock(priv);
    return rv;

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

6267 6268 6269 6270 6271 6272 6273
    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;
    }

6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348
    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;

cleanup:
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    VIR_FREE(ret.uri_out);
    remoteDriverUnlock(priv);
    return rv;

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

6349 6350 6351 6352 6353 6354 6355
    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;
    }

6356 6357 6358 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 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434
    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;

cleanup:
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    remoteDriverUnlock(priv);
    return rv;

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

6435 6436 6437 6438 6439 6440 6441
    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;
    }

6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 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 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506
    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;

cleanup:
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    remoteDriverUnlock(priv);
    return rv;

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

6507 6508 6509 6510 6511 6512 6513
    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;
    }

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

cleanup:
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    remoteDriverUnlock(priv);
    return rv;

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

6579 6580 6581 6582 6583 6584 6585
    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;
    }

6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613
    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;

cleanup:
    remoteFreeTypedParameters(args.params.params_val, args.params.params_len);
    remoteDriverUnlock(priv);
    return rv;
}


6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682
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);

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

6683
static void
6684 6685 6686 6687
remoteDomainEventQueue(struct private_data *priv, virDomainEventPtr event)
{
    virDomainEventStateQueue(priv->domainEventState, event);
}
6688

6689 6690 6691 6692 6693 6694
/* 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
6695
get_nonnull_domain(virConnectPtr conn, remote_nonnull_domain domain)
6696 6697
{
    virDomainPtr dom;
6698
    dom = virGetDomain(conn, domain.name, BAD_CAST domain.uuid);
6699 6700 6701 6702 6703
    if (dom) dom->id = domain.id;
    return dom;
}

static virNetworkPtr
6704
get_nonnull_network(virConnectPtr conn, remote_nonnull_network network)
6705
{
6706
    return virGetNetwork(conn, network.name, BAD_CAST network.uuid);
6707 6708
}

D
Daniel Veillard 已提交
6709
static virInterfacePtr
6710
get_nonnull_interface(virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
6711
{
6712
    return virGetInterface(conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
6713 6714
}

6715
static virStoragePoolPtr
6716
get_nonnull_storage_pool(virConnectPtr conn, remote_nonnull_storage_pool pool)
6717
{
6718 6719
    return virGetStoragePool(conn, pool.name, BAD_CAST pool.uuid,
                             NULL, NULL);
6720 6721 6722
}

static virStorageVolPtr
6723
get_nonnull_storage_vol(virConnectPtr conn, remote_nonnull_storage_vol vol)
6724
{
6725 6726
    return virGetStorageVol(conn, vol.pool, vol.name, vol.key,
                            NULL, NULL);
6727 6728
}

6729
static virNodeDevicePtr
6730
get_nonnull_node_device(virConnectPtr conn, remote_nonnull_node_device dev)
6731 6732 6733 6734
{
    return virGetNodeDevice(conn, dev.name);
}

6735
static virSecretPtr
6736
get_nonnull_secret(virConnectPtr conn, remote_nonnull_secret secret)
6737
{
6738
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
6739 6740
}

6741
static virNWFilterPtr
6742
get_nonnull_nwfilter(virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
6743
{
6744
    return virGetNWFilter(conn, nwfilter.name, BAD_CAST nwfilter.uuid);
6745 6746
}

C
Chris Lalancette 已提交
6747
static virDomainSnapshotPtr
6748
get_nonnull_domain_snapshot(virDomainPtr domain, remote_nonnull_domain_snapshot snapshot)
C
Chris Lalancette 已提交
6749 6750 6751 6752
{
    return virGetDomainSnapshot(domain, snapshot.name);
}

6753

6754 6755
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
6756
make_nonnull_domain(remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
6757
{
6758
    dom_dst->id = dom_src->id;
6759
    dom_dst->name = dom_src->name;
6760
    memcpy(dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
6761 6762 6763
}

static void
6764
make_nonnull_network(remote_nonnull_network *net_dst, virNetworkPtr net_src)
6765 6766
{
    net_dst->name = net_src->name;
6767
    memcpy(net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
6768 6769
}

D
Daniel Veillard 已提交
6770
static void
6771
make_nonnull_interface(remote_nonnull_interface *interface_dst,
D
Daniel Veillard 已提交
6772 6773 6774 6775 6776 6777
                        virInterfacePtr interface_src)
{
    interface_dst->name = interface_src->name;
    interface_dst->mac = interface_src->mac;
}

6778
static void
6779
make_nonnull_storage_pool(remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
6780 6781
{
    pool_dst->name = pool_src->name;
6782
    memcpy(pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
6783 6784 6785
}

static void
6786
make_nonnull_storage_vol(remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
6787 6788 6789 6790 6791 6792
{
    vol_dst->pool = vol_src->pool;
    vol_dst->name = vol_src->name;
    vol_dst->key = vol_src->key;
}

6793
static void
6794
make_nonnull_secret(remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
6795
{
6796
    memcpy(secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
6797 6798
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = secret_src->usageID;
6799 6800
}

6801
static void
6802
make_nonnull_nwfilter(remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
6803 6804
{
    nwfilter_dst->name = nwfilter_src->name;
6805
    memcpy(nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
6806 6807
}

C
Chris Lalancette 已提交
6808
static void
6809
make_nonnull_domain_snapshot(remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
C
Chris Lalancette 已提交
6810 6811
{
    snapshot_dst->name = snapshot_src->name;
6812
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
6813 6814
}

6815 6816
/*----------------------------------------------------------------------*/

6817 6818 6819 6820 6821
unsigned long remoteVersion(void)
{
    return REMOTE_PROTOCOL_VERSION;
}

6822
static virDriver remote_driver = {
6823 6824
    .no = VIR_DRV_REMOTE,
    .name = "remote",
6825 6826
    .connectOpen = remoteConnectOpen, /* 0.3.0 */
    .connectClose = remoteConnectClose, /* 0.3.0 */
6827 6828 6829 6830 6831 6832 6833
    .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 */
6834
    .nodeGetInfo = remoteNodeGetInfo, /* 0.3.0 */
6835 6836 6837 6838
    .connectGetCapabilities = remoteConnectGetCapabilities, /* 0.3.0 */
    .connectListDomains = remoteConnectListDomains, /* 0.3.0 */
    .connectNumOfDomains = remoteConnectNumOfDomains, /* 0.3.0 */
    .connectListAllDomains = remoteConnectListAllDomains, /* 0.9.13 */
6839
    .domainCreateXML = remoteDomainCreateXML, /* 0.3.0 */
6840
    .domainCreateXMLWithFiles = remoteDomainCreateXMLWithFiles, /* 1.1.1 */
6841 6842 6843 6844 6845
    .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 */
6846
    .domainPMSuspendForDuration = remoteDomainPMSuspendForDuration, /* 0.9.10 */
6847
    .domainPMWakeup = remoteDomainPMWakeup, /* 0.9.11 */
6848
    .domainShutdown = remoteDomainShutdown, /* 0.3.0 */
6849
    .domainShutdownFlags = remoteDomainShutdownFlags, /* 0.9.10 */
6850
    .domainReboot = remoteDomainReboot, /* 0.3.0 */
6851
    .domainReset = remoteDomainReset, /* 0.9.7 */
6852
    .domainDestroy = remoteDomainDestroy, /* 0.3.0 */
6853
    .domainDestroyFlags = remoteDomainDestroyFlags, /* 0.9.4 */
6854 6855 6856 6857 6858
    .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 */
6859
    .domainSetMemoryStatsPeriod = remoteDomainSetMemoryStatsPeriod, /* 1.1.1 */
6860 6861 6862 6863 6864 6865
    .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 */
6866
    .domainGetControlInfo = remoteDomainGetControlInfo, /* 0.9.3 */
6867
    .domainSave = remoteDomainSave, /* 0.3.0 */
E
Eric Blake 已提交
6868
    .domainSaveFlags = remoteDomainSaveFlags, /* 0.9.4 */
6869
    .domainRestore = remoteDomainRestore, /* 0.3.0 */
E
Eric Blake 已提交
6870
    .domainRestoreFlags = remoteDomainRestoreFlags, /* 0.9.4 */
E
Eric Blake 已提交
6871 6872
    .domainSaveImageGetXMLDesc = remoteDomainSaveImageGetXMLDesc, /* 0.9.4 */
    .domainSaveImageDefineXML = remoteDomainSaveImageDefineXML, /* 0.9.4 */
6873 6874 6875 6876 6877 6878
    .domainCoreDump = remoteDomainCoreDump, /* 0.3.0 */
    .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 */
6879
    .domainPinVcpuFlags = remoteDomainPinVcpuFlags, /* 0.9.3 */
E
Eric Blake 已提交
6880
    .domainGetVcpuPinInfo = remoteDomainGetVcpuPinInfo, /* 0.9.3 */
6881 6882
    .domainPinEmulator = remoteDomainPinEmulator, /* 0.10.0 */
    .domainGetEmulatorPinInfo = remoteDomainGetEmulatorPinInfo, /* 0.10.0 */
6883 6884 6885
    .domainGetVcpus = remoteDomainGetVcpus, /* 0.3.0 */
    .domainGetMaxVcpus = remoteDomainGetMaxVcpus, /* 0.3.0 */
    .domainGetSecurityLabel = remoteDomainGetSecurityLabel, /* 0.6.1 */
M
Marcelo Cerri 已提交
6886
    .domainGetSecurityLabelList = remoteDomainGetSecurityLabelList, /* 0.10.0 */
6887 6888
    .nodeGetSecurityModel = remoteNodeGetSecurityModel, /* 0.6.1 */
    .domainGetXMLDesc = remoteDomainGetXMLDesc, /* 0.3.0 */
6889 6890 6891 6892
    .connectDomainXMLFromNative = remoteConnectDomainXMLFromNative, /* 0.6.4 */
    .connectDomainXMLToNative = remoteConnectDomainXMLToNative, /* 0.6.4 */
    .connectListDefinedDomains = remoteConnectListDefinedDomains, /* 0.3.0 */
    .connectNumOfDefinedDomains = remoteConnectNumOfDefinedDomains, /* 0.3.0 */
6893 6894
    .domainCreate = remoteDomainCreate, /* 0.3.0 */
    .domainCreateWithFlags = remoteDomainCreateWithFlags, /* 0.8.2 */
6895
    .domainCreateWithFiles = remoteDomainCreateWithFiles, /* 1.1.1 */
6896 6897
    .domainDefineXML = remoteDomainDefineXML, /* 0.3.0 */
    .domainUndefine = remoteDomainUndefine, /* 0.3.0 */
6898
    .domainUndefineFlags = remoteDomainUndefineFlags, /* 0.9.4 */
6899 6900 6901 6902 6903 6904 6905 6906 6907
    .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 */
6908
    .domainGetSchedulerParametersFlags = remoteDomainGetSchedulerParametersFlags, /* 0.9.2 */
6909
    .domainSetSchedulerParameters = remoteDomainSetSchedulerParameters, /* 0.3.0 */
6910
    .domainSetSchedulerParametersFlags = remoteDomainSetSchedulerParametersFlags, /* 0.9.2 */
6911 6912 6913
    .domainMigratePrepare = remoteDomainMigratePrepare, /* 0.3.2 */
    .domainMigratePerform = remoteDomainMigratePerform, /* 0.3.2 */
    .domainMigrateFinish = remoteDomainMigrateFinish, /* 0.3.2 */
6914
    .domainBlockResize = remoteDomainBlockResize, /* 0.9.8 */
6915
    .domainBlockStats = remoteDomainBlockStats, /* 0.3.2 */
6916
    .domainBlockStatsFlags = remoteDomainBlockStatsFlags, /* 0.9.5 */
6917
    .domainInterfaceStats = remoteDomainInterfaceStats, /* 0.3.2 */
6918 6919
    .domainSetInterfaceParameters = remoteDomainSetInterfaceParameters, /* 0.9.9 */
    .domainGetInterfaceParameters = remoteDomainGetInterfaceParameters, /* 0.9.9 */
6920 6921 6922 6923
    .domainMemoryStats = remoteDomainMemoryStats, /* 0.7.5 */
    .domainBlockPeek = remoteDomainBlockPeek, /* 0.4.2 */
    .domainMemoryPeek = remoteDomainMemoryPeek, /* 0.4.2 */
    .domainGetBlockInfo = remoteDomainGetBlockInfo, /* 0.8.1 */
6924
    .nodeGetCPUStats = remoteNodeGetCPUStats, /* 0.9.3 */
6925
    .nodeGetMemoryStats = remoteNodeGetMemoryStats, /* 0.9.3 */
6926 6927
    .nodeGetCellsFreeMemory = remoteNodeGetCellsFreeMemory, /* 0.3.3 */
    .nodeGetFreeMemory = remoteNodeGetFreeMemory, /* 0.3.3 */
6928 6929
    .connectDomainEventRegister = remoteConnectDomainEventRegister, /* 0.5.0 */
    .connectDomainEventDeregister = remoteConnectDomainEventDeregister, /* 0.5.0 */
6930 6931 6932
    .domainMigratePrepare2 = remoteDomainMigratePrepare2, /* 0.5.0 */
    .domainMigrateFinish2 = remoteDomainMigrateFinish2, /* 0.5.0 */
    .nodeDeviceDettach = remoteNodeDeviceDettach, /* 0.6.1 */
6933
    .nodeDeviceDetachFlags = remoteNodeDeviceDetachFlags, /* 1.0.5 */
6934 6935 6936
    .nodeDeviceReAttach = remoteNodeDeviceReAttach, /* 0.6.1 */
    .nodeDeviceReset = remoteNodeDeviceReset, /* 0.6.1 */
    .domainMigratePrepareTunnel = remoteDomainMigratePrepareTunnel, /* 0.7.2 */
6937 6938
    .connectIsEncrypted = remoteConnectIsEncrypted, /* 0.7.3 */
    .connectIsSecure = remoteConnectIsSecure, /* 0.7.3 */
6939 6940 6941
    .domainIsActive = remoteDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = remoteDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = remoteDomainIsUpdated, /* 0.8.6 */
6942 6943
    .connectCompareCPU = remoteConnectCompareCPU, /* 0.7.5 */
    .connectBaselineCPU = remoteConnectBaselineCPU, /* 0.7.7 */
6944
    .domainGetJobInfo = remoteDomainGetJobInfo, /* 0.7.7 */
6945
    .domainGetJobStats = remoteDomainGetJobStats, /* 1.0.3 */
6946 6947
    .domainAbortJob = remoteDomainAbortJob, /* 0.7.7 */
    .domainMigrateSetMaxDowntime = remoteDomainMigrateSetMaxDowntime, /* 0.8.0 */
6948 6949
    .domainMigrateGetCompressionCache = remoteDomainMigrateGetCompressionCache, /* 1.0.3 */
    .domainMigrateSetCompressionCache = remoteDomainMigrateSetCompressionCache, /* 1.0.3 */
6950
    .domainMigrateSetMaxSpeed = remoteDomainMigrateSetMaxSpeed, /* 0.9.0 */
6951
    .domainMigrateGetMaxSpeed = remoteDomainMigrateGetMaxSpeed, /* 0.9.5 */
6952 6953
    .connectDomainEventRegisterAny = remoteConnectDomainEventRegisterAny, /* 0.8.0 */
    .connectDomainEventDeregisterAny = remoteConnectDomainEventDeregisterAny, /* 0.8.0 */
6954 6955 6956 6957 6958 6959 6960
    .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 */
6961
    .domainListAllSnapshots = remoteDomainListAllSnapshots, /* 0.9.13 */
6962
    .domainSnapshotNumChildren = remoteDomainSnapshotNumChildren, /* 0.9.7 */
6963
    .domainSnapshotListAllChildren = remoteDomainSnapshotListAllChildren, /* 0.9.13 */
6964
    .domainSnapshotListChildrenNames = remoteDomainSnapshotListChildrenNames, /* 0.9.7 */
6965 6966
    .domainSnapshotLookupByName = remoteDomainSnapshotLookupByName, /* 0.8.0 */
    .domainHasCurrentSnapshot = remoteDomainHasCurrentSnapshot, /* 0.8.0 */
6967
    .domainSnapshotGetParent = remoteDomainSnapshotGetParent, /* 0.9.7 */
6968 6969
    .domainSnapshotCurrent = remoteDomainSnapshotCurrent, /* 0.8.0 */
    .domainRevertToSnapshot = remoteDomainRevertToSnapshot, /* 0.8.0 */
E
Eric Blake 已提交
6970 6971
    .domainSnapshotIsCurrent = remoteDomainSnapshotIsCurrent, /* 0.9.13 */
    .domainSnapshotHasMetadata = remoteDomainSnapshotHasMetadata, /* 0.9.13 */
6972
    .domainSnapshotDelete = remoteDomainSnapshotDelete, /* 0.8.0 */
6973 6974 6975
    .domainQemuMonitorCommand = remoteDomainQemuMonitorCommand, /* 0.8.3 */
    .domainQemuAttach = remoteDomainQemuAttach, /* 0.9.4 */
    .domainQemuAgentCommand = remoteDomainQemuAgentCommand, /* 0.10.0 */
6976
    .domainOpenConsole = remoteDomainOpenConsole, /* 0.8.6 */
6977
    .domainOpenChannel = remoteDomainOpenChannel, /* 1.0.2 */
6978
    .domainOpenGraphics = remoteDomainOpenGraphics, /* 0.9.7 */
6979
    .domainInjectNMI = remoteDomainInjectNMI, /* 0.9.2 */
6980 6981 6982 6983 6984 6985
    .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 */
6986
    .domainSendKey = remoteDomainSendKey, /* 0.9.3 */
6987
    .domainSendProcessSignal = remoteDomainSendProcessSignal, /* 1.0.1 */
6988 6989 6990 6991
    .domainBlockJobAbort = remoteDomainBlockJobAbort, /* 0.9.4 */
    .domainGetBlockJobInfo = remoteDomainGetBlockJobInfo, /* 0.9.4 */
    .domainBlockJobSetSpeed = remoteDomainBlockJobSetSpeed, /* 0.9.4 */
    .domainBlockPull = remoteDomainBlockPull, /* 0.9.4 */
6992
    .domainBlockRebase = remoteDomainBlockRebase, /* 0.9.10 */
6993
    .domainBlockCommit = remoteDomainBlockCommit, /* 0.10.2 */
6994 6995
    .connectSetKeepAlive = remoteConnectSetKeepAlive, /* 0.9.8 */
    .connectIsAlive = remoteConnectIsAlive, /* 0.9.8 */
6996
    .nodeSuspendForDuration = remoteNodeSuspendForDuration, /* 0.9.8 */
6997 6998
    .domainSetBlockIoTune = remoteDomainSetBlockIoTune, /* 0.9.8 */
    .domainGetBlockIoTune = remoteDomainGetBlockIoTune, /* 0.9.8 */
6999 7000
    .domainSetNumaParameters = remoteDomainSetNumaParameters, /* 0.9.9 */
    .domainGetNumaParameters = remoteDomainGetNumaParameters, /* 0.9.9 */
7001
    .domainGetCPUStats = remoteDomainGetCPUStats, /* 0.9.10 */
7002
    .domainGetDiskErrors = remoteDomainGetDiskErrors, /* 0.9.10 */
7003 7004
    .domainSetMetadata = remoteDomainSetMetadata, /* 0.9.10 */
    .domainGetMetadata = remoteDomainGetMetadata, /* 0.9.10 */
7005
    .domainGetHostname = remoteDomainGetHostname, /* 0.10.0 */
7006 7007
    .nodeSetMemoryParameters = remoteNodeSetMemoryParameters, /* 0.10.2 */
    .nodeGetMemoryParameters = remoteNodeGetMemoryParameters, /* 0.10.2 */
7008
    .nodeGetCPUMap = remoteNodeGetCPUMap, /* 1.0.0 */
7009
    .domainFSTrim = remoteDomainFSTrim, /* 1.0.1 */
7010
    .domainLxcOpenNamespace = remoteDomainLxcOpenNamespace, /* 1.0.2 */
7011 7012 7013 7014 7015 7016
    .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 */
7017
    .connectGetCPUModelNames = remoteConnectGetCPUModelNames, /* 1.1.3 */
7018 7019 7020
};

static virNetworkDriver network_driver = {
7021
    .name = "remote",
7022 7023
    .networkOpen = remoteNetworkOpen, /* 0.3.0 */
    .networkClose = remoteNetworkClose, /* 0.3.0 */
7024 7025 7026 7027 7028
    .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 */
7029 7030 7031 7032 7033
    .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 */
7034
    .networkUpdate = remoteNetworkUpdate, /* 0.10.2 */
7035 7036 7037 7038 7039 7040 7041 7042
    .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 */
7043 7044
};

D
Daniel Veillard 已提交
7045 7046
static virInterfaceDriver interface_driver = {
    .name = "remote",
7047 7048
    .interfaceOpen = remoteInterfaceOpen, /* 0.7.2 */
    .interfaceClose = remoteInterfaceClose, /* 0.7.2 */
7049 7050 7051 7052 7053
    .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 */
7054 7055 7056 7057 7058 7059 7060 7061
    .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 */
7062 7063 7064
    .interfaceChangeBegin = remoteInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = remoteInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = remoteInterfaceChangeRollback, /* 0.9.2 */
D
Daniel Veillard 已提交
7065 7066
};

7067 7068
static virStorageDriver storage_driver = {
    .name = "remote",
7069 7070
    .storageOpen = remoteStorageOpen, /* 0.4.1 */
    .storageClose = remoteStorageClose, /* 0.4.1 */
7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111
    .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 */
7112 7113
};

7114 7115
static virSecretDriver secret_driver = {
    .name = "remote",
7116 7117
    .secretOpen = remoteSecretOpen, /* 0.7.1 */
    .secretClose = remoteSecretClose, /* 0.7.1 */
7118 7119 7120 7121 7122 7123 7124 7125 7126 7127
    .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 */
7128 7129
};

7130
static virNodeDeviceDriver node_device_driver = {
7131
    .name = "remote",
7132 7133
    .nodeDeviceOpen = remoteNodeDeviceOpen, /* 0.5.0 */
    .nodeDeviceClose = remoteNodeDeviceClose, /* 0.5.0 */
7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144
    .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 */
7145 7146
};

7147 7148
static virNWFilterDriver nwfilter_driver = {
    .name = "remote",
7149 7150
    .nwfilterOpen = remoteNWFilterOpen, /* 0.8.0 */
    .nwfilterClose = remoteNWFilterClose, /* 0.8.0 */
7151 7152
    .nwfilterLookupByUUID = remoteNWFilterLookupByUUID, /* 0.8.0 */
    .nwfilterLookupByName = remoteNWFilterLookupByName, /* 0.8.0 */
7153 7154 7155 7156 7157 7158
    .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 */
7159 7160
};

7161

A
Atsushi SAKAI 已提交
7162
#ifdef WITH_LIBVIRTD
7163
static virStateDriver state_driver = {
7164
    .name = "Remote",
7165
    .stateInitialize = remoteStateInitialize,
7166
};
A
Atsushi SAKAI 已提交
7167
#endif
7168 7169


7170
/** remoteRegister:
7171 7172
 *
 * Register driver with libvirt driver system.
7173 7174
 *
 * Returns -1 on error.
7175 7176
 */
int
7177
remoteRegister(void)
7178
{
7179 7180
    remoteDriver = &remote_driver;

7181 7182 7183 7184
    if (virRegisterDriver(&remote_driver) == -1) return -1;
    if (virRegisterNetworkDriver(&network_driver) == -1) return -1;
    if (virRegisterInterfaceDriver(&interface_driver) == -1) return -1;
    if (virRegisterStorageDriver(&storage_driver) == -1) return -1;
7185
    if (virRegisterNodeDeviceDriver(&node_device_driver) == -1) return -1;
7186
    if (virRegisterSecretDriver(&secret_driver) == -1) return -1;
7187
    if (virRegisterNWFilterDriver(&nwfilter_driver) == -1) return -1;
A
Atsushi SAKAI 已提交
7188
#ifdef WITH_LIBVIRTD
7189
    if (virRegisterStateDriver(&state_driver) == -1) return -1;
A
Atsushi SAKAI 已提交
7190
#endif
7191 7192 7193

    return 0;
}