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

24
#include <config.h>
25

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

29 30
#include <libxml/uri.h>

31 32 33
#include "virnetclient.h"
#include "virnetclientprogram.h"
#include "virnetclientstream.h"
34
#include "virterror_internal.h"
35
#include "logging.h"
36
#include "datatypes.h"
37
#include "domain_event.h"
38
#include "driver.h"
39 40
#include "buf.h"
#include "qparams.h"
41
#include "remote_driver.h"
42
#include "remote_protocol.h"
C
Chris Lalancette 已提交
43
#include "qemu_protocol.h"
44
#include "memory.h"
45
#include "util.h"
E
Eric Blake 已提交
46
#include "virfile.h"
47
#include "command.h"
48
#include "intprops.h"
49

50 51
#define VIR_FROM_THIS VIR_FROM_REMOTE

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

70 71
static int inside_daemon = 0;

72 73
struct private_data {
    virMutex lock;
74

75 76 77
    virNetClientPtr client;
    virNetClientProgramPtr remoteProgram;
    virNetClientProgramPtr qemuProgram;
78

79
    int counter; /* Serial number for RPC */
80

81
    virNetTLSContextPtr tls;
82

83
    int is_secure;              /* Secure if TLS or SASL or UNIX sockets */
84
    char *type;                 /* Cached return from remoteType. */
85
    int localUses;              /* Ref count for private data */
86
    char *hostname;             /* Original hostname */
87

88
    virDomainEventStatePtr domainEventState;
89 90
};

91
enum {
92
    REMOTE_CALL_QEMU              = (1 << 0),
93 94 95
};


96 97 98 99 100 101 102 103 104 105
static void remoteDriverLock(struct private_data *driver)
{
    virMutexLock(&driver->lock);
}

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

106
static int call (virConnectPtr conn, struct private_data *priv,
E
Eric Blake 已提交
107
                 unsigned int flags, int proc_nr,
108 109
                 xdrproc_t args_filter, char *args,
                 xdrproc_t ret_filter, char *ret);
110
static int remoteAuthenticate (virConnectPtr conn, struct private_data *priv,
111
                               virConnectAuthPtr auth, const char *authtype);
112
#if HAVE_SASL
113
static int remoteAuthSASL (virConnectPtr conn, struct private_data *priv,
114
                           virConnectAuthPtr auth, const char *mech);
115
#endif
116
#if HAVE_POLKIT
117
static int remoteAuthPolkit (virConnectPtr conn, struct private_data *priv,
118
                             virConnectAuthPtr auth);
119
#endif /* HAVE_POLKIT */
120 121

#define remoteError(code, ...)                                    \
122
    virReportErrorHelper(VIR_FROM_REMOTE, code, __FILE__,         \
123
                         __FUNCTION__, __LINE__, __VA_ARGS__)
124

125 126
static virDomainPtr get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network (virConnectPtr conn, remote_nonnull_network network);
127
static virNWFilterPtr get_nonnull_nwfilter (virConnectPtr conn, remote_nonnull_nwfilter nwfilter);
128
static virInterfacePtr get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface);
129 130
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);
131
static virNodeDevicePtr get_nonnull_node_device (virConnectPtr conn, remote_nonnull_node_device dev);
132
static virSecretPtr get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret);
C
Chris Lalancette 已提交
133
static virDomainSnapshotPtr get_nonnull_domain_snapshot (virDomainPtr domain, remote_nonnull_domain_snapshot snapshot);
134 135
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);
D
Daniel Veillard 已提交
136
static void make_nonnull_interface (remote_nonnull_interface *interface_dst, virInterfacePtr interface_src);
137 138
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);
139
static void make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src);
140
static void make_nonnull_nwfilter (remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src);
C
Chris Lalancette 已提交
141
static void make_nonnull_domain_snapshot (remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src);
142 143
static void remoteDomainEventQueueFlush(int timer, void *opaque);
static void remoteDomainEventQueue(struct private_data *priv, virDomainEventPtr event);
144 145 146 147 148
/*----------------------------------------------------------------------*/

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

A
Atsushi SAKAI 已提交
149
#ifdef WITH_LIBVIRTD
150
static int
151
remoteStartup(int privileged ATTRIBUTE_UNUSED)
152 153 154 155 156 157 158
{
    /* Mark that we're inside the daemon so we can avoid
     * re-entering ourselves
     */
    inside_daemon = 1;
    return 0;
}
A
Atsushi SAKAI 已提交
159
#endif
160

161
#ifndef WIN32
162
/**
163
 * remoteFindDaemonPath:
164 165
 *
 * Tries to find the path to the libvirtd binary.
166
 *
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
 * 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
    };
    int i;
    const char *customDaemon = getenv("LIBVIRTD_PATH");

    if (customDaemon)
        return(customDaemon);

    for (i = 0; serverPaths[i]; i++) {
E
Eric Blake 已提交
184
        if (virFileIsExecutable(serverPaths[i])) {
185 186 187 188 189
            return serverPaths[i];
        }
    }
    return NULL;
}
190
#endif
191

192

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
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);

226 227 228 229 230
static void
remoteDomainBuildEventBlockJob(virNetClientProgramPtr prog,
                               virNetClientPtr client,
                               void *evdata, void *opaque);

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
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 },
264 265 266 267
    { REMOTE_PROC_DOMAIN_EVENT_BLOCK_JOB,
      remoteDomainBuildEventBlockJob,
      sizeof(remote_domain_event_block_job_msg),
      (xdrproc_t)xdr_remote_domain_event_block_job_msg },
268
};
269

270
enum virDrvOpenRemoteFlags {
271
    VIR_DRV_OPEN_REMOTE_RO = (1 << 0),
272 273
    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 */
274
};
275

276

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
/*
 * 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
 */
294
static int
295 296 297
doRemoteOpen (virConnectPtr conn,
              struct private_data *priv,
              virConnectAuthPtr auth ATTRIBUTE_UNUSED,
298
              unsigned int flags)
299
{
300
    struct qparam_set *vars = NULL;
301
    char *transport_str = NULL;
302 303 304 305 306 307 308
    enum {
        trans_tls,
        trans_unix,
        trans_ssh,
        trans_ext,
        trans_tcp,
    } transport;
309

310 311
    /* We handle *ALL*  URIs here. The caller has rejected any
     * URIs we don't care about */
312

313 314 315
    if (conn->uri) {
        if (!conn->uri->scheme) {
            /* This is the ///var/lib/xen/xend-socket local path style */
316
            transport = trans_unix;
317 318
        } else {
            transport_str = get_transport_from_scheme (conn->uri->scheme);
319

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
            if (!transport_str) {
                if (conn->uri->server)
                    transport = trans_tls;
                else
                    transport = trans_unix;
            } else {
                if (STRCASEEQ (transport_str, "tls"))
                    transport = trans_tls;
                else if (STRCASEEQ (transport_str, "unix"))
                    transport = trans_unix;
                else if (STRCASEEQ (transport_str, "ssh"))
                    transport = trans_ssh;
                else if (STRCASEEQ (transport_str, "ext"))
                    transport = trans_ext;
                else if (STRCASEEQ (transport_str, "tcp"))
                    transport = trans_tcp;
                else {
337 338 339
                    remoteError(VIR_ERR_INVALID_ARG, "%s",
                                _("remote_open: transport in URL not recognised "
                                  "(should be tls|unix|ssh|ext|tcp)"));
340 341 342 343 344 345 346
                    return VIR_DRV_OPEN_ERROR;
                }
            }
        }
    } else {
        /* No URI, then must be probing so use UNIX socket */
        transport = trans_unix;
347
    }
348

E
Eric Blake 已提交
349
    /* Local variables which we will initialize. These can
350 351
     * get freed in the failed: path.
     */
352 353
    char *name = NULL, *command = NULL, *sockname = NULL, *netcat = NULL;
    char *port = NULL, *authtype = NULL, *username = NULL;
354
    bool sanity = true, verify = true, tty = true;
355
    char *pkipath = NULL, *keyfile = NULL;
356

357 358 359
    /* Return code from this function, and the private data. */
    int retcode = VIR_DRV_OPEN_ERROR;

360
    /* Remote server defaults to "localhost" if not specified. */
361
    if (conn->uri && conn->uri->port != 0) {
362
        if (virAsprintf(&port, "%d", conn->uri->port) == -1) goto out_of_memory;
363 364 365 366 367 368 369
    } else if (transport == trans_tls) {
        port = strdup (LIBVIRTD_TLS_PORT);
        if (!port) goto out_of_memory;
    } else if (transport == trans_tcp) {
        port = strdup (LIBVIRTD_TCP_PORT);
        if (!port) goto out_of_memory;
    } else
370
        port = NULL; /* Port not used for unix, ext., default for ssh */
371

372

373 374
    priv->hostname = strdup (conn->uri && conn->uri->server ?
                             conn->uri->server : "localhost");
375 376
    if (!priv->hostname)
        goto out_of_memory;
377 378
    if (conn->uri && conn->uri->user) {
        username = strdup (conn->uri->user);
379 380
        if (!username)
            goto out_of_memory;
381 382
    }

383 384 385 386 387
    /* 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).
     */
388 389
    struct qparam *var;
    int i;
390 391
    char *query;

392
    if (conn->uri) {
393
#ifdef HAVE_XMLURI_QUERY_RAW
394
        query = conn->uri->query_raw;
395
#else
396
        query = conn->uri->query;
397
#endif
398 399 400 401 402 403
        vars = qparam_query_parse (query);
        if (vars == NULL) goto failed;

        for (i = 0; i < vars->n; i++) {
            var = &vars->p[i];
            if (STRCASEEQ (var->name, "name")) {
P
Phil Petty 已提交
404
                VIR_FREE(name);
405 406 407 408
                name = strdup (var->value);
                if (!name) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "command")) {
P
Phil Petty 已提交
409
                VIR_FREE(command);
410 411 412 413
                command = strdup (var->value);
                if (!command) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "socket")) {
P
Phil Petty 已提交
414
                VIR_FREE(sockname);
415 416 417 418
                sockname = strdup (var->value);
                if (!sockname) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "auth")) {
P
Phil Petty 已提交
419
                VIR_FREE(authtype);
420 421 422 423
                authtype = strdup (var->value);
                if (!authtype) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "netcat")) {
P
Phil Petty 已提交
424
                VIR_FREE(netcat);
425 426 427
                netcat = strdup (var->value);
                if (!netcat) goto out_of_memory;
                var->ignore = 1;
428 429 430 431
            } else if (STRCASEEQ (var->name, "keyfile")) {
                VIR_FREE(keyfile);
                keyfile = strdup (var->value);
                if (!keyfile) goto out_of_memory;
432 433
            } else if (STRCASEEQ (var->name, "no_sanity")) {
                sanity = atoi(var->value) == 0;
434
                var->ignore = 1;
435
            } else if (STRCASEEQ (var->name, "no_verify")) {
436
                verify = atoi (var->value) == 0;
437 438
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "no_tty")) {
439
                tty = atoi (var->value) == 0;
440
                var->ignore = 1;
441
            } else if (STRCASEEQ(var->name, "pkipath")) {
P
Phil Petty 已提交
442
                VIR_FREE(pkipath);
443 444 445 446
                pkipath = strdup(var->value);
                if (!pkipath) goto out_of_memory;
                var->ignore = 1;
            } else {
447
                VIR_DEBUG("passing through variable '%s' ('%s') to remote end",
448
                      var->name, var->value);
449
            }
450
        }
451

452 453
        /* Construct the original name. */
        if (!name) {
454 455 456
            if (conn->uri->scheme &&
                (STREQ(conn->uri->scheme, "remote") ||
                 STRPREFIX(conn->uri->scheme, "remote+"))) {
457 458 459 460 461
                /* Allow remote serve to probe */
                name = strdup("");
            } else {
                xmlURI tmpuri = {
                    .scheme = conn->uri->scheme,
462
#ifdef HAVE_XMLURI_QUERY_RAW
463
                    .query_raw = qparam_get_query (vars),
464
#else
465
                    .query = qparam_get_query (vars),
466
#endif
467 468 469 470 471 472 473 474 475
                    .path = conn->uri->path,
                    .fragment = conn->uri->fragment,
                };

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

477
                name = (char *) xmlSaveUri (&tmpuri);
478

479 480 481 482 483 484 485 486 487 488
#ifdef HAVE_XMLURI_QUERY_RAW
                VIR_FREE(tmpuri.query_raw);
#else
                VIR_FREE(tmpuri.query);
#endif

                /* Restore transport scheme */
                if (transport_str)
                    transport_str[-1] = '+';
            }
489 490
        }

491
        free_qparam_set (vars);
492
        vars = NULL;
493 494 495 496
    } else {
        /* Probe URI server side */
        name = strdup("");
    }
497

498
    if (!name) {
499
        virReportOOMError();
500
        goto failed;
501 502
    }

503
    VIR_DEBUG("proceeding with name = %s", name);
504

505 506
    /* For ext transport, command is required. */
    if (transport == trans_ext && !command) {
507 508
        remoteError(VIR_ERR_INVALID_ARG, "%s",
                    _("remote_open: for 'ext' transport, command is required"));
509 510 511
        goto failed;
    }

512 513

    VIR_DEBUG("Connecting with transport %d", transport);
514 515 516
    /* Connect to the remote service. */
    switch (transport) {
    case trans_tls:
517 518
        priv->tls = virNetTLSContextNewClientPath(pkipath,
                                                  geteuid() != 0 ? true : false,
519
                                                  sanity, verify);
520 521
        if (!priv->tls)
            goto failed;
522
        priv->is_secure = 1;
523 524

        /*FALLTHROUGH*/
525 526 527
    case trans_tcp:
        priv->client = virNetClientNewTCP(priv->hostname, port);
        if (!priv->client)
528 529
            goto failed;

530 531 532 533
        if (priv->tls) {
            VIR_DEBUG("Starting TLS session");
            if (virNetClientSetTLSSession(priv->client, priv->tls) < 0)
                goto failed;
534 535 536 537
        }

        break;

538
#ifndef WIN32
539
    case trans_unix:
540
        if (!sockname) {
541
            if (flags & VIR_DRV_OPEN_REMOTE_USER) {
542
                char *userdir = virGetUserDirectory(getuid());
543

544
                if (!userdir)
545
                    goto failed;
546

547 548
                if (virAsprintf(&sockname, "@%s" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) {
                    VIR_FREE(userdir);
549
                    goto out_of_memory;
550 551
                }
                VIR_FREE(userdir);
552
            } else {
553
                if (flags & VIR_DRV_OPEN_REMOTE_RO)
554
                    sockname = strdup(LIBVIRTD_PRIV_UNIX_SOCKET_RO);
555
                else
556
                    sockname = strdup(LIBVIRTD_PRIV_UNIX_SOCKET);
557 558
                if (sockname == NULL)
                    goto out_of_memory;
559
            }
560
            VIR_DEBUG("Proceeding with sockname %s", sockname);
561 562
        }

563 564 565
        if (!(priv->client = virNetClientNewUNIX(sockname,
                                                 flags & VIR_DRV_OPEN_REMOTE_AUTOSTART,
                                                 remoteFindDaemonPath())))
C
Chris Lalancette 已提交
566
            goto failed;
567

568
        priv->is_secure = 1;
569 570
        break;

571 572 573 574
    case trans_ssh:
        command = command ? command : strdup ("ssh");
        if (command == NULL)
            goto out_of_memory;
J
Jim Meyering 已提交
575

576 577 578 579 580 581 582 583 584 585 586 587 588
        if (!sockname) {
            if (flags & VIR_DRV_OPEN_REMOTE_RO)
                sockname = strdup(LIBVIRTD_PRIV_UNIX_SOCKET_RO);
            else
                sockname = strdup(LIBVIRTD_PRIV_UNIX_SOCKET);
            if (sockname == NULL)
                goto out_of_memory;
        }

        if (!(priv->client = virNetClientNewSSH(priv->hostname,
                                                port,
                                                command,
                                                username,
589 590
                                                !tty,
                                                !verify,
591
                                                netcat ? netcat : "nc",
592
                                                keyfile,
593 594
                                                sockname)))
            goto failed;
595 596

        priv->is_secure = 1;
597
        break;
598 599

    case trans_ext: {
600 601
        char const *cmd_argv[] = { command, NULL };
        if (!(priv->client = virNetClientNewExternal(cmd_argv)))
602 603
            goto failed;

E
Eric Blake 已提交
604
        /* Do not set 'is_secure' flag since we can't guarantee
605 606
         * an external program is secure, and this flag must be
         * pessimistic */
607 608
    }   break;

609 610 611 612 613
#else /* WIN32 */

    case trans_unix:
    case trans_ssh:
    case trans_ext:
614 615 616
        remoteError(VIR_ERR_INVALID_ARG, "%s",
                    _("transport methods unix, ssh and ext are not supported "
                      "under Windows"));
617
        goto failed;
618 619

#endif /* WIN32 */
620 621
    } /* switch (transport) */

622 623 624 625 626
    if (!(priv->remoteProgram = virNetClientProgramNew(REMOTE_PROGRAM,
                                                       REMOTE_PROTOCOL_VERSION,
                                                       remoteDomainEvents,
                                                       ARRAY_CARDINALITY(remoteDomainEvents),
                                                       conn)))
627
        goto failed;
628 629 630 631 632
    if (!(priv->qemuProgram = virNetClientProgramNew(QEMU_PROGRAM,
                                                     QEMU_PROTOCOL_VERSION,
                                                     NULL,
                                                     0,
                                                     NULL)))
633 634
        goto failed;

635 636
    if (virNetClientAddProgram(priv->client, priv->remoteProgram) < 0 ||
        virNetClientAddProgram(priv->client, priv->qemuProgram) < 0)
637
        goto failed;
638 639

    /* Try and authenticate with server */
640 641
    VIR_DEBUG("Trying authentication");
    if (remoteAuthenticate(conn, priv, auth, authtype) == -1)
642 643
        goto failed;

644
    /* Finally we can call the remote side's open function. */
645 646
    {
        remote_open_args args = { &name, flags };
647

648 649
        VIR_DEBUG("Trying to open URI %s", name);
        if (call (conn, priv, 0, REMOTE_PROC_OPEN,
650 651 652 653
                  (xdrproc_t) xdr_remote_open_args, (char *) &args,
                  (xdrproc_t) xdr_void, (char *) NULL) == -1)
            goto failed;
    }
654

655 656 657 658
    /* Now try and find out what URI the daemon used */
    if (conn->uri == NULL) {
        remote_get_uri_ret uriret;

659
        VIR_DEBUG("Trying to query remote URI");
660
        memset (&uriret, 0, sizeof uriret);
661 662 663 664
        if (call (conn, priv, 0,
                  REMOTE_PROC_GET_URI,
                  (xdrproc_t) xdr_void, (char *) NULL,
                  (xdrproc_t) xdr_remote_get_uri_ret, (char *) &uriret) < 0)
665 666
            goto failed;

667
        VIR_DEBUG("Auto-probed URI is %s", uriret.uri);
668 669 670
        conn->uri = xmlParseURI(uriret.uri);
        VIR_FREE(uriret.uri);
        if (!conn->uri) {
671
            virReportOOMError();
672 673 674 675
            goto failed;
        }
    }

676 677 678 679
    if (!(priv->domainEventState = virDomainEventStateNew(remoteDomainEventQueueFlush,
                                                          conn,
                                                          NULL,
                                                          false)))
680 681
        goto failed;

682 683 684
    /* Successful. */
    retcode = VIR_DRV_OPEN_SUCCESS;

685
 cleanup:
686
    /* Free up the URL and strings. */
687 688 689 690 691
    VIR_FREE(name);
    VIR_FREE(command);
    VIR_FREE(sockname);
    VIR_FREE(authtype);
    VIR_FREE(netcat);
692
    VIR_FREE(keyfile);
693 694
    VIR_FREE(username);
    VIR_FREE(port);
695
    VIR_FREE(pkipath);
696 697

    return retcode;
698 699

 out_of_memory:
700
    virReportOOMError();
701 702
    if (vars)
        free_qparam_set (vars);
703 704

 failed:
705
    virNetClientClose(priv->client);
706 707
    virNetClientFree(priv->client);
    priv->client = NULL;
708

709
    VIR_FREE(priv->hostname);
710
    goto cleanup;
711 712
}

713
static struct private_data *
714
remoteAllocPrivateData(void)
715
{
716
    struct private_data *priv;
717
    if (VIR_ALLOC(priv) < 0) {
718
        virReportOOMError();
719
        return NULL;
720 721
    }

722
    if (virMutexInit(&priv->lock) < 0) {
723 724
        remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("cannot initialize mutex"));
725
        VIR_FREE(priv);
726
        return NULL;
727 728 729
    }
    remoteDriverLock(priv);
    priv->localUses = 1;
730 731 732 733 734 735 736

    return priv;
}

static int
remoteOpenSecondaryDriver(virConnectPtr conn,
                          virConnectAuthPtr auth,
E
Eric Blake 已提交
737
                          unsigned int flags,
738 739 740 741 742
                          struct private_data **priv)
{
    int ret;
    int rflags = 0;

743
    if (!((*priv) = remoteAllocPrivateData()))
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
        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
remoteOpen (virConnectPtr conn,
            virConnectAuthPtr auth,
764
            unsigned int flags)
765 766 767
{
    struct private_data *priv;
    int ret, rflags = 0;
768
    const char *autostart = getenv("LIBVIRT_AUTOSTART");
769

770
    if (inside_daemon && (!conn->uri || (conn->uri && !conn->uri->server)))
771 772
        return VIR_DRV_OPEN_DECLINED;

773
    if (!(priv = remoteAllocPrivateData()))
774
        return VIR_DRV_OPEN_ERROR;
775

776
    if (flags & VIR_CONNECT_RO)
777 778
        rflags |= VIR_DRV_OPEN_REMOTE_RO;

779 780 781 782 783 784 785 786 787
    /*
     * 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 已提交
788
        conn->uri->scheme &&
789 790
        ((strchr(conn->uri->scheme, '+') == 0)||
         (strstr(conn->uri->scheme, "+unix") != NULL)) &&
791 792
        (STREQ(conn->uri->path, "/session") ||
         STRPREFIX(conn->uri->scheme, "test+")) &&
793
        getuid() > 0) {
794
        VIR_DEBUG("Auto-spawn user daemon instance");
795
        rflags |= VIR_DRV_OPEN_REMOTE_USER;
796 797 798
        if (!autostart ||
            STRNEQ(autostart, "0"))
            rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
799 800 801
    }

    /*
J
John Levon 已提交
802 803 804 805
     * 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.
806 807
     */
    if (!conn->uri) {
808
        VIR_DEBUG("Auto-probe remote URI");
J
John Levon 已提交
809
#ifndef __sun
810
        if (getuid() > 0) {
811
            VIR_DEBUG("Auto-spawn user daemon instance");
812
            rflags |= VIR_DRV_OPEN_REMOTE_USER;
813 814 815
            if (!autostart ||
                STRNEQ(autostart, "0"))
                rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
816
        }
J
John Levon 已提交
817
#endif
818
    }
819

820
    ret = doRemoteOpen(conn, priv, auth, rflags);
821 822
    if (ret != VIR_DRV_OPEN_SUCCESS) {
        conn->privateData = NULL;
823
        remoteDriverUnlock(priv);
824
        VIR_FREE(priv);
825 826
    } else {
        conn->privateData = priv;
827
        remoteDriverUnlock(priv);
828 829 830 831 832
    }
    return ret;
}


833 834 835 836 837 838 839 840
/* In a string "driver+transport" return a pointer to "transport". */
static char *
get_transport_from_scheme (char *scheme)
{
    char *p = strchr (scheme, '+');
    return p ? p+1 : 0;
}

841
/*----------------------------------------------------------------------*/
842

843 844

static int
845
doRemoteClose (virConnectPtr conn, struct private_data *priv)
846
{
847 848
    int ret = 0;

849 850 851
    if (call (conn, priv, 0, REMOTE_PROC_CLOSE,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
852
        ret = -1;
853

854 855
    virNetTLSContextFree(priv->tls);
    priv->tls = NULL;
856
    virNetClientClose(priv->client);
857 858 859 860 861 862 863 864 865 866 867 868 869 870
    virNetClientFree(priv->client);
    priv->client = NULL;
    virNetClientProgramFree(priv->remoteProgram);
    virNetClientProgramFree(priv->qemuProgram);
    priv->remoteProgram = priv->qemuProgram = NULL;

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

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

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

872
    return ret;
873 874
}

875
static int
876
remoteClose (virConnectPtr conn)
877
{
878 879
    int ret = 0;
    struct private_data *priv = conn->privateData;
880

881 882 883 884 885 886 887 888
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        ret = doRemoteClose(conn, priv);
        conn->privateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE (priv);
889
    }
890 891
    if (priv)
        remoteDriverUnlock(priv);
892

893 894
    return ret;
}
895

896

897 898 899 900 901 902 903 904 905 906 907 908 909 910
/* 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 *
remoteType (virConnectPtr conn)
{
    char *rv = NULL;
    remote_get_type_ret ret;
    struct private_data *priv = conn->privateData;
911

912
    remoteDriverLock(priv);
913

914 915 916 917 918
    /* Cached? */
    if (priv->type) {
        rv = priv->type;
        goto done;
    }
919

920 921 922 923 924
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_GET_TYPE,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_get_type_ret, (char *) &ret) == -1)
        goto done;
925

926 927
    /* Stash. */
    rv = priv->type = ret.type;
928

929 930 931 932
done:
    remoteDriverUnlock(priv);
    return rv;
}
933

934 935 936 937 938 939
static int remoteIsSecure(virConnectPtr conn)
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
    remote_is_secure_ret ret;
    remoteDriverLock(priv);
940

941 942 943 944 945
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_IS_SECURE,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_is_secure_ret, (char *) &ret) == -1)
        goto done;
946

947 948 949 950 951 952 953 954 955
    /* 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;
956

957 958 959 960
done:
    remoteDriverUnlock(priv);
    return rv;
}
961

962 963 964 965 966 967 968
static int remoteIsEncrypted(virConnectPtr conn)
{
    int rv = -1;
    int encrypted = 0;
    struct private_data *priv = conn->privateData;
    remote_is_secure_ret ret;
    remoteDriverLock(priv);
969

970 971 972 973 974
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_IS_SECURE,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_is_secure_ret, (char *) &ret) == -1)
        goto done;
975

976 977
    if (virNetClientIsEncrypted(priv->client))
        encrypted = 1;
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993

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

994 995 996
static int
remoteNodeGetCPUStats (virConnectPtr conn,
                       int cpuNum,
997
                       virNodeCPUStatsPtr params, int *nparams,
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
                       unsigned int flags)
{
    int rv = -1;
    remote_node_get_cpu_stats_args args;
    remote_node_get_cpu_stats_ret ret;
    int i = -1;
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

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

    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)
        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) {
        remoteError(VIR_ERR_RPC, "%s",
                    _("remoteNodeGetCPUStats: "
                      "returned number of stats exceeds limit"));
        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) {
            remoteError(VIR_ERR_INTERNAL_ERROR,
                        _("Stats %s too big for destination"),
                        ret.params.params_val[i].field);
            goto cleanup;
        }
        params[i].value = ret.params.params_val[i].value;
    }

    rv = 0;

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

1060 1061 1062
static int
remoteNodeGetMemoryStats (virConnectPtr conn,
                          int cellNum,
1063
                          virNodeMemoryStatsPtr params, int *nparams,
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
                          unsigned int flags)
{
    int rv = -1;
    remote_node_get_memory_stats_args args;
    remote_node_get_memory_stats_ret ret;
    int i = -1;
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

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

    memset (&ret, 0, sizeof ret);
    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)
        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) {
        remoteError(VIR_ERR_RPC, "%s",
                    _("remoteNodeGetMemoryStats: "
                      "returned number of stats exceeds limit"));
        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) {
            remoteError(VIR_ERR_INTERNAL_ERROR,
                        _("Stats %s too big for destination"),
                        ret.params.params_val[i].field);
            goto cleanup;
        }
        params[i].value = ret.params.params_val[i].value;
    }

    rv = 0;

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

1124 1125 1126 1127 1128 1129
static int
remoteNodeGetCellsFreeMemory(virConnectPtr conn,
                            unsigned long long *freeMems,
                            int startCell,
                            int maxCells)
{
1130
    int rv = -1;
1131 1132 1133
    remote_node_get_cells_free_memory_args args;
    remote_node_get_cells_free_memory_ret ret;
    int i;
1134
    struct private_data *priv = conn->privateData;
1135

1136 1137
    remoteDriverLock(priv);

1138
    if (maxCells > REMOTE_NODE_MAX_CELLS) {
1139 1140 1141
        remoteError(VIR_ERR_RPC,
                    _("too many NUMA cells: %d > %d"),
                    maxCells, REMOTE_NODE_MAX_CELLS);
1142
        goto done;
1143 1144 1145
    }

    args.startCell = startCell;
1146
    args.maxcells = maxCells;
1147 1148 1149 1150 1151

    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)
1152
        goto done;
1153

1154 1155
    for (i = 0 ; i < ret.cells.cells_len ; i++)
        freeMems[i] = ret.cells.cells_val[i];
1156 1157 1158

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

1159
    rv = ret.cells.cells_len;
1160 1161

done:
1162
    remoteDriverUnlock(priv);
1163
    return rv;
1164 1165
}

1166 1167 1168
static int
remoteListDomains (virConnectPtr conn, int *ids, int maxids)
{
1169
    int rv = -1;
1170 1171 1172
    int i;
    remote_list_domains_args args;
    remote_list_domains_ret ret;
1173
    struct private_data *priv = conn->privateData;
1174

1175 1176
    remoteDriverLock(priv);

1177
    if (maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
1178 1179 1180
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain IDs: %d > %d"),
                    maxids, REMOTE_DOMAIN_ID_LIST_MAX);
1181
        goto done;
1182 1183 1184 1185 1186 1187 1188
    }
    args.maxids = maxids;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_LIST_DOMAINS,
              (xdrproc_t) xdr_remote_list_domains_args, (char *) &args,
              (xdrproc_t) xdr_remote_list_domains_ret, (char *) &ret) == -1)
1189
        goto done;
1190 1191

    if (ret.ids.ids_len > maxids) {
1192 1193 1194
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain IDs: %d > %d"),
                    ret.ids.ids_len, maxids);
1195
        goto cleanup;
1196 1197 1198 1199 1200
    }

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

1201 1202 1203
    rv = ret.ids.ids_len;

cleanup:
1204 1205
    xdr_free ((xdrproc_t) xdr_remote_list_domains_ret, (char *) &ret);

1206
done:
1207
    remoteDriverUnlock(priv);
1208
    return rv;
1209 1210
}

1211
/* Helper to serialize typed parameters. */
1212
static int
1213 1214
remoteSerializeTypedParameters(virTypedParameterPtr params,
                               int nparams,
1215 1216
                               remote_typed_param **args_params_val,
                               u_int *args_params_len)
1217
{
1218
    int i;
1219
    int rv = -1;
1220
    remote_typed_param *val;
1221

1222 1223
    *args_params_len = nparams;
    if (VIR_ALLOC_N(val, nparams) < 0) {
1224
        virReportOOMError();
1225
        goto cleanup;
1226 1227 1228
    }

    for (i = 0; i < nparams; ++i) {
1229
        /* call() will free this: */
1230 1231
        val[i].field = strdup (params[i].field);
        if (val[i].field == NULL) {
1232
            virReportOOMError();
1233
            goto cleanup;
1234
        }
1235
        val[i].value.type = params[i].type;
1236
        switch (params[i].type) {
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
        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;
1255
        default:
1256 1257 1258
            remoteError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                params[i].type);
            goto cleanup;
1259 1260 1261
        }
    }

1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
    *args_params_val = val;
    val = NULL;
    rv = 0;

cleanup:
    if (val) {
        for (i = 0; i < nparams; i++)
            VIR_FREE(val[i].field);
        VIR_FREE(val);
    }
    return rv;
}

/* Helper to deserialize typed parameters. */
static int
1277 1278
remoteDeserializeTypedParameters(remote_typed_param *ret_params_val,
                                 u_int ret_params_len,
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
                                 int limit,
                                 virTypedParameterPtr params,
                                 int *nparams)
{
    int i;
    int rv = -1;

    /* Check the length of the returned list carefully. */
    if (ret_params_len > limit || ret_params_len > *nparams) {
        remoteError(VIR_ERR_RPC, "%s",
                    _("returned number of parameters exceeds limit"));
        goto cleanup;
    }

    *nparams = ret_params_len;

    /* Deserialise the result. */
    for (i = 0; i < ret_params_len; ++i) {
        if (virStrcpyStatic(params[i].field,
                            ret_params_val[i].field) == NULL) {
            remoteError(VIR_ERR_INTERNAL_ERROR,
                        _("Parameter %s too big for destination"),
                        ret_params_val[i].field);
            goto cleanup;
        }
        params[i].type = ret_params_val[i].value.type;
        switch (params[i].type) {
        case VIR_TYPED_PARAM_INT:
            params[i].value.i =
                ret_params_val[i].value.remote_typed_param_value_u.i;
            break;
        case VIR_TYPED_PARAM_UINT:
            params[i].value.ui =
                ret_params_val[i].value.remote_typed_param_value_u.ui;
            break;
        case VIR_TYPED_PARAM_LLONG:
            params[i].value.l =
                ret_params_val[i].value.remote_typed_param_value_u.l;
            break;
        case VIR_TYPED_PARAM_ULLONG:
            params[i].value.ul =
                ret_params_val[i].value.remote_typed_param_value_u.ul;
            break;
        case VIR_TYPED_PARAM_DOUBLE:
            params[i].value.d =
                ret_params_val[i].value.remote_typed_param_value_u.d;
            break;
        case VIR_TYPED_PARAM_BOOLEAN:
            params[i].value.b =
                ret_params_val[i].value.remote_typed_param_value_u.b;
            break;
        default:
            remoteError(VIR_ERR_RPC, _("unknown parameter type: %d"),
                        params[i].type);
            goto cleanup;
        }
    }

    rv = 0;

cleanup:
    return rv;
}

1343 1344
static int
remoteDomainGetMemoryParameters (virDomainPtr domain,
1345
                                 virTypedParameterPtr params, int *nparams,
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
                                 unsigned int flags)
{
    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);

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

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

1374 1375
    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
1376 1377 1378 1379
                                         REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX,
                                         params,
                                         nparams) < 0)
        goto cleanup;
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390

    rv = 0;

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

1391 1392
static int
remoteDomainGetBlkioParameters (virDomainPtr domain,
1393 1394
                                virTypedParameterPtr params, int *nparams,
                                unsigned int flags)
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
{
    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);

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

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

1422 1423
    if (remoteDeserializeTypedParameters(ret.params.params_val,
                                         ret.params.params_len,
1424 1425 1426 1427
                                         REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX,
                                         params,
                                         nparams) < 0)
        goto cleanup;
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438

    rv = 0;

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

1439
static int
E
Eric Blake 已提交
1440
remoteDomainGetVcpuPinInfo (virDomainPtr domain,
1441 1442 1443 1444 1445 1446 1447
                            int ncpumaps,
                            unsigned char *cpumaps,
                            int maplen,
                            unsigned int flags)
{
    int rv = -1;
    int i;
E
Eric Blake 已提交
1448 1449
    remote_domain_get_vcpu_pin_info_args args;
    remote_domain_get_vcpu_pin_info_ret ret;
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    if (ncpumaps > REMOTE_VCPUINFO_MAX) {
        remoteError(VIR_ERR_RPC,
                    _("vCPU count exceeds maximum: %d > %d"),
                    ncpumaps, REMOTE_VCPUINFO_MAX);
        goto done;
    }

    if (INT_MULTIPLY_OVERFLOW(ncpumaps, maplen) ||
        ncpumaps * maplen > REMOTE_CPUMAPS_MAX) {
        remoteError(VIR_ERR_RPC,
                    _("vCPU map buffer length exceeds maximum: %d > %d"),
                    ncpumaps * maplen, REMOTE_CPUMAPS_MAX);
        goto done;
    }

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

    memset (&ret, 0, sizeof ret);

E
Eric Blake 已提交
1476 1477
    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_VCPU_PIN_INFO,
              (xdrproc_t) xdr_remote_domain_get_vcpu_pin_info_args,
1478
              (char *) &args,
E
Eric Blake 已提交
1479
              (xdrproc_t) xdr_remote_domain_get_vcpu_pin_info_ret,
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
              (char *) &ret) == -1)
        goto done;

    if (ret.num > ncpumaps) {
        remoteError(VIR_ERR_RPC,
                    _("host reports too many vCPUs: %d > %d"),
                    ret.num, ncpumaps);
        goto cleanup;
    }

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

    memset (cpumaps, 0, ncpumaps * maplen);

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

    rv = ret.num;

cleanup:
E
Eric Blake 已提交
1505
    xdr_free ((xdrproc_t) xdr_remote_domain_get_vcpu_pin_info_ret, (char *) &ret);
1506 1507 1508 1509 1510 1511

done:
    remoteDriverUnlock(priv);
    return rv;
}

1512 1513 1514 1515 1516 1517 1518
static int
remoteDomainGetVcpus (virDomainPtr domain,
                      virVcpuInfoPtr info,
                      int maxinfo,
                      unsigned char *cpumaps,
                      int maplen)
{
1519
    int rv = -1;
1520 1521 1522
    int i;
    remote_domain_get_vcpus_args args;
    remote_domain_get_vcpus_ret ret;
1523
    struct private_data *priv = domain->conn->privateData;
1524

1525 1526
    remoteDriverLock(priv);

1527
    if (maxinfo > REMOTE_VCPUINFO_MAX) {
1528 1529 1530
        remoteError(VIR_ERR_RPC,
                    _("vCPU count exceeds maximum: %d > %d"),
                    maxinfo, REMOTE_VCPUINFO_MAX);
1531
        goto done;
1532
    }
1533 1534
    if (INT_MULTIPLY_OVERFLOW(maxinfo, maplen) ||
        maxinfo * maplen > REMOTE_CPUMAPS_MAX) {
1535 1536 1537
        remoteError(VIR_ERR_RPC,
                    _("vCPU map buffer length exceeds maximum: %d > %d"),
                    maxinfo * maplen, REMOTE_CPUMAPS_MAX);
1538
        goto done;
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
    }

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

    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)
1549
        goto done;
1550 1551

    if (ret.info.info_len > maxinfo) {
1552 1553 1554
        remoteError(VIR_ERR_RPC,
                    _("host reports too many vCPUs: %d > %d"),
                    ret.info.info_len, maxinfo);
1555
        goto cleanup;
1556
    }
1557
    if (ret.cpumaps.cpumaps_len > maxinfo * maplen) {
1558 1559 1560
        remoteError(VIR_ERR_RPC,
                    _("host reports map buffer length exceeds maximum: %d > %d"),
                    ret.cpumaps.cpumaps_len, maxinfo * maplen);
1561
        goto cleanup;
1562 1563
    }

1564 1565 1566
    memset (info, 0, sizeof (virVcpuInfo) * maxinfo);
    memset (cpumaps, 0, maxinfo * maplen);

1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
    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];

1577 1578 1579
    rv = ret.info.info_len;

cleanup:
1580
    xdr_free ((xdrproc_t) xdr_remote_domain_get_vcpus_ret, (char *) &ret);
1581 1582

done:
1583
    remoteDriverUnlock(priv);
1584
    return rv;
1585 1586
}

1587 1588 1589 1590 1591 1592
static int
remoteDomainGetSecurityLabel (virDomainPtr domain, virSecurityLabelPtr seclabel)
{
    remote_domain_get_security_label_args args;
    remote_domain_get_security_label_ret ret;
    struct private_data *priv = domain->conn->privateData;
1593 1594 1595
    int rv = -1;

    remoteDriverLock(priv);
1596 1597 1598

    make_nonnull_domain (&args.dom, domain);
    memset (&ret, 0, sizeof ret);
1599 1600
    memset (seclabel, 0, sizeof (*seclabel));

1601 1602 1603
    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) {
1604
        goto done;
1605 1606 1607 1608
    }

    if (ret.label.label_val != NULL) {
        if (strlen (ret.label.label_val) >= sizeof seclabel->label) {
1609 1610
            remoteError(VIR_ERR_RPC, _("security label exceeds maximum: %zd"),
                        sizeof seclabel->label - 1);
A
Alex Jia 已提交
1611
            goto cleanup;
1612 1613 1614 1615 1616
        }
        strcpy (seclabel->label, ret.label.label_val);
        seclabel->enforcing = ret.enforcing;
    }

1617 1618
    rv = 0;

A
Alex Jia 已提交
1619 1620 1621
cleanup:
    xdr_free((xdrproc_t) xdr_remote_domain_get_security_label_ret, (char *)&ret);

1622 1623 1624
done:
    remoteDriverUnlock(priv);
    return rv;
1625 1626
}

1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
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;

    memset(&ret, 0, sizeof ret);
    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;
}

1660 1661 1662 1663 1664
static int
remoteNodeGetSecurityModel (virConnectPtr conn, virSecurityModelPtr secmodel)
{
    remote_node_get_security_model_ret ret;
    struct private_data *priv = conn->privateData;
1665 1666 1667
    int rv = -1;

    remoteDriverLock(priv);
1668 1669

    memset (&ret, 0, sizeof ret);
1670 1671
    memset (secmodel, 0, sizeof (*secmodel));

1672 1673 1674
    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) {
1675
        goto done;
1676 1677 1678 1679
    }

    if (ret.model.model_val != NULL) {
        if (strlen (ret.model.model_val) >= sizeof secmodel->model) {
1680 1681
            remoteError(VIR_ERR_RPC, _("security model exceeds maximum: %zd"),
                        sizeof secmodel->model - 1);
A
Alex Jia 已提交
1682
            goto cleanup;
1683 1684 1685 1686 1687 1688
        }
        strcpy (secmodel->model, ret.model.model_val);
    }

    if (ret.doi.doi_val != NULL) {
        if (strlen (ret.doi.doi_val) >= sizeof secmodel->doi) {
1689 1690
            remoteError(VIR_ERR_RPC, _("security doi exceeds maximum: %zd"),
                        sizeof secmodel->doi - 1);
A
Alex Jia 已提交
1691
            goto cleanup;
1692 1693 1694
        }
        strcpy (secmodel->doi, ret.doi.doi_val);
    }
1695 1696 1697

    rv = 0;

A
Alex Jia 已提交
1698 1699 1700
cleanup:
    xdr_free((xdrproc_t) xdr_remote_node_get_security_model_ret, (char *)&ret);

1701 1702 1703
done:
    remoteDriverUnlock(priv);
    return rv;
1704 1705
}

1706 1707 1708 1709 1710 1711 1712
static int
remoteDomainMigratePrepare (virConnectPtr dconn,
                            char **cookie, int *cookielen,
                            const char *uri_in, char **uri_out,
                            unsigned long flags, const char *dname,
                            unsigned long resource)
{
1713
    int rv = -1;
1714 1715
    remote_domain_migrate_prepare_args args;
    remote_domain_migrate_prepare_ret ret;
1716
    struct private_data *priv = dconn->privateData;
1717

1718 1719
    remoteDriverLock(priv);

1720 1721 1722 1723 1724 1725 1726 1727 1728
    args.uri_in = uri_in == NULL ? NULL : (char **) &uri_in;
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;

    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)
1729
        goto done;
1730 1731 1732 1733 1734 1735 1736 1737

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

1738 1739 1740
    rv = 0;

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

D
Daniel Veillard 已提交
1745 1746 1747 1748 1749 1750 1751 1752
static int
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)
{
1753
    int rv = -1;
D
Daniel Veillard 已提交
1754 1755
    remote_domain_migrate_prepare2_args args;
    remote_domain_migrate_prepare2_ret ret;
1756
    struct private_data *priv = dconn->privateData;
D
Daniel Veillard 已提交
1757

1758 1759
    remoteDriverLock(priv);

D
Daniel Veillard 已提交
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
    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;

    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)
1770
        goto done;
D
Daniel Veillard 已提交
1771 1772

    if (ret.cookie.cookie_len > 0) {
1773 1774 1775 1776 1777
        if (!cookie || !cookielen) {
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("caller ignores cookie or cookielen"));
            goto error;
        }
D
Daniel Veillard 已提交
1778 1779 1780
        *cookie = ret.cookie.cookie_val; /* Caller frees. */
        *cookielen = ret.cookie.cookie_len;
    }
1781 1782 1783 1784 1785 1786
    if (ret.uri_out) {
        if (!uri_out) {
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("caller ignores uri_out"));
            goto error;
        }
D
Daniel Veillard 已提交
1787
        *uri_out = *ret.uri_out; /* Caller frees. */
1788
    }
D
Daniel Veillard 已提交
1789

1790 1791 1792
    rv = 0;

done:
1793
    remoteDriverUnlock(priv);
1794
    return rv;
1795 1796 1797 1798 1799 1800
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 已提交
1801 1802
}

1803 1804 1805
static int
remoteDomainCreate (virDomainPtr domain)
{
1806
    int rv = -1;
1807
    remote_domain_create_args args;
1808 1809
    remote_domain_lookup_by_uuid_args args2;
    remote_domain_lookup_by_uuid_ret ret2;
1810
    struct private_data *priv = domain->conn->privateData;
1811

1812 1813
    remoteDriverLock(priv);

1814 1815 1816 1817 1818
    make_nonnull_domain (&args.dom, domain);

    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)
1819
        goto done;
1820

1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
    /* 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.
     */
    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)
        goto done;

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

1835 1836 1837
    rv = 0;

done:
1838
    remoteDriverUnlock(priv);
1839
    return rv;
1840 1841
}

1842 1843
static char *
remoteDomainGetSchedulerType (virDomainPtr domain, int *nparams)
1844
{
1845 1846 1847
    char *rv = NULL;
    remote_domain_get_scheduler_type_args args;
    remote_domain_get_scheduler_type_ret ret;
1848
    struct private_data *priv = domain->conn->privateData;
1849

1850 1851
    remoteDriverLock(priv);

1852 1853
    make_nonnull_domain (&args.dom, domain);

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

    if (nparams) *nparams = ret.nparams;

    /* Caller frees this. */
1863 1864 1865
    rv = ret.type;

done:
1866
    remoteDriverUnlock(priv);
1867
    return rv;
1868 1869
}

1870 1871 1872
static int
remoteDomainMemoryStats (virDomainPtr domain,
                         struct _virDomainMemoryStat *stats,
1873 1874
                         unsigned int nr_stats,
                         unsigned int flags)
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
{
    int rv = -1;
    remote_domain_memory_stats_args args;
    remote_domain_memory_stats_ret ret;
    struct private_data *priv = domain->conn->privateData;
    unsigned int i;

    remoteDriverLock(priv);

    make_nonnull_domain (&args.dom, domain);
    if (nr_stats > REMOTE_DOMAIN_MEMORY_STATS_MAX) {
1886 1887 1888
        remoteError(VIR_ERR_RPC,
                    _("too many memory stats requested: %d > %d"), nr_stats,
                    REMOTE_DOMAIN_MEMORY_STATS_MAX);
1889 1890 1891
        goto done;
    }
    args.maxStats = nr_stats;
1892
    args.flags = flags;
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
    memset (&ret, 0, sizeof ret);

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

1914 1915 1916 1917 1918 1919 1920 1921
static int
remoteDomainBlockPeek (virDomainPtr domain,
                       const char *path,
                       unsigned long long offset,
                       size_t size,
                       void *buffer,
                       unsigned int flags)
{
1922
    int rv = -1;
1923 1924
    remote_domain_block_peek_args args;
    remote_domain_block_peek_ret ret;
1925
    struct private_data *priv = domain->conn->privateData;
1926

1927 1928
    remoteDriverLock(priv);

1929
    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
1930 1931 1932
        remoteError(VIR_ERR_RPC,
                    _("block peek request too large for remote protocol, %zi > %d"),
                    size, REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX);
1933
        goto done;
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
    }

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

    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)
1948
        goto done;
1949 1950

    if (ret.buffer.buffer_len != size) {
1951 1952
        remoteError(VIR_ERR_RPC, "%s",
                    _("returned buffer is not same size as requested"));
1953
        goto cleanup;
1954 1955 1956
    }

    memcpy (buffer, ret.buffer.buffer_val, size);
1957 1958 1959
    rv = 0;

cleanup:
1960
    VIR_FREE(ret.buffer.buffer_val);
1961

1962
done:
1963
    remoteDriverUnlock(priv);
1964
    return rv;
1965 1966
}

R
Richard W.M. Jones 已提交
1967 1968 1969 1970 1971 1972 1973
static int
remoteDomainMemoryPeek (virDomainPtr domain,
                        unsigned long long offset,
                        size_t size,
                        void *buffer,
                        unsigned int flags)
{
1974
    int rv = -1;
R
Richard W.M. Jones 已提交
1975 1976
    remote_domain_memory_peek_args args;
    remote_domain_memory_peek_ret ret;
1977
    struct private_data *priv = domain->conn->privateData;
R
Richard W.M. Jones 已提交
1978

1979 1980
    remoteDriverLock(priv);

R
Richard W.M. Jones 已提交
1981
    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
1982 1983 1984
        remoteError(VIR_ERR_RPC,
                    _("memory peek request too large for remote protocol, %zi > %d"),
                    size, REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX);
1985
        goto done;
R
Richard W.M. Jones 已提交
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
    }

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

    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)
1999
        goto done;
R
Richard W.M. Jones 已提交
2000 2001

    if (ret.buffer.buffer_len != size) {
2002 2003
        remoteError(VIR_ERR_RPC, "%s",
                    _("returned buffer is not same size as requested"));
2004
        goto cleanup;
R
Richard W.M. Jones 已提交
2005 2006 2007
    }

    memcpy (buffer, ret.buffer.buffer_val, size);
2008 2009 2010
    rv = 0;

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

2013
done:
2014
    remoteDriverUnlock(priv);
2015
    return rv;
R
Richard W.M. Jones 已提交
2016 2017
}

2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
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;
}

2056 2057
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
2058
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
2059
remoteGenericOpen(virConnectPtr conn, virConnectAuthPtr auth,
2060
                  unsigned int flags, void **genericPrivateData)
2061
{
2062 2063 2064
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
2065
    if (conn->driver &&
2066
        STREQ (conn->driver->name, "remote")) {
2067 2068
        struct private_data *priv;

2069
        /* If we're here, the remote driver is already
2070
         * in use due to a) a QEMU uri, or b) a remote
2071
         * URI. So we can re-use existing connection */
2072 2073 2074
        priv = conn->privateData;
        remoteDriverLock(priv);
        priv->localUses++;
2075 2076 2077 2078 2079 2080 2081 2082 2083
        *genericPrivateData = priv;
        remoteDriverUnlock(priv);
        return VIR_DRV_OPEN_SUCCESS;
    } else if (conn->networkDriver &&
               STREQ (conn->networkDriver->name, "remote")) {
        struct private_data *priv = conn->networkPrivateData;
        remoteDriverLock(priv);
        *genericPrivateData = priv;
        priv->localUses++;
2084
        remoteDriverUnlock(priv);
2085
        return VIR_DRV_OPEN_SUCCESS;
2086 2087 2088
    } else {
        /* Using a non-remote driver, so we need to open a
         * new connection for network APIs, forcing it to
2089
         * use the UNIX transport. This handles Xen driver
2090
         * which doesn't have its own impl of the network APIs. */
2091
        struct private_data *priv;
2092 2093
        int ret = remoteOpenSecondaryDriver(conn, auth, flags, &priv);
        *genericPrivateData = priv;
2094 2095
        return ret;
    }
2096 2097 2098
}

static int
2099
remoteGenericClose(virConnectPtr conn, void **genericPrivateData)
2100
{
2101
    int rv = 0;
2102
    struct private_data *priv = *genericPrivateData;
2103

2104 2105 2106 2107
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        rv = doRemoteClose(conn, priv);
2108
        *genericPrivateData = NULL;
2109 2110 2111
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
2112
    }
2113 2114
    if (priv)
        remoteDriverUnlock(priv);
2115
    return rv;
2116 2117
}

2118
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
2119 2120
remoteNetworkOpen(virConnectPtr conn, virConnectAuthPtr auth,
                  unsigned int flags)
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
{
    return remoteGenericOpen(conn, auth, flags, &conn->networkPrivateData);
}

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

D
Daniel Veillard 已提交
2131 2132
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
2133
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
2134 2135
remoteInterfaceOpen(virConnectPtr conn, virConnectAuthPtr auth,
                    unsigned int flags)
D
Daniel Veillard 已提交
2136
{
2137
    return remoteGenericOpen(conn, auth, flags, &conn->interfacePrivateData);
D
Daniel Veillard 已提交
2138 2139 2140
}

static int
2141
remoteInterfaceClose(virConnectPtr conn)
D
Daniel Veillard 已提交
2142
{
2143
    return remoteGenericClose(conn, &conn->interfacePrivateData);
D
Daniel Veillard 已提交
2144 2145
}

2146 2147
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
2148
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
2149 2150
remoteStorageOpen(virConnectPtr conn, virConnectAuthPtr auth,
                  unsigned int flags)
2151
{
2152
    return remoteGenericOpen(conn, auth, flags, &conn->storagePrivateData);
2153 2154 2155
}

static int
2156
remoteStorageClose(virConnectPtr conn)
2157
{
2158
    return remoteGenericClose(conn, &conn->storagePrivateData);
2159 2160
}

2161 2162 2163 2164 2165 2166
static char *
remoteFindStoragePoolSources (virConnectPtr conn,
                              const char *type,
                              const char *srcSpec,
                              unsigned int flags)
{
2167
    char *rv = NULL;
2168 2169
    remote_find_storage_pool_sources_args args;
    remote_find_storage_pool_sources_ret ret;
2170
    struct private_data *priv = conn->storagePrivateData;
2171 2172
    const char *emptyString = "";

2173 2174
    remoteDriverLock(priv);

2175 2176 2177 2178 2179 2180 2181
    args.type = (char*)type;
    /*
     * I'd think the following would work here:
     *    args.srcSpec = (char**)&srcSpec;
     * since srcSpec is a remote_string (not a remote_nonnull_string).
     *
     * But when srcSpec is NULL, this yields:
2182
     *    libvir: Remote error : marshaling args
2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
     *
     * So for now I'm working around this by turning NULL srcSpecs
     * into empty strings.
     */
    args.srcSpec = srcSpec ? (char **)&srcSpec : (char **)&emptyString;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_FIND_STORAGE_POOL_SOURCES,
              (xdrproc_t) xdr_remote_find_storage_pool_sources_args, (char *) &args,
2193
              (xdrproc_t) xdr_remote_find_storage_pool_sources_ret, (char *) &ret) == -1)
2194
        goto done;
2195

2196 2197 2198 2199
    rv = ret.xml;
    ret.xml = NULL; /* To stop xdr_free free'ing it */

    xdr_free ((xdrproc_t) xdr_remote_find_storage_pool_sources_ret, (char *) &ret);
2200 2201

done:
2202
    remoteDriverUnlock(priv);
2203
    return rv;
2204 2205
}

2206 2207
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
2208
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
2209 2210
remoteDevMonOpen(virConnectPtr conn, virConnectAuthPtr auth,
                 unsigned int flags)
2211
{
2212
    return remoteGenericOpen(conn, auth, flags, &conn->devMonPrivateData);
2213 2214
}

2215 2216
static int
remoteDevMonClose(virConnectPtr conn)
2217
{
2218
    return remoteGenericClose(conn, &conn->devMonPrivateData);
2219 2220
}

2221 2222 2223 2224 2225
static int
remoteNodeDeviceDettach (virNodeDevicePtr dev)
{
    int rv = -1;
    remote_node_device_dettach_args args;
2226 2227
    /* This method is unusual in that it uses the HV driver, not the devMon driver
     * hence its use of privateData, instead of devMonPrivateData */
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

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

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteNodeDeviceReAttach (virNodeDevicePtr dev)
{
    int rv = -1;
    remote_node_device_re_attach_args args;
2251 2252
    /* This method is unusual in that it uses the HV driver, not the devMon driver
     * hence its use of privateData, instead of devMonPrivateData */
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

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

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteNodeDeviceReset (virNodeDevicePtr dev)
{
    int rv = -1;
    remote_node_device_reset_args args;
2276 2277
    /* This method is unusual in that it uses the HV driver, not the devMon driver
     * hence its use of privateData, instead of devMonPrivateData */
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
    struct private_data *priv = dev->conn->privateData;

    remoteDriverLock(priv);

    args.name = dev->name;

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

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

2296 2297 2298
/* ------------------------------------------------------------- */

static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
2299 2300
remoteNWFilterOpen(virConnectPtr conn, virConnectAuthPtr auth,
                   unsigned int flags)
2301
{
2302
    return remoteGenericOpen(conn, auth, flags, &conn->nwfilterPrivateData);
2303 2304 2305
}

static int
2306
remoteNWFilterClose(virConnectPtr conn)
2307
{
2308
    return remoteGenericClose(conn, &conn->nwfilterPrivateData);
2309 2310
}

2311 2312
/*----------------------------------------------------------------------*/

2313
static int
E
Eric Blake 已提交
2314 2315
remoteAuthenticate (virConnectPtr conn, struct private_data *priv,
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
2316
                    const char *authtype)
2317 2318
{
    struct remote_auth_list_ret ret;
2319
    int err, type = REMOTE_AUTH_NONE;
2320 2321

    memset(&ret, 0, sizeof ret);
2322
    err = call (conn, priv, 0,
2323 2324 2325
                REMOTE_PROC_AUTH_LIST,
                (xdrproc_t) xdr_void, (char *) NULL,
                (xdrproc_t) xdr_remote_auth_list_ret, (char *) &ret);
2326 2327 2328 2329 2330 2331 2332
    if (err < 0) {
        virErrorPtr verr = virGetLastError();
        if (verr && verr->code == VIR_ERR_NO_SUPPORT) {
            /* Missing RPC - old server - ignore */
            virResetLastError();
            return 0;
        }
2333
        return -1;
2334
    }
2335 2336 2337 2338

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

2339 2340 2341 2342 2343 2344 2345 2346
    if (authtype) {
        int want, i;
        if (STRCASEEQ(authtype, "sasl") ||
            STRCASEEQLEN(authtype, "sasl.", 5)) {
            want = REMOTE_AUTH_SASL;
        } else if (STRCASEEQ(authtype, "polkit")) {
            want = REMOTE_AUTH_POLKIT;
        } else {
2347 2348
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("unknown authentication type %s"), authtype);
2349 2350 2351 2352 2353 2354 2355
            return -1;
        }
        for (i = 0 ; i < ret.types.types_len ; i++) {
            if (ret.types.types_val[i] == want)
                type = want;
        }
        if (type == REMOTE_AUTH_NONE) {
2356 2357 2358
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("requested authentication type %s rejected"),
                        authtype);
2359 2360 2361 2362 2363 2364 2365
            return -1;
        }
    } else {
        type = ret.types.types_val[0];
    }

    switch (type) {
2366
#if HAVE_SASL
2367 2368 2369 2370 2371 2372
    case REMOTE_AUTH_SASL: {
        const char *mech = NULL;
        if (authtype &&
            STRCASEEQLEN(authtype, "sasl.", 5))
            mech = authtype + 5;

2373
        if (remoteAuthSASL(conn, priv, auth, mech) < 0) {
2374
            VIR_FREE(ret.types.types_val);
2375 2376 2377
            return -1;
        }
        break;
2378
    }
2379 2380
#endif

2381 2382
#if HAVE_POLKIT
    case REMOTE_AUTH_POLKIT:
2383
        if (remoteAuthPolkit(conn, priv, auth) < 0) {
2384
            VIR_FREE(ret.types.types_val);
2385 2386 2387 2388 2389
            return -1;
        }
        break;
#endif

2390 2391 2392 2393 2394
    case REMOTE_AUTH_NONE:
        /* Nothing todo, hurrah ! */
        break;

    default:
2395 2396 2397
        remoteError(VIR_ERR_AUTH_FAILED,
                    _("unsupported authentication type %d"),
                    ret.types.types_val[0]);
2398
        VIR_FREE(ret.types.types_val);
2399 2400 2401
        return -1;
    }

2402
    VIR_FREE(ret.types.types_val);
2403 2404 2405 2406 2407 2408 2409

    return 0;
}



#if HAVE_SASL
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
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)
{
2488
    sasl_callback_t *cbs;
2489
    int i, n;
2490
    if (VIR_ALLOC_N(cbs, ncredtype+1) < 0) {
2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
        return NULL;
    }

    for (i = 0, n = 0 ; i < ncredtype ; i++) {
        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
2510
 *
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524
 * 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,
                                     virConnectCredentialPtr *cred)
{
    int ninteract;
    if (!cred)
        return -1;

    for (ninteract = 0 ; interact[ninteract].id != 0 ; ninteract++)
        ; /* empty */

2525
    if (VIR_ALLOC_N(*cred, ninteract) < 0)
2526 2527 2528 2529 2530
        return -1;

    for (ninteract = 0 ; interact[ninteract].id != 0 ; ninteract++) {
        (*cred)[ninteract].type = remoteAuthCredSASL2Vir(interact[ninteract].id);
        if (!(*cred)[ninteract].type) {
2531
            VIR_FREE(*cred);
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549
            return -1;
        }
        if (interact[ninteract].challenge)
            (*cred)[ninteract].challenge = interact[ninteract].challenge;
        (*cred)[ninteract].prompt = interact[ninteract].prompt;
        if (interact[ninteract].defresult)
            (*cred)[ninteract].defresult = interact[ninteract].defresult;
        (*cred)[ninteract].result = NULL;
    }

    return ninteract;
}

static void remoteAuthFreeCredentials(virConnectCredentialPtr cred,
                                      int ncred)
{
    int i;
    for (i = 0 ; i < ncred ; i++)
2550 2551
        VIR_FREE(cred[i].result);
    VIR_FREE(cred);
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
}


/*
 * @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)
{
    int ninteract;
    for (ninteract = 0 ; interact[ninteract].id != 0 ; ninteract++) {
        interact[ninteract].result = cred[ninteract].result;
        interact[ninteract].len = cred[ninteract].resultlen;
    }
}

/* Perform the SASL authentication process
2573 2574
 */
static int
2575
remoteAuthSASL (virConnectPtr conn, struct private_data *priv,
2576
                virConnectAuthPtr auth, const char *wantmech)
2577 2578 2579 2580 2581 2582 2583
{
    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;
2584
    char *serverin = NULL;
2585
    size_t clientoutlen, serverinlen;
2586 2587
    const char *mech;
    int err, complete;
2588
    int ssf;
2589 2590 2591 2592 2593 2594
    sasl_callback_t *saslcb = NULL;
    sasl_interact_t *interact = NULL;
    virConnectCredentialPtr cred = NULL;
    int ncred = 0;
    int ret = -1;
    const char *mechlist;
2595
    virNetSASLContextPtr saslCtxt;
E
Eric Blake 已提交
2596
    virNetSASLSessionPtr sasl = NULL;
2597

2598
    VIR_DEBUG("Client initialize SASL authentication");
2599

2600
    if (!(saslCtxt = virNetSASLContextNewClient()))
2601 2602
        goto cleanup;

2603 2604 2605 2606 2607 2608
    if (auth) {
        if ((saslcb = remoteAuthMakeCallbacks(auth->credtype, auth->ncredtype)) == NULL)
            goto cleanup;
    } else {
        saslcb = NULL;
    }
2609 2610

    /* Setup a handle for being a client */
2611 2612 2613 2614 2615 2616
    if (!(sasl = virNetSASLSessionNewClient(saslCtxt,
                                            "libvirt",
                                            priv->hostname,
                                            virNetClientLocalAddrString(priv->client),
                                            virNetClientRemoteAddrString(priv->client),
                                            saslcb)))
2617
        goto cleanup;
2618

2619
    /* Initialize some connection props we care about */
2620 2621
    if (priv->tls) {
        if ((ssf = virNetClientGetTLSKeySize(priv->client)) < 0)
2622
            goto cleanup;
2623

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

2626
        VIR_DEBUG("Setting external SSF %d", ssf);
2627
        if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
2628
            goto cleanup;
2629 2630
    }

2631 2632
    /* 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 */
2633 2634 2635 2636
    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)
2637
        goto cleanup;
2638

2639 2640
    /* First call is to inquire about supported mechanisms in the server */
    memset (&iret, 0, sizeof iret);
2641
    if (call (conn, priv, 0, REMOTE_PROC_AUTH_SASL_INIT,
2642
              (xdrproc_t) xdr_void, (char *)NULL,
2643 2644
              (xdrproc_t) xdr_remote_auth_sasl_init_ret, (char *) &iret) != 0)
        goto cleanup;
2645 2646


2647 2648 2649
    mechlist = iret.mechlist;
    if (wantmech) {
        if (strstr(mechlist, wantmech) == NULL) {
2650 2651 2652
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("SASL mechanism %s not supported by server"),
                        wantmech);
2653
            VIR_FREE(iret.mechlist);
2654 2655 2656 2657 2658
            goto cleanup;
        }
        mechlist = wantmech;
    }
 restart:
2659
    /* Start the auth negotiation on the client end first */
2660
    VIR_DEBUG("Client start negotiation mechlist '%s'", mechlist);
2661 2662 2663 2664 2665 2666
    if ((err = virNetSASLSessionClientStart(sasl,
                                            mechlist,
                                            &interact,
                                            &clientout,
                                            &clientoutlen,
                                            &mech)) < 0)
2667 2668 2669
        goto cleanup;

    /* Need to gather some credentials from the client */
2670
    if (err == VIR_NET_SASL_INTERACT) {
2671
        const char *msg;
2672 2673 2674 2675
        if (cred) {
            remoteAuthFreeCredentials(cred, ncred);
            cred = NULL;
        }
2676 2677 2678
        if ((ncred = remoteAuthMakeCredentials(interact, &cred)) < 0) {
            remoteError(VIR_ERR_AUTH_FAILED, "%s",
                        _("Failed to make auth credentials"));
2679
            VIR_FREE(iret.mechlist);
2680 2681 2682
            goto cleanup;
        }
        /* Run the authentication callback */
2683
        if (auth && auth->cb) {
2684 2685 2686
            if ((*(auth->cb))(cred, ncred, auth->cbdata) >= 0) {
                remoteAuthFillInteract(cred, interact);
                goto restart;
2687
            }
2688
            msg = "Failed to collect auth credentials";
2689
        } else {
2690
            msg = "No authentication callback available";
2691
        }
2692
        remoteError(VIR_ERR_AUTH_FAILED, "%s", msg);
2693
        goto cleanup;
2694
    }
2695
    VIR_FREE(iret.mechlist);
2696 2697

    if (clientoutlen > REMOTE_AUTH_SASL_DATA_MAX) {
2698
        remoteError(VIR_ERR_AUTH_FAILED,
2699
                    _("SASL negotiation data too long: %zu bytes"),
2700
                    clientoutlen);
2701
        goto cleanup;
2702 2703 2704 2705 2706 2707 2708
    }
    /* NB, distinction of NULL vs "" is *critical* in SASL */
    memset(&sargs, 0, sizeof sargs);
    sargs.nil = clientout ? 0 : 1;
    sargs.data.data_val = (char*)clientout;
    sargs.data.data_len = clientoutlen;
    sargs.mech = (char*)mech;
2709 2710
    VIR_DEBUG("Server start negotiation with mech %s. Data %zu bytes %p",
              mech, clientoutlen, clientout);
2711 2712 2713

    /* Now send the initial auth data to the server */
    memset (&sret, 0, sizeof sret);
2714
    if (call (conn, priv, 0, REMOTE_PROC_AUTH_SASL_START,
2715
              (xdrproc_t) xdr_remote_auth_sasl_start_args, (char *) &sargs,
2716 2717
              (xdrproc_t) xdr_remote_auth_sasl_start_ret, (char *) &sret) != 0)
        goto cleanup;
2718 2719 2720 2721 2722

    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;
2723 2724
    VIR_DEBUG("Client step result complete: %d. Data %zu bytes %p",
              complete, serverinlen, serverin);
2725 2726 2727

    /* Loop-the-loop...
     * Even if the server has completed, the client must *always* do at least one step
D
Daniel Veillard 已提交
2728
     * in this loop to verify the server isn't lying about something. Mutual auth */
2729
    for (;;) {
2730
    restep:
2731 2732 2733 2734 2735 2736
        if ((err = virNetSASLSessionClientStep(sasl,
                                               serverin,
                                               serverinlen,
                                               &interact,
                                               &clientout,
                                               &clientoutlen)) < 0)
2737
            goto cleanup;
2738

2739
        /* Need to gather some credentials from the client */
2740
        if (err == VIR_NET_SASL_INTERACT) {
2741
            const char *msg;
2742 2743 2744 2745 2746
            if (cred) {
                remoteAuthFreeCredentials(cred, ncred);
                cred = NULL;
            }
            if ((ncred = remoteAuthMakeCredentials(interact, &cred)) < 0) {
2747 2748
                remoteError(VIR_ERR_AUTH_FAILED, "%s",
                            _("Failed to make auth credentials"));
2749 2750 2751
                goto cleanup;
            }
            /* Run the authentication callback */
2752
            if (auth && auth->cb) {
2753 2754 2755
                if ((*(auth->cb))(cred, ncred, auth->cbdata) >= 0) {
                    remoteAuthFillInteract(cred, interact);
                    goto restep;
2756
                }
2757
                msg = _("Failed to collect auth credentials");
2758
            } else {
2759
                msg = _("No authentication callback available");
2760
            }
2761
            remoteError(VIR_ERR_AUTH_FAILED, "%s", msg);
2762
            goto cleanup;
2763 2764
        }

2765
        VIR_FREE(serverin);
2766 2767
        VIR_DEBUG("Client step result %d. Data %zu bytes %p",
                  err, clientoutlen, clientout);
2768 2769

        /* Previous server call showed completion & we're now locally complete too */
2770
        if (complete && err == VIR_NET_SASL_COMPLETE)
2771 2772 2773 2774 2775 2776 2777 2778
            break;

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

        memset (&pret, 0, sizeof pret);
2783
        if (call (conn, priv, 0, REMOTE_PROC_AUTH_SASL_STEP,
2784
                  (xdrproc_t) xdr_remote_auth_sasl_step_args, (char *) &pargs,
2785 2786
                  (xdrproc_t) xdr_remote_auth_sasl_step_ret, (char *) &pret) != 0)
            goto cleanup;
2787 2788 2789 2790 2791 2792

        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;

2793 2794
        VIR_DEBUG("Client step result complete: %d. Data %zu bytes %p",
                  complete, serverinlen, serverin);
2795 2796

        /* This server call shows complete, and earlier client step was OK */
2797
        if (complete && err == VIR_NET_SASL_COMPLETE) {
2798
            VIR_FREE(serverin);
2799 2800 2801 2802
            break;
        }
    }

2803 2804
    /* Check for suitable SSF if not already secure (TLS or UNIX sock) */
    if (!priv->is_secure) {
2805
        if ((ssf = virNetSASLSessionGetKeySize(sasl)) < 0)
2806
            goto cleanup;
2807

2808
        VIR_DEBUG("SASL SSF value %d", ssf);
2809
        if (ssf < 56) { /* 56 == DES level, good for Kerberos */
2810 2811
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("negotiation SSF %d was not strong enough"), ssf);
2812
            goto cleanup;
2813
        }
2814
        priv->is_secure = 1;
2815 2816
    }

2817
    VIR_DEBUG("SASL authentication complete");
2818
    virNetClientSetSASLSession(priv->client, sasl);
2819 2820 2821
    ret = 0;

 cleanup:
2822
    VIR_FREE(serverin);
2823

2824
    VIR_FREE(saslcb);
2825
    remoteAuthFreeCredentials(cred, ncred);
2826 2827
    virNetSASLSessionFree(sasl);
    virNetSASLContextFree(saslCtxt);
2828

2829
    return ret;
2830 2831 2832
}
#endif /* HAVE_SASL */

2833 2834

#if HAVE_POLKIT
2835
# if HAVE_POLKIT1
2836
static int
2837
remoteAuthPolkit (virConnectPtr conn, struct private_data *priv,
2838 2839 2840
                  virConnectAuthPtr auth ATTRIBUTE_UNUSED)
{
    remote_auth_polkit_ret ret;
2841
    VIR_DEBUG("Client initialize PolicyKit-1 authentication");
2842 2843

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

2850
    VIR_DEBUG("PolicyKit-1 authentication complete");
2851 2852
    return 0;
}
2853
# elif HAVE_POLKIT0
2854 2855 2856
/* Perform the PolicyKit authentication process
 */
static int
2857
remoteAuthPolkit (virConnectPtr conn, struct private_data *priv,
2858 2859 2860
                  virConnectAuthPtr auth)
{
    remote_auth_polkit_ret ret;
2861
    int i, allowcb = 0;
2862 2863 2864 2865 2866 2867 2868 2869
    virConnectCredential cred = {
        VIR_CRED_EXTERNAL,
        conn->flags & VIR_CONNECT_RO ? "org.libvirt.unix.monitor" : "org.libvirt.unix.manage",
        "PolicyKit",
        NULL,
        NULL,
        0,
    };
2870
    VIR_DEBUG("Client initialize PolicyKit-0 authentication");
2871

2872
    if (auth && auth->cb) {
2873
        /* Check if the necessary credential type for PolicyKit is supported */
2874 2875 2876 2877
        for (i = 0 ; i < auth->ncredtype ; i++) {
            if (auth->credtype[i] == VIR_CRED_EXTERNAL)
                allowcb = 1;
        }
2878

2879
        if (allowcb) {
2880
            VIR_DEBUG("Client run callback for PolicyKit authentication");
2881 2882
            /* Run the authentication callback */
            if ((*(auth->cb))(&cred, 1, auth->cbdata) < 0) {
2883 2884
                remoteError(VIR_ERR_AUTH_FAILED, "%s",
                            _("Failed to collect auth credentials"));
2885 2886
                return -1;
            }
2887
        } else {
2888
            VIR_DEBUG("Client auth callback does not support PolicyKit");
2889 2890
        }
    } else {
2891
        VIR_DEBUG("No auth callback provided");
2892 2893 2894
    }

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

2901
    VIR_DEBUG("PolicyKit-0 authentication complete");
2902 2903
    return 0;
}
2904
# endif /* HAVE_POLKIT0 */
2905
#endif /* HAVE_POLKIT */
2906 2907
/*----------------------------------------------------------------------*/

2908 2909 2910 2911
static int remoteDomainEventRegister(virConnectPtr conn,
                                     virConnectDomainEventCallback callback,
                                     void *opaque,
                                     virFreeCallback freecb)
2912
{
2913
    int rv = -1;
2914 2915
    struct private_data *priv = conn->privateData;

2916 2917
    remoteDriverLock(priv);

2918
    if (priv->domainEventState->timer < 0) {
2919
         remoteError(VIR_ERR_NO_SUPPORT, "%s", _("no event support"));
2920
         goto done;
2921
    }
2922 2923

    if (virDomainEventCallbackListAdd(conn, priv->domainEventState->callbacks,
2924
                                      callback, opaque, freecb) < 0) {
2925
         remoteError(VIR_ERR_RPC, "%s", _("adding cb to list"));
2926
         goto done;
2927 2928
    }

2929 2930 2931
    if (virDomainEventCallbackListCountID(conn,
                                          priv->domainEventState->callbacks,
                                          VIR_DOMAIN_EVENT_ID_LIFECYCLE) == 1) {
2932 2933 2934 2935
        /* Tell the server when we are the first callback deregistering */
        if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_EVENTS_REGISTER,
                (xdrproc_t) xdr_void, (char *) NULL,
                (xdrproc_t) xdr_void, (char *) NULL) == -1)
2936
            goto done;
2937 2938
    }

2939 2940 2941
    rv = 0;

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

2946 2947
static int remoteDomainEventDeregister(virConnectPtr conn,
                                       virConnectDomainEventCallback callback)
2948 2949
{
    struct private_data *priv = conn->privateData;
2950
    int rv = -1;
2951

2952 2953
    remoteDriverLock(priv);

2954 2955 2956 2957
    if (virDomainEventStateDeregister(conn,
                                      priv->domainEventState,
                                      callback) < 0)
        goto done;
2958

2959 2960 2961
    if (virDomainEventCallbackListCountID(conn,
                                          priv->domainEventState->callbacks,
                                          VIR_DOMAIN_EVENT_ID_LIFECYCLE) == 0) {
2962 2963 2964 2965 2966
        /* Tell the server when we are the last callback deregistering */
        if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_EVENTS_DEREGISTER,
                  (xdrproc_t) xdr_void, (char *) NULL,
                  (xdrproc_t) xdr_void, (char *) NULL) == -1)
            goto done;
2967 2968
    }

2969 2970 2971
    rv = 0;

done:
2972
    remoteDriverUnlock(priv);
2973
    return rv;
2974
}
2975

2976 2977 2978 2979 2980

static void
remoteDomainBuildEventLifecycle(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
2981
{
2982 2983 2984
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_lifecycle_msg *msg = evdata;
2985 2986 2987
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

2988
    dom = get_nonnull_domain(conn, msg->dom);
2989
    if (!dom)
2990
        return;
2991

2992
    event = virDomainEventNewFromDom(dom, msg->event, msg->detail);
2993
    virDomainFree(dom);
2994 2995

    remoteDomainEventQueue(priv, event);
2996 2997 2998
}


2999 3000 3001 3002
static void
remoteDomainBuildEventReboot(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                             virNetClientPtr client ATTRIBUTE_UNUSED,
                             void *evdata, void *opaque)
3003
{
3004 3005 3006
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_reboot_msg *msg = evdata;
3007 3008 3009
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

3010
    dom = get_nonnull_domain(conn, msg->dom);
3011
    if (!dom)
3012
        return;
3013 3014 3015

    event = virDomainEventRebootNewFromDom(dom);
    virDomainFree(dom);
3016 3017

    remoteDomainEventQueue(priv, event);
3018 3019 3020
}


3021 3022 3023 3024
static void
remoteDomainBuildEventRTCChange(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                virNetClientPtr client ATTRIBUTE_UNUSED,
                                void *evdata, void *opaque)
3025
{
3026 3027 3028
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_rtc_change_msg *msg = evdata;
3029 3030 3031
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

3032
    dom = get_nonnull_domain(conn, msg->dom);
3033
    if (!dom)
3034
        return;
3035

3036
    event = virDomainEventRTCChangeNewFromDom(dom, msg->offset);
3037
    virDomainFree(dom);
3038 3039

    remoteDomainEventQueue(priv, event);
3040 3041 3042
}


3043 3044 3045 3046
static void
remoteDomainBuildEventWatchdog(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
3047
{
3048 3049 3050
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_watchdog_msg *msg = evdata;
3051 3052 3053
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

3054
    dom = get_nonnull_domain(conn, msg->dom);
3055
    if (!dom)
3056
        return;
3057

3058
    event = virDomainEventWatchdogNewFromDom(dom, msg->action);
3059
    virDomainFree(dom);
3060 3061

    remoteDomainEventQueue(priv, event);
3062 3063 3064
}


3065 3066 3067 3068
static void
remoteDomainBuildEventIOError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                              virNetClientPtr client ATTRIBUTE_UNUSED,
                              void *evdata, void *opaque)
3069
{
3070 3071 3072
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_io_error_msg *msg = evdata;
3073 3074 3075
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

3076
    dom = get_nonnull_domain(conn, msg->dom);
3077
    if (!dom)
3078
        return;
3079 3080

    event = virDomainEventIOErrorNewFromDom(dom,
3081 3082 3083
                                            msg->srcPath,
                                            msg->devAlias,
                                            msg->action);
3084
    virDomainFree(dom);
3085 3086

    remoteDomainEventQueue(priv, event);
3087 3088 3089
}


3090 3091 3092 3093
static void
remoteDomainBuildEventIOErrorReason(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                    virNetClientPtr client ATTRIBUTE_UNUSED,
                                    void *evdata, void *opaque)
3094
{
3095 3096 3097
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_io_error_reason_msg *msg = evdata;
3098 3099 3100
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

3101
    dom = get_nonnull_domain(conn,msg->dom);
3102
    if (!dom)
3103
        return;
3104 3105

    event = virDomainEventIOErrorReasonNewFromDom(dom,
3106 3107 3108 3109
                                                  msg->srcPath,
                                                  msg->devAlias,
                                                  msg->action,
                                                  msg->reason);
3110 3111

    virDomainFree(dom);
3112 3113

    remoteDomainEventQueue(priv, event);
3114 3115
}

3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137
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);
}
3138

3139 3140 3141 3142
static void
remoteDomainBuildEventGraphics(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                               virNetClientPtr client ATTRIBUTE_UNUSED,
                               void *evdata, void *opaque)
3143
{
3144 3145 3146
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_graphics_msg *msg = evdata;
3147 3148 3149 3150 3151 3152 3153
    virDomainPtr dom;
    virDomainEventPtr event = NULL;
    virDomainEventGraphicsAddressPtr localAddr = NULL;
    virDomainEventGraphicsAddressPtr remoteAddr = NULL;
    virDomainEventGraphicsSubjectPtr subject = NULL;
    int i;

3154
    dom = get_nonnull_domain(conn, msg->dom);
3155
    if (!dom)
3156
        return;
3157 3158 3159

    if (VIR_ALLOC(localAddr) < 0)
        goto no_memory;
3160 3161 3162
    localAddr->family = msg->local.family;
    if (!(localAddr->service = strdup(msg->local.service)) ||
        !(localAddr->node = strdup(msg->local.node)))
3163 3164 3165 3166
        goto no_memory;

    if (VIR_ALLOC(remoteAddr) < 0)
        goto no_memory;
3167 3168 3169
    remoteAddr->family = msg->remote.family;
    if (!(remoteAddr->service = strdup(msg->remote.service)) ||
        !(remoteAddr->node = strdup(msg->remote.node)))
3170 3171 3172 3173
        goto no_memory;

    if (VIR_ALLOC(subject) < 0)
        goto no_memory;
3174
    if (VIR_ALLOC_N(subject->identities, msg->subject.subject_len) < 0)
3175
        goto no_memory;
3176
    subject->nidentity = msg->subject.subject_len;
3177
    for (i = 0 ; i < subject->nidentity ; i++) {
3178 3179
        if (!(subject->identities[i].type = strdup(msg->subject.subject_val[i].type)) ||
            !(subject->identities[i].name = strdup(msg->subject.subject_val[i].name)))
3180 3181 3182 3183
            goto no_memory;
    }

    event = virDomainEventGraphicsNewFromDom(dom,
3184
                                             msg->phase,
3185 3186
                                             localAddr,
                                             remoteAddr,
3187
                                             msg->authScheme,
3188 3189 3190 3191
                                             subject);

    virDomainFree(dom);

3192 3193
    remoteDomainEventQueue(priv, event);
    return;
3194

3195
no_memory:
3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213
    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) {
        for (i = 0 ; i < subject->nidentity ; i++) {
            VIR_FREE(subject->identities[i].type);
            VIR_FREE(subject->identities[i].name);
        }
        VIR_FREE(subject->identities);
        VIR_FREE(subject);
    }
3214
    return;
3215 3216 3217
}


3218 3219 3220 3221
static void
remoteDomainBuildEventControlError(virNetClientProgramPtr prog ATTRIBUTE_UNUSED,
                                   virNetClientPtr client ATTRIBUTE_UNUSED,
                                   void *evdata, void *opaque)
3222
{
3223 3224 3225
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
    remote_domain_event_control_error_msg *msg = evdata;
3226 3227 3228
    virDomainPtr dom;
    virDomainEventPtr event = NULL;

3229
    dom = get_nonnull_domain(conn, msg->dom);
3230
    if (!dom)
3231
        return;
3232 3233 3234 3235 3236

    event = virDomainEventControlErrorNewFromDom(dom);
    xdr_free ((xdrproc_t) &xdr_remote_domain_event_control_error_msg, (char *) &msg);

    virDomainFree(dom);
3237 3238

    remoteDomainEventQueue(priv, event);
3239 3240 3241
}


J
Jim Meyering 已提交
3242
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
3243 3244
remoteSecretOpen(virConnectPtr conn, virConnectAuthPtr auth,
                 unsigned int flags)
3245
{
3246
    return remoteGenericOpen(conn, auth, flags, &conn->secretPrivateData);
3247 3248 3249 3250 3251
}

static int
remoteSecretClose (virConnectPtr conn)
{
3252
    return remoteGenericClose(conn, &conn->secretPrivateData);
3253 3254 3255 3256
}

static unsigned char *
remoteSecretGetValue (virSecretPtr secret, size_t *value_size,
3257
                      unsigned int flags, unsigned int internalFlags)
3258 3259 3260 3261 3262 3263 3264 3265
{
    unsigned char *rv = NULL;
    remote_secret_get_value_args args;
    remote_secret_get_value_ret ret;
    struct private_data *priv = secret->conn->secretPrivateData;

    remoteDriverLock (priv);

3266 3267 3268 3269 3270 3271
    /* internalFlags intentionally do not go over the wire */
    if (internalFlags) {
        remoteError(VIR_ERR_NO_SUPPORT, "%s", _("no internalFlags support"));
        goto done;
    }

3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288
    make_nonnull_secret (&args.secret, secret);
    args.flags = flags;

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

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

done:
    remoteDriverUnlock (priv);
    return rv;
}

3289 3290 3291 3292 3293 3294

static int
remoteStreamSend(virStreamPtr st,
                 const char *data,
                 size_t nbytes)
{
3295
    VIR_DEBUG("st=%p data=%p nbytes=%zu", st, data, nbytes);
3296
    struct private_data *priv = st->conn->privateData;
3297
    virNetClientStreamPtr privst = st->privateData;
3298 3299 3300 3301
    int rv = -1;

    remoteDriverLock(priv);

3302
    if (virNetClientStreamRaiseError(privst))
3303 3304
        goto cleanup;

3305 3306 3307 3308 3309
    rv = virNetClientStreamSendPacket(privst,
                                      priv->client,
                                      VIR_NET_CONTINUE,
                                      data,
                                      nbytes);
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322

cleanup:
    remoteDriverUnlock(priv);

    return rv;
}


static int
remoteStreamRecv(virStreamPtr st,
                 char *data,
                 size_t nbytes)
{
3323
    VIR_DEBUG("st=%p data=%p nbytes=%zu", st, data, nbytes);
3324
    struct private_data *priv = st->conn->privateData;
3325
    virNetClientStreamPtr privst = st->privateData;
3326 3327 3328 3329
    int rv = -1;

    remoteDriverLock(priv);

3330
    if (virNetClientStreamRaiseError(privst))
3331 3332
        goto cleanup;

3333 3334 3335 3336 3337
    rv = virNetClientStreamRecvPacket(privst,
                                      priv->client,
                                      data,
                                      nbytes,
                                      (st->flags & VIR_STREAM_NONBLOCK));
3338

3339
    VIR_DEBUG("Done %d", rv);
3340 3341 3342 3343 3344 3345 3346

cleanup:
    remoteDriverUnlock(priv);

    return rv;
}

3347 3348 3349 3350 3351 3352
struct remoteStreamCallbackData {
    virStreamPtr st;
    virStreamEventCallback cb;
    void *opaque;
    virFreeCallback ff;
};
3353

3354 3355 3356
static void remoteStreamEventCallback(virNetClientStreamPtr stream ATTRIBUTE_UNUSED,
                                      int events,
                                      void *opaque)
3357
{
3358
    struct remoteStreamCallbackData *cbdata = opaque;
3359

3360
    (cbdata->cb)(cbdata->st, events, cbdata->opaque);
3361 3362 3363
}


3364
static void remoteStreamCallbackFree(void *opaque)
3365
{
3366 3367 3368 3369 3370 3371 3372
    struct remoteStreamCallbackData *cbdata = opaque;

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

    virStreamFree(cbdata->st);
    VIR_FREE(opaque);
3373 3374 3375
}


3376
static int
3377 3378 3379 3380 3381
remoteStreamEventAddCallback(virStreamPtr st,
                             int events,
                             virStreamEventCallback cb,
                             void *opaque,
                             virFreeCallback ff)
3382
{
3383
    struct private_data *priv = st->conn->privateData;
3384
    virNetClientStreamPtr privst = st->privateData;
3385
    int ret = -1;
3386
    struct remoteStreamCallbackData *cbdata;
3387

3388 3389 3390
    if (VIR_ALLOC(cbdata) < 0) {
        virReportOOMError();
        return -1;
3391
    }
3392 3393 3394 3395
    cbdata->cb = cb;
    cbdata->opaque = opaque;
    cbdata->ff = ff;
    cbdata->st = st;
3396 3397
    virStreamRef(st);

3398
    remoteDriverLock(priv);
3399

3400 3401 3402 3403 3404 3405 3406 3407
    if ((ret = virNetClientStreamEventAddCallback(privst,
                                                  events,
                                                  remoteStreamEventCallback,
                                                  cbdata,
                                                  remoteStreamCallbackFree)) < 0) {
        VIR_FREE(cbdata);
        goto cleanup;
    }
3408 3409 3410 3411

cleanup:
    remoteDriverUnlock(priv);
    return ret;
3412 3413
}

3414

3415
static int
3416 3417
remoteStreamEventUpdateCallback(virStreamPtr st,
                                int events)
3418
{
3419
    struct private_data *priv = st->conn->privateData;
3420
    virNetClientStreamPtr privst = st->privateData;
3421 3422 3423 3424
    int ret = -1;

    remoteDriverLock(priv);

3425
    ret = virNetClientStreamEventUpdateCallback(privst, events);
3426

3427 3428 3429
    remoteDriverUnlock(priv);
    return ret;
}
3430 3431 3432


static int
3433
remoteStreamEventRemoveCallback(virStreamPtr st)
3434
{
3435
    struct private_data *priv = st->conn->privateData;
3436
    virNetClientStreamPtr privst = st->privateData;
3437 3438 3439 3440
    int ret = -1;

    remoteDriverLock(priv);

3441
    ret = virNetClientStreamEventRemoveCallback(privst);
3442 3443 3444

    remoteDriverUnlock(priv);
    return ret;
3445 3446
}

3447

3448 3449 3450 3451
static int
remoteStreamFinish(virStreamPtr st)
{
    struct private_data *priv = st->conn->privateData;
3452
    virNetClientStreamPtr privst = st->privateData;
3453 3454 3455 3456
    int ret = -1;

    remoteDriverLock(priv);

3457
    if (virNetClientStreamRaiseError(privst))
3458 3459
        goto cleanup;

3460 3461 3462 3463 3464
    ret = virNetClientStreamSendPacket(privst,
                                       priv->client,
                                       VIR_NET_OK,
                                       NULL,
                                       0);
3465 3466

cleanup:
3467 3468 3469 3470
    virNetClientRemoveStream(priv->client, privst);
    virNetClientStreamFree(privst);
    st->privateData = NULL;
    st->driver = NULL;
3471 3472 3473 3474 3475

    remoteDriverUnlock(priv);
    return ret;
}

3476

3477 3478 3479 3480
static int
remoteStreamAbort(virStreamPtr st)
{
    struct private_data *priv = st->conn->privateData;
3481
    virNetClientStreamPtr privst = st->privateData;
3482 3483 3484 3485
    int ret = -1;

    remoteDriverLock(priv);

3486
    if (virNetClientStreamRaiseError(privst))
3487 3488
        goto cleanup;

3489 3490 3491 3492 3493
    ret = virNetClientStreamSendPacket(privst,
                                       priv->client,
                                       VIR_NET_ERROR,
                                       NULL,
                                       0);
3494 3495

cleanup:
3496 3497 3498 3499
    virNetClientRemoveStream(priv->client, privst);
    virNetClientStreamFree(privst);
    st->privateData = NULL;
    st->driver = NULL;
3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515

    remoteDriverUnlock(priv);
    return ret;
}


static virStreamDriver remoteStreamDrv = {
    .streamRecv = remoteStreamRecv,
    .streamSend = remoteStreamSend,
    .streamFinish = remoteStreamFinish,
    .streamAbort = remoteStreamAbort,
    .streamAddCallback = remoteStreamEventAddCallback,
    .streamUpdateCallback = remoteStreamEventUpdateCallback,
    .streamRemoveCallback = remoteStreamEventRemoveCallback,
};

3516

3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530
static int remoteDomainEventRegisterAny(virConnectPtr conn,
                                        virDomainPtr dom,
                                        int eventID,
                                        virConnectDomainEventGenericCallback callback,
                                        void *opaque,
                                        virFreeCallback freecb)
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
    remote_domain_events_register_any_args args;
    int callbackID;

    remoteDriverLock(priv);

3531
    if (priv->domainEventState->timer < 0) {
3532
         remoteError(VIR_ERR_NO_SUPPORT, "%s", _("no event support"));
3533 3534 3535
         goto done;
    }

3536 3537
    if ((callbackID = virDomainEventCallbackListAddID(conn,
                                                      priv->domainEventState->callbacks,
3538 3539
                                                      dom, eventID,
                                                      callback, opaque, freecb)) < 0) {
3540
         remoteError(VIR_ERR_RPC, "%s", _("adding cb to list"));
3541 3542 3543 3544 3545
         goto done;
    }

    /* If this is the first callback for this eventID, we need to enable
     * events on the server */
3546 3547 3548
    if (virDomainEventCallbackListCountID(conn,
                                          priv->domainEventState->callbacks,
                                          eventID) == 1) {
3549 3550 3551 3552 3553
        args.eventID = eventID;

        if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_EVENTS_REGISTER_ANY,
                  (xdrproc_t) xdr_remote_domain_events_register_any_args, (char *) &args,
                  (xdrproc_t) xdr_void, (char *)NULL) == -1) {
3554 3555 3556
            virDomainEventCallbackListRemoveID(conn,
                                               priv->domainEventState->callbacks,
                                               callbackID);
3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578
            goto done;
        }
    }

    rv = callbackID;

done:
    remoteDriverUnlock(priv);
    return rv;
}


static int remoteDomainEventDeregisterAny(virConnectPtr conn,
                                          int callbackID)
{
    struct private_data *priv = conn->privateData;
    int rv = -1;
    remote_domain_events_deregister_any_args args;
    int eventID;

    remoteDriverLock(priv);

3579 3580 3581
    if ((eventID = virDomainEventCallbackListEventID(conn,
                                                     priv->domainEventState->callbacks,
                                                     callbackID)) < 0) {
3582
        remoteError(VIR_ERR_RPC, _("unable to find callback ID %d"), callbackID);
3583 3584 3585
        goto done;
    }

3586 3587 3588 3589
    if (virDomainEventStateDeregisterAny(conn,
                                         priv->domainEventState,
                                         callbackID) < 0)
        goto done;
3590 3591 3592

    /* If that was the last callback for this eventID, we need to disable
     * events on the server */
3593 3594 3595
    if (virDomainEventCallbackListCountID(conn,
                                          priv->domainEventState->callbacks,
                                          eventID) == 0) {
3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610
        args.eventID = eventID;

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

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

3611

3612 3613
/*----------------------------------------------------------------------*/

C
Chris Lalancette 已提交
3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624
static int
remoteQemuDomainMonitorCommand (virDomainPtr domain, const char *cmd,
                                char **result, unsigned int flags)
{
    int rv = -1;
    qemu_monitor_command_args args;
    qemu_monitor_command_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

3625
    make_nonnull_domain(&args.dom, domain);
C
Chris Lalancette 已提交
3626 3627 3628 3629
    args.cmd = (char *)cmd;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
3630
    if (call (domain->conn, priv, REMOTE_CALL_QEMU, QEMU_PROC_MONITOR_COMMAND,
C
Chris Lalancette 已提交
3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650
              (xdrproc_t) xdr_qemu_monitor_command_args, (char *) &args,
              (xdrproc_t) xdr_qemu_monitor_command_ret, (char *) &ret) == -1)
        goto done;

    *result = strdup(ret.result);
    if (*result == NULL) {
        virReportOOMError();
        goto cleanup;
    }

    rv = 0;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

3651 3652 3653

static char *
remoteDomainMigrateBegin3(virDomainPtr domain,
3654
                          const char *xmlin,
3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671
                          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));

    make_nonnull_domain (&args.dom, domain);
3672
    args.xmlin = xmlin == NULL ? NULL : (char **) &xmlin;
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 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;

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

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
            remoteError(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 */

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;

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

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
            remoteError(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) {
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("caller ignores uri_out"));
            goto error;
        }
        *uri_out = *ret.uri_out; /* Caller frees. */
    }

    rv = 0;

done:
    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;
3788
    virNetClientStreamPtr netst;
3789 3790 3791 3792 3793 3794

    remoteDriverLock(priv);

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

3795
    if (!(netst = virNetClientStreamNew(priv->remoteProgram,
3796
                                        REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL3,
3797
                                        priv->counter)))
3798 3799
        goto done;

3800 3801 3802 3803 3804
    if (virNetClientAddStream(priv->client, netst) < 0) {
        virNetClientStreamFree(netst);
        goto done;
    }

3805
    st->driver = &remoteStreamDrv;
3806
    st->privateData = netst;
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817

    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) {
3818 3819
        virNetClientRemoveStream(priv->client, netst);
        virNetClientStreamFree(netst);
3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846
        goto done;
    }

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
            remoteError(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;

done:
    remoteDriverUnlock(priv);
    return rv;

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


static int
remoteDomainMigratePerform3(virDomainPtr dom,
3847
                            const char *xmlin,
3848 3849 3850 3851
                            const char *cookiein,
                            int cookieinlen,
                            char **cookieout,
                            int *cookieoutlen,
3852
                            const char *dconnuri,
3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869
                            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);

3870
    args.xmlin = xmlin == NULL ? NULL : (char **) &xmlin;
3871 3872 3873 3874
    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;
3875 3876
    args.uri = uri == NULL ? NULL : (char **) &uri;
    args.dconnuri = dconnuri == NULL ? NULL : (char **) &dconnuri;
3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905
    args.resource = resource;

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

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
            remoteError(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;

done:
    remoteDriverUnlock(priv);
    return rv;

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


3906
static virDomainPtr
3907 3908 3909 3910 3911 3912
remoteDomainMigrateFinish3(virConnectPtr dconn,
                           const char *dname,
                           const char *cookiein,
                           int cookieinlen,
                           char **cookieout,
                           int *cookieoutlen,
3913
                           const char *dconnuri,
3914 3915
                           const char *uri,
                           unsigned long flags,
3916
                           int cancelled)
3917 3918 3919 3920
{
    remote_domain_migrate_finish3_args args;
    remote_domain_migrate_finish3_ret ret;
    struct private_data *priv = dconn->privateData;
3921
    virDomainPtr rv = NULL;
3922 3923 3924 3925 3926 3927 3928 3929 3930

    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;
3931 3932
    args.uri = uri == NULL ? NULL : (char **) &uri;
    args.dconnuri = dconnuri == NULL ? NULL : (char **) &dconnuri;
3933 3934 3935 3936 3937 3938 3939 3940
    args.flags = flags;
    args.cancelled = cancelled;

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

3941
    rv = get_nonnull_domain(dconn, ret.dom);
3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000

    if (ret.cookie_out.cookie_out_len > 0) {
        if (!cookieout || !cookieoutlen) {
            remoteError(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_ret, (char *) &ret);

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

    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 (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)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}


4001 4002
#include "remote_client_bodies.h"
#include "qemu_client_bodies.h"
4003

4004 4005 4006 4007 4008
/*
 * Serial a set of arguments into a method call message,
 * send that to the server and wait for reply
 */
static int
4009 4010
call (virConnectPtr conn ATTRIBUTE_UNUSED,
      struct private_data *priv,
E
Eric Blake 已提交
4011
      unsigned int flags,
4012 4013 4014 4015
      int proc_nr,
      xdrproc_t args_filter, char *args,
      xdrproc_t ret_filter, char *ret)
{
4016
    int rv;
4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034
    virNetClientProgramPtr prog = flags & REMOTE_CALL_QEMU ? priv->qemuProgram : priv->remoteProgram;
    int counter = priv->counter++;
    virNetClientPtr client = priv->client;
    priv->localUses++;

    /* 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,
                                 args_filter, args,
                                 ret_filter, ret);
    remoteDriverLock(priv);
    priv->localUses--;
4035

4036
    return rv;
4037 4038 4039
}


4040 4041
static void remoteDomainEventDispatchFunc(virConnectPtr conn,
                                          virDomainEventPtr event,
4042
                                          virConnectDomainEventGenericCallback cb,
4043 4044 4045 4046 4047 4048 4049
                                          void *cbopaque,
                                          void *opaque)
{
    struct private_data *priv = opaque;

    /* Drop the lock whle dispatching, for sake of re-entrancy */
    remoteDriverUnlock(priv);
4050
    VIR_DEBUG("Dispatch event %p %p", event, conn);
4051 4052 4053 4054
    virDomainEventDispatchDefaultFunc(conn, event, cb, cbopaque, NULL);
    remoteDriverLock(priv);
}

4055
static void
4056
remoteDomainEventQueueFlush(int timer ATTRIBUTE_UNUSED, void *opaque)
4057
{
4058 4059 4060 4061
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;


4062
    remoteDriverLock(priv);
4063
    VIR_DEBUG("Event queue flush %p", conn);
4064

4065 4066 4067
    virDomainEventStateFlush(priv->domainEventState,
                             remoteDomainEventDispatchFunc,
                             priv);
4068
    remoteDriverUnlock(priv);
4069 4070
}

4071
static void
4072 4073 4074 4075
remoteDomainEventQueue(struct private_data *priv, virDomainEventPtr event)
{
    virDomainEventStateQueue(priv->domainEventState, event);
}
4076

4077 4078 4079 4080 4081 4082
/* 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
4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096
get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain)
{
    virDomainPtr dom;
    dom = virGetDomain (conn, domain.name, BAD_CAST domain.uuid);
    if (dom) dom->id = domain.id;
    return dom;
}

static virNetworkPtr
get_nonnull_network (virConnectPtr conn, remote_nonnull_network network)
{
    return virGetNetwork (conn, network.name, BAD_CAST network.uuid);
}

D
Daniel Veillard 已提交
4097
static virInterfacePtr
4098
get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
4099
{
4100
    return virGetInterface (conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
4101 4102
}

4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114
static virStoragePoolPtr
get_nonnull_storage_pool (virConnectPtr conn, remote_nonnull_storage_pool pool)
{
    return virGetStoragePool (conn, pool.name, BAD_CAST pool.uuid);
}

static virStorageVolPtr
get_nonnull_storage_vol (virConnectPtr conn, remote_nonnull_storage_vol vol)
{
    return virGetStorageVol (conn, vol.pool, vol.name, vol.key);
}

4115 4116 4117 4118 4119 4120
static virNodeDevicePtr
get_nonnull_node_device (virConnectPtr conn, remote_nonnull_node_device dev)
{
    return virGetNodeDevice(conn, dev.name);
}

4121 4122 4123
static virSecretPtr
get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret)
{
4124
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
4125 4126
}

4127 4128 4129 4130 4131 4132
static virNWFilterPtr
get_nonnull_nwfilter (virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
{
    return virGetNWFilter (conn, nwfilter.name, BAD_CAST nwfilter.uuid);
}

C
Chris Lalancette 已提交
4133 4134 4135 4136 4137 4138
static virDomainSnapshotPtr
get_nonnull_domain_snapshot (virDomainPtr domain, remote_nonnull_domain_snapshot snapshot)
{
    return virGetDomainSnapshot(domain, snapshot.name);
}

4139

4140 4141 4142 4143
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
make_nonnull_domain (remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
{
4144
    dom_dst->id = dom_src->id;
4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155
    dom_dst->name = dom_src->name;
    memcpy (dom_dst->uuid, dom_src->uuid, VIR_UUID_BUFLEN);
}

static void
make_nonnull_network (remote_nonnull_network *net_dst, virNetworkPtr net_src)
{
    net_dst->name = net_src->name;
    memcpy (net_dst->uuid, net_src->uuid, VIR_UUID_BUFLEN);
}

D
Daniel Veillard 已提交
4156 4157 4158 4159 4160 4161 4162 4163
static void
make_nonnull_interface (remote_nonnull_interface *interface_dst,
                        virInterfacePtr interface_src)
{
    interface_dst->name = interface_src->name;
    interface_dst->mac = interface_src->mac;
}

4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178
static void
make_nonnull_storage_pool (remote_nonnull_storage_pool *pool_dst, virStoragePoolPtr pool_src)
{
    pool_dst->name = pool_src->name;
    memcpy (pool_dst->uuid, pool_src->uuid, VIR_UUID_BUFLEN);
}

static void
make_nonnull_storage_vol (remote_nonnull_storage_vol *vol_dst, virStorageVolPtr vol_src)
{
    vol_dst->pool = vol_src->pool;
    vol_dst->name = vol_src->name;
    vol_dst->key = vol_src->key;
}

4179 4180 4181
static void
make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
{
4182
    memcpy (secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
4183 4184
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = secret_src->usageID;
4185 4186
}

4187 4188 4189 4190 4191 4192 4193
static void
make_nonnull_nwfilter (remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src)
{
    nwfilter_dst->name = nwfilter_src->name;
    memcpy (nwfilter_dst->uuid, nwfilter_src->uuid, VIR_UUID_BUFLEN);
}

C
Chris Lalancette 已提交
4194 4195 4196 4197
static void
make_nonnull_domain_snapshot (remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
{
    snapshot_dst->name = snapshot_src->name;
4198
    make_nonnull_domain(&snapshot_dst->dom, snapshot_src->domain);
C
Chris Lalancette 已提交
4199 4200
}

4201 4202
/*----------------------------------------------------------------------*/

4203 4204 4205 4206 4207
unsigned long remoteVersion(void)
{
    return REMOTE_PROTOCOL_VERSION;
}

4208
static virDriver remote_driver = {
4209 4210
    .no = VIR_DRV_REMOTE,
    .name = "remote",
4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232
    .open = remoteOpen, /* 0.3.0 */
    .close = remoteClose, /* 0.3.0 */
    .supports_feature = remoteSupportsFeature, /* 0.3.0 */
    .type = remoteType, /* 0.3.0 */
    .version = remoteGetVersion, /* 0.3.0 */
    .libvirtVersion = remoteGetLibVersion, /* 0.7.3 */
    .getHostname = remoteGetHostname, /* 0.3.0 */
    .getSysinfo = remoteGetSysinfo, /* 0.8.8 */
    .getMaxVcpus = remoteGetMaxVcpus, /* 0.3.0 */
    .nodeGetInfo = remoteNodeGetInfo, /* 0.3.0 */
    .getCapabilities = remoteGetCapabilities, /* 0.3.0 */
    .listDomains = remoteListDomains, /* 0.3.0 */
    .numOfDomains = remoteNumOfDomains, /* 0.3.0 */
    .domainCreateXML = remoteDomainCreateXML, /* 0.3.0 */
    .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 */
    .domainShutdown = remoteDomainShutdown, /* 0.3.0 */
    .domainReboot = remoteDomainReboot, /* 0.3.0 */
    .domainDestroy = remoteDomainDestroy, /* 0.3.0 */
4233
    .domainDestroyFlags = remoteDomainDestroyFlags, /* 0.9.4 */
4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244
    .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 */
    .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 */
4245
    .domainGetControlInfo = remoteDomainGetControlInfo, /* 0.9.3 */
4246
    .domainSave = remoteDomainSave, /* 0.3.0 */
E
Eric Blake 已提交
4247
    .domainSaveFlags = remoteDomainSaveFlags, /* 0.9.4 */
4248
    .domainRestore = remoteDomainRestore, /* 0.3.0 */
E
Eric Blake 已提交
4249
    .domainRestoreFlags = remoteDomainRestoreFlags, /* 0.9.4 */
E
Eric Blake 已提交
4250 4251
    .domainSaveImageGetXMLDesc = remoteDomainSaveImageGetXMLDesc, /* 0.9.4 */
    .domainSaveImageDefineXML = remoteDomainSaveImageDefineXML, /* 0.9.4 */
4252 4253 4254 4255 4256 4257
    .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 */
4258
    .domainPinVcpuFlags = remoteDomainPinVcpuFlags, /* 0.9.3 */
E
Eric Blake 已提交
4259
    .domainGetVcpuPinInfo = remoteDomainGetVcpuPinInfo, /* 0.9.3 */
4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272
    .domainGetVcpus = remoteDomainGetVcpus, /* 0.3.0 */
    .domainGetMaxVcpus = remoteDomainGetMaxVcpus, /* 0.3.0 */
    .domainGetSecurityLabel = remoteDomainGetSecurityLabel, /* 0.6.1 */
    .nodeGetSecurityModel = remoteNodeGetSecurityModel, /* 0.6.1 */
    .domainGetXMLDesc = remoteDomainGetXMLDesc, /* 0.3.0 */
    .domainXMLFromNative = remoteDomainXMLFromNative, /* 0.6.4 */
    .domainXMLToNative = remoteDomainXMLToNative, /* 0.6.4 */
    .listDefinedDomains = remoteListDefinedDomains, /* 0.3.0 */
    .numOfDefinedDomains = remoteNumOfDefinedDomains, /* 0.3.0 */
    .domainCreate = remoteDomainCreate, /* 0.3.0 */
    .domainCreateWithFlags = remoteDomainCreateWithFlags, /* 0.8.2 */
    .domainDefineXML = remoteDomainDefineXML, /* 0.3.0 */
    .domainUndefine = remoteDomainUndefine, /* 0.3.0 */
4273
    .domainUndefineFlags = remoteDomainUndefineFlags, /* 0.9.4 */
4274 4275 4276 4277 4278 4279 4280 4281 4282
    .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 */
4283
    .domainGetSchedulerParametersFlags = remoteDomainGetSchedulerParametersFlags, /* 0.9.2 */
4284
    .domainSetSchedulerParameters = remoteDomainSetSchedulerParameters, /* 0.3.0 */
4285
    .domainSetSchedulerParametersFlags = remoteDomainSetSchedulerParametersFlags, /* 0.9.2 */
4286 4287 4288 4289 4290 4291 4292 4293 4294
    .domainMigratePrepare = remoteDomainMigratePrepare, /* 0.3.2 */
    .domainMigratePerform = remoteDomainMigratePerform, /* 0.3.2 */
    .domainMigrateFinish = remoteDomainMigrateFinish, /* 0.3.2 */
    .domainBlockStats = remoteDomainBlockStats, /* 0.3.2 */
    .domainInterfaceStats = remoteDomainInterfaceStats, /* 0.3.2 */
    .domainMemoryStats = remoteDomainMemoryStats, /* 0.7.5 */
    .domainBlockPeek = remoteDomainBlockPeek, /* 0.4.2 */
    .domainMemoryPeek = remoteDomainMemoryPeek, /* 0.4.2 */
    .domainGetBlockInfo = remoteDomainGetBlockInfo, /* 0.8.1 */
4295
    .nodeGetCPUStats = remoteNodeGetCPUStats, /* 0.9.3 */
4296
    .nodeGetMemoryStats = remoteNodeGetMemoryStats, /* 0.9.3 */
4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332
    .nodeGetCellsFreeMemory = remoteNodeGetCellsFreeMemory, /* 0.3.3 */
    .nodeGetFreeMemory = remoteNodeGetFreeMemory, /* 0.3.3 */
    .domainEventRegister = remoteDomainEventRegister, /* 0.5.0 */
    .domainEventDeregister = remoteDomainEventDeregister, /* 0.5.0 */
    .domainMigratePrepare2 = remoteDomainMigratePrepare2, /* 0.5.0 */
    .domainMigrateFinish2 = remoteDomainMigrateFinish2, /* 0.5.0 */
    .nodeDeviceDettach = remoteNodeDeviceDettach, /* 0.6.1 */
    .nodeDeviceReAttach = remoteNodeDeviceReAttach, /* 0.6.1 */
    .nodeDeviceReset = remoteNodeDeviceReset, /* 0.6.1 */
    .domainMigratePrepareTunnel = remoteDomainMigratePrepareTunnel, /* 0.7.2 */
    .isEncrypted = remoteIsEncrypted, /* 0.7.3 */
    .isSecure = remoteIsSecure, /* 0.7.3 */
    .domainIsActive = remoteDomainIsActive, /* 0.7.3 */
    .domainIsPersistent = remoteDomainIsPersistent, /* 0.7.3 */
    .domainIsUpdated = remoteDomainIsUpdated, /* 0.8.6 */
    .cpuCompare = remoteCPUCompare, /* 0.7.5 */
    .cpuBaseline = remoteCPUBaseline, /* 0.7.7 */
    .domainGetJobInfo = remoteDomainGetJobInfo, /* 0.7.7 */
    .domainAbortJob = remoteDomainAbortJob, /* 0.7.7 */
    .domainMigrateSetMaxDowntime = remoteDomainMigrateSetMaxDowntime, /* 0.8.0 */
    .domainMigrateSetMaxSpeed = remoteDomainMigrateSetMaxSpeed, /* 0.9.0 */
    .domainEventRegisterAny = remoteDomainEventRegisterAny, /* 0.8.0 */
    .domainEventDeregisterAny = remoteDomainEventDeregisterAny, /* 0.8.0 */
    .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 */
    .domainSnapshotLookupByName = remoteDomainSnapshotLookupByName, /* 0.8.0 */
    .domainHasCurrentSnapshot = remoteDomainHasCurrentSnapshot, /* 0.8.0 */
    .domainSnapshotCurrent = remoteDomainSnapshotCurrent, /* 0.8.0 */
    .domainRevertToSnapshot = remoteDomainRevertToSnapshot, /* 0.8.0 */
    .domainSnapshotDelete = remoteDomainSnapshotDelete, /* 0.8.0 */
    .qemuDomainMonitorCommand = remoteQemuDomainMonitorCommand, /* 0.8.3 */
4333
    .qemuDomainAttach = qemuDomainAttach, /* 0.9.4 */
4334 4335
    .domainOpenConsole = remoteDomainOpenConsole, /* 0.8.6 */
    .domainInjectNMI = remoteDomainInjectNMI, /* 0.9.2 */
4336 4337 4338 4339 4340 4341
    .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 */
4342
    .domainSendKey = remoteDomainSendKey, /* 0.9.3 */
4343 4344 4345 4346
    .domainBlockJobAbort = remoteDomainBlockJobAbort, /* 0.9.4 */
    .domainGetBlockJobInfo = remoteDomainGetBlockJobInfo, /* 0.9.4 */
    .domainBlockJobSetSpeed = remoteDomainBlockJobSetSpeed, /* 0.9.4 */
    .domainBlockPull = remoteDomainBlockPull, /* 0.9.4 */
4347 4348 4349
};

static virNetworkDriver network_driver = {
4350
    .name = "remote",
4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369
    .open = remoteNetworkOpen, /* 0.3.0 */
    .close = remoteNetworkClose, /* 0.3.0 */
    .numOfNetworks = remoteNumOfNetworks, /* 0.3.0 */
    .listNetworks = remoteListNetworks, /* 0.3.0 */
    .numOfDefinedNetworks = remoteNumOfDefinedNetworks, /* 0.3.0 */
    .listDefinedNetworks = remoteListDefinedNetworks, /* 0.3.0 */
    .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 */
    .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 */
4370 4371
};

D
Daniel Veillard 已提交
4372 4373
static virInterfaceDriver interface_driver = {
    .name = "remote",
4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387
    .open = remoteInterfaceOpen, /* 0.7.2 */
    .close = remoteInterfaceClose, /* 0.7.2 */
    .numOfInterfaces = remoteNumOfInterfaces, /* 0.7.2 */
    .listInterfaces = remoteListInterfaces, /* 0.7.2 */
    .numOfDefinedInterfaces = remoteNumOfDefinedInterfaces, /* 0.7.2 */
    .listDefinedInterfaces = remoteListDefinedInterfaces, /* 0.7.2 */
    .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 */
4388 4389 4390
    .interfaceChangeBegin = remoteInterfaceChangeBegin, /* 0.9.2 */
    .interfaceChangeCommit = remoteInterfaceChangeCommit, /* 0.9.2 */
    .interfaceChangeRollback = remoteInterfaceChangeRollback, /* 0.9.2 */
D
Daniel Veillard 已提交
4391 4392
};

4393 4394
static virStorageDriver storage_driver = {
    .name = "remote",
4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433
    .open = remoteStorageOpen, /* 0.4.1 */
    .close = remoteStorageClose, /* 0.4.1 */
    .numOfPools = remoteNumOfStoragePools, /* 0.4.1 */
    .listPools = remoteListStoragePools, /* 0.4.1 */
    .numOfDefinedPools = remoteNumOfDefinedStoragePools, /* 0.4.1 */
    .listDefinedPools = remoteListDefinedStoragePools, /* 0.4.1 */
    .findPoolSources = remoteFindStoragePoolSources, /* 0.4.5 */
    .poolLookupByName = remoteStoragePoolLookupByName, /* 0.4.1 */
    .poolLookupByUUID = remoteStoragePoolLookupByUUID, /* 0.4.1 */
    .poolLookupByVolume = remoteStoragePoolLookupByVolume, /* 0.4.1 */
    .poolCreateXML = remoteStoragePoolCreateXML, /* 0.4.1 */
    .poolDefineXML = remoteStoragePoolDefineXML, /* 0.4.1 */
    .poolBuild = remoteStoragePoolBuild, /* 0.4.1 */
    .poolUndefine = remoteStoragePoolUndefine, /* 0.4.1 */
    .poolCreate = remoteStoragePoolCreate, /* 0.4.1 */
    .poolDestroy = remoteStoragePoolDestroy, /* 0.4.1 */
    .poolDelete = remoteStoragePoolDelete, /* 0.4.1 */
    .poolRefresh = remoteStoragePoolRefresh, /* 0.4.1 */
    .poolGetInfo = remoteStoragePoolGetInfo, /* 0.4.1 */
    .poolGetXMLDesc = remoteStoragePoolGetXMLDesc, /* 0.4.1 */
    .poolGetAutostart = remoteStoragePoolGetAutostart, /* 0.4.1 */
    .poolSetAutostart = remoteStoragePoolSetAutostart, /* 0.4.1 */
    .poolNumOfVolumes = remoteStoragePoolNumOfVolumes, /* 0.4.1 */
    .poolListVolumes = remoteStoragePoolListVolumes, /* 0.4.1 */

    .volLookupByName = remoteStorageVolLookupByName, /* 0.4.1 */
    .volLookupByKey = remoteStorageVolLookupByKey, /* 0.4.1 */
    .volLookupByPath = remoteStorageVolLookupByPath, /* 0.4.1 */
    .volCreateXML = remoteStorageVolCreateXML, /* 0.4.1 */
    .volCreateXMLFrom = remoteStorageVolCreateXMLFrom, /* 0.6.4 */
    .volDownload = remoteStorageVolDownload, /* 0.9.0 */
    .volUpload = remoteStorageVolUpload, /* 0.9.0 */
    .volDelete = remoteStorageVolDelete, /* 0.4.1 */
    .volWipe = remoteStorageVolWipe, /* 0.8.0 */
    .volGetInfo = remoteStorageVolGetInfo, /* 0.4.1 */
    .volGetXMLDesc = remoteStorageVolGetXMLDesc, /* 0.4.1 */
    .volGetPath = remoteStorageVolGetPath, /* 0.4.1 */
    .poolIsActive = remoteStoragePoolIsActive, /* 0.7.3 */
    .poolIsPersistent = remoteStoragePoolIsPersistent, /* 0.7.3 */
4434 4435
};

4436 4437
static virSecretDriver secret_driver = {
    .name = "remote",
4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448
    .open = remoteSecretOpen, /* 0.7.1 */
    .close = remoteSecretClose, /* 0.7.1 */
    .numOfSecrets = remoteNumOfSecrets, /* 0.7.1 */
    .listSecrets = remoteListSecrets, /* 0.7.1 */
    .lookupByUUID = remoteSecretLookupByUUID, /* 0.7.1 */
    .lookupByUsage = remoteSecretLookupByUsage, /* 0.7.1 */
    .defineXML = remoteSecretDefineXML, /* 0.7.1 */
    .getXMLDesc = remoteSecretGetXMLDesc, /* 0.7.1 */
    .setValue = remoteSecretSetValue, /* 0.7.1 */
    .getValue = remoteSecretGetValue, /* 0.7.1 */
    .undefine = remoteSecretUndefine /* 0.7.1 */
4449 4450
};

4451 4452
static virDeviceMonitor dev_monitor = {
    .name = "remote",
4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463
    .open = remoteDevMonOpen, /* 0.5.0 */
    .close = remoteDevMonClose, /* 0.5.0 */
    .numOfDevices = remoteNodeNumOfDevices, /* 0.5.0 */
    .listDevices = remoteNodeListDevices, /* 0.5.0 */
    .deviceLookupByName = remoteNodeDeviceLookupByName, /* 0.5.0 */
    .deviceGetXMLDesc = remoteNodeDeviceGetXMLDesc, /* 0.5.0 */
    .deviceGetParent = remoteNodeDeviceGetParent, /* 0.5.0 */
    .deviceNumOfCaps = remoteNodeDeviceNumOfCaps, /* 0.5.0 */
    .deviceListCaps = remoteNodeDeviceListCaps, /* 0.5.0 */
    .deviceCreateXML = remoteNodeDeviceCreateXML, /* 0.6.3 */
    .deviceDestroy = remoteNodeDeviceDestroy /* 0.6.3 */
4464 4465
};

4466 4467
static virNWFilterDriver nwfilter_driver = {
    .name = "remote",
4468 4469 4470 4471 4472 4473 4474 4475 4476
    .open = remoteNWFilterOpen, /* 0.8.0 */
    .close = remoteNWFilterClose, /* 0.8.0 */
    .nwfilterLookupByUUID = remoteNWFilterLookupByUUID, /* 0.8.0 */
    .nwfilterLookupByName = remoteNWFilterLookupByName, /* 0.8.0 */
    .getXMLDesc           = remoteNWFilterGetXMLDesc, /* 0.8.0 */
    .defineXML            = remoteNWFilterDefineXML, /* 0.8.0 */
    .undefine             = remoteNWFilterUndefine, /* 0.8.0 */
    .numOfNWFilters       = remoteNumOfNWFilters, /* 0.8.0 */
    .listNWFilters        = remoteListNWFilters, /* 0.8.0 */
4477 4478
};

4479

A
Atsushi SAKAI 已提交
4480
#ifdef WITH_LIBVIRTD
4481
static virStateDriver state_driver = {
4482
    .name = "Remote",
4483
    .initialize = remoteStartup,
4484
};
A
Atsushi SAKAI 已提交
4485
#endif
4486 4487


4488
/** remoteRegister:
4489 4490
 *
 * Register driver with libvirt driver system.
4491 4492
 *
 * Returns -1 on error.
4493 4494 4495 4496
 */
int
remoteRegister (void)
{
4497
    if (virRegisterDriver (&remote_driver) == -1) return -1;
4498
    if (virRegisterNetworkDriver (&network_driver) == -1) return -1;
D
Daniel Veillard 已提交
4499
    if (virRegisterInterfaceDriver (&interface_driver) == -1) return -1;
4500
    if (virRegisterStorageDriver (&storage_driver) == -1) return -1;
4501
    if (virRegisterDeviceMonitor (&dev_monitor) == -1) return -1;
4502
    if (virRegisterSecretDriver (&secret_driver) == -1) return -1;
4503
    if (virRegisterNWFilterDriver(&nwfilter_driver) == -1) return -1;
A
Atsushi SAKAI 已提交
4504
#ifdef WITH_LIBVIRTD
4505
    if (virRegisterStateDriver (&state_driver) == -1) return -1;
A
Atsushi SAKAI 已提交
4506
#endif
4507 4508 4509

    return 0;
}