proxy_internal.c 28.6 KB
Newer Older
1 2 3
/*
 * proxy_client.c: client side of the communication with the libvirt proxy.
 *
4
 * Copyright (C) 2006, 2008 Red Hat, Inc.
5 6 7 8 9 10
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

11
#include <config.h>
J
Jim Meyering 已提交
12

13 14 15 16 17 18 19 20 21 22
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
23
#include <string.h>
24
#include "internal.h"
25 26
#include "driver.h"
#include "proxy_internal.h"
27
#include "util.h"
28
#include "xen_unified.h"
29
#include "memory.h"
30 31 32

#define STANDALONE

33 34 35
static int debug = 0;

static int xenProxyClose(virConnectPtr conn);
36
static int xenProxyOpen(virConnectPtr conn, xmlURIPtr uri, virConnectAuthPtr auth, int flags);
37 38
static int xenProxyGetVersion(virConnectPtr conn, unsigned long *hvVer);
static int xenProxyNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
39
static char *xenProxyGetCapabilities(virConnectPtr conn);
40 41 42 43
static int xenProxyListDomains(virConnectPtr conn, int *ids, int maxids);
static int xenProxyNumOfDomains(virConnectPtr conn);
static unsigned long xenProxyDomainGetMaxMemory(virDomainPtr domain);
static int xenProxyDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info);
44
static char *xenProxyDomainGetOSType(virDomainPtr domain);
45

46
struct xenUnifiedDriver xenProxyDriver = {
47 48 49
    xenProxyOpen, /* open */
    xenProxyClose, /* close */
    xenProxyGetVersion, /* version */
50 51
    NULL, /* hostname */
    NULL, /* URI */
52
    xenProxyNodeGetInfo, /* nodeGetInfo */
53
    xenProxyGetCapabilities, /* getCapabilities */
54 55 56 57 58 59 60 61
    xenProxyListDomains, /* listDomains */
    xenProxyNumOfDomains, /* numOfDomains */
    NULL, /* domainCreateLinux */
    NULL, /* domainSuspend */
    NULL, /* domainResume */
    NULL, /* domainShutdown */
    NULL, /* domainReboot */
    NULL, /* domainDestroy */
62
    xenProxyDomainGetOSType, /* domainGetOSType */
63 64 65 66 67
    xenProxyDomainGetMaxMemory, /* domainGetMaxMemory */
    NULL, /* domainSetMaxMemory */
    NULL, /* domainSetMemory */
    xenProxyDomainGetInfo, /* domainGetInfo */
    NULL, /* domainSave */
68
    NULL, /* domainRestore */
D
Daniel Veillard 已提交
69
    NULL, /* domainCoreDump */
70 71
    NULL, /* domainSetVcpus */
    NULL, /* domainPinVcpu */
72
    NULL, /* domainGetVcpus */
73
    NULL, /* domainGetMaxVcpus */
74 75 76 77 78
    NULL, /* listDefinedDomains */
    NULL, /* numOfDefinedDomains */
    NULL, /* domainCreate */
    NULL, /* domainDefineXML */
    NULL, /* domainUndefine */
79 80
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
81 82
    NULL, /* domainGetAutostart */
    NULL, /* domainSetAutostart */
83 84 85
    NULL, /* domainGetSchedulerType */
    NULL, /* domainGetSchedulerParameters */
    NULL, /* domainSetSchedulerParameters */
86
};
87

88

89 90 91 92 93 94 95 96 97
/************************************************************************
 *									*
 *			Error handling					*
 *									*
 ************************************************************************/

/**
 * virProxyError:
 * @conn: the connection if available
D
Daniel Veillard 已提交
98
 * @error: the error number
99 100 101 102 103 104 105
 * @info: extra information string
 *
 * Handle an error at the xend daemon interface
 */
static void
virProxyError(virConnectPtr conn, virErrorNumber error, const char *info)
{
106 107
    const char *errmsg;

108 109 110 111
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
112
    __virRaiseError(conn, NULL, NULL, VIR_FROM_PROXY, error, VIR_ERR_ERROR,
113 114 115 116 117 118 119 120 121 122 123 124
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

/************************************************************************
 *									*
 *	Automatic startup of the proxy server if it is not running	*
 *									*
 ************************************************************************/
/**
 * virProxyFindServerPath:
 *
 * Tries to find the path to the gam_server binary.
125
 *
126 127 128 129 130 131 132
 * Returns path on success or NULL in case of error.
 */
static const char *
virProxyFindServerPath(void)
{
    static const char *serverPaths[] = {
        BINDIR "/libvirt_proxy",
133
        "/usr/bin/libvirt_proxy_dbg",
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
        NULL
    };
    int i;
    const char *debugProxy = getenv("LIBVIRT_DEBUG_PROXY");

    if (debugProxy)
        return(debugProxy);

    for (i = 0; serverPaths[i]; i++) {
        if (access(serverPaths[i], X_OK | R_OK) == 0) {
            return serverPaths[i];
        }
    }
    return NULL;
}

/**
 * virProxyForkServer:
 *
 * Forks and try to launch the proxy server processing the requests for
 * libvirt when communicating with Xen.
 *
 * Returns 0 in case of success or -1 in case of detected error.
 */
static int
virProxyForkServer(void)
{
    const char *proxyPath = virProxyFindServerPath();
    int ret, pid, status;
163
    const char *proxyarg[2];
164 165 166

    if (!proxyPath) {
        fprintf(stderr, "failed to find libvirt_proxy\n");
167
        return(-1);
168 169 170 171 172
    }

    if (debug)
        fprintf(stderr, "Asking to launch %s\n", proxyPath);

173 174 175 176 177 178
    proxyarg[0] = proxyPath;
    proxyarg[1] = NULL;

    if (virExec(NULL, proxyarg, NULL, NULL,
                &pid, -1, NULL, NULL, VIR_EXEC_DAEMON) < 0)
        fprintf(stderr, "Failed to fork libvirt_proxy\n");
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

    /*
     * do a waitpid on the intermediate process to avoid zombies.
     */
retry_wait:
    ret = waitpid(pid, &status, 0);
    if (ret < 0) {
        if (errno == EINTR)
            goto retry_wait;
    }

    return (0);
}

/************************************************************************
 *									*
 *		Processing of client sockets				*
 *									*
 ************************************************************************/

/**
 * virProxyOpenClientSocket:
201
 * @path: the filename for the socket
202 203 204 205 206 207 208 209 210 211 212 213 214 215
 *
 * try to connect to the socket open by libvirt_proxy
 *
 * Returns the associated file descriptor or -1 in case of failure
 */
static int
virProxyOpenClientSocket(const char *path) {
    int fd;
    struct sockaddr_un addr;
    int trials = 0;

retry:
    fd = socket(PF_UNIX, SOCK_STREAM, 0);
    if (fd < 0) {
216
        return(-1);
217 218 219
    }

    /*
220
     * Abstract socket do not hit the filesystem, way more secure and
D
Daniel Veillard 已提交
221
     * guaranteed to be atomic
222 223 224 225 226 227 228 229 230 231
     */
    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    addr.sun_path[0] = '\0';
    strncpy(&addr.sun_path[1], path, (sizeof(addr) - 4) - 2);

    /*
     * now bind the socket to that address and listen on it
     */
    if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
232 233 234 235 236 237 238 239 240
        close(fd);
        if (trials < 3) {
            if (virProxyForkServer() < 0)
                return(-1);
            trials++;
            usleep(5000 * trials * trials);
            goto retry;
        }
        return (-1);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    }

    if (debug > 0)
        fprintf(stderr, "connected to unix socket %s via %d\n", path, fd);

    return (fd);
}

/**
 * virProxyCloseClientSocket:
 * @fd: the file descriptor for the socket
 *
 * Close the socket from that client
 *
 * Returns 0 in case of success and -1 in case of error
 */
static int
virProxyCloseClientSocket(int fd) {
    int ret;

    if (fd < 0)
        return(-1);

    ret = close(fd);
    if (ret != 0)
266
        fprintf(stderr, _("Failed to close socket %d\n"), fd);
267
    else if (debug > 0)
268
        fprintf(stderr, "Closed socket %d\n", fd);
269 270 271 272 273
    return(ret);
}

/**
 * virProxyReadClientSocket:
274
 * @fd: the socket
275
 * @buffer: the target memory area
D
Daniel Veillard 已提交
276
 * @len: the length in bytes
277
 * @quiet: quiet access
278 279 280 281 282 283
 *
 * Process a read from a client socket
 *
 * Returns the number of byte read or -1 in case of error.
 */
static int
284
virProxyReadClientSocket(int fd, char *buffer, int len, int quiet) {
285 286 287 288 289 290 291 292 293
    int ret;

    if ((fd < 0) || (buffer == NULL) || (len < 0))
        return(-1);

retry:
    ret = read(fd, buffer, len);
    if (ret < 0) {
        if (errno == EINTR) {
294 295 296 297 298
            if (debug > 0)
                fprintf(stderr, "read socket %d interrupted\n", fd);
            goto retry;
        }
        if (!quiet)
299
            fprintf(stderr, _("Failed to read socket %d\n"), fd);
300
        return(-1);
301 302 303
    }

    if (debug)
304 305
        fprintf(stderr, "read %d bytes from socket %d\n",
                ret, fd);
306 307 308 309 310
    return(ret);
}

/**
 * virProxyWriteClientSocket:
311
 * @fd: the socket
312
 * @data: the data
D
Daniel Veillard 已提交
313
 * @len: the length of data in bytes
314 315 316 317 318 319 320 321 322 323
 *
 * Process a read from a client socket
 */
static int
virProxyWriteClientSocket(int fd, const char *data, int len) {
    int ret;

    if ((fd < 0) || (data == NULL) || (len < 0))
        return(-1);

324
    ret = safewrite(fd, data, len);
325
    if (ret < 0) {
326
        fprintf(stderr, _("Failed to write to socket %d\n"), fd);
327
        return(-1);
328 329
    }
    if (debug)
330 331
        fprintf(stderr, "wrote %d bytes to socket %d\n",
                len, fd);
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

    return(0);
}

/************************************************************************
 *									*
 *			Proxy commands processing			*
 *									*
 ************************************************************************/

/**
 * xenProxyClose:
 * @conn: pointer to the hypervisor connection
 *
 * Shutdown the Xen proxy communication layer
 */
348
static int
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
xenProxyClose(virConnectPtr conn)
{
    xenUnifiedPrivatePtr priv;

    if (conn == NULL) {
        virProxyError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return -1;
    }

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (!priv) {
        virProxyError (NULL, VIR_ERR_INTERNAL_ERROR, __FUNCTION__);
        return -1;
    }

    /* Fail silently. */
    if (priv->proxy == -1)
        return -1;

    virProxyCloseClientSocket (priv->proxy);
    priv->proxy = -1;

    return 0;
372 373
}

374
static int
375
xenProxyCommand(virConnectPtr conn, virProxyPacketPtr request,
376
                virProxyFullPacketPtr answer, int quiet) {
377 378 379
    static int serial = 0;
    int ret;
    virProxyPacketPtr res = NULL;
380
    xenUnifiedPrivatePtr priv;
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395
    if (conn == NULL) {
        virProxyError (NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return -1;
    }

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (!priv) {
        virProxyError (NULL, VIR_ERR_INTERNAL_ERROR, __FUNCTION__);
        return -1;
    }

    /* Fail silently. */
    if (priv->proxy == -1)
        return -1;
396 397 398 399 400 401 402 403 404

    /*
     * normal communication serial numbers are in 0..4095
     */
    ++serial;
    if (serial >= 4096)
        serial = 0;
    request->version = PROXY_PROTO_VERSION;
    request->serial = serial;
405
    ret  = virProxyWriteClientSocket(priv->proxy, (const char *) request,
406 407 408 409 410 411
                                     request->len);
    if (ret < 0)
        return(-1);
retry:
    if (answer == NULL) {
        /* read in situ */
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
        ret  = virProxyReadClientSocket(priv->proxy, (char *) request,
                                        sizeof(virProxyPacket), quiet);
        if (ret < 0)
            return(-1);
        if (ret != sizeof(virProxyPacket)) {
            fprintf(stderr,
                    _("Communication error with proxy: got %d bytes of %d\n"),
                    ret, (int) sizeof(virProxyPacket));
            xenProxyClose(conn);
            return(-1);
        }
        res = request;
        if (res->len != sizeof(virProxyPacket)) {
            fprintf(stderr,
                    _("Communication error with proxy: expected %d bytes got %d\n"),
                    (int) sizeof(virProxyPacket), res->len);
            xenProxyClose(conn);
            return(-1);
        }
431
    } else {
432
        /* read in packet provided */
433
        ret  = virProxyReadClientSocket(priv->proxy, (char *) answer,
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
                                        sizeof(virProxyPacket), quiet);
        if (ret < 0)
            return(-1);
        if (ret != sizeof(virProxyPacket)) {
            fprintf(stderr,
                    _("Communication error with proxy: got %d bytes of %d\n"),
                    ret, (int) sizeof(virProxyPacket));
            xenProxyClose(conn);
            return(-1);
        }
        res = (virProxyPacketPtr) answer;
        if ((res->len < sizeof(virProxyPacket)) ||
            (res->len > sizeof(virProxyFullPacket))) {
            fprintf(stderr,
                    _("Communication error with proxy: got %d bytes packet\n"),
                    res->len);
            xenProxyClose(conn);
            return(-1);
        }
        if (res->len > sizeof(virProxyPacket)) {
            ret  = virProxyReadClientSocket(priv->proxy,
                                   (char *) &(answer->extra.arg[0]),
                                            res->len - ret, quiet);
            if (ret != (int) (res->len - sizeof(virProxyPacket))) {
                fprintf(stderr,
                        _("Communication error with proxy: got %d bytes of %d\n"),
                        ret, (int) sizeof(virProxyPacket));
                xenProxyClose(conn);
                return(-1);
            }
        }
465 466 467 468 469 470
    }
    /*
     * do more checks on the incoming packet.
     */
    if ((res == NULL) || (res->version != PROXY_PROTO_VERSION) ||
        (res->len < sizeof(virProxyPacket))) {
J
Jim Meyering 已提交
471 472 473 474
        fprintf(stderr, "%s",
                _("Communication error with proxy: malformed packet\n"));
        xenProxyClose(conn);
        return(-1);
475 476 477
    }
    if (res->serial != serial) {
        TODO /* Asynchronous communication */
478
        fprintf(stderr, _("got asynchronous packet number %d\n"), res->serial);
479 480 481 482 483 484
        goto retry;
    }
    return(0);
}

/**
485
 * xenProxyOpen:
486
 * @conn: pointer to the hypervisor connection
487 488
 * @name: URL for the target, NULL for local
 * @flags: combination of virDrvOpenFlag(s)
489 490
 *
 * Try to initialize the Xen proxy communication layer
491
 * This can be opened only for a read-only kind of access
492 493 494 495
 *
 * Returns 0 in case of success, and -1 in case of failure
 */
int
496 497 498 499
xenProxyOpen(virConnectPtr conn,
             xmlURIPtr uri ATTRIBUTE_UNUSED,
             virConnectAuthPtr auth ATTRIBUTE_UNUSED,
             int flags)
500
{
501 502 503
    virProxyPacket req;
    int ret;
    int fd;
504
    xenUnifiedPrivatePtr priv;
505

506
    if (!(flags & VIR_CONNECT_RO))
507
        return(-1);
508 509 510 511

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    priv->proxy = -1;

512 513
    fd = virProxyOpenClientSocket(PROXY_SOCKET_PATH);
    if (fd < 0) {
514
            virProxyError(NULL, VIR_ERR_NO_XEN, PROXY_SOCKET_PATH);
515
        return(-1);
516
    }
517
    priv->proxy = fd;
518 519 520 521

    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_NONE;
    req.len = sizeof(req);
522
    ret = xenProxyCommand(conn, &req, NULL, 1);
523
    if ((ret < 0) || (req.command != VIR_PROXY_NONE)) {
524
            virProxyError(NULL, VIR_ERR_OPERATION_FAILED, __FUNCTION__);
525
        xenProxyClose(conn);
526
        return(-1);
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    }
    return(0);
}

/************************************************************************
 *									*
 *			Driver entry points				*
 *									*
 ************************************************************************/

/**
 * xenProxyGetVersion:
 * @conn: pointer to the Xen Daemon block
 * @hvVer: return value for the version of the running hypervisor (OUT)
 *
 * Get the version level of the Hypervisor running.
 *
 * Returns -1 in case of error, 0 otherwise. if the version can't be
 *    extracted by lack of capacities returns 0 and @hvVer is 0, otherwise
 *    @hvVer value is major * 1,000,000 + minor * 1,000 + release
 */
static int
xenProxyGetVersion(virConnectPtr conn, unsigned long *hvVer)
{
    virProxyPacket req;
    int ret;

    if (!VIR_IS_CONNECT(conn)) {
        virProxyError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }
    if (hvVer == NULL) {
        virProxyError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
560
        return (-1);
561 562 563 564
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_VERSION;
    req.len = sizeof(req);
565
    ret = xenProxyCommand(conn, &req, NULL, 0);
566 567
    if (ret < 0) {
        xenProxyClose(conn);
568
        return(-1);
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
    }
    *hvVer = req.data.larg;
    return(0);
}

/**
 * xenProxyListDomains:
 * @conn: pointer to the hypervisor connection
 * @ids: array to collect the list of IDs of active domains
 * @maxids: size of @ids
 *
 * Collect the list of active domains, and store their ID in @maxids
 *
 * Returns the number of domain found or -1 in case of error
 */
static int
xenProxyListDomains(virConnectPtr conn, int *ids, int maxids)
{
587 588 589 590 591 592 593 594 595 596 597
    virProxyPacket req;
    virProxyFullPacket ans;
    int ret;
    int nb;

    if (!VIR_IS_CONNECT(conn)) {
        virProxyError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }
    if ((ids == NULL) || (maxids <= 0)) {
        virProxyError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
598
        return (-1);
599 600 601 602
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_LIST;
    req.len = sizeof(req);
603
    ret = xenProxyCommand(conn, &req, &ans, 0);
604 605
    if (ret < 0) {
        xenProxyClose(conn);
606
        return(-1);
607 608
    }
    nb = ans.data.arg;
609 610
    if ((nb > 1020) || (nb <= 0) ||
        (ans.len <= sizeof(virProxyPacket)) ||
611
        (ans.len > sizeof(virProxyFullPacket))) {
612
        virProxyError(conn, VIR_ERR_OPERATION_FAILED, __FUNCTION__);
613
        return(-1);
614
    }
615 616 617 618 619
    if (nb > maxids)
        nb = maxids;
    memmove(ids, &ans.extra.arg[0], nb * sizeof(int));

    return(nb);
620 621 622 623 624 625 626 627 628 629 630 631 632
}

/**
 * xenProxyNumOfDomains:
 * @conn: pointer to the hypervisor connection
 *
 * Provides the number of active domains.
 *
 * Returns the number of domain found or -1 in case of error
 */
static int
xenProxyNumOfDomains(virConnectPtr conn)
{
633 634 635 636 637 638 639 640 641 642
    virProxyPacket req;
    int ret;

    if (!VIR_IS_CONNECT(conn)) {
        virProxyError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_NUM_DOMAIN;
    req.len = sizeof(req);
643
    ret = xenProxyCommand(conn, &req, NULL, 0);
644 645
    if (ret < 0) {
        xenProxyClose(conn);
646
        return(-1);
647 648
    }
    return(req.data.arg);
649 650
}

651

652
/**
653
 * xenProxyDomainGetDomMaxMemory:
654 655 656
 * @conn: pointer to the hypervisor connection
 * @id: the domain ID number
 *
657
 * Ask the Xen Daemon for the maximum memory allowed for a domain
658
 *
659
 * Returns the memory size in kilobytes or 0 in case of error.
660
 */
661 662 663 664 665 666 667 668
static unsigned long
xenProxyDomainGetDomMaxMemory(virConnectPtr conn, int id)
{
    virProxyPacket req;
    int ret;

    if (!VIR_IS_CONNECT(conn)) {
        virProxyError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
669
        return (0);
670 671 672 673 674
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_MAX_MEMORY;
    req.data.arg = id;
    req.len = sizeof(req);
675
    ret = xenProxyCommand(conn, &req, NULL, 0);
676 677
    if (ret < 0) {
        xenProxyClose(conn);
678
        return(0);
679 680
    }
    return(req.data.larg);
681 682 683
}

/**
684 685
 * xenProxyDomainGetMaxMemory:
 * @domain: pointer to the domain block
686
 *
687
 * Ask the Xen Daemon for the maximum memory allowed for a domain
688
 *
689
 * Returns the memory size in kilobytes or 0 in case of error.
690
 */
691 692
static unsigned long
xenProxyDomainGetMaxMemory(virDomainPtr domain)
693
{
694
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
695 696 697 698
        if (domain == NULL)
            virProxyError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        else
            virProxyError(domain->conn, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
699 700
        return (0);
    }
701
    if (domain->id < 0)
702
        return (0);
703
    return(xenProxyDomainGetDomMaxMemory(domain->conn, domain->id));
704 705 706
}

/**
707 708 709
 * xenProxyDomainGetInfo:
 * @domain: a domain object
 * @info: pointer to a virDomainInfo structure allocated by the user
710
 *
711 712
 * This method looks up information about a domain and update the
 * information block provided.
713
 *
714
 * Returns 0 in case of success, -1 in case of error
715 716
 */
static int
717
xenProxyDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
718
{
719 720 721 722 723
    virProxyPacket req;
    virProxyFullPacket ans;
    int ret;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
724 725 726 727 728
        if (domain == NULL)
            virProxyError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        else
            virProxyError(domain->conn, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
729
    }
730
    if (domain->id < 0)
731
        return (-1);
732 733
    if (info == NULL) {
        virProxyError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
734
        return (-1);
735 736 737
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_DOMAIN_INFO;
738
    req.data.arg = domain->id;
739
    req.len = sizeof(req);
740
    ret = xenProxyCommand(domain->conn, &req, &ans, 0);
741 742
    if (ret < 0) {
        xenProxyClose(domain->conn);
743
        return(-1);
744 745 746
    }
    if (ans.len != sizeof(virProxyPacket) + sizeof(virDomainInfo)) {
        virProxyError(domain->conn, VIR_ERR_OPERATION_FAILED, __FUNCTION__);
747
        return (-1);
748 749
    }
    memmove(info, &ans.extra.dinfo, sizeof(virDomainInfo));
750

751 752
    return(0);
}
753 754

/**
755 756 757
 * xenProxyLookupByID:
 * @conn: pointer to the hypervisor connection
 * @id: the domain ID number
758
 *
759
 * Try to find a domain based on the hypervisor ID number
760
 *
761
 * Returns a new domain object or NULL in case of failure
762
 */
763
virDomainPtr
764
xenProxyLookupByID(virConnectPtr conn, int id)
765
{
766 767
    virProxyPacket req;
    virProxyFullPacket ans;
768
    unsigned char uuid[VIR_UUID_BUFLEN];
769 770 771 772 773 774 775 776
    const char *name;
    int ret;
    virDomainPtr res;

    if (!VIR_IS_CONNECT(conn)) {
        virProxyError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
777 778
    if (id < 0) {
        virProxyError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
779
        return (NULL);
780
    }
781 782 783 784
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_LOOKUP_ID;
    req.data.arg = id;
    req.len = sizeof(req);
785
    ret = xenProxyCommand(conn, &req, &ans, 0);
786 787
    if (ret < 0) {
        xenProxyClose(conn);
788
        return(NULL);
789
    }
790
    if (ans.data.arg == -1) {
791
        return(NULL);
792
    }
793 794
    memcpy(uuid, &ans.extra.str[0], VIR_UUID_BUFLEN);
    name = &ans.extra.str[VIR_UUID_BUFLEN];
795
    res = virGetDomain(conn, name, uuid);
796
        if (res) res->id = id;
797
    return(res);
798 799 800
}

/**
801 802 803
 * xenProxyLookupByUUID:
 * @conn: pointer to the hypervisor connection
 * @uuid: the raw UUID for the domain
804
 *
805
 * Try to lookup a domain on xend based on its UUID.
806
 *
807
 * Returns a new domain object or NULL in case of failure
808
 */
809
virDomainPtr
810
xenProxyLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
811
{
812 813 814 815 816 817 818 819 820 821 822
    virProxyFullPacket req;
    const char *name;
    int ret;
    virDomainPtr res;

    if (!VIR_IS_CONNECT(conn)) {
        virProxyError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (uuid == NULL) {
        virProxyError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
823
        return (NULL);
824 825 826
    }
    memset(&req, 0, sizeof(virProxyPacket));
    req.command = VIR_PROXY_LOOKUP_UUID;
827
    req.len = sizeof(virProxyPacket) + VIR_UUID_BUFLEN;
828 829
    memcpy(&req.extra.str[0], uuid, VIR_UUID_BUFLEN);

830
    ret = xenProxyCommand(conn, (virProxyPacketPtr) &req, &req, 0);
831 832
    if (ret < 0) {
        xenProxyClose(conn);
833
        return(NULL);
834 835
    }
    if (req.data.arg == -1) {
836
        return(NULL);
837 838 839
    }
    name = &req.extra.str[0];
    res = virGetDomain(conn, name, uuid);
840
        if (res) res->id = req.data.arg;
841
    return(res);
842 843
}

844
/**
845
 * xenProxyLookupByName:
846 847 848 849 850 851 852
 * @conn: A xend instance
 * @name: The name of the domain
 *
 * This method looks up information about a domain based on its name
 *
 * Returns a new domain object or NULL in case of failure
 */
853 854
virDomainPtr
xenProxyLookupByName(virConnectPtr conn, const char *name)
855
{
856 857 858 859 860 861 862 863 864 865
    virProxyFullPacket req;
    int ret, len;
    virDomainPtr res;

    if (!VIR_IS_CONNECT(conn)) {
        virProxyError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
    if (name == NULL) {
        virProxyError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
866
        return (NULL);
867 868 869 870
    }
    len = strlen(name);
    if (len > 1000) {
        virProxyError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
871
        return (NULL);
872 873 874 875 876
    }
    memset(&req, 0, sizeof(virProxyPacket));
    req.command = VIR_PROXY_LOOKUP_NAME;
    req.len = sizeof(virProxyPacket) + len + 1;
    strcpy(&req.extra.str[0], name);
877
    ret = xenProxyCommand(conn, (virProxyPacketPtr) &req, &req, 0);
878 879
    if (ret < 0) {
        xenProxyClose(conn);
880
        return(NULL);
881 882
    }
    if (req.data.arg == -1) {
883
        return(NULL);
884 885
    }
    res = virGetDomain(conn, name, (const unsigned char *)&req.extra.str[0]);
886
        if (res) res->id = req.data.arg;
887
    return(res);
888
}
889

890 891 892 893
/**
 * xenProxyNodeGetInfo:
 * @conn: pointer to the Xen Daemon block
 * @info: pointer to a virNodeInfo structure allocated by the user
894
 *
895 896 897 898 899 900
 * Extract hardware information about the node.
 *
 * Returns 0 in case of success and -1 in case of failure.
 */
static int
xenProxyNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info) {
901 902 903 904 905 906 907 908 909 910
    virProxyPacket req;
    virProxyFullPacket ans;
    int ret;

    if (!VIR_IS_CONNECT(conn)) {
        virProxyError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (-1);
    }
    if (info == NULL) {
        virProxyError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
911
        return (-1);
912 913 914 915 916
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_NODE_INFO;
    req.data.arg = 0;
    req.len = sizeof(req);
917
    ret = xenProxyCommand(conn, &req, &ans, 0);
918 919
    if (ret < 0) {
        xenProxyClose(conn);
920
        return(-1);
921 922
    }
    if (ans.data.arg == -1) {
923
        return(-1);
924 925
    }
    if (ans.len != sizeof(virProxyPacket) + sizeof(virNodeInfo)) {
926
        return(-1);
927 928 929
    }
    memcpy(info, &ans.extra.ninfo, sizeof(virNodeInfo));
    return(0);
930
}
931

932 933 934
/**
 * xenProxyGetCapabilities:
 * @conn: pointer to the Xen Daemon block
935
 *
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
 * Extract capabilities of the hypervisor.
 *
 * Returns capabilities in case of success (freed by caller)
 * and NULL in case of failure.
 */
static char *
xenProxyGetCapabilities (virConnectPtr conn)
{
    virProxyPacket req;
    virProxyFullPacket ans;
    int ret, xmllen;
    char *xml;

    if (!VIR_IS_CONNECT(conn)) {
        virProxyError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return NULL;
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_GET_CAPABILITIES;
    req.data.arg = 0;
    req.len = sizeof(req);
    ret = xenProxyCommand(conn, &req, &ans, 0);
    if (ret < 0) {
        xenProxyClose(conn);
        return NULL;
    }
    if (ans.data.arg == -1)
        return NULL;
    if (ans.len <= sizeof(virProxyPacket)) {
        virProxyError(conn, VIR_ERR_OPERATION_FAILED, __FUNCTION__);
        return NULL;
    }

    xmllen = ans.len - sizeof (virProxyPacket);
970
    if (VIR_ALLOC_N(xml, xmllen+1) < 0) {
971 972 973 974 975 976 977 978 979
        virProxyError (conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
        return NULL;
    }
    memmove (xml, ans.extra.str, xmllen);
    xml[xmllen] = '\0';

    return xml;
}

980 981 982 983 984 985 986
/**
 * xenProxyDomainDumpXML:
 * @domain: a domain object
 * @flags: xml generation flags
 *
 * This method generates an XML description of a domain.
 *
987
 * Returns the XML document on success, NULL otherwise.
988
 */
989
char *
990
xenProxyDomainDumpXML(virDomainPtr domain, int flags ATTRIBUTE_UNUSED)
991 992 993 994 995 996 997 998
{
    virProxyPacket req;
    virProxyFullPacket ans;
    int ret;
    int xmllen;
    char *xml;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
999 1000 1001 1002
        if (domain == NULL)
            virProxyError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        else
            virProxyError(domain->conn, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1003 1004
        return (NULL);
    }
1005
    if (domain->id < 0)
1006
        return (NULL);
1007 1008
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_DOMAIN_XML;
1009
    req.data.arg = domain->id;
1010 1011 1012 1013
    req.len = sizeof(req);
    ret = xenProxyCommand(domain->conn, &req, &ans, 0);
    if (ret < 0) {
        xenProxyClose(domain->conn);
1014
        return(NULL);
1015 1016 1017
    }
    if (ans.len <= sizeof(virProxyPacket)) {
        virProxyError(domain->conn, VIR_ERR_OPERATION_FAILED, __FUNCTION__);
1018
        return (NULL);
1019 1020
    }
    xmllen = ans.len - sizeof(virProxyPacket);
1021
    if (VIR_ALLOC_N(xml, xmllen+1) < 0) {
1022
        virProxyError(domain->conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
1023
        return NULL;
1024 1025 1026 1027 1028 1029
    }
    memmove(xml, &ans.extra.dinfo, xmllen);
    xml[xmllen] = '\0';

    return(xml);
}
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

/**
 * xenProxyDomainGetOSType:
 * @domain: a domain object
 *
 * Get the type of domain operation system.
 *
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
 */
static char *
xenProxyDomainGetOSType(virDomainPtr domain)
{
    virProxyPacket req;
    virProxyFullPacket ans;
    int ret;
    int oslen;
    char *ostype;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
1050 1051 1052 1053
        if (domain == NULL)
            virProxyError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        else
            virProxyError(domain->conn, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
1054 1055 1056 1057
        return (NULL);
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_DOMAIN_OSTYPE;
1058
    req.data.arg = domain->id;
1059 1060 1061 1062
    req.len = sizeof(req);
    ret = xenProxyCommand(domain->conn, &req, &ans, 0);
    if (ret < 0) {
        xenProxyClose(domain->conn);
1063
        return(NULL);
1064
    }
1065
    if ((ans.len == sizeof(virProxyPacket)) && (ans.data.arg < 0)) {
1066
        return(NULL);
1067 1068
    }

1069 1070
    if (ans.len <= sizeof(virProxyPacket)) {
        virProxyError(domain->conn, VIR_ERR_OPERATION_FAILED, __FUNCTION__);
1071
        return (NULL);
1072 1073
    }
    oslen = ans.len - sizeof(virProxyPacket);
1074
    if (VIR_ALLOC_N(ostype, oslen+1) < 0) {
1075
        virProxyError(domain->conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
1076 1077 1078 1079 1080 1081 1082
        return NULL;
    }
    memmove(ostype, &ans.extra.dinfo, oslen);
    ostype[oslen] = '\0';

    return(ostype);
}
1083

1084