xs_internal.c 23.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * 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>
 */

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

13 14 15 16 17 18 19 20 21 22
#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>

23
#include <xen/dom0_ops.h>
24
#include <xen/version.h>
25
#include <xen/xen.h>
26 27 28 29 30

#include <xs.h>

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

35
#ifdef __linux__
36
#define XEN_HYPERVISOR_SOCKET "/proc/xen/privcmd"
37 38 39 40 41
#elif define(__sun__)
#define XEN_HYPERVISOR_SOCKET "/dev/xen/privcmd"
#else
#error "unsupported platform"
#endif
42

43
#ifndef PROXY
44 45
static char *xenStoreDomainGetOSType(virDomainPtr domain);

46
struct xenUnifiedDriver xenStoreDriver = {
47 48 49
    xenStoreOpen, /* open */
    xenStoreClose, /* close */
    NULL, /* version */
50 51
    NULL, /* hostname */
    NULL, /* URI */
52
    NULL, /* nodeGetInfo */
53
    NULL, /* getCapabilities */
54 55 56 57 58
    xenStoreListDomains, /* listDomains */
    NULL, /* numOfDomains */
    NULL, /* domainCreateLinux */
    NULL, /* domainSuspend */
    NULL, /* domainResume */
59 60
    xenStoreDomainShutdown, /* domainShutdown */
    xenStoreDomainReboot, /* domainReboot */
61
    NULL, /* domainDestroy */
62
    xenStoreDomainGetOSType, /* domainGetOSType */
63
    xenStoreDomainGetMaxMemory, /* domainGetMaxMemory */
64 65
    NULL, /* domainSetMaxMemory */
    xenStoreDomainSetMemory, /* domainSetMemory */
66 67
    xenStoreGetDomainInfo, /* 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
#endif /* ! PROXY */
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
/**
 * 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);
107
    __virRaiseError(conn, NULL, NULL, VIR_FROM_XENSTORE, error, VIR_ERR_ERROR,
108 109 110 111 112 113 114 115
                    errmsg, info, NULL, 0, 0, errmsg, info);
}

/************************************************************************
 *									*
 *		Helper internal APIs					*
 *									*
 ************************************************************************/
116
#ifndef PROXY
117 118 119 120 121 122 123 124 125 126 127 128 129 130
/**
 * 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)
{
131 132 133 134 135 136 137
    xenUnifiedPrivatePtr priv;

    if (conn == NULL)
        return NULL;

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL || path == NULL || nb == NULL)
138 139
        return (NULL);

140
    return xs_directory (priv->xshandle, 0, path, nb);
141
}
142
#endif /* ! PROXY */
143 144 145

/**
 * virDomainDoStoreQuery:
146 147
 * @conn: pointer to the hypervisor connection
 * @domid: id of the domain
148 149 150 151 152 153 154
 * @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 *
155
virDomainDoStoreQuery(virConnectPtr conn, int domid, const char *path)
156 157 158
{
    char s[256];
    unsigned int len = 0;
159 160 161 162
    xenUnifiedPrivatePtr priv;

    if (!conn)
        return NULL;
163

164 165
    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
166 167
        return (NULL);

168
    snprintf(s, 255, "/local/domain/%d/%s", domid, path);
169 170
    s[255] = 0;

171
    return xs_read(priv->xshandle, 0, &s[0], &len);
172 173
}

174
#ifndef PROXY
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/**
 * 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];
190
    xenUnifiedPrivatePtr priv;
191 192 193 194
    int ret = -1;

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (-1);
195 196 197

    priv = (xenUnifiedPrivatePtr) domain->conn->privateData;
    if (priv->xshandle == NULL)
198 199 200 201
        return (-1);
    if (domain->conn->flags & VIR_CONNECT_RO)
        return (-1);

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

205
    if (xs_write(priv->xshandle, 0, &s[0], value, strlen(value)))
206 207 208 209 210 211 212 213 214 215 216 217 218
        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
 */
219
static char *
220 221 222 223 224
virDomainGetVM(virDomainPtr domain)
{
    char *vm;
    char query[200];
    unsigned int len;
225
    xenUnifiedPrivatePtr priv;
226 227 228

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (NULL);
229 230 231

    priv = (xenUnifiedPrivatePtr) domain->conn->privateData;
    if (priv->xshandle == NULL)
232 233 234 235 236
        return (NULL);

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

237
    vm = xs_read(priv->xshandle, 0, &query[0], &len);
238 239 240 241 242 243 244 245 246 247

    return (vm);
}

/**
 * virDomainGetVMInfo:
 * @domain: a domain object
 * @vm: the xenstore vm path
 * @name: the value's path
 *
248
 * Internal API extracting one information the device used
249 250 251 252
 * by the domain from xensttore
 *
 * Returns the new string or NULL in case of error
 */
253
static char *
254 255 256 257 258
virDomainGetVMInfo(virDomainPtr domain, const char *vm, const char *name)
{
    char s[256];
    char *ret = NULL;
    unsigned int len = 0;
259
    xenUnifiedPrivatePtr priv;
260 261 262

    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (NULL);
263 264 265

    priv = (xenUnifiedPrivatePtr) domain->conn->privateData;
    if (priv->xshandle == NULL)
266 267 268 269 270
        return (NULL);

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

271
    ret = xs_read(priv->xshandle, 0, &s[0], &len);
272 273 274 275

    return (ret);
}

276
#endif /* ! PROXY */
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

/************************************************************************
 *									*
 *		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
294
xenStoreOpen(virConnectPtr conn,
295
             xmlURIPtr uri ATTRIBUTE_UNUSED,
296
             virConnectAuthPtr auth ATTRIBUTE_UNUSED,
297
             int flags ATTRIBUTE_UNUSED)
298
{
299
    xenUnifiedPrivatePtr priv = (xenUnifiedPrivatePtr) conn->privateData;
300

301
#ifdef PROXY
302
    priv->xshandle = xs_daemon_open_readonly();
303
#else
304
    if (flags & VIR_CONNECT_RO)
305
        priv->xshandle = xs_daemon_open_readonly();
306
    else
307
        priv->xshandle = xs_daemon_open();
308
#endif /* ! PROXY */
309

310
    if (priv->xshandle == NULL) {
311
        /*
312 313 314
         * not being able to connect via the socket as a normal user
         * is rather normal, this should fallback to the proxy (or
         * remote) mechanism.
315
         */
316
        if (getuid() == 0) {
317 318 319
            virXenStoreError(NULL, VIR_ERR_NO_XEN,
                                 _("failed to connect to Xen Store"));
        }
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
        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)
{
336 337
    xenUnifiedPrivatePtr priv;

338 339
    if (conn == NULL) {
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
340
        return(-1);
341 342
    }

343 344 345 346 347
    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
        return(-1);

    xs_daemon_close(priv->xshandle);
348 349 350
    return (0);
}

351
#ifndef PROXY
352 353 354
/**
 * xenStoreGetDomainInfo:
 * @domain: pointer to the domain block
355
 * @info: the place where information should be stored
356
 *
357
 * Do an hypervisor call to get the related set of domain information.
358 359 360 361 362 363 364 365 366
 *
 * 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];
367
    xenUnifiedPrivatePtr priv;
368

369 370 371
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (-1);

372 373
    if ((domain == NULL) || (domain->conn == NULL) || (info == NULL)) {
        virXenStoreError(domain ? domain->conn : NULL, VIR_ERR_INVALID_ARG,
374 375
                         __FUNCTION__);
        return(-1);
376
    }
377 378 379

    priv = (xenUnifiedPrivatePtr) domain->conn->privateData;
    if (priv->xshandle == NULL)
380
        return(-1);
381

382
    if (domain->id == -1)
383
        return(-1);
384

385
    tmp = virDomainDoStoreQuery(domain->conn, domain->id, "running");
386 387 388 389 390
    if (tmp != NULL) {
        if (tmp[0] == '1')
            info->state = VIR_DOMAIN_RUNNING;
        free(tmp);
    } else {
391
        info->state = VIR_DOMAIN_NOSTATE;
392
    }
393
    tmp = virDomainDoStoreQuery(domain->conn, domain->id, "memory/target");
394 395 396 397 398 399 400 401 402 403
    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 */
404
    tmp = virDomainDoStoreQuery(domain->conn, domain->id, "cpu_time");
405 406 407 408 409 410 411
    if (tmp != NULL) {
        info->cpuTime = atol(tmp);
        free(tmp);
    } else {
        info->cpuTime = 0;
    }
#endif
412
    snprintf(request, 199, "/local/domain/%d/cpu", domain->id);
413 414 415 416 417 418 419 420 421 422
    request[199] = 0;
    tmp2 = virConnectDoStoreList(domain->conn, request, &nb_vcpus);
    if (tmp2 != NULL) {
        info->nrVirtCpu = nb_vcpus;
        free(tmp2);
    }
    return (0);
}

/**
423
 * xenStoreDomainSetMemory:
424 425 426 427 428 429 430 431
 * @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
432
xenStoreDomainSetMemory(virDomainPtr domain, unsigned long memory)
433 434 435 436
{
    int ret;
    char value[20];

D
Daniel Veillard 已提交
437 438
    if ((domain == NULL) || (domain->conn == NULL) ||
        (memory < 1024 * MIN_XEN_GUEST_SIZE)) {
439
        virXenStoreError(domain ? domain->conn : NULL, VIR_ERR_INVALID_ARG,
440 441
                         __FUNCTION__);
        return(-1);
442
    }
443
    if (domain->id == -1)
444
        return(-1);
D
Daniel Veillard 已提交
445
    if ((domain->id == 0) && (memory < (2 * MIN_XEN_GUEST_SIZE * 1024)))
446
        return(-1);
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    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;

469 470
    if (!VIR_IS_CONNECTED_DOMAIN(domain))
        return (ret);
471
    if (domain->id == -1)
472
        return(-1);
473

474
    tmp = virDomainDoStoreQuery(domain->conn, domain->id, "memory/target");
475
    if (tmp != NULL) {
476 477
        ret = (unsigned long) atol(tmp);
        free(tmp);
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    }
    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;
496
    xenUnifiedPrivatePtr priv;
497

498
    if (conn == NULL) {
499
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
500
        return -1;
501
    }
502 503 504 505 506 507 508

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL) {
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
        return(-1);
    }
    idlist = xs_directory(priv->xshandle, 0, "/local/domain", &num);
509 510
    if (idlist) {
        free(idlist);
511
        ret = num;
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
    }
    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;
533
    xenUnifiedPrivatePtr priv;
534 535 536

    if ((conn == NULL) || (ids == NULL)) {
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
537
        return(-1);
538
    }
539 540 541

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
542 543
        return(-1);

544
    idlist = xs_directory (priv->xshandle, 0, "/local/domain", &num);
545
    if (idlist == NULL)
546
        return(-1);
547 548

    for (ret = 0, i = 0; (i < num) && (ret < maxids); i++) {
549 550 551 552 553
        id = strtol(idlist[i], &endptr, 10);
        if ((endptr == idlist[i]) || (*endptr != 0)) {
            ret = -1;
            break;
        }
554
#if 0
555 556
        if (virConnectCheckStoreID(conn, (int) id) < 0)
            continue;
557
#endif
558
        ids[ret++] = (int) id;
559
    }
560
    free(idlist);
561 562 563 564
    return(ret);
}

/**
565
 * xenStoreLookupByName:
566 567 568 569 570 571 572 573
 * @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
574
xenStoreLookupByName(virConnectPtr conn, const char *name)
575 576 577 578 579
{
    virDomainPtr ret = NULL;
    unsigned int num, i, len;
    long id = -1;
    char **idlist = NULL, *endptr;
580
    char prop[200], *tmp;
581 582
    int found = 0;
    struct xend_domain *xenddomain = NULL;
583
    xenUnifiedPrivatePtr priv;
584 585 586

    if ((conn == NULL) || (name == NULL)) {
        virXenStoreError(conn, VIR_ERR_INVALID_ARG, __FUNCTION__);
587
        return(NULL);
588
    }
589 590 591

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
592 593
        return(NULL);

594
    idlist = xs_directory(priv->xshandle, 0, "/local/domain", &num);
595
    if (idlist == NULL)
596
        goto done;
597 598

    for (i = 0; i < num; i++) {
599 600 601 602
        id = strtol(idlist[i], &endptr, 10);
        if ((endptr == idlist[i]) || (*endptr != 0)) {
            goto done;
        }
603
#if 0
604 605
        if (virConnectCheckStoreID(conn, (int) id) < 0)
            continue;
606
#endif
607 608 609 610 611 612 613 614 615
        snprintf(prop, 199, "/local/domain/%s/name", idlist[i]);
        prop[199] = 0;
        tmp = xs_read(priv->xshandle, 0, prop, &len);
        if (tmp != NULL) {
            found = STREQ (name, tmp);
            free(tmp);
            if (found)
                break;
        }
616 617
    }
    if (!found)
618
        goto done;
619

620
    ret = virGetDomain(conn, name, NULL);
621
    if (ret == NULL)
622
        goto done;
623

624
    ret->id = id;
625 626

done:
627 628
        free(xenddomain);
        free(idlist);
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647

    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,
648
                         __FUNCTION__);
649 650
        return(-1);
    }
651
    if (domain->id == -1 || domain->id == 0)
652
        return(-1);
653
    /*
654
     * this is very hackish, the domU kernel probes for a special
655 656
     * node in the xenstore and launch the shutdown command if found.
     */
657
    return(virDomainDoStoreWrite(domain, "control/shutdown", "poweroff"));
658 659
}

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
/**
 * 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,
676
                         __FUNCTION__);
677 678
        return(-1);
    }
679
    if (domain->id == -1 || domain->id == 0)
680
        return(-1);
681
    /*
682
     * this is very hackish, the domU kernel probes for a special
683 684 685 686
     * node in the xenstore and launch the shutdown command if found.
     */
    return(virDomainDoStoreWrite(domain, "control/shutdown", "reboot"));
}
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

/*
 * 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,
703
                         __FUNCTION__);
704 705 706 707 708 709 710 711 712 713 714
        return(NULL);
    }

    vm = virDomainGetVM(domain);
    if (vm) {
        str = virDomainGetVMInfo(domain, vm, "image/ostype");
        free(vm);
    }

    return (str);
}
715
#endif /* ! PROXY */
716

717 718
/**
 * xenStoreDomainGetVNCPort:
719 720
 * @conn: the hypervisor connection
 * @domid: id of the domain
721 722
 *
 * Return the port number on which the domain is listening for VNC
723
 * connections.
724 725 726
 *
 * Returns the port number, -1 in case of error
 */
727
int             xenStoreDomainGetVNCPort(virConnectPtr conn, int domid) {
728 729 730
    char *tmp;
    int ret = -1;

731
    tmp = virDomainDoStoreQuery(conn, domid, "console/vnc-port");
732 733 734 735 736 737 738 739 740 741 742 743
    if (tmp != NULL) {
        char *end;
        ret = strtol(tmp, &end, 10);
        if (ret == 0 && end == tmp)
            ret = -1;
        free(tmp);
    }
    return(ret);
}

/**
 * xenStoreDomainGetConsolePath:
744 745
 * @conn: the hypervisor connection
 * @domid: id of the domain
746 747 748 749 750 751 752 753
 *
 * 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
 */
754 755
char *          xenStoreDomainGetConsolePath(virConnectPtr conn, int domid) {
  return virDomainDoStoreQuery(conn, domid, "console/tty");
756
}
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

#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;
774
    xenUnifiedPrivatePtr priv;
775 776 777 778

    if (id < 0)
        return(NULL);

779 780
    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
781 782 783 784 785
        return (NULL);

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

786
    vm = xs_read(priv->xshandle, 0, &query[0], &len);
787 788 789

    if (vm) {
        snprintf(query, 199, "%s/image/ostype", vm);
790
        str = xs_read(priv->xshandle, 0, &query[0], &len);
791 792
        free(vm);
    }
793 794 795
    if (str == NULL)
        str = strdup("linux");

796 797 798 799

    return (str);
}
#endif /* PROXY */
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

/*
 * 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;
818
    xenUnifiedPrivatePtr priv;
819 820 821

    if (id < 0)
        return(NULL);
822 823 824

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
825 826 827 828 829 830 831
        return (NULL);
    if (mac == NULL)
        return (NULL);
    maclen = strlen(mac);
    if (maclen <= 0)
        return (NULL);

832
    snprintf(dir, sizeof(dir), "/local/domain/0/backend/vif/%d", id);
833
    list = xs_directory(priv->xshandle, 0, dir, &num);
834
    if (list == NULL)
835
        return(NULL);
836
    for (i = 0; i < num; i++) {
837 838 839 840 841 842 843 844 845 846 847
        snprintf(path, sizeof(path), "%s/%s/%s", dir, list[i], "mac");
        val = xs_read(priv->xshandle, 0, path, &len);
        if (val == NULL)
            break;
        if ((maclen != len) || memcmp(val, mac, len)) {
            free(val);
        } else {
            ret = strdup(list[i]);
            free(val);
            break;
        }
848 849 850 851
    }
    free(list);
    return(ret);
}
852

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
/*
 * xenStoreDomainGetDiskID:
 * @conn: pointer to the connection.
 * @id: the domain id
 * @dev: the virtual block device name
 *
 * Get the reference (i.e. the string number) for the device on that domain
 * which uses the given virtual block device name
 *
 * Returns the new string or NULL in case of error, the string must be
 *         freed by the caller.
 */
char *
xenStoreDomainGetDiskID(virConnectPtr conn, int id, const char *dev) {
    char dir[80], path[128], **list = NULL, *val = NULL;
    unsigned int devlen, len, i, num;
    char *ret = NULL;
    xenUnifiedPrivatePtr priv;

    if (id < 0)
        return(NULL);

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
        return (NULL);
    if (dev == NULL)
        return (NULL);
    devlen = strlen(dev);
    if (devlen <= 0)
        return (NULL);

    snprintf(dir, sizeof(dir), "/local/domain/0/backend/vbd/%d", id);
    list = xs_directory(priv->xshandle, 0, dir, &num);
886 887 888 889 890 891 892 893 894 895 896 897 898 899
    if (list != NULL) {
        for (i = 0; i < num; i++) {
            snprintf(path, sizeof(path), "%s/%s/%s", dir, list[i], "dev");
            val = xs_read(priv->xshandle, 0, path, &len);
            if (val == NULL)
                break;
            if ((devlen != len) || memcmp(val, dev, len)) {
                free (val);
            } else {
                ret = strdup(list[i]);
                free (val);
                free (list);
                return (ret);
            }
900
        }
901
        free (list);
902
    }
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
    snprintf(dir, sizeof(dir), "/local/domain/0/backend/tap/%d", id);
    list = xs_directory(priv->xshandle, 0, dir, &num);
    if (list != NULL) {
        for (i = 0; i < num; i++) {
            snprintf(path, sizeof(path), "%s/%s/%s", dir, list[i], "dev");
            val = xs_read(priv->xshandle, 0, path, &len);
            if (val == NULL)
                break;
            if ((devlen != len) || memcmp(val, dev, len)) {
                free (val);
            } else {
                ret = strdup(list[i]);
                free (val);
                free (list);
                return (ret);
            }
        }
        free (list);
    }
    return (NULL);
923 924
}

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
char *xenStoreDomainGetName(virConnectPtr conn,
                            int id) {
    char prop[200];
    xenUnifiedPrivatePtr priv;
    unsigned int len;

    priv = (xenUnifiedPrivatePtr) conn->privateData;
    if (priv->xshandle == NULL)
        return(NULL);

    snprintf(prop, 199, "/local/domain/%d/name", id);
    prop[199] = 0;
    return xs_read(priv->xshandle, 0, prop, &len);
}