xs_internal.c 20.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * xs_internal.c: access to Xen Store
 *
 * Copyright (C) 2006 Red Hat, Inc.
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include <stdint.h>

#include <xen/dom0_ops.h>
#include <xen/version.h>
#include <xen/xen.h>

#include <xs.h>

#include "internal.h"
#include "driver.h"
#include "xs_internal.h"
30
#include "xen_internal.h" /* for xenHypervisorCheckID */
31 32 33

#define XEN_HYPERVISOR_SOCKET "/proc/xen/privcmd"

34
#ifndef PROXY
35 36
static char *xenStoreDomainGetOSType(virDomainPtr domain);

37
static virDriver xenStoreDriver = {
38
    VIR_DRV_XEN_STORE,
39
    "XenStore",
40 41 42
    (DOM0_INTERFACE_VERSION >> 24) * 1000000 +
    ((DOM0_INTERFACE_VERSION >> 16) & 0xFF) * 1000 +
    (DOM0_INTERFACE_VERSION & 0xFFFF),
43 44 45 46 47
    NULL, /* init */
    xenStoreOpen, /* open */
    xenStoreClose, /* close */
    NULL, /* type */
    NULL, /* version */
48
    NULL, /* nodeGetInfo */
49 50 51 52 53
    xenStoreListDomains, /* listDomains */
    NULL, /* numOfDomains */
    NULL, /* domainCreateLinux */
    NULL, /* domainLookupByID */
    NULL, /* domainLookupByUUID */
54
    xenStoreDomainLookupByName, /* domainLookupByName */
55 56
    NULL, /* domainSuspend */
    NULL, /* domainResume */
57 58
    xenStoreDomainShutdown, /* domainShutdown */
    xenStoreDomainReboot, /* domainReboot */
59
    NULL, /* domainDestroy */
60
    xenStoreDomainGetOSType, /* domainGetOSType */
61
    xenStoreDomainGetMaxMemory, /* domainGetMaxMemory */
62 63
    NULL, /* domainSetMaxMemory */
    xenStoreDomainSetMemory, /* domainSetMemory */
64 65
    xenStoreGetDomainInfo, /* domainGetInfo */
    NULL, /* domainSave */
66
    NULL, /* domainRestore */
D
Daniel Veillard 已提交
67
    NULL, /* domainCoreDump */
68 69
    NULL, /* domainSetVcpus */
    NULL, /* domainPinVcpu */
70 71
    NULL, /* domainGetVcpus */
    NULL, /* domainDumpXML */
72 73 74 75 76
    NULL, /* listDefinedDomains */
    NULL, /* numOfDefinedDomains */
    NULL, /* domainCreate */
    NULL, /* domainDefineXML */
    NULL, /* domainUndefine */
77 78
    NULL, /* domainAttachDevice */
    NULL, /* domainDetachDevice */
79 80
};

81 82 83 84 85 86 87 88 89
/**
 * xenStoreRegister:
 *
 * Registers the xenStore driver
 */
void xenStoreRegister(void)
{
    virRegisterDriver(&xenStoreDriver);
}
90
#endif /* ! PROXY */
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/**
 * virXenStoreError:
 * @conn: the connection if available
 * @error: the error number
 * @info: extra information string
 *
 * Handle an error at the xend store interface
 */
static void
virXenStoreError(virConnectPtr conn, virErrorNumber error, const char *info)
{
    const char *errmsg;

    if (error == VIR_ERR_OK)
        return;

    errmsg = __virErrorMsg(error, info);
    __virRaiseError(conn, NULL, VIR_FROM_XENSTORE, error, VIR_ERR_ERROR,
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

/************************************************************************
 *									*
 *		Helper internal APIs					*
 *									*
 ************************************************************************/
118
#ifndef PROXY
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/**
 * virConnectDoStoreList:
 * @conn: pointer to the hypervisor connection
 * @path: the absolute path of the directory in the store to list
 * @nb: OUT pointer to the number of items found
 *
 * Internal API querying the Xenstore for a list
 *
 * Returns a string which must be freed by the caller or NULL in case of error
 */
static char **
virConnectDoStoreList(virConnectPtr conn, const char *path,
                      unsigned int *nb)
{
    if ((conn == NULL) || (conn->xshandle == NULL) || (path == NULL) ||
        (nb == NULL))
        return (NULL);

    return xs_directory(conn->xshandle, 0, path, nb);
}
139
#endif /* ! PROXY */
140 141 142

/**
 * virDomainDoStoreQuery:
143 144
 * @conn: pointer to the hypervisor connection
 * @domid: id of the domain
145 146 147 148 149 150 151
 * @path: the relative path of the data in the store to retrieve
 *
 * Internal API querying the Xenstore for a string value.
 *
 * Returns a string which must be freed by the caller or NULL in case of error
 */
static char *
152
virDomainDoStoreQuery(virConnectPtr conn, int domid, const char *path)
153 154 155 156
{
    char s[256];
    unsigned int len = 0;

157
    if (!conn || conn->xshandle == NULL)
158 159
        return (NULL);

160
    snprintf(s, 255, "/local/domain/%d/%s", domid, path);
161 162
    s[255] = 0;

163
    return xs_read(conn->xshandle, 0, &s[0], &len);
164 165
}

166
#ifndef PROXY
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 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
/**
 * virDomainDoStoreWrite:
 * @domain: a domain object
 * @path: the relative path of the data in the store to retrieve
 *
 * Internal API setting up a string value in the Xenstore
 * Requires write access to the XenStore
 *
 * Returns 0 in case of success, -1 in case of failure
 */
static int
virDomainDoStoreWrite(virDomainPtr domain, const char *path,
                      const char *value)
{
    char s[256];

    int ret = -1;

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (-1);
    if (domain->conn->xshandle == NULL)
        return (-1);
    if (domain->conn->flags & VIR_CONNECT_RO)
        return (-1);

    snprintf(s, 255, "/local/domain/%d/%s", domain->handle, path);
    s[255] = 0;

    if (xs_write(domain->conn->xshandle, 0, &s[0], value, strlen(value)))
        ret = 0;

    return (ret);
}

/**
 * virDomainGetVM:
 * @domain: a domain object
 *
 * Internal API extracting a xenstore vm path.
 *
 * Returns the new string or NULL in case of error
 */
char *
virDomainGetVM(virDomainPtr domain)
{
    char *vm;
    char query[200];
    unsigned int len;

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (NULL);
    if (domain->conn->xshandle == NULL)
        return (NULL);

    snprintf(query, 199, "/local/domain/%d/vm", virDomainGetID(domain));
    query[199] = 0;

    vm = xs_read(domain->conn->xshandle, 0, &query[0], &len);

    return (vm);
}

/**
 * virDomainGetVMInfo:
 * @domain: a domain object
 * @vm: the xenstore vm path
 * @name: the value's path
 *
 * Internal API extracting one information the device used 
 * by the domain from xensttore
 *
 * Returns the new string or NULL in case of error
 */
char *
virDomainGetVMInfo(virDomainPtr domain, const char *vm, const char *name)
{
    char s[256];
    char *ret = NULL;
    unsigned int len = 0;

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (NULL);
    if (domain->conn->xshandle == NULL)
        return (NULL);

    snprintf(s, 255, "%s/%s", vm, name);
    s[255] = 0;

    ret = xs_read(domain->conn->xshandle, 0, &s[0], &len);

    return (ret);
}

260
#if 0
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
/**
 * virConnectCheckStoreID:
 * @conn: pointer to the hypervisor connection
 * @id: the id number as returned from Xenstore
 *
 * the xenstore sometimes list non-running domains, double check
 * from the hypervisor if we have direct access
 *
 * Returns -1 if the check failed, 0 if successful or not possible to check
 */
static int
virConnectCheckStoreID(virConnectPtr conn, int id)
{
    if (conn->handle >= 0) {
        int tmp;

277
        tmp = xenHypervisorCheckID(conn, id);
278 279 280 281 282
        if (tmp < 0)
            return (-1);
    }
    return (0);
}
283
#endif
284
#endif /* ! PROXY */
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

/************************************************************************
 *									*
 *		Canonical internal APIs					*
 *									*
 ************************************************************************/
/**
 * xenStoreOpen:
 * @conn: pointer to the connection block
 * @name: URL for the target, NULL for local
 * @flags: combination of virDrvOpenFlag(s)
 *
 * Connects to the Xen hypervisor.
 *
 * Returns 0 or -1 in case of error.
 */
int
xenStoreOpen(virConnectPtr conn, const char *name, int flags)
{
304
    if ((name != NULL) && (strcasecmp(name, "xen")))
305 306
        return(-1);

307 308 309
#ifdef PROXY
    conn->xshandle = xs_daemon_open_readonly();
#else
310 311 312 313
    if (flags & VIR_DRV_OPEN_RO)
	conn->xshandle = xs_daemon_open_readonly();
    else
	conn->xshandle = xs_daemon_open();
314
#endif /* ! PROXY */
315 316 317 318

    if (conn->xshandle == NULL) {
        if (!(flags & VIR_DRV_OPEN_QUIET))
            virXenStoreError(conn, VIR_ERR_NO_XEN, 
319
	                     _("failed to connect to Xen Store"));
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
        return (-1);
    }
    return (0);
}

/**
 * xenStoreClose:
 * @conn: pointer to the connection block
 *
 * Close the connection to the Xen hypervisor.
 *
 * Returns 0 in case of success or -1 in case of error.
 */
int
xenStoreClose(virConnectPtr conn)
{
    if (conn == NULL) {
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }
    if (conn->xshandle == NULL)
	return(-1);

    xs_daemon_close(conn->xshandle);
    return (0);
}

347
#ifndef PROXY
348 349 350
/**
 * xenStoreGetDomainInfo:
 * @domain: pointer to the domain block
351
 * @info: the place where information should be stored
352
 *
353
 * Do an hypervisor call to get the related set of domain information.
354 355 356 357 358 359 360 361 362 363
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
xenStoreGetDomainInfo(virDomainPtr domain, virDomainInfoPtr info)
{
    char *tmp, **tmp2;
    unsigned int nb_vcpus;
    char request[200];

364 365 366
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (-1);

367 368 369 370 371 372 373
    if ((domain == NULL) || (domain->conn == NULL) || (info == NULL)) {
        virXenStoreError(domain ? domain->conn : NULL, VIR_ERR_INVALID_ARG,
	                 __FUNCTION__);
	return(-1);
    }
    if (domain->conn->xshandle == NULL)
        return(-1);
374 375
    if (domain->handle == -1)
        return(-1);
376

377
    tmp = virDomainDoStoreQuery(domain->conn, domain->handle, "running");
378 379 380 381 382 383 384
    if (tmp != NULL) {
        if (tmp[0] == '1')
            info->state = VIR_DOMAIN_RUNNING;
        free(tmp);
    } else {
        info->state = VIR_DOMAIN_NONE;
    }
385
    tmp = virDomainDoStoreQuery(domain->conn, domain->handle, "memory/target");
386 387 388 389 390 391 392 393 394 395
    if (tmp != NULL) {
        info->memory = atol(tmp);
        info->maxMem = atol(tmp);
        free(tmp);
    } else {
        info->memory = 0;
        info->maxMem = 0;
    }
#if 0
    /* doesn't seems to work */
396
    tmp = virDomainDoStoreQuery(domain->conn, domain->handle, "cpu_time");
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    if (tmp != NULL) {
        info->cpuTime = atol(tmp);
        free(tmp);
    } else {
        info->cpuTime = 0;
    }
#endif
    snprintf(request, 199, "/local/domain/%d/cpu", domain->handle);
    request[199] = 0;
    tmp2 = virConnectDoStoreList(domain->conn, request, &nb_vcpus);
    if (tmp2 != NULL) {
        info->nrVirtCpu = nb_vcpus;
        free(tmp2);
    }
    return (0);
}

/**
415
 * xenStoreDomainSetMemory:
416 417 418 419 420 421 422 423
 * @domain: pointer to the domain block
 * @memory: the max memory size in kilobytes.
 *
 * Change the maximum amount of memory allowed in the xen store
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
424
xenStoreDomainSetMemory(virDomainPtr domain, unsigned long memory)
425 426 427 428 429 430 431 432 433
{
    int ret;
    char value[20];

    if ((domain == NULL) || (domain->conn == NULL) || (memory < 4096)) {
        virXenStoreError(domain ? domain->conn : NULL, VIR_ERR_INVALID_ARG,
	                 __FUNCTION__);
	return(-1);
    }
434 435
    if (domain->handle == -1)
        return(-1);
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    snprintf(value, 19, "%lu", memory);
    value[19] = 0;
    ret = virDomainDoStoreWrite(domain, "memory/target", &value[0]);
    if (ret < 0)
        return (-1);
    return (0);
}

/**
 * xenStoreDomainGetMaxMemory:
 * @domain: pointer to the domain block
 *
 * Ask the xenstore for the maximum memory allowed for a domain
 *
 * Returns the memory size in kilobytes or 0 in case of error.
 */
unsigned long
xenStoreDomainGetMaxMemory(virDomainPtr domain)
{
    char *tmp;
    unsigned long ret = 0;

458 459
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (ret);
460 461
    if (domain->handle == -1)
        return(-1);
462 463

    tmp = virDomainDoStoreQuery(domain->conn, domain->handle, "memory/target");
464 465 466 467 468 469 470 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 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
    if (tmp != NULL) {
	ret = (unsigned long) atol(tmp);
	free(tmp);
    }
    return(ret);
}

/**
 * xenStoreNumOfDomains:
 * @conn: pointer to the hypervisor connection
 *
 * Provides the number of active domains.
 *
 * Returns the number of domain found or -1 in case of error
 */
int
xenStoreNumOfDomains(virConnectPtr conn)
{
    unsigned int num;
    char **idlist;
    int ret = -1;

    if ((conn == NULL) || (conn->xshandle == NULL)) {
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }
    idlist = xs_directory(conn->xshandle, 0, "/local/domain", &num);
    if (idlist) {
        free(idlist);
	ret = num;
    }
    return(ret);
}

/**
 * xenStoreListDomains:
 * @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
 */
int
xenStoreListDomains(virConnectPtr conn, int *ids, int maxids)
{
    char **idlist = NULL, *endptr;
    unsigned int num, i;
    int ret;
    long id;

    if ((conn == NULL) || (ids == NULL)) {
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(-1);
    }
    if (conn->xshandle == NULL)
        return(-1);

    idlist = xs_directory(conn->xshandle, 0, "/local/domain", &num);
    if (idlist == NULL)
	return(-1);

    for (ret = 0, i = 0; (i < num) && (ret < maxids); i++) {
	id = strtol(idlist[i], &endptr, 10);
	if ((endptr == idlist[i]) || (*endptr != 0)) {
	    ret = -1;
	    break;
	}
533
#if 0
534 535
	if (virConnectCheckStoreID(conn, (int) id) < 0)
	    continue;
536
#endif
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
	ids[ret++] = (int) id;
    }
    return(ret);
}

/**
 * xenStoreDomainLookupByName:
 * @conn: A xend instance
 * @name: The name of the domain
 *
 * Try to lookup a domain on the Xen Store based on its name.
 *
 * Returns a new domain object or NULL in case of failure
 */
virDomainPtr
xenStoreDomainLookupByName(virConnectPtr conn, const char *name)
{
    virDomainPtr ret = NULL;
    unsigned int num, i, len;
    long id = -1;
    char **idlist = NULL, *endptr;
    char prop[200], *tmp, *path = NULL;
    int found = 0;
    struct xend_domain *xenddomain = NULL;

    if ((conn == NULL) || (name == NULL)) {
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
	return(NULL);
    }
    if (conn->xshandle == NULL)
        return(NULL);

    idlist = xs_directory(conn->xshandle, 0, "/local/domain", &num);
    if (idlist == NULL)
	goto done;

    for (i = 0; i < num; i++) {
	id = strtol(idlist[i], &endptr, 10);
	if ((endptr == idlist[i]) || (*endptr != 0)) {
	    goto done;
	}
578
#if 0
579 580
	if (virConnectCheckStoreID(conn, (int) id) < 0)
	    continue;
581
#endif
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	snprintf(prop, 199, "/local/domain/%s/name", idlist[i]);
	prop[199] = 0;
	tmp = xs_read(conn->xshandle, 0, prop, &len);
	if (tmp != NULL) {
	    found = !strcmp(name, tmp);
	    free(tmp);
	    if (found)
		break;
	}
    }
    path = xs_get_domain_path(conn->xshandle, (unsigned int) id);

    if (!found)
        return(NULL);

597 598
    ret = virGetDomain(conn, name, NULL);
    if (ret == NULL) {
599
        virXenStoreError(conn, VIR_ERR_NO_MEMORY, _("allocating domain"));
600 601
	if (path != NULL)
	    free(path);
602
	goto done;
603
    }
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
    ret->handle = id;
    ret->path = path;

done:
    if (xenddomain != NULL)
	free(xenddomain);
    if (idlist != NULL)
	free(idlist);

    return(ret);
}

/**
 * xenStoreDomainShutdown:
 * @domain: pointer to the Domain block
 *
 * Shutdown the domain, the OS is requested to properly shutdown
 * and the domain may ignore it.  It will return immediately
 * after queuing the request.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
xenStoreDomainShutdown(virDomainPtr domain)
{
    if ((domain == NULL) || (domain->conn == NULL)) {
        virXenStoreError((domain ? domain->conn : NULL), VIR_ERR_INVALID_ARG,
	                 __FUNCTION__);
        return(-1);
    }
634 635
    if (domain->handle == -1)
        return(-1);
636 637 638 639 640 641 642
    /*
     * this is very hackish, the domU kernel probes for a special 
     * node in the xenstore and launch the shutdown command if found.
     */
    return(virDomainDoStoreWrite(domain, "control/shutdown", "halt"));
}

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
/**
 * xenStoreDomainReboot:
 * @domain: pointer to the Domain block
 * @flags: extra flags for the reboot operation, not used yet
 *
 * Reboot the domain, the OS is requested to properly shutdown
 * and reboot but the domain may ignore it.  It will return immediately
 * after queuing the request.
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
xenStoreDomainReboot(virDomainPtr domain, unsigned int flags ATTRIBUTE_UNUSED)
{
    if ((domain == NULL) || (domain->conn == NULL)) {
        virXenStoreError((domain ? domain->conn : NULL), VIR_ERR_INVALID_ARG,
	                 __FUNCTION__);
        return(-1);
    }
662 663
    if (domain->handle == -1)
        return(-1);
664 665 666 667 668 669
    /*
     * this is very hackish, the domU kernel probes for a special 
     * node in the xenstore and launch the shutdown command if found.
     */
    return(virDomainDoStoreWrite(domain, "control/shutdown", "reboot"));
}
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

/*
 * xenStoreDomainGetOSType:
 * @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 *
xenStoreDomainGetOSType(virDomainPtr domain) {
    char *vm, *str = NULL;

    if ((domain == NULL) || (domain->conn == NULL)) {
        virXenStoreError((domain ? domain->conn : NULL), VIR_ERR_INVALID_ARG,
	                 __FUNCTION__);
        return(NULL);
    }

    vm = virDomainGetVM(domain);
    if (vm) {
        str = virDomainGetVMInfo(domain, vm, "image/ostype");
        free(vm);
    }
695 696
    if (str == NULL)
        str = strdup("linux");
697 698 699

    return (str);
}
700
#endif /* ! PROXY */
701

702 703
/**
 * xenStoreDomainGetVNCPort:
704 705
 * @conn: the hypervisor connection
 * @domid: id of the domain
706 707 708 709 710 711
 *
 * Return the port number on which the domain is listening for VNC
 * connections. 
 *
 * Returns the port number, -1 in case of error
 */
712
int             xenStoreDomainGetVNCPort(virConnectPtr conn, int domid) {
713 714 715
    char *tmp;
    int ret = -1;

716
    tmp = virDomainDoStoreQuery(conn, domid, "console/vnc-port");
717 718 719 720 721 722 723 724 725 726 727 728
    if (tmp != NULL) {
        char *end;
        ret = strtol(tmp, &end, 10);
        if (ret == 0 && end == tmp)
            ret = -1;
        free(tmp);
    }
    return(ret);
}

/**
 * xenStoreDomainGetConsolePath:
729 730
 * @conn: the hypervisor connection
 * @domid: id of the domain
731 732 733 734 735 736 737 738
 *
 * Return the path to the psuedo TTY on which the guest domain's
 * serial console is attached.
 *
 * Returns the path to the serial console. It is the callers
 * responsibilty to free() the return string. Returns NULL
 * on error
 */
739 740
char *          xenStoreDomainGetConsolePath(virConnectPtr conn, int domid) {
  return virDomainDoStoreQuery(conn, domid, "console/tty");
741
}
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

#ifdef PROXY
/*
 * xenStoreDomainGetOSTypeID:
 * @conn: pointer to the connection.
 * @id: the domain id
 *
 * 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.
 */
char *
xenStoreDomainGetOSTypeID(virConnectPtr conn, int id) {
    char *vm, *str = NULL;
    char query[200];
    unsigned int len;

    if (id < 0)
        return(NULL);


    if (conn->xshandle == NULL)
        return (NULL);

    snprintf(query, 199, "/local/domain/%d/vm", id);
    query[199] = 0;

    vm = xs_read(conn->xshandle, 0, &query[0], &len);

    if (vm) {
        snprintf(query, 199, "%s/image/ostype", vm);
	str = xs_read(conn->xshandle, 0, &query[0], &len);
        free(vm);
    }
777 778 779
    if (str == NULL)
        str = strdup("linux");

780 781 782 783

    return (str);
}
#endif /* PROXY */
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824

/*
 * xenStoreDomainGetNetworkID:
 * @conn: pointer to the connection.
 * @id: the domain id
 * @mac: the mac address
 *
 * Get the reference (i.e. the string number) for the device on that domain
 * which uses the given mac address
 *
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
 */
char *
xenStoreDomainGetNetworkID(virConnectPtr conn, int id, const char *mac) {
    char dir[80], path[128], **list = NULL, *val = NULL;
    unsigned int maclen, len, i, num;
    char *ret = NULL;

    if (id < 0)
        return(NULL);
    if (conn->xshandle == NULL)
        return (NULL);
    if (mac == NULL)
        return (NULL);
    maclen = strlen(mac);
    if (maclen <= 0)
        return (NULL);

    sprintf(dir, "/local/domain/0/backend/vif/%d", id);
    list = xs_directory(conn->xshandle, 0, dir, &num);
    if (list == NULL)
	return(NULL);
    for (i = 0; i < num; i++) {
	sprintf(path, "%s/%s/%s", dir, list[i], "mac");
	val = xs_read(conn->xshandle, 0, path, &len);
	if (val == NULL)
	    break;
	if ((maclen != len) || memcmp(val, mac, len)) {
	    free(val);
	} else {
825
	    ret = strdup(list[i]);
826 827 828 829 830 831 832
	    free(val);
	    break;
	}
    }
    free(list);
    return(ret);
}