proxy_internal.c 28.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * proxy_client.c: client side of the communication with the libvirt proxy.
 *
 * Copyright (C) 2006 Red Hat, Inc.
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

#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>
#include "internal.h"
22 23
#include "driver.h"
#include "proxy_internal.h"
24
#include "xen_unified.h"
25 26 27

#define STANDALONE

28 29 30 31 32 33
static int debug = 0;

static int xenProxyClose(virConnectPtr conn);
static int xenProxyOpen(virConnectPtr conn, const char *name, int flags);
static int xenProxyGetVersion(virConnectPtr conn, unsigned long *hvVer);
static int xenProxyNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info);
34
static char *xenProxyGetCapabilities(virConnectPtr conn);
35 36 37 38 39 40 41 42 43
static int xenProxyListDomains(virConnectPtr conn, int *ids, int maxids);
static int xenProxyNumOfDomains(virConnectPtr conn);
static virDomainPtr xenProxyLookupByID(virConnectPtr conn, int id);
static virDomainPtr xenProxyLookupByUUID(virConnectPtr conn,
					 const unsigned char *uuid);
static virDomainPtr xenProxyDomainLookupByName(virConnectPtr conn,
					       const char *domname);
static unsigned long xenProxyDomainGetMaxMemory(virDomainPtr domain);
static int xenProxyDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info);
44
static char *xenProxyDomainDumpXML(virDomainPtr domain, int flags);
45
static char *xenProxyDomainGetOSType(virDomainPtr domain);
46

47 48
virDriver xenProxyDriver = {
    -1,
49 50 51 52 53 54
    "XenProxy",
    0,
    xenProxyOpen, /* open */
    xenProxyClose, /* close */
    NULL, /* type */
    xenProxyGetVersion, /* version */
55
    NULL, /* getMaxVcpus */
56
    xenProxyNodeGetInfo, /* nodeGetInfo */
57
    xenProxyGetCapabilities, /* getCapabilities */
58 59 60 61 62 63 64 65 66 67 68
    xenProxyListDomains, /* listDomains */
    xenProxyNumOfDomains, /* numOfDomains */
    NULL, /* domainCreateLinux */
    xenProxyLookupByID, /* domainLookupByID */
    xenProxyLookupByUUID, /* domainLookupByUUID */
    xenProxyDomainLookupByName, /* domainLookupByName */
    NULL, /* domainSuspend */
    NULL, /* domainResume */
    NULL, /* domainShutdown */
    NULL, /* domainReboot */
    NULL, /* domainDestroy */
69
    xenProxyDomainGetOSType, /* domainGetOSType */
70 71 72 73 74
    xenProxyDomainGetMaxMemory, /* domainGetMaxMemory */
    NULL, /* domainSetMaxMemory */
    NULL, /* domainSetMemory */
    xenProxyDomainGetInfo, /* domainGetInfo */
    NULL, /* domainSave */
75
    NULL, /* domainRestore */
D
Daniel Veillard 已提交
76
    NULL, /* domainCoreDump */
77 78
    NULL, /* domainSetVcpus */
    NULL, /* domainPinVcpu */
79
    NULL, /* domainGetVcpus */
80
    NULL, /* domainGetMaxVcpus */
81
    xenProxyDomainDumpXML, /* domainDumpXML */
82 83 84 85 86
    NULL, /* listDefinedDomains */
    NULL, /* numOfDefinedDomains */
    NULL, /* domainCreate */
    NULL, /* domainDefineXML */
    NULL, /* domainUndefine */
87 88
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
89 90
    NULL, /* domainGetAutostart */
    NULL, /* domainSetAutostart */
91
};
92

93
/**
94
 * xenProxyInit:
95
 *
96
 * Initialise the xen proxy driver.
97
 */
98 99
int
xenProxyInit (void)
100
{
101
    return 0;
102
}
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/************************************************************************
 *									*
 *			Error handling					*
 *									*
 ************************************************************************/

/**
 * virProxyError:
 * @conn: the connection if available
 * @error: the error noumber
 * @info: extra information string
 *
 * Handle an error at the xend daemon interface
 */
static void
virProxyError(virConnectPtr conn, virErrorNumber error, const char *info)
{
121 122
    const char *errmsg;

123 124 125 126
    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
127
    __virRaiseError(conn, NULL, NULL, VIR_FROM_PROXY, error, VIR_ERR_ERROR,
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
                    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.
 * 
 * Returns path on success or NULL in case of error.
 */
static const char *
virProxyFindServerPath(void)
{
    static const char *serverPaths[] = {
        BINDIR "/libvirt_proxy",
148
        "/usr/bin/libvirt_proxy_dbg",
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        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;

    if (!proxyPath) {
        fprintf(stderr, "failed to find libvirt_proxy\n");
	return(-1);
    }

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

    /* Become a daemon */
    pid = fork();
    if (pid == 0) {
        long open_max;
	long i;

        /* don't hold open fd opened from the client of the library */
	open_max = sysconf (_SC_OPEN_MAX);
	for (i = 0; i < open_max; i++)
	    fcntl (i, F_SETFD, FD_CLOEXEC);

        setsid();
        if (fork() == 0) {
            execl(proxyPath, proxyPath, NULL);
201
            fprintf(stderr, _("failed to exec %s\n"), proxyPath);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        }
        /*
         * calling exit() generate troubles for termination handlers
         */
        _exit(0);
    }

    /*
     * 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:
 * @path: the fileame for the socket
 *
 * 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) {
	return(-1);
    }

    /*
     * Abstract socket do not hit the filesystem, way more secure and 
     * garanteed to be atomic
     */
    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) {
	close(fd);
	if (trials < 3) {
	    if (virProxyForkServer() < 0)
	        return(-1);
	    trials++;
	    usleep(5000 * trials * trials);
	    goto retry;
	}
	return (-1);
    }

    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)
295
        fprintf(stderr, _("Failed to close socket %d\n"), fd);
296 297 298 299 300 301 302 303 304 305
    else if (debug > 0)
	fprintf(stderr, "Closed socket %d\n", fd);
    return(ret);
}

/**
 * virProxyReadClientSocket:
 * @fd: the socket 
 * @buffer: the target memory area
 * @len: the lenght in bytes
306
 * @quiet: quiet access
307 308 309 310 311 312
 *
 * Process a read from a client socket
 *
 * Returns the number of byte read or -1 in case of error.
 */
static int
313
virProxyReadClientSocket(int fd, char *buffer, int len, int quiet) {
314 315 316 317 318 319 320 321 322 323 324 325 326
    int ret;

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

retry:
    ret = read(fd, buffer, len);
    if (ret < 0) {
        if (errno == EINTR) {
	    if (debug > 0)
	        fprintf(stderr, "read socket %d interrupted\n", fd);
	    goto retry;
	}
327
	if (!quiet)
328
            fprintf(stderr, _("Failed to read socket %d\n"), fd);
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
	return(-1);
    }

    if (debug)
	fprintf(stderr, "read %d bytes from socket %d\n",
		ret, fd);
    return(ret);
}

/**
 * virProxyWriteClientSocket:
 * @fd: the socket 
 * @data: the data
 * @len: the lenght of data in bytes
 *
 * 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);

retry:
    ret = write(fd, data, len);
    if (ret < 0) {
        if (errno == EINTR) {
	    if (debug > 0)
	        fprintf(stderr, "write socket %d, %d bytes interrupted\n",
		        fd, len);
	    goto retry;
	}
362
        fprintf(stderr, _("Failed to write to socket %d\n"), fd);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	return(-1);
    }
    if (debug)
	fprintf(stderr, "wrote %d bytes to socket %d\n",
		len, fd);

    return(0);
}

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

/**
 * xenProxyClose:
 * @conn: pointer to the hypervisor connection
 *
 * Shutdown the Xen proxy communication layer
 */
384
static int
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
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;
408 409 410 411
}

static int 
xenProxyCommand(virConnectPtr conn, virProxyPacketPtr request,
412
                virProxyFullPacketPtr answer, int quiet) {
413 414 415
    static int serial = 0;
    int ret;
    virProxyPacketPtr res = NULL;
416
    xenUnifiedPrivatePtr priv;
417

418 419 420 421 422 423 424 425 426 427 428 429 430 431
    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;
432 433 434 435 436 437 438 439 440

    /*
     * normal communication serial numbers are in 0..4095
     */
    ++serial;
    if (serial >= 4096)
        serial = 0;
    request->version = PROXY_PROTO_VERSION;
    request->serial = serial;
441
    ret  = virProxyWriteClientSocket(priv->proxy, (const char *) request,
442 443 444 445 446 447
                                     request->len);
    if (ret < 0)
        return(-1);
retry:
    if (answer == NULL) {
        /* read in situ */
448
	ret  = virProxyReadClientSocket(priv->proxy, (char *) request,
449
	                                sizeof(virProxyPacket), quiet);
450 451 452 453
	if (ret < 0)
	    return(-1);
	if (ret != sizeof(virProxyPacket)) {
	    fprintf(stderr,
454
		    _("Communication error with proxy: got %d bytes of %d\n"),
455
		    ret, (int) sizeof(virProxyPacket));
456 457 458 459 460 461
	    xenProxyClose(conn);
	    return(-1);
	}
	res = request;
	if (res->len != sizeof(virProxyPacket)) {
	    fprintf(stderr,
462
		    _("Communication error with proxy: expected %d bytes got %d\n"),
463
		    (int) sizeof(virProxyPacket), res->len);
464 465 466 467
	    xenProxyClose(conn);
	    return(-1);
	}
    } else {
468
        /* read in packet provided */
469
        ret  = virProxyReadClientSocket(priv->proxy, (char *) answer,
470
	                                sizeof(virProxyPacket), quiet);
471 472 473 474
	if (ret < 0)
	    return(-1);
	if (ret != sizeof(virProxyPacket)) {
	    fprintf(stderr,
475
		    _("Communication error with proxy: got %d bytes of %d\n"),
476
		    ret, (int) sizeof(virProxyPacket));
477 478 479
	    xenProxyClose(conn);
	    return(-1);
	}
480
	res = (virProxyPacketPtr) answer;
481
	if ((res->len < sizeof(virProxyPacket)) ||
482
	    (res->len > sizeof(virProxyFullPacket))) {
483
	    fprintf(stderr,
484
		    _("Communication error with proxy: got %d bytes packet\n"),
485 486 487 488 489
		    res->len);
	    xenProxyClose(conn);
	    return(-1);
	}
	if (res->len > sizeof(virProxyPacket)) {
490
	    ret  = virProxyReadClientSocket(priv->proxy,
491
	                           (char *) &(answer->extra.arg[0]),
492
	                                    res->len - ret, quiet);
493 494
	    if (ret != (int) (res->len - sizeof(virProxyPacket))) {
		fprintf(stderr,
495
			_("Communication error with proxy: got %d bytes of %d\n"),
496
			ret, (int) sizeof(virProxyPacket));
497 498 499 500 501 502 503 504 505 506 507
		xenProxyClose(conn);
		return(-1);
	    }
	}
    }
    /*
     * do more checks on the incoming packet.
     */
    if ((res == NULL) || (res->version != PROXY_PROTO_VERSION) ||
        (res->len < sizeof(virProxyPacket))) {
	fprintf(stderr,
508
		_("Communication error with proxy: malformed packet\n"));
509 510 511 512 513
	xenProxyClose(conn);
	return(-1);
    }
    if (res->serial != serial) {
        TODO /* Asynchronous communication */
514
	fprintf(stderr, _("got asynchronous packet number %d\n"), res->serial);
515 516 517 518 519 520
        goto retry;
    }
    return(0);
}

/**
521
 * xenProxyOpen:
522
 * @conn: pointer to the hypervisor connection
523 524
 * @name: URL for the target, NULL for local
 * @flags: combination of virDrvOpenFlag(s)
525 526
 *
 * Try to initialize the Xen proxy communication layer
527
 * This can be opened only for a read-only kind of access
528 529 530 531
 *
 * Returns 0 in case of success, and -1 in case of failure
 */
int
532
xenProxyOpen(virConnectPtr conn, const char *name ATTRIBUTE_UNUSED, int flags)
533
{
534 535 536
    virProxyPacket req;
    int ret;
    int fd;
537
    xenUnifiedPrivatePtr priv;
538 539 540
    
    if (!(flags & VIR_DRV_OPEN_RO))
        return(-1);
541 542 543 544

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

545 546
    fd = virProxyOpenClientSocket(PROXY_SOCKET_PATH);
    if (fd < 0) {
547
	    virProxyError(NULL, VIR_ERR_NO_XEN, PROXY_SOCKET_PATH);
548
        return(-1);
549
    }
550
    priv->proxy = fd;
551 552 553 554

    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_NONE;
    req.len = sizeof(req);
555
    ret = xenProxyCommand(conn, &req, NULL, 1);
556
    if ((ret < 0) || (req.command != VIR_PROXY_NONE)) {
557
	    virProxyError(NULL, VIR_ERR_OPERATION_FAILED, __FUNCTION__);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
        xenProxyClose(conn);
	return(-1);
    }
    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__);
	return (-1);
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_VERSION;
    req.len = sizeof(req);
598
    ret = xenProxyCommand(conn, &req, NULL, 0);
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
    if (ret < 0) {
        xenProxyClose(conn);
	return(-1);
    }
    *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)
{
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
    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__);
	return (-1);
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_LIST;
    req.len = sizeof(req);
636
    ret = xenProxyCommand(conn, &req, &ans, 0);
637 638 639 640 641
    if (ret < 0) {
        xenProxyClose(conn);
	return(-1);
    }
    nb = ans.data.arg;
642 643 644 645
    if ((nb > 1020) || (nb <= 0) ||
        (ans.len <= sizeof(virProxyPacket)) ||
	(ans.len > sizeof(virProxyFullPacket))) {
        virProxyError(conn, VIR_ERR_OPERATION_FAILED, __FUNCTION__);
646
        return(-1);
647
    }
648 649 650 651 652
    if (nb > maxids)
        nb = maxids;
    memmove(ids, &ans.extra.arg[0], nb * sizeof(int));

    return(nb);
653 654 655 656 657 658 659 660 661 662 663 664 665
}

/**
 * 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)
{
666 667 668 669 670 671 672 673 674 675
    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);
676
    ret = xenProxyCommand(conn, &req, NULL, 0);
677 678 679 680 681
    if (ret < 0) {
        xenProxyClose(conn);
	return(-1);
    }
    return(req.data.arg);
682 683
}

684

685
/**
686
 * xenProxyDomainGetDomMaxMemory:
687 688 689
 * @conn: pointer to the hypervisor connection
 * @id: the domain ID number
 *
690
 * Ask the Xen Daemon for the maximum memory allowed for a domain
691
 *
692
 * Returns the memory size in kilobytes or 0 in case of error.
693
 */
694 695 696 697 698 699 700 701
static unsigned long
xenProxyDomainGetDomMaxMemory(virConnectPtr conn, int id)
{
    virProxyPacket req;
    int ret;

    if (!VIR_IS_CONNECT(conn)) {
        virProxyError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
702
        return (0);
703 704 705 706 707
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_MAX_MEMORY;
    req.data.arg = id;
    req.len = sizeof(req);
708
    ret = xenProxyCommand(conn, &req, NULL, 0);
709 710
    if (ret < 0) {
        xenProxyClose(conn);
711
        return(0);
712 713
    }
    return(req.data.larg);
714 715 716
}

/**
717 718
 * xenProxyDomainGetMaxMemory:
 * @domain: pointer to the domain block
719
 *
720
 * Ask the Xen Daemon for the maximum memory allowed for a domain
721
 *
722
 * Returns the memory size in kilobytes or 0 in case of error.
723
 */
724 725
static unsigned long
xenProxyDomainGetMaxMemory(virDomainPtr domain)
726
{
727 728 729 730 731 732 733
    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
	if (domain == NULL)
	    virProxyError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
	else
	    virProxyError(domain->conn, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (0);
    }
734
    if (domain->id < 0)
735
        return (0);
736
    return(xenProxyDomainGetDomMaxMemory(domain->conn, domain->id));
737 738 739
}

/**
740 741 742
 * xenProxyDomainGetInfo:
 * @domain: a domain object
 * @info: pointer to a virDomainInfo structure allocated by the user
743
 *
744 745
 * This method looks up information about a domain and update the
 * information block provided.
746
 *
747
 * Returns 0 in case of success, -1 in case of error
748 749
 */
static int
750
xenProxyDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info)
751
{
752 753 754 755 756
    virProxyPacket req;
    virProxyFullPacket ans;
    int ret;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
757 758 759 760 761
        if (domain == NULL)
            virProxyError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        else
            virProxyError(domain->conn, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (-1);
762
    }
763
    if (domain->id < 0)
764
        return (-1);
765 766
    if (info == NULL) {
        virProxyError(domain->conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
767
        return (-1);
768 769 770
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_DOMAIN_INFO;
771
    req.data.arg = domain->id;
772
    req.len = sizeof(req);
773
    ret = xenProxyCommand(domain->conn, &req, &ans, 0);
774 775
    if (ret < 0) {
        xenProxyClose(domain->conn);
776
        return(-1);
777 778 779
    }
    if (ans.len != sizeof(virProxyPacket) + sizeof(virDomainInfo)) {
        virProxyError(domain->conn, VIR_ERR_OPERATION_FAILED, __FUNCTION__);
780
        return (-1);
781 782
    }
    memmove(info, &ans.extra.dinfo, sizeof(virDomainInfo));
783

784 785
    return(0);
}
786 787

/**
788 789 790
 * xenProxyLookupByID:
 * @conn: pointer to the hypervisor connection
 * @id: the domain ID number
791
 *
792
 * Try to find a domain based on the hypervisor ID number
793
 *
794
 * Returns a new domain object or NULL in case of failure
795
 */
796 797
static virDomainPtr
xenProxyLookupByID(virConnectPtr conn, int id)
798
{
799 800
    virProxyPacket req;
    virProxyFullPacket ans;
801
    unsigned char uuid[VIR_UUID_BUFLEN];
802 803 804 805 806 807 808 809
    const char *name;
    int ret;
    virDomainPtr res;

    if (!VIR_IS_CONNECT(conn)) {
        virProxyError(conn, VIR_ERR_INVALID_CONN, __FUNCTION__);
        return (NULL);
    }
810 811 812 813
    if (id < 0) {
        virProxyError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return (NULL);
    }
814 815 816 817
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_LOOKUP_ID;
    req.data.arg = id;
    req.len = sizeof(req);
818
    ret = xenProxyCommand(conn, &req, &ans, 0);
819 820 821 822
    if (ret < 0) {
        xenProxyClose(conn);
	return(NULL);
    }
823
    if (ans.data.arg == -1) {
824 825
	return(NULL);
    }
826 827
    memcpy(uuid, &ans.extra.str[0], VIR_UUID_BUFLEN);
    name = &ans.extra.str[VIR_UUID_BUFLEN];
828 829 830
    res = virGetDomain(conn, name, uuid);

    if (res == NULL)
831
        virProxyError(conn, VIR_ERR_NO_MEMORY, _("allocating domain"));
832
    else
833
	res->id = id;
834 835
    
    return(res);
836 837 838
}

/**
839 840 841
 * xenProxyLookupByUUID:
 * @conn: pointer to the hypervisor connection
 * @uuid: the raw UUID for the domain
842
 *
843
 * Try to lookup a domain on xend based on its UUID.
844
 *
845
 * Returns a new domain object or NULL in case of failure
846
 */
847 848
static virDomainPtr
xenProxyLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
849
{
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    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__);
	return (NULL);
    }
    memset(&req, 0, sizeof(virProxyPacket));
    req.command = VIR_PROXY_LOOKUP_UUID;
865
    req.len = sizeof(virProxyPacket) + VIR_UUID_BUFLEN;
866
    ret = xenProxyCommand(conn, (virProxyPacketPtr) &req, &req, 0);
867 868 869 870 871 872 873 874 875 876 877
    if (ret < 0) {
        xenProxyClose(conn);
	return(NULL);
    }
    if (req.data.arg == -1) {
	return(NULL);
    }
    name = &req.extra.str[0];
    res = virGetDomain(conn, name, uuid);

    if (res == NULL)
878
        virProxyError(conn, VIR_ERR_NO_MEMORY, _("allocating domain"));
879
    else
880
	res->id = req.data.arg;
881 882
    
    return(res);
883 884
}

885 886 887 888 889 890 891 892 893 894
/**
 * xenProxyDomainLookupByName:
 * @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
 */
static virDomainPtr
895
xenProxyDomainLookupByName(virConnectPtr conn, const char *name)
896
{
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
    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__);
	return (NULL);
    }
    len = strlen(name);
    if (len > 1000) {
        virProxyError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return (NULL);
    }
    memset(&req, 0, sizeof(virProxyPacket));
    req.command = VIR_PROXY_LOOKUP_NAME;
    req.len = sizeof(virProxyPacket) + len + 1;
    strcpy(&req.extra.str[0], name);
918
    ret = xenProxyCommand(conn, (virProxyPacketPtr) &req, &req, 0);
919 920 921 922 923 924 925 926 927 928
    if (ret < 0) {
        xenProxyClose(conn);
	return(NULL);
    }
    if (req.data.arg == -1) {
	return(NULL);
    }
    res = virGetDomain(conn, name, (const unsigned char *)&req.extra.str[0]);

    if (res == NULL)
929
        virProxyError(conn, VIR_ERR_NO_MEMORY, _("allocating domain"));
930
    else
931
	res->id = req.data.arg;
932 933
    
    return(res);
934
}
935

936 937 938 939 940 941 942 943 944 945 946
/**
 * xenProxyNodeGetInfo:
 * @conn: pointer to the Xen Daemon block
 * @info: pointer to a virNodeInfo structure allocated by the user
 * 
 * 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) {
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
    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__);
	return (-1);
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_NODE_INFO;
    req.data.arg = 0;
    req.len = sizeof(req);
963
    ret = xenProxyCommand(conn, &req, &ans, 0);
964 965 966 967 968 969 970 971 972 973 974 975
    if (ret < 0) {
        xenProxyClose(conn);
	return(-1);
    }
    if (ans.data.arg == -1) {
	return(-1);
    }
    if (ans.len != sizeof(virProxyPacket) + sizeof(virNodeInfo)) {
	return(-1);
    }
    memcpy(info, &ans.extra.ninfo, sizeof(virNodeInfo));
    return(0);
976
}
977

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
/**
 * xenProxyGetCapabilities:
 * @conn: pointer to the Xen Daemon block
 * 
 * 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);
    xml = malloc (xmllen+1);
    if (!xml) {
        virProxyError (conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
        return NULL;
    }
    memmove (xml, ans.extra.str, xmllen);
    xml[xmllen] = '\0';

    return xml;
}

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
/**
 * xenProxyDomainDumpXML:
 * @domain: a domain object
 * @flags: xml generation flags
 *
 * This method generates an XML description of a domain.
 *
 * Returns the XML document on success, NULL otherwise. 
 */
static char *
1037
xenProxyDomainDumpXML(virDomainPtr domain, int flags ATTRIBUTE_UNUSED)
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
{
    virProxyPacket req;
    virProxyFullPacket ans;
    int ret;
    int xmllen;
    char *xml;

    if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
	if (domain == NULL)
	    virProxyError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
	else
	    virProxyError(domain->conn, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (NULL);
    }
1052
    if (domain->id < 0)
1053
        return (NULL);
1054 1055
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_DOMAIN_XML;
1056
    req.data.arg = domain->id;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    req.len = sizeof(req);
    ret = xenProxyCommand(domain->conn, &req, &ans, 0);
    if (ret < 0) {
        xenProxyClose(domain->conn);
	return(NULL);
    }
    if (ans.len <= sizeof(virProxyPacket)) {
        virProxyError(domain->conn, VIR_ERR_OPERATION_FAILED, __FUNCTION__);
	return (NULL);
    }
    xmllen = ans.len - sizeof(virProxyPacket);
    if (!(xml = malloc(xmllen+1))) {
1069 1070
	virProxyError(domain->conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
        return NULL;
1071 1072 1073 1074 1075 1076
    }
    memmove(xml, &ans.extra.dinfo, xmllen);
    xml[xmllen] = '\0';

    return(xml);
}
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104

/**
 * 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)) {
	if (domain == NULL)
	    virProxyError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
	else
	    virProxyError(domain->conn, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
        return (NULL);
    }
    memset(&req, 0, sizeof(req));
    req.command = VIR_PROXY_DOMAIN_OSTYPE;
1105
    req.data.arg = domain->id;
1106 1107 1108 1109 1110 1111
    req.len = sizeof(req);
    ret = xenProxyCommand(domain->conn, &req, &ans, 0);
    if (ret < 0) {
        xenProxyClose(domain->conn);
	return(NULL);
    }
1112 1113 1114 1115
    if ((ans.len == sizeof(virProxyPacket)) && (ans.data.arg < 0)) {
	return(NULL);
    }

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    if (ans.len <= sizeof(virProxyPacket)) {
        virProxyError(domain->conn, VIR_ERR_OPERATION_FAILED, __FUNCTION__);
	return (NULL);
    }
    oslen = ans.len - sizeof(virProxyPacket);
    if (!(ostype = malloc(oslen+1))) {
	virProxyError(domain->conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
        return NULL;
    }
    memmove(ostype, &ans.extra.dinfo, oslen);
    ostype[oslen] = '\0';

    return(ostype);
}
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

/*
 * vim: set tabstop=4:
 * vim: set shiftwidth=4:
 * vim: set expandtab:
 */
/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */