remote_driver.c 333.8 KB
Newer Older
1 2 3 4
/*
 * remote_internal.c: driver to provide access to libvirtd running
 *   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 28
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
29
#include <string.h>
30 31 32
#include <assert.h>
#include <signal.h>
#include <sys/types.h>
33 34
#include <sys/stat.h>
#include <fcntl.h>
35
#include <arpa/inet.h>
E
Eric Blake 已提交
36
#include <sys/wait.h>
37

38 39 40 41 42
/* Windows socket compatibility functions. */
#include <errno.h>
#include <sys/socket.h>

#ifndef HAVE_WINSOCK2_H /* Unix & Cygwin. */
43 44 45 46 47 48
# include <sys/un.h>
# include <net/if.h>
# include <netinet/in.h>
# include <netinet/tcp.h>
#endif

49
#ifdef HAVE_PWD_H
50
# include <pwd.h>
51 52 53
#endif

#ifdef HAVE_PATHS_H
54
# include <paths.h>
55 56
#endif

57
#include <rpc/types.h>
58 59 60
#include <rpc/xdr.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
61
#include "gnutls_1_0_compat.h"
62
#if HAVE_SASL
63
# include <sasl/sasl.h>
64
#endif
65 66
#include <libxml/uri.h>

J
Jim Meyering 已提交
67
#include <netdb.h>
68

69 70
#include <poll.h>

71
#include "virterror_internal.h"
72
#include "logging.h"
73
#include "datatypes.h"
74
#include "domain_event.h"
75
#include "driver.h"
76 77
#include "buf.h"
#include "qparams.h"
78
#include "remote_driver.h"
79
#include "remote_protocol.h"
C
Chris Lalancette 已提交
80
#include "qemu_protocol.h"
81
#include "memory.h"
82
#include "util.h"
83
#include "event.h"
84
#include "ignore-value.h"
85
#include "files.h"
86

87 88
#define VIR_FROM_THIS VIR_FROM_REMOTE

89 90
static int inside_daemon = 0;

91 92 93 94 95 96 97 98 99 100 101 102 103
struct remote_thread_call;


enum {
    REMOTE_MODE_WAIT_TX,
    REMOTE_MODE_WAIT_RX,
    REMOTE_MODE_COMPLETE,
    REMOTE_MODE_ERROR,
};

struct remote_thread_call {
    int mode;

104 105
    /* Buffer for outgoing data packet
     * 4 byte length, followed by RPC message header+body */
106 107 108 109 110 111 112 113 114
    char buffer[4 + REMOTE_MESSAGE_MAX];
    unsigned int bufferLength;
    unsigned int bufferOffset;

    unsigned int serial;
    unsigned int proc_nr;

    virCond cond;

115
    int want_reply;
116 117 118 119 120 121 122 123
    xdrproc_t ret_filter;
    char *ret;

    remote_error err;

    struct remote_thread_call *next;
};

124 125 126 127 128 129 130
struct private_stream_data {
    unsigned int has_error : 1;
    remote_error err;

    unsigned int serial;
    unsigned int proc_nr;

131 132 133 134 135 136 137
    virStreamEventCallback cb;
    void *cbOpaque;
    virFreeCallback cbFree;
    int cbEvents;
    int cbTimer;
    int cbDispatch;

138 139 140 141 142 143 144 145 146 147 148 149 150
    /* XXX this is potentially unbounded if the client
     * app has domain events registered, since packets
     * may be read off wire, while app isn't ready to
     * recv them. Figure out how to address this some
     * time....
     */
    char *incoming;
    unsigned int incomingOffset;
    unsigned int incomingLength;

    struct private_stream_data *next;
};

151
struct private_data {
152 153
    virMutex lock;

154
    int sock;                   /* Socket. */
155
    int errfd;                /* File handle connected to remote stderr */
156
    int watch;                  /* File handle watch */
157
    pid_t pid;                  /* PID of tunnel process */
158
    int uses_tls;               /* TLS enabled on socket? */
159
    int is_secure;              /* Secure if TLS or SASL or UNIX sockets */
160 161 162
    gnutls_session_t session;   /* GnuTLS session (if uses_tls != 0). */
    char *type;                 /* Cached return from remoteType. */
    int counter;                /* Generates serial numbers for RPC. */
163
    int localUses;              /* Ref count for private data */
164 165
    char *hostname;             /* Original hostname */
    FILE *debugLog;             /* Debug remote protocol */
166

167 168
#if HAVE_SASL
    sasl_conn_t *saslconn;      /* SASL context */
169

170 171 172
    const char *saslDecoded;
    unsigned int saslDecodedLength;
    unsigned int saslDecodedOffset;
173 174 175 176

    const char *saslEncoded;
    unsigned int saslEncodedLength;
    unsigned int saslEncodedOffset;
177
#endif
178

179 180
    /* Buffer for incoming data packets
     * 4 byte length, followed by RPC message header+body */
181 182 183 184
    char buffer[4 + REMOTE_MESSAGE_MAX];
    unsigned int bufferLength;
    unsigned int bufferOffset;

185 186 187 188 189 190 191
    /* The list of domain event callbacks */
    virDomainEventCallbackListPtr callbackList;
    /* The queue of domain events generated
       during a call / response rpc          */
    virDomainEventQueuePtr domainEvents;
    /* Timer for flushing domainEvents queue */
    int eventFlushTimer;
192 193
    /* Flag if we're in process of dispatching */
    int domainEventDispatching;
194 195 196 197 198 199 200

    /* Self-pipe to wakeup threads waiting in poll() */
    int wakeupSendFD;
    int wakeupReadFD;

    /* List of threads currently waiting for dispatch */
    struct remote_thread_call *waitDispatch;
201 202

    struct private_stream_data *streams;
203 204
};

205
enum {
206
    REMOTE_CALL_IN_OPEN           = (1 << 0),
C
Chris Lalancette 已提交
207
    REMOTE_CALL_QUIET_MISSING_RPC = (1 << 1),
208 209
    REMOTE_CALL_QEMU              = (1 << 2),
    REMOTE_CALL_NONBLOCK          = (1 << 3),
210 211 212
};


213 214 215 216 217 218 219 220 221 222
static void remoteDriverLock(struct private_data *driver)
{
    virMutexLock(&driver->lock);
}

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

223 224 225 226
static int remoteIO(virConnectPtr conn,
                    struct private_data *priv,
                    int flags,
                    struct remote_thread_call *thiscall);
227 228 229 230
static int call (virConnectPtr conn, struct private_data *priv,
                 int flags, int proc_nr,
                 xdrproc_t args_filter, char *args,
                 xdrproc_t ret_filter, char *ret);
231 232
static int remoteAuthenticate (virConnectPtr conn, struct private_data *priv, int in_open,
                               virConnectAuthPtr auth, const char *authtype);
233
#if HAVE_SASL
234 235
static int remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open,
                           virConnectAuthPtr auth, const char *mech);
236
#endif
237
#if HAVE_POLKIT
238 239
static int remoteAuthPolkit (virConnectPtr conn, struct private_data *priv, int in_open,
                             virConnectAuthPtr auth);
240
#endif /* HAVE_POLKIT */
241 242 243

#define remoteError(code, ...)                                    \
    virReportErrorHelper(NULL, VIR_FROM_REMOTE, code, __FILE__,   \
244
                         __FUNCTION__, __LINE__, __VA_ARGS__)
245

246 247
static virDomainPtr get_nonnull_domain (virConnectPtr conn, remote_nonnull_domain domain);
static virNetworkPtr get_nonnull_network (virConnectPtr conn, remote_nonnull_network network);
248
static virNWFilterPtr get_nonnull_nwfilter (virConnectPtr conn, remote_nonnull_nwfilter nwfilter);
249
static virInterfacePtr get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface);
250 251
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);
252
static virNodeDevicePtr get_nonnull_node_device (virConnectPtr conn, remote_nonnull_node_device dev);
253
static virSecretPtr get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret);
C
Chris Lalancette 已提交
254
static virDomainSnapshotPtr get_nonnull_domain_snapshot (virDomainPtr domain, remote_nonnull_domain_snapshot snapshot);
255 256
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 已提交
257
static void make_nonnull_interface (remote_nonnull_interface *interface_dst, virInterfacePtr interface_src);
258 259
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);
260
static void make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src);
261
static void make_nonnull_nwfilter (remote_nonnull_nwfilter *nwfilter_dst, virNWFilterPtr nwfilter_src);
C
Chris Lalancette 已提交
262
static void make_nonnull_domain_snapshot (remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src);
263
void remoteDomainEventFired(int watch, int fd, int event, void *data);
264
void remoteDomainEventQueueFlush(int timer, void *opaque);
265 266 267 268 269 270
/*----------------------------------------------------------------------*/

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

/* GnuTLS functions used by remoteOpen. */
271
static int initialize_gnutls(char *pkipath, int flags);
272
static gnutls_session_t negotiate_gnutls_on_connection (virConnectPtr conn, struct private_data *priv, int no_verify);
273

A
Atsushi SAKAI 已提交
274
#ifdef WITH_LIBVIRTD
275
static int
276
remoteStartup(int privileged ATTRIBUTE_UNUSED)
277 278 279 280 281 282 283
{
    /* Mark that we're inside the daemon so we can avoid
     * re-entering ourselves
     */
    inside_daemon = 1;
    return 0;
}
A
Atsushi SAKAI 已提交
284
#endif
285

286
#ifndef WIN32
287 288 289 290
/**
 * remoteFindServerPath:
 *
 * Tries to find the path to the libvirtd binary.
291
 *
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
 * 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 已提交
309
        if (virFileIsExecutable(serverPaths[i])) {
310 311 312 313 314 315 316 317 318 319 320 321 322
            return serverPaths[i];
        }
    }
    return NULL;
}

/**
 * qemuForkDaemon:
 *
 * Forks and try to launch the libvirtd daemon
 *
 * Returns 0 in case of success or -1 in case of detected error.
 */
323
static int
324
remoteForkDaemon(void)
325 326
{
    const char *daemonPath = remoteFindDaemonPath();
327
    const char *const daemonargs[] = { daemonPath, "--timeout=30", NULL };
328
    pid_t pid;
329 330

    if (!daemonPath) {
331 332
        remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("failed to find libvirtd binary"));
333
        return -1;
334 335
    }

336
    if (virExecDaemonize(daemonargs, NULL, NULL,
337 338
                         &pid, -1, NULL, NULL,
                         VIR_EXEC_CLEAR_CAPS,
339
                         NULL, NULL, NULL) < 0)
340
        return -1;
341

342
    return 0;
343
}
344
#endif
345

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

352

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/*
 * 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
 */
370
static int
371 372 373 374
doRemoteOpen (virConnectPtr conn,
              struct private_data *priv,
              virConnectAuthPtr auth ATTRIBUTE_UNUSED,
              int flags)
375
{
376
    struct qparam_set *vars = NULL;
377
    int wakeupFD[2] = { -1, -1 };
378
    char *transport_str = NULL;
379 380 381 382 383 384 385
    enum {
        trans_tls,
        trans_unix,
        trans_ssh,
        trans_ext,
        trans_tcp,
    } transport;
386

387 388
    /* We handle *ALL*  URIs here. The caller has rejected any
     * URIs we don't care about */
389

390 391 392
    if (conn->uri) {
        if (!conn->uri->scheme) {
            /* This is the ///var/lib/xen/xend-socket local path style */
393
            transport = trans_unix;
394 395
        } else {
            transport_str = get_transport_from_scheme (conn->uri->scheme);
396

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
            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 {
414 415 416
                    remoteError(VIR_ERR_INVALID_ARG, "%s",
                                _("remote_open: transport in URL not recognised "
                                  "(should be tls|unix|ssh|ext|tcp)"));
417 418 419 420 421 422 423
                    return VIR_DRV_OPEN_ERROR;
                }
            }
        }
    } else {
        /* No URI, then must be probing so use UNIX socket */
        transport = trans_unix;
424
    }
425

E
Eric Blake 已提交
426
    /* Local variables which we will initialize. These can
427 428
     * get freed in the failed: path.
     */
429 430
    char *name = NULL, *command = NULL, *sockname = NULL, *netcat = NULL;
    char *port = NULL, *authtype = NULL, *username = NULL;
431
    int no_verify = 0, no_tty = 0;
432
    char **cmd_argv = NULL;
433
    char *pkipath = NULL;
434

435 436 437
    /* Return code from this function, and the private data. */
    int retcode = VIR_DRV_OPEN_ERROR;

438
    /* Remote server defaults to "localhost" if not specified. */
439
    if (conn->uri && conn->uri->port != 0) {
440
        if (virAsprintf(&port, "%d", conn->uri->port) == -1) goto out_of_memory;
441 442 443 444 445 446 447
    } 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
448
        port = NULL; /* Port not used for unix, ext., default for ssh */
449

450

451 452
    priv->hostname = strdup (conn->uri && conn->uri->server ?
                             conn->uri->server : "localhost");
453 454
    if (!priv->hostname)
        goto out_of_memory;
455 456
    if (conn->uri && conn->uri->user) {
        username = strdup (conn->uri->user);
457 458
        if (!username)
            goto out_of_memory;
459 460
    }

461 462 463 464 465
    /* 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).
     */
466 467
    struct qparam *var;
    int i;
468 469
    char *query;

470
    if (conn->uri) {
471
#ifdef HAVE_XMLURI_QUERY_RAW
472
        query = conn->uri->query_raw;
473
#else
474
        query = conn->uri->query;
475
#endif
476 477 478 479 480 481
        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 已提交
482
                VIR_FREE(name);
483 484 485 486
                name = strdup (var->value);
                if (!name) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "command")) {
P
Phil Petty 已提交
487
                VIR_FREE(command);
488 489 490 491
                command = strdup (var->value);
                if (!command) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "socket")) {
P
Phil Petty 已提交
492
                VIR_FREE(sockname);
493 494 495 496
                sockname = strdup (var->value);
                if (!sockname) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "auth")) {
P
Phil Petty 已提交
497
                VIR_FREE(authtype);
498 499 500 501
                authtype = strdup (var->value);
                if (!authtype) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "netcat")) {
P
Phil Petty 已提交
502
                VIR_FREE(netcat);
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
                netcat = strdup (var->value);
                if (!netcat) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "no_verify")) {
                no_verify = atoi (var->value);
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "no_tty")) {
                no_tty = atoi (var->value);
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "debug")) {
                if (var->value &&
                    STRCASEEQ (var->value, "stdout"))
                    priv->debugLog = stdout;
                else
                    priv->debugLog = stderr;
518
            } else if (STRCASEEQ(var->name, "pkipath")) {
P
Phil Petty 已提交
519
                VIR_FREE(pkipath);
520 521 522 523
                pkipath = strdup(var->value);
                if (!pkipath) goto out_of_memory;
                var->ignore = 1;
            } else {
524
                VIR_DEBUG("passing through variable '%s' ('%s') to remote end",
525
                      var->name, var->value);
526
            }
527
        }
528

529 530
        /* Construct the original name. */
        if (!name) {
531 532 533
            if (conn->uri->scheme &&
                (STREQ(conn->uri->scheme, "remote") ||
                 STRPREFIX(conn->uri->scheme, "remote+"))) {
534 535 536 537 538
                /* Allow remote serve to probe */
                name = strdup("");
            } else {
                xmlURI tmpuri = {
                    .scheme = conn->uri->scheme,
539
#ifdef HAVE_XMLURI_QUERY_RAW
540
                    .query_raw = qparam_get_query (vars),
541
#else
542
                    .query = qparam_get_query (vars),
543
#endif
544 545 546 547 548 549 550 551 552
                    .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';
                }
553

554
                name = (char *) xmlSaveUri (&tmpuri);
555

556 557 558 559 560 561 562 563 564 565
#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] = '+';
            }
566 567
        }

568
        free_qparam_set (vars);
569
        vars = NULL;
570 571 572 573
    } else {
        /* Probe URI server side */
        name = strdup("");
    }
574

575
    if (!name) {
576
        virReportOOMError();
577
        goto failed;
578 579
    }

580
    VIR_DEBUG("proceeding with name = %s", name);
581

582 583
    /* For ext transport, command is required. */
    if (transport == trans_ext && !command) {
584 585
        remoteError(VIR_ERR_INVALID_ARG, "%s",
                    _("remote_open: for 'ext' transport, command is required"));
586 587 588
        goto failed;
    }

589 590 591
    /* Connect to the remote service. */
    switch (transport) {
    case trans_tls:
592
        if (initialize_gnutls(pkipath, flags) == -1) goto failed;
593
        priv->uses_tls = 1;
594
        priv->is_secure = 1;
595 596 597

        /*FALLTHROUGH*/
    case trans_tcp: {
598
        /* http://people.redhat.com/drepper/userapi-ipv6.html */
599 600
        struct addrinfo *res, *r;
        struct addrinfo hints;
601
        int saved_errno = EINVAL;
602 603 604
        memset (&hints, 0, sizeof hints);
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_ADDRCONFIG;
605
        int e = getaddrinfo (priv->hostname, port, &hints, &res);
606
        if (e != 0) {
607 608 609
            remoteError(VIR_ERR_SYSTEM_ERROR,
                        _("unable to resolve hostname '%s': %s"),
                        priv->hostname, gai_strerror (e));
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
            goto failed;
        }

        /* Try to connect to each returned address in turn. */
        /* XXX This loop contains a subtle problem.  In the case
         * where a host is accessible over IPv4 and IPv6, it will
         * try the IPv4 and IPv6 addresses in turn.  However it
         * should be able to present different client certificates
         * (because the commonName field in a client cert contains
         * the client IP address, which is different for IPv4 and
         * IPv6).  At the moment we only have a single client
         * certificate, and no way to specify what address family
         * that certificate belongs to.
         */
        for (r = res; r; r = r->ai_next) {
            int no_slow_start = 1;

627 628
            priv->sock = socket (r->ai_family, SOCK_STREAM, 0);
            if (priv->sock == -1) {
J
Jim Meyering 已提交
629
                saved_errno = errno;
630 631 632 633
                continue;
            }

            /* Disable Nagle - Dan Berrange. */
634
            setsockopt (priv->sock,
635 636 637
                        IPPROTO_TCP, TCP_NODELAY, (void *)&no_slow_start,
                        sizeof no_slow_start);

638
            if (connect (priv->sock, r->ai_addr, r->ai_addrlen) == -1) {
J
Jim Meyering 已提交
639
                saved_errno = errno;
640
                VIR_FORCE_CLOSE(priv->sock);
641 642 643
                continue;
            }

644 645
            if (priv->uses_tls) {
                priv->session =
646
                    negotiate_gnutls_on_connection
647
                      (conn, priv, no_verify);
648
                if (!priv->session) {
649
                    VIR_FORCE_CLOSE(priv->sock);
650
                    goto failed;
651 652 653 654 655 656
                }
            }
            goto tcp_connected;
        }

        freeaddrinfo (res);
657
        virReportSystemError(saved_errno,
658
                             _("unable to connect to libvirtd at '%s'"),
659
                             priv->hostname);
660 661 662 663 664
        goto failed;

       tcp_connected:
        freeaddrinfo (res);

665 666
        /* NB. All versioning is done by the RPC headers, so we don't
         * need to worry (at this point anyway) about versioning. */
667 668 669
        break;
    }

670
#ifndef WIN32
671 672
    case trans_unix: {
        if (!sockname) {
673
            if (flags & VIR_DRV_OPEN_REMOTE_USER) {
674
                char *userdir = virGetUserDirectory(getuid());
675

676
                if (!userdir)
677
                    goto failed;
678

679 680
                if (virAsprintf(&sockname, "@%s" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) {
                    VIR_FREE(userdir);
681
                    goto out_of_memory;
682 683
                }
                VIR_FREE(userdir);
684
            } else {
685
                if (flags & VIR_DRV_OPEN_REMOTE_RO)
686 687 688
                    sockname = strdup (LIBVIRTD_PRIV_UNIX_SOCKET_RO);
                else
                    sockname = strdup (LIBVIRTD_PRIV_UNIX_SOCKET);
689 690
                if (sockname == NULL)
                    goto out_of_memory;
691
            }
692 693
        }

694 695 696
# ifndef UNIX_PATH_MAX
#  define UNIX_PATH_MAX(addr) (sizeof (addr).sun_path)
# endif
697
        struct sockaddr_un addr;
698 699
        int trials = 0;

700 701
        memset (&addr, 0, sizeof addr);
        addr.sun_family = AF_UNIX;
C
Chris Lalancette 已提交
702
        if (virStrcpyStatic(addr.sun_path, sockname) == NULL) {
703 704
            remoteError(VIR_ERR_INTERNAL_ERROR,
                        _("Socket %s too big for destination"), sockname);
C
Chris Lalancette 已提交
705 706
            goto failed;
        }
707 708
        if (addr.sun_path[0] == '@')
            addr.sun_path[0] = '\0';
709

710
      autostart_retry:
711
        priv->is_secure = 1;
712 713
        priv->sock = socket (AF_UNIX, SOCK_STREAM, 0);
        if (priv->sock == -1) {
714
            virReportSystemError(errno, "%s",
715
                                 _("unable to create socket"));
716 717
            goto failed;
        }
718 719 720 721 722 723 724 725 726 727
        if (connect (priv->sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
            /* We might have to autostart the daemon in some cases....
             * It takes a short while for the daemon to startup, hence we
             * have a number of retries, with a small sleep. This will
             * sometimes cause multiple daemons to be started - this is
             * ok because the duplicates will fail to bind to the socket
             * and immediately exit, leaving just one daemon.
             */
            if (errno == ECONNREFUSED &&
                flags & VIR_DRV_OPEN_REMOTE_AUTOSTART &&
728
                trials < 20) {
729
                VIR_FORCE_CLOSE(priv->sock);
730
                if (trials > 0 ||
731
                    remoteForkDaemon() == 0) {
732
                    trials++;
733
                    usleep(1000 * 100 * trials);
734 735 736
                    goto autostart_retry;
                }
            }
737
            virReportSystemError(errno,
738 739
              _("unable to connect to '%s', libvirtd may need to be started"),
              sockname);
740 741 742 743 744 745 746
            goto failed;
        }

        break;
    }

    case trans_ssh: {
747
        int j, nr_args = 6;
748 749 750

        if (username) nr_args += 2; /* For -l username */
        if (no_tty) nr_args += 5;   /* For -T -o BatchMode=yes -e none */
751
        if (port) nr_args += 2;     /* For -p port */
752

753
        command = command ? command : strdup ("ssh");
754 755
        if (command == NULL)
            goto out_of_memory;
756

757 758
        /* Generate the final command argv[] array.
         *   ssh [-p $port] [-l $username] $hostname $netcat -U $sockname [NULL] */
759 760
        if (VIR_ALLOC_N(cmd_argv, nr_args) < 0)
            goto out_of_memory;
J
Jim Meyering 已提交
761

762 763
        j = 0;
        cmd_argv[j++] = strdup (command);
764 765 766 767
        if (port) {
            cmd_argv[j++] = strdup ("-p");
            cmd_argv[j++] = strdup (port);
        }
768 769 770 771
        if (username) {
            cmd_argv[j++] = strdup ("-l");
            cmd_argv[j++] = strdup (username);
        }
772 773 774 775 776 777 778
        if (no_tty) {
            cmd_argv[j++] = strdup ("-T");
            cmd_argv[j++] = strdup ("-o");
            cmd_argv[j++] = strdup ("BatchMode=yes");
            cmd_argv[j++] = strdup ("-e");
            cmd_argv[j++] = strdup ("none");
        }
779
        cmd_argv[j++] = strdup (priv->hostname);
780 781
        cmd_argv[j++] = strdup (netcat ? netcat : "nc");
        cmd_argv[j++] = strdup ("-U");
782 783 784 785
        cmd_argv[j++] = strdup (sockname ? sockname :
                                (flags & VIR_CONNECT_RO
                                 ? LIBVIRTD_PRIV_UNIX_SOCKET_RO
                                 : LIBVIRTD_PRIV_UNIX_SOCKET));
786 787
        cmd_argv[j++] = 0;
        assert (j == nr_args);
788 789 790
        for (j = 0; j < (nr_args-1); j++)
            if (cmd_argv[j] == NULL)
                goto out_of_memory;
791 792

        priv->is_secure = 1;
793 794 795 796
    }

        /*FALLTHROUGH*/
    case trans_ext: {
797
        pid_t pid;
798
        int sv[2];
799
        int errfd[2];
800 801 802 803 804 805

        /* Fork off the external process.  Use socketpair to create a private
         * (unnamed) Unix domain socket to the child process so we don't have
         * to faff around with two file descriptors (a la 'pipe(2)').
         */
        if (socketpair (PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
806
            virReportSystemError(errno, "%s",
807
                                 _("unable to create socket pair"));
808 809 810
            goto failed;
        }

811 812 813 814 815 816
        if (pipe(errfd) == -1) {
            virReportSystemError(errno, "%s",
                                 _("unable to create socket pair"));
            goto failed;
        }

817
        if (virExec((const char**)cmd_argv, NULL, NULL,
818
                    &pid, sv[1], &(sv[1]), &(errfd[1]),
819
                    VIR_EXEC_CLEAR_CAPS) < 0)
820 821 822
            goto failed;

        /* Parent continues here. */
823 824
        VIR_FORCE_CLOSE(sv[1]);
        VIR_FORCE_CLOSE(errfd[1]);
825
        priv->sock = sv[0];
826
        priv->errfd = errfd[0];
827
        priv->pid = pid;
828 829 830 831

        /* Do not set 'is_secure' flag since we can't guarentee
         * an external program is secure, and this flag must be
         * pessimistic */
832
    }
833 834 835 836 837
#else /* WIN32 */

    case trans_unix:
    case trans_ssh:
    case trans_ext:
838 839 840
        remoteError(VIR_ERR_INVALID_ARG, "%s",
                    _("transport methods unix, ssh and ext are not supported "
                      "under Windows"));
841
        goto failed;
842 843 844

#endif /* WIN32 */

845 846
    } /* switch (transport) */

847
    if (virSetNonBlock(priv->sock) < 0) {
848
        virReportSystemError(errno, "%s",
849
                             _("unable to make socket non-blocking"));
850 851 852
        goto failed;
    }

853 854 855 856 857 858
    if ((priv->errfd != -1) && virSetNonBlock(priv->errfd) < 0) {
        virReportSystemError(errno, "%s",
                             _("unable to make socket non-blocking"));
        goto failed;
    }

859
    if (pipe(wakeupFD) < 0) {
860
        virReportSystemError(errno, "%s",
861
                             _("unable to make pipe"));
862 863 864 865
        goto failed;
    }
    priv->wakeupReadFD = wakeupFD[0];
    priv->wakeupSendFD = wakeupFD[1];
866 867

    /* Try and authenticate with server */
868
    if (remoteAuthenticate(conn, priv, 1, auth, authtype) == -1)
869 870
        goto failed;

871 872 873
    /* Finally we can call the remote side's open function. */
    remote_open_args args = { &name, flags };

874
    if (call (conn, priv, REMOTE_CALL_IN_OPEN, REMOTE_PROC_OPEN,
875 876 877 878
              (xdrproc_t) xdr_remote_open_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto failed;

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
    /* Now try and find out what URI the daemon used */
    if (conn->uri == NULL) {
        remote_get_uri_ret uriret;
        int urierr;

        memset (&uriret, 0, sizeof uriret);
        urierr = call (conn, priv,
                       REMOTE_CALL_IN_OPEN | REMOTE_CALL_QUIET_MISSING_RPC,
                       REMOTE_PROC_GET_URI,
                       (xdrproc_t) xdr_void, (char *) NULL,
                       (xdrproc_t) xdr_remote_get_uri_ret, (char *) &uriret);
        if (urierr == -2) {
            /* Should not really happen, since we only probe local libvirtd's,
               & the library should always match the daemon. Only case is post
               RPM upgrade where an old daemon instance is still running with
               new client. Too bad. It is not worth the hassle to fix this */
895 896
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("unable to auto-detect URI"));
897 898 899 900 901 902
            goto failed;
        }
        if (urierr == -1) {
            goto failed;
        }

903
        VIR_DEBUG("Auto-probed URI is %s", uriret.uri);
904 905 906
        conn->uri = xmlParseURI(uriret.uri);
        VIR_FREE(uriret.uri);
        if (!conn->uri) {
907
            virReportOOMError();
908 909 910 911
            goto failed;
        }
    }

912
    if(VIR_ALLOC(priv->callbackList)<0) {
913
        virReportOOMError();
914 915 916 917
        goto failed;
    }

    if(VIR_ALLOC(priv->domainEvents)<0) {
918
        virReportOOMError();
919 920 921
        goto failed;
    }

922
    VIR_DEBUG0("Adding Handler for remote events");
923
    /* Set up a callback to listen on the socket data */
924
    if ((priv->watch = virEventAddHandle(priv->sock,
925
                                         VIR_EVENT_HANDLE_READABLE,
926
                                         remoteDomainEventFired,
927
                                         conn, NULL)) < 0) {
928
        VIR_DEBUG0("virEventAddHandle failed: No addHandleImpl defined."
929 930 931
               " continuing without events.");
    } else {

932
        VIR_DEBUG0("Adding Timeout for remote event queue flushing");
933
        if ( (priv->eventFlushTimer = virEventAddTimeout(-1,
934 935
                                                         remoteDomainEventQueueFlush,
                                                         conn, NULL)) < 0) {
936
            VIR_DEBUG0("virEventAddTimeout failed: No addTimeoutImpl defined. "
937
                    "continuing without events.");
938
            virEventRemoveHandle(priv->watch);
939
            priv->watch = -1;
940 941
        }
    }
942 943 944
    /* Successful. */
    retcode = VIR_DRV_OPEN_SUCCESS;

945
 cleanup:
946
    /* Free up the URL and strings. */
947 948 949 950 951 952 953
    VIR_FREE(name);
    VIR_FREE(command);
    VIR_FREE(sockname);
    VIR_FREE(authtype);
    VIR_FREE(netcat);
    VIR_FREE(username);
    VIR_FREE(port);
954 955 956
    if (cmd_argv) {
        char **cmd_argv_ptr = cmd_argv;
        while (*cmd_argv_ptr) {
957
            VIR_FREE(*cmd_argv_ptr);
958 959
            cmd_argv_ptr++;
        }
960
        VIR_FREE(cmd_argv);
961
    }
962
    VIR_FREE(pkipath);
963 964

    return retcode;
965 966

 out_of_memory:
967
    virReportOOMError();
968 969
    if (vars)
        free_qparam_set (vars);
970 971 972

 failed:
    /* Close the socket if we failed. */
973
    VIR_FORCE_CLOSE(priv->errfd);
974

975 976 977 978 979
    if (priv->sock >= 0) {
        if (priv->uses_tls && priv->session) {
            gnutls_bye (priv->session, GNUTLS_SHUT_RDWR);
            gnutls_deinit (priv->session);
        }
980
        VIR_FORCE_CLOSE(priv->sock);
981
#ifndef WIN32
982 983 984
        if (priv->pid > 0) {
            pid_t reap;
            do {
985
retry:
986 987
                reap = waitpid(priv->pid, NULL, 0);
                if (reap == -1 && errno == EINTR)
988
                    goto retry;
989 990
            } while (reap != -1 && reap != priv->pid);
        }
991
#endif
992 993
    }

994 995
    VIR_FORCE_CLOSE(wakeupFD[0]);
    VIR_FORCE_CLOSE(wakeupFD[1]);
996

997
    VIR_FREE(priv->hostname);
998
    goto cleanup;
999 1000
}

1001
static struct private_data *
1002
remoteAllocPrivateData(void)
1003
{
1004
    struct private_data *priv;
1005
    if (VIR_ALLOC(priv) < 0) {
1006
        virReportOOMError();
1007
        return NULL;
1008 1009
    }

1010
    if (virMutexInit(&priv->lock) < 0) {
1011 1012
        remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("cannot initialize mutex"));
1013
        VIR_FREE(priv);
1014
        return NULL;
1015 1016 1017
    }
    remoteDriverLock(priv);
    priv->localUses = 1;
1018
    priv->watch = -1;
1019
    priv->sock = -1;
1020
    priv->errfd = -1;
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033

    return priv;
}

static int
remoteOpenSecondaryDriver(virConnectPtr conn,
                          virConnectAuthPtr auth,
                          int flags,
                          struct private_data **priv)
{
    int ret;
    int rflags = 0;

1034
    if (!((*priv) = remoteAllocPrivateData()))
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
        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,
            int flags)
{
    struct private_data *priv;
    int ret, rflags = 0;
1059
    const char *autostart = getenv("LIBVIRT_AUTOSTART");
1060

1061
    if (inside_daemon && (!conn->uri || (conn->uri && !conn->uri->server)))
1062 1063
        return VIR_DRV_OPEN_DECLINED;

1064
    if (!(priv = remoteAllocPrivateData()))
1065
        return VIR_DRV_OPEN_ERROR;
1066

1067
    if (flags & VIR_CONNECT_RO)
1068 1069
        rflags |= VIR_DRV_OPEN_REMOTE_RO;

1070 1071 1072 1073 1074 1075 1076 1077 1078
    /*
     * 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 已提交
1079
        conn->uri->scheme &&
1080 1081
        ((strchr(conn->uri->scheme, '+') == 0)||
         (strstr(conn->uri->scheme, "+unix") != NULL)) &&
1082 1083
        (STREQ(conn->uri->path, "/session") ||
         STRPREFIX(conn->uri->scheme, "test+")) &&
1084
        getuid() > 0) {
1085
        VIR_DEBUG0("Auto-spawn user daemon instance");
1086
        rflags |= VIR_DRV_OPEN_REMOTE_USER;
1087 1088 1089
        if (!autostart ||
            STRNEQ(autostart, "0"))
            rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1090 1091 1092
    }

    /*
J
John Levon 已提交
1093 1094 1095 1096
     * 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.
1097 1098
     */
    if (!conn->uri) {
1099
        VIR_DEBUG0("Auto-probe remote URI");
J
John Levon 已提交
1100
#ifndef __sun
1101
        if (getuid() > 0) {
1102
            VIR_DEBUG0("Auto-spawn user daemon instance");
1103
            rflags |= VIR_DRV_OPEN_REMOTE_USER;
1104 1105 1106
            if (!autostart ||
                STRNEQ(autostart, "0"))
                rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1107
        }
J
John Levon 已提交
1108
#endif
1109
    }
1110

1111
    ret = doRemoteOpen(conn, priv, auth, rflags);
1112 1113
    if (ret != VIR_DRV_OPEN_SUCCESS) {
        conn->privateData = NULL;
1114
        remoteDriverUnlock(priv);
1115
        VIR_FREE(priv);
1116 1117
    } else {
        conn->privateData = priv;
1118
        remoteDriverUnlock(priv);
1119 1120 1121 1122 1123
    }
    return ret;
}


1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
/* 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;
}

/* GnuTLS functions used by remoteOpen. */
static gnutls_certificate_credentials_t x509_cred;

1135 1136

static int
1137
check_cert_file(const char *type, const char *file)
1138 1139 1140
{
    struct stat sb;
    if (stat(file, &sb) < 0) {
1141
        virReportSystemError(errno,
1142 1143
                             _("Cannot access %s '%s'"),
                             type, file);
1144 1145 1146 1147 1148 1149
        return -1;
    }
    return 0;
}


1150
static void remote_debug_gnutls_log(int level, const char* str) {
1151
    VIR_DEBUG("%d %s", level, str);
1152 1153
}

1154
static int
1155
initialize_gnutls(char *pkipath, int flags)
1156
{
E
Eric Blake 已提交
1157
    static int initialized = 0;
1158
    int err;
1159
    char *gnutlsdebug;
1160 1161 1162 1163 1164 1165
    char *libvirt_cacert = NULL;
    char *libvirt_clientkey = NULL;
    char *libvirt_clientcert = NULL;
    int ret = -1;
    char *userdir = NULL;
    char *user_pki_path = NULL;
1166

E
Eric Blake 已提交
1167
    if (initialized) return 0;
1168 1169 1170

    gnutls_global_init ();

1171 1172 1173 1174 1175 1176 1177 1178
    if ((gnutlsdebug = getenv("LIBVIRT_GNUTLS_DEBUG")) != NULL) {
        int val;
        if (virStrToLong_i(gnutlsdebug, NULL, 10, &val) < 0)
            val = 10;
        gnutls_global_set_log_level(val);
        gnutls_global_set_log_function(remote_debug_gnutls_log);
    }

1179 1180 1181
    /* X509 stuff */
    err = gnutls_certificate_allocate_credentials (&x509_cred);
    if (err) {
1182 1183 1184
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to allocate TLS credentials: %s"),
                    gnutls_strerror (err));
1185 1186 1187
        return -1;
    }

1188 1189 1190 1191
    if (pkipath) {
        if ((virAsprintf(&libvirt_cacert, "%s/%s", pkipath,
                        "cacert.pem")) < 0)
            goto out_of_memory;
1192

1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
        if ((virAsprintf(&libvirt_clientkey, "%s/%s", pkipath,
                        "clientkey.pem")) < 0)
            goto out_of_memory;

        if ((virAsprintf(&libvirt_clientcert, "%s/%s", pkipath,
                        "clientcert.pem")) < 0)
             goto out_of_memory;
    } else if (flags & VIR_DRV_OPEN_REMOTE_USER) {
        userdir = virGetUserDirectory(getuid());

        if (!userdir)
            goto out_of_memory;

        if (virAsprintf(&user_pki_path, "%s/.pki/libvirt", userdir) < 0)
            goto out_of_memory;

        if ((virAsprintf(&libvirt_cacert, "%s/%s", user_pki_path,
                        "cacert.pem")) < 0)
            goto out_of_memory;

        if ((virAsprintf(&libvirt_clientkey, "%s/%s", user_pki_path,
                        "clientkey.pem")) < 0)
            goto out_of_memory;

        if ((virAsprintf(&libvirt_clientcert, "%s/%s", user_pki_path,
                        "clientcert.pem")) < 0)
            goto out_of_memory;

        /* Use default location as long as one of CA certificate,
         * client key, and client certificate can not be found in
         * $HOME/.pki/libvirt, we don't want to make user confused
         * with one file is here, the other is there.
         */
        if (!virFileExists(libvirt_cacert) ||
            !virFileExists(libvirt_clientkey) ||
            !virFileExists(libvirt_clientcert)) {
            VIR_FREE(libvirt_cacert);
            VIR_FREE(libvirt_clientkey);
            VIR_FREE(libvirt_clientcert);

            libvirt_cacert = strdup(LIBVIRT_CACERT);
            if (!libvirt_cacert) goto out_of_memory;

            libvirt_clientkey = strdup(LIBVIRT_CLIENTKEY);
            if (!libvirt_clientkey) goto out_of_memory;

            libvirt_clientcert = strdup(LIBVIRT_CLIENTCERT);
            if (!libvirt_clientcert) goto out_of_memory;
        }
    } else {
        libvirt_cacert = strdup(LIBVIRT_CACERT);
        if (!libvirt_cacert) goto out_of_memory;

        libvirt_clientkey = strdup(LIBVIRT_CLIENTKEY);
        if (!libvirt_clientkey) goto out_of_memory;

        libvirt_clientcert = strdup(LIBVIRT_CLIENTCERT);
        if (!libvirt_clientcert) goto out_of_memory;
    }

    if (check_cert_file("CA certificate", libvirt_cacert) < 0)
        goto error;
    if (check_cert_file("client key", libvirt_clientkey) < 0)
        goto error;
    if (check_cert_file("client certificate", libvirt_clientcert) < 0)
        goto error;
1259

1260
    /* Set the trusted CA cert. */
1261
    VIR_DEBUG("loading CA file %s", libvirt_cacert);
1262
    err =
1263
        gnutls_certificate_set_x509_trust_file (x509_cred, libvirt_cacert,
1264 1265
                                                GNUTLS_X509_FMT_PEM);
    if (err < 0) {
1266 1267 1268
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to load CA certificate: %s"),
                    gnutls_strerror (err));
1269
        goto error;
1270 1271 1272
    }

    /* Set the client certificate and private key. */
1273
    VIR_DEBUG("loading client cert and key from files %s and %s",
1274
          libvirt_clientcert, libvirt_clientkey);
1275 1276
    err =
        gnutls_certificate_set_x509_key_file (x509_cred,
1277 1278
                                              libvirt_clientcert,
                                              libvirt_clientkey,
1279 1280
                                              GNUTLS_X509_FMT_PEM);
    if (err < 0) {
1281 1282 1283
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to load private key/certificate: %s"),
                    gnutls_strerror (err));
1284
        goto error;
1285 1286
    }

E
Eric Blake 已提交
1287
    initialized = 1;
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
    ret = 0;

cleanup:
    VIR_FREE(libvirt_cacert);
    VIR_FREE(libvirt_clientkey);
    VIR_FREE(libvirt_clientcert);
    VIR_FREE(userdir);
    VIR_FREE(user_pki_path);
    return ret;

error:
    ret = -1;
    goto cleanup;

out_of_memory:
    ret = -1;
    virReportOOMError();
    goto cleanup;
1306 1307
}

1308
static int verify_certificate (virConnectPtr conn, struct private_data *priv, gnutls_session_t session);
1309

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
#if HAVE_WINSOCK2_H
static ssize_t
custom_gnutls_push(void *s, const void *buf, size_t len)
{
    return send((size_t)s, buf, len, 0);
}

static ssize_t
custom_gnutls_pull(void *s, void *buf, size_t len)
{
    return recv((size_t)s, buf, len, 0);
}
#endif

1324 1325
static gnutls_session_t
negotiate_gnutls_on_connection (virConnectPtr conn,
1326 1327
                                struct private_data *priv,
                                int no_verify)
1328 1329 1330 1331 1332 1333
{
    const int cert_type_priority[3] = {
        GNUTLS_CRT_X509,
        GNUTLS_CRT_OPENPGP,
        0
    };
1334
    bool success = false;
1335 1336 1337
    int err;
    gnutls_session_t session;

1338
    /* Initialize TLS session
1339 1340 1341
     */
    err = gnutls_init (&session, GNUTLS_CLIENT);
    if (err) {
1342 1343 1344
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to initialize TLS client: %s"),
                    gnutls_strerror (err));
1345 1346 1347 1348 1349 1350
        return NULL;
    }

    /* Use default priorities */
    err = gnutls_set_default_priority (session);
    if (err) {
1351 1352 1353
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to set TLS algorithm priority: %s"),
                    gnutls_strerror (err));
1354
        goto cleanup;
1355 1356 1357 1358 1359
    }
    err =
        gnutls_certificate_type_set_priority (session,
                                              cert_type_priority);
    if (err) {
1360 1361 1362
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to set certificate priority: %s"),
                    gnutls_strerror (err));
1363
        goto cleanup;
1364 1365 1366 1367 1368 1369
    }

    /* put the x509 credentials to the current session
     */
    err = gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
    if (err) {
1370 1371 1372
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to set session credentials: %s"),
                    gnutls_strerror (err));
1373
        goto cleanup;
1374 1375 1376
    }

    gnutls_transport_set_ptr (session,
1377
                              (gnutls_transport_ptr_t) (long) priv->sock);
1378

1379 1380 1381 1382 1383 1384 1385
#if HAVE_WINSOCK2_H
    /* Make sure GnuTLS uses gnulib's replacment functions for send() and
     * recv() on Windows */
    gnutls_transport_set_push_function(session, custom_gnutls_push);
    gnutls_transport_set_pull_function(session, custom_gnutls_pull);
#endif

1386 1387 1388 1389 1390 1391
    /* Perform the TLS handshake. */
 again:
    err = gnutls_handshake (session);
    if (err < 0) {
        if (err == GNUTLS_E_AGAIN || err == GNUTLS_E_INTERRUPTED)
            goto again;
1392 1393 1394
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to complete TLS handshake: %s"),
                    gnutls_strerror (err));
1395
        goto cleanup;
1396 1397 1398
    }

    /* Verify certificate. */
1399
    if (verify_certificate (conn, priv, session) == -1) {
1400
        VIR_DEBUG0("failed to verify peer's certificate");
1401 1402
        if (!no_verify)
            goto cleanup;
1403
    }
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414

    /* At this point, the server is verifying _our_ certificate, IP address,
     * etc.  If we make the grade, it will send us a '\1' byte.
     */
    char buf[1];
    int len;
 again_2:
    len = gnutls_record_recv (session, buf, 1);
    if (len < 0 && len != GNUTLS_E_UNEXPECTED_PACKET_LENGTH) {
        if (len == GNUTLS_E_AGAIN || len == GNUTLS_E_INTERRUPTED)
            goto again_2;
1415 1416 1417
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to complete TLS initialization: %s"),
                    gnutls_strerror (len));
1418
        goto cleanup;
1419 1420
    }
    if (len != 1 || buf[0] != '\1') {
1421 1422 1423
        remoteError(VIR_ERR_RPC, "%s",
                    _("server verification (of our certificate or IP "
                      "address) failed"));
1424
        goto cleanup;
1425 1426 1427 1428 1429 1430 1431
    }

#if 0
    /* Print session info. */
    print_info (session);
#endif

1432 1433 1434 1435 1436 1437 1438 1439
    success = true;

cleanup:
    if (!success) {
        gnutls_deinit(session);
        session = NULL;
    }

1440 1441 1442 1443 1444
    return session;
}

static int
verify_certificate (virConnectPtr conn ATTRIBUTE_UNUSED,
1445 1446
                    struct private_data *priv,
                    gnutls_session_t session)
1447 1448 1449 1450 1451 1452 1453 1454
{
    int ret;
    unsigned int status;
    const gnutls_datum_t *certs;
    unsigned int nCerts, i;
    time_t now;

    if ((ret = gnutls_certificate_verify_peers2 (session, &status)) < 0) {
1455 1456 1457
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to verify server certificate: %s"),
                    gnutls_strerror (ret));
1458 1459
        return -1;
    }
1460

1461
    if ((now = time(NULL)) == ((time_t)-1)) {
1462
        virReportSystemError(errno, "%s",
1463
                             _("cannot get current time"));
1464 1465 1466 1467
        return -1;
    }

    if (status != 0) {
1468
        const char *reason = _("Invalid certificate");
1469 1470

        if (status & GNUTLS_CERT_INVALID)
1471
            reason = _("The certificate is not trusted.");
1472

1473
        if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
1474
            reason = _("The certificate hasn't got a known issuer.");
1475

1476
        if (status & GNUTLS_CERT_REVOKED)
1477
            reason = _("The certificate has been revoked.");
1478 1479

#ifndef GNUTLS_1_0_COMPAT
1480
        if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
1481
            reason = _("The certificate uses an insecure algorithm");
1482
#endif
1483

1484 1485 1486
        remoteError(VIR_ERR_RPC,
                    _("server certificate failed validation: %s"),
                    reason);
1487 1488 1489 1490
        return -1;
    }

    if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) {
1491
        remoteError(VIR_ERR_RPC,  "%s",_("Certificate type is not X.509"));
1492 1493
        return -1;
    }
1494

1495
    if (!(certs = gnutls_certificate_get_peers(session, &nCerts))) {
1496
        remoteError(VIR_ERR_RPC,  "%s",_("gnutls_certificate_get_peers failed"));
1497 1498
        return -1;
    }
1499

1500 1501 1502 1503 1504
    for (i = 0 ; i < nCerts ; i++) {
        gnutls_x509_crt_t cert;

        ret = gnutls_x509_crt_init (&cert);
        if (ret < 0) {
1505 1506 1507
            remoteError(VIR_ERR_GNUTLS_ERROR,
                        _("unable to initialize certificate: %s"),
                        gnutls_strerror (ret));
1508 1509
            return -1;
        }
1510

1511 1512
        ret = gnutls_x509_crt_import (cert, &certs[i], GNUTLS_X509_FMT_DER);
        if (ret < 0) {
1513 1514 1515
            remoteError(VIR_ERR_GNUTLS_ERROR,
                        _("unable to import certificate: %s"),
                        gnutls_strerror (ret));
1516 1517 1518
            gnutls_x509_crt_deinit (cert);
            return -1;
        }
1519

1520
        if (gnutls_x509_crt_get_expiration_time (cert) < now) {
1521
            remoteError(VIR_ERR_RPC, "%s", _("The certificate has expired"));
1522 1523 1524
            gnutls_x509_crt_deinit (cert);
            return -1;
        }
1525

1526
        if (gnutls_x509_crt_get_activation_time (cert) > now) {
1527 1528
            remoteError(VIR_ERR_RPC, "%s",
                        _("The certificate is not yet activated"));
1529 1530 1531
            gnutls_x509_crt_deinit (cert);
            return -1;
        }
1532

1533
        if (i == 0) {
1534
            if (!gnutls_x509_crt_check_hostname (cert, priv->hostname)) {
1535 1536 1537
                remoteError(VIR_ERR_RPC,
                            _("Certificate's owner does not match the hostname (%s)"),
                            priv->hostname);
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
                gnutls_x509_crt_deinit (cert);
                return -1;
            }
        }
    }

    return 0;
}

/*----------------------------------------------------------------------*/

1549

1550
static int
1551
doRemoteClose (virConnectPtr conn, struct private_data *priv)
1552
{
1553 1554 1555 1556 1557
    if (priv->eventFlushTimer >= 0) {
        /* Remove timeout */
        virEventRemoveTimeout(priv->eventFlushTimer);
        /* Remove handle for remote events */
        virEventRemoveHandle(priv->watch);
1558
        priv->watch = -1;
1559
    }
1560

1561 1562 1563 1564 1565
    if (call (conn, priv, 0, REMOTE_PROC_CLOSE,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        return -1;

1566
    /* Close socket. */
1567
    if (priv->uses_tls && priv->session) {
1568
        gnutls_bye (priv->session, GNUTLS_SHUT_RDWR);
1569 1570 1571 1572 1573 1574
        gnutls_deinit (priv->session);
    }
#if HAVE_SASL
    if (priv->saslconn)
        sasl_dispose (&priv->saslconn);
#endif
1575 1576
    VIR_FORCE_CLOSE(priv->sock);
    VIR_FORCE_CLOSE(priv->errfd);
1577

1578
#ifndef WIN32
1579 1580 1581
    if (priv->pid > 0) {
        pid_t reap;
        do {
1582
retry:
1583 1584
            reap = waitpid(priv->pid, NULL, 0);
            if (reap == -1 && errno == EINTR)
1585
                goto retry;
1586 1587
        } while (reap != -1 && reap != priv->pid);
    }
1588
#endif
1589 1590
    VIR_FORCE_CLOSE(priv->wakeupReadFD);
    VIR_FORCE_CLOSE(priv->wakeupSendFD);
1591

1592

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

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

1599 1600 1601 1602 1603 1604
    /* Free callback list */
    virDomainEventCallbackListFree(priv->callbackList);

    /* Free queued events */
    virDomainEventQueueFree(priv->domainEvents);

1605 1606 1607
    return 0;
}

1608 1609 1610
static int
remoteClose (virConnectPtr conn)
{
1611
    int ret = 0;
1612
    struct private_data *priv = conn->privateData;
1613

1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        ret = doRemoteClose(conn, priv);
        conn->privateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE (priv);
    }
    if (priv)
        remoteDriverUnlock(priv);
1625 1626 1627 1628

    return ret;
}

1629 1630 1631
static int
remoteSupportsFeature (virConnectPtr conn, int feature)
{
1632
    int rv = -1;
1633 1634
    remote_supports_feature_args args;
    remote_supports_feature_ret ret;
1635
    struct private_data *priv = conn->privateData;
1636

1637 1638
    remoteDriverLock(priv);

1639
    /* VIR_DRV_FEATURE_REMOTE* features are handled directly. */
1640 1641 1642 1643
    if (feature == VIR_DRV_FEATURE_REMOTE) {
        rv = 1;
        goto done;
    }
1644 1645 1646 1647 1648 1649 1650

    args.feature = feature;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_SUPPORTS_FEATURE,
              (xdrproc_t) xdr_remote_supports_feature_args, (char *) &args,
              (xdrproc_t) xdr_remote_supports_feature_ret, (char *) &ret) == -1)
1651 1652 1653
        goto done;

    rv = ret.supported;
1654

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

1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
/* 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)
{
1671
    char *rv = NULL;
1672
    remote_get_type_ret ret;
1673
    struct private_data *priv = conn->privateData;
1674

1675 1676
    remoteDriverLock(priv);

1677
    /* Cached? */
1678 1679 1680 1681
    if (priv->type) {
        rv = priv->type;
        goto done;
    }
1682 1683 1684 1685 1686

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

    /* Stash. */
1690 1691 1692
    rv = priv->type = ret.type;

done:
1693
    remoteDriverUnlock(priv);
1694
    return rv;
1695 1696 1697
}

static int
1698
remoteGetVersion (virConnectPtr conn, unsigned long *hvVer)
1699
{
1700
    int rv = -1;
1701
    remote_get_version_ret ret;
1702
    struct private_data *priv = conn->privateData;
1703

1704 1705
    remoteDriverLock(priv);

1706 1707 1708 1709
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_GET_VERSION,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_get_version_ret, (char *) &ret) == -1)
1710
        goto done;
1711 1712

    if (hvVer) *hvVer = ret.hv_ver;
1713 1714 1715
    rv = 0;

done:
1716
    remoteDriverUnlock(priv);
1717
    return rv;
1718 1719
}

1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
static int
remoteGetLibVersion (virConnectPtr conn, unsigned long *libVer)
{
    int rv = -1;
    remote_get_lib_version_ret ret;
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_GET_LIB_VERSION,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_get_lib_version_ret,
              (char *) &ret) == -1)
        goto done;

    if (libVer) *libVer = ret.lib_ver;
    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

1744 1745 1746
static char *
remoteGetHostname (virConnectPtr conn)
{
1747
    char *rv = NULL;
1748
    remote_get_hostname_ret ret;
1749
    struct private_data *priv = conn->privateData;
1750

1751 1752
    remoteDriverLock(priv);

1753 1754 1755 1756
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_GET_HOSTNAME,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_get_hostname_ret, (char *) &ret) == -1)
1757
        goto done;
1758 1759

    /* Caller frees this. */
1760 1761 1762
    rv = ret.hostname;

done:
1763
    remoteDriverUnlock(priv);
1764
    return rv;
1765 1766
}

1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
static char *
remoteGetSysinfo (virConnectPtr conn, unsigned int flags)
{
    char *rv = NULL;
    remote_get_sysinfo_args args;
    remote_get_sysinfo_ret ret;
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    memset (&ret, 0, sizeof ret);
    args.flags = flags;
    if (call (conn, priv, 0, REMOTE_PROC_GET_SYSINFO,
              (xdrproc_t) xdr_remote_get_sysinfo_args, (char *) &args,
              (xdrproc_t) xdr_remote_get_sysinfo_ret, (char *) &ret) == -1)
        goto done;

    /* Caller frees this. */
    rv = ret.sysinfo;

done:
    remoteDriverUnlock(priv);
    return rv;
}

1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
static int remoteIsSecure(virConnectPtr conn)
{
    int rv = -1;
    struct private_data *priv = conn->privateData;
    remote_is_secure_ret ret;
    remoteDriverLock(priv);

    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;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int remoteIsEncrypted(virConnectPtr conn)
{
    int rv = -1;
    int encrypted = 0;
    struct private_data *priv = conn->privateData;
    remote_is_secure_ret ret;
    remoteDriverLock(priv);

    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;

    if (priv->uses_tls)
        encrypted = 1;
#if HAVE_SASL
    else if (priv->saslconn)
        encrypted = 1;
#endif


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


1858 1859 1860
static int
remoteGetMaxVcpus (virConnectPtr conn, const char *type)
{
1861
    int rv = -1;
1862 1863
    remote_get_max_vcpus_args args;
    remote_get_max_vcpus_ret ret;
1864
    struct private_data *priv = conn->privateData;
1865

1866 1867
    remoteDriverLock(priv);

1868
    memset (&ret, 0, sizeof ret);
1869
    args.type = type == NULL ? NULL : (char **) &type;
1870 1871 1872
    if (call (conn, priv, 0, REMOTE_PROC_GET_MAX_VCPUS,
              (xdrproc_t) xdr_remote_get_max_vcpus_args, (char *) &args,
              (xdrproc_t) xdr_remote_get_max_vcpus_ret, (char *) &ret) == -1)
1873 1874 1875
        goto done;

    rv = ret.max_vcpus;
1876

1877
done:
1878
    remoteDriverUnlock(priv);
1879
    return rv;
1880 1881 1882 1883 1884
}

static int
remoteNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info)
{
1885
    int rv = -1;
1886
    remote_node_get_info_ret ret;
1887
    struct private_data *priv = conn->privateData;
1888

1889 1890
    remoteDriverLock(priv);

1891 1892 1893 1894
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NODE_GET_INFO,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_node_get_info_ret, (char *) &ret) == -1)
1895
        goto done;
1896

C
Chris Lalancette 已提交
1897 1898
    if (virStrcpyStatic(info->model, ret.model) == NULL)
        goto done;
1899 1900 1901 1902 1903 1904 1905
    info->memory = ret.memory;
    info->cpus = ret.cpus;
    info->mhz = ret.mhz;
    info->nodes = ret.nodes;
    info->sockets = ret.sockets;
    info->cores = ret.cores;
    info->threads = ret.threads;
1906 1907 1908
    rv = 0;

done:
1909
    remoteDriverUnlock(priv);
1910
    return rv;
1911 1912 1913 1914 1915
}

static char *
remoteGetCapabilities (virConnectPtr conn)
{
1916
    char *rv = NULL;
1917
    remote_get_capabilities_ret ret;
1918
    struct private_data *priv = conn->privateData;
1919

1920 1921
    remoteDriverLock(priv);

1922 1923 1924 1925
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_GET_CAPABILITIES,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_get_capabilities_ret, (char *)&ret) == -1)
1926
        goto done;
1927 1928

    /* Caller frees this. */
1929 1930 1931
    rv = ret.capabilities;

done:
1932
    remoteDriverUnlock(priv);
1933
    return rv;
1934 1935
}

1936 1937 1938 1939 1940 1941
static int
remoteNodeGetCellsFreeMemory(virConnectPtr conn,
                            unsigned long long *freeMems,
                            int startCell,
                            int maxCells)
{
1942
    int rv = -1;
1943 1944 1945
    remote_node_get_cells_free_memory_args args;
    remote_node_get_cells_free_memory_ret ret;
    int i;
1946
    struct private_data *priv = conn->privateData;
1947

1948 1949
    remoteDriverLock(priv);

1950
    if (maxCells > REMOTE_NODE_MAX_CELLS) {
1951 1952 1953
        remoteError(VIR_ERR_RPC,
                    _("too many NUMA cells: %d > %d"),
                    maxCells, REMOTE_NODE_MAX_CELLS);
1954
        goto done;
1955 1956 1957 1958 1959 1960 1961 1962 1963
    }

    args.startCell = startCell;
    args.maxCells = maxCells;

    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)
1964
        goto done;
1965 1966 1967 1968 1969 1970

    for (i = 0 ; i < ret.freeMems.freeMems_len ; i++)
        freeMems[i] = ret.freeMems.freeMems_val[i];

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

1971 1972 1973
    rv = ret.freeMems.freeMems_len;

done:
1974
    remoteDriverUnlock(priv);
1975
    return rv;
1976 1977 1978 1979 1980
}

static unsigned long long
remoteNodeGetFreeMemory (virConnectPtr conn)
{
1981
    unsigned long long rv = 0; /* 0 is error value this special function*/
1982
    remote_node_get_free_memory_ret ret;
1983
    struct private_data *priv = conn->privateData;
1984

1985 1986
    remoteDriverLock(priv);

1987 1988 1989 1990
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NODE_GET_FREE_MEMORY,
              (xdrproc_t) xdr_void, NULL,
              (xdrproc_t) xdr_remote_node_get_free_memory_ret, (char *)&ret) == -1)
1991 1992 1993
        goto done;

    rv = ret.freeMem;
1994

1995
done:
1996
    remoteDriverUnlock(priv);
1997
    return rv;
1998 1999 2000
}


2001 2002 2003
static int
remoteListDomains (virConnectPtr conn, int *ids, int maxids)
{
2004
    int rv = -1;
2005 2006 2007
    int i;
    remote_list_domains_args args;
    remote_list_domains_ret ret;
2008
    struct private_data *priv = conn->privateData;
2009

2010 2011
    remoteDriverLock(priv);

2012
    if (maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
2013 2014 2015
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain IDs: %d > %d"),
                    maxids, REMOTE_DOMAIN_ID_LIST_MAX);
2016
        goto done;
2017 2018 2019 2020 2021 2022 2023
    }
    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)
2024
        goto done;
2025 2026

    if (ret.ids.ids_len > maxids) {
2027 2028 2029
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain IDs: %d > %d"),
                    ret.ids.ids_len, maxids);
2030
        goto cleanup;
2031 2032 2033 2034 2035
    }

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

2036 2037 2038
    rv = ret.ids.ids_len;

cleanup:
2039 2040
    xdr_free ((xdrproc_t) xdr_remote_list_domains_ret, (char *) &ret);

2041
done:
2042
    remoteDriverUnlock(priv);
2043
    return rv;
2044 2045 2046 2047 2048
}

static int
remoteNumOfDomains (virConnectPtr conn)
{
2049
    int rv = -1;
2050
    remote_num_of_domains_ret ret;
2051
    struct private_data *priv = conn->privateData;
2052

2053 2054
    remoteDriverLock(priv);

2055 2056 2057 2058
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NUM_OF_DOMAINS,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_num_of_domains_ret, (char *) &ret) == -1)
2059 2060 2061
        goto done;

    rv = ret.num;
2062

2063
done:
2064
    remoteDriverUnlock(priv);
2065
    return rv;
2066 2067
}

2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
static int
remoteDomainIsActive(virDomainPtr domain)
{
    int rv = -1;
    remote_domain_is_active_args args;
    remote_domain_is_active_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain (&args.dom, domain);

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_IS_ACTIVE,
              (xdrproc_t) xdr_remote_domain_is_active_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_is_active_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.active;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteDomainIsPersistent(virDomainPtr domain)
{
    int rv = -1;
    remote_domain_is_persistent_args args;
    remote_domain_is_persistent_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain (&args.dom, domain);

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_IS_PERSISTENT,
              (xdrproc_t) xdr_remote_domain_is_persistent_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_is_persistent_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.persistent;

done:
    remoteDriverUnlock(priv);
    return rv;
}

O
Osier Yang 已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
static int
remoteDomainIsUpdated(virDomainPtr domain)
{
    int rv = -1;
    remote_domain_is_updated_args args;
    remote_domain_is_updated_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain (&args.dom, domain);

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_IS_UPDATED,
              (xdrproc_t) xdr_remote_domain_is_updated_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_is_updated_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.updated;

done:
    remoteDriverUnlock(priv);
    return rv;
}

2140
static virDomainPtr
2141
remoteDomainCreateXML (virConnectPtr conn,
2142 2143 2144
                         const char *xmlDesc,
                         unsigned int flags)
{
2145
    virDomainPtr dom = NULL;
2146 2147
    remote_domain_create_xml_args args;
    remote_domain_create_xml_ret ret;
2148
    struct private_data *priv = conn->privateData;
2149

2150 2151
    remoteDriverLock(priv);

2152 2153 2154 2155
    args.xml_desc = (char *) xmlDesc;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
2156 2157 2158
    if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_CREATE_XML,
              (xdrproc_t) xdr_remote_domain_create_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_create_xml_ret, (char *) &ret) == -1)
2159
        goto done;
2160 2161

    dom = get_nonnull_domain (conn, ret.dom);
2162
    xdr_free ((xdrproc_t) &xdr_remote_domain_create_xml_ret, (char *) &ret);
2163

2164
done:
2165
    remoteDriverUnlock(priv);
2166 2167 2168 2169 2170 2171
    return dom;
}

static virDomainPtr
remoteDomainLookupByID (virConnectPtr conn, int id)
{
2172
    virDomainPtr dom = NULL;
2173 2174
    remote_domain_lookup_by_id_args args;
    remote_domain_lookup_by_id_ret ret;
2175
    struct private_data *priv = conn->privateData;
2176

2177 2178
    remoteDriverLock(priv);

2179 2180 2181 2182 2183 2184
    args.id = id;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_LOOKUP_BY_ID,
              (xdrproc_t) xdr_remote_domain_lookup_by_id_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_lookup_by_id_ret, (char *) &ret) == -1)
2185
        goto done;
2186 2187 2188 2189

    dom = get_nonnull_domain (conn, ret.dom);
    xdr_free ((xdrproc_t) &xdr_remote_domain_lookup_by_id_ret, (char *) &ret);

2190
done:
2191
    remoteDriverUnlock(priv);
2192 2193 2194 2195 2196 2197
    return dom;
}

static virDomainPtr
remoteDomainLookupByUUID (virConnectPtr conn, const unsigned char *uuid)
{
2198
    virDomainPtr dom = NULL;
2199 2200
    remote_domain_lookup_by_uuid_args args;
    remote_domain_lookup_by_uuid_ret ret;
2201
    struct private_data *priv = conn->privateData;
2202

2203 2204
    remoteDriverLock(priv);

2205 2206 2207 2208 2209 2210
    memcpy (args.uuid, uuid, VIR_UUID_BUFLEN);

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_LOOKUP_BY_UUID,
              (xdrproc_t) xdr_remote_domain_lookup_by_uuid_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_lookup_by_uuid_ret, (char *) &ret) == -1)
2211
        goto done;
2212 2213 2214

    dom = get_nonnull_domain (conn, ret.dom);
    xdr_free ((xdrproc_t) &xdr_remote_domain_lookup_by_uuid_ret, (char *) &ret);
2215 2216

done:
2217
    remoteDriverUnlock(priv);
2218 2219 2220 2221 2222 2223
    return dom;
}

static virDomainPtr
remoteDomainLookupByName (virConnectPtr conn, const char *name)
{
2224
    virDomainPtr dom = NULL;
2225 2226
    remote_domain_lookup_by_name_args args;
    remote_domain_lookup_by_name_ret ret;
2227
    struct private_data *priv = conn->privateData;
2228

2229 2230
    remoteDriverLock(priv);

2231 2232 2233 2234 2235 2236
    args.name = (char *) name;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_LOOKUP_BY_NAME,
              (xdrproc_t) xdr_remote_domain_lookup_by_name_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_lookup_by_name_ret, (char *) &ret) == -1)
2237
        goto done;
2238 2239 2240 2241

    dom = get_nonnull_domain (conn, ret.dom);
    xdr_free ((xdrproc_t) &xdr_remote_domain_lookup_by_name_ret, (char *) &ret);

2242
done:
2243
    remoteDriverUnlock(priv);
2244 2245 2246 2247 2248 2249
    return dom;
}

static int
remoteDomainSuspend (virDomainPtr domain)
{
2250
    int rv = -1;
2251
    remote_domain_suspend_args args;
2252
    struct private_data *priv = domain->conn->privateData;
2253

2254 2255
    remoteDriverLock(priv);

2256 2257 2258 2259 2260
    make_nonnull_domain (&args.dom, domain);

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SUSPEND,
              (xdrproc_t) xdr_remote_domain_suspend_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
2261
        goto done;
2262

2263 2264 2265
    rv = 0;

done:
2266
    remoteDriverUnlock(priv);
2267
    return rv;
2268 2269 2270 2271 2272
}

static int
remoteDomainResume (virDomainPtr domain)
{
2273
    int rv = -1;
2274
    remote_domain_resume_args args;
2275
    struct private_data *priv = domain->conn->privateData;
2276

2277 2278
    remoteDriverLock(priv);

2279 2280 2281 2282 2283
    make_nonnull_domain (&args.dom, domain);

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_RESUME,
              (xdrproc_t) xdr_remote_domain_resume_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
2284
        goto done;
2285

2286 2287 2288
    rv = 0;

done:
2289
    remoteDriverUnlock(priv);
2290
    return rv;
2291 2292 2293 2294 2295
}

static int
remoteDomainShutdown (virDomainPtr domain)
{
2296
    int rv = -1;
2297
    remote_domain_shutdown_args args;
2298
    struct private_data *priv = domain->conn->privateData;
2299

2300 2301
    remoteDriverLock(priv);

2302 2303 2304 2305 2306
    make_nonnull_domain (&args.dom, domain);

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SHUTDOWN,
              (xdrproc_t) xdr_remote_domain_shutdown_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
2307
        goto done;
2308

2309 2310 2311
    rv = 0;

done:
2312
    remoteDriverUnlock(priv);
2313
    return rv;
2314 2315 2316 2317 2318
}

static int
remoteDomainReboot (virDomainPtr domain, unsigned int flags)
{
2319
    int rv = -1;
2320
    remote_domain_reboot_args args;
2321
    struct private_data *priv = domain->conn->privateData;
2322

2323 2324
    remoteDriverLock(priv);

2325 2326 2327 2328 2329 2330
    make_nonnull_domain (&args.dom, domain);
    args.flags = flags;

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_REBOOT,
              (xdrproc_t) xdr_remote_domain_reboot_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
2331
        goto done;
2332

2333 2334 2335
    rv = 0;

done:
2336
    remoteDriverUnlock(priv);
2337
    return rv;
2338 2339 2340 2341 2342
}

static int
remoteDomainDestroy (virDomainPtr domain)
{
2343
    int rv = -1;
2344
    remote_domain_destroy_args args;
2345
    struct private_data *priv = domain->conn->privateData;
2346

2347 2348
    remoteDriverLock(priv);

2349 2350 2351 2352 2353
    make_nonnull_domain (&args.dom, domain);

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_DESTROY,
              (xdrproc_t) xdr_remote_domain_destroy_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
2354
        goto done;
2355

2356
    rv = 0;
2357
    domain->id = -1;
2358 2359

done:
2360
    remoteDriverUnlock(priv);
2361
    return rv;
2362 2363 2364 2365 2366
}

static char *
remoteDomainGetOSType (virDomainPtr domain)
{
2367
    char *rv = NULL;
2368 2369
    remote_domain_get_os_type_args args;
    remote_domain_get_os_type_ret ret;
2370
    struct private_data *priv = domain->conn->privateData;
2371

2372 2373
    remoteDriverLock(priv);

2374 2375 2376 2377 2378 2379
    make_nonnull_domain (&args.dom, domain);

    memset (&ret, 0, sizeof ret);
    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_OS_TYPE,
              (xdrproc_t) xdr_remote_domain_get_os_type_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_get_os_type_ret, (char *) &ret) == -1)
2380
        goto done;
2381 2382

    /* Caller frees. */
2383 2384 2385
    rv = ret.type;

done:
2386
    remoteDriverUnlock(priv);
2387
    return rv;
2388 2389 2390 2391 2392
}

static unsigned long
remoteDomainGetMaxMemory (virDomainPtr domain)
{
2393
    unsigned long rv = 0;
2394 2395
    remote_domain_get_max_memory_args args;
    remote_domain_get_max_memory_ret ret;
2396
    struct private_data *priv = domain->conn->privateData;
2397

2398 2399
    remoteDriverLock(priv);

2400 2401 2402 2403 2404 2405
    make_nonnull_domain (&args.dom, domain);

    memset (&ret, 0, sizeof ret);
    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_MAX_MEMORY,
              (xdrproc_t) xdr_remote_domain_get_max_memory_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_get_max_memory_ret, (char *) &ret) == -1)
2406 2407 2408
        goto done;

    rv = ret.memory;
2409

2410
done:
2411
    remoteDriverUnlock(priv);
2412
    return rv;
2413 2414 2415 2416 2417
}

static int
remoteDomainSetMaxMemory (virDomainPtr domain, unsigned long memory)
{
2418
    int rv = -1;
2419
    remote_domain_set_max_memory_args args;
2420
    struct private_data *priv = domain->conn->privateData;
2421

2422 2423
    remoteDriverLock(priv);

2424 2425 2426 2427 2428 2429
    make_nonnull_domain (&args.dom, domain);
    args.memory = memory;

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SET_MAX_MEMORY,
              (xdrproc_t) xdr_remote_domain_set_max_memory_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
2430
        goto done;
2431

2432 2433 2434
    rv = 0;

done:
2435
    remoteDriverUnlock(priv);
2436
    return rv;
2437 2438 2439 2440 2441
}

static int
remoteDomainSetMemory (virDomainPtr domain, unsigned long memory)
{
2442
    int rv = -1;
2443
    remote_domain_set_memory_args args;
2444
    struct private_data *priv = domain->conn->privateData;
2445

2446 2447
    remoteDriverLock(priv);

2448 2449 2450 2451 2452 2453
    make_nonnull_domain (&args.dom, domain);
    args.memory = memory;

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SET_MEMORY,
              (xdrproc_t) xdr_remote_domain_set_memory_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
2454
        goto done;
2455

2456 2457 2458
    rv = 0;

done:
2459
    remoteDriverUnlock(priv);
2460
    return rv;
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 2488 2489
static int
remoteDomainSetMemoryFlags (virDomainPtr domain, unsigned long memory, unsigned int flags)
{
    int rv = -1;
    remote_domain_set_memory_flags_args args;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SET_MEMORY_FLAGS,
              (xdrproc_t) xdr_remote_domain_set_memory_flags_args,
              (char *) &args,
              (xdrproc_t) xdr_void,
              (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
static int
remoteDomainSetMemoryParameters (virDomainPtr domain,
                                 virMemoryParameterPtr params,
                                 int nparams,
                                 unsigned int flags)
{
    int rv = -1;
    remote_domain_set_memory_parameters_args args;
    int i, do_error;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain (&args.dom, domain);

    /* Serialise the memory parameters. */
    args.params.params_len = nparams;
    args.flags = flags;
    if (VIR_ALLOC_N(args.params.params_val, nparams) < 0) {
        virReportOOMError();
        goto done;
    }

    do_error = 0;
    for (i = 0; i < nparams; ++i) {
2515
        /* call() will free this: */
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658
        args.params.params_val[i].field = strdup (params[i].field);
        if (args.params.params_val[i].field == NULL) {
            virReportOOMError();
            do_error = 1;
        }
        args.params.params_val[i].value.type = params[i].type;
        switch (params[i].type) {
        case VIR_DOMAIN_MEMORY_PARAM_INT:
            args.params.params_val[i].value.remote_memory_param_value_u.i =
                params[i].value.i; break;
        case VIR_DOMAIN_MEMORY_PARAM_UINT:
            args.params.params_val[i].value.remote_memory_param_value_u.ui =
                params[i].value.ui; break;
        case VIR_DOMAIN_MEMORY_PARAM_LLONG:
            args.params.params_val[i].value.remote_memory_param_value_u.l =
                params[i].value.l; break;
        case VIR_DOMAIN_MEMORY_PARAM_ULLONG:
            args.params.params_val[i].value.remote_memory_param_value_u.ul =
                params[i].value.ul; break;
        case VIR_DOMAIN_MEMORY_PARAM_DOUBLE:
            args.params.params_val[i].value.remote_memory_param_value_u.d =
                params[i].value.d; break;
        case VIR_DOMAIN_MEMORY_PARAM_BOOLEAN:
            args.params.params_val[i].value.remote_memory_param_value_u.b =
                params[i].value.b; break;
        default:
            remoteError(VIR_ERR_RPC, "%s", _("unknown parameter type"));
            do_error = 1;
        }
    }

    if (do_error) {
        xdr_free ((xdrproc_t) xdr_remote_domain_set_memory_parameters_args,
                  (char *) &args);
        goto done;
    }

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SET_MEMORY_PARAMETERS,
              (xdrproc_t) xdr_remote_domain_set_memory_parameters_args,
              (char *) &args, (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteDomainGetMemoryParameters (virDomainPtr domain,
                                 virMemoryParameterPtr params, int *nparams,
                                 unsigned int flags)
{
    int rv = -1;
    remote_domain_get_memory_parameters_args args;
    remote_domain_get_memory_parameters_ret ret;
    int i = -1;
    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;

    /* Check the length of the returned list carefully. */
    if (ret.params.params_len > REMOTE_DOMAIN_MEMORY_PARAMETERS_MAX ||
        ret.params.params_len > *nparams) {
        remoteError(VIR_ERR_RPC, "%s",
                    _("remoteDomainGetMemoryParameters: "
                      "returned number of parameters exceeds limit"));
        goto cleanup;
    }
    /* 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;
    }

    *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,
                        _("Parameter %s too big for destination"),
                        ret.params.params_val[i].field);
            goto cleanup;
        }
        params[i].type = ret.params.params_val[i].value.type;
        switch (params[i].type) {
        case VIR_DOMAIN_MEMORY_PARAM_INT:
            params[i].value.i =
                ret.params.params_val[i].value.remote_memory_param_value_u.i;
            break;
        case VIR_DOMAIN_MEMORY_PARAM_UINT:
            params[i].value.ui =
                ret.params.params_val[i].value.remote_memory_param_value_u.ui;
            break;
        case VIR_DOMAIN_MEMORY_PARAM_LLONG:
            params[i].value.l =
                ret.params.params_val[i].value.remote_memory_param_value_u.l;
            break;
        case VIR_DOMAIN_MEMORY_PARAM_ULLONG:
            params[i].value.ul =
                ret.params.params_val[i].value.remote_memory_param_value_u.ul;
            break;
        case VIR_DOMAIN_MEMORY_PARAM_DOUBLE:
            params[i].value.d =
                ret.params.params_val[i].value.remote_memory_param_value_u.d;
            break;
        case VIR_DOMAIN_MEMORY_PARAM_BOOLEAN:
            params[i].value.b =
                ret.params.params_val[i].value.remote_memory_param_value_u.b;
            break;
        default:
            remoteError(VIR_ERR_RPC, "%s",
                        _("remoteDomainGetMemoryParameters: "
                          "unknown parameter type"));
            goto cleanup;
        }
    }

    rv = 0;

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

2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827
static int
remoteDomainSetBlkioParameters (virDomainPtr domain,
                                virBlkioParameterPtr params,
                                 int nparams,
                                 unsigned int flags)
{
    int rv = -1;
    remote_domain_set_blkio_parameters_args args;
    int i, do_error;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain (&args.dom, domain);

    /* Serialise the blkio parameters. */
    args.params.params_len = nparams;
    args.flags = flags;
    if (VIR_ALLOC_N(args.params.params_val, nparams) < 0) {
        virReportOOMError();
        goto done;
    }

    do_error = 0;
    for (i = 0; i < nparams; ++i) {
        // call() will free this:
        args.params.params_val[i].field = strdup (params[i].field);
        if (args.params.params_val[i].field == NULL) {
            virReportOOMError();
            do_error = 1;
        }
        args.params.params_val[i].value.type = params[i].type;
        switch (params[i].type) {
        case VIR_DOMAIN_BLKIO_PARAM_INT:
            args.params.params_val[i].value.remote_blkio_param_value_u.i =
                params[i].value.i; break;
        case VIR_DOMAIN_BLKIO_PARAM_UINT:
            args.params.params_val[i].value.remote_blkio_param_value_u.ui =
                params[i].value.ui; break;
        case VIR_DOMAIN_BLKIO_PARAM_LLONG:
            args.params.params_val[i].value.remote_blkio_param_value_u.l =
                params[i].value.l; break;
        case VIR_DOMAIN_BLKIO_PARAM_ULLONG:
            args.params.params_val[i].value.remote_blkio_param_value_u.ul =
                params[i].value.ul; break;
        case VIR_DOMAIN_BLKIO_PARAM_DOUBLE:
            args.params.params_val[i].value.remote_blkio_param_value_u.d =
                params[i].value.d; break;
        case VIR_DOMAIN_BLKIO_PARAM_BOOLEAN:
            args.params.params_val[i].value.remote_blkio_param_value_u.b =
                params[i].value.b; break;
        default:
            remoteError(VIR_ERR_RPC, "%s", _("unknown parameter type"));
            do_error = 1;
        }
    }

    if (do_error) {
        xdr_free ((xdrproc_t) xdr_remote_domain_set_blkio_parameters_args,
                  (char *) &args);
        goto done;
    }

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SET_BLKIO_PARAMETERS,
              (xdrproc_t) xdr_remote_domain_set_blkio_parameters_args,
              (char *) &args, (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteDomainGetBlkioParameters (virDomainPtr domain,
                                 virBlkioParameterPtr params, int *nparams,
                                 unsigned int flags)
{
    int rv = -1;
    remote_domain_get_blkio_parameters_args args;
    remote_domain_get_blkio_parameters_ret ret;
    int i = -1;
    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;

    /* Check the length of the returned list carefully. */
    if (ret.params.params_len > REMOTE_DOMAIN_BLKIO_PARAMETERS_MAX ||
        ret.params.params_len > *nparams) {
        remoteError(VIR_ERR_RPC, "%s",
                    _("remoteDomainGetBlkioParameters: "
                      "returned number of parameters exceeds limit"));
        goto cleanup;
    }
    /* 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;
    }

    *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,
                        _("Parameter %s too big for destination"),
                        ret.params.params_val[i].field);
            goto cleanup;
        }
        params[i].type = ret.params.params_val[i].value.type;
        switch (params[i].type) {
        case VIR_DOMAIN_BLKIO_PARAM_INT:
            params[i].value.i =
                ret.params.params_val[i].value.remote_blkio_param_value_u.i;
            break;
        case VIR_DOMAIN_BLKIO_PARAM_UINT:
            params[i].value.ui =
                ret.params.params_val[i].value.remote_blkio_param_value_u.ui;
            break;
        case VIR_DOMAIN_BLKIO_PARAM_LLONG:
            params[i].value.l =
                ret.params.params_val[i].value.remote_blkio_param_value_u.l;
            break;
        case VIR_DOMAIN_BLKIO_PARAM_ULLONG:
            params[i].value.ul =
                ret.params.params_val[i].value.remote_blkio_param_value_u.ul;
            break;
        case VIR_DOMAIN_BLKIO_PARAM_DOUBLE:
            params[i].value.d =
                ret.params.params_val[i].value.remote_blkio_param_value_u.d;
            break;
        case VIR_DOMAIN_BLKIO_PARAM_BOOLEAN:
            params[i].value.b =
                ret.params.params_val[i].value.remote_blkio_param_value_u.b;
            break;
        default:
            remoteError(VIR_ERR_RPC, "%s",
                        _("remoteDomainGetBlkioParameters: "
                          "unknown parameter type"));
            goto cleanup;
        }
    }

    rv = 0;

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

2828 2829 2830
static int
remoteDomainGetInfo (virDomainPtr domain, virDomainInfoPtr info)
{
2831
    int rv = -1;
2832 2833
    remote_domain_get_info_args args;
    remote_domain_get_info_ret ret;
2834
    struct private_data *priv = domain->conn->privateData;
2835

2836 2837
    remoteDriverLock(priv);

2838 2839 2840 2841 2842 2843
    make_nonnull_domain (&args.dom, domain);

    memset (&ret, 0, sizeof ret);
    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_INFO,
              (xdrproc_t) xdr_remote_domain_get_info_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_get_info_ret, (char *) &ret) == -1)
2844
        goto done;
2845 2846 2847 2848 2849 2850

    info->state = ret.state;
    info->maxMem = ret.max_mem;
    info->memory = ret.memory;
    info->nrVirtCpu = ret.nr_virt_cpu;
    info->cpuTime = ret.cpu_time;
2851

2852 2853 2854
    rv = 0;

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

static int
remoteDomainSave (virDomainPtr domain, const char *to)
{
2862
    int rv = -1;
2863
    remote_domain_save_args args;
2864
    struct private_data *priv = domain->conn->privateData;
2865

2866 2867
    remoteDriverLock(priv);

2868 2869 2870 2871 2872 2873
    make_nonnull_domain (&args.dom, domain);
    args.to = (char *) to;

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SAVE,
              (xdrproc_t) xdr_remote_domain_save_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
2874
        goto done;
2875

2876 2877 2878
    rv = 0;

done:
2879
    remoteDriverUnlock(priv);
2880
    return rv;
2881 2882 2883 2884 2885
}

static int
remoteDomainRestore (virConnectPtr conn, const char *from)
{
2886
    int rv = -1;
2887
    remote_domain_restore_args args;
2888
    struct private_data *priv = conn->privateData;
2889

2890 2891
    remoteDriverLock(priv);

2892 2893 2894 2895 2896
    args.from = (char *) from;

    if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_RESTORE,
              (xdrproc_t) xdr_remote_domain_restore_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
2897
        goto done;
2898

2899 2900 2901
    rv = 0;

done:
2902
    remoteDriverUnlock(priv);
2903
    return rv;
2904 2905 2906 2907 2908
}

static int
remoteDomainCoreDump (virDomainPtr domain, const char *to, int flags)
{
2909
    int rv = -1;
2910
    remote_domain_core_dump_args args;
2911
    struct private_data *priv = domain->conn->privateData;
2912

2913 2914
    remoteDriverLock(priv);

2915 2916 2917 2918 2919 2920 2921
    make_nonnull_domain (&args.dom, domain);
    args.to = (char *) to;
    args.flags = flags;

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_CORE_DUMP,
              (xdrproc_t) xdr_remote_domain_core_dump_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
2922
        goto done;
2923

2924 2925 2926
    rv = 0;

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

static int
remoteDomainSetVcpus (virDomainPtr domain, unsigned int nvcpus)
{
2934
    int rv = -1;
2935
    remote_domain_set_vcpus_args args;
2936
    struct private_data *priv = domain->conn->privateData;
2937

2938 2939
    remoteDriverLock(priv);

2940 2941 2942 2943 2944 2945
    make_nonnull_domain (&args.dom, domain);
    args.nvcpus = nvcpus;

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SET_VCPUS,
              (xdrproc_t) xdr_remote_domain_set_vcpus_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
2946
        goto done;
2947

2948 2949 2950
    rv = 0;

done:
2951
    remoteDriverUnlock(priv);
2952
    return rv;
2953 2954
}

E
Eric Blake 已提交
2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007
static int
remoteDomainSetVcpusFlags (virDomainPtr domain, unsigned int nvcpus,
                           unsigned int flags)
{
    int rv = -1;
    remote_domain_set_vcpus_flags_args args;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SET_VCPUS_FLAGS,
              (xdrproc_t) xdr_remote_domain_set_vcpus_flags_args,
              (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteDomainGetVcpusFlags (virDomainPtr domain, unsigned int flags)
{
    int rv = -1;
    remote_domain_get_vcpus_flags_args args;
    remote_domain_get_vcpus_flags_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_VCPUS_FLAGS,
              (xdrproc_t) xdr_remote_domain_get_vcpus_flags_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_get_vcpus_flags_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.num;

done:
    remoteDriverUnlock(priv);
    return rv;
}

3008 3009 3010 3011 3012 3013
static int
remoteDomainPinVcpu (virDomainPtr domain,
                     unsigned int vcpu,
                     unsigned char *cpumap,
                     int maplen)
{
3014
    int rv = -1;
3015
    remote_domain_pin_vcpu_args args;
3016
    struct private_data *priv = domain->conn->privateData;
3017

3018 3019
    remoteDriverLock(priv);

3020
    if (maplen > REMOTE_CPUMAP_MAX) {
3021 3022 3023
        remoteError(VIR_ERR_RPC,
                    _("map length greater than maximum: %d > %d"),
                    maplen, REMOTE_CPUMAP_MAX);
3024
        goto done;
3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
    }

    make_nonnull_domain (&args.dom, domain);
    args.vcpu = vcpu;
    args.cpumap.cpumap_len = maplen;
    args.cpumap.cpumap_val = (char *) cpumap;

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_PIN_VCPU,
              (xdrproc_t) xdr_remote_domain_pin_vcpu_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
3035
        goto done;
3036

3037 3038 3039
    rv = 0;

done:
3040
    remoteDriverUnlock(priv);
3041
    return rv;
3042 3043 3044 3045 3046 3047 3048 3049 3050
}

static int
remoteDomainGetVcpus (virDomainPtr domain,
                      virVcpuInfoPtr info,
                      int maxinfo,
                      unsigned char *cpumaps,
                      int maplen)
{
3051
    int rv = -1;
3052 3053 3054
    int i;
    remote_domain_get_vcpus_args args;
    remote_domain_get_vcpus_ret ret;
3055
    struct private_data *priv = domain->conn->privateData;
3056

3057 3058
    remoteDriverLock(priv);

3059
    if (maxinfo > REMOTE_VCPUINFO_MAX) {
3060 3061 3062
        remoteError(VIR_ERR_RPC,
                    _("vCPU count exceeds maximum: %d > %d"),
                    maxinfo, REMOTE_VCPUINFO_MAX);
3063
        goto done;
3064
    }
3065
    if (maxinfo * maplen > REMOTE_CPUMAPS_MAX) {
3066 3067 3068
        remoteError(VIR_ERR_RPC,
                    _("vCPU map buffer length exceeds maximum: %d > %d"),
                    maxinfo * maplen, REMOTE_CPUMAPS_MAX);
3069
        goto done;
3070 3071 3072 3073 3074 3075 3076 3077 3078 3079
    }

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

    if (ret.info.info_len > maxinfo) {
3083 3084 3085
        remoteError(VIR_ERR_RPC,
                    _("host reports too many vCPUs: %d > %d"),
                    ret.info.info_len, maxinfo);
3086
        goto cleanup;
3087
    }
3088
    if (ret.cpumaps.cpumaps_len > maxinfo * maplen) {
3089 3090 3091
        remoteError(VIR_ERR_RPC,
                    _("host reports map buffer length exceeds maximum: %d > %d"),
                    ret.cpumaps.cpumaps_len, maxinfo * maplen);
3092
        goto cleanup;
3093 3094
    }

3095 3096 3097
    memset (info, 0, sizeof (virVcpuInfo) * maxinfo);
    memset (cpumaps, 0, maxinfo * maplen);

3098 3099 3100 3101 3102 3103 3104 3105 3106 3107
    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];

3108 3109 3110
    rv = ret.info.info_len;

cleanup:
3111
    xdr_free ((xdrproc_t) xdr_remote_domain_get_vcpus_ret, (char *) &ret);
3112 3113

done:
3114
    remoteDriverUnlock(priv);
3115
    return rv;
3116 3117 3118 3119 3120
}

static int
remoteDomainGetMaxVcpus (virDomainPtr domain)
{
3121
    int rv = -1;
3122 3123
    remote_domain_get_max_vcpus_args args;
    remote_domain_get_max_vcpus_ret ret;
3124
    struct private_data *priv = domain->conn->privateData;
3125

3126 3127
    remoteDriverLock(priv);

3128 3129 3130
    make_nonnull_domain (&args.dom, domain);

    memset (&ret, 0, sizeof ret);
3131
    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_MAX_VCPUS,
3132 3133
              (xdrproc_t) xdr_remote_domain_get_max_vcpus_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_get_max_vcpus_ret, (char *) &ret) == -1)
3134
        goto done;
3135

3136 3137 3138
    rv = ret.num;

done:
3139
    remoteDriverUnlock(priv);
3140
    return rv;
3141 3142
}

3143 3144 3145 3146 3147 3148
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;
3149 3150 3151
    int rv = -1;

    remoteDriverLock(priv);
3152 3153 3154

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

3157 3158 3159
    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) {
3160
        goto done;
3161 3162 3163 3164
    }

    if (ret.label.label_val != NULL) {
        if (strlen (ret.label.label_val) >= sizeof seclabel->label) {
3165 3166
            remoteError(VIR_ERR_RPC, _("security label exceeds maximum: %zd"),
                        sizeof seclabel->label - 1);
3167
            goto done;
3168 3169 3170 3171 3172
        }
        strcpy (seclabel->label, ret.label.label_val);
        seclabel->enforcing = ret.enforcing;
    }

3173 3174 3175 3176 3177
    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
3178 3179 3180 3181 3182 3183 3184
}

static int
remoteNodeGetSecurityModel (virConnectPtr conn, virSecurityModelPtr secmodel)
{
    remote_node_get_security_model_ret ret;
    struct private_data *priv = conn->privateData;
3185 3186 3187
    int rv = -1;

    remoteDriverLock(priv);
3188 3189

    memset (&ret, 0, sizeof ret);
3190 3191
    memset (secmodel, 0, sizeof (*secmodel));

3192 3193 3194
    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) {
3195
        goto done;
3196 3197 3198 3199
    }

    if (ret.model.model_val != NULL) {
        if (strlen (ret.model.model_val) >= sizeof secmodel->model) {
3200 3201
            remoteError(VIR_ERR_RPC, _("security model exceeds maximum: %zd"),
                        sizeof secmodel->model - 1);
3202
            goto done;
3203 3204 3205 3206 3207 3208
        }
        strcpy (secmodel->model, ret.model.model_val);
    }

    if (ret.doi.doi_val != NULL) {
        if (strlen (ret.doi.doi_val) >= sizeof secmodel->doi) {
3209 3210
            remoteError(VIR_ERR_RPC, _("security doi exceeds maximum: %zd"),
                        sizeof secmodel->doi - 1);
3211
            goto done;
3212 3213 3214
        }
        strcpy (secmodel->doi, ret.doi.doi_val);
    }
3215 3216 3217 3218 3219 3220

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
3221 3222
}

3223 3224 3225
static char *
remoteDomainDumpXML (virDomainPtr domain, int flags)
{
3226
    char *rv = NULL;
3227 3228
    remote_domain_dump_xml_args args;
    remote_domain_dump_xml_ret ret;
3229
    struct private_data *priv = domain->conn->privateData;
3230

3231 3232
    remoteDriverLock(priv);

3233 3234 3235 3236 3237 3238 3239
    make_nonnull_domain (&args.dom, domain);
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_DUMP_XML,
              (xdrproc_t) xdr_remote_domain_dump_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_dump_xml_ret, (char *) &ret) == -1)
3240
        goto done;
3241 3242

    /* Caller frees. */
3243 3244 3245
    rv = ret.xml;

done:
3246
    remoteDriverUnlock(priv);
3247
    return rv;
3248 3249
}

3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311
static char *
remoteDomainXMLFromNative (virConnectPtr conn,
                           const char *format,
                           const char *config,
                           unsigned int flags)
{
    char *rv = NULL;
    remote_domain_xml_from_native_args args;
    remote_domain_xml_from_native_ret ret;
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.nativeFormat = (char *)format;
    args.nativeConfig = (char *)config;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_XML_FROM_NATIVE,
              (xdrproc_t) xdr_remote_domain_xml_from_native_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_xml_from_native_ret, (char *) &ret) == -1)
        goto done;

    /* Caller frees. */
    rv = ret.domainXml;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static char *
remoteDomainXMLToNative (virConnectPtr conn,
                         const char *format,
                         const char *xml,
                         unsigned int flags)
{
    char *rv = NULL;
    remote_domain_xml_to_native_args args;
    remote_domain_xml_to_native_ret ret;
    struct private_data *priv = conn->privateData;

    remoteDriverLock(priv);

    args.nativeFormat = (char *)format;
    args.domainXml = (char *)xml;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_XML_TO_NATIVE,
              (xdrproc_t) xdr_remote_domain_xml_to_native_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_xml_to_native_ret, (char *) &ret) == -1)
        goto done;

    /* Caller frees. */
    rv = ret.nativeConfig;

done:
    remoteDriverUnlock(priv);
    return rv;
}

3312 3313 3314 3315 3316 3317 3318
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)
{
3319
    int rv = -1;
3320 3321
    remote_domain_migrate_prepare_args args;
    remote_domain_migrate_prepare_ret ret;
3322
    struct private_data *priv = dconn->privateData;
3323

3324 3325
    remoteDriverLock(priv);

3326 3327 3328 3329 3330 3331 3332 3333 3334
    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)
3335
        goto done;
3336 3337 3338 3339 3340 3341 3342 3343

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

3344 3345 3346
    rv = 0;

done:
3347
    remoteDriverUnlock(priv);
3348
    return rv;
3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359
}

static int
remoteDomainMigratePerform (virDomainPtr domain,
                            const char *cookie,
                            int cookielen,
                            const char *uri,
                            unsigned long flags,
                            const char *dname,
                            unsigned long resource)
{
3360
    int rv = -1;
3361
    remote_domain_migrate_perform_args args;
3362
    struct private_data *priv = domain->conn->privateData;
3363

3364 3365
    remoteDriverLock(priv);

3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376
    make_nonnull_domain (&args.dom, domain);
    args.cookie.cookie_len = cookielen;
    args.cookie.cookie_val = (char *) cookie;
    args.uri = (char *) uri;
    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_PERFORM,
              (xdrproc_t) xdr_remote_domain_migrate_perform_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
3377
        goto done;
3378

3379 3380 3381
    rv = 0;

done:
3382
    remoteDriverUnlock(priv);
3383
    return rv;
3384 3385 3386 3387 3388 3389 3390 3391 3392 3393
}

static virDomainPtr
remoteDomainMigrateFinish (virConnectPtr dconn,
                           const char *dname,
                           const char *cookie,
                           int cookielen,
                           const char *uri,
                           unsigned long flags)
{
3394
    virDomainPtr ddom = NULL;
3395 3396
    remote_domain_migrate_finish_args args;
    remote_domain_migrate_finish_ret ret;
3397
    struct private_data *priv = dconn->privateData;
3398

3399 3400
    remoteDriverLock(priv);

3401 3402 3403 3404 3405 3406 3407 3408 3409 3410
    args.dname = (char *) dname;
    args.cookie.cookie_len = cookielen;
    args.cookie.cookie_val = (char *) cookie;
    args.uri = (char *) uri;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (dconn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_FINISH,
              (xdrproc_t) xdr_remote_domain_migrate_finish_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_migrate_finish_ret, (char *) &ret) == -1)
3411
        goto done;
3412 3413 3414 3415

    ddom = get_nonnull_domain (dconn, ret.ddom);
    xdr_free ((xdrproc_t) &xdr_remote_domain_migrate_finish_ret, (char *) &ret);

3416
done:
3417
    remoteDriverUnlock(priv);
3418 3419 3420
    return ddom;
}

D
Daniel Veillard 已提交
3421 3422 3423 3424 3425 3426 3427 3428
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)
{
3429
    int rv = -1;
D
Daniel Veillard 已提交
3430 3431
    remote_domain_migrate_prepare2_args args;
    remote_domain_migrate_prepare2_ret ret;
3432
    struct private_data *priv = dconn->privateData;
D
Daniel Veillard 已提交
3433

3434 3435
    remoteDriverLock(priv);

D
Daniel Veillard 已提交
3436 3437 3438 3439 3440 3441 3442 3443 3444 3445
    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)
3446
        goto done;
D
Daniel Veillard 已提交
3447 3448

    if (ret.cookie.cookie_len > 0) {
3449 3450 3451 3452 3453
        if (!cookie || !cookielen) {
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("caller ignores cookie or cookielen"));
            goto error;
        }
D
Daniel Veillard 已提交
3454 3455 3456
        *cookie = ret.cookie.cookie_val; /* Caller frees. */
        *cookielen = ret.cookie.cookie_len;
    }
3457 3458 3459 3460 3461 3462
    if (ret.uri_out) {
        if (!uri_out) {
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("caller ignores uri_out"));
            goto error;
        }
D
Daniel Veillard 已提交
3463
        *uri_out = *ret.uri_out; /* Caller frees. */
3464
    }
D
Daniel Veillard 已提交
3465

3466 3467 3468
    rv = 0;

done:
3469
    remoteDriverUnlock(priv);
3470
    return rv;
3471 3472 3473 3474 3475 3476
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 已提交
3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487
}

static virDomainPtr
remoteDomainMigrateFinish2 (virConnectPtr dconn,
                            const char *dname,
                            const char *cookie,
                            int cookielen,
                            const char *uri,
                            unsigned long flags,
                            int retcode)
{
3488
    virDomainPtr ddom = NULL;
D
Daniel Veillard 已提交
3489 3490
    remote_domain_migrate_finish2_args args;
    remote_domain_migrate_finish2_ret ret;
3491
    struct private_data *priv = dconn->privateData;
D
Daniel Veillard 已提交
3492

3493 3494
    remoteDriverLock(priv);

D
Daniel Veillard 已提交
3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505
    args.dname = (char *) dname;
    args.cookie.cookie_len = cookielen;
    args.cookie.cookie_val = (char *) cookie;
    args.uri = (char *) uri;
    args.flags = flags;
    args.retcode = retcode;

    memset (&ret, 0, sizeof ret);
    if (call (dconn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_FINISH2,
              (xdrproc_t) xdr_remote_domain_migrate_finish2_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_migrate_finish2_ret, (char *) &ret) == -1)
3506
        goto done;
D
Daniel Veillard 已提交
3507 3508 3509 3510

    ddom = get_nonnull_domain (dconn, ret.ddom);
    xdr_free ((xdrproc_t) &xdr_remote_domain_migrate_finish2_ret, (char *) &ret);

3511
done:
3512
    remoteDriverUnlock(priv);
D
Daniel Veillard 已提交
3513 3514 3515
    return ddom;
}

3516 3517 3518
static int
remoteListDefinedDomains (virConnectPtr conn, char **const names, int maxnames)
{
3519
    int rv = -1;
3520 3521 3522
    int i;
    remote_list_defined_domains_args args;
    remote_list_defined_domains_ret ret;
3523
    struct private_data *priv = conn->privateData;
3524

3525 3526
    remoteDriverLock(priv);

3527
    if (maxnames > REMOTE_DOMAIN_NAME_LIST_MAX) {
3528 3529 3530
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain names: %d > %d"),
                    maxnames, REMOTE_DOMAIN_NAME_LIST_MAX);
3531
        goto done;
3532 3533 3534 3535 3536 3537 3538
    }
    args.maxnames = maxnames;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_LIST_DEFINED_DOMAINS,
              (xdrproc_t) xdr_remote_list_defined_domains_args, (char *) &args,
              (xdrproc_t) xdr_remote_list_defined_domains_ret, (char *) &ret) == -1)
3539
        goto done;
3540 3541

    if (ret.names.names_len > maxnames) {
3542 3543 3544
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain names: %d > %d"),
                    ret.names.names_len, maxnames);
3545
        goto cleanup;
3546 3547 3548 3549 3550 3551 3552
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
3553
    for (i = 0; i < ret.names.names_len; ++i) {
3554 3555
        names[i] = strdup (ret.names.names_val[i]);

3556 3557 3558 3559
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

3560
            virReportOOMError();
3561 3562 3563 3564
            goto cleanup;
        }
    }

3565 3566 3567
    rv = ret.names.names_len;

cleanup:
3568 3569
    xdr_free ((xdrproc_t) xdr_remote_list_defined_domains_ret, (char *) &ret);

3570
done:
3571
    remoteDriverUnlock(priv);
3572
    return rv;
3573 3574 3575 3576 3577
}

static int
remoteNumOfDefinedDomains (virConnectPtr conn)
{
3578
    int rv = -1;
3579
    remote_num_of_defined_domains_ret ret;
3580
    struct private_data *priv = conn->privateData;
3581

3582 3583
    remoteDriverLock(priv);

3584 3585 3586 3587
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NUM_OF_DEFINED_DOMAINS,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_num_of_defined_domains_ret, (char *) &ret) == -1)
3588
        goto done;
3589

3590 3591 3592
    rv = ret.num;

done:
3593
    remoteDriverUnlock(priv);
3594
    return rv;
3595 3596 3597 3598 3599
}

static int
remoteDomainCreate (virDomainPtr domain)
{
3600
    int rv = -1;
3601
    remote_domain_create_args args;
3602 3603
    remote_domain_lookup_by_uuid_args args2;
    remote_domain_lookup_by_uuid_ret ret2;
3604
    struct private_data *priv = domain->conn->privateData;
3605

3606 3607
    remoteDriverLock(priv);

3608 3609 3610 3611 3612
    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)
3613
        goto done;
3614

3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628
    /* 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);

3629 3630 3631
    rv = 0;

done:
3632
    remoteDriverUnlock(priv);
3633
    return rv;
3634 3635
}

3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667
static int
remoteDomainCreateWithFlags (virDomainPtr domain, unsigned int flags)
{
    int rv = -1;
    remote_domain_create_with_flags_args args;
    remote_domain_create_with_flags_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_CREATE_WITH_FLAGS,
              (xdrproc_t) xdr_remote_domain_create_with_flags_args,
              (char *) &args,
              (xdrproc_t) xdr_remote_domain_create_with_flags_ret,
              (char *) &ret) == -1)
        goto done;

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

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

3668 3669 3670
static virDomainPtr
remoteDomainDefineXML (virConnectPtr conn, const char *xml)
{
3671
    virDomainPtr dom = NULL;
3672 3673
    remote_domain_define_xml_args args;
    remote_domain_define_xml_ret ret;
3674
    struct private_data *priv = conn->privateData;
3675

3676 3677
    remoteDriverLock(priv);

3678 3679 3680 3681 3682 3683
    args.xml = (char *) xml;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_DOMAIN_DEFINE_XML,
              (xdrproc_t) xdr_remote_domain_define_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_define_xml_ret, (char *) &ret) == -1)
3684
        goto done;
3685 3686 3687 3688

    dom = get_nonnull_domain (conn, ret.dom);
    xdr_free ((xdrproc_t) xdr_remote_domain_define_xml_ret, (char *) &ret);

3689
done:
3690
    remoteDriverUnlock(priv);
3691 3692 3693 3694 3695 3696
    return dom;
}

static int
remoteDomainUndefine (virDomainPtr domain)
{
3697
    int rv = -1;
3698
    remote_domain_undefine_args args;
3699
    struct private_data *priv = domain->conn->privateData;
3700

3701 3702
    remoteDriverLock(priv);

3703 3704 3705 3706 3707
    make_nonnull_domain (&args.dom, domain);

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_UNDEFINE,
              (xdrproc_t) xdr_remote_domain_undefine_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
3708
        goto done;
3709

3710 3711 3712
    rv = 0;

done:
3713
    remoteDriverUnlock(priv);
3714
    return rv;
3715 3716 3717
}

static int
3718
remoteDomainAttachDevice (virDomainPtr domain, const char *xml)
3719
{
3720
    int rv = -1;
3721
    remote_domain_attach_device_args args;
3722
    struct private_data *priv = domain->conn->privateData;
3723

3724 3725
    remoteDriverLock(priv);

3726
    make_nonnull_domain (&args.dom, domain);
3727
    args.xml = (char *) xml;
3728 3729 3730 3731

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_ATTACH_DEVICE,
              (xdrproc_t) xdr_remote_domain_attach_device_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
3732
        goto done;
3733

3734 3735 3736
    rv = 0;

done:
3737
    remoteDriverUnlock(priv);
3738
    return rv;
3739 3740
}

J
Jim Fehlig 已提交
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
static int
remoteDomainAttachDeviceFlags (virDomainPtr domain, const char *xml,
                               unsigned int flags)
{
    int rv = -1;
    remote_domain_attach_device_flags_args args;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_ATTACH_DEVICE_FLAGS,
              (xdrproc_t) xdr_remote_domain_attach_device_flags_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

3767
static int
3768
remoteDomainDetachDevice (virDomainPtr domain, const char *xml)
3769
{
3770
    int rv = -1;
3771
    remote_domain_detach_device_args args;
3772
    struct private_data *priv = domain->conn->privateData;
3773

3774 3775
    remoteDriverLock(priv);

3776
    make_nonnull_domain (&args.dom, domain);
3777
    args.xml = (char *) xml;
3778 3779 3780 3781

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_DETACH_DEVICE,
              (xdrproc_t) xdr_remote_domain_detach_device_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
3782
        goto done;
3783

3784 3785 3786
    rv = 0;

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

J
Jim Fehlig 已提交
3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816
static int
remoteDomainDetachDeviceFlags (virDomainPtr domain, const char *xml,
                               unsigned int flags)
{
    int rv = -1;
    remote_domain_detach_device_flags_args args;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_DETACH_DEVICE_FLAGS,
              (xdrproc_t) xdr_remote_domain_detach_device_flags_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842
static int
remoteDomainUpdateDeviceFlags (virDomainPtr domain, const char *xml,
                               unsigned int flags)
{
    int rv = -1;
    remote_domain_update_device_flags_args args;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_UPDATE_DEVICE_FLAGS,
              (xdrproc_t) xdr_remote_domain_update_device_flags_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

3843 3844 3845
static int
remoteDomainGetAutostart (virDomainPtr domain, int *autostart)
{
3846
    int rv = -1;
3847 3848
    remote_domain_get_autostart_args args;
    remote_domain_get_autostart_ret ret;
3849
    struct private_data *priv = domain->conn->privateData;
3850

3851 3852
    remoteDriverLock(priv);

3853 3854 3855 3856 3857 3858
    make_nonnull_domain (&args.dom, domain);

    memset (&ret, 0, sizeof ret);
    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_AUTOSTART,
              (xdrproc_t) xdr_remote_domain_get_autostart_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_get_autostart_ret, (char *) &ret) == -1)
3859
        goto done;
3860 3861

    if (autostart) *autostart = ret.autostart;
3862 3863 3864
    rv = 0;

done:
3865
    remoteDriverUnlock(priv);
3866
    return rv;
3867 3868 3869 3870 3871
}

static int
remoteDomainSetAutostart (virDomainPtr domain, int autostart)
{
3872
    int rv = -1;
3873
    remote_domain_set_autostart_args args;
3874
    struct private_data *priv = domain->conn->privateData;
3875

3876 3877
    remoteDriverLock(priv);

3878 3879 3880 3881 3882 3883
    make_nonnull_domain (&args.dom, domain);
    args.autostart = autostart;

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SET_AUTOSTART,
              (xdrproc_t) xdr_remote_domain_set_autostart_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
3884
        goto done;
3885

3886 3887 3888
    rv = 0;

done:
3889
    remoteDriverUnlock(priv);
3890
    return rv;
3891 3892
}

3893 3894 3895
static char *
remoteDomainGetSchedulerType (virDomainPtr domain, int *nparams)
{
3896
    char *rv = NULL;
3897 3898
    remote_domain_get_scheduler_type_args args;
    remote_domain_get_scheduler_type_ret ret;
3899
    struct private_data *priv = domain->conn->privateData;
3900

3901 3902
    remoteDriverLock(priv);

3903 3904 3905 3906 3907 3908
    make_nonnull_domain (&args.dom, domain);

    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)
3909
        goto done;
3910 3911 3912 3913

    if (nparams) *nparams = ret.nparams;

    /* Caller frees this. */
3914 3915 3916
    rv = ret.type;

done:
3917
    remoteDriverUnlock(priv);
3918
    return rv;
3919 3920 3921 3922 3923 3924
}

static int
remoteDomainGetSchedulerParameters (virDomainPtr domain,
                                    virSchedParameterPtr params, int *nparams)
{
3925
    int rv = -1;
3926 3927
    remote_domain_get_scheduler_parameters_args args;
    remote_domain_get_scheduler_parameters_ret ret;
3928
    int i = -1;
3929
    struct private_data *priv = domain->conn->privateData;
3930

3931 3932
    remoteDriverLock(priv);

3933 3934 3935 3936 3937 3938 3939
    make_nonnull_domain (&args.dom, domain);
    args.nparams = *nparams;

    memset (&ret, 0, sizeof ret);
    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_SCHEDULER_PARAMETERS,
              (xdrproc_t) xdr_remote_domain_get_scheduler_parameters_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_get_scheduler_parameters_ret, (char *) &ret) == -1)
3940
        goto done;
3941 3942 3943 3944

    /* Check the length of the returned list carefully. */
    if (ret.params.params_len > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX ||
        ret.params.params_len > *nparams) {
3945 3946 3947
        remoteError(VIR_ERR_RPC, "%s",
                    _("remoteDomainGetSchedulerParameters: "
                      "returned number of parameters exceeds limit"));
3948
        goto cleanup;
3949 3950 3951 3952 3953
    }
    *nparams = ret.params.params_len;

    /* Deserialise the result. */
    for (i = 0; i < *nparams; ++i) {
C
Chris Lalancette 已提交
3954
        if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) == NULL) {
3955 3956 3957
            remoteError(VIR_ERR_INTERNAL_ERROR,
                        _("Parameter %s too big for destination"),
                        ret.params.params_val[i].field);
C
Chris Lalancette 已提交
3958 3959
            goto cleanup;
        }
3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974
        params[i].type = ret.params.params_val[i].value.type;
        switch (params[i].type) {
        case VIR_DOMAIN_SCHED_FIELD_INT:
            params[i].value.i = ret.params.params_val[i].value.remote_sched_param_value_u.i; break;
        case VIR_DOMAIN_SCHED_FIELD_UINT:
            params[i].value.ui = ret.params.params_val[i].value.remote_sched_param_value_u.ui; break;
        case VIR_DOMAIN_SCHED_FIELD_LLONG:
            params[i].value.l = ret.params.params_val[i].value.remote_sched_param_value_u.l; break;
        case VIR_DOMAIN_SCHED_FIELD_ULLONG:
            params[i].value.ul = ret.params.params_val[i].value.remote_sched_param_value_u.ul; break;
        case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
            params[i].value.d = ret.params.params_val[i].value.remote_sched_param_value_u.d; break;
        case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
            params[i].value.b = ret.params.params_val[i].value.remote_sched_param_value_u.b; break;
        default:
3975 3976 3977
            remoteError(VIR_ERR_RPC, "%s",
                        _("remoteDomainGetSchedulerParameters: "
                          "unknown parameter type"));
3978
            goto cleanup;
3979 3980 3981
        }
    }

3982 3983 3984
    rv = 0;

cleanup:
3985
    xdr_free ((xdrproc_t) xdr_remote_domain_get_scheduler_parameters_ret, (char *) &ret);
3986
done:
3987
    remoteDriverUnlock(priv);
3988
    return rv;
3989 3990 3991 3992 3993 3994
}

static int
remoteDomainSetSchedulerParameters (virDomainPtr domain,
                                    virSchedParameterPtr params, int nparams)
{
3995
    int rv = -1;
3996 3997
    remote_domain_set_scheduler_parameters_args args;
    int i, do_error;
3998
    struct private_data *priv = domain->conn->privateData;
3999

4000 4001
    remoteDriverLock(priv);

4002 4003 4004 4005
    make_nonnull_domain (&args.dom, domain);

    /* Serialise the scheduler parameters. */
    args.params.params_len = nparams;
4006
    if (VIR_ALLOC_N(args.params.params_val, nparams) < 0) {
4007
        virReportOOMError();
4008
        goto done;
4009 4010 4011 4012
    }

    do_error = 0;
    for (i = 0; i < nparams; ++i) {
4013
        /* call() will free this: */
4014 4015
        args.params.params_val[i].field = strdup (params[i].field);
        if (args.params.params_val[i].field == NULL) {
4016
            virReportOOMError();
4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033
            do_error = 1;
        }
        args.params.params_val[i].value.type = params[i].type;
        switch (params[i].type) {
        case VIR_DOMAIN_SCHED_FIELD_INT:
            args.params.params_val[i].value.remote_sched_param_value_u.i = params[i].value.i; break;
        case VIR_DOMAIN_SCHED_FIELD_UINT:
            args.params.params_val[i].value.remote_sched_param_value_u.ui = params[i].value.ui; break;
        case VIR_DOMAIN_SCHED_FIELD_LLONG:
            args.params.params_val[i].value.remote_sched_param_value_u.l = params[i].value.l; break;
        case VIR_DOMAIN_SCHED_FIELD_ULLONG:
            args.params.params_val[i].value.remote_sched_param_value_u.ul = params[i].value.ul; break;
        case VIR_DOMAIN_SCHED_FIELD_DOUBLE:
            args.params.params_val[i].value.remote_sched_param_value_u.d = params[i].value.d; break;
        case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
            args.params.params_val[i].value.remote_sched_param_value_u.b = params[i].value.b; break;
        default:
4034
            remoteError(VIR_ERR_RPC, "%s", _("unknown parameter type"));
4035 4036 4037 4038 4039 4040
            do_error = 1;
        }
    }

    if (do_error) {
        xdr_free ((xdrproc_t) xdr_remote_domain_set_scheduler_parameters_args, (char *) &args);
4041
        goto done;
4042 4043 4044 4045 4046
    }

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SET_SCHEDULER_PARAMETERS,
              (xdrproc_t) xdr_remote_domain_set_scheduler_parameters_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
4047
        goto done;
4048

4049 4050 4051
    rv = 0;

done:
4052
    remoteDriverUnlock(priv);
4053
    return rv;
4054 4055
}

4056 4057 4058 4059
static int
remoteDomainBlockStats (virDomainPtr domain, const char *path,
                        struct _virDomainBlockStats *stats)
{
4060
    int rv = -1;
4061 4062
    remote_domain_block_stats_args args;
    remote_domain_block_stats_ret ret;
4063
    struct private_data *priv = domain->conn->privateData;
4064

4065 4066
    remoteDriverLock(priv);

4067 4068 4069 4070 4071 4072 4073 4074
    make_nonnull_domain (&args.dom, domain);
    args.path = (char *) path;

    memset (&ret, 0, sizeof ret);
    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_BLOCK_STATS,
              (xdrproc_t) xdr_remote_domain_block_stats_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_block_stats_ret, (char *) &ret)
        == -1)
4075
        goto done;
4076 4077 4078 4079 4080 4081 4082

    stats->rd_req = ret.rd_req;
    stats->rd_bytes = ret.rd_bytes;
    stats->wr_req = ret.wr_req;
    stats->wr_bytes = ret.wr_bytes;
    stats->errs = ret.errs;

4083 4084 4085
    rv = 0;

done:
4086
    remoteDriverUnlock(priv);
4087
    return rv;
4088 4089 4090 4091 4092 4093
}

static int
remoteDomainInterfaceStats (virDomainPtr domain, const char *path,
                            struct _virDomainInterfaceStats *stats)
{
4094
    int rv = -1;
4095 4096
    remote_domain_interface_stats_args args;
    remote_domain_interface_stats_ret ret;
4097
    struct private_data *priv = domain->conn->privateData;
4098

4099 4100
    remoteDriverLock(priv);

4101 4102 4103 4104 4105 4106 4107 4108 4109
    make_nonnull_domain (&args.dom, domain);
    args.path = (char *) path;

    memset (&ret, 0, sizeof ret);
    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_INTERFACE_STATS,
              (xdrproc_t) xdr_remote_domain_interface_stats_args,
                (char *) &args,
              (xdrproc_t) xdr_remote_domain_interface_stats_ret,
                (char *) &ret) == -1)
4110
        goto done;
4111 4112 4113 4114 4115 4116 4117 4118 4119 4120

    stats->rx_bytes = ret.rx_bytes;
    stats->rx_packets = ret.rx_packets;
    stats->rx_errs = ret.rx_errs;
    stats->rx_drop = ret.rx_drop;
    stats->tx_bytes = ret.tx_bytes;
    stats->tx_packets = ret.tx_packets;
    stats->tx_errs = ret.tx_errs;
    stats->tx_drop = ret.tx_drop;

4121 4122 4123
    rv = 0;

done:
4124
    remoteDriverUnlock(priv);
4125
    return rv;
4126 4127
}

4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142
static int
remoteDomainMemoryStats (virDomainPtr domain,
                         struct _virDomainMemoryStat *stats,
                         unsigned int nr_stats)
{
    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) {
4143 4144 4145
        remoteError(VIR_ERR_RPC,
                    _("too many memory stats requested: %d > %d"), nr_stats,
                    REMOTE_DOMAIN_MEMORY_STATS_MAX);
4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170
        goto done;
    }
    args.maxStats = nr_stats;
    args.flags = 0;
    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;
}

4171 4172 4173 4174 4175 4176 4177 4178
static int
remoteDomainBlockPeek (virDomainPtr domain,
                       const char *path,
                       unsigned long long offset,
                       size_t size,
                       void *buffer,
                       unsigned int flags)
{
4179
    int rv = -1;
4180 4181
    remote_domain_block_peek_args args;
    remote_domain_block_peek_ret ret;
4182
    struct private_data *priv = domain->conn->privateData;
4183

4184 4185
    remoteDriverLock(priv);

4186
    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
4187 4188 4189
        remoteError(VIR_ERR_RPC,
                    _("block peek request too large for remote protocol, %zi > %d"),
                    size, REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX);
4190
        goto done;
4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204
    }

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

    if (ret.buffer.buffer_len != size) {
4208 4209
        remoteError(VIR_ERR_RPC, "%s",
                    _("returned buffer is not same size as requested"));
4210
        goto cleanup;
4211 4212 4213
    }

    memcpy (buffer, ret.buffer.buffer_val, size);
4214 4215 4216
    rv = 0;

cleanup:
4217
    VIR_FREE(ret.buffer.buffer_val);
4218

4219
done:
4220
    remoteDriverUnlock(priv);
4221
    return rv;
4222 4223
}

R
Richard W.M. Jones 已提交
4224 4225 4226 4227 4228 4229 4230
static int
remoteDomainMemoryPeek (virDomainPtr domain,
                        unsigned long long offset,
                        size_t size,
                        void *buffer,
                        unsigned int flags)
{
4231
    int rv = -1;
R
Richard W.M. Jones 已提交
4232 4233
    remote_domain_memory_peek_args args;
    remote_domain_memory_peek_ret ret;
4234
    struct private_data *priv = domain->conn->privateData;
R
Richard W.M. Jones 已提交
4235

4236 4237
    remoteDriverLock(priv);

R
Richard W.M. Jones 已提交
4238
    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
4239 4240 4241
        remoteError(VIR_ERR_RPC,
                    _("memory peek request too large for remote protocol, %zi > %d"),
                    size, REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX);
4242
        goto done;
R
Richard W.M. Jones 已提交
4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255
    }

    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)
4256
        goto done;
R
Richard W.M. Jones 已提交
4257 4258

    if (ret.buffer.buffer_len != size) {
4259 4260
        remoteError(VIR_ERR_RPC, "%s",
                    _("returned buffer is not same size as requested"));
4261
        goto cleanup;
R
Richard W.M. Jones 已提交
4262 4263 4264
    }

    memcpy (buffer, ret.buffer.buffer_val, size);
4265 4266 4267
    rv = 0;

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

4270
done:
4271
    remoteDriverUnlock(priv);
4272
    return rv;
R
Richard W.M. Jones 已提交
4273 4274
}

4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308
static int
remoteDomainGetBlockInfo (virDomainPtr domain,
                          const char *path,
                          virDomainBlockInfoPtr info,
                          unsigned int flags)
{
    int rv = -1;
    remote_domain_get_block_info_args args;
    remote_domain_get_block_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;

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

    info->allocation = ret.allocation;
    info->capacity = ret.capacity;
    info->physical = ret.physical;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381
static int
remoteDomainManagedSave (virDomainPtr domain, unsigned int flags)
{
    int rv = -1;
    remote_domain_managed_save_args args;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_MANAGED_SAVE,
              (xdrproc_t) xdr_remote_domain_managed_save_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteDomainHasManagedSaveImage (virDomainPtr domain, unsigned int flags)
{
    int rv = -1;
    remote_domain_has_managed_save_image_args args;
    remote_domain_has_managed_save_image_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_HAS_MANAGED_SAVE_IMAGE,
              (xdrproc_t) xdr_remote_domain_has_managed_save_image_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_has_managed_save_image_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.ret;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteDomainManagedSaveRemove (virDomainPtr domain, unsigned int flags)
{
    int rv = -1;
    remote_domain_managed_save_remove_args args;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_MANAGED_SAVE_REMOVE,
              (xdrproc_t) xdr_remote_domain_managed_save_remove_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

4382 4383
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
4384
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
4385
remoteNetworkOpen (virConnectPtr conn,
4386
                   virConnectAuthPtr auth,
4387
                   int flags)
4388
{
4389 4390 4391
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
4392
    if (conn->driver &&
4393
        STREQ (conn->driver->name, "remote")) {
4394 4395 4396
        struct private_data *priv;

       /* If we're here, the remote driver is already
4397 4398 4399
         * in use due to a) a QEMU uri, or b) a remote
         * URI. So we can re-use existing connection
         */
4400 4401 4402 4403 4404
        priv = conn->privateData;
        remoteDriverLock(priv);
        priv->localUses++;
        conn->networkPrivateData = priv;
        remoteDriverUnlock(priv);
4405
        return VIR_DRV_OPEN_SUCCESS;
4406 4407 4408
    } else {
        /* Using a non-remote driver, so we need to open a
         * new connection for network APIs, forcing it to
4409 4410
         * use the UNIX transport. This handles Xen driver
         * which doesn't have its own impl of the network APIs.
4411
         */
4412
        struct private_data *priv;
4413 4414 4415 4416 4417 4418
        int ret;
        ret = remoteOpenSecondaryDriver(conn,
                                        auth,
                                        flags,
                                        &priv);
        if (ret == VIR_DRV_OPEN_SUCCESS)
4419 4420 4421
            conn->networkPrivateData = priv;
        return ret;
    }
4422 4423 4424
}

static int
4425 4426
remoteNetworkClose (virConnectPtr conn)
{
4427
    int rv = 0;
4428 4429
    struct private_data *priv = conn->networkPrivateData;

4430 4431 4432 4433 4434 4435 4436 4437
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        rv = doRemoteClose(conn, priv);
        conn->networkPrivateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
4438
    }
4439 4440
    if (priv)
        remoteDriverUnlock(priv);
4441
    return rv;
4442 4443 4444 4445 4446
}

static int
remoteNumOfNetworks (virConnectPtr conn)
{
4447
    int rv = -1;
4448
    remote_num_of_networks_ret ret;
4449
    struct private_data *priv = conn->networkPrivateData;
4450

4451 4452
    remoteDriverLock(priv);

4453 4454 4455 4456
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NUM_OF_NETWORKS,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_num_of_networks_ret, (char *) &ret) == -1)
4457 4458 4459
        goto done;

    rv = ret.num;
4460

4461
done:
4462
    remoteDriverUnlock(priv);
4463
    return rv;
4464 4465 4466 4467 4468
}

static int
remoteListNetworks (virConnectPtr conn, char **const names, int maxnames)
{
4469
    int rv = -1;
4470 4471 4472
    int i;
    remote_list_networks_args args;
    remote_list_networks_ret ret;
4473
    struct private_data *priv = conn->networkPrivateData;
4474

4475 4476
    remoteDriverLock(priv);

4477
    if (maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
4478 4479 4480
        remoteError(VIR_ERR_RPC,
                    _("too many remote networks: %d > %d"),
                    maxnames, REMOTE_NETWORK_NAME_LIST_MAX);
4481
        goto done;
4482 4483 4484 4485 4486 4487 4488
    }
    args.maxnames = maxnames;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_LIST_NETWORKS,
              (xdrproc_t) xdr_remote_list_networks_args, (char *) &args,
              (xdrproc_t) xdr_remote_list_networks_ret, (char *) &ret) == -1)
4489
        goto done;
4490 4491

    if (ret.names.names_len > maxnames) {
4492 4493 4494
        remoteError(VIR_ERR_RPC,
                    _("too many remote networks: %d > %d"),
                    ret.names.names_len, maxnames);
4495
        goto cleanup;
4496 4497 4498 4499 4500 4501 4502
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
4503
    for (i = 0; i < ret.names.names_len; ++i) {
4504 4505
        names[i] = strdup (ret.names.names_val[i]);

4506 4507 4508 4509
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

4510
            virReportOOMError();
4511 4512 4513 4514
            goto cleanup;
        }
    }

4515 4516 4517
    rv = ret.names.names_len;

cleanup:
4518 4519
    xdr_free ((xdrproc_t) xdr_remote_list_networks_ret, (char *) &ret);

4520
done:
4521
    remoteDriverUnlock(priv);
4522
    return rv;
4523 4524 4525 4526 4527
}

static int
remoteNumOfDefinedNetworks (virConnectPtr conn)
{
4528
    int rv = -1;
4529
    remote_num_of_defined_networks_ret ret;
4530
    struct private_data *priv = conn->networkPrivateData;
4531

4532 4533
    remoteDriverLock(priv);

4534 4535 4536 4537
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NUM_OF_DEFINED_NETWORKS,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_num_of_defined_networks_ret, (char *) &ret) == -1)
4538 4539 4540
        goto done;

    rv = ret.num;
4541

4542
done:
4543
    remoteDriverUnlock(priv);
4544
    return rv;
4545 4546 4547 4548 4549 4550
}

static int
remoteListDefinedNetworks (virConnectPtr conn,
                           char **const names, int maxnames)
{
4551
    int rv = -1;
4552 4553 4554
    int i;
    remote_list_defined_networks_args args;
    remote_list_defined_networks_ret ret;
4555
    struct private_data *priv = conn->networkPrivateData;
4556

4557 4558
    remoteDriverLock(priv);

4559
    if (maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
4560 4561 4562
        remoteError(VIR_ERR_RPC,
                    _("too many remote networks: %d > %d"),
                    maxnames, REMOTE_NETWORK_NAME_LIST_MAX);
4563
        goto done;
4564 4565 4566 4567 4568 4569 4570
    }
    args.maxnames = maxnames;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_LIST_DEFINED_NETWORKS,
              (xdrproc_t) xdr_remote_list_defined_networks_args, (char *) &args,
              (xdrproc_t) xdr_remote_list_defined_networks_ret, (char *) &ret) == -1)
4571
        goto done;
4572 4573

    if (ret.names.names_len > maxnames) {
4574 4575 4576
        remoteError(VIR_ERR_RPC,
                    _("too many remote networks: %d > %d"),
                    ret.names.names_len, maxnames);
4577
        goto cleanup;
4578 4579 4580 4581 4582 4583 4584
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
4585
    for (i = 0; i < ret.names.names_len; ++i) {
4586 4587
        names[i] = strdup (ret.names.names_val[i]);

4588 4589 4590 4591
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

4592
            virReportOOMError();
4593 4594 4595 4596
            goto cleanup;
        }
    }

4597 4598 4599
    rv = ret.names.names_len;

cleanup:
4600 4601
    xdr_free ((xdrproc_t) xdr_remote_list_defined_networks_ret, (char *) &ret);

4602
done:
4603
    remoteDriverUnlock(priv);
4604
    return rv;
4605 4606 4607 4608 4609 4610
}

static virNetworkPtr
remoteNetworkLookupByUUID (virConnectPtr conn,
                           const unsigned char *uuid)
{
4611
    virNetworkPtr net = NULL;
4612 4613
    remote_network_lookup_by_uuid_args args;
    remote_network_lookup_by_uuid_ret ret;
4614
    struct private_data *priv = conn->networkPrivateData;
4615

4616 4617
    remoteDriverLock(priv);

4618 4619 4620 4621 4622 4623
    memcpy (args.uuid, uuid, VIR_UUID_BUFLEN);

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NETWORK_LOOKUP_BY_UUID,
              (xdrproc_t) xdr_remote_network_lookup_by_uuid_args, (char *) &args,
              (xdrproc_t) xdr_remote_network_lookup_by_uuid_ret, (char *) &ret) == -1)
4624
        goto done;
4625 4626 4627 4628

    net = get_nonnull_network (conn, ret.net);
    xdr_free ((xdrproc_t) &xdr_remote_network_lookup_by_uuid_ret, (char *) &ret);

4629
done:
4630
    remoteDriverUnlock(priv);
4631 4632 4633 4634 4635 4636 4637
    return net;
}

static virNetworkPtr
remoteNetworkLookupByName (virConnectPtr conn,
                           const char *name)
{
4638
    virNetworkPtr net = NULL;
4639 4640
    remote_network_lookup_by_name_args args;
    remote_network_lookup_by_name_ret ret;
4641
    struct private_data *priv = conn->networkPrivateData;
4642

4643 4644
    remoteDriverLock(priv);

4645 4646 4647 4648 4649 4650
    args.name = (char *) name;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NETWORK_LOOKUP_BY_NAME,
              (xdrproc_t) xdr_remote_network_lookup_by_name_args, (char *) &args,
              (xdrproc_t) xdr_remote_network_lookup_by_name_ret, (char *) &ret) == -1)
4651
        goto done;
4652 4653 4654 4655

    net = get_nonnull_network (conn, ret.net);
    xdr_free ((xdrproc_t) &xdr_remote_network_lookup_by_name_ret, (char *) &ret);

4656
done:
4657
    remoteDriverUnlock(priv);
4658 4659 4660
    return net;
}

4661 4662 4663 4664 4665 4666
static int
remoteNetworkIsActive(virNetworkPtr network)
{
    int rv = -1;
    remote_network_is_active_args args;
    remote_network_is_active_ret ret;
4667
    struct private_data *priv = network->conn->networkPrivateData;
4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690

    remoteDriverLock(priv);

    make_nonnull_network (&args.net, network);

    if (call (network->conn, priv, 0, REMOTE_PROC_NETWORK_IS_ACTIVE,
              (xdrproc_t) xdr_remote_network_is_active_args, (char *) &args,
              (xdrproc_t) xdr_remote_network_is_active_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.active;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteNetworkIsPersistent(virNetworkPtr network)
{
    int rv = -1;
    remote_network_is_persistent_args args;
    remote_network_is_persistent_ret ret;
4691
    struct private_data *priv = network->conn->networkPrivateData;
4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708

    remoteDriverLock(priv);

    make_nonnull_network (&args.net, network);

    if (call (network->conn, priv, 0, REMOTE_PROC_NETWORK_IS_PERSISTENT,
              (xdrproc_t) xdr_remote_network_is_persistent_args, (char *) &args,
              (xdrproc_t) xdr_remote_network_is_persistent_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.persistent;

done:
    remoteDriverUnlock(priv);
    return rv;
}

4709 4710 4711
static virNetworkPtr
remoteNetworkCreateXML (virConnectPtr conn, const char *xmlDesc)
{
4712
    virNetworkPtr net = NULL;
4713 4714
    remote_network_create_xml_args args;
    remote_network_create_xml_ret ret;
4715
    struct private_data *priv = conn->networkPrivateData;
4716

4717 4718
    remoteDriverLock(priv);

4719 4720 4721 4722 4723 4724
    args.xml = (char *) xmlDesc;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NETWORK_CREATE_XML,
              (xdrproc_t) xdr_remote_network_create_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_network_create_xml_ret, (char *) &ret) == -1)
4725
        goto done;
4726 4727 4728 4729

    net = get_nonnull_network (conn, ret.net);
    xdr_free ((xdrproc_t) &xdr_remote_network_create_xml_ret, (char *) &ret);

4730
done:
4731
    remoteDriverUnlock(priv);
4732 4733 4734 4735 4736 4737
    return net;
}

static virNetworkPtr
remoteNetworkDefineXML (virConnectPtr conn, const char *xml)
{
4738
    virNetworkPtr net = NULL;
4739 4740
    remote_network_define_xml_args args;
    remote_network_define_xml_ret ret;
4741
    struct private_data *priv = conn->networkPrivateData;
4742

4743 4744
    remoteDriverLock(priv);

4745 4746 4747 4748 4749 4750
    args.xml = (char *) xml;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NETWORK_DEFINE_XML,
              (xdrproc_t) xdr_remote_network_define_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_network_define_xml_ret, (char *) &ret) == -1)
4751
        goto done;
4752 4753 4754 4755

    net = get_nonnull_network (conn, ret.net);
    xdr_free ((xdrproc_t) &xdr_remote_network_define_xml_ret, (char *) &ret);

4756
done:
4757
    remoteDriverUnlock(priv);
4758 4759 4760 4761 4762 4763
    return net;
}

static int
remoteNetworkUndefine (virNetworkPtr network)
{
4764
    int rv = -1;
4765
    remote_network_undefine_args args;
4766
    struct private_data *priv = network->conn->networkPrivateData;
4767

4768 4769
    remoteDriverLock(priv);

4770 4771 4772 4773 4774
    make_nonnull_network (&args.net, network);

    if (call (network->conn, priv, 0, REMOTE_PROC_NETWORK_UNDEFINE,
              (xdrproc_t) xdr_remote_network_undefine_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
4775
        goto done;
4776

4777 4778 4779
    rv = 0;

done:
4780
    remoteDriverUnlock(priv);
4781
    return rv;
4782 4783 4784 4785 4786
}

static int
remoteNetworkCreate (virNetworkPtr network)
{
4787
    int rv = -1;
4788
    remote_network_create_args args;
4789
    struct private_data *priv = network->conn->networkPrivateData;
4790

4791 4792
    remoteDriverLock(priv);

4793 4794 4795 4796 4797
    make_nonnull_network (&args.net, network);

    if (call (network->conn, priv, 0, REMOTE_PROC_NETWORK_CREATE,
              (xdrproc_t) xdr_remote_network_create_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
4798
        goto done;
4799

4800 4801 4802
    rv = 0;

done:
4803
    remoteDriverUnlock(priv);
4804
    return rv;
4805 4806 4807 4808 4809
}

static int
remoteNetworkDestroy (virNetworkPtr network)
{
4810
    int rv = -1;
4811
    remote_network_destroy_args args;
4812
    struct private_data *priv = network->conn->networkPrivateData;
4813

4814 4815
    remoteDriverLock(priv);

4816 4817 4818 4819 4820
    make_nonnull_network (&args.net, network);

    if (call (network->conn, priv, 0, REMOTE_PROC_NETWORK_DESTROY,
              (xdrproc_t) xdr_remote_network_destroy_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
4821
        goto done;
4822

4823 4824 4825
    rv = 0;

done:
4826
    remoteDriverUnlock(priv);
4827
    return rv;
4828 4829 4830 4831 4832
}

static char *
remoteNetworkDumpXML (virNetworkPtr network, int flags)
{
4833
    char *rv = NULL;
4834 4835
    remote_network_dump_xml_args args;
    remote_network_dump_xml_ret ret;
4836
    struct private_data *priv = network->conn->networkPrivateData;
4837

4838 4839
    remoteDriverLock(priv);

4840 4841 4842 4843 4844 4845 4846
    make_nonnull_network (&args.net, network);
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (network->conn, priv, 0, REMOTE_PROC_NETWORK_DUMP_XML,
              (xdrproc_t) xdr_remote_network_dump_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_network_dump_xml_ret, (char *) &ret) == -1)
4847
        goto done;
4848 4849

    /* Caller frees. */
4850 4851 4852
    rv = ret.xml;

done:
4853
    remoteDriverUnlock(priv);
4854
    return rv;
4855 4856 4857 4858 4859
}

static char *
remoteNetworkGetBridgeName (virNetworkPtr network)
{
4860
    char *rv = NULL;
4861 4862
    remote_network_get_bridge_name_args args;
    remote_network_get_bridge_name_ret ret;
4863
    struct private_data *priv = network->conn->networkPrivateData;
4864

4865 4866
    remoteDriverLock(priv);

4867 4868 4869 4870 4871 4872
    make_nonnull_network (&args.net, network);

    memset (&ret, 0, sizeof ret);
    if (call (network->conn, priv, 0, REMOTE_PROC_NETWORK_GET_BRIDGE_NAME,
              (xdrproc_t) xdr_remote_network_get_bridge_name_args, (char *) &args,
              (xdrproc_t) xdr_remote_network_get_bridge_name_ret, (char *) &ret) == -1)
4873
        goto done;
4874 4875

    /* Caller frees. */
4876 4877 4878
    rv = ret.name;

done:
4879
    remoteDriverUnlock(priv);
4880
    return rv;
4881 4882 4883 4884 4885
}

static int
remoteNetworkGetAutostart (virNetworkPtr network, int *autostart)
{
4886
    int rv = -1;
4887 4888
    remote_network_get_autostart_args args;
    remote_network_get_autostart_ret ret;
4889
    struct private_data *priv = network->conn->networkPrivateData;
4890

4891 4892
    remoteDriverLock(priv);

4893 4894 4895 4896 4897 4898
    make_nonnull_network (&args.net, network);

    memset (&ret, 0, sizeof ret);
    if (call (network->conn, priv, 0, REMOTE_PROC_NETWORK_GET_AUTOSTART,
              (xdrproc_t) xdr_remote_network_get_autostart_args, (char *) &args,
              (xdrproc_t) xdr_remote_network_get_autostart_ret, (char *) &ret) == -1)
4899
        goto done;
4900 4901 4902

    if (autostart) *autostart = ret.autostart;

4903 4904 4905
    rv = 0;

done:
4906
    remoteDriverUnlock(priv);
4907
    return rv;
4908 4909 4910 4911 4912
}

static int
remoteNetworkSetAutostart (virNetworkPtr network, int autostart)
{
4913
    int rv = -1;
4914
    remote_network_set_autostart_args args;
4915
    struct private_data *priv = network->conn->networkPrivateData;
4916

4917 4918
    remoteDriverLock(priv);

4919 4920 4921 4922 4923 4924
    make_nonnull_network (&args.net, network);
    args.autostart = autostart;

    if (call (network->conn, priv, 0, REMOTE_PROC_NETWORK_SET_AUTOSTART,
              (xdrproc_t) xdr_remote_network_set_autostart_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
4925
        goto done;
4926

4927 4928 4929
    rv = 0;

done:
4930
    remoteDriverUnlock(priv);
4931
    return rv;
4932 4933
}

4934 4935 4936



D
Daniel Veillard 已提交
4937 4938
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
4939
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
D
Daniel Veillard 已提交
4940
remoteInterfaceOpen (virConnectPtr conn,
J
Jim Meyering 已提交
4941 4942
                     virConnectAuthPtr auth,
                     int flags)
D
Daniel Veillard 已提交
4943 4944 4945 4946
{
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
4947
    if (conn->driver &&
D
Daniel Veillard 已提交
4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032
        STREQ (conn->driver->name, "remote")) {
        struct private_data *priv;

       /* If we're here, the remote driver is already
         * in use due to a) a QEMU uri, or b) a remote
         * URI. So we can re-use existing connection
         */
        priv = conn->privateData;
        remoteDriverLock(priv);
        priv->localUses++;
        conn->interfacePrivateData = priv;
        remoteDriverUnlock(priv);
        return VIR_DRV_OPEN_SUCCESS;
    } else {
        /* Using a non-remote driver, so we need to open a
         * new connection for interface APIs, forcing it to
         * use the UNIX transport. This handles Xen driver
         * which doesn't have its own impl of the interface APIs.
         */
        struct private_data *priv;
        int ret;
        ret = remoteOpenSecondaryDriver(conn,
                                        auth,
                                        flags,
                                        &priv);
        if (ret == VIR_DRV_OPEN_SUCCESS)
            conn->interfacePrivateData = priv;
        return ret;
    }
}

static int
remoteInterfaceClose (virConnectPtr conn)
{
    int rv = 0;
    struct private_data *priv = conn->interfacePrivateData;

    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        rv = doRemoteClose(conn, priv);
        conn->interfacePrivateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
    }
    if (priv)
        remoteDriverUnlock(priv);
    return rv;
}

static int
remoteNumOfInterfaces (virConnectPtr conn)
{
    int rv = -1;
    remote_num_of_interfaces_ret ret;
    struct private_data *priv = conn->interfacePrivateData;

    remoteDriverLock(priv);

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NUM_OF_INTERFACES,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_num_of_interfaces_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.num;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteListInterfaces (virConnectPtr conn, char **const names, int maxnames)
{
    int rv = -1;
    int i;
    remote_list_interfaces_args args;
    remote_list_interfaces_ret ret;
    struct private_data *priv = conn->interfacePrivateData;

    remoteDriverLock(priv);

    if (maxnames > REMOTE_INTERFACE_NAME_LIST_MAX) {
5033 5034 5035
        remoteError(VIR_ERR_RPC,
                    _("too many remote interfaces: %d > %d"),
                    maxnames, REMOTE_INTERFACE_NAME_LIST_MAX);
D
Daniel Veillard 已提交
5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046
        goto done;
    }
    args.maxnames = maxnames;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_LIST_INTERFACES,
              (xdrproc_t) xdr_remote_list_interfaces_args, (char *) &args,
              (xdrproc_t) xdr_remote_list_interfaces_ret, (char *) &ret) == -1)
        goto done;

    if (ret.names.names_len > maxnames) {
5047 5048 5049
        remoteError(VIR_ERR_RPC,
                    _("too many remote interfaces: %d > %d"),
                    ret.names.names_len, maxnames);
D
Daniel Veillard 已提交
5050 5051 5052 5053 5054 5055 5056 5057
        goto cleanup;
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
5058
    for (i = 0; i < ret.names.names_len; ++i) {
D
Daniel Veillard 已提交
5059 5060
        names[i] = strdup (ret.names.names_val[i]);

5061 5062 5063 5064
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

5065
            virReportOOMError();
5066 5067 5068 5069
            goto cleanup;
        }
    }

D
Daniel Veillard 已提交
5070 5071 5072 5073 5074 5075 5076 5077 5078 5079
    rv = ret.names.names_len;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113
static int
remoteNumOfDefinedInterfaces (virConnectPtr conn)
{
    int rv = -1;
    remote_num_of_defined_interfaces_ret ret;
    struct private_data *priv = conn->interfacePrivateData;

    remoteDriverLock(priv);

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NUM_OF_DEFINED_INTERFACES,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_num_of_defined_interfaces_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.num;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteListDefinedInterfaces (virConnectPtr conn, char **const names, int maxnames)
{
    int rv = -1;
    int i;
    remote_list_defined_interfaces_args args;
    remote_list_defined_interfaces_ret ret;
    struct private_data *priv = conn->interfacePrivateData;

    remoteDriverLock(priv);

    if (maxnames > REMOTE_DEFINED_INTERFACE_NAME_LIST_MAX) {
5114 5115 5116
        remoteError(VIR_ERR_RPC,
                    _("too many remote interfaces: %d > %d"),
                    maxnames, REMOTE_DEFINED_INTERFACE_NAME_LIST_MAX);
5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127
        goto done;
    }
    args.maxnames = maxnames;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_LIST_DEFINED_INTERFACES,
              (xdrproc_t) xdr_remote_list_defined_interfaces_args, (char *) &args,
              (xdrproc_t) xdr_remote_list_defined_interfaces_ret, (char *) &ret) == -1)
        goto done;

    if (ret.names.names_len > maxnames) {
5128 5129 5130
        remoteError(VIR_ERR_RPC,
                    _("too many remote interfaces: %d > %d"),
                    ret.names.names_len, maxnames);
5131 5132 5133 5134 5135 5136 5137 5138
        goto cleanup;
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
5139
    for (i = 0; i < ret.names.names_len; ++i) {
5140 5141
        names[i] = strdup (ret.names.names_val[i]);

5142 5143 5144 5145
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

5146
            virReportOOMError();
5147 5148 5149 5150
            goto cleanup;
        }
    }

5151 5152 5153 5154 5155 5156 5157 5158 5159 5160
    rv = ret.names.names_len;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

D
Daniel Veillard 已提交
5161 5162 5163 5164
static virInterfacePtr
remoteInterfaceLookupByName (virConnectPtr conn,
                             const char *name)
{
5165
    virInterfacePtr iface = NULL;
D
Daniel Veillard 已提交
5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179
    remote_interface_lookup_by_name_args args;
    remote_interface_lookup_by_name_ret ret;
    struct private_data *priv = conn->interfacePrivateData;

    remoteDriverLock(priv);

    args.name = (char *) name;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_INTERFACE_LOOKUP_BY_NAME,
              (xdrproc_t) xdr_remote_interface_lookup_by_name_args, (char *) &args,
              (xdrproc_t) xdr_remote_interface_lookup_by_name_ret, (char *) &ret) == -1)
        goto done;

5180
    iface = get_nonnull_interface (conn, ret.iface);
D
Daniel Veillard 已提交
5181 5182 5183 5184
    xdr_free ((xdrproc_t) &xdr_remote_interface_lookup_by_name_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
5185
    return iface;
D
Daniel Veillard 已提交
5186 5187 5188 5189 5190 5191
}

static virInterfacePtr
remoteInterfaceLookupByMACString (virConnectPtr conn,
                                  const char *mac)
{
5192
    virInterfacePtr iface = NULL;
D
Daniel Veillard 已提交
5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206
    remote_interface_lookup_by_mac_string_args args;
    remote_interface_lookup_by_mac_string_ret ret;
    struct private_data *priv = conn->interfacePrivateData;

    remoteDriverLock(priv);

    args.mac = (char *) mac;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_INTERFACE_LOOKUP_BY_MAC_STRING,
              (xdrproc_t) xdr_remote_interface_lookup_by_mac_string_args, (char *) &args,
              (xdrproc_t) xdr_remote_interface_lookup_by_mac_string_ret, (char *) &ret) == -1)
        goto done;

5207
    iface = get_nonnull_interface (conn, ret.iface);
D
Daniel Veillard 已提交
5208 5209 5210 5211
    xdr_free ((xdrproc_t) &xdr_remote_interface_lookup_by_mac_string_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
5212
    return iface;
D
Daniel Veillard 已提交
5213 5214
}

5215 5216 5217 5218 5219 5220 5221

static int
remoteInterfaceIsActive(virInterfacePtr iface)
{
    int rv = -1;
    remote_interface_is_active_args args;
    remote_interface_is_active_ret ret;
5222
    struct private_data *priv = iface->conn->interfacePrivateData;
5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240

    remoteDriverLock(priv);

    make_nonnull_interface (&args.iface, iface);

    if (call (iface->conn, priv, 0, REMOTE_PROC_INTERFACE_IS_ACTIVE,
              (xdrproc_t) xdr_remote_interface_is_active_args, (char *) &args,
              (xdrproc_t) xdr_remote_interface_is_active_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.active;

done:
    remoteDriverUnlock(priv);
    return rv;
}


D
Daniel Veillard 已提交
5241
static char *
5242
remoteInterfaceGetXMLDesc (virInterfacePtr iface,
D
Daniel Veillard 已提交
5243 5244 5245 5246 5247
                           unsigned int flags)
{
    char *rv = NULL;
    remote_interface_get_xml_desc_args args;
    remote_interface_get_xml_desc_ret ret;
5248
    struct private_data *priv = iface->conn->interfacePrivateData;
D
Daniel Veillard 已提交
5249 5250 5251

    remoteDriverLock(priv);

5252
    make_nonnull_interface (&args.iface, iface);
D
Daniel Veillard 已提交
5253 5254 5255
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
5256
    if (call (iface->conn, priv, 0, REMOTE_PROC_INTERFACE_GET_XML_DESC,
D
Daniel Veillard 已提交
5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273
              (xdrproc_t) xdr_remote_interface_get_xml_desc_args, (char *) &args,
              (xdrproc_t) xdr_remote_interface_get_xml_desc_ret, (char *) &ret) == -1)
        goto done;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

static virInterfacePtr
remoteInterfaceDefineXML (virConnectPtr conn,
                          const char *xmlDesc,
                          unsigned int flags)
{
5274
    virInterfacePtr iface = NULL;
D
Daniel Veillard 已提交
5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289
    remote_interface_define_xml_args args;
    remote_interface_define_xml_ret ret;
    struct private_data *priv = conn->interfacePrivateData;

    remoteDriverLock(priv);

    args.xml = (char *) xmlDesc;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_INTERFACE_DEFINE_XML,
              (xdrproc_t) xdr_remote_interface_define_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_interface_define_xml_ret, (char *) &ret) == -1)
        goto done;

5290
    iface = get_nonnull_interface (conn, ret.iface);
D
Daniel Veillard 已提交
5291 5292 5293 5294
    xdr_free ((xdrproc_t) &xdr_remote_interface_define_xml_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
5295
    return iface;
D
Daniel Veillard 已提交
5296 5297 5298
}

static int
5299
remoteInterfaceUndefine (virInterfacePtr iface)
D
Daniel Veillard 已提交
5300 5301 5302
{
    int rv = -1;
    remote_interface_undefine_args args;
5303
    struct private_data *priv = iface->conn->interfacePrivateData;
D
Daniel Veillard 已提交
5304 5305 5306

    remoteDriverLock(priv);

5307
    make_nonnull_interface (&args.iface, iface);
D
Daniel Veillard 已提交
5308

5309
    if (call (iface->conn, priv, 0, REMOTE_PROC_INTERFACE_UNDEFINE,
D
Daniel Veillard 已提交
5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321
              (xdrproc_t) xdr_remote_interface_undefine_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
5322
remoteInterfaceCreate (virInterfacePtr iface,
D
Daniel Veillard 已提交
5323 5324 5325 5326
                       unsigned int flags)
{
    int rv = -1;
    remote_interface_create_args args;
5327
    struct private_data *priv = iface->conn->interfacePrivateData;
D
Daniel Veillard 已提交
5328 5329 5330

    remoteDriverLock(priv);

5331
    make_nonnull_interface (&args.iface, iface);
D
Daniel Veillard 已提交
5332 5333
    args.flags = flags;

5334
    if (call (iface->conn, priv, 0, REMOTE_PROC_INTERFACE_CREATE,
D
Daniel Veillard 已提交
5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346
              (xdrproc_t) xdr_remote_interface_create_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
5347
remoteInterfaceDestroy (virInterfacePtr iface,
D
Daniel Veillard 已提交
5348 5349 5350 5351
                        unsigned int flags)
{
    int rv = -1;
    remote_interface_destroy_args args;
5352
    struct private_data *priv = iface->conn->interfacePrivateData;
D
Daniel Veillard 已提交
5353 5354 5355

    remoteDriverLock(priv);

5356
    make_nonnull_interface (&args.iface, iface);
D
Daniel Veillard 已提交
5357 5358
    args.flags = flags;

5359
    if (call (iface->conn, priv, 0, REMOTE_PROC_INTERFACE_DESTROY,
D
Daniel Veillard 已提交
5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370
              (xdrproc_t) xdr_remote_interface_destroy_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

5371 5372
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
5373
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
5374 5375 5376 5377 5378 5379 5380
remoteStorageOpen (virConnectPtr conn,
                   virConnectAuthPtr auth,
                   int flags)
{
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
5381
    if (conn->driver &&
5382
        STREQ (conn->driver->name, "remote")) {
5383
        struct private_data *priv = conn->privateData;
5384 5385 5386 5387
        /* If we're here, the remote driver is already
         * in use due to a) a QEMU uri, or b) a remote
         * URI. So we can re-use existing connection
         */
5388 5389 5390 5391
        remoteDriverLock(priv);
        priv->localUses++;
        conn->storagePrivateData = priv;
        remoteDriverUnlock(priv);
5392 5393 5394
        return VIR_DRV_OPEN_SUCCESS;
    } else if (conn->networkDriver &&
               STREQ (conn->networkDriver->name, "remote")) {
5395 5396 5397 5398 5399
        struct private_data *priv = conn->networkPrivateData;
        remoteDriverLock(priv);
        conn->storagePrivateData = priv;
        priv->localUses++;
        remoteDriverUnlock(priv);
5400 5401 5402 5403 5404 5405 5406
        return VIR_DRV_OPEN_SUCCESS;
    } else {
        /* Using a non-remote driver, so we need to open a
         * new connection for network APIs, forcing it to
         * use the UNIX transport. This handles Xen driver
         * which doesn't have its own impl of the network APIs.
         */
5407
        struct private_data *priv;
5408 5409 5410 5411 5412 5413
        int ret;
        ret = remoteOpenSecondaryDriver(conn,
                                        auth,
                                        flags,
                                        &priv);
        if (ret == VIR_DRV_OPEN_SUCCESS)
5414 5415 5416 5417 5418 5419 5420 5421 5422
            conn->storagePrivateData = priv;
        return ret;
    }
}

static int
remoteStorageClose (virConnectPtr conn)
{
    int ret = 0;
5423 5424
    struct private_data *priv = conn->storagePrivateData;

5425 5426 5427 5428 5429 5430 5431 5432
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        ret = doRemoteClose(conn, priv);
        conn->storagePrivateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
5433
    }
5434 5435
    if (priv)
        remoteDriverUnlock(priv);
5436

5437 5438 5439 5440 5441 5442
    return ret;
}

static int
remoteNumOfStoragePools (virConnectPtr conn)
{
5443
    int rv = -1;
5444
    remote_num_of_storage_pools_ret ret;
5445
    struct private_data *priv = conn->storagePrivateData;
5446

5447 5448
    remoteDriverLock(priv);

5449 5450 5451 5452
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NUM_OF_STORAGE_POOLS,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_num_of_storage_pools_ret, (char *) &ret) == -1)
5453 5454 5455
        goto done;

    rv = ret.num;
5456

5457
done:
5458
    remoteDriverUnlock(priv);
5459
    return rv;
5460 5461 5462 5463 5464
}

static int
remoteListStoragePools (virConnectPtr conn, char **const names, int maxnames)
{
5465
    int rv = -1;
5466 5467 5468
    int i;
    remote_list_storage_pools_args args;
    remote_list_storage_pools_ret ret;
5469
    struct private_data *priv = conn->storagePrivateData;
5470

5471 5472
    remoteDriverLock(priv);

5473
    if (maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
5474
        remoteError(VIR_ERR_RPC, "%s", _("too many storage pools requested"));
5475
        goto done;
5476 5477 5478 5479 5480 5481 5482
    }
    args.maxnames = maxnames;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_LIST_STORAGE_POOLS,
              (xdrproc_t) xdr_remote_list_storage_pools_args, (char *) &args,
              (xdrproc_t) xdr_remote_list_storage_pools_ret, (char *) &ret) == -1)
5483
        goto done;
5484 5485

    if (ret.names.names_len > maxnames) {
5486
        remoteError(VIR_ERR_RPC, "%s", _("too many storage pools received"));
5487
        goto cleanup;
5488 5489 5490 5491 5492 5493 5494
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
5495
    for (i = 0; i < ret.names.names_len; ++i) {
5496 5497
        names[i] = strdup (ret.names.names_val[i]);

5498 5499 5500 5501
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

5502
            virReportOOMError();
5503 5504 5505 5506
            goto cleanup;
        }
    }

5507 5508 5509
    rv = ret.names.names_len;

cleanup:
5510 5511
    xdr_free ((xdrproc_t) xdr_remote_list_storage_pools_ret, (char *) &ret);

5512
done:
5513
    remoteDriverUnlock(priv);
5514
    return rv;
5515 5516 5517 5518 5519
}

static int
remoteNumOfDefinedStoragePools (virConnectPtr conn)
{
5520
    int rv = -1;
5521
    remote_num_of_defined_storage_pools_ret ret;
5522
    struct private_data *priv = conn->storagePrivateData;
5523

5524 5525
    remoteDriverLock(priv);

5526 5527 5528 5529
    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NUM_OF_DEFINED_STORAGE_POOLS,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_num_of_defined_storage_pools_ret, (char *) &ret) == -1)
5530
        goto done;
5531

5532 5533 5534
    rv = ret.num;

done:
5535
    remoteDriverUnlock(priv);
5536
    return rv;
5537 5538 5539 5540 5541 5542
}

static int
remoteListDefinedStoragePools (virConnectPtr conn,
                               char **const names, int maxnames)
{
5543
    int rv = -1;
5544 5545 5546
    int i;
    remote_list_defined_storage_pools_args args;
    remote_list_defined_storage_pools_ret ret;
5547
    struct private_data *priv = conn->storagePrivateData;
5548

5549 5550
    remoteDriverLock(priv);

5551
    if (maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
5552
        remoteError(VIR_ERR_RPC, "%s", _("too many storage pools requested"));
5553
        goto done;
5554 5555 5556 5557 5558 5559 5560
    }
    args.maxnames = maxnames;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_LIST_DEFINED_STORAGE_POOLS,
              (xdrproc_t) xdr_remote_list_defined_storage_pools_args, (char *) &args,
              (xdrproc_t) xdr_remote_list_defined_storage_pools_ret, (char *) &ret) == -1)
5561
        goto done;
5562 5563

    if (ret.names.names_len > maxnames) {
5564
        remoteError(VIR_ERR_RPC, "%s", _("too many storage pools received"));
5565
        goto cleanup;
5566 5567 5568 5569 5570 5571 5572
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
5573
    for (i = 0; i < ret.names.names_len; ++i) {
5574 5575
        names[i] = strdup (ret.names.names_val[i]);

5576 5577 5578 5579
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

5580
            virReportOOMError();
5581 5582 5583 5584
            goto cleanup;
        }
    }

5585 5586 5587
    rv = ret.names.names_len;

cleanup:
5588 5589
    xdr_free ((xdrproc_t) xdr_remote_list_defined_storage_pools_ret, (char *) &ret);

5590
done:
5591
    remoteDriverUnlock(priv);
5592
    return rv;
5593 5594
}

5595 5596 5597 5598 5599 5600
static char *
remoteFindStoragePoolSources (virConnectPtr conn,
                              const char *type,
                              const char *srcSpec,
                              unsigned int flags)
{
5601
    char *rv = NULL;
5602 5603
    remote_find_storage_pool_sources_args args;
    remote_find_storage_pool_sources_ret ret;
5604
    struct private_data *priv = conn->storagePrivateData;
5605 5606
    const char *emptyString = "";

5607 5608
    remoteDriverLock(priv);

5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627
    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:
     *    libvir: Remote error : marshalling args
     *
     * 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,
              (xdrproc_t) xdr_remote_find_storage_pool_sources_ret, (char *) &ret) == -1)
5628
        goto done;
5629

5630
    rv = ret.xml;
5631 5632 5633 5634
    ret.xml = NULL; /* To stop xdr_free free'ing it */

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

5635
done:
5636
    remoteDriverUnlock(priv);
5637
    return rv;
5638 5639
}

5640 5641 5642 5643
static virStoragePoolPtr
remoteStoragePoolLookupByUUID (virConnectPtr conn,
                               const unsigned char *uuid)
{
5644
    virStoragePoolPtr pool = NULL;
5645 5646
    remote_storage_pool_lookup_by_uuid_args args;
    remote_storage_pool_lookup_by_uuid_ret ret;
5647
    struct private_data *priv = conn->storagePrivateData;
5648

5649 5650
    remoteDriverLock(priv);

5651 5652 5653 5654 5655 5656
    memcpy (args.uuid, uuid, VIR_UUID_BUFLEN);

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_STORAGE_POOL_LOOKUP_BY_UUID,
              (xdrproc_t) xdr_remote_storage_pool_lookup_by_uuid_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_lookup_by_uuid_ret, (char *) &ret) == -1)
5657
        goto done;
5658 5659 5660 5661

    pool = get_nonnull_storage_pool (conn, ret.pool);
    xdr_free ((xdrproc_t) &xdr_remote_storage_pool_lookup_by_uuid_ret, (char *) &ret);

5662
done:
5663
    remoteDriverUnlock(priv);
5664 5665 5666 5667 5668 5669 5670
    return pool;
}

static virStoragePoolPtr
remoteStoragePoolLookupByName (virConnectPtr conn,
                               const char *name)
{
5671
    virStoragePoolPtr pool = NULL;
5672 5673
    remote_storage_pool_lookup_by_name_args args;
    remote_storage_pool_lookup_by_name_ret ret;
5674
    struct private_data *priv = conn->storagePrivateData;
5675

5676 5677
    remoteDriverLock(priv);

5678 5679 5680 5681 5682 5683
    args.name = (char *) name;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_STORAGE_POOL_LOOKUP_BY_NAME,
              (xdrproc_t) xdr_remote_storage_pool_lookup_by_name_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_lookup_by_name_ret, (char *) &ret) == -1)
5684
        goto done;
5685 5686 5687 5688

    pool = get_nonnull_storage_pool (conn, ret.pool);
    xdr_free ((xdrproc_t) &xdr_remote_storage_pool_lookup_by_name_ret, (char *) &ret);

5689
done:
5690
    remoteDriverUnlock(priv);
5691 5692 5693 5694 5695 5696
    return pool;
}

static virStoragePoolPtr
remoteStoragePoolLookupByVolume (virStorageVolPtr vol)
{
5697
    virStoragePoolPtr pool = NULL;
5698 5699
    remote_storage_pool_lookup_by_volume_args args;
    remote_storage_pool_lookup_by_volume_ret ret;
5700
    struct private_data *priv = vol->conn->storagePrivateData;
5701

5702 5703
    remoteDriverLock(priv);

5704 5705 5706 5707 5708 5709
    make_nonnull_storage_vol (&args.vol, vol);

    memset (&ret, 0, sizeof ret);
    if (call (vol->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_LOOKUP_BY_VOLUME,
              (xdrproc_t) xdr_remote_storage_pool_lookup_by_volume_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_lookup_by_volume_ret, (char *) &ret) == -1)
5710
        goto done;
5711 5712 5713 5714

    pool = get_nonnull_storage_pool (vol->conn, ret.pool);
    xdr_free ((xdrproc_t) &xdr_remote_storage_pool_lookup_by_volume_ret, (char *) &ret);

5715
done:
5716
    remoteDriverUnlock(priv);
5717 5718 5719 5720
    return pool;
}


5721 5722 5723 5724 5725 5726
static int
remoteStoragePoolIsActive(virStoragePoolPtr pool)
{
    int rv = -1;
    remote_storage_pool_is_active_args args;
    remote_storage_pool_is_active_ret ret;
5727
    struct private_data *priv = pool->conn->storagePrivateData;
5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750

    remoteDriverLock(priv);

    make_nonnull_storage_pool (&args.pool, pool);

    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_IS_ACTIVE,
              (xdrproc_t) xdr_remote_storage_pool_is_active_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_is_active_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.active;

done:
    remoteDriverUnlock(priv);
    return rv;
}

static int
remoteStoragePoolIsPersistent(virStoragePoolPtr pool)
{
    int rv = -1;
    remote_storage_pool_is_persistent_args args;
    remote_storage_pool_is_persistent_ret ret;
5751
    struct private_data *priv = pool->conn->storagePrivateData;
5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769

    remoteDriverLock(priv);

    make_nonnull_storage_pool (&args.pool, pool);

    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_IS_PERSISTENT,
              (xdrproc_t) xdr_remote_storage_pool_is_persistent_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_is_persistent_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.persistent;

done:
    remoteDriverUnlock(priv);
    return rv;
}


5770 5771 5772
static virStoragePoolPtr
remoteStoragePoolCreateXML (virConnectPtr conn, const char *xmlDesc, unsigned int flags)
{
5773
    virStoragePoolPtr pool = NULL;
5774 5775
    remote_storage_pool_create_xml_args args;
    remote_storage_pool_create_xml_ret ret;
5776
    struct private_data *priv = conn->storagePrivateData;
5777

5778 5779
    remoteDriverLock(priv);

5780 5781 5782 5783 5784 5785 5786
    args.xml = (char *) xmlDesc;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_STORAGE_POOL_CREATE_XML,
              (xdrproc_t) xdr_remote_storage_pool_create_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_create_xml_ret, (char *) &ret) == -1)
5787
        goto done;
5788 5789 5790 5791

    pool = get_nonnull_storage_pool (conn, ret.pool);
    xdr_free ((xdrproc_t) &xdr_remote_storage_pool_create_xml_ret, (char *) &ret);

5792
done:
5793
    remoteDriverUnlock(priv);
5794 5795 5796 5797 5798 5799
    return pool;
}

static virStoragePoolPtr
remoteStoragePoolDefineXML (virConnectPtr conn, const char *xml, unsigned int flags)
{
5800
    virStoragePoolPtr pool = NULL;
5801 5802
    remote_storage_pool_define_xml_args args;
    remote_storage_pool_define_xml_ret ret;
5803
    struct private_data *priv = conn->storagePrivateData;
5804

5805 5806
    remoteDriverLock(priv);

5807 5808 5809 5810 5811 5812 5813
    args.xml = (char *) xml;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_STORAGE_POOL_DEFINE_XML,
              (xdrproc_t) xdr_remote_storage_pool_define_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_define_xml_ret, (char *) &ret) == -1)
5814
        goto done;
5815 5816 5817 5818

    pool = get_nonnull_storage_pool (conn, ret.pool);
    xdr_free ((xdrproc_t) &xdr_remote_storage_pool_define_xml_ret, (char *) &ret);

5819
done:
5820
    remoteDriverUnlock(priv);
5821 5822 5823 5824 5825 5826
    return pool;
}

static int
remoteStoragePoolUndefine (virStoragePoolPtr pool)
{
5827
    int rv = -1;
5828
    remote_storage_pool_undefine_args args;
5829
    struct private_data *priv = pool->conn->storagePrivateData;
5830

5831 5832
    remoteDriverLock(priv);

5833 5834 5835 5836 5837
    make_nonnull_storage_pool (&args.pool, pool);

    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_UNDEFINE,
              (xdrproc_t) xdr_remote_storage_pool_undefine_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
5838
        goto done;
5839

5840 5841 5842
    rv = 0;

done:
5843
    remoteDriverUnlock(priv);
5844
    return rv;
5845 5846 5847 5848 5849
}

static int
remoteStoragePoolCreate (virStoragePoolPtr pool, unsigned int flags)
{
5850
    int rv = -1;
5851
    remote_storage_pool_create_args args;
5852
    struct private_data *priv = pool->conn->storagePrivateData;
5853

5854 5855
    remoteDriverLock(priv);

5856 5857 5858 5859 5860 5861
    make_nonnull_storage_pool (&args.pool, pool);
    args.flags = flags;

    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_CREATE,
              (xdrproc_t) xdr_remote_storage_pool_create_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
5862
        goto done;
5863

5864 5865 5866
    rv = 0;

done:
5867
    remoteDriverUnlock(priv);
5868
    return rv;
5869 5870 5871 5872 5873 5874
}

static int
remoteStoragePoolBuild (virStoragePoolPtr pool,
                        unsigned int flags)
{
5875
    int rv = -1;
5876
    remote_storage_pool_build_args args;
5877
    struct private_data *priv = pool->conn->storagePrivateData;
5878

5879 5880
    remoteDriverLock(priv);

5881 5882 5883 5884 5885 5886
    make_nonnull_storage_pool (&args.pool, pool);
    args.flags = flags;

    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_BUILD,
              (xdrproc_t) xdr_remote_storage_pool_build_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
5887
        goto done;
5888

5889 5890 5891
    rv = 0;

done:
5892
    remoteDriverUnlock(priv);
5893
    return rv;
5894 5895 5896 5897 5898
}

static int
remoteStoragePoolDestroy (virStoragePoolPtr pool)
{
5899
    int rv = -1;
5900
    remote_storage_pool_destroy_args args;
5901
    struct private_data *priv = pool->conn->storagePrivateData;
5902

5903 5904
    remoteDriverLock(priv);

5905 5906 5907 5908 5909
    make_nonnull_storage_pool (&args.pool, pool);

    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_DESTROY,
              (xdrproc_t) xdr_remote_storage_pool_destroy_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
5910
        goto done;
5911

5912 5913 5914
    rv = 0;

done:
5915
    remoteDriverUnlock(priv);
5916
    return rv;
5917 5918 5919 5920 5921 5922
}

static int
remoteStoragePoolDelete (virStoragePoolPtr pool,
                         unsigned int flags)
{
5923
    int rv = -1;
5924
    remote_storage_pool_delete_args args;
5925
    struct private_data *priv = pool->conn->storagePrivateData;
5926

5927 5928
    remoteDriverLock(priv);

5929 5930 5931 5932 5933 5934
    make_nonnull_storage_pool (&args.pool, pool);
    args.flags = flags;

    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_DELETE,
              (xdrproc_t) xdr_remote_storage_pool_delete_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
5935
        goto done;
5936

5937 5938 5939
    rv = 0;

done:
5940
    remoteDriverUnlock(priv);
5941
    return rv;
5942 5943 5944 5945 5946 5947
}

static int
remoteStoragePoolRefresh (virStoragePoolPtr pool,
                          unsigned int flags)
{
5948
    int rv = -1;
5949
    remote_storage_pool_refresh_args args;
5950
    struct private_data *priv = pool->conn->storagePrivateData;
5951

5952 5953
    remoteDriverLock(priv);

5954 5955 5956 5957 5958 5959
    make_nonnull_storage_pool (&args.pool, pool);
    args.flags = flags;

    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_REFRESH,
              (xdrproc_t) xdr_remote_storage_pool_refresh_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
5960
        goto done;
5961

5962 5963 5964
    rv = 0;

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

static int
remoteStoragePoolGetInfo (virStoragePoolPtr pool, virStoragePoolInfoPtr info)
{
5972
    int rv = -1;
5973 5974
    remote_storage_pool_get_info_args args;
    remote_storage_pool_get_info_ret ret;
5975
    struct private_data *priv = pool->conn->storagePrivateData;
5976

5977 5978
    remoteDriverLock(priv);

5979 5980 5981 5982 5983 5984
    make_nonnull_storage_pool (&args.pool, pool);

    memset (&ret, 0, sizeof ret);
    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_GET_INFO,
              (xdrproc_t) xdr_remote_storage_pool_get_info_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_get_info_ret, (char *) &ret) == -1)
5985
        goto done;
5986 5987 5988 5989 5990 5991

    info->state = ret.state;
    info->capacity = ret.capacity;
    info->allocation = ret.allocation;
    info->available = ret.available;

5992 5993 5994
    rv = 0;

done:
5995
    remoteDriverUnlock(priv);
5996
    return rv;
5997 5998 5999 6000 6001 6002
}

static char *
remoteStoragePoolDumpXML (virStoragePoolPtr pool,
                          unsigned int flags)
{
6003
    char *rv = NULL;
6004 6005
    remote_storage_pool_dump_xml_args args;
    remote_storage_pool_dump_xml_ret ret;
6006
    struct private_data *priv = pool->conn->storagePrivateData;
6007

6008 6009
    remoteDriverLock(priv);

6010 6011 6012 6013 6014 6015 6016
    make_nonnull_storage_pool (&args.pool, pool);
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_DUMP_XML,
              (xdrproc_t) xdr_remote_storage_pool_dump_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_dump_xml_ret, (char *) &ret) == -1)
6017
        goto done;
6018 6019

    /* Caller frees. */
6020 6021 6022
    rv = ret.xml;

done:
6023
    remoteDriverUnlock(priv);
6024
    return rv;
6025 6026 6027 6028 6029
}

static int
remoteStoragePoolGetAutostart (virStoragePoolPtr pool, int *autostart)
{
6030
    int rv = -1;
6031 6032
    remote_storage_pool_get_autostart_args args;
    remote_storage_pool_get_autostart_ret ret;
6033
    struct private_data *priv = pool->conn->storagePrivateData;
6034

6035 6036
    remoteDriverLock(priv);

6037 6038 6039 6040 6041 6042
    make_nonnull_storage_pool (&args.pool, pool);

    memset (&ret, 0, sizeof ret);
    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_GET_AUTOSTART,
              (xdrproc_t) xdr_remote_storage_pool_get_autostart_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_get_autostart_ret, (char *) &ret) == -1)
6043
        goto done;
6044 6045 6046

    if (autostart) *autostart = ret.autostart;

6047 6048 6049
    rv = 0;

done:
6050
    remoteDriverUnlock(priv);
6051
    return rv;
6052 6053 6054 6055 6056
}

static int
remoteStoragePoolSetAutostart (virStoragePoolPtr pool, int autostart)
{
6057
    int rv = -1;
6058
    remote_storage_pool_set_autostart_args args;
6059
    struct private_data *priv = pool->conn->storagePrivateData;
6060

6061 6062
    remoteDriverLock(priv);

6063 6064 6065 6066 6067 6068
    make_nonnull_storage_pool (&args.pool, pool);
    args.autostart = autostart;

    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_SET_AUTOSTART,
              (xdrproc_t) xdr_remote_storage_pool_set_autostart_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
6069
        goto done;
6070

6071 6072 6073
    rv = 0;

done:
6074
    remoteDriverUnlock(priv);
6075
    return rv;
6076 6077 6078 6079 6080 6081
}


static int
remoteStoragePoolNumOfVolumes (virStoragePoolPtr pool)
{
6082
    int rv = -1;
6083 6084
    remote_storage_pool_num_of_volumes_args args;
    remote_storage_pool_num_of_volumes_ret ret;
6085
    struct private_data *priv = pool->conn->storagePrivateData;
6086

6087 6088
    remoteDriverLock(priv);

6089 6090 6091 6092 6093 6094
    make_nonnull_storage_pool(&args.pool, pool);

    memset (&ret, 0, sizeof ret);
    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_NUM_OF_VOLUMES,
              (xdrproc_t) xdr_remote_storage_pool_num_of_volumes_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_num_of_volumes_ret, (char *) &ret) == -1)
6095 6096 6097
        goto done;

    rv = ret.num;
6098

6099
done:
6100
    remoteDriverUnlock(priv);
6101
    return rv;
6102 6103 6104 6105 6106
}

static int
remoteStoragePoolListVolumes (virStoragePoolPtr pool, char **const names, int maxnames)
{
6107
    int rv = -1;
6108 6109 6110
    int i;
    remote_storage_pool_list_volumes_args args;
    remote_storage_pool_list_volumes_ret ret;
6111
    struct private_data *priv = pool->conn->storagePrivateData;
6112

6113 6114
    remoteDriverLock(priv);

6115
    if (maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX) {
6116
        remoteError(VIR_ERR_RPC, "%s", _("too many storage volumes requested"));
6117
        goto done;
6118 6119 6120 6121 6122 6123 6124 6125
    }
    args.maxnames = maxnames;
    make_nonnull_storage_pool(&args.pool, pool);

    memset (&ret, 0, sizeof ret);
    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_POOL_LIST_VOLUMES,
              (xdrproc_t) xdr_remote_storage_pool_list_volumes_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_pool_list_volumes_ret, (char *) &ret) == -1)
6126
        goto done;
6127 6128

    if (ret.names.names_len > maxnames) {
6129
        remoteError(VIR_ERR_RPC, "%s", _("too many storage volumes received"));
6130
        goto cleanup;
6131 6132 6133 6134 6135 6136 6137
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
6138
    for (i = 0; i < ret.names.names_len; ++i) {
6139 6140
        names[i] = strdup (ret.names.names_val[i]);

6141 6142 6143 6144
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

6145
            virReportOOMError();
6146 6147 6148 6149
            goto cleanup;
        }
    }

6150 6151 6152
    rv = ret.names.names_len;

cleanup:
6153 6154
    xdr_free ((xdrproc_t) xdr_remote_storage_pool_list_volumes_ret, (char *) &ret);

6155
done:
6156
    remoteDriverUnlock(priv);
6157
    return rv;
6158 6159 6160 6161 6162 6163 6164 6165
}



static virStorageVolPtr
remoteStorageVolLookupByName (virStoragePoolPtr pool,
                              const char *name)
{
6166
    virStorageVolPtr vol = NULL;
6167 6168
    remote_storage_vol_lookup_by_name_args args;
    remote_storage_vol_lookup_by_name_ret ret;
6169
    struct private_data *priv = pool->conn->storagePrivateData;
6170

6171 6172
    remoteDriverLock(priv);

6173 6174 6175 6176 6177 6178 6179
    make_nonnull_storage_pool(&args.pool, pool);
    args.name = (char *) name;

    memset (&ret, 0, sizeof ret);
    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_VOL_LOOKUP_BY_NAME,
              (xdrproc_t) xdr_remote_storage_vol_lookup_by_name_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_vol_lookup_by_name_ret, (char *) &ret) == -1)
6180
        goto done;
6181 6182 6183 6184

    vol = get_nonnull_storage_vol (pool->conn, ret.vol);
    xdr_free ((xdrproc_t) &xdr_remote_storage_vol_lookup_by_name_ret, (char *) &ret);

6185
done:
6186
    remoteDriverUnlock(priv);
6187 6188 6189 6190 6191 6192 6193
    return vol;
}

static virStorageVolPtr
remoteStorageVolLookupByKey (virConnectPtr conn,
                             const char *key)
{
6194
    virStorageVolPtr  vol = NULL;
6195 6196
    remote_storage_vol_lookup_by_key_args args;
    remote_storage_vol_lookup_by_key_ret ret;
6197
    struct private_data *priv = conn->storagePrivateData;
6198

6199 6200
    remoteDriverLock(priv);

6201 6202 6203 6204 6205 6206
    args.key = (char *) key;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_STORAGE_VOL_LOOKUP_BY_KEY,
              (xdrproc_t) xdr_remote_storage_vol_lookup_by_key_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_vol_lookup_by_key_ret, (char *) &ret) == -1)
6207
        goto done;
6208 6209 6210 6211

    vol = get_nonnull_storage_vol (conn, ret.vol);
    xdr_free ((xdrproc_t) &xdr_remote_storage_vol_lookup_by_key_ret, (char *) &ret);

6212
done:
6213
    remoteDriverUnlock(priv);
6214 6215 6216 6217 6218 6219 6220
    return vol;
}

static virStorageVolPtr
remoteStorageVolLookupByPath (virConnectPtr conn,
                              const char *path)
{
6221
    virStorageVolPtr vol = NULL;
6222 6223
    remote_storage_vol_lookup_by_path_args args;
    remote_storage_vol_lookup_by_path_ret ret;
6224
    struct private_data *priv = conn->storagePrivateData;
6225

6226 6227
    remoteDriverLock(priv);

6228 6229 6230 6231 6232 6233
    args.path = (char *) path;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_STORAGE_VOL_LOOKUP_BY_PATH,
              (xdrproc_t) xdr_remote_storage_vol_lookup_by_path_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_vol_lookup_by_path_ret, (char *) &ret) == -1)
6234
        goto done;
6235 6236 6237 6238

    vol = get_nonnull_storage_vol (conn, ret.vol);
    xdr_free ((xdrproc_t) &xdr_remote_storage_vol_lookup_by_path_ret, (char *) &ret);

6239
done:
6240
    remoteDriverUnlock(priv);
6241 6242 6243 6244 6245 6246 6247
    return vol;
}

static virStorageVolPtr
remoteStorageVolCreateXML (virStoragePoolPtr pool, const char *xmlDesc,
                           unsigned int flags)
{
6248
    virStorageVolPtr vol = NULL;
6249 6250
    remote_storage_vol_create_xml_args args;
    remote_storage_vol_create_xml_ret ret;
6251
    struct private_data *priv = pool->conn->storagePrivateData;
6252

6253 6254
    remoteDriverLock(priv);

6255 6256 6257 6258 6259 6260 6261 6262
    make_nonnull_storage_pool (&args.pool, pool);
    args.xml = (char *) xmlDesc;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (pool->conn, priv, 0, REMOTE_PROC_STORAGE_VOL_CREATE_XML,
              (xdrproc_t) xdr_remote_storage_vol_create_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_vol_create_xml_ret, (char *) &ret) == -1)
6263
        goto done;
6264 6265 6266 6267

    vol = get_nonnull_storage_vol (pool->conn, ret.vol);
    xdr_free ((xdrproc_t) &xdr_remote_storage_vol_create_xml_ret, (char *) &ret);

6268
done:
6269
    remoteDriverUnlock(priv);
6270 6271 6272
    return vol;
}

6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304
static virStorageVolPtr
remoteStorageVolCreateXMLFrom (virStoragePoolPtr pool,
                               const char *xmlDesc,
                               virStorageVolPtr clonevol,
                               unsigned int flags)
{
    virStorageVolPtr newvol = NULL;
    remote_storage_vol_create_xml_from_args args;
    remote_storage_vol_create_xml_from_ret ret;
    struct private_data *priv = pool->conn->storagePrivateData;

    remoteDriverLock(priv);

    make_nonnull_storage_pool (&args.pool, pool);
    make_nonnull_storage_vol (&args.clonevol, clonevol);
    args.xml = (char *) xmlDesc;
    args.flags = flags;

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

    newvol = get_nonnull_storage_vol (pool->conn, ret.vol);
    xdr_free ((xdrproc_t) &xdr_remote_storage_vol_create_xml_from_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
    return newvol;
}

6305 6306 6307 6308
static int
remoteStorageVolDelete (virStorageVolPtr vol,
                        unsigned int flags)
{
6309
    int rv = -1;
6310
    remote_storage_vol_delete_args args;
6311
    struct private_data *priv = vol->conn->storagePrivateData;
6312

6313 6314
    remoteDriverLock(priv);

6315 6316 6317 6318 6319 6320
    make_nonnull_storage_vol (&args.vol, vol);
    args.flags = flags;

    if (call (vol->conn, priv, 0, REMOTE_PROC_STORAGE_VOL_DELETE,
              (xdrproc_t) xdr_remote_storage_vol_delete_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
6321
        goto done;
6322

6323 6324 6325
    rv = 0;

done:
6326
    remoteDriverUnlock(priv);
6327
    return rv;
6328 6329
}

6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355
static int
remoteStorageVolWipe(virStorageVolPtr vol,
                     unsigned int flags)
{
    int rv = -1;
    remote_storage_vol_wipe_args args;
    struct private_data *priv = vol->conn->storagePrivateData;

    remoteDriverLock(priv);

    make_nonnull_storage_vol(&args.vol, vol);
    args.flags = flags;

    if (call(vol->conn, priv, 0, REMOTE_PROC_STORAGE_VOL_WIPE,
             (xdrproc_t) xdr_remote_storage_vol_wipe_args, (char *) &args,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}


6356 6357 6358
static int
remoteStorageVolGetInfo (virStorageVolPtr vol, virStorageVolInfoPtr info)
{
6359
    int rv = -1;
6360 6361
    remote_storage_vol_get_info_args args;
    remote_storage_vol_get_info_ret ret;
6362
    struct private_data *priv = vol->conn->storagePrivateData;
6363

6364 6365
    remoteDriverLock(priv);

6366 6367 6368 6369 6370 6371
    make_nonnull_storage_vol (&args.vol, vol);

    memset (&ret, 0, sizeof ret);
    if (call (vol->conn, priv, 0, REMOTE_PROC_STORAGE_VOL_GET_INFO,
              (xdrproc_t) xdr_remote_storage_vol_get_info_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_vol_get_info_ret, (char *) &ret) == -1)
6372
        goto done;
6373 6374 6375 6376 6377

    info->type = ret.type;
    info->capacity = ret.capacity;
    info->allocation = ret.allocation;

6378 6379 6380
    rv = 0;

done:
6381
    remoteDriverUnlock(priv);
6382
    return rv;
6383 6384 6385 6386 6387 6388
}

static char *
remoteStorageVolDumpXML (virStorageVolPtr vol,
                         unsigned int flags)
{
6389
    char *rv = NULL;
6390 6391
    remote_storage_vol_dump_xml_args args;
    remote_storage_vol_dump_xml_ret ret;
6392
    struct private_data *priv = vol->conn->storagePrivateData;
6393

6394 6395
    remoteDriverLock(priv);

6396 6397 6398 6399 6400 6401 6402
    make_nonnull_storage_vol (&args.vol, vol);
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (vol->conn, priv, 0, REMOTE_PROC_STORAGE_VOL_DUMP_XML,
              (xdrproc_t) xdr_remote_storage_vol_dump_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_vol_dump_xml_ret, (char *) &ret) == -1)
6403
        goto done;
6404 6405

    /* Caller frees. */
6406 6407 6408
    rv = ret.xml;

done:
6409
    remoteDriverUnlock(priv);
6410
    return rv;
6411 6412 6413 6414 6415
}

static char *
remoteStorageVolGetPath (virStorageVolPtr vol)
{
6416
    char *rv = NULL;
6417 6418
    remote_storage_vol_get_path_args args;
    remote_storage_vol_get_path_ret ret;
6419
    struct private_data *priv = vol->conn->storagePrivateData;
6420

6421 6422
    remoteDriverLock(priv);

6423 6424 6425 6426 6427 6428
    make_nonnull_storage_vol (&args.vol, vol);

    memset (&ret, 0, sizeof ret);
    if (call (vol->conn, priv, 0, REMOTE_PROC_STORAGE_VOL_GET_PATH,
              (xdrproc_t) xdr_remote_storage_vol_get_path_args, (char *) &args,
              (xdrproc_t) xdr_remote_storage_vol_get_path_ret, (char *) &ret) == -1)
6429
        goto done;
6430 6431

    /* Caller frees. */
6432 6433 6434
    rv = ret.name;

done:
6435
    remoteDriverUnlock(priv);
6436
    return rv;
6437 6438 6439
}


6440 6441
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
6442
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
6443 6444 6445 6446
remoteDevMonOpen(virConnectPtr conn,
                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                 int flags ATTRIBUTE_UNUSED)
{
6447 6448 6449
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
6450
    if (conn->driver &&
6451
        STREQ (conn->driver->name, "remote")) {
6452
        struct private_data *priv = conn->privateData;
6453 6454 6455 6456
        /* If we're here, the remote driver is already
         * in use due to a) a QEMU uri, or b) a remote
         * URI. So we can re-use existing connection
         */
6457 6458 6459 6460
        remoteDriverLock(priv);
        priv->localUses++;
        conn->devMonPrivateData = priv;
        remoteDriverUnlock(priv);
6461
        return VIR_DRV_OPEN_SUCCESS;
6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476
    } else if (conn->networkDriver &&
               STREQ (conn->networkDriver->name, "remote")) {
        struct private_data *priv = conn->networkPrivateData;
        remoteDriverLock(priv);
        conn->devMonPrivateData = priv;
        priv->localUses++;
        remoteDriverUnlock(priv);
        return VIR_DRV_OPEN_SUCCESS;
    } else {
        /* Using a non-remote driver, so we need to open a
         * new connection for network APIs, forcing it to
         * use the UNIX transport. This handles Xen driver
         * which doesn't have its own impl of the network APIs.
         */
        struct private_data *priv;
6477 6478 6479 6480 6481 6482
        int ret;
        ret = remoteOpenSecondaryDriver(conn,
                                        auth,
                                        flags,
                                        &priv);
        if (ret == VIR_DRV_OPEN_SUCCESS)
6483 6484 6485
            conn->devMonPrivateData = priv;
        return ret;
    }
6486 6487 6488 6489 6490
}

static int remoteDevMonClose(virConnectPtr conn)
{
    int ret = 0;
6491 6492
    struct private_data *priv = conn->devMonPrivateData;

6493 6494 6495 6496 6497 6498 6499 6500
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        ret = doRemoteClose(conn, priv);
        conn->devMonPrivateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
6501
    }
6502 6503
    if (priv)
        remoteDriverUnlock(priv);
6504 6505 6506 6507 6508 6509 6510
    return ret;
}

static int remoteNodeNumOfDevices(virConnectPtr conn,
                                  const char *cap,
                                  unsigned int flags)
{
6511
    int rv = -1;
6512 6513
    remote_node_num_of_devices_args args;
    remote_node_num_of_devices_ret ret;
6514
    struct private_data *priv = conn->devMonPrivateData;
6515

6516 6517
    remoteDriverLock(priv);

6518 6519 6520 6521 6522 6523 6524
    args.cap = cap ? (char **)&cap : NULL;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NODE_NUM_OF_DEVICES,
              (xdrproc_t) xdr_remote_node_num_of_devices_args, (char *) &args,
              (xdrproc_t) xdr_remote_node_num_of_devices_ret, (char *) &ret) == -1)
6525 6526 6527
        goto done;

    rv = ret.num;
6528

6529
done:
6530
    remoteDriverUnlock(priv);
6531
    return rv;
6532 6533 6534 6535 6536 6537 6538 6539 6540
}


static int remoteNodeListDevices(virConnectPtr conn,
                                 const char *cap,
                                 char **const names,
                                 int maxnames,
                                 unsigned int flags)
{
6541
    int rv = -1;
6542 6543 6544
    int i;
    remote_node_list_devices_args args;
    remote_node_list_devices_ret ret;
6545
    struct private_data *priv = conn->devMonPrivateData;
6546

6547 6548
    remoteDriverLock(priv);

6549
    if (maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
6550
        remoteError(VIR_ERR_RPC, "%s", _("too many device names requested"));
6551
        goto done;
6552 6553 6554 6555 6556 6557 6558 6559 6560
    }
    args.cap = cap ? (char **)&cap : NULL;
    args.maxnames = maxnames;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NODE_LIST_DEVICES,
              (xdrproc_t) xdr_remote_node_list_devices_args, (char *) &args,
              (xdrproc_t) xdr_remote_node_list_devices_ret, (char *) &ret) == -1)
6561
        goto done;
6562 6563

    if (ret.names.names_len > maxnames) {
6564
        remoteError(VIR_ERR_RPC, "%s", _("too many device names received"));
6565
        goto cleanup;
6566 6567 6568 6569 6570 6571 6572
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
6573
    for (i = 0; i < ret.names.names_len; ++i) {
6574 6575
        names[i] = strdup (ret.names.names_val[i]);

6576 6577 6578 6579
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

6580
            virReportOOMError();
6581 6582 6583 6584
            goto cleanup;
        }
    }

6585 6586 6587
    rv = ret.names.names_len;

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

6590
done:
6591
    remoteDriverUnlock(priv);
6592
    return rv;
6593 6594 6595 6596 6597 6598 6599 6600
}


static virNodeDevicePtr remoteNodeDeviceLookupByName(virConnectPtr conn,
                                                     const char *name)
{
    remote_node_device_lookup_by_name_args args;
    remote_node_device_lookup_by_name_ret ret;
6601
    virNodeDevicePtr dev = NULL;
6602
    struct private_data *priv = conn->devMonPrivateData;
6603

6604 6605
    remoteDriverLock(priv);

6606 6607 6608 6609 6610 6611
    args.name = (char *)name;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NODE_DEVICE_LOOKUP_BY_NAME,
              (xdrproc_t) xdr_remote_node_device_lookup_by_name_args, (char *) &args,
              (xdrproc_t) xdr_remote_node_device_lookup_by_name_ret, (char *) &ret) == -1)
6612
        goto done;
6613 6614 6615 6616 6617

    dev = get_nonnull_node_device(conn, ret.dev);

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

6618
done:
6619
    remoteDriverUnlock(priv);
6620 6621 6622 6623 6624 6625
    return dev;
}

static char *remoteNodeDeviceDumpXML(virNodeDevicePtr dev,
                                     unsigned int flags)
{
6626
    char *rv = NULL;
6627 6628
    remote_node_device_dump_xml_args args;
    remote_node_device_dump_xml_ret ret;
6629
    struct private_data *priv = dev->conn->devMonPrivateData;
6630

6631 6632
    remoteDriverLock(priv);

6633 6634 6635 6636 6637 6638 6639
    args.name = dev->name;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (dev->conn, priv, 0, REMOTE_PROC_NODE_DEVICE_DUMP_XML,
              (xdrproc_t) xdr_remote_node_device_dump_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_node_device_dump_xml_ret, (char *) &ret) == -1)
6640
        goto done;
6641 6642

    /* Caller frees. */
6643 6644 6645
    rv = ret.xml;

done:
6646
    remoteDriverUnlock(priv);
6647
    return rv;
6648 6649 6650 6651
}

static char *remoteNodeDeviceGetParent(virNodeDevicePtr dev)
{
6652
    char *rv = NULL;
6653 6654
    remote_node_device_get_parent_args args;
    remote_node_device_get_parent_ret ret;
6655
    struct private_data *priv = dev->conn->devMonPrivateData;
6656

6657 6658
    remoteDriverLock(priv);

6659 6660 6661 6662 6663 6664
    args.name = dev->name;

    memset (&ret, 0, sizeof ret);
    if (call (dev->conn, priv, 0, REMOTE_PROC_NODE_DEVICE_GET_PARENT,
              (xdrproc_t) xdr_remote_node_device_get_parent_args, (char *) &args,
              (xdrproc_t) xdr_remote_node_device_get_parent_ret, (char *) &ret) == -1)
6665
        goto done;
6666 6667

    /* Caller frees. */
6668
    rv = ret.parent ? *ret.parent : NULL;
6669
    VIR_FREE(ret.parent);
6670 6671

done:
6672
    remoteDriverUnlock(priv);
6673
    return rv;
6674 6675 6676 6677
}

static int remoteNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
6678
    int rv = -1;
6679 6680
    remote_node_device_num_of_caps_args args;
    remote_node_device_num_of_caps_ret ret;
6681
    struct private_data *priv = dev->conn->devMonPrivateData;
6682

6683 6684
    remoteDriverLock(priv);

6685 6686 6687 6688 6689 6690
    args.name = dev->name;

    memset (&ret, 0, sizeof ret);
    if (call (dev->conn, priv, 0, REMOTE_PROC_NODE_DEVICE_NUM_OF_CAPS,
              (xdrproc_t) xdr_remote_node_device_num_of_caps_args, (char *) &args,
              (xdrproc_t) xdr_remote_node_device_num_of_caps_ret, (char *) &ret) == -1)
6691 6692 6693
        goto done;

    rv = ret.num;
6694

6695
done:
6696
    remoteDriverUnlock(priv);
6697
    return rv;
6698 6699 6700 6701 6702 6703
}

static int remoteNodeDeviceListCaps(virNodeDevicePtr dev,
                                    char **const names,
                                    int maxnames)
{
6704
    int rv = -1;
6705 6706 6707
    int i;
    remote_node_device_list_caps_args args;
    remote_node_device_list_caps_ret ret;
6708
    struct private_data *priv = dev->conn->devMonPrivateData;
6709

6710 6711
    remoteDriverLock(priv);

6712
    if (maxnames > REMOTE_NODE_DEVICE_CAPS_LIST_MAX) {
6713
        remoteError(VIR_ERR_RPC, "%s", _("too many capability names requested"));
6714
        goto done;
6715 6716 6717 6718 6719 6720 6721 6722
    }
    args.maxnames = maxnames;
    args.name = dev->name;

    memset (&ret, 0, sizeof ret);
    if (call (dev->conn, priv, 0, REMOTE_PROC_NODE_DEVICE_LIST_CAPS,
              (xdrproc_t) xdr_remote_node_device_list_caps_args, (char *) &args,
              (xdrproc_t) xdr_remote_node_device_list_caps_ret, (char *) &ret) == -1)
6723
        goto done;
6724 6725

    if (ret.names.names_len > maxnames) {
6726
        remoteError(VIR_ERR_RPC, "%s", _("too many capability names received"));
6727
        goto cleanup;
6728 6729 6730 6731 6732 6733 6734
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
6735
    for (i = 0; i < ret.names.names_len; ++i) {
6736 6737
        names[i] = strdup (ret.names.names_val[i]);

6738 6739 6740 6741
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

6742
            virReportOOMError();
6743 6744 6745 6746
            goto cleanup;
        }
    }

6747 6748 6749
    rv = ret.names.names_len;

cleanup:
6750 6751
    xdr_free ((xdrproc_t) xdr_remote_node_device_list_caps_ret, (char *) &ret);

6752
done:
6753
    remoteDriverUnlock(priv);
6754
    return rv;
6755 6756
}

6757 6758 6759 6760 6761
static int
remoteNodeDeviceDettach (virNodeDevicePtr dev)
{
    int rv = -1;
    remote_node_device_dettach_args args;
6762 6763
    /* This method is unusual in that it uses the HV driver, not the devMon driver
     * hence its use of privateData, instead of devMonPrivateData */
6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786
    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;
6787 6788
    /* This method is unusual in that it uses the HV driver, not the devMon driver
     * hence its use of privateData, instead of devMonPrivateData */
6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811
    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;
6812 6813
    /* This method is unusual in that it uses the HV driver, not the devMon driver
     * hence its use of privateData, instead of devMonPrivateData */
6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831
    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;
}

6832

6833 6834 6835 6836 6837 6838 6839 6840
static virNodeDevicePtr
remoteNodeDeviceCreateXML(virConnectPtr conn,
                          const char *xmlDesc,
                          unsigned int flags)
{
    remote_node_device_create_xml_args args;
    remote_node_device_create_xml_ret ret;
    virNodeDevicePtr dev = NULL;
6841
    struct private_data *priv = conn->devMonPrivateData;
6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866

    remoteDriverLock(priv);

    memset(&ret, 0, sizeof ret);
    args.xml_desc = (char *)xmlDesc;
    args.flags = flags;

    if (call(conn, priv, 0, REMOTE_PROC_NODE_DEVICE_CREATE_XML,
             (xdrproc_t) xdr_remote_node_device_create_xml_args, (char *) &args,
             (xdrproc_t) xdr_remote_node_device_create_xml_ret, (char *) &ret) == -1)
        goto done;

    dev = get_nonnull_node_device(conn, ret.dev);
    xdr_free ((xdrproc_t) xdr_remote_node_device_create_xml_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
    return dev;
}

static int
remoteNodeDeviceDestroy(virNodeDevicePtr dev)
{
    int rv = -1;
    remote_node_device_destroy_args args;
6867
    struct private_data *priv = dev->conn->devMonPrivateData;
6868 6869 6870 6871 6872

    remoteDriverLock(priv);

    args.name = dev->name;

6873
    if (call(dev->conn, priv, 0, REMOTE_PROC_NODE_DEVICE_DESTROY,
6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884
             (xdrproc_t) xdr_remote_node_device_destroy_args, (char *) &args,
             (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034
/* ------------------------------------------------------------- */

static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
remoteNWFilterOpen (virConnectPtr conn,
                    virConnectAuthPtr auth,
                    int flags)
{
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

    if (conn->driver &&
        STREQ (conn->driver->name, "remote")) {
        struct private_data *priv;

       /* If we're here, the remote driver is already
         * in use due to a) a QEMU uri, or b) a remote
         * URI. So we can re-use existing connection
         */
        priv = conn->privateData;
        remoteDriverLock(priv);
        priv->localUses++;
        conn->nwfilterPrivateData = priv;
        remoteDriverUnlock(priv);
        return VIR_DRV_OPEN_SUCCESS;
    } else {
        /* Using a non-remote driver, so we need to open a
         * new connection for network filtering APIs, forcing it to
         * use the UNIX transport. This handles Xen driver
         * which doesn't have its own impl of the network filtering APIs.
         */
        struct private_data *priv;
        int ret;
        ret = remoteOpenSecondaryDriver(conn,
                                        auth,
                                        flags,
                                        &priv);
        if (ret == VIR_DRV_OPEN_SUCCESS)
            conn->nwfilterPrivateData = priv;
        return ret;
    }
}

static int
remoteNWFilterClose (virConnectPtr conn)
{
    int rv = 0;
    struct private_data *priv = conn->nwfilterPrivateData;

    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        rv = doRemoteClose(conn, priv);
        conn->nwfilterPrivateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
    }
    if (priv)
        remoteDriverUnlock(priv);
    return rv;
}


static int
remoteNumOfNWFilters (virConnectPtr conn)
{
    int rv = -1;
    remote_num_of_nwfilters_ret ret;
    struct private_data *priv = conn->nwfilterPrivateData;

    remoteDriverLock(priv);

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NUM_OF_NWFILTERS,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_num_of_nwfilters_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.num;

done:
    remoteDriverUnlock(priv);
    return rv;
}


static virNWFilterPtr
remoteNWFilterDefineXML (virConnectPtr conn, const char *xmlDesc,
                         unsigned int flags ATTRIBUTE_UNUSED)
{
    virNWFilterPtr net = NULL;
    remote_nwfilter_define_xml_args args;
    remote_nwfilter_define_xml_ret ret;
    struct private_data *priv = conn->nwfilterPrivateData;

    remoteDriverLock(priv);

    args.xml = (char *) xmlDesc;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NWFILTER_DEFINE_XML,
              (xdrproc_t) xdr_remote_nwfilter_define_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_nwfilter_define_xml_ret, (char *) &ret) == -1)
        goto done;

    net = get_nonnull_nwfilter (conn, ret.nwfilter);
    xdr_free ((xdrproc_t) &xdr_remote_nwfilter_define_xml_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
    return net;
}


static int
remoteNWFilterUndefine (virNWFilterPtr nwfilter)
{
    int rv = -1;
    remote_nwfilter_undefine_args args;
    struct private_data *priv = nwfilter->conn->nwfilterPrivateData;

    remoteDriverLock(priv);

    make_nonnull_nwfilter (&args.nwfilter, nwfilter);

    if (call (nwfilter->conn, priv, 0, REMOTE_PROC_NWFILTER_UNDEFINE,
              (xdrproc_t) xdr_remote_nwfilter_undefine_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}


static int
remoteListNWFilters (virConnectPtr conn, char **const names, int maxnames)
{
    int rv = -1;
    int i;
    remote_list_nwfilters_args args;
    remote_list_nwfilters_ret ret;
    struct private_data *priv = conn->nwfilterPrivateData;

    remoteDriverLock(priv);

    if (maxnames > REMOTE_NWFILTER_NAME_LIST_MAX) {
7035 7036 7037
        remoteError(VIR_ERR_RPC,
                    _("too many remote nwfilters: %d > %d"),
                    maxnames, REMOTE_NWFILTER_NAME_LIST_MAX);
7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048
        goto done;
    }
    args.maxnames = maxnames;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_LIST_NWFILTERS,
              (xdrproc_t) xdr_remote_list_nwfilters_args, (char *) &args,
              (xdrproc_t) xdr_remote_list_nwfilters_ret, (char *) &ret) == -1)
        goto done;

    if (ret.names.names_len > maxnames) {
7049 7050 7051
        remoteError(VIR_ERR_RPC,
                    _("too many remote nwfilters: %d > %d"),
                    ret.names.names_len, maxnames);
7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165
        goto cleanup;
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
    for (i = 0; i < ret.names.names_len; ++i) {
        names[i] = strdup (ret.names.names_val[i]);

        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

            virReportOOMError();
            goto cleanup;
        }
    }

    rv = ret.names.names_len;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}



static virNWFilterPtr
remoteNWFilterLookupByUUID (virConnectPtr conn,
                            const unsigned char *uuid)
{
    virNWFilterPtr net = NULL;
    remote_nwfilter_lookup_by_uuid_args args;
    remote_nwfilter_lookup_by_uuid_ret ret;
    struct private_data *priv = conn->nwfilterPrivateData;

    remoteDriverLock(priv);

    memcpy (args.uuid, uuid, VIR_UUID_BUFLEN);

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NWFILTER_LOOKUP_BY_UUID,
              (xdrproc_t) xdr_remote_nwfilter_lookup_by_uuid_args, (char *) &args,
              (xdrproc_t) xdr_remote_nwfilter_lookup_by_uuid_ret, (char *) &ret) == -1)
        goto done;

    net = get_nonnull_nwfilter (conn, ret.nwfilter);
    xdr_free ((xdrproc_t) &xdr_remote_nwfilter_lookup_by_uuid_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
    return net;
}

static virNWFilterPtr
remoteNWFilterLookupByName (virConnectPtr conn,
                            const char *name)
{
    virNWFilterPtr net = NULL;
    remote_nwfilter_lookup_by_name_args args;
    remote_nwfilter_lookup_by_name_ret ret;
    struct private_data *priv = conn->nwfilterPrivateData;

    remoteDriverLock(priv);

    args.name = (char *) name;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_NWFILTER_LOOKUP_BY_NAME,
              (xdrproc_t) xdr_remote_nwfilter_lookup_by_name_args, (char *) &args,
              (xdrproc_t) xdr_remote_nwfilter_lookup_by_name_ret, (char *) &ret) == -1)
        goto done;

    net = get_nonnull_nwfilter (conn, ret.nwfilter);
    xdr_free ((xdrproc_t) &xdr_remote_nwfilter_lookup_by_name_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
    return net;
}


static char *
remoteNWFilterGetXMLDesc (virNWFilterPtr nwfilter, unsigned int flags)
{
    char *rv = NULL;
    remote_nwfilter_get_xml_desc_args args;
    remote_nwfilter_get_xml_desc_ret ret;
    struct private_data *priv = nwfilter->conn->nwfilterPrivateData;

    remoteDriverLock(priv);

    make_nonnull_nwfilter (&args.nwfilter, nwfilter);
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (nwfilter->conn, priv, 0, REMOTE_PROC_NWFILTER_GET_XML_DESC,
              (xdrproc_t) xdr_remote_nwfilter_get_xml_desc_args, (char *) &args,
              (xdrproc_t) xdr_remote_nwfilter_get_xml_desc_ret, (char *) &ret) == -1)
        goto done;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

7166

7167 7168
/*----------------------------------------------------------------------*/

7169
static int
E
Eric Blake 已提交
7170 7171 7172
remoteAuthenticate (virConnectPtr conn, struct private_data *priv,
                    int in_open ATTRIBUTE_UNUSED,
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
7173
                    const char *authtype)
7174 7175
{
    struct remote_auth_list_ret ret;
7176
    int err, type = REMOTE_AUTH_NONE;
7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192

    memset(&ret, 0, sizeof ret);
    err = call (conn, priv,
                REMOTE_CALL_IN_OPEN | REMOTE_CALL_QUIET_MISSING_RPC,
                REMOTE_PROC_AUTH_LIST,
                (xdrproc_t) xdr_void, (char *) NULL,
                (xdrproc_t) xdr_remote_auth_list_ret, (char *) &ret);
    if (err == -2) /* Missing RPC - old server - ignore */
        return 0;

    if (err < 0)
        return -1;

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

7193 7194 7195 7196 7197 7198 7199 7200
    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 {
7201 7202
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("unknown authentication type %s"), authtype);
7203 7204 7205 7206 7207 7208 7209
            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) {
7210 7211 7212
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("requested authentication type %s rejected"),
                        authtype);
7213 7214 7215 7216 7217 7218 7219
            return -1;
        }
    } else {
        type = ret.types.types_val[0];
    }

    switch (type) {
7220
#if HAVE_SASL
7221 7222 7223 7224 7225 7226 7227
    case REMOTE_AUTH_SASL: {
        const char *mech = NULL;
        if (authtype &&
            STRCASEEQLEN(authtype, "sasl.", 5))
            mech = authtype + 5;

        if (remoteAuthSASL(conn, priv, in_open, auth, mech) < 0) {
7228
            VIR_FREE(ret.types.types_val);
7229 7230 7231
            return -1;
        }
        break;
7232
    }
7233 7234
#endif

7235 7236
#if HAVE_POLKIT
    case REMOTE_AUTH_POLKIT:
7237
        if (remoteAuthPolkit(conn, priv, in_open, auth) < 0) {
7238
            VIR_FREE(ret.types.types_val);
7239 7240 7241 7242 7243
            return -1;
        }
        break;
#endif

7244 7245 7246 7247 7248
    case REMOTE_AUTH_NONE:
        /* Nothing todo, hurrah ! */
        break;

    default:
7249 7250 7251
        remoteError(VIR_ERR_AUTH_FAILED,
                    _("unsupported authentication type %d"),
                    ret.types.types_val[0]);
7252
        VIR_FREE(ret.types.types_val);
7253 7254 7255
        return -1;
    }

7256
    VIR_FREE(ret.types.types_val);
7257 7258 7259 7260 7261 7262 7263

    return 0;
}



#if HAVE_SASL
7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341
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)
{
7342
    sasl_callback_t *cbs;
7343
    int i, n;
7344
    if (VIR_ALLOC_N(cbs, ncredtype+1) < 0) {
7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363
        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
7364
 *
7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378
 * 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 */

7379
    if (VIR_ALLOC_N(*cred, ninteract) < 0)
7380 7381 7382 7383 7384
        return -1;

    for (ninteract = 0 ; interact[ninteract].id != 0 ; ninteract++) {
        (*cred)[ninteract].type = remoteAuthCredSASL2Vir(interact[ninteract].id);
        if (!(*cred)[ninteract].type) {
7385
            VIR_FREE(*cred);
7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403
            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++)
7404 7405
        VIR_FREE(cred[i].result);
    VIR_FREE(cred);
7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426
}


/*
 * @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
7427 7428
 */
static int
7429 7430
remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open,
                virConnectAuthPtr auth, const char *wantmech)
7431 7432
{
    sasl_conn_t *saslconn = NULL;
7433
    sasl_security_properties_t secprops;
7434 7435 7436 7437 7438 7439
    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;
7440
    char *serverin = NULL;
7441 7442 7443
    unsigned int clientoutlen, serverinlen;
    const char *mech;
    int err, complete;
7444
    virSocketAddr sa;
7445
    char *localAddr = NULL, *remoteAddr = NULL;
7446 7447
    const void *val;
    sasl_ssf_t ssf;
7448 7449 7450 7451 7452 7453
    sasl_callback_t *saslcb = NULL;
    sasl_interact_t *interact = NULL;
    virConnectCredentialPtr cred = NULL;
    int ncred = 0;
    int ret = -1;
    const char *mechlist;
7454

7455
    VIR_DEBUG0("Client initialize SASL authentication");
7456 7457 7458
    /* Sets up the SASL library as a whole */
    err = sasl_client_init(NULL);
    if (err != SASL_OK) {
7459 7460 7461
        remoteError(VIR_ERR_AUTH_FAILED,
                    _("failed to initialize SASL library: %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
7462
        goto cleanup;
7463 7464 7465
    }

    /* Get local address in form  IPADDR:PORT */
7466 7467
    sa.len = sizeof(sa.data.stor);
    if (getsockname(priv->sock, &sa.data.sa, &sa.len) < 0) {
7468
        virReportSystemError(errno, "%s",
7469
                             _("failed to get sock address"));
7470
        goto cleanup;
7471
    }
7472
    if ((localAddr = virSocketFormatAddrFull(&sa, true, ";")) == NULL)
7473
        goto cleanup;
7474 7475

    /* Get remote address in form  IPADDR:PORT */
7476 7477
    sa.len = sizeof(sa.data.stor);
    if (getpeername(priv->sock, &sa.data.sa, &sa.len) < 0) {
7478
        virReportSystemError(errno, "%s",
7479
                             _("failed to get peer address"));
7480
        goto cleanup;
7481
    }
7482
    if ((remoteAddr = virSocketFormatAddrFull(&sa, true, ";")) == NULL)
7483 7484
        goto cleanup;

7485 7486 7487 7488 7489 7490
    if (auth) {
        if ((saslcb = remoteAuthMakeCallbacks(auth->credtype, auth->ncredtype)) == NULL)
            goto cleanup;
    } else {
        saslcb = NULL;
    }
7491 7492 7493 7494 7495 7496

    /* Setup a handle for being a client */
    err = sasl_client_new("libvirt",
                          priv->hostname,
                          localAddr,
                          remoteAddr,
7497
                          saslcb,
7498 7499
                          SASL_SUCCESS_DATA,
                          &saslconn);
7500

7501
    if (err != SASL_OK) {
7502 7503 7504
        remoteError(VIR_ERR_AUTH_FAILED,
                    _("Failed to create SASL client context: %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
7505
        goto cleanup;
7506 7507
    }

7508 7509 7510 7511 7512 7513
    /* Initialize some connection props we care about */
    if (priv->uses_tls) {
        gnutls_cipher_algorithm_t cipher;

        cipher = gnutls_cipher_get(priv->session);
        if (!(ssf = (sasl_ssf_t)gnutls_cipher_get_key_size(cipher))) {
7514 7515
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("invalid cipher size for TLS session"));
7516
            goto cleanup;
7517 7518 7519
        }
        ssf *= 8; /* key size is bytes, sasl wants bits */

7520
        VIR_DEBUG("Setting external SSF %d", ssf);
7521 7522
        err = sasl_setprop(saslconn, SASL_SSF_EXTERNAL, &ssf);
        if (err != SASL_OK) {
7523 7524 7525
            remoteError(VIR_ERR_INTERNAL_ERROR,
                        _("cannot set external SSF %d (%s)"),
                        err, sasl_errstring(err, NULL, NULL));
7526
            goto cleanup;
7527 7528 7529 7530
        }
    }

    memset (&secprops, 0, sizeof secprops);
7531 7532 7533
    /* If we've got a secure channel (TLS or UNIX sock), we don't care about SSF */
    secprops.min_ssf = priv->is_secure ? 0 : 56; /* Equiv to DES supported by all Kerberos */
    secprops.max_ssf = priv->is_secure ? 0 : 100000; /* Very strong ! AES == 256 */
7534
    secprops.maxbufsize = 100000;
7535 7536
    /* If we're not secure, then forbid any anonymous or trivially crackable auth */
    secprops.security_flags = priv->is_secure ? 0 :
7537 7538 7539 7540
        SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT;

    err = sasl_setprop(saslconn, SASL_SEC_PROPS, &secprops);
    if (err != SASL_OK) {
7541 7542 7543
        remoteError(VIR_ERR_INTERNAL_ERROR,
                    _("cannot set security props %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
7544
        goto cleanup;
7545 7546
    }

7547 7548 7549 7550
    /* First call is to inquire about supported mechanisms in the server */
    memset (&iret, 0, sizeof iret);
    if (call (conn, priv, in_open, REMOTE_PROC_AUTH_SASL_INIT,
              (xdrproc_t) xdr_void, (char *)NULL,
7551 7552
              (xdrproc_t) xdr_remote_auth_sasl_init_ret, (char *) &iret) != 0)
        goto cleanup;
7553 7554


7555 7556 7557
    mechlist = iret.mechlist;
    if (wantmech) {
        if (strstr(mechlist, wantmech) == NULL) {
7558 7559 7560
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("SASL mechanism %s not supported by server"),
                        wantmech);
7561
            VIR_FREE(iret.mechlist);
7562 7563 7564 7565 7566
            goto cleanup;
        }
        mechlist = wantmech;
    }
 restart:
7567
    /* Start the auth negotiation on the client end first */
7568
    VIR_DEBUG("Client start negotiation mechlist '%s'", mechlist);
7569
    err = sasl_client_start(saslconn,
7570 7571
                            mechlist,
                            &interact,
7572 7573 7574
                            &clientout,
                            &clientoutlen,
                            &mech);
7575
    if (err != SASL_OK && err != SASL_CONTINUE && err != SASL_INTERACT) {
7576 7577 7578
        remoteError(VIR_ERR_AUTH_FAILED,
                    _("Failed to start SASL negotiation: %d (%s)"),
                    err, sasl_errdetail(saslconn));
7579
        VIR_FREE(iret.mechlist);
7580 7581 7582 7583 7584
        goto cleanup;
    }

    /* Need to gather some credentials from the client */
    if (err == SASL_INTERACT) {
7585
        const char *msg;
7586 7587 7588 7589
        if (cred) {
            remoteAuthFreeCredentials(cred, ncred);
            cred = NULL;
        }
7590 7591 7592
        if ((ncred = remoteAuthMakeCredentials(interact, &cred)) < 0) {
            remoteError(VIR_ERR_AUTH_FAILED, "%s",
                        _("Failed to make auth credentials"));
7593
            VIR_FREE(iret.mechlist);
7594 7595 7596
            goto cleanup;
        }
        /* Run the authentication callback */
7597
        if (auth && auth->cb) {
7598 7599 7600
            if ((*(auth->cb))(cred, ncred, auth->cbdata) >= 0) {
                remoteAuthFillInteract(cred, interact);
                goto restart;
7601
            }
7602
            msg = "Failed to collect auth credentials";
7603
        } else {
7604
            msg = "No authentication callback available";
7605
        }
7606
        remoteError(VIR_ERR_AUTH_FAILED, "%s", msg);
7607
        goto cleanup;
7608
    }
7609
    VIR_FREE(iret.mechlist);
7610 7611

    if (clientoutlen > REMOTE_AUTH_SASL_DATA_MAX) {
7612 7613 7614
        remoteError(VIR_ERR_AUTH_FAILED,
                    _("SASL negotiation data too long: %d bytes"),
                    clientoutlen);
7615
        goto cleanup;
7616 7617 7618 7619 7620 7621 7622
    }
    /* 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;
7623
    VIR_DEBUG("Server start negotiation with mech %s. Data %d bytes %p", mech, clientoutlen, clientout);
7624 7625 7626 7627 7628

    /* Now send the initial auth data to the server */
    memset (&sret, 0, sizeof sret);
    if (call (conn, priv, in_open, REMOTE_PROC_AUTH_SASL_START,
              (xdrproc_t) xdr_remote_auth_sasl_start_args, (char *) &sargs,
7629 7630
              (xdrproc_t) xdr_remote_auth_sasl_start_ret, (char *) &sret) != 0)
        goto cleanup;
7631 7632 7633 7634 7635

    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;
7636
    VIR_DEBUG("Client step result complete: %d. Data %d bytes %p",
7637
          complete, serverinlen, serverin);
7638 7639 7640

    /* Loop-the-loop...
     * Even if the server has completed, the client must *always* do at least one step
D
Daniel Veillard 已提交
7641
     * in this loop to verify the server isn't lying about something. Mutual auth */
7642
    for (;;) {
7643
    restep:
7644 7645 7646
        err = sasl_client_step(saslconn,
                               serverin,
                               serverinlen,
7647
                               &interact,
7648 7649
                               &clientout,
                               &clientoutlen);
7650
        if (err != SASL_OK && err != SASL_CONTINUE && err != SASL_INTERACT) {
7651 7652 7653
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("Failed SASL step: %d (%s)"),
                        err, sasl_errdetail(saslconn));
7654 7655 7656 7657
            goto cleanup;
        }
        /* Need to gather some credentials from the client */
        if (err == SASL_INTERACT) {
7658
            const char *msg;
7659 7660 7661 7662 7663
            if (cred) {
                remoteAuthFreeCredentials(cred, ncred);
                cred = NULL;
            }
            if ((ncred = remoteAuthMakeCredentials(interact, &cred)) < 0) {
7664 7665
                remoteError(VIR_ERR_AUTH_FAILED, "%s",
                            _("Failed to make auth credentials"));
7666 7667 7668
                goto cleanup;
            }
            /* Run the authentication callback */
7669
            if (auth && auth->cb) {
7670 7671 7672
                if ((*(auth->cb))(cred, ncred, auth->cbdata) >= 0) {
                    remoteAuthFillInteract(cred, interact);
                    goto restep;
7673
                }
7674
                msg = _("Failed to collect auth credentials");
7675
            } else {
7676
                msg = _("No authentication callback available");
7677
            }
7678
            remoteError(VIR_ERR_AUTH_FAILED, "%s", msg);
7679
            goto cleanup;
7680 7681
        }

7682
        VIR_FREE(serverin);
7683
        VIR_DEBUG("Client step result %d. Data %d bytes %p", err, clientoutlen, clientout);
7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694

        /* Previous server call showed completion & we're now locally complete too */
        if (complete && err == SASL_OK)
            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;
7695
        VIR_DEBUG("Server step with %d bytes %p", clientoutlen, clientout);
7696 7697 7698 7699

        memset (&pret, 0, sizeof pret);
        if (call (conn, priv, in_open, REMOTE_PROC_AUTH_SASL_STEP,
                  (xdrproc_t) xdr_remote_auth_sasl_step_args, (char *) &pargs,
7700 7701
                  (xdrproc_t) xdr_remote_auth_sasl_step_ret, (char *) &pret) != 0)
            goto cleanup;
7702 7703 7704 7705 7706 7707

        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;

7708
        VIR_DEBUG("Client step result complete: %d. Data %d bytes %p",
7709
              complete, serverinlen, serverin);
7710 7711 7712

        /* This server call shows complete, and earlier client step was OK */
        if (complete && err == SASL_OK) {
7713
            VIR_FREE(serverin);
7714 7715 7716 7717
            break;
        }
    }

7718 7719
    /* Check for suitable SSF if not already secure (TLS or UNIX sock) */
    if (!priv->is_secure) {
7720 7721
        err = sasl_getprop(saslconn, SASL_SSF, &val);
        if (err != SASL_OK) {
7722 7723 7724
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("cannot query SASL ssf on connection %d (%s)"),
                        err, sasl_errstring(err, NULL, NULL));
7725
            goto cleanup;
7726 7727
        }
        ssf = *(const int *)val;
7728
        VIR_DEBUG("SASL SSF value %d", ssf);
7729
        if (ssf < 56) { /* 56 == DES level, good for Kerberos */
7730 7731
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("negotiation SSF %d was not strong enough"), ssf);
7732
            goto cleanup;
7733
        }
7734
        priv->is_secure = 1;
7735 7736
    }

7737
    VIR_DEBUG0("SASL authentication complete");
7738
    priv->saslconn = saslconn;
7739 7740 7741
    ret = 0;

 cleanup:
7742 7743 7744
    VIR_FREE(localAddr);
    VIR_FREE(remoteAddr);
    VIR_FREE(serverin);
7745

7746
    VIR_FREE(saslcb);
7747 7748 7749
    remoteAuthFreeCredentials(cred, ncred);
    if (ret != 0 && saslconn)
        sasl_dispose(&saslconn);
7750

7751
    return ret;
7752 7753 7754
}
#endif /* HAVE_SASL */

7755 7756

#if HAVE_POLKIT
7757
# if HAVE_POLKIT1
7758 7759 7760 7761 7762
static int
remoteAuthPolkit (virConnectPtr conn, struct private_data *priv, int in_open,
                  virConnectAuthPtr auth ATTRIBUTE_UNUSED)
{
    remote_auth_polkit_ret ret;
7763
    VIR_DEBUG0("Client initialize PolicyKit-1 authentication");
7764 7765 7766 7767 7768 7769 7770 7771

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

7772
    VIR_DEBUG0("PolicyKit-1 authentication complete");
7773 7774
    return 0;
}
7775
# elif HAVE_POLKIT0
7776 7777 7778 7779 7780 7781 7782
/* Perform the PolicyKit authentication process
 */
static int
remoteAuthPolkit (virConnectPtr conn, struct private_data *priv, int in_open,
                  virConnectAuthPtr auth)
{
    remote_auth_polkit_ret ret;
7783
    int i, allowcb = 0;
7784 7785 7786 7787 7788 7789 7790 7791
    virConnectCredential cred = {
        VIR_CRED_EXTERNAL,
        conn->flags & VIR_CONNECT_RO ? "org.libvirt.unix.monitor" : "org.libvirt.unix.manage",
        "PolicyKit",
        NULL,
        NULL,
        0,
    };
7792
    VIR_DEBUG0("Client initialize PolicyKit-0 authentication");
7793

7794
    if (auth && auth->cb) {
7795
        /* Check if the necessary credential type for PolicyKit is supported */
7796 7797 7798 7799
        for (i = 0 ; i < auth->ncredtype ; i++) {
            if (auth->credtype[i] == VIR_CRED_EXTERNAL)
                allowcb = 1;
        }
7800

7801
        if (allowcb) {
7802
            VIR_DEBUG0("Client run callback for PolicyKit authentication");
7803 7804
            /* Run the authentication callback */
            if ((*(auth->cb))(&cred, 1, auth->cbdata) < 0) {
7805 7806
                remoteError(VIR_ERR_AUTH_FAILED, "%s",
                            _("Failed to collect auth credentials"));
7807 7808
                return -1;
            }
7809
        } else {
7810
            VIR_DEBUG0("Client auth callback does not support PolicyKit");
7811 7812
        }
    } else {
7813
        VIR_DEBUG0("No auth callback provided");
7814 7815 7816 7817 7818 7819 7820 7821 7822
    }

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

7823
    VIR_DEBUG0("PolicyKit-0 authentication complete");
7824 7825
    return 0;
}
7826
# endif /* HAVE_POLKIT0 */
7827
#endif /* HAVE_POLKIT */
7828 7829
/*----------------------------------------------------------------------*/

7830 7831 7832 7833
static int remoteDomainEventRegister(virConnectPtr conn,
                                     virConnectDomainEventCallback callback,
                                     void *opaque,
                                     virFreeCallback freecb)
7834
{
7835
    int rv = -1;
7836 7837
    struct private_data *priv = conn->privateData;

7838 7839
    remoteDriverLock(priv);

7840
    if (priv->eventFlushTimer < 0) {
7841
         remoteError(VIR_ERR_NO_SUPPORT, "%s", _("no event support"));
7842
         goto done;
7843
    }
7844
    if (virDomainEventCallbackListAdd(conn, priv->callbackList,
7845
                                      callback, opaque, freecb) < 0) {
7846
         remoteError(VIR_ERR_RPC, "%s", _("adding cb to list"));
7847
         goto done;
7848 7849
    }

7850
    if (virDomainEventCallbackListCountID(conn, priv->callbackList, VIR_DOMAIN_EVENT_ID_LIFECYCLE) == 1) {
7851 7852 7853 7854
        /* 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)
7855
            goto done;
7856 7857
    }

7858 7859 7860
    rv = 0;

done:
7861
    remoteDriverUnlock(priv);
7862
    return rv;
7863 7864
}

7865 7866
static int remoteDomainEventDeregister(virConnectPtr conn,
                                       virConnectDomainEventCallback callback)
7867 7868
{
    struct private_data *priv = conn->privateData;
7869
    int rv = -1;
7870

7871 7872
    remoteDriverLock(priv);

7873 7874 7875
    if (priv->domainEventDispatching) {
        if (virDomainEventCallbackListMarkDelete(conn, priv->callbackList,
                                                 callback) < 0) {
7876
            remoteError(VIR_ERR_RPC, "%s", _("marking cb for deletion"));
7877
            goto done;
7878 7879 7880 7881
        }
    } else {
        if (virDomainEventCallbackListRemove(conn, priv->callbackList,
                                             callback) < 0) {
7882
            remoteError(VIR_ERR_RPC, "%s", _("removing cb from list"));
7883 7884
            goto done;
        }
7885
    }
7886

7887 7888 7889 7890 7891 7892
    if (virDomainEventCallbackListCountID(conn, priv->callbackList, VIR_DOMAIN_EVENT_ID_LIFECYCLE) == 0) {
        /* 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;
7893 7894
    }

7895 7896 7897
    rv = 0;

done:
7898
    remoteDriverUnlock(priv);
7899
    return rv;
7900
}
7901

7902 7903 7904 7905 7906 7907 7908 7909
/**
 * remoteDomainReadEventLifecycle
 *
 * Read the domain lifecycle event data off the wire
 */
static virDomainEventPtr
remoteDomainReadEventLifecycle(virConnectPtr conn, XDR *xdr)
{
7910
    remote_domain_event_lifecycle_msg msg;
7911 7912 7913 7914 7915
    virDomainPtr dom;
    virDomainEventPtr event = NULL;
    memset (&msg, 0, sizeof msg);

    /* unmarshall parameters, and process it*/
7916
    if (! xdr_remote_domain_event_lifecycle_msg(xdr, &msg) ) {
7917 7918
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall lifecycle event"));
7919 7920 7921 7922 7923 7924 7925 7926
        return NULL;
    }

    dom = get_nonnull_domain(conn,msg.dom);
    if (!dom)
        return NULL;

    event = virDomainEventNewFromDom(dom, msg.event, msg.detail);
7927
    xdr_free ((xdrproc_t) &xdr_remote_domain_event_lifecycle_msg, (char *) &msg);
7928 7929 7930 7931 7932 7933

    virDomainFree(dom);
    return event;
}


7934 7935 7936 7937 7938 7939 7940 7941 7942 7943
static virDomainEventPtr
remoteDomainReadEventReboot(virConnectPtr conn, XDR *xdr)
{
    remote_domain_event_reboot_msg msg;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;
    memset (&msg, 0, sizeof msg);

    /* unmarshall parameters, and process it*/
    if (! xdr_remote_domain_event_reboot_msg(xdr, &msg) ) {
7944 7945
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall reboot event"));
7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960
        return NULL;
    }

    dom = get_nonnull_domain(conn,msg.dom);
    if (!dom)
        return NULL;

    event = virDomainEventRebootNewFromDom(dom);
    xdr_free ((xdrproc_t) &xdr_remote_domain_event_reboot_msg, (char *) &msg);

    virDomainFree(dom);
    return event;
}


7961 7962 7963 7964 7965 7966 7967 7968 7969 7970
static virDomainEventPtr
remoteDomainReadEventRTCChange(virConnectPtr conn, XDR *xdr)
{
    remote_domain_event_rtc_change_msg msg;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;
    memset (&msg, 0, sizeof msg);

    /* unmarshall parameters, and process it*/
    if (! xdr_remote_domain_event_rtc_change_msg(xdr, &msg) ) {
7971 7972
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall reboot event"));
7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987
        return NULL;
    }

    dom = get_nonnull_domain(conn,msg.dom);
    if (!dom)
        return NULL;

    event = virDomainEventRTCChangeNewFromDom(dom, msg.offset);
    xdr_free ((xdrproc_t) &xdr_remote_domain_event_rtc_change_msg, (char *) &msg);

    virDomainFree(dom);
    return event;
}


7988 7989 7990 7991 7992 7993 7994 7995 7996 7997
static virDomainEventPtr
remoteDomainReadEventWatchdog(virConnectPtr conn, XDR *xdr)
{
    remote_domain_event_watchdog_msg msg;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;
    memset (&msg, 0, sizeof msg);

    /* unmarshall parameters, and process it*/
    if (! xdr_remote_domain_event_watchdog_msg(xdr, &msg) ) {
7998 7999
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall reboot event"));
8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014
        return NULL;
    }

    dom = get_nonnull_domain(conn,msg.dom);
    if (!dom)
        return NULL;

    event = virDomainEventWatchdogNewFromDom(dom, msg.action);
    xdr_free ((xdrproc_t) &xdr_remote_domain_event_watchdog_msg, (char *) &msg);

    virDomainFree(dom);
    return event;
}


8015 8016 8017 8018 8019 8020 8021 8022 8023 8024
static virDomainEventPtr
remoteDomainReadEventIOError(virConnectPtr conn, XDR *xdr)
{
    remote_domain_event_io_error_msg msg;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;
    memset (&msg, 0, sizeof msg);

    /* unmarshall parameters, and process it*/
    if (! xdr_remote_domain_event_io_error_msg(xdr, &msg) ) {
8025 8026
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall reboot event"));
8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044
        return NULL;
    }

    dom = get_nonnull_domain(conn,msg.dom);
    if (!dom)
        return NULL;

    event = virDomainEventIOErrorNewFromDom(dom,
                                            msg.srcPath,
                                            msg.devAlias,
                                            msg.action);
    xdr_free ((xdrproc_t) &xdr_remote_domain_event_io_error_msg, (char *) &msg);

    virDomainFree(dom);
    return event;
}


8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075
static virDomainEventPtr
remoteDomainReadEventIOErrorReason(virConnectPtr conn, XDR *xdr)
{
    remote_domain_event_io_error_reason_msg msg;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;
    memset (&msg, 0, sizeof msg);

    /* unmarshall parameters, and process it*/
    if (! xdr_remote_domain_event_io_error_reason_msg(xdr, &msg) ) {
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall reboot event"));
        return NULL;
    }

    dom = get_nonnull_domain(conn,msg.dom);
    if (!dom)
        return NULL;

    event = virDomainEventIOErrorReasonNewFromDom(dom,
                                                  msg.srcPath,
                                                  msg.devAlias,
                                                  msg.action,
                                                  msg.reason);
    xdr_free ((xdrproc_t) &xdr_remote_domain_event_io_error_reason_msg, (char *) &msg);

    virDomainFree(dom);
    return event;
}


8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090
static virDomainEventPtr
remoteDomainReadEventGraphics(virConnectPtr conn, XDR *xdr)
{
    remote_domain_event_graphics_msg msg;
    virDomainPtr dom;
    virDomainEventPtr event = NULL;
    virDomainEventGraphicsAddressPtr localAddr = NULL;
    virDomainEventGraphicsAddressPtr remoteAddr = NULL;
    virDomainEventGraphicsSubjectPtr subject = NULL;
    int i;

    memset (&msg, 0, sizeof msg);

    /* unmarshall parameters, and process it*/
    if (! xdr_remote_domain_event_graphics_msg(xdr, &msg) ) {
8091 8092
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall reboot event"));
8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160
        return NULL;
    }

    dom = get_nonnull_domain(conn,msg.dom);
    if (!dom)
        return NULL;

    if (VIR_ALLOC(localAddr) < 0)
        goto no_memory;
    localAddr->family = msg.local.family;
    if (!(localAddr->service = strdup(msg.local.service)) ||
        !(localAddr->node = strdup(msg.local.node)))
        goto no_memory;

    if (VIR_ALLOC(remoteAddr) < 0)
        goto no_memory;
    remoteAddr->family = msg.remote.family;
    if (!(remoteAddr->service = strdup(msg.remote.service)) ||
        !(remoteAddr->node = strdup(msg.remote.node)))
        goto no_memory;

    if (VIR_ALLOC(subject) < 0)
        goto no_memory;
    if (VIR_ALLOC_N(subject->identities, msg.subject.subject_len) < 0)
        goto no_memory;
    subject->nidentity = msg.subject.subject_len;
    for (i = 0 ; i < subject->nidentity ; i++) {
        if (!(subject->identities[i].type = strdup(msg.subject.subject_val[i].type)) ||
            !(subject->identities[i].name = strdup(msg.subject.subject_val[i].name)))
            goto no_memory;
    }

    event = virDomainEventGraphicsNewFromDom(dom,
                                             msg.phase,
                                             localAddr,
                                             remoteAddr,
                                             msg.authScheme,
                                             subject);
    xdr_free ((xdrproc_t) &xdr_remote_domain_event_graphics_msg, (char *) &msg);

    virDomainFree(dom);
    return event;

no_memory:
    xdr_free ((xdrproc_t) &xdr_remote_domain_event_graphics_msg, (char *) &msg);

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


J
Jim Meyering 已提交
8161
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
8162 8163 8164 8165 8166 8167 8168
remoteSecretOpen (virConnectPtr conn,
                  virConnectAuthPtr auth,
                  int flags)
{
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
8169
    if (conn->driver &&
8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261
        STREQ (conn->driver->name, "remote")) {
        struct private_data *priv;

        /* If we're here, the remote driver is already
         * in use due to a) a QEMU uri, or b) a remote
         * URI. So we can re-use existing connection
         */
        priv = conn->privateData;
        remoteDriverLock(priv);
        priv->localUses++;
        conn->secretPrivateData = 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);
        conn->secretPrivateData = priv;
        priv->localUses++;
        remoteDriverUnlock(priv);
        return VIR_DRV_OPEN_SUCCESS;
    } else {
        /* Using a non-remote driver, so we need to open a
         * new connection for secret APIs, forcing it to
         * use the UNIX transport.
         */
        struct private_data *priv;
        int ret;
        ret = remoteOpenSecondaryDriver(conn,
                                        auth,
                                        flags,
                                        &priv);
        if (ret == VIR_DRV_OPEN_SUCCESS)
            conn->secretPrivateData = priv;
        return ret;
    }
}

static int
remoteSecretClose (virConnectPtr conn)
{
    int rv = 0;
    struct private_data *priv = conn->secretPrivateData;

    conn->secretPrivateData = NULL;
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        rv = doRemoteClose(conn, priv);
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
    }
    if (priv)
        remoteDriverUnlock(priv);
    return rv;
}

static int
remoteSecretNumOfSecrets (virConnectPtr conn)
{
    int rv = -1;
    remote_num_of_secrets_ret ret;
    struct private_data *priv = conn->secretPrivateData;

    remoteDriverLock (priv);

    memset (&ret, 0, sizeof (ret));
    if (call (conn, priv, 0, REMOTE_PROC_NUM_OF_SECRETS,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_remote_num_of_secrets_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.num;

done:
    remoteDriverUnlock (priv);
    return rv;
}

static int
remoteSecretListSecrets (virConnectPtr conn, char **uuids, int maxuuids)
{
    int rv = -1;
    int i;
    remote_list_secrets_args args;
    remote_list_secrets_ret ret;
    struct private_data *priv = conn->secretPrivateData;

    remoteDriverLock(priv);

    if (maxuuids > REMOTE_SECRET_UUID_LIST_MAX) {
8262 8263
        remoteError(VIR_ERR_RPC, _("too many remote secret UUIDs: %d > %d"),
                    maxuuids, REMOTE_SECRET_UUID_LIST_MAX);
8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274
        goto done;
    }
    args.maxuuids = maxuuids;

    memset (&ret, 0, sizeof ret);
    if (call (conn, priv, 0, REMOTE_PROC_LIST_SECRETS,
              (xdrproc_t) xdr_remote_list_secrets_args, (char *) &args,
              (xdrproc_t) xdr_remote_list_secrets_ret, (char *) &ret) == -1)
        goto done;

    if (ret.uuids.uuids_len > maxuuids) {
8275 8276
        remoteError(VIR_ERR_RPC, _("too many remote secret UUIDs: %d > %d"),
                    ret.uuids.uuids_len, maxuuids);
8277 8278 8279 8280 8281 8282 8283
        goto cleanup;
    }

    /* This call is caller-frees.  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
8284
    for (i = 0; i < ret.uuids.uuids_len; ++i) {
8285 8286
        uuids[i] = strdup (ret.uuids.uuids_val[i]);

8287 8288 8289 8290
        if (uuids[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(uuids[i]);

8291
            virReportOOMError();
8292 8293 8294 8295
            goto cleanup;
        }
    }

8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306
    rv = ret.uuids.uuids_len;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

static virSecretPtr
8307
remoteSecretLookupByUUID (virConnectPtr conn, const unsigned char *uuid)
8308 8309
{
    virSecretPtr rv = NULL;
8310 8311
    remote_secret_lookup_by_uuid_args args;
    remote_secret_lookup_by_uuid_ret ret;
8312 8313 8314 8315
    struct private_data *priv = conn->secretPrivateData;

    remoteDriverLock (priv);

8316
    memcpy (args.uuid, uuid, VIR_UUID_BUFLEN);
8317 8318

    memset (&ret, 0, sizeof (ret));
8319 8320 8321
    if (call (conn, priv, 0, REMOTE_PROC_SECRET_LOOKUP_BY_UUID,
              (xdrproc_t) xdr_remote_secret_lookup_by_uuid_args, (char *) &args,
              (xdrproc_t) xdr_remote_secret_lookup_by_uuid_ret, (char *) &ret) == -1)
8322 8323 8324
        goto done;

    rv = get_nonnull_secret (conn, ret.secret);
8325
    xdr_free ((xdrproc_t) xdr_remote_secret_lookup_by_uuid_ret,
8326 8327 8328 8329 8330 8331 8332
              (char *) &ret);

done:
    remoteDriverUnlock (priv);
    return rv;
}

8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360
static virSecretPtr
remoteSecretLookupByUsage (virConnectPtr conn, int usageType, const char *usageID)
{
    virSecretPtr rv = NULL;
    remote_secret_lookup_by_usage_args args;
    remote_secret_lookup_by_usage_ret ret;
    struct private_data *priv = conn->secretPrivateData;

    remoteDriverLock (priv);

    args.usageType = usageType;
    args.usageID = (char *)usageID;

    memset (&ret, 0, sizeof (ret));
    if (call (conn, priv, 0, REMOTE_PROC_SECRET_LOOKUP_BY_USAGE,
              (xdrproc_t) xdr_remote_secret_lookup_by_usage_args, (char *) &args,
              (xdrproc_t) xdr_remote_secret_lookup_by_usage_ret, (char *) &ret) == -1)
        goto done;

    rv = get_nonnull_secret (conn, ret.secret);
    xdr_free ((xdrproc_t) xdr_remote_secret_lookup_by_usage_ret,
              (char *) &ret);

done:
    remoteDriverUnlock (priv);
    return rv;
}

8361 8362 8363 8364 8365
static virSecretPtr
remoteSecretDefineXML (virConnectPtr conn, const char *xml, unsigned int flags)
{
    virSecretPtr rv = NULL;
    remote_secret_define_xml_args args;
8366
    remote_secret_define_xml_ret ret;
8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380
    struct private_data *priv = conn->secretPrivateData;

    remoteDriverLock (priv);

    args.xml = (char *) xml;
    args.flags = flags;

    memset (&ret, 0, sizeof (ret));
    if (call (conn, priv, 0, REMOTE_PROC_SECRET_DEFINE_XML,
              (xdrproc_t) xdr_remote_secret_define_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_secret_define_xml_ret, (char *) &ret) == -1)
        goto done;

    rv = get_nonnull_secret (conn, ret.secret);
8381
    xdr_free ((xdrproc_t) xdr_remote_secret_define_xml_ret,
8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493
              (char *) &ret);

done:
    remoteDriverUnlock (priv);
    return rv;
}

static char *
remoteSecretGetXMLDesc (virSecretPtr secret, unsigned int flags)
{
    char *rv = NULL;
    remote_secret_get_xml_desc_args args;
    remote_secret_get_xml_desc_ret ret;
    struct private_data *priv = secret->conn->secretPrivateData;

    remoteDriverLock (priv);

    make_nonnull_secret (&args.secret, secret);
    args.flags = flags;

    memset (&ret, 0, sizeof (ret));
    if (call (secret->conn, priv, 0, REMOTE_PROC_SECRET_GET_XML_DESC,
              (xdrproc_t) xdr_remote_secret_get_xml_desc_args, (char *) &args,
              (xdrproc_t) xdr_remote_secret_get_xml_desc_ret, (char *) &ret) == -1)
        goto done;

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

done:
    remoteDriverUnlock (priv);
    return rv;
}

static int
remoteSecretSetValue (virSecretPtr secret, const unsigned char *value,
                      size_t value_size, unsigned int flags)
{
    int rv = -1;
    remote_secret_set_value_args args;
    struct private_data *priv = secret->conn->secretPrivateData;

    remoteDriverLock (priv);

    make_nonnull_secret (&args.secret, secret);
    args.value.value_len = value_size;
    args.value.value_val = (char *) value;
    args.flags = flags;

    if (call (secret->conn, priv, 0, REMOTE_PROC_SECRET_SET_VALUE,
              (xdrproc_t) xdr_remote_secret_set_value_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock (priv);
    return rv;
}

static unsigned char *
remoteSecretGetValue (virSecretPtr secret, size_t *value_size,
                      unsigned int flags)
{
    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);

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

static int
remoteSecretUndefine (virSecretPtr secret)
{
    int rv = -1;
    remote_secret_undefine_args args;
    struct private_data *priv = secret->conn->secretPrivateData;

    remoteDriverLock (priv);

    make_nonnull_secret (&args.secret, secret);

    if (call (secret->conn, priv, 0, REMOTE_PROC_SECRET_UNDEFINE,
              (xdrproc_t) xdr_remote_secret_undefine_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock (priv);
    return rv;
}

8494 8495 8496 8497 8498 8499 8500 8501 8502 8503

static struct private_stream_data *
remoteStreamOpen(virStreamPtr st,
                 int output ATTRIBUTE_UNUSED,
                 unsigned int proc_nr,
                 unsigned int serial)
{
    struct private_data *priv = st->conn->privateData;
    struct private_stream_data *stpriv;

8504
    if (VIR_ALLOC(stpriv) < 0) {
8505
        virReportOOMError();
8506
        return NULL;
8507
    }
8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519

    /* Initialize call object used to receive replies */
    stpriv->proc_nr = proc_nr;
    stpriv->serial = serial;

    stpriv->next = priv->streams;
    priv->streams = stpriv;

    return stpriv;
}


8520 8521 8522 8523 8524 8525
static void
remoteStreamEventTimerUpdate(struct private_stream_data *privst)
{
    if (!privst->cb)
        return;

8526 8527 8528 8529 8530
    VIR_DEBUG("Check timer offset=%d %d", privst->incomingOffset, privst->cbEvents);
    if ((privst->incomingOffset &&
         (privst->cbEvents & VIR_STREAM_EVENT_READABLE)) ||
        (privst->cbEvents & VIR_STREAM_EVENT_WRITABLE)) {
        VIR_DEBUG0("Enabling event timer");
8531
        virEventUpdateTimeout(privst->cbTimer, 0);
8532 8533 8534 8535
    } else {
        VIR_DEBUG0("Disabling event timer");
        virEventUpdateTimeout(privst->cbTimer, -1);
    }
8536 8537 8538
}


8539 8540 8541 8542 8543 8544
static int
remoteStreamPacket(virStreamPtr st,
                   int status,
                   const char *data,
                   size_t nbytes)
{
8545
    VIR_DEBUG("st=%p status=%d data=%p nbytes=%zu", st, status, data, nbytes);
8546 8547 8548 8549 8550
    struct private_data *priv = st->conn->privateData;
    struct private_stream_data *privst = st->privateData;
    XDR xdr;
    struct remote_thread_call *thiscall;
    remote_message_header hdr;
8551
    int ret;
8552 8553 8554 8555

    memset(&hdr, 0, sizeof hdr);

    if (VIR_ALLOC(thiscall) < 0) {
8556
        virReportOOMError();
8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568
        return -1;
    }

    thiscall->mode = REMOTE_MODE_WAIT_TX;
    thiscall->serial = privst->serial;
    thiscall->proc_nr = privst->proc_nr;
    if (status == REMOTE_OK ||
        status == REMOTE_ERROR)
        thiscall->want_reply = 1;

    if (virCondInit(&thiscall->cond) < 0) {
        VIR_FREE(thiscall);
8569 8570
        remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("cannot initialize mutex"));
8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594
        return -1;
    }

    /* Don't fill in any other fields in 'thiscall' since
     * we're not expecting a reply for this */

    hdr.prog = REMOTE_PROGRAM;
    hdr.vers = REMOTE_PROTOCOL_VERSION;
    hdr.proc = privst->proc_nr;
    hdr.type = REMOTE_STREAM;
    hdr.serial = privst->serial;
    hdr.status = status;


    /* Length must include the length word itself (always encoded in
     * 4 bytes as per RFC 4506), so offset start length. We write this
     * later.
     */
    thiscall->bufferLength = REMOTE_MESSAGE_HEADER_XDR_LEN;

    /* Serialise header followed by args. */
    xdrmem_create (&xdr, thiscall->buffer + thiscall->bufferLength,
                   REMOTE_MESSAGE_MAX, XDR_ENCODE);
    if (!xdr_remote_message_header (&xdr, &hdr)) {
8595
        remoteError(VIR_ERR_RPC, "%s", _("xdr_remote_message_header failed"));
8596 8597 8598 8599 8600 8601 8602 8603
        goto error;
    }

    thiscall->bufferLength += xdr_getpos (&xdr);
    xdr_destroy (&xdr);

    if (status == REMOTE_CONTINUE) {
        if (((4 + REMOTE_MESSAGE_MAX) - thiscall->bufferLength) < nbytes) {
8604 8605
            remoteError(VIR_ERR_RPC, _("data size %zu too large for payload %d"),
                        nbytes, ((4 + REMOTE_MESSAGE_MAX) - thiscall->bufferLength));
8606 8607 8608 8609 8610 8611 8612 8613 8614 8615
            goto error;
        }

        memcpy(thiscall->buffer + thiscall->bufferLength, data, nbytes);
        thiscall->bufferLength += nbytes;
    }

    /* Go back to packet start and encode the length word. */
    xdrmem_create (&xdr, thiscall->buffer, REMOTE_MESSAGE_HEADER_XDR_LEN, XDR_ENCODE);
    if (!xdr_u_int (&xdr, &thiscall->bufferLength)) {
8616
        remoteError(VIR_ERR_RPC, "%s", _("xdr_u_int (length word)"));
8617 8618 8619 8620
        goto error;
    }
    xdr_destroy (&xdr);

8621
    ret = remoteIO(st->conn, priv, 0, thiscall);
8622
    ignore_value(virCondDestroy(&thiscall->cond));
8623 8624
    VIR_FREE(thiscall);
    if (ret < 0)
8625 8626 8627 8628 8629 8630
        return -1;

    return nbytes;

error:
    xdr_destroy (&xdr);
8631
    ignore_value(virCondDestroy(&thiscall->cond));
8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642
    VIR_FREE(thiscall);
    return -1;
}

static int
remoteStreamHasError(virStreamPtr st) {
    struct private_stream_data *privst = st->privateData;
    if (!privst->has_error) {
        return 0;
    }

8643
    VIR_DEBUG0("Raising async error");
8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691
    virRaiseErrorFull(st->conn,
                      __FILE__, __FUNCTION__, __LINE__,
                      privst->err.domain,
                      privst->err.code,
                      privst->err.level,
                      privst->err.str1 ? *privst->err.str1 : NULL,
                      privst->err.str2 ? *privst->err.str2 : NULL,
                      privst->err.str3 ? *privst->err.str3 : NULL,
                      privst->err.int1,
                      privst->err.int2,
                      "%s", privst->err.message ? *privst->err.message : NULL);

    return 1;
}

static void
remoteStreamRelease(virStreamPtr st)
{
    struct private_data *priv = st->conn->privateData;
    struct private_stream_data *privst = st->privateData;

    if (priv->streams == privst)
        priv->streams = privst->next;
    else {
        struct private_stream_data *tmp = priv->streams;
        while (tmp && tmp->next) {
            if (tmp->next == privst) {
                tmp->next = privst->next;
                break;
            }
        }
    }

    if (privst->has_error)
        xdr_free((xdrproc_t)xdr_remote_error,  (char *)&privst->err);

    VIR_FREE(privst);

    st->driver = NULL;
    st->privateData = NULL;
}


static int
remoteStreamSend(virStreamPtr st,
                 const char *data,
                 size_t nbytes)
{
8692
    VIR_DEBUG("st=%p data=%p nbytes=%zu", st, data, nbytes);
8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720
    struct private_data *priv = st->conn->privateData;
    int rv = -1;

    remoteDriverLock(priv);

    if (remoteStreamHasError(st))
        goto cleanup;

    rv = remoteStreamPacket(st,
                            REMOTE_CONTINUE,
                            data,
                            nbytes);

cleanup:
    if (rv == -1)
        remoteStreamRelease(st);

    remoteDriverUnlock(priv);

    return rv;
}


static int
remoteStreamRecv(virStreamPtr st,
                 char *data,
                 size_t nbytes)
{
8721
    VIR_DEBUG("st=%p data=%p nbytes=%zu", st, data, nbytes);
8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732
    struct private_data *priv = st->conn->privateData;
    struct private_stream_data *privst = st->privateData;
    int rv = -1;

    remoteDriverLock(priv);

    if (remoteStreamHasError(st))
        goto cleanup;

    if (!privst->incomingOffset) {
        struct remote_thread_call *thiscall;
8733
        int ret;
8734

8735
        if (st->flags & VIR_STREAM_NONBLOCK) {
8736
            VIR_DEBUG0("Non-blocking mode and no data available");
8737 8738 8739 8740
            rv = -2;
            goto cleanup;
        }

8741
        if (VIR_ALLOC(thiscall) < 0) {
8742
            virReportOOMError();
8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754
            goto cleanup;
        }

        /* We're not really doing an RPC calls, so we're
         * skipping straight to RX part */
        thiscall->mode = REMOTE_MODE_WAIT_RX;
        thiscall->serial = privst->serial;
        thiscall->proc_nr = privst->proc_nr;
        thiscall->want_reply = 1;

        if (virCondInit(&thiscall->cond) < 0) {
            VIR_FREE(thiscall);
8755 8756
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("cannot initialize mutex"));
8757 8758 8759
            goto cleanup;
        }

8760
        ret = remoteIO(st->conn, priv, 0, thiscall);
8761
        ignore_value(virCondDestroy(&thiscall->cond));
8762 8763
        VIR_FREE(thiscall);
        if (ret < 0)
8764 8765 8766
            goto cleanup;
    }

8767
    VIR_DEBUG("After IO %d", privst->incomingOffset);
8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784
    if (privst->incomingOffset) {
        int want = privst->incomingOffset;
        if (want > nbytes)
            want = nbytes;
        memcpy(data, privst->incoming, want);
        if (want < privst->incomingOffset) {
            memmove(privst->incoming, privst->incoming + want, privst->incomingOffset - want);
            privst->incomingOffset -= want;
        } else {
            VIR_FREE(privst->incoming);
            privst->incomingOffset = privst->incomingLength = 0;
        }
        rv = want;
    } else {
        rv = 0;
    }

8785 8786
    remoteStreamEventTimerUpdate(privst);

8787
    VIR_DEBUG("Done %d", rv);
8788 8789 8790 8791 8792 8793 8794 8795 8796

cleanup:
    if (rv == -1)
        remoteStreamRelease(st);
    remoteDriverUnlock(priv);

    return rv;
}

8797 8798 8799 8800 8801 8802 8803

static void
remoteStreamEventTimer(int timer ATTRIBUTE_UNUSED, void *opaque)
{
    virStreamPtr st = opaque;
    struct private_data *priv = st->conn->privateData;
    struct private_stream_data *privst = st->privateData;
8804
    int events = 0;
8805 8806

    remoteDriverLock(priv);
8807

8808 8809
    if (privst->cb &&
        (privst->cbEvents & VIR_STREAM_EVENT_READABLE) &&
8810 8811 8812 8813 8814 8815 8816
        privst->incomingOffset)
        events |= VIR_STREAM_EVENT_READABLE;
    if (privst->cb &&
        (privst->cbEvents & VIR_STREAM_EVENT_WRITABLE))
        events |= VIR_STREAM_EVENT_WRITABLE;
    VIR_DEBUG("Got Timer dispatch %d %d offset=%d", events, privst->cbEvents, privst->incomingOffset);
    if (events) {
8817 8818 8819 8820 8821 8822
        virStreamEventCallback cb = privst->cb;
        void *cbOpaque = privst->cbOpaque;
        virFreeCallback cbFree = privst->cbFree;

        privst->cbDispatch = 1;
        remoteDriverUnlock(priv);
8823
        (cb)(st, events, cbOpaque);
8824 8825 8826 8827 8828 8829
        remoteDriverLock(priv);
        privst->cbDispatch = 0;

        if (!privst->cb && cbFree)
            (cbFree)(cbOpaque);
    }
8830

8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842
    remoteDriverUnlock(priv);
}


static void
remoteStreamEventTimerFree(void *opaque)
{
    virStreamPtr st = opaque;
    virUnrefStream(st);
}


8843
static int
8844 8845 8846 8847 8848
remoteStreamEventAddCallback(virStreamPtr st,
                             int events,
                             virStreamEventCallback cb,
                             void *opaque,
                             virFreeCallback ff)
8849
{
8850 8851 8852 8853 8854 8855 8856 8857
    struct private_data *priv = st->conn->privateData;
    struct private_stream_data *privst = st->privateData;
    int ret = -1;

    remoteDriverLock(priv);

    if (privst->cb) {
        remoteError(VIR_ERR_INTERNAL_ERROR,
8858
                    "%s", _("multiple stream callbacks not supported"));
8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876
        goto cleanup;
    }

    virStreamRef(st);
    if ((privst->cbTimer =
         virEventAddTimeout(-1,
                            remoteStreamEventTimer,
                            st,
                            remoteStreamEventTimerFree)) < 0) {
        virUnrefStream(st);
        goto cleanup;
    }

    privst->cb = cb;
    privst->cbOpaque = opaque;
    privst->cbFree = ff;
    privst->cbEvents = events;

8877 8878
    remoteStreamEventTimerUpdate(privst);

8879 8880 8881 8882 8883
    ret = 0;

cleanup:
    remoteDriverUnlock(priv);
    return ret;
8884 8885 8886
}

static int
8887 8888
remoteStreamEventUpdateCallback(virStreamPtr st,
                                int events)
8889
{
8890 8891 8892 8893 8894 8895 8896 8897
    struct private_data *priv = st->conn->privateData;
    struct private_stream_data *privst = st->privateData;
    int ret = -1;

    remoteDriverLock(priv);

    if (!privst->cb) {
        remoteError(VIR_ERR_INTERNAL_ERROR,
8898
                    "%s", _("no stream callback registered"));
8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910
        goto cleanup;
    }

    privst->cbEvents = events;

    remoteStreamEventTimerUpdate(privst);

    ret = 0;

cleanup:
    remoteDriverUnlock(priv);
    return ret;
8911 8912 8913 8914
}


static int
8915
remoteStreamEventRemoveCallback(virStreamPtr st)
8916
{
8917 8918 8919 8920 8921 8922 8923 8924
    struct private_data *priv = st->conn->privateData;
    struct private_stream_data *privst = st->privateData;
    int ret = -1;

    remoteDriverLock(priv);

    if (!privst->cb) {
        remoteError(VIR_ERR_INTERNAL_ERROR,
8925
                    "%s", _("no stream callback registered"));
8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942
        goto cleanup;
    }

    if (!privst->cbDispatch &&
        privst->cbFree)
        (privst->cbFree)(privst->cbOpaque);
    privst->cb = NULL;
    privst->cbOpaque = NULL;
    privst->cbFree = NULL;
    privst->cbEvents = 0;
    virEventRemoveTimeout(privst->cbTimer);

    ret = 0;

cleanup:
    remoteDriverUnlock(priv);
    return ret;
8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003
}

static int
remoteStreamFinish(virStreamPtr st)
{
    struct private_data *priv = st->conn->privateData;
    int ret = -1;

    remoteDriverLock(priv);

    if (remoteStreamHasError(st))
        goto cleanup;

    ret = remoteStreamPacket(st,
                             REMOTE_OK,
                             NULL,
                             0);

cleanup:
    remoteStreamRelease(st);

    remoteDriverUnlock(priv);
    return ret;
}

static int
remoteStreamAbort(virStreamPtr st)
{
    struct private_data *priv = st->conn->privateData;
    int ret = -1;

    remoteDriverLock(priv);

    if (remoteStreamHasError(st))
        goto cleanup;

    ret = remoteStreamPacket(st,
                             REMOTE_ERROR,
                             NULL,
                             0);

cleanup:
    remoteStreamRelease(st);

    remoteDriverUnlock(priv);
    return ret;
}



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


C
Chris Lalancette 已提交
9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044
static int
remoteDomainMigratePrepareTunnel(virConnectPtr conn,
                                 virStreamPtr st,
                                 unsigned long flags,
                                 const char *dname,
                                 unsigned long resource,
                                 const char *dom_xml)
{
    struct private_data *priv = conn->privateData;
    struct private_stream_data *privst = NULL;
    int rv = -1;
    remote_domain_migrate_prepare_tunnel_args args;

    remoteDriverLock(priv);

    if (!(privst = remoteStreamOpen(st, 1, REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL, priv->counter)))
        goto done;

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

    args.flags = flags;
    args.dname = dname == NULL ? NULL : (char **) &dname;
    args.resource = resource;
    args.dom_xml = (char *) dom_xml;

    if (call(conn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_PREPARE_TUNNEL,
             (xdrproc_t) xdr_remote_domain_migrate_prepare_tunnel_args, (char *) &args,
             (xdrproc_t) xdr_void, NULL) == -1) {
        remoteStreamRelease(st);
        goto done;
    }

    rv = 0;

done:
    remoteDriverUnlock(priv);

    return rv;
}

J
Jiri Denemark 已提交
9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071

static int
remoteCPUCompare(virConnectPtr conn, const char *xmlDesc,
                 unsigned int flags ATTRIBUTE_UNUSED)
{
    struct private_data *priv = conn->privateData;
    remote_cpu_compare_args args;
    remote_cpu_compare_ret ret;
    int rv = VIR_CPU_COMPARE_ERROR;

    remoteDriverLock(priv);

    args.xml = (char *) xmlDesc;

    memset(&ret, 0, sizeof (ret));
    if (call(conn, priv, 0, REMOTE_PROC_CPU_COMPARE,
             (xdrproc_t) xdr_remote_cpu_compare_args, (char *) &args,
             (xdrproc_t) xdr_remote_cpu_compare_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.result;

done:
    remoteDriverUnlock(priv);
    return rv;
}

9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102

static char *
remoteCPUBaseline(virConnectPtr conn,
                  const char **xmlCPUs,
                  unsigned int ncpus,
                  unsigned int flags)
{
    struct private_data *priv = conn->privateData;
    remote_cpu_baseline_args args;
    remote_cpu_baseline_ret ret;
    char *cpu = NULL;

    remoteDriverLock(priv);

    args.xmlCPUs.xmlCPUs_len = ncpus;
    args.xmlCPUs.xmlCPUs_val = (char **) xmlCPUs;
    args.flags = flags;

    memset(&ret, 0, sizeof (ret));
    if (call(conn, priv, 0, REMOTE_PROC_CPU_BASELINE,
             (xdrproc_t) xdr_remote_cpu_baseline_args, (char *) &args,
             (xdrproc_t) xdr_remote_cpu_baseline_ret, (char *) &ret) == -1)
        goto done;

    cpu = ret.cpu;

done:
    remoteDriverUnlock(priv);
    return cpu;
}

9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142

static int
remoteDomainGetJobInfo (virDomainPtr domain, virDomainJobInfoPtr info)
{
    int rv = -1;
    remote_domain_get_job_info_args args;
    remote_domain_get_job_info_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain (&args.dom, domain);

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

    info->type = ret.type;
    info->timeElapsed = ret.timeElapsed;
    info->timeRemaining = ret.timeRemaining;
    info->dataTotal = ret.dataTotal;
    info->dataProcessed = ret.dataProcessed;
    info->dataRemaining = ret.dataRemaining;
    info->memTotal = ret.memTotal;
    info->memProcessed = ret.memProcessed;
    info->memRemaining = ret.memRemaining;
    info->fileTotal = ret.fileTotal;
    info->fileProcessed = ret.fileProcessed;
    info->fileRemaining = ret.fileRemaining;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}


9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166
static int
remoteDomainAbortJob (virDomainPtr domain)
{
    int rv = -1;
    remote_domain_abort_job_args args;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain (&args.dom, domain);

    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_ABORT_JOB,
              (xdrproc_t) xdr_remote_domain_abort_job_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}


9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195
static int
remoteDomainMigrateSetMaxDowntime(virDomainPtr domain,
                                  unsigned long long downtime,
                                  unsigned int flags)
{
    struct private_data *priv = domain->conn->privateData;
    remote_domain_migrate_set_max_downtime_args args;
    int rv = -1;

    remoteDriverLock(priv);

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

    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_SET_MAX_DOWNTIME,
             (xdrproc_t) xdr_remote_domain_migrate_set_max_downtime_args,
             (char *) &args,
             (xdrproc_t) xdr_void,
             (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224
static int
remoteDomainMigrateSetMaxSpeed(virDomainPtr domain,
                               unsigned long bandwidth,
                               unsigned int flags)
{
    struct private_data *priv = domain->conn->privateData;
    remote_domain_migrate_set_max_speed_args args;
    int rv = -1;

    remoteDriverLock(priv);

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

    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_MIGRATE_SET_MAX_SPEED,
             (xdrproc_t) xdr_remote_domain_migrate_set_max_speed_args,
             (char *) &args,
             (xdrproc_t) xdr_void,
             (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}

C
Chris Lalancette 已提交
9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323
static virDomainSnapshotPtr
remoteDomainSnapshotCreateXML(virDomainPtr domain,
                              const char *xmlDesc,
                              unsigned int flags)
{
    virDomainSnapshotPtr snapshot = NULL;
    remote_domain_snapshot_create_xml_args args;
    remote_domain_snapshot_create_xml_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain (&args.domain, domain);
    args.xml_desc = (char *) xmlDesc;
    args.flags = flags;

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

    snapshot = get_nonnull_domain_snapshot(domain, ret.snap);
    xdr_free ((xdrproc_t) &xdr_remote_domain_snapshot_create_xml_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
    return snapshot;
}


static char *
remoteDomainSnapshotDumpXML(virDomainSnapshotPtr snapshot, unsigned int flags)
{
    char *rv = NULL;
    remote_domain_snapshot_dump_xml_args args;
    remote_domain_snapshot_dump_xml_ret ret;
    struct private_data *priv = snapshot->domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain_snapshot(&args.snap, snapshot);
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
    if (call (snapshot->domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SNAPSHOT_DUMP_XML,
              (xdrproc_t) xdr_remote_domain_snapshot_dump_xml_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_snapshot_dump_xml_ret, (char *) &ret) == -1)
        goto done;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}


static int
remoteDomainSnapshotNum (virDomainPtr domain, unsigned int flags)
{
    int rv = -1;
    remote_domain_snapshot_num_args args;
    remote_domain_snapshot_num_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

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

    rv = ret.num;

done:
    remoteDriverUnlock(priv);
    return rv;
}


static int
remoteDomainSnapshotListNames (virDomainPtr domain, char **const names,
                               int nameslen, unsigned int flags)
{
    int rv = -1;
    int i;
    remote_domain_snapshot_list_names_args args;
    remote_domain_snapshot_list_names_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

    if (nameslen > REMOTE_DOMAIN_SNAPSHOT_LIST_NAMES_MAX) {
9324 9325 9326
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain snapshot names: %d > %d"),
                    nameslen, REMOTE_DOMAIN_SNAPSHOT_LIST_NAMES_MAX);
C
Chris Lalancette 已提交
9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340
        goto done;
    }

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

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

    if (ret.names.names_len > nameslen) {
9341 9342 9343
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain snapshots: %d > %d"),
                    ret.names.names_len, nameslen);
C
Chris Lalancette 已提交
9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509
        goto cleanup;
    }

    /* This call is caller-frees (although that isn't clear from
     * the documentation).  However xdr_free will free up both the
     * names and the list of pointers, so we have to strdup the
     * names here.
     */
    for (i = 0; i < ret.names.names_len; ++i) {
        names[i] = strdup (ret.names.names_val[i]);

        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

            virReportOOMError();
            goto cleanup;
        }
    }

    rv = ret.names.names_len;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}


static virDomainSnapshotPtr
remoteDomainSnapshotLookupByName (virDomainPtr domain, const char *name,
                                  unsigned int flags)
{
    virDomainSnapshotPtr snapshot = NULL;
    remote_domain_snapshot_lookup_by_name_args args;
    remote_domain_snapshot_lookup_by_name_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

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

    snapshot = get_nonnull_domain_snapshot (domain, ret.snap);
    xdr_free ((xdrproc_t) &xdr_remote_domain_snapshot_lookup_by_name_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
    return snapshot;
}


static int
remoteDomainHasCurrentSnapshot(virDomainPtr domain, unsigned int flags)
{
    int rv = -1;
    remote_domain_has_current_snapshot_args args;
    remote_domain_has_current_snapshot_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

    if (call(domain->conn, priv, 0, REMOTE_PROC_DOMAIN_HAS_CURRENT_SNAPSHOT,
             (xdrproc_t) xdr_remote_domain_has_current_snapshot_args, (char *) &args,
             (xdrproc_t) xdr_remote_domain_has_current_snapshot_ret, (char *) &ret) == -1)
        goto done;

    rv = ret.result;

done:
    remoteDriverUnlock(priv);
    return rv;
}


static virDomainSnapshotPtr
remoteDomainSnapshotCurrent(virDomainPtr domain,
                            unsigned int flags)
{
    virDomainSnapshotPtr snapshot = NULL;
    remote_domain_snapshot_current_args args;
    remote_domain_snapshot_current_ret ret;
    struct private_data *priv = domain->conn->privateData;

    remoteDriverLock(priv);

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

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

    snapshot = get_nonnull_domain_snapshot(domain, ret.snap);
    xdr_free((xdrproc_t) &xdr_remote_domain_snapshot_current_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
    return snapshot;
}


static int
remoteDomainRevertToSnapshot (virDomainSnapshotPtr snapshot,
                              unsigned int flags)
{
    int rv = -1;
    remote_domain_revert_to_snapshot_args args;
    struct private_data *priv = snapshot->domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain_snapshot(&args.snap, snapshot);
    args.flags = flags;

    if (call (snapshot->domain->conn, priv, 0, REMOTE_PROC_DOMAIN_REVERT_TO_SNAPSHOT,
              (xdrproc_t) xdr_remote_domain_revert_to_snapshot_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
}


static int
remoteDomainSnapshotDelete (virDomainSnapshotPtr snapshot,
                            unsigned int flags)
{
    int rv = -1;
    remote_domain_snapshot_delete_args args;
    struct private_data *priv = snapshot->domain->conn->privateData;

    remoteDriverLock(priv);

    make_nonnull_domain_snapshot(&args.snap, snapshot);
    args.flags = flags;

    if (call (snapshot->domain->conn, priv, 0, REMOTE_PROC_DOMAIN_SNAPSHOT_DELETE,
              (xdrproc_t) xdr_remote_domain_snapshot_delete_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto done;

    rv = 0;

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

9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525
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);

    if (priv->eventFlushTimer < 0) {
9526
         remoteError(VIR_ERR_NO_SUPPORT, "%s", _("no event support"));
9527 9528 9529 9530 9531 9532
         goto done;
    }

    if ((callbackID = virDomainEventCallbackListAddID(conn, priv->callbackList,
                                                      dom, eventID,
                                                      callback, opaque, freecb)) < 0) {
9533
         remoteError(VIR_ERR_RPC, "%s", _("adding cb to list"));
9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568
         goto done;
    }

    /* If this is the first callback for this eventID, we need to enable
     * events on the server */
    if (virDomainEventCallbackListCountID(conn, priv->callbackList, eventID) == 1) {
        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) {
            virDomainEventCallbackListRemoveID(conn, priv->callbackList, callbackID);
            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);

    if ((eventID = virDomainEventCallbackListEventID(conn, priv->callbackList, callbackID)) < 0) {
9569
        remoteError(VIR_ERR_RPC, _("unable to find callback ID %d"), callbackID);
9570 9571 9572 9573 9574 9575
        goto done;
    }

    if (priv->domainEventDispatching) {
        if (virDomainEventCallbackListMarkDeleteID(conn, priv->callbackList,
                                                   callbackID) < 0) {
9576
            remoteError(VIR_ERR_RPC, "%s", _("marking cb for deletion"));
9577 9578 9579 9580 9581
            goto done;
        }
    } else {
        if (virDomainEventCallbackListRemoveID(conn, priv->callbackList,
                                               callbackID) < 0) {
9582
            remoteError(VIR_ERR_RPC, "%s", _("removing cb from list"));
9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605
            goto done;
        }
    }

    /* If that was the last callback for this eventID, we need to disable
     * events on the server */
    if (virDomainEventCallbackListCountID(conn, priv->callbackList, eventID) == 0) {
        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;
}


9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645
static int
remoteDomainOpenConsole(virDomainPtr dom,
                        const char *devname,
                        virStreamPtr st,
                        unsigned int flags)
{
    struct private_data *priv = dom->conn->privateData;
    struct private_stream_data *privst = NULL;
    int rv = -1;
    remote_domain_open_console_args args;

    remoteDriverLock(priv);

    if (!(privst = remoteStreamOpen(st, 1, REMOTE_PROC_DOMAIN_OPEN_CONSOLE, priv->counter)))
        goto done;

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

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

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

    rv = 0;

done:
    remoteDriverUnlock(priv);

    return rv;

}


9646 9647
/*----------------------------------------------------------------------*/

C
Chris Lalancette 已提交
9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663
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);

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

    memset (&ret, 0, sizeof ret);
9664
    if (call (domain->conn, priv, REMOTE_CALL_QEMU, QEMU_PROC_MONITOR_COMMAND,
C
Chris Lalancette 已提交
9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686
              (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;
}

/*----------------------------------------------------------------------*/
9687

9688
static struct remote_thread_call *
9689
prepareCall(struct private_data *priv,
C
Chris Lalancette 已提交
9690
            int flags,
9691 9692 9693 9694
            int proc_nr,
            xdrproc_t args_filter, char *args,
            xdrproc_t ret_filter, char *ret)
{
9695
    XDR xdr;
9696 9697 9698
    struct remote_message_header hdr;
    struct remote_thread_call *rv;

9699
    if (VIR_ALLOC(rv) < 0) {
9700
        virReportOOMError();
9701
        return NULL;
9702
    }
9703 9704 9705

    if (virCondInit(&rv->cond) < 0) {
        VIR_FREE(rv);
9706 9707
        remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("cannot initialize mutex"));
9708 9709
        return NULL;
    }
9710 9711

    /* Get a unique serial number for this message. */
9712 9713 9714 9715
    rv->serial = priv->counter++;
    rv->proc_nr = proc_nr;
    rv->ret_filter = ret_filter;
    rv->ret = ret;
9716
    rv->want_reply = 1;
9717

9718
    if (flags & REMOTE_CALL_QEMU) {
C
Chris Lalancette 已提交
9719 9720 9721 9722 9723 9724 9725
        hdr.prog = QEMU_PROGRAM;
        hdr.vers = QEMU_PROTOCOL_VERSION;
    }
    else {
        hdr.prog = REMOTE_PROGRAM;
        hdr.vers = REMOTE_PROTOCOL_VERSION;
    }
9726
    hdr.proc = proc_nr;
9727
    hdr.type = REMOTE_CALL;
9728
    hdr.serial = rv->serial;
9729 9730 9731
    hdr.status = REMOTE_OK;

    /* Serialise header followed by args. */
9732
    xdrmem_create (&xdr, rv->buffer+4, REMOTE_MESSAGE_MAX, XDR_ENCODE);
9733
    if (!xdr_remote_message_header (&xdr, &hdr)) {
9734
        remoteError(VIR_ERR_RPC, "%s", _("xdr_remote_message_header failed"));
9735
        goto error;
9736 9737 9738
    }

    if (!(*args_filter) (&xdr, args)) {
9739
        remoteError(VIR_ERR_RPC, "%s", _("marshalling args"));
9740
        goto error;
9741 9742 9743
    }

    /* Get the length stored in buffer. */
9744
    rv->bufferLength = xdr_getpos (&xdr);
9745 9746 9747 9748 9749
    xdr_destroy (&xdr);

    /* Length must include the length word itself (always encoded in
     * 4 bytes as per RFC 4506).
     */
9750
    rv->bufferLength += REMOTE_MESSAGE_HEADER_XDR_LEN;
9751 9752

    /* Encode the length word. */
9753 9754
    xdrmem_create (&xdr, rv->buffer, REMOTE_MESSAGE_HEADER_XDR_LEN, XDR_ENCODE);
    if (!xdr_u_int (&xdr, &rv->bufferLength)) {
9755
        remoteError(VIR_ERR_RPC, "%s", _("xdr_u_int (length word)"));
9756
        goto error;
9757 9758 9759
    }
    xdr_destroy (&xdr);

9760 9761 9762 9763
    return rv;

error:
    xdr_destroy (&xdr);
9764
    ignore_value(virCondDestroy(&rv->cond));
9765 9766 9767 9768 9769 9770 9771
    VIR_FREE(rv);
    return NULL;
}



static int
9772
remoteIOWriteBuffer(struct private_data *priv,
9773
                    const char *bytes, int len)
9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785
{
    int ret;

    if (priv->uses_tls) {
    tls_resend:
        ret = gnutls_record_send (priv->session, bytes, len);
        if (ret < 0) {
            if (ret == GNUTLS_E_INTERRUPTED)
                goto tls_resend;
            if (ret == GNUTLS_E_AGAIN)
                return 0;

9786
            remoteError(VIR_ERR_GNUTLS_ERROR, "%s", gnutls_strerror (ret));
9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797
            return -1;
        }
    } else {
    resend:
        ret = send (priv->sock, bytes, len, 0);
        if (ret == -1) {
            if (errno == EINTR)
                goto resend;
            if (errno == EWOULDBLOCK)
                return 0;

9798
            virReportSystemError(errno, "%s", _("cannot send data"));
9799 9800 9801 9802 9803 9804 9805 9806 9807 9808
            return -1;

        }
    }

    return ret;
}


static int
9809
remoteIOReadBuffer(struct private_data *priv,
9810
                   char *bytes, int len)
9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824
{
    int ret;

    if (priv->uses_tls) {
    tls_resend:
        ret = gnutls_record_recv (priv->session, bytes, len);
        if (ret == GNUTLS_E_INTERRUPTED)
            goto tls_resend;
        if (ret == GNUTLS_E_AGAIN)
            return 0;

        /* Treat 0 == EOF as an error */
        if (ret <= 0) {
            if (ret < 0)
9825 9826 9827
                remoteError(VIR_ERR_GNUTLS_ERROR,
                            _("failed to read from TLS socket %s"),
                            gnutls_strerror (ret));
9828
            else
9829 9830
                remoteError(VIR_ERR_SYSTEM_ERROR, "%s",
                            _("server closed connection"));
9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842
            return -1;
        }
    } else {
    resend:
        ret = recv (priv->sock, bytes, len, 0);
        if (ret <= 0) {
            if (ret == -1) {
                if (errno == EINTR)
                    goto resend;
                if (errno == EWOULDBLOCK)
                    return 0;

9843 9844
                char errout[1024] = "\0";
                if (priv->errfd != -1) {
9845 9846 9847 9848 9849
                    if (saferead(priv->errfd, errout, sizeof(errout)) < 0) {
                        virReportSystemError(errno, "%s",
                                             _("cannot recv data"));
                        return -1;
                    }
9850 9851
                }

9852
                virReportSystemError(errno,
9853 9854
                                     _("cannot recv data: %s"), errout);

9855
            } else {
9856 9857
                char errout[1024] = "\0";
                if (priv->errfd != -1) {
9858 9859 9860 9861 9862 9863
                    if (saferead(priv->errfd, errout, sizeof(errout)) < 0) {
                        remoteError(VIR_ERR_SYSTEM_ERROR,
                                    _("server closed connection: %s"),
                                    virStrerror(errno, errout, sizeof errout));
                        return -1;
                    }
9864 9865
                }

9866 9867
                remoteError(VIR_ERR_SYSTEM_ERROR,
                            _("server closed connection: %s"), errout);
9868 9869 9870 9871 9872 9873 9874 9875 9876 9877
            }
            return -1;
        }
    }

    return ret;
}


static int
9878
remoteIOWriteMessage(struct private_data *priv,
9879
                     struct remote_thread_call *thecall)
9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892
{
#if HAVE_SASL
    if (priv->saslconn) {
        const char *output;
        unsigned int outputlen;
        int err, ret;

        if (!priv->saslEncoded) {
            err = sasl_encode(priv->saslconn,
                              thecall->buffer + thecall->bufferOffset,
                              thecall->bufferLength - thecall->bufferOffset,
                              &output, &outputlen);
            if (err != SASL_OK) {
9893 9894 9895
                remoteError(VIR_ERR_INTERNAL_ERROR,
                            _("failed to encode SASL data: %s"),
                            sasl_errstring(err, NULL, NULL));
9896 9897 9898 9899 9900 9901 9902 9903 9904
                return -1;
            }
            priv->saslEncoded = output;
            priv->saslEncodedLength = outputlen;
            priv->saslEncodedOffset = 0;

            thecall->bufferOffset = thecall->bufferLength;
        }

9905
        ret = remoteIOWriteBuffer(priv,
9906 9907
                                  priv->saslEncoded + priv->saslEncodedOffset,
                                  priv->saslEncodedLength - priv->saslEncodedOffset);
9908 9909 9910 9911 9912 9913 9914
        if (ret < 0)
            return ret;
        priv->saslEncodedOffset += ret;

        if (priv->saslEncodedOffset == priv->saslEncodedLength) {
            priv->saslEncoded = NULL;
            priv->saslEncodedOffset = priv->saslEncodedLength = 0;
9915 9916 9917 9918
            if (thecall->want_reply)
                thecall->mode = REMOTE_MODE_WAIT_RX;
            else
                thecall->mode = REMOTE_MODE_COMPLETE;
9919 9920 9921 9922
        }
    } else {
#endif
        int ret;
9923
        ret = remoteIOWriteBuffer(priv,
9924 9925
                                  thecall->buffer + thecall->bufferOffset,
                                  thecall->bufferLength - thecall->bufferOffset);
9926 9927 9928 9929 9930 9931
        if (ret < 0)
            return ret;
        thecall->bufferOffset += ret;

        if (thecall->bufferOffset == thecall->bufferLength) {
            thecall->bufferOffset = thecall->bufferLength = 0;
9932 9933 9934 9935
            if (thecall->want_reply)
                thecall->mode = REMOTE_MODE_WAIT_RX;
            else
                thecall->mode = REMOTE_MODE_COMPLETE;
9936 9937 9938 9939 9940 9941 9942 9943 9944
        }
#if HAVE_SASL
    }
#endif
    return 0;
}


static int
9945
remoteIOHandleOutput(struct private_data *priv) {
9946 9947 9948 9949 9950 9951 9952 9953 9954 9955
    struct remote_thread_call *thecall = priv->waitDispatch;

    while (thecall &&
           thecall->mode != REMOTE_MODE_WAIT_TX)
        thecall = thecall->next;

    if (!thecall)
        return -1; /* Shouldn't happen, but you never know... */

    while (thecall) {
9956
        int ret = remoteIOWriteMessage(priv, thecall);
9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969
        if (ret < 0)
            return ret;

        if (thecall->mode == REMOTE_MODE_WAIT_TX)
            return 0; /* Blocking write, to back to event loop */

        thecall = thecall->next;
    }

    return 0; /* No more calls to send, all done */
}

static int
9970
remoteIOReadMessage(struct private_data *priv) {
9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983
    unsigned int wantData;

    /* Start by reading length word */
    if (priv->bufferLength == 0)
        priv->bufferLength = 4;

    wantData = priv->bufferLength - priv->bufferOffset;

#if HAVE_SASL
    if (priv->saslconn) {
        if (priv->saslDecoded == NULL) {
            char encoded[8192];
            int ret, err;
9984
            ret = remoteIOReadBuffer(priv, encoded, sizeof(encoded));
9985 9986 9987 9988 9989 9990 9991 9992
            if (ret < 0)
                return -1;
            if (ret == 0)
                return 0;

            err = sasl_decode(priv->saslconn, encoded, ret,
                              &priv->saslDecoded, &priv->saslDecodedLength);
            if (err != SASL_OK) {
9993 9994 9995
                remoteError(VIR_ERR_INTERNAL_ERROR,
                            _("failed to decode SASL data: %s"),
                            sasl_errstring(err, NULL, NULL));
9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009
                return -1;
            }
            priv->saslDecodedOffset = 0;
        }

        if ((priv->saslDecodedLength - priv->saslDecodedOffset) < wantData)
            wantData = (priv->saslDecodedLength - priv->saslDecodedOffset);

        memcpy(priv->buffer + priv->bufferOffset,
               priv->saslDecoded + priv->saslDecodedOffset,
               wantData);
        priv->saslDecodedOffset += wantData;
        priv->bufferOffset += wantData;
        if (priv->saslDecodedOffset == priv->saslDecodedLength) {
10010
            priv->saslDecodedOffset = priv->saslDecodedLength = 0;
10011 10012 10013 10014 10015 10016 10017 10018
            priv->saslDecoded = NULL;
        }

        return wantData;
    } else {
#endif
        int ret;

10019
        ret = remoteIOReadBuffer(priv,
10020 10021
                                 priv->buffer + priv->bufferOffset,
                                 wantData);
10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033
        if (ret < 0)
            return -1;
        if (ret == 0)
            return 0;

        priv->bufferOffset += ret;

        return ret;
#if HAVE_SASL
    }
#endif
}
10034 10035


10036
static int
10037
remoteIODecodeMessageLength(struct private_data *priv) {
10038
    XDR xdr;
10039
    unsigned int len;
10040 10041

    xdrmem_create (&xdr, priv->buffer, priv->bufferLength, XDR_DECODE);
10042
    if (!xdr_u_int (&xdr, &len)) {
10043
        remoteError(VIR_ERR_RPC, "%s", _("xdr_u_int (length word, reply)"));
10044 10045 10046 10047
        return -1;
    }
    xdr_destroy (&xdr);

10048
    if (len < REMOTE_MESSAGE_HEADER_XDR_LEN) {
10049 10050
        remoteError(VIR_ERR_RPC, "%s",
                    _("packet received from server too small"));
10051 10052 10053
        return -1;
    }

10054
    /* Length includes length word - adjust to real length to read. */
10055
    len -= REMOTE_MESSAGE_HEADER_XDR_LEN;
10056

10057
    if (len > REMOTE_MESSAGE_MAX) {
10058 10059
        remoteError(VIR_ERR_RPC, "%s",
                    _("packet received from server too large"));
10060 10061 10062
        return -1;
    }

10063 10064 10065
    /* Extend our declared buffer length and carry
       on reading the header + payload */
    priv->bufferLength += len;
10066
    VIR_DEBUG("Got length, now need %d total (%d more)", priv->bufferLength, len);
10067 10068 10069 10070 10071
    return 0;
}


static int
10072 10073 10074 10075 10076 10077 10078 10079 10080 10081
processCallDispatchReply(virConnectPtr conn, struct private_data *priv,
                         remote_message_header *hdr,
                         XDR *xdr);

static int
processCallDispatchMessage(virConnectPtr conn, struct private_data *priv,
                           int in_open,
                           remote_message_header *hdr,
                           XDR *xdr);

10082 10083 10084 10085 10086
static int
processCallDispatchStream(virConnectPtr conn, struct private_data *priv,
                          remote_message_header *hdr,
                          XDR *xdr);

10087 10088 10089

static int
processCallDispatch(virConnectPtr conn, struct private_data *priv,
C
Chris Lalancette 已提交
10090
                    int flags) {
10091 10092 10093
    XDR xdr;
    struct remote_message_header hdr;
    int len = priv->bufferLength - 4;
10094
    int rv = -1;
C
Chris Lalancette 已提交
10095 10096
    int expectedprog;
    int expectedvers;
10097

10098 10099 10100
    /* Length word has already been read */
    priv->bufferOffset = 4;

10101
    /* Deserialise reply header. */
10102
    xdrmem_create (&xdr, priv->buffer + priv->bufferOffset, len, XDR_DECODE);
10103
    if (!xdr_remote_message_header (&xdr, &hdr)) {
10104
        remoteError(VIR_ERR_RPC, "%s", _("invalid header in reply"));
10105 10106 10107
        return -1;
    }

10108 10109
    priv->bufferOffset += xdr_getpos(&xdr);

C
Chris Lalancette 已提交
10110 10111
    expectedprog = REMOTE_PROGRAM;
    expectedvers = REMOTE_PROTOCOL_VERSION;
10112
    if (flags & REMOTE_CALL_QEMU) {
C
Chris Lalancette 已提交
10113 10114 10115 10116
        expectedprog = QEMU_PROGRAM;
        expectedvers = QEMU_PROTOCOL_VERSION;
    }

10117
    /* Check program, version, etc. are what we expect. */
C
Chris Lalancette 已提交
10118
    if (hdr.prog != expectedprog) {
10119 10120
        remoteError(VIR_ERR_RPC,
                    _("unknown program (received %x, expected %x)"),
C
Chris Lalancette 已提交
10121
                    hdr.prog, expectedprog);
10122 10123
        return -1;
    }
C
Chris Lalancette 已提交
10124
    if (hdr.vers != expectedvers) {
10125 10126
        remoteError(VIR_ERR_RPC,
                    _("unknown protocol version (received %x, expected %x)"),
C
Chris Lalancette 已提交
10127
                    hdr.vers, expectedvers);
10128 10129 10130
        return -1;
    }

10131

10132 10133
    switch (hdr.type) {
    case REMOTE_REPLY: /* Normal RPC replies */
C
Chris Lalancette 已提交
10134
        rv = processCallDispatchReply(conn, priv, &hdr, &xdr);
10135
        break;
10136

10137
    case REMOTE_MESSAGE: /* Async notifications */
10138
        VIR_DEBUG("Dispatch event %d %d", hdr.proc, priv->bufferLength);
C
Chris Lalancette 已提交
10139
        rv = processCallDispatchMessage(conn, priv, flags & REMOTE_CALL_IN_OPEN,
10140 10141 10142
                                        &hdr, &xdr);
        break;

10143
    case REMOTE_STREAM: /* Stream protocol */
C
Chris Lalancette 已提交
10144
        rv = processCallDispatchStream(conn, priv, &hdr, &xdr);
10145 10146
        break;

10147
    default:
10148 10149 10150
        remoteError(VIR_ERR_RPC,
                    _("got unexpected RPC call %d from server"),
                    hdr.proc);
10151 10152
        rv = -1;
        break;
10153
    }
10154

10155 10156 10157 10158 10159 10160
    xdr_destroy(&xdr);
    return rv;
}


static int
10161 10162
processCallDispatchReply(virConnectPtr conn ATTRIBUTE_UNUSED,
                         struct private_data *priv,
10163 10164 10165 10166
                         remote_message_header *hdr,
                         XDR *xdr) {
    struct remote_thread_call *thecall;

10167 10168 10169 10170
    /* Ok, definitely got an RPC reply now find
       out who's been waiting for it */
    thecall = priv->waitDispatch;
    while (thecall &&
10171
           thecall->serial != hdr->serial)
10172 10173 10174
        thecall = thecall->next;

    if (!thecall) {
10175 10176 10177
        remoteError(VIR_ERR_RPC,
                    _("no call waiting for reply with serial %d"),
                    hdr->serial);
10178 10179
        return -1;
    }
10180

10181
    if (hdr->proc != thecall->proc_nr) {
10182 10183 10184
        remoteError(VIR_ERR_RPC,
                    _("unknown procedure (received %x, expected %x)"),
                    hdr->proc, thecall->proc_nr);
10185 10186 10187 10188 10189 10190 10191
        return -1;
    }

    /* Status is either REMOTE_OK (meaning that what follows is a ret
     * structure), or REMOTE_ERROR (and what follows is a remote_error
     * structure).
     */
10192
    switch (hdr->status) {
10193
    case REMOTE_OK:
10194
        if (!(*thecall->ret_filter) (xdr, thecall->ret)) {
10195
            remoteError(VIR_ERR_RPC, "%s", _("unmarshalling ret"));
10196 10197
            return -1;
        }
10198
        thecall->mode = REMOTE_MODE_COMPLETE;
10199 10200 10201
        return 0;

    case REMOTE_ERROR:
10202
        memset (&thecall->err, 0, sizeof thecall->err);
10203
        if (!xdr_remote_error (xdr, &thecall->err)) {
10204
            remoteError(VIR_ERR_RPC, "%s", _("unmarshalling remote_error"));
10205 10206
            return -1;
        }
10207 10208
        thecall->mode = REMOTE_MODE_ERROR;
        return 0;
10209 10210

    default:
10211
        remoteError(VIR_ERR_RPC, _("unknown status (received %x)"), hdr->status);
10212 10213 10214 10215
        return -1;
    }
}

10216 10217 10218 10219 10220
static int
processCallDispatchMessage(virConnectPtr conn, struct private_data *priv,
                           int in_open,
                           remote_message_header *hdr,
                           XDR *xdr) {
10221
    virDomainEventPtr event = NULL;
10222 10223 10224 10225 10226
    /* An async message has come in while we were waiting for the
     * response. Process it to pull it off the wire, and try again
     */

    if (in_open) {
10227
        VIR_DEBUG("Ignoring bogus event %d received while in open", hdr->proc);
10228 10229 10230
        return -1;
    }

10231
    switch (hdr->proc) {
10232
    case REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE:
10233 10234 10235
        event = remoteDomainReadEventLifecycle(conn, xdr);
        break;

10236 10237 10238 10239
    case REMOTE_PROC_DOMAIN_EVENT_REBOOT:
        event = remoteDomainReadEventReboot(conn, xdr);
        break;

10240 10241 10242 10243
    case REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE:
        event = remoteDomainReadEventRTCChange(conn, xdr);
        break;

10244 10245 10246 10247
    case REMOTE_PROC_DOMAIN_EVENT_WATCHDOG:
        event = remoteDomainReadEventWatchdog(conn, xdr);
        break;

10248 10249 10250 10251
    case REMOTE_PROC_DOMAIN_EVENT_IO_ERROR:
        event = remoteDomainReadEventIOError(conn, xdr);
        break;

10252 10253 10254 10255
    case REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON:
        event = remoteDomainReadEventIOErrorReason(conn, xdr);
        break;

10256 10257 10258 10259
    case REMOTE_PROC_DOMAIN_EVENT_GRAPHICS:
        event = remoteDomainReadEventGraphics(conn, xdr);
        break;

10260
    default:
10261
        VIR_DEBUG("Unexpected event proc %d", hdr->proc);
10262
        break;
10263
    }
10264
    VIR_DEBUG("Event ready for queue %p %p", event, conn);
10265 10266 10267 10268 10269 10270

    if (!event)
        return -1;

    if (virDomainEventQueuePush(priv->domainEvents,
                                event) < 0) {
10271
        VIR_DEBUG0("Error adding event to queue");
10272 10273 10274 10275
        virDomainEventFree(event);
    }
    virEventUpdateTimeout(priv->eventFlushTimer, 0);

10276 10277 10278
    return 0;
}

10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294
static int
processCallDispatchStream(virConnectPtr conn ATTRIBUTE_UNUSED,
                          struct private_data *priv,
                          remote_message_header *hdr,
                          XDR *xdr) {
    struct private_stream_data *privst;
    struct remote_thread_call *thecall;

    /* Try and find a matching stream */
    privst = priv->streams;
    while (privst &&
           privst->serial != hdr->serial &&
           privst->proc_nr != hdr->proc)
        privst = privst->next;

    if (!privst) {
10295 10296
        VIR_DEBUG("No registered stream matching serial=%d, proc=%d",
                  hdr->serial, hdr->proc);
10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314
        return -1;
    }

    /* See if there's also a (optional) call waiting for this reply */
    thecall = priv->waitDispatch;
    while (thecall &&
           thecall->serial != hdr->serial)
        thecall = thecall->next;


    /* Status is either REMOTE_OK (meaning that what follows is a ret
     * structure), or REMOTE_ERROR (and what follows is a remote_error
     * structure).
     */
    switch (hdr->status) {
    case REMOTE_CONTINUE: {
        int avail = privst->incomingLength - privst->incomingOffset;
        int need = priv->bufferLength - priv->bufferOffset;
10315
        VIR_DEBUG0("Got a stream data packet");
10316 10317 10318 10319 10320 10321 10322

        /* XXX flag stream as complete somwhere if need==0 */

        if (need > avail) {
            int extra = need - avail;
            if (VIR_REALLOC_N(privst->incoming,
                              privst->incomingLength + extra) < 0) {
10323
                VIR_DEBUG0("Out of memory handling stream data");
10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334
                return -1;
            }
            privst->incomingLength += extra;
        }

        memcpy(privst->incoming + privst->incomingOffset,
               priv->buffer + priv->bufferOffset,
               priv->bufferLength - priv->bufferOffset);
        privst->incomingOffset += (priv->bufferLength - priv->bufferOffset);

        if (thecall && thecall->want_reply) {
10335
            VIR_DEBUG("Got sync data packet offset=%d", privst->incomingOffset);
10336 10337
            thecall->mode = REMOTE_MODE_COMPLETE;
        } else {
10338
            VIR_DEBUG("Got aysnc data packet offset=%d", privst->incomingOffset);
10339
            remoteStreamEventTimerUpdate(privst);
10340 10341 10342 10343 10344
        }
        return 0;
    }

    case REMOTE_OK:
10345
        VIR_DEBUG0("Got a synchronous confirm");
10346
        if (!thecall) {
10347
            VIR_DEBUG0("Got unexpected stream finish confirmation");
10348 10349 10350 10351 10352 10353 10354
            return -1;
        }
        thecall->mode = REMOTE_MODE_COMPLETE;
        return 0;

    case REMOTE_ERROR:
        if (thecall && thecall->want_reply) {
10355
            VIR_DEBUG0("Got a synchronous error");
10356 10357 10358
            /* Give the error straight to this call */
            memset (&thecall->err, 0, sizeof thecall->err);
            if (!xdr_remote_error (xdr, &thecall->err)) {
10359
                remoteError(VIR_ERR_RPC, "%s", _("unmarshalling remote_error"));
10360 10361 10362 10363
                return -1;
            }
            thecall->mode = REMOTE_MODE_ERROR;
        } else {
10364
            VIR_DEBUG0("Got a asynchronous error");
10365 10366
            /* No call, so queue the error against the stream */
            if (privst->has_error) {
10367
                VIR_DEBUG0("Got unexpected duplicate stream error");
10368 10369 10370 10371 10372
                return -1;
            }
            privst->has_error = 1;
            memset (&privst->err, 0, sizeof privst->err);
            if (!xdr_remote_error (xdr, &privst->err)) {
10373
                VIR_DEBUG0("Failed to unmarshall error");
10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384
                return -1;
            }
        }
        return 0;

    default:
        VIR_WARN("Stream with unexpected serial=%d, proc=%d, status=%d",
                 hdr->serial, hdr->proc, hdr->status);
        return -1;
    }
}
10385 10386

static int
10387
remoteIOHandleInput(virConnectPtr conn, struct private_data *priv,
C
Chris Lalancette 已提交
10388
                    int flags)
10389
{
10390
    /* Read as much data as is available, until we get
10391
     * EAGAIN
10392
     */
10393
    for (;;) {
10394
        int ret = remoteIOReadMessage(priv);
10395

10396 10397 10398 10399
        if (ret < 0)
            return -1;
        if (ret == 0)
            return 0;  /* Blocking on read */
10400

10401 10402 10403
        /* Check for completion of our goal */
        if (priv->bufferOffset == priv->bufferLength) {
            if (priv->bufferOffset == 4) {
10404
                ret = remoteIODecodeMessageLength(priv);
10405 10406 10407 10408 10409 10410 10411 10412 10413
                if (ret < 0)
                    return -1;

                /*
                 * We'll carry on around the loop to immediately
                 * process the message body, because it has probably
                 * already arrived. Worst case, we'll get EAGAIN on
                 * next iteration.
                 */
10414
            } else {
C
Chris Lalancette 已提交
10415
                ret = processCallDispatch(conn, priv, flags);
10416
                priv->bufferOffset = priv->bufferLength = 0;
10417
                /*
10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431
                 * We've completed one call, but we don't want to
                 * spin around the loop forever if there are many
                 * incoming async events, or replies for other
                 * thread's RPC calls. We want to get out & let
                 * any other thread take over as soon as we've
                 * got our reply. When SASL is active though, we
                 * may have read more data off the wire than we
                 * initially wanted & cached it in memory. In this
                 * case, poll() would not detect that there is more
                 * ready todo.
                 *
                 * So if SASL is active *and* some SASL data is
                 * already cached, then we'll process that now,
                 * before returning.
10432
                 */
10433 10434 10435 10436 10437 10438
#if HAVE_SASL
                if (ret == 0 &&
                    priv->saslconn &&
                    priv->saslDecoded)
                    continue;
#endif
10439
                return ret;
10440 10441 10442
            }
        }
    }
10443 10444
}

10445 10446 10447 10448 10449
/*
 * Process all calls pending dispatch/receive until we
 * get a reply to our own call. Then quit and pass the buck
 * to someone else.
 */
10450
static int
10451 10452
remoteIOEventLoop(virConnectPtr conn,
                  struct private_data *priv,
C
Chris Lalancette 已提交
10453
                  int flags,
10454
                  struct remote_thread_call *thiscall)
10455
{
10456 10457
    struct pollfd fds[2];
    int ret;
10458

10459 10460 10461 10462 10463 10464 10465
    fds[0].fd = priv->sock;
    fds[1].fd = priv->wakeupReadFD;

    for (;;) {
        struct remote_thread_call *tmp = priv->waitDispatch;
        struct remote_thread_call *prev;
        char ignore;
10466
#ifdef HAVE_PTHREAD_SIGMASK
10467
        sigset_t oldmask, blockedsigs;
10468
#endif
10469 10470 10471 10472 10473 10474
        int timeout = -1;

        /* If we have existing SASL decoded data we
         * don't want to sleep in the poll(), just
         * check if any other FDs are also ready
         */
10475
#if HAVE_SASL
10476 10477
        if (priv->saslDecoded)
            timeout = 0;
10478
#endif
10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492

        fds[0].events = fds[0].revents = 0;
        fds[1].events = fds[1].revents = 0;

        fds[1].events = POLLIN;
        while (tmp) {
            if (tmp->mode == REMOTE_MODE_WAIT_RX)
                fds[0].events |= POLLIN;
            if (tmp->mode == REMOTE_MODE_WAIT_TX)
                fds[0].events |= POLLOUT;

            tmp = tmp->next;
        }

10493 10494 10495
        if (priv->streams)
            fds[0].events |= POLLIN;

10496 10497 10498 10499
        /* Release lock while poll'ing so other threads
         * can stuff themselves on the queue */
        remoteDriverUnlock(priv);

10500 10501 10502 10503 10504
        /* Block SIGWINCH from interrupting poll in curses programs,
         * then restore the original signal mask again immediately
         * after the call (RHBZ#567931).  Same for SIGCHLD and SIGPIPE
         * at the suggestion of Paolo Bonzini and Daniel Berrange.
         */
10505
#ifdef HAVE_PTHREAD_SIGMASK
10506 10507 10508 10509
        sigemptyset (&blockedsigs);
        sigaddset (&blockedsigs, SIGWINCH);
        sigaddset (&blockedsigs, SIGCHLD);
        sigaddset (&blockedsigs, SIGPIPE);
10510
        ignore_value(pthread_sigmask(SIG_BLOCK, &blockedsigs, &oldmask));
10511
#endif
10512

10513
    repoll:
10514
        ret = poll(fds, ARRAY_CARDINALITY(fds), timeout);
10515
        if (ret < 0 && errno == EAGAIN)
10516
            goto repoll;
10517

10518
#ifdef HAVE_PTHREAD_SIGMASK
10519
        ignore_value(pthread_sigmask(SIG_SETMASK, &oldmask, NULL));
10520
#endif
10521

10522 10523
        remoteDriverLock(priv);

10524 10525 10526
        /* If we have existing SASL decoded data, pretend
         * the socket became readable so we consume it
         */
10527
#if HAVE_SASL
10528 10529
        if (priv->saslDecoded)
            fds[0].revents |= POLLIN;
10530
#endif
10531

10532
        if (fds[1].revents) {
10533
            ssize_t s;
10534
            VIR_DEBUG0("Woken up from poll by other thread");
10535 10536 10537 10538 10539 10540 10541 10542 10543 10544
            s = saferead(priv->wakeupReadFD, &ignore, sizeof(ignore));
            if (s < 0) {
                virReportSystemError(errno, "%s",
                                     _("read on wakeup fd failed"));
                goto error;
            } else if (s != sizeof(ignore)) {
                remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("read on wakeup fd failed"));
                goto error;
            }
10545 10546 10547 10548 10549
        }

        if (ret < 0) {
            if (errno == EWOULDBLOCK)
                continue;
10550
            virReportSystemError(errno,
10551
                                 "%s", _("poll on socket failed"));
10552
            goto error;
10553 10554 10555
        }

        if (fds[0].revents & POLLOUT) {
10556
            if (remoteIOHandleOutput(priv) < 0)
10557
                goto error;
10558
        }
10559 10560

        if (fds[0].revents & POLLIN) {
C
Chris Lalancette 已提交
10561
            if (remoteIOHandleInput(conn, priv, flags) < 0)
10562
                goto error;
10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584
        }

        /* Iterate through waiting threads and if
         * any are complete then tell 'em to wakeup
         */
        tmp = priv->waitDispatch;
        prev = NULL;
        while (tmp) {
            if (tmp != thiscall &&
                (tmp->mode == REMOTE_MODE_COMPLETE ||
                 tmp->mode == REMOTE_MODE_ERROR)) {
                /* Take them out of the list */
                if (prev)
                    prev->next = tmp->next;
                else
                    priv->waitDispatch = tmp->next;

                /* And wake them up....
                 * ...they won't actually wakeup until
                 * we release our mutex a short while
                 * later...
                 */
10585
                VIR_DEBUG("Waking up sleep %d %p %p", tmp->proc_nr, tmp, priv->waitDispatch);
10586
                virCondSignal(&tmp->cond);
10587
            }
10588 10589
            prev = tmp;
            tmp = tmp->next;
10590 10591
        }

10592 10593 10594 10595 10596 10597 10598
        /* Now see if *we* are done */
        if (thiscall->mode == REMOTE_MODE_COMPLETE ||
            thiscall->mode == REMOTE_MODE_ERROR) {
            /* We're at head of the list already, so
             * remove us
             */
            priv->waitDispatch = thiscall->next;
10599
            VIR_DEBUG("Giving up the buck %d %p %p", thiscall->proc_nr, thiscall, priv->waitDispatch);
10600 10601 10602
            /* See if someone else is still waiting
             * and if so, then pass the buck ! */
            if (priv->waitDispatch) {
10603
                VIR_DEBUG("Passing the buck to %d %p", priv->waitDispatch->proc_nr, priv->waitDispatch);
10604 10605 10606 10607
                virCondSignal(&priv->waitDispatch->cond);
            }
            return 0;
        }
10608

10609 10610

        if (fds[0].revents & (POLLHUP | POLLERR)) {
10611 10612
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("received hangup / error event on socket"));
10613
            goto error;
10614 10615
        }
    }
10616 10617 10618 10619


error:
    priv->waitDispatch = thiscall->next;
10620
    VIR_DEBUG("Giving up the buck due to I/O error %d %p %p", thiscall->proc_nr, thiscall, priv->waitDispatch);
10621 10622 10623
    /* See if someone else is still waiting
     * and if so, then pass the buck ! */
    if (priv->waitDispatch) {
10624
        VIR_DEBUG("Passing the buck to %d %p", priv->waitDispatch->proc_nr, priv->waitDispatch);
10625 10626 10627
        virCondSignal(&priv->waitDispatch->cond);
    }
    return -1;
10628 10629
}

10630
/*
10631
 * This function sends a message to remote server and awaits a reply
10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645
 *
 * NB. This does not free the args structure (not desirable, since you
 * often want this allocated on the stack or else it contains strings
 * which come from the user).  It does however free any intermediate
 * results, eg. the error structure if there is one.
 *
 * NB(2). Make sure to memset (&ret, 0, sizeof ret) before calling,
 * else Bad Things will happen in the XDR code.
 *
 * NB(3) You must have the private_data lock before calling this
 *
 * NB(4) This is very complicated. Due to connection cloning, multiple
 * threads can want to use the socket at once. Obviously only one of
 * them can. So if someone's using the socket, other threads are put
10646
 * to sleep on condition variables. The existing thread may completely
10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662
 * send & receive their RPC call/reply while they're asleep. Or it
 * may only get around to dealing with sending the call. Or it may
 * get around to neither. So upon waking up from slumber, the other
 * thread may or may not have more work todo.
 *
 * We call this dance  'passing the buck'
 *
 *      http://en.wikipedia.org/wiki/Passing_the_buck
 *
 *   "Buck passing or passing the buck is the action of transferring
 *    responsibility or blame unto another person. It is also used as
 *    a strategy in power politics when the actions of one country/
 *    nation are blamed on another, providing an opportunity for war."
 *
 * NB(5) Don't Panic!
 */
10663
static int
10664 10665 10666 10667
remoteIO(virConnectPtr conn,
         struct private_data *priv,
         int flags,
         struct remote_thread_call *thiscall)
10668
{
10669 10670
    int rv;

10671
    VIR_DEBUG("Do proc=%d serial=%d length=%d wait=%p",
10672 10673
          thiscall->proc_nr, thiscall->serial,
          thiscall->bufferLength, priv->waitDispatch);
10674

10675 10676 10677 10678 10679
    /* Check to see if another thread is dispatching */
    if (priv->waitDispatch) {
        /* Stick ourselves on the end of the wait queue */
        struct remote_thread_call *tmp = priv->waitDispatch;
        char ignore = 1;
10680
        ssize_t s;
10681 10682 10683 10684 10685 10686
        while (tmp && tmp->next)
            tmp = tmp->next;
        if (tmp)
            tmp->next = thiscall;
        else
            priv->waitDispatch = thiscall;
10687

10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700
        /* Force other thread to wakeup from poll */
        s = safewrite(priv->wakeupSendFD, &ignore, sizeof(ignore));
        if (s < 0) {
            char errout[1024];
            remoteError(VIR_ERR_INTERNAL_ERROR,
                        _("failed to wake up polling thread: %s"),
                        virStrerror(errno, errout, sizeof errout));
            return -1;
        } else if (s != sizeof(ignore)) {
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("failed to wake up polling thread"));
            return -1;
        }
10701

10702
        VIR_DEBUG("Going to sleep %d %p %p", thiscall->proc_nr, priv->waitDispatch, thiscall);
10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715
        /* Go to sleep while other thread is working... */
        if (virCondWait(&thiscall->cond, &priv->lock) < 0) {
            if (priv->waitDispatch == thiscall) {
                priv->waitDispatch = thiscall->next;
            } else {
                tmp = priv->waitDispatch;
                while (tmp && tmp->next &&
                       tmp->next != thiscall) {
                    tmp = tmp->next;
                }
                if (tmp && tmp->next == thiscall)
                    tmp->next = thiscall->next;
            }
10716 10717
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("failed to wait on condition"));
10718
            return -1;
10719
        }
10720

10721
        VIR_DEBUG("Wokeup from sleep %d %p %p", thiscall->proc_nr, priv->waitDispatch, thiscall);
10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735
        /* Two reasons we can be woken up
         *  1. Other thread has got our reply ready for us
         *  2. Other thread is all done, and it is our turn to
         *     be the dispatcher to finish waiting for
         *     our reply
         */
        if (thiscall->mode == REMOTE_MODE_COMPLETE ||
            thiscall->mode == REMOTE_MODE_ERROR) {
            /*
             * We avoided catching the buck and our reply is ready !
             * We've already had 'thiscall' removed from the list
             * so just need to (maybe) handle errors & free it
             */
            goto cleanup;
10736
        }
10737 10738 10739

        /* Grr, someone passed the buck onto us ... */

10740
    } else {
10741 10742 10743 10744
        /* We're first to catch the buck */
        priv->waitDispatch = thiscall;
    }

10745
    VIR_DEBUG("We have the buck %d %p %p", thiscall->proc_nr, priv->waitDispatch, thiscall);
10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762
    /*
     * The buck stops here!
     *
     * At this point we're about to own the dispatch
     * process...
     */

    /*
     * Avoid needless wake-ups of the event loop in the
     * case where this call is being made from a different
     * thread than the event loop. These wake-ups would
     * cause the event loop thread to be blocked on the
     * mutex for the duration of the call
     */
    if (priv->watch >= 0)
        virEventUpdateHandle(priv->watch, 0);

C
Chris Lalancette 已提交
10763
    rv = remoteIOEventLoop(conn, priv, flags, thiscall);
10764 10765 10766 10767

    if (priv->watch >= 0)
        virEventUpdateHandle(priv->watch, VIR_EVENT_HANDLE_READABLE);

10768
    if (rv < 0)
10769 10770 10771
        return -1;

cleanup:
10772
    VIR_DEBUG("All done with our call %d %p %p", thiscall->proc_nr,
10773
          priv->waitDispatch, thiscall);
10774
    if (thiscall->mode == REMOTE_MODE_ERROR) {
10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803
        /* Interop for virErrorNumber glitch in 0.8.0, if server is
         * 0.7.1 through 0.7.7; see comments in virterror.h. */
        switch (thiscall->err.code) {
        case VIR_WAR_NO_NWFILTER:
            /* no way to tell old VIR_WAR_NO_SECRET apart from
             * VIR_WAR_NO_NWFILTER, but both are very similar
             * warnings, so ignore the difference */
            break;
        case VIR_ERR_INVALID_NWFILTER:
        case VIR_ERR_NO_NWFILTER:
        case VIR_ERR_BUILD_FIREWALL:
            /* server was trying to pass VIR_ERR_INVALID_SECRET,
             * VIR_ERR_NO_SECRET, or VIR_ERR_CONFIG_UNSUPPORTED */
            if (thiscall->err.domain != VIR_FROM_NWFILTER)
                thiscall->err.code += 4;
            break;
        case VIR_WAR_NO_SECRET:
            if (thiscall->err.domain == VIR_FROM_QEMU)
                thiscall->err.code = VIR_ERR_OPERATION_TIMEOUT;
            break;
        case VIR_ERR_INVALID_SECRET:
            if (thiscall->err.domain == VIR_FROM_XEN)
                thiscall->err.code = VIR_ERR_MIGRATE_PERSIST_FAILED;
            break;
        default:
            /* Nothing to alter. */
            break;
        }

10804 10805 10806 10807 10808 10809
        /* See if caller asked us to keep quiet about missing RPCs
         * eg for interop with older servers */
        if (flags & REMOTE_CALL_QUIET_MISSING_RPC &&
            thiscall->err.domain == VIR_FROM_REMOTE &&
            thiscall->err.code == VIR_ERR_RPC &&
            thiscall->err.level == VIR_ERR_ERROR &&
10810
            thiscall->err.message &&
10811 10812
            STRPREFIX(*thiscall->err.message, "unknown procedure")) {
            rv = -2;
10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833
        } else if (thiscall->err.domain == VIR_FROM_REMOTE &&
                   thiscall->err.code == VIR_ERR_RPC &&
                   thiscall->err.level == VIR_ERR_ERROR &&
                   thiscall->err.message &&
                   STRPREFIX(*thiscall->err.message, "unknown procedure")) {
            /*
             * convert missing remote entry points into the unsupported
             * feature error
             */
            virRaiseErrorFull(flags & REMOTE_CALL_IN_OPEN ? NULL : conn,
                              __FILE__, __FUNCTION__, __LINE__,
                              thiscall->err.domain,
                              VIR_ERR_NO_SUPPORT,
                              thiscall->err.level,
                              thiscall->err.str1 ? *thiscall->err.str1 : NULL,
                              thiscall->err.str2 ? *thiscall->err.str2 : NULL,
                              thiscall->err.str3 ? *thiscall->err.str3 : NULL,
                              thiscall->err.int1,
                              thiscall->err.int2,
                              "%s", *thiscall->err.message);
            rv = -1;
10834
        } else {
10835 10836 10837 10838 10839 10840 10841 10842 10843 10844
            virRaiseErrorFull(flags & REMOTE_CALL_IN_OPEN ? NULL : conn,
                              __FILE__, __FUNCTION__, __LINE__,
                              thiscall->err.domain,
                              thiscall->err.code,
                              thiscall->err.level,
                              thiscall->err.str1 ? *thiscall->err.str1 : NULL,
                              thiscall->err.str2 ? *thiscall->err.str2 : NULL,
                              thiscall->err.str3 ? *thiscall->err.str3 : NULL,
                              thiscall->err.int1,
                              thiscall->err.int2,
10845
                              "%s", thiscall->err.message ? *thiscall->err.message : "unknown");
10846
            rv = -1;
10847
        }
10848
        xdr_free((xdrproc_t)xdr_remote_error,  (char *)&thiscall->err);
10849 10850
    } else {
        rv = 0;
10851
    }
10852 10853
    return rv;
}
10854

10855 10856 10857 10858 10859 10860 10861

/*
 * Serial a set of arguments into a method call message,
 * send that to the server and wait for reply
 */
static int
call (virConnectPtr conn, struct private_data *priv,
C
Chris Lalancette 已提交
10862
      int flags,
10863 10864 10865 10866 10867
      int proc_nr,
      xdrproc_t args_filter, char *args,
      xdrproc_t ret_filter, char *ret)
{
    struct remote_thread_call *thiscall;
10868
    int rv;
10869

C
Chris Lalancette 已提交
10870
    thiscall = prepareCall(priv, flags, proc_nr, args_filter, args,
10871 10872 10873 10874 10875 10876
                           ret_filter, ret);

    if (!thiscall) {
        return -1;
    }

10877
    rv = remoteIO(conn, priv, flags, thiscall);
10878
    ignore_value(virCondDestroy(&thiscall->cond));
10879 10880
    VIR_FREE(thiscall);
    return rv;
10881 10882 10883
}


10884 10885 10886 10887 10888 10889 10890 10891 10892 10893
/** remoteDomainEventFired:
 *
 * The callback for monitoring the remote socket
 * for event data
 */
void
remoteDomainEventFired(int watch,
                       int fd,
                       int event,
                       void *opaque)
10894
{
10895 10896
    virConnectPtr        conn = opaque;
    struct private_data *priv = conn->privateData;
10897

10898
    remoteDriverLock(priv);
10899

10900 10901 10902
    /* This should be impossible, but it doesn't hurt to check */
    if (priv->waitDispatch)
        goto done;
10903

10904
    VIR_DEBUG("Event fired %d %d %d %X", watch, fd, event, event);
10905

10906
    if (event & (VIR_EVENT_HANDLE_HANGUP | VIR_EVENT_HANDLE_ERROR)) {
10907
         VIR_DEBUG("%s : VIR_EVENT_HANDLE_HANGUP or "
10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919
               "VIR_EVENT_HANDLE_ERROR encountered", __FUNCTION__);
         virEventRemoveHandle(watch);
         priv->watch = -1;
         goto done;
    }

    if (fd != priv->sock) {
        virEventRemoveHandle(watch);
        priv->watch = -1;
        goto done;
    }

10920
    if (remoteIOHandleInput(conn, priv, 0) < 0)
10921
        VIR_DEBUG0("Something went wrong during async message processing");
10922 10923 10924

done:
    remoteDriverUnlock(priv);
10925 10926
}

10927 10928
static void remoteDomainEventDispatchFunc(virConnectPtr conn,
                                          virDomainEventPtr event,
10929
                                          virConnectDomainEventGenericCallback cb,
10930 10931 10932 10933 10934 10935 10936
                                          void *cbopaque,
                                          void *opaque)
{
    struct private_data *priv = opaque;

    /* Drop the lock whle dispatching, for sake of re-entrancy */
    remoteDriverUnlock(priv);
10937
    VIR_DEBUG("Dispatch event %p %p", event, conn);
10938 10939 10940 10941
    virDomainEventDispatchDefaultFunc(conn, event, cb, cbopaque, NULL);
    remoteDriverLock(priv);
}

10942 10943
void
remoteDomainEventQueueFlush(int timer ATTRIBUTE_UNUSED, void *opaque)
10944
{
10945 10946
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
10947
    virDomainEventQueue tempQueue;
10948 10949 10950

    remoteDriverLock(priv);

10951
    VIR_DEBUG("Event queue flush %p", conn);
10952 10953 10954 10955 10956 10957 10958 10959
    priv->domainEventDispatching = 1;

    /* Copy the queue, so we're reentrant safe */
    tempQueue.count = priv->domainEvents->count;
    tempQueue.events = priv->domainEvents->events;
    priv->domainEvents->count = 0;
    priv->domainEvents->events = NULL;

10960
    virEventUpdateTimeout(priv->eventFlushTimer, -1);
10961 10962
    virDomainEventQueueDispatch(&tempQueue, priv->callbackList,
                                remoteDomainEventDispatchFunc, priv);
10963

10964 10965 10966 10967 10968
    /* Purge any deleted callbacks */
    virDomainEventCallbackListPurgeMarked(priv->callbackList);

    priv->domainEventDispatching = 0;

10969
    remoteDriverUnlock(priv);
10970 10971
}

10972

10973 10974
/* get_nonnull_domain and get_nonnull_network turn an on-wire
 * (name, uuid) pair into virDomainPtr or virNetworkPtr object.
10975
 * These can return NULL if underlying memory allocations fail,
10976
 * but if they do then virterror_internal.has been set.
10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992
 */
static virDomainPtr
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 已提交
10993
static virInterfacePtr
10994
get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
10995
{
10996
    return virGetInterface (conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
10997 10998
}

10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010
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);
}

11011 11012 11013 11014 11015 11016
static virNodeDevicePtr
get_nonnull_node_device (virConnectPtr conn, remote_nonnull_node_device dev)
{
    return virGetNodeDevice(conn, dev.name);
}

11017 11018 11019
static virSecretPtr
get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret)
{
11020
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
11021 11022
}

11023 11024 11025 11026 11027 11028
static virNWFilterPtr
get_nonnull_nwfilter (virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
{
    return virGetNWFilter (conn, nwfilter.name, BAD_CAST nwfilter.uuid);
}

C
Chris Lalancette 已提交
11029 11030 11031 11032 11033 11034
static virDomainSnapshotPtr
get_nonnull_domain_snapshot (virDomainPtr domain, remote_nonnull_domain_snapshot snapshot)
{
    return virGetDomainSnapshot(domain, snapshot.name);
}

11035

11036 11037 11038 11039
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
make_nonnull_domain (remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
{
11040
    dom_dst->id = dom_src->id;
11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051
    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 已提交
11052 11053 11054 11055 11056 11057 11058 11059
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;
}

11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074
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;
}

11075 11076 11077
static void
make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
{
11078
    memcpy (secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
11079 11080
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = secret_src->usageID;
11081 11082
}

11083 11084 11085 11086 11087 11088 11089
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 已提交
11090 11091 11092 11093 11094 11095 11096
static void
make_nonnull_domain_snapshot (remote_nonnull_domain_snapshot *snapshot_dst, virDomainSnapshotPtr snapshot_src)
{
    snapshot_dst->name = snapshot_src->name;
    make_nonnull_domain(&snapshot_dst->domain, snapshot_src->domain);
}

11097 11098
/*----------------------------------------------------------------------*/

11099 11100 11101 11102 11103
unsigned long remoteVersion(void)
{
    return REMOTE_PROTOCOL_VERSION;
}

11104
static virDriver remote_driver = {
11105 11106 11107 11108 11109 11110 11111
    VIR_DRV_REMOTE,
    "remote",
    remoteOpen, /* open */
    remoteClose, /* close */
    remoteSupportsFeature, /* supports_feature */
    remoteType, /* type */
    remoteGetVersion, /* version */
11112
    remoteGetLibVersion, /* libvirtVersion */
11113
    remoteGetHostname, /* getHostname */
11114
    remoteGetSysinfo, /* getSysinfo */
11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132
    remoteGetMaxVcpus, /* getMaxVcpus */
    remoteNodeGetInfo, /* nodeGetInfo */
    remoteGetCapabilities, /* getCapabilities */
    remoteListDomains, /* listDomains */
    remoteNumOfDomains, /* numOfDomains */
    remoteDomainCreateXML, /* domainCreateXML */
    remoteDomainLookupByID, /* domainLookupByID */
    remoteDomainLookupByUUID, /* domainLookupByUUID */
    remoteDomainLookupByName, /* domainLookupByName */
    remoteDomainSuspend, /* domainSuspend */
    remoteDomainResume, /* domainResume */
    remoteDomainShutdown, /* domainShutdown */
    remoteDomainReboot, /* domainReboot */
    remoteDomainDestroy, /* domainDestroy */
    remoteDomainGetOSType, /* domainGetOSType */
    remoteDomainGetMaxMemory, /* domainGetMaxMemory */
    remoteDomainSetMaxMemory, /* domainSetMaxMemory */
    remoteDomainSetMemory, /* domainSetMemory */
11133
    remoteDomainSetMemoryFlags, /* domainSetMemoryFlags */
11134 11135
    remoteDomainSetMemoryParameters, /* domainSetMemoryParameters */
    remoteDomainGetMemoryParameters, /* domainGetMemoryParameters */
11136 11137
    remoteDomainSetBlkioParameters, /* domainSetBlkioParameters */
    remoteDomainGetBlkioParameters, /* domainGetBlkioParameters */
11138 11139 11140 11141 11142
    remoteDomainGetInfo, /* domainGetInfo */
    remoteDomainSave, /* domainSave */
    remoteDomainRestore, /* domainRestore */
    remoteDomainCoreDump, /* domainCoreDump */
    remoteDomainSetVcpus, /* domainSetVcpus */
E
Eric Blake 已提交
11143 11144
    remoteDomainSetVcpusFlags, /* domainSetVcpusFlags */
    remoteDomainGetVcpusFlags, /* domainGetVcpusFlags */
11145 11146 11147 11148 11149 11150
    remoteDomainPinVcpu, /* domainPinVcpu */
    remoteDomainGetVcpus, /* domainGetVcpus */
    remoteDomainGetMaxVcpus, /* domainGetMaxVcpus */
    remoteDomainGetSecurityLabel, /* domainGetSecurityLabel */
    remoteNodeGetSecurityModel, /* nodeGetSecurityModel */
    remoteDomainDumpXML, /* domainDumpXML */
11151 11152
    remoteDomainXMLFromNative, /* domainXMLFromNative */
    remoteDomainXMLToNative, /* domainXMLToNative */
11153 11154 11155
    remoteListDefinedDomains, /* listDefinedDomains */
    remoteNumOfDefinedDomains, /* numOfDefinedDomains */
    remoteDomainCreate, /* domainCreate */
11156
    remoteDomainCreateWithFlags, /* domainCreateWithFlags */
11157 11158 11159
    remoteDomainDefineXML, /* domainDefineXML */
    remoteDomainUndefine, /* domainUndefine */
    remoteDomainAttachDevice, /* domainAttachDevice */
J
Jim Fehlig 已提交
11160
    remoteDomainAttachDeviceFlags, /* domainAttachDeviceFlags */
11161
    remoteDomainDetachDevice, /* domainDetachDevice */
J
Jim Fehlig 已提交
11162
    remoteDomainDetachDeviceFlags, /* domainDetachDeviceFlags */
11163
    remoteDomainUpdateDeviceFlags, /* domainUpdateDeviceFlags */
11164 11165 11166 11167 11168 11169 11170 11171 11172 11173
    remoteDomainGetAutostart, /* domainGetAutostart */
    remoteDomainSetAutostart, /* domainSetAutostart */
    remoteDomainGetSchedulerType, /* domainGetSchedulerType */
    remoteDomainGetSchedulerParameters, /* domainGetSchedulerParameters */
    remoteDomainSetSchedulerParameters, /* domainSetSchedulerParameters */
    remoteDomainMigratePrepare, /* domainMigratePrepare */
    remoteDomainMigratePerform, /* domainMigratePerform */
    remoteDomainMigrateFinish, /* domainMigrateFinish */
    remoteDomainBlockStats, /* domainBlockStats */
    remoteDomainInterfaceStats, /* domainInterfaceStats */
11174
    remoteDomainMemoryStats, /* domainMemoryStats */
11175 11176
    remoteDomainBlockPeek, /* domainBlockPeek */
    remoteDomainMemoryPeek, /* domainMemoryPeek */
11177
    remoteDomainGetBlockInfo, /* domainGetBlockInfo */
11178 11179 11180 11181 11182 11183 11184 11185 11186
    remoteNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    remoteNodeGetFreeMemory, /* getFreeMemory */
    remoteDomainEventRegister, /* domainEventRegister */
    remoteDomainEventDeregister, /* domainEventDeregister */
    remoteDomainMigratePrepare2, /* domainMigratePrepare2 */
    remoteDomainMigrateFinish2, /* domainMigrateFinish2 */
    remoteNodeDeviceDettach, /* nodeDeviceDettach */
    remoteNodeDeviceReAttach, /* nodeDeviceReAttach */
    remoteNodeDeviceReset, /* nodeDeviceReset */
C
Chris Lalancette 已提交
11187
    remoteDomainMigratePrepareTunnel, /* domainMigratePrepareTunnel */
11188 11189 11190 11191
    remoteIsEncrypted, /* isEncrypted */
    remoteIsSecure, /* isSecure */
    remoteDomainIsActive, /* domainIsActive */
    remoteDomainIsPersistent, /* domainIsPersistent */
O
Osier Yang 已提交
11192
    remoteDomainIsUpdated, /* domainIsUpdated */
J
Jiri Denemark 已提交
11193
    remoteCPUCompare, /* cpuCompare */
11194
    remoteCPUBaseline, /* cpuBaseline */
11195
    remoteDomainGetJobInfo, /* domainGetJobInfo */
11196
    remoteDomainAbortJob, /* domainFinishJob */
11197
    remoteDomainMigrateSetMaxDowntime, /* domainMigrateSetMaxDowntime */
11198
    remoteDomainMigrateSetMaxSpeed, /* domainMigrateSetMaxSpeed */
11199 11200
    remoteDomainEventRegisterAny, /* domainEventRegisterAny */
    remoteDomainEventDeregisterAny, /* domainEventDeregisterAny */
11201 11202 11203
    remoteDomainManagedSave, /* domainManagedSave */
    remoteDomainHasManagedSaveImage, /* domainHasManagedSaveImage */
    remoteDomainManagedSaveRemove, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
11204 11205 11206 11207 11208 11209 11210 11211 11212
    remoteDomainSnapshotCreateXML, /* domainSnapshotCreateXML */
    remoteDomainSnapshotDumpXML, /* domainSnapshotDumpXML */
    remoteDomainSnapshotNum, /* domainSnapshotNum */
    remoteDomainSnapshotListNames, /* domainSnapshotListNames */
    remoteDomainSnapshotLookupByName, /* domainSnapshotLookupByName */
    remoteDomainHasCurrentSnapshot, /* domainHasCurrentSnapshot */
    remoteDomainSnapshotCurrent, /* domainSnapshotCurrent */
    remoteDomainRevertToSnapshot, /* domainRevertToSnapshot */
    remoteDomainSnapshotDelete, /* domainSnapshotDelete */
C
Chris Lalancette 已提交
11213
    remoteQemuDomainMonitorCommand, /* qemuDomainMonitorCommand */
11214
    remoteDomainOpenConsole, /* domainOpenConsole */
11215 11216 11217
};

static virNetworkDriver network_driver = {
11218
    .name = "remote",
11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235
    .open = remoteNetworkOpen,
    .close = remoteNetworkClose,
    .numOfNetworks = remoteNumOfNetworks,
    .listNetworks = remoteListNetworks,
    .numOfDefinedNetworks = remoteNumOfDefinedNetworks,
    .listDefinedNetworks = remoteListDefinedNetworks,
    .networkLookupByUUID = remoteNetworkLookupByUUID,
    .networkLookupByName = remoteNetworkLookupByName,
    .networkCreateXML = remoteNetworkCreateXML,
    .networkDefineXML = remoteNetworkDefineXML,
    .networkUndefine = remoteNetworkUndefine,
    .networkCreate = remoteNetworkCreate,
    .networkDestroy = remoteNetworkDestroy,
    .networkDumpXML = remoteNetworkDumpXML,
    .networkGetBridgeName = remoteNetworkGetBridgeName,
    .networkGetAutostart = remoteNetworkGetAutostart,
    .networkSetAutostart = remoteNetworkSetAutostart,
11236 11237
    .networkIsActive = remoteNetworkIsActive,
    .networkIsPersistent = remoteNetworkIsPersistent,
11238 11239
};

D
Daniel Veillard 已提交
11240 11241 11242 11243 11244 11245
static virInterfaceDriver interface_driver = {
    .name = "remote",
    .open = remoteInterfaceOpen,
    .close = remoteInterfaceClose,
    .numOfInterfaces = remoteNumOfInterfaces,
    .listInterfaces = remoteListInterfaces,
11246 11247
    .numOfDefinedInterfaces = remoteNumOfDefinedInterfaces,
    .listDefinedInterfaces = remoteListDefinedInterfaces,
D
Daniel Veillard 已提交
11248 11249 11250 11251 11252 11253 11254
    .interfaceLookupByName = remoteInterfaceLookupByName,
    .interfaceLookupByMACString = remoteInterfaceLookupByMACString,
    .interfaceGetXMLDesc = remoteInterfaceGetXMLDesc,
    .interfaceDefineXML = remoteInterfaceDefineXML,
    .interfaceUndefine = remoteInterfaceUndefine,
    .interfaceCreate = remoteInterfaceCreate,
    .interfaceDestroy = remoteInterfaceDestroy,
11255
    .interfaceIsActive = remoteInterfaceIsActive,
D
Daniel Veillard 已提交
11256 11257
};

11258 11259 11260 11261 11262 11263 11264 11265
static virStorageDriver storage_driver = {
    .name = "remote",
    .open = remoteStorageOpen,
    .close = remoteStorageClose,
    .numOfPools = remoteNumOfStoragePools,
    .listPools = remoteListStoragePools,
    .numOfDefinedPools = remoteNumOfDefinedStoragePools,
    .listDefinedPools = remoteListDefinedStoragePools,
11266
    .findPoolSources = remoteFindStoragePoolSources,
11267
    .poolLookupByName = remoteStoragePoolLookupByName,
11268
    .poolLookupByUUID = remoteStoragePoolLookupByUUID,
11269 11270 11271
    .poolLookupByVolume = remoteStoragePoolLookupByVolume,
    .poolCreateXML = remoteStoragePoolCreateXML,
    .poolDefineXML = remoteStoragePoolDefineXML,
11272
    .poolBuild = remoteStoragePoolBuild,
11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288
    .poolUndefine = remoteStoragePoolUndefine,
    .poolCreate = remoteStoragePoolCreate,
    .poolDestroy = remoteStoragePoolDestroy,
    .poolDelete = remoteStoragePoolDelete,
    .poolRefresh = remoteStoragePoolRefresh,
    .poolGetInfo = remoteStoragePoolGetInfo,
    .poolGetXMLDesc = remoteStoragePoolDumpXML,
    .poolGetAutostart = remoteStoragePoolGetAutostart,
    .poolSetAutostart = remoteStoragePoolSetAutostart,
    .poolNumOfVolumes = remoteStoragePoolNumOfVolumes,
    .poolListVolumes = remoteStoragePoolListVolumes,

    .volLookupByName = remoteStorageVolLookupByName,
    .volLookupByKey = remoteStorageVolLookupByKey,
    .volLookupByPath = remoteStorageVolLookupByPath,
    .volCreateXML = remoteStorageVolCreateXML,
11289
    .volCreateXMLFrom = remoteStorageVolCreateXMLFrom,
11290
    .volDelete = remoteStorageVolDelete,
11291
    .volWipe = remoteStorageVolWipe,
11292 11293 11294
    .volGetInfo = remoteStorageVolGetInfo,
    .volGetXMLDesc = remoteStorageVolDumpXML,
    .volGetPath = remoteStorageVolGetPath,
11295 11296
    .poolIsActive = remoteStoragePoolIsActive,
    .poolIsPersistent = remoteStoragePoolIsPersistent,
11297 11298
};

11299 11300 11301 11302 11303 11304
static virSecretDriver secret_driver = {
    .name = "remote",
    .open = remoteSecretOpen,
    .close = remoteSecretClose,
    .numOfSecrets = remoteSecretNumOfSecrets,
    .listSecrets = remoteSecretListSecrets,
11305
    .lookupByUUID = remoteSecretLookupByUUID,
11306
    .lookupByUsage = remoteSecretLookupByUsage,
11307 11308 11309 11310 11311 11312 11313
    .defineXML = remoteSecretDefineXML,
    .getXMLDesc = remoteSecretGetXMLDesc,
    .setValue = remoteSecretSetValue,
    .getValue = remoteSecretGetValue,
    .undefine = remoteSecretUndefine
};

11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324
static virDeviceMonitor dev_monitor = {
    .name = "remote",
    .open = remoteDevMonOpen,
    .close = remoteDevMonClose,
    .numOfDevices = remoteNodeNumOfDevices,
    .listDevices = remoteNodeListDevices,
    .deviceLookupByName = remoteNodeDeviceLookupByName,
    .deviceDumpXML = remoteNodeDeviceDumpXML,
    .deviceGetParent = remoteNodeDeviceGetParent,
    .deviceNumOfCaps = remoteNodeDeviceNumOfCaps,
    .deviceListCaps = remoteNodeDeviceListCaps,
11325 11326
    .deviceCreateXML = remoteNodeDeviceCreateXML,
    .deviceDestroy = remoteNodeDeviceDestroy
11327 11328
};

11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341
static virNWFilterDriver nwfilter_driver = {
    .name = "remote",
    .open = remoteNWFilterOpen,
    .close = remoteNWFilterClose,
    .nwfilterLookupByUUID = remoteNWFilterLookupByUUID,
    .nwfilterLookupByName = remoteNWFilterLookupByName,
    .getXMLDesc           = remoteNWFilterGetXMLDesc,
    .defineXML            = remoteNWFilterDefineXML,
    .undefine             = remoteNWFilterUndefine,
    .numOfNWFilters       = remoteNumOfNWFilters,
    .listNWFilters        = remoteListNWFilters,
};

11342

A
Atsushi SAKAI 已提交
11343
#ifdef WITH_LIBVIRTD
11344
static virStateDriver state_driver = {
11345
    .name = "Remote",
11346
    .initialize = remoteStartup,
11347
};
A
Atsushi SAKAI 已提交
11348
#endif
11349 11350


11351
/** remoteRegister:
11352 11353
 *
 * Register driver with libvirt driver system.
11354 11355
 *
 * Returns -1 on error.
11356 11357 11358 11359
 */
int
remoteRegister (void)
{
11360
    if (virRegisterDriver (&remote_driver) == -1) return -1;
11361
    if (virRegisterNetworkDriver (&network_driver) == -1) return -1;
D
Daniel Veillard 已提交
11362
    if (virRegisterInterfaceDriver (&interface_driver) == -1) return -1;
11363
    if (virRegisterStorageDriver (&storage_driver) == -1) return -1;
11364
    if (virRegisterDeviceMonitor (&dev_monitor) == -1) return -1;
11365
    if (virRegisterSecretDriver (&secret_driver) == -1) return -1;
11366
    if (virRegisterNWFilterDriver(&nwfilter_driver) == -1) return -1;
A
Atsushi SAKAI 已提交
11367
#ifdef WITH_LIBVIRTD
11368
    if (virRegisterStateDriver (&state_driver) == -1) return -1;
A
Atsushi SAKAI 已提交
11369
#endif
11370 11371 11372

    return 0;
}