remote_driver.c 313.4 KB
Newer Older
1 2 3 4
/*
 * remote_internal.c: driver to provide access to libvirtd running
 *   on a remote machine
 *
5
 * Copyright (C) 2007-2010 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

86 87
#define VIR_FROM_THIS VIR_FROM_REMOTE

88
#ifdef WIN32
89
# define pipe(fds) _pipe(fds,4096, _O_BINARY)
90 91 92
#endif


93 94
static int inside_daemon = 0;

95 96 97 98 99 100 101 102 103 104 105 106 107
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;

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

    unsigned int serial;
    unsigned int proc_nr;

    virCond cond;

119
    int want_reply;
120 121 122 123 124 125 126 127
    xdrproc_t ret_filter;
    char *ret;

    remote_error err;

    struct remote_thread_call *next;
};

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
struct private_stream_data {
    unsigned int has_error : 1;
    remote_error err;

    unsigned int serial;
    unsigned int proc_nr;

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

148
struct private_data {
149 150
    virMutex lock;

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

164 165
#if HAVE_SASL
    sasl_conn_t *saslconn;      /* SASL context */
166

167 168 169
    const char *saslDecoded;
    unsigned int saslDecodedLength;
    unsigned int saslDecodedOffset;
170 171 172 173

    const char *saslEncoded;
    unsigned int saslEncodedLength;
    unsigned int saslEncodedOffset;
174
#endif
175

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

182 183 184 185 186 187 188
    /* 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;
189 190
    /* Flag if we're in process of dispatching */
    int domainEventDispatching;
191 192 193 194 195 196 197

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

    /* List of threads currently waiting for dispatch */
    struct remote_thread_call *waitDispatch;
198 199

    struct private_stream_data *streams;
200 201
};

202
enum {
C
Chris Lalancette 已提交
203 204 205
    REMOTE_CALL_IN_OPEN = (1 << 0),
    REMOTE_CALL_QUIET_MISSING_RPC = (1 << 1),
    REMOTE_QEMU_CALL = (1 << 2),
206 207 208
};


209 210 211 212 213 214 215 216 217 218
static void remoteDriverLock(struct private_data *driver)
{
    virMutexLock(&driver->lock);
}

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

219 220 221 222
static int remoteIO(virConnectPtr conn,
                    struct private_data *priv,
                    int flags,
                    struct remote_thread_call *thiscall);
223 224 225 226
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);
227 228
static int remoteAuthenticate (virConnectPtr conn, struct private_data *priv, int in_open,
                               virConnectAuthPtr auth, const char *authtype);
229
#if HAVE_SASL
230 231
static int remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open,
                           virConnectAuthPtr auth, const char *mech);
232
#endif
233
#if HAVE_POLKIT
234 235
static int remoteAuthPolkit (virConnectPtr conn, struct private_data *priv, int in_open,
                             virConnectAuthPtr auth);
236
#endif /* HAVE_POLKIT */
237 238 239

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

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

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

/* GnuTLS functions used by remoteOpen. */
267
static int initialize_gnutls(void);
268
static gnutls_session_t negotiate_gnutls_on_connection (virConnectPtr conn, struct private_data *priv, int no_verify);
269

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

282
#ifndef WIN32
283 284 285 286
/**
 * remoteFindServerPath:
 *
 * Tries to find the path to the libvirtd binary.
287
 *
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
 * 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++) {
        if (access(serverPaths[i], X_OK | R_OK) == 0) {
            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.
 */
319
static int
320
remoteForkDaemon(void)
321 322
{
    const char *daemonPath = remoteFindDaemonPath();
323
    const char *const daemonargs[] = { daemonPath, "--timeout=30", NULL };
324
    pid_t pid;
325 326

    if (!daemonPath) {
327 328
        remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("failed to find libvirtd binary"));
329
        return -1;
330 331
    }

332
    if (virExecDaemonize(daemonargs, NULL, NULL,
333 334
                         &pid, -1, NULL, NULL,
                         VIR_EXEC_CLEAR_CAPS,
335
                         NULL, NULL, NULL) < 0)
336
        return -1;
337

338
    return 0;
339
}
340
#endif
341

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

348

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

383 384
    /* We handle *ALL*  URIs here. The caller has rejected any
     * URIs we don't care about */
385

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

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

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

430 431 432
    /* Return code from this function, and the private data. */
    int retcode = VIR_DRV_OPEN_ERROR;

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

445

446 447
    priv->hostname = strdup (conn->uri && conn->uri->server ?
                             conn->uri->server : "localhost");
448 449
    if (!priv->hostname)
        goto out_of_memory;
450 451
    if (conn->uri && conn->uri->user) {
        username = strdup (conn->uri->user);
452 453
        if (!username)
            goto out_of_memory;
454 455
    }

456 457 458 459 460
    /* 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).
     */
461 462
    struct qparam *var;
    int i;
463 464
    char *query;

465
    if (conn->uri) {
466
#ifdef HAVE_XMLURI_QUERY_RAW
467
        query = conn->uri->query_raw;
468
#else
469
        query = conn->uri->query;
470
#endif
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
        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")) {
                name = strdup (var->value);
                if (!name) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "command")) {
                command = strdup (var->value);
                if (!command) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "socket")) {
                sockname = strdup (var->value);
                if (!sockname) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "auth")) {
                authtype = strdup (var->value);
                if (!authtype) goto out_of_memory;
                var->ignore = 1;
            } else if (STRCASEEQ (var->name, "netcat")) {
                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;
            } else
                DEBUG("passing through variable '%s' ('%s') to remote end",
                      var->name, var->value);
        }
512

513 514
        /* Construct the original name. */
        if (!name) {
515 516 517
            if (conn->uri->scheme &&
                (STREQ(conn->uri->scheme, "remote") ||
                 STRPREFIX(conn->uri->scheme, "remote+"))) {
518 519 520 521 522
                /* Allow remote serve to probe */
                name = strdup("");
            } else {
                xmlURI tmpuri = {
                    .scheme = conn->uri->scheme,
523
#ifdef HAVE_XMLURI_QUERY_RAW
524
                    .query_raw = qparam_get_query (vars),
525
#else
526
                    .query = qparam_get_query (vars),
527
#endif
528 529 530 531 532 533 534 535 536
                    .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';
                }
537

538
                name = (char *) xmlSaveUri (&tmpuri);
539

540 541 542 543 544 545 546 547 548 549
#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] = '+';
            }
550 551
        }

552
        free_qparam_set (vars);
553
        vars = NULL;
554 555 556 557
    } else {
        /* Probe URI server side */
        name = strdup("");
    }
558

559
    if (!name) {
560
        virReportOOMError();
561
        goto failed;
562 563
    }

564
    DEBUG("proceeding with name = %s", name);
565

566 567
    /* For ext transport, command is required. */
    if (transport == trans_ext && !command) {
568 569
        remoteError(VIR_ERR_INVALID_ARG, "%s",
                    _("remote_open: for 'ext' transport, command is required"));
570 571 572
        goto failed;
    }

573 574 575
    /* Connect to the remote service. */
    switch (transport) {
    case trans_tls:
576
        if (initialize_gnutls() == -1) goto failed;
577
        priv->uses_tls = 1;
578
        priv->is_secure = 1;
579 580 581 582 583 584

        /*FALLTHROUGH*/
    case trans_tcp: {
        // http://people.redhat.com/drepper/userapi-ipv6.html
        struct addrinfo *res, *r;
        struct addrinfo hints;
585
        int saved_errno = EINVAL;
586 587 588
        memset (&hints, 0, sizeof hints);
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_ADDRCONFIG;
589
        int e = getaddrinfo (priv->hostname, port, &hints, &res);
590
        if (e != 0) {
591 592 593
            remoteError(VIR_ERR_SYSTEM_ERROR,
                        _("unable to resolve hostname '%s': %s"),
                        priv->hostname, gai_strerror (e));
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
            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;

611 612
            priv->sock = socket (r->ai_family, SOCK_STREAM, 0);
            if (priv->sock == -1) {
J
Jim Meyering 已提交
613
                saved_errno = errno;
614 615 616 617
                continue;
            }

            /* Disable Nagle - Dan Berrange. */
618
            setsockopt (priv->sock,
619 620 621
                        IPPROTO_TCP, TCP_NODELAY, (void *)&no_slow_start,
                        sizeof no_slow_start);

622
            if (connect (priv->sock, r->ai_addr, r->ai_addrlen) == -1) {
J
Jim Meyering 已提交
623
                saved_errno = errno;
624
                close (priv->sock);
625 626 627
                continue;
            }

628 629
            if (priv->uses_tls) {
                priv->session =
630
                    negotiate_gnutls_on_connection
631
                      (conn, priv, no_verify);
632 633 634
                if (!priv->session) {
                    close (priv->sock);
                    priv->sock = -1;
635
                    goto failed;
636 637 638 639 640 641
                }
            }
            goto tcp_connected;
        }

        freeaddrinfo (res);
642
        virReportSystemError(saved_errno,
643
                             _("unable to connect to libvirtd at '%s'"),
644
                             priv->hostname);
645 646 647 648 649 650 651 652 653 654
        goto failed;

       tcp_connected:
        freeaddrinfo (res);

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

655
#ifndef WIN32
656 657
    case trans_unix: {
        if (!sockname) {
658
            if (flags & VIR_DRV_OPEN_REMOTE_USER) {
659
                char *userdir = virGetUserDirectory(getuid());
660

661
                if (!userdir)
662
                    goto failed;
663

664 665
                if (virAsprintf(&sockname, "@%s" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0) {
                    VIR_FREE(userdir);
666
                    goto out_of_memory;
667 668
                }
                VIR_FREE(userdir);
669
            } else {
670
                if (flags & VIR_DRV_OPEN_REMOTE_RO)
671 672 673
                    sockname = strdup (LIBVIRTD_PRIV_UNIX_SOCKET_RO);
                else
                    sockname = strdup (LIBVIRTD_PRIV_UNIX_SOCKET);
674 675
                if (sockname == NULL)
                    goto out_of_memory;
676
            }
677 678
        }

679 680 681
# ifndef UNIX_PATH_MAX
#  define UNIX_PATH_MAX(addr) (sizeof (addr).sun_path)
# endif
682
        struct sockaddr_un addr;
683 684
        int trials = 0;

685 686
        memset (&addr, 0, sizeof addr);
        addr.sun_family = AF_UNIX;
C
Chris Lalancette 已提交
687
        if (virStrcpyStatic(addr.sun_path, sockname) == NULL) {
688 689
            remoteError(VIR_ERR_INTERNAL_ERROR,
                        _("Socket %s too big for destination"), sockname);
C
Chris Lalancette 已提交
690 691
            goto failed;
        }
692 693
        if (addr.sun_path[0] == '@')
            addr.sun_path[0] = '\0';
694

695
      autostart_retry:
696
        priv->is_secure = 1;
697 698
        priv->sock = socket (AF_UNIX, SOCK_STREAM, 0);
        if (priv->sock == -1) {
699
            virReportSystemError(errno, "%s",
700
                                 _("unable to create socket"));
701 702
            goto failed;
        }
703 704 705 706 707 708 709 710 711 712
        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 &&
713
                trials < 20) {
714 715
                close(priv->sock);
                priv->sock = -1;
716
                if (trials > 0 ||
717
                    remoteForkDaemon() == 0) {
718
                    trials++;
719
                    usleep(1000 * 100 * trials);
720 721 722
                    goto autostart_retry;
                }
            }
723
            virReportSystemError(errno,
724 725
              _("unable to connect to '%s', libvirtd may need to be started"),
              sockname);
726 727 728 729 730 731 732
            goto failed;
        }

        break;
    }

    case trans_ssh: {
733
        int j, nr_args = 6;
734 735 736

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

739
        command = command ? command : strdup ("ssh");
740 741
        if (command == NULL)
            goto out_of_memory;
742 743

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

748 749
        j = 0;
        cmd_argv[j++] = strdup (command);
750 751 752 753
        if (port) {
            cmd_argv[j++] = strdup ("-p");
            cmd_argv[j++] = strdup (port);
        }
754 755 756 757
        if (username) {
            cmd_argv[j++] = strdup ("-l");
            cmd_argv[j++] = strdup (username);
        }
758 759 760 761 762 763 764
        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");
        }
765
        cmd_argv[j++] = strdup (priv->hostname);
766 767
        cmd_argv[j++] = strdup (netcat ? netcat : "nc");
        cmd_argv[j++] = strdup ("-U");
768 769 770 771
        cmd_argv[j++] = strdup (sockname ? sockname :
                                (flags & VIR_CONNECT_RO
                                 ? LIBVIRTD_PRIV_UNIX_SOCKET_RO
                                 : LIBVIRTD_PRIV_UNIX_SOCKET));
772 773
        cmd_argv[j++] = 0;
        assert (j == nr_args);
774 775 776
        for (j = 0; j < (nr_args-1); j++)
            if (cmd_argv[j] == NULL)
                goto out_of_memory;
777 778

        priv->is_secure = 1;
779 780 781 782
    }

        /*FALLTHROUGH*/
    case trans_ext: {
783
        pid_t pid;
784
        int sv[2];
785
        int errfd[2];
786 787 788 789 790 791

        /* 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) {
792
            virReportSystemError(errno, "%s",
793
                                 _("unable to create socket pair"));
794 795 796
            goto failed;
        }

797 798 799 800 801 802
        if (pipe(errfd) == -1) {
            virReportSystemError(errno, "%s",
                                 _("unable to create socket pair"));
            goto failed;
        }

803
        if (virExec((const char**)cmd_argv, NULL, NULL,
804
                    &pid, sv[1], &(sv[1]), &(errfd[1]),
805
                    VIR_EXEC_CLEAR_CAPS) < 0)
806 807 808 809
            goto failed;

        /* Parent continues here. */
        close (sv[1]);
810
        close (errfd[1]);
811
        priv->sock = sv[0];
812
        priv->errfd = errfd[0];
813
        priv->pid = pid;
814 815 816 817

        /* Do not set 'is_secure' flag since we can't guarentee
         * an external program is secure, and this flag must be
         * pessimistic */
818
    }
819 820 821 822 823
#else /* WIN32 */

    case trans_unix:
    case trans_ssh:
    case trans_ext:
824 825 826
        remoteError(VIR_ERR_INVALID_ARG, "%s",
                    _("transport methods unix, ssh and ext are not supported "
                      "under Windows"));
827
        goto failed;
828 829 830

#endif /* WIN32 */

831 832
    } /* switch (transport) */

833
    if (virSetNonBlock(priv->sock) < 0) {
834
        virReportSystemError(errno, "%s",
835
                             _("unable to make socket non-blocking"));
836 837 838
        goto failed;
    }

839 840 841 842 843 844
    if ((priv->errfd != -1) && virSetNonBlock(priv->errfd) < 0) {
        virReportSystemError(errno, "%s",
                             _("unable to make socket non-blocking"));
        goto failed;
    }

845
    if (pipe(wakeupFD) < 0) {
846
        virReportSystemError(errno, "%s",
847
                             _("unable to make pipe"));
848 849 850 851
        goto failed;
    }
    priv->wakeupReadFD = wakeupFD[0];
    priv->wakeupSendFD = wakeupFD[1];
852 853

    /* Try and authenticate with server */
854
    if (remoteAuthenticate(conn, priv, 1, auth, authtype) == -1)
855 856
        goto failed;

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

860
    if (call (conn, priv, REMOTE_CALL_IN_OPEN, REMOTE_PROC_OPEN,
861 862 863 864
              (xdrproc_t) xdr_remote_open_args, (char *) &args,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        goto failed;

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
    /* 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 */
881 882
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("unable to auto-detect URI"));
883 884 885 886 887 888 889 890 891 892
            goto failed;
        }
        if (urierr == -1) {
            goto failed;
        }

        DEBUG("Auto-probed URI is %s", uriret.uri);
        conn->uri = xmlParseURI(uriret.uri);
        VIR_FREE(uriret.uri);
        if (!conn->uri) {
893
            virReportOOMError();
894 895 896 897
            goto failed;
        }
    }

898
    if(VIR_ALLOC(priv->callbackList)<0) {
899
        virReportOOMError();
900 901 902 903
        goto failed;
    }

    if(VIR_ALLOC(priv->domainEvents)<0) {
904
        virReportOOMError();
905 906 907 908 909
        goto failed;
    }

    DEBUG0("Adding Handler for remote events");
    /* Set up a callback to listen on the socket data */
910
    if ((priv->watch = virEventAddHandle(priv->sock,
911
                                         VIR_EVENT_HANDLE_READABLE,
912
                                         remoteDomainEventFired,
913
                                         conn, NULL)) < 0) {
914 915 916 917 918 919
        DEBUG0("virEventAddHandle failed: No addHandleImpl defined."
               " continuing without events.");
    } else {

        DEBUG0("Adding Timeout for remote event queue flushing");
        if ( (priv->eventFlushTimer = virEventAddTimeout(-1,
920 921
                                                         remoteDomainEventQueueFlush,
                                                         conn, NULL)) < 0) {
922 923
            DEBUG0("virEventAddTimeout failed: No addTimeoutImpl defined. "
                    "continuing without events.");
924
            virEventRemoveHandle(priv->watch);
925
            priv->watch = -1;
926 927
        }
    }
928 929 930
    /* Successful. */
    retcode = VIR_DRV_OPEN_SUCCESS;

931
 cleanup:
932
    /* Free up the URL and strings. */
933 934 935 936 937 938 939
    VIR_FREE(name);
    VIR_FREE(command);
    VIR_FREE(sockname);
    VIR_FREE(authtype);
    VIR_FREE(netcat);
    VIR_FREE(username);
    VIR_FREE(port);
940 941 942
    if (cmd_argv) {
        char **cmd_argv_ptr = cmd_argv;
        while (*cmd_argv_ptr) {
943
            VIR_FREE(*cmd_argv_ptr);
944 945
            cmd_argv_ptr++;
        }
946
        VIR_FREE(cmd_argv);
947 948 949
    }

    return retcode;
950 951

 out_of_memory:
952
    virReportOOMError();
953 954
    if (vars)
        free_qparam_set (vars);
955 956 957

 failed:
    /* Close the socket if we failed. */
958 959 960
    if (priv->errfd >= 0)
        close(priv->errfd);

961 962 963 964 965 966
    if (priv->sock >= 0) {
        if (priv->uses_tls && priv->session) {
            gnutls_bye (priv->session, GNUTLS_SHUT_RDWR);
            gnutls_deinit (priv->session);
        }
        close (priv->sock);
967
#ifndef WIN32
968 969 970
        if (priv->pid > 0) {
            pid_t reap;
            do {
971
retry:
972 973
                reap = waitpid(priv->pid, NULL, 0);
                if (reap == -1 && errno == EINTR)
974
                    goto retry;
975 976
            } while (reap != -1 && reap != priv->pid);
        }
977
#endif
978 979
    }

980 981 982 983 984
    if (wakeupFD[0] >= 0) {
        close(wakeupFD[0]);
        close(wakeupFD[1]);
    }

985
    VIR_FREE(priv->hostname);
986
    goto cleanup;
987 988
}

989
static struct private_data *
990
remoteAllocPrivateData(void)
991
{
992
    struct private_data *priv;
993
    if (VIR_ALLOC(priv) < 0) {
994
        virReportOOMError();
995
        return NULL;
996 997
    }

998
    if (virMutexInit(&priv->lock) < 0) {
999 1000
        remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("cannot initialize mutex"));
1001
        VIR_FREE(priv);
1002
        return NULL;
1003 1004 1005
    }
    remoteDriverLock(priv);
    priv->localUses = 1;
1006
    priv->watch = -1;
1007
    priv->sock = -1;
1008
    priv->errfd = -1;
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

    return priv;
}

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

1022
    if (!((*priv) = remoteAllocPrivateData()))
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
        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;
1047
    const char *autostart = getenv("LIBVIRT_AUTOSTART");
1048

1049
    if (inside_daemon && (!conn->uri || (conn->uri && !conn->uri->server)))
1050 1051
        return VIR_DRV_OPEN_DECLINED;

1052
    if (!(priv = remoteAllocPrivateData()))
1053
        return VIR_DRV_OPEN_ERROR;
1054

1055
    if (flags & VIR_CONNECT_RO)
1056 1057
        rflags |= VIR_DRV_OPEN_REMOTE_RO;

1058 1059 1060 1061 1062 1063 1064 1065 1066
    /*
     * 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 已提交
1067
        conn->uri->scheme &&
1068 1069
        ((strchr(conn->uri->scheme, '+') == 0)||
         (strstr(conn->uri->scheme, "+unix") != NULL)) &&
1070 1071
        (STREQ(conn->uri->path, "/session") ||
         STRPREFIX(conn->uri->scheme, "test+")) &&
1072 1073 1074
        getuid() > 0) {
        DEBUG0("Auto-spawn user daemon instance");
        rflags |= VIR_DRV_OPEN_REMOTE_USER;
1075 1076 1077
        if (!autostart ||
            STRNEQ(autostart, "0"))
            rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1078 1079 1080
    }

    /*
J
John Levon 已提交
1081 1082 1083 1084
     * 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.
1085 1086 1087
     */
    if (!conn->uri) {
        DEBUG0("Auto-probe remote URI");
J
John Levon 已提交
1088
#ifndef __sun
1089 1090 1091
        if (getuid() > 0) {
            DEBUG0("Auto-spawn user daemon instance");
            rflags |= VIR_DRV_OPEN_REMOTE_USER;
1092 1093 1094
            if (!autostart ||
                STRNEQ(autostart, "0"))
                rflags |= VIR_DRV_OPEN_REMOTE_AUTOSTART;
1095
        }
J
John Levon 已提交
1096
#endif
1097
    }
1098

1099
    ret = doRemoteOpen(conn, priv, auth, rflags);
1100 1101
    if (ret != VIR_DRV_OPEN_SUCCESS) {
        conn->privateData = NULL;
1102
        remoteDriverUnlock(priv);
1103
        VIR_FREE(priv);
1104 1105
    } else {
        conn->privateData = priv;
1106
        remoteDriverUnlock(priv);
1107 1108 1109 1110 1111
    }
    return ret;
}


1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
/* 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;

1123 1124

static int
1125
check_cert_file(const char *type, const char *file)
1126 1127 1128
{
    struct stat sb;
    if (stat(file, &sb) < 0) {
1129
        virReportSystemError(errno,
1130 1131
                             _("Cannot access %s '%s'"),
                             type, file);
1132 1133 1134 1135 1136 1137
        return -1;
    }
    return 0;
}


1138 1139 1140 1141
static void remote_debug_gnutls_log(int level, const char* str) {
    DEBUG("%d %s", level, str);
}

1142
static int
1143
initialize_gnutls(void)
1144
{
E
Eric Blake 已提交
1145
    static int initialized = 0;
1146
    int err;
1147
    char *gnutlsdebug;
1148

E
Eric Blake 已提交
1149
    if (initialized) return 0;
1150 1151 1152

    gnutls_global_init ();

1153 1154 1155 1156 1157 1158 1159 1160
    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);
    }

1161 1162 1163
    /* X509 stuff */
    err = gnutls_certificate_allocate_credentials (&x509_cred);
    if (err) {
1164 1165 1166
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to allocate TLS credentials: %s"),
                    gnutls_strerror (err));
1167 1168 1169
        return -1;
    }

1170

1171
    if (check_cert_file("CA certificate", LIBVIRT_CACERT) < 0)
1172
        return -1;
1173
    if (check_cert_file("client key", LIBVIRT_CLIENTKEY) < 0)
1174
        return -1;
1175
    if (check_cert_file("client certificate", LIBVIRT_CLIENTCERT) < 0)
1176 1177
        return -1;

1178
    /* Set the trusted CA cert. */
1179
    DEBUG("loading CA file %s", LIBVIRT_CACERT);
1180 1181 1182 1183
    err =
        gnutls_certificate_set_x509_trust_file (x509_cred, LIBVIRT_CACERT,
                                                GNUTLS_X509_FMT_PEM);
    if (err < 0) {
1184 1185 1186
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to load CA certificate: %s"),
                    gnutls_strerror (err));
1187 1188 1189 1190
        return -1;
    }

    /* Set the client certificate and private key. */
1191 1192
    DEBUG("loading client cert and key from files %s and %s",
          LIBVIRT_CLIENTCERT, LIBVIRT_CLIENTKEY);
1193 1194 1195 1196 1197 1198
    err =
        gnutls_certificate_set_x509_key_file (x509_cred,
                                              LIBVIRT_CLIENTCERT,
                                              LIBVIRT_CLIENTKEY,
                                              GNUTLS_X509_FMT_PEM);
    if (err < 0) {
1199 1200 1201
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to load private key/certificate: %s"),
                    gnutls_strerror (err));
1202 1203 1204
        return -1;
    }

E
Eric Blake 已提交
1205
    initialized = 1;
1206 1207 1208
    return 0;
}

1209
static int verify_certificate (virConnectPtr conn, struct private_data *priv, gnutls_session_t session);
1210 1211 1212

static gnutls_session_t
negotiate_gnutls_on_connection (virConnectPtr conn,
1213 1214
                                struct private_data *priv,
                                int no_verify)
1215 1216 1217 1218 1219 1220 1221 1222 1223
{
    const int cert_type_priority[3] = {
        GNUTLS_CRT_X509,
        GNUTLS_CRT_OPENPGP,
        0
    };
    int err;
    gnutls_session_t session;

1224
    /* Initialize TLS session
1225 1226 1227
     */
    err = gnutls_init (&session, GNUTLS_CLIENT);
    if (err) {
1228 1229 1230
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to initialize TLS client: %s"),
                    gnutls_strerror (err));
1231 1232 1233 1234 1235 1236
        return NULL;
    }

    /* Use default priorities */
    err = gnutls_set_default_priority (session);
    if (err) {
1237 1238 1239
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to set TLS algorithm priority: %s"),
                    gnutls_strerror (err));
1240 1241 1242 1243 1244 1245
        return NULL;
    }
    err =
        gnutls_certificate_type_set_priority (session,
                                              cert_type_priority);
    if (err) {
1246 1247 1248
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to set certificate priority: %s"),
                    gnutls_strerror (err));
1249 1250 1251 1252 1253 1254 1255
        return NULL;
    }

    /* put the x509 credentials to the current session
     */
    err = gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, x509_cred);
    if (err) {
1256 1257 1258
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to set session credentials: %s"),
                    gnutls_strerror (err));
1259 1260 1261 1262
        return NULL;
    }

    gnutls_transport_set_ptr (session,
1263
                              (gnutls_transport_ptr_t) (long) priv->sock);
1264 1265 1266 1267 1268 1269 1270

    /* Perform the TLS handshake. */
 again:
    err = gnutls_handshake (session);
    if (err < 0) {
        if (err == GNUTLS_E_AGAIN || err == GNUTLS_E_INTERRUPTED)
            goto again;
1271 1272 1273
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to complete TLS handshake: %s"),
                    gnutls_strerror (err));
1274 1275 1276 1277
        return NULL;
    }

    /* Verify certificate. */
1278
    if (verify_certificate (conn, priv, session) == -1) {
1279 1280 1281
        DEBUG0("failed to verify peer's certificate");
        if (!no_verify) return NULL;
    }
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292

    /* 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;
1293 1294 1295
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to complete TLS initialization: %s"),
                    gnutls_strerror (len));
1296 1297 1298
        return NULL;
    }
    if (len != 1 || buf[0] != '\1') {
1299 1300 1301
        remoteError(VIR_ERR_RPC, "%s",
                    _("server verification (of our certificate or IP "
                      "address) failed"));
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
        return NULL;
    }

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

    return session;
}

static int
verify_certificate (virConnectPtr conn ATTRIBUTE_UNUSED,
1315 1316
                    struct private_data *priv,
                    gnutls_session_t session)
1317 1318 1319 1320 1321 1322 1323 1324
{
    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) {
1325 1326 1327
        remoteError(VIR_ERR_GNUTLS_ERROR,
                    _("unable to verify server certificate: %s"),
                    gnutls_strerror (ret));
1328 1329
        return -1;
    }
1330

1331
    if ((now = time(NULL)) == ((time_t)-1)) {
1332
        virReportSystemError(errno, "%s",
1333
                             _("cannot get current time"));
1334 1335 1336 1337
        return -1;
    }

    if (status != 0) {
1338
        const char *reason = _("Invalid certificate");
1339 1340

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

1343
        if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
1344
            reason = _("The certificate hasn't got a known issuer.");
1345

1346
        if (status & GNUTLS_CERT_REVOKED)
1347
            reason = _("The certificate has been revoked.");
1348 1349

#ifndef GNUTLS_1_0_COMPAT
1350
        if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
1351
            reason = _("The certificate uses an insecure algorithm");
1352
#endif
1353

1354 1355 1356
        remoteError(VIR_ERR_RPC,
                    _("server certificate failed validation: %s"),
                    reason);
1357 1358 1359 1360
        return -1;
    }

    if (gnutls_certificate_type_get(session) != GNUTLS_CRT_X509) {
1361
        remoteError(VIR_ERR_RPC,  "%s",_("Certificate type is not X.509"));
1362 1363
        return -1;
    }
1364

1365
    if (!(certs = gnutls_certificate_get_peers(session, &nCerts))) {
1366
        remoteError(VIR_ERR_RPC,  "%s",_("gnutls_certificate_get_peers failed"));
1367 1368
        return -1;
    }
1369

1370 1371 1372 1373 1374
    for (i = 0 ; i < nCerts ; i++) {
        gnutls_x509_crt_t cert;

        ret = gnutls_x509_crt_init (&cert);
        if (ret < 0) {
1375 1376 1377
            remoteError(VIR_ERR_GNUTLS_ERROR,
                        _("unable to initialize certificate: %s"),
                        gnutls_strerror (ret));
1378 1379
            return -1;
        }
1380

1381 1382
        ret = gnutls_x509_crt_import (cert, &certs[i], GNUTLS_X509_FMT_DER);
        if (ret < 0) {
1383 1384 1385
            remoteError(VIR_ERR_GNUTLS_ERROR,
                        _("unable to import certificate: %s"),
                        gnutls_strerror (ret));
1386 1387 1388
            gnutls_x509_crt_deinit (cert);
            return -1;
        }
1389

1390
        if (gnutls_x509_crt_get_expiration_time (cert) < now) {
1391
            remoteError(VIR_ERR_RPC, "%s", _("The certificate has expired"));
1392 1393 1394
            gnutls_x509_crt_deinit (cert);
            return -1;
        }
1395

1396
        if (gnutls_x509_crt_get_activation_time (cert) > now) {
1397 1398
            remoteError(VIR_ERR_RPC, "%s",
                        _("The certificate is not yet activated"));
1399 1400 1401
            gnutls_x509_crt_deinit (cert);
            return -1;
        }
1402

1403
        if (i == 0) {
1404
            if (!gnutls_x509_crt_check_hostname (cert, priv->hostname)) {
1405 1406 1407
                remoteError(VIR_ERR_RPC,
                            _("Certificate's owner does not match the hostname (%s)"),
                            priv->hostname);
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
                gnutls_x509_crt_deinit (cert);
                return -1;
            }
        }
    }

    return 0;
}

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

1419

1420
static int
1421
doRemoteClose (virConnectPtr conn, struct private_data *priv)
1422
{
1423 1424 1425 1426 1427
    if (priv->eventFlushTimer >= 0) {
        /* Remove timeout */
        virEventRemoveTimeout(priv->eventFlushTimer);
        /* Remove handle for remote events */
        virEventRemoveHandle(priv->watch);
1428
        priv->watch = -1;
1429
    }
1430

1431 1432 1433 1434 1435
    if (call (conn, priv, 0, REMOTE_PROC_CLOSE,
              (xdrproc_t) xdr_void, (char *) NULL,
              (xdrproc_t) xdr_void, (char *) NULL) == -1)
        return -1;

1436
    /* Close socket. */
1437
    if (priv->uses_tls && priv->session) {
1438
        gnutls_bye (priv->session, GNUTLS_SHUT_RDWR);
1439 1440 1441 1442 1443 1444
        gnutls_deinit (priv->session);
    }
#if HAVE_SASL
    if (priv->saslconn)
        sasl_dispose (&priv->saslconn);
#endif
1445
    close (priv->sock);
1446
    close (priv->errfd);
1447

1448
#ifndef WIN32
1449 1450 1451
    if (priv->pid > 0) {
        pid_t reap;
        do {
1452
retry:
1453 1454
            reap = waitpid(priv->pid, NULL, 0);
            if (reap == -1 && errno == EINTR)
1455
                goto retry;
1456 1457
        } while (reap != -1 && reap != priv->pid);
    }
1458
#endif
1459 1460 1461 1462 1463
    if (priv->wakeupReadFD >= 0) {
        close(priv->wakeupReadFD);
        close(priv->wakeupSendFD);
    }

1464

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

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

1471 1472 1473 1474 1475 1476
    /* Free callback list */
    virDomainEventCallbackListFree(priv->callbackList);

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

1477 1478 1479
    return 0;
}

1480 1481 1482
static int
remoteClose (virConnectPtr conn)
{
1483
    int ret = 0;
1484
    struct private_data *priv = conn->privateData;
1485

1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
    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);
1497 1498 1499 1500

    return ret;
}

1501 1502 1503
static int
remoteSupportsFeature (virConnectPtr conn, int feature)
{
1504
    int rv = -1;
1505 1506
    remote_supports_feature_args args;
    remote_supports_feature_ret ret;
1507
    struct private_data *priv = conn->privateData;
1508

1509 1510
    remoteDriverLock(priv);

1511
    /* VIR_DRV_FEATURE_REMOTE* features are handled directly. */
1512 1513 1514 1515
    if (feature == VIR_DRV_FEATURE_REMOTE) {
        rv = 1;
        goto done;
    }
1516 1517 1518 1519 1520 1521 1522

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

    rv = ret.supported;
1526

1527
done:
1528
    remoteDriverUnlock(priv);
1529
    return rv;
1530 1531
}

1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
/* 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)
{
1543
    char *rv = NULL;
1544
    remote_get_type_ret ret;
1545
    struct private_data *priv = conn->privateData;
1546

1547 1548
    remoteDriverLock(priv);

1549
    /* Cached? */
1550 1551 1552 1553
    if (priv->type) {
        rv = priv->type;
        goto done;
    }
1554 1555 1556 1557 1558

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

    /* Stash. */
1562 1563 1564
    rv = priv->type = ret.type;

done:
1565
    remoteDriverUnlock(priv);
1566
    return rv;
1567 1568 1569
}

static int
1570
remoteGetVersion (virConnectPtr conn, unsigned long *hvVer)
1571
{
1572
    int rv = -1;
1573
    remote_get_version_ret ret;
1574
    struct private_data *priv = conn->privateData;
1575

1576 1577
    remoteDriverLock(priv);

1578 1579 1580 1581
    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)
1582
        goto done;
1583 1584

    if (hvVer) *hvVer = ret.hv_ver;
1585 1586 1587
    rv = 0;

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

1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
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;
}

1616 1617 1618
static char *
remoteGetHostname (virConnectPtr conn)
{
1619
    char *rv = NULL;
1620
    remote_get_hostname_ret ret;
1621
    struct private_data *priv = conn->privateData;
1622

1623 1624
    remoteDriverLock(priv);

1625 1626 1627 1628
    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)
1629
        goto done;
1630 1631

    /* Caller frees this. */
1632 1633 1634
    rv = ret.hostname;

done:
1635
    remoteDriverUnlock(priv);
1636
    return rv;
1637 1638
}

1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
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;
}


1705 1706 1707
static int
remoteGetMaxVcpus (virConnectPtr conn, const char *type)
{
1708
    int rv = -1;
1709 1710
    remote_get_max_vcpus_args args;
    remote_get_max_vcpus_ret ret;
1711
    struct private_data *priv = conn->privateData;
1712

1713 1714
    remoteDriverLock(priv);

1715
    memset (&ret, 0, sizeof ret);
1716
    args.type = type == NULL ? NULL : (char **) &type;
1717 1718 1719
    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)
1720 1721 1722
        goto done;

    rv = ret.max_vcpus;
1723

1724
done:
1725
    remoteDriverUnlock(priv);
1726
    return rv;
1727 1728 1729 1730 1731
}

static int
remoteNodeGetInfo (virConnectPtr conn, virNodeInfoPtr info)
{
1732
    int rv = -1;
1733
    remote_node_get_info_ret ret;
1734
    struct private_data *priv = conn->privateData;
1735

1736 1737
    remoteDriverLock(priv);

1738 1739 1740 1741
    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)
1742
        goto done;
1743

C
Chris Lalancette 已提交
1744 1745
    if (virStrcpyStatic(info->model, ret.model) == NULL)
        goto done;
1746 1747 1748 1749 1750 1751 1752
    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;
1753 1754 1755
    rv = 0;

done:
1756
    remoteDriverUnlock(priv);
1757
    return rv;
1758 1759 1760 1761 1762
}

static char *
remoteGetCapabilities (virConnectPtr conn)
{
1763
    char *rv = NULL;
1764
    remote_get_capabilities_ret ret;
1765
    struct private_data *priv = conn->privateData;
1766

1767 1768
    remoteDriverLock(priv);

1769 1770 1771 1772
    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)
1773
        goto done;
1774 1775

    /* Caller frees this. */
1776 1777 1778
    rv = ret.capabilities;

done:
1779
    remoteDriverUnlock(priv);
1780
    return rv;
1781 1782
}

1783 1784 1785 1786 1787 1788
static int
remoteNodeGetCellsFreeMemory(virConnectPtr conn,
                            unsigned long long *freeMems,
                            int startCell,
                            int maxCells)
{
1789
    int rv = -1;
1790 1791 1792
    remote_node_get_cells_free_memory_args args;
    remote_node_get_cells_free_memory_ret ret;
    int i;
1793
    struct private_data *priv = conn->privateData;
1794

1795 1796
    remoteDriverLock(priv);

1797
    if (maxCells > REMOTE_NODE_MAX_CELLS) {
1798 1799 1800
        remoteError(VIR_ERR_RPC,
                    _("too many NUMA cells: %d > %d"),
                    maxCells, REMOTE_NODE_MAX_CELLS);
1801
        goto done;
1802 1803 1804 1805 1806 1807 1808 1809 1810
    }

    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)
1811
        goto done;
1812 1813 1814 1815 1816 1817

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

1818 1819 1820
    rv = ret.freeMems.freeMems_len;

done:
1821
    remoteDriverUnlock(priv);
1822
    return rv;
1823 1824 1825 1826 1827
}

static unsigned long long
remoteNodeGetFreeMemory (virConnectPtr conn)
{
1828
    unsigned long long rv = 0; /* 0 is error value this special function*/
1829
    remote_node_get_free_memory_ret ret;
1830
    struct private_data *priv = conn->privateData;
1831

1832 1833
    remoteDriverLock(priv);

1834 1835 1836 1837
    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)
1838 1839 1840
        goto done;

    rv = ret.freeMem;
1841

1842
done:
1843
    remoteDriverUnlock(priv);
1844
    return rv;
1845 1846 1847
}


1848 1849 1850
static int
remoteListDomains (virConnectPtr conn, int *ids, int maxids)
{
1851
    int rv = -1;
1852 1853 1854
    int i;
    remote_list_domains_args args;
    remote_list_domains_ret ret;
1855
    struct private_data *priv = conn->privateData;
1856

1857 1858
    remoteDriverLock(priv);

1859
    if (maxids > REMOTE_DOMAIN_ID_LIST_MAX) {
1860 1861 1862
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain IDs: %d > %d"),
                    maxids, REMOTE_DOMAIN_ID_LIST_MAX);
1863
        goto done;
1864 1865 1866 1867 1868 1869 1870
    }
    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)
1871
        goto done;
1872 1873

    if (ret.ids.ids_len > maxids) {
1874 1875 1876
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain IDs: %d > %d"),
                    ret.ids.ids_len, maxids);
1877
        goto cleanup;
1878 1879 1880 1881 1882
    }

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

1883 1884 1885
    rv = ret.ids.ids_len;

cleanup:
1886 1887
    xdr_free ((xdrproc_t) xdr_remote_list_domains_ret, (char *) &ret);

1888
done:
1889
    remoteDriverUnlock(priv);
1890
    return rv;
1891 1892 1893 1894 1895
}

static int
remoteNumOfDomains (virConnectPtr conn)
{
1896
    int rv = -1;
1897
    remote_num_of_domains_ret ret;
1898
    struct private_data *priv = conn->privateData;
1899

1900 1901
    remoteDriverLock(priv);

1902 1903 1904 1905
    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)
1906 1907 1908
        goto done;

    rv = ret.num;
1909

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

1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
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;
}

1963
static virDomainPtr
1964
remoteDomainCreateXML (virConnectPtr conn,
1965 1966 1967
                         const char *xmlDesc,
                         unsigned int flags)
{
1968
    virDomainPtr dom = NULL;
1969 1970
    remote_domain_create_xml_args args;
    remote_domain_create_xml_ret ret;
1971
    struct private_data *priv = conn->privateData;
1972

1973 1974
    remoteDriverLock(priv);

1975 1976 1977 1978
    args.xml_desc = (char *) xmlDesc;
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
1979 1980 1981
    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)
1982
        goto done;
1983 1984

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

1987
done:
1988
    remoteDriverUnlock(priv);
1989 1990 1991 1992 1993 1994
    return dom;
}

static virDomainPtr
remoteDomainLookupByID (virConnectPtr conn, int id)
{
1995
    virDomainPtr dom = NULL;
1996 1997
    remote_domain_lookup_by_id_args args;
    remote_domain_lookup_by_id_ret ret;
1998
    struct private_data *priv = conn->privateData;
1999

2000 2001
    remoteDriverLock(priv);

2002 2003 2004 2005 2006 2007
    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)
2008
        goto done;
2009 2010 2011 2012

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

2013
done:
2014
    remoteDriverUnlock(priv);
2015 2016 2017 2018 2019 2020
    return dom;
}

static virDomainPtr
remoteDomainLookupByUUID (virConnectPtr conn, const unsigned char *uuid)
{
2021
    virDomainPtr dom = NULL;
2022 2023
    remote_domain_lookup_by_uuid_args args;
    remote_domain_lookup_by_uuid_ret ret;
2024
    struct private_data *priv = conn->privateData;
2025

2026 2027
    remoteDriverLock(priv);

2028 2029 2030 2031 2032 2033
    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)
2034
        goto done;
2035 2036 2037

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

done:
2040
    remoteDriverUnlock(priv);
2041 2042 2043 2044 2045 2046
    return dom;
}

static virDomainPtr
remoteDomainLookupByName (virConnectPtr conn, const char *name)
{
2047
    virDomainPtr dom = NULL;
2048 2049
    remote_domain_lookup_by_name_args args;
    remote_domain_lookup_by_name_ret ret;
2050
    struct private_data *priv = conn->privateData;
2051

2052 2053
    remoteDriverLock(priv);

2054 2055 2056 2057 2058 2059
    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)
2060
        goto done;
2061 2062 2063 2064

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

2065
done:
2066
    remoteDriverUnlock(priv);
2067 2068 2069 2070 2071 2072
    return dom;
}

static int
remoteDomainSuspend (virDomainPtr domain)
{
2073
    int rv = -1;
2074
    remote_domain_suspend_args args;
2075
    struct private_data *priv = domain->conn->privateData;
2076

2077 2078
    remoteDriverLock(priv);

2079 2080 2081 2082 2083
    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)
2084
        goto done;
2085

2086 2087 2088
    rv = 0;

done:
2089
    remoteDriverUnlock(priv);
2090
    return rv;
2091 2092 2093 2094 2095
}

static int
remoteDomainResume (virDomainPtr domain)
{
2096
    int rv = -1;
2097
    remote_domain_resume_args args;
2098
    struct private_data *priv = domain->conn->privateData;
2099

2100 2101
    remoteDriverLock(priv);

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

2109 2110 2111
    rv = 0;

done:
2112
    remoteDriverUnlock(priv);
2113
    return rv;
2114 2115 2116 2117 2118
}

static int
remoteDomainShutdown (virDomainPtr domain)
{
2119
    int rv = -1;
2120
    remote_domain_shutdown_args args;
2121
    struct private_data *priv = domain->conn->privateData;
2122

2123 2124
    remoteDriverLock(priv);

2125 2126 2127 2128 2129
    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)
2130
        goto done;
2131

2132 2133 2134
    rv = 0;

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

static int
remoteDomainReboot (virDomainPtr domain, unsigned int flags)
{
2142
    int rv = -1;
2143
    remote_domain_reboot_args args;
2144
    struct private_data *priv = domain->conn->privateData;
2145

2146 2147
    remoteDriverLock(priv);

2148 2149 2150 2151 2152 2153
    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)
2154
        goto done;
2155

2156 2157 2158
    rv = 0;

done:
2159
    remoteDriverUnlock(priv);
2160
    return rv;
2161 2162 2163 2164 2165
}

static int
remoteDomainDestroy (virDomainPtr domain)
{
2166
    int rv = -1;
2167
    remote_domain_destroy_args args;
2168
    struct private_data *priv = domain->conn->privateData;
2169

2170 2171
    remoteDriverLock(priv);

2172 2173 2174 2175 2176
    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)
2177
        goto done;
2178

2179
    rv = 0;
2180
    domain->id = -1;
2181 2182

done:
2183
    remoteDriverUnlock(priv);
2184
    return rv;
2185 2186 2187 2188 2189
}

static char *
remoteDomainGetOSType (virDomainPtr domain)
{
2190
    char *rv = NULL;
2191 2192
    remote_domain_get_os_type_args args;
    remote_domain_get_os_type_ret ret;
2193
    struct private_data *priv = domain->conn->privateData;
2194

2195 2196
    remoteDriverLock(priv);

2197 2198 2199 2200 2201 2202
    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)
2203
        goto done;
2204 2205

    /* Caller frees. */
2206 2207 2208
    rv = ret.type;

done:
2209
    remoteDriverUnlock(priv);
2210
    return rv;
2211 2212 2213 2214 2215
}

static unsigned long
remoteDomainGetMaxMemory (virDomainPtr domain)
{
2216
    unsigned long rv = 0;
2217 2218
    remote_domain_get_max_memory_args args;
    remote_domain_get_max_memory_ret ret;
2219
    struct private_data *priv = domain->conn->privateData;
2220

2221 2222
    remoteDriverLock(priv);

2223 2224 2225 2226 2227 2228
    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)
2229 2230 2231
        goto done;

    rv = ret.memory;
2232

2233
done:
2234
    remoteDriverUnlock(priv);
2235
    return rv;
2236 2237 2238 2239 2240
}

static int
remoteDomainSetMaxMemory (virDomainPtr domain, unsigned long memory)
{
2241
    int rv = -1;
2242
    remote_domain_set_max_memory_args args;
2243
    struct private_data *priv = domain->conn->privateData;
2244

2245 2246
    remoteDriverLock(priv);

2247 2248 2249 2250 2251 2252
    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)
2253
        goto done;
2254

2255 2256 2257
    rv = 0;

done:
2258
    remoteDriverUnlock(priv);
2259
    return rv;
2260 2261 2262 2263 2264
}

static int
remoteDomainSetMemory (virDomainPtr domain, unsigned long memory)
{
2265
    int rv = -1;
2266
    remote_domain_set_memory_args args;
2267
    struct private_data *priv = domain->conn->privateData;
2268

2269 2270
    remoteDriverLock(priv);

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

2279 2280 2281
    rv = 0;

done:
2282
    remoteDriverUnlock(priv);
2283
    return rv;
2284 2285
}

2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454
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) {
        // 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_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;
}

2455 2456 2457
static int
remoteDomainGetInfo (virDomainPtr domain, virDomainInfoPtr info)
{
2458
    int rv = -1;
2459 2460
    remote_domain_get_info_args args;
    remote_domain_get_info_ret ret;
2461
    struct private_data *priv = domain->conn->privateData;
2462

2463 2464
    remoteDriverLock(priv);

2465 2466 2467 2468 2469 2470
    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)
2471
        goto done;
2472 2473 2474 2475 2476 2477

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

2479 2480 2481
    rv = 0;

done:
2482
    remoteDriverUnlock(priv);
2483
    return rv;
2484 2485 2486 2487 2488
}

static int
remoteDomainSave (virDomainPtr domain, const char *to)
{
2489
    int rv = -1;
2490
    remote_domain_save_args args;
2491
    struct private_data *priv = domain->conn->privateData;
2492

2493 2494
    remoteDriverLock(priv);

2495 2496 2497 2498 2499 2500
    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)
2501
        goto done;
2502

2503 2504 2505
    rv = 0;

done:
2506
    remoteDriverUnlock(priv);
2507
    return rv;
2508 2509 2510 2511 2512
}

static int
remoteDomainRestore (virConnectPtr conn, const char *from)
{
2513
    int rv = -1;
2514
    remote_domain_restore_args args;
2515
    struct private_data *priv = conn->privateData;
2516

2517 2518
    remoteDriverLock(priv);

2519 2520 2521 2522 2523
    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)
2524
        goto done;
2525

2526 2527 2528
    rv = 0;

done:
2529
    remoteDriverUnlock(priv);
2530
    return rv;
2531 2532 2533 2534 2535
}

static int
remoteDomainCoreDump (virDomainPtr domain, const char *to, int flags)
{
2536
    int rv = -1;
2537
    remote_domain_core_dump_args args;
2538
    struct private_data *priv = domain->conn->privateData;
2539

2540 2541
    remoteDriverLock(priv);

2542 2543 2544 2545 2546 2547 2548
    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)
2549
        goto done;
2550

2551 2552 2553
    rv = 0;

done:
2554
    remoteDriverUnlock(priv);
2555
    return rv;
2556 2557 2558 2559 2560
}

static int
remoteDomainSetVcpus (virDomainPtr domain, unsigned int nvcpus)
{
2561
    int rv = -1;
2562
    remote_domain_set_vcpus_args args;
2563
    struct private_data *priv = domain->conn->privateData;
2564

2565 2566
    remoteDriverLock(priv);

2567 2568 2569 2570 2571 2572
    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)
2573
        goto done;
2574

2575 2576 2577
    rv = 0;

done:
2578
    remoteDriverUnlock(priv);
2579
    return rv;
2580 2581 2582 2583 2584 2585 2586 2587
}

static int
remoteDomainPinVcpu (virDomainPtr domain,
                     unsigned int vcpu,
                     unsigned char *cpumap,
                     int maplen)
{
2588
    int rv = -1;
2589
    remote_domain_pin_vcpu_args args;
2590
    struct private_data *priv = domain->conn->privateData;
2591

2592 2593
    remoteDriverLock(priv);

2594
    if (maplen > REMOTE_CPUMAP_MAX) {
2595 2596 2597
        remoteError(VIR_ERR_RPC,
                    _("map length greater than maximum: %d > %d"),
                    maplen, REMOTE_CPUMAP_MAX);
2598
        goto done;
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608
    }

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

2611 2612 2613
    rv = 0;

done:
2614
    remoteDriverUnlock(priv);
2615
    return rv;
2616 2617 2618 2619 2620 2621 2622 2623 2624
}

static int
remoteDomainGetVcpus (virDomainPtr domain,
                      virVcpuInfoPtr info,
                      int maxinfo,
                      unsigned char *cpumaps,
                      int maplen)
{
2625
    int rv = -1;
2626 2627 2628
    int i;
    remote_domain_get_vcpus_args args;
    remote_domain_get_vcpus_ret ret;
2629
    struct private_data *priv = domain->conn->privateData;
2630

2631 2632
    remoteDriverLock(priv);

2633
    if (maxinfo > REMOTE_VCPUINFO_MAX) {
2634 2635 2636
        remoteError(VIR_ERR_RPC,
                    _("vCPU count exceeds maximum: %d > %d"),
                    maxinfo, REMOTE_VCPUINFO_MAX);
2637
        goto done;
2638
    }
2639
    if (maxinfo * maplen > REMOTE_CPUMAPS_MAX) {
2640 2641 2642
        remoteError(VIR_ERR_RPC,
                    _("vCPU map buffer length exceeds maximum: %d > %d"),
                    maxinfo * maplen, REMOTE_CPUMAPS_MAX);
2643
        goto done;
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653
    }

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

    if (ret.info.info_len > maxinfo) {
2657 2658 2659
        remoteError(VIR_ERR_RPC,
                    _("host reports too many vCPUs: %d > %d"),
                    ret.info.info_len, maxinfo);
2660
        goto cleanup;
2661
    }
2662
    if (ret.cpumaps.cpumaps_len > maxinfo * maplen) {
2663 2664 2665
        remoteError(VIR_ERR_RPC,
                    _("host reports map buffer length exceeds maximum: %d > %d"),
                    ret.cpumaps.cpumaps_len, maxinfo * maplen);
2666
        goto cleanup;
2667 2668
    }

2669 2670 2671
    memset (info, 0, sizeof (virVcpuInfo) * maxinfo);
    memset (cpumaps, 0, maxinfo * maplen);

2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
    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];

2682 2683 2684
    rv = ret.info.info_len;

cleanup:
2685
    xdr_free ((xdrproc_t) xdr_remote_domain_get_vcpus_ret, (char *) &ret);
2686 2687

done:
2688
    remoteDriverUnlock(priv);
2689
    return rv;
2690 2691 2692 2693 2694
}

static int
remoteDomainGetMaxVcpus (virDomainPtr domain)
{
2695
    int rv = -1;
2696 2697
    remote_domain_get_max_vcpus_args args;
    remote_domain_get_max_vcpus_ret ret;
2698
    struct private_data *priv = domain->conn->privateData;
2699

2700 2701
    remoteDriverLock(priv);

2702 2703 2704
    make_nonnull_domain (&args.dom, domain);

    memset (&ret, 0, sizeof ret);
2705
    if (call (domain->conn, priv, 0, REMOTE_PROC_DOMAIN_GET_MAX_VCPUS,
2706 2707
              (xdrproc_t) xdr_remote_domain_get_max_vcpus_args, (char *) &args,
              (xdrproc_t) xdr_remote_domain_get_max_vcpus_ret, (char *) &ret) == -1)
2708
        goto done;
2709

2710 2711 2712
    rv = ret.num;

done:
2713
    remoteDriverUnlock(priv);
2714
    return rv;
2715 2716
}

2717 2718 2719 2720 2721 2722
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;
2723 2724 2725
    int rv = -1;

    remoteDriverLock(priv);
2726 2727 2728

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

2731 2732 2733
    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) {
2734
        goto done;
2735 2736 2737 2738
    }

    if (ret.label.label_val != NULL) {
        if (strlen (ret.label.label_val) >= sizeof seclabel->label) {
2739 2740
            remoteError(VIR_ERR_RPC, _("security label exceeds maximum: %zd"),
                        sizeof seclabel->label - 1);
2741
            goto done;
2742 2743 2744 2745 2746
        }
        strcpy (seclabel->label, ret.label.label_val);
        seclabel->enforcing = ret.enforcing;
    }

2747 2748 2749 2750 2751
    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
2752 2753 2754 2755 2756 2757 2758
}

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

    remoteDriverLock(priv);
2762 2763

    memset (&ret, 0, sizeof ret);
2764 2765
    memset (secmodel, 0, sizeof (*secmodel));

2766 2767 2768
    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) {
2769
        goto done;
2770 2771 2772 2773
    }

    if (ret.model.model_val != NULL) {
        if (strlen (ret.model.model_val) >= sizeof secmodel->model) {
2774 2775
            remoteError(VIR_ERR_RPC, _("security model exceeds maximum: %zd"),
                        sizeof secmodel->model - 1);
2776
            goto done;
2777 2778 2779 2780 2781 2782
        }
        strcpy (secmodel->model, ret.model.model_val);
    }

    if (ret.doi.doi_val != NULL) {
        if (strlen (ret.doi.doi_val) >= sizeof secmodel->doi) {
2783 2784
            remoteError(VIR_ERR_RPC, _("security doi exceeds maximum: %zd"),
                        sizeof secmodel->doi - 1);
2785
            goto done;
2786 2787 2788
        }
        strcpy (secmodel->doi, ret.doi.doi_val);
    }
2789 2790 2791 2792 2793 2794

    rv = 0;

done:
    remoteDriverUnlock(priv);
    return rv;
2795 2796
}

2797 2798 2799
static char *
remoteDomainDumpXML (virDomainPtr domain, int flags)
{
2800
    char *rv = NULL;
2801 2802
    remote_domain_dump_xml_args args;
    remote_domain_dump_xml_ret ret;
2803
    struct private_data *priv = domain->conn->privateData;
2804

2805 2806
    remoteDriverLock(priv);

2807 2808 2809 2810 2811 2812 2813
    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)
2814
        goto done;
2815 2816

    /* Caller frees. */
2817 2818 2819
    rv = ret.xml;

done:
2820
    remoteDriverUnlock(priv);
2821
    return rv;
2822 2823
}

2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
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;
}

2886 2887 2888 2889 2890 2891 2892
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)
{
2893
    int rv = -1;
2894 2895
    remote_domain_migrate_prepare_args args;
    remote_domain_migrate_prepare_ret ret;
2896
    struct private_data *priv = dconn->privateData;
2897

2898 2899
    remoteDriverLock(priv);

2900 2901 2902 2903 2904 2905 2906 2907 2908
    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)
2909
        goto done;
2910 2911 2912 2913 2914 2915 2916 2917

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

2918 2919 2920
    rv = 0;

done:
2921
    remoteDriverUnlock(priv);
2922
    return rv;
2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933
}

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

2938 2939
    remoteDriverLock(priv);

2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
    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)
2951
        goto done;
2952

2953 2954 2955
    rv = 0;

done:
2956
    remoteDriverUnlock(priv);
2957
    return rv;
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967
}

static virDomainPtr
remoteDomainMigrateFinish (virConnectPtr dconn,
                           const char *dname,
                           const char *cookie,
                           int cookielen,
                           const char *uri,
                           unsigned long flags)
{
2968
    virDomainPtr ddom = NULL;
2969 2970
    remote_domain_migrate_finish_args args;
    remote_domain_migrate_finish_ret ret;
2971
    struct private_data *priv = dconn->privateData;
2972

2973 2974
    remoteDriverLock(priv);

2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
    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)
2985
        goto done;
2986 2987 2988 2989

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

2990
done:
2991
    remoteDriverUnlock(priv);
2992 2993 2994
    return ddom;
}

D
Daniel Veillard 已提交
2995 2996 2997 2998 2999 3000 3001 3002
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)
{
3003
    int rv = -1;
D
Daniel Veillard 已提交
3004 3005
    remote_domain_migrate_prepare2_args args;
    remote_domain_migrate_prepare2_ret ret;
3006
    struct private_data *priv = dconn->privateData;
D
Daniel Veillard 已提交
3007

3008 3009
    remoteDriverLock(priv);

D
Daniel Veillard 已提交
3010 3011 3012 3013 3014 3015 3016 3017 3018 3019
    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)
3020
        goto done;
D
Daniel Veillard 已提交
3021 3022

    if (ret.cookie.cookie_len > 0) {
3023 3024 3025 3026 3027
        if (!cookie || !cookielen) {
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("caller ignores cookie or cookielen"));
            goto error;
        }
D
Daniel Veillard 已提交
3028 3029 3030
        *cookie = ret.cookie.cookie_val; /* Caller frees. */
        *cookielen = ret.cookie.cookie_len;
    }
3031 3032 3033 3034 3035 3036
    if (ret.uri_out) {
        if (!uri_out) {
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("caller ignores uri_out"));
            goto error;
        }
D
Daniel Veillard 已提交
3037
        *uri_out = *ret.uri_out; /* Caller frees. */
3038
    }
D
Daniel Veillard 已提交
3039

3040 3041 3042
    rv = 0;

done:
3043
    remoteDriverUnlock(priv);
3044
    return rv;
3045 3046 3047 3048 3049 3050
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 已提交
3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061
}

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

3067 3068
    remoteDriverLock(priv);

D
Daniel Veillard 已提交
3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079
    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)
3080
        goto done;
D
Daniel Veillard 已提交
3081 3082 3083 3084

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

3085
done:
3086
    remoteDriverUnlock(priv);
D
Daniel Veillard 已提交
3087 3088 3089
    return ddom;
}

3090 3091 3092
static int
remoteListDefinedDomains (virConnectPtr conn, char **const names, int maxnames)
{
3093
    int rv = -1;
3094 3095 3096
    int i;
    remote_list_defined_domains_args args;
    remote_list_defined_domains_ret ret;
3097
    struct private_data *priv = conn->privateData;
3098

3099 3100
    remoteDriverLock(priv);

3101
    if (maxnames > REMOTE_DOMAIN_NAME_LIST_MAX) {
3102 3103 3104
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain names: %d > %d"),
                    maxnames, REMOTE_DOMAIN_NAME_LIST_MAX);
3105
        goto done;
3106 3107 3108 3109 3110 3111 3112
    }
    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)
3113
        goto done;
3114 3115

    if (ret.names.names_len > maxnames) {
3116 3117 3118
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain names: %d > %d"),
                    ret.names.names_len, maxnames);
3119
        goto cleanup;
3120 3121 3122 3123 3124 3125 3126
    }

    /* 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.
     */
3127
    for (i = 0; i < ret.names.names_len; ++i) {
3128 3129
        names[i] = strdup (ret.names.names_val[i]);

3130 3131 3132 3133
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

3134
            virReportOOMError();
3135 3136 3137 3138
            goto cleanup;
        }
    }

3139 3140 3141
    rv = ret.names.names_len;

cleanup:
3142 3143
    xdr_free ((xdrproc_t) xdr_remote_list_defined_domains_ret, (char *) &ret);

3144
done:
3145
    remoteDriverUnlock(priv);
3146
    return rv;
3147 3148 3149 3150 3151
}

static int
remoteNumOfDefinedDomains (virConnectPtr conn)
{
3152
    int rv = -1;
3153
    remote_num_of_defined_domains_ret ret;
3154
    struct private_data *priv = conn->privateData;
3155

3156 3157
    remoteDriverLock(priv);

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

3164 3165 3166
    rv = ret.num;

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

static int
remoteDomainCreate (virDomainPtr domain)
{
3174
    int rv = -1;
3175
    remote_domain_create_args args;
3176 3177
    remote_domain_lookup_by_uuid_args args2;
    remote_domain_lookup_by_uuid_ret ret2;
3178
    struct private_data *priv = domain->conn->privateData;
3179

3180 3181
    remoteDriverLock(priv);

3182 3183 3184 3185 3186
    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)
3187
        goto done;
3188

3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202
    /* 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);

3203 3204 3205
    rv = 0;

done:
3206
    remoteDriverUnlock(priv);
3207
    return rv;
3208 3209
}

3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241
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;
}

3242 3243 3244
static virDomainPtr
remoteDomainDefineXML (virConnectPtr conn, const char *xml)
{
3245
    virDomainPtr dom = NULL;
3246 3247
    remote_domain_define_xml_args args;
    remote_domain_define_xml_ret ret;
3248
    struct private_data *priv = conn->privateData;
3249

3250 3251
    remoteDriverLock(priv);

3252 3253 3254 3255 3256 3257
    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)
3258
        goto done;
3259 3260 3261 3262

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

3263
done:
3264
    remoteDriverUnlock(priv);
3265 3266 3267 3268 3269 3270
    return dom;
}

static int
remoteDomainUndefine (virDomainPtr domain)
{
3271
    int rv = -1;
3272
    remote_domain_undefine_args args;
3273
    struct private_data *priv = domain->conn->privateData;
3274

3275 3276
    remoteDriverLock(priv);

3277 3278 3279 3280 3281
    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)
3282
        goto done;
3283

3284 3285 3286
    rv = 0;

done:
3287
    remoteDriverUnlock(priv);
3288
    return rv;
3289 3290 3291
}

static int
3292
remoteDomainAttachDevice (virDomainPtr domain, const char *xml)
3293
{
3294
    int rv = -1;
3295
    remote_domain_attach_device_args args;
3296
    struct private_data *priv = domain->conn->privateData;
3297

3298 3299
    remoteDriverLock(priv);

3300
    make_nonnull_domain (&args.dom, domain);
3301
    args.xml = (char *) xml;
3302 3303 3304 3305

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

3308 3309 3310
    rv = 0;

done:
3311
    remoteDriverUnlock(priv);
3312
    return rv;
3313 3314
}

J
Jim Fehlig 已提交
3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340
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;
}

3341
static int
3342
remoteDomainDetachDevice (virDomainPtr domain, const char *xml)
3343
{
3344
    int rv = -1;
3345
    remote_domain_detach_device_args args;
3346
    struct private_data *priv = domain->conn->privateData;
3347

3348 3349
    remoteDriverLock(priv);

3350
    make_nonnull_domain (&args.dom, domain);
3351
    args.xml = (char *) xml;
3352 3353 3354 3355

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

3358 3359 3360
    rv = 0;

done:
3361
    remoteDriverUnlock(priv);
3362
    return rv;
3363 3364
}

J
Jim Fehlig 已提交
3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390
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;
}

3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416
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;
}

3417 3418 3419
static int
remoteDomainGetAutostart (virDomainPtr domain, int *autostart)
{
3420
    int rv = -1;
3421 3422
    remote_domain_get_autostart_args args;
    remote_domain_get_autostart_ret ret;
3423
    struct private_data *priv = domain->conn->privateData;
3424

3425 3426
    remoteDriverLock(priv);

3427 3428 3429 3430 3431 3432
    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)
3433
        goto done;
3434 3435

    if (autostart) *autostart = ret.autostart;
3436 3437 3438
    rv = 0;

done:
3439
    remoteDriverUnlock(priv);
3440
    return rv;
3441 3442 3443 3444 3445
}

static int
remoteDomainSetAutostart (virDomainPtr domain, int autostart)
{
3446
    int rv = -1;
3447
    remote_domain_set_autostart_args args;
3448
    struct private_data *priv = domain->conn->privateData;
3449

3450 3451
    remoteDriverLock(priv);

3452 3453 3454 3455 3456 3457
    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)
3458
        goto done;
3459

3460 3461 3462
    rv = 0;

done:
3463
    remoteDriverUnlock(priv);
3464
    return rv;
3465 3466
}

3467 3468 3469
static char *
remoteDomainGetSchedulerType (virDomainPtr domain, int *nparams)
{
3470
    char *rv = NULL;
3471 3472
    remote_domain_get_scheduler_type_args args;
    remote_domain_get_scheduler_type_ret ret;
3473
    struct private_data *priv = domain->conn->privateData;
3474

3475 3476
    remoteDriverLock(priv);

3477 3478 3479 3480 3481 3482
    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)
3483
        goto done;
3484 3485 3486 3487

    if (nparams) *nparams = ret.nparams;

    /* Caller frees this. */
3488 3489 3490
    rv = ret.type;

done:
3491
    remoteDriverUnlock(priv);
3492
    return rv;
3493 3494 3495 3496 3497 3498
}

static int
remoteDomainGetSchedulerParameters (virDomainPtr domain,
                                    virSchedParameterPtr params, int *nparams)
{
3499
    int rv = -1;
3500 3501
    remote_domain_get_scheduler_parameters_args args;
    remote_domain_get_scheduler_parameters_ret ret;
3502
    int i = -1;
3503
    struct private_data *priv = domain->conn->privateData;
3504

3505 3506
    remoteDriverLock(priv);

3507 3508 3509 3510 3511 3512 3513
    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)
3514
        goto done;
3515 3516 3517 3518

    /* Check the length of the returned list carefully. */
    if (ret.params.params_len > REMOTE_DOMAIN_SCHEDULER_PARAMETERS_MAX ||
        ret.params.params_len > *nparams) {
3519 3520 3521
        remoteError(VIR_ERR_RPC, "%s",
                    _("remoteDomainGetSchedulerParameters: "
                      "returned number of parameters exceeds limit"));
3522
        goto cleanup;
3523 3524 3525 3526 3527
    }
    *nparams = ret.params.params_len;

    /* Deserialise the result. */
    for (i = 0; i < *nparams; ++i) {
C
Chris Lalancette 已提交
3528
        if (virStrcpyStatic(params[i].field, ret.params.params_val[i].field) == NULL) {
3529 3530 3531
            remoteError(VIR_ERR_INTERNAL_ERROR,
                        _("Parameter %s too big for destination"),
                        ret.params.params_val[i].field);
C
Chris Lalancette 已提交
3532 3533
            goto cleanup;
        }
3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548
        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:
3549 3550 3551
            remoteError(VIR_ERR_RPC, "%s",
                        _("remoteDomainGetSchedulerParameters: "
                          "unknown parameter type"));
3552
            goto cleanup;
3553 3554 3555
        }
    }

3556 3557 3558
    rv = 0;

cleanup:
3559
    xdr_free ((xdrproc_t) xdr_remote_domain_get_scheduler_parameters_ret, (char *) &ret);
3560
done:
3561
    remoteDriverUnlock(priv);
3562
    return rv;
3563 3564 3565 3566 3567 3568
}

static int
remoteDomainSetSchedulerParameters (virDomainPtr domain,
                                    virSchedParameterPtr params, int nparams)
{
3569
    int rv = -1;
3570 3571
    remote_domain_set_scheduler_parameters_args args;
    int i, do_error;
3572
    struct private_data *priv = domain->conn->privateData;
3573

3574 3575
    remoteDriverLock(priv);

3576 3577 3578 3579
    make_nonnull_domain (&args.dom, domain);

    /* Serialise the scheduler parameters. */
    args.params.params_len = nparams;
3580
    if (VIR_ALLOC_N(args.params.params_val, nparams) < 0) {
3581
        virReportOOMError();
3582
        goto done;
3583 3584 3585 3586 3587 3588 3589
    }

    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) {
3590
            virReportOOMError();
3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607
            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:
3608
            remoteError(VIR_ERR_RPC, "%s", _("unknown parameter type"));
3609 3610 3611 3612 3613 3614
            do_error = 1;
        }
    }

    if (do_error) {
        xdr_free ((xdrproc_t) xdr_remote_domain_set_scheduler_parameters_args, (char *) &args);
3615
        goto done;
3616 3617 3618 3619 3620
    }

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

3623 3624 3625
    rv = 0;

done:
3626
    remoteDriverUnlock(priv);
3627
    return rv;
3628 3629
}

3630 3631 3632 3633
static int
remoteDomainBlockStats (virDomainPtr domain, const char *path,
                        struct _virDomainBlockStats *stats)
{
3634
    int rv = -1;
3635 3636
    remote_domain_block_stats_args args;
    remote_domain_block_stats_ret ret;
3637
    struct private_data *priv = domain->conn->privateData;
3638

3639 3640
    remoteDriverLock(priv);

3641 3642 3643 3644 3645 3646 3647 3648
    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)
3649
        goto done;
3650 3651 3652 3653 3654 3655 3656

    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;

3657 3658 3659
    rv = 0;

done:
3660
    remoteDriverUnlock(priv);
3661
    return rv;
3662 3663 3664 3665 3666 3667
}

static int
remoteDomainInterfaceStats (virDomainPtr domain, const char *path,
                            struct _virDomainInterfaceStats *stats)
{
3668
    int rv = -1;
3669 3670
    remote_domain_interface_stats_args args;
    remote_domain_interface_stats_ret ret;
3671
    struct private_data *priv = domain->conn->privateData;
3672

3673 3674
    remoteDriverLock(priv);

3675 3676 3677 3678 3679 3680 3681 3682 3683
    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)
3684
        goto done;
3685 3686 3687 3688 3689 3690 3691 3692 3693 3694

    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;

3695 3696 3697
    rv = 0;

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

3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716
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) {
3717 3718 3719
        remoteError(VIR_ERR_RPC,
                    _("too many memory stats requested: %d > %d"), nr_stats,
                    REMOTE_DOMAIN_MEMORY_STATS_MAX);
3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744
        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;
}

3745 3746 3747 3748 3749 3750 3751 3752
static int
remoteDomainBlockPeek (virDomainPtr domain,
                       const char *path,
                       unsigned long long offset,
                       size_t size,
                       void *buffer,
                       unsigned int flags)
{
3753
    int rv = -1;
3754 3755
    remote_domain_block_peek_args args;
    remote_domain_block_peek_ret ret;
3756
    struct private_data *priv = domain->conn->privateData;
3757

3758 3759
    remoteDriverLock(priv);

3760
    if (size > REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX) {
3761 3762 3763
        remoteError(VIR_ERR_RPC,
                    _("block peek request too large for remote protocol, %zi > %d"),
                    size, REMOTE_DOMAIN_BLOCK_PEEK_BUFFER_MAX);
3764
        goto done;
3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778
    }

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

    if (ret.buffer.buffer_len != size) {
3782 3783
        remoteError(VIR_ERR_RPC, "%s",
                    _("returned buffer is not same size as requested"));
3784
        goto cleanup;
3785 3786 3787
    }

    memcpy (buffer, ret.buffer.buffer_val, size);
3788 3789 3790
    rv = 0;

cleanup:
3791
    VIR_FREE(ret.buffer.buffer_val);
3792

3793
done:
3794
    remoteDriverUnlock(priv);
3795
    return rv;
3796 3797
}

R
Richard W.M. Jones 已提交
3798 3799 3800 3801 3802 3803 3804
static int
remoteDomainMemoryPeek (virDomainPtr domain,
                        unsigned long long offset,
                        size_t size,
                        void *buffer,
                        unsigned int flags)
{
3805
    int rv = -1;
R
Richard W.M. Jones 已提交
3806 3807
    remote_domain_memory_peek_args args;
    remote_domain_memory_peek_ret ret;
3808
    struct private_data *priv = domain->conn->privateData;
R
Richard W.M. Jones 已提交
3809

3810 3811
    remoteDriverLock(priv);

R
Richard W.M. Jones 已提交
3812
    if (size > REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX) {
3813 3814 3815
        remoteError(VIR_ERR_RPC,
                    _("memory peek request too large for remote protocol, %zi > %d"),
                    size, REMOTE_DOMAIN_MEMORY_PEEK_BUFFER_MAX);
3816
        goto done;
R
Richard W.M. Jones 已提交
3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829
    }

    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)
3830
        goto done;
R
Richard W.M. Jones 已提交
3831 3832

    if (ret.buffer.buffer_len != size) {
3833 3834
        remoteError(VIR_ERR_RPC, "%s",
                    _("returned buffer is not same size as requested"));
3835
        goto cleanup;
R
Richard W.M. Jones 已提交
3836 3837 3838
    }

    memcpy (buffer, ret.buffer.buffer_val, size);
3839 3840 3841
    rv = 0;

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

3844
done:
3845
    remoteDriverUnlock(priv);
3846
    return rv;
R
Richard W.M. Jones 已提交
3847 3848
}

3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882
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;
}

3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955
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;
}

3956 3957
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
3958
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
3959
remoteNetworkOpen (virConnectPtr conn,
3960
                   virConnectAuthPtr auth,
3961
                   int flags)
3962
{
3963 3964 3965
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
3966
    if (conn->driver &&
3967
        STREQ (conn->driver->name, "remote")) {
3968 3969 3970
        struct private_data *priv;

       /* If we're here, the remote driver is already
3971 3972 3973
         * in use due to a) a QEMU uri, or b) a remote
         * URI. So we can re-use existing connection
         */
3974 3975 3976 3977 3978
        priv = conn->privateData;
        remoteDriverLock(priv);
        priv->localUses++;
        conn->networkPrivateData = priv;
        remoteDriverUnlock(priv);
3979
        return VIR_DRV_OPEN_SUCCESS;
3980 3981 3982
    } else {
        /* Using a non-remote driver, so we need to open a
         * new connection for network APIs, forcing it to
3983 3984
         * use the UNIX transport. This handles Xen driver
         * which doesn't have its own impl of the network APIs.
3985
         */
3986
        struct private_data *priv;
3987 3988 3989 3990 3991 3992
        int ret;
        ret = remoteOpenSecondaryDriver(conn,
                                        auth,
                                        flags,
                                        &priv);
        if (ret == VIR_DRV_OPEN_SUCCESS)
3993 3994 3995
            conn->networkPrivateData = priv;
        return ret;
    }
3996 3997 3998
}

static int
3999 4000
remoteNetworkClose (virConnectPtr conn)
{
4001
    int rv = 0;
4002 4003
    struct private_data *priv = conn->networkPrivateData;

4004 4005 4006 4007 4008 4009 4010 4011
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        rv = doRemoteClose(conn, priv);
        conn->networkPrivateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
4012
    }
4013 4014
    if (priv)
        remoteDriverUnlock(priv);
4015
    return rv;
4016 4017 4018 4019 4020
}

static int
remoteNumOfNetworks (virConnectPtr conn)
{
4021
    int rv = -1;
4022
    remote_num_of_networks_ret ret;
4023
    struct private_data *priv = conn->networkPrivateData;
4024

4025 4026
    remoteDriverLock(priv);

4027 4028 4029 4030
    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)
4031 4032 4033
        goto done;

    rv = ret.num;
4034

4035
done:
4036
    remoteDriverUnlock(priv);
4037
    return rv;
4038 4039 4040 4041 4042
}

static int
remoteListNetworks (virConnectPtr conn, char **const names, int maxnames)
{
4043
    int rv = -1;
4044 4045 4046
    int i;
    remote_list_networks_args args;
    remote_list_networks_ret ret;
4047
    struct private_data *priv = conn->networkPrivateData;
4048

4049 4050
    remoteDriverLock(priv);

4051
    if (maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
4052 4053 4054
        remoteError(VIR_ERR_RPC,
                    _("too many remote networks: %d > %d"),
                    maxnames, REMOTE_NETWORK_NAME_LIST_MAX);
4055
        goto done;
4056 4057 4058 4059 4060 4061 4062
    }
    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)
4063
        goto done;
4064 4065

    if (ret.names.names_len > maxnames) {
4066 4067 4068
        remoteError(VIR_ERR_RPC,
                    _("too many remote networks: %d > %d"),
                    ret.names.names_len, maxnames);
4069
        goto cleanup;
4070 4071 4072 4073 4074 4075 4076
    }

    /* 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.
     */
4077
    for (i = 0; i < ret.names.names_len; ++i) {
4078 4079
        names[i] = strdup (ret.names.names_val[i]);

4080 4081 4082 4083
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

4084
            virReportOOMError();
4085 4086 4087 4088
            goto cleanup;
        }
    }

4089 4090 4091
    rv = ret.names.names_len;

cleanup:
4092 4093
    xdr_free ((xdrproc_t) xdr_remote_list_networks_ret, (char *) &ret);

4094
done:
4095
    remoteDriverUnlock(priv);
4096
    return rv;
4097 4098 4099 4100 4101
}

static int
remoteNumOfDefinedNetworks (virConnectPtr conn)
{
4102
    int rv = -1;
4103
    remote_num_of_defined_networks_ret ret;
4104
    struct private_data *priv = conn->networkPrivateData;
4105

4106 4107
    remoteDriverLock(priv);

4108 4109 4110 4111
    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)
4112 4113 4114
        goto done;

    rv = ret.num;
4115

4116
done:
4117
    remoteDriverUnlock(priv);
4118
    return rv;
4119 4120 4121 4122 4123 4124
}

static int
remoteListDefinedNetworks (virConnectPtr conn,
                           char **const names, int maxnames)
{
4125
    int rv = -1;
4126 4127 4128
    int i;
    remote_list_defined_networks_args args;
    remote_list_defined_networks_ret ret;
4129
    struct private_data *priv = conn->networkPrivateData;
4130

4131 4132
    remoteDriverLock(priv);

4133
    if (maxnames > REMOTE_NETWORK_NAME_LIST_MAX) {
4134 4135 4136
        remoteError(VIR_ERR_RPC,
                    _("too many remote networks: %d > %d"),
                    maxnames, REMOTE_NETWORK_NAME_LIST_MAX);
4137
        goto done;
4138 4139 4140 4141 4142 4143 4144
    }
    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)
4145
        goto done;
4146 4147

    if (ret.names.names_len > maxnames) {
4148 4149 4150
        remoteError(VIR_ERR_RPC,
                    _("too many remote networks: %d > %d"),
                    ret.names.names_len, maxnames);
4151
        goto cleanup;
4152 4153 4154 4155 4156 4157 4158
    }

    /* 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.
     */
4159
    for (i = 0; i < ret.names.names_len; ++i) {
4160 4161
        names[i] = strdup (ret.names.names_val[i]);

4162 4163 4164 4165
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

4166
            virReportOOMError();
4167 4168 4169 4170
            goto cleanup;
        }
    }

4171 4172 4173
    rv = ret.names.names_len;

cleanup:
4174 4175
    xdr_free ((xdrproc_t) xdr_remote_list_defined_networks_ret, (char *) &ret);

4176
done:
4177
    remoteDriverUnlock(priv);
4178
    return rv;
4179 4180 4181 4182 4183 4184
}

static virNetworkPtr
remoteNetworkLookupByUUID (virConnectPtr conn,
                           const unsigned char *uuid)
{
4185
    virNetworkPtr net = NULL;
4186 4187
    remote_network_lookup_by_uuid_args args;
    remote_network_lookup_by_uuid_ret ret;
4188
    struct private_data *priv = conn->networkPrivateData;
4189

4190 4191
    remoteDriverLock(priv);

4192 4193 4194 4195 4196 4197
    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)
4198
        goto done;
4199 4200 4201 4202

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

4203
done:
4204
    remoteDriverUnlock(priv);
4205 4206 4207 4208 4209 4210 4211
    return net;
}

static virNetworkPtr
remoteNetworkLookupByName (virConnectPtr conn,
                           const char *name)
{
4212
    virNetworkPtr net = NULL;
4213 4214
    remote_network_lookup_by_name_args args;
    remote_network_lookup_by_name_ret ret;
4215
    struct private_data *priv = conn->networkPrivateData;
4216

4217 4218
    remoteDriverLock(priv);

4219 4220 4221 4222 4223 4224
    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)
4225
        goto done;
4226 4227 4228 4229

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

4230
done:
4231
    remoteDriverUnlock(priv);
4232 4233 4234
    return net;
}

4235 4236 4237 4238 4239 4240
static int
remoteNetworkIsActive(virNetworkPtr network)
{
    int rv = -1;
    remote_network_is_active_args args;
    remote_network_is_active_ret ret;
4241
    struct private_data *priv = network->conn->networkPrivateData;
4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264

    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;
4265
    struct private_data *priv = network->conn->networkPrivateData;
4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282

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

4283 4284 4285
static virNetworkPtr
remoteNetworkCreateXML (virConnectPtr conn, const char *xmlDesc)
{
4286
    virNetworkPtr net = NULL;
4287 4288
    remote_network_create_xml_args args;
    remote_network_create_xml_ret ret;
4289
    struct private_data *priv = conn->networkPrivateData;
4290

4291 4292
    remoteDriverLock(priv);

4293 4294 4295 4296 4297 4298
    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)
4299
        goto done;
4300 4301 4302 4303

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

4304
done:
4305
    remoteDriverUnlock(priv);
4306 4307 4308 4309 4310 4311
    return net;
}

static virNetworkPtr
remoteNetworkDefineXML (virConnectPtr conn, const char *xml)
{
4312
    virNetworkPtr net = NULL;
4313 4314
    remote_network_define_xml_args args;
    remote_network_define_xml_ret ret;
4315
    struct private_data *priv = conn->networkPrivateData;
4316

4317 4318
    remoteDriverLock(priv);

4319 4320 4321 4322 4323 4324
    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)
4325
        goto done;
4326 4327 4328 4329

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

4330
done:
4331
    remoteDriverUnlock(priv);
4332 4333 4334 4335 4336 4337
    return net;
}

static int
remoteNetworkUndefine (virNetworkPtr network)
{
4338
    int rv = -1;
4339
    remote_network_undefine_args args;
4340
    struct private_data *priv = network->conn->networkPrivateData;
4341

4342 4343
    remoteDriverLock(priv);

4344 4345 4346 4347 4348
    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)
4349
        goto done;
4350

4351 4352 4353
    rv = 0;

done:
4354
    remoteDriverUnlock(priv);
4355
    return rv;
4356 4357 4358 4359 4360
}

static int
remoteNetworkCreate (virNetworkPtr network)
{
4361
    int rv = -1;
4362
    remote_network_create_args args;
4363
    struct private_data *priv = network->conn->networkPrivateData;
4364

4365 4366
    remoteDriverLock(priv);

4367 4368 4369 4370 4371
    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)
4372
        goto done;
4373

4374 4375 4376
    rv = 0;

done:
4377
    remoteDriverUnlock(priv);
4378
    return rv;
4379 4380 4381 4382 4383
}

static int
remoteNetworkDestroy (virNetworkPtr network)
{
4384
    int rv = -1;
4385
    remote_network_destroy_args args;
4386
    struct private_data *priv = network->conn->networkPrivateData;
4387

4388 4389
    remoteDriverLock(priv);

4390 4391 4392 4393 4394
    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)
4395
        goto done;
4396

4397 4398 4399
    rv = 0;

done:
4400
    remoteDriverUnlock(priv);
4401
    return rv;
4402 4403 4404 4405 4406
}

static char *
remoteNetworkDumpXML (virNetworkPtr network, int flags)
{
4407
    char *rv = NULL;
4408 4409
    remote_network_dump_xml_args args;
    remote_network_dump_xml_ret ret;
4410
    struct private_data *priv = network->conn->networkPrivateData;
4411

4412 4413
    remoteDriverLock(priv);

4414 4415 4416 4417 4418 4419 4420
    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)
4421
        goto done;
4422 4423

    /* Caller frees. */
4424 4425 4426
    rv = ret.xml;

done:
4427
    remoteDriverUnlock(priv);
4428
    return rv;
4429 4430 4431 4432 4433
}

static char *
remoteNetworkGetBridgeName (virNetworkPtr network)
{
4434
    char *rv = NULL;
4435 4436
    remote_network_get_bridge_name_args args;
    remote_network_get_bridge_name_ret ret;
4437
    struct private_data *priv = network->conn->networkPrivateData;
4438

4439 4440
    remoteDriverLock(priv);

4441 4442 4443 4444 4445 4446
    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)
4447
        goto done;
4448 4449

    /* Caller frees. */
4450 4451 4452
    rv = ret.name;

done:
4453
    remoteDriverUnlock(priv);
4454
    return rv;
4455 4456 4457 4458 4459
}

static int
remoteNetworkGetAutostart (virNetworkPtr network, int *autostart)
{
4460
    int rv = -1;
4461 4462
    remote_network_get_autostart_args args;
    remote_network_get_autostart_ret ret;
4463
    struct private_data *priv = network->conn->networkPrivateData;
4464

4465 4466
    remoteDriverLock(priv);

4467 4468 4469 4470 4471 4472
    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)
4473
        goto done;
4474 4475 4476

    if (autostart) *autostart = ret.autostart;

4477 4478 4479
    rv = 0;

done:
4480
    remoteDriverUnlock(priv);
4481
    return rv;
4482 4483 4484 4485 4486
}

static int
remoteNetworkSetAutostart (virNetworkPtr network, int autostart)
{
4487
    int rv = -1;
4488
    remote_network_set_autostart_args args;
4489
    struct private_data *priv = network->conn->networkPrivateData;
4490

4491 4492
    remoteDriverLock(priv);

4493 4494 4495 4496 4497 4498
    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)
4499
        goto done;
4500

4501 4502 4503
    rv = 0;

done:
4504
    remoteDriverUnlock(priv);
4505
    return rv;
4506 4507
}

4508 4509 4510



D
Daniel Veillard 已提交
4511 4512
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
4513
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
D
Daniel Veillard 已提交
4514
remoteInterfaceOpen (virConnectPtr conn,
J
Jim Meyering 已提交
4515 4516
                     virConnectAuthPtr auth,
                     int flags)
D
Daniel Veillard 已提交
4517 4518 4519 4520
{
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
4521
    if (conn->driver &&
D
Daniel Veillard 已提交
4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606
        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) {
4607 4608 4609
        remoteError(VIR_ERR_RPC,
                    _("too many remote interfaces: %d > %d"),
                    maxnames, REMOTE_INTERFACE_NAME_LIST_MAX);
D
Daniel Veillard 已提交
4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620
        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) {
4621 4622 4623
        remoteError(VIR_ERR_RPC,
                    _("too many remote interfaces: %d > %d"),
                    ret.names.names_len, maxnames);
D
Daniel Veillard 已提交
4624 4625 4626 4627 4628 4629 4630 4631
        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.
     */
4632
    for (i = 0; i < ret.names.names_len; ++i) {
D
Daniel Veillard 已提交
4633 4634
        names[i] = strdup (ret.names.names_val[i]);

4635 4636 4637 4638
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

4639
            virReportOOMError();
4640 4641 4642 4643
            goto cleanup;
        }
    }

D
Daniel Veillard 已提交
4644 4645 4646 4647 4648 4649 4650 4651 4652 4653
    rv = ret.names.names_len;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687
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) {
4688 4689 4690
        remoteError(VIR_ERR_RPC,
                    _("too many remote interfaces: %d > %d"),
                    maxnames, REMOTE_DEFINED_INTERFACE_NAME_LIST_MAX);
4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701
        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) {
4702 4703 4704
        remoteError(VIR_ERR_RPC,
                    _("too many remote interfaces: %d > %d"),
                    ret.names.names_len, maxnames);
4705 4706 4707 4708 4709 4710 4711 4712
        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.
     */
4713
    for (i = 0; i < ret.names.names_len; ++i) {
4714 4715
        names[i] = strdup (ret.names.names_val[i]);

4716 4717 4718 4719
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

4720
            virReportOOMError();
4721 4722 4723 4724
            goto cleanup;
        }
    }

4725 4726 4727 4728 4729 4730 4731 4732 4733 4734
    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 已提交
4735 4736 4737 4738
static virInterfacePtr
remoteInterfaceLookupByName (virConnectPtr conn,
                             const char *name)
{
4739
    virInterfacePtr iface = NULL;
D
Daniel Veillard 已提交
4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753
    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;

4754
    iface = get_nonnull_interface (conn, ret.iface);
D
Daniel Veillard 已提交
4755 4756 4757 4758
    xdr_free ((xdrproc_t) &xdr_remote_interface_lookup_by_name_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
4759
    return iface;
D
Daniel Veillard 已提交
4760 4761 4762 4763 4764 4765
}

static virInterfacePtr
remoteInterfaceLookupByMACString (virConnectPtr conn,
                                  const char *mac)
{
4766
    virInterfacePtr iface = NULL;
D
Daniel Veillard 已提交
4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780
    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;

4781
    iface = get_nonnull_interface (conn, ret.iface);
D
Daniel Veillard 已提交
4782 4783 4784 4785
    xdr_free ((xdrproc_t) &xdr_remote_interface_lookup_by_mac_string_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
4786
    return iface;
D
Daniel Veillard 已提交
4787 4788
}

4789 4790 4791 4792 4793 4794 4795

static int
remoteInterfaceIsActive(virInterfacePtr iface)
{
    int rv = -1;
    remote_interface_is_active_args args;
    remote_interface_is_active_ret ret;
4796
    struct private_data *priv = iface->conn->interfacePrivateData;
4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814

    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 已提交
4815
static char *
4816
remoteInterfaceGetXMLDesc (virInterfacePtr iface,
D
Daniel Veillard 已提交
4817 4818 4819 4820 4821
                           unsigned int flags)
{
    char *rv = NULL;
    remote_interface_get_xml_desc_args args;
    remote_interface_get_xml_desc_ret ret;
4822
    struct private_data *priv = iface->conn->interfacePrivateData;
D
Daniel Veillard 已提交
4823 4824 4825

    remoteDriverLock(priv);

4826
    make_nonnull_interface (&args.iface, iface);
D
Daniel Veillard 已提交
4827 4828 4829
    args.flags = flags;

    memset (&ret, 0, sizeof ret);
4830
    if (call (iface->conn, priv, 0, REMOTE_PROC_INTERFACE_GET_XML_DESC,
D
Daniel Veillard 已提交
4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847
              (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)
{
4848
    virInterfacePtr iface = NULL;
D
Daniel Veillard 已提交
4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863
    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;

4864
    iface = get_nonnull_interface (conn, ret.iface);
D
Daniel Veillard 已提交
4865 4866 4867 4868
    xdr_free ((xdrproc_t) &xdr_remote_interface_define_xml_ret, (char *) &ret);

done:
    remoteDriverUnlock(priv);
4869
    return iface;
D
Daniel Veillard 已提交
4870 4871 4872
}

static int
4873
remoteInterfaceUndefine (virInterfacePtr iface)
D
Daniel Veillard 已提交
4874 4875 4876
{
    int rv = -1;
    remote_interface_undefine_args args;
4877
    struct private_data *priv = iface->conn->interfacePrivateData;
D
Daniel Veillard 已提交
4878 4879 4880

    remoteDriverLock(priv);

4881
    make_nonnull_interface (&args.iface, iface);
D
Daniel Veillard 已提交
4882

4883
    if (call (iface->conn, priv, 0, REMOTE_PROC_INTERFACE_UNDEFINE,
D
Daniel Veillard 已提交
4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895
              (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
4896
remoteInterfaceCreate (virInterfacePtr iface,
D
Daniel Veillard 已提交
4897 4898 4899 4900
                       unsigned int flags)
{
    int rv = -1;
    remote_interface_create_args args;
4901
    struct private_data *priv = iface->conn->interfacePrivateData;
D
Daniel Veillard 已提交
4902 4903 4904

    remoteDriverLock(priv);

4905
    make_nonnull_interface (&args.iface, iface);
D
Daniel Veillard 已提交
4906 4907
    args.flags = flags;

4908
    if (call (iface->conn, priv, 0, REMOTE_PROC_INTERFACE_CREATE,
D
Daniel Veillard 已提交
4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920
              (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
4921
remoteInterfaceDestroy (virInterfacePtr iface,
D
Daniel Veillard 已提交
4922 4923 4924 4925
                        unsigned int flags)
{
    int rv = -1;
    remote_interface_destroy_args args;
4926
    struct private_data *priv = iface->conn->interfacePrivateData;
D
Daniel Veillard 已提交
4927 4928 4929

    remoteDriverLock(priv);

4930
    make_nonnull_interface (&args.iface, iface);
D
Daniel Veillard 已提交
4931 4932
    args.flags = flags;

4933
    if (call (iface->conn, priv, 0, REMOTE_PROC_INTERFACE_DESTROY,
D
Daniel Veillard 已提交
4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944
              (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;
}

4945 4946
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
4947
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
4948 4949 4950 4951 4952 4953 4954
remoteStorageOpen (virConnectPtr conn,
                   virConnectAuthPtr auth,
                   int flags)
{
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
4955
    if (conn->driver &&
4956
        STREQ (conn->driver->name, "remote")) {
4957
        struct private_data *priv = conn->privateData;
4958 4959 4960 4961
        /* 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
         */
4962 4963 4964 4965
        remoteDriverLock(priv);
        priv->localUses++;
        conn->storagePrivateData = priv;
        remoteDriverUnlock(priv);
4966 4967 4968
        return VIR_DRV_OPEN_SUCCESS;
    } else if (conn->networkDriver &&
               STREQ (conn->networkDriver->name, "remote")) {
4969 4970 4971 4972 4973
        struct private_data *priv = conn->networkPrivateData;
        remoteDriverLock(priv);
        conn->storagePrivateData = priv;
        priv->localUses++;
        remoteDriverUnlock(priv);
4974 4975 4976 4977 4978 4979 4980
        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.
         */
4981
        struct private_data *priv;
4982 4983 4984 4985 4986 4987
        int ret;
        ret = remoteOpenSecondaryDriver(conn,
                                        auth,
                                        flags,
                                        &priv);
        if (ret == VIR_DRV_OPEN_SUCCESS)
4988 4989 4990 4991 4992 4993 4994 4995 4996
            conn->storagePrivateData = priv;
        return ret;
    }
}

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

4999 5000 5001 5002 5003 5004 5005 5006
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        ret = doRemoteClose(conn, priv);
        conn->storagePrivateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
5007
    }
5008 5009
    if (priv)
        remoteDriverUnlock(priv);
5010

5011 5012 5013 5014 5015 5016
    return ret;
}

static int
remoteNumOfStoragePools (virConnectPtr conn)
{
5017
    int rv = -1;
5018
    remote_num_of_storage_pools_ret ret;
5019
    struct private_data *priv = conn->storagePrivateData;
5020

5021 5022
    remoteDriverLock(priv);

5023 5024 5025 5026
    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)
5027 5028 5029
        goto done;

    rv = ret.num;
5030

5031
done:
5032
    remoteDriverUnlock(priv);
5033
    return rv;
5034 5035 5036 5037 5038
}

static int
remoteListStoragePools (virConnectPtr conn, char **const names, int maxnames)
{
5039
    int rv = -1;
5040 5041 5042
    int i;
    remote_list_storage_pools_args args;
    remote_list_storage_pools_ret ret;
5043
    struct private_data *priv = conn->storagePrivateData;
5044

5045 5046
    remoteDriverLock(priv);

5047
    if (maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
5048
        remoteError(VIR_ERR_RPC, "%s", _("too many storage pools requested"));
5049
        goto done;
5050 5051 5052 5053 5054 5055 5056
    }
    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)
5057
        goto done;
5058 5059

    if (ret.names.names_len > maxnames) {
5060
        remoteError(VIR_ERR_RPC, "%s", _("too many storage pools received"));
5061
        goto cleanup;
5062 5063 5064 5065 5066 5067 5068
    }

    /* 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.
     */
5069
    for (i = 0; i < ret.names.names_len; ++i) {
5070 5071
        names[i] = strdup (ret.names.names_val[i]);

5072 5073 5074 5075
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

5076
            virReportOOMError();
5077 5078 5079 5080
            goto cleanup;
        }
    }

5081 5082 5083
    rv = ret.names.names_len;

cleanup:
5084 5085
    xdr_free ((xdrproc_t) xdr_remote_list_storage_pools_ret, (char *) &ret);

5086
done:
5087
    remoteDriverUnlock(priv);
5088
    return rv;
5089 5090 5091 5092 5093
}

static int
remoteNumOfDefinedStoragePools (virConnectPtr conn)
{
5094
    int rv = -1;
5095
    remote_num_of_defined_storage_pools_ret ret;
5096
    struct private_data *priv = conn->storagePrivateData;
5097

5098 5099
    remoteDriverLock(priv);

5100 5101 5102 5103
    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)
5104
        goto done;
5105

5106 5107 5108
    rv = ret.num;

done:
5109
    remoteDriverUnlock(priv);
5110
    return rv;
5111 5112 5113 5114 5115 5116
}

static int
remoteListDefinedStoragePools (virConnectPtr conn,
                               char **const names, int maxnames)
{
5117
    int rv = -1;
5118 5119 5120
    int i;
    remote_list_defined_storage_pools_args args;
    remote_list_defined_storage_pools_ret ret;
5121
    struct private_data *priv = conn->storagePrivateData;
5122

5123 5124
    remoteDriverLock(priv);

5125
    if (maxnames > REMOTE_STORAGE_POOL_NAME_LIST_MAX) {
5126
        remoteError(VIR_ERR_RPC, "%s", _("too many storage pools requested"));
5127
        goto done;
5128 5129 5130 5131 5132 5133 5134
    }
    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)
5135
        goto done;
5136 5137

    if (ret.names.names_len > maxnames) {
5138
        remoteError(VIR_ERR_RPC, "%s", _("too many storage pools received"));
5139
        goto cleanup;
5140 5141 5142 5143 5144 5145 5146
    }

    /* 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.
     */
5147
    for (i = 0; i < ret.names.names_len; ++i) {
5148 5149
        names[i] = strdup (ret.names.names_val[i]);

5150 5151 5152 5153
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

5154
            virReportOOMError();
5155 5156 5157 5158
            goto cleanup;
        }
    }

5159 5160 5161
    rv = ret.names.names_len;

cleanup:
5162 5163
    xdr_free ((xdrproc_t) xdr_remote_list_defined_storage_pools_ret, (char *) &ret);

5164
done:
5165
    remoteDriverUnlock(priv);
5166
    return rv;
5167 5168
}

5169 5170 5171 5172 5173 5174
static char *
remoteFindStoragePoolSources (virConnectPtr conn,
                              const char *type,
                              const char *srcSpec,
                              unsigned int flags)
{
5175
    char *rv = NULL;
5176 5177
    remote_find_storage_pool_sources_args args;
    remote_find_storage_pool_sources_ret ret;
5178
    struct private_data *priv = conn->storagePrivateData;
5179 5180
    const char *emptyString = "";

5181 5182
    remoteDriverLock(priv);

5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201
    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)
5202
        goto done;
5203

5204
    rv = ret.xml;
5205 5206 5207 5208
    ret.xml = NULL; /* To stop xdr_free free'ing it */

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

5209
done:
5210
    remoteDriverUnlock(priv);
5211
    return rv;
5212 5213
}

5214 5215 5216 5217
static virStoragePoolPtr
remoteStoragePoolLookupByUUID (virConnectPtr conn,
                               const unsigned char *uuid)
{
5218
    virStoragePoolPtr pool = NULL;
5219 5220
    remote_storage_pool_lookup_by_uuid_args args;
    remote_storage_pool_lookup_by_uuid_ret ret;
5221
    struct private_data *priv = conn->storagePrivateData;
5222

5223 5224
    remoteDriverLock(priv);

5225 5226 5227 5228 5229 5230
    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)
5231
        goto done;
5232 5233 5234 5235

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

5236
done:
5237
    remoteDriverUnlock(priv);
5238 5239 5240 5241 5242 5243 5244
    return pool;
}

static virStoragePoolPtr
remoteStoragePoolLookupByName (virConnectPtr conn,
                               const char *name)
{
5245
    virStoragePoolPtr pool = NULL;
5246 5247
    remote_storage_pool_lookup_by_name_args args;
    remote_storage_pool_lookup_by_name_ret ret;
5248
    struct private_data *priv = conn->storagePrivateData;
5249

5250 5251
    remoteDriverLock(priv);

5252 5253 5254 5255 5256 5257
    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)
5258
        goto done;
5259 5260 5261 5262

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

5263
done:
5264
    remoteDriverUnlock(priv);
5265 5266 5267 5268 5269 5270
    return pool;
}

static virStoragePoolPtr
remoteStoragePoolLookupByVolume (virStorageVolPtr vol)
{
5271
    virStoragePoolPtr pool = NULL;
5272 5273
    remote_storage_pool_lookup_by_volume_args args;
    remote_storage_pool_lookup_by_volume_ret ret;
5274
    struct private_data *priv = vol->conn->storagePrivateData;
5275

5276 5277
    remoteDriverLock(priv);

5278 5279 5280 5281 5282 5283
    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)
5284
        goto done;
5285 5286 5287 5288

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

5289
done:
5290
    remoteDriverUnlock(priv);
5291 5292 5293 5294
    return pool;
}


5295 5296 5297 5298 5299 5300
static int
remoteStoragePoolIsActive(virStoragePoolPtr pool)
{
    int rv = -1;
    remote_storage_pool_is_active_args args;
    remote_storage_pool_is_active_ret ret;
5301
    struct private_data *priv = pool->conn->storagePrivateData;
5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324

    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;
5325
    struct private_data *priv = pool->conn->storagePrivateData;
5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343

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


5344 5345 5346
static virStoragePoolPtr
remoteStoragePoolCreateXML (virConnectPtr conn, const char *xmlDesc, unsigned int flags)
{
5347
    virStoragePoolPtr pool = NULL;
5348 5349
    remote_storage_pool_create_xml_args args;
    remote_storage_pool_create_xml_ret ret;
5350
    struct private_data *priv = conn->storagePrivateData;
5351

5352 5353
    remoteDriverLock(priv);

5354 5355 5356 5357 5358 5359 5360
    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)
5361
        goto done;
5362 5363 5364 5365

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

5366
done:
5367
    remoteDriverUnlock(priv);
5368 5369 5370 5371 5372 5373
    return pool;
}

static virStoragePoolPtr
remoteStoragePoolDefineXML (virConnectPtr conn, const char *xml, unsigned int flags)
{
5374
    virStoragePoolPtr pool = NULL;
5375 5376
    remote_storage_pool_define_xml_args args;
    remote_storage_pool_define_xml_ret ret;
5377
    struct private_data *priv = conn->storagePrivateData;
5378

5379 5380
    remoteDriverLock(priv);

5381 5382 5383 5384 5385 5386 5387
    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)
5388
        goto done;
5389 5390 5391 5392

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

5393
done:
5394
    remoteDriverUnlock(priv);
5395 5396 5397 5398 5399 5400
    return pool;
}

static int
remoteStoragePoolUndefine (virStoragePoolPtr pool)
{
5401
    int rv = -1;
5402
    remote_storage_pool_undefine_args args;
5403
    struct private_data *priv = pool->conn->storagePrivateData;
5404

5405 5406
    remoteDriverLock(priv);

5407 5408 5409 5410 5411
    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)
5412
        goto done;
5413

5414 5415 5416
    rv = 0;

done:
5417
    remoteDriverUnlock(priv);
5418
    return rv;
5419 5420 5421 5422 5423
}

static int
remoteStoragePoolCreate (virStoragePoolPtr pool, unsigned int flags)
{
5424
    int rv = -1;
5425
    remote_storage_pool_create_args args;
5426
    struct private_data *priv = pool->conn->storagePrivateData;
5427

5428 5429
    remoteDriverLock(priv);

5430 5431 5432 5433 5434 5435
    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)
5436
        goto done;
5437

5438 5439 5440
    rv = 0;

done:
5441
    remoteDriverUnlock(priv);
5442
    return rv;
5443 5444 5445 5446 5447 5448
}

static int
remoteStoragePoolBuild (virStoragePoolPtr pool,
                        unsigned int flags)
{
5449
    int rv = -1;
5450
    remote_storage_pool_build_args args;
5451
    struct private_data *priv = pool->conn->storagePrivateData;
5452

5453 5454
    remoteDriverLock(priv);

5455 5456 5457 5458 5459 5460
    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)
5461
        goto done;
5462

5463 5464 5465
    rv = 0;

done:
5466
    remoteDriverUnlock(priv);
5467
    return rv;
5468 5469 5470 5471 5472
}

static int
remoteStoragePoolDestroy (virStoragePoolPtr pool)
{
5473
    int rv = -1;
5474
    remote_storage_pool_destroy_args args;
5475
    struct private_data *priv = pool->conn->storagePrivateData;
5476

5477 5478
    remoteDriverLock(priv);

5479 5480 5481 5482 5483
    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)
5484
        goto done;
5485

5486 5487 5488
    rv = 0;

done:
5489
    remoteDriverUnlock(priv);
5490
    return rv;
5491 5492 5493 5494 5495 5496
}

static int
remoteStoragePoolDelete (virStoragePoolPtr pool,
                         unsigned int flags)
{
5497
    int rv = -1;
5498
    remote_storage_pool_delete_args args;
5499
    struct private_data *priv = pool->conn->storagePrivateData;
5500

5501 5502
    remoteDriverLock(priv);

5503 5504 5505 5506 5507 5508
    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)
5509
        goto done;
5510

5511 5512 5513
    rv = 0;

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

static int
remoteStoragePoolRefresh (virStoragePoolPtr pool,
                          unsigned int flags)
{
5522
    int rv = -1;
5523
    remote_storage_pool_refresh_args args;
5524
    struct private_data *priv = pool->conn->storagePrivateData;
5525

5526 5527
    remoteDriverLock(priv);

5528 5529 5530 5531 5532 5533
    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)
5534
        goto done;
5535

5536 5537 5538
    rv = 0;

done:
5539
    remoteDriverUnlock(priv);
5540
    return rv;
5541 5542 5543 5544 5545
}

static int
remoteStoragePoolGetInfo (virStoragePoolPtr pool, virStoragePoolInfoPtr info)
{
5546
    int rv = -1;
5547 5548
    remote_storage_pool_get_info_args args;
    remote_storage_pool_get_info_ret ret;
5549
    struct private_data *priv = pool->conn->storagePrivateData;
5550

5551 5552
    remoteDriverLock(priv);

5553 5554 5555 5556 5557 5558
    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)
5559
        goto done;
5560 5561 5562 5563 5564 5565

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

5566 5567 5568
    rv = 0;

done:
5569
    remoteDriverUnlock(priv);
5570
    return rv;
5571 5572 5573 5574 5575 5576
}

static char *
remoteStoragePoolDumpXML (virStoragePoolPtr pool,
                          unsigned int flags)
{
5577
    char *rv = NULL;
5578 5579
    remote_storage_pool_dump_xml_args args;
    remote_storage_pool_dump_xml_ret ret;
5580
    struct private_data *priv = pool->conn->storagePrivateData;
5581

5582 5583
    remoteDriverLock(priv);

5584 5585 5586 5587 5588 5589 5590
    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)
5591
        goto done;
5592 5593

    /* Caller frees. */
5594 5595 5596
    rv = ret.xml;

done:
5597
    remoteDriverUnlock(priv);
5598
    return rv;
5599 5600 5601 5602 5603
}

static int
remoteStoragePoolGetAutostart (virStoragePoolPtr pool, int *autostart)
{
5604
    int rv = -1;
5605 5606
    remote_storage_pool_get_autostart_args args;
    remote_storage_pool_get_autostart_ret ret;
5607
    struct private_data *priv = pool->conn->storagePrivateData;
5608

5609 5610
    remoteDriverLock(priv);

5611 5612 5613 5614 5615 5616
    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)
5617
        goto done;
5618 5619 5620

    if (autostart) *autostart = ret.autostart;

5621 5622 5623
    rv = 0;

done:
5624
    remoteDriverUnlock(priv);
5625
    return rv;
5626 5627 5628 5629 5630
}

static int
remoteStoragePoolSetAutostart (virStoragePoolPtr pool, int autostart)
{
5631
    int rv = -1;
5632
    remote_storage_pool_set_autostart_args args;
5633
    struct private_data *priv = pool->conn->storagePrivateData;
5634

5635 5636
    remoteDriverLock(priv);

5637 5638 5639 5640 5641 5642
    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)
5643
        goto done;
5644

5645 5646 5647
    rv = 0;

done:
5648
    remoteDriverUnlock(priv);
5649
    return rv;
5650 5651 5652 5653 5654 5655
}


static int
remoteStoragePoolNumOfVolumes (virStoragePoolPtr pool)
{
5656
    int rv = -1;
5657 5658
    remote_storage_pool_num_of_volumes_args args;
    remote_storage_pool_num_of_volumes_ret ret;
5659
    struct private_data *priv = pool->conn->storagePrivateData;
5660

5661 5662
    remoteDriverLock(priv);

5663 5664 5665 5666 5667 5668
    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)
5669 5670 5671
        goto done;

    rv = ret.num;
5672

5673
done:
5674
    remoteDriverUnlock(priv);
5675
    return rv;
5676 5677 5678 5679 5680
}

static int
remoteStoragePoolListVolumes (virStoragePoolPtr pool, char **const names, int maxnames)
{
5681
    int rv = -1;
5682 5683 5684
    int i;
    remote_storage_pool_list_volumes_args args;
    remote_storage_pool_list_volumes_ret ret;
5685
    struct private_data *priv = pool->conn->storagePrivateData;
5686

5687 5688
    remoteDriverLock(priv);

5689
    if (maxnames > REMOTE_STORAGE_VOL_NAME_LIST_MAX) {
5690
        remoteError(VIR_ERR_RPC, "%s", _("too many storage volumes requested"));
5691
        goto done;
5692 5693 5694 5695 5696 5697 5698 5699
    }
    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)
5700
        goto done;
5701 5702

    if (ret.names.names_len > maxnames) {
5703
        remoteError(VIR_ERR_RPC, "%s", _("too many storage volumes received"));
5704
        goto cleanup;
5705 5706 5707 5708 5709 5710 5711
    }

    /* 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.
     */
5712
    for (i = 0; i < ret.names.names_len; ++i) {
5713 5714
        names[i] = strdup (ret.names.names_val[i]);

5715 5716 5717 5718
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

5719
            virReportOOMError();
5720 5721 5722 5723
            goto cleanup;
        }
    }

5724 5725 5726
    rv = ret.names.names_len;

cleanup:
5727 5728
    xdr_free ((xdrproc_t) xdr_remote_storage_pool_list_volumes_ret, (char *) &ret);

5729
done:
5730
    remoteDriverUnlock(priv);
5731
    return rv;
5732 5733 5734 5735 5736 5737 5738 5739
}



static virStorageVolPtr
remoteStorageVolLookupByName (virStoragePoolPtr pool,
                              const char *name)
{
5740
    virStorageVolPtr vol = NULL;
5741 5742
    remote_storage_vol_lookup_by_name_args args;
    remote_storage_vol_lookup_by_name_ret ret;
5743
    struct private_data *priv = pool->conn->storagePrivateData;
5744

5745 5746
    remoteDriverLock(priv);

5747 5748 5749 5750 5751 5752 5753
    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)
5754
        goto done;
5755 5756 5757 5758

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

5759
done:
5760
    remoteDriverUnlock(priv);
5761 5762 5763 5764 5765 5766 5767
    return vol;
}

static virStorageVolPtr
remoteStorageVolLookupByKey (virConnectPtr conn,
                             const char *key)
{
5768
    virStorageVolPtr  vol = NULL;
5769 5770
    remote_storage_vol_lookup_by_key_args args;
    remote_storage_vol_lookup_by_key_ret ret;
5771
    struct private_data *priv = conn->storagePrivateData;
5772

5773 5774
    remoteDriverLock(priv);

5775 5776 5777 5778 5779 5780
    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)
5781
        goto done;
5782 5783 5784 5785

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

5786
done:
5787
    remoteDriverUnlock(priv);
5788 5789 5790 5791 5792 5793 5794
    return vol;
}

static virStorageVolPtr
remoteStorageVolLookupByPath (virConnectPtr conn,
                              const char *path)
{
5795
    virStorageVolPtr vol = NULL;
5796 5797
    remote_storage_vol_lookup_by_path_args args;
    remote_storage_vol_lookup_by_path_ret ret;
5798
    struct private_data *priv = conn->storagePrivateData;
5799

5800 5801
    remoteDriverLock(priv);

5802 5803 5804 5805 5806 5807
    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)
5808
        goto done;
5809 5810 5811 5812

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

5813
done:
5814
    remoteDriverUnlock(priv);
5815 5816 5817 5818 5819 5820 5821
    return vol;
}

static virStorageVolPtr
remoteStorageVolCreateXML (virStoragePoolPtr pool, const char *xmlDesc,
                           unsigned int flags)
{
5822
    virStorageVolPtr vol = NULL;
5823 5824
    remote_storage_vol_create_xml_args args;
    remote_storage_vol_create_xml_ret ret;
5825
    struct private_data *priv = pool->conn->storagePrivateData;
5826

5827 5828
    remoteDriverLock(priv);

5829 5830 5831 5832 5833 5834 5835 5836
    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)
5837
        goto done;
5838 5839 5840 5841

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

5842
done:
5843
    remoteDriverUnlock(priv);
5844 5845 5846
    return vol;
}

5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878
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;
}

5879 5880 5881 5882
static int
remoteStorageVolDelete (virStorageVolPtr vol,
                        unsigned int flags)
{
5883
    int rv = -1;
5884
    remote_storage_vol_delete_args args;
5885
    struct private_data *priv = vol->conn->storagePrivateData;
5886

5887 5888
    remoteDriverLock(priv);

5889 5890 5891 5892 5893 5894
    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)
5895
        goto done;
5896

5897 5898 5899
    rv = 0;

done:
5900
    remoteDriverUnlock(priv);
5901
    return rv;
5902 5903
}

5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929
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;
}


5930 5931 5932
static int
remoteStorageVolGetInfo (virStorageVolPtr vol, virStorageVolInfoPtr info)
{
5933
    int rv = -1;
5934 5935
    remote_storage_vol_get_info_args args;
    remote_storage_vol_get_info_ret ret;
5936
    struct private_data *priv = vol->conn->storagePrivateData;
5937

5938 5939
    remoteDriverLock(priv);

5940 5941 5942 5943 5944 5945
    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)
5946
        goto done;
5947 5948 5949 5950 5951

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

5952 5953 5954
    rv = 0;

done:
5955
    remoteDriverUnlock(priv);
5956
    return rv;
5957 5958 5959 5960 5961 5962
}

static char *
remoteStorageVolDumpXML (virStorageVolPtr vol,
                         unsigned int flags)
{
5963
    char *rv = NULL;
5964 5965
    remote_storage_vol_dump_xml_args args;
    remote_storage_vol_dump_xml_ret ret;
5966
    struct private_data *priv = vol->conn->storagePrivateData;
5967

5968 5969
    remoteDriverLock(priv);

5970 5971 5972 5973 5974 5975 5976
    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)
5977
        goto done;
5978 5979

    /* Caller frees. */
5980 5981 5982
    rv = ret.xml;

done:
5983
    remoteDriverUnlock(priv);
5984
    return rv;
5985 5986 5987 5988 5989
}

static char *
remoteStorageVolGetPath (virStorageVolPtr vol)
{
5990
    char *rv = NULL;
5991 5992
    remote_storage_vol_get_path_args args;
    remote_storage_vol_get_path_ret ret;
5993
    struct private_data *priv = vol->conn->storagePrivateData;
5994

5995 5996
    remoteDriverLock(priv);

5997 5998 5999 6000 6001 6002
    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)
6003
        goto done;
6004 6005

    /* Caller frees. */
6006 6007 6008
    rv = ret.name;

done:
6009
    remoteDriverUnlock(priv);
6010
    return rv;
6011 6012 6013
}


6014 6015
/*----------------------------------------------------------------------*/

J
Jim Meyering 已提交
6016
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
6017 6018 6019 6020
remoteDevMonOpen(virConnectPtr conn,
                 virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                 int flags ATTRIBUTE_UNUSED)
{
6021 6022 6023
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
6024
    if (conn->driver &&
6025
        STREQ (conn->driver->name, "remote")) {
6026
        struct private_data *priv = conn->privateData;
6027 6028 6029 6030
        /* 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
         */
6031 6032 6033 6034
        remoteDriverLock(priv);
        priv->localUses++;
        conn->devMonPrivateData = priv;
        remoteDriverUnlock(priv);
6035
        return VIR_DRV_OPEN_SUCCESS;
6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050
    } 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;
6051 6052 6053 6054 6055 6056
        int ret;
        ret = remoteOpenSecondaryDriver(conn,
                                        auth,
                                        flags,
                                        &priv);
        if (ret == VIR_DRV_OPEN_SUCCESS)
6057 6058 6059
            conn->devMonPrivateData = priv;
        return ret;
    }
6060 6061 6062 6063 6064
}

static int remoteDevMonClose(virConnectPtr conn)
{
    int ret = 0;
6065 6066
    struct private_data *priv = conn->devMonPrivateData;

6067 6068 6069 6070 6071 6072 6073 6074
    remoteDriverLock(priv);
    priv->localUses--;
    if (!priv->localUses) {
        ret = doRemoteClose(conn, priv);
        conn->devMonPrivateData = NULL;
        remoteDriverUnlock(priv);
        virMutexDestroy(&priv->lock);
        VIR_FREE(priv);
6075
    }
6076 6077
    if (priv)
        remoteDriverUnlock(priv);
6078 6079 6080 6081 6082 6083 6084
    return ret;
}

static int remoteNodeNumOfDevices(virConnectPtr conn,
                                  const char *cap,
                                  unsigned int flags)
{
6085
    int rv = -1;
6086 6087
    remote_node_num_of_devices_args args;
    remote_node_num_of_devices_ret ret;
6088
    struct private_data *priv = conn->devMonPrivateData;
6089

6090 6091
    remoteDriverLock(priv);

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

    rv = ret.num;
6102

6103
done:
6104
    remoteDriverUnlock(priv);
6105
    return rv;
6106 6107 6108 6109 6110 6111 6112 6113 6114
}


static int remoteNodeListDevices(virConnectPtr conn,
                                 const char *cap,
                                 char **const names,
                                 int maxnames,
                                 unsigned int flags)
{
6115
    int rv = -1;
6116 6117 6118
    int i;
    remote_node_list_devices_args args;
    remote_node_list_devices_ret ret;
6119
    struct private_data *priv = conn->devMonPrivateData;
6120

6121 6122
    remoteDriverLock(priv);

6123
    if (maxnames > REMOTE_NODE_DEVICE_NAME_LIST_MAX) {
6124
        remoteError(VIR_ERR_RPC, "%s", _("too many device names requested"));
6125
        goto done;
6126 6127 6128 6129 6130 6131 6132 6133 6134
    }
    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)
6135
        goto done;
6136 6137

    if (ret.names.names_len > maxnames) {
6138
        remoteError(VIR_ERR_RPC, "%s", _("too many device names received"));
6139
        goto cleanup;
6140 6141 6142 6143 6144 6145 6146
    }

    /* 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.
     */
6147
    for (i = 0; i < ret.names.names_len; ++i) {
6148 6149
        names[i] = strdup (ret.names.names_val[i]);

6150 6151 6152 6153
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

6154
            virReportOOMError();
6155 6156 6157 6158
            goto cleanup;
        }
    }

6159 6160 6161
    rv = ret.names.names_len;

cleanup:
6162 6163
    xdr_free ((xdrproc_t) xdr_remote_node_list_devices_ret, (char *) &ret);

6164
done:
6165
    remoteDriverUnlock(priv);
6166
    return rv;
6167 6168 6169 6170 6171 6172 6173 6174
}


static virNodeDevicePtr remoteNodeDeviceLookupByName(virConnectPtr conn,
                                                     const char *name)
{
    remote_node_device_lookup_by_name_args args;
    remote_node_device_lookup_by_name_ret ret;
6175
    virNodeDevicePtr dev = NULL;
6176
    struct private_data *priv = conn->devMonPrivateData;
6177

6178 6179
    remoteDriverLock(priv);

6180 6181 6182 6183 6184 6185
    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)
6186
        goto done;
6187 6188 6189 6190 6191

    dev = get_nonnull_node_device(conn, ret.dev);

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

6192
done:
6193
    remoteDriverUnlock(priv);
6194 6195 6196 6197 6198 6199
    return dev;
}

static char *remoteNodeDeviceDumpXML(virNodeDevicePtr dev,
                                     unsigned int flags)
{
6200
    char *rv = NULL;
6201 6202
    remote_node_device_dump_xml_args args;
    remote_node_device_dump_xml_ret ret;
6203
    struct private_data *priv = dev->conn->devMonPrivateData;
6204

6205 6206
    remoteDriverLock(priv);

6207 6208 6209 6210 6211 6212 6213
    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)
6214
        goto done;
6215 6216

    /* Caller frees. */
6217 6218 6219
    rv = ret.xml;

done:
6220
    remoteDriverUnlock(priv);
6221
    return rv;
6222 6223 6224 6225
}

static char *remoteNodeDeviceGetParent(virNodeDevicePtr dev)
{
6226
    char *rv = NULL;
6227 6228
    remote_node_device_get_parent_args args;
    remote_node_device_get_parent_ret ret;
6229
    struct private_data *priv = dev->conn->devMonPrivateData;
6230

6231 6232
    remoteDriverLock(priv);

6233 6234 6235 6236 6237 6238
    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)
6239
        goto done;
6240 6241

    /* Caller frees. */
6242
    rv = ret.parent ? *ret.parent : NULL;
6243
    VIR_FREE(ret.parent);
6244 6245

done:
6246
    remoteDriverUnlock(priv);
6247
    return rv;
6248 6249 6250 6251
}

static int remoteNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
6252
    int rv = -1;
6253 6254
    remote_node_device_num_of_caps_args args;
    remote_node_device_num_of_caps_ret ret;
6255
    struct private_data *priv = dev->conn->devMonPrivateData;
6256

6257 6258
    remoteDriverLock(priv);

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

    rv = ret.num;
6268

6269
done:
6270
    remoteDriverUnlock(priv);
6271
    return rv;
6272 6273 6274 6275 6276 6277
}

static int remoteNodeDeviceListCaps(virNodeDevicePtr dev,
                                    char **const names,
                                    int maxnames)
{
6278
    int rv = -1;
6279 6280 6281
    int i;
    remote_node_device_list_caps_args args;
    remote_node_device_list_caps_ret ret;
6282
    struct private_data *priv = dev->conn->devMonPrivateData;
6283

6284 6285
    remoteDriverLock(priv);

6286
    if (maxnames > REMOTE_NODE_DEVICE_CAPS_LIST_MAX) {
6287
        remoteError(VIR_ERR_RPC, "%s", _("too many capability names requested"));
6288
        goto done;
6289 6290 6291 6292 6293 6294 6295 6296
    }
    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)
6297
        goto done;
6298 6299

    if (ret.names.names_len > maxnames) {
6300
        remoteError(VIR_ERR_RPC, "%s", _("too many capability names received"));
6301
        goto cleanup;
6302 6303 6304 6305 6306 6307 6308
    }

    /* 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.
     */
6309
    for (i = 0; i < ret.names.names_len; ++i) {
6310 6311
        names[i] = strdup (ret.names.names_val[i]);

6312 6313 6314 6315
        if (names[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(names[i]);

6316
            virReportOOMError();
6317 6318 6319 6320
            goto cleanup;
        }
    }

6321 6322 6323
    rv = ret.names.names_len;

cleanup:
6324 6325
    xdr_free ((xdrproc_t) xdr_remote_node_device_list_caps_ret, (char *) &ret);

6326
done:
6327
    remoteDriverUnlock(priv);
6328
    return rv;
6329 6330
}

6331 6332 6333 6334 6335
static int
remoteNodeDeviceDettach (virNodeDevicePtr dev)
{
    int rv = -1;
    remote_node_device_dettach_args args;
6336 6337
    /* This method is unusual in that it uses the HV driver, not the devMon driver
     * hence its use of privateData, instead of devMonPrivateData */
6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360
    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;
6361 6362
    /* This method is unusual in that it uses the HV driver, not the devMon driver
     * hence its use of privateData, instead of devMonPrivateData */
6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385
    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;
6386 6387
    /* This method is unusual in that it uses the HV driver, not the devMon driver
     * hence its use of privateData, instead of devMonPrivateData */
6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405
    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;
}

6406

6407 6408 6409 6410 6411 6412 6413 6414
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;
6415
    struct private_data *priv = conn->devMonPrivateData;
6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440

    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;
6441
    struct private_data *priv = dev->conn->devMonPrivateData;
6442 6443 6444 6445 6446

    remoteDriverLock(priv);

    args.name = dev->name;

6447
    if (call(dev->conn, priv, 0, REMOTE_PROC_NODE_DEVICE_DESTROY,
6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458
             (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;
}

6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608
/* ------------------------------------------------------------- */

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) {
6609 6610 6611
        remoteError(VIR_ERR_RPC,
                    _("too many remote nwfilters: %d > %d"),
                    maxnames, REMOTE_NWFILTER_NAME_LIST_MAX);
6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622
        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) {
6623 6624 6625
        remoteError(VIR_ERR_RPC,
                    _("too many remote nwfilters: %d > %d"),
                    ret.names.names_len, maxnames);
6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739
        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;
}

6740

6741 6742
/*----------------------------------------------------------------------*/

6743
static int
E
Eric Blake 已提交
6744 6745 6746
remoteAuthenticate (virConnectPtr conn, struct private_data *priv,
                    int in_open ATTRIBUTE_UNUSED,
                    virConnectAuthPtr auth ATTRIBUTE_UNUSED,
6747
                    const char *authtype)
6748 6749
{
    struct remote_auth_list_ret ret;
6750
    int err, type = REMOTE_AUTH_NONE;
6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766

    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;

6767 6768 6769 6770 6771 6772 6773 6774
    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 {
6775 6776
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("unknown authentication type %s"), authtype);
6777 6778 6779 6780 6781 6782 6783
            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) {
6784 6785 6786
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("requested authentication type %s rejected"),
                        authtype);
6787 6788 6789 6790 6791 6792 6793
            return -1;
        }
    } else {
        type = ret.types.types_val[0];
    }

    switch (type) {
6794
#if HAVE_SASL
6795 6796 6797 6798 6799 6800 6801
    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) {
6802
            VIR_FREE(ret.types.types_val);
6803 6804 6805
            return -1;
        }
        break;
6806
    }
6807 6808
#endif

6809 6810
#if HAVE_POLKIT
    case REMOTE_AUTH_POLKIT:
6811
        if (remoteAuthPolkit(conn, priv, in_open, auth) < 0) {
6812
            VIR_FREE(ret.types.types_val);
6813 6814 6815 6816 6817
            return -1;
        }
        break;
#endif

6818 6819 6820 6821 6822
    case REMOTE_AUTH_NONE:
        /* Nothing todo, hurrah ! */
        break;

    default:
6823 6824 6825
        remoteError(VIR_ERR_AUTH_FAILED,
                    _("unsupported authentication type %d"),
                    ret.types.types_val[0]);
6826
        VIR_FREE(ret.types.types_val);
6827 6828 6829
        return -1;
    }

6830
    VIR_FREE(ret.types.types_val);
6831 6832 6833 6834 6835 6836 6837 6838

    return 0;
}



#if HAVE_SASL
/*
6839
 * NB, keep in sync with similar method in daemon/remote.c
6840
 */
6841
static char *addrToString(struct sockaddr_storage *ss, socklen_t salen)
6842 6843 6844 6845
{
    char host[NI_MAXHOST], port[NI_MAXSERV];
    char *addr;
    int err;
6846
    struct sockaddr *sa = (struct sockaddr *)ss;
6847

6848 6849 6850 6851 6852 6853 6854 6855
    if (sa->sa_family == AF_UNIX) {
        if (!(addr = strdup("127.0.0.1;0"))) {
            virReportOOMError();
            return NULL;
        }
        return addr;
    }

6856
    if ((err = getnameinfo(sa, salen,
6857 6858 6859
                           host, sizeof(host),
                           port, sizeof(port),
                           NI_NUMERICHOST | NI_NUMERICSERV)) != 0) {
6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876
        char ip[INET6_ADDRSTRLEN];
        void *rawaddr;

        if (sa->sa_family == AF_INET)
            rawaddr = &((struct sockaddr_in *)sa)->sin_addr;
        else
            rawaddr = &((struct sockaddr_in6 *)sa)->sin6_addr;

        if (inet_ntop(sa->sa_family, rawaddr, ip, sizeof ip)) {
            remoteError(VIR_ERR_UNKNOWN_HOST,
                        _("Cannot resolve address %s: %s"),
                        ip, gai_strerror(err));
        } else {
            remoteError(VIR_ERR_UNKNOWN_HOST,
                        _("Cannot resolve address: %s"),
                        gai_strerror(err));
        }
6877 6878 6879
        return NULL;
    }

6880
    if (virAsprintf(&addr, "%s;%s", host, port) == -1) {
6881
        virReportOOMError();
6882 6883 6884 6885 6886 6887 6888
        return NULL;
    }

    return addr;
}


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
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)
{
6967
    sasl_callback_t *cbs;
6968
    int i, n;
6969
    if (VIR_ALLOC_N(cbs, ncredtype+1) < 0) {
6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988
        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
6989
 *
6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003
 * 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 */

7004
    if (VIR_ALLOC_N(*cred, ninteract) < 0)
7005 7006 7007 7008 7009
        return -1;

    for (ninteract = 0 ; interact[ninteract].id != 0 ; ninteract++) {
        (*cred)[ninteract].type = remoteAuthCredSASL2Vir(interact[ninteract].id);
        if (!(*cred)[ninteract].type) {
7010
            VIR_FREE(*cred);
7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028
            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++)
7029 7030
        VIR_FREE(cred[i].result);
    VIR_FREE(cred);
7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051
}


/*
 * @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
7052 7053
 */
static int
7054 7055
remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open,
                virConnectAuthPtr auth, const char *wantmech)
7056 7057
{
    sasl_conn_t *saslconn = NULL;
7058
    sasl_security_properties_t secprops;
7059 7060 7061 7062 7063 7064
    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;
7065
    char *serverin = NULL;
7066 7067 7068 7069 7070
    unsigned int clientoutlen, serverinlen;
    const char *mech;
    int err, complete;
    struct sockaddr_storage sa;
    socklen_t salen;
7071
    char *localAddr = NULL, *remoteAddr = NULL;
7072 7073
    const void *val;
    sasl_ssf_t ssf;
7074 7075 7076 7077 7078 7079
    sasl_callback_t *saslcb = NULL;
    sasl_interact_t *interact = NULL;
    virConnectCredentialPtr cred = NULL;
    int ncred = 0;
    int ret = -1;
    const char *mechlist;
7080

7081
    DEBUG0("Client initialize SASL authentication");
7082 7083 7084
    /* Sets up the SASL library as a whole */
    err = sasl_client_init(NULL);
    if (err != SASL_OK) {
7085 7086 7087
        remoteError(VIR_ERR_AUTH_FAILED,
                    _("failed to initialize SASL library: %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
7088
        goto cleanup;
7089 7090 7091 7092 7093
    }

    /* Get local address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getsockname(priv->sock, (struct sockaddr*)&sa, &salen) < 0) {
7094
        virReportSystemError(errno, "%s",
7095
                             _("failed to get sock address"));
7096
        goto cleanup;
7097
    }
7098 7099
    if ((localAddr = addrToString(&sa, salen)) == NULL)
        goto cleanup;
7100 7101 7102 7103

    /* Get remote address in form  IPADDR:PORT */
    salen = sizeof(sa);
    if (getpeername(priv->sock, (struct sockaddr*)&sa, &salen) < 0) {
7104
        virReportSystemError(errno, "%s",
7105
                             _("failed to get peer address"));
7106
        goto cleanup;
7107
    }
7108 7109 7110
    if ((remoteAddr = addrToString(&sa, salen)) == NULL)
        goto cleanup;

7111 7112 7113 7114 7115 7116
    if (auth) {
        if ((saslcb = remoteAuthMakeCallbacks(auth->credtype, auth->ncredtype)) == NULL)
            goto cleanup;
    } else {
        saslcb = NULL;
    }
7117 7118 7119 7120 7121 7122

    /* Setup a handle for being a client */
    err = sasl_client_new("libvirt",
                          priv->hostname,
                          localAddr,
                          remoteAddr,
7123
                          saslcb,
7124 7125
                          SASL_SUCCESS_DATA,
                          &saslconn);
7126

7127
    if (err != SASL_OK) {
7128 7129 7130
        remoteError(VIR_ERR_AUTH_FAILED,
                    _("Failed to create SASL client context: %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
7131
        goto cleanup;
7132 7133
    }

7134 7135 7136 7137 7138 7139
    /* 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))) {
7140 7141
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("invalid cipher size for TLS session"));
7142
            goto cleanup;
7143 7144 7145
        }
        ssf *= 8; /* key size is bytes, sasl wants bits */

7146
        DEBUG("Setting external SSF %d", ssf);
7147 7148
        err = sasl_setprop(saslconn, SASL_SSF_EXTERNAL, &ssf);
        if (err != SASL_OK) {
7149 7150 7151
            remoteError(VIR_ERR_INTERNAL_ERROR,
                        _("cannot set external SSF %d (%s)"),
                        err, sasl_errstring(err, NULL, NULL));
7152
            goto cleanup;
7153 7154 7155 7156
        }
    }

    memset (&secprops, 0, sizeof secprops);
7157 7158 7159
    /* 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 */
7160
    secprops.maxbufsize = 100000;
7161 7162
    /* If we're not secure, then forbid any anonymous or trivially crackable auth */
    secprops.security_flags = priv->is_secure ? 0 :
7163 7164 7165 7166
        SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT;

    err = sasl_setprop(saslconn, SASL_SEC_PROPS, &secprops);
    if (err != SASL_OK) {
7167 7168 7169
        remoteError(VIR_ERR_INTERNAL_ERROR,
                    _("cannot set security props %d (%s)"),
                    err, sasl_errstring(err, NULL, NULL));
7170
        goto cleanup;
7171 7172
    }

7173 7174 7175 7176
    /* 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,
7177 7178
              (xdrproc_t) xdr_remote_auth_sasl_init_ret, (char *) &iret) != 0)
        goto cleanup;
7179 7180


7181 7182 7183
    mechlist = iret.mechlist;
    if (wantmech) {
        if (strstr(mechlist, wantmech) == NULL) {
7184 7185 7186
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("SASL mechanism %s not supported by server"),
                        wantmech);
7187
            VIR_FREE(iret.mechlist);
7188 7189 7190 7191 7192
            goto cleanup;
        }
        mechlist = wantmech;
    }
 restart:
7193
    /* Start the auth negotiation on the client end first */
7194
    DEBUG("Client start negotiation mechlist '%s'", mechlist);
7195
    err = sasl_client_start(saslconn,
7196 7197
                            mechlist,
                            &interact,
7198 7199 7200
                            &clientout,
                            &clientoutlen,
                            &mech);
7201
    if (err != SASL_OK && err != SASL_CONTINUE && err != SASL_INTERACT) {
7202 7203 7204
        remoteError(VIR_ERR_AUTH_FAILED,
                    _("Failed to start SASL negotiation: %d (%s)"),
                    err, sasl_errdetail(saslconn));
7205
        VIR_FREE(iret.mechlist);
7206 7207 7208 7209 7210
        goto cleanup;
    }

    /* Need to gather some credentials from the client */
    if (err == SASL_INTERACT) {
7211
        const char *msg;
7212 7213 7214 7215
        if (cred) {
            remoteAuthFreeCredentials(cred, ncred);
            cred = NULL;
        }
7216 7217 7218
        if ((ncred = remoteAuthMakeCredentials(interact, &cred)) < 0) {
            remoteError(VIR_ERR_AUTH_FAILED, "%s",
                        _("Failed to make auth credentials"));
7219
            VIR_FREE(iret.mechlist);
7220 7221 7222
            goto cleanup;
        }
        /* Run the authentication callback */
7223
        if (auth && auth->cb) {
7224 7225 7226
            if ((*(auth->cb))(cred, ncred, auth->cbdata) >= 0) {
                remoteAuthFillInteract(cred, interact);
                goto restart;
7227
            }
7228
            msg = "Failed to collect auth credentials";
7229
        } else {
7230
            msg = "No authentication callback available";
7231
        }
7232
        remoteError(VIR_ERR_AUTH_FAILED, "%s", msg);
7233
        goto cleanup;
7234
    }
7235
    VIR_FREE(iret.mechlist);
7236 7237

    if (clientoutlen > REMOTE_AUTH_SASL_DATA_MAX) {
7238 7239 7240
        remoteError(VIR_ERR_AUTH_FAILED,
                    _("SASL negotiation data too long: %d bytes"),
                    clientoutlen);
7241
        goto cleanup;
7242 7243 7244 7245 7246 7247 7248
    }
    /* 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;
7249
    DEBUG("Server start negotiation with mech %s. Data %d bytes %p", mech, clientoutlen, clientout);
7250 7251 7252 7253 7254

    /* 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,
7255 7256
              (xdrproc_t) xdr_remote_auth_sasl_start_ret, (char *) &sret) != 0)
        goto cleanup;
7257 7258 7259 7260 7261

    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;
7262 7263
    DEBUG("Client step result complete: %d. Data %d bytes %p",
          complete, serverinlen, serverin);
7264 7265 7266

    /* Loop-the-loop...
     * Even if the server has completed, the client must *always* do at least one step
D
Daniel Veillard 已提交
7267
     * in this loop to verify the server isn't lying about something. Mutual auth */
7268
    for (;;) {
7269
    restep:
7270 7271 7272
        err = sasl_client_step(saslconn,
                               serverin,
                               serverinlen,
7273
                               &interact,
7274 7275
                               &clientout,
                               &clientoutlen);
7276
        if (err != SASL_OK && err != SASL_CONTINUE && err != SASL_INTERACT) {
7277 7278 7279
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("Failed SASL step: %d (%s)"),
                        err, sasl_errdetail(saslconn));
7280 7281 7282 7283
            goto cleanup;
        }
        /* Need to gather some credentials from the client */
        if (err == SASL_INTERACT) {
7284
            const char *msg;
7285 7286 7287 7288 7289
            if (cred) {
                remoteAuthFreeCredentials(cred, ncred);
                cred = NULL;
            }
            if ((ncred = remoteAuthMakeCredentials(interact, &cred)) < 0) {
7290 7291
                remoteError(VIR_ERR_AUTH_FAILED, "%s",
                            _("Failed to make auth credentials"));
7292 7293 7294
                goto cleanup;
            }
            /* Run the authentication callback */
7295
            if (auth && auth->cb) {
7296 7297 7298
                if ((*(auth->cb))(cred, ncred, auth->cbdata) >= 0) {
                    remoteAuthFillInteract(cred, interact);
                    goto restep;
7299
                }
7300
                msg = _("Failed to collect auth credentials");
7301
            } else {
7302
                msg = _("No authentication callback available");
7303
            }
7304
            remoteError(VIR_ERR_AUTH_FAILED, "%s", msg);
7305
            goto cleanup;
7306 7307
        }

7308
        VIR_FREE(serverin);
7309
        DEBUG("Client step result %d. Data %d bytes %p", err, clientoutlen, clientout);
7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320

        /* 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;
7321
        DEBUG("Server step with %d bytes %p", clientoutlen, clientout);
7322 7323 7324 7325

        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,
7326 7327
                  (xdrproc_t) xdr_remote_auth_sasl_step_ret, (char *) &pret) != 0)
            goto cleanup;
7328 7329 7330 7331 7332 7333

        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;

7334 7335
        DEBUG("Client step result complete: %d. Data %d bytes %p",
              complete, serverinlen, serverin);
7336 7337 7338

        /* This server call shows complete, and earlier client step was OK */
        if (complete && err == SASL_OK) {
7339
            VIR_FREE(serverin);
7340 7341 7342 7343
            break;
        }
    }

7344 7345
    /* Check for suitable SSF if not already secure (TLS or UNIX sock) */
    if (!priv->is_secure) {
7346 7347
        err = sasl_getprop(saslconn, SASL_SSF, &val);
        if (err != SASL_OK) {
7348 7349 7350
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("cannot query SASL ssf on connection %d (%s)"),
                        err, sasl_errstring(err, NULL, NULL));
7351
            goto cleanup;
7352 7353
        }
        ssf = *(const int *)val;
7354
        DEBUG("SASL SSF value %d", ssf);
7355
        if (ssf < 56) { /* 56 == DES level, good for Kerberos */
7356 7357
            remoteError(VIR_ERR_AUTH_FAILED,
                        _("negotiation SSF %d was not strong enough"), ssf);
7358
            goto cleanup;
7359
        }
7360
        priv->is_secure = 1;
7361 7362
    }

7363
    DEBUG0("SASL authentication complete");
7364
    priv->saslconn = saslconn;
7365 7366 7367
    ret = 0;

 cleanup:
7368 7369 7370
    VIR_FREE(localAddr);
    VIR_FREE(remoteAddr);
    VIR_FREE(serverin);
7371

7372
    VIR_FREE(saslcb);
7373 7374 7375
    remoteAuthFreeCredentials(cred, ncred);
    if (ret != 0 && saslconn)
        sasl_dispose(&saslconn);
7376

7377
    return ret;
7378 7379 7380
}
#endif /* HAVE_SASL */

7381 7382

#if HAVE_POLKIT
7383
# if HAVE_POLKIT1
7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400
static int
remoteAuthPolkit (virConnectPtr conn, struct private_data *priv, int in_open,
                  virConnectAuthPtr auth ATTRIBUTE_UNUSED)
{
    remote_auth_polkit_ret ret;
    DEBUG0("Client initialize PolicyKit-1 authentication");

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

    DEBUG0("PolicyKit-1 authentication complete");
    return 0;
}
7401
# elif HAVE_POLKIT0
7402 7403 7404 7405 7406 7407 7408
/* Perform the PolicyKit authentication process
 */
static int
remoteAuthPolkit (virConnectPtr conn, struct private_data *priv, int in_open,
                  virConnectAuthPtr auth)
{
    remote_auth_polkit_ret ret;
7409
    int i, allowcb = 0;
7410 7411 7412 7413 7414 7415 7416 7417
    virConnectCredential cred = {
        VIR_CRED_EXTERNAL,
        conn->flags & VIR_CONNECT_RO ? "org.libvirt.unix.monitor" : "org.libvirt.unix.manage",
        "PolicyKit",
        NULL,
        NULL,
        0,
    };
7418
    DEBUG0("Client initialize PolicyKit-0 authentication");
7419

7420
    if (auth && auth->cb) {
7421
        /* Check if the necessary credential type for PolicyKit is supported */
7422 7423 7424 7425
        for (i = 0 ; i < auth->ncredtype ; i++) {
            if (auth->credtype[i] == VIR_CRED_EXTERNAL)
                allowcb = 1;
        }
7426

7427
        if (allowcb) {
7428
            DEBUG0("Client run callback for PolicyKit authentication");
7429 7430
            /* Run the authentication callback */
            if ((*(auth->cb))(&cred, 1, auth->cbdata) < 0) {
7431 7432
                remoteError(VIR_ERR_AUTH_FAILED, "%s",
                            _("Failed to collect auth credentials"));
7433 7434
                return -1;
            }
7435
        } else {
7436
            DEBUG0("Client auth callback does not support PolicyKit");
7437 7438
        }
    } else {
7439
        DEBUG0("No auth callback provided");
7440 7441 7442 7443 7444 7445 7446 7447 7448
    }

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

7449
    DEBUG0("PolicyKit-0 authentication complete");
7450 7451
    return 0;
}
7452
# endif /* HAVE_POLKIT0 */
7453
#endif /* HAVE_POLKIT */
7454 7455
/*----------------------------------------------------------------------*/

7456 7457 7458 7459
static int remoteDomainEventRegister(virConnectPtr conn,
                                     virConnectDomainEventCallback callback,
                                     void *opaque,
                                     virFreeCallback freecb)
7460
{
7461
    int rv = -1;
7462 7463
    struct private_data *priv = conn->privateData;

7464 7465
    remoteDriverLock(priv);

7466
    if (priv->eventFlushTimer < 0) {
7467
         remoteError(VIR_ERR_NO_SUPPORT, "%s", _("no event support"));
7468
         goto done;
7469
    }
7470
    if (virDomainEventCallbackListAdd(conn, priv->callbackList,
7471
                                      callback, opaque, freecb) < 0) {
7472
         remoteError(VIR_ERR_RPC, "%s", _("adding cb to list"));
7473
         goto done;
7474 7475
    }

7476
    if (virDomainEventCallbackListCountID(conn, priv->callbackList, VIR_DOMAIN_EVENT_ID_LIFECYCLE) == 1) {
7477 7478 7479 7480
        /* 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)
7481
            goto done;
7482 7483
    }

7484 7485 7486
    rv = 0;

done:
7487
    remoteDriverUnlock(priv);
7488
    return rv;
7489 7490
}

7491 7492
static int remoteDomainEventDeregister(virConnectPtr conn,
                                       virConnectDomainEventCallback callback)
7493 7494
{
    struct private_data *priv = conn->privateData;
7495
    int rv = -1;
7496

7497 7498
    remoteDriverLock(priv);

7499 7500 7501
    if (priv->domainEventDispatching) {
        if (virDomainEventCallbackListMarkDelete(conn, priv->callbackList,
                                                 callback) < 0) {
7502
            remoteError(VIR_ERR_RPC, "%s", _("marking cb for deletion"));
7503
            goto done;
7504 7505 7506 7507
        }
    } else {
        if (virDomainEventCallbackListRemove(conn, priv->callbackList,
                                             callback) < 0) {
7508
            remoteError(VIR_ERR_RPC, "%s", _("removing cb from list"));
7509 7510
            goto done;
        }
7511
    }
7512

7513 7514 7515 7516 7517 7518
    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;
7519 7520
    }

7521 7522 7523
    rv = 0;

done:
7524
    remoteDriverUnlock(priv);
7525
    return rv;
7526
}
7527

7528 7529 7530 7531 7532 7533 7534 7535
/**
 * remoteDomainReadEventLifecycle
 *
 * Read the domain lifecycle event data off the wire
 */
static virDomainEventPtr
remoteDomainReadEventLifecycle(virConnectPtr conn, XDR *xdr)
{
7536
    remote_domain_event_lifecycle_msg msg;
7537 7538 7539 7540 7541
    virDomainPtr dom;
    virDomainEventPtr event = NULL;
    memset (&msg, 0, sizeof msg);

    /* unmarshall parameters, and process it*/
7542
    if (! xdr_remote_domain_event_lifecycle_msg(xdr, &msg) ) {
7543 7544
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall lifecycle event"));
7545 7546 7547 7548 7549 7550 7551 7552
        return NULL;
    }

    dom = get_nonnull_domain(conn,msg.dom);
    if (!dom)
        return NULL;

    event = virDomainEventNewFromDom(dom, msg.event, msg.detail);
7553
    xdr_free ((xdrproc_t) &xdr_remote_domain_event_lifecycle_msg, (char *) &msg);
7554 7555 7556 7557 7558 7559

    virDomainFree(dom);
    return event;
}


7560 7561 7562 7563 7564 7565 7566 7567 7568 7569
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) ) {
7570 7571
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall reboot event"));
7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586
        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;
}


7587 7588 7589 7590 7591 7592 7593 7594 7595 7596
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) ) {
7597 7598
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall reboot event"));
7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613
        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;
}


7614 7615 7616 7617 7618 7619 7620 7621 7622 7623
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) ) {
7624 7625
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall reboot event"));
7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640
        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;
}


7641 7642 7643 7644 7645 7646 7647 7648 7649 7650
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) ) {
7651 7652
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall reboot event"));
7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670
        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;
}


7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701
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;
}


7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716
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) ) {
7717 7718
        remoteError(VIR_ERR_RPC, "%s",
                    _("unable to demarshall reboot event"));
7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786
        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 已提交
7787
static virDrvOpenStatus ATTRIBUTE_NONNULL (1)
7788 7789 7790 7791 7792 7793 7794
remoteSecretOpen (virConnectPtr conn,
                  virConnectAuthPtr auth,
                  int flags)
{
    if (inside_daemon)
        return VIR_DRV_OPEN_DECLINED;

J
Jim Meyering 已提交
7795
    if (conn->driver &&
7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887
        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) {
7888 7889
        remoteError(VIR_ERR_RPC, _("too many remote secret UUIDs: %d > %d"),
                    maxuuids, REMOTE_SECRET_UUID_LIST_MAX);
7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900
        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) {
7901 7902
        remoteError(VIR_ERR_RPC, _("too many remote secret UUIDs: %d > %d"),
                    ret.uuids.uuids_len, maxuuids);
7903 7904 7905 7906 7907 7908 7909
        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.
     */
7910
    for (i = 0; i < ret.uuids.uuids_len; ++i) {
7911 7912
        uuids[i] = strdup (ret.uuids.uuids_val[i]);

7913 7914 7915 7916
        if (uuids[i] == NULL) {
            for (--i; i >= 0; --i)
                VIR_FREE(uuids[i]);

7917
            virReportOOMError();
7918 7919 7920 7921
            goto cleanup;
        }
    }

7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932
    rv = ret.uuids.uuids_len;

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

done:
    remoteDriverUnlock(priv);
    return rv;
}

static virSecretPtr
7933
remoteSecretLookupByUUID (virConnectPtr conn, const unsigned char *uuid)
7934 7935
{
    virSecretPtr rv = NULL;
7936 7937
    remote_secret_lookup_by_uuid_args args;
    remote_secret_lookup_by_uuid_ret ret;
7938 7939 7940 7941
    struct private_data *priv = conn->secretPrivateData;

    remoteDriverLock (priv);

7942
    memcpy (args.uuid, uuid, VIR_UUID_BUFLEN);
7943 7944

    memset (&ret, 0, sizeof (ret));
7945 7946 7947
    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)
7948 7949 7950
        goto done;

    rv = get_nonnull_secret (conn, ret.secret);
7951
    xdr_free ((xdrproc_t) xdr_remote_secret_lookup_by_uuid_ret,
7952 7953 7954 7955 7956 7957 7958
              (char *) &ret);

done:
    remoteDriverUnlock (priv);
    return rv;
}

7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986
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;
}

7987 7988 7989 7990 7991
static virSecretPtr
remoteSecretDefineXML (virConnectPtr conn, const char *xml, unsigned int flags)
{
    virSecretPtr rv = NULL;
    remote_secret_define_xml_args args;
7992
    remote_secret_define_xml_ret ret;
7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006
    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);
8007
    xdr_free ((xdrproc_t) xdr_remote_secret_define_xml_ret,
8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119
              (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;
}

8120 8121 8122 8123 8124 8125 8126 8127 8128 8129

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;

8130
    if (VIR_ALLOC(stpriv) < 0) {
8131
        virReportOOMError();
8132
        return NULL;
8133
    }
8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151

    /* Initialize call object used to receive replies */
    stpriv->proc_nr = proc_nr;
    stpriv->serial = serial;

    stpriv->next = priv->streams;
    priv->streams = stpriv;

    return stpriv;
}


static int
remoteStreamPacket(virStreamPtr st,
                   int status,
                   const char *data,
                   size_t nbytes)
{
8152
    DEBUG("st=%p status=%d data=%p nbytes=%zu", st, status, data, nbytes);
8153 8154 8155 8156 8157 8158 8159 8160 8161
    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;

    memset(&hdr, 0, sizeof hdr);

    if (VIR_ALLOC(thiscall) < 0) {
8162
        virReportOOMError();
8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174
        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);
8175 8176
        remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("cannot initialize mutex"));
8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200
        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)) {
8201
        remoteError(VIR_ERR_RPC, "%s", _("xdr_remote_message_header failed"));
8202 8203 8204 8205 8206 8207 8208 8209
        goto error;
    }

    thiscall->bufferLength += xdr_getpos (&xdr);
    xdr_destroy (&xdr);

    if (status == REMOTE_CONTINUE) {
        if (((4 + REMOTE_MESSAGE_MAX) - thiscall->bufferLength) < nbytes) {
8210 8211
            remoteError(VIR_ERR_RPC, _("data size %zu too large for payload %d"),
                        nbytes, ((4 + REMOTE_MESSAGE_MAX) - thiscall->bufferLength));
8212 8213 8214 8215 8216 8217 8218 8219 8220 8221
            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)) {
8222
        remoteError(VIR_ERR_RPC, "%s", _("xdr_u_int (length word)"));
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 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294
        goto error;
    }
    xdr_destroy (&xdr);

    /* remoteIO frees 'thiscall' for us (XXX that's dubious semantics) */
    if (remoteIO(st->conn, priv, 0, thiscall) < 0)
        return -1;

    return nbytes;

error:
    xdr_destroy (&xdr);
    VIR_FREE(thiscall);
    return -1;
}

static int
remoteStreamHasError(virStreamPtr st) {
    struct private_stream_data *privst = st->privateData;
    if (!privst->has_error) {
        return 0;
    }

    VIR_WARN0("Raising async error");
    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)
{
8295
    DEBUG("st=%p data=%p nbytes=%zu", st, data, nbytes);
8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323
    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)
{
8324
    DEBUG("st=%p data=%p nbytes=%zu", st, data, nbytes);
8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337
    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;

        if (VIR_ALLOC(thiscall) < 0) {
8338
            virReportOOMError();
8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350
            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);
8351 8352
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("cannot initialize mutex"));
8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 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
            goto cleanup;
        }

        /* remoteIO frees 'thiscall' for us (XXX that's dubious semantics) */
        if (remoteIO(st->conn, priv, 0, thiscall) < 0)
            goto cleanup;
    }

    DEBUG("After IO %d", privst->incomingOffset);
    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;
    }

    DEBUG("Done %d", rv);

cleanup:
    if (rv == -1)
        remoteStreamRelease(st);
    remoteDriverUnlock(priv);

    return rv;
}

static int
remoteStreamEventAddCallback(virStreamPtr stream ATTRIBUTE_UNUSED,
                             int events ATTRIBUTE_UNUSED,
                             virStreamEventCallback cb ATTRIBUTE_UNUSED,
                             void *opaque ATTRIBUTE_UNUSED,
                             virFreeCallback ff ATTRIBUTE_UNUSED)
{
    return -1;
}

static int
remoteStreamEventUpdateCallback(virStreamPtr stream ATTRIBUTE_UNUSED,
                                int events ATTRIBUTE_UNUSED)
{
    return -1;
}


static int
remoteStreamEventRemoveCallback(virStreamPtr stream ATTRIBUTE_UNUSED)
{
    return -1;
}

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 已提交
8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512
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 已提交
8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539

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

8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570

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

8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610

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


8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634
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;
}


8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663
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;
}

C
Chris Lalancette 已提交
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 8692 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 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762
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) {
8763 8764 8765
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain snapshot names: %d > %d"),
                    nameslen, REMOTE_DOMAIN_SNAPSHOT_LIST_NAMES_MAX);
C
Chris Lalancette 已提交
8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779
        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) {
8780 8781 8782
        remoteError(VIR_ERR_RPC,
                    _("too many remote domain snapshots: %d > %d"),
                    ret.names.names_len, nameslen);
C
Chris Lalancette 已提交
8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948
        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;
}
8949

8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964
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) {
8965
         remoteError(VIR_ERR_NO_SUPPORT, "%s", _("no event support"));
8966 8967 8968 8969 8970 8971
         goto done;
    }

    if ((callbackID = virDomainEventCallbackListAddID(conn, priv->callbackList,
                                                      dom, eventID,
                                                      callback, opaque, freecb)) < 0) {
8972
         remoteError(VIR_ERR_RPC, "%s", _("adding cb to list"));
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 9004 9005 9006 9007
         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) {
9008
        remoteError(VIR_ERR_RPC, _("unable to find callback ID %d"), callbackID);
9009 9010 9011 9012 9013 9014
        goto done;
    }

    if (priv->domainEventDispatching) {
        if (virDomainEventCallbackListMarkDeleteID(conn, priv->callbackList,
                                                   callbackID) < 0) {
9015
            remoteError(VIR_ERR_RPC, "%s", _("marking cb for deletion"));
9016 9017 9018 9019 9020
            goto done;
        }
    } else {
        if (virDomainEventCallbackListRemoveID(conn, priv->callbackList,
                                               callbackID) < 0) {
9021
            remoteError(VIR_ERR_RPC, "%s", _("removing cb from list"));
9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044
            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;
}


9045 9046
/*----------------------------------------------------------------------*/

C
Chris Lalancette 已提交
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 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085
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);
    if (call (domain->conn, priv, REMOTE_QEMU_CALL, QEMU_PROC_MONITOR_COMMAND,
              (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;
}

/*----------------------------------------------------------------------*/
9086

9087
static struct remote_thread_call *
9088
prepareCall(struct private_data *priv,
C
Chris Lalancette 已提交
9089
            int flags,
9090 9091 9092 9093
            int proc_nr,
            xdrproc_t args_filter, char *args,
            xdrproc_t ret_filter, char *ret)
{
9094
    XDR xdr;
9095 9096 9097
    struct remote_message_header hdr;
    struct remote_thread_call *rv;

9098
    if (VIR_ALLOC(rv) < 0) {
9099
        virReportOOMError();
9100
        return NULL;
9101
    }
9102 9103 9104

    if (virCondInit(&rv->cond) < 0) {
        VIR_FREE(rv);
9105 9106
        remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                    _("cannot initialize mutex"));
9107 9108
        return NULL;
    }
9109 9110

    /* Get a unique serial number for this message. */
9111 9112 9113 9114
    rv->serial = priv->counter++;
    rv->proc_nr = proc_nr;
    rv->ret_filter = ret_filter;
    rv->ret = ret;
9115
    rv->want_reply = 1;
9116

C
Chris Lalancette 已提交
9117 9118 9119 9120 9121 9122 9123 9124
    if (flags & REMOTE_QEMU_CALL) {
        hdr.prog = QEMU_PROGRAM;
        hdr.vers = QEMU_PROTOCOL_VERSION;
    }
    else {
        hdr.prog = REMOTE_PROGRAM;
        hdr.vers = REMOTE_PROTOCOL_VERSION;
    }
9125
    hdr.proc = proc_nr;
9126
    hdr.type = REMOTE_CALL;
9127
    hdr.serial = rv->serial;
9128 9129 9130
    hdr.status = REMOTE_OK;

    /* Serialise header followed by args. */
9131
    xdrmem_create (&xdr, rv->buffer+4, REMOTE_MESSAGE_MAX, XDR_ENCODE);
9132
    if (!xdr_remote_message_header (&xdr, &hdr)) {
9133
        remoteError(VIR_ERR_RPC, "%s", _("xdr_remote_message_header failed"));
9134
        goto error;
9135 9136 9137
    }

    if (!(*args_filter) (&xdr, args)) {
9138
        remoteError(VIR_ERR_RPC, "%s", _("marshalling args"));
9139
        goto error;
9140 9141 9142
    }

    /* Get the length stored in buffer. */
9143
    rv->bufferLength = xdr_getpos (&xdr);
9144 9145 9146 9147 9148
    xdr_destroy (&xdr);

    /* Length must include the length word itself (always encoded in
     * 4 bytes as per RFC 4506).
     */
9149
    rv->bufferLength += REMOTE_MESSAGE_HEADER_XDR_LEN;
9150 9151

    /* Encode the length word. */
9152 9153
    xdrmem_create (&xdr, rv->buffer, REMOTE_MESSAGE_HEADER_XDR_LEN, XDR_ENCODE);
    if (!xdr_u_int (&xdr, &rv->bufferLength)) {
9154
        remoteError(VIR_ERR_RPC, "%s", _("xdr_u_int (length word)"));
9155
        goto error;
9156 9157 9158
    }
    xdr_destroy (&xdr);

9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169
    return rv;

error:
    xdr_destroy (&xdr);
    VIR_FREE(rv);
    return NULL;
}



static int
9170
remoteIOWriteBuffer(struct private_data *priv,
9171
                    const char *bytes, int len)
9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183
{
    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;

9184
            remoteError(VIR_ERR_GNUTLS_ERROR, "%s", gnutls_strerror (ret));
9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195
            return -1;
        }
    } else {
    resend:
        ret = send (priv->sock, bytes, len, 0);
        if (ret == -1) {
            if (errno == EINTR)
                goto resend;
            if (errno == EWOULDBLOCK)
                return 0;

9196
            virReportSystemError(errno, "%s", _("cannot send data"));
9197 9198 9199 9200 9201 9202 9203 9204 9205 9206
            return -1;

        }
    }

    return ret;
}


static int
9207
remoteIOReadBuffer(struct private_data *priv,
9208
                   char *bytes, int len)
9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222
{
    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)
9223 9224 9225
                remoteError(VIR_ERR_GNUTLS_ERROR,
                            _("failed to read from TLS socket %s"),
                            gnutls_strerror (ret));
9226
            else
9227 9228
                remoteError(VIR_ERR_SYSTEM_ERROR, "%s",
                            _("server closed connection"));
9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240
            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;

9241 9242
                char errout[1024] = "\0";
                if (priv->errfd != -1) {
9243 9244 9245 9246 9247
                    if (saferead(priv->errfd, errout, sizeof(errout)) < 0) {
                        virReportSystemError(errno, "%s",
                                             _("cannot recv data"));
                        return -1;
                    }
9248 9249
                }

9250
                virReportSystemError(errno,
9251 9252
                                     _("cannot recv data: %s"), errout);

9253
            } else {
9254 9255
                char errout[1024] = "\0";
                if (priv->errfd != -1) {
9256 9257 9258 9259 9260 9261
                    if (saferead(priv->errfd, errout, sizeof(errout)) < 0) {
                        remoteError(VIR_ERR_SYSTEM_ERROR,
                                    _("server closed connection: %s"),
                                    virStrerror(errno, errout, sizeof errout));
                        return -1;
                    }
9262 9263
                }

9264 9265
                remoteError(VIR_ERR_SYSTEM_ERROR,
                            _("server closed connection: %s"), errout);
9266 9267 9268 9269 9270 9271 9272 9273 9274 9275
            }
            return -1;
        }
    }

    return ret;
}


static int
9276
remoteIOWriteMessage(struct private_data *priv,
9277
                     struct remote_thread_call *thecall)
9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290
{
#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) {
9291 9292 9293
                remoteError(VIR_ERR_INTERNAL_ERROR,
                            _("failed to encode SASL data: %s"),
                            sasl_errstring(err, NULL, NULL));
9294 9295 9296 9297 9298 9299 9300 9301 9302
                return -1;
            }
            priv->saslEncoded = output;
            priv->saslEncodedLength = outputlen;
            priv->saslEncodedOffset = 0;

            thecall->bufferOffset = thecall->bufferLength;
        }

9303
        ret = remoteIOWriteBuffer(priv,
9304 9305
                                  priv->saslEncoded + priv->saslEncodedOffset,
                                  priv->saslEncodedLength - priv->saslEncodedOffset);
9306 9307 9308 9309 9310 9311 9312
        if (ret < 0)
            return ret;
        priv->saslEncodedOffset += ret;

        if (priv->saslEncodedOffset == priv->saslEncodedLength) {
            priv->saslEncoded = NULL;
            priv->saslEncodedOffset = priv->saslEncodedLength = 0;
9313 9314 9315 9316
            if (thecall->want_reply)
                thecall->mode = REMOTE_MODE_WAIT_RX;
            else
                thecall->mode = REMOTE_MODE_COMPLETE;
9317 9318 9319 9320
        }
    } else {
#endif
        int ret;
9321
        ret = remoteIOWriteBuffer(priv,
9322 9323
                                  thecall->buffer + thecall->bufferOffset,
                                  thecall->bufferLength - thecall->bufferOffset);
9324 9325 9326 9327 9328 9329
        if (ret < 0)
            return ret;
        thecall->bufferOffset += ret;

        if (thecall->bufferOffset == thecall->bufferLength) {
            thecall->bufferOffset = thecall->bufferLength = 0;
9330 9331 9332 9333
            if (thecall->want_reply)
                thecall->mode = REMOTE_MODE_WAIT_RX;
            else
                thecall->mode = REMOTE_MODE_COMPLETE;
9334 9335 9336 9337 9338 9339 9340 9341 9342
        }
#if HAVE_SASL
    }
#endif
    return 0;
}


static int
9343
remoteIOHandleOutput(struct private_data *priv) {
9344 9345 9346 9347 9348 9349 9350 9351 9352 9353
    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) {
9354
        int ret = remoteIOWriteMessage(priv, thecall);
9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367
        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
9368
remoteIOReadMessage(struct private_data *priv) {
9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381
    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;
9382
            ret = remoteIOReadBuffer(priv, encoded, sizeof(encoded));
9383 9384 9385 9386 9387 9388 9389 9390
            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) {
9391 9392 9393
                remoteError(VIR_ERR_INTERNAL_ERROR,
                            _("failed to decode SASL data: %s"),
                            sasl_errstring(err, NULL, NULL));
9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407
                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) {
9408
            priv->saslDecodedOffset = priv->saslDecodedLength = 0;
9409 9410 9411 9412 9413 9414 9415 9416
            priv->saslDecoded = NULL;
        }

        return wantData;
    } else {
#endif
        int ret;

9417
        ret = remoteIOReadBuffer(priv,
9418 9419
                                 priv->buffer + priv->bufferOffset,
                                 wantData);
9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431
        if (ret < 0)
            return -1;
        if (ret == 0)
            return 0;

        priv->bufferOffset += ret;

        return ret;
#if HAVE_SASL
    }
#endif
}
9432 9433


9434
static int
9435
remoteIODecodeMessageLength(struct private_data *priv) {
9436
    XDR xdr;
9437
    unsigned int len;
9438 9439

    xdrmem_create (&xdr, priv->buffer, priv->bufferLength, XDR_DECODE);
9440
    if (!xdr_u_int (&xdr, &len)) {
9441
        remoteError(VIR_ERR_RPC, "%s", _("xdr_u_int (length word, reply)"));
9442 9443 9444 9445
        return -1;
    }
    xdr_destroy (&xdr);

9446
    if (len < REMOTE_MESSAGE_HEADER_XDR_LEN) {
9447 9448
        remoteError(VIR_ERR_RPC, "%s",
                    _("packet received from server too small"));
9449 9450 9451
        return -1;
    }

9452
    /* Length includes length word - adjust to real length to read. */
9453
    len -= REMOTE_MESSAGE_HEADER_XDR_LEN;
9454

9455
    if (len > REMOTE_MESSAGE_MAX) {
9456 9457
        remoteError(VIR_ERR_RPC, "%s",
                    _("packet received from server too large"));
9458 9459 9460
        return -1;
    }

9461 9462 9463 9464 9465 9466 9467 9468 9469
    /* Extend our declared buffer length and carry
       on reading the header + payload */
    priv->bufferLength += len;
    DEBUG("Got length, now need %d total (%d more)", priv->bufferLength, len);
    return 0;
}


static int
9470 9471 9472 9473 9474 9475 9476 9477 9478 9479
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);

9480 9481 9482 9483 9484
static int
processCallDispatchStream(virConnectPtr conn, struct private_data *priv,
                          remote_message_header *hdr,
                          XDR *xdr);

9485 9486 9487

static int
processCallDispatch(virConnectPtr conn, struct private_data *priv,
C
Chris Lalancette 已提交
9488
                    int flags) {
9489 9490 9491
    XDR xdr;
    struct remote_message_header hdr;
    int len = priv->bufferLength - 4;
9492
    int rv = -1;
C
Chris Lalancette 已提交
9493 9494
    int expectedprog;
    int expectedvers;
9495

9496 9497 9498
    /* Length word has already been read */
    priv->bufferOffset = 4;

9499
    /* Deserialise reply header. */
9500
    xdrmem_create (&xdr, priv->buffer + priv->bufferOffset, len, XDR_DECODE);
9501
    if (!xdr_remote_message_header (&xdr, &hdr)) {
9502
        remoteError(VIR_ERR_RPC, "%s", _("invalid header in reply"));
9503 9504 9505
        return -1;
    }

9506 9507
    priv->bufferOffset += xdr_getpos(&xdr);

C
Chris Lalancette 已提交
9508 9509 9510 9511 9512 9513 9514
    expectedprog = REMOTE_PROGRAM;
    expectedvers = REMOTE_PROTOCOL_VERSION;
    if (flags & REMOTE_QEMU_CALL) {
        expectedprog = QEMU_PROGRAM;
        expectedvers = QEMU_PROTOCOL_VERSION;
    }

9515
    /* Check program, version, etc. are what we expect. */
C
Chris Lalancette 已提交
9516
    if (hdr.prog != expectedprog) {
9517 9518
        remoteError(VIR_ERR_RPC,
                    _("unknown program (received %x, expected %x)"),
C
Chris Lalancette 已提交
9519
                    hdr.prog, expectedprog);
9520 9521
        return -1;
    }
C
Chris Lalancette 已提交
9522
    if (hdr.vers != expectedvers) {
9523 9524
        remoteError(VIR_ERR_RPC,
                    _("unknown protocol version (received %x, expected %x)"),
C
Chris Lalancette 已提交
9525
                    hdr.vers, expectedvers);
9526 9527 9528
        return -1;
    }

9529

9530 9531
    switch (hdr.type) {
    case REMOTE_REPLY: /* Normal RPC replies */
C
Chris Lalancette 已提交
9532
        rv = processCallDispatchReply(conn, priv, &hdr, &xdr);
9533
        break;
9534

9535
    case REMOTE_MESSAGE: /* Async notifications */
C
Chris Lalancette 已提交
9536
        rv = processCallDispatchMessage(conn, priv, flags & REMOTE_CALL_IN_OPEN,
9537 9538 9539
                                        &hdr, &xdr);
        break;

9540
    case REMOTE_STREAM: /* Stream protocol */
C
Chris Lalancette 已提交
9541
        rv = processCallDispatchStream(conn, priv, &hdr, &xdr);
9542 9543
        break;

9544
    default:
9545 9546 9547
        remoteError(VIR_ERR_RPC,
                    _("got unexpected RPC call %d from server"),
                    hdr.proc);
9548 9549
        rv = -1;
        break;
9550
    }
9551

9552 9553 9554 9555 9556 9557
    xdr_destroy(&xdr);
    return rv;
}


static int
9558 9559
processCallDispatchReply(virConnectPtr conn ATTRIBUTE_UNUSED,
                         struct private_data *priv,
9560 9561 9562 9563
                         remote_message_header *hdr,
                         XDR *xdr) {
    struct remote_thread_call *thecall;

9564 9565 9566 9567
    /* Ok, definitely got an RPC reply now find
       out who's been waiting for it */
    thecall = priv->waitDispatch;
    while (thecall &&
9568
           thecall->serial != hdr->serial)
9569 9570 9571
        thecall = thecall->next;

    if (!thecall) {
9572 9573 9574
        remoteError(VIR_ERR_RPC,
                    _("no call waiting for reply with serial %d"),
                    hdr->serial);
9575 9576
        return -1;
    }
9577

9578
    if (hdr->proc != thecall->proc_nr) {
9579 9580 9581
        remoteError(VIR_ERR_RPC,
                    _("unknown procedure (received %x, expected %x)"),
                    hdr->proc, thecall->proc_nr);
9582 9583 9584 9585 9586 9587 9588
        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).
     */
9589
    switch (hdr->status) {
9590
    case REMOTE_OK:
9591
        if (!(*thecall->ret_filter) (xdr, thecall->ret)) {
9592
            remoteError(VIR_ERR_RPC, "%s", _("unmarshalling ret"));
9593 9594
            return -1;
        }
9595
        thecall->mode = REMOTE_MODE_COMPLETE;
9596 9597 9598
        return 0;

    case REMOTE_ERROR:
9599
        memset (&thecall->err, 0, sizeof thecall->err);
9600
        if (!xdr_remote_error (xdr, &thecall->err)) {
9601
            remoteError(VIR_ERR_RPC, "%s", _("unmarshalling remote_error"));
9602 9603
            return -1;
        }
9604 9605
        thecall->mode = REMOTE_MODE_ERROR;
        return 0;
9606 9607

    default:
9608
        remoteError(VIR_ERR_RPC, _("unknown status (received %x)"), hdr->status);
9609 9610 9611 9612
        return -1;
    }
}

9613 9614 9615 9616 9617
static int
processCallDispatchMessage(virConnectPtr conn, struct private_data *priv,
                           int in_open,
                           remote_message_header *hdr,
                           XDR *xdr) {
9618
    virDomainEventPtr event = NULL;
9619 9620 9621 9622 9623 9624 9625 9626 9627 9628
    /* An async message has come in while we were waiting for the
     * response. Process it to pull it off the wire, and try again
     */
    DEBUG0("Encountered an event while waiting for a response");

    if (in_open) {
        DEBUG("Ignoring bogus event %d received while in open", hdr->proc);
        return -1;
    }

9629
    switch (hdr->proc) {
9630
    case REMOTE_PROC_DOMAIN_EVENT_LIFECYCLE:
9631 9632 9633
        event = remoteDomainReadEventLifecycle(conn, xdr);
        break;

9634 9635 9636 9637
    case REMOTE_PROC_DOMAIN_EVENT_REBOOT:
        event = remoteDomainReadEventReboot(conn, xdr);
        break;

9638 9639 9640 9641
    case REMOTE_PROC_DOMAIN_EVENT_RTC_CHANGE:
        event = remoteDomainReadEventRTCChange(conn, xdr);
        break;

9642 9643 9644 9645
    case REMOTE_PROC_DOMAIN_EVENT_WATCHDOG:
        event = remoteDomainReadEventWatchdog(conn, xdr);
        break;

9646 9647 9648 9649
    case REMOTE_PROC_DOMAIN_EVENT_IO_ERROR:
        event = remoteDomainReadEventIOError(conn, xdr);
        break;

9650 9651 9652 9653
    case REMOTE_PROC_DOMAIN_EVENT_IO_ERROR_REASON:
        event = remoteDomainReadEventIOErrorReason(conn, xdr);
        break;

9654 9655 9656 9657
    case REMOTE_PROC_DOMAIN_EVENT_GRAPHICS:
        event = remoteDomainReadEventGraphics(conn, xdr);
        break;

9658
    default:
9659
        DEBUG("Unexpected event proc %d", hdr->proc);
9660
        break;
9661
    }
9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672

    if (!event)
        return -1;

    if (virDomainEventQueuePush(priv->domainEvents,
                                event) < 0) {
        DEBUG0("Error adding event to queue");
        virDomainEventFree(event);
    }
    virEventUpdateTimeout(priv->eventFlushTimer, 0);

9673 9674 9675
    return 0;
}

9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754
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) {
        VIR_WARN("No registered stream matching serial=%d, proc=%d",
                 hdr->serial, hdr->proc);
        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;
        VIR_WARN0("Got a stream data packet");

        /* 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) {
                VIR_WARN0("Out of memory");
                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) {
            VIR_WARN("Got sync data packet offset=%d", privst->incomingOffset);
            thecall->mode = REMOTE_MODE_COMPLETE;
        } else {
            VIR_WARN("Got aysnc data packet offset=%d", privst->incomingOffset);
        }
        return 0;
    }

    case REMOTE_OK:
        VIR_WARN0("Got a synchronous confirm");
        if (!thecall) {
            VIR_WARN0("Got unexpected stream finish confirmation");
            return -1;
        }
        thecall->mode = REMOTE_MODE_COMPLETE;
        return 0;

    case REMOTE_ERROR:
        if (thecall && thecall->want_reply) {
            VIR_WARN0("Got a synchronous error");
            /* Give the error straight to this call */
            memset (&thecall->err, 0, sizeof thecall->err);
            if (!xdr_remote_error (xdr, &thecall->err)) {
9755
                remoteError(VIR_ERR_RPC, "%s", _("unmarshalling remote_error"));
9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780
                return -1;
            }
            thecall->mode = REMOTE_MODE_ERROR;
        } else {
            VIR_WARN0("Got a asynchronous error");
            /* No call, so queue the error against the stream */
            if (privst->has_error) {
                VIR_WARN0("Got unexpected duplicate stream error");
                return -1;
            }
            privst->has_error = 1;
            memset (&privst->err, 0, sizeof privst->err);
            if (!xdr_remote_error (xdr, &privst->err)) {
                VIR_WARN0("Failed to unmarshall error");
                return -1;
            }
        }
        return 0;

    default:
        VIR_WARN("Stream with unexpected serial=%d, proc=%d, status=%d",
                 hdr->serial, hdr->proc, hdr->status);
        return -1;
    }
}
9781 9782

static int
9783
remoteIOHandleInput(virConnectPtr conn, struct private_data *priv,
C
Chris Lalancette 已提交
9784
                    int flags)
9785
{
9786
    /* Read as much data as is available, until we get
9787
     * EAGAIN
9788
     */
9789
    for (;;) {
9790
        int ret = remoteIOReadMessage(priv);
9791

9792 9793 9794 9795
        if (ret < 0)
            return -1;
        if (ret == 0)
            return 0;  /* Blocking on read */
9796

9797 9798 9799
        /* Check for completion of our goal */
        if (priv->bufferOffset == priv->bufferLength) {
            if (priv->bufferOffset == 4) {
9800
                ret = remoteIODecodeMessageLength(priv);
9801 9802 9803 9804 9805 9806 9807 9808 9809
                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.
                 */
9810
            } else {
C
Chris Lalancette 已提交
9811
                ret = processCallDispatch(conn, priv, flags);
9812
                priv->bufferOffset = priv->bufferLength = 0;
9813 9814 9815 9816 9817 9818 9819 9820
                /*
                 * We've completed one call, so return even
                 * though there might still be more data on
                 * the wire. We need to actually let the caller
                 * deal with this arrived message to keep good
                 * response, and also to correctly handle EOF.
                 */
                return ret;
9821 9822 9823
            }
        }
    }
9824 9825
}

9826 9827 9828 9829 9830
/*
 * Process all calls pending dispatch/receive until we
 * get a reply to our own call. Then quit and pass the buck
 * to someone else.
 */
9831
static int
9832 9833
remoteIOEventLoop(virConnectPtr conn,
                  struct private_data *priv,
C
Chris Lalancette 已提交
9834
                  int flags,
9835
                  struct remote_thread_call *thiscall)
9836
{
9837 9838
    struct pollfd fds[2];
    int ret;
9839

9840 9841 9842 9843 9844 9845 9846
    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;
9847
#ifdef HAVE_PTHREAD_SIGMASK
9848
        sigset_t oldmask, blockedsigs;
9849
#endif
9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863

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

9864 9865 9866
        if (priv->streams)
            fds[0].events |= POLLIN;

9867 9868 9869 9870
        /* Release lock while poll'ing so other threads
         * can stuff themselves on the queue */
        remoteDriverUnlock(priv);

9871 9872 9873 9874 9875
        /* 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.
         */
9876
#ifdef HAVE_PTHREAD_SIGMASK
9877 9878 9879 9880
        sigemptyset (&blockedsigs);
        sigaddset (&blockedsigs, SIGWINCH);
        sigaddset (&blockedsigs, SIGCHLD);
        sigaddset (&blockedsigs, SIGPIPE);
9881
        ignore_value(pthread_sigmask(SIG_BLOCK, &blockedsigs, &oldmask));
9882
#endif
9883

9884 9885
    repoll:
        ret = poll(fds, ARRAY_CARDINALITY(fds), -1);
9886
        if (ret < 0 && errno == EAGAIN)
9887
            goto repoll;
9888

9889
#ifdef HAVE_PTHREAD_SIGMASK
9890
        ignore_value(pthread_sigmask(SIG_SETMASK, &oldmask, NULL));
9891
#endif
9892

9893 9894 9895
        remoteDriverLock(priv);

        if (fds[1].revents) {
9896
            ssize_t s;
9897
            DEBUG0("Woken up from poll by other thread");
9898 9899 9900 9901 9902 9903 9904 9905 9906 9907
            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;
            }
9908 9909 9910 9911 9912
        }

        if (ret < 0) {
            if (errno == EWOULDBLOCK)
                continue;
9913
            virReportSystemError(errno,
9914
                                 "%s", _("poll on socket failed"));
9915
            goto error;
9916 9917 9918
        }

        if (fds[0].revents & POLLOUT) {
9919
            if (remoteIOHandleOutput(priv) < 0)
9920
                goto error;
9921
        }
9922 9923

        if (fds[0].revents & POLLIN) {
C
Chris Lalancette 已提交
9924
            if (remoteIOHandleInput(conn, priv, flags) < 0)
9925
                goto error;
9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949
        }

        /* 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...
                 */
                DEBUG("Waking up sleep %d %p %p", tmp->proc_nr, tmp, priv->waitDispatch);
                virCondSignal(&tmp->cond);
9950
            }
9951 9952
            prev = tmp;
            tmp = tmp->next;
9953 9954
        }

9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970
        /* 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;
            DEBUG("Giving up the buck %d %p %p", thiscall->proc_nr, thiscall, priv->waitDispatch);
            /* See if someone else is still waiting
             * and if so, then pass the buck ! */
            if (priv->waitDispatch) {
                DEBUG("Passing the buck to %d %p", priv->waitDispatch->proc_nr, priv->waitDispatch);
                virCondSignal(&priv->waitDispatch->cond);
            }
            return 0;
        }
9971

9972 9973

        if (fds[0].revents & (POLLHUP | POLLERR)) {
9974 9975
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("received hangup / error event on socket"));
9976
            goto error;
9977 9978
        }
    }
9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990


error:
    priv->waitDispatch = thiscall->next;
    DEBUG("Giving up the buck due to I/O error %d %p %p", thiscall->proc_nr, thiscall, priv->waitDispatch);
    /* See if someone else is still waiting
     * and if so, then pass the buck ! */
    if (priv->waitDispatch) {
        DEBUG("Passing the buck to %d %p", priv->waitDispatch->proc_nr, priv->waitDispatch);
        virCondSignal(&priv->waitDispatch->cond);
    }
    return -1;
9991 9992
}

9993
/*
9994
 * This function sends a message to remote server and awaits a reply
9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008
 *
 * 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
10009
 * to sleep on condition variables. The existing thread may completely
10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025
 * 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!
 */
10026
static int
10027 10028 10029 10030
remoteIO(virConnectPtr conn,
         struct private_data *priv,
         int flags,
         struct remote_thread_call *thiscall)
10031
{
10032 10033
    int rv;

10034 10035 10036
    DEBUG("Do proc=%d serial=%d length=%d wait=%p",
          thiscall->proc_nr, thiscall->serial,
          thiscall->bufferLength, priv->waitDispatch);
10037

10038 10039 10040 10041 10042
    /* 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;
10043
        ssize_t s;
10044 10045 10046 10047 10048 10049
        while (tmp && tmp->next)
            tmp = tmp->next;
        if (tmp)
            tmp->next = thiscall;
        else
            priv->waitDispatch = thiscall;
10050

10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065
        /* 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));
            VIR_FREE(thiscall);
            return -1;
        } else if (s != sizeof(ignore)) {
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("failed to wake up polling thread"));
            VIR_FREE(thiscall);
            return -1;
        }
10066

10067
        DEBUG("Going to sleep %d %p %p", thiscall->proc_nr, priv->waitDispatch, thiscall);
10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080
        /* 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;
            }
10081 10082
            remoteError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("failed to wait on condition"));
10083
            VIR_FREE(thiscall);
10084
            return -1;
10085
        }
10086

10087
        DEBUG("Wokeup from sleep %d %p %p", thiscall->proc_nr, priv->waitDispatch, thiscall);
10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101
        /* 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;
10102
        }
10103 10104 10105

        /* Grr, someone passed the buck onto us ... */

10106
    } else {
10107 10108 10109 10110
        /* We're first to catch the buck */
        priv->waitDispatch = thiscall;
    }

10111
    DEBUG("We have the buck %d %p %p", thiscall->proc_nr, priv->waitDispatch, thiscall);
10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128
    /*
     * 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 已提交
10129
    rv = remoteIOEventLoop(conn, priv, flags, thiscall);
10130 10131 10132 10133 10134 10135 10136 10137 10138 10139

    if (priv->watch >= 0)
        virEventUpdateHandle(priv->watch, VIR_EVENT_HANDLE_READABLE);

    if (rv < 0) {
        VIR_FREE(thiscall);
        return -1;
    }

cleanup:
10140
    DEBUG("All done with our call %d %p %p", thiscall->proc_nr, priv->waitDispatch, thiscall);
10141 10142 10143 10144 10145 10146 10147
    if (thiscall->mode == REMOTE_MODE_ERROR) {
        /* 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 &&
10148
            thiscall->err.message &&
10149 10150
            STRPREFIX(*thiscall->err.message, "unknown procedure")) {
            rv = -2;
10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171
        } 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;
10172
        } else {
10173 10174 10175 10176 10177 10178 10179 10180 10181 10182
            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,
10183
                              "%s", thiscall->err.message ? *thiscall->err.message : "unknown");
10184
            rv = -1;
10185
        }
10186
        xdr_free((xdrproc_t)xdr_remote_error,  (char *)&thiscall->err);
10187 10188
    } else {
        rv = 0;
10189
    }
10190 10191 10192
    VIR_FREE(thiscall);
    return rv;
}
10193

10194 10195 10196 10197 10198 10199 10200

/*
 * 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 已提交
10201
      int flags,
10202 10203 10204 10205 10206 10207
      int proc_nr,
      xdrproc_t args_filter, char *args,
      xdrproc_t ret_filter, char *ret)
{
    struct remote_thread_call *thiscall;

C
Chris Lalancette 已提交
10208
    thiscall = prepareCall(priv, flags, proc_nr, args_filter, args,
10209 10210 10211
                           ret_filter, ret);

    if (!thiscall) {
10212
        virReportOOMError();
10213 10214 10215 10216 10217 10218 10219
        return -1;
    }

    return remoteIO(conn, priv, flags, thiscall);
}


10220 10221 10222 10223 10224 10225 10226 10227 10228 10229
/** remoteDomainEventFired:
 *
 * The callback for monitoring the remote socket
 * for event data
 */
void
remoteDomainEventFired(int watch,
                       int fd,
                       int event,
                       void *opaque)
10230
{
10231 10232
    virConnectPtr        conn = opaque;
    struct private_data *priv = conn->privateData;
10233

10234
    remoteDriverLock(priv);
10235

10236 10237 10238
    /* This should be impossible, but it doesn't hurt to check */
    if (priv->waitDispatch)
        goto done;
10239

10240
    DEBUG("Event fired %d %d %d %X", watch, fd, event, event);
10241

10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255
    if (event & (VIR_EVENT_HANDLE_HANGUP | VIR_EVENT_HANDLE_ERROR)) {
         DEBUG("%s : VIR_EVENT_HANDLE_HANGUP or "
               "VIR_EVENT_HANDLE_ERROR encountered", __FUNCTION__);
         virEventRemoveHandle(watch);
         priv->watch = -1;
         goto done;
    }

    if (fd != priv->sock) {
        virEventRemoveHandle(watch);
        priv->watch = -1;
        goto done;
    }

10256
    if (remoteIOHandleInput(conn, priv, 0) < 0)
10257 10258 10259 10260
        DEBUG0("Something went wrong during async message processing");

done:
    remoteDriverUnlock(priv);
10261 10262
}

10263 10264
static void remoteDomainEventDispatchFunc(virConnectPtr conn,
                                          virDomainEventPtr event,
10265
                                          virConnectDomainEventGenericCallback cb,
10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276
                                          void *cbopaque,
                                          void *opaque)
{
    struct private_data *priv = opaque;

    /* Drop the lock whle dispatching, for sake of re-entrancy */
    remoteDriverUnlock(priv);
    virDomainEventDispatchDefaultFunc(conn, event, cb, cbopaque, NULL);
    remoteDriverLock(priv);
}

10277 10278
void
remoteDomainEventQueueFlush(int timer ATTRIBUTE_UNUSED, void *opaque)
10279
{
10280 10281
    virConnectPtr conn = opaque;
    struct private_data *priv = conn->privateData;
10282
    virDomainEventQueue tempQueue;
10283 10284 10285

    remoteDriverLock(priv);

10286 10287 10288 10289 10290 10291 10292 10293 10294 10295
    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;

    virDomainEventQueueDispatch(&tempQueue, priv->callbackList,
                                remoteDomainEventDispatchFunc, priv);
10296 10297
    virEventUpdateTimeout(priv->eventFlushTimer, -1);

10298 10299 10300 10301 10302
    /* Purge any deleted callbacks */
    virDomainEventCallbackListPurgeMarked(priv->callbackList);

    priv->domainEventDispatching = 0;

10303
    remoteDriverUnlock(priv);
10304 10305
}

10306

10307 10308
/* get_nonnull_domain and get_nonnull_network turn an on-wire
 * (name, uuid) pair into virDomainPtr or virNetworkPtr object.
10309
 * These can return NULL if underlying memory allocations fail,
10310
 * but if they do then virterror_internal.has been set.
10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326
 */
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 已提交
10327
static virInterfacePtr
10328
get_nonnull_interface (virConnectPtr conn, remote_nonnull_interface iface)
D
Daniel Veillard 已提交
10329
{
10330
    return virGetInterface (conn, iface.name, iface.mac);
D
Daniel Veillard 已提交
10331 10332
}

10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344
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);
}

10345 10346 10347 10348 10349 10350
static virNodeDevicePtr
get_nonnull_node_device (virConnectPtr conn, remote_nonnull_node_device dev)
{
    return virGetNodeDevice(conn, dev.name);
}

10351 10352 10353
static virSecretPtr
get_nonnull_secret (virConnectPtr conn, remote_nonnull_secret secret)
{
10354
    return virGetSecret(conn, BAD_CAST secret.uuid, secret.usageType, secret.usageID);
10355 10356
}

10357 10358 10359 10360 10361 10362
static virNWFilterPtr
get_nonnull_nwfilter (virConnectPtr conn, remote_nonnull_nwfilter nwfilter)
{
    return virGetNWFilter (conn, nwfilter.name, BAD_CAST nwfilter.uuid);
}

C
Chris Lalancette 已提交
10363 10364 10365 10366 10367 10368
static virDomainSnapshotPtr
get_nonnull_domain_snapshot (virDomainPtr domain, remote_nonnull_domain_snapshot snapshot)
{
    return virGetDomainSnapshot(domain, snapshot.name);
}

10369

10370 10371 10372 10373
/* Make remote_nonnull_domain and remote_nonnull_network. */
static void
make_nonnull_domain (remote_nonnull_domain *dom_dst, virDomainPtr dom_src)
{
10374
    dom_dst->id = dom_src->id;
10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385
    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 已提交
10386 10387 10388 10389 10390 10391 10392 10393
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;
}

10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408
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;
}

10409 10410 10411
static void
make_nonnull_secret (remote_nonnull_secret *secret_dst, virSecretPtr secret_src)
{
10412
    memcpy (secret_dst->uuid, secret_src->uuid, VIR_UUID_BUFLEN);
10413 10414
    secret_dst->usageType = secret_src->usageType;
    secret_dst->usageID = secret_src->usageID;
10415 10416
}

10417 10418 10419 10420 10421 10422 10423
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 已提交
10424 10425 10426 10427 10428 10429 10430
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);
}

10431 10432
/*----------------------------------------------------------------------*/

10433 10434 10435 10436 10437
unsigned long remoteVersion(void)
{
    return REMOTE_PROTOCOL_VERSION;
}

10438
static virDriver remote_driver = {
10439 10440 10441 10442 10443 10444 10445
    VIR_DRV_REMOTE,
    "remote",
    remoteOpen, /* open */
    remoteClose, /* close */
    remoteSupportsFeature, /* supports_feature */
    remoteType, /* type */
    remoteGetVersion, /* version */
10446
    remoteGetLibVersion, /* libvirtVersion */
10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476
    remoteGetHostname, /* getHostname */
    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 */
    remoteDomainGetInfo, /* domainGetInfo */
    remoteDomainSave, /* domainSave */
    remoteDomainRestore, /* domainRestore */
    remoteDomainCoreDump, /* domainCoreDump */
    remoteDomainSetVcpus, /* domainSetVcpus */
    remoteDomainPinVcpu, /* domainPinVcpu */
    remoteDomainGetVcpus, /* domainGetVcpus */
    remoteDomainGetMaxVcpus, /* domainGetMaxVcpus */
    remoteDomainGetSecurityLabel, /* domainGetSecurityLabel */
    remoteNodeGetSecurityModel, /* nodeGetSecurityModel */
    remoteDomainDumpXML, /* domainDumpXML */
10477 10478
    remoteDomainXMLFromNative, /* domainXMLFromNative */
    remoteDomainXMLToNative, /* domainXMLToNative */
10479 10480 10481
    remoteListDefinedDomains, /* listDefinedDomains */
    remoteNumOfDefinedDomains, /* numOfDefinedDomains */
    remoteDomainCreate, /* domainCreate */
10482
    remoteDomainCreateWithFlags, /* domainCreateWithFlags */
10483 10484 10485
    remoteDomainDefineXML, /* domainDefineXML */
    remoteDomainUndefine, /* domainUndefine */
    remoteDomainAttachDevice, /* domainAttachDevice */
J
Jim Fehlig 已提交
10486
    remoteDomainAttachDeviceFlags, /* domainAttachDeviceFlags */
10487
    remoteDomainDetachDevice, /* domainDetachDevice */
J
Jim Fehlig 已提交
10488
    remoteDomainDetachDeviceFlags, /* domainDetachDeviceFlags */
10489
    remoteDomainUpdateDeviceFlags, /* domainUpdateDeviceFlags */
10490 10491 10492 10493 10494 10495 10496 10497 10498 10499
    remoteDomainGetAutostart, /* domainGetAutostart */
    remoteDomainSetAutostart, /* domainSetAutostart */
    remoteDomainGetSchedulerType, /* domainGetSchedulerType */
    remoteDomainGetSchedulerParameters, /* domainGetSchedulerParameters */
    remoteDomainSetSchedulerParameters, /* domainSetSchedulerParameters */
    remoteDomainMigratePrepare, /* domainMigratePrepare */
    remoteDomainMigratePerform, /* domainMigratePerform */
    remoteDomainMigrateFinish, /* domainMigrateFinish */
    remoteDomainBlockStats, /* domainBlockStats */
    remoteDomainInterfaceStats, /* domainInterfaceStats */
10500
    remoteDomainMemoryStats, /* domainMemoryStats */
10501 10502
    remoteDomainBlockPeek, /* domainBlockPeek */
    remoteDomainMemoryPeek, /* domainMemoryPeek */
10503
    remoteDomainGetBlockInfo, /* domainGetBlockInfo */
10504 10505 10506 10507 10508 10509 10510 10511 10512
    remoteNodeGetCellsFreeMemory, /* nodeGetCellsFreeMemory */
    remoteNodeGetFreeMemory, /* getFreeMemory */
    remoteDomainEventRegister, /* domainEventRegister */
    remoteDomainEventDeregister, /* domainEventDeregister */
    remoteDomainMigratePrepare2, /* domainMigratePrepare2 */
    remoteDomainMigrateFinish2, /* domainMigrateFinish2 */
    remoteNodeDeviceDettach, /* nodeDeviceDettach */
    remoteNodeDeviceReAttach, /* nodeDeviceReAttach */
    remoteNodeDeviceReset, /* nodeDeviceReset */
C
Chris Lalancette 已提交
10513
    remoteDomainMigratePrepareTunnel, /* domainMigratePrepareTunnel */
10514 10515 10516 10517
    remoteIsEncrypted, /* isEncrypted */
    remoteIsSecure, /* isSecure */
    remoteDomainIsActive, /* domainIsActive */
    remoteDomainIsPersistent, /* domainIsPersistent */
J
Jiri Denemark 已提交
10518
    remoteCPUCompare, /* cpuCompare */
10519
    remoteCPUBaseline, /* cpuBaseline */
10520
    remoteDomainGetJobInfo, /* domainGetJobInfo */
10521
    remoteDomainAbortJob, /* domainFinishJob */
10522
    remoteDomainMigrateSetMaxDowntime, /* domainMigrateSetMaxDowntime */
10523 10524
    remoteDomainEventRegisterAny, /* domainEventRegisterAny */
    remoteDomainEventDeregisterAny, /* domainEventDeregisterAny */
10525 10526 10527
    remoteDomainManagedSave, /* domainManagedSave */
    remoteDomainHasManagedSaveImage, /* domainHasManagedSaveImage */
    remoteDomainManagedSaveRemove, /* domainManagedSaveRemove */
C
Chris Lalancette 已提交
10528 10529 10530 10531 10532 10533 10534 10535 10536
    remoteDomainSnapshotCreateXML, /* domainSnapshotCreateXML */
    remoteDomainSnapshotDumpXML, /* domainSnapshotDumpXML */
    remoteDomainSnapshotNum, /* domainSnapshotNum */
    remoteDomainSnapshotListNames, /* domainSnapshotListNames */
    remoteDomainSnapshotLookupByName, /* domainSnapshotLookupByName */
    remoteDomainHasCurrentSnapshot, /* domainHasCurrentSnapshot */
    remoteDomainSnapshotCurrent, /* domainSnapshotCurrent */
    remoteDomainRevertToSnapshot, /* domainRevertToSnapshot */
    remoteDomainSnapshotDelete, /* domainSnapshotDelete */
C
Chris Lalancette 已提交
10537
    remoteQemuDomainMonitorCommand, /* qemuDomainMonitorCommand */
10538 10539
    remoteDomainSetMemoryParameters, /* domainSetMemoryParameters */
    remoteDomainGetMemoryParameters, /* domainGetMemoryParameters */
10540 10541 10542
};

static virNetworkDriver network_driver = {
10543
    .name = "remote",
10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560
    .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,
10561 10562
    .networkIsActive = remoteNetworkIsActive,
    .networkIsPersistent = remoteNetworkIsPersistent,
10563 10564
};

D
Daniel Veillard 已提交
10565 10566 10567 10568 10569 10570
static virInterfaceDriver interface_driver = {
    .name = "remote",
    .open = remoteInterfaceOpen,
    .close = remoteInterfaceClose,
    .numOfInterfaces = remoteNumOfInterfaces,
    .listInterfaces = remoteListInterfaces,
10571 10572
    .numOfDefinedInterfaces = remoteNumOfDefinedInterfaces,
    .listDefinedInterfaces = remoteListDefinedInterfaces,
D
Daniel Veillard 已提交
10573 10574 10575 10576 10577 10578 10579
    .interfaceLookupByName = remoteInterfaceLookupByName,
    .interfaceLookupByMACString = remoteInterfaceLookupByMACString,
    .interfaceGetXMLDesc = remoteInterfaceGetXMLDesc,
    .interfaceDefineXML = remoteInterfaceDefineXML,
    .interfaceUndefine = remoteInterfaceUndefine,
    .interfaceCreate = remoteInterfaceCreate,
    .interfaceDestroy = remoteInterfaceDestroy,
10580
    .interfaceIsActive = remoteInterfaceIsActive,
D
Daniel Veillard 已提交
10581 10582
};

10583 10584 10585 10586 10587 10588 10589 10590
static virStorageDriver storage_driver = {
    .name = "remote",
    .open = remoteStorageOpen,
    .close = remoteStorageClose,
    .numOfPools = remoteNumOfStoragePools,
    .listPools = remoteListStoragePools,
    .numOfDefinedPools = remoteNumOfDefinedStoragePools,
    .listDefinedPools = remoteListDefinedStoragePools,
10591
    .findPoolSources = remoteFindStoragePoolSources,
10592
    .poolLookupByName = remoteStoragePoolLookupByName,
10593
    .poolLookupByUUID = remoteStoragePoolLookupByUUID,
10594 10595 10596
    .poolLookupByVolume = remoteStoragePoolLookupByVolume,
    .poolCreateXML = remoteStoragePoolCreateXML,
    .poolDefineXML = remoteStoragePoolDefineXML,
10597
    .poolBuild = remoteStoragePoolBuild,
10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613
    .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,
10614
    .volCreateXMLFrom = remoteStorageVolCreateXMLFrom,
10615
    .volDelete = remoteStorageVolDelete,
10616
    .volWipe = remoteStorageVolWipe,
10617 10618 10619
    .volGetInfo = remoteStorageVolGetInfo,
    .volGetXMLDesc = remoteStorageVolDumpXML,
    .volGetPath = remoteStorageVolGetPath,
10620 10621
    .poolIsActive = remoteStoragePoolIsActive,
    .poolIsPersistent = remoteStoragePoolIsPersistent,
10622 10623
};

10624 10625 10626 10627 10628 10629
static virSecretDriver secret_driver = {
    .name = "remote",
    .open = remoteSecretOpen,
    .close = remoteSecretClose,
    .numOfSecrets = remoteSecretNumOfSecrets,
    .listSecrets = remoteSecretListSecrets,
10630
    .lookupByUUID = remoteSecretLookupByUUID,
10631
    .lookupByUsage = remoteSecretLookupByUsage,
10632 10633 10634 10635 10636 10637 10638
    .defineXML = remoteSecretDefineXML,
    .getXMLDesc = remoteSecretGetXMLDesc,
    .setValue = remoteSecretSetValue,
    .getValue = remoteSecretGetValue,
    .undefine = remoteSecretUndefine
};

10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649
static virDeviceMonitor dev_monitor = {
    .name = "remote",
    .open = remoteDevMonOpen,
    .close = remoteDevMonClose,
    .numOfDevices = remoteNodeNumOfDevices,
    .listDevices = remoteNodeListDevices,
    .deviceLookupByName = remoteNodeDeviceLookupByName,
    .deviceDumpXML = remoteNodeDeviceDumpXML,
    .deviceGetParent = remoteNodeDeviceGetParent,
    .deviceNumOfCaps = remoteNodeDeviceNumOfCaps,
    .deviceListCaps = remoteNodeDeviceListCaps,
10650 10651
    .deviceCreateXML = remoteNodeDeviceCreateXML,
    .deviceDestroy = remoteNodeDeviceDestroy
10652 10653
};

10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666
static virNWFilterDriver nwfilter_driver = {
    .name = "remote",
    .open = remoteNWFilterOpen,
    .close = remoteNWFilterClose,
    .nwfilterLookupByUUID = remoteNWFilterLookupByUUID,
    .nwfilterLookupByName = remoteNWFilterLookupByName,
    .getXMLDesc           = remoteNWFilterGetXMLDesc,
    .defineXML            = remoteNWFilterDefineXML,
    .undefine             = remoteNWFilterUndefine,
    .numOfNWFilters       = remoteNumOfNWFilters,
    .listNWFilters        = remoteListNWFilters,
};

10667

A
Atsushi SAKAI 已提交
10668
#ifdef WITH_LIBVIRTD
10669
static virStateDriver state_driver = {
10670
    .name = "Remote",
10671
    .initialize = remoteStartup,
10672
};
A
Atsushi SAKAI 已提交
10673
#endif
10674 10675


10676
/** remoteRegister:
10677 10678
 *
 * Register driver with libvirt driver system.
10679 10680
 *
 * Returns -1 on error.
10681 10682 10683 10684
 */
int
remoteRegister (void)
{
10685
    if (virRegisterDriver (&remote_driver) == -1) return -1;
10686
    if (virRegisterNetworkDriver (&network_driver) == -1) return -1;
D
Daniel Veillard 已提交
10687
    if (virRegisterInterfaceDriver (&interface_driver) == -1) return -1;
10688
    if (virRegisterStorageDriver (&storage_driver) == -1) return -1;
10689
    if (virRegisterDeviceMonitor (&dev_monitor) == -1) return -1;
10690
    if (virRegisterSecretDriver (&secret_driver) == -1) return -1;
10691
    if (virRegisterNWFilterDriver(&nwfilter_driver) == -1) return -1;
A
Atsushi SAKAI 已提交
10692
#ifdef WITH_LIBVIRTD
10693
    if (virRegisterStateDriver (&state_driver) == -1) return -1;
A
Atsushi SAKAI 已提交
10694
#endif
10695 10696 10697

    return 0;
}